radio-si470x-usb.c 23 KB

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