radio-si470x-usb.c 26 KB

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