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 | |
Hans de Goede | be0c44f | 2012-08-11 06:34:55 -0300 | [diff] [blame] | 38 | #if defined(CONFIG_LEDS_CLASS) || \ |
| 39 | (defined(CONFIG_LEDS_CLASS_MODULE) && defined(CONFIG_RADIO_SHARK2_MODULE)) |
| 40 | #define SHARK_USE_LEDS 1 |
| 41 | #endif |
| 42 | |
Hans de Goede | 4faba76 | 2012-06-23 04:39:58 -0300 | [diff] [blame] | 43 | MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>"); |
| 44 | MODULE_DESCRIPTION("Griffin radioSHARK2, USB radio receiver driver"); |
| 45 | MODULE_LICENSE("GPL"); |
| 46 | |
| 47 | static int debug; |
| 48 | module_param(debug, int, 0); |
| 49 | MODULE_PARM_DESC(debug, "Debug level (0-1)"); |
| 50 | |
Hans de Goede | 4faba76 | 2012-06-23 04:39:58 -0300 | [diff] [blame] | 51 | #define SHARK_IN_EP 0x83 |
| 52 | #define SHARK_OUT_EP 0x05 |
| 53 | |
| 54 | #define TB_LEN 7 |
| 55 | #define DRV_NAME "radioshark2" |
| 56 | |
| 57 | #define v4l2_dev_to_shark(d) container_of(d, struct shark_device, v4l2_dev) |
| 58 | |
| 59 | enum { BLUE_LED, RED_LED, NO_LEDS }; |
| 60 | |
Hans de Goede | 4faba76 | 2012-06-23 04:39:58 -0300 | [diff] [blame] | 61 | struct shark_device { |
| 62 | struct usb_device *usbdev; |
| 63 | struct v4l2_device v4l2_dev; |
| 64 | struct radio_tea5777 tea; |
| 65 | |
Hans de Goede | be0c44f | 2012-08-11 06:34:55 -0300 | [diff] [blame] | 66 | #ifdef SHARK_USE_LEDS |
Hans de Goede | 4faba76 | 2012-06-23 04:39:58 -0300 | [diff] [blame] | 67 | struct work_struct led_work; |
| 68 | struct led_classdev leds[NO_LEDS]; |
| 69 | char led_names[NO_LEDS][32]; |
| 70 | atomic_t brightness[NO_LEDS]; |
| 71 | unsigned long brightness_new; |
Hans de Goede | be0c44f | 2012-08-11 06:34:55 -0300 | [diff] [blame] | 72 | #endif |
Hans de Goede | 4faba76 | 2012-06-23 04:39:58 -0300 | [diff] [blame] | 73 | |
| 74 | u8 *transfer_buffer; |
| 75 | }; |
| 76 | |
| 77 | static atomic_t shark_instance = ATOMIC_INIT(0); |
| 78 | |
| 79 | static int shark_write_reg(struct radio_tea5777 *tea, u64 reg) |
| 80 | { |
| 81 | struct shark_device *shark = tea->private_data; |
| 82 | int i, res, actual_len; |
| 83 | |
| 84 | memset(shark->transfer_buffer, 0, TB_LEN); |
| 85 | shark->transfer_buffer[0] = 0x81; /* Write register command */ |
| 86 | for (i = 0; i < 6; i++) |
| 87 | shark->transfer_buffer[i + 1] = (reg >> (40 - i * 8)) & 0xff; |
| 88 | |
Andy Shevchenko | 9697b54 | 2012-08-07 12:43:06 -0300 | [diff] [blame] | 89 | v4l2_dbg(1, debug, tea->v4l2_dev, "shark2-write: %*ph\n", |
| 90 | 7, shark->transfer_buffer); |
Hans de Goede | 4faba76 | 2012-06-23 04:39:58 -0300 | [diff] [blame] | 91 | |
| 92 | res = usb_interrupt_msg(shark->usbdev, |
| 93 | usb_sndintpipe(shark->usbdev, SHARK_OUT_EP), |
| 94 | shark->transfer_buffer, TB_LEN, |
| 95 | &actual_len, 1000); |
| 96 | if (res < 0) { |
| 97 | v4l2_err(tea->v4l2_dev, "write error: %d\n", res); |
| 98 | return res; |
| 99 | } |
| 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | static int shark_read_reg(struct radio_tea5777 *tea, u32 *reg_ret) |
| 105 | { |
| 106 | struct shark_device *shark = tea->private_data; |
| 107 | int i, res, actual_len; |
| 108 | u32 reg = 0; |
| 109 | |
| 110 | memset(shark->transfer_buffer, 0, TB_LEN); |
| 111 | shark->transfer_buffer[0] = 0x82; |
| 112 | res = usb_interrupt_msg(shark->usbdev, |
| 113 | usb_sndintpipe(shark->usbdev, SHARK_OUT_EP), |
| 114 | shark->transfer_buffer, TB_LEN, |
| 115 | &actual_len, 1000); |
| 116 | if (res < 0) { |
| 117 | v4l2_err(tea->v4l2_dev, "request-read error: %d\n", res); |
| 118 | return res; |
| 119 | } |
| 120 | |
| 121 | res = usb_interrupt_msg(shark->usbdev, |
| 122 | usb_rcvintpipe(shark->usbdev, SHARK_IN_EP), |
| 123 | shark->transfer_buffer, TB_LEN, |
| 124 | &actual_len, 1000); |
| 125 | if (res < 0) { |
| 126 | v4l2_err(tea->v4l2_dev, "read error: %d\n", res); |
| 127 | return res; |
| 128 | } |
| 129 | |
| 130 | for (i = 0; i < 3; i++) |
| 131 | reg |= shark->transfer_buffer[i] << (16 - i * 8); |
| 132 | |
Andy Shevchenko | 9697b54 | 2012-08-07 12:43:06 -0300 | [diff] [blame] | 133 | v4l2_dbg(1, debug, tea->v4l2_dev, "shark2-read: %*ph\n", |
| 134 | 3, shark->transfer_buffer); |
Hans de Goede | 4faba76 | 2012-06-23 04:39:58 -0300 | [diff] [blame] | 135 | |
| 136 | *reg_ret = reg; |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | static struct radio_tea5777_ops shark_tea_ops = { |
| 141 | .write_reg = shark_write_reg, |
| 142 | .read_reg = shark_read_reg, |
| 143 | }; |
| 144 | |
Hans de Goede | be0c44f | 2012-08-11 06:34:55 -0300 | [diff] [blame] | 145 | #ifdef SHARK_USE_LEDS |
Hans de Goede | 4faba76 | 2012-06-23 04:39:58 -0300 | [diff] [blame] | 146 | static void shark_led_work(struct work_struct *work) |
| 147 | { |
| 148 | struct shark_device *shark = |
| 149 | container_of(work, struct shark_device, led_work); |
| 150 | int i, res, brightness, actual_len; |
Hans de Goede | 4faba76 | 2012-06-23 04:39:58 -0300 | [diff] [blame] | 151 | |
| 152 | for (i = 0; i < 2; i++) { |
| 153 | if (!test_and_clear_bit(i, &shark->brightness_new)) |
| 154 | continue; |
| 155 | |
| 156 | brightness = atomic_read(&shark->brightness[i]); |
| 157 | memset(shark->transfer_buffer, 0, TB_LEN); |
| 158 | shark->transfer_buffer[0] = 0x83 + i; |
| 159 | shark->transfer_buffer[1] = brightness; |
| 160 | res = usb_interrupt_msg(shark->usbdev, |
| 161 | usb_sndintpipe(shark->usbdev, |
| 162 | SHARK_OUT_EP), |
| 163 | shark->transfer_buffer, TB_LEN, |
| 164 | &actual_len, 1000); |
| 165 | if (res < 0) |
| 166 | v4l2_err(&shark->v4l2_dev, "set LED %s error: %d\n", |
| 167 | shark->led_names[i], res); |
| 168 | } |
Hans de Goede | 4faba76 | 2012-06-23 04:39:58 -0300 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | static void shark_led_set_blue(struct led_classdev *led_cdev, |
| 172 | enum led_brightness value) |
| 173 | { |
| 174 | struct shark_device *shark = |
| 175 | container_of(led_cdev, struct shark_device, leds[BLUE_LED]); |
| 176 | |
| 177 | atomic_set(&shark->brightness[BLUE_LED], value); |
| 178 | set_bit(BLUE_LED, &shark->brightness_new); |
| 179 | schedule_work(&shark->led_work); |
| 180 | } |
| 181 | |
| 182 | static void shark_led_set_red(struct led_classdev *led_cdev, |
| 183 | enum led_brightness value) |
| 184 | { |
| 185 | struct shark_device *shark = |
| 186 | container_of(led_cdev, struct shark_device, leds[RED_LED]); |
| 187 | |
| 188 | atomic_set(&shark->brightness[RED_LED], value); |
| 189 | set_bit(RED_LED, &shark->brightness_new); |
| 190 | schedule_work(&shark->led_work); |
| 191 | } |
| 192 | |
Hans de Goede | be0c44f | 2012-08-11 06:34:55 -0300 | [diff] [blame] | 193 | static const struct led_classdev shark_led_templates[NO_LEDS] = { |
| 194 | [BLUE_LED] = { |
| 195 | .name = "%s:blue:", |
| 196 | .brightness = LED_OFF, |
| 197 | .max_brightness = 127, |
| 198 | .brightness_set = shark_led_set_blue, |
| 199 | }, |
| 200 | [RED_LED] = { |
| 201 | .name = "%s:red:", |
| 202 | .brightness = LED_OFF, |
| 203 | .max_brightness = 1, |
| 204 | .brightness_set = shark_led_set_red, |
| 205 | }, |
| 206 | }; |
| 207 | |
| 208 | static int shark_register_leds(struct shark_device *shark, struct device *dev) |
| 209 | { |
| 210 | int i, retval; |
| 211 | |
Hans de Goede | d1f280d | 2012-08-11 12:37:13 -0300 | [diff] [blame] | 212 | atomic_set(&shark->brightness[BLUE_LED], 127); |
Hans de Goede | be0c44f | 2012-08-11 06:34:55 -0300 | [diff] [blame] | 213 | INIT_WORK(&shark->led_work, shark_led_work); |
| 214 | for (i = 0; i < NO_LEDS; i++) { |
| 215 | shark->leds[i] = shark_led_templates[i]; |
| 216 | snprintf(shark->led_names[i], sizeof(shark->led_names[0]), |
| 217 | shark->leds[i].name, shark->v4l2_dev.name); |
| 218 | shark->leds[i].name = shark->led_names[i]; |
| 219 | retval = led_classdev_register(dev, &shark->leds[i]); |
| 220 | if (retval) { |
| 221 | v4l2_err(&shark->v4l2_dev, |
| 222 | "couldn't register led: %s\n", |
| 223 | shark->led_names[i]); |
| 224 | return retval; |
| 225 | } |
| 226 | } |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | static void shark_unregister_leds(struct shark_device *shark) |
| 231 | { |
| 232 | int i; |
| 233 | |
| 234 | for (i = 0; i < NO_LEDS; i++) |
| 235 | led_classdev_unregister(&shark->leds[i]); |
| 236 | |
| 237 | cancel_work_sync(&shark->led_work); |
| 238 | } |
Hans de Goede | d1f280d | 2012-08-11 12:37:13 -0300 | [diff] [blame] | 239 | |
Mauro Carvalho Chehab | c5b0b3c | 2013-11-01 12:44:54 -0300 | [diff] [blame] | 240 | #ifdef CONFIG_PM |
Hans de Goede | d1f280d | 2012-08-11 12:37:13 -0300 | [diff] [blame] | 241 | static void shark_resume_leds(struct shark_device *shark) |
| 242 | { |
| 243 | int i; |
| 244 | |
| 245 | for (i = 0; i < NO_LEDS; i++) |
| 246 | set_bit(i, &shark->brightness_new); |
| 247 | |
| 248 | schedule_work(&shark->led_work); |
| 249 | } |
Mauro Carvalho Chehab | c5b0b3c | 2013-11-01 12:44:54 -0300 | [diff] [blame] | 250 | #endif |
Hans de Goede | be0c44f | 2012-08-11 06:34:55 -0300 | [diff] [blame] | 251 | #else |
| 252 | static int shark_register_leds(struct shark_device *shark, struct device *dev) |
| 253 | { |
| 254 | v4l2_warn(&shark->v4l2_dev, |
Paul Bolle | 0bb9121 | 2013-03-26 09:45:38 +0100 | [diff] [blame] | 255 | "CONFIG_LEDS_CLASS not enabled, LED support disabled\n"); |
Hans de Goede | be0c44f | 2012-08-11 06:34:55 -0300 | [diff] [blame] | 256 | return 0; |
| 257 | } |
| 258 | static inline void shark_unregister_leds(struct shark_device *shark) { } |
Hans de Goede | d1f280d | 2012-08-11 12:37:13 -0300 | [diff] [blame] | 259 | static inline void shark_resume_leds(struct shark_device *shark) { } |
Hans de Goede | be0c44f | 2012-08-11 06:34:55 -0300 | [diff] [blame] | 260 | #endif |
| 261 | |
Hans de Goede | 4faba76 | 2012-06-23 04:39:58 -0300 | [diff] [blame] | 262 | static void usb_shark_disconnect(struct usb_interface *intf) |
| 263 | { |
| 264 | struct v4l2_device *v4l2_dev = usb_get_intfdata(intf); |
| 265 | struct shark_device *shark = v4l2_dev_to_shark(v4l2_dev); |
Hans de Goede | 4faba76 | 2012-06-23 04:39:58 -0300 | [diff] [blame] | 266 | |
| 267 | mutex_lock(&shark->tea.mutex); |
| 268 | v4l2_device_disconnect(&shark->v4l2_dev); |
| 269 | radio_tea5777_exit(&shark->tea); |
| 270 | mutex_unlock(&shark->tea.mutex); |
| 271 | |
Hans de Goede | be0c44f | 2012-08-11 06:34:55 -0300 | [diff] [blame] | 272 | shark_unregister_leds(shark); |
Hans de Goede | 4faba76 | 2012-06-23 04:39:58 -0300 | [diff] [blame] | 273 | |
| 274 | v4l2_device_put(&shark->v4l2_dev); |
| 275 | } |
| 276 | |
| 277 | static void usb_shark_release(struct v4l2_device *v4l2_dev) |
| 278 | { |
| 279 | struct shark_device *shark = v4l2_dev_to_shark(v4l2_dev); |
| 280 | |
Hans de Goede | 4faba76 | 2012-06-23 04:39:58 -0300 | [diff] [blame] | 281 | v4l2_device_unregister(&shark->v4l2_dev); |
| 282 | kfree(shark->transfer_buffer); |
| 283 | kfree(shark); |
| 284 | } |
| 285 | |
| 286 | static int usb_shark_probe(struct usb_interface *intf, |
| 287 | const struct usb_device_id *id) |
| 288 | { |
| 289 | struct shark_device *shark; |
Hans de Goede | be0c44f | 2012-08-11 06:34:55 -0300 | [diff] [blame] | 290 | int retval = -ENOMEM; |
Hans de Goede | 4faba76 | 2012-06-23 04:39:58 -0300 | [diff] [blame] | 291 | |
| 292 | shark = kzalloc(sizeof(struct shark_device), GFP_KERNEL); |
| 293 | if (!shark) |
| 294 | return retval; |
| 295 | |
| 296 | shark->transfer_buffer = kmalloc(TB_LEN, GFP_KERNEL); |
| 297 | if (!shark->transfer_buffer) |
| 298 | goto err_alloc_buffer; |
| 299 | |
Hans de Goede | 4faba76 | 2012-06-23 04:39:58 -0300 | [diff] [blame] | 300 | v4l2_device_set_name(&shark->v4l2_dev, DRV_NAME, &shark_instance); |
Hans de Goede | be0c44f | 2012-08-11 06:34:55 -0300 | [diff] [blame] | 301 | |
| 302 | retval = shark_register_leds(shark, &intf->dev); |
| 303 | if (retval) |
| 304 | goto err_reg_leds; |
Hans de Goede | 4faba76 | 2012-06-23 04:39:58 -0300 | [diff] [blame] | 305 | |
| 306 | shark->v4l2_dev.release = usb_shark_release; |
Hans de Goede | 4faba76 | 2012-06-23 04:39:58 -0300 | [diff] [blame] | 307 | retval = v4l2_device_register(&intf->dev, &shark->v4l2_dev); |
| 308 | if (retval) { |
| 309 | v4l2_err(&shark->v4l2_dev, "couldn't register v4l2_device\n"); |
| 310 | goto err_reg_dev; |
| 311 | } |
| 312 | |
| 313 | shark->usbdev = interface_to_usbdev(intf); |
| 314 | shark->tea.v4l2_dev = &shark->v4l2_dev; |
| 315 | shark->tea.private_data = shark; |
| 316 | shark->tea.ops = &shark_tea_ops; |
| 317 | shark->tea.has_am = true; |
| 318 | shark->tea.write_before_read = true; |
| 319 | strlcpy(shark->tea.card, "Griffin radioSHARK2", |
| 320 | sizeof(shark->tea.card)); |
| 321 | usb_make_path(shark->usbdev, shark->tea.bus_info, |
| 322 | sizeof(shark->tea.bus_info)); |
| 323 | |
| 324 | retval = radio_tea5777_init(&shark->tea, THIS_MODULE); |
| 325 | if (retval) { |
| 326 | v4l2_err(&shark->v4l2_dev, "couldn't init tea5777\n"); |
| 327 | goto err_init_tea; |
| 328 | } |
| 329 | |
Hans de Goede | 4faba76 | 2012-06-23 04:39:58 -0300 | [diff] [blame] | 330 | return 0; |
| 331 | |
| 332 | err_init_tea: |
| 333 | v4l2_device_unregister(&shark->v4l2_dev); |
| 334 | err_reg_dev: |
Hans de Goede | be0c44f | 2012-08-11 06:34:55 -0300 | [diff] [blame] | 335 | shark_unregister_leds(shark); |
| 336 | err_reg_leds: |
Hans de Goede | 4faba76 | 2012-06-23 04:39:58 -0300 | [diff] [blame] | 337 | kfree(shark->transfer_buffer); |
| 338 | err_alloc_buffer: |
| 339 | kfree(shark); |
| 340 | |
| 341 | return retval; |
| 342 | } |
| 343 | |
Hans de Goede | d1f280d | 2012-08-11 12:37:13 -0300 | [diff] [blame] | 344 | #ifdef CONFIG_PM |
Mauro Carvalho Chehab | 1f71927 | 2012-09-13 17:37:40 -0300 | [diff] [blame] | 345 | static int usb_shark_suspend(struct usb_interface *intf, pm_message_t message) |
Hans de Goede | d1f280d | 2012-08-11 12:37:13 -0300 | [diff] [blame] | 346 | { |
| 347 | return 0; |
| 348 | } |
| 349 | |
Mauro Carvalho Chehab | 1f71927 | 2012-09-13 17:37:40 -0300 | [diff] [blame] | 350 | static int usb_shark_resume(struct usb_interface *intf) |
Hans de Goede | d1f280d | 2012-08-11 12:37:13 -0300 | [diff] [blame] | 351 | { |
| 352 | struct v4l2_device *v4l2_dev = usb_get_intfdata(intf); |
| 353 | struct shark_device *shark = v4l2_dev_to_shark(v4l2_dev); |
| 354 | int ret; |
| 355 | |
| 356 | mutex_lock(&shark->tea.mutex); |
| 357 | ret = radio_tea5777_set_freq(&shark->tea); |
| 358 | mutex_unlock(&shark->tea.mutex); |
| 359 | |
| 360 | shark_resume_leds(shark); |
| 361 | |
| 362 | return ret; |
| 363 | } |
| 364 | #endif |
| 365 | |
Hans de Goede | 4faba76 | 2012-06-23 04:39:58 -0300 | [diff] [blame] | 366 | /* Specify the bcdDevice value, as the radioSHARK and radioSHARK2 share ids */ |
| 367 | static struct usb_device_id usb_shark_device_table[] = { |
| 368 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION | |
| 369 | USB_DEVICE_ID_MATCH_INT_CLASS, |
| 370 | .idVendor = 0x077d, |
| 371 | .idProduct = 0x627a, |
| 372 | .bcdDevice_lo = 0x0010, |
| 373 | .bcdDevice_hi = 0x0010, |
| 374 | .bInterfaceClass = 3, |
| 375 | }, |
| 376 | { } |
| 377 | }; |
| 378 | MODULE_DEVICE_TABLE(usb, usb_shark_device_table); |
| 379 | |
| 380 | static struct usb_driver usb_shark_driver = { |
| 381 | .name = DRV_NAME, |
| 382 | .probe = usb_shark_probe, |
| 383 | .disconnect = usb_shark_disconnect, |
| 384 | .id_table = usb_shark_device_table, |
Hans de Goede | d1f280d | 2012-08-11 12:37:13 -0300 | [diff] [blame] | 385 | #ifdef CONFIG_PM |
| 386 | .suspend = usb_shark_suspend, |
| 387 | .resume = usb_shark_resume, |
| 388 | .reset_resume = usb_shark_resume, |
| 389 | #endif |
Hans de Goede | 4faba76 | 2012-06-23 04:39:58 -0300 | [diff] [blame] | 390 | }; |
| 391 | module_usb_driver(usb_shark_driver); |