radio-si470x.c 43 KB

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