radio-shark.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. #include <linux/init.h>
  24. #include <linux/kernel.h>
  25. #include <linux/leds.h>
  26. #include <linux/module.h>
  27. #include <linux/slab.h>
  28. #include <linux/usb.h>
  29. #include <linux/workqueue.h>
  30. #include <media/v4l2-device.h>
  31. #include <media/drv-intf/tea575x.h>
  32. #if defined(CONFIG_LEDS_CLASS) || \
  33. (defined(CONFIG_LEDS_CLASS_MODULE) && defined(CONFIG_RADIO_SHARK_MODULE))
  34. #define SHARK_USE_LEDS 1
  35. #endif
  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. /* Note BLUE_IS_PULSE comes after NO_LEDS as it is a status bit, not a LED */
  51. enum { BLUE_LED, BLUE_PULSE_LED, RED_LED, NO_LEDS, BLUE_IS_PULSE };
  52. struct shark_device {
  53. struct usb_device *usbdev;
  54. struct v4l2_device v4l2_dev;
  55. struct snd_tea575x tea;
  56. #ifdef SHARK_USE_LEDS
  57. struct work_struct led_work;
  58. struct led_classdev leds[NO_LEDS];
  59. char led_names[NO_LEDS][32];
  60. atomic_t brightness[NO_LEDS];
  61. unsigned long brightness_new;
  62. #endif
  63. u8 *transfer_buffer;
  64. u32 last_val;
  65. };
  66. static atomic_t shark_instance = ATOMIC_INIT(0);
  67. static void shark_write_val(struct snd_tea575x *tea, u32 val)
  68. {
  69. struct shark_device *shark = tea->private_data;
  70. int i, res, actual_len;
  71. /* Avoid unnecessary (slow) USB transfers */
  72. if (shark->last_val == val)
  73. return;
  74. memset(shark->transfer_buffer, 0, TB_LEN);
  75. shark->transfer_buffer[0] = 0xc0; /* Write shift register command */
  76. for (i = 0; i < 4; i++)
  77. shark->transfer_buffer[i] |= (val >> (24 - i * 8)) & 0xff;
  78. res = usb_interrupt_msg(shark->usbdev,
  79. usb_sndintpipe(shark->usbdev, SHARK_OUT_EP),
  80. shark->transfer_buffer, TB_LEN,
  81. &actual_len, 1000);
  82. if (res >= 0)
  83. shark->last_val = val;
  84. else
  85. v4l2_err(&shark->v4l2_dev, "set-freq error: %d\n", res);
  86. }
  87. static u32 shark_read_val(struct snd_tea575x *tea)
  88. {
  89. struct shark_device *shark = tea->private_data;
  90. int i, res, actual_len;
  91. u32 val = 0;
  92. memset(shark->transfer_buffer, 0, TB_LEN);
  93. shark->transfer_buffer[0] = 0x80;
  94. res = usb_interrupt_msg(shark->usbdev,
  95. usb_sndintpipe(shark->usbdev, SHARK_OUT_EP),
  96. shark->transfer_buffer, TB_LEN,
  97. &actual_len, 1000);
  98. if (res < 0) {
  99. v4l2_err(&shark->v4l2_dev, "request-status error: %d\n", res);
  100. return shark->last_val;
  101. }
  102. res = usb_interrupt_msg(shark->usbdev,
  103. usb_rcvintpipe(shark->usbdev, SHARK_IN_EP),
  104. shark->transfer_buffer, TB_LEN,
  105. &actual_len, 1000);
  106. if (res < 0) {
  107. v4l2_err(&shark->v4l2_dev, "get-status error: %d\n", res);
  108. return shark->last_val;
  109. }
  110. for (i = 0; i < 4; i++)
  111. val |= shark->transfer_buffer[i] << (24 - i * 8);
  112. shark->last_val = val;
  113. /*
  114. * The shark does not allow actually reading the stereo / mono pin :(
  115. * So assume that when we're tuned to an FM station and mono has not
  116. * been requested, that we're receiving stereo.
  117. */
  118. if (((val & TEA575X_BIT_BAND_MASK) == TEA575X_BIT_BAND_FM) &&
  119. !(val & TEA575X_BIT_MONO))
  120. shark->tea.stereo = true;
  121. else
  122. shark->tea.stereo = false;
  123. return val;
  124. }
  125. static const struct snd_tea575x_ops shark_tea_ops = {
  126. .write_val = shark_write_val,
  127. .read_val = shark_read_val,
  128. };
  129. #ifdef SHARK_USE_LEDS
  130. static void shark_led_work(struct work_struct *work)
  131. {
  132. struct shark_device *shark =
  133. container_of(work, struct shark_device, led_work);
  134. int i, res, brightness, actual_len;
  135. for (i = 0; i < 3; i++) {
  136. if (!test_and_clear_bit(i, &shark->brightness_new))
  137. continue;
  138. brightness = atomic_read(&shark->brightness[i]);
  139. memset(shark->transfer_buffer, 0, TB_LEN);
  140. if (i != RED_LED) {
  141. shark->transfer_buffer[0] = 0xA0 + i;
  142. shark->transfer_buffer[1] = brightness;
  143. } else
  144. shark->transfer_buffer[0] = brightness ? 0xA9 : 0xA8;
  145. res = usb_interrupt_msg(shark->usbdev,
  146. usb_sndintpipe(shark->usbdev, 0x05),
  147. shark->transfer_buffer, TB_LEN,
  148. &actual_len, 1000);
  149. if (res < 0)
  150. v4l2_err(&shark->v4l2_dev, "set LED %s error: %d\n",
  151. shark->led_names[i], res);
  152. }
  153. }
  154. static void shark_led_set_blue(struct led_classdev *led_cdev,
  155. enum led_brightness value)
  156. {
  157. struct shark_device *shark =
  158. container_of(led_cdev, struct shark_device, leds[BLUE_LED]);
  159. atomic_set(&shark->brightness[BLUE_LED], value);
  160. set_bit(BLUE_LED, &shark->brightness_new);
  161. clear_bit(BLUE_IS_PULSE, &shark->brightness_new);
  162. schedule_work(&shark->led_work);
  163. }
  164. static void shark_led_set_blue_pulse(struct led_classdev *led_cdev,
  165. enum led_brightness value)
  166. {
  167. struct shark_device *shark = container_of(led_cdev,
  168. struct shark_device, leds[BLUE_PULSE_LED]);
  169. atomic_set(&shark->brightness[BLUE_PULSE_LED], 256 - value);
  170. set_bit(BLUE_PULSE_LED, &shark->brightness_new);
  171. set_bit(BLUE_IS_PULSE, &shark->brightness_new);
  172. schedule_work(&shark->led_work);
  173. }
  174. static void shark_led_set_red(struct led_classdev *led_cdev,
  175. enum led_brightness value)
  176. {
  177. struct shark_device *shark =
  178. container_of(led_cdev, struct shark_device, leds[RED_LED]);
  179. atomic_set(&shark->brightness[RED_LED], value);
  180. set_bit(RED_LED, &shark->brightness_new);
  181. schedule_work(&shark->led_work);
  182. }
  183. static const struct led_classdev shark_led_templates[NO_LEDS] = {
  184. [BLUE_LED] = {
  185. .name = "%s:blue:",
  186. .brightness = LED_OFF,
  187. .max_brightness = 127,
  188. .brightness_set = shark_led_set_blue,
  189. },
  190. [BLUE_PULSE_LED] = {
  191. .name = "%s:blue-pulse:",
  192. .brightness = LED_OFF,
  193. .max_brightness = 255,
  194. .brightness_set = shark_led_set_blue_pulse,
  195. },
  196. [RED_LED] = {
  197. .name = "%s:red:",
  198. .brightness = LED_OFF,
  199. .max_brightness = 1,
  200. .brightness_set = shark_led_set_red,
  201. },
  202. };
  203. static int shark_register_leds(struct shark_device *shark, struct device *dev)
  204. {
  205. int i, retval;
  206. atomic_set(&shark->brightness[BLUE_LED], 127);
  207. INIT_WORK(&shark->led_work, shark_led_work);
  208. for (i = 0; i < NO_LEDS; i++) {
  209. shark->leds[i] = shark_led_templates[i];
  210. snprintf(shark->led_names[i], sizeof(shark->led_names[0]),
  211. shark->leds[i].name, shark->v4l2_dev.name);
  212. shark->leds[i].name = shark->led_names[i];
  213. retval = led_classdev_register(dev, &shark->leds[i]);
  214. if (retval) {
  215. v4l2_err(&shark->v4l2_dev,
  216. "couldn't register led: %s\n",
  217. shark->led_names[i]);
  218. return retval;
  219. }
  220. }
  221. return 0;
  222. }
  223. static void shark_unregister_leds(struct shark_device *shark)
  224. {
  225. int i;
  226. for (i = 0; i < NO_LEDS; i++)
  227. led_classdev_unregister(&shark->leds[i]);
  228. cancel_work_sync(&shark->led_work);
  229. }
  230. static inline void shark_resume_leds(struct shark_device *shark)
  231. {
  232. if (test_bit(BLUE_IS_PULSE, &shark->brightness_new))
  233. set_bit(BLUE_PULSE_LED, &shark->brightness_new);
  234. else
  235. set_bit(BLUE_LED, &shark->brightness_new);
  236. set_bit(RED_LED, &shark->brightness_new);
  237. schedule_work(&shark->led_work);
  238. }
  239. #else
  240. static int shark_register_leds(struct shark_device *shark, struct device *dev)
  241. {
  242. v4l2_warn(&shark->v4l2_dev,
  243. "CONFIG_LEDS_CLASS not enabled, LED support disabled\n");
  244. return 0;
  245. }
  246. static inline void shark_unregister_leds(struct shark_device *shark) { }
  247. static inline void shark_resume_leds(struct shark_device *shark) { }
  248. #endif
  249. static void usb_shark_disconnect(struct usb_interface *intf)
  250. {
  251. struct v4l2_device *v4l2_dev = usb_get_intfdata(intf);
  252. struct shark_device *shark = v4l2_dev_to_shark(v4l2_dev);
  253. mutex_lock(&shark->tea.mutex);
  254. v4l2_device_disconnect(&shark->v4l2_dev);
  255. snd_tea575x_exit(&shark->tea);
  256. mutex_unlock(&shark->tea.mutex);
  257. shark_unregister_leds(shark);
  258. v4l2_device_put(&shark->v4l2_dev);
  259. }
  260. static void usb_shark_release(struct v4l2_device *v4l2_dev)
  261. {
  262. struct shark_device *shark = v4l2_dev_to_shark(v4l2_dev);
  263. v4l2_device_unregister(&shark->v4l2_dev);
  264. kfree(shark->transfer_buffer);
  265. kfree(shark);
  266. }
  267. static int usb_shark_probe(struct usb_interface *intf,
  268. const struct usb_device_id *id)
  269. {
  270. struct shark_device *shark;
  271. int retval = -ENOMEM;
  272. shark = kzalloc(sizeof(struct shark_device), GFP_KERNEL);
  273. if (!shark)
  274. return retval;
  275. shark->transfer_buffer = kmalloc(TB_LEN, GFP_KERNEL);
  276. if (!shark->transfer_buffer)
  277. goto err_alloc_buffer;
  278. v4l2_device_set_name(&shark->v4l2_dev, DRV_NAME, &shark_instance);
  279. retval = shark_register_leds(shark, &intf->dev);
  280. if (retval)
  281. goto err_reg_leds;
  282. shark->v4l2_dev.release = usb_shark_release;
  283. retval = v4l2_device_register(&intf->dev, &shark->v4l2_dev);
  284. if (retval) {
  285. v4l2_err(&shark->v4l2_dev, "couldn't register v4l2_device\n");
  286. goto err_reg_dev;
  287. }
  288. shark->usbdev = interface_to_usbdev(intf);
  289. shark->tea.v4l2_dev = &shark->v4l2_dev;
  290. shark->tea.private_data = shark;
  291. shark->tea.radio_nr = -1;
  292. shark->tea.ops = &shark_tea_ops;
  293. shark->tea.cannot_mute = true;
  294. shark->tea.has_am = true;
  295. strlcpy(shark->tea.card, "Griffin radioSHARK",
  296. sizeof(shark->tea.card));
  297. usb_make_path(shark->usbdev, shark->tea.bus_info,
  298. sizeof(shark->tea.bus_info));
  299. retval = snd_tea575x_init(&shark->tea, THIS_MODULE);
  300. if (retval) {
  301. v4l2_err(&shark->v4l2_dev, "couldn't init tea5757\n");
  302. goto err_init_tea;
  303. }
  304. return 0;
  305. err_init_tea:
  306. v4l2_device_unregister(&shark->v4l2_dev);
  307. err_reg_dev:
  308. shark_unregister_leds(shark);
  309. err_reg_leds:
  310. kfree(shark->transfer_buffer);
  311. err_alloc_buffer:
  312. kfree(shark);
  313. return retval;
  314. }
  315. #ifdef CONFIG_PM
  316. static int usb_shark_suspend(struct usb_interface *intf, pm_message_t message)
  317. {
  318. return 0;
  319. }
  320. static int usb_shark_resume(struct usb_interface *intf)
  321. {
  322. struct v4l2_device *v4l2_dev = usb_get_intfdata(intf);
  323. struct shark_device *shark = v4l2_dev_to_shark(v4l2_dev);
  324. mutex_lock(&shark->tea.mutex);
  325. snd_tea575x_set_freq(&shark->tea);
  326. mutex_unlock(&shark->tea.mutex);
  327. shark_resume_leds(shark);
  328. return 0;
  329. }
  330. #endif
  331. /* Specify the bcdDevice value, as the radioSHARK and radioSHARK2 share ids */
  332. static const struct usb_device_id usb_shark_device_table[] = {
  333. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION |
  334. USB_DEVICE_ID_MATCH_INT_CLASS,
  335. .idVendor = 0x077d,
  336. .idProduct = 0x627a,
  337. .bcdDevice_lo = 0x0001,
  338. .bcdDevice_hi = 0x0001,
  339. .bInterfaceClass = 3,
  340. },
  341. { }
  342. };
  343. MODULE_DEVICE_TABLE(usb, usb_shark_device_table);
  344. static struct usb_driver usb_shark_driver = {
  345. .name = DRV_NAME,
  346. .probe = usb_shark_probe,
  347. .disconnect = usb_shark_disconnect,
  348. .id_table = usb_shark_device_table,
  349. #ifdef CONFIG_PM
  350. .suspend = usb_shark_suspend,
  351. .resume = usb_shark_resume,
  352. .reset_resume = usb_shark_resume,
  353. #endif
  354. };
  355. module_usb_driver(usb_shark_driver);