radio-si470x-usb.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. /*
  2. * drivers/media/radio/si470x/radio-si470x-usb.c
  3. *
  4. * USB driver for radios with Silicon Labs Si470x FM Radio Receivers
  5. *
  6. * Copyright (c) 2009 Tobias Lorenz <tobias.lorenz@gmx.net>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. /*
  19. * ToDo:
  20. * - add firmware download/update support
  21. */
  22. /* driver definitions */
  23. #define DRIVER_AUTHOR "Tobias Lorenz <tobias.lorenz@gmx.net>"
  24. #define DRIVER_CARD "Silicon Labs Si470x FM Radio Receiver"
  25. #define DRIVER_DESC "USB radio driver for Si470x FM Radio Receivers"
  26. #define DRIVER_VERSION "1.0.10"
  27. /* kernel includes */
  28. #include <linux/usb.h>
  29. #include <linux/hid.h>
  30. #include <linux/slab.h>
  31. #include "radio-si470x.h"
  32. /* USB Device ID List */
  33. static const struct usb_device_id si470x_usb_driver_id_table[] = {
  34. /* Silicon Labs USB FM Radio Reference Design */
  35. { USB_DEVICE_AND_INTERFACE_INFO(0x10c4, 0x818a, USB_CLASS_HID, 0, 0) },
  36. /* ADS/Tech FM Radio Receiver (formerly Instant FM Music) */
  37. { USB_DEVICE_AND_INTERFACE_INFO(0x06e1, 0xa155, USB_CLASS_HID, 0, 0) },
  38. /* KWorld USB FM Radio SnapMusic Mobile 700 (FM700) */
  39. { USB_DEVICE_AND_INTERFACE_INFO(0x1b80, 0xd700, USB_CLASS_HID, 0, 0) },
  40. /* Sanei Electric, Inc. FM USB Radio (sold as DealExtreme.com PCear) */
  41. { USB_DEVICE_AND_INTERFACE_INFO(0x10c5, 0x819a, USB_CLASS_HID, 0, 0) },
  42. /* Axentia ALERT FM USB Receiver */
  43. { USB_DEVICE_AND_INTERFACE_INFO(0x12cf, 0x7111, USB_CLASS_HID, 0, 0) },
  44. /* Terminating entry */
  45. { }
  46. };
  47. MODULE_DEVICE_TABLE(usb, si470x_usb_driver_id_table);
  48. /**************************************************************************
  49. * Module Parameters
  50. **************************************************************************/
  51. /* Radio Nr */
  52. static int radio_nr = -1;
  53. module_param(radio_nr, int, 0444);
  54. MODULE_PARM_DESC(radio_nr, "Radio Nr");
  55. /* USB timeout */
  56. static unsigned int usb_timeout = 500;
  57. module_param(usb_timeout, uint, 0644);
  58. MODULE_PARM_DESC(usb_timeout, "USB timeout (ms): *500*");
  59. /* RDS buffer blocks */
  60. static unsigned int rds_buf = 100;
  61. module_param(rds_buf, uint, 0444);
  62. MODULE_PARM_DESC(rds_buf, "RDS buffer entries: *100*");
  63. /* RDS maximum block errors */
  64. static unsigned short max_rds_errors = 1;
  65. /* 0 means 0 errors requiring correction */
  66. /* 1 means 1-2 errors requiring correction (used by original USBRadio.exe) */
  67. /* 2 means 3-5 errors requiring correction */
  68. /* 3 means 6+ errors or errors in checkword, correction not possible */
  69. module_param(max_rds_errors, ushort, 0644);
  70. MODULE_PARM_DESC(max_rds_errors, "RDS maximum block errors: *1*");
  71. /**************************************************************************
  72. * USB HID Reports
  73. **************************************************************************/
  74. /* Reports 1-16 give direct read/write access to the 16 Si470x registers */
  75. /* with the (REPORT_ID - 1) corresponding to the register address across USB */
  76. /* endpoint 0 using GET_REPORT and SET_REPORT */
  77. #define REGISTER_REPORT_SIZE (RADIO_REGISTER_SIZE + 1)
  78. #define REGISTER_REPORT(reg) ((reg) + 1)
  79. /* Report 17 gives direct read/write access to the entire Si470x register */
  80. /* map across endpoint 0 using GET_REPORT and SET_REPORT */
  81. #define ENTIRE_REPORT_SIZE (RADIO_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
  82. #define ENTIRE_REPORT 17
  83. /* Report 18 is used to send the lowest 6 Si470x registers up the HID */
  84. /* interrupt endpoint 1 to Windows every 20 milliseconds for status */
  85. #define RDS_REPORT_SIZE (RDS_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
  86. #define RDS_REPORT 18
  87. /* Report 19: LED state */
  88. #define LED_REPORT_SIZE 3
  89. #define LED_REPORT 19
  90. /* Report 19: stream */
  91. #define STREAM_REPORT_SIZE 3
  92. #define STREAM_REPORT 19
  93. /* Report 20: scratch */
  94. #define SCRATCH_PAGE_SIZE 63
  95. #define SCRATCH_REPORT_SIZE (SCRATCH_PAGE_SIZE + 1)
  96. #define SCRATCH_REPORT 20
  97. /* Reports 19-22: flash upgrade of the C8051F321 */
  98. #define WRITE_REPORT_SIZE 4
  99. #define WRITE_REPORT 19
  100. #define FLASH_REPORT_SIZE 64
  101. #define FLASH_REPORT 20
  102. #define CRC_REPORT_SIZE 3
  103. #define CRC_REPORT 21
  104. #define RESPONSE_REPORT_SIZE 2
  105. #define RESPONSE_REPORT 22
  106. /* Report 23: currently unused, but can accept 60 byte reports on the HID */
  107. /* interrupt out endpoint 2 every 1 millisecond */
  108. #define UNUSED_REPORT 23
  109. #define MAX_REPORT_SIZE 64
  110. /**************************************************************************
  111. * Software/Hardware Versions from Scratch Page
  112. **************************************************************************/
  113. #define RADIO_HW_VERSION 1
  114. /**************************************************************************
  115. * LED State Definitions
  116. **************************************************************************/
  117. #define LED_COMMAND 0x35
  118. #define NO_CHANGE_LED 0x00
  119. #define ALL_COLOR_LED 0x01 /* streaming state */
  120. #define BLINK_GREEN_LED 0x02 /* connect state */
  121. #define BLINK_RED_LED 0x04
  122. #define BLINK_ORANGE_LED 0x10 /* disconnect state */
  123. #define SOLID_GREEN_LED 0x20 /* tuning/seeking state */
  124. #define SOLID_RED_LED 0x40 /* bootload state */
  125. #define SOLID_ORANGE_LED 0x80
  126. /**************************************************************************
  127. * Stream State Definitions
  128. **************************************************************************/
  129. #define STREAM_COMMAND 0x36
  130. #define STREAM_VIDPID 0x00
  131. #define STREAM_AUDIO 0xff
  132. /**************************************************************************
  133. * Bootloader / Flash Commands
  134. **************************************************************************/
  135. /* unique id sent to bootloader and required to put into a bootload state */
  136. #define UNIQUE_BL_ID 0x34
  137. /* mask for the flash data */
  138. #define FLASH_DATA_MASK 0x55
  139. /* bootloader commands */
  140. #define GET_SW_VERSION_COMMAND 0x00
  141. #define SET_PAGE_COMMAND 0x01
  142. #define ERASE_PAGE_COMMAND 0x02
  143. #define WRITE_PAGE_COMMAND 0x03
  144. #define CRC_ON_PAGE_COMMAND 0x04
  145. #define READ_FLASH_BYTE_COMMAND 0x05
  146. #define RESET_DEVICE_COMMAND 0x06
  147. #define GET_HW_VERSION_COMMAND 0x07
  148. #define BLANK 0xff
  149. /* bootloader command responses */
  150. #define COMMAND_OK 0x01
  151. #define COMMAND_FAILED 0x02
  152. #define COMMAND_PENDING 0x03
  153. /**************************************************************************
  154. * General Driver Functions - REGISTER_REPORTs
  155. **************************************************************************/
  156. /*
  157. * si470x_get_report - receive a HID report
  158. */
  159. static int si470x_get_report(struct si470x_device *radio, void *buf, int size)
  160. {
  161. unsigned char *report = buf;
  162. int retval;
  163. retval = usb_control_msg(radio->usbdev,
  164. usb_rcvctrlpipe(radio->usbdev, 0),
  165. HID_REQ_GET_REPORT,
  166. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
  167. report[0], 2,
  168. buf, size, usb_timeout);
  169. if (retval < 0)
  170. dev_warn(&radio->intf->dev,
  171. "si470x_get_report: usb_control_msg returned %d\n",
  172. retval);
  173. return retval;
  174. }
  175. /*
  176. * si470x_set_report - send a HID report
  177. */
  178. static int si470x_set_report(struct si470x_device *radio, void *buf, int size)
  179. {
  180. unsigned char *report = buf;
  181. int retval;
  182. retval = usb_control_msg(radio->usbdev,
  183. usb_sndctrlpipe(radio->usbdev, 0),
  184. HID_REQ_SET_REPORT,
  185. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
  186. report[0], 2,
  187. buf, size, usb_timeout);
  188. if (retval < 0)
  189. dev_warn(&radio->intf->dev,
  190. "si470x_set_report: usb_control_msg returned %d\n",
  191. retval);
  192. return retval;
  193. }
  194. /*
  195. * si470x_get_register - read register
  196. */
  197. static int si470x_get_register(struct si470x_device *radio, int regnr)
  198. {
  199. int retval;
  200. radio->usb_buf[0] = REGISTER_REPORT(regnr);
  201. retval = si470x_get_report(radio, radio->usb_buf, REGISTER_REPORT_SIZE);
  202. if (retval >= 0)
  203. radio->registers[regnr] = get_unaligned_be16(&radio->usb_buf[1]);
  204. return (retval < 0) ? -EINVAL : 0;
  205. }
  206. /*
  207. * si470x_set_register - write register
  208. */
  209. static int si470x_set_register(struct si470x_device *radio, int regnr)
  210. {
  211. int retval;
  212. radio->usb_buf[0] = REGISTER_REPORT(regnr);
  213. put_unaligned_be16(radio->registers[regnr], &radio->usb_buf[1]);
  214. retval = si470x_set_report(radio, radio->usb_buf, REGISTER_REPORT_SIZE);
  215. return (retval < 0) ? -EINVAL : 0;
  216. }
  217. /**************************************************************************
  218. * General Driver Functions - ENTIRE_REPORT
  219. **************************************************************************/
  220. /*
  221. * si470x_get_all_registers - read entire registers
  222. */
  223. static int si470x_get_all_registers(struct si470x_device *radio)
  224. {
  225. int retval;
  226. unsigned char regnr;
  227. radio->usb_buf[0] = ENTIRE_REPORT;
  228. retval = si470x_get_report(radio, radio->usb_buf, ENTIRE_REPORT_SIZE);
  229. if (retval >= 0)
  230. for (regnr = 0; regnr < RADIO_REGISTER_NUM; regnr++)
  231. radio->registers[regnr] = get_unaligned_be16(
  232. &radio->usb_buf[regnr * RADIO_REGISTER_SIZE + 1]);
  233. return (retval < 0) ? -EINVAL : 0;
  234. }
  235. /**************************************************************************
  236. * General Driver Functions - LED_REPORT
  237. **************************************************************************/
  238. /*
  239. * si470x_set_led_state - sets the led state
  240. */
  241. static int si470x_set_led_state(struct si470x_device *radio,
  242. unsigned char led_state)
  243. {
  244. int retval;
  245. radio->usb_buf[0] = LED_REPORT;
  246. radio->usb_buf[1] = LED_COMMAND;
  247. radio->usb_buf[2] = led_state;
  248. retval = si470x_set_report(radio, radio->usb_buf, LED_REPORT_SIZE);
  249. return (retval < 0) ? -EINVAL : 0;
  250. }
  251. /**************************************************************************
  252. * General Driver Functions - SCRATCH_REPORT
  253. **************************************************************************/
  254. /*
  255. * si470x_get_scratch_versions - gets the scratch page and version infos
  256. */
  257. static int si470x_get_scratch_page_versions(struct si470x_device *radio)
  258. {
  259. int retval;
  260. radio->usb_buf[0] = SCRATCH_REPORT;
  261. retval = si470x_get_report(radio, radio->usb_buf, SCRATCH_REPORT_SIZE);
  262. if (retval < 0)
  263. dev_warn(&radio->intf->dev, "si470x_get_scratch: si470x_get_report returned %d\n",
  264. retval);
  265. else {
  266. radio->software_version = radio->usb_buf[1];
  267. radio->hardware_version = radio->usb_buf[2];
  268. }
  269. return (retval < 0) ? -EINVAL : 0;
  270. }
  271. /**************************************************************************
  272. * RDS Driver Functions
  273. **************************************************************************/
  274. /*
  275. * si470x_int_in_callback - rds callback and processing function
  276. *
  277. * TODO: do we need to use mutex locks in some sections?
  278. */
  279. static void si470x_int_in_callback(struct urb *urb)
  280. {
  281. struct si470x_device *radio = urb->context;
  282. int retval;
  283. unsigned char regnr;
  284. unsigned char blocknum;
  285. unsigned short bler; /* rds block errors */
  286. unsigned short rds;
  287. unsigned char tmpbuf[3];
  288. if (urb->status) {
  289. if (urb->status == -ENOENT ||
  290. urb->status == -ECONNRESET ||
  291. urb->status == -ESHUTDOWN) {
  292. return;
  293. } else {
  294. dev_warn(&radio->intf->dev,
  295. "non-zero urb status (%d)\n", urb->status);
  296. goto resubmit; /* Maybe we can recover. */
  297. }
  298. }
  299. /* Sometimes the device returns len 0 packets */
  300. if (urb->actual_length != RDS_REPORT_SIZE)
  301. goto resubmit;
  302. radio->registers[STATUSRSSI] =
  303. get_unaligned_be16(&radio->int_in_buffer[1]);
  304. if (radio->registers[STATUSRSSI] & STATUSRSSI_STC)
  305. complete(&radio->completion);
  306. if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS)) {
  307. /* Update RDS registers with URB data */
  308. for (regnr = 1; regnr < RDS_REGISTER_NUM; regnr++)
  309. radio->registers[STATUSRSSI + regnr] =
  310. get_unaligned_be16(&radio->int_in_buffer[
  311. regnr * RADIO_REGISTER_SIZE + 1]);
  312. /* get rds blocks */
  313. if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0) {
  314. /* No RDS group ready, better luck next time */
  315. goto resubmit;
  316. }
  317. if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSS) == 0) {
  318. /* RDS decoder not synchronized */
  319. goto resubmit;
  320. }
  321. for (blocknum = 0; blocknum < 4; blocknum++) {
  322. switch (blocknum) {
  323. default:
  324. bler = (radio->registers[STATUSRSSI] &
  325. STATUSRSSI_BLERA) >> 9;
  326. rds = radio->registers[RDSA];
  327. break;
  328. case 1:
  329. bler = (radio->registers[READCHAN] &
  330. READCHAN_BLERB) >> 14;
  331. rds = radio->registers[RDSB];
  332. break;
  333. case 2:
  334. bler = (radio->registers[READCHAN] &
  335. READCHAN_BLERC) >> 12;
  336. rds = radio->registers[RDSC];
  337. break;
  338. case 3:
  339. bler = (radio->registers[READCHAN] &
  340. READCHAN_BLERD) >> 10;
  341. rds = radio->registers[RDSD];
  342. break;
  343. }
  344. /* Fill the V4L2 RDS buffer */
  345. put_unaligned_le16(rds, &tmpbuf);
  346. tmpbuf[2] = blocknum; /* offset name */
  347. tmpbuf[2] |= blocknum << 3; /* received offset */
  348. if (bler > max_rds_errors)
  349. tmpbuf[2] |= 0x80; /* uncorrectable errors */
  350. else if (bler > 0)
  351. tmpbuf[2] |= 0x40; /* corrected error(s) */
  352. /* copy RDS block to internal buffer */
  353. memcpy(&radio->buffer[radio->wr_index], &tmpbuf, 3);
  354. radio->wr_index += 3;
  355. /* wrap write pointer */
  356. if (radio->wr_index >= radio->buf_size)
  357. radio->wr_index = 0;
  358. /* check for overflow */
  359. if (radio->wr_index == radio->rd_index) {
  360. /* increment and wrap read pointer */
  361. radio->rd_index += 3;
  362. if (radio->rd_index >= radio->buf_size)
  363. radio->rd_index = 0;
  364. }
  365. }
  366. if (radio->wr_index != radio->rd_index)
  367. wake_up_interruptible(&radio->read_queue);
  368. }
  369. resubmit:
  370. /* Resubmit if we're still running. */
  371. if (radio->int_in_running && radio->usbdev) {
  372. retval = usb_submit_urb(radio->int_in_urb, GFP_ATOMIC);
  373. if (retval) {
  374. dev_warn(&radio->intf->dev,
  375. "resubmitting urb failed (%d)", retval);
  376. radio->int_in_running = 0;
  377. }
  378. }
  379. radio->status_rssi_auto_update = radio->int_in_running;
  380. }
  381. static int si470x_fops_open(struct file *file)
  382. {
  383. return v4l2_fh_open(file);
  384. }
  385. static int si470x_fops_release(struct file *file)
  386. {
  387. return v4l2_fh_release(file);
  388. }
  389. static void si470x_usb_release(struct v4l2_device *v4l2_dev)
  390. {
  391. struct si470x_device *radio =
  392. container_of(v4l2_dev, struct si470x_device, v4l2_dev);
  393. usb_free_urb(radio->int_in_urb);
  394. v4l2_ctrl_handler_free(&radio->hdl);
  395. v4l2_device_unregister(&radio->v4l2_dev);
  396. kfree(radio->int_in_buffer);
  397. kfree(radio->buffer);
  398. kfree(radio->usb_buf);
  399. kfree(radio);
  400. }
  401. /**************************************************************************
  402. * Video4Linux Interface
  403. **************************************************************************/
  404. /*
  405. * si470x_vidioc_querycap - query device capabilities
  406. */
  407. static int si470x_vidioc_querycap(struct file *file, void *priv,
  408. struct v4l2_capability *capability)
  409. {
  410. struct si470x_device *radio = video_drvdata(file);
  411. strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver));
  412. strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
  413. usb_make_path(radio->usbdev, capability->bus_info,
  414. sizeof(capability->bus_info));
  415. capability->device_caps = V4L2_CAP_HW_FREQ_SEEK | V4L2_CAP_READWRITE |
  416. V4L2_CAP_TUNER | V4L2_CAP_RADIO | V4L2_CAP_RDS_CAPTURE;
  417. capability->capabilities = capability->device_caps | V4L2_CAP_DEVICE_CAPS;
  418. return 0;
  419. }
  420. static int si470x_start_usb(struct si470x_device *radio)
  421. {
  422. int retval;
  423. /* initialize interrupt urb */
  424. usb_fill_int_urb(radio->int_in_urb, radio->usbdev,
  425. usb_rcvintpipe(radio->usbdev,
  426. radio->int_in_endpoint->bEndpointAddress),
  427. radio->int_in_buffer,
  428. le16_to_cpu(radio->int_in_endpoint->wMaxPacketSize),
  429. si470x_int_in_callback,
  430. radio,
  431. radio->int_in_endpoint->bInterval);
  432. radio->int_in_running = 1;
  433. mb();
  434. retval = usb_submit_urb(radio->int_in_urb, GFP_KERNEL);
  435. if (retval) {
  436. dev_info(&radio->intf->dev,
  437. "submitting int urb failed (%d)\n", retval);
  438. radio->int_in_running = 0;
  439. }
  440. radio->status_rssi_auto_update = radio->int_in_running;
  441. /* start radio */
  442. retval = si470x_start(radio);
  443. if (retval < 0)
  444. return retval;
  445. v4l2_ctrl_handler_setup(&radio->hdl);
  446. return retval;
  447. }
  448. /**************************************************************************
  449. * USB Interface
  450. **************************************************************************/
  451. /*
  452. * si470x_usb_driver_probe - probe for the device
  453. */
  454. static int si470x_usb_driver_probe(struct usb_interface *intf,
  455. const struct usb_device_id *id)
  456. {
  457. struct si470x_device *radio;
  458. struct usb_host_interface *iface_desc;
  459. struct usb_endpoint_descriptor *endpoint;
  460. int i, int_end_size, retval;
  461. unsigned char version_warning = 0;
  462. /* private data allocation and initialization */
  463. radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL);
  464. if (!radio) {
  465. retval = -ENOMEM;
  466. goto err_initial;
  467. }
  468. radio->usb_buf = kmalloc(MAX_REPORT_SIZE, GFP_KERNEL);
  469. if (radio->usb_buf == NULL) {
  470. retval = -ENOMEM;
  471. goto err_radio;
  472. }
  473. radio->usbdev = interface_to_usbdev(intf);
  474. radio->intf = intf;
  475. radio->band = 1; /* Default to 76 - 108 MHz */
  476. mutex_init(&radio->lock);
  477. init_completion(&radio->completion);
  478. radio->get_register = si470x_get_register;
  479. radio->set_register = si470x_set_register;
  480. radio->fops_open = si470x_fops_open;
  481. radio->fops_release = si470x_fops_release;
  482. radio->vidioc_querycap = si470x_vidioc_querycap;
  483. iface_desc = intf->cur_altsetting;
  484. /* Set up interrupt endpoint information. */
  485. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  486. endpoint = &iface_desc->endpoint[i].desc;
  487. if (usb_endpoint_is_int_in(endpoint))
  488. radio->int_in_endpoint = endpoint;
  489. }
  490. if (!radio->int_in_endpoint) {
  491. dev_info(&intf->dev, "could not find interrupt in endpoint\n");
  492. retval = -EIO;
  493. goto err_usbbuf;
  494. }
  495. int_end_size = le16_to_cpu(radio->int_in_endpoint->wMaxPacketSize);
  496. radio->int_in_buffer = kmalloc(int_end_size, GFP_KERNEL);
  497. if (!radio->int_in_buffer) {
  498. dev_info(&intf->dev, "could not allocate int_in_buffer");
  499. retval = -ENOMEM;
  500. goto err_usbbuf;
  501. }
  502. radio->int_in_urb = usb_alloc_urb(0, GFP_KERNEL);
  503. if (!radio->int_in_urb) {
  504. retval = -ENOMEM;
  505. goto err_intbuffer;
  506. }
  507. radio->v4l2_dev.release = si470x_usb_release;
  508. /*
  509. * The si470x SiLabs reference design uses the same USB IDs as
  510. * 'Thanko's Raremono' si4734 based receiver. So check here which we
  511. * have: attempt to read the device ID from the si470x: the lower 12
  512. * bits should be 0x0242 for the si470x.
  513. *
  514. * We use this check to determine which device we are dealing with.
  515. */
  516. if (id->idVendor == 0x10c4 && id->idProduct == 0x818a) {
  517. retval = usb_control_msg(radio->usbdev,
  518. usb_rcvctrlpipe(radio->usbdev, 0),
  519. HID_REQ_GET_REPORT,
  520. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
  521. 1, 2,
  522. radio->usb_buf, 3, 500);
  523. if (retval != 3 ||
  524. (get_unaligned_be16(&radio->usb_buf[1]) & 0xfff) != 0x0242) {
  525. dev_info(&intf->dev, "this is not a si470x device.\n");
  526. retval = -ENODEV;
  527. goto err_urb;
  528. }
  529. }
  530. retval = v4l2_device_register(&intf->dev, &radio->v4l2_dev);
  531. if (retval < 0) {
  532. dev_err(&intf->dev, "couldn't register v4l2_device\n");
  533. goto err_urb;
  534. }
  535. v4l2_ctrl_handler_init(&radio->hdl, 2);
  536. v4l2_ctrl_new_std(&radio->hdl, &si470x_ctrl_ops,
  537. V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
  538. v4l2_ctrl_new_std(&radio->hdl, &si470x_ctrl_ops,
  539. V4L2_CID_AUDIO_VOLUME, 0, 15, 1, 15);
  540. if (radio->hdl.error) {
  541. retval = radio->hdl.error;
  542. dev_err(&intf->dev, "couldn't register control\n");
  543. goto err_dev;
  544. }
  545. radio->videodev = si470x_viddev_template;
  546. radio->videodev.ctrl_handler = &radio->hdl;
  547. radio->videodev.lock = &radio->lock;
  548. radio->videodev.v4l2_dev = &radio->v4l2_dev;
  549. radio->videodev.release = video_device_release_empty;
  550. video_set_drvdata(&radio->videodev, radio);
  551. /* get device and chip versions */
  552. if (si470x_get_all_registers(radio) < 0) {
  553. retval = -EIO;
  554. goto err_ctrl;
  555. }
  556. dev_info(&intf->dev, "DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",
  557. radio->registers[DEVICEID], radio->registers[SI_CHIPID]);
  558. if ((radio->registers[SI_CHIPID] & SI_CHIPID_FIRMWARE) < RADIO_FW_VERSION) {
  559. dev_warn(&intf->dev,
  560. "This driver is known to work with firmware version %hu,\n",
  561. RADIO_FW_VERSION);
  562. dev_warn(&intf->dev,
  563. "but the device has firmware version %hu.\n",
  564. radio->registers[SI_CHIPID] & SI_CHIPID_FIRMWARE);
  565. version_warning = 1;
  566. }
  567. /* get software and hardware versions */
  568. if (si470x_get_scratch_page_versions(radio) < 0) {
  569. retval = -EIO;
  570. goto err_ctrl;
  571. }
  572. dev_info(&intf->dev, "software version %d, hardware version %d\n",
  573. radio->software_version, radio->hardware_version);
  574. if (radio->hardware_version < RADIO_HW_VERSION) {
  575. dev_warn(&intf->dev,
  576. "This driver is known to work with hardware version %hu,\n",
  577. RADIO_HW_VERSION);
  578. dev_warn(&intf->dev,
  579. "but the device has hardware version %hu.\n",
  580. radio->hardware_version);
  581. version_warning = 1;
  582. }
  583. /* give out version warning */
  584. if (version_warning == 1) {
  585. dev_warn(&intf->dev,
  586. "If you have some trouble using this driver,\n");
  587. dev_warn(&intf->dev,
  588. "please report to V4L ML at linux-media@vger.kernel.org\n");
  589. }
  590. /* set led to connect state */
  591. si470x_set_led_state(radio, BLINK_GREEN_LED);
  592. /* rds buffer allocation */
  593. radio->buf_size = rds_buf * 3;
  594. radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
  595. if (!radio->buffer) {
  596. retval = -EIO;
  597. goto err_ctrl;
  598. }
  599. /* rds buffer configuration */
  600. radio->wr_index = 0;
  601. radio->rd_index = 0;
  602. init_waitqueue_head(&radio->read_queue);
  603. usb_set_intfdata(intf, radio);
  604. /* start radio */
  605. retval = si470x_start_usb(radio);
  606. if (retval < 0)
  607. goto err_buf;
  608. /* set initial frequency */
  609. si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
  610. /* register video device */
  611. retval = video_register_device(&radio->videodev, VFL_TYPE_RADIO,
  612. radio_nr);
  613. if (retval) {
  614. dev_err(&intf->dev, "Could not register video device\n");
  615. goto err_all;
  616. }
  617. return 0;
  618. err_all:
  619. usb_kill_urb(radio->int_in_urb);
  620. err_buf:
  621. kfree(radio->buffer);
  622. err_ctrl:
  623. v4l2_ctrl_handler_free(&radio->hdl);
  624. err_dev:
  625. v4l2_device_unregister(&radio->v4l2_dev);
  626. err_urb:
  627. usb_free_urb(radio->int_in_urb);
  628. err_intbuffer:
  629. kfree(radio->int_in_buffer);
  630. err_usbbuf:
  631. kfree(radio->usb_buf);
  632. err_radio:
  633. kfree(radio);
  634. err_initial:
  635. return retval;
  636. }
  637. /*
  638. * si470x_usb_driver_suspend - suspend the device
  639. */
  640. static int si470x_usb_driver_suspend(struct usb_interface *intf,
  641. pm_message_t message)
  642. {
  643. struct si470x_device *radio = usb_get_intfdata(intf);
  644. dev_info(&intf->dev, "suspending now...\n");
  645. /* shutdown interrupt handler */
  646. if (radio->int_in_running) {
  647. radio->int_in_running = 0;
  648. if (radio->int_in_urb)
  649. usb_kill_urb(radio->int_in_urb);
  650. }
  651. /* cancel read processes */
  652. wake_up_interruptible(&radio->read_queue);
  653. /* stop radio */
  654. si470x_stop(radio);
  655. return 0;
  656. }
  657. /*
  658. * si470x_usb_driver_resume - resume the device
  659. */
  660. static int si470x_usb_driver_resume(struct usb_interface *intf)
  661. {
  662. struct si470x_device *radio = usb_get_intfdata(intf);
  663. int ret;
  664. dev_info(&intf->dev, "resuming now...\n");
  665. /* start radio */
  666. ret = si470x_start_usb(radio);
  667. if (ret == 0)
  668. v4l2_ctrl_handler_setup(&radio->hdl);
  669. return ret;
  670. }
  671. /*
  672. * si470x_usb_driver_disconnect - disconnect the device
  673. */
  674. static void si470x_usb_driver_disconnect(struct usb_interface *intf)
  675. {
  676. struct si470x_device *radio = usb_get_intfdata(intf);
  677. mutex_lock(&radio->lock);
  678. v4l2_device_disconnect(&radio->v4l2_dev);
  679. video_unregister_device(&radio->videodev);
  680. usb_kill_urb(radio->int_in_urb);
  681. usb_set_intfdata(intf, NULL);
  682. mutex_unlock(&radio->lock);
  683. v4l2_device_put(&radio->v4l2_dev);
  684. }
  685. /*
  686. * si470x_usb_driver - usb driver interface
  687. *
  688. * A note on suspend/resume: this driver had only empty suspend/resume
  689. * functions, and when I tried to test suspend/resume it always disconnected
  690. * instead of resuming (using my ADS InstantFM stick). So I've decided to
  691. * remove these callbacks until someone else with better hardware can
  692. * implement and test this.
  693. */
  694. static struct usb_driver si470x_usb_driver = {
  695. .name = DRIVER_NAME,
  696. .probe = si470x_usb_driver_probe,
  697. .disconnect = si470x_usb_driver_disconnect,
  698. .suspend = si470x_usb_driver_suspend,
  699. .resume = si470x_usb_driver_resume,
  700. .reset_resume = si470x_usb_driver_resume,
  701. .id_table = si470x_usb_driver_id_table,
  702. };
  703. module_usb_driver(si470x_usb_driver);
  704. MODULE_LICENSE("GPL");
  705. MODULE_AUTHOR(DRIVER_AUTHOR);
  706. MODULE_DESCRIPTION(DRIVER_DESC);
  707. MODULE_VERSION(DRIVER_VERSION);