radio-shark.c 11 KB

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