radio-shark2.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. #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 "radio-tea5777.h"
  36. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  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. static void shark_led_set_blue(struct led_classdev *led_cdev,
  49. enum led_brightness value);
  50. static void shark_led_set_red(struct led_classdev *led_cdev,
  51. enum led_brightness value);
  52. static const struct led_classdev shark_led_templates[NO_LEDS] = {
  53. [BLUE_LED] = {
  54. .name = "%s:blue:",
  55. .brightness = LED_OFF,
  56. .max_brightness = 127,
  57. .brightness_set = shark_led_set_blue,
  58. },
  59. [RED_LED] = {
  60. .name = "%s:red:",
  61. .brightness = LED_OFF,
  62. .max_brightness = 1,
  63. .brightness_set = shark_led_set_red,
  64. },
  65. };
  66. struct shark_device {
  67. struct usb_device *usbdev;
  68. struct v4l2_device v4l2_dev;
  69. struct radio_tea5777 tea;
  70. struct work_struct led_work;
  71. struct led_classdev leds[NO_LEDS];
  72. char led_names[NO_LEDS][32];
  73. atomic_t brightness[NO_LEDS];
  74. unsigned long brightness_new;
  75. u8 *transfer_buffer;
  76. };
  77. static atomic_t shark_instance = ATOMIC_INIT(0);
  78. static int shark_write_reg(struct radio_tea5777 *tea, u64 reg)
  79. {
  80. struct shark_device *shark = tea->private_data;
  81. int i, res, actual_len;
  82. memset(shark->transfer_buffer, 0, TB_LEN);
  83. shark->transfer_buffer[0] = 0x81; /* Write register command */
  84. for (i = 0; i < 6; i++)
  85. shark->transfer_buffer[i + 1] = (reg >> (40 - i * 8)) & 0xff;
  86. v4l2_dbg(1, debug, tea->v4l2_dev,
  87. "shark2-write: %02x %02x %02x %02x %02x %02x %02x\n",
  88. shark->transfer_buffer[0], shark->transfer_buffer[1],
  89. shark->transfer_buffer[2], shark->transfer_buffer[3],
  90. shark->transfer_buffer[4], shark->transfer_buffer[5],
  91. shark->transfer_buffer[6]);
  92. res = usb_interrupt_msg(shark->usbdev,
  93. usb_sndintpipe(shark->usbdev, SHARK_OUT_EP),
  94. shark->transfer_buffer, TB_LEN,
  95. &actual_len, 1000);
  96. if (res < 0) {
  97. v4l2_err(tea->v4l2_dev, "write error: %d\n", res);
  98. return res;
  99. }
  100. return 0;
  101. }
  102. static int shark_read_reg(struct radio_tea5777 *tea, u32 *reg_ret)
  103. {
  104. struct shark_device *shark = tea->private_data;
  105. int i, res, actual_len;
  106. u32 reg = 0;
  107. memset(shark->transfer_buffer, 0, TB_LEN);
  108. shark->transfer_buffer[0] = 0x82;
  109. res = usb_interrupt_msg(shark->usbdev,
  110. usb_sndintpipe(shark->usbdev, SHARK_OUT_EP),
  111. shark->transfer_buffer, TB_LEN,
  112. &actual_len, 1000);
  113. if (res < 0) {
  114. v4l2_err(tea->v4l2_dev, "request-read error: %d\n", res);
  115. return res;
  116. }
  117. res = usb_interrupt_msg(shark->usbdev,
  118. usb_rcvintpipe(shark->usbdev, SHARK_IN_EP),
  119. shark->transfer_buffer, TB_LEN,
  120. &actual_len, 1000);
  121. if (res < 0) {
  122. v4l2_err(tea->v4l2_dev, "read error: %d\n", res);
  123. return res;
  124. }
  125. for (i = 0; i < 3; i++)
  126. reg |= shark->transfer_buffer[i] << (16 - i * 8);
  127. v4l2_dbg(1, debug, tea->v4l2_dev, "shark2-read: %02x %02x %02x\n",
  128. shark->transfer_buffer[0], shark->transfer_buffer[1],
  129. shark->transfer_buffer[2]);
  130. *reg_ret = reg;
  131. return 0;
  132. }
  133. static struct radio_tea5777_ops shark_tea_ops = {
  134. .write_reg = shark_write_reg,
  135. .read_reg = shark_read_reg,
  136. };
  137. static void shark_led_work(struct work_struct *work)
  138. {
  139. struct shark_device *shark =
  140. container_of(work, struct shark_device, led_work);
  141. int i, res, brightness, actual_len;
  142. /*
  143. * We use the v4l2_dev lock and registered bit to ensure the device
  144. * does not get unplugged and unreffed while we're running.
  145. */
  146. mutex_lock(&shark->tea.mutex);
  147. if (!video_is_registered(&shark->tea.vd))
  148. goto leave;
  149. for (i = 0; i < 2; i++) {
  150. if (!test_and_clear_bit(i, &shark->brightness_new))
  151. continue;
  152. brightness = atomic_read(&shark->brightness[i]);
  153. memset(shark->transfer_buffer, 0, TB_LEN);
  154. shark->transfer_buffer[0] = 0x83 + i;
  155. shark->transfer_buffer[1] = brightness;
  156. res = usb_interrupt_msg(shark->usbdev,
  157. usb_sndintpipe(shark->usbdev,
  158. SHARK_OUT_EP),
  159. shark->transfer_buffer, TB_LEN,
  160. &actual_len, 1000);
  161. if (res < 0)
  162. v4l2_err(&shark->v4l2_dev, "set LED %s error: %d\n",
  163. shark->led_names[i], res);
  164. }
  165. leave:
  166. mutex_unlock(&shark->tea.mutex);
  167. }
  168. static void shark_led_set_blue(struct led_classdev *led_cdev,
  169. enum led_brightness value)
  170. {
  171. struct shark_device *shark =
  172. container_of(led_cdev, struct shark_device, leds[BLUE_LED]);
  173. atomic_set(&shark->brightness[BLUE_LED], value);
  174. set_bit(BLUE_LED, &shark->brightness_new);
  175. schedule_work(&shark->led_work);
  176. }
  177. static void shark_led_set_red(struct led_classdev *led_cdev,
  178. enum led_brightness value)
  179. {
  180. struct shark_device *shark =
  181. container_of(led_cdev, struct shark_device, leds[RED_LED]);
  182. atomic_set(&shark->brightness[RED_LED], value);
  183. set_bit(RED_LED, &shark->brightness_new);
  184. schedule_work(&shark->led_work);
  185. }
  186. static void usb_shark_disconnect(struct usb_interface *intf)
  187. {
  188. struct v4l2_device *v4l2_dev = usb_get_intfdata(intf);
  189. struct shark_device *shark = v4l2_dev_to_shark(v4l2_dev);
  190. int i;
  191. mutex_lock(&shark->tea.mutex);
  192. v4l2_device_disconnect(&shark->v4l2_dev);
  193. radio_tea5777_exit(&shark->tea);
  194. mutex_unlock(&shark->tea.mutex);
  195. for (i = 0; i < NO_LEDS; i++)
  196. led_classdev_unregister(&shark->leds[i]);
  197. v4l2_device_put(&shark->v4l2_dev);
  198. }
  199. static void usb_shark_release(struct v4l2_device *v4l2_dev)
  200. {
  201. struct shark_device *shark = v4l2_dev_to_shark(v4l2_dev);
  202. cancel_work_sync(&shark->led_work);
  203. v4l2_device_unregister(&shark->v4l2_dev);
  204. kfree(shark->transfer_buffer);
  205. kfree(shark);
  206. }
  207. static int usb_shark_probe(struct usb_interface *intf,
  208. const struct usb_device_id *id)
  209. {
  210. struct shark_device *shark;
  211. int i, retval = -ENOMEM;
  212. shark = kzalloc(sizeof(struct shark_device), GFP_KERNEL);
  213. if (!shark)
  214. return retval;
  215. shark->transfer_buffer = kmalloc(TB_LEN, GFP_KERNEL);
  216. if (!shark->transfer_buffer)
  217. goto err_alloc_buffer;
  218. /*
  219. * Work around a bug in usbhid/hid-core.c, where it leaves a dangling
  220. * pointer in intfdata causing v4l2-device.c to not set it. Which
  221. * results in usb_shark_disconnect() referencing the dangling pointer
  222. *
  223. * REMOVE (as soon as the above bug is fixed, patch submitted)
  224. */
  225. usb_set_intfdata(intf, NULL);
  226. shark->v4l2_dev.release = usb_shark_release;
  227. v4l2_device_set_name(&shark->v4l2_dev, DRV_NAME, &shark_instance);
  228. retval = v4l2_device_register(&intf->dev, &shark->v4l2_dev);
  229. if (retval) {
  230. v4l2_err(&shark->v4l2_dev, "couldn't register v4l2_device\n");
  231. goto err_reg_dev;
  232. }
  233. shark->usbdev = interface_to_usbdev(intf);
  234. shark->tea.v4l2_dev = &shark->v4l2_dev;
  235. shark->tea.private_data = shark;
  236. shark->tea.ops = &shark_tea_ops;
  237. shark->tea.has_am = true;
  238. shark->tea.write_before_read = true;
  239. strlcpy(shark->tea.card, "Griffin radioSHARK2",
  240. sizeof(shark->tea.card));
  241. usb_make_path(shark->usbdev, shark->tea.bus_info,
  242. sizeof(shark->tea.bus_info));
  243. retval = radio_tea5777_init(&shark->tea, THIS_MODULE);
  244. if (retval) {
  245. v4l2_err(&shark->v4l2_dev, "couldn't init tea5777\n");
  246. goto err_init_tea;
  247. }
  248. INIT_WORK(&shark->led_work, shark_led_work);
  249. for (i = 0; i < NO_LEDS; i++) {
  250. shark->leds[i] = shark_led_templates[i];
  251. snprintf(shark->led_names[i], sizeof(shark->led_names[0]),
  252. shark->leds[i].name, shark->v4l2_dev.name);
  253. shark->leds[i].name = shark->led_names[i];
  254. /*
  255. * We don't fail the probe if we fail to register the leds,
  256. * because once we've called radio_tea5777_init, the /dev/radio0
  257. * node may be opened from userspace holding a reference to us!
  258. *
  259. * Note we cannot register the leds first instead as
  260. * shark_led_work depends on the v4l2 mutex and registered bit.
  261. */
  262. retval = led_classdev_register(&intf->dev, &shark->leds[i]);
  263. if (retval)
  264. v4l2_err(&shark->v4l2_dev,
  265. "couldn't register led: %s\n",
  266. shark->led_names[i]);
  267. }
  268. return 0;
  269. err_init_tea:
  270. v4l2_device_unregister(&shark->v4l2_dev);
  271. err_reg_dev:
  272. kfree(shark->transfer_buffer);
  273. err_alloc_buffer:
  274. kfree(shark);
  275. return retval;
  276. }
  277. /* Specify the bcdDevice value, as the radioSHARK and radioSHARK2 share ids */
  278. static struct usb_device_id usb_shark_device_table[] = {
  279. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION |
  280. USB_DEVICE_ID_MATCH_INT_CLASS,
  281. .idVendor = 0x077d,
  282. .idProduct = 0x627a,
  283. .bcdDevice_lo = 0x0010,
  284. .bcdDevice_hi = 0x0010,
  285. .bInterfaceClass = 3,
  286. },
  287. { }
  288. };
  289. MODULE_DEVICE_TABLE(usb, usb_shark_device_table);
  290. static struct usb_driver usb_shark_driver = {
  291. .name = DRV_NAME,
  292. .probe = usb_shark_probe,
  293. .disconnect = usb_shark_disconnect,
  294. .id_table = usb_shark_device_table,
  295. };
  296. module_usb_driver(usb_shark_driver);