radio-si470x.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494
  1. /*
  2. * drivers/media/radio/radio-si470x.c
  3. *
  4. * Driver for USB radios for the Silicon Labs Si470x FM Radio Receivers:
  5. * - Silicon Labs USB FM Radio Reference Design
  6. * - ADS/Tech FM Radio Receiver (formerly Instant FM Music) (RDX-155-EF)
  7. *
  8. * Copyright (c) 2008 Tobias Lorenz <tobias.lorenz@gmx.net>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. /*
  25. * History:
  26. * 2008-01-12 Tobias Lorenz <tobias.lorenz@gmx.net>
  27. * Version 1.0.0
  28. * - First working version
  29. * 2008-01-13 Tobias Lorenz <tobias.lorenz@gmx.net>
  30. * Version 1.0.1
  31. * - Improved error handling, every function now returns errno
  32. * - Improved multi user access (start/mute/stop)
  33. * - Channel doesn't get lost anymore after start/mute/stop
  34. * - RDS support added (polling mode via interrupt EP 1)
  35. * - marked default module parameters with *value*
  36. * - switched from bit structs to bit masks
  37. * - header file cleaned and integrated
  38. * 2008-01-14 Tobias Lorenz <tobias.lorenz@gmx.net>
  39. * Version 1.0.2
  40. * - hex values are now lower case
  41. * - commented USB ID for ADS/Tech moved on todo list
  42. * - blacklisted si470x in hid-quirks.c
  43. * - rds buffer handling functions integrated into *_work, *_read
  44. * - rds_command in si470x_poll exchanged against simple retval
  45. * - check for firmware version 15
  46. * - code order and prototypes still remain the same
  47. * - spacing and bottom of band codes remain the same
  48. * 2008-01-16 Tobias Lorenz <tobias.lorenz@gmx.net>
  49. * Version 1.0.3
  50. * - code reordered to avoid function prototypes
  51. * - switch/case defaults are now more user-friendly
  52. * - unified comment style
  53. * - applied all checkpatch.pl v1.12 suggestions
  54. * except the warning about the too long lines with bit comments
  55. * - renamed FMRADIO to RADIO to cut line length (checkpatch.pl)
  56. * 2008-01-22 Tobias Lorenz <tobias.lorenz@gmx.net>
  57. * Version 1.0.4
  58. * - avoid poss. locking when doing copy_to_user which may sleep
  59. * - RDS is automatically activated on read now
  60. * - code cleaned of unnecessary rds_commands
  61. * - USB Vendor/Product ID for ADS/Tech FM Radio Receiver verified
  62. * (thanks to Guillaume RAMOUSSE)
  63. * 2008-01-27 Tobias Lorenz <tobias.lorenz@gmx.net>
  64. * Version 1.0.5
  65. * - number of seek_retries changed to tune_timeout
  66. * - fixed problem with incomplete tune operations by own buffers
  67. * - optimization of variables
  68. * - improved error logging
  69. *
  70. * ToDo:
  71. * - add seeking support
  72. * - add firmware download/update support
  73. * - RDS support: interrupt mode, instead of polling
  74. * - add LED status output (check if that's not already done in firmware)
  75. */
  76. /* driver definitions */
  77. #define DRIVER_AUTHOR "Tobias Lorenz <tobias.lorenz@gmx.net>"
  78. #define DRIVER_NAME "radio-si470x"
  79. #define DRIVER_KERNEL_VERSION KERNEL_VERSION(1, 0, 5)
  80. #define DRIVER_CARD "Silicon Labs Si470x FM Radio Receiver"
  81. #define DRIVER_DESC "USB radio driver for Si470x FM Radio Receivers"
  82. #define DRIVER_VERSION "1.0.5"
  83. /* kernel includes */
  84. #include <linux/kernel.h>
  85. #include <linux/module.h>
  86. #include <linux/init.h>
  87. #include <linux/slab.h>
  88. #include <linux/input.h>
  89. #include <linux/usb.h>
  90. #include <linux/hid.h>
  91. #include <linux/version.h>
  92. #include <linux/videodev2.h>
  93. #include <media/v4l2-common.h>
  94. #include <media/rds.h>
  95. /* USB Device ID List */
  96. static struct usb_device_id si470x_usb_driver_id_table[] = {
  97. /* Silicon Labs USB FM Radio Reference Design */
  98. { USB_DEVICE_AND_INTERFACE_INFO(0x10c4, 0x818a, USB_CLASS_HID, 0, 0) },
  99. /* ADS/Tech FM Radio Receiver (formerly Instant FM Music) */
  100. { USB_DEVICE_AND_INTERFACE_INFO(0x06e1, 0xa155, USB_CLASS_HID, 0, 0) },
  101. /* Terminating entry */
  102. { }
  103. };
  104. MODULE_DEVICE_TABLE(usb, si470x_usb_driver_id_table);
  105. /**************************************************************************
  106. * Module Parameters
  107. **************************************************************************/
  108. /* Radio Nr */
  109. static int radio_nr = -1;
  110. module_param(radio_nr, int, 0);
  111. MODULE_PARM_DESC(radio_nr, "Radio Nr");
  112. /* Spacing (kHz) */
  113. /* 0: 200 kHz (USA, Australia) */
  114. /* 1: 100 kHz (Europe, Japan) */
  115. /* 2: 50 kHz */
  116. static unsigned short space = 2;
  117. module_param(space, ushort, 0);
  118. MODULE_PARM_DESC(radio_nr, "Spacing: 0=200kHz 1=100kHz *2=50kHz*");
  119. /* Bottom of Band (MHz) */
  120. /* 0: 87.5 - 108 MHz (USA, Europe)*/
  121. /* 1: 76 - 108 MHz (Japan wide band) */
  122. /* 2: 76 - 90 MHz (Japan) */
  123. static unsigned short band = 1;
  124. module_param(band, ushort, 0);
  125. MODULE_PARM_DESC(radio_nr, "Band: 0=87.5..108MHz *1=76..108MHz* 2=76..90MHz");
  126. /* De-emphasis */
  127. /* 0: 75 us (USA) */
  128. /* 1: 50 us (Europe, Australia, Japan) */
  129. static unsigned short de = 1;
  130. module_param(de, ushort, 0);
  131. MODULE_PARM_DESC(radio_nr, "De-emphasis: 0=75us *1=50us*");
  132. /* USB timeout */
  133. static unsigned int usb_timeout = 500;
  134. module_param(usb_timeout, uint, 0);
  135. MODULE_PARM_DESC(usb_timeout, "USB timeout (ms): *500*");
  136. /* Tune timeout */
  137. static unsigned int tune_timeout = 3000;
  138. module_param(tune_timeout, uint, 0);
  139. MODULE_PARM_DESC(tune_timeout, "Tune timeout: *3000*");
  140. /* RDS buffer blocks */
  141. static unsigned int rds_buf = 100;
  142. module_param(rds_buf, uint, 0);
  143. MODULE_PARM_DESC(rds_buf, "RDS buffer entries: *100*");
  144. /* RDS maximum block errors */
  145. static unsigned short max_rds_errors = 1;
  146. /* 0 means 0 errors requiring correction */
  147. /* 1 means 1-2 errors requiring correction (used by original USBRadio.exe) */
  148. /* 2 means 3-5 errors requiring correction */
  149. /* 3 means 6+ errors or errors in checkword, correction not possible */
  150. module_param(max_rds_errors, ushort, 0);
  151. MODULE_PARM_DESC(max_rds_errors, "RDS maximum block errors: *1*");
  152. /* RDS poll frequency */
  153. static unsigned int rds_poll_time = 40;
  154. /* 40 is used by the original USBRadio.exe */
  155. /* 50 is used by radio-cadet */
  156. /* 75 should be okay */
  157. /* 80 is the usual RDS receive interval */
  158. module_param(rds_poll_time, uint, 0);
  159. MODULE_PARM_DESC(rds_poll_time, "RDS poll time (ms): *40*");
  160. /**************************************************************************
  161. * Register Definitions
  162. **************************************************************************/
  163. #define RADIO_REGISTER_SIZE 2 /* 16 register bit width */
  164. #define RADIO_REGISTER_NUM 16 /* DEVICEID ... RDSD */
  165. #define RDS_REGISTER_NUM 6 /* STATUSRSSI ... RDSD */
  166. #define DEVICEID 0 /* Device ID */
  167. #define DEVICEID_PN 0xf000 /* bits 15..12: Part Number */
  168. #define DEVICEID_MFGID 0x0fff /* bits 11..00: Manufacturer ID */
  169. #define CHIPID 1 /* Chip ID */
  170. #define CHIPID_REV 0xfc00 /* bits 15..10: Chip Version */
  171. #define CHIPID_DEV 0x0200 /* bits 09..09: Device */
  172. #define CHIPID_FIRMWARE 0x01ff /* bits 08..00: Firmware Version */
  173. #define POWERCFG 2 /* Power Configuration */
  174. #define POWERCFG_DSMUTE 0x8000 /* bits 15..15: Softmute Disable */
  175. #define POWERCFG_DMUTE 0x4000 /* bits 14..14: Mute Disable */
  176. #define POWERCFG_MONO 0x2000 /* bits 13..13: Mono Select */
  177. #define POWERCFG_RDSM 0x0800 /* bits 11..11: RDS Mode (Si4701 only) */
  178. #define POWERCFG_SKMODE 0x0400 /* bits 10..10: Seek Mode */
  179. #define POWERCFG_SEEKUP 0x0200 /* bits 09..09: Seek Direction */
  180. #define POWERCFG_SEEK 0x0100 /* bits 08..08: Seek */
  181. #define POWERCFG_DISABLE 0x0040 /* bits 06..06: Powerup Disable */
  182. #define POWERCFG_ENABLE 0x0001 /* bits 00..00: Powerup Enable */
  183. #define CHANNEL 3 /* Channel */
  184. #define CHANNEL_TUNE 0x8000 /* bits 15..15: Tune */
  185. #define CHANNEL_CHAN 0x03ff /* bits 09..00: Channel Select */
  186. #define SYSCONFIG1 4 /* System Configuration 1 */
  187. #define SYSCONFIG1_RDSIEN 0x8000 /* bits 15..15: RDS Interrupt Enable (Si4701 only) */
  188. #define SYSCONFIG1_STCIEN 0x4000 /* bits 14..14: Seek/Tune Complete Interrupt Enable */
  189. #define SYSCONFIG1_RDS 0x1000 /* bits 12..12: RDS Enable (Si4701 only) */
  190. #define SYSCONFIG1_DE 0x0800 /* bits 11..11: De-emphasis (0=75us 1=50us) */
  191. #define SYSCONFIG1_AGCD 0x0400 /* bits 10..10: AGC Disable */
  192. #define SYSCONFIG1_BLNDADJ 0x00c0 /* bits 07..06: Stereo/Mono Blend Level Adjustment */
  193. #define SYSCONFIG1_GPIO3 0x0030 /* bits 05..04: General Purpose I/O 3 */
  194. #define SYSCONFIG1_GPIO2 0x000c /* bits 03..02: General Purpose I/O 2 */
  195. #define SYSCONFIG1_GPIO1 0x0003 /* bits 01..00: General Purpose I/O 1 */
  196. #define SYSCONFIG2 5 /* System Configuration 2 */
  197. #define SYSCONFIG2_SEEKTH 0xff00 /* bits 15..08: RSSI Seek Threshold */
  198. #define SYSCONFIG2_BAND 0x0080 /* bits 07..06: Band Select */
  199. #define SYSCONFIG2_SPACE 0x0030 /* bits 05..04: Channel Spacing */
  200. #define SYSCONFIG2_VOLUME 0x000f /* bits 03..00: Volume */
  201. #define SYSCONFIG3 6 /* System Configuration 3 */
  202. #define SYSCONFIG3_SMUTER 0xc000 /* bits 15..14: Softmute Attack/Recover Rate */
  203. #define SYSCONFIG3_SMUTEA 0x3000 /* bits 13..12: Softmute Attenuation */
  204. #define SYSCONFIG3_SKSNR 0x00f0 /* bits 07..04: Seek SNR Threshold */
  205. #define SYSCONFIG3_SKCNT 0x000f /* bits 03..00: Seek FM Impulse Detection Threshold */
  206. #define TEST1 7 /* Test 1 */
  207. #define TEST1_AHIZEN 0x4000 /* bits 14..14: Audio High-Z Enable */
  208. #define TEST2 8 /* Test 2 */
  209. /* TEST2 only contains reserved bits */
  210. #define BOOTCONFIG 9 /* Boot Configuration */
  211. /* BOOTCONFIG only contains reserved bits */
  212. #define STATUSRSSI 10 /* Status RSSI */
  213. #define STATUSRSSI_RDSR 0x8000 /* bits 15..15: RDS Ready (Si4701 only) */
  214. #define STATUSRSSI_STC 0x4000 /* bits 14..14: Seek/Tune Complete */
  215. #define STATUSRSSI_SF 0x2000 /* bits 13..13: Seek Fail/Band Limit */
  216. #define STATUSRSSI_AFCRL 0x1000 /* bits 12..12: AFC Rail */
  217. #define STATUSRSSI_RDSS 0x0800 /* bits 11..11: RDS Synchronized (Si4701 only) */
  218. #define STATUSRSSI_BLERA 0x0600 /* bits 10..09: RDS Block A Errors (Si4701 only) */
  219. #define STATUSRSSI_ST 0x0100 /* bits 08..08: Stereo Indicator */
  220. #define STATUSRSSI_RSSI 0x00ff /* bits 07..00: RSSI (Received Signal Strength Indicator) */
  221. #define READCHAN 11 /* Read Channel */
  222. #define READCHAN_BLERB 0xc000 /* bits 15..14: RDS Block D Errors (Si4701 only) */
  223. #define READCHAN_BLERC 0x3000 /* bits 13..12: RDS Block C Errors (Si4701 only) */
  224. #define READCHAN_BLERD 0x0c00 /* bits 11..10: RDS Block B Errors (Si4701 only) */
  225. #define READCHAN_READCHAN 0x03ff /* bits 09..00: Read Channel */
  226. #define RDSA 12 /* RDSA */
  227. #define RDSA_RDSA 0xffff /* bits 15..00: RDS Block A Data (Si4701 only) */
  228. #define RDSB 13 /* RDSB */
  229. #define RDSB_RDSB 0xffff /* bits 15..00: RDS Block B Data (Si4701 only) */
  230. #define RDSC 14 /* RDSC */
  231. #define RDSC_RDSC 0xffff /* bits 15..00: RDS Block C Data (Si4701 only) */
  232. #define RDSD 15 /* RDSD */
  233. #define RDSD_RDSD 0xffff /* bits 15..00: RDS Block D Data (Si4701 only) */
  234. /**************************************************************************
  235. * USB HID Reports
  236. **************************************************************************/
  237. /* Reports 1-16 give direct read/write access to the 16 Si470x registers */
  238. /* with the (REPORT_ID - 1) corresponding to the register address across USB */
  239. /* endpoint 0 using GET_REPORT and SET_REPORT */
  240. #define REGISTER_REPORT_SIZE (RADIO_REGISTER_SIZE + 1)
  241. #define REGISTER_REPORT(reg) ((reg) + 1)
  242. /* Report 17 gives direct read/write access to the entire Si470x register */
  243. /* map across endpoint 0 using GET_REPORT and SET_REPORT */
  244. #define ENTIRE_REPORT_SIZE (RADIO_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
  245. #define ENTIRE_REPORT 17
  246. /* Report 18 is used to send the lowest 6 Si470x registers up the HID */
  247. /* interrupt endpoint 1 to Windows every 20 milliseconds for status */
  248. #define RDS_REPORT_SIZE (RDS_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
  249. #define RDS_REPORT 18
  250. /* Report 19: LED state */
  251. #define LED_REPORT_SIZE 3
  252. #define LED_REPORT 19
  253. /* Report 19: stream */
  254. #define STREAM_REPORT_SIZE 3
  255. #define STREAM_REPORT 19
  256. /* Report 20: scratch */
  257. #define SCRATCH_PAGE_SIZE 63
  258. #define SCRATCH_REPORT_SIZE (SCRATCH_PAGE_SIZE + 1)
  259. #define SCRATCH_REPORT 20
  260. /* Reports 19-22: flash upgrade of the C8051F321 */
  261. #define WRITE_REPORT 19
  262. #define FLASH_REPORT 20
  263. #define CRC_REPORT 21
  264. #define RESPONSE_REPORT 22
  265. /* Report 23: currently unused, but can accept 60 byte reports on the HID */
  266. /* interrupt out endpoint 2 every 1 millisecond */
  267. #define UNUSED_REPORT 23
  268. /**************************************************************************
  269. * Software/Hardware Versions
  270. **************************************************************************/
  271. #define RADIO_SW_VERSION_NOT_BOOTLOADABLE 6
  272. #define RADIO_SW_VERSION 7
  273. #define RADIO_SW_VERSION_CURRENT 15
  274. #define RADIO_HW_VERSION 1
  275. #define SCRATCH_PAGE_SW_VERSION 1
  276. #define SCRATCH_PAGE_HW_VERSION 2
  277. /**************************************************************************
  278. * LED State Definitions
  279. **************************************************************************/
  280. #define LED_COMMAND 0x35
  281. #define NO_CHANGE_LED 0x00
  282. #define ALL_COLOR_LED 0x01 /* streaming state */
  283. #define BLINK_GREEN_LED 0x02 /* connect state */
  284. #define BLINK_RED_LED 0x04
  285. #define BLINK_ORANGE_LED 0x10 /* disconnect state */
  286. #define SOLID_GREEN_LED 0x20 /* tuning/seeking state */
  287. #define SOLID_RED_LED 0x40 /* bootload state */
  288. #define SOLID_ORANGE_LED 0x80
  289. /**************************************************************************
  290. * Stream State Definitions
  291. **************************************************************************/
  292. #define STREAM_COMMAND 0x36
  293. #define STREAM_VIDPID 0x00
  294. #define STREAM_AUDIO 0xff
  295. /**************************************************************************
  296. * Bootloader / Flash Commands
  297. **************************************************************************/
  298. /* unique id sent to bootloader and required to put into a bootload state */
  299. #define UNIQUE_BL_ID 0x34
  300. /* mask for the flash data */
  301. #define FLASH_DATA_MASK 0x55
  302. /* bootloader commands */
  303. #define GET_SW_VERSION_COMMAND 0x00
  304. #define SET_PAGE_COMMAND 0x01
  305. #define ERASE_PAGE_COMMAND 0x02
  306. #define WRITE_PAGE_COMMAND 0x03
  307. #define CRC_ON_PAGE_COMMAND 0x04
  308. #define READ_FLASH_BYTE_COMMAND 0x05
  309. #define RESET_DEVICE_COMMAND 0x06
  310. #define GET_HW_VERSION_COMMAND 0x07
  311. #define BLANK 0xff
  312. /* bootloader command responses */
  313. #define COMMAND_OK 0x01
  314. #define COMMAND_FAILED 0x02
  315. #define COMMAND_PENDING 0x03
  316. /* buffer sizes */
  317. #define COMMAND_BUFFER_SIZE 4
  318. #define RESPONSE_BUFFER_SIZE 2
  319. #define FLASH_BUFFER_SIZE 64
  320. #define CRC_BUFFER_SIZE 3
  321. /**************************************************************************
  322. * General Driver Definitions
  323. **************************************************************************/
  324. /*
  325. * si470x_device - private data
  326. */
  327. struct si470x_device {
  328. /* reference to USB and video device */
  329. struct usb_device *usbdev;
  330. struct video_device *videodev;
  331. /* driver management */
  332. unsigned int users;
  333. /* Silabs internal registers (0..15) */
  334. unsigned short registers[RADIO_REGISTER_NUM];
  335. /* RDS receive buffer */
  336. struct work_struct work;
  337. wait_queue_head_t read_queue;
  338. struct timer_list timer;
  339. spinlock_t lock; /* buffer locking */
  340. unsigned char *buffer; /* size is always multiple of three */
  341. unsigned int buf_size;
  342. unsigned int rd_index;
  343. unsigned int wr_index;
  344. };
  345. /*
  346. * The frequency is set in units of 62.5 Hz when using V4L2_TUNER_CAP_LOW,
  347. * 62.5 kHz otherwise.
  348. * The tuner is able to have a channel spacing of 50, 100 or 200 kHz.
  349. * tuner->capability is therefore set to V4L2_TUNER_CAP_LOW
  350. * The FREQ_MUL is then: 1 MHz / 62.5 Hz = 16000
  351. */
  352. #define FREQ_MUL (1000000 / 62.5)
  353. /**************************************************************************
  354. * General Driver Functions
  355. **************************************************************************/
  356. /*
  357. * si470x_get_report - receive a HID report
  358. */
  359. static int si470x_get_report(struct si470x_device *radio, void *buf, int size)
  360. {
  361. unsigned char *report = (unsigned char *) buf;
  362. int retval;
  363. retval = usb_control_msg(radio->usbdev,
  364. usb_rcvctrlpipe(radio->usbdev, 0),
  365. HID_REQ_GET_REPORT,
  366. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
  367. report[0], 2,
  368. buf, size, usb_timeout);
  369. if (retval < 0)
  370. printk(KERN_WARNING DRIVER_NAME
  371. ": si470x_get_report: usb_control_msg returned %d\n",
  372. retval);
  373. return retval;
  374. }
  375. /*
  376. * si470x_set_report - send a HID report
  377. */
  378. static int si470x_set_report(struct si470x_device *radio, void *buf, int size)
  379. {
  380. unsigned char *report = (unsigned char *) buf;
  381. int retval;
  382. retval = usb_control_msg(radio->usbdev,
  383. usb_sndctrlpipe(radio->usbdev, 0),
  384. HID_REQ_SET_REPORT,
  385. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
  386. report[0], 2,
  387. buf, size, usb_timeout);
  388. if (retval < 0)
  389. printk(KERN_WARNING DRIVER_NAME
  390. ": si470x_set_report: usb_control_msg returned %d\n",
  391. retval);
  392. return retval;
  393. }
  394. /*
  395. * si470x_get_register - read register
  396. */
  397. static int si470x_get_register(struct si470x_device *radio, int regnr)
  398. {
  399. unsigned char buf[REGISTER_REPORT_SIZE];
  400. int retval;
  401. buf[0] = REGISTER_REPORT(regnr);
  402. retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
  403. if (retval >= 0)
  404. radio->registers[regnr] = (buf[1] << 8) | buf[2];
  405. return (retval < 0) ? -EINVAL : 0;
  406. }
  407. /*
  408. * si470x_set_register - write register
  409. */
  410. static int si470x_set_register(struct si470x_device *radio, int regnr)
  411. {
  412. unsigned char buf[REGISTER_REPORT_SIZE];
  413. int retval;
  414. buf[0] = REGISTER_REPORT(regnr);
  415. buf[1] = (radio->registers[regnr] & 0xff00) >> 8;
  416. buf[2] = (radio->registers[regnr] & 0x00ff);
  417. retval = si470x_set_report(radio, (void *) &buf, sizeof(buf));
  418. return (retval < 0) ? -EINVAL : 0;
  419. }
  420. /*
  421. * si470x_get_all_registers - read entire registers
  422. */
  423. static int si470x_get_all_registers(struct si470x_device *radio)
  424. {
  425. unsigned char buf[ENTIRE_REPORT_SIZE];
  426. int retval;
  427. unsigned char regnr;
  428. buf[0] = ENTIRE_REPORT;
  429. retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
  430. if (retval >= 0)
  431. for (regnr = 0; regnr < RADIO_REGISTER_NUM; regnr++)
  432. radio->registers[regnr] =
  433. (buf[regnr * RADIO_REGISTER_SIZE + 1] << 8) |
  434. buf[regnr * RADIO_REGISTER_SIZE + 2];
  435. return (retval < 0) ? -EINVAL : 0;
  436. }
  437. /*
  438. * si470x_get_rds_registers - read rds registers
  439. */
  440. static int si470x_get_rds_registers(struct si470x_device *radio)
  441. {
  442. unsigned char buf[RDS_REPORT_SIZE];
  443. int retval;
  444. int size;
  445. unsigned char regnr;
  446. buf[0] = RDS_REPORT;
  447. retval = usb_interrupt_msg(radio->usbdev,
  448. usb_rcvintpipe(radio->usbdev, 1),
  449. (void *) &buf, sizeof(buf), &size, usb_timeout);
  450. if (size != sizeof(buf))
  451. printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_register: "
  452. "return size differs: %d != %ld\n", size, sizeof(buf));
  453. if (retval < 0)
  454. printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: "
  455. "usb_interrupt_msg returned %d\n", retval);
  456. if (retval >= 0)
  457. for (regnr = 0; regnr < RDS_REGISTER_NUM; regnr++)
  458. radio->registers[STATUSRSSI + regnr] =
  459. (buf[regnr * RADIO_REGISTER_SIZE + 1] << 8) |
  460. buf[regnr * RADIO_REGISTER_SIZE + 2];
  461. return (retval < 0) ? -EINVAL : 0;
  462. }
  463. /*
  464. * si470x_set_chan - set the channel
  465. */
  466. static int si470x_set_chan(struct si470x_device *radio, unsigned short chan)
  467. {
  468. int retval;
  469. unsigned long timeout;
  470. bool timed_out = 0;
  471. /* start tuning */
  472. radio->registers[CHANNEL] &= ~CHANNEL_CHAN;
  473. radio->registers[CHANNEL] |= CHANNEL_TUNE | chan;
  474. retval = si470x_set_register(radio, CHANNEL);
  475. if (retval < 0)
  476. return retval;
  477. /* wait till seek operation has completed */
  478. timeout = jiffies + msecs_to_jiffies(tune_timeout);
  479. do {
  480. retval = si470x_get_register(radio, STATUSRSSI);
  481. if (retval < 0)
  482. return retval;
  483. timed_out = time_after(jiffies, timeout);
  484. } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
  485. (!timed_out));
  486. if (timed_out)
  487. printk(KERN_WARNING DRIVER_NAME
  488. ": seek does not finish after %d ms\n", tune_timeout);
  489. /* stop tuning */
  490. radio->registers[CHANNEL] &= ~CHANNEL_TUNE;
  491. return si470x_set_register(radio, CHANNEL);
  492. }
  493. /*
  494. * si470x_get_freq - get the frequency
  495. */
  496. static unsigned int si470x_get_freq(struct si470x_device *radio)
  497. {
  498. unsigned int spacing, band_bottom, freq;
  499. unsigned short chan;
  500. int retval;
  501. /* Spacing (kHz) */
  502. switch (space) {
  503. /* 0: 200 kHz (USA, Australia) */
  504. case 0 : spacing = 0.200 * FREQ_MUL; break;
  505. /* 1: 100 kHz (Europe, Japan) */
  506. case 1 : spacing = 0.100 * FREQ_MUL; break;
  507. /* 2: 50 kHz */
  508. default: spacing = 0.050 * FREQ_MUL; break;
  509. };
  510. /* Bottom of Band (MHz) */
  511. switch (band) {
  512. /* 0: 87.5 - 108 MHz (USA, Europe) */
  513. case 0 : band_bottom = 87.5 * FREQ_MUL; break;
  514. /* 1: 76 - 108 MHz (Japan wide band) */
  515. default: band_bottom = 76 * FREQ_MUL; break;
  516. /* 2: 76 - 90 MHz (Japan) */
  517. case 2 : band_bottom = 76 * FREQ_MUL; break;
  518. };
  519. /* read channel */
  520. retval = si470x_get_register(radio, READCHAN);
  521. if (retval < 0)
  522. return retval;
  523. chan = radio->registers[READCHAN] & READCHAN_READCHAN;
  524. /* Frequency (MHz) = Spacing (kHz) x Channel + Bottom of Band (MHz) */
  525. freq = chan * spacing + band_bottom;
  526. return freq;
  527. }
  528. /*
  529. * si470x_set_freq - set the frequency
  530. */
  531. static int si470x_set_freq(struct si470x_device *radio, unsigned int freq)
  532. {
  533. unsigned int spacing, band_bottom;
  534. unsigned short chan;
  535. /* Spacing (kHz) */
  536. switch (space) {
  537. /* 0: 200 kHz (USA, Australia) */
  538. case 0 : spacing = 0.200 * FREQ_MUL; break;
  539. /* 1: 100 kHz (Europe, Japan) */
  540. case 1 : spacing = 0.100 * FREQ_MUL; break;
  541. /* 2: 50 kHz */
  542. default: spacing = 0.050 * FREQ_MUL; break;
  543. };
  544. /* Bottom of Band (MHz) */
  545. switch (band) {
  546. /* 0: 87.5 - 108 MHz (USA, Europe) */
  547. case 0 : band_bottom = 87.5 * FREQ_MUL; break;
  548. /* 1: 76 - 108 MHz (Japan wide band) */
  549. default: band_bottom = 76 * FREQ_MUL; break;
  550. /* 2: 76 - 90 MHz (Japan) */
  551. case 2 : band_bottom = 76 * FREQ_MUL; break;
  552. };
  553. /* Chan = [ Freq (Mhz) - Bottom of Band (MHz) ] / Spacing (kHz) */
  554. chan = (freq - band_bottom) / spacing;
  555. return si470x_set_chan(radio, chan);
  556. }
  557. /*
  558. * si470x_start - switch on radio
  559. */
  560. static int si470x_start(struct si470x_device *radio)
  561. {
  562. int retval;
  563. /* powercfg */
  564. radio->registers[POWERCFG] =
  565. POWERCFG_DMUTE | POWERCFG_ENABLE | POWERCFG_RDSM;
  566. retval = si470x_set_register(radio, POWERCFG);
  567. if (retval < 0)
  568. return retval;
  569. /* sysconfig 1 */
  570. radio->registers[SYSCONFIG1] = SYSCONFIG1_DE;
  571. retval = si470x_set_register(radio, SYSCONFIG1);
  572. if (retval < 0)
  573. return retval;
  574. /* sysconfig 2 */
  575. radio->registers[SYSCONFIG2] =
  576. (0x3f << 8) | /* SEEKTH */
  577. (band << 6) | /* BAND */
  578. (space << 4) | /* SPACE */
  579. 15; /* VOLUME (max) */
  580. retval = si470x_set_register(radio, SYSCONFIG2);
  581. if (retval < 0)
  582. return retval;
  583. /* reset last channel */
  584. return si470x_set_chan(radio,
  585. radio->registers[CHANNEL] & CHANNEL_CHAN);
  586. }
  587. /*
  588. * si470x_stop - switch off radio
  589. */
  590. static int si470x_stop(struct si470x_device *radio)
  591. {
  592. int retval;
  593. /* sysconfig 1 */
  594. radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
  595. retval = si470x_set_register(radio, SYSCONFIG1);
  596. if (retval < 0)
  597. return retval;
  598. /* powercfg */
  599. radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
  600. /* POWERCFG_ENABLE has to automatically go low */
  601. radio->registers[POWERCFG] |= POWERCFG_ENABLE | POWERCFG_DISABLE;
  602. return si470x_set_register(radio, POWERCFG);
  603. }
  604. /*
  605. * si470x_rds_on - switch on rds reception
  606. */
  607. static int si470x_rds_on(struct si470x_device *radio)
  608. {
  609. /* sysconfig 1 */
  610. radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDS;
  611. return si470x_set_register(radio, SYSCONFIG1);
  612. }
  613. /**************************************************************************
  614. * RDS Driver Functions
  615. **************************************************************************/
  616. /*
  617. * si470x_rds - rds processing function
  618. */
  619. static void si470x_rds(struct si470x_device *radio)
  620. {
  621. /* get rds blocks */
  622. if (si470x_get_rds_registers(radio) < 0)
  623. return;
  624. if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0) {
  625. /* No RDS group ready */
  626. return;
  627. }
  628. if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSS) == 0) {
  629. /* RDS decoder not synchronized */
  630. return;
  631. }
  632. /* copy four RDS blocks to internal buffer */
  633. if (spin_trylock(&radio->lock)) {
  634. unsigned char blocknum;
  635. unsigned short bler; /* rds block errors */
  636. unsigned short rds;
  637. unsigned char tmpbuf[3];
  638. unsigned char i;
  639. /* process each rds block */
  640. for (blocknum = 0; blocknum < 4; blocknum++) {
  641. switch (blocknum) {
  642. default:
  643. bler = (radio->registers[STATUSRSSI] &
  644. STATUSRSSI_BLERA) >> 9;
  645. rds = radio->registers[RDSA];
  646. break;
  647. case 1:
  648. bler = (radio->registers[READCHAN] &
  649. READCHAN_BLERB) >> 14;
  650. rds = radio->registers[RDSB];
  651. break;
  652. case 2:
  653. bler = (radio->registers[READCHAN] &
  654. READCHAN_BLERC) >> 12;
  655. rds = radio->registers[RDSC];
  656. break;
  657. case 3:
  658. bler = (radio->registers[READCHAN] &
  659. READCHAN_BLERD) >> 10;
  660. rds = radio->registers[RDSD];
  661. break;
  662. };
  663. /* Fill the V4L2 RDS buffer */
  664. tmpbuf[0] = rds & 0x00ff; /* LSB */
  665. tmpbuf[1] = (rds & 0xff00) >> 8;/* MSB */
  666. tmpbuf[2] = blocknum; /* offset name */
  667. tmpbuf[2] |= blocknum << 3; /* received offset */
  668. if (bler > max_rds_errors)
  669. tmpbuf[2] |= 0x80; /* uncorrectable errors */
  670. else if (bler > 0)
  671. tmpbuf[2] |= 0x40; /* corrected error(s) */
  672. /* copy RDS block to internal buffer */
  673. for (i = 0; i < 3; i++) {
  674. radio->buffer[radio->wr_index] = tmpbuf[i];
  675. radio->wr_index++;
  676. }
  677. /* wrap write pointer */
  678. if (radio->wr_index >= radio->buf_size)
  679. radio->wr_index = 0;
  680. /* check for overflow */
  681. if (radio->wr_index == radio->rd_index) {
  682. /* increment and wrap read pointer */
  683. radio->rd_index += 3;
  684. if (radio->rd_index >= radio->buf_size)
  685. radio->rd_index = 0;
  686. }
  687. }
  688. spin_unlock(&radio->lock);
  689. }
  690. /* wake up read queue */
  691. if (radio->wr_index != radio->rd_index)
  692. wake_up_interruptible(&radio->read_queue);
  693. }
  694. /*
  695. * si470x_timer - rds timer function
  696. */
  697. static void si470x_timer(unsigned long data)
  698. {
  699. struct si470x_device *radio = (struct si470x_device *) data;
  700. schedule_work(&radio->work);
  701. }
  702. /*
  703. * si470x_work - rds work function
  704. */
  705. static void si470x_work(struct work_struct *work)
  706. {
  707. struct si470x_device *radio = container_of(work, struct si470x_device,
  708. work);
  709. if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
  710. return;
  711. si470x_rds(radio);
  712. mod_timer(&radio->timer, jiffies + msecs_to_jiffies(rds_poll_time));
  713. }
  714. /**************************************************************************
  715. * File Operations Interface
  716. **************************************************************************/
  717. /*
  718. * si470x_fops_read - read RDS data
  719. */
  720. static ssize_t si470x_fops_read(struct file *file, char __user *buf,
  721. size_t count, loff_t *ppos)
  722. {
  723. struct si470x_device *radio = video_get_drvdata(video_devdata(file));
  724. int retval = 0;
  725. /* switch on rds reception */
  726. if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
  727. si470x_rds_on(radio);
  728. schedule_work(&radio->work);
  729. }
  730. /* block if no new data available */
  731. while (radio->wr_index == radio->rd_index) {
  732. if (file->f_flags & O_NONBLOCK)
  733. return -EWOULDBLOCK;
  734. interruptible_sleep_on(&radio->read_queue);
  735. }
  736. /* calculate block count from byte count */
  737. count /= 3;
  738. /* copy RDS block out of internal buffer and to user buffer */
  739. if (spin_trylock(&radio->lock)) {
  740. unsigned int block_count = 0;
  741. while (block_count < count) {
  742. if (radio->rd_index == radio->wr_index)
  743. break;
  744. /* always transfer rds complete blocks */
  745. if (copy_to_user(buf,
  746. &radio->buffer[radio->rd_index], 3))
  747. /* retval = -EFAULT; */
  748. break;
  749. /* increment and wrap read pointer */
  750. radio->rd_index += 3;
  751. if (radio->rd_index >= radio->buf_size)
  752. radio->rd_index = 0;
  753. /* increment counters */
  754. block_count++;
  755. buf += 3;
  756. retval += 3;
  757. }
  758. spin_unlock(&radio->lock);
  759. }
  760. return retval;
  761. }
  762. /*
  763. * si470x_fops_poll - poll RDS data
  764. */
  765. static unsigned int si470x_fops_poll(struct file *file,
  766. struct poll_table_struct *pts)
  767. {
  768. struct si470x_device *radio = video_get_drvdata(video_devdata(file));
  769. /* switch on rds reception */
  770. if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
  771. si470x_rds_on(radio);
  772. schedule_work(&radio->work);
  773. }
  774. poll_wait(file, &radio->read_queue, pts);
  775. if (radio->rd_index != radio->wr_index)
  776. return POLLIN | POLLRDNORM;
  777. return 0;
  778. }
  779. /*
  780. * si470x_fops_open - file open
  781. */
  782. static int si470x_fops_open(struct inode *inode, struct file *file)
  783. {
  784. struct si470x_device *radio = video_get_drvdata(video_devdata(file));
  785. radio->users++;
  786. if (radio->users == 1)
  787. return si470x_start(radio);
  788. return 0;
  789. }
  790. /*
  791. * si470x_fops_release - file release
  792. */
  793. static int si470x_fops_release(struct inode *inode, struct file *file)
  794. {
  795. struct si470x_device *radio = video_get_drvdata(video_devdata(file));
  796. if (!radio)
  797. return -ENODEV;
  798. radio->users--;
  799. if (radio->users == 0) {
  800. /* stop rds reception */
  801. del_timer_sync(&radio->timer);
  802. flush_scheduled_work();
  803. /* cancel read processes */
  804. wake_up_interruptible(&radio->read_queue);
  805. return si470x_stop(radio);
  806. }
  807. return 0;
  808. }
  809. /*
  810. * si470x_fops - file operations interface
  811. */
  812. static const struct file_operations si470x_fops = {
  813. .owner = THIS_MODULE,
  814. .llseek = no_llseek,
  815. .read = si470x_fops_read,
  816. .poll = si470x_fops_poll,
  817. .ioctl = video_ioctl2,
  818. .compat_ioctl = v4l_compat_ioctl32,
  819. .open = si470x_fops_open,
  820. .release = si470x_fops_release,
  821. };
  822. /**************************************************************************
  823. * Video4Linux Interface
  824. **************************************************************************/
  825. /*
  826. * si470x_v4l2_queryctrl - query control
  827. */
  828. static struct v4l2_queryctrl si470x_v4l2_queryctrl[] = {
  829. /* HINT: the disabled controls are only here to satify kradio and such apps */
  830. {
  831. .id = V4L2_CID_AUDIO_VOLUME,
  832. .type = V4L2_CTRL_TYPE_INTEGER,
  833. .name = "Volume",
  834. .minimum = 0,
  835. .maximum = 15,
  836. .step = 1,
  837. .default_value = 15,
  838. },
  839. {
  840. .id = V4L2_CID_AUDIO_BALANCE,
  841. .flags = V4L2_CTRL_FLAG_DISABLED,
  842. },
  843. {
  844. .id = V4L2_CID_AUDIO_BASS,
  845. .flags = V4L2_CTRL_FLAG_DISABLED,
  846. },
  847. {
  848. .id = V4L2_CID_AUDIO_TREBLE,
  849. .flags = V4L2_CTRL_FLAG_DISABLED,
  850. },
  851. {
  852. .id = V4L2_CID_AUDIO_MUTE,
  853. .type = V4L2_CTRL_TYPE_BOOLEAN,
  854. .name = "Mute",
  855. .minimum = 0,
  856. .maximum = 1,
  857. .step = 1,
  858. .default_value = 1,
  859. },
  860. {
  861. .id = V4L2_CID_AUDIO_LOUDNESS,
  862. .flags = V4L2_CTRL_FLAG_DISABLED,
  863. },
  864. };
  865. /*
  866. * si470x_vidioc_querycap - query device capabilities
  867. */
  868. static int si470x_vidioc_querycap(struct file *file, void *priv,
  869. struct v4l2_capability *capability)
  870. {
  871. strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver));
  872. strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
  873. sprintf(capability->bus_info, "USB");
  874. capability->version = DRIVER_KERNEL_VERSION;
  875. capability->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
  876. return 0;
  877. }
  878. /*
  879. * si470x_vidioc_g_input - get input
  880. */
  881. static int si470x_vidioc_g_input(struct file *filp, void *priv,
  882. unsigned int *i)
  883. {
  884. *i = 0;
  885. return 0;
  886. }
  887. /*
  888. * si470x_vidioc_s_input - set input
  889. */
  890. static int si470x_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  891. {
  892. if (i != 0)
  893. return -EINVAL;
  894. return 0;
  895. }
  896. /*
  897. * si470x_vidioc_queryctrl - enumerate control items
  898. */
  899. static int si470x_vidioc_queryctrl(struct file *file, void *priv,
  900. struct v4l2_queryctrl *qc)
  901. {
  902. unsigned char i;
  903. int retval = -EINVAL;
  904. for (i = 0; i < ARRAY_SIZE(si470x_v4l2_queryctrl); i++) {
  905. if (qc->id && qc->id == si470x_v4l2_queryctrl[i].id) {
  906. memcpy(qc, &(si470x_v4l2_queryctrl[i]), sizeof(*qc));
  907. retval = 0;
  908. break;
  909. }
  910. }
  911. if (retval < 0)
  912. printk(KERN_WARNING DRIVER_NAME
  913. ": query control failed with %d\n", retval);
  914. return retval;
  915. }
  916. /*
  917. * si470x_vidioc_g_ctrl - get the value of a control
  918. */
  919. static int si470x_vidioc_g_ctrl(struct file *file, void *priv,
  920. struct v4l2_control *ctrl)
  921. {
  922. struct si470x_device *radio = video_get_drvdata(video_devdata(file));
  923. switch (ctrl->id) {
  924. case V4L2_CID_AUDIO_VOLUME:
  925. ctrl->value = radio->registers[SYSCONFIG2] &
  926. SYSCONFIG2_VOLUME;
  927. break;
  928. case V4L2_CID_AUDIO_MUTE:
  929. ctrl->value = ((radio->registers[POWERCFG] &
  930. POWERCFG_DMUTE) == 0) ? 1 : 0;
  931. break;
  932. }
  933. return 0;
  934. }
  935. /*
  936. * si470x_vidioc_s_ctrl - set the value of a control
  937. */
  938. static int si470x_vidioc_s_ctrl(struct file *file, void *priv,
  939. struct v4l2_control *ctrl)
  940. {
  941. struct si470x_device *radio = video_get_drvdata(video_devdata(file));
  942. int retval;
  943. switch (ctrl->id) {
  944. case V4L2_CID_AUDIO_VOLUME:
  945. radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_VOLUME;
  946. radio->registers[SYSCONFIG2] |= ctrl->value;
  947. retval = si470x_set_register(radio, SYSCONFIG2);
  948. break;
  949. case V4L2_CID_AUDIO_MUTE:
  950. if (ctrl->value == 1)
  951. radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
  952. else
  953. radio->registers[POWERCFG] |= POWERCFG_DMUTE;
  954. retval = si470x_set_register(radio, POWERCFG);
  955. break;
  956. default:
  957. retval = -EINVAL;
  958. }
  959. if (retval < 0)
  960. printk(KERN_WARNING DRIVER_NAME
  961. ": set control failed with %d\n", retval);
  962. return retval;
  963. }
  964. /*
  965. * si470x_vidioc_g_audio - get audio attributes
  966. */
  967. static int si470x_vidioc_g_audio(struct file *file, void *priv,
  968. struct v4l2_audio *audio)
  969. {
  970. if (audio->index > 1)
  971. return -EINVAL;
  972. strcpy(audio->name, "Radio");
  973. audio->capability = V4L2_AUDCAP_STEREO;
  974. return 0;
  975. }
  976. /*
  977. * si470x_vidioc_s_audio - set audio attributes
  978. */
  979. static int si470x_vidioc_s_audio(struct file *file, void *priv,
  980. struct v4l2_audio *audio)
  981. {
  982. if (audio->index != 0)
  983. return -EINVAL;
  984. return 0;
  985. }
  986. /*
  987. * si470x_vidioc_g_tuner - get tuner attributes
  988. */
  989. static int si470x_vidioc_g_tuner(struct file *file, void *priv,
  990. struct v4l2_tuner *tuner)
  991. {
  992. struct si470x_device *radio = video_get_drvdata(video_devdata(file));
  993. int retval;
  994. if (tuner->index > 0)
  995. return -EINVAL;
  996. /* read status rssi */
  997. retval = si470x_get_register(radio, STATUSRSSI);
  998. if (retval < 0)
  999. return retval;
  1000. strcpy(tuner->name, "FM");
  1001. tuner->type = V4L2_TUNER_RADIO;
  1002. switch (band) {
  1003. /* 0: 87.5 - 108 MHz (USA, Europe, default) */
  1004. default:
  1005. tuner->rangelow = 87.5 * FREQ_MUL;
  1006. tuner->rangehigh = 108 * FREQ_MUL;
  1007. break;
  1008. /* 1: 76 - 108 MHz (Japan wide band) */
  1009. case 1 :
  1010. tuner->rangelow = 76 * FREQ_MUL;
  1011. tuner->rangehigh = 108 * FREQ_MUL;
  1012. break;
  1013. /* 2: 76 - 90 MHz (Japan) */
  1014. case 2 :
  1015. tuner->rangelow = 76 * FREQ_MUL;
  1016. tuner->rangehigh = 90 * FREQ_MUL;
  1017. break;
  1018. };
  1019. tuner->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
  1020. tuner->capability = V4L2_TUNER_CAP_LOW;
  1021. /* Stereo indicator == Stereo (instead of Mono) */
  1022. if ((radio->registers[STATUSRSSI] & STATUSRSSI_ST) == 1)
  1023. tuner->audmode = V4L2_TUNER_MODE_STEREO;
  1024. else
  1025. tuner->audmode = V4L2_TUNER_MODE_MONO;
  1026. /* min is worst, max is best; signal:0..0xffff; rssi: 0..0xff */
  1027. tuner->signal = (radio->registers[STATUSRSSI] & STATUSRSSI_RSSI)
  1028. * 0x0101;
  1029. /* automatic frequency control: -1: freq to low, 1 freq to high */
  1030. tuner->afc = 0;
  1031. return 0;
  1032. }
  1033. /*
  1034. * si470x_vidioc_s_tuner - set tuner attributes
  1035. */
  1036. static int si470x_vidioc_s_tuner(struct file *file, void *priv,
  1037. struct v4l2_tuner *tuner)
  1038. {
  1039. struct si470x_device *radio = video_get_drvdata(video_devdata(file));
  1040. int retval;
  1041. if (tuner->index > 0)
  1042. return -EINVAL;
  1043. if (tuner->audmode == V4L2_TUNER_MODE_MONO)
  1044. radio->registers[POWERCFG] |= POWERCFG_MONO; /* force mono */
  1045. else
  1046. radio->registers[POWERCFG] &= ~POWERCFG_MONO; /* try stereo */
  1047. retval = si470x_set_register(radio, POWERCFG);
  1048. if (retval < 0)
  1049. printk(KERN_WARNING DRIVER_NAME
  1050. ": set tuner failed with %d\n", retval);
  1051. return retval;
  1052. }
  1053. /*
  1054. * si470x_vidioc_g_frequency - get tuner or modulator radio frequency
  1055. */
  1056. static int si470x_vidioc_g_frequency(struct file *file, void *priv,
  1057. struct v4l2_frequency *freq)
  1058. {
  1059. struct si470x_device *radio = video_get_drvdata(video_devdata(file));
  1060. freq->type = V4L2_TUNER_RADIO;
  1061. freq->frequency = si470x_get_freq(radio);
  1062. return 0;
  1063. }
  1064. /*
  1065. * si470x_vidioc_s_frequency - set tuner or modulator radio frequency
  1066. */
  1067. static int si470x_vidioc_s_frequency(struct file *file, void *priv,
  1068. struct v4l2_frequency *freq)
  1069. {
  1070. struct si470x_device *radio = video_get_drvdata(video_devdata(file));
  1071. int retval;
  1072. if (freq->type != V4L2_TUNER_RADIO)
  1073. return -EINVAL;
  1074. retval = si470x_set_freq(radio, freq->frequency);
  1075. if (retval < 0)
  1076. printk(KERN_WARNING DRIVER_NAME
  1077. ": set frequency failed with %d\n", retval);
  1078. return 0;
  1079. }
  1080. /*
  1081. * si470x_viddev_tamples - video device interface
  1082. */
  1083. static struct video_device si470x_viddev_template = {
  1084. .fops = &si470x_fops,
  1085. .name = DRIVER_NAME,
  1086. .type = VID_TYPE_TUNER,
  1087. .release = video_device_release,
  1088. .vidioc_querycap = si470x_vidioc_querycap,
  1089. .vidioc_g_input = si470x_vidioc_g_input,
  1090. .vidioc_s_input = si470x_vidioc_s_input,
  1091. .vidioc_queryctrl = si470x_vidioc_queryctrl,
  1092. .vidioc_g_ctrl = si470x_vidioc_g_ctrl,
  1093. .vidioc_s_ctrl = si470x_vidioc_s_ctrl,
  1094. .vidioc_g_audio = si470x_vidioc_g_audio,
  1095. .vidioc_s_audio = si470x_vidioc_s_audio,
  1096. .vidioc_g_tuner = si470x_vidioc_g_tuner,
  1097. .vidioc_s_tuner = si470x_vidioc_s_tuner,
  1098. .vidioc_g_frequency = si470x_vidioc_g_frequency,
  1099. .vidioc_s_frequency = si470x_vidioc_s_frequency,
  1100. .owner = THIS_MODULE,
  1101. };
  1102. /**************************************************************************
  1103. * USB Interface
  1104. **************************************************************************/
  1105. /*
  1106. * si470x_usb_driver_probe - probe for the device
  1107. */
  1108. static int si470x_usb_driver_probe(struct usb_interface *intf,
  1109. const struct usb_device_id *id)
  1110. {
  1111. struct si470x_device *radio;
  1112. /* memory and interface allocations */
  1113. radio = kmalloc(sizeof(struct si470x_device), GFP_KERNEL);
  1114. if (!radio)
  1115. return -ENOMEM;
  1116. radio->videodev = video_device_alloc();
  1117. if (!radio->videodev) {
  1118. kfree(radio);
  1119. return -ENOMEM;
  1120. }
  1121. memcpy(radio->videodev, &si470x_viddev_template,
  1122. sizeof(si470x_viddev_template));
  1123. radio->users = 0;
  1124. radio->usbdev = interface_to_usbdev(intf);
  1125. video_set_drvdata(radio->videodev, radio);
  1126. if (video_register_device(radio->videodev, VFL_TYPE_RADIO, radio_nr)) {
  1127. printk(KERN_WARNING DRIVER_NAME
  1128. ": Could not register video device\n");
  1129. video_device_release(radio->videodev);
  1130. kfree(radio);
  1131. return -EIO;
  1132. }
  1133. usb_set_intfdata(intf, radio);
  1134. /* show some infos about the specific device */
  1135. if (si470x_get_all_registers(radio) < 0) {
  1136. video_device_release(radio->videodev);
  1137. kfree(radio);
  1138. return -EIO;
  1139. }
  1140. printk(KERN_INFO DRIVER_NAME ": DeviceID=0x%4.4x ChipID=0x%4.4x\n",
  1141. radio->registers[DEVICEID], radio->registers[CHIPID]);
  1142. /* check if firmware is current */
  1143. if ((radio->registers[CHIPID] & CHIPID_FIRMWARE)
  1144. < RADIO_SW_VERSION_CURRENT)
  1145. printk(KERN_WARNING DRIVER_NAME
  1146. ": This driver is known to work with chip version %d, "
  1147. "but the device has firmware %d.\n"
  1148. DRIVER_NAME
  1149. "If you have some trouble using this driver, please "
  1150. "report to V4L ML at video4linux-list@redhat.com\n",
  1151. radio->registers[CHIPID] & CHIPID_FIRMWARE,
  1152. RADIO_SW_VERSION_CURRENT);
  1153. /* set initial frequency */
  1154. si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
  1155. /* rds initialization */
  1156. radio->buf_size = rds_buf * 3;
  1157. radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
  1158. if (!radio->buffer) {
  1159. video_device_release(radio->videodev);
  1160. kfree(radio);
  1161. return -ENOMEM;
  1162. }
  1163. radio->wr_index = 0;
  1164. radio->rd_index = 0;
  1165. init_waitqueue_head(&radio->read_queue);
  1166. /* prepare polling via eventd */
  1167. INIT_WORK(&radio->work, si470x_work);
  1168. init_timer(&radio->timer);
  1169. radio->timer.function = si470x_timer;
  1170. radio->timer.data = (unsigned long) radio;
  1171. return 0;
  1172. }
  1173. /*
  1174. * si470x_usb_driver_disconnect - disconnect the device
  1175. */
  1176. static void si470x_usb_driver_disconnect(struct usb_interface *intf)
  1177. {
  1178. struct si470x_device *radio = usb_get_intfdata(intf);
  1179. usb_set_intfdata(intf, NULL);
  1180. if (radio) {
  1181. del_timer_sync(&radio->timer);
  1182. flush_scheduled_work();
  1183. video_unregister_device(radio->videodev);
  1184. kfree(radio->buffer);
  1185. kfree(radio);
  1186. }
  1187. }
  1188. /*
  1189. * si470x_usb_driver - usb driver interface
  1190. */
  1191. static struct usb_driver si470x_usb_driver = {
  1192. .name = DRIVER_NAME,
  1193. .probe = si470x_usb_driver_probe,
  1194. .disconnect = si470x_usb_driver_disconnect,
  1195. .id_table = si470x_usb_driver_id_table,
  1196. };
  1197. /**************************************************************************
  1198. * Module Interface
  1199. **************************************************************************/
  1200. /*
  1201. * si470x_module_init - module init
  1202. */
  1203. static int __init si470x_module_init(void)
  1204. {
  1205. printk(KERN_INFO DRIVER_DESC ", Version " DRIVER_VERSION "\n");
  1206. return usb_register(&si470x_usb_driver);
  1207. }
  1208. /*
  1209. * si470x_module_exit - module exit
  1210. */
  1211. static void __exit si470x_module_exit(void)
  1212. {
  1213. usb_deregister(&si470x_usb_driver);
  1214. }
  1215. module_init(si470x_module_init);
  1216. module_exit(si470x_module_exit);
  1217. MODULE_LICENSE("GPL");
  1218. MODULE_AUTHOR(DRIVER_AUTHOR);
  1219. MODULE_DESCRIPTION(DRIVER_DESC);
  1220. MODULE_VERSION(DRIVER_VERSION);