radio-shark.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*
  2. * Linux V4L2 radio driver for the Griffin radioSHARK USB radio receiver
  3. *
  4. * Note the radioSHARK offers the audio through a regular USB audio device,
  5. * this driver only handles the tuning.
  6. *
  7. * The info necessary to drive the shark was taken from the small userspace
  8. * shark.c program by Michael Rolig, 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. #include <linux/init.h>
  28. #include <linux/kernel.h>
  29. #include <linux/leds.h>
  30. #include <linux/module.h>
  31. #include <linux/slab.h>
  32. #include <linux/usb.h>
  33. #include <linux/workqueue.h>
  34. #include <media/v4l2-device.h>
  35. #include <sound/tea575x-tuner.h>
  36. /*
  37. * Version Information
  38. */
  39. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  40. MODULE_DESCRIPTION("Griffin radioSHARK, USB radio receiver driver");
  41. MODULE_LICENSE("GPL");
  42. #define SHARK_IN_EP 0x83
  43. #define SHARK_OUT_EP 0x05
  44. #define TEA575X_BIT_MONO (1<<22) /* 0 = stereo, 1 = mono */
  45. #define TEA575X_BIT_BAND_MASK (3<<20)
  46. #define TEA575X_BIT_BAND_FM (0<<20)
  47. #define TB_LEN 6
  48. #define DRV_NAME "radioshark"
  49. #define v4l2_dev_to_shark(d) container_of(d, struct shark_device, v4l2_dev)
  50. enum { BLUE_LED, BLUE_PULSE_LED, RED_LED, NO_LEDS };
  51. static void shark_led_set_blue(struct led_classdev *led_cdev,
  52. enum led_brightness value);
  53. static void shark_led_set_blue_pulse(struct led_classdev *led_cdev,
  54. enum led_brightness value);
  55. static void shark_led_set_red(struct led_classdev *led_cdev,
  56. enum led_brightness value);
  57. static const struct led_classdev shark_led_templates[NO_LEDS] = {
  58. [BLUE_LED] = {
  59. .name = "%s:blue:",
  60. .brightness = LED_OFF,
  61. .max_brightness = 127,
  62. .brightness_set = shark_led_set_blue,
  63. },
  64. [BLUE_PULSE_LED] = {
  65. .name = "%s:blue-pulse:",
  66. .brightness = LED_OFF,
  67. .max_brightness = 255,
  68. .brightness_set = shark_led_set_blue_pulse,
  69. },
  70. [RED_LED] = {
  71. .name = "%s:red:",
  72. .brightness = LED_OFF,
  73. .max_brightness = 1,
  74. .brightness_set = shark_led_set_red,
  75. },
  76. };
  77. struct shark_device {
  78. struct usb_device *usbdev;
  79. struct v4l2_device v4l2_dev;
  80. struct snd_tea575x tea;
  81. struct work_struct led_work;
  82. struct led_classdev leds[NO_LEDS];
  83. char led_names[NO_LEDS][32];
  84. atomic_t brightness[NO_LEDS];
  85. unsigned long brightness_new;
  86. u8 *transfer_buffer;
  87. u32 last_val;
  88. };
  89. static atomic_t shark_instance = ATOMIC_INIT(0);
  90. static void shark_write_val(struct snd_tea575x *tea, u32 val)
  91. {
  92. struct shark_device *shark = tea->private_data;
  93. int i, res, actual_len;
  94. /* Avoid unnecessary (slow) USB transfers */
  95. if (shark->last_val == val)
  96. return;
  97. memset(shark->transfer_buffer, 0, TB_LEN);
  98. shark->transfer_buffer[0] = 0xc0; /* Write shift register command */
  99. for (i = 0; i < 4; i++)
  100. shark->transfer_buffer[i] |= (val >> (24 - i * 8)) & 0xff;
  101. res = usb_interrupt_msg(shark->usbdev,
  102. usb_sndintpipe(shark->usbdev, SHARK_OUT_EP),
  103. shark->transfer_buffer, TB_LEN,
  104. &actual_len, 1000);
  105. if (res >= 0)
  106. shark->last_val = val;
  107. else
  108. v4l2_err(&shark->v4l2_dev, "set-freq error: %d\n", res);
  109. }
  110. static u32 shark_read_val(struct snd_tea575x *tea)
  111. {
  112. struct shark_device *shark = tea->private_data;
  113. int i, res, actual_len;
  114. u32 val = 0;
  115. memset(shark->transfer_buffer, 0, TB_LEN);
  116. shark->transfer_buffer[0] = 0x80;
  117. res = usb_interrupt_msg(shark->usbdev,
  118. usb_sndintpipe(shark->usbdev, SHARK_OUT_EP),
  119. shark->transfer_buffer, TB_LEN,
  120. &actual_len, 1000);
  121. if (res < 0) {
  122. v4l2_err(&shark->v4l2_dev, "request-status error: %d\n", res);
  123. return shark->last_val;
  124. }
  125. res = usb_interrupt_msg(shark->usbdev,
  126. usb_rcvintpipe(shark->usbdev, SHARK_IN_EP),
  127. shark->transfer_buffer, TB_LEN,
  128. &actual_len, 1000);
  129. if (res < 0) {
  130. v4l2_err(&shark->v4l2_dev, "get-status error: %d\n", res);
  131. return shark->last_val;
  132. }
  133. for (i = 0; i < 4; i++)
  134. val |= shark->transfer_buffer[i] << (24 - i * 8);
  135. shark->last_val = val;
  136. /*
  137. * The shark does not allow actually reading the stereo / mono pin :(
  138. * So assume that when we're tuned to an FM station and mono has not
  139. * been requested, that we're receiving stereo.
  140. */
  141. if (((val & TEA575X_BIT_BAND_MASK) == TEA575X_BIT_BAND_FM) &&
  142. !(val & TEA575X_BIT_MONO))
  143. shark->tea.stereo = true;
  144. else
  145. shark->tea.stereo = false;
  146. return val;
  147. }
  148. static struct snd_tea575x_ops shark_tea_ops = {
  149. .write_val = shark_write_val,
  150. .read_val = shark_read_val,
  151. };
  152. static void shark_led_work(struct work_struct *work)
  153. {
  154. struct shark_device *shark =
  155. container_of(work, struct shark_device, led_work);
  156. int i, res, brightness, actual_len;
  157. /*
  158. * We use the v4l2_dev lock and registered bit to ensure the device
  159. * does not get unplugged and unreffed while we're running.
  160. */
  161. mutex_lock(&shark->tea.mutex);
  162. if (!video_is_registered(&shark->tea.vd))
  163. goto leave;
  164. for (i = 0; i < 3; i++) {
  165. if (!test_and_clear_bit(i, &shark->brightness_new))
  166. continue;
  167. brightness = atomic_read(&shark->brightness[i]);
  168. memset(shark->transfer_buffer, 0, TB_LEN);
  169. if (i != RED_LED) {
  170. shark->transfer_buffer[0] = 0xA0 + i;
  171. shark->transfer_buffer[1] = brightness;
  172. } else
  173. shark->transfer_buffer[0] = brightness ? 0xA9 : 0xA8;
  174. res = usb_interrupt_msg(shark->usbdev,
  175. usb_sndintpipe(shark->usbdev, 0x05),
  176. shark->transfer_buffer, TB_LEN,
  177. &actual_len, 1000);
  178. if (res < 0)
  179. v4l2_err(&shark->v4l2_dev, "set LED %s error: %d\n",
  180. shark->led_names[i], res);
  181. }
  182. leave:
  183. mutex_unlock(&shark->tea.mutex);
  184. }
  185. static void shark_led_set_blue(struct led_classdev *led_cdev,
  186. enum led_brightness value)
  187. {
  188. struct shark_device *shark =
  189. container_of(led_cdev, struct shark_device, leds[BLUE_LED]);
  190. atomic_set(&shark->brightness[BLUE_LED], value);
  191. set_bit(BLUE_LED, &shark->brightness_new);
  192. schedule_work(&shark->led_work);
  193. }
  194. static void shark_led_set_blue_pulse(struct led_classdev *led_cdev,
  195. enum led_brightness value)
  196. {
  197. struct shark_device *shark = container_of(led_cdev,
  198. struct shark_device, leds[BLUE_PULSE_LED]);
  199. atomic_set(&shark->brightness[BLUE_PULSE_LED], 256 - value);
  200. set_bit(BLUE_PULSE_LED, &shark->brightness_new);
  201. schedule_work(&shark->led_work);
  202. }
  203. static void shark_led_set_red(struct led_classdev *led_cdev,
  204. enum led_brightness value)
  205. {
  206. struct shark_device *shark =
  207. container_of(led_cdev, struct shark_device, leds[RED_LED]);
  208. atomic_set(&shark->brightness[RED_LED], value);
  209. set_bit(RED_LED, &shark->brightness_new);
  210. schedule_work(&shark->led_work);
  211. }
  212. static void usb_shark_disconnect(struct usb_interface *intf)
  213. {
  214. struct v4l2_device *v4l2_dev = usb_get_intfdata(intf);
  215. struct shark_device *shark = v4l2_dev_to_shark(v4l2_dev);
  216. int i;
  217. mutex_lock(&shark->tea.mutex);
  218. v4l2_device_disconnect(&shark->v4l2_dev);
  219. snd_tea575x_exit(&shark->tea);
  220. mutex_unlock(&shark->tea.mutex);
  221. for (i = 0; i < NO_LEDS; i++)
  222. led_classdev_unregister(&shark->leds[i]);
  223. v4l2_device_put(&shark->v4l2_dev);
  224. }
  225. static void usb_shark_release(struct v4l2_device *v4l2_dev)
  226. {
  227. struct shark_device *shark = v4l2_dev_to_shark(v4l2_dev);
  228. cancel_work_sync(&shark->led_work);
  229. v4l2_device_unregister(&shark->v4l2_dev);
  230. kfree(shark->transfer_buffer);
  231. kfree(shark);
  232. }
  233. static int usb_shark_probe(struct usb_interface *intf,
  234. const struct usb_device_id *id)
  235. {
  236. struct shark_device *shark;
  237. int i, retval = -ENOMEM;
  238. shark = kzalloc(sizeof(struct shark_device), GFP_KERNEL);
  239. if (!shark)
  240. return retval;
  241. shark->transfer_buffer = kmalloc(TB_LEN, GFP_KERNEL);
  242. if (!shark->transfer_buffer)
  243. goto err_alloc_buffer;
  244. /*
  245. * Work around a bug in usbhid/hid-core.c, where it leaves a dangling
  246. * pointer in intfdata causing v4l2-device.c to not set it. Which
  247. * results in usb_shark_disconnect() referencing the dangling pointer
  248. *
  249. * REMOVE (as soon as the above bug is fixed, patch submitted)
  250. */
  251. usb_set_intfdata(intf, NULL);
  252. shark->v4l2_dev.release = usb_shark_release;
  253. v4l2_device_set_name(&shark->v4l2_dev, DRV_NAME, &shark_instance);
  254. retval = v4l2_device_register(&intf->dev, &shark->v4l2_dev);
  255. if (retval) {
  256. v4l2_err(&shark->v4l2_dev, "couldn't register v4l2_device\n");
  257. goto err_reg_dev;
  258. }
  259. shark->usbdev = interface_to_usbdev(intf);
  260. shark->tea.v4l2_dev = &shark->v4l2_dev;
  261. shark->tea.private_data = shark;
  262. shark->tea.radio_nr = -1;
  263. shark->tea.ops = &shark_tea_ops;
  264. shark->tea.cannot_mute = true;
  265. strlcpy(shark->tea.card, "Griffin radioSHARK",
  266. sizeof(shark->tea.card));
  267. usb_make_path(shark->usbdev, shark->tea.bus_info,
  268. sizeof(shark->tea.bus_info));
  269. retval = snd_tea575x_init(&shark->tea, THIS_MODULE);
  270. if (retval) {
  271. v4l2_err(&shark->v4l2_dev, "couldn't init tea5757\n");
  272. goto err_init_tea;
  273. }
  274. INIT_WORK(&shark->led_work, shark_led_work);
  275. for (i = 0; i < NO_LEDS; i++) {
  276. shark->leds[i] = shark_led_templates[i];
  277. snprintf(shark->led_names[i], sizeof(shark->led_names[0]),
  278. shark->leds[i].name, shark->v4l2_dev.name);
  279. shark->leds[i].name = shark->led_names[i];
  280. /*
  281. * We don't fail the probe if we fail to register the leds,
  282. * because once we've called snd_tea575x_init, the /dev/radio0
  283. * node may be opened from userspace holding a reference to us!
  284. *
  285. * Note we cannot register the leds first instead as
  286. * shark_led_work depends on the v4l2 mutex and registered bit.
  287. */
  288. retval = led_classdev_register(&intf->dev, &shark->leds[i]);
  289. if (retval)
  290. v4l2_err(&shark->v4l2_dev,
  291. "couldn't register led: %s\n",
  292. shark->led_names[i]);
  293. }
  294. return 0;
  295. err_init_tea:
  296. v4l2_device_unregister(&shark->v4l2_dev);
  297. err_reg_dev:
  298. kfree(shark->transfer_buffer);
  299. err_alloc_buffer:
  300. kfree(shark);
  301. return retval;
  302. }
  303. /* Specify the bcdDevice value, as the radioSHARK and radioSHARK2 share ids */
  304. static struct usb_device_id usb_shark_device_table[] = {
  305. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION |
  306. USB_DEVICE_ID_MATCH_INT_CLASS,
  307. .idVendor = 0x077d,
  308. .idProduct = 0x627a,
  309. .bcdDevice_lo = 0x0001,
  310. .bcdDevice_hi = 0x0001,
  311. .bInterfaceClass = 3,
  312. },
  313. { }
  314. };
  315. MODULE_DEVICE_TABLE(usb, usb_shark_device_table);
  316. static struct usb_driver usb_shark_driver = {
  317. .name = DRV_NAME,
  318. .probe = usb_shark_probe,
  319. .disconnect = usb_shark_disconnect,
  320. .id_table = usb_shark_device_table,
  321. };
  322. module_usb_driver(usb_shark_driver);