radio-si470x.c 45 KB

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