radio-si470x.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891
  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_v4l2_queryctrl - query control
  1030. */
  1031. static struct v4l2_queryctrl si470x_v4l2_queryctrl[] = {
  1032. {
  1033. .id = V4L2_CID_AUDIO_VOLUME,
  1034. .type = V4L2_CTRL_TYPE_INTEGER,
  1035. .name = "Volume",
  1036. .minimum = 0,
  1037. .maximum = 15,
  1038. .step = 1,
  1039. .default_value = 15,
  1040. },
  1041. {
  1042. .id = V4L2_CID_AUDIO_MUTE,
  1043. .type = V4L2_CTRL_TYPE_BOOLEAN,
  1044. .name = "Mute",
  1045. .minimum = 0,
  1046. .maximum = 1,
  1047. .step = 1,
  1048. .default_value = 1,
  1049. },
  1050. };
  1051. /*
  1052. * si470x_vidioc_querycap - query device capabilities
  1053. */
  1054. static int si470x_vidioc_querycap(struct file *file, void *priv,
  1055. struct v4l2_capability *capability)
  1056. {
  1057. struct si470x_device *radio = video_drvdata(file);
  1058. strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver));
  1059. strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
  1060. usb_make_path(radio->usbdev, capability->bus_info, sizeof(capability->bus_info));
  1061. capability->version = DRIVER_KERNEL_VERSION;
  1062. capability->capabilities = V4L2_CAP_HW_FREQ_SEEK |
  1063. V4L2_CAP_TUNER | V4L2_CAP_RADIO;
  1064. return 0;
  1065. }
  1066. /*
  1067. * si470x_vidioc_queryctrl - enumerate control items
  1068. */
  1069. static int si470x_vidioc_queryctrl(struct file *file, void *priv,
  1070. struct v4l2_queryctrl *qc)
  1071. {
  1072. unsigned char i = 0;
  1073. int retval = -EINVAL;
  1074. /* abort if qc->id is below V4L2_CID_BASE */
  1075. if (qc->id < V4L2_CID_BASE)
  1076. goto done;
  1077. /* search video control */
  1078. for (i = 0; i < ARRAY_SIZE(si470x_v4l2_queryctrl); i++) {
  1079. if (qc->id == si470x_v4l2_queryctrl[i].id) {
  1080. memcpy(qc, &(si470x_v4l2_queryctrl[i]), sizeof(*qc));
  1081. retval = 0; /* found */
  1082. break;
  1083. }
  1084. }
  1085. /* disable unsupported base controls */
  1086. /* to satisfy kradio and such apps */
  1087. if ((retval == -EINVAL) && (qc->id < V4L2_CID_LASTP1)) {
  1088. qc->flags = V4L2_CTRL_FLAG_DISABLED;
  1089. retval = 0;
  1090. }
  1091. done:
  1092. if (retval < 0)
  1093. printk(KERN_WARNING DRIVER_NAME
  1094. ": query controls failed with %d\n", retval);
  1095. return retval;
  1096. }
  1097. /*
  1098. * si470x_vidioc_g_ctrl - get the value of a control
  1099. */
  1100. static int si470x_vidioc_g_ctrl(struct file *file, void *priv,
  1101. struct v4l2_control *ctrl)
  1102. {
  1103. struct si470x_device *radio = video_drvdata(file);
  1104. int retval = 0;
  1105. /* safety checks */
  1106. if (radio->disconnected) {
  1107. retval = -EIO;
  1108. goto done;
  1109. }
  1110. switch (ctrl->id) {
  1111. case V4L2_CID_AUDIO_VOLUME:
  1112. ctrl->value = radio->registers[SYSCONFIG2] &
  1113. SYSCONFIG2_VOLUME;
  1114. break;
  1115. case V4L2_CID_AUDIO_MUTE:
  1116. ctrl->value = ((radio->registers[POWERCFG] &
  1117. POWERCFG_DMUTE) == 0) ? 1 : 0;
  1118. break;
  1119. default:
  1120. retval = -EINVAL;
  1121. }
  1122. done:
  1123. if (retval < 0)
  1124. printk(KERN_WARNING DRIVER_NAME
  1125. ": get control failed with %d\n", retval);
  1126. return retval;
  1127. }
  1128. /*
  1129. * si470x_vidioc_s_ctrl - set the value of a control
  1130. */
  1131. static int si470x_vidioc_s_ctrl(struct file *file, void *priv,
  1132. struct v4l2_control *ctrl)
  1133. {
  1134. struct si470x_device *radio = video_drvdata(file);
  1135. int retval = 0;
  1136. /* safety checks */
  1137. if (radio->disconnected) {
  1138. retval = -EIO;
  1139. goto done;
  1140. }
  1141. switch (ctrl->id) {
  1142. case V4L2_CID_AUDIO_VOLUME:
  1143. radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_VOLUME;
  1144. radio->registers[SYSCONFIG2] |= ctrl->value;
  1145. retval = si470x_set_register(radio, SYSCONFIG2);
  1146. break;
  1147. case V4L2_CID_AUDIO_MUTE:
  1148. if (ctrl->value == 1)
  1149. radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
  1150. else
  1151. radio->registers[POWERCFG] |= POWERCFG_DMUTE;
  1152. retval = si470x_set_register(radio, POWERCFG);
  1153. break;
  1154. default:
  1155. retval = -EINVAL;
  1156. }
  1157. done:
  1158. if (retval < 0)
  1159. printk(KERN_WARNING DRIVER_NAME
  1160. ": set control failed with %d\n", retval);
  1161. return retval;
  1162. }
  1163. /*
  1164. * si470x_vidioc_g_audio - get audio attributes
  1165. */
  1166. static int si470x_vidioc_g_audio(struct file *file, void *priv,
  1167. struct v4l2_audio *audio)
  1168. {
  1169. /* driver constants */
  1170. audio->index = 0;
  1171. strcpy(audio->name, "Radio");
  1172. audio->capability = V4L2_AUDCAP_STEREO;
  1173. audio->mode = 0;
  1174. return 0;
  1175. }
  1176. /*
  1177. * si470x_vidioc_g_tuner - get tuner attributes
  1178. */
  1179. static int si470x_vidioc_g_tuner(struct file *file, void *priv,
  1180. struct v4l2_tuner *tuner)
  1181. {
  1182. struct si470x_device *radio = video_drvdata(file);
  1183. int retval = 0;
  1184. /* safety checks */
  1185. if (radio->disconnected) {
  1186. retval = -EIO;
  1187. goto done;
  1188. }
  1189. if (tuner->index != 0) {
  1190. retval = -EINVAL;
  1191. goto done;
  1192. }
  1193. retval = si470x_get_register(radio, STATUSRSSI);
  1194. if (retval < 0)
  1195. goto done;
  1196. /* driver constants */
  1197. strcpy(tuner->name, "FM");
  1198. tuner->type = V4L2_TUNER_RADIO;
  1199. tuner->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
  1200. /* range limits */
  1201. switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
  1202. /* 0: 87.5 - 108 MHz (USA, Europe, default) */
  1203. default:
  1204. tuner->rangelow = 87.5 * FREQ_MUL;
  1205. tuner->rangehigh = 108 * FREQ_MUL;
  1206. break;
  1207. /* 1: 76 - 108 MHz (Japan wide band) */
  1208. case 1 :
  1209. tuner->rangelow = 76 * FREQ_MUL;
  1210. tuner->rangehigh = 108 * FREQ_MUL;
  1211. break;
  1212. /* 2: 76 - 90 MHz (Japan) */
  1213. case 2 :
  1214. tuner->rangelow = 76 * FREQ_MUL;
  1215. tuner->rangehigh = 90 * FREQ_MUL;
  1216. break;
  1217. };
  1218. /* stereo indicator == stereo (instead of mono) */
  1219. if ((radio->registers[STATUSRSSI] & STATUSRSSI_ST) == 0)
  1220. tuner->rxsubchans = V4L2_TUNER_SUB_MONO;
  1221. else
  1222. tuner->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
  1223. /* mono/stereo selector */
  1224. if ((radio->registers[POWERCFG] & POWERCFG_MONO) == 0)
  1225. tuner->audmode = V4L2_TUNER_MODE_STEREO;
  1226. else
  1227. tuner->audmode = V4L2_TUNER_MODE_MONO;
  1228. /* min is worst, max is best; signal:0..0xffff; rssi: 0..0xff */
  1229. /* measured in units of dbµV in 1 db increments (max at ~75 dbµV) */
  1230. tuner->signal = (radio->registers[STATUSRSSI] & STATUSRSSI_RSSI);
  1231. /* the ideal factor is 0xffff/75 = 873,8 */
  1232. tuner->signal = (tuner->signal * 873) + (8 * tuner->signal / 10);
  1233. /* automatic frequency control: -1: freq to low, 1 freq to high */
  1234. /* AFCRL does only indicate that freq. differs, not if too low/high */
  1235. tuner->afc = (radio->registers[STATUSRSSI] & STATUSRSSI_AFCRL) ? 1 : 0;
  1236. done:
  1237. if (retval < 0)
  1238. printk(KERN_WARNING DRIVER_NAME
  1239. ": get tuner failed with %d\n", retval);
  1240. return retval;
  1241. }
  1242. /*
  1243. * si470x_vidioc_s_tuner - set tuner attributes
  1244. */
  1245. static int si470x_vidioc_s_tuner(struct file *file, void *priv,
  1246. struct v4l2_tuner *tuner)
  1247. {
  1248. struct si470x_device *radio = video_drvdata(file);
  1249. int retval = -EINVAL;
  1250. /* safety checks */
  1251. if (radio->disconnected) {
  1252. retval = -EIO;
  1253. goto done;
  1254. }
  1255. if (tuner->index != 0)
  1256. goto done;
  1257. /* mono/stereo selector */
  1258. switch (tuner->audmode) {
  1259. case V4L2_TUNER_MODE_MONO:
  1260. radio->registers[POWERCFG] |= POWERCFG_MONO; /* force mono */
  1261. break;
  1262. case V4L2_TUNER_MODE_STEREO:
  1263. radio->registers[POWERCFG] &= ~POWERCFG_MONO; /* try stereo */
  1264. break;
  1265. default:
  1266. goto done;
  1267. }
  1268. retval = si470x_set_register(radio, POWERCFG);
  1269. done:
  1270. if (retval < 0)
  1271. printk(KERN_WARNING DRIVER_NAME
  1272. ": set tuner failed with %d\n", retval);
  1273. return retval;
  1274. }
  1275. /*
  1276. * si470x_vidioc_g_frequency - get tuner or modulator radio frequency
  1277. */
  1278. static int si470x_vidioc_g_frequency(struct file *file, void *priv,
  1279. struct v4l2_frequency *freq)
  1280. {
  1281. struct si470x_device *radio = video_drvdata(file);
  1282. int retval = 0;
  1283. /* safety checks */
  1284. if (radio->disconnected) {
  1285. retval = -EIO;
  1286. goto done;
  1287. }
  1288. if (freq->tuner != 0) {
  1289. retval = -EINVAL;
  1290. goto done;
  1291. }
  1292. freq->type = V4L2_TUNER_RADIO;
  1293. retval = si470x_get_freq(radio, &freq->frequency);
  1294. done:
  1295. if (retval < 0)
  1296. printk(KERN_WARNING DRIVER_NAME
  1297. ": get frequency failed with %d\n", retval);
  1298. return retval;
  1299. }
  1300. /*
  1301. * si470x_vidioc_s_frequency - set tuner or modulator radio frequency
  1302. */
  1303. static int si470x_vidioc_s_frequency(struct file *file, void *priv,
  1304. struct v4l2_frequency *freq)
  1305. {
  1306. struct si470x_device *radio = video_drvdata(file);
  1307. int retval = 0;
  1308. /* safety checks */
  1309. if (radio->disconnected) {
  1310. retval = -EIO;
  1311. goto done;
  1312. }
  1313. if (freq->tuner != 0) {
  1314. retval = -EINVAL;
  1315. goto done;
  1316. }
  1317. retval = si470x_set_freq(radio, freq->frequency);
  1318. done:
  1319. if (retval < 0)
  1320. printk(KERN_WARNING DRIVER_NAME
  1321. ": set frequency failed with %d\n", retval);
  1322. return retval;
  1323. }
  1324. /*
  1325. * si470x_vidioc_s_hw_freq_seek - set hardware frequency seek
  1326. */
  1327. static int si470x_vidioc_s_hw_freq_seek(struct file *file, void *priv,
  1328. struct v4l2_hw_freq_seek *seek)
  1329. {
  1330. struct si470x_device *radio = video_drvdata(file);
  1331. int retval = 0;
  1332. /* safety checks */
  1333. if (radio->disconnected) {
  1334. retval = -EIO;
  1335. goto done;
  1336. }
  1337. if (seek->tuner != 0) {
  1338. retval = -EINVAL;
  1339. goto done;
  1340. }
  1341. retval = si470x_set_seek(radio, seek->wrap_around, seek->seek_upward);
  1342. done:
  1343. if (retval < 0)
  1344. printk(KERN_WARNING DRIVER_NAME
  1345. ": set hardware frequency seek failed with %d\n",
  1346. retval);
  1347. return retval;
  1348. }
  1349. /*
  1350. * si470x_ioctl_ops - video device ioctl operations
  1351. */
  1352. static const struct v4l2_ioctl_ops si470x_ioctl_ops = {
  1353. .vidioc_querycap = si470x_vidioc_querycap,
  1354. .vidioc_queryctrl = si470x_vidioc_queryctrl,
  1355. .vidioc_g_ctrl = si470x_vidioc_g_ctrl,
  1356. .vidioc_s_ctrl = si470x_vidioc_s_ctrl,
  1357. .vidioc_g_audio = si470x_vidioc_g_audio,
  1358. .vidioc_g_tuner = si470x_vidioc_g_tuner,
  1359. .vidioc_s_tuner = si470x_vidioc_s_tuner,
  1360. .vidioc_g_frequency = si470x_vidioc_g_frequency,
  1361. .vidioc_s_frequency = si470x_vidioc_s_frequency,
  1362. .vidioc_s_hw_freq_seek = si470x_vidioc_s_hw_freq_seek,
  1363. };
  1364. /*
  1365. * si470x_viddev_template - video device interface
  1366. */
  1367. static struct video_device si470x_viddev_template = {
  1368. .fops = &si470x_fops,
  1369. .name = DRIVER_NAME,
  1370. .release = video_device_release,
  1371. .ioctl_ops = &si470x_ioctl_ops,
  1372. };
  1373. /**************************************************************************
  1374. * USB Interface
  1375. **************************************************************************/
  1376. /*
  1377. * si470x_usb_driver_probe - probe for the device
  1378. */
  1379. static int si470x_usb_driver_probe(struct usb_interface *intf,
  1380. const struct usb_device_id *id)
  1381. {
  1382. struct si470x_device *radio;
  1383. struct usb_host_interface *iface_desc;
  1384. struct usb_endpoint_descriptor *endpoint;
  1385. int i, int_end_size, retval = 0;
  1386. /* private data allocation and initialization */
  1387. radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL);
  1388. if (!radio) {
  1389. retval = -ENOMEM;
  1390. goto err_initial;
  1391. }
  1392. radio->users = 0;
  1393. radio->disconnected = 0;
  1394. radio->usbdev = interface_to_usbdev(intf);
  1395. radio->intf = intf;
  1396. mutex_init(&radio->disconnect_lock);
  1397. mutex_init(&radio->lock);
  1398. iface_desc = intf->cur_altsetting;
  1399. /* Set up interrupt endpoint information. */
  1400. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  1401. endpoint = &iface_desc->endpoint[i].desc;
  1402. if (((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
  1403. USB_DIR_IN) && ((endpoint->bmAttributes &
  1404. USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT))
  1405. radio->int_in_endpoint = endpoint;
  1406. }
  1407. if (!radio->int_in_endpoint) {
  1408. printk(KERN_INFO DRIVER_NAME
  1409. ": could not find interrupt in endpoint\n");
  1410. retval = -EIO;
  1411. goto err_radio;
  1412. }
  1413. int_end_size = le16_to_cpu(radio->int_in_endpoint->wMaxPacketSize);
  1414. radio->int_in_buffer = kmalloc(int_end_size, GFP_KERNEL);
  1415. if (!radio->int_in_buffer) {
  1416. printk(KERN_INFO DRIVER_NAME
  1417. "could not allocate int_in_buffer");
  1418. retval = -ENOMEM;
  1419. goto err_radio;
  1420. }
  1421. radio->int_in_urb = usb_alloc_urb(0, GFP_KERNEL);
  1422. if (!radio->int_in_urb) {
  1423. printk(KERN_INFO DRIVER_NAME "could not allocate int_in_urb");
  1424. retval = -ENOMEM;
  1425. goto err_intbuffer;
  1426. }
  1427. /* video device allocation and initialization */
  1428. radio->videodev = video_device_alloc();
  1429. if (!radio->videodev) {
  1430. retval = -ENOMEM;
  1431. goto err_intbuffer;
  1432. }
  1433. memcpy(radio->videodev, &si470x_viddev_template,
  1434. sizeof(si470x_viddev_template));
  1435. video_set_drvdata(radio->videodev, radio);
  1436. /* show some infos about the specific si470x device */
  1437. if (si470x_get_all_registers(radio) < 0) {
  1438. retval = -EIO;
  1439. goto err_video;
  1440. }
  1441. printk(KERN_INFO DRIVER_NAME ": DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",
  1442. radio->registers[DEVICEID], radio->registers[CHIPID]);
  1443. /* get software and hardware versions */
  1444. if (si470x_get_scratch_page_versions(radio) < 0) {
  1445. retval = -EIO;
  1446. goto err_video;
  1447. }
  1448. printk(KERN_INFO DRIVER_NAME
  1449. ": software version %d, hardware version %d\n",
  1450. radio->software_version, radio->hardware_version);
  1451. /* check if device and firmware is current */
  1452. if ((radio->registers[CHIPID] & CHIPID_FIRMWARE)
  1453. < RADIO_SW_VERSION_CURRENT) {
  1454. printk(KERN_WARNING DRIVER_NAME
  1455. ": This driver is known to work with "
  1456. "firmware version %hu,\n", RADIO_SW_VERSION_CURRENT);
  1457. printk(KERN_WARNING DRIVER_NAME
  1458. ": but the device has firmware version %hu.\n",
  1459. radio->registers[CHIPID] & CHIPID_FIRMWARE);
  1460. printk(KERN_WARNING DRIVER_NAME
  1461. ": If you have some trouble using this driver,\n");
  1462. printk(KERN_WARNING DRIVER_NAME
  1463. ": please report to V4L ML at "
  1464. "linux-media@vger.kernel.org\n");
  1465. }
  1466. /* set initial frequency */
  1467. si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
  1468. /* set led to connect state */
  1469. si470x_set_led_state(radio, BLINK_GREEN_LED);
  1470. /* rds buffer allocation */
  1471. radio->buf_size = rds_buf * 3;
  1472. radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
  1473. if (!radio->buffer) {
  1474. retval = -EIO;
  1475. goto err_video;
  1476. }
  1477. /* rds buffer configuration */
  1478. radio->wr_index = 0;
  1479. radio->rd_index = 0;
  1480. init_waitqueue_head(&radio->read_queue);
  1481. /* register video device */
  1482. retval = video_register_device(radio->videodev, VFL_TYPE_RADIO, radio_nr);
  1483. if (retval) {
  1484. printk(KERN_WARNING DRIVER_NAME
  1485. ": Could not register video device\n");
  1486. goto err_all;
  1487. }
  1488. usb_set_intfdata(intf, radio);
  1489. return 0;
  1490. err_all:
  1491. kfree(radio->buffer);
  1492. err_video:
  1493. video_device_release(radio->videodev);
  1494. err_intbuffer:
  1495. kfree(radio->int_in_buffer);
  1496. err_radio:
  1497. kfree(radio);
  1498. err_initial:
  1499. return retval;
  1500. }
  1501. /*
  1502. * si470x_usb_driver_suspend - suspend the device
  1503. */
  1504. static int si470x_usb_driver_suspend(struct usb_interface *intf,
  1505. pm_message_t message)
  1506. {
  1507. printk(KERN_INFO DRIVER_NAME ": suspending now...\n");
  1508. return 0;
  1509. }
  1510. /*
  1511. * si470x_usb_driver_resume - resume the device
  1512. */
  1513. static int si470x_usb_driver_resume(struct usb_interface *intf)
  1514. {
  1515. printk(KERN_INFO DRIVER_NAME ": resuming now...\n");
  1516. return 0;
  1517. }
  1518. /*
  1519. * si470x_usb_driver_disconnect - disconnect the device
  1520. */
  1521. static void si470x_usb_driver_disconnect(struct usb_interface *intf)
  1522. {
  1523. struct si470x_device *radio = usb_get_intfdata(intf);
  1524. mutex_lock(&radio->disconnect_lock);
  1525. radio->disconnected = 1;
  1526. usb_set_intfdata(intf, NULL);
  1527. if (radio->users == 0) {
  1528. /* set led to disconnect state */
  1529. si470x_set_led_state(radio, BLINK_ORANGE_LED);
  1530. /* Free data structures. */
  1531. usb_free_urb(radio->int_in_urb);
  1532. kfree(radio->int_in_buffer);
  1533. video_unregister_device(radio->videodev);
  1534. kfree(radio->buffer);
  1535. kfree(radio);
  1536. }
  1537. mutex_unlock(&radio->disconnect_lock);
  1538. }
  1539. /*
  1540. * si470x_usb_driver - usb driver interface
  1541. */
  1542. static struct usb_driver si470x_usb_driver = {
  1543. .name = DRIVER_NAME,
  1544. .probe = si470x_usb_driver_probe,
  1545. .disconnect = si470x_usb_driver_disconnect,
  1546. .suspend = si470x_usb_driver_suspend,
  1547. .resume = si470x_usb_driver_resume,
  1548. .id_table = si470x_usb_driver_id_table,
  1549. .supports_autosuspend = 1,
  1550. };
  1551. /**************************************************************************
  1552. * Module Interface
  1553. **************************************************************************/
  1554. /*
  1555. * si470x_module_init - module init
  1556. */
  1557. static int __init si470x_module_init(void)
  1558. {
  1559. printk(KERN_INFO DRIVER_DESC ", Version " DRIVER_VERSION "\n");
  1560. return usb_register(&si470x_usb_driver);
  1561. }
  1562. /*
  1563. * si470x_module_exit - module exit
  1564. */
  1565. static void __exit si470x_module_exit(void)
  1566. {
  1567. usb_deregister(&si470x_usb_driver);
  1568. }
  1569. module_init(si470x_module_init);
  1570. module_exit(si470x_module_exit);
  1571. MODULE_LICENSE("GPL");
  1572. MODULE_AUTHOR(DRIVER_AUTHOR);
  1573. MODULE_DESCRIPTION(DRIVER_DESC);
  1574. MODULE_VERSION(DRIVER_VERSION);