radio-si470x-usb.c 24 KB

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