radio-shark2.c 10 KB

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