radio-si470x.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606
  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. /* Silabs internal registers (0..15) */
  356. unsigned short registers[RADIO_REGISTER_NUM];
  357. /* RDS receive buffer */
  358. struct delayed_work work;
  359. wait_queue_head_t read_queue;
  360. struct mutex lock; /* buffer locking */
  361. unsigned char *buffer; /* size is always multiple of three */
  362. unsigned int buf_size;
  363. unsigned int rd_index;
  364. unsigned int wr_index;
  365. };
  366. /*
  367. * Lock to prevent kfree of data before all users have releases the device.
  368. */
  369. static DEFINE_MUTEX(open_close_lock);
  370. /*
  371. * The frequency is set in units of 62.5 Hz when using V4L2_TUNER_CAP_LOW,
  372. * 62.5 kHz otherwise.
  373. * The tuner is able to have a channel spacing of 50, 100 or 200 kHz.
  374. * tuner->capability is therefore set to V4L2_TUNER_CAP_LOW
  375. * The FREQ_MUL is then: 1 MHz / 62.5 Hz = 16000
  376. */
  377. #define FREQ_MUL (1000000 / 62.5)
  378. /**************************************************************************
  379. * General Driver Functions
  380. **************************************************************************/
  381. /*
  382. * si470x_get_report - receive a HID report
  383. */
  384. static int si470x_get_report(struct si470x_device *radio, void *buf, int size)
  385. {
  386. unsigned char *report = (unsigned char *) buf;
  387. int retval;
  388. retval = usb_control_msg(radio->usbdev,
  389. usb_rcvctrlpipe(radio->usbdev, 0),
  390. HID_REQ_GET_REPORT,
  391. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
  392. report[0], 2,
  393. buf, size, usb_timeout);
  394. if (retval < 0)
  395. printk(KERN_WARNING DRIVER_NAME
  396. ": si470x_get_report: usb_control_msg returned %d\n",
  397. retval);
  398. return retval;
  399. }
  400. /*
  401. * si470x_set_report - send a HID report
  402. */
  403. static int si470x_set_report(struct si470x_device *radio, void *buf, int size)
  404. {
  405. unsigned char *report = (unsigned char *) buf;
  406. int retval;
  407. retval = usb_control_msg(radio->usbdev,
  408. usb_sndctrlpipe(radio->usbdev, 0),
  409. HID_REQ_SET_REPORT,
  410. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
  411. report[0], 2,
  412. buf, size, usb_timeout);
  413. if (retval < 0)
  414. printk(KERN_WARNING DRIVER_NAME
  415. ": si470x_set_report: usb_control_msg returned %d\n",
  416. retval);
  417. return retval;
  418. }
  419. /*
  420. * si470x_get_register - read register
  421. */
  422. static int si470x_get_register(struct si470x_device *radio, int regnr)
  423. {
  424. unsigned char buf[REGISTER_REPORT_SIZE];
  425. int retval;
  426. buf[0] = REGISTER_REPORT(regnr);
  427. retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
  428. if (retval >= 0)
  429. radio->registers[regnr] = be16_to_cpu(get_unaligned(
  430. (unsigned short *) &buf[1]));
  431. return (retval < 0) ? -EINVAL : 0;
  432. }
  433. /*
  434. * si470x_set_register - write register
  435. */
  436. static int si470x_set_register(struct si470x_device *radio, int regnr)
  437. {
  438. unsigned char buf[REGISTER_REPORT_SIZE];
  439. int retval;
  440. buf[0] = REGISTER_REPORT(regnr);
  441. put_unaligned(cpu_to_be16(radio->registers[regnr]),
  442. (unsigned short *) &buf[1]);
  443. retval = si470x_set_report(radio, (void *) &buf, sizeof(buf));
  444. return (retval < 0) ? -EINVAL : 0;
  445. }
  446. /*
  447. * si470x_get_all_registers - read entire registers
  448. */
  449. static int si470x_get_all_registers(struct si470x_device *radio)
  450. {
  451. unsigned char buf[ENTIRE_REPORT_SIZE];
  452. int retval;
  453. unsigned char regnr;
  454. buf[0] = ENTIRE_REPORT;
  455. retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
  456. if (retval >= 0)
  457. for (regnr = 0; regnr < RADIO_REGISTER_NUM; regnr++)
  458. radio->registers[regnr] = be16_to_cpu(get_unaligned(
  459. (unsigned short *)
  460. &buf[regnr * RADIO_REGISTER_SIZE + 1]));
  461. return (retval < 0) ? -EINVAL : 0;
  462. }
  463. /*
  464. * si470x_get_rds_registers - read rds registers
  465. */
  466. static int si470x_get_rds_registers(struct si470x_device *radio)
  467. {
  468. unsigned char buf[RDS_REPORT_SIZE];
  469. int retval;
  470. int size;
  471. unsigned char regnr;
  472. buf[0] = RDS_REPORT;
  473. retval = usb_interrupt_msg(radio->usbdev,
  474. usb_rcvintpipe(radio->usbdev, 1),
  475. (void *) &buf, sizeof(buf), &size, usb_timeout);
  476. if (size != sizeof(buf))
  477. printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: "
  478. "return size differs: %d != %zu\n", size, sizeof(buf));
  479. if (retval < 0)
  480. printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: "
  481. "usb_interrupt_msg returned %d\n", retval);
  482. if (retval >= 0)
  483. for (regnr = 0; regnr < RDS_REGISTER_NUM; regnr++)
  484. radio->registers[STATUSRSSI + regnr] =
  485. be16_to_cpu(get_unaligned((unsigned short *)
  486. &buf[regnr * RADIO_REGISTER_SIZE + 1]));
  487. return (retval < 0) ? -EINVAL : 0;
  488. }
  489. /*
  490. * si470x_set_chan - set the channel
  491. */
  492. static int si470x_set_chan(struct si470x_device *radio, unsigned short chan)
  493. {
  494. int retval;
  495. unsigned long timeout;
  496. bool timed_out = 0;
  497. /* start tuning */
  498. radio->registers[CHANNEL] &= ~CHANNEL_CHAN;
  499. radio->registers[CHANNEL] |= CHANNEL_TUNE | chan;
  500. retval = si470x_set_register(radio, CHANNEL);
  501. if (retval < 0)
  502. return retval;
  503. /* wait till seek operation has completed */
  504. timeout = jiffies + msecs_to_jiffies(tune_timeout);
  505. do {
  506. retval = si470x_get_register(radio, STATUSRSSI);
  507. if (retval < 0)
  508. return retval;
  509. timed_out = time_after(jiffies, timeout);
  510. } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
  511. (!timed_out));
  512. if (timed_out)
  513. printk(KERN_WARNING DRIVER_NAME
  514. ": seek does not finish after %u ms\n", tune_timeout);
  515. /* stop tuning */
  516. radio->registers[CHANNEL] &= ~CHANNEL_TUNE;
  517. return si470x_set_register(radio, CHANNEL);
  518. }
  519. /*
  520. * si470x_get_freq - get the frequency
  521. */
  522. static unsigned int si470x_get_freq(struct si470x_device *radio)
  523. {
  524. unsigned int spacing, band_bottom, freq;
  525. unsigned short chan;
  526. int retval;
  527. /* Spacing (kHz) */
  528. switch (space) {
  529. /* 0: 200 kHz (USA, Australia) */
  530. case 0 : spacing = 0.200 * FREQ_MUL; break;
  531. /* 1: 100 kHz (Europe, Japan) */
  532. case 1 : spacing = 0.100 * FREQ_MUL; break;
  533. /* 2: 50 kHz */
  534. default: spacing = 0.050 * FREQ_MUL; break;
  535. };
  536. /* Bottom of Band (MHz) */
  537. switch (band) {
  538. /* 0: 87.5 - 108 MHz (USA, Europe) */
  539. case 0 : band_bottom = 87.5 * FREQ_MUL; break;
  540. /* 1: 76 - 108 MHz (Japan wide band) */
  541. default: band_bottom = 76 * FREQ_MUL; break;
  542. /* 2: 76 - 90 MHz (Japan) */
  543. case 2 : band_bottom = 76 * FREQ_MUL; break;
  544. };
  545. /* read channel */
  546. retval = si470x_get_register(radio, READCHAN);
  547. if (retval < 0)
  548. return retval;
  549. chan = radio->registers[READCHAN] & READCHAN_READCHAN;
  550. /* Frequency (MHz) = Spacing (kHz) x Channel + Bottom of Band (MHz) */
  551. freq = chan * spacing + band_bottom;
  552. return freq;
  553. }
  554. /*
  555. * si470x_set_freq - set the frequency
  556. */
  557. static int si470x_set_freq(struct si470x_device *radio, unsigned int freq)
  558. {
  559. unsigned int spacing, band_bottom;
  560. unsigned short chan;
  561. /* Spacing (kHz) */
  562. switch (space) {
  563. /* 0: 200 kHz (USA, Australia) */
  564. case 0 : spacing = 0.200 * FREQ_MUL; break;
  565. /* 1: 100 kHz (Europe, Japan) */
  566. case 1 : spacing = 0.100 * FREQ_MUL; break;
  567. /* 2: 50 kHz */
  568. default: spacing = 0.050 * FREQ_MUL; break;
  569. };
  570. /* Bottom of Band (MHz) */
  571. switch (band) {
  572. /* 0: 87.5 - 108 MHz (USA, Europe) */
  573. case 0 : band_bottom = 87.5 * FREQ_MUL; break;
  574. /* 1: 76 - 108 MHz (Japan wide band) */
  575. default: band_bottom = 76 * FREQ_MUL; break;
  576. /* 2: 76 - 90 MHz (Japan) */
  577. case 2 : band_bottom = 76 * FREQ_MUL; break;
  578. };
  579. /* Chan = [ Freq (Mhz) - Bottom of Band (MHz) ] / Spacing (kHz) */
  580. chan = (freq - band_bottom) / spacing;
  581. return si470x_set_chan(radio, chan);
  582. }
  583. /*
  584. * si470x_start - switch on radio
  585. */
  586. static int si470x_start(struct si470x_device *radio)
  587. {
  588. int retval;
  589. /* powercfg */
  590. radio->registers[POWERCFG] =
  591. POWERCFG_DMUTE | POWERCFG_ENABLE | POWERCFG_RDSM;
  592. retval = si470x_set_register(radio, POWERCFG);
  593. if (retval < 0)
  594. return retval;
  595. /* sysconfig 1 */
  596. radio->registers[SYSCONFIG1] = SYSCONFIG1_DE;
  597. retval = si470x_set_register(radio, SYSCONFIG1);
  598. if (retval < 0)
  599. return retval;
  600. /* sysconfig 2 */
  601. radio->registers[SYSCONFIG2] =
  602. (0x3f << 8) | /* SEEKTH */
  603. (band << 6) | /* BAND */
  604. (space << 4) | /* SPACE */
  605. 15; /* VOLUME (max) */
  606. retval = si470x_set_register(radio, SYSCONFIG2);
  607. if (retval < 0)
  608. return retval;
  609. /* reset last channel */
  610. return si470x_set_chan(radio,
  611. radio->registers[CHANNEL] & CHANNEL_CHAN);
  612. }
  613. /*
  614. * si470x_stop - switch off radio
  615. */
  616. static int si470x_stop(struct si470x_device *radio)
  617. {
  618. int retval;
  619. /* sysconfig 1 */
  620. radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
  621. retval = si470x_set_register(radio, SYSCONFIG1);
  622. if (retval < 0)
  623. return retval;
  624. /* powercfg */
  625. radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
  626. /* POWERCFG_ENABLE has to automatically go low */
  627. radio->registers[POWERCFG] |= POWERCFG_ENABLE | POWERCFG_DISABLE;
  628. return si470x_set_register(radio, POWERCFG);
  629. }
  630. /*
  631. * si470x_rds_on - switch on rds reception
  632. */
  633. static int si470x_rds_on(struct si470x_device *radio)
  634. {
  635. int retval;
  636. /* sysconfig 1 */
  637. mutex_lock(&radio->lock);
  638. radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDS;
  639. retval = si470x_set_register(radio, SYSCONFIG1);
  640. if (retval < 0)
  641. radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
  642. mutex_unlock(&radio->lock);
  643. return retval;
  644. }
  645. /**************************************************************************
  646. * RDS Driver Functions
  647. **************************************************************************/
  648. /*
  649. * si470x_rds - rds processing function
  650. */
  651. static void si470x_rds(struct si470x_device *radio)
  652. {
  653. unsigned char blocknum;
  654. unsigned short bler; /* rds block errors */
  655. unsigned short rds;
  656. unsigned char tmpbuf[3];
  657. /* get rds blocks */
  658. if (si470x_get_rds_registers(radio) < 0)
  659. return;
  660. if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0) {
  661. /* No RDS group ready */
  662. return;
  663. }
  664. if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSS) == 0) {
  665. /* RDS decoder not synchronized */
  666. return;
  667. }
  668. /* copy all four RDS blocks to internal buffer */
  669. mutex_lock(&radio->lock);
  670. for (blocknum = 0; blocknum < 4; blocknum++) {
  671. switch (blocknum) {
  672. default:
  673. bler = (radio->registers[STATUSRSSI] &
  674. STATUSRSSI_BLERA) >> 9;
  675. rds = radio->registers[RDSA];
  676. break;
  677. case 1:
  678. bler = (radio->registers[READCHAN] &
  679. READCHAN_BLERB) >> 14;
  680. rds = radio->registers[RDSB];
  681. break;
  682. case 2:
  683. bler = (radio->registers[READCHAN] &
  684. READCHAN_BLERC) >> 12;
  685. rds = radio->registers[RDSC];
  686. break;
  687. case 3:
  688. bler = (radio->registers[READCHAN] &
  689. READCHAN_BLERD) >> 10;
  690. rds = radio->registers[RDSD];
  691. break;
  692. };
  693. /* Fill the V4L2 RDS buffer */
  694. put_unaligned(cpu_to_le16(rds), (unsigned short *) &tmpbuf);
  695. tmpbuf[2] = blocknum; /* offset name */
  696. tmpbuf[2] |= blocknum << 3; /* received offset */
  697. if (bler > max_rds_errors)
  698. tmpbuf[2] |= 0x80; /* uncorrectable errors */
  699. else if (bler > 0)
  700. tmpbuf[2] |= 0x40; /* corrected error(s) */
  701. /* copy RDS block to internal buffer */
  702. memcpy(&radio->buffer[radio->wr_index], &tmpbuf, 3);
  703. radio->wr_index += 3;
  704. /* wrap write pointer */
  705. if (radio->wr_index >= radio->buf_size)
  706. radio->wr_index = 0;
  707. /* check for overflow */
  708. if (radio->wr_index == radio->rd_index) {
  709. /* increment and wrap read pointer */
  710. radio->rd_index += 3;
  711. if (radio->rd_index >= radio->buf_size)
  712. radio->rd_index = 0;
  713. }
  714. }
  715. mutex_unlock(&radio->lock);
  716. /* wake up read queue */
  717. if (radio->wr_index != radio->rd_index)
  718. wake_up_interruptible(&radio->read_queue);
  719. }
  720. /*
  721. * si470x_work - rds work function
  722. */
  723. static void si470x_work(struct work_struct *work)
  724. {
  725. struct si470x_device *radio = container_of(work, struct si470x_device,
  726. work.work);
  727. if (radio->disconnected)
  728. return;
  729. if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
  730. return;
  731. si470x_rds(radio);
  732. schedule_delayed_work(&radio->work, msecs_to_jiffies(rds_poll_time));
  733. }
  734. /**************************************************************************
  735. * File Operations Interface
  736. **************************************************************************/
  737. /*
  738. * si470x_fops_read - read RDS data
  739. */
  740. static ssize_t si470x_fops_read(struct file *file, char __user *buf,
  741. size_t count, loff_t *ppos)
  742. {
  743. struct si470x_device *radio = video_get_drvdata(video_devdata(file));
  744. int retval = 0;
  745. unsigned int block_count = 0;
  746. /* switch on rds reception */
  747. if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
  748. si470x_rds_on(radio);
  749. schedule_delayed_work(&radio->work,
  750. msecs_to_jiffies(rds_poll_time));
  751. }
  752. /* block if no new data available */
  753. while (radio->wr_index == radio->rd_index) {
  754. if (file->f_flags & O_NONBLOCK)
  755. return -EWOULDBLOCK;
  756. if (wait_event_interruptible(radio->read_queue,
  757. radio->wr_index != radio->rd_index) < 0)
  758. return -EINTR;
  759. }
  760. /* calculate block count from byte count */
  761. count /= 3;
  762. /* copy RDS block out of internal buffer and to user buffer */
  763. mutex_lock(&radio->lock);
  764. while (block_count < count) {
  765. if (radio->rd_index == radio->wr_index)
  766. break;
  767. /* always transfer rds complete blocks */
  768. if (copy_to_user(buf, &radio->buffer[radio->rd_index], 3))
  769. /* retval = -EFAULT; */
  770. break;
  771. /* increment and wrap read pointer */
  772. radio->rd_index += 3;
  773. if (radio->rd_index >= radio->buf_size)
  774. radio->rd_index = 0;
  775. /* increment counters */
  776. block_count++;
  777. buf += 3;
  778. retval += 3;
  779. }
  780. mutex_unlock(&radio->lock);
  781. return retval;
  782. }
  783. /*
  784. * si470x_fops_poll - poll RDS data
  785. */
  786. static unsigned int si470x_fops_poll(struct file *file,
  787. struct poll_table_struct *pts)
  788. {
  789. struct si470x_device *radio = video_get_drvdata(video_devdata(file));
  790. /* switch on rds reception */
  791. if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
  792. si470x_rds_on(radio);
  793. schedule_delayed_work(&radio->work,
  794. msecs_to_jiffies(rds_poll_time));
  795. }
  796. poll_wait(file, &radio->read_queue, pts);
  797. if (radio->rd_index != radio->wr_index)
  798. return POLLIN | POLLRDNORM;
  799. return 0;
  800. }
  801. /*
  802. * si470x_fops_open - file open
  803. */
  804. static int si470x_fops_open(struct inode *inode, struct file *file)
  805. {
  806. struct si470x_device *radio = video_get_drvdata(video_devdata(file));
  807. int retval;
  808. radio->users++;
  809. retval = usb_autopm_get_interface(radio->intf);
  810. if (retval < 0) {
  811. radio->users--;
  812. return -EIO;
  813. }
  814. if (radio->users == 1) {
  815. retval = si470x_start(radio);
  816. if (retval < 0)
  817. usb_autopm_put_interface(radio->intf);
  818. return retval;
  819. }
  820. return 0;
  821. }
  822. /*
  823. * si470x_fops_release - file release
  824. */
  825. static int si470x_fops_release(struct inode *inode, struct file *file)
  826. {
  827. struct si470x_device *radio = video_get_drvdata(video_devdata(file));
  828. int retval = 0;
  829. if (!radio)
  830. return -ENODEV;
  831. mutex_lock(&open_close_lock);
  832. radio->users--;
  833. if (radio->users == 0) {
  834. if (radio->disconnected) {
  835. video_unregister_device(radio->videodev);
  836. kfree(radio->buffer);
  837. kfree(radio);
  838. goto done;
  839. }
  840. /* stop rds reception */
  841. cancel_delayed_work_sync(&radio->work);
  842. /* cancel read processes */
  843. wake_up_interruptible(&radio->read_queue);
  844. retval = si470x_stop(radio);
  845. usb_autopm_put_interface(radio->intf);
  846. }
  847. done:
  848. mutex_unlock(&open_close_lock);
  849. return retval;
  850. }
  851. /*
  852. * si470x_fops - file operations interface
  853. */
  854. static const struct file_operations si470x_fops = {
  855. .owner = THIS_MODULE,
  856. .llseek = no_llseek,
  857. .read = si470x_fops_read,
  858. .poll = si470x_fops_poll,
  859. .ioctl = video_ioctl2,
  860. #ifdef CONFIG_COMPAT
  861. .compat_ioctl = v4l_compat_ioctl32,
  862. #endif
  863. .open = si470x_fops_open,
  864. .release = si470x_fops_release,
  865. };
  866. /**************************************************************************
  867. * Video4Linux Interface
  868. **************************************************************************/
  869. /*
  870. * si470x_v4l2_queryctrl - query control
  871. */
  872. static struct v4l2_queryctrl si470x_v4l2_queryctrl[] = {
  873. /* HINT: the disabled controls are only here to satify kradio and such apps */
  874. {
  875. .id = V4L2_CID_AUDIO_VOLUME,
  876. .type = V4L2_CTRL_TYPE_INTEGER,
  877. .name = "Volume",
  878. .minimum = 0,
  879. .maximum = 15,
  880. .step = 1,
  881. .default_value = 15,
  882. },
  883. {
  884. .id = V4L2_CID_AUDIO_BALANCE,
  885. .flags = V4L2_CTRL_FLAG_DISABLED,
  886. },
  887. {
  888. .id = V4L2_CID_AUDIO_BASS,
  889. .flags = V4L2_CTRL_FLAG_DISABLED,
  890. },
  891. {
  892. .id = V4L2_CID_AUDIO_TREBLE,
  893. .flags = V4L2_CTRL_FLAG_DISABLED,
  894. },
  895. {
  896. .id = V4L2_CID_AUDIO_MUTE,
  897. .type = V4L2_CTRL_TYPE_BOOLEAN,
  898. .name = "Mute",
  899. .minimum = 0,
  900. .maximum = 1,
  901. .step = 1,
  902. .default_value = 1,
  903. },
  904. {
  905. .id = V4L2_CID_AUDIO_LOUDNESS,
  906. .flags = V4L2_CTRL_FLAG_DISABLED,
  907. },
  908. };
  909. /*
  910. * si470x_vidioc_querycap - query device capabilities
  911. */
  912. static int si470x_vidioc_querycap(struct file *file, void *priv,
  913. struct v4l2_capability *capability)
  914. {
  915. strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver));
  916. strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
  917. sprintf(capability->bus_info, "USB");
  918. capability->version = DRIVER_KERNEL_VERSION;
  919. capability->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
  920. return 0;
  921. }
  922. /*
  923. * si470x_vidioc_g_input - get input
  924. */
  925. static int si470x_vidioc_g_input(struct file *filp, void *priv,
  926. unsigned int *i)
  927. {
  928. *i = 0;
  929. return 0;
  930. }
  931. /*
  932. * si470x_vidioc_s_input - set input
  933. */
  934. static int si470x_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  935. {
  936. if (i != 0)
  937. return -EINVAL;
  938. return 0;
  939. }
  940. /*
  941. * si470x_vidioc_queryctrl - enumerate control items
  942. */
  943. static int si470x_vidioc_queryctrl(struct file *file, void *priv,
  944. struct v4l2_queryctrl *qc)
  945. {
  946. unsigned char i;
  947. int retval = -EINVAL;
  948. for (i = 0; i < ARRAY_SIZE(si470x_v4l2_queryctrl); i++) {
  949. if (qc->id && qc->id == si470x_v4l2_queryctrl[i].id) {
  950. memcpy(qc, &(si470x_v4l2_queryctrl[i]), sizeof(*qc));
  951. retval = 0;
  952. break;
  953. }
  954. }
  955. if (retval < 0)
  956. printk(KERN_WARNING DRIVER_NAME
  957. ": query control failed with %d\n", retval);
  958. return retval;
  959. }
  960. /*
  961. * si470x_vidioc_g_ctrl - get the value of a control
  962. */
  963. static int si470x_vidioc_g_ctrl(struct file *file, void *priv,
  964. struct v4l2_control *ctrl)
  965. {
  966. struct si470x_device *radio = video_get_drvdata(video_devdata(file));
  967. if (radio->disconnected)
  968. return -EIO;
  969. switch (ctrl->id) {
  970. case V4L2_CID_AUDIO_VOLUME:
  971. ctrl->value = radio->registers[SYSCONFIG2] &
  972. SYSCONFIG2_VOLUME;
  973. break;
  974. case V4L2_CID_AUDIO_MUTE:
  975. ctrl->value = ((radio->registers[POWERCFG] &
  976. POWERCFG_DMUTE) == 0) ? 1 : 0;
  977. break;
  978. }
  979. return 0;
  980. }
  981. /*
  982. * si470x_vidioc_s_ctrl - set the value of a control
  983. */
  984. static int si470x_vidioc_s_ctrl(struct file *file, void *priv,
  985. struct v4l2_control *ctrl)
  986. {
  987. struct si470x_device *radio = video_get_drvdata(video_devdata(file));
  988. int retval;
  989. if (radio->disconnected)
  990. return -EIO;
  991. switch (ctrl->id) {
  992. case V4L2_CID_AUDIO_VOLUME:
  993. radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_VOLUME;
  994. radio->registers[SYSCONFIG2] |= ctrl->value;
  995. retval = si470x_set_register(radio, SYSCONFIG2);
  996. break;
  997. case V4L2_CID_AUDIO_MUTE:
  998. if (ctrl->value == 1)
  999. radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
  1000. else
  1001. radio->registers[POWERCFG] |= POWERCFG_DMUTE;
  1002. retval = si470x_set_register(radio, POWERCFG);
  1003. break;
  1004. default:
  1005. retval = -EINVAL;
  1006. }
  1007. if (retval < 0)
  1008. printk(KERN_WARNING DRIVER_NAME
  1009. ": set control failed with %d\n", retval);
  1010. return retval;
  1011. }
  1012. /*
  1013. * si470x_vidioc_g_audio - get audio attributes
  1014. */
  1015. static int si470x_vidioc_g_audio(struct file *file, void *priv,
  1016. struct v4l2_audio *audio)
  1017. {
  1018. if (audio->index > 1)
  1019. return -EINVAL;
  1020. strcpy(audio->name, "Radio");
  1021. audio->capability = V4L2_AUDCAP_STEREO;
  1022. return 0;
  1023. }
  1024. /*
  1025. * si470x_vidioc_s_audio - set audio attributes
  1026. */
  1027. static int si470x_vidioc_s_audio(struct file *file, void *priv,
  1028. struct v4l2_audio *audio)
  1029. {
  1030. if (audio->index != 0)
  1031. return -EINVAL;
  1032. return 0;
  1033. }
  1034. /*
  1035. * si470x_vidioc_g_tuner - get tuner attributes
  1036. */
  1037. static int si470x_vidioc_g_tuner(struct file *file, void *priv,
  1038. struct v4l2_tuner *tuner)
  1039. {
  1040. struct si470x_device *radio = video_get_drvdata(video_devdata(file));
  1041. int retval;
  1042. if (radio->disconnected)
  1043. return -EIO;
  1044. if (tuner->index > 0)
  1045. return -EINVAL;
  1046. /* read status rssi */
  1047. retval = si470x_get_register(radio, STATUSRSSI);
  1048. if (retval < 0)
  1049. return retval;
  1050. strcpy(tuner->name, "FM");
  1051. tuner->type = V4L2_TUNER_RADIO;
  1052. switch (band) {
  1053. /* 0: 87.5 - 108 MHz (USA, Europe, default) */
  1054. default:
  1055. tuner->rangelow = 87.5 * FREQ_MUL;
  1056. tuner->rangehigh = 108 * FREQ_MUL;
  1057. break;
  1058. /* 1: 76 - 108 MHz (Japan wide band) */
  1059. case 1 :
  1060. tuner->rangelow = 76 * FREQ_MUL;
  1061. tuner->rangehigh = 108 * FREQ_MUL;
  1062. break;
  1063. /* 2: 76 - 90 MHz (Japan) */
  1064. case 2 :
  1065. tuner->rangelow = 76 * FREQ_MUL;
  1066. tuner->rangehigh = 90 * FREQ_MUL;
  1067. break;
  1068. };
  1069. tuner->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
  1070. tuner->capability = V4L2_TUNER_CAP_LOW;
  1071. /* Stereo indicator == Stereo (instead of Mono) */
  1072. if ((radio->registers[STATUSRSSI] & STATUSRSSI_ST) == 1)
  1073. tuner->audmode = V4L2_TUNER_MODE_STEREO;
  1074. else
  1075. tuner->audmode = V4L2_TUNER_MODE_MONO;
  1076. /* min is worst, max is best; signal:0..0xffff; rssi: 0..0xff */
  1077. tuner->signal = (radio->registers[STATUSRSSI] & STATUSRSSI_RSSI)
  1078. * 0x0101;
  1079. /* automatic frequency control: -1: freq to low, 1 freq to high */
  1080. tuner->afc = 0;
  1081. return 0;
  1082. }
  1083. /*
  1084. * si470x_vidioc_s_tuner - set tuner attributes
  1085. */
  1086. static int si470x_vidioc_s_tuner(struct file *file, void *priv,
  1087. struct v4l2_tuner *tuner)
  1088. {
  1089. struct si470x_device *radio = video_get_drvdata(video_devdata(file));
  1090. int retval;
  1091. if (radio->disconnected)
  1092. return -EIO;
  1093. if (tuner->index > 0)
  1094. return -EINVAL;
  1095. if (tuner->audmode == V4L2_TUNER_MODE_MONO)
  1096. radio->registers[POWERCFG] |= POWERCFG_MONO; /* force mono */
  1097. else
  1098. radio->registers[POWERCFG] &= ~POWERCFG_MONO; /* try stereo */
  1099. retval = si470x_set_register(radio, POWERCFG);
  1100. if (retval < 0)
  1101. printk(KERN_WARNING DRIVER_NAME
  1102. ": set tuner failed with %d\n", retval);
  1103. return retval;
  1104. }
  1105. /*
  1106. * si470x_vidioc_g_frequency - get tuner or modulator radio frequency
  1107. */
  1108. static int si470x_vidioc_g_frequency(struct file *file, void *priv,
  1109. struct v4l2_frequency *freq)
  1110. {
  1111. struct si470x_device *radio = video_get_drvdata(video_devdata(file));
  1112. if (radio->disconnected)
  1113. return -EIO;
  1114. freq->type = V4L2_TUNER_RADIO;
  1115. freq->frequency = si470x_get_freq(radio);
  1116. return 0;
  1117. }
  1118. /*
  1119. * si470x_vidioc_s_frequency - set tuner or modulator radio frequency
  1120. */
  1121. static int si470x_vidioc_s_frequency(struct file *file, void *priv,
  1122. struct v4l2_frequency *freq)
  1123. {
  1124. struct si470x_device *radio = video_get_drvdata(video_devdata(file));
  1125. int retval;
  1126. if (radio->disconnected)
  1127. return -EIO;
  1128. if (freq->type != V4L2_TUNER_RADIO)
  1129. return -EINVAL;
  1130. retval = si470x_set_freq(radio, freq->frequency);
  1131. if (retval < 0)
  1132. printk(KERN_WARNING DRIVER_NAME
  1133. ": set frequency failed with %d\n", retval);
  1134. return 0;
  1135. }
  1136. /*
  1137. * si470x_viddev_tamples - video device interface
  1138. */
  1139. static struct video_device si470x_viddev_template = {
  1140. .fops = &si470x_fops,
  1141. .name = DRIVER_NAME,
  1142. .type = VID_TYPE_TUNER,
  1143. .release = video_device_release,
  1144. .vidioc_querycap = si470x_vidioc_querycap,
  1145. .vidioc_g_input = si470x_vidioc_g_input,
  1146. .vidioc_s_input = si470x_vidioc_s_input,
  1147. .vidioc_queryctrl = si470x_vidioc_queryctrl,
  1148. .vidioc_g_ctrl = si470x_vidioc_g_ctrl,
  1149. .vidioc_s_ctrl = si470x_vidioc_s_ctrl,
  1150. .vidioc_g_audio = si470x_vidioc_g_audio,
  1151. .vidioc_s_audio = si470x_vidioc_s_audio,
  1152. .vidioc_g_tuner = si470x_vidioc_g_tuner,
  1153. .vidioc_s_tuner = si470x_vidioc_s_tuner,
  1154. .vidioc_g_frequency = si470x_vidioc_g_frequency,
  1155. .vidioc_s_frequency = si470x_vidioc_s_frequency,
  1156. .owner = THIS_MODULE,
  1157. };
  1158. /**************************************************************************
  1159. * USB Interface
  1160. **************************************************************************/
  1161. /*
  1162. * si470x_usb_driver_probe - probe for the device
  1163. */
  1164. static int si470x_usb_driver_probe(struct usb_interface *intf,
  1165. const struct usb_device_id *id)
  1166. {
  1167. struct si470x_device *radio;
  1168. int retval = -ENOMEM;
  1169. /* private data allocation */
  1170. radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL);
  1171. if (!radio)
  1172. goto err_initial;
  1173. /* video device allocation */
  1174. radio->videodev = video_device_alloc();
  1175. if (!radio->videodev)
  1176. goto err_radio;
  1177. /* initial configuration */
  1178. memcpy(radio->videodev, &si470x_viddev_template,
  1179. sizeof(si470x_viddev_template));
  1180. radio->users = 0;
  1181. radio->usbdev = interface_to_usbdev(intf);
  1182. radio->intf = intf;
  1183. mutex_init(&radio->lock);
  1184. video_set_drvdata(radio->videodev, radio);
  1185. /* show some infos about the specific device */
  1186. retval = -EIO;
  1187. if (si470x_get_all_registers(radio) < 0)
  1188. goto err_all;
  1189. printk(KERN_INFO DRIVER_NAME ": DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",
  1190. radio->registers[DEVICEID], radio->registers[CHIPID]);
  1191. /* check if firmware is current */
  1192. if ((radio->registers[CHIPID] & CHIPID_FIRMWARE)
  1193. < RADIO_SW_VERSION_CURRENT) {
  1194. printk(KERN_WARNING DRIVER_NAME
  1195. ": This driver is known to work with "
  1196. "firmware version %hu,\n", RADIO_SW_VERSION_CURRENT);
  1197. printk(KERN_WARNING DRIVER_NAME
  1198. ": but the device has firmware version %hu.\n",
  1199. radio->registers[CHIPID] & CHIPID_FIRMWARE);
  1200. printk(KERN_WARNING DRIVER_NAME
  1201. ": If you have some trouble using this driver,\n");
  1202. printk(KERN_WARNING DRIVER_NAME
  1203. ": please report to V4L ML at "
  1204. "video4linux-list@redhat.com\n");
  1205. }
  1206. /* set initial frequency */
  1207. si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
  1208. /* rds buffer allocation */
  1209. radio->buf_size = rds_buf * 3;
  1210. radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
  1211. if (!radio->buffer)
  1212. goto err_all;
  1213. /* rds buffer configuration */
  1214. radio->wr_index = 0;
  1215. radio->rd_index = 0;
  1216. init_waitqueue_head(&radio->read_queue);
  1217. /* prepare rds work function */
  1218. INIT_DELAYED_WORK(&radio->work, si470x_work);
  1219. /* register video device */
  1220. if (video_register_device(radio->videodev, VFL_TYPE_RADIO, radio_nr)) {
  1221. printk(KERN_WARNING DRIVER_NAME
  1222. ": Could not register video device\n");
  1223. goto err_all;
  1224. }
  1225. usb_set_intfdata(intf, radio);
  1226. return 0;
  1227. err_all:
  1228. video_device_release(radio->videodev);
  1229. kfree(radio->buffer);
  1230. err_radio:
  1231. kfree(radio);
  1232. err_initial:
  1233. return retval;
  1234. }
  1235. /*
  1236. * si470x_usb_driver_suspend - suspend the device
  1237. */
  1238. static int si470x_usb_driver_suspend(struct usb_interface *intf,
  1239. pm_message_t message)
  1240. {
  1241. struct si470x_device *radio = usb_get_intfdata(intf);
  1242. printk(KERN_INFO DRIVER_NAME ": suspending now...\n");
  1243. cancel_delayed_work_sync(&radio->work);
  1244. return 0;
  1245. }
  1246. /*
  1247. * si470x_usb_driver_resume - resume the device
  1248. */
  1249. static int si470x_usb_driver_resume(struct usb_interface *intf)
  1250. {
  1251. struct si470x_device *radio = usb_get_intfdata(intf);
  1252. printk(KERN_INFO DRIVER_NAME ": resuming now...\n");
  1253. mutex_lock(&radio->lock);
  1254. if (radio->users && radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS)
  1255. schedule_delayed_work(&radio->work,
  1256. msecs_to_jiffies(rds_poll_time));
  1257. mutex_unlock(&radio->lock);
  1258. return 0;
  1259. }
  1260. /*
  1261. * si470x_usb_driver_disconnect - disconnect the device
  1262. */
  1263. static void si470x_usb_driver_disconnect(struct usb_interface *intf)
  1264. {
  1265. struct si470x_device *radio = usb_get_intfdata(intf);
  1266. mutex_lock(&open_close_lock);
  1267. radio->disconnected = 1;
  1268. cancel_delayed_work_sync(&radio->work);
  1269. usb_set_intfdata(intf, NULL);
  1270. if (radio->users == 0) {
  1271. video_unregister_device(radio->videodev);
  1272. kfree(radio->buffer);
  1273. kfree(radio);
  1274. }
  1275. mutex_unlock(&open_close_lock);
  1276. }
  1277. /*
  1278. * si470x_usb_driver - usb driver interface
  1279. */
  1280. static struct usb_driver si470x_usb_driver = {
  1281. .name = DRIVER_NAME,
  1282. .probe = si470x_usb_driver_probe,
  1283. .disconnect = si470x_usb_driver_disconnect,
  1284. .suspend = si470x_usb_driver_suspend,
  1285. .resume = si470x_usb_driver_resume,
  1286. .id_table = si470x_usb_driver_id_table,
  1287. .supports_autosuspend = 1,
  1288. };
  1289. /**************************************************************************
  1290. * Module Interface
  1291. **************************************************************************/
  1292. /*
  1293. * si470x_module_init - module init
  1294. */
  1295. static int __init si470x_module_init(void)
  1296. {
  1297. printk(KERN_INFO DRIVER_DESC ", Version " DRIVER_VERSION "\n");
  1298. return usb_register(&si470x_usb_driver);
  1299. }
  1300. /*
  1301. * si470x_module_exit - module exit
  1302. */
  1303. static void __exit si470x_module_exit(void)
  1304. {
  1305. usb_deregister(&si470x_usb_driver);
  1306. }
  1307. module_init(si470x_module_init);
  1308. module_exit(si470x_module_exit);
  1309. MODULE_LICENSE("GPL");
  1310. MODULE_AUTHOR(DRIVER_AUTHOR);
  1311. MODULE_DESCRIPTION(DRIVER_DESC);
  1312. MODULE_VERSION(DRIVER_VERSION);