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 103 v4l2_dbg(1, debug, tea->v4l2_dev, 104 "shark2-write: %02x %02x %02x %02x %02x %02x %02x\n", 105 shark->transfer_buffer[0], shark->transfer_buffer[1], 106 shark->transfer_buffer[2], shark->transfer_buffer[3], 107 shark->transfer_buffer[4], shark->transfer_buffer[5], 108 shark->transfer_buffer[6]); 109 110 res = usb_interrupt_msg(shark->usbdev, 111 usb_sndintpipe(shark->usbdev, SHARK_OUT_EP), 112 shark->transfer_buffer, TB_LEN, 113 &actual_len, 1000); 114 if (res < 0) { 115 v4l2_err(tea->v4l2_dev, "write error: %d\n", res); 116 return res; 117 } 118 119 return 0; 120 } 121 122 static int shark_read_reg(struct radio_tea5777 *tea, u32 *reg_ret) 123 { 124 struct shark_device *shark = tea->private_data; 125 int i, res, actual_len; 126 u32 reg = 0; 127 128 memset(shark->transfer_buffer, 0, TB_LEN); 129 shark->transfer_buffer[0] = 0x82; 130 res = usb_interrupt_msg(shark->usbdev, 131 usb_sndintpipe(shark->usbdev, SHARK_OUT_EP), 132 shark->transfer_buffer, TB_LEN, 133 &actual_len, 1000); 134 if (res < 0) { 135 v4l2_err(tea->v4l2_dev, "request-read error: %d\n", res); 136 return res; 137 } 138 139 res = usb_interrupt_msg(shark->usbdev, 140 usb_rcvintpipe(shark->usbdev, SHARK_IN_EP), 141 shark->transfer_buffer, TB_LEN, 142 &actual_len, 1000); 143 if (res < 0) { 144 v4l2_err(tea->v4l2_dev, "read error: %d\n", res); 145 return res; 146 } 147 148 for (i = 0; i < 3; i++) 149 reg |= shark->transfer_buffer[i] << (16 - i * 8); 150 151 v4l2_dbg(1, debug, tea->v4l2_dev, "shark2-read: %02x %02x %02x\n", 152 shark->transfer_buffer[0], shark->transfer_buffer[1], 153 shark->transfer_buffer[2]); 154 155 *reg_ret = reg; 156 return 0; 157 } 158 159 static struct radio_tea5777_ops shark_tea_ops = { 160 .write_reg = shark_write_reg, 161 .read_reg = shark_read_reg, 162 }; 163 164 static void shark_led_work(struct work_struct *work) 165 { 166 struct shark_device *shark = 167 container_of(work, struct shark_device, led_work); 168 int i, res, brightness, actual_len; 169 170 for (i = 0; i < 2; i++) { 171 if (!test_and_clear_bit(i, &shark->brightness_new)) 172 continue; 173 174 brightness = atomic_read(&shark->brightness[i]); 175 memset(shark->transfer_buffer, 0, TB_LEN); 176 shark->transfer_buffer[0] = 0x83 + i; 177 shark->transfer_buffer[1] = brightness; 178 res = usb_interrupt_msg(shark->usbdev, 179 usb_sndintpipe(shark->usbdev, 180 SHARK_OUT_EP), 181 shark->transfer_buffer, TB_LEN, 182 &actual_len, 1000); 183 if (res < 0) 184 v4l2_err(&shark->v4l2_dev, "set LED %s error: %d\n", 185 shark->led_names[i], res); 186 } 187 } 188 189 static void shark_led_set_blue(struct led_classdev *led_cdev, 190 enum led_brightness value) 191 { 192 struct shark_device *shark = 193 container_of(led_cdev, struct shark_device, leds[BLUE_LED]); 194 195 atomic_set(&shark->brightness[BLUE_LED], value); 196 set_bit(BLUE_LED, &shark->brightness_new); 197 schedule_work(&shark->led_work); 198 } 199 200 static void shark_led_set_red(struct led_classdev *led_cdev, 201 enum led_brightness value) 202 { 203 struct shark_device *shark = 204 container_of(led_cdev, struct shark_device, leds[RED_LED]); 205 206 atomic_set(&shark->brightness[RED_LED], value); 207 set_bit(RED_LED, &shark->brightness_new); 208 schedule_work(&shark->led_work); 209 } 210 211 static void usb_shark_disconnect(struct usb_interface *intf) 212 { 213 struct v4l2_device *v4l2_dev = usb_get_intfdata(intf); 214 struct shark_device *shark = v4l2_dev_to_shark(v4l2_dev); 215 int i; 216 217 mutex_lock(&shark->tea.mutex); 218 v4l2_device_disconnect(&shark->v4l2_dev); 219 radio_tea5777_exit(&shark->tea); 220 mutex_unlock(&shark->tea.mutex); 221 222 for (i = 0; i < NO_LEDS; i++) 223 led_classdev_unregister(&shark->leds[i]); 224 225 cancel_work_sync(&shark->led_work); 226 227 v4l2_device_put(&shark->v4l2_dev); 228 } 229 230 static void usb_shark_release(struct v4l2_device *v4l2_dev) 231 { 232 struct shark_device *shark = v4l2_dev_to_shark(v4l2_dev); 233 234 v4l2_device_unregister(&shark->v4l2_dev); 235 kfree(shark->transfer_buffer); 236 kfree(shark); 237 } 238 239 static int usb_shark_probe(struct usb_interface *intf, 240 const struct usb_device_id *id) 241 { 242 struct shark_device *shark; 243 int i, retval = -ENOMEM; 244 245 shark = kzalloc(sizeof(struct shark_device), GFP_KERNEL); 246 if (!shark) 247 return retval; 248 249 shark->transfer_buffer = kmalloc(TB_LEN, GFP_KERNEL); 250 if (!shark->transfer_buffer) 251 goto err_alloc_buffer; 252 253 shark->v4l2_dev.release = usb_shark_release; 254 v4l2_device_set_name(&shark->v4l2_dev, DRV_NAME, &shark_instance); 255 retval = v4l2_device_register(&intf->dev, &shark->v4l2_dev); 256 if (retval) { 257 v4l2_err(&shark->v4l2_dev, "couldn't register v4l2_device\n"); 258 goto err_reg_dev; 259 } 260 261 shark->usbdev = interface_to_usbdev(intf); 262 shark->tea.v4l2_dev = &shark->v4l2_dev; 263 shark->tea.private_data = shark; 264 shark->tea.ops = &shark_tea_ops; 265 shark->tea.has_am = true; 266 shark->tea.write_before_read = true; 267 strlcpy(shark->tea.card, "Griffin radioSHARK2", 268 sizeof(shark->tea.card)); 269 usb_make_path(shark->usbdev, shark->tea.bus_info, 270 sizeof(shark->tea.bus_info)); 271 272 retval = radio_tea5777_init(&shark->tea, THIS_MODULE); 273 if (retval) { 274 v4l2_err(&shark->v4l2_dev, "couldn't init tea5777\n"); 275 goto err_init_tea; 276 } 277 278 INIT_WORK(&shark->led_work, shark_led_work); 279 for (i = 0; i < NO_LEDS; i++) { 280 shark->leds[i] = shark_led_templates[i]; 281 snprintf(shark->led_names[i], sizeof(shark->led_names[0]), 282 shark->leds[i].name, shark->v4l2_dev.name); 283 shark->leds[i].name = shark->led_names[i]; 284 /* 285 * We don't fail the probe if we fail to register the leds, 286 * because once we've called radio_tea5777_init, the /dev/radio0 287 * node may be opened from userspace holding a reference to us! 288 * 289 * Note we cannot register the leds first instead as 290 * shark_led_work depends on the v4l2 mutex and registered bit. 291 */ 292 retval = led_classdev_register(&intf->dev, &shark->leds[i]); 293 if (retval) 294 v4l2_err(&shark->v4l2_dev, 295 "couldn't register led: %s\n", 296 shark->led_names[i]); 297 } 298 299 return 0; 300 301 err_init_tea: 302 v4l2_device_unregister(&shark->v4l2_dev); 303 err_reg_dev: 304 kfree(shark->transfer_buffer); 305 err_alloc_buffer: 306 kfree(shark); 307 308 return retval; 309 } 310 311 /* Specify the bcdDevice value, as the radioSHARK and radioSHARK2 share ids */ 312 static struct usb_device_id usb_shark_device_table[] = { 313 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION | 314 USB_DEVICE_ID_MATCH_INT_CLASS, 315 .idVendor = 0x077d, 316 .idProduct = 0x627a, 317 .bcdDevice_lo = 0x0010, 318 .bcdDevice_hi = 0x0010, 319 .bInterfaceClass = 3, 320 }, 321 { } 322 }; 323 MODULE_DEVICE_TABLE(usb, usb_shark_device_table); 324 325 static struct usb_driver usb_shark_driver = { 326 .name = DRV_NAME, 327 .probe = usb_shark_probe, 328 .disconnect = usb_shark_disconnect, 329 .id_table = usb_shark_device_table, 330 }; 331 module_usb_driver(usb_shark_driver); 332