Hans de Goede | 4faba76 | 2012-06-23 04:39:58 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Linux V4L2 radio driver for the Griffin radioSHARK2 USB radio receiver |
| 3 | * |
| 4 | * Note the radioSHARK2 offers the audio through a regular USB audio device, |
| 5 | * this driver only handles the tuning. |
| 6 | * |
| 7 | * The info necessary to drive the shark2 was taken from the small userspace |
| 8 | * shark2.c program by Hisaaki Shibata, which he kindly placed in the Public |
| 9 | * Domain. |
| 10 | * |
| 11 | * Copyright (c) 2012 Hans de Goede <hdegoede@redhat.com> |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License |
| 24 | * along with this program; if not, write to the Free Software |
| 25 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 26 | */ |
| 27 | |
| 28 | #include <linux/init.h> |
| 29 | #include <linux/kernel.h> |
| 30 | #include <linux/leds.h> |
| 31 | #include <linux/module.h> |
| 32 | #include <linux/slab.h> |
| 33 | #include <linux/usb.h> |
| 34 | #include <linux/workqueue.h> |
| 35 | #include <media/v4l2-device.h> |
| 36 | #include "radio-tea5777.h" |
| 37 | |
| 38 | MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>"); |
| 39 | MODULE_DESCRIPTION("Griffin radioSHARK2, USB radio receiver driver"); |
| 40 | MODULE_LICENSE("GPL"); |
| 41 | |
| 42 | static int debug; |
| 43 | module_param(debug, int, 0); |
| 44 | MODULE_PARM_DESC(debug, "Debug level (0-1)"); |
| 45 | |
| 46 | |
| 47 | #define SHARK_IN_EP 0x83 |
| 48 | #define SHARK_OUT_EP 0x05 |
| 49 | |
| 50 | #define TB_LEN 7 |
| 51 | #define DRV_NAME "radioshark2" |
| 52 | |
| 53 | #define v4l2_dev_to_shark(d) container_of(d, struct shark_device, v4l2_dev) |
| 54 | |
| 55 | enum { BLUE_LED, RED_LED, NO_LEDS }; |
| 56 | |
| 57 | static void shark_led_set_blue(struct led_classdev *led_cdev, |
| 58 | enum led_brightness value); |
| 59 | static void shark_led_set_red(struct led_classdev *led_cdev, |
| 60 | enum led_brightness value); |
| 61 | |
| 62 | static const struct led_classdev shark_led_templates[NO_LEDS] = { |
| 63 | [BLUE_LED] = { |
| 64 | .name = "%s:blue:", |
| 65 | .brightness = LED_OFF, |
| 66 | .max_brightness = 127, |
| 67 | .brightness_set = shark_led_set_blue, |
| 68 | }, |
| 69 | [RED_LED] = { |
| 70 | .name = "%s:red:", |
| 71 | .brightness = LED_OFF, |
| 72 | .max_brightness = 1, |
| 73 | .brightness_set = shark_led_set_red, |
| 74 | }, |
| 75 | }; |
| 76 | |
| 77 | struct shark_device { |
| 78 | struct usb_device *usbdev; |
| 79 | struct v4l2_device v4l2_dev; |
| 80 | struct radio_tea5777 tea; |
| 81 | |
| 82 | struct work_struct led_work; |
| 83 | struct led_classdev leds[NO_LEDS]; |
| 84 | char led_names[NO_LEDS][32]; |
| 85 | atomic_t brightness[NO_LEDS]; |
| 86 | unsigned long brightness_new; |
| 87 | |
| 88 | u8 *transfer_buffer; |
| 89 | }; |
| 90 | |
| 91 | static atomic_t shark_instance = ATOMIC_INIT(0); |
| 92 | |
| 93 | static int shark_write_reg(struct radio_tea5777 *tea, u64 reg) |
| 94 | { |
| 95 | struct shark_device *shark = tea->private_data; |
| 96 | int i, res, actual_len; |
| 97 | |
| 98 | memset(shark->transfer_buffer, 0, TB_LEN); |
| 99 | shark->transfer_buffer[0] = 0x81; /* Write register command */ |
| 100 | for (i = 0; i < 6; i++) |
| 101 | shark->transfer_buffer[i + 1] = (reg >> (40 - i * 8)) & 0xff; |
| 102 | |
Andy Shevchenko | 9697b54 | 2012-08-07 12:43:06 -0300 | [diff] [blame^] | 103 | v4l2_dbg(1, debug, tea->v4l2_dev, "shark2-write: %*ph\n", |
| 104 | 7, shark->transfer_buffer); |
Hans de Goede | 4faba76 | 2012-06-23 04:39:58 -0300 | [diff] [blame] | 105 | |
| 106 | res = usb_interrupt_msg(shark->usbdev, |
| 107 | usb_sndintpipe(shark->usbdev, SHARK_OUT_EP), |
| 108 | shark->transfer_buffer, TB_LEN, |
| 109 | &actual_len, 1000); |
| 110 | if (res < 0) { |
| 111 | v4l2_err(tea->v4l2_dev, "write error: %d\n", res); |
| 112 | return res; |
| 113 | } |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | static int shark_read_reg(struct radio_tea5777 *tea, u32 *reg_ret) |
| 119 | { |
| 120 | struct shark_device *shark = tea->private_data; |
| 121 | int i, res, actual_len; |
| 122 | u32 reg = 0; |
| 123 | |
| 124 | memset(shark->transfer_buffer, 0, TB_LEN); |
| 125 | shark->transfer_buffer[0] = 0x82; |
| 126 | res = usb_interrupt_msg(shark->usbdev, |
| 127 | usb_sndintpipe(shark->usbdev, SHARK_OUT_EP), |
| 128 | shark->transfer_buffer, TB_LEN, |
| 129 | &actual_len, 1000); |
| 130 | if (res < 0) { |
| 131 | v4l2_err(tea->v4l2_dev, "request-read error: %d\n", res); |
| 132 | return res; |
| 133 | } |
| 134 | |
| 135 | res = usb_interrupt_msg(shark->usbdev, |
| 136 | usb_rcvintpipe(shark->usbdev, SHARK_IN_EP), |
| 137 | shark->transfer_buffer, TB_LEN, |
| 138 | &actual_len, 1000); |
| 139 | if (res < 0) { |
| 140 | v4l2_err(tea->v4l2_dev, "read error: %d\n", res); |
| 141 | return res; |
| 142 | } |
| 143 | |
| 144 | for (i = 0; i < 3; i++) |
| 145 | reg |= shark->transfer_buffer[i] << (16 - i * 8); |
| 146 | |
Andy Shevchenko | 9697b54 | 2012-08-07 12:43:06 -0300 | [diff] [blame^] | 147 | v4l2_dbg(1, debug, tea->v4l2_dev, "shark2-read: %*ph\n", |
| 148 | 3, shark->transfer_buffer); |
Hans de Goede | 4faba76 | 2012-06-23 04:39:58 -0300 | [diff] [blame] | 149 | |
| 150 | *reg_ret = reg; |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | static struct radio_tea5777_ops shark_tea_ops = { |
| 155 | .write_reg = shark_write_reg, |
| 156 | .read_reg = shark_read_reg, |
| 157 | }; |
| 158 | |
| 159 | static void shark_led_work(struct work_struct *work) |
| 160 | { |
| 161 | struct shark_device *shark = |
| 162 | container_of(work, struct shark_device, led_work); |
| 163 | int i, res, brightness, actual_len; |
| 164 | /* |
| 165 | * We use the v4l2_dev lock and registered bit to ensure the device |
| 166 | * does not get unplugged and unreffed while we're running. |
| 167 | */ |
| 168 | mutex_lock(&shark->tea.mutex); |
| 169 | if (!video_is_registered(&shark->tea.vd)) |
| 170 | goto leave; |
| 171 | |
| 172 | for (i = 0; i < 2; i++) { |
| 173 | if (!test_and_clear_bit(i, &shark->brightness_new)) |
| 174 | continue; |
| 175 | |
| 176 | brightness = atomic_read(&shark->brightness[i]); |
| 177 | memset(shark->transfer_buffer, 0, TB_LEN); |
| 178 | shark->transfer_buffer[0] = 0x83 + i; |
| 179 | shark->transfer_buffer[1] = brightness; |
| 180 | res = usb_interrupt_msg(shark->usbdev, |
| 181 | usb_sndintpipe(shark->usbdev, |
| 182 | SHARK_OUT_EP), |
| 183 | shark->transfer_buffer, TB_LEN, |
| 184 | &actual_len, 1000); |
| 185 | if (res < 0) |
| 186 | v4l2_err(&shark->v4l2_dev, "set LED %s error: %d\n", |
| 187 | shark->led_names[i], res); |
| 188 | } |
| 189 | leave: |
| 190 | mutex_unlock(&shark->tea.mutex); |
| 191 | } |
| 192 | |
| 193 | static void shark_led_set_blue(struct led_classdev *led_cdev, |
| 194 | enum led_brightness value) |
| 195 | { |
| 196 | struct shark_device *shark = |
| 197 | container_of(led_cdev, struct shark_device, leds[BLUE_LED]); |
| 198 | |
| 199 | atomic_set(&shark->brightness[BLUE_LED], value); |
| 200 | set_bit(BLUE_LED, &shark->brightness_new); |
| 201 | schedule_work(&shark->led_work); |
| 202 | } |
| 203 | |
| 204 | static void shark_led_set_red(struct led_classdev *led_cdev, |
| 205 | enum led_brightness value) |
| 206 | { |
| 207 | struct shark_device *shark = |
| 208 | container_of(led_cdev, struct shark_device, leds[RED_LED]); |
| 209 | |
| 210 | atomic_set(&shark->brightness[RED_LED], value); |
| 211 | set_bit(RED_LED, &shark->brightness_new); |
| 212 | schedule_work(&shark->led_work); |
| 213 | } |
| 214 | |
| 215 | static void usb_shark_disconnect(struct usb_interface *intf) |
| 216 | { |
| 217 | struct v4l2_device *v4l2_dev = usb_get_intfdata(intf); |
| 218 | struct shark_device *shark = v4l2_dev_to_shark(v4l2_dev); |
| 219 | int i; |
| 220 | |
| 221 | mutex_lock(&shark->tea.mutex); |
| 222 | v4l2_device_disconnect(&shark->v4l2_dev); |
| 223 | radio_tea5777_exit(&shark->tea); |
| 224 | mutex_unlock(&shark->tea.mutex); |
| 225 | |
| 226 | for (i = 0; i < NO_LEDS; i++) |
| 227 | led_classdev_unregister(&shark->leds[i]); |
| 228 | |
| 229 | v4l2_device_put(&shark->v4l2_dev); |
| 230 | } |
| 231 | |
| 232 | static void usb_shark_release(struct v4l2_device *v4l2_dev) |
| 233 | { |
| 234 | struct shark_device *shark = v4l2_dev_to_shark(v4l2_dev); |
| 235 | |
| 236 | cancel_work_sync(&shark->led_work); |
| 237 | v4l2_device_unregister(&shark->v4l2_dev); |
| 238 | kfree(shark->transfer_buffer); |
| 239 | kfree(shark); |
| 240 | } |
| 241 | |
| 242 | static int usb_shark_probe(struct usb_interface *intf, |
| 243 | const struct usb_device_id *id) |
| 244 | { |
| 245 | struct shark_device *shark; |
| 246 | int i, retval = -ENOMEM; |
| 247 | |
| 248 | shark = kzalloc(sizeof(struct shark_device), GFP_KERNEL); |
| 249 | if (!shark) |
| 250 | return retval; |
| 251 | |
| 252 | shark->transfer_buffer = kmalloc(TB_LEN, GFP_KERNEL); |
| 253 | if (!shark->transfer_buffer) |
| 254 | goto err_alloc_buffer; |
| 255 | |
| 256 | /* |
| 257 | * Work around a bug in usbhid/hid-core.c, where it leaves a dangling |
| 258 | * pointer in intfdata causing v4l2-device.c to not set it. Which |
| 259 | * results in usb_shark_disconnect() referencing the dangling pointer |
| 260 | * |
| 261 | * REMOVE (as soon as the above bug is fixed, patch submitted) |
| 262 | */ |
| 263 | usb_set_intfdata(intf, NULL); |
| 264 | |
| 265 | shark->v4l2_dev.release = usb_shark_release; |
| 266 | v4l2_device_set_name(&shark->v4l2_dev, DRV_NAME, &shark_instance); |
| 267 | retval = v4l2_device_register(&intf->dev, &shark->v4l2_dev); |
| 268 | if (retval) { |
| 269 | v4l2_err(&shark->v4l2_dev, "couldn't register v4l2_device\n"); |
| 270 | goto err_reg_dev; |
| 271 | } |
| 272 | |
| 273 | shark->usbdev = interface_to_usbdev(intf); |
| 274 | shark->tea.v4l2_dev = &shark->v4l2_dev; |
| 275 | shark->tea.private_data = shark; |
| 276 | shark->tea.ops = &shark_tea_ops; |
| 277 | shark->tea.has_am = true; |
| 278 | shark->tea.write_before_read = true; |
| 279 | strlcpy(shark->tea.card, "Griffin radioSHARK2", |
| 280 | sizeof(shark->tea.card)); |
| 281 | usb_make_path(shark->usbdev, shark->tea.bus_info, |
| 282 | sizeof(shark->tea.bus_info)); |
| 283 | |
| 284 | retval = radio_tea5777_init(&shark->tea, THIS_MODULE); |
| 285 | if (retval) { |
| 286 | v4l2_err(&shark->v4l2_dev, "couldn't init tea5777\n"); |
| 287 | goto err_init_tea; |
| 288 | } |
| 289 | |
| 290 | INIT_WORK(&shark->led_work, shark_led_work); |
| 291 | for (i = 0; i < NO_LEDS; i++) { |
| 292 | shark->leds[i] = shark_led_templates[i]; |
| 293 | snprintf(shark->led_names[i], sizeof(shark->led_names[0]), |
| 294 | shark->leds[i].name, shark->v4l2_dev.name); |
| 295 | shark->leds[i].name = shark->led_names[i]; |
| 296 | /* |
| 297 | * We don't fail the probe if we fail to register the leds, |
| 298 | * because once we've called radio_tea5777_init, the /dev/radio0 |
| 299 | * node may be opened from userspace holding a reference to us! |
| 300 | * |
| 301 | * Note we cannot register the leds first instead as |
| 302 | * shark_led_work depends on the v4l2 mutex and registered bit. |
| 303 | */ |
| 304 | retval = led_classdev_register(&intf->dev, &shark->leds[i]); |
| 305 | if (retval) |
| 306 | v4l2_err(&shark->v4l2_dev, |
| 307 | "couldn't register led: %s\n", |
| 308 | shark->led_names[i]); |
| 309 | } |
| 310 | |
| 311 | return 0; |
| 312 | |
| 313 | err_init_tea: |
| 314 | v4l2_device_unregister(&shark->v4l2_dev); |
| 315 | err_reg_dev: |
| 316 | kfree(shark->transfer_buffer); |
| 317 | err_alloc_buffer: |
| 318 | kfree(shark); |
| 319 | |
| 320 | return retval; |
| 321 | } |
| 322 | |
| 323 | /* Specify the bcdDevice value, as the radioSHARK and radioSHARK2 share ids */ |
| 324 | static struct usb_device_id usb_shark_device_table[] = { |
| 325 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION | |
| 326 | USB_DEVICE_ID_MATCH_INT_CLASS, |
| 327 | .idVendor = 0x077d, |
| 328 | .idProduct = 0x627a, |
| 329 | .bcdDevice_lo = 0x0010, |
| 330 | .bcdDevice_hi = 0x0010, |
| 331 | .bInterfaceClass = 3, |
| 332 | }, |
| 333 | { } |
| 334 | }; |
| 335 | MODULE_DEVICE_TABLE(usb, usb_shark_device_table); |
| 336 | |
| 337 | static struct usb_driver usb_shark_driver = { |
| 338 | .name = DRV_NAME, |
| 339 | .probe = usb_shark_probe, |
| 340 | .disconnect = usb_shark_disconnect, |
| 341 | .id_table = usb_shark_device_table, |
| 342 | }; |
| 343 | module_usb_driver(usb_shark_driver); |