radio-si470x.c 42 KB

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