radio-si470x.c 45 KB

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