radio-si470x-usb.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  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. * RDS Driver Functions
  284. **************************************************************************/
  285. /*
  286. * si470x_int_in_callback - rds callback and processing function
  287. *
  288. * TODO: do we need to use mutex locks in some sections?
  289. */
  290. static void si470x_int_in_callback(struct urb *urb)
  291. {
  292. struct si470x_device *radio = urb->context;
  293. unsigned char buf[RDS_REPORT_SIZE];
  294. int retval;
  295. unsigned char regnr;
  296. unsigned char blocknum;
  297. unsigned short bler; /* rds block errors */
  298. unsigned short rds;
  299. unsigned char tmpbuf[3];
  300. if (urb->status) {
  301. if (urb->status == -ENOENT ||
  302. urb->status == -ECONNRESET ||
  303. urb->status == -ESHUTDOWN) {
  304. return;
  305. } else {
  306. dev_warn(&radio->intf->dev,
  307. "non-zero urb status (%d)\n", urb->status);
  308. goto resubmit; /* Maybe we can recover. */
  309. }
  310. }
  311. /* safety checks */
  312. if (radio->disconnected)
  313. return;
  314. if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
  315. goto resubmit;
  316. if (urb->actual_length > 0) {
  317. /* Update RDS registers with URB data */
  318. buf[0] = RDS_REPORT;
  319. for (regnr = 0; regnr < RDS_REGISTER_NUM; regnr++)
  320. radio->registers[STATUSRSSI + regnr] =
  321. get_unaligned_be16(&radio->int_in_buffer[
  322. regnr * RADIO_REGISTER_SIZE + 1]);
  323. /* get rds blocks */
  324. if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0) {
  325. /* No RDS group ready, better luck next time */
  326. goto resubmit;
  327. }
  328. if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSS) == 0) {
  329. /* RDS decoder not synchronized */
  330. goto resubmit;
  331. }
  332. for (blocknum = 0; blocknum < 4; blocknum++) {
  333. switch (blocknum) {
  334. default:
  335. bler = (radio->registers[STATUSRSSI] &
  336. STATUSRSSI_BLERA) >> 9;
  337. rds = radio->registers[RDSA];
  338. break;
  339. case 1:
  340. bler = (radio->registers[READCHAN] &
  341. READCHAN_BLERB) >> 14;
  342. rds = radio->registers[RDSB];
  343. break;
  344. case 2:
  345. bler = (radio->registers[READCHAN] &
  346. READCHAN_BLERC) >> 12;
  347. rds = radio->registers[RDSC];
  348. break;
  349. case 3:
  350. bler = (radio->registers[READCHAN] &
  351. READCHAN_BLERD) >> 10;
  352. rds = radio->registers[RDSD];
  353. break;
  354. };
  355. /* Fill the V4L2 RDS buffer */
  356. put_unaligned_le16(rds, &tmpbuf);
  357. tmpbuf[2] = blocknum; /* offset name */
  358. tmpbuf[2] |= blocknum << 3; /* received offset */
  359. if (bler > max_rds_errors)
  360. tmpbuf[2] |= 0x80; /* uncorrectable errors */
  361. else if (bler > 0)
  362. tmpbuf[2] |= 0x40; /* corrected error(s) */
  363. /* copy RDS block to internal buffer */
  364. memcpy(&radio->buffer[radio->wr_index], &tmpbuf, 3);
  365. radio->wr_index += 3;
  366. /* wrap write pointer */
  367. if (radio->wr_index >= radio->buf_size)
  368. radio->wr_index = 0;
  369. /* check for overflow */
  370. if (radio->wr_index == radio->rd_index) {
  371. /* increment and wrap read pointer */
  372. radio->rd_index += 3;
  373. if (radio->rd_index >= radio->buf_size)
  374. radio->rd_index = 0;
  375. }
  376. }
  377. if (radio->wr_index != radio->rd_index)
  378. wake_up_interruptible(&radio->read_queue);
  379. }
  380. resubmit:
  381. /* Resubmit if we're still running. */
  382. if (radio->int_in_running && radio->usbdev) {
  383. retval = usb_submit_urb(radio->int_in_urb, GFP_ATOMIC);
  384. if (retval) {
  385. dev_warn(&radio->intf->dev,
  386. "resubmitting urb failed (%d)", retval);
  387. radio->int_in_running = 0;
  388. }
  389. }
  390. }
  391. /**************************************************************************
  392. * File Operations Interface
  393. **************************************************************************/
  394. /*
  395. * si470x_fops_read - read RDS data
  396. */
  397. static ssize_t si470x_fops_read(struct file *file, char __user *buf,
  398. size_t count, loff_t *ppos)
  399. {
  400. struct si470x_device *radio = video_drvdata(file);
  401. int retval = 0;
  402. unsigned int block_count = 0;
  403. /* switch on rds reception */
  404. if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
  405. si470x_rds_on(radio);
  406. /* block if no new data available */
  407. while (radio->wr_index == radio->rd_index) {
  408. if (file->f_flags & O_NONBLOCK) {
  409. retval = -EWOULDBLOCK;
  410. goto done;
  411. }
  412. if (wait_event_interruptible(radio->read_queue,
  413. radio->wr_index != radio->rd_index) < 0) {
  414. retval = -EINTR;
  415. goto done;
  416. }
  417. }
  418. /* calculate block count from byte count */
  419. count /= 3;
  420. /* copy RDS block out of internal buffer and to user buffer */
  421. mutex_lock(&radio->lock);
  422. while (block_count < count) {
  423. if (radio->rd_index == radio->wr_index)
  424. break;
  425. /* always transfer rds complete blocks */
  426. if (copy_to_user(buf, &radio->buffer[radio->rd_index], 3))
  427. /* retval = -EFAULT; */
  428. break;
  429. /* increment and wrap read pointer */
  430. radio->rd_index += 3;
  431. if (radio->rd_index >= radio->buf_size)
  432. radio->rd_index = 0;
  433. /* increment counters */
  434. block_count++;
  435. buf += 3;
  436. retval += 3;
  437. }
  438. mutex_unlock(&radio->lock);
  439. done:
  440. return retval;
  441. }
  442. /*
  443. * si470x_fops_poll - poll RDS data
  444. */
  445. static unsigned int si470x_fops_poll(struct file *file,
  446. struct poll_table_struct *pts)
  447. {
  448. struct si470x_device *radio = video_drvdata(file);
  449. int retval = 0;
  450. /* switch on rds reception */
  451. if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
  452. si470x_rds_on(radio);
  453. poll_wait(file, &radio->read_queue, pts);
  454. if (radio->rd_index != radio->wr_index)
  455. retval = POLLIN | POLLRDNORM;
  456. return retval;
  457. }
  458. /*
  459. * si470x_fops_open - file open
  460. */
  461. static int si470x_fops_open(struct file *file)
  462. {
  463. struct si470x_device *radio = video_drvdata(file);
  464. int retval;
  465. lock_kernel();
  466. radio->users++;
  467. retval = usb_autopm_get_interface(radio->intf);
  468. if (retval < 0) {
  469. radio->users--;
  470. retval = -EIO;
  471. goto done;
  472. }
  473. if (radio->users == 1) {
  474. /* start radio */
  475. retval = si470x_start(radio);
  476. if (retval < 0) {
  477. usb_autopm_put_interface(radio->intf);
  478. goto done;
  479. }
  480. /* initialize interrupt urb */
  481. usb_fill_int_urb(radio->int_in_urb, radio->usbdev,
  482. usb_rcvintpipe(radio->usbdev,
  483. radio->int_in_endpoint->bEndpointAddress),
  484. radio->int_in_buffer,
  485. le16_to_cpu(radio->int_in_endpoint->wMaxPacketSize),
  486. si470x_int_in_callback,
  487. radio,
  488. radio->int_in_endpoint->bInterval);
  489. radio->int_in_running = 1;
  490. mb();
  491. retval = usb_submit_urb(radio->int_in_urb, GFP_KERNEL);
  492. if (retval) {
  493. dev_info(&radio->intf->dev,
  494. "submitting int urb failed (%d)\n", retval);
  495. radio->int_in_running = 0;
  496. usb_autopm_put_interface(radio->intf);
  497. }
  498. }
  499. done:
  500. unlock_kernel();
  501. return retval;
  502. }
  503. /*
  504. * si470x_fops_release - file release
  505. */
  506. static int si470x_fops_release(struct file *file)
  507. {
  508. struct si470x_device *radio = video_drvdata(file);
  509. int retval = 0;
  510. /* safety check */
  511. if (!radio) {
  512. retval = -ENODEV;
  513. goto done;
  514. }
  515. mutex_lock(&radio->disconnect_lock);
  516. radio->users--;
  517. if (radio->users == 0) {
  518. /* shutdown interrupt handler */
  519. if (radio->int_in_running) {
  520. radio->int_in_running = 0;
  521. if (radio->int_in_urb)
  522. usb_kill_urb(radio->int_in_urb);
  523. }
  524. if (radio->disconnected) {
  525. video_unregister_device(radio->videodev);
  526. kfree(radio->int_in_buffer);
  527. kfree(radio->buffer);
  528. kfree(radio);
  529. goto unlock;
  530. }
  531. /* cancel read processes */
  532. wake_up_interruptible(&radio->read_queue);
  533. /* stop radio */
  534. retval = si470x_stop(radio);
  535. usb_autopm_put_interface(radio->intf);
  536. }
  537. unlock:
  538. mutex_unlock(&radio->disconnect_lock);
  539. done:
  540. return retval;
  541. }
  542. /*
  543. * si470x_fops - file operations interface
  544. */
  545. const struct v4l2_file_operations si470x_fops = {
  546. .owner = THIS_MODULE,
  547. .read = si470x_fops_read,
  548. .poll = si470x_fops_poll,
  549. .ioctl = video_ioctl2,
  550. .open = si470x_fops_open,
  551. .release = si470x_fops_release,
  552. };
  553. /**************************************************************************
  554. * Video4Linux Interface
  555. **************************************************************************/
  556. /*
  557. * si470x_vidioc_querycap - query device capabilities
  558. */
  559. int si470x_vidioc_querycap(struct file *file, void *priv,
  560. struct v4l2_capability *capability)
  561. {
  562. struct si470x_device *radio = video_drvdata(file);
  563. strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver));
  564. strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
  565. usb_make_path(radio->usbdev, capability->bus_info,
  566. sizeof(capability->bus_info));
  567. capability->version = DRIVER_KERNEL_VERSION;
  568. capability->capabilities = V4L2_CAP_HW_FREQ_SEEK |
  569. V4L2_CAP_TUNER | V4L2_CAP_RADIO | V4L2_CAP_RDS_CAPTURE;
  570. return 0;
  571. }
  572. /**************************************************************************
  573. * USB Interface
  574. **************************************************************************/
  575. /*
  576. * si470x_usb_driver_probe - probe for the device
  577. */
  578. static int si470x_usb_driver_probe(struct usb_interface *intf,
  579. const struct usb_device_id *id)
  580. {
  581. struct si470x_device *radio;
  582. struct usb_host_interface *iface_desc;
  583. struct usb_endpoint_descriptor *endpoint;
  584. int i, int_end_size, retval = 0;
  585. /* private data allocation and initialization */
  586. radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL);
  587. if (!radio) {
  588. retval = -ENOMEM;
  589. goto err_initial;
  590. }
  591. radio->users = 0;
  592. radio->disconnected = 0;
  593. radio->usbdev = interface_to_usbdev(intf);
  594. radio->intf = intf;
  595. mutex_init(&radio->disconnect_lock);
  596. mutex_init(&radio->lock);
  597. iface_desc = intf->cur_altsetting;
  598. /* Set up interrupt endpoint information. */
  599. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  600. endpoint = &iface_desc->endpoint[i].desc;
  601. if (((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
  602. USB_DIR_IN) && ((endpoint->bmAttributes &
  603. USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT))
  604. radio->int_in_endpoint = endpoint;
  605. }
  606. if (!radio->int_in_endpoint) {
  607. dev_info(&intf->dev, "could not find interrupt in endpoint\n");
  608. retval = -EIO;
  609. goto err_radio;
  610. }
  611. int_end_size = le16_to_cpu(radio->int_in_endpoint->wMaxPacketSize);
  612. radio->int_in_buffer = kmalloc(int_end_size, GFP_KERNEL);
  613. if (!radio->int_in_buffer) {
  614. dev_info(&intf->dev, "could not allocate int_in_buffer");
  615. retval = -ENOMEM;
  616. goto err_radio;
  617. }
  618. radio->int_in_urb = usb_alloc_urb(0, GFP_KERNEL);
  619. if (!radio->int_in_urb) {
  620. dev_info(&intf->dev, "could not allocate int_in_urb");
  621. retval = -ENOMEM;
  622. goto err_intbuffer;
  623. }
  624. /* video device allocation and initialization */
  625. radio->videodev = video_device_alloc();
  626. if (!radio->videodev) {
  627. retval = -ENOMEM;
  628. goto err_intbuffer;
  629. }
  630. memcpy(radio->videodev, &si470x_viddev_template,
  631. sizeof(si470x_viddev_template));
  632. video_set_drvdata(radio->videodev, radio);
  633. /* show some infos about the specific si470x device */
  634. if (si470x_get_all_registers(radio) < 0) {
  635. retval = -EIO;
  636. goto err_video;
  637. }
  638. dev_info(&intf->dev, "DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",
  639. radio->registers[DEVICEID], radio->registers[CHIPID]);
  640. /* get software and hardware versions */
  641. if (si470x_get_scratch_page_versions(radio) < 0) {
  642. retval = -EIO;
  643. goto err_video;
  644. }
  645. dev_info(&intf->dev, "software version %d, hardware version %d\n",
  646. radio->software_version, radio->hardware_version);
  647. /* check if device and firmware is current */
  648. if ((radio->registers[CHIPID] & CHIPID_FIRMWARE)
  649. < RADIO_SW_VERSION_CURRENT) {
  650. dev_warn(&intf->dev,
  651. "This driver is known to work with "
  652. "firmware version %hu,\n", RADIO_SW_VERSION_CURRENT);
  653. dev_warn(&intf->dev,
  654. "but the device has firmware version %hu.\n",
  655. radio->registers[CHIPID] & CHIPID_FIRMWARE);
  656. dev_warn(&intf->dev,
  657. "If you have some trouble using this driver,\n");
  658. dev_warn(&intf->dev,
  659. "please report to V4L ML at "
  660. "linux-media@vger.kernel.org\n");
  661. }
  662. /* set initial frequency */
  663. si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
  664. /* set led to connect state */
  665. si470x_set_led_state(radio, BLINK_GREEN_LED);
  666. /* rds buffer allocation */
  667. radio->buf_size = rds_buf * 3;
  668. radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
  669. if (!radio->buffer) {
  670. retval = -EIO;
  671. goto err_video;
  672. }
  673. /* rds buffer configuration */
  674. radio->wr_index = 0;
  675. radio->rd_index = 0;
  676. init_waitqueue_head(&radio->read_queue);
  677. /* register video device */
  678. retval = video_register_device(radio->videodev, VFL_TYPE_RADIO,
  679. radio_nr);
  680. if (retval) {
  681. dev_warn(&intf->dev, "Could not register video device\n");
  682. goto err_all;
  683. }
  684. usb_set_intfdata(intf, radio);
  685. return 0;
  686. err_all:
  687. kfree(radio->buffer);
  688. err_video:
  689. video_device_release(radio->videodev);
  690. err_intbuffer:
  691. kfree(radio->int_in_buffer);
  692. err_radio:
  693. kfree(radio);
  694. err_initial:
  695. return retval;
  696. }
  697. /*
  698. * si470x_usb_driver_suspend - suspend the device
  699. */
  700. static int si470x_usb_driver_suspend(struct usb_interface *intf,
  701. pm_message_t message)
  702. {
  703. dev_info(&intf->dev, "suspending now...\n");
  704. return 0;
  705. }
  706. /*
  707. * si470x_usb_driver_resume - resume the device
  708. */
  709. static int si470x_usb_driver_resume(struct usb_interface *intf)
  710. {
  711. dev_info(&intf->dev, "resuming now...\n");
  712. return 0;
  713. }
  714. /*
  715. * si470x_usb_driver_disconnect - disconnect the device
  716. */
  717. static void si470x_usb_driver_disconnect(struct usb_interface *intf)
  718. {
  719. struct si470x_device *radio = usb_get_intfdata(intf);
  720. mutex_lock(&radio->disconnect_lock);
  721. radio->disconnected = 1;
  722. usb_set_intfdata(intf, NULL);
  723. if (radio->users == 0) {
  724. /* set led to disconnect state */
  725. si470x_set_led_state(radio, BLINK_ORANGE_LED);
  726. /* Free data structures. */
  727. usb_free_urb(radio->int_in_urb);
  728. kfree(radio->int_in_buffer);
  729. video_unregister_device(radio->videodev);
  730. kfree(radio->buffer);
  731. kfree(radio);
  732. }
  733. mutex_unlock(&radio->disconnect_lock);
  734. }
  735. /*
  736. * si470x_usb_driver - usb driver interface
  737. */
  738. static struct usb_driver si470x_usb_driver = {
  739. .name = DRIVER_NAME,
  740. .probe = si470x_usb_driver_probe,
  741. .disconnect = si470x_usb_driver_disconnect,
  742. .suspend = si470x_usb_driver_suspend,
  743. .resume = si470x_usb_driver_resume,
  744. .id_table = si470x_usb_driver_id_table,
  745. .supports_autosuspend = 1,
  746. };
  747. /**************************************************************************
  748. * Module Interface
  749. **************************************************************************/
  750. /*
  751. * si470x_module_init - module init
  752. */
  753. static int __init si470x_module_init(void)
  754. {
  755. printk(KERN_INFO DRIVER_DESC ", Version " DRIVER_VERSION "\n");
  756. return usb_register(&si470x_usb_driver);
  757. }
  758. /*
  759. * si470x_module_exit - module exit
  760. */
  761. static void __exit si470x_module_exit(void)
  762. {
  763. usb_deregister(&si470x_usb_driver);
  764. }
  765. module_init(si470x_module_init);
  766. module_exit(si470x_module_exit);
  767. MODULE_LICENSE("GPL");
  768. MODULE_AUTHOR(DRIVER_AUTHOR);
  769. MODULE_DESCRIPTION(DRIVER_DESC);
  770. MODULE_VERSION(DRIVER_VERSION);