radio-si470x.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869
  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. * - KWorld USB FM Radio SnapMusic Mobile 700 (FM700)
  8. * - Sanei Electric, Inc. FM USB Radio (sold as DealExtreme.com PCear)
  9. *
  10. * Copyright (c) 2009 Tobias Lorenz <tobias.lorenz@gmx.net>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. */
  26. /*
  27. * History:
  28. * 2008-01-12 Tobias Lorenz <tobias.lorenz@gmx.net>
  29. * Version 1.0.0
  30. * - First working version
  31. * 2008-01-13 Tobias Lorenz <tobias.lorenz@gmx.net>
  32. * Version 1.0.1
  33. * - Improved error handling, every function now returns errno
  34. * - Improved multi user access (start/mute/stop)
  35. * - Channel doesn't get lost anymore after start/mute/stop
  36. * - RDS support added (polling mode via interrupt EP 1)
  37. * - marked default module parameters with *value*
  38. * - switched from bit structs to bit masks
  39. * - header file cleaned and integrated
  40. * 2008-01-14 Tobias Lorenz <tobias.lorenz@gmx.net>
  41. * Version 1.0.2
  42. * - hex values are now lower case
  43. * - commented USB ID for ADS/Tech moved on todo list
  44. * - blacklisted si470x in hid-quirks.c
  45. * - rds buffer handling functions integrated into *_work, *_read
  46. * - rds_command in si470x_poll exchanged against simple retval
  47. * - check for firmware version 15
  48. * - code order and prototypes still remain the same
  49. * - spacing and bottom of band codes remain the same
  50. * 2008-01-16 Tobias Lorenz <tobias.lorenz@gmx.net>
  51. * Version 1.0.3
  52. * - code reordered to avoid function prototypes
  53. * - switch/case defaults are now more user-friendly
  54. * - unified comment style
  55. * - applied all checkpatch.pl v1.12 suggestions
  56. * except the warning about the too long lines with bit comments
  57. * - renamed FMRADIO to RADIO to cut line length (checkpatch.pl)
  58. * 2008-01-22 Tobias Lorenz <tobias.lorenz@gmx.net>
  59. * Version 1.0.4
  60. * - avoid poss. locking when doing copy_to_user which may sleep
  61. * - RDS is automatically activated on read now
  62. * - code cleaned of unnecessary rds_commands
  63. * - USB Vendor/Product ID for ADS/Tech FM Radio Receiver verified
  64. * (thanks to Guillaume RAMOUSSE)
  65. * 2008-01-27 Tobias Lorenz <tobias.lorenz@gmx.net>
  66. * Version 1.0.5
  67. * - number of seek_retries changed to tune_timeout
  68. * - fixed problem with incomplete tune operations by own buffers
  69. * - optimization of variables and printf types
  70. * - improved error logging
  71. * 2008-01-31 Tobias Lorenz <tobias.lorenz@gmx.net>
  72. * Oliver Neukum <oliver@neukum.org>
  73. * Version 1.0.6
  74. * - fixed coverity checker warnings in *_usb_driver_disconnect
  75. * - probe()/open() race by correct ordering in probe()
  76. * - DMA coherency rules by separate allocation of all buffers
  77. * - use of endianness macros
  78. * - abuse of spinlock, replaced by mutex
  79. * - racy handling of timer in disconnect,
  80. * replaced by delayed_work
  81. * - racy interruptible_sleep_on(),
  82. * replaced with wait_event_interruptible()
  83. * - handle signals in read()
  84. * 2008-02-08 Tobias Lorenz <tobias.lorenz@gmx.net>
  85. * Oliver Neukum <oliver@neukum.org>
  86. * Version 1.0.7
  87. * - usb autosuspend support
  88. * - unplugging fixed
  89. * 2008-05-07 Tobias Lorenz <tobias.lorenz@gmx.net>
  90. * Version 1.0.8
  91. * - hardware frequency seek support
  92. * - afc indication
  93. * - more safety checks, let si470x_get_freq return errno
  94. * - vidioc behavior corrected according to v4l2 spec
  95. * 2008-10-20 Alexey Klimov <klimov.linux@gmail.com>
  96. * - add support for KWorld USB FM Radio FM700
  97. * - blacklisted KWorld radio in hid-core.c and hid-ids.h
  98. * 2008-12-03 Mark Lord <mlord@pobox.com>
  99. * - add support for DealExtreme USB Radio
  100. * 2009-01-31 Bob Ross <pigiron@gmx.com>
  101. * - correction of stereo detection/setting
  102. * - correction of signal strength indicator scaling
  103. * 2009-01-31 Rick Bronson <rick@efn.org>
  104. * Tobias Lorenz <tobias.lorenz@gmx.net>
  105. * - add LED status output
  106. * - get HW/SW version from scratchpad
  107. * 2009-06-16 Edouard Lafargue <edouard@lafargue.name>
  108. * Version 1.0.10
  109. * - add support for interrupt mode for RDS endpoint,
  110. * instead of polling.
  111. * Improves RDS reception significantly
  112. *
  113. * ToDo:
  114. * - add firmware download/update support
  115. */
  116. /* driver definitions */
  117. #define DRIVER_AUTHOR "Tobias Lorenz <tobias.lorenz@gmx.net>"
  118. #define DRIVER_NAME "radio-si470x"
  119. #define DRIVER_KERNEL_VERSION KERNEL_VERSION(1, 0, 10)
  120. #define DRIVER_CARD "Silicon Labs Si470x FM Radio Receiver"
  121. #define DRIVER_DESC "USB radio driver for Si470x FM Radio Receivers"
  122. #define DRIVER_VERSION "1.0.10"
  123. /* kernel includes */
  124. #include <linux/kernel.h>
  125. #include <linux/module.h>
  126. #include <linux/init.h>
  127. #include <linux/slab.h>
  128. #include <linux/smp_lock.h>
  129. #include <linux/input.h>
  130. #include <linux/usb.h>
  131. #include <linux/hid.h>
  132. #include <linux/version.h>
  133. #include <linux/videodev2.h>
  134. #include <linux/mutex.h>
  135. #include <media/v4l2-common.h>
  136. #include <media/v4l2-ioctl.h>
  137. #include <media/rds.h>
  138. #include <asm/unaligned.h>
  139. /* USB Device ID List */
  140. static struct usb_device_id si470x_usb_driver_id_table[] = {
  141. /* Silicon Labs USB FM Radio Reference Design */
  142. { USB_DEVICE_AND_INTERFACE_INFO(0x10c4, 0x818a, USB_CLASS_HID, 0, 0) },
  143. /* ADS/Tech FM Radio Receiver (formerly Instant FM Music) */
  144. { USB_DEVICE_AND_INTERFACE_INFO(0x06e1, 0xa155, USB_CLASS_HID, 0, 0) },
  145. /* KWorld USB FM Radio SnapMusic Mobile 700 (FM700) */
  146. { USB_DEVICE_AND_INTERFACE_INFO(0x1b80, 0xd700, USB_CLASS_HID, 0, 0) },
  147. /* Sanei Electric, Inc. FM USB Radio (sold as DealExtreme.com PCear) */
  148. { USB_DEVICE_AND_INTERFACE_INFO(0x10c5, 0x819a, USB_CLASS_HID, 0, 0) },
  149. /* Terminating entry */
  150. { }
  151. };
  152. MODULE_DEVICE_TABLE(usb, si470x_usb_driver_id_table);
  153. /**************************************************************************
  154. * Module Parameters
  155. **************************************************************************/
  156. /* Radio Nr */
  157. static int radio_nr = -1;
  158. module_param(radio_nr, int, 0444);
  159. MODULE_PARM_DESC(radio_nr, "Radio Nr");
  160. /* Spacing (kHz) */
  161. /* 0: 200 kHz (USA, Australia) */
  162. /* 1: 100 kHz (Europe, Japan) */
  163. /* 2: 50 kHz */
  164. static unsigned short space = 2;
  165. module_param(space, ushort, 0444);
  166. MODULE_PARM_DESC(space, "Spacing: 0=200kHz 1=100kHz *2=50kHz*");
  167. /* Bottom of Band (MHz) */
  168. /* 0: 87.5 - 108 MHz (USA, Europe)*/
  169. /* 1: 76 - 108 MHz (Japan wide band) */
  170. /* 2: 76 - 90 MHz (Japan) */
  171. static unsigned short band = 1;
  172. module_param(band, ushort, 0444);
  173. MODULE_PARM_DESC(band, "Band: 0=87.5..108MHz *1=76..108MHz* 2=76..90MHz");
  174. /* De-emphasis */
  175. /* 0: 75 us (USA) */
  176. /* 1: 50 us (Europe, Australia, Japan) */
  177. static unsigned short de = 1;
  178. module_param(de, ushort, 0444);
  179. MODULE_PARM_DESC(de, "De-emphasis: 0=75us *1=50us*");
  180. /* USB timeout */
  181. static unsigned int usb_timeout = 500;
  182. module_param(usb_timeout, uint, 0644);
  183. MODULE_PARM_DESC(usb_timeout, "USB timeout (ms): *500*");
  184. /* Tune timeout */
  185. static unsigned int tune_timeout = 3000;
  186. module_param(tune_timeout, uint, 0644);
  187. MODULE_PARM_DESC(tune_timeout, "Tune timeout: *3000*");
  188. /* Seek timeout */
  189. static unsigned int seek_timeout = 5000;
  190. module_param(seek_timeout, uint, 0644);
  191. MODULE_PARM_DESC(seek_timeout, "Seek timeout: *5000*");
  192. /* RDS buffer blocks */
  193. static unsigned int rds_buf = 100;
  194. module_param(rds_buf, uint, 0444);
  195. MODULE_PARM_DESC(rds_buf, "RDS buffer entries: *100*");
  196. /* RDS maximum block errors */
  197. static unsigned short max_rds_errors = 1;
  198. /* 0 means 0 errors requiring correction */
  199. /* 1 means 1-2 errors requiring correction (used by original USBRadio.exe) */
  200. /* 2 means 3-5 errors requiring correction */
  201. /* 3 means 6+ errors or errors in checkword, correction not possible */
  202. module_param(max_rds_errors, ushort, 0644);
  203. MODULE_PARM_DESC(max_rds_errors, "RDS maximum block errors: *1*");
  204. /**************************************************************************
  205. * Register Definitions
  206. **************************************************************************/
  207. #define RADIO_REGISTER_SIZE 2 /* 16 register bit width */
  208. #define RADIO_REGISTER_NUM 16 /* DEVICEID ... RDSD */
  209. #define RDS_REGISTER_NUM 6 /* STATUSRSSI ... RDSD */
  210. #define DEVICEID 0 /* Device ID */
  211. #define DEVICEID_PN 0xf000 /* bits 15..12: Part Number */
  212. #define DEVICEID_MFGID 0x0fff /* bits 11..00: Manufacturer ID */
  213. #define CHIPID 1 /* Chip ID */
  214. #define CHIPID_REV 0xfc00 /* bits 15..10: Chip Version */
  215. #define CHIPID_DEV 0x0200 /* bits 09..09: Device */
  216. #define CHIPID_FIRMWARE 0x01ff /* bits 08..00: Firmware Version */
  217. #define POWERCFG 2 /* Power Configuration */
  218. #define POWERCFG_DSMUTE 0x8000 /* bits 15..15: Softmute Disable */
  219. #define POWERCFG_DMUTE 0x4000 /* bits 14..14: Mute Disable */
  220. #define POWERCFG_MONO 0x2000 /* bits 13..13: Mono Select */
  221. #define POWERCFG_RDSM 0x0800 /* bits 11..11: RDS Mode (Si4701 only) */
  222. #define POWERCFG_SKMODE 0x0400 /* bits 10..10: Seek Mode */
  223. #define POWERCFG_SEEKUP 0x0200 /* bits 09..09: Seek Direction */
  224. #define POWERCFG_SEEK 0x0100 /* bits 08..08: Seek */
  225. #define POWERCFG_DISABLE 0x0040 /* bits 06..06: Powerup Disable */
  226. #define POWERCFG_ENABLE 0x0001 /* bits 00..00: Powerup Enable */
  227. #define CHANNEL 3 /* Channel */
  228. #define CHANNEL_TUNE 0x8000 /* bits 15..15: Tune */
  229. #define CHANNEL_CHAN 0x03ff /* bits 09..00: Channel Select */
  230. #define SYSCONFIG1 4 /* System Configuration 1 */
  231. #define SYSCONFIG1_RDSIEN 0x8000 /* bits 15..15: RDS Interrupt Enable (Si4701 only) */
  232. #define SYSCONFIG1_STCIEN 0x4000 /* bits 14..14: Seek/Tune Complete Interrupt Enable */
  233. #define SYSCONFIG1_RDS 0x1000 /* bits 12..12: RDS Enable (Si4701 only) */
  234. #define SYSCONFIG1_DE 0x0800 /* bits 11..11: De-emphasis (0=75us 1=50us) */
  235. #define SYSCONFIG1_AGCD 0x0400 /* bits 10..10: AGC Disable */
  236. #define SYSCONFIG1_BLNDADJ 0x00c0 /* bits 07..06: Stereo/Mono Blend Level Adjustment */
  237. #define SYSCONFIG1_GPIO3 0x0030 /* bits 05..04: General Purpose I/O 3 */
  238. #define SYSCONFIG1_GPIO2 0x000c /* bits 03..02: General Purpose I/O 2 */
  239. #define SYSCONFIG1_GPIO1 0x0003 /* bits 01..00: General Purpose I/O 1 */
  240. #define SYSCONFIG2 5 /* System Configuration 2 */
  241. #define SYSCONFIG2_SEEKTH 0xff00 /* bits 15..08: RSSI Seek Threshold */
  242. #define SYSCONFIG2_BAND 0x0080 /* bits 07..06: Band Select */
  243. #define SYSCONFIG2_SPACE 0x0030 /* bits 05..04: Channel Spacing */
  244. #define SYSCONFIG2_VOLUME 0x000f /* bits 03..00: Volume */
  245. #define SYSCONFIG3 6 /* System Configuration 3 */
  246. #define SYSCONFIG3_SMUTER 0xc000 /* bits 15..14: Softmute Attack/Recover Rate */
  247. #define SYSCONFIG3_SMUTEA 0x3000 /* bits 13..12: Softmute Attenuation */
  248. #define SYSCONFIG3_SKSNR 0x00f0 /* bits 07..04: Seek SNR Threshold */
  249. #define SYSCONFIG3_SKCNT 0x000f /* bits 03..00: Seek FM Impulse Detection Threshold */
  250. #define TEST1 7 /* Test 1 */
  251. #define TEST1_AHIZEN 0x4000 /* bits 14..14: Audio High-Z Enable */
  252. #define TEST2 8 /* Test 2 */
  253. /* TEST2 only contains reserved bits */
  254. #define BOOTCONFIG 9 /* Boot Configuration */
  255. /* BOOTCONFIG only contains reserved bits */
  256. #define STATUSRSSI 10 /* Status RSSI */
  257. #define STATUSRSSI_RDSR 0x8000 /* bits 15..15: RDS Ready (Si4701 only) */
  258. #define STATUSRSSI_STC 0x4000 /* bits 14..14: Seek/Tune Complete */
  259. #define STATUSRSSI_SF 0x2000 /* bits 13..13: Seek Fail/Band Limit */
  260. #define STATUSRSSI_AFCRL 0x1000 /* bits 12..12: AFC Rail */
  261. #define STATUSRSSI_RDSS 0x0800 /* bits 11..11: RDS Synchronized (Si4701 only) */
  262. #define STATUSRSSI_BLERA 0x0600 /* bits 10..09: RDS Block A Errors (Si4701 only) */
  263. #define STATUSRSSI_ST 0x0100 /* bits 08..08: Stereo Indicator */
  264. #define STATUSRSSI_RSSI 0x00ff /* bits 07..00: RSSI (Received Signal Strength Indicator) */
  265. #define READCHAN 11 /* Read Channel */
  266. #define READCHAN_BLERB 0xc000 /* bits 15..14: RDS Block D Errors (Si4701 only) */
  267. #define READCHAN_BLERC 0x3000 /* bits 13..12: RDS Block C Errors (Si4701 only) */
  268. #define READCHAN_BLERD 0x0c00 /* bits 11..10: RDS Block B Errors (Si4701 only) */
  269. #define READCHAN_READCHAN 0x03ff /* bits 09..00: Read Channel */
  270. #define RDSA 12 /* RDSA */
  271. #define RDSA_RDSA 0xffff /* bits 15..00: RDS Block A Data (Si4701 only) */
  272. #define RDSB 13 /* RDSB */
  273. #define RDSB_RDSB 0xffff /* bits 15..00: RDS Block B Data (Si4701 only) */
  274. #define RDSC 14 /* RDSC */
  275. #define RDSC_RDSC 0xffff /* bits 15..00: RDS Block C Data (Si4701 only) */
  276. #define RDSD 15 /* RDSD */
  277. #define RDSD_RDSD 0xffff /* bits 15..00: RDS Block D Data (Si4701 only) */
  278. /**************************************************************************
  279. * USB HID Reports
  280. **************************************************************************/
  281. /* Reports 1-16 give direct read/write access to the 16 Si470x registers */
  282. /* with the (REPORT_ID - 1) corresponding to the register address across USB */
  283. /* endpoint 0 using GET_REPORT and SET_REPORT */
  284. #define REGISTER_REPORT_SIZE (RADIO_REGISTER_SIZE + 1)
  285. #define REGISTER_REPORT(reg) ((reg) + 1)
  286. /* Report 17 gives direct read/write access to the entire Si470x register */
  287. /* map across endpoint 0 using GET_REPORT and SET_REPORT */
  288. #define ENTIRE_REPORT_SIZE (RADIO_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
  289. #define ENTIRE_REPORT 17
  290. /* Report 18 is used to send the lowest 6 Si470x registers up the HID */
  291. /* interrupt endpoint 1 to Windows every 20 milliseconds for status */
  292. #define RDS_REPORT_SIZE (RDS_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
  293. #define RDS_REPORT 18
  294. /* Report 19: LED state */
  295. #define LED_REPORT_SIZE 3
  296. #define LED_REPORT 19
  297. /* Report 19: stream */
  298. #define STREAM_REPORT_SIZE 3
  299. #define STREAM_REPORT 19
  300. /* Report 20: scratch */
  301. #define SCRATCH_PAGE_SIZE 63
  302. #define SCRATCH_REPORT_SIZE (SCRATCH_PAGE_SIZE + 1)
  303. #define SCRATCH_REPORT 20
  304. /* Reports 19-22: flash upgrade of the C8051F321 */
  305. #define WRITE_REPORT_SIZE 4
  306. #define WRITE_REPORT 19
  307. #define FLASH_REPORT_SIZE 64
  308. #define FLASH_REPORT 20
  309. #define CRC_REPORT_SIZE 3
  310. #define CRC_REPORT 21
  311. #define RESPONSE_REPORT_SIZE 2
  312. #define RESPONSE_REPORT 22
  313. /* Report 23: currently unused, but can accept 60 byte reports on the HID */
  314. /* interrupt out endpoint 2 every 1 millisecond */
  315. #define UNUSED_REPORT 23
  316. /**************************************************************************
  317. * Software/Hardware Versions
  318. **************************************************************************/
  319. #define RADIO_SW_VERSION_NOT_BOOTLOADABLE 6
  320. #define RADIO_SW_VERSION 7
  321. #define RADIO_SW_VERSION_CURRENT 15
  322. #define RADIO_HW_VERSION 1
  323. #define SCRATCH_PAGE_SW_VERSION 1
  324. #define SCRATCH_PAGE_HW_VERSION 2
  325. /**************************************************************************
  326. * LED State Definitions
  327. **************************************************************************/
  328. #define LED_COMMAND 0x35
  329. #define NO_CHANGE_LED 0x00
  330. #define ALL_COLOR_LED 0x01 /* streaming state */
  331. #define BLINK_GREEN_LED 0x02 /* connect state */
  332. #define BLINK_RED_LED 0x04
  333. #define BLINK_ORANGE_LED 0x10 /* disconnect state */
  334. #define SOLID_GREEN_LED 0x20 /* tuning/seeking state */
  335. #define SOLID_RED_LED 0x40 /* bootload state */
  336. #define SOLID_ORANGE_LED 0x80
  337. /**************************************************************************
  338. * Stream State Definitions
  339. **************************************************************************/
  340. #define STREAM_COMMAND 0x36
  341. #define STREAM_VIDPID 0x00
  342. #define STREAM_AUDIO 0xff
  343. /**************************************************************************
  344. * Bootloader / Flash Commands
  345. **************************************************************************/
  346. /* unique id sent to bootloader and required to put into a bootload state */
  347. #define UNIQUE_BL_ID 0x34
  348. /* mask for the flash data */
  349. #define FLASH_DATA_MASK 0x55
  350. /* bootloader commands */
  351. #define GET_SW_VERSION_COMMAND 0x00
  352. #define SET_PAGE_COMMAND 0x01
  353. #define ERASE_PAGE_COMMAND 0x02
  354. #define WRITE_PAGE_COMMAND 0x03
  355. #define CRC_ON_PAGE_COMMAND 0x04
  356. #define READ_FLASH_BYTE_COMMAND 0x05
  357. #define RESET_DEVICE_COMMAND 0x06
  358. #define GET_HW_VERSION_COMMAND 0x07
  359. #define BLANK 0xff
  360. /* bootloader command responses */
  361. #define COMMAND_OK 0x01
  362. #define COMMAND_FAILED 0x02
  363. #define COMMAND_PENDING 0x03
  364. /**************************************************************************
  365. * General Driver Definitions
  366. **************************************************************************/
  367. /*
  368. * si470x_device - private data
  369. */
  370. struct si470x_device {
  371. /* reference to USB and video device */
  372. struct usb_device *usbdev;
  373. struct usb_interface *intf;
  374. struct video_device *videodev;
  375. /* Interrupt endpoint handling */
  376. char *int_in_buffer;
  377. struct usb_endpoint_descriptor *int_in_endpoint;
  378. struct urb *int_in_urb;
  379. int int_in_running;
  380. /* driver management */
  381. unsigned int users;
  382. unsigned char disconnected;
  383. struct mutex disconnect_lock;
  384. /* Silabs internal registers (0..15) */
  385. unsigned short registers[RADIO_REGISTER_NUM];
  386. /* RDS receive buffer */
  387. wait_queue_head_t read_queue;
  388. struct mutex lock; /* buffer locking */
  389. unsigned char *buffer; /* size is always multiple of three */
  390. unsigned int buf_size;
  391. unsigned int rd_index;
  392. unsigned int wr_index;
  393. /* scratch page */
  394. unsigned char software_version;
  395. unsigned char hardware_version;
  396. };
  397. /*
  398. * The frequency is set in units of 62.5 Hz when using V4L2_TUNER_CAP_LOW,
  399. * 62.5 kHz otherwise.
  400. * The tuner is able to have a channel spacing of 50, 100 or 200 kHz.
  401. * tuner->capability is therefore set to V4L2_TUNER_CAP_LOW
  402. * The FREQ_MUL is then: 1 MHz / 62.5 Hz = 16000
  403. */
  404. #define FREQ_MUL (1000000 / 62.5)
  405. /**************************************************************************
  406. * General Driver Functions - REGISTER_REPORTs
  407. **************************************************************************/
  408. /*
  409. * si470x_get_report - receive a HID report
  410. */
  411. static int si470x_get_report(struct si470x_device *radio, void *buf, int size)
  412. {
  413. unsigned char *report = (unsigned char *) buf;
  414. int retval;
  415. retval = usb_control_msg(radio->usbdev,
  416. usb_rcvctrlpipe(radio->usbdev, 0),
  417. HID_REQ_GET_REPORT,
  418. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
  419. report[0], 2,
  420. buf, size, usb_timeout);
  421. if (retval < 0)
  422. printk(KERN_WARNING DRIVER_NAME
  423. ": si470x_get_report: usb_control_msg returned %d\n",
  424. retval);
  425. return retval;
  426. }
  427. /*
  428. * si470x_set_report - send a HID report
  429. */
  430. static int si470x_set_report(struct si470x_device *radio, void *buf, int size)
  431. {
  432. unsigned char *report = (unsigned char *) buf;
  433. int retval;
  434. retval = usb_control_msg(radio->usbdev,
  435. usb_sndctrlpipe(radio->usbdev, 0),
  436. HID_REQ_SET_REPORT,
  437. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
  438. report[0], 2,
  439. buf, size, usb_timeout);
  440. if (retval < 0)
  441. printk(KERN_WARNING DRIVER_NAME
  442. ": si470x_set_report: usb_control_msg returned %d\n",
  443. retval);
  444. return retval;
  445. }
  446. /*
  447. * si470x_get_register - read register
  448. */
  449. static int si470x_get_register(struct si470x_device *radio, int regnr)
  450. {
  451. unsigned char buf[REGISTER_REPORT_SIZE];
  452. int retval;
  453. buf[0] = REGISTER_REPORT(regnr);
  454. retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
  455. if (retval >= 0)
  456. radio->registers[regnr] = get_unaligned_be16(&buf[1]);
  457. return (retval < 0) ? -EINVAL : 0;
  458. }
  459. /*
  460. * si470x_set_register - write register
  461. */
  462. static int si470x_set_register(struct si470x_device *radio, int regnr)
  463. {
  464. unsigned char buf[REGISTER_REPORT_SIZE];
  465. int retval;
  466. buf[0] = REGISTER_REPORT(regnr);
  467. put_unaligned_be16(radio->registers[regnr], &buf[1]);
  468. retval = si470x_set_report(radio, (void *) &buf, sizeof(buf));
  469. return (retval < 0) ? -EINVAL : 0;
  470. }
  471. /*
  472. * si470x_set_chan - set the channel
  473. */
  474. static int si470x_set_chan(struct si470x_device *radio, unsigned short chan)
  475. {
  476. int retval;
  477. unsigned long timeout;
  478. bool timed_out = 0;
  479. /* start tuning */
  480. radio->registers[CHANNEL] &= ~CHANNEL_CHAN;
  481. radio->registers[CHANNEL] |= CHANNEL_TUNE | chan;
  482. retval = si470x_set_register(radio, CHANNEL);
  483. if (retval < 0)
  484. goto done;
  485. /* wait till tune operation has completed */
  486. timeout = jiffies + msecs_to_jiffies(tune_timeout);
  487. do {
  488. retval = si470x_get_register(radio, STATUSRSSI);
  489. if (retval < 0)
  490. goto stop;
  491. timed_out = time_after(jiffies, timeout);
  492. } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
  493. (!timed_out));
  494. if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
  495. printk(KERN_WARNING DRIVER_NAME ": tune does not complete\n");
  496. if (timed_out)
  497. printk(KERN_WARNING DRIVER_NAME
  498. ": tune timed out after %u ms\n", tune_timeout);
  499. stop:
  500. /* stop tuning */
  501. radio->registers[CHANNEL] &= ~CHANNEL_TUNE;
  502. retval = si470x_set_register(radio, CHANNEL);
  503. done:
  504. return retval;
  505. }
  506. /*
  507. * si470x_get_freq - get the frequency
  508. */
  509. static int si470x_get_freq(struct si470x_device *radio, unsigned int *freq)
  510. {
  511. unsigned int spacing, band_bottom;
  512. unsigned short chan;
  513. int retval;
  514. /* Spacing (kHz) */
  515. switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) {
  516. /* 0: 200 kHz (USA, Australia) */
  517. case 0:
  518. spacing = 0.200 * FREQ_MUL; break;
  519. /* 1: 100 kHz (Europe, Japan) */
  520. case 1:
  521. spacing = 0.100 * FREQ_MUL; break;
  522. /* 2: 50 kHz */
  523. default:
  524. spacing = 0.050 * FREQ_MUL; break;
  525. };
  526. /* Bottom of Band (MHz) */
  527. switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
  528. /* 0: 87.5 - 108 MHz (USA, Europe) */
  529. case 0:
  530. band_bottom = 87.5 * FREQ_MUL; break;
  531. /* 1: 76 - 108 MHz (Japan wide band) */
  532. default:
  533. band_bottom = 76 * FREQ_MUL; break;
  534. /* 2: 76 - 90 MHz (Japan) */
  535. case 2:
  536. band_bottom = 76 * FREQ_MUL; break;
  537. };
  538. /* read channel */
  539. retval = si470x_get_register(radio, READCHAN);
  540. chan = radio->registers[READCHAN] & READCHAN_READCHAN;
  541. /* Frequency (MHz) = Spacing (kHz) x Channel + Bottom of Band (MHz) */
  542. *freq = chan * spacing + band_bottom;
  543. return retval;
  544. }
  545. /*
  546. * si470x_set_freq - set the frequency
  547. */
  548. static int si470x_set_freq(struct si470x_device *radio, unsigned int freq)
  549. {
  550. unsigned int spacing, band_bottom;
  551. unsigned short chan;
  552. /* Spacing (kHz) */
  553. switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) {
  554. /* 0: 200 kHz (USA, Australia) */
  555. case 0:
  556. spacing = 0.200 * FREQ_MUL; break;
  557. /* 1: 100 kHz (Europe, Japan) */
  558. case 1:
  559. spacing = 0.100 * FREQ_MUL; break;
  560. /* 2: 50 kHz */
  561. default:
  562. spacing = 0.050 * FREQ_MUL; break;
  563. };
  564. /* Bottom of Band (MHz) */
  565. switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
  566. /* 0: 87.5 - 108 MHz (USA, Europe) */
  567. case 0:
  568. band_bottom = 87.5 * FREQ_MUL; break;
  569. /* 1: 76 - 108 MHz (Japan wide band) */
  570. default:
  571. band_bottom = 76 * FREQ_MUL; break;
  572. /* 2: 76 - 90 MHz (Japan) */
  573. case 2:
  574. band_bottom = 76 * FREQ_MUL; break;
  575. };
  576. /* Chan = [ Freq (Mhz) - Bottom of Band (MHz) ] / Spacing (kHz) */
  577. chan = (freq - band_bottom) / spacing;
  578. return si470x_set_chan(radio, chan);
  579. }
  580. /*
  581. * si470x_set_seek - set seek
  582. */
  583. static int si470x_set_seek(struct si470x_device *radio,
  584. unsigned int wrap_around, unsigned int seek_upward)
  585. {
  586. int retval = 0;
  587. unsigned long timeout;
  588. bool timed_out = 0;
  589. /* start seeking */
  590. radio->registers[POWERCFG] |= POWERCFG_SEEK;
  591. if (wrap_around == 1)
  592. radio->registers[POWERCFG] &= ~POWERCFG_SKMODE;
  593. else
  594. radio->registers[POWERCFG] |= POWERCFG_SKMODE;
  595. if (seek_upward == 1)
  596. radio->registers[POWERCFG] |= POWERCFG_SEEKUP;
  597. else
  598. radio->registers[POWERCFG] &= ~POWERCFG_SEEKUP;
  599. retval = si470x_set_register(radio, POWERCFG);
  600. if (retval < 0)
  601. goto done;
  602. /* wait till seek operation has completed */
  603. timeout = jiffies + msecs_to_jiffies(seek_timeout);
  604. do {
  605. retval = si470x_get_register(radio, STATUSRSSI);
  606. if (retval < 0)
  607. goto stop;
  608. timed_out = time_after(jiffies, timeout);
  609. } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
  610. (!timed_out));
  611. if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
  612. printk(KERN_WARNING DRIVER_NAME ": seek does not complete\n");
  613. if (radio->registers[STATUSRSSI] & STATUSRSSI_SF)
  614. printk(KERN_WARNING DRIVER_NAME
  615. ": seek failed / band limit reached\n");
  616. if (timed_out)
  617. printk(KERN_WARNING DRIVER_NAME
  618. ": seek timed out after %u ms\n", seek_timeout);
  619. stop:
  620. /* stop seeking */
  621. radio->registers[POWERCFG] &= ~POWERCFG_SEEK;
  622. retval = si470x_set_register(radio, POWERCFG);
  623. done:
  624. /* try again, if timed out */
  625. if ((retval == 0) && timed_out)
  626. retval = -EAGAIN;
  627. return retval;
  628. }
  629. /*
  630. * si470x_start - switch on radio
  631. */
  632. static int si470x_start(struct si470x_device *radio)
  633. {
  634. int retval;
  635. /* powercfg */
  636. radio->registers[POWERCFG] =
  637. POWERCFG_DMUTE | POWERCFG_ENABLE | POWERCFG_RDSM;
  638. retval = si470x_set_register(radio, POWERCFG);
  639. if (retval < 0)
  640. goto done;
  641. /* sysconfig 1 */
  642. radio->registers[SYSCONFIG1] = SYSCONFIG1_DE;
  643. retval = si470x_set_register(radio, SYSCONFIG1);
  644. if (retval < 0)
  645. goto done;
  646. /* sysconfig 2 */
  647. radio->registers[SYSCONFIG2] =
  648. (0x3f << 8) | /* SEEKTH */
  649. ((band << 6) & SYSCONFIG2_BAND) | /* BAND */
  650. ((space << 4) & SYSCONFIG2_SPACE) | /* SPACE */
  651. 15; /* VOLUME (max) */
  652. retval = si470x_set_register(radio, SYSCONFIG2);
  653. if (retval < 0)
  654. goto done;
  655. /* reset last channel */
  656. retval = si470x_set_chan(radio,
  657. radio->registers[CHANNEL] & CHANNEL_CHAN);
  658. done:
  659. return retval;
  660. }
  661. /*
  662. * si470x_stop - switch off radio
  663. */
  664. static int si470x_stop(struct si470x_device *radio)
  665. {
  666. int retval;
  667. /* sysconfig 1 */
  668. radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
  669. retval = si470x_set_register(radio, SYSCONFIG1);
  670. if (retval < 0)
  671. goto done;
  672. /* powercfg */
  673. radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
  674. /* POWERCFG_ENABLE has to automatically go low */
  675. radio->registers[POWERCFG] |= POWERCFG_ENABLE | POWERCFG_DISABLE;
  676. retval = si470x_set_register(radio, POWERCFG);
  677. done:
  678. return retval;
  679. }
  680. /*
  681. * si470x_rds_on - switch on rds reception
  682. */
  683. static int si470x_rds_on(struct si470x_device *radio)
  684. {
  685. int retval;
  686. /* sysconfig 1 */
  687. mutex_lock(&radio->lock);
  688. radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDS;
  689. retval = si470x_set_register(radio, SYSCONFIG1);
  690. if (retval < 0)
  691. radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
  692. mutex_unlock(&radio->lock);
  693. return retval;
  694. }
  695. /**************************************************************************
  696. * General Driver Functions - ENTIRE_REPORT
  697. **************************************************************************/
  698. /*
  699. * si470x_get_all_registers - read entire registers
  700. */
  701. static int si470x_get_all_registers(struct si470x_device *radio)
  702. {
  703. unsigned char buf[ENTIRE_REPORT_SIZE];
  704. int retval;
  705. unsigned char regnr;
  706. buf[0] = ENTIRE_REPORT;
  707. retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
  708. if (retval >= 0)
  709. for (regnr = 0; regnr < RADIO_REGISTER_NUM; regnr++)
  710. radio->registers[regnr] = get_unaligned_be16(
  711. &buf[regnr * RADIO_REGISTER_SIZE + 1]);
  712. return (retval < 0) ? -EINVAL : 0;
  713. }
  714. /**************************************************************************
  715. * General Driver Functions - LED_REPORT
  716. **************************************************************************/
  717. /*
  718. * si470x_set_led_state - sets the led state
  719. */
  720. static int si470x_set_led_state(struct si470x_device *radio,
  721. unsigned char led_state)
  722. {
  723. unsigned char buf[LED_REPORT_SIZE];
  724. int retval;
  725. buf[0] = LED_REPORT;
  726. buf[1] = LED_COMMAND;
  727. buf[2] = led_state;
  728. retval = si470x_set_report(radio, (void *) &buf, sizeof(buf));
  729. return (retval < 0) ? -EINVAL : 0;
  730. }
  731. /**************************************************************************
  732. * General Driver Functions - SCRATCH_REPORT
  733. **************************************************************************/
  734. /*
  735. * si470x_get_scratch_versions - gets the scratch page and version infos
  736. */
  737. static int si470x_get_scratch_page_versions(struct si470x_device *radio)
  738. {
  739. unsigned char buf[SCRATCH_REPORT_SIZE];
  740. int retval;
  741. buf[0] = SCRATCH_REPORT;
  742. retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
  743. if (retval < 0)
  744. printk(KERN_WARNING DRIVER_NAME ": si470x_get_scratch: "
  745. "si470x_get_report returned %d\n", retval);
  746. else {
  747. radio->software_version = buf[1];
  748. radio->hardware_version = buf[2];
  749. }
  750. return (retval < 0) ? -EINVAL : 0;
  751. }
  752. /**************************************************************************
  753. * RDS Driver Functions
  754. **************************************************************************/
  755. /*
  756. * si470x_int_in_callback - rds callback and processing function
  757. *
  758. * TODO: do we need to use mutex locks in some sections?
  759. */
  760. static void si470x_int_in_callback(struct urb *urb)
  761. {
  762. struct si470x_device *radio = urb->context;
  763. unsigned char buf[RDS_REPORT_SIZE];
  764. int retval;
  765. unsigned char regnr;
  766. unsigned char blocknum;
  767. unsigned short bler; /* rds block errors */
  768. unsigned short rds;
  769. unsigned char tmpbuf[3];
  770. if (urb->status) {
  771. if (urb->status == -ENOENT ||
  772. urb->status == -ECONNRESET ||
  773. urb->status == -ESHUTDOWN) {
  774. return;
  775. } else {
  776. printk(KERN_WARNING DRIVER_NAME
  777. ": non-zero urb status (%d)\n", urb->status);
  778. goto resubmit; /* Maybe we can recover. */
  779. }
  780. }
  781. /* safety checks */
  782. if (radio->disconnected)
  783. return;
  784. if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
  785. goto resubmit;
  786. if (urb->actual_length > 0) {
  787. /* Update RDS registers with URB data */
  788. buf[0] = RDS_REPORT;
  789. for (regnr = 0; regnr < RDS_REGISTER_NUM; regnr++)
  790. radio->registers[STATUSRSSI + regnr] =
  791. get_unaligned_be16(&radio->int_in_buffer[
  792. regnr * RADIO_REGISTER_SIZE + 1]);
  793. /* get rds blocks */
  794. if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0) {
  795. /* No RDS group ready, better luck next time */
  796. goto resubmit;
  797. }
  798. if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSS) == 0) {
  799. /* RDS decoder not synchronized */
  800. goto resubmit;
  801. }
  802. for (blocknum = 0; blocknum < 4; blocknum++) {
  803. switch (blocknum) {
  804. default:
  805. bler = (radio->registers[STATUSRSSI] &
  806. STATUSRSSI_BLERA) >> 9;
  807. rds = radio->registers[RDSA];
  808. break;
  809. case 1:
  810. bler = (radio->registers[READCHAN] &
  811. READCHAN_BLERB) >> 14;
  812. rds = radio->registers[RDSB];
  813. break;
  814. case 2:
  815. bler = (radio->registers[READCHAN] &
  816. READCHAN_BLERC) >> 12;
  817. rds = radio->registers[RDSC];
  818. break;
  819. case 3:
  820. bler = (radio->registers[READCHAN] &
  821. READCHAN_BLERD) >> 10;
  822. rds = radio->registers[RDSD];
  823. break;
  824. };
  825. /* Fill the V4L2 RDS buffer */
  826. put_unaligned_le16(rds, &tmpbuf);
  827. tmpbuf[2] = blocknum; /* offset name */
  828. tmpbuf[2] |= blocknum << 3; /* received offset */
  829. if (bler > max_rds_errors)
  830. tmpbuf[2] |= 0x80; /* uncorrectable errors */
  831. else if (bler > 0)
  832. tmpbuf[2] |= 0x40; /* corrected error(s) */
  833. /* copy RDS block to internal buffer */
  834. memcpy(&radio->buffer[radio->wr_index], &tmpbuf, 3);
  835. radio->wr_index += 3;
  836. /* wrap write pointer */
  837. if (radio->wr_index >= radio->buf_size)
  838. radio->wr_index = 0;
  839. /* check for overflow */
  840. if (radio->wr_index == radio->rd_index) {
  841. /* increment and wrap read pointer */
  842. radio->rd_index += 3;
  843. if (radio->rd_index >= radio->buf_size)
  844. radio->rd_index = 0;
  845. }
  846. }
  847. if (radio->wr_index != radio->rd_index)
  848. wake_up_interruptible(&radio->read_queue);
  849. }
  850. resubmit:
  851. /* Resubmit if we're still running. */
  852. if (radio->int_in_running && radio->usbdev) {
  853. retval = usb_submit_urb(radio->int_in_urb, GFP_ATOMIC);
  854. if (retval) {
  855. printk(KERN_WARNING DRIVER_NAME
  856. ": resubmitting urb failed (%d)", retval);
  857. radio->int_in_running = 0;
  858. }
  859. }
  860. }
  861. /**************************************************************************
  862. * File Operations Interface
  863. **************************************************************************/
  864. /*
  865. * si470x_fops_read - read RDS data
  866. */
  867. static ssize_t si470x_fops_read(struct file *file, char __user *buf,
  868. size_t count, loff_t *ppos)
  869. {
  870. struct si470x_device *radio = video_drvdata(file);
  871. int retval = 0;
  872. unsigned int block_count = 0;
  873. /* switch on rds reception */
  874. if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
  875. si470x_rds_on(radio);
  876. }
  877. /* block if no new data available */
  878. while (radio->wr_index == radio->rd_index) {
  879. if (file->f_flags & O_NONBLOCK) {
  880. retval = -EWOULDBLOCK;
  881. goto done;
  882. }
  883. if (wait_event_interruptible(radio->read_queue,
  884. radio->wr_index != radio->rd_index) < 0) {
  885. retval = -EINTR;
  886. goto done;
  887. }
  888. }
  889. /* calculate block count from byte count */
  890. count /= 3;
  891. /* copy RDS block out of internal buffer and to user buffer */
  892. mutex_lock(&radio->lock);
  893. while (block_count < count) {
  894. if (radio->rd_index == radio->wr_index)
  895. break;
  896. /* always transfer rds complete blocks */
  897. if (copy_to_user(buf, &radio->buffer[radio->rd_index], 3))
  898. /* retval = -EFAULT; */
  899. break;
  900. /* increment and wrap read pointer */
  901. radio->rd_index += 3;
  902. if (radio->rd_index >= radio->buf_size)
  903. radio->rd_index = 0;
  904. /* increment counters */
  905. block_count++;
  906. buf += 3;
  907. retval += 3;
  908. }
  909. mutex_unlock(&radio->lock);
  910. done:
  911. return retval;
  912. }
  913. /*
  914. * si470x_fops_poll - poll RDS data
  915. */
  916. static unsigned int si470x_fops_poll(struct file *file,
  917. struct poll_table_struct *pts)
  918. {
  919. struct si470x_device *radio = video_drvdata(file);
  920. int retval = 0;
  921. /* switch on rds reception */
  922. if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
  923. si470x_rds_on(radio);
  924. }
  925. poll_wait(file, &radio->read_queue, pts);
  926. if (radio->rd_index != radio->wr_index)
  927. retval = POLLIN | POLLRDNORM;
  928. return retval;
  929. }
  930. /*
  931. * si470x_fops_open - file open
  932. */
  933. static int si470x_fops_open(struct file *file)
  934. {
  935. struct si470x_device *radio = video_drvdata(file);
  936. int retval;
  937. lock_kernel();
  938. radio->users++;
  939. retval = usb_autopm_get_interface(radio->intf);
  940. if (retval < 0) {
  941. radio->users--;
  942. retval = -EIO;
  943. goto done;
  944. }
  945. if (radio->users == 1) {
  946. /* start radio */
  947. retval = si470x_start(radio);
  948. if (retval < 0) {
  949. usb_autopm_put_interface(radio->intf);
  950. goto done;
  951. }
  952. /* initialize interrupt urb */
  953. usb_fill_int_urb(radio->int_in_urb, radio->usbdev,
  954. usb_rcvintpipe(radio->usbdev,
  955. radio->int_in_endpoint->bEndpointAddress),
  956. radio->int_in_buffer,
  957. le16_to_cpu(radio->int_in_endpoint->wMaxPacketSize),
  958. si470x_int_in_callback,
  959. radio,
  960. radio->int_in_endpoint->bInterval);
  961. radio->int_in_running = 1;
  962. mb();
  963. retval = usb_submit_urb(radio->int_in_urb, GFP_KERNEL);
  964. if (retval) {
  965. printk(KERN_INFO DRIVER_NAME
  966. ": submitting int urb failed (%d)\n", retval);
  967. radio->int_in_running = 0;
  968. usb_autopm_put_interface(radio->intf);
  969. }
  970. }
  971. done:
  972. unlock_kernel();
  973. return retval;
  974. }
  975. /*
  976. * si470x_fops_release - file release
  977. */
  978. static int si470x_fops_release(struct file *file)
  979. {
  980. struct si470x_device *radio = video_drvdata(file);
  981. int retval = 0;
  982. /* safety check */
  983. if (!radio) {
  984. retval = -ENODEV;
  985. goto done;
  986. }
  987. mutex_lock(&radio->disconnect_lock);
  988. radio->users--;
  989. if (radio->users == 0) {
  990. /* shutdown interrupt handler */
  991. if (radio->int_in_running) {
  992. radio->int_in_running = 0;
  993. if (radio->int_in_urb)
  994. usb_kill_urb(radio->int_in_urb);
  995. }
  996. if (radio->disconnected) {
  997. video_unregister_device(radio->videodev);
  998. kfree(radio->int_in_buffer);
  999. kfree(radio->buffer);
  1000. kfree(radio);
  1001. goto unlock;
  1002. }
  1003. /* cancel read processes */
  1004. wake_up_interruptible(&radio->read_queue);
  1005. /* stop radio */
  1006. retval = si470x_stop(radio);
  1007. usb_autopm_put_interface(radio->intf);
  1008. }
  1009. unlock:
  1010. mutex_unlock(&radio->disconnect_lock);
  1011. done:
  1012. return retval;
  1013. }
  1014. /*
  1015. * si470x_fops - file operations interface
  1016. */
  1017. static const struct v4l2_file_operations si470x_fops = {
  1018. .owner = THIS_MODULE,
  1019. .read = si470x_fops_read,
  1020. .poll = si470x_fops_poll,
  1021. .ioctl = video_ioctl2,
  1022. .open = si470x_fops_open,
  1023. .release = si470x_fops_release,
  1024. };
  1025. /**************************************************************************
  1026. * Video4Linux Interface
  1027. **************************************************************************/
  1028. /*
  1029. * si470x_vidioc_querycap - query device capabilities
  1030. */
  1031. static int si470x_vidioc_querycap(struct file *file, void *priv,
  1032. struct v4l2_capability *capability)
  1033. {
  1034. struct si470x_device *radio = video_drvdata(file);
  1035. strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver));
  1036. strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
  1037. usb_make_path(radio->usbdev, capability->bus_info, sizeof(capability->bus_info));
  1038. capability->version = DRIVER_KERNEL_VERSION;
  1039. capability->capabilities = V4L2_CAP_HW_FREQ_SEEK |
  1040. V4L2_CAP_TUNER | V4L2_CAP_RADIO | V4L2_CAP_RDS_CAPTURE;
  1041. return 0;
  1042. }
  1043. /*
  1044. * si470x_vidioc_queryctrl - enumerate control items
  1045. */
  1046. static int si470x_vidioc_queryctrl(struct file *file, void *priv,
  1047. struct v4l2_queryctrl *qc)
  1048. {
  1049. int retval = -EINVAL;
  1050. /* abort if qc->id is below V4L2_CID_BASE */
  1051. if (qc->id < V4L2_CID_BASE)
  1052. goto done;
  1053. /* search video control */
  1054. switch (qc->id) {
  1055. case V4L2_CID_AUDIO_VOLUME:
  1056. return v4l2_ctrl_query_fill(qc, 0, 15, 1, 15);
  1057. case V4L2_CID_AUDIO_MUTE:
  1058. return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
  1059. }
  1060. /* disable unsupported base controls */
  1061. /* to satisfy kradio and such apps */
  1062. if ((retval == -EINVAL) && (qc->id < V4L2_CID_LASTP1)) {
  1063. qc->flags = V4L2_CTRL_FLAG_DISABLED;
  1064. retval = 0;
  1065. }
  1066. done:
  1067. if (retval < 0)
  1068. printk(KERN_WARNING DRIVER_NAME
  1069. ": query controls failed with %d\n", retval);
  1070. return retval;
  1071. }
  1072. /*
  1073. * si470x_vidioc_g_ctrl - get the value of a control
  1074. */
  1075. static int si470x_vidioc_g_ctrl(struct file *file, void *priv,
  1076. struct v4l2_control *ctrl)
  1077. {
  1078. struct si470x_device *radio = video_drvdata(file);
  1079. int retval = 0;
  1080. /* safety checks */
  1081. if (radio->disconnected) {
  1082. retval = -EIO;
  1083. goto done;
  1084. }
  1085. switch (ctrl->id) {
  1086. case V4L2_CID_AUDIO_VOLUME:
  1087. ctrl->value = radio->registers[SYSCONFIG2] &
  1088. SYSCONFIG2_VOLUME;
  1089. break;
  1090. case V4L2_CID_AUDIO_MUTE:
  1091. ctrl->value = ((radio->registers[POWERCFG] &
  1092. POWERCFG_DMUTE) == 0) ? 1 : 0;
  1093. break;
  1094. default:
  1095. retval = -EINVAL;
  1096. }
  1097. done:
  1098. if (retval < 0)
  1099. printk(KERN_WARNING DRIVER_NAME
  1100. ": get control failed with %d\n", retval);
  1101. return retval;
  1102. }
  1103. /*
  1104. * si470x_vidioc_s_ctrl - set the value of a control
  1105. */
  1106. static int si470x_vidioc_s_ctrl(struct file *file, void *priv,
  1107. struct v4l2_control *ctrl)
  1108. {
  1109. struct si470x_device *radio = video_drvdata(file);
  1110. int retval = 0;
  1111. /* safety checks */
  1112. if (radio->disconnected) {
  1113. retval = -EIO;
  1114. goto done;
  1115. }
  1116. switch (ctrl->id) {
  1117. case V4L2_CID_AUDIO_VOLUME:
  1118. radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_VOLUME;
  1119. radio->registers[SYSCONFIG2] |= ctrl->value;
  1120. retval = si470x_set_register(radio, SYSCONFIG2);
  1121. break;
  1122. case V4L2_CID_AUDIO_MUTE:
  1123. if (ctrl->value == 1)
  1124. radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
  1125. else
  1126. radio->registers[POWERCFG] |= POWERCFG_DMUTE;
  1127. retval = si470x_set_register(radio, POWERCFG);
  1128. break;
  1129. default:
  1130. retval = -EINVAL;
  1131. }
  1132. done:
  1133. if (retval < 0)
  1134. printk(KERN_WARNING DRIVER_NAME
  1135. ": set control failed with %d\n", retval);
  1136. return retval;
  1137. }
  1138. /*
  1139. * si470x_vidioc_g_audio - get audio attributes
  1140. */
  1141. static int si470x_vidioc_g_audio(struct file *file, void *priv,
  1142. struct v4l2_audio *audio)
  1143. {
  1144. /* driver constants */
  1145. audio->index = 0;
  1146. strcpy(audio->name, "Radio");
  1147. audio->capability = V4L2_AUDCAP_STEREO;
  1148. audio->mode = 0;
  1149. return 0;
  1150. }
  1151. /*
  1152. * si470x_vidioc_g_tuner - get tuner attributes
  1153. */
  1154. static int si470x_vidioc_g_tuner(struct file *file, void *priv,
  1155. struct v4l2_tuner *tuner)
  1156. {
  1157. struct si470x_device *radio = video_drvdata(file);
  1158. int retval = 0;
  1159. /* safety checks */
  1160. if (radio->disconnected) {
  1161. retval = -EIO;
  1162. goto done;
  1163. }
  1164. if (tuner->index != 0) {
  1165. retval = -EINVAL;
  1166. goto done;
  1167. }
  1168. retval = si470x_get_register(radio, STATUSRSSI);
  1169. if (retval < 0)
  1170. goto done;
  1171. /* driver constants */
  1172. strcpy(tuner->name, "FM");
  1173. tuner->type = V4L2_TUNER_RADIO;
  1174. tuner->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
  1175. V4L2_TUNER_CAP_RDS;
  1176. /* range limits */
  1177. switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
  1178. /* 0: 87.5 - 108 MHz (USA, Europe, default) */
  1179. default:
  1180. tuner->rangelow = 87.5 * FREQ_MUL;
  1181. tuner->rangehigh = 108 * FREQ_MUL;
  1182. break;
  1183. /* 1: 76 - 108 MHz (Japan wide band) */
  1184. case 1 :
  1185. tuner->rangelow = 76 * FREQ_MUL;
  1186. tuner->rangehigh = 108 * FREQ_MUL;
  1187. break;
  1188. /* 2: 76 - 90 MHz (Japan) */
  1189. case 2 :
  1190. tuner->rangelow = 76 * FREQ_MUL;
  1191. tuner->rangehigh = 90 * FREQ_MUL;
  1192. break;
  1193. };
  1194. /* stereo indicator == stereo (instead of mono) */
  1195. if ((radio->registers[STATUSRSSI] & STATUSRSSI_ST) == 0)
  1196. tuner->rxsubchans = V4L2_TUNER_SUB_MONO;
  1197. else
  1198. tuner->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
  1199. /* If there is a reliable method of detecting an RDS channel,
  1200. then this code should check for that before setting this
  1201. RDS subchannel. */
  1202. tuner->rxsubchans |= V4L2_TUNER_SUB_RDS;
  1203. /* mono/stereo selector */
  1204. if ((radio->registers[POWERCFG] & POWERCFG_MONO) == 0)
  1205. tuner->audmode = V4L2_TUNER_MODE_STEREO;
  1206. else
  1207. tuner->audmode = V4L2_TUNER_MODE_MONO;
  1208. /* min is worst, max is best; signal:0..0xffff; rssi: 0..0xff */
  1209. /* measured in units of dbµV in 1 db increments (max at ~75 dbµV) */
  1210. tuner->signal = (radio->registers[STATUSRSSI] & STATUSRSSI_RSSI);
  1211. /* the ideal factor is 0xffff/75 = 873,8 */
  1212. tuner->signal = (tuner->signal * 873) + (8 * tuner->signal / 10);
  1213. /* automatic frequency control: -1: freq to low, 1 freq to high */
  1214. /* AFCRL does only indicate that freq. differs, not if too low/high */
  1215. tuner->afc = (radio->registers[STATUSRSSI] & STATUSRSSI_AFCRL) ? 1 : 0;
  1216. done:
  1217. if (retval < 0)
  1218. printk(KERN_WARNING DRIVER_NAME
  1219. ": get tuner failed with %d\n", retval);
  1220. return retval;
  1221. }
  1222. /*
  1223. * si470x_vidioc_s_tuner - set tuner attributes
  1224. */
  1225. static int si470x_vidioc_s_tuner(struct file *file, void *priv,
  1226. struct v4l2_tuner *tuner)
  1227. {
  1228. struct si470x_device *radio = video_drvdata(file);
  1229. int retval = -EINVAL;
  1230. /* safety checks */
  1231. if (radio->disconnected) {
  1232. retval = -EIO;
  1233. goto done;
  1234. }
  1235. if (tuner->index != 0)
  1236. goto done;
  1237. /* mono/stereo selector */
  1238. switch (tuner->audmode) {
  1239. case V4L2_TUNER_MODE_MONO:
  1240. radio->registers[POWERCFG] |= POWERCFG_MONO; /* force mono */
  1241. break;
  1242. case V4L2_TUNER_MODE_STEREO:
  1243. radio->registers[POWERCFG] &= ~POWERCFG_MONO; /* try stereo */
  1244. break;
  1245. default:
  1246. goto done;
  1247. }
  1248. retval = si470x_set_register(radio, POWERCFG);
  1249. done:
  1250. if (retval < 0)
  1251. printk(KERN_WARNING DRIVER_NAME
  1252. ": set tuner failed with %d\n", retval);
  1253. return retval;
  1254. }
  1255. /*
  1256. * si470x_vidioc_g_frequency - get tuner or modulator radio frequency
  1257. */
  1258. static int si470x_vidioc_g_frequency(struct file *file, void *priv,
  1259. struct v4l2_frequency *freq)
  1260. {
  1261. struct si470x_device *radio = video_drvdata(file);
  1262. int retval = 0;
  1263. /* safety checks */
  1264. if (radio->disconnected) {
  1265. retval = -EIO;
  1266. goto done;
  1267. }
  1268. if (freq->tuner != 0) {
  1269. retval = -EINVAL;
  1270. goto done;
  1271. }
  1272. freq->type = V4L2_TUNER_RADIO;
  1273. retval = si470x_get_freq(radio, &freq->frequency);
  1274. done:
  1275. if (retval < 0)
  1276. printk(KERN_WARNING DRIVER_NAME
  1277. ": get frequency failed with %d\n", retval);
  1278. return retval;
  1279. }
  1280. /*
  1281. * si470x_vidioc_s_frequency - set tuner or modulator radio frequency
  1282. */
  1283. static int si470x_vidioc_s_frequency(struct file *file, void *priv,
  1284. struct v4l2_frequency *freq)
  1285. {
  1286. struct si470x_device *radio = video_drvdata(file);
  1287. int retval = 0;
  1288. /* safety checks */
  1289. if (radio->disconnected) {
  1290. retval = -EIO;
  1291. goto done;
  1292. }
  1293. if (freq->tuner != 0) {
  1294. retval = -EINVAL;
  1295. goto done;
  1296. }
  1297. retval = si470x_set_freq(radio, freq->frequency);
  1298. done:
  1299. if (retval < 0)
  1300. printk(KERN_WARNING DRIVER_NAME
  1301. ": set frequency failed with %d\n", retval);
  1302. return retval;
  1303. }
  1304. /*
  1305. * si470x_vidioc_s_hw_freq_seek - set hardware frequency seek
  1306. */
  1307. static int si470x_vidioc_s_hw_freq_seek(struct file *file, void *priv,
  1308. struct v4l2_hw_freq_seek *seek)
  1309. {
  1310. struct si470x_device *radio = video_drvdata(file);
  1311. int retval = 0;
  1312. /* safety checks */
  1313. if (radio->disconnected) {
  1314. retval = -EIO;
  1315. goto done;
  1316. }
  1317. if (seek->tuner != 0) {
  1318. retval = -EINVAL;
  1319. goto done;
  1320. }
  1321. retval = si470x_set_seek(radio, seek->wrap_around, seek->seek_upward);
  1322. done:
  1323. if (retval < 0)
  1324. printk(KERN_WARNING DRIVER_NAME
  1325. ": set hardware frequency seek failed with %d\n",
  1326. retval);
  1327. return retval;
  1328. }
  1329. /*
  1330. * si470x_ioctl_ops - video device ioctl operations
  1331. */
  1332. static const struct v4l2_ioctl_ops si470x_ioctl_ops = {
  1333. .vidioc_querycap = si470x_vidioc_querycap,
  1334. .vidioc_queryctrl = si470x_vidioc_queryctrl,
  1335. .vidioc_g_ctrl = si470x_vidioc_g_ctrl,
  1336. .vidioc_s_ctrl = si470x_vidioc_s_ctrl,
  1337. .vidioc_g_audio = si470x_vidioc_g_audio,
  1338. .vidioc_g_tuner = si470x_vidioc_g_tuner,
  1339. .vidioc_s_tuner = si470x_vidioc_s_tuner,
  1340. .vidioc_g_frequency = si470x_vidioc_g_frequency,
  1341. .vidioc_s_frequency = si470x_vidioc_s_frequency,
  1342. .vidioc_s_hw_freq_seek = si470x_vidioc_s_hw_freq_seek,
  1343. };
  1344. /*
  1345. * si470x_viddev_template - video device interface
  1346. */
  1347. static struct video_device si470x_viddev_template = {
  1348. .fops = &si470x_fops,
  1349. .name = DRIVER_NAME,
  1350. .release = video_device_release,
  1351. .ioctl_ops = &si470x_ioctl_ops,
  1352. };
  1353. /**************************************************************************
  1354. * USB Interface
  1355. **************************************************************************/
  1356. /*
  1357. * si470x_usb_driver_probe - probe for the device
  1358. */
  1359. static int si470x_usb_driver_probe(struct usb_interface *intf,
  1360. const struct usb_device_id *id)
  1361. {
  1362. struct si470x_device *radio;
  1363. struct usb_host_interface *iface_desc;
  1364. struct usb_endpoint_descriptor *endpoint;
  1365. int i, int_end_size, retval = 0;
  1366. /* private data allocation and initialization */
  1367. radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL);
  1368. if (!radio) {
  1369. retval = -ENOMEM;
  1370. goto err_initial;
  1371. }
  1372. radio->users = 0;
  1373. radio->disconnected = 0;
  1374. radio->usbdev = interface_to_usbdev(intf);
  1375. radio->intf = intf;
  1376. mutex_init(&radio->disconnect_lock);
  1377. mutex_init(&radio->lock);
  1378. iface_desc = intf->cur_altsetting;
  1379. /* Set up interrupt endpoint information. */
  1380. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  1381. endpoint = &iface_desc->endpoint[i].desc;
  1382. if (((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
  1383. USB_DIR_IN) && ((endpoint->bmAttributes &
  1384. USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT))
  1385. radio->int_in_endpoint = endpoint;
  1386. }
  1387. if (!radio->int_in_endpoint) {
  1388. printk(KERN_INFO DRIVER_NAME
  1389. ": could not find interrupt in endpoint\n");
  1390. retval = -EIO;
  1391. goto err_radio;
  1392. }
  1393. int_end_size = le16_to_cpu(radio->int_in_endpoint->wMaxPacketSize);
  1394. radio->int_in_buffer = kmalloc(int_end_size, GFP_KERNEL);
  1395. if (!radio->int_in_buffer) {
  1396. printk(KERN_INFO DRIVER_NAME
  1397. "could not allocate int_in_buffer");
  1398. retval = -ENOMEM;
  1399. goto err_radio;
  1400. }
  1401. radio->int_in_urb = usb_alloc_urb(0, GFP_KERNEL);
  1402. if (!radio->int_in_urb) {
  1403. printk(KERN_INFO DRIVER_NAME "could not allocate int_in_urb");
  1404. retval = -ENOMEM;
  1405. goto err_intbuffer;
  1406. }
  1407. /* video device allocation and initialization */
  1408. radio->videodev = video_device_alloc();
  1409. if (!radio->videodev) {
  1410. retval = -ENOMEM;
  1411. goto err_intbuffer;
  1412. }
  1413. memcpy(radio->videodev, &si470x_viddev_template,
  1414. sizeof(si470x_viddev_template));
  1415. video_set_drvdata(radio->videodev, radio);
  1416. /* show some infos about the specific si470x device */
  1417. if (si470x_get_all_registers(radio) < 0) {
  1418. retval = -EIO;
  1419. goto err_video;
  1420. }
  1421. printk(KERN_INFO DRIVER_NAME ": DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",
  1422. radio->registers[DEVICEID], radio->registers[CHIPID]);
  1423. /* get software and hardware versions */
  1424. if (si470x_get_scratch_page_versions(radio) < 0) {
  1425. retval = -EIO;
  1426. goto err_video;
  1427. }
  1428. printk(KERN_INFO DRIVER_NAME
  1429. ": software version %d, hardware version %d\n",
  1430. radio->software_version, radio->hardware_version);
  1431. /* check if device and firmware is current */
  1432. if ((radio->registers[CHIPID] & CHIPID_FIRMWARE)
  1433. < RADIO_SW_VERSION_CURRENT) {
  1434. printk(KERN_WARNING DRIVER_NAME
  1435. ": This driver is known to work with "
  1436. "firmware version %hu,\n", RADIO_SW_VERSION_CURRENT);
  1437. printk(KERN_WARNING DRIVER_NAME
  1438. ": but the device has firmware version %hu.\n",
  1439. radio->registers[CHIPID] & CHIPID_FIRMWARE);
  1440. printk(KERN_WARNING DRIVER_NAME
  1441. ": If you have some trouble using this driver,\n");
  1442. printk(KERN_WARNING DRIVER_NAME
  1443. ": please report to V4L ML at "
  1444. "linux-media@vger.kernel.org\n");
  1445. }
  1446. /* set initial frequency */
  1447. si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
  1448. /* set led to connect state */
  1449. si470x_set_led_state(radio, BLINK_GREEN_LED);
  1450. /* rds buffer allocation */
  1451. radio->buf_size = rds_buf * 3;
  1452. radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
  1453. if (!radio->buffer) {
  1454. retval = -EIO;
  1455. goto err_video;
  1456. }
  1457. /* rds buffer configuration */
  1458. radio->wr_index = 0;
  1459. radio->rd_index = 0;
  1460. init_waitqueue_head(&radio->read_queue);
  1461. /* register video device */
  1462. retval = video_register_device(radio->videodev, VFL_TYPE_RADIO, radio_nr);
  1463. if (retval) {
  1464. printk(KERN_WARNING DRIVER_NAME
  1465. ": Could not register video device\n");
  1466. goto err_all;
  1467. }
  1468. usb_set_intfdata(intf, radio);
  1469. return 0;
  1470. err_all:
  1471. kfree(radio->buffer);
  1472. err_video:
  1473. video_device_release(radio->videodev);
  1474. err_intbuffer:
  1475. kfree(radio->int_in_buffer);
  1476. err_radio:
  1477. kfree(radio);
  1478. err_initial:
  1479. return retval;
  1480. }
  1481. /*
  1482. * si470x_usb_driver_suspend - suspend the device
  1483. */
  1484. static int si470x_usb_driver_suspend(struct usb_interface *intf,
  1485. pm_message_t message)
  1486. {
  1487. printk(KERN_INFO DRIVER_NAME ": suspending now...\n");
  1488. return 0;
  1489. }
  1490. /*
  1491. * si470x_usb_driver_resume - resume the device
  1492. */
  1493. static int si470x_usb_driver_resume(struct usb_interface *intf)
  1494. {
  1495. printk(KERN_INFO DRIVER_NAME ": resuming now...\n");
  1496. return 0;
  1497. }
  1498. /*
  1499. * si470x_usb_driver_disconnect - disconnect the device
  1500. */
  1501. static void si470x_usb_driver_disconnect(struct usb_interface *intf)
  1502. {
  1503. struct si470x_device *radio = usb_get_intfdata(intf);
  1504. mutex_lock(&radio->disconnect_lock);
  1505. radio->disconnected = 1;
  1506. usb_set_intfdata(intf, NULL);
  1507. if (radio->users == 0) {
  1508. /* set led to disconnect state */
  1509. si470x_set_led_state(radio, BLINK_ORANGE_LED);
  1510. /* Free data structures. */
  1511. usb_free_urb(radio->int_in_urb);
  1512. kfree(radio->int_in_buffer);
  1513. video_unregister_device(radio->videodev);
  1514. kfree(radio->buffer);
  1515. kfree(radio);
  1516. }
  1517. mutex_unlock(&radio->disconnect_lock);
  1518. }
  1519. /*
  1520. * si470x_usb_driver - usb driver interface
  1521. */
  1522. static struct usb_driver si470x_usb_driver = {
  1523. .name = DRIVER_NAME,
  1524. .probe = si470x_usb_driver_probe,
  1525. .disconnect = si470x_usb_driver_disconnect,
  1526. .suspend = si470x_usb_driver_suspend,
  1527. .resume = si470x_usb_driver_resume,
  1528. .id_table = si470x_usb_driver_id_table,
  1529. .supports_autosuspend = 1,
  1530. };
  1531. /**************************************************************************
  1532. * Module Interface
  1533. **************************************************************************/
  1534. /*
  1535. * si470x_module_init - module init
  1536. */
  1537. static int __init si470x_module_init(void)
  1538. {
  1539. printk(KERN_INFO DRIVER_DESC ", Version " DRIVER_VERSION "\n");
  1540. return usb_register(&si470x_usb_driver);
  1541. }
  1542. /*
  1543. * si470x_module_exit - module exit
  1544. */
  1545. static void __exit si470x_module_exit(void)
  1546. {
  1547. usb_deregister(&si470x_usb_driver);
  1548. }
  1549. module_init(si470x_module_init);
  1550. module_exit(si470x_module_exit);
  1551. MODULE_LICENSE("GPL");
  1552. MODULE_AUTHOR(DRIVER_AUTHOR);
  1553. MODULE_DESCRIPTION(DRIVER_DESC);
  1554. MODULE_VERSION(DRIVER_VERSION);