radio-si470x-usb.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  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_KERNEL_VERSION KERNEL_VERSION(1, 0, 10)
  29. #define DRIVER_CARD "Silicon Labs Si470x FM Radio Receiver"
  30. #define DRIVER_DESC "USB radio driver for Si470x FM Radio Receivers"
  31. #define DRIVER_VERSION "1.0.10"
  32. /* kernel includes */
  33. #include <linux/usb.h>
  34. #include <linux/hid.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. /* Terminating entry */
  47. { }
  48. };
  49. MODULE_DEVICE_TABLE(usb, si470x_usb_driver_id_table);
  50. /**************************************************************************
  51. * Module Parameters
  52. **************************************************************************/
  53. /* Radio Nr */
  54. static int radio_nr = -1;
  55. module_param(radio_nr, int, 0444);
  56. MODULE_PARM_DESC(radio_nr, "Radio Nr");
  57. /* USB timeout */
  58. static unsigned int usb_timeout = 500;
  59. module_param(usb_timeout, uint, 0644);
  60. MODULE_PARM_DESC(usb_timeout, "USB timeout (ms): *500*");
  61. /* RDS buffer blocks */
  62. static unsigned int rds_buf = 100;
  63. module_param(rds_buf, uint, 0444);
  64. MODULE_PARM_DESC(rds_buf, "RDS buffer entries: *100*");
  65. /* RDS maximum block errors */
  66. static unsigned short max_rds_errors = 1;
  67. /* 0 means 0 errors requiring correction */
  68. /* 1 means 1-2 errors requiring correction (used by original USBRadio.exe) */
  69. /* 2 means 3-5 errors requiring correction */
  70. /* 3 means 6+ errors or errors in checkword, correction not possible */
  71. module_param(max_rds_errors, ushort, 0644);
  72. MODULE_PARM_DESC(max_rds_errors, "RDS maximum block errors: *1*");
  73. /**************************************************************************
  74. * USB HID Reports
  75. **************************************************************************/
  76. /* Reports 1-16 give direct read/write access to the 16 Si470x registers */
  77. /* with the (REPORT_ID - 1) corresponding to the register address across USB */
  78. /* endpoint 0 using GET_REPORT and SET_REPORT */
  79. #define REGISTER_REPORT_SIZE (RADIO_REGISTER_SIZE + 1)
  80. #define REGISTER_REPORT(reg) ((reg) + 1)
  81. /* Report 17 gives direct read/write access to the entire Si470x register */
  82. /* map across endpoint 0 using GET_REPORT and SET_REPORT */
  83. #define ENTIRE_REPORT_SIZE (RADIO_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
  84. #define ENTIRE_REPORT 17
  85. /* Report 18 is used to send the lowest 6 Si470x registers up the HID */
  86. /* interrupt endpoint 1 to Windows every 20 milliseconds for status */
  87. #define RDS_REPORT_SIZE (RDS_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
  88. #define RDS_REPORT 18
  89. /* Report 19: LED state */
  90. #define LED_REPORT_SIZE 3
  91. #define LED_REPORT 19
  92. /* Report 19: stream */
  93. #define STREAM_REPORT_SIZE 3
  94. #define STREAM_REPORT 19
  95. /* Report 20: scratch */
  96. #define SCRATCH_PAGE_SIZE 63
  97. #define SCRATCH_REPORT_SIZE (SCRATCH_PAGE_SIZE + 1)
  98. #define SCRATCH_REPORT 20
  99. /* Reports 19-22: flash upgrade of the C8051F321 */
  100. #define WRITE_REPORT_SIZE 4
  101. #define WRITE_REPORT 19
  102. #define FLASH_REPORT_SIZE 64
  103. #define FLASH_REPORT 20
  104. #define CRC_REPORT_SIZE 3
  105. #define CRC_REPORT 21
  106. #define RESPONSE_REPORT_SIZE 2
  107. #define RESPONSE_REPORT 22
  108. /* Report 23: currently unused, but can accept 60 byte reports on the HID */
  109. /* interrupt out endpoint 2 every 1 millisecond */
  110. #define UNUSED_REPORT 23
  111. /**************************************************************************
  112. * Software/Hardware Versions
  113. **************************************************************************/
  114. #define RADIO_SW_VERSION_NOT_BOOTLOADABLE 6
  115. #define RADIO_SW_VERSION 7
  116. #define RADIO_SW_VERSION_CURRENT 15
  117. #define RADIO_HW_VERSION 1
  118. #define SCRATCH_PAGE_SW_VERSION 1
  119. #define SCRATCH_PAGE_HW_VERSION 2
  120. /**************************************************************************
  121. * LED State Definitions
  122. **************************************************************************/
  123. #define LED_COMMAND 0x35
  124. #define NO_CHANGE_LED 0x00
  125. #define ALL_COLOR_LED 0x01 /* streaming state */
  126. #define BLINK_GREEN_LED 0x02 /* connect state */
  127. #define BLINK_RED_LED 0x04
  128. #define BLINK_ORANGE_LED 0x10 /* disconnect state */
  129. #define SOLID_GREEN_LED 0x20 /* tuning/seeking state */
  130. #define SOLID_RED_LED 0x40 /* bootload state */
  131. #define SOLID_ORANGE_LED 0x80
  132. /**************************************************************************
  133. * Stream State Definitions
  134. **************************************************************************/
  135. #define STREAM_COMMAND 0x36
  136. #define STREAM_VIDPID 0x00
  137. #define STREAM_AUDIO 0xff
  138. /**************************************************************************
  139. * Bootloader / Flash Commands
  140. **************************************************************************/
  141. /* unique id sent to bootloader and required to put into a bootload state */
  142. #define UNIQUE_BL_ID 0x34
  143. /* mask for the flash data */
  144. #define FLASH_DATA_MASK 0x55
  145. /* bootloader commands */
  146. #define GET_SW_VERSION_COMMAND 0x00
  147. #define SET_PAGE_COMMAND 0x01
  148. #define ERASE_PAGE_COMMAND 0x02
  149. #define WRITE_PAGE_COMMAND 0x03
  150. #define CRC_ON_PAGE_COMMAND 0x04
  151. #define READ_FLASH_BYTE_COMMAND 0x05
  152. #define RESET_DEVICE_COMMAND 0x06
  153. #define GET_HW_VERSION_COMMAND 0x07
  154. #define BLANK 0xff
  155. /* bootloader command responses */
  156. #define COMMAND_OK 0x01
  157. #define COMMAND_FAILED 0x02
  158. #define COMMAND_PENDING 0x03
  159. /**************************************************************************
  160. * General Driver Functions - REGISTER_REPORTs
  161. **************************************************************************/
  162. /*
  163. * si470x_get_report - receive a HID report
  164. */
  165. static int si470x_get_report(struct si470x_device *radio, void *buf, int size)
  166. {
  167. unsigned char *report = (unsigned char *) buf;
  168. int retval;
  169. retval = usb_control_msg(radio->usbdev,
  170. usb_rcvctrlpipe(radio->usbdev, 0),
  171. HID_REQ_GET_REPORT,
  172. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
  173. report[0], 2,
  174. buf, size, usb_timeout);
  175. if (retval < 0)
  176. dev_warn(&radio->intf->dev,
  177. "si470x_get_report: usb_control_msg returned %d\n",
  178. retval);
  179. return retval;
  180. }
  181. /*
  182. * si470x_set_report - send a HID report
  183. */
  184. static int si470x_set_report(struct si470x_device *radio, void *buf, int size)
  185. {
  186. unsigned char *report = (unsigned char *) buf;
  187. int retval;
  188. retval = usb_control_msg(radio->usbdev,
  189. usb_sndctrlpipe(radio->usbdev, 0),
  190. HID_REQ_SET_REPORT,
  191. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
  192. report[0], 2,
  193. buf, size, usb_timeout);
  194. if (retval < 0)
  195. dev_warn(&radio->intf->dev,
  196. "si470x_set_report: usb_control_msg returned %d\n",
  197. retval);
  198. return retval;
  199. }
  200. /*
  201. * si470x_get_register - read register
  202. */
  203. int si470x_get_register(struct si470x_device *radio, int regnr)
  204. {
  205. unsigned char buf[REGISTER_REPORT_SIZE];
  206. int retval;
  207. buf[0] = REGISTER_REPORT(regnr);
  208. retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
  209. if (retval >= 0)
  210. radio->registers[regnr] = get_unaligned_be16(&buf[1]);
  211. return (retval < 0) ? -EINVAL : 0;
  212. }
  213. /*
  214. * si470x_set_register - write register
  215. */
  216. int si470x_set_register(struct si470x_device *radio, int regnr)
  217. {
  218. unsigned char buf[REGISTER_REPORT_SIZE];
  219. int retval;
  220. buf[0] = REGISTER_REPORT(regnr);
  221. put_unaligned_be16(radio->registers[regnr], &buf[1]);
  222. retval = si470x_set_report(radio, (void *) &buf, sizeof(buf));
  223. return (retval < 0) ? -EINVAL : 0;
  224. }
  225. /**************************************************************************
  226. * General Driver Functions - ENTIRE_REPORT
  227. **************************************************************************/
  228. /*
  229. * si470x_get_all_registers - read entire registers
  230. */
  231. static int si470x_get_all_registers(struct si470x_device *radio)
  232. {
  233. unsigned char buf[ENTIRE_REPORT_SIZE];
  234. int retval;
  235. unsigned char regnr;
  236. buf[0] = ENTIRE_REPORT;
  237. retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
  238. if (retval >= 0)
  239. for (regnr = 0; regnr < RADIO_REGISTER_NUM; regnr++)
  240. radio->registers[regnr] = get_unaligned_be16(
  241. &buf[regnr * RADIO_REGISTER_SIZE + 1]);
  242. return (retval < 0) ? -EINVAL : 0;
  243. }
  244. /**************************************************************************
  245. * General Driver Functions - LED_REPORT
  246. **************************************************************************/
  247. /*
  248. * si470x_set_led_state - sets the led state
  249. */
  250. static int si470x_set_led_state(struct si470x_device *radio,
  251. unsigned char led_state)
  252. {
  253. unsigned char buf[LED_REPORT_SIZE];
  254. int retval;
  255. buf[0] = LED_REPORT;
  256. buf[1] = LED_COMMAND;
  257. buf[2] = led_state;
  258. retval = si470x_set_report(radio, (void *) &buf, sizeof(buf));
  259. return (retval < 0) ? -EINVAL : 0;
  260. }
  261. /**************************************************************************
  262. * General Driver Functions - SCRATCH_REPORT
  263. **************************************************************************/
  264. /*
  265. * si470x_get_scratch_versions - gets the scratch page and version infos
  266. */
  267. static int si470x_get_scratch_page_versions(struct si470x_device *radio)
  268. {
  269. unsigned char buf[SCRATCH_REPORT_SIZE];
  270. int retval;
  271. buf[0] = SCRATCH_REPORT;
  272. retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
  273. if (retval < 0)
  274. dev_warn(&radio->intf->dev, "si470x_get_scratch: "
  275. "si470x_get_report returned %d\n", retval);
  276. else {
  277. radio->software_version = buf[1];
  278. radio->hardware_version = buf[2];
  279. }
  280. return (retval < 0) ? -EINVAL : 0;
  281. }
  282. /**************************************************************************
  283. * General Driver Functions - DISCONNECT_CHECK
  284. **************************************************************************/
  285. /*
  286. * si470x_disconnect_check - check whether radio disconnects
  287. */
  288. int si470x_disconnect_check(struct si470x_device *radio)
  289. {
  290. if (radio->disconnected)
  291. return -EIO;
  292. else
  293. return 0;
  294. }
  295. /**************************************************************************
  296. * RDS Driver Functions
  297. **************************************************************************/
  298. /*
  299. * si470x_int_in_callback - rds callback and processing function
  300. *
  301. * TODO: do we need to use mutex locks in some sections?
  302. */
  303. static void si470x_int_in_callback(struct urb *urb)
  304. {
  305. struct si470x_device *radio = urb->context;
  306. unsigned char buf[RDS_REPORT_SIZE];
  307. int retval;
  308. unsigned char regnr;
  309. unsigned char blocknum;
  310. unsigned short bler; /* rds block errors */
  311. unsigned short rds;
  312. unsigned char tmpbuf[3];
  313. if (urb->status) {
  314. if (urb->status == -ENOENT ||
  315. urb->status == -ECONNRESET ||
  316. urb->status == -ESHUTDOWN) {
  317. return;
  318. } else {
  319. dev_warn(&radio->intf->dev,
  320. "non-zero urb status (%d)\n", urb->status);
  321. goto resubmit; /* Maybe we can recover. */
  322. }
  323. }
  324. /* safety checks */
  325. if (radio->disconnected)
  326. return;
  327. if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
  328. goto resubmit;
  329. if (urb->actual_length > 0) {
  330. /* Update RDS registers with URB data */
  331. buf[0] = RDS_REPORT;
  332. for (regnr = 0; regnr < RDS_REGISTER_NUM; regnr++)
  333. radio->registers[STATUSRSSI + regnr] =
  334. get_unaligned_be16(&radio->int_in_buffer[
  335. regnr * RADIO_REGISTER_SIZE + 1]);
  336. /* get rds blocks */
  337. if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0) {
  338. /* No RDS group ready, better luck next time */
  339. goto resubmit;
  340. }
  341. if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSS) == 0) {
  342. /* RDS decoder not synchronized */
  343. goto resubmit;
  344. }
  345. for (blocknum = 0; blocknum < 4; blocknum++) {
  346. switch (blocknum) {
  347. default:
  348. bler = (radio->registers[STATUSRSSI] &
  349. STATUSRSSI_BLERA) >> 9;
  350. rds = radio->registers[RDSA];
  351. break;
  352. case 1:
  353. bler = (radio->registers[READCHAN] &
  354. READCHAN_BLERB) >> 14;
  355. rds = radio->registers[RDSB];
  356. break;
  357. case 2:
  358. bler = (radio->registers[READCHAN] &
  359. READCHAN_BLERC) >> 12;
  360. rds = radio->registers[RDSC];
  361. break;
  362. case 3:
  363. bler = (radio->registers[READCHAN] &
  364. READCHAN_BLERD) >> 10;
  365. rds = radio->registers[RDSD];
  366. break;
  367. };
  368. /* Fill the V4L2 RDS buffer */
  369. put_unaligned_le16(rds, &tmpbuf);
  370. tmpbuf[2] = blocknum; /* offset name */
  371. tmpbuf[2] |= blocknum << 3; /* received offset */
  372. if (bler > max_rds_errors)
  373. tmpbuf[2] |= 0x80; /* uncorrectable errors */
  374. else if (bler > 0)
  375. tmpbuf[2] |= 0x40; /* corrected error(s) */
  376. /* copy RDS block to internal buffer */
  377. memcpy(&radio->buffer[radio->wr_index], &tmpbuf, 3);
  378. radio->wr_index += 3;
  379. /* wrap write pointer */
  380. if (radio->wr_index >= radio->buf_size)
  381. radio->wr_index = 0;
  382. /* check for overflow */
  383. if (radio->wr_index == radio->rd_index) {
  384. /* increment and wrap read pointer */
  385. radio->rd_index += 3;
  386. if (radio->rd_index >= radio->buf_size)
  387. radio->rd_index = 0;
  388. }
  389. }
  390. if (radio->wr_index != radio->rd_index)
  391. wake_up_interruptible(&radio->read_queue);
  392. }
  393. resubmit:
  394. /* Resubmit if we're still running. */
  395. if (radio->int_in_running && radio->usbdev) {
  396. retval = usb_submit_urb(radio->int_in_urb, GFP_ATOMIC);
  397. if (retval) {
  398. dev_warn(&radio->intf->dev,
  399. "resubmitting urb failed (%d)", retval);
  400. radio->int_in_running = 0;
  401. }
  402. }
  403. }
  404. /**************************************************************************
  405. * File Operations Interface
  406. **************************************************************************/
  407. /*
  408. * si470x_fops_read - read RDS data
  409. */
  410. static ssize_t si470x_fops_read(struct file *file, char __user *buf,
  411. size_t count, loff_t *ppos)
  412. {
  413. struct si470x_device *radio = video_drvdata(file);
  414. int retval = 0;
  415. unsigned int block_count = 0;
  416. /* switch on rds reception */
  417. if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
  418. si470x_rds_on(radio);
  419. /* block if no new data available */
  420. while (radio->wr_index == radio->rd_index) {
  421. if (file->f_flags & O_NONBLOCK) {
  422. retval = -EWOULDBLOCK;
  423. goto done;
  424. }
  425. if (wait_event_interruptible(radio->read_queue,
  426. radio->wr_index != radio->rd_index) < 0) {
  427. retval = -EINTR;
  428. goto done;
  429. }
  430. }
  431. /* calculate block count from byte count */
  432. count /= 3;
  433. /* copy RDS block out of internal buffer and to user buffer */
  434. mutex_lock(&radio->lock);
  435. while (block_count < count) {
  436. if (radio->rd_index == radio->wr_index)
  437. break;
  438. /* always transfer rds complete blocks */
  439. if (copy_to_user(buf, &radio->buffer[radio->rd_index], 3))
  440. /* retval = -EFAULT; */
  441. break;
  442. /* increment and wrap read pointer */
  443. radio->rd_index += 3;
  444. if (radio->rd_index >= radio->buf_size)
  445. radio->rd_index = 0;
  446. /* increment counters */
  447. block_count++;
  448. buf += 3;
  449. retval += 3;
  450. }
  451. mutex_unlock(&radio->lock);
  452. done:
  453. return retval;
  454. }
  455. /*
  456. * si470x_fops_poll - poll RDS data
  457. */
  458. static unsigned int si470x_fops_poll(struct file *file,
  459. struct poll_table_struct *pts)
  460. {
  461. struct si470x_device *radio = video_drvdata(file);
  462. int retval = 0;
  463. /* switch on rds reception */
  464. if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
  465. si470x_rds_on(radio);
  466. poll_wait(file, &radio->read_queue, pts);
  467. if (radio->rd_index != radio->wr_index)
  468. retval = POLLIN | POLLRDNORM;
  469. return retval;
  470. }
  471. /*
  472. * si470x_fops_open - file open
  473. */
  474. static int si470x_fops_open(struct file *file)
  475. {
  476. struct si470x_device *radio = video_drvdata(file);
  477. int retval;
  478. lock_kernel();
  479. radio->users++;
  480. retval = usb_autopm_get_interface(radio->intf);
  481. if (retval < 0) {
  482. radio->users--;
  483. retval = -EIO;
  484. goto done;
  485. }
  486. if (radio->users == 1) {
  487. /* start radio */
  488. retval = si470x_start(radio);
  489. if (retval < 0) {
  490. usb_autopm_put_interface(radio->intf);
  491. goto done;
  492. }
  493. /* initialize interrupt urb */
  494. usb_fill_int_urb(radio->int_in_urb, radio->usbdev,
  495. usb_rcvintpipe(radio->usbdev,
  496. radio->int_in_endpoint->bEndpointAddress),
  497. radio->int_in_buffer,
  498. le16_to_cpu(radio->int_in_endpoint->wMaxPacketSize),
  499. si470x_int_in_callback,
  500. radio,
  501. radio->int_in_endpoint->bInterval);
  502. radio->int_in_running = 1;
  503. mb();
  504. retval = usb_submit_urb(radio->int_in_urb, GFP_KERNEL);
  505. if (retval) {
  506. dev_info(&radio->intf->dev,
  507. "submitting int urb failed (%d)\n", retval);
  508. radio->int_in_running = 0;
  509. usb_autopm_put_interface(radio->intf);
  510. }
  511. }
  512. done:
  513. unlock_kernel();
  514. return retval;
  515. }
  516. /*
  517. * si470x_fops_release - file release
  518. */
  519. static int si470x_fops_release(struct file *file)
  520. {
  521. struct si470x_device *radio = video_drvdata(file);
  522. int retval = 0;
  523. /* safety check */
  524. if (!radio) {
  525. retval = -ENODEV;
  526. goto done;
  527. }
  528. mutex_lock(&radio->disconnect_lock);
  529. radio->users--;
  530. if (radio->users == 0) {
  531. /* shutdown interrupt handler */
  532. if (radio->int_in_running) {
  533. radio->int_in_running = 0;
  534. if (radio->int_in_urb)
  535. usb_kill_urb(radio->int_in_urb);
  536. }
  537. if (radio->disconnected) {
  538. video_unregister_device(radio->videodev);
  539. kfree(radio->int_in_buffer);
  540. kfree(radio->buffer);
  541. kfree(radio);
  542. goto unlock;
  543. }
  544. /* cancel read processes */
  545. wake_up_interruptible(&radio->read_queue);
  546. /* stop radio */
  547. retval = si470x_stop(radio);
  548. usb_autopm_put_interface(radio->intf);
  549. }
  550. unlock:
  551. mutex_unlock(&radio->disconnect_lock);
  552. done:
  553. return retval;
  554. }
  555. /*
  556. * si470x_fops - file operations interface
  557. */
  558. const struct v4l2_file_operations si470x_fops = {
  559. .owner = THIS_MODULE,
  560. .read = si470x_fops_read,
  561. .poll = si470x_fops_poll,
  562. .ioctl = video_ioctl2,
  563. .open = si470x_fops_open,
  564. .release = si470x_fops_release,
  565. };
  566. /**************************************************************************
  567. * Video4Linux Interface
  568. **************************************************************************/
  569. /*
  570. * si470x_vidioc_querycap - query device capabilities
  571. */
  572. int si470x_vidioc_querycap(struct file *file, void *priv,
  573. struct v4l2_capability *capability)
  574. {
  575. struct si470x_device *radio = video_drvdata(file);
  576. strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver));
  577. strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
  578. usb_make_path(radio->usbdev, capability->bus_info,
  579. sizeof(capability->bus_info));
  580. capability->version = DRIVER_KERNEL_VERSION;
  581. capability->capabilities = V4L2_CAP_HW_FREQ_SEEK |
  582. V4L2_CAP_TUNER | V4L2_CAP_RADIO | V4L2_CAP_RDS_CAPTURE;
  583. return 0;
  584. }
  585. /**************************************************************************
  586. * USB Interface
  587. **************************************************************************/
  588. /*
  589. * si470x_usb_driver_probe - probe for the device
  590. */
  591. static int si470x_usb_driver_probe(struct usb_interface *intf,
  592. const struct usb_device_id *id)
  593. {
  594. struct si470x_device *radio;
  595. struct usb_host_interface *iface_desc;
  596. struct usb_endpoint_descriptor *endpoint;
  597. int i, int_end_size, retval = 0;
  598. /* private data allocation and initialization */
  599. radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL);
  600. if (!radio) {
  601. retval = -ENOMEM;
  602. goto err_initial;
  603. }
  604. radio->users = 0;
  605. radio->disconnected = 0;
  606. radio->usbdev = interface_to_usbdev(intf);
  607. radio->intf = intf;
  608. mutex_init(&radio->disconnect_lock);
  609. mutex_init(&radio->lock);
  610. iface_desc = intf->cur_altsetting;
  611. /* Set up interrupt endpoint information. */
  612. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  613. endpoint = &iface_desc->endpoint[i].desc;
  614. if (((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
  615. USB_DIR_IN) && ((endpoint->bmAttributes &
  616. USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT))
  617. radio->int_in_endpoint = endpoint;
  618. }
  619. if (!radio->int_in_endpoint) {
  620. dev_info(&intf->dev, "could not find interrupt in endpoint\n");
  621. retval = -EIO;
  622. goto err_radio;
  623. }
  624. int_end_size = le16_to_cpu(radio->int_in_endpoint->wMaxPacketSize);
  625. radio->int_in_buffer = kmalloc(int_end_size, GFP_KERNEL);
  626. if (!radio->int_in_buffer) {
  627. dev_info(&intf->dev, "could not allocate int_in_buffer");
  628. retval = -ENOMEM;
  629. goto err_radio;
  630. }
  631. radio->int_in_urb = usb_alloc_urb(0, GFP_KERNEL);
  632. if (!radio->int_in_urb) {
  633. dev_info(&intf->dev, "could not allocate int_in_urb");
  634. retval = -ENOMEM;
  635. goto err_intbuffer;
  636. }
  637. /* video device allocation and initialization */
  638. radio->videodev = video_device_alloc();
  639. if (!radio->videodev) {
  640. retval = -ENOMEM;
  641. goto err_intbuffer;
  642. }
  643. memcpy(radio->videodev, &si470x_viddev_template,
  644. sizeof(si470x_viddev_template));
  645. video_set_drvdata(radio->videodev, radio);
  646. /* show some infos about the specific si470x device */
  647. if (si470x_get_all_registers(radio) < 0) {
  648. retval = -EIO;
  649. goto err_video;
  650. }
  651. dev_info(&intf->dev, "DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",
  652. radio->registers[DEVICEID], radio->registers[CHIPID]);
  653. /* get software and hardware versions */
  654. if (si470x_get_scratch_page_versions(radio) < 0) {
  655. retval = -EIO;
  656. goto err_video;
  657. }
  658. dev_info(&intf->dev, "software version %d, hardware version %d\n",
  659. radio->software_version, radio->hardware_version);
  660. /* check if device and firmware is current */
  661. if ((radio->registers[CHIPID] & CHIPID_FIRMWARE)
  662. < RADIO_SW_VERSION_CURRENT) {
  663. dev_warn(&intf->dev,
  664. "This driver is known to work with "
  665. "firmware version %hu,\n", RADIO_SW_VERSION_CURRENT);
  666. dev_warn(&intf->dev,
  667. "but the device has firmware version %hu.\n",
  668. radio->registers[CHIPID] & CHIPID_FIRMWARE);
  669. dev_warn(&intf->dev,
  670. "If you have some trouble using this driver,\n");
  671. dev_warn(&intf->dev,
  672. "please report to V4L ML at "
  673. "linux-media@vger.kernel.org\n");
  674. }
  675. /* set initial frequency */
  676. si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
  677. /* set led to connect state */
  678. si470x_set_led_state(radio, BLINK_GREEN_LED);
  679. /* rds buffer allocation */
  680. radio->buf_size = rds_buf * 3;
  681. radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
  682. if (!radio->buffer) {
  683. retval = -EIO;
  684. goto err_video;
  685. }
  686. /* rds buffer configuration */
  687. radio->wr_index = 0;
  688. radio->rd_index = 0;
  689. init_waitqueue_head(&radio->read_queue);
  690. /* register video device */
  691. retval = video_register_device(radio->videodev, VFL_TYPE_RADIO,
  692. radio_nr);
  693. if (retval) {
  694. dev_warn(&intf->dev, "Could not register video device\n");
  695. goto err_all;
  696. }
  697. usb_set_intfdata(intf, radio);
  698. return 0;
  699. err_all:
  700. kfree(radio->buffer);
  701. err_video:
  702. video_device_release(radio->videodev);
  703. err_intbuffer:
  704. kfree(radio->int_in_buffer);
  705. err_radio:
  706. kfree(radio);
  707. err_initial:
  708. return retval;
  709. }
  710. /*
  711. * si470x_usb_driver_suspend - suspend the device
  712. */
  713. static int si470x_usb_driver_suspend(struct usb_interface *intf,
  714. pm_message_t message)
  715. {
  716. dev_info(&intf->dev, "suspending now...\n");
  717. return 0;
  718. }
  719. /*
  720. * si470x_usb_driver_resume - resume the device
  721. */
  722. static int si470x_usb_driver_resume(struct usb_interface *intf)
  723. {
  724. dev_info(&intf->dev, "resuming now...\n");
  725. return 0;
  726. }
  727. /*
  728. * si470x_usb_driver_disconnect - disconnect the device
  729. */
  730. static void si470x_usb_driver_disconnect(struct usb_interface *intf)
  731. {
  732. struct si470x_device *radio = usb_get_intfdata(intf);
  733. mutex_lock(&radio->disconnect_lock);
  734. radio->disconnected = 1;
  735. usb_set_intfdata(intf, NULL);
  736. if (radio->users == 0) {
  737. /* set led to disconnect state */
  738. si470x_set_led_state(radio, BLINK_ORANGE_LED);
  739. /* Free data structures. */
  740. usb_free_urb(radio->int_in_urb);
  741. kfree(radio->int_in_buffer);
  742. video_unregister_device(radio->videodev);
  743. kfree(radio->buffer);
  744. kfree(radio);
  745. }
  746. mutex_unlock(&radio->disconnect_lock);
  747. }
  748. /*
  749. * si470x_usb_driver - usb driver interface
  750. */
  751. static struct usb_driver si470x_usb_driver = {
  752. .name = DRIVER_NAME,
  753. .probe = si470x_usb_driver_probe,
  754. .disconnect = si470x_usb_driver_disconnect,
  755. .suspend = si470x_usb_driver_suspend,
  756. .resume = si470x_usb_driver_resume,
  757. .id_table = si470x_usb_driver_id_table,
  758. .supports_autosuspend = 1,
  759. };
  760. /**************************************************************************
  761. * Module Interface
  762. **************************************************************************/
  763. /*
  764. * si470x_module_init - module init
  765. */
  766. static int __init si470x_module_init(void)
  767. {
  768. printk(KERN_INFO DRIVER_DESC ", Version " DRIVER_VERSION "\n");
  769. return usb_register(&si470x_usb_driver);
  770. }
  771. /*
  772. * si470x_module_exit - module exit
  773. */
  774. static void __exit si470x_module_exit(void)
  775. {
  776. usb_deregister(&si470x_usb_driver);
  777. }
  778. module_init(si470x_module_init);
  779. module_exit(si470x_module_exit);
  780. MODULE_LICENSE("GPL");
  781. MODULE_AUTHOR(DRIVER_AUTHOR);
  782. MODULE_DESCRIPTION(DRIVER_DESC);
  783. MODULE_VERSION(DRIVER_VERSION);