diff options
author | Joonyoung Shim <jy0922.shim@samsung.com> | 2009-08-09 13:22:20 -0400 |
---|---|---|
committer | Mauro Carvalho Chehab <mchehab@redhat.com> | 2009-09-12 11:18:40 -0400 |
commit | b09cd16354454daa1f047b4982691a3826787a0b (patch) | |
tree | 8969ec668f879f1a4b77b4c22ab106f1ea89db84 /drivers/media/radio | |
parent | 4efa2d75d4a3b86c2d47c422237c848d1f04ba58 (diff) |
V4L/DVB (12413): radio-si470x: separate common and usb code
This patch is a preceding work to add the i2c interface of si470x.
The si470x directory includes a common file and usb specific file and
header file.
The part unrelated with usb interface and i2c interface exists in
radio-si470x-common.c file, and The usb specific part exists in
radio-si470x-usb.c file.
Signed-off-by: Joonyoung Shim <jy0922.shim@samsung.com>
[tobias.lorenz@gmx.net: Small changes, due to new include "linux/smp_lock.h"]
Signed-off-by: Tobias Lorenz <tobias.lorenz@gmx.net>
Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
Diffstat (limited to 'drivers/media/radio')
-rw-r--r-- | drivers/media/radio/Kconfig | 26 | ||||
-rw-r--r-- | drivers/media/radio/Makefile | 2 | ||||
-rw-r--r-- | drivers/media/radio/radio-si470x.c | 1869 | ||||
-rw-r--r-- | drivers/media/radio/si470x/Kconfig | 24 | ||||
-rw-r--r-- | drivers/media/radio/si470x/Makefile | 7 | ||||
-rw-r--r-- | drivers/media/radio/si470x/radio-si470x-common.c | 794 | ||||
-rw-r--r-- | drivers/media/radio/si470x/radio-si470x-usb.c | 958 | ||||
-rw-r--r-- | drivers/media/radio/si470x/radio-si470x.h | 201 |
8 files changed, 1989 insertions, 1892 deletions
diff --git a/drivers/media/radio/Kconfig b/drivers/media/radio/Kconfig index 3315cac875e5..41bbec6df327 100644 --- a/drivers/media/radio/Kconfig +++ b/drivers/media/radio/Kconfig | |||
@@ -351,29 +351,11 @@ config USB_DSBR | |||
351 | To compile this driver as a module, choose M here: the | 351 | To compile this driver as a module, choose M here: the |
352 | module will be called dsbr100. | 352 | module will be called dsbr100. |
353 | 353 | ||
354 | config USB_SI470X | 354 | config RADIO_SI470X |
355 | tristate "Silicon Labs Si470x FM Radio Receiver support" | 355 | bool "Silicon Labs Si470x FM Radio Receiver support" |
356 | depends on USB && VIDEO_V4L2 | 356 | depends on VIDEO_V4L2 |
357 | ---help--- | ||
358 | This is a driver for USB devices with the Silicon Labs SI470x | ||
359 | chip. Currently these devices are known to work: | ||
360 | - 10c4:818a: Silicon Labs USB FM Radio Reference Design | ||
361 | - 06e1:a155: ADS/Tech FM Radio Receiver (formerly Instant FM Music) | ||
362 | - 1b80:d700: KWorld USB FM Radio SnapMusic Mobile 700 (FM700) | ||
363 | |||
364 | Sound is provided by the ALSA USB Audio/MIDI driver. Therefore | ||
365 | if you don't want to use the device solely for RDS receiving, | ||
366 | it is recommended to also select SND_USB_AUDIO. | ||
367 | |||
368 | Please have a look at the documentation, especially on how | ||
369 | to redirect the audio stream from the radio to your sound device: | ||
370 | Documentation/video4linux/si470x.txt | ||
371 | |||
372 | Say Y here if you want to connect this type of radio to your | ||
373 | computer's USB port. | ||
374 | 357 | ||
375 | To compile this driver as a module, choose M here: the | 358 | source "drivers/media/radio/si470x/Kconfig" |
376 | module will be called radio-si470x. | ||
377 | 359 | ||
378 | config USB_MR800 | 360 | config USB_MR800 |
379 | tristate "AverMedia MR 800 USB FM radio support" | 361 | tristate "AverMedia MR 800 USB FM radio support" |
diff --git a/drivers/media/radio/Makefile b/drivers/media/radio/Makefile index 0f2b35b3e560..18111e1fea8f 100644 --- a/drivers/media/radio/Makefile +++ b/drivers/media/radio/Makefile | |||
@@ -17,7 +17,7 @@ obj-$(CONFIG_RADIO_GEMTEK_PCI) += radio-gemtek-pci.o | |||
17 | obj-$(CONFIG_RADIO_TRUST) += radio-trust.o | 17 | obj-$(CONFIG_RADIO_TRUST) += radio-trust.o |
18 | obj-$(CONFIG_RADIO_MAESTRO) += radio-maestro.o | 18 | obj-$(CONFIG_RADIO_MAESTRO) += radio-maestro.o |
19 | obj-$(CONFIG_USB_DSBR) += dsbr100.o | 19 | obj-$(CONFIG_USB_DSBR) += dsbr100.o |
20 | obj-$(CONFIG_USB_SI470X) += radio-si470x.o | 20 | obj-$(CONFIG_RADIO_SI470X) += si470x/ |
21 | obj-$(CONFIG_USB_MR800) += radio-mr800.o | 21 | obj-$(CONFIG_USB_MR800) += radio-mr800.o |
22 | obj-$(CONFIG_RADIO_TEA5764) += radio-tea5764.o | 22 | obj-$(CONFIG_RADIO_TEA5764) += radio-tea5764.o |
23 | 23 | ||
diff --git a/drivers/media/radio/radio-si470x.c b/drivers/media/radio/radio-si470x.c deleted file mode 100644 index 8e6dd84ed208..000000000000 --- a/drivers/media/radio/radio-si470x.c +++ /dev/null | |||
@@ -1,1869 +0,0 @@ | |||
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 | * - KWorld USB FM Radio SnapMusic Mobile 700 (FM700) | ||
8 | * - Sanei Electric, Inc. FM USB Radio (sold as DealExtreme.com PCear) | ||
9 | * | ||
10 | * Copyright (c) 2009 Tobias Lorenz <tobias.lorenz@gmx.net> | ||
11 | * | ||
12 | * This program is free software; you can redistribute it and/or modify | ||
13 | * it under the terms of the GNU General Public License as published by | ||
14 | * the Free Software Foundation; either version 2 of the License, or | ||
15 | * (at your option) any later version. | ||
16 | * | ||
17 | * This program is distributed in the hope that it will be useful, | ||
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
20 | * GNU General Public License for more details. | ||
21 | * | ||
22 | * You should have received a copy of the GNU General Public License | ||
23 | * along with this program; if not, write to the Free Software | ||
24 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
25 | */ | ||
26 | |||
27 | |||
28 | /* | ||
29 | * History: | ||
30 | * 2008-01-12 Tobias Lorenz <tobias.lorenz@gmx.net> | ||
31 | * Version 1.0.0 | ||
32 | * - First working version | ||
33 | * 2008-01-13 Tobias Lorenz <tobias.lorenz@gmx.net> | ||
34 | * Version 1.0.1 | ||
35 | * - Improved error handling, every function now returns errno | ||
36 | * - Improved multi user access (start/mute/stop) | ||
37 | * - Channel doesn't get lost anymore after start/mute/stop | ||
38 | * - RDS support added (polling mode via interrupt EP 1) | ||
39 | * - marked default module parameters with *value* | ||
40 | * - switched from bit structs to bit masks | ||
41 | * - header file cleaned and integrated | ||
42 | * 2008-01-14 Tobias Lorenz <tobias.lorenz@gmx.net> | ||
43 | * Version 1.0.2 | ||
44 | * - hex values are now lower case | ||
45 | * - commented USB ID for ADS/Tech moved on todo list | ||
46 | * - blacklisted si470x in hid-quirks.c | ||
47 | * - rds buffer handling functions integrated into *_work, *_read | ||
48 | * - rds_command in si470x_poll exchanged against simple retval | ||
49 | * - check for firmware version 15 | ||
50 | * - code order and prototypes still remain the same | ||
51 | * - spacing and bottom of band codes remain the same | ||
52 | * 2008-01-16 Tobias Lorenz <tobias.lorenz@gmx.net> | ||
53 | * Version 1.0.3 | ||
54 | * - code reordered to avoid function prototypes | ||
55 | * - switch/case defaults are now more user-friendly | ||
56 | * - unified comment style | ||
57 | * - applied all checkpatch.pl v1.12 suggestions | ||
58 | * except the warning about the too long lines with bit comments | ||
59 | * - renamed FMRADIO to RADIO to cut line length (checkpatch.pl) | ||
60 | * 2008-01-22 Tobias Lorenz <tobias.lorenz@gmx.net> | ||
61 | * Version 1.0.4 | ||
62 | * - avoid poss. locking when doing copy_to_user which may sleep | ||
63 | * - RDS is automatically activated on read now | ||
64 | * - code cleaned of unnecessary rds_commands | ||
65 | * - USB Vendor/Product ID for ADS/Tech FM Radio Receiver verified | ||
66 | * (thanks to Guillaume RAMOUSSE) | ||
67 | * 2008-01-27 Tobias Lorenz <tobias.lorenz@gmx.net> | ||
68 | * Version 1.0.5 | ||
69 | * - number of seek_retries changed to tune_timeout | ||
70 | * - fixed problem with incomplete tune operations by own buffers | ||
71 | * - optimization of variables and printf types | ||
72 | * - improved error logging | ||
73 | * 2008-01-31 Tobias Lorenz <tobias.lorenz@gmx.net> | ||
74 | * Oliver Neukum <oliver@neukum.org> | ||
75 | * Version 1.0.6 | ||
76 | * - fixed coverity checker warnings in *_usb_driver_disconnect | ||
77 | * - probe()/open() race by correct ordering in probe() | ||
78 | * - DMA coherency rules by separate allocation of all buffers | ||
79 | * - use of endianness macros | ||
80 | * - abuse of spinlock, replaced by mutex | ||
81 | * - racy handling of timer in disconnect, | ||
82 | * replaced by delayed_work | ||
83 | * - racy interruptible_sleep_on(), | ||
84 | * replaced with wait_event_interruptible() | ||
85 | * - handle signals in read() | ||
86 | * 2008-02-08 Tobias Lorenz <tobias.lorenz@gmx.net> | ||
87 | * Oliver Neukum <oliver@neukum.org> | ||
88 | * Version 1.0.7 | ||
89 | * - usb autosuspend support | ||
90 | * - unplugging fixed | ||
91 | * 2008-05-07 Tobias Lorenz <tobias.lorenz@gmx.net> | ||
92 | * Version 1.0.8 | ||
93 | * - hardware frequency seek support | ||
94 | * - afc indication | ||
95 | * - more safety checks, let si470x_get_freq return errno | ||
96 | * - vidioc behavior corrected according to v4l2 spec | ||
97 | * 2008-10-20 Alexey Klimov <klimov.linux@gmail.com> | ||
98 | * - add support for KWorld USB FM Radio FM700 | ||
99 | * - blacklisted KWorld radio in hid-core.c and hid-ids.h | ||
100 | * 2008-12-03 Mark Lord <mlord@pobox.com> | ||
101 | * - add support for DealExtreme USB Radio | ||
102 | * 2009-01-31 Bob Ross <pigiron@gmx.com> | ||
103 | * - correction of stereo detection/setting | ||
104 | * - correction of signal strength indicator scaling | ||
105 | * 2009-01-31 Rick Bronson <rick@efn.org> | ||
106 | * Tobias Lorenz <tobias.lorenz@gmx.net> | ||
107 | * - add LED status output | ||
108 | * - get HW/SW version from scratchpad | ||
109 | * 2009-06-16 Edouard Lafargue <edouard@lafargue.name> | ||
110 | * Version 1.0.10 | ||
111 | * - add support for interrupt mode for RDS endpoint, | ||
112 | * instead of polling. | ||
113 | * Improves RDS reception significantly | ||
114 | * | ||
115 | * ToDo: | ||
116 | * - add firmware download/update support | ||
117 | */ | ||
118 | |||
119 | |||
120 | /* driver definitions */ | ||
121 | #define DRIVER_AUTHOR "Tobias Lorenz <tobias.lorenz@gmx.net>" | ||
122 | #define DRIVER_NAME "radio-si470x" | ||
123 | #define DRIVER_KERNEL_VERSION KERNEL_VERSION(1, 0, 10) | ||
124 | #define DRIVER_CARD "Silicon Labs Si470x FM Radio Receiver" | ||
125 | #define DRIVER_DESC "USB radio driver for Si470x FM Radio Receivers" | ||
126 | #define DRIVER_VERSION "1.0.10" | ||
127 | |||
128 | |||
129 | /* kernel includes */ | ||
130 | #include <linux/kernel.h> | ||
131 | #include <linux/module.h> | ||
132 | #include <linux/init.h> | ||
133 | #include <linux/slab.h> | ||
134 | #include <linux/smp_lock.h> | ||
135 | #include <linux/input.h> | ||
136 | #include <linux/usb.h> | ||
137 | #include <linux/hid.h> | ||
138 | #include <linux/version.h> | ||
139 | #include <linux/videodev2.h> | ||
140 | #include <linux/mutex.h> | ||
141 | #include <media/v4l2-common.h> | ||
142 | #include <media/v4l2-ioctl.h> | ||
143 | #include <media/rds.h> | ||
144 | #include <asm/unaligned.h> | ||
145 | |||
146 | |||
147 | /* USB Device ID List */ | ||
148 | static struct usb_device_id si470x_usb_driver_id_table[] = { | ||
149 | /* Silicon Labs USB FM Radio Reference Design */ | ||
150 | { USB_DEVICE_AND_INTERFACE_INFO(0x10c4, 0x818a, USB_CLASS_HID, 0, 0) }, | ||
151 | /* ADS/Tech FM Radio Receiver (formerly Instant FM Music) */ | ||
152 | { USB_DEVICE_AND_INTERFACE_INFO(0x06e1, 0xa155, USB_CLASS_HID, 0, 0) }, | ||
153 | /* KWorld USB FM Radio SnapMusic Mobile 700 (FM700) */ | ||
154 | { USB_DEVICE_AND_INTERFACE_INFO(0x1b80, 0xd700, USB_CLASS_HID, 0, 0) }, | ||
155 | /* Sanei Electric, Inc. FM USB Radio (sold as DealExtreme.com PCear) */ | ||
156 | { USB_DEVICE_AND_INTERFACE_INFO(0x10c5, 0x819a, USB_CLASS_HID, 0, 0) }, | ||
157 | /* Terminating entry */ | ||
158 | { } | ||
159 | }; | ||
160 | MODULE_DEVICE_TABLE(usb, si470x_usb_driver_id_table); | ||
161 | |||
162 | |||
163 | |||
164 | /************************************************************************** | ||
165 | * Module Parameters | ||
166 | **************************************************************************/ | ||
167 | |||
168 | /* Radio Nr */ | ||
169 | static int radio_nr = -1; | ||
170 | module_param(radio_nr, int, 0444); | ||
171 | MODULE_PARM_DESC(radio_nr, "Radio Nr"); | ||
172 | |||
173 | /* Spacing (kHz) */ | ||
174 | /* 0: 200 kHz (USA, Australia) */ | ||
175 | /* 1: 100 kHz (Europe, Japan) */ | ||
176 | /* 2: 50 kHz */ | ||
177 | static unsigned short space = 2; | ||
178 | module_param(space, ushort, 0444); | ||
179 | MODULE_PARM_DESC(space, "Spacing: 0=200kHz 1=100kHz *2=50kHz*"); | ||
180 | |||
181 | /* Bottom of Band (MHz) */ | ||
182 | /* 0: 87.5 - 108 MHz (USA, Europe)*/ | ||
183 | /* 1: 76 - 108 MHz (Japan wide band) */ | ||
184 | /* 2: 76 - 90 MHz (Japan) */ | ||
185 | static unsigned short band = 1; | ||
186 | module_param(band, ushort, 0444); | ||
187 | MODULE_PARM_DESC(band, "Band: 0=87.5..108MHz *1=76..108MHz* 2=76..90MHz"); | ||
188 | |||
189 | /* De-emphasis */ | ||
190 | /* 0: 75 us (USA) */ | ||
191 | /* 1: 50 us (Europe, Australia, Japan) */ | ||
192 | static unsigned short de = 1; | ||
193 | module_param(de, ushort, 0444); | ||
194 | MODULE_PARM_DESC(de, "De-emphasis: 0=75us *1=50us*"); | ||
195 | |||
196 | /* USB timeout */ | ||
197 | static unsigned int usb_timeout = 500; | ||
198 | module_param(usb_timeout, uint, 0644); | ||
199 | MODULE_PARM_DESC(usb_timeout, "USB timeout (ms): *500*"); | ||
200 | |||
201 | /* Tune timeout */ | ||
202 | static unsigned int tune_timeout = 3000; | ||
203 | module_param(tune_timeout, uint, 0644); | ||
204 | MODULE_PARM_DESC(tune_timeout, "Tune timeout: *3000*"); | ||
205 | |||
206 | /* Seek timeout */ | ||
207 | static unsigned int seek_timeout = 5000; | ||
208 | module_param(seek_timeout, uint, 0644); | ||
209 | MODULE_PARM_DESC(seek_timeout, "Seek timeout: *5000*"); | ||
210 | |||
211 | /* RDS buffer blocks */ | ||
212 | static unsigned int rds_buf = 100; | ||
213 | module_param(rds_buf, uint, 0444); | ||
214 | MODULE_PARM_DESC(rds_buf, "RDS buffer entries: *100*"); | ||
215 | |||
216 | /* RDS maximum block errors */ | ||
217 | static unsigned short max_rds_errors = 1; | ||
218 | /* 0 means 0 errors requiring correction */ | ||
219 | /* 1 means 1-2 errors requiring correction (used by original USBRadio.exe) */ | ||
220 | /* 2 means 3-5 errors requiring correction */ | ||
221 | /* 3 means 6+ errors or errors in checkword, correction not possible */ | ||
222 | module_param(max_rds_errors, ushort, 0644); | ||
223 | MODULE_PARM_DESC(max_rds_errors, "RDS maximum block errors: *1*"); | ||
224 | |||
225 | |||
226 | /************************************************************************** | ||
227 | * Register Definitions | ||
228 | **************************************************************************/ | ||
229 | #define RADIO_REGISTER_SIZE 2 /* 16 register bit width */ | ||
230 | #define RADIO_REGISTER_NUM 16 /* DEVICEID ... RDSD */ | ||
231 | #define RDS_REGISTER_NUM 6 /* STATUSRSSI ... RDSD */ | ||
232 | |||
233 | #define DEVICEID 0 /* Device ID */ | ||
234 | #define DEVICEID_PN 0xf000 /* bits 15..12: Part Number */ | ||
235 | #define DEVICEID_MFGID 0x0fff /* bits 11..00: Manufacturer ID */ | ||
236 | |||
237 | #define CHIPID 1 /* Chip ID */ | ||
238 | #define CHIPID_REV 0xfc00 /* bits 15..10: Chip Version */ | ||
239 | #define CHIPID_DEV 0x0200 /* bits 09..09: Device */ | ||
240 | #define CHIPID_FIRMWARE 0x01ff /* bits 08..00: Firmware Version */ | ||
241 | |||
242 | #define POWERCFG 2 /* Power Configuration */ | ||
243 | #define POWERCFG_DSMUTE 0x8000 /* bits 15..15: Softmute Disable */ | ||
244 | #define POWERCFG_DMUTE 0x4000 /* bits 14..14: Mute Disable */ | ||
245 | #define POWERCFG_MONO 0x2000 /* bits 13..13: Mono Select */ | ||
246 | #define POWERCFG_RDSM 0x0800 /* bits 11..11: RDS Mode (Si4701 only) */ | ||
247 | #define POWERCFG_SKMODE 0x0400 /* bits 10..10: Seek Mode */ | ||
248 | #define POWERCFG_SEEKUP 0x0200 /* bits 09..09: Seek Direction */ | ||
249 | #define POWERCFG_SEEK 0x0100 /* bits 08..08: Seek */ | ||
250 | #define POWERCFG_DISABLE 0x0040 /* bits 06..06: Powerup Disable */ | ||
251 | #define POWERCFG_ENABLE 0x0001 /* bits 00..00: Powerup Enable */ | ||
252 | |||
253 | #define CHANNEL 3 /* Channel */ | ||
254 | #define CHANNEL_TUNE 0x8000 /* bits 15..15: Tune */ | ||
255 | #define CHANNEL_CHAN 0x03ff /* bits 09..00: Channel Select */ | ||
256 | |||
257 | #define SYSCONFIG1 4 /* System Configuration 1 */ | ||
258 | #define SYSCONFIG1_RDSIEN 0x8000 /* bits 15..15: RDS Interrupt Enable (Si4701 only) */ | ||
259 | #define SYSCONFIG1_STCIEN 0x4000 /* bits 14..14: Seek/Tune Complete Interrupt Enable */ | ||
260 | #define SYSCONFIG1_RDS 0x1000 /* bits 12..12: RDS Enable (Si4701 only) */ | ||
261 | #define SYSCONFIG1_DE 0x0800 /* bits 11..11: De-emphasis (0=75us 1=50us) */ | ||
262 | #define SYSCONFIG1_AGCD 0x0400 /* bits 10..10: AGC Disable */ | ||
263 | #define SYSCONFIG1_BLNDADJ 0x00c0 /* bits 07..06: Stereo/Mono Blend Level Adjustment */ | ||
264 | #define SYSCONFIG1_GPIO3 0x0030 /* bits 05..04: General Purpose I/O 3 */ | ||
265 | #define SYSCONFIG1_GPIO2 0x000c /* bits 03..02: General Purpose I/O 2 */ | ||
266 | #define SYSCONFIG1_GPIO1 0x0003 /* bits 01..00: General Purpose I/O 1 */ | ||
267 | |||
268 | #define SYSCONFIG2 5 /* System Configuration 2 */ | ||
269 | #define SYSCONFIG2_SEEKTH 0xff00 /* bits 15..08: RSSI Seek Threshold */ | ||
270 | #define SYSCONFIG2_BAND 0x0080 /* bits 07..06: Band Select */ | ||
271 | #define SYSCONFIG2_SPACE 0x0030 /* bits 05..04: Channel Spacing */ | ||
272 | #define SYSCONFIG2_VOLUME 0x000f /* bits 03..00: Volume */ | ||
273 | |||
274 | #define SYSCONFIG3 6 /* System Configuration 3 */ | ||
275 | #define SYSCONFIG3_SMUTER 0xc000 /* bits 15..14: Softmute Attack/Recover Rate */ | ||
276 | #define SYSCONFIG3_SMUTEA 0x3000 /* bits 13..12: Softmute Attenuation */ | ||
277 | #define SYSCONFIG3_SKSNR 0x00f0 /* bits 07..04: Seek SNR Threshold */ | ||
278 | #define SYSCONFIG3_SKCNT 0x000f /* bits 03..00: Seek FM Impulse Detection Threshold */ | ||
279 | |||
280 | #define TEST1 7 /* Test 1 */ | ||
281 | #define TEST1_AHIZEN 0x4000 /* bits 14..14: Audio High-Z Enable */ | ||
282 | |||
283 | #define TEST2 8 /* Test 2 */ | ||
284 | /* TEST2 only contains reserved bits */ | ||
285 | |||
286 | #define BOOTCONFIG 9 /* Boot Configuration */ | ||
287 | /* BOOTCONFIG only contains reserved bits */ | ||
288 | |||
289 | #define STATUSRSSI 10 /* Status RSSI */ | ||
290 | #define STATUSRSSI_RDSR 0x8000 /* bits 15..15: RDS Ready (Si4701 only) */ | ||
291 | #define STATUSRSSI_STC 0x4000 /* bits 14..14: Seek/Tune Complete */ | ||
292 | #define STATUSRSSI_SF 0x2000 /* bits 13..13: Seek Fail/Band Limit */ | ||
293 | #define STATUSRSSI_AFCRL 0x1000 /* bits 12..12: AFC Rail */ | ||
294 | #define STATUSRSSI_RDSS 0x0800 /* bits 11..11: RDS Synchronized (Si4701 only) */ | ||
295 | #define STATUSRSSI_BLERA 0x0600 /* bits 10..09: RDS Block A Errors (Si4701 only) */ | ||
296 | #define STATUSRSSI_ST 0x0100 /* bits 08..08: Stereo Indicator */ | ||
297 | #define STATUSRSSI_RSSI 0x00ff /* bits 07..00: RSSI (Received Signal Strength Indicator) */ | ||
298 | |||
299 | #define READCHAN 11 /* Read Channel */ | ||
300 | #define READCHAN_BLERB 0xc000 /* bits 15..14: RDS Block D Errors (Si4701 only) */ | ||
301 | #define READCHAN_BLERC 0x3000 /* bits 13..12: RDS Block C Errors (Si4701 only) */ | ||
302 | #define READCHAN_BLERD 0x0c00 /* bits 11..10: RDS Block B Errors (Si4701 only) */ | ||
303 | #define READCHAN_READCHAN 0x03ff /* bits 09..00: Read Channel */ | ||
304 | |||
305 | #define RDSA 12 /* RDSA */ | ||
306 | #define RDSA_RDSA 0xffff /* bits 15..00: RDS Block A Data (Si4701 only) */ | ||
307 | |||
308 | #define RDSB 13 /* RDSB */ | ||
309 | #define RDSB_RDSB 0xffff /* bits 15..00: RDS Block B Data (Si4701 only) */ | ||
310 | |||
311 | #define RDSC 14 /* RDSC */ | ||
312 | #define RDSC_RDSC 0xffff /* bits 15..00: RDS Block C Data (Si4701 only) */ | ||
313 | |||
314 | #define RDSD 15 /* RDSD */ | ||
315 | #define RDSD_RDSD 0xffff /* bits 15..00: RDS Block D Data (Si4701 only) */ | ||
316 | |||
317 | |||
318 | |||
319 | /************************************************************************** | ||
320 | * USB HID Reports | ||
321 | **************************************************************************/ | ||
322 | |||
323 | /* Reports 1-16 give direct read/write access to the 16 Si470x registers */ | ||
324 | /* with the (REPORT_ID - 1) corresponding to the register address across USB */ | ||
325 | /* endpoint 0 using GET_REPORT and SET_REPORT */ | ||
326 | #define REGISTER_REPORT_SIZE (RADIO_REGISTER_SIZE + 1) | ||
327 | #define REGISTER_REPORT(reg) ((reg) + 1) | ||
328 | |||
329 | /* Report 17 gives direct read/write access to the entire Si470x register */ | ||
330 | /* map across endpoint 0 using GET_REPORT and SET_REPORT */ | ||
331 | #define ENTIRE_REPORT_SIZE (RADIO_REGISTER_NUM * RADIO_REGISTER_SIZE + 1) | ||
332 | #define ENTIRE_REPORT 17 | ||
333 | |||
334 | /* Report 18 is used to send the lowest 6 Si470x registers up the HID */ | ||
335 | /* interrupt endpoint 1 to Windows every 20 milliseconds for status */ | ||
336 | #define RDS_REPORT_SIZE (RDS_REGISTER_NUM * RADIO_REGISTER_SIZE + 1) | ||
337 | #define RDS_REPORT 18 | ||
338 | |||
339 | /* Report 19: LED state */ | ||
340 | #define LED_REPORT_SIZE 3 | ||
341 | #define LED_REPORT 19 | ||
342 | |||
343 | /* Report 19: stream */ | ||
344 | #define STREAM_REPORT_SIZE 3 | ||
345 | #define STREAM_REPORT 19 | ||
346 | |||
347 | /* Report 20: scratch */ | ||
348 | #define SCRATCH_PAGE_SIZE 63 | ||
349 | #define SCRATCH_REPORT_SIZE (SCRATCH_PAGE_SIZE + 1) | ||
350 | #define SCRATCH_REPORT 20 | ||
351 | |||
352 | /* Reports 19-22: flash upgrade of the C8051F321 */ | ||
353 | #define WRITE_REPORT_SIZE 4 | ||
354 | #define WRITE_REPORT 19 | ||
355 | #define FLASH_REPORT_SIZE 64 | ||
356 | #define FLASH_REPORT 20 | ||
357 | #define CRC_REPORT_SIZE 3 | ||
358 | #define CRC_REPORT 21 | ||
359 | #define RESPONSE_REPORT_SIZE 2 | ||
360 | #define RESPONSE_REPORT 22 | ||
361 | |||
362 | /* Report 23: currently unused, but can accept 60 byte reports on the HID */ | ||
363 | /* interrupt out endpoint 2 every 1 millisecond */ | ||
364 | #define UNUSED_REPORT 23 | ||
365 | |||
366 | |||
367 | |||
368 | /************************************************************************** | ||
369 | * Software/Hardware Versions | ||
370 | **************************************************************************/ | ||
371 | #define RADIO_SW_VERSION_NOT_BOOTLOADABLE 6 | ||
372 | #define RADIO_SW_VERSION 7 | ||
373 | #define RADIO_SW_VERSION_CURRENT 15 | ||
374 | #define RADIO_HW_VERSION 1 | ||
375 | |||
376 | #define SCRATCH_PAGE_SW_VERSION 1 | ||
377 | #define SCRATCH_PAGE_HW_VERSION 2 | ||
378 | |||
379 | |||
380 | |||
381 | /************************************************************************** | ||
382 | * LED State Definitions | ||
383 | **************************************************************************/ | ||
384 | #define LED_COMMAND 0x35 | ||
385 | |||
386 | #define NO_CHANGE_LED 0x00 | ||
387 | #define ALL_COLOR_LED 0x01 /* streaming state */ | ||
388 | #define BLINK_GREEN_LED 0x02 /* connect state */ | ||
389 | #define BLINK_RED_LED 0x04 | ||
390 | #define BLINK_ORANGE_LED 0x10 /* disconnect state */ | ||
391 | #define SOLID_GREEN_LED 0x20 /* tuning/seeking state */ | ||
392 | #define SOLID_RED_LED 0x40 /* bootload state */ | ||
393 | #define SOLID_ORANGE_LED 0x80 | ||
394 | |||
395 | |||
396 | |||
397 | /************************************************************************** | ||
398 | * Stream State Definitions | ||
399 | **************************************************************************/ | ||
400 | #define STREAM_COMMAND 0x36 | ||
401 | #define STREAM_VIDPID 0x00 | ||
402 | #define STREAM_AUDIO 0xff | ||
403 | |||
404 | |||
405 | |||
406 | /************************************************************************** | ||
407 | * Bootloader / Flash Commands | ||
408 | **************************************************************************/ | ||
409 | |||
410 | /* unique id sent to bootloader and required to put into a bootload state */ | ||
411 | #define UNIQUE_BL_ID 0x34 | ||
412 | |||
413 | /* mask for the flash data */ | ||
414 | #define FLASH_DATA_MASK 0x55 | ||
415 | |||
416 | /* bootloader commands */ | ||
417 | #define GET_SW_VERSION_COMMAND 0x00 | ||
418 | #define SET_PAGE_COMMAND 0x01 | ||
419 | #define ERASE_PAGE_COMMAND 0x02 | ||
420 | #define WRITE_PAGE_COMMAND 0x03 | ||
421 | #define CRC_ON_PAGE_COMMAND 0x04 | ||
422 | #define READ_FLASH_BYTE_COMMAND 0x05 | ||
423 | #define RESET_DEVICE_COMMAND 0x06 | ||
424 | #define GET_HW_VERSION_COMMAND 0x07 | ||
425 | #define BLANK 0xff | ||
426 | |||
427 | /* bootloader command responses */ | ||
428 | #define COMMAND_OK 0x01 | ||
429 | #define COMMAND_FAILED 0x02 | ||
430 | #define COMMAND_PENDING 0x03 | ||
431 | |||
432 | |||
433 | |||
434 | /************************************************************************** | ||
435 | * General Driver Definitions | ||
436 | **************************************************************************/ | ||
437 | |||
438 | /* | ||
439 | * si470x_device - private data | ||
440 | */ | ||
441 | struct si470x_device { | ||
442 | /* reference to USB and video device */ | ||
443 | struct usb_device *usbdev; | ||
444 | struct usb_interface *intf; | ||
445 | struct video_device *videodev; | ||
446 | |||
447 | /* Interrupt endpoint handling */ | ||
448 | char *int_in_buffer; | ||
449 | struct usb_endpoint_descriptor *int_in_endpoint; | ||
450 | struct urb *int_in_urb; | ||
451 | int int_in_running; | ||
452 | |||
453 | /* driver management */ | ||
454 | unsigned int users; | ||
455 | unsigned char disconnected; | ||
456 | struct mutex disconnect_lock; | ||
457 | |||
458 | /* Silabs internal registers (0..15) */ | ||
459 | unsigned short registers[RADIO_REGISTER_NUM]; | ||
460 | |||
461 | /* RDS receive buffer */ | ||
462 | wait_queue_head_t read_queue; | ||
463 | struct mutex lock; /* buffer locking */ | ||
464 | unsigned char *buffer; /* size is always multiple of three */ | ||
465 | unsigned int buf_size; | ||
466 | unsigned int rd_index; | ||
467 | unsigned int wr_index; | ||
468 | |||
469 | /* scratch page */ | ||
470 | unsigned char software_version; | ||
471 | unsigned char hardware_version; | ||
472 | }; | ||
473 | |||
474 | |||
475 | /* | ||
476 | * The frequency is set in units of 62.5 Hz when using V4L2_TUNER_CAP_LOW, | ||
477 | * 62.5 kHz otherwise. | ||
478 | * The tuner is able to have a channel spacing of 50, 100 or 200 kHz. | ||
479 | * tuner->capability is therefore set to V4L2_TUNER_CAP_LOW | ||
480 | * The FREQ_MUL is then: 1 MHz / 62.5 Hz = 16000 | ||
481 | */ | ||
482 | #define FREQ_MUL (1000000 / 62.5) | ||
483 | |||
484 | |||
485 | |||
486 | /************************************************************************** | ||
487 | * General Driver Functions - REGISTER_REPORTs | ||
488 | **************************************************************************/ | ||
489 | |||
490 | /* | ||
491 | * si470x_get_report - receive a HID report | ||
492 | */ | ||
493 | static int si470x_get_report(struct si470x_device *radio, void *buf, int size) | ||
494 | { | ||
495 | unsigned char *report = (unsigned char *) buf; | ||
496 | int retval; | ||
497 | |||
498 | retval = usb_control_msg(radio->usbdev, | ||
499 | usb_rcvctrlpipe(radio->usbdev, 0), | ||
500 | HID_REQ_GET_REPORT, | ||
501 | USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN, | ||
502 | report[0], 2, | ||
503 | buf, size, usb_timeout); | ||
504 | |||
505 | if (retval < 0) | ||
506 | printk(KERN_WARNING DRIVER_NAME | ||
507 | ": si470x_get_report: usb_control_msg returned %d\n", | ||
508 | retval); | ||
509 | return retval; | ||
510 | } | ||
511 | |||
512 | |||
513 | /* | ||
514 | * si470x_set_report - send a HID report | ||
515 | */ | ||
516 | static int si470x_set_report(struct si470x_device *radio, void *buf, int size) | ||
517 | { | ||
518 | unsigned char *report = (unsigned char *) buf; | ||
519 | int retval; | ||
520 | |||
521 | retval = usb_control_msg(radio->usbdev, | ||
522 | usb_sndctrlpipe(radio->usbdev, 0), | ||
523 | HID_REQ_SET_REPORT, | ||
524 | USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT, | ||
525 | report[0], 2, | ||
526 | buf, size, usb_timeout); | ||
527 | |||
528 | if (retval < 0) | ||
529 | printk(KERN_WARNING DRIVER_NAME | ||
530 | ": si470x_set_report: usb_control_msg returned %d\n", | ||
531 | retval); | ||
532 | return retval; | ||
533 | } | ||
534 | |||
535 | |||
536 | /* | ||
537 | * si470x_get_register - read register | ||
538 | */ | ||
539 | static int si470x_get_register(struct si470x_device *radio, int regnr) | ||
540 | { | ||
541 | unsigned char buf[REGISTER_REPORT_SIZE]; | ||
542 | int retval; | ||
543 | |||
544 | buf[0] = REGISTER_REPORT(regnr); | ||
545 | |||
546 | retval = si470x_get_report(radio, (void *) &buf, sizeof(buf)); | ||
547 | |||
548 | if (retval >= 0) | ||
549 | radio->registers[regnr] = get_unaligned_be16(&buf[1]); | ||
550 | |||
551 | return (retval < 0) ? -EINVAL : 0; | ||
552 | } | ||
553 | |||
554 | |||
555 | /* | ||
556 | * si470x_set_register - write register | ||
557 | */ | ||
558 | static int si470x_set_register(struct si470x_device *radio, int regnr) | ||
559 | { | ||
560 | unsigned char buf[REGISTER_REPORT_SIZE]; | ||
561 | int retval; | ||
562 | |||
563 | buf[0] = REGISTER_REPORT(regnr); | ||
564 | put_unaligned_be16(radio->registers[regnr], &buf[1]); | ||
565 | |||
566 | retval = si470x_set_report(radio, (void *) &buf, sizeof(buf)); | ||
567 | |||
568 | return (retval < 0) ? -EINVAL : 0; | ||
569 | } | ||
570 | |||
571 | |||
572 | /* | ||
573 | * si470x_set_chan - set the channel | ||
574 | */ | ||
575 | static int si470x_set_chan(struct si470x_device *radio, unsigned short chan) | ||
576 | { | ||
577 | int retval; | ||
578 | unsigned long timeout; | ||
579 | bool timed_out = 0; | ||
580 | |||
581 | /* start tuning */ | ||
582 | radio->registers[CHANNEL] &= ~CHANNEL_CHAN; | ||
583 | radio->registers[CHANNEL] |= CHANNEL_TUNE | chan; | ||
584 | retval = si470x_set_register(radio, CHANNEL); | ||
585 | if (retval < 0) | ||
586 | goto done; | ||
587 | |||
588 | /* wait till tune operation has completed */ | ||
589 | timeout = jiffies + msecs_to_jiffies(tune_timeout); | ||
590 | do { | ||
591 | retval = si470x_get_register(radio, STATUSRSSI); | ||
592 | if (retval < 0) | ||
593 | goto stop; | ||
594 | timed_out = time_after(jiffies, timeout); | ||
595 | } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) && | ||
596 | (!timed_out)); | ||
597 | if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) | ||
598 | printk(KERN_WARNING DRIVER_NAME ": tune does not complete\n"); | ||
599 | if (timed_out) | ||
600 | printk(KERN_WARNING DRIVER_NAME | ||
601 | ": tune timed out after %u ms\n", tune_timeout); | ||
602 | |||
603 | stop: | ||
604 | /* stop tuning */ | ||
605 | radio->registers[CHANNEL] &= ~CHANNEL_TUNE; | ||
606 | retval = si470x_set_register(radio, CHANNEL); | ||
607 | |||
608 | done: | ||
609 | return retval; | ||
610 | } | ||
611 | |||
612 | |||
613 | /* | ||
614 | * si470x_get_freq - get the frequency | ||
615 | */ | ||
616 | static int si470x_get_freq(struct si470x_device *radio, unsigned int *freq) | ||
617 | { | ||
618 | unsigned int spacing, band_bottom; | ||
619 | unsigned short chan; | ||
620 | int retval; | ||
621 | |||
622 | /* Spacing (kHz) */ | ||
623 | switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) { | ||
624 | /* 0: 200 kHz (USA, Australia) */ | ||
625 | case 0: | ||
626 | spacing = 0.200 * FREQ_MUL; break; | ||
627 | /* 1: 100 kHz (Europe, Japan) */ | ||
628 | case 1: | ||
629 | spacing = 0.100 * FREQ_MUL; break; | ||
630 | /* 2: 50 kHz */ | ||
631 | default: | ||
632 | spacing = 0.050 * FREQ_MUL; break; | ||
633 | }; | ||
634 | |||
635 | /* Bottom of Band (MHz) */ | ||
636 | switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) { | ||
637 | /* 0: 87.5 - 108 MHz (USA, Europe) */ | ||
638 | case 0: | ||
639 | band_bottom = 87.5 * FREQ_MUL; break; | ||
640 | /* 1: 76 - 108 MHz (Japan wide band) */ | ||
641 | default: | ||
642 | band_bottom = 76 * FREQ_MUL; break; | ||
643 | /* 2: 76 - 90 MHz (Japan) */ | ||
644 | case 2: | ||
645 | band_bottom = 76 * FREQ_MUL; break; | ||
646 | }; | ||
647 | |||
648 | /* read channel */ | ||
649 | retval = si470x_get_register(radio, READCHAN); | ||
650 | chan = radio->registers[READCHAN] & READCHAN_READCHAN; | ||
651 | |||
652 | /* Frequency (MHz) = Spacing (kHz) x Channel + Bottom of Band (MHz) */ | ||
653 | *freq = chan * spacing + band_bottom; | ||
654 | |||
655 | return retval; | ||
656 | } | ||
657 | |||
658 | |||
659 | /* | ||
660 | * si470x_set_freq - set the frequency | ||
661 | */ | ||
662 | static int si470x_set_freq(struct si470x_device *radio, unsigned int freq) | ||
663 | { | ||
664 | unsigned int spacing, band_bottom; | ||
665 | unsigned short chan; | ||
666 | |||
667 | /* Spacing (kHz) */ | ||
668 | switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) { | ||
669 | /* 0: 200 kHz (USA, Australia) */ | ||
670 | case 0: | ||
671 | spacing = 0.200 * FREQ_MUL; break; | ||
672 | /* 1: 100 kHz (Europe, Japan) */ | ||
673 | case 1: | ||
674 | spacing = 0.100 * FREQ_MUL; break; | ||
675 | /* 2: 50 kHz */ | ||
676 | default: | ||
677 | spacing = 0.050 * FREQ_MUL; break; | ||
678 | }; | ||
679 | |||
680 | /* Bottom of Band (MHz) */ | ||
681 | switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) { | ||
682 | /* 0: 87.5 - 108 MHz (USA, Europe) */ | ||
683 | case 0: | ||
684 | band_bottom = 87.5 * FREQ_MUL; break; | ||
685 | /* 1: 76 - 108 MHz (Japan wide band) */ | ||
686 | default: | ||
687 | band_bottom = 76 * FREQ_MUL; break; | ||
688 | /* 2: 76 - 90 MHz (Japan) */ | ||
689 | case 2: | ||
690 | band_bottom = 76 * FREQ_MUL; break; | ||
691 | }; | ||
692 | |||
693 | /* Chan = [ Freq (Mhz) - Bottom of Band (MHz) ] / Spacing (kHz) */ | ||
694 | chan = (freq - band_bottom) / spacing; | ||
695 | |||
696 | return si470x_set_chan(radio, chan); | ||
697 | } | ||
698 | |||
699 | |||
700 | /* | ||
701 | * si470x_set_seek - set seek | ||
702 | */ | ||
703 | static int si470x_set_seek(struct si470x_device *radio, | ||
704 | unsigned int wrap_around, unsigned int seek_upward) | ||
705 | { | ||
706 | int retval = 0; | ||
707 | unsigned long timeout; | ||
708 | bool timed_out = 0; | ||
709 | |||
710 | /* start seeking */ | ||
711 | radio->registers[POWERCFG] |= POWERCFG_SEEK; | ||
712 | if (wrap_around == 1) | ||
713 | radio->registers[POWERCFG] &= ~POWERCFG_SKMODE; | ||
714 | else | ||
715 | radio->registers[POWERCFG] |= POWERCFG_SKMODE; | ||
716 | if (seek_upward == 1) | ||
717 | radio->registers[POWERCFG] |= POWERCFG_SEEKUP; | ||
718 | else | ||
719 | radio->registers[POWERCFG] &= ~POWERCFG_SEEKUP; | ||
720 | retval = si470x_set_register(radio, POWERCFG); | ||
721 | if (retval < 0) | ||
722 | goto done; | ||
723 | |||
724 | /* wait till seek operation has completed */ | ||
725 | timeout = jiffies + msecs_to_jiffies(seek_timeout); | ||
726 | do { | ||
727 | retval = si470x_get_register(radio, STATUSRSSI); | ||
728 | if (retval < 0) | ||
729 | goto stop; | ||
730 | timed_out = time_after(jiffies, timeout); | ||
731 | } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) && | ||
732 | (!timed_out)); | ||
733 | if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) | ||
734 | printk(KERN_WARNING DRIVER_NAME ": seek does not complete\n"); | ||
735 | if (radio->registers[STATUSRSSI] & STATUSRSSI_SF) | ||
736 | printk(KERN_WARNING DRIVER_NAME | ||
737 | ": seek failed / band limit reached\n"); | ||
738 | if (timed_out) | ||
739 | printk(KERN_WARNING DRIVER_NAME | ||
740 | ": seek timed out after %u ms\n", seek_timeout); | ||
741 | |||
742 | stop: | ||
743 | /* stop seeking */ | ||
744 | radio->registers[POWERCFG] &= ~POWERCFG_SEEK; | ||
745 | retval = si470x_set_register(radio, POWERCFG); | ||
746 | |||
747 | done: | ||
748 | /* try again, if timed out */ | ||
749 | if ((retval == 0) && timed_out) | ||
750 | retval = -EAGAIN; | ||
751 | |||
752 | return retval; | ||
753 | } | ||
754 | |||
755 | |||
756 | /* | ||
757 | * si470x_start - switch on radio | ||
758 | */ | ||
759 | static int si470x_start(struct si470x_device *radio) | ||
760 | { | ||
761 | int retval; | ||
762 | |||
763 | /* powercfg */ | ||
764 | radio->registers[POWERCFG] = | ||
765 | POWERCFG_DMUTE | POWERCFG_ENABLE | POWERCFG_RDSM; | ||
766 | retval = si470x_set_register(radio, POWERCFG); | ||
767 | if (retval < 0) | ||
768 | goto done; | ||
769 | |||
770 | /* sysconfig 1 */ | ||
771 | radio->registers[SYSCONFIG1] = SYSCONFIG1_DE; | ||
772 | retval = si470x_set_register(radio, SYSCONFIG1); | ||
773 | if (retval < 0) | ||
774 | goto done; | ||
775 | |||
776 | /* sysconfig 2 */ | ||
777 | radio->registers[SYSCONFIG2] = | ||
778 | (0x3f << 8) | /* SEEKTH */ | ||
779 | ((band << 6) & SYSCONFIG2_BAND) | /* BAND */ | ||
780 | ((space << 4) & SYSCONFIG2_SPACE) | /* SPACE */ | ||
781 | 15; /* VOLUME (max) */ | ||
782 | retval = si470x_set_register(radio, SYSCONFIG2); | ||
783 | if (retval < 0) | ||
784 | goto done; | ||
785 | |||
786 | /* reset last channel */ | ||
787 | retval = si470x_set_chan(radio, | ||
788 | radio->registers[CHANNEL] & CHANNEL_CHAN); | ||
789 | |||
790 | done: | ||
791 | return retval; | ||
792 | } | ||
793 | |||
794 | |||
795 | /* | ||
796 | * si470x_stop - switch off radio | ||
797 | */ | ||
798 | static int si470x_stop(struct si470x_device *radio) | ||
799 | { | ||
800 | int retval; | ||
801 | |||
802 | /* sysconfig 1 */ | ||
803 | radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS; | ||
804 | retval = si470x_set_register(radio, SYSCONFIG1); | ||
805 | if (retval < 0) | ||
806 | goto done; | ||
807 | |||
808 | /* powercfg */ | ||
809 | radio->registers[POWERCFG] &= ~POWERCFG_DMUTE; | ||
810 | /* POWERCFG_ENABLE has to automatically go low */ | ||
811 | radio->registers[POWERCFG] |= POWERCFG_ENABLE | POWERCFG_DISABLE; | ||
812 | retval = si470x_set_register(radio, POWERCFG); | ||
813 | |||
814 | done: | ||
815 | return retval; | ||
816 | } | ||
817 | |||
818 | |||
819 | /* | ||
820 | * si470x_rds_on - switch on rds reception | ||
821 | */ | ||
822 | static int si470x_rds_on(struct si470x_device *radio) | ||
823 | { | ||
824 | int retval; | ||
825 | |||
826 | /* sysconfig 1 */ | ||
827 | mutex_lock(&radio->lock); | ||
828 | radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDS; | ||
829 | retval = si470x_set_register(radio, SYSCONFIG1); | ||
830 | if (retval < 0) | ||
831 | radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS; | ||
832 | mutex_unlock(&radio->lock); | ||
833 | |||
834 | return retval; | ||
835 | } | ||
836 | |||
837 | |||
838 | |||
839 | /************************************************************************** | ||
840 | * General Driver Functions - ENTIRE_REPORT | ||
841 | **************************************************************************/ | ||
842 | |||
843 | /* | ||
844 | * si470x_get_all_registers - read entire registers | ||
845 | */ | ||
846 | static int si470x_get_all_registers(struct si470x_device *radio) | ||
847 | { | ||
848 | unsigned char buf[ENTIRE_REPORT_SIZE]; | ||
849 | int retval; | ||
850 | unsigned char regnr; | ||
851 | |||
852 | buf[0] = ENTIRE_REPORT; | ||
853 | |||
854 | retval = si470x_get_report(radio, (void *) &buf, sizeof(buf)); | ||
855 | |||
856 | if (retval >= 0) | ||
857 | for (regnr = 0; regnr < RADIO_REGISTER_NUM; regnr++) | ||
858 | radio->registers[regnr] = get_unaligned_be16( | ||
859 | &buf[regnr * RADIO_REGISTER_SIZE + 1]); | ||
860 | |||
861 | return (retval < 0) ? -EINVAL : 0; | ||
862 | } | ||
863 | |||
864 | |||
865 | |||
866 | /************************************************************************** | ||
867 | * General Driver Functions - LED_REPORT | ||
868 | **************************************************************************/ | ||
869 | |||
870 | /* | ||
871 | * si470x_set_led_state - sets the led state | ||
872 | */ | ||
873 | static int si470x_set_led_state(struct si470x_device *radio, | ||
874 | unsigned char led_state) | ||
875 | { | ||
876 | unsigned char buf[LED_REPORT_SIZE]; | ||
877 | int retval; | ||
878 | |||
879 | buf[0] = LED_REPORT; | ||
880 | buf[1] = LED_COMMAND; | ||
881 | buf[2] = led_state; | ||
882 | |||
883 | retval = si470x_set_report(radio, (void *) &buf, sizeof(buf)); | ||
884 | |||
885 | return (retval < 0) ? -EINVAL : 0; | ||
886 | } | ||
887 | |||
888 | |||
889 | |||
890 | /************************************************************************** | ||
891 | * General Driver Functions - SCRATCH_REPORT | ||
892 | **************************************************************************/ | ||
893 | |||
894 | /* | ||
895 | * si470x_get_scratch_versions - gets the scratch page and version infos | ||
896 | */ | ||
897 | static int si470x_get_scratch_page_versions(struct si470x_device *radio) | ||
898 | { | ||
899 | unsigned char buf[SCRATCH_REPORT_SIZE]; | ||
900 | int retval; | ||
901 | |||
902 | buf[0] = SCRATCH_REPORT; | ||
903 | |||
904 | retval = si470x_get_report(radio, (void *) &buf, sizeof(buf)); | ||
905 | |||
906 | if (retval < 0) | ||
907 | printk(KERN_WARNING DRIVER_NAME ": si470x_get_scratch: " | ||
908 | "si470x_get_report returned %d\n", retval); | ||
909 | else { | ||
910 | radio->software_version = buf[1]; | ||
911 | radio->hardware_version = buf[2]; | ||
912 | } | ||
913 | |||
914 | return (retval < 0) ? -EINVAL : 0; | ||
915 | } | ||
916 | |||
917 | |||
918 | |||
919 | /************************************************************************** | ||
920 | * RDS Driver Functions | ||
921 | **************************************************************************/ | ||
922 | |||
923 | /* | ||
924 | * si470x_int_in_callback - rds callback and processing function | ||
925 | * | ||
926 | * TODO: do we need to use mutex locks in some sections? | ||
927 | */ | ||
928 | static void si470x_int_in_callback(struct urb *urb) | ||
929 | { | ||
930 | struct si470x_device *radio = urb->context; | ||
931 | unsigned char buf[RDS_REPORT_SIZE]; | ||
932 | int retval; | ||
933 | unsigned char regnr; | ||
934 | unsigned char blocknum; | ||
935 | unsigned short bler; /* rds block errors */ | ||
936 | unsigned short rds; | ||
937 | unsigned char tmpbuf[3]; | ||
938 | |||
939 | if (urb->status) { | ||
940 | if (urb->status == -ENOENT || | ||
941 | urb->status == -ECONNRESET || | ||
942 | urb->status == -ESHUTDOWN) { | ||
943 | return; | ||
944 | } else { | ||
945 | printk(KERN_WARNING DRIVER_NAME | ||
946 | ": non-zero urb status (%d)\n", urb->status); | ||
947 | goto resubmit; /* Maybe we can recover. */ | ||
948 | } | ||
949 | } | ||
950 | |||
951 | /* safety checks */ | ||
952 | if (radio->disconnected) | ||
953 | return; | ||
954 | if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) | ||
955 | goto resubmit; | ||
956 | |||
957 | if (urb->actual_length > 0) { | ||
958 | /* Update RDS registers with URB data */ | ||
959 | buf[0] = RDS_REPORT; | ||
960 | for (regnr = 0; regnr < RDS_REGISTER_NUM; regnr++) | ||
961 | radio->registers[STATUSRSSI + regnr] = | ||
962 | get_unaligned_be16(&radio->int_in_buffer[ | ||
963 | regnr * RADIO_REGISTER_SIZE + 1]); | ||
964 | /* get rds blocks */ | ||
965 | if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0) { | ||
966 | /* No RDS group ready, better luck next time */ | ||
967 | goto resubmit; | ||
968 | } | ||
969 | if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSS) == 0) { | ||
970 | /* RDS decoder not synchronized */ | ||
971 | goto resubmit; | ||
972 | } | ||
973 | for (blocknum = 0; blocknum < 4; blocknum++) { | ||
974 | switch (blocknum) { | ||
975 | default: | ||
976 | bler = (radio->registers[STATUSRSSI] & | ||
977 | STATUSRSSI_BLERA) >> 9; | ||
978 | rds = radio->registers[RDSA]; | ||
979 | break; | ||
980 | case 1: | ||
981 | bler = (radio->registers[READCHAN] & | ||
982 | READCHAN_BLERB) >> 14; | ||
983 | rds = radio->registers[RDSB]; | ||
984 | break; | ||
985 | case 2: | ||
986 | bler = (radio->registers[READCHAN] & | ||
987 | READCHAN_BLERC) >> 12; | ||
988 | rds = radio->registers[RDSC]; | ||
989 | break; | ||
990 | case 3: | ||
991 | bler = (radio->registers[READCHAN] & | ||
992 | READCHAN_BLERD) >> 10; | ||
993 | rds = radio->registers[RDSD]; | ||
994 | break; | ||
995 | }; | ||
996 | |||
997 | /* Fill the V4L2 RDS buffer */ | ||
998 | put_unaligned_le16(rds, &tmpbuf); | ||
999 | tmpbuf[2] = blocknum; /* offset name */ | ||
1000 | tmpbuf[2] |= blocknum << 3; /* received offset */ | ||
1001 | if (bler > max_rds_errors) | ||
1002 | tmpbuf[2] |= 0x80; /* uncorrectable errors */ | ||
1003 | else if (bler > 0) | ||
1004 | tmpbuf[2] |= 0x40; /* corrected error(s) */ | ||
1005 | |||
1006 | /* copy RDS block to internal buffer */ | ||
1007 | memcpy(&radio->buffer[radio->wr_index], &tmpbuf, 3); | ||
1008 | radio->wr_index += 3; | ||
1009 | |||
1010 | /* wrap write pointer */ | ||
1011 | if (radio->wr_index >= radio->buf_size) | ||
1012 | radio->wr_index = 0; | ||
1013 | |||
1014 | /* check for overflow */ | ||
1015 | if (radio->wr_index == radio->rd_index) { | ||
1016 | /* increment and wrap read pointer */ | ||
1017 | radio->rd_index += 3; | ||
1018 | if (radio->rd_index >= radio->buf_size) | ||
1019 | radio->rd_index = 0; | ||
1020 | } | ||
1021 | } | ||
1022 | if (radio->wr_index != radio->rd_index) | ||
1023 | wake_up_interruptible(&radio->read_queue); | ||
1024 | } | ||
1025 | |||
1026 | resubmit: | ||
1027 | /* Resubmit if we're still running. */ | ||
1028 | if (radio->int_in_running && radio->usbdev) { | ||
1029 | retval = usb_submit_urb(radio->int_in_urb, GFP_ATOMIC); | ||
1030 | if (retval) { | ||
1031 | printk(KERN_WARNING DRIVER_NAME | ||
1032 | ": resubmitting urb failed (%d)", retval); | ||
1033 | radio->int_in_running = 0; | ||
1034 | } | ||
1035 | } | ||
1036 | } | ||
1037 | |||
1038 | |||
1039 | |||
1040 | /************************************************************************** | ||
1041 | * File Operations Interface | ||
1042 | **************************************************************************/ | ||
1043 | |||
1044 | /* | ||
1045 | * si470x_fops_read - read RDS data | ||
1046 | */ | ||
1047 | static ssize_t si470x_fops_read(struct file *file, char __user *buf, | ||
1048 | size_t count, loff_t *ppos) | ||
1049 | { | ||
1050 | struct si470x_device *radio = video_drvdata(file); | ||
1051 | int retval = 0; | ||
1052 | unsigned int block_count = 0; | ||
1053 | |||
1054 | /* switch on rds reception */ | ||
1055 | if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) { | ||
1056 | si470x_rds_on(radio); | ||
1057 | } | ||
1058 | |||
1059 | /* block if no new data available */ | ||
1060 | while (radio->wr_index == radio->rd_index) { | ||
1061 | if (file->f_flags & O_NONBLOCK) { | ||
1062 | retval = -EWOULDBLOCK; | ||
1063 | goto done; | ||
1064 | } | ||
1065 | if (wait_event_interruptible(radio->read_queue, | ||
1066 | radio->wr_index != radio->rd_index) < 0) { | ||
1067 | retval = -EINTR; | ||
1068 | goto done; | ||
1069 | } | ||
1070 | } | ||
1071 | |||
1072 | /* calculate block count from byte count */ | ||
1073 | count /= 3; | ||
1074 | |||
1075 | /* copy RDS block out of internal buffer and to user buffer */ | ||
1076 | mutex_lock(&radio->lock); | ||
1077 | while (block_count < count) { | ||
1078 | if (radio->rd_index == radio->wr_index) | ||
1079 | break; | ||
1080 | |||
1081 | /* always transfer rds complete blocks */ | ||
1082 | if (copy_to_user(buf, &radio->buffer[radio->rd_index], 3)) | ||
1083 | /* retval = -EFAULT; */ | ||
1084 | break; | ||
1085 | |||
1086 | /* increment and wrap read pointer */ | ||
1087 | radio->rd_index += 3; | ||
1088 | if (radio->rd_index >= radio->buf_size) | ||
1089 | radio->rd_index = 0; | ||
1090 | |||
1091 | /* increment counters */ | ||
1092 | block_count++; | ||
1093 | buf += 3; | ||
1094 | retval += 3; | ||
1095 | } | ||
1096 | mutex_unlock(&radio->lock); | ||
1097 | |||
1098 | done: | ||
1099 | return retval; | ||
1100 | } | ||
1101 | |||
1102 | |||
1103 | /* | ||
1104 | * si470x_fops_poll - poll RDS data | ||
1105 | */ | ||
1106 | static unsigned int si470x_fops_poll(struct file *file, | ||
1107 | struct poll_table_struct *pts) | ||
1108 | { | ||
1109 | struct si470x_device *radio = video_drvdata(file); | ||
1110 | int retval = 0; | ||
1111 | |||
1112 | /* switch on rds reception */ | ||
1113 | if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) { | ||
1114 | si470x_rds_on(radio); | ||
1115 | } | ||
1116 | |||
1117 | poll_wait(file, &radio->read_queue, pts); | ||
1118 | |||
1119 | if (radio->rd_index != radio->wr_index) | ||
1120 | retval = POLLIN | POLLRDNORM; | ||
1121 | |||
1122 | return retval; | ||
1123 | } | ||
1124 | |||
1125 | |||
1126 | /* | ||
1127 | * si470x_fops_open - file open | ||
1128 | */ | ||
1129 | static int si470x_fops_open(struct file *file) | ||
1130 | { | ||
1131 | struct si470x_device *radio = video_drvdata(file); | ||
1132 | int retval; | ||
1133 | |||
1134 | lock_kernel(); | ||
1135 | radio->users++; | ||
1136 | |||
1137 | retval = usb_autopm_get_interface(radio->intf); | ||
1138 | if (retval < 0) { | ||
1139 | radio->users--; | ||
1140 | retval = -EIO; | ||
1141 | goto done; | ||
1142 | } | ||
1143 | |||
1144 | if (radio->users == 1) { | ||
1145 | /* start radio */ | ||
1146 | retval = si470x_start(radio); | ||
1147 | if (retval < 0) { | ||
1148 | usb_autopm_put_interface(radio->intf); | ||
1149 | goto done; | ||
1150 | } | ||
1151 | |||
1152 | /* initialize interrupt urb */ | ||
1153 | usb_fill_int_urb(radio->int_in_urb, radio->usbdev, | ||
1154 | usb_rcvintpipe(radio->usbdev, | ||
1155 | radio->int_in_endpoint->bEndpointAddress), | ||
1156 | radio->int_in_buffer, | ||
1157 | le16_to_cpu(radio->int_in_endpoint->wMaxPacketSize), | ||
1158 | si470x_int_in_callback, | ||
1159 | radio, | ||
1160 | radio->int_in_endpoint->bInterval); | ||
1161 | |||
1162 | radio->int_in_running = 1; | ||
1163 | mb(); | ||
1164 | |||
1165 | retval = usb_submit_urb(radio->int_in_urb, GFP_KERNEL); | ||
1166 | if (retval) { | ||
1167 | printk(KERN_INFO DRIVER_NAME | ||
1168 | ": submitting int urb failed (%d)\n", retval); | ||
1169 | radio->int_in_running = 0; | ||
1170 | usb_autopm_put_interface(radio->intf); | ||
1171 | } | ||
1172 | } | ||
1173 | |||
1174 | done: | ||
1175 | unlock_kernel(); | ||
1176 | return retval; | ||
1177 | } | ||
1178 | |||
1179 | |||
1180 | /* | ||
1181 | * si470x_fops_release - file release | ||
1182 | */ | ||
1183 | static int si470x_fops_release(struct file *file) | ||
1184 | { | ||
1185 | struct si470x_device *radio = video_drvdata(file); | ||
1186 | int retval = 0; | ||
1187 | |||
1188 | /* safety check */ | ||
1189 | if (!radio) { | ||
1190 | retval = -ENODEV; | ||
1191 | goto done; | ||
1192 | } | ||
1193 | |||
1194 | mutex_lock(&radio->disconnect_lock); | ||
1195 | radio->users--; | ||
1196 | if (radio->users == 0) { | ||
1197 | /* shutdown interrupt handler */ | ||
1198 | if (radio->int_in_running) { | ||
1199 | radio->int_in_running = 0; | ||
1200 | if (radio->int_in_urb) | ||
1201 | usb_kill_urb(radio->int_in_urb); | ||
1202 | } | ||
1203 | |||
1204 | if (radio->disconnected) { | ||
1205 | video_unregister_device(radio->videodev); | ||
1206 | kfree(radio->int_in_buffer); | ||
1207 | kfree(radio->buffer); | ||
1208 | kfree(radio); | ||
1209 | goto unlock; | ||
1210 | } | ||
1211 | |||
1212 | /* cancel read processes */ | ||
1213 | wake_up_interruptible(&radio->read_queue); | ||
1214 | |||
1215 | /* stop radio */ | ||
1216 | retval = si470x_stop(radio); | ||
1217 | usb_autopm_put_interface(radio->intf); | ||
1218 | } | ||
1219 | unlock: | ||
1220 | mutex_unlock(&radio->disconnect_lock); | ||
1221 | done: | ||
1222 | return retval; | ||
1223 | } | ||
1224 | |||
1225 | |||
1226 | /* | ||
1227 | * si470x_fops - file operations interface | ||
1228 | */ | ||
1229 | static const struct v4l2_file_operations si470x_fops = { | ||
1230 | .owner = THIS_MODULE, | ||
1231 | .read = si470x_fops_read, | ||
1232 | .poll = si470x_fops_poll, | ||
1233 | .ioctl = video_ioctl2, | ||
1234 | .open = si470x_fops_open, | ||
1235 | .release = si470x_fops_release, | ||
1236 | }; | ||
1237 | |||
1238 | |||
1239 | |||
1240 | /************************************************************************** | ||
1241 | * Video4Linux Interface | ||
1242 | **************************************************************************/ | ||
1243 | |||
1244 | /* | ||
1245 | * si470x_vidioc_querycap - query device capabilities | ||
1246 | */ | ||
1247 | static int si470x_vidioc_querycap(struct file *file, void *priv, | ||
1248 | struct v4l2_capability *capability) | ||
1249 | { | ||
1250 | struct si470x_device *radio = video_drvdata(file); | ||
1251 | |||
1252 | strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver)); | ||
1253 | strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card)); | ||
1254 | usb_make_path(radio->usbdev, capability->bus_info, sizeof(capability->bus_info)); | ||
1255 | capability->version = DRIVER_KERNEL_VERSION; | ||
1256 | capability->capabilities = V4L2_CAP_HW_FREQ_SEEK | | ||
1257 | V4L2_CAP_TUNER | V4L2_CAP_RADIO | V4L2_CAP_RDS_CAPTURE; | ||
1258 | |||
1259 | return 0; | ||
1260 | } | ||
1261 | |||
1262 | |||
1263 | /* | ||
1264 | * si470x_vidioc_queryctrl - enumerate control items | ||
1265 | */ | ||
1266 | static int si470x_vidioc_queryctrl(struct file *file, void *priv, | ||
1267 | struct v4l2_queryctrl *qc) | ||
1268 | { | ||
1269 | int retval = -EINVAL; | ||
1270 | |||
1271 | /* abort if qc->id is below V4L2_CID_BASE */ | ||
1272 | if (qc->id < V4L2_CID_BASE) | ||
1273 | goto done; | ||
1274 | |||
1275 | /* search video control */ | ||
1276 | switch (qc->id) { | ||
1277 | case V4L2_CID_AUDIO_VOLUME: | ||
1278 | return v4l2_ctrl_query_fill(qc, 0, 15, 1, 15); | ||
1279 | case V4L2_CID_AUDIO_MUTE: | ||
1280 | return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1); | ||
1281 | } | ||
1282 | |||
1283 | /* disable unsupported base controls */ | ||
1284 | /* to satisfy kradio and such apps */ | ||
1285 | if ((retval == -EINVAL) && (qc->id < V4L2_CID_LASTP1)) { | ||
1286 | qc->flags = V4L2_CTRL_FLAG_DISABLED; | ||
1287 | retval = 0; | ||
1288 | } | ||
1289 | |||
1290 | done: | ||
1291 | if (retval < 0) | ||
1292 | printk(KERN_WARNING DRIVER_NAME | ||
1293 | ": query controls failed with %d\n", retval); | ||
1294 | return retval; | ||
1295 | } | ||
1296 | |||
1297 | |||
1298 | /* | ||
1299 | * si470x_vidioc_g_ctrl - get the value of a control | ||
1300 | */ | ||
1301 | static int si470x_vidioc_g_ctrl(struct file *file, void *priv, | ||
1302 | struct v4l2_control *ctrl) | ||
1303 | { | ||
1304 | struct si470x_device *radio = video_drvdata(file); | ||
1305 | int retval = 0; | ||
1306 | |||
1307 | /* safety checks */ | ||
1308 | if (radio->disconnected) { | ||
1309 | retval = -EIO; | ||
1310 | goto done; | ||
1311 | } | ||
1312 | |||
1313 | switch (ctrl->id) { | ||
1314 | case V4L2_CID_AUDIO_VOLUME: | ||
1315 | ctrl->value = radio->registers[SYSCONFIG2] & | ||
1316 | SYSCONFIG2_VOLUME; | ||
1317 | break; | ||
1318 | case V4L2_CID_AUDIO_MUTE: | ||
1319 | ctrl->value = ((radio->registers[POWERCFG] & | ||
1320 | POWERCFG_DMUTE) == 0) ? 1 : 0; | ||
1321 | break; | ||
1322 | default: | ||
1323 | retval = -EINVAL; | ||
1324 | } | ||
1325 | |||
1326 | done: | ||
1327 | if (retval < 0) | ||
1328 | printk(KERN_WARNING DRIVER_NAME | ||
1329 | ": get control failed with %d\n", retval); | ||
1330 | return retval; | ||
1331 | } | ||
1332 | |||
1333 | |||
1334 | /* | ||
1335 | * si470x_vidioc_s_ctrl - set the value of a control | ||
1336 | */ | ||
1337 | static int si470x_vidioc_s_ctrl(struct file *file, void *priv, | ||
1338 | struct v4l2_control *ctrl) | ||
1339 | { | ||
1340 | struct si470x_device *radio = video_drvdata(file); | ||
1341 | int retval = 0; | ||
1342 | |||
1343 | /* safety checks */ | ||
1344 | if (radio->disconnected) { | ||
1345 | retval = -EIO; | ||
1346 | goto done; | ||
1347 | } | ||
1348 | |||
1349 | switch (ctrl->id) { | ||
1350 | case V4L2_CID_AUDIO_VOLUME: | ||
1351 | radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_VOLUME; | ||
1352 | radio->registers[SYSCONFIG2] |= ctrl->value; | ||
1353 | retval = si470x_set_register(radio, SYSCONFIG2); | ||
1354 | break; | ||
1355 | case V4L2_CID_AUDIO_MUTE: | ||
1356 | if (ctrl->value == 1) | ||
1357 | radio->registers[POWERCFG] &= ~POWERCFG_DMUTE; | ||
1358 | else | ||
1359 | radio->registers[POWERCFG] |= POWERCFG_DMUTE; | ||
1360 | retval = si470x_set_register(radio, POWERCFG); | ||
1361 | break; | ||
1362 | default: | ||
1363 | retval = -EINVAL; | ||
1364 | } | ||
1365 | |||
1366 | done: | ||
1367 | if (retval < 0) | ||
1368 | printk(KERN_WARNING DRIVER_NAME | ||
1369 | ": set control failed with %d\n", retval); | ||
1370 | return retval; | ||
1371 | } | ||
1372 | |||
1373 | |||
1374 | /* | ||
1375 | * si470x_vidioc_g_audio - get audio attributes | ||
1376 | */ | ||
1377 | static int si470x_vidioc_g_audio(struct file *file, void *priv, | ||
1378 | struct v4l2_audio *audio) | ||
1379 | { | ||
1380 | /* driver constants */ | ||
1381 | audio->index = 0; | ||
1382 | strcpy(audio->name, "Radio"); | ||
1383 | audio->capability = V4L2_AUDCAP_STEREO; | ||
1384 | audio->mode = 0; | ||
1385 | |||
1386 | return 0; | ||
1387 | } | ||
1388 | |||
1389 | |||
1390 | /* | ||
1391 | * si470x_vidioc_g_tuner - get tuner attributes | ||
1392 | */ | ||
1393 | static int si470x_vidioc_g_tuner(struct file *file, void *priv, | ||
1394 | struct v4l2_tuner *tuner) | ||
1395 | { | ||
1396 | struct si470x_device *radio = video_drvdata(file); | ||
1397 | int retval = 0; | ||
1398 | |||
1399 | /* safety checks */ | ||
1400 | if (radio->disconnected) { | ||
1401 | retval = -EIO; | ||
1402 | goto done; | ||
1403 | } | ||
1404 | if (tuner->index != 0) { | ||
1405 | retval = -EINVAL; | ||
1406 | goto done; | ||
1407 | } | ||
1408 | |||
1409 | retval = si470x_get_register(radio, STATUSRSSI); | ||
1410 | if (retval < 0) | ||
1411 | goto done; | ||
1412 | |||
1413 | /* driver constants */ | ||
1414 | strcpy(tuner->name, "FM"); | ||
1415 | tuner->type = V4L2_TUNER_RADIO; | ||
1416 | tuner->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO | | ||
1417 | V4L2_TUNER_CAP_RDS; | ||
1418 | |||
1419 | /* range limits */ | ||
1420 | switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) { | ||
1421 | /* 0: 87.5 - 108 MHz (USA, Europe, default) */ | ||
1422 | default: | ||
1423 | tuner->rangelow = 87.5 * FREQ_MUL; | ||
1424 | tuner->rangehigh = 108 * FREQ_MUL; | ||
1425 | break; | ||
1426 | /* 1: 76 - 108 MHz (Japan wide band) */ | ||
1427 | case 1 : | ||
1428 | tuner->rangelow = 76 * FREQ_MUL; | ||
1429 | tuner->rangehigh = 108 * FREQ_MUL; | ||
1430 | break; | ||
1431 | /* 2: 76 - 90 MHz (Japan) */ | ||
1432 | case 2 : | ||
1433 | tuner->rangelow = 76 * FREQ_MUL; | ||
1434 | tuner->rangehigh = 90 * FREQ_MUL; | ||
1435 | break; | ||
1436 | }; | ||
1437 | |||
1438 | /* stereo indicator == stereo (instead of mono) */ | ||
1439 | if ((radio->registers[STATUSRSSI] & STATUSRSSI_ST) == 0) | ||
1440 | tuner->rxsubchans = V4L2_TUNER_SUB_MONO; | ||
1441 | else | ||
1442 | tuner->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO; | ||
1443 | /* If there is a reliable method of detecting an RDS channel, | ||
1444 | then this code should check for that before setting this | ||
1445 | RDS subchannel. */ | ||
1446 | tuner->rxsubchans |= V4L2_TUNER_SUB_RDS; | ||
1447 | |||
1448 | /* mono/stereo selector */ | ||
1449 | if ((radio->registers[POWERCFG] & POWERCFG_MONO) == 0) | ||
1450 | tuner->audmode = V4L2_TUNER_MODE_STEREO; | ||
1451 | else | ||
1452 | tuner->audmode = V4L2_TUNER_MODE_MONO; | ||
1453 | |||
1454 | /* min is worst, max is best; signal:0..0xffff; rssi: 0..0xff */ | ||
1455 | /* measured in units of dbµV in 1 db increments (max at ~75 dbµV) */ | ||
1456 | tuner->signal = (radio->registers[STATUSRSSI] & STATUSRSSI_RSSI); | ||
1457 | /* the ideal factor is 0xffff/75 = 873,8 */ | ||
1458 | tuner->signal = (tuner->signal * 873) + (8 * tuner->signal / 10); | ||
1459 | |||
1460 | /* automatic frequency control: -1: freq to low, 1 freq to high */ | ||
1461 | /* AFCRL does only indicate that freq. differs, not if too low/high */ | ||
1462 | tuner->afc = (radio->registers[STATUSRSSI] & STATUSRSSI_AFCRL) ? 1 : 0; | ||
1463 | |||
1464 | done: | ||
1465 | if (retval < 0) | ||
1466 | printk(KERN_WARNING DRIVER_NAME | ||
1467 | ": get tuner failed with %d\n", retval); | ||
1468 | return retval; | ||
1469 | } | ||
1470 | |||
1471 | |||
1472 | /* | ||
1473 | * si470x_vidioc_s_tuner - set tuner attributes | ||
1474 | */ | ||
1475 | static int si470x_vidioc_s_tuner(struct file *file, void *priv, | ||
1476 | struct v4l2_tuner *tuner) | ||
1477 | { | ||
1478 | struct si470x_device *radio = video_drvdata(file); | ||
1479 | int retval = -EINVAL; | ||
1480 | |||
1481 | /* safety checks */ | ||
1482 | if (radio->disconnected) { | ||
1483 | retval = -EIO; | ||
1484 | goto done; | ||
1485 | } | ||
1486 | if (tuner->index != 0) | ||
1487 | goto done; | ||
1488 | |||
1489 | /* mono/stereo selector */ | ||
1490 | switch (tuner->audmode) { | ||
1491 | case V4L2_TUNER_MODE_MONO: | ||
1492 | radio->registers[POWERCFG] |= POWERCFG_MONO; /* force mono */ | ||
1493 | break; | ||
1494 | case V4L2_TUNER_MODE_STEREO: | ||
1495 | radio->registers[POWERCFG] &= ~POWERCFG_MONO; /* try stereo */ | ||
1496 | break; | ||
1497 | default: | ||
1498 | goto done; | ||
1499 | } | ||
1500 | |||
1501 | retval = si470x_set_register(radio, POWERCFG); | ||
1502 | |||
1503 | done: | ||
1504 | if (retval < 0) | ||
1505 | printk(KERN_WARNING DRIVER_NAME | ||
1506 | ": set tuner failed with %d\n", retval); | ||
1507 | return retval; | ||
1508 | } | ||
1509 | |||
1510 | |||
1511 | /* | ||
1512 | * si470x_vidioc_g_frequency - get tuner or modulator radio frequency | ||
1513 | */ | ||
1514 | static int si470x_vidioc_g_frequency(struct file *file, void *priv, | ||
1515 | struct v4l2_frequency *freq) | ||
1516 | { | ||
1517 | struct si470x_device *radio = video_drvdata(file); | ||
1518 | int retval = 0; | ||
1519 | |||
1520 | /* safety checks */ | ||
1521 | if (radio->disconnected) { | ||
1522 | retval = -EIO; | ||
1523 | goto done; | ||
1524 | } | ||
1525 | if (freq->tuner != 0) { | ||
1526 | retval = -EINVAL; | ||
1527 | goto done; | ||
1528 | } | ||
1529 | |||
1530 | freq->type = V4L2_TUNER_RADIO; | ||
1531 | retval = si470x_get_freq(radio, &freq->frequency); | ||
1532 | |||
1533 | done: | ||
1534 | if (retval < 0) | ||
1535 | printk(KERN_WARNING DRIVER_NAME | ||
1536 | ": get frequency failed with %d\n", retval); | ||
1537 | return retval; | ||
1538 | } | ||
1539 | |||
1540 | |||
1541 | /* | ||
1542 | * si470x_vidioc_s_frequency - set tuner or modulator radio frequency | ||
1543 | */ | ||
1544 | static int si470x_vidioc_s_frequency(struct file *file, void *priv, | ||
1545 | struct v4l2_frequency *freq) | ||
1546 | { | ||
1547 | struct si470x_device *radio = video_drvdata(file); | ||
1548 | int retval = 0; | ||
1549 | |||
1550 | /* safety checks */ | ||
1551 | if (radio->disconnected) { | ||
1552 | retval = -EIO; | ||
1553 | goto done; | ||
1554 | } | ||
1555 | if (freq->tuner != 0) { | ||
1556 | retval = -EINVAL; | ||
1557 | goto done; | ||
1558 | } | ||
1559 | |||
1560 | retval = si470x_set_freq(radio, freq->frequency); | ||
1561 | |||
1562 | done: | ||
1563 | if (retval < 0) | ||
1564 | printk(KERN_WARNING DRIVER_NAME | ||
1565 | ": set frequency failed with %d\n", retval); | ||
1566 | return retval; | ||
1567 | } | ||
1568 | |||
1569 | |||
1570 | /* | ||
1571 | * si470x_vidioc_s_hw_freq_seek - set hardware frequency seek | ||
1572 | */ | ||
1573 | static int si470x_vidioc_s_hw_freq_seek(struct file *file, void *priv, | ||
1574 | struct v4l2_hw_freq_seek *seek) | ||
1575 | { | ||
1576 | struct si470x_device *radio = video_drvdata(file); | ||
1577 | int retval = 0; | ||
1578 | |||
1579 | /* safety checks */ | ||
1580 | if (radio->disconnected) { | ||
1581 | retval = -EIO; | ||
1582 | goto done; | ||
1583 | } | ||
1584 | if (seek->tuner != 0) { | ||
1585 | retval = -EINVAL; | ||
1586 | goto done; | ||
1587 | } | ||
1588 | |||
1589 | retval = si470x_set_seek(radio, seek->wrap_around, seek->seek_upward); | ||
1590 | |||
1591 | done: | ||
1592 | if (retval < 0) | ||
1593 | printk(KERN_WARNING DRIVER_NAME | ||
1594 | ": set hardware frequency seek failed with %d\n", | ||
1595 | retval); | ||
1596 | return retval; | ||
1597 | } | ||
1598 | |||
1599 | |||
1600 | /* | ||
1601 | * si470x_ioctl_ops - video device ioctl operations | ||
1602 | */ | ||
1603 | static const struct v4l2_ioctl_ops si470x_ioctl_ops = { | ||
1604 | .vidioc_querycap = si470x_vidioc_querycap, | ||
1605 | .vidioc_queryctrl = si470x_vidioc_queryctrl, | ||
1606 | .vidioc_g_ctrl = si470x_vidioc_g_ctrl, | ||
1607 | .vidioc_s_ctrl = si470x_vidioc_s_ctrl, | ||
1608 | .vidioc_g_audio = si470x_vidioc_g_audio, | ||
1609 | .vidioc_g_tuner = si470x_vidioc_g_tuner, | ||
1610 | .vidioc_s_tuner = si470x_vidioc_s_tuner, | ||
1611 | .vidioc_g_frequency = si470x_vidioc_g_frequency, | ||
1612 | .vidioc_s_frequency = si470x_vidioc_s_frequency, | ||
1613 | .vidioc_s_hw_freq_seek = si470x_vidioc_s_hw_freq_seek, | ||
1614 | }; | ||
1615 | |||
1616 | |||
1617 | /* | ||
1618 | * si470x_viddev_template - video device interface | ||
1619 | */ | ||
1620 | static struct video_device si470x_viddev_template = { | ||
1621 | .fops = &si470x_fops, | ||
1622 | .name = DRIVER_NAME, | ||
1623 | .release = video_device_release, | ||
1624 | .ioctl_ops = &si470x_ioctl_ops, | ||
1625 | }; | ||
1626 | |||
1627 | |||
1628 | |||
1629 | /************************************************************************** | ||
1630 | * USB Interface | ||
1631 | **************************************************************************/ | ||
1632 | |||
1633 | /* | ||
1634 | * si470x_usb_driver_probe - probe for the device | ||
1635 | */ | ||
1636 | static int si470x_usb_driver_probe(struct usb_interface *intf, | ||
1637 | const struct usb_device_id *id) | ||
1638 | { | ||
1639 | struct si470x_device *radio; | ||
1640 | struct usb_host_interface *iface_desc; | ||
1641 | struct usb_endpoint_descriptor *endpoint; | ||
1642 | int i, int_end_size, retval = 0; | ||
1643 | |||
1644 | /* private data allocation and initialization */ | ||
1645 | radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL); | ||
1646 | if (!radio) { | ||
1647 | retval = -ENOMEM; | ||
1648 | goto err_initial; | ||
1649 | } | ||
1650 | radio->users = 0; | ||
1651 | radio->disconnected = 0; | ||
1652 | radio->usbdev = interface_to_usbdev(intf); | ||
1653 | radio->intf = intf; | ||
1654 | mutex_init(&radio->disconnect_lock); | ||
1655 | mutex_init(&radio->lock); | ||
1656 | |||
1657 | iface_desc = intf->cur_altsetting; | ||
1658 | |||
1659 | /* Set up interrupt endpoint information. */ | ||
1660 | for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { | ||
1661 | endpoint = &iface_desc->endpoint[i].desc; | ||
1662 | if (((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == | ||
1663 | USB_DIR_IN) && ((endpoint->bmAttributes & | ||
1664 | USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)) | ||
1665 | radio->int_in_endpoint = endpoint; | ||
1666 | } | ||
1667 | if (!radio->int_in_endpoint) { | ||
1668 | printk(KERN_INFO DRIVER_NAME | ||
1669 | ": could not find interrupt in endpoint\n"); | ||
1670 | retval = -EIO; | ||
1671 | goto err_radio; | ||
1672 | } | ||
1673 | |||
1674 | int_end_size = le16_to_cpu(radio->int_in_endpoint->wMaxPacketSize); | ||
1675 | |||
1676 | radio->int_in_buffer = kmalloc(int_end_size, GFP_KERNEL); | ||
1677 | if (!radio->int_in_buffer) { | ||
1678 | printk(KERN_INFO DRIVER_NAME | ||
1679 | "could not allocate int_in_buffer"); | ||
1680 | retval = -ENOMEM; | ||
1681 | goto err_radio; | ||
1682 | } | ||
1683 | |||
1684 | radio->int_in_urb = usb_alloc_urb(0, GFP_KERNEL); | ||
1685 | if (!radio->int_in_urb) { | ||
1686 | printk(KERN_INFO DRIVER_NAME "could not allocate int_in_urb"); | ||
1687 | retval = -ENOMEM; | ||
1688 | goto err_intbuffer; | ||
1689 | } | ||
1690 | |||
1691 | /* video device allocation and initialization */ | ||
1692 | radio->videodev = video_device_alloc(); | ||
1693 | if (!radio->videodev) { | ||
1694 | retval = -ENOMEM; | ||
1695 | goto err_intbuffer; | ||
1696 | } | ||
1697 | memcpy(radio->videodev, &si470x_viddev_template, | ||
1698 | sizeof(si470x_viddev_template)); | ||
1699 | video_set_drvdata(radio->videodev, radio); | ||
1700 | |||
1701 | /* show some infos about the specific si470x device */ | ||
1702 | if (si470x_get_all_registers(radio) < 0) { | ||
1703 | retval = -EIO; | ||
1704 | goto err_video; | ||
1705 | } | ||
1706 | printk(KERN_INFO DRIVER_NAME ": DeviceID=0x%4.4hx ChipID=0x%4.4hx\n", | ||
1707 | radio->registers[DEVICEID], radio->registers[CHIPID]); | ||
1708 | |||
1709 | /* get software and hardware versions */ | ||
1710 | if (si470x_get_scratch_page_versions(radio) < 0) { | ||
1711 | retval = -EIO; | ||
1712 | goto err_video; | ||
1713 | } | ||
1714 | printk(KERN_INFO DRIVER_NAME | ||
1715 | ": software version %d, hardware version %d\n", | ||
1716 | radio->software_version, radio->hardware_version); | ||
1717 | |||
1718 | /* check if device and firmware is current */ | ||
1719 | if ((radio->registers[CHIPID] & CHIPID_FIRMWARE) | ||
1720 | < RADIO_SW_VERSION_CURRENT) { | ||
1721 | printk(KERN_WARNING DRIVER_NAME | ||
1722 | ": This driver is known to work with " | ||
1723 | "firmware version %hu,\n", RADIO_SW_VERSION_CURRENT); | ||
1724 | printk(KERN_WARNING DRIVER_NAME | ||
1725 | ": but the device has firmware version %hu.\n", | ||
1726 | radio->registers[CHIPID] & CHIPID_FIRMWARE); | ||
1727 | printk(KERN_WARNING DRIVER_NAME | ||
1728 | ": If you have some trouble using this driver,\n"); | ||
1729 | printk(KERN_WARNING DRIVER_NAME | ||
1730 | ": please report to V4L ML at " | ||
1731 | "linux-media@vger.kernel.org\n"); | ||
1732 | } | ||
1733 | |||
1734 | /* set initial frequency */ | ||
1735 | si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */ | ||
1736 | |||
1737 | /* set led to connect state */ | ||
1738 | si470x_set_led_state(radio, BLINK_GREEN_LED); | ||
1739 | |||
1740 | /* rds buffer allocation */ | ||
1741 | radio->buf_size = rds_buf * 3; | ||
1742 | radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL); | ||
1743 | if (!radio->buffer) { | ||
1744 | retval = -EIO; | ||
1745 | goto err_video; | ||
1746 | } | ||
1747 | |||
1748 | /* rds buffer configuration */ | ||
1749 | radio->wr_index = 0; | ||
1750 | radio->rd_index = 0; | ||
1751 | init_waitqueue_head(&radio->read_queue); | ||
1752 | |||
1753 | /* register video device */ | ||
1754 | retval = video_register_device(radio->videodev, VFL_TYPE_RADIO, radio_nr); | ||
1755 | if (retval) { | ||
1756 | printk(KERN_WARNING DRIVER_NAME | ||
1757 | ": Could not register video device\n"); | ||
1758 | goto err_all; | ||
1759 | } | ||
1760 | usb_set_intfdata(intf, radio); | ||
1761 | |||
1762 | return 0; | ||
1763 | err_all: | ||
1764 | kfree(radio->buffer); | ||
1765 | err_video: | ||
1766 | video_device_release(radio->videodev); | ||
1767 | err_intbuffer: | ||
1768 | kfree(radio->int_in_buffer); | ||
1769 | err_radio: | ||
1770 | kfree(radio); | ||
1771 | err_initial: | ||
1772 | return retval; | ||
1773 | } | ||
1774 | |||
1775 | |||
1776 | /* | ||
1777 | * si470x_usb_driver_suspend - suspend the device | ||
1778 | */ | ||
1779 | static int si470x_usb_driver_suspend(struct usb_interface *intf, | ||
1780 | pm_message_t message) | ||
1781 | { | ||
1782 | printk(KERN_INFO DRIVER_NAME ": suspending now...\n"); | ||
1783 | |||
1784 | return 0; | ||
1785 | } | ||
1786 | |||
1787 | |||
1788 | /* | ||
1789 | * si470x_usb_driver_resume - resume the device | ||
1790 | */ | ||
1791 | static int si470x_usb_driver_resume(struct usb_interface *intf) | ||
1792 | { | ||
1793 | printk(KERN_INFO DRIVER_NAME ": resuming now...\n"); | ||
1794 | |||
1795 | return 0; | ||
1796 | } | ||
1797 | |||
1798 | |||
1799 | /* | ||
1800 | * si470x_usb_driver_disconnect - disconnect the device | ||
1801 | */ | ||
1802 | static void si470x_usb_driver_disconnect(struct usb_interface *intf) | ||
1803 | { | ||
1804 | struct si470x_device *radio = usb_get_intfdata(intf); | ||
1805 | |||
1806 | mutex_lock(&radio->disconnect_lock); | ||
1807 | radio->disconnected = 1; | ||
1808 | usb_set_intfdata(intf, NULL); | ||
1809 | if (radio->users == 0) { | ||
1810 | /* set led to disconnect state */ | ||
1811 | si470x_set_led_state(radio, BLINK_ORANGE_LED); | ||
1812 | |||
1813 | /* Free data structures. */ | ||
1814 | usb_free_urb(radio->int_in_urb); | ||
1815 | |||
1816 | kfree(radio->int_in_buffer); | ||
1817 | video_unregister_device(radio->videodev); | ||
1818 | kfree(radio->buffer); | ||
1819 | kfree(radio); | ||
1820 | } | ||
1821 | mutex_unlock(&radio->disconnect_lock); | ||
1822 | } | ||
1823 | |||
1824 | |||
1825 | /* | ||
1826 | * si470x_usb_driver - usb driver interface | ||
1827 | */ | ||
1828 | static struct usb_driver si470x_usb_driver = { | ||
1829 | .name = DRIVER_NAME, | ||
1830 | .probe = si470x_usb_driver_probe, | ||
1831 | .disconnect = si470x_usb_driver_disconnect, | ||
1832 | .suspend = si470x_usb_driver_suspend, | ||
1833 | .resume = si470x_usb_driver_resume, | ||
1834 | .id_table = si470x_usb_driver_id_table, | ||
1835 | .supports_autosuspend = 1, | ||
1836 | }; | ||
1837 | |||
1838 | |||
1839 | |||
1840 | /************************************************************************** | ||
1841 | * Module Interface | ||
1842 | **************************************************************************/ | ||
1843 | |||
1844 | /* | ||
1845 | * si470x_module_init - module init | ||
1846 | */ | ||
1847 | static int __init si470x_module_init(void) | ||
1848 | { | ||
1849 | printk(KERN_INFO DRIVER_DESC ", Version " DRIVER_VERSION "\n"); | ||
1850 | return usb_register(&si470x_usb_driver); | ||
1851 | } | ||
1852 | |||
1853 | |||
1854 | /* | ||
1855 | * si470x_module_exit - module exit | ||
1856 | */ | ||
1857 | static void __exit si470x_module_exit(void) | ||
1858 | { | ||
1859 | usb_deregister(&si470x_usb_driver); | ||
1860 | } | ||
1861 | |||
1862 | |||
1863 | module_init(si470x_module_init); | ||
1864 | module_exit(si470x_module_exit); | ||
1865 | |||
1866 | MODULE_LICENSE("GPL"); | ||
1867 | MODULE_AUTHOR(DRIVER_AUTHOR); | ||
1868 | MODULE_DESCRIPTION(DRIVER_DESC); | ||
1869 | MODULE_VERSION(DRIVER_VERSION); | ||
diff --git a/drivers/media/radio/si470x/Kconfig b/drivers/media/radio/si470x/Kconfig new file mode 100644 index 000000000000..20d05c04fa27 --- /dev/null +++ b/drivers/media/radio/si470x/Kconfig | |||
@@ -0,0 +1,24 @@ | |||
1 | config USB_SI470X | ||
2 | tristate "Silicon Labs Si470x FM Radio Receiver support with USB" | ||
3 | depends on USB && RADIO_SI470X | ||
4 | ---help--- | ||
5 | This is a driver for USB devices with the Silicon Labs SI470x | ||
6 | chip. Currently these devices are known to work: | ||
7 | - 10c4:818a: Silicon Labs USB FM Radio Reference Design | ||
8 | - 06e1:a155: ADS/Tech FM Radio Receiver (formerly Instant FM Music) | ||
9 | - 1b80:d700: KWorld USB FM Radio SnapMusic Mobile 700 (FM700) | ||
10 | - 10c5:819a: Sanei Electric FM USB Radio (aka DealExtreme.com PCear) | ||
11 | |||
12 | Sound is provided by the ALSA USB Audio/MIDI driver. Therefore | ||
13 | if you don't want to use the device solely for RDS receiving, | ||
14 | it is recommended to also select SND_USB_AUDIO. | ||
15 | |||
16 | Please have a look at the documentation, especially on how | ||
17 | to redirect the audio stream from the radio to your sound device: | ||
18 | Documentation/video4linux/si470x.txt | ||
19 | |||
20 | Say Y here if you want to connect this type of radio to your | ||
21 | computer's USB port. | ||
22 | |||
23 | To compile this driver as a module, choose M here: the | ||
24 | module will be called radio-usb-si470x. | ||
diff --git a/drivers/media/radio/si470x/Makefile b/drivers/media/radio/si470x/Makefile new file mode 100644 index 000000000000..3cb777fe36bf --- /dev/null +++ b/drivers/media/radio/si470x/Makefile | |||
@@ -0,0 +1,7 @@ | |||
1 | # | ||
2 | # Makefile for radios with Silicon Labs Si470x FM Radio Receivers | ||
3 | # | ||
4 | |||
5 | radio-usb-si470x-objs := radio-si470x-usb.o radio-si470x-common.o | ||
6 | |||
7 | obj-$(CONFIG_USB_SI470X) += radio-usb-si470x.o | ||
diff --git a/drivers/media/radio/si470x/radio-si470x-common.c b/drivers/media/radio/si470x/radio-si470x-common.c new file mode 100644 index 000000000000..b64559d561e5 --- /dev/null +++ b/drivers/media/radio/si470x/radio-si470x-common.c | |||
@@ -0,0 +1,794 @@ | |||
1 | /* | ||
2 | * drivers/media/radio/si470x/radio-si470x-common.c | ||
3 | * | ||
4 | * Driver for radios with Silicon Labs Si470x FM Radio Receivers | ||
5 | * | ||
6 | * Copyright (c) 2009 Tobias Lorenz <tobias.lorenz@gmx.net> | ||
7 | * | ||
8 | * This program is free software; you can redistribute it and/or modify | ||
9 | * it under the terms of the GNU General Public License as published by | ||
10 | * the Free Software Foundation; either version 2 of the License, or | ||
11 | * (at your option) any later version. | ||
12 | * | ||
13 | * This program is distributed in the hope that it will be useful, | ||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
16 | * GNU General Public License for more details. | ||
17 | * | ||
18 | * You should have received a copy of the GNU General Public License | ||
19 | * along with this program; if not, write to the Free Software | ||
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
21 | */ | ||
22 | |||
23 | |||
24 | /* | ||
25 | * History: | ||
26 | * 2008-01-12 Tobias Lorenz <tobias.lorenz@gmx.net> | ||
27 | * Version 1.0.0 | ||
28 | * - First working version | ||
29 | * 2008-01-13 Tobias Lorenz <tobias.lorenz@gmx.net> | ||
30 | * Version 1.0.1 | ||
31 | * - Improved error handling, every function now returns errno | ||
32 | * - Improved multi user access (start/mute/stop) | ||
33 | * - Channel doesn't get lost anymore after start/mute/stop | ||
34 | * - RDS support added (polling mode via interrupt EP 1) | ||
35 | * - marked default module parameters with *value* | ||
36 | * - switched from bit structs to bit masks | ||
37 | * - header file cleaned and integrated | ||
38 | * 2008-01-14 Tobias Lorenz <tobias.lorenz@gmx.net> | ||
39 | * Version 1.0.2 | ||
40 | * - hex values are now lower case | ||
41 | * - commented USB ID for ADS/Tech moved on todo list | ||
42 | * - blacklisted si470x in hid-quirks.c | ||
43 | * - rds buffer handling functions integrated into *_work, *_read | ||
44 | * - rds_command in si470x_poll exchanged against simple retval | ||
45 | * - check for firmware version 15 | ||
46 | * - code order and prototypes still remain the same | ||
47 | * - spacing and bottom of band codes remain the same | ||
48 | * 2008-01-16 Tobias Lorenz <tobias.lorenz@gmx.net> | ||
49 | * Version 1.0.3 | ||
50 | * - code reordered to avoid function prototypes | ||
51 | * - switch/case defaults are now more user-friendly | ||
52 | * - unified comment style | ||
53 | * - applied all checkpatch.pl v1.12 suggestions | ||
54 | * except the warning about the too long lines with bit comments | ||
55 | * - renamed FMRADIO to RADIO to cut line length (checkpatch.pl) | ||
56 | * 2008-01-22 Tobias Lorenz <tobias.lorenz@gmx.net> | ||
57 | * Version 1.0.4 | ||
58 | * - avoid poss. locking when doing copy_to_user which may sleep | ||
59 | * - RDS is automatically activated on read now | ||
60 | * - code cleaned of unnecessary rds_commands | ||
61 | * - USB Vendor/Product ID for ADS/Tech FM Radio Receiver verified | ||
62 | * (thanks to Guillaume RAMOUSSE) | ||
63 | * 2008-01-27 Tobias Lorenz <tobias.lorenz@gmx.net> | ||
64 | * Version 1.0.5 | ||
65 | * - number of seek_retries changed to tune_timeout | ||
66 | * - fixed problem with incomplete tune operations by own buffers | ||
67 | * - optimization of variables and printf types | ||
68 | * - improved error logging | ||
69 | * 2008-01-31 Tobias Lorenz <tobias.lorenz@gmx.net> | ||
70 | * Oliver Neukum <oliver@neukum.org> | ||
71 | * Version 1.0.6 | ||
72 | * - fixed coverity checker warnings in *_usb_driver_disconnect | ||
73 | * - probe()/open() race by correct ordering in probe() | ||
74 | * - DMA coherency rules by separate allocation of all buffers | ||
75 | * - use of endianness macros | ||
76 | * - abuse of spinlock, replaced by mutex | ||
77 | * - racy handling of timer in disconnect, | ||
78 | * replaced by delayed_work | ||
79 | * - racy interruptible_sleep_on(), | ||
80 | * replaced with wait_event_interruptible() | ||
81 | * - handle signals in read() | ||
82 | * 2008-02-08 Tobias Lorenz <tobias.lorenz@gmx.net> | ||
83 | * Oliver Neukum <oliver@neukum.org> | ||
84 | * Version 1.0.7 | ||
85 | * - usb autosuspend support | ||
86 | * - unplugging fixed | ||
87 | * 2008-05-07 Tobias Lorenz <tobias.lorenz@gmx.net> | ||
88 | * Version 1.0.8 | ||
89 | * - hardware frequency seek support | ||
90 | * - afc indication | ||
91 | * - more safety checks, let si470x_get_freq return errno | ||
92 | * - vidioc behavior corrected according to v4l2 spec | ||
93 | * 2008-10-20 Alexey Klimov <klimov.linux@gmail.com> | ||
94 | * - add support for KWorld USB FM Radio FM700 | ||
95 | * - blacklisted KWorld radio in hid-core.c and hid-ids.h | ||
96 | * 2008-12-03 Mark Lord <mlord@pobox.com> | ||
97 | * - add support for DealExtreme USB Radio | ||
98 | * 2009-01-31 Bob Ross <pigiron@gmx.com> | ||
99 | * - correction of stereo detection/setting | ||
100 | * - correction of signal strength indicator scaling | ||
101 | * 2009-01-31 Rick Bronson <rick@efn.org> | ||
102 | * Tobias Lorenz <tobias.lorenz@gmx.net> | ||
103 | * - add LED status output | ||
104 | * - get HW/SW version from scratchpad | ||
105 | * 2009-06-16 Edouard Lafargue <edouard@lafargue.name> | ||
106 | * Version 1.0.10 | ||
107 | * - add support for interrupt mode for RDS endpoint, | ||
108 | * instead of polling. | ||
109 | * Improves RDS reception significantly | ||
110 | */ | ||
111 | |||
112 | |||
113 | /* kernel includes */ | ||
114 | #include "radio-si470x.h" | ||
115 | |||
116 | |||
117 | |||
118 | /************************************************************************** | ||
119 | * Module Parameters | ||
120 | **************************************************************************/ | ||
121 | |||
122 | /* Spacing (kHz) */ | ||
123 | /* 0: 200 kHz (USA, Australia) */ | ||
124 | /* 1: 100 kHz (Europe, Japan) */ | ||
125 | /* 2: 50 kHz */ | ||
126 | static unsigned short space = 2; | ||
127 | module_param(space, ushort, 0444); | ||
128 | MODULE_PARM_DESC(space, "Spacing: 0=200kHz 1=100kHz *2=50kHz*"); | ||
129 | |||
130 | /* Bottom of Band (MHz) */ | ||
131 | /* 0: 87.5 - 108 MHz (USA, Europe)*/ | ||
132 | /* 1: 76 - 108 MHz (Japan wide band) */ | ||
133 | /* 2: 76 - 90 MHz (Japan) */ | ||
134 | static unsigned short band = 1; | ||
135 | module_param(band, ushort, 0444); | ||
136 | MODULE_PARM_DESC(band, "Band: 0=87.5..108MHz *1=76..108MHz* 2=76..90MHz"); | ||
137 | |||
138 | /* De-emphasis */ | ||
139 | /* 0: 75 us (USA) */ | ||
140 | /* 1: 50 us (Europe, Australia, Japan) */ | ||
141 | static unsigned short de = 1; | ||
142 | module_param(de, ushort, 0444); | ||
143 | MODULE_PARM_DESC(de, "De-emphasis: 0=75us *1=50us*"); | ||
144 | |||
145 | /* Tune timeout */ | ||
146 | static unsigned int tune_timeout = 3000; | ||
147 | module_param(tune_timeout, uint, 0644); | ||
148 | MODULE_PARM_DESC(tune_timeout, "Tune timeout: *3000*"); | ||
149 | |||
150 | /* Seek timeout */ | ||
151 | static unsigned int seek_timeout = 5000; | ||
152 | module_param(seek_timeout, uint, 0644); | ||
153 | MODULE_PARM_DESC(seek_timeout, "Seek timeout: *5000*"); | ||
154 | |||
155 | |||
156 | |||
157 | /************************************************************************** | ||
158 | * Generic Functions | ||
159 | **************************************************************************/ | ||
160 | |||
161 | /* | ||
162 | * si470x_set_chan - set the channel | ||
163 | */ | ||
164 | static int si470x_set_chan(struct si470x_device *radio, unsigned short chan) | ||
165 | { | ||
166 | int retval; | ||
167 | unsigned long timeout; | ||
168 | bool timed_out = 0; | ||
169 | |||
170 | /* start tuning */ | ||
171 | radio->registers[CHANNEL] &= ~CHANNEL_CHAN; | ||
172 | radio->registers[CHANNEL] |= CHANNEL_TUNE | chan; | ||
173 | retval = si470x_set_register(radio, CHANNEL); | ||
174 | if (retval < 0) | ||
175 | goto done; | ||
176 | |||
177 | /* wait till tune operation has completed */ | ||
178 | timeout = jiffies + msecs_to_jiffies(tune_timeout); | ||
179 | do { | ||
180 | retval = si470x_get_register(radio, STATUSRSSI); | ||
181 | if (retval < 0) | ||
182 | goto stop; | ||
183 | timed_out = time_after(jiffies, timeout); | ||
184 | } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) && | ||
185 | (!timed_out)); | ||
186 | if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) | ||
187 | printk(KERN_WARNING DRIVER_NAME ": tune does not complete\n"); | ||
188 | if (timed_out) | ||
189 | printk(KERN_WARNING DRIVER_NAME | ||
190 | ": tune timed out after %u ms\n", tune_timeout); | ||
191 | |||
192 | stop: | ||
193 | /* stop tuning */ | ||
194 | radio->registers[CHANNEL] &= ~CHANNEL_TUNE; | ||
195 | retval = si470x_set_register(radio, CHANNEL); | ||
196 | |||
197 | done: | ||
198 | return retval; | ||
199 | } | ||
200 | |||
201 | |||
202 | /* | ||
203 | * si470x_get_freq - get the frequency | ||
204 | */ | ||
205 | static int si470x_get_freq(struct si470x_device *radio, unsigned int *freq) | ||
206 | { | ||
207 | unsigned int spacing, band_bottom; | ||
208 | unsigned short chan; | ||
209 | int retval; | ||
210 | |||
211 | /* Spacing (kHz) */ | ||
212 | switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) { | ||
213 | /* 0: 200 kHz (USA, Australia) */ | ||
214 | case 0: | ||
215 | spacing = 0.200 * FREQ_MUL; break; | ||
216 | /* 1: 100 kHz (Europe, Japan) */ | ||
217 | case 1: | ||
218 | spacing = 0.100 * FREQ_MUL; break; | ||
219 | /* 2: 50 kHz */ | ||
220 | default: | ||
221 | spacing = 0.050 * FREQ_MUL; break; | ||
222 | }; | ||
223 | |||
224 | /* Bottom of Band (MHz) */ | ||
225 | switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) { | ||
226 | /* 0: 87.5 - 108 MHz (USA, Europe) */ | ||
227 | case 0: | ||
228 | band_bottom = 87.5 * FREQ_MUL; break; | ||
229 | /* 1: 76 - 108 MHz (Japan wide band) */ | ||
230 | default: | ||
231 | band_bottom = 76 * FREQ_MUL; break; | ||
232 | /* 2: 76 - 90 MHz (Japan) */ | ||
233 | case 2: | ||
234 | band_bottom = 76 * FREQ_MUL; break; | ||
235 | }; | ||
236 | |||
237 | /* read channel */ | ||
238 | retval = si470x_get_register(radio, READCHAN); | ||
239 | chan = radio->registers[READCHAN] & READCHAN_READCHAN; | ||
240 | |||
241 | /* Frequency (MHz) = Spacing (kHz) x Channel + Bottom of Band (MHz) */ | ||
242 | *freq = chan * spacing + band_bottom; | ||
243 | |||
244 | return retval; | ||
245 | } | ||
246 | |||
247 | |||
248 | /* | ||
249 | * si470x_set_freq - set the frequency | ||
250 | */ | ||
251 | int si470x_set_freq(struct si470x_device *radio, unsigned int freq) | ||
252 | { | ||
253 | unsigned int spacing, band_bottom; | ||
254 | unsigned short chan; | ||
255 | |||
256 | /* Spacing (kHz) */ | ||
257 | switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) { | ||
258 | /* 0: 200 kHz (USA, Australia) */ | ||
259 | case 0: | ||
260 | spacing = 0.200 * FREQ_MUL; break; | ||
261 | /* 1: 100 kHz (Europe, Japan) */ | ||
262 | case 1: | ||
263 | spacing = 0.100 * FREQ_MUL; break; | ||
264 | /* 2: 50 kHz */ | ||
265 | default: | ||
266 | spacing = 0.050 * FREQ_MUL; break; | ||
267 | }; | ||
268 | |||
269 | /* Bottom of Band (MHz) */ | ||
270 | switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) { | ||
271 | /* 0: 87.5 - 108 MHz (USA, Europe) */ | ||
272 | case 0: | ||
273 | band_bottom = 87.5 * FREQ_MUL; break; | ||
274 | /* 1: 76 - 108 MHz (Japan wide band) */ | ||
275 | default: | ||
276 | band_bottom = 76 * FREQ_MUL; break; | ||
277 | /* 2: 76 - 90 MHz (Japan) */ | ||
278 | case 2: | ||
279 | band_bottom = 76 * FREQ_MUL; break; | ||
280 | }; | ||
281 | |||
282 | /* Chan = [ Freq (Mhz) - Bottom of Band (MHz) ] / Spacing (kHz) */ | ||
283 | chan = (freq - band_bottom) / spacing; | ||
284 | |||
285 | return si470x_set_chan(radio, chan); | ||
286 | } | ||
287 | |||
288 | |||
289 | /* | ||
290 | * si470x_set_seek - set seek | ||
291 | */ | ||
292 | static int si470x_set_seek(struct si470x_device *radio, | ||
293 | unsigned int wrap_around, unsigned int seek_upward) | ||
294 | { | ||
295 | int retval = 0; | ||
296 | unsigned long timeout; | ||
297 | bool timed_out = 0; | ||
298 | |||
299 | /* start seeking */ | ||
300 | radio->registers[POWERCFG] |= POWERCFG_SEEK; | ||
301 | if (wrap_around == 1) | ||
302 | radio->registers[POWERCFG] &= ~POWERCFG_SKMODE; | ||
303 | else | ||
304 | radio->registers[POWERCFG] |= POWERCFG_SKMODE; | ||
305 | if (seek_upward == 1) | ||
306 | radio->registers[POWERCFG] |= POWERCFG_SEEKUP; | ||
307 | else | ||
308 | radio->registers[POWERCFG] &= ~POWERCFG_SEEKUP; | ||
309 | retval = si470x_set_register(radio, POWERCFG); | ||
310 | if (retval < 0) | ||
311 | goto done; | ||
312 | |||
313 | /* wait till seek operation has completed */ | ||
314 | timeout = jiffies + msecs_to_jiffies(seek_timeout); | ||
315 | do { | ||
316 | retval = si470x_get_register(radio, STATUSRSSI); | ||
317 | if (retval < 0) | ||
318 | goto stop; | ||
319 | timed_out = time_after(jiffies, timeout); | ||
320 | } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) && | ||
321 | (!timed_out)); | ||
322 | if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) | ||
323 | printk(KERN_WARNING DRIVER_NAME ": seek does not complete\n"); | ||
324 | if (radio->registers[STATUSRSSI] & STATUSRSSI_SF) | ||
325 | printk(KERN_WARNING DRIVER_NAME | ||
326 | ": seek failed / band limit reached\n"); | ||
327 | if (timed_out) | ||
328 | printk(KERN_WARNING DRIVER_NAME | ||
329 | ": seek timed out after %u ms\n", seek_timeout); | ||
330 | |||
331 | stop: | ||
332 | /* stop seeking */ | ||
333 | radio->registers[POWERCFG] &= ~POWERCFG_SEEK; | ||
334 | retval = si470x_set_register(radio, POWERCFG); | ||
335 | |||
336 | done: | ||
337 | /* try again, if timed out */ | ||
338 | if ((retval == 0) && timed_out) | ||
339 | retval = -EAGAIN; | ||
340 | |||
341 | return retval; | ||
342 | } | ||
343 | |||
344 | |||
345 | /* | ||
346 | * si470x_start - switch on radio | ||
347 | */ | ||
348 | int si470x_start(struct si470x_device *radio) | ||
349 | { | ||
350 | int retval; | ||
351 | |||
352 | /* powercfg */ | ||
353 | radio->registers[POWERCFG] = | ||
354 | POWERCFG_DMUTE | POWERCFG_ENABLE | POWERCFG_RDSM; | ||
355 | retval = si470x_set_register(radio, POWERCFG); | ||
356 | if (retval < 0) | ||
357 | goto done; | ||
358 | |||
359 | /* sysconfig 1 */ | ||
360 | radio->registers[SYSCONFIG1] = SYSCONFIG1_DE; | ||
361 | retval = si470x_set_register(radio, SYSCONFIG1); | ||
362 | if (retval < 0) | ||
363 | goto done; | ||
364 | |||
365 | /* sysconfig 2 */ | ||
366 | radio->registers[SYSCONFIG2] = | ||
367 | (0x3f << 8) | /* SEEKTH */ | ||
368 | ((band << 6) & SYSCONFIG2_BAND) | /* BAND */ | ||
369 | ((space << 4) & SYSCONFIG2_SPACE) | /* SPACE */ | ||
370 | 15; /* VOLUME (max) */ | ||
371 | retval = si470x_set_register(radio, SYSCONFIG2); | ||
372 | if (retval < 0) | ||
373 | goto done; | ||
374 | |||
375 | /* reset last channel */ | ||
376 | retval = si470x_set_chan(radio, | ||
377 | radio->registers[CHANNEL] & CHANNEL_CHAN); | ||
378 | |||
379 | done: | ||
380 | return retval; | ||
381 | } | ||
382 | |||
383 | |||
384 | /* | ||
385 | * si470x_stop - switch off radio | ||
386 | */ | ||
387 | int si470x_stop(struct si470x_device *radio) | ||
388 | { | ||
389 | int retval; | ||
390 | |||
391 | /* sysconfig 1 */ | ||
392 | radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS; | ||
393 | retval = si470x_set_register(radio, SYSCONFIG1); | ||
394 | if (retval < 0) | ||
395 | goto done; | ||
396 | |||
397 | /* powercfg */ | ||
398 | radio->registers[POWERCFG] &= ~POWERCFG_DMUTE; | ||
399 | /* POWERCFG_ENABLE has to automatically go low */ | ||
400 | radio->registers[POWERCFG] |= POWERCFG_ENABLE | POWERCFG_DISABLE; | ||
401 | retval = si470x_set_register(radio, POWERCFG); | ||
402 | |||
403 | done: | ||
404 | return retval; | ||
405 | } | ||
406 | |||
407 | |||
408 | /* | ||
409 | * si470x_rds_on - switch on rds reception | ||
410 | */ | ||
411 | int si470x_rds_on(struct si470x_device *radio) | ||
412 | { | ||
413 | int retval; | ||
414 | |||
415 | /* sysconfig 1 */ | ||
416 | mutex_lock(&radio->lock); | ||
417 | radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDS; | ||
418 | retval = si470x_set_register(radio, SYSCONFIG1); | ||
419 | if (retval < 0) | ||
420 | radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS; | ||
421 | mutex_unlock(&radio->lock); | ||
422 | |||
423 | return retval; | ||
424 | } | ||
425 | |||
426 | |||
427 | |||
428 | /************************************************************************** | ||
429 | * Video4Linux Interface | ||
430 | **************************************************************************/ | ||
431 | |||
432 | /* | ||
433 | * si470x_vidioc_queryctrl - enumerate control items | ||
434 | */ | ||
435 | static int si470x_vidioc_queryctrl(struct file *file, void *priv, | ||
436 | struct v4l2_queryctrl *qc) | ||
437 | { | ||
438 | int retval = -EINVAL; | ||
439 | |||
440 | /* abort if qc->id is below V4L2_CID_BASE */ | ||
441 | if (qc->id < V4L2_CID_BASE) | ||
442 | goto done; | ||
443 | |||
444 | /* search video control */ | ||
445 | switch (qc->id) { | ||
446 | case V4L2_CID_AUDIO_VOLUME: | ||
447 | return v4l2_ctrl_query_fill(qc, 0, 15, 1, 15); | ||
448 | case V4L2_CID_AUDIO_MUTE: | ||
449 | return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1); | ||
450 | } | ||
451 | |||
452 | /* disable unsupported base controls */ | ||
453 | /* to satisfy kradio and such apps */ | ||
454 | if ((retval == -EINVAL) && (qc->id < V4L2_CID_LASTP1)) { | ||
455 | qc->flags = V4L2_CTRL_FLAG_DISABLED; | ||
456 | retval = 0; | ||
457 | } | ||
458 | |||
459 | done: | ||
460 | if (retval < 0) | ||
461 | printk(KERN_WARNING DRIVER_NAME | ||
462 | ": query controls failed with %d\n", retval); | ||
463 | return retval; | ||
464 | } | ||
465 | |||
466 | |||
467 | /* | ||
468 | * si470x_vidioc_g_ctrl - get the value of a control | ||
469 | */ | ||
470 | static int si470x_vidioc_g_ctrl(struct file *file, void *priv, | ||
471 | struct v4l2_control *ctrl) | ||
472 | { | ||
473 | struct si470x_device *radio = video_drvdata(file); | ||
474 | int retval = 0; | ||
475 | |||
476 | /* safety checks */ | ||
477 | if (radio->disconnected) { | ||
478 | retval = -EIO; | ||
479 | goto done; | ||
480 | } | ||
481 | |||
482 | switch (ctrl->id) { | ||
483 | case V4L2_CID_AUDIO_VOLUME: | ||
484 | ctrl->value = radio->registers[SYSCONFIG2] & | ||
485 | SYSCONFIG2_VOLUME; | ||
486 | break; | ||
487 | case V4L2_CID_AUDIO_MUTE: | ||
488 | ctrl->value = ((radio->registers[POWERCFG] & | ||
489 | POWERCFG_DMUTE) == 0) ? 1 : 0; | ||
490 | break; | ||
491 | default: | ||
492 | retval = -EINVAL; | ||
493 | } | ||
494 | |||
495 | done: | ||
496 | if (retval < 0) | ||
497 | printk(KERN_WARNING DRIVER_NAME | ||
498 | ": get control failed with %d\n", retval); | ||
499 | return retval; | ||
500 | } | ||
501 | |||
502 | |||
503 | /* | ||
504 | * si470x_vidioc_s_ctrl - set the value of a control | ||
505 | */ | ||
506 | static int si470x_vidioc_s_ctrl(struct file *file, void *priv, | ||
507 | struct v4l2_control *ctrl) | ||
508 | { | ||
509 | struct si470x_device *radio = video_drvdata(file); | ||
510 | int retval = 0; | ||
511 | |||
512 | /* safety checks */ | ||
513 | if (radio->disconnected) { | ||
514 | retval = -EIO; | ||
515 | goto done; | ||
516 | } | ||
517 | |||
518 | switch (ctrl->id) { | ||
519 | case V4L2_CID_AUDIO_VOLUME: | ||
520 | radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_VOLUME; | ||
521 | radio->registers[SYSCONFIG2] |= ctrl->value; | ||
522 | retval = si470x_set_register(radio, SYSCONFIG2); | ||
523 | break; | ||
524 | case V4L2_CID_AUDIO_MUTE: | ||
525 | if (ctrl->value == 1) | ||
526 | radio->registers[POWERCFG] &= ~POWERCFG_DMUTE; | ||
527 | else | ||
528 | radio->registers[POWERCFG] |= POWERCFG_DMUTE; | ||
529 | retval = si470x_set_register(radio, POWERCFG); | ||
530 | break; | ||
531 | default: | ||
532 | retval = -EINVAL; | ||
533 | } | ||
534 | |||
535 | done: | ||
536 | if (retval < 0) | ||
537 | printk(KERN_WARNING DRIVER_NAME | ||
538 | ": set control failed with %d\n", retval); | ||
539 | return retval; | ||
540 | } | ||
541 | |||
542 | |||
543 | /* | ||
544 | * si470x_vidioc_g_audio - get audio attributes | ||
545 | */ | ||
546 | static int si470x_vidioc_g_audio(struct file *file, void *priv, | ||
547 | struct v4l2_audio *audio) | ||
548 | { | ||
549 | /* driver constants */ | ||
550 | audio->index = 0; | ||
551 | strcpy(audio->name, "Radio"); | ||
552 | audio->capability = V4L2_AUDCAP_STEREO; | ||
553 | audio->mode = 0; | ||
554 | |||
555 | return 0; | ||
556 | } | ||
557 | |||
558 | |||
559 | /* | ||
560 | * si470x_vidioc_g_tuner - get tuner attributes | ||
561 | */ | ||
562 | static int si470x_vidioc_g_tuner(struct file *file, void *priv, | ||
563 | struct v4l2_tuner *tuner) | ||
564 | { | ||
565 | struct si470x_device *radio = video_drvdata(file); | ||
566 | int retval = 0; | ||
567 | |||
568 | /* safety checks */ | ||
569 | if (radio->disconnected) { | ||
570 | retval = -EIO; | ||
571 | goto done; | ||
572 | } | ||
573 | if (tuner->index != 0) { | ||
574 | retval = -EINVAL; | ||
575 | goto done; | ||
576 | } | ||
577 | |||
578 | retval = si470x_get_register(radio, STATUSRSSI); | ||
579 | if (retval < 0) | ||
580 | goto done; | ||
581 | |||
582 | /* driver constants */ | ||
583 | strcpy(tuner->name, "FM"); | ||
584 | tuner->type = V4L2_TUNER_RADIO; | ||
585 | tuner->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO | | ||
586 | V4L2_TUNER_CAP_RDS; | ||
587 | |||
588 | /* range limits */ | ||
589 | switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) { | ||
590 | /* 0: 87.5 - 108 MHz (USA, Europe, default) */ | ||
591 | default: | ||
592 | tuner->rangelow = 87.5 * FREQ_MUL; | ||
593 | tuner->rangehigh = 108 * FREQ_MUL; | ||
594 | break; | ||
595 | /* 1: 76 - 108 MHz (Japan wide band) */ | ||
596 | case 1: | ||
597 | tuner->rangelow = 76 * FREQ_MUL; | ||
598 | tuner->rangehigh = 108 * FREQ_MUL; | ||
599 | break; | ||
600 | /* 2: 76 - 90 MHz (Japan) */ | ||
601 | case 2: | ||
602 | tuner->rangelow = 76 * FREQ_MUL; | ||
603 | tuner->rangehigh = 90 * FREQ_MUL; | ||
604 | break; | ||
605 | }; | ||
606 | |||
607 | /* stereo indicator == stereo (instead of mono) */ | ||
608 | if ((radio->registers[STATUSRSSI] & STATUSRSSI_ST) == 0) | ||
609 | tuner->rxsubchans = V4L2_TUNER_SUB_MONO; | ||
610 | else | ||
611 | tuner->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO; | ||
612 | /* If there is a reliable method of detecting an RDS channel, | ||
613 | then this code should check for that before setting this | ||
614 | RDS subchannel. */ | ||
615 | tuner->rxsubchans |= V4L2_TUNER_SUB_RDS; | ||
616 | |||
617 | /* mono/stereo selector */ | ||
618 | if ((radio->registers[POWERCFG] & POWERCFG_MONO) == 0) | ||
619 | tuner->audmode = V4L2_TUNER_MODE_STEREO; | ||
620 | else | ||
621 | tuner->audmode = V4L2_TUNER_MODE_MONO; | ||
622 | |||
623 | /* min is worst, max is best; signal:0..0xffff; rssi: 0..0xff */ | ||
624 | /* measured in units of db쨉V in 1 db increments (max at ~75 db쨉V) */ | ||
625 | tuner->signal = (radio->registers[STATUSRSSI] & STATUSRSSI_RSSI); | ||
626 | /* the ideal factor is 0xffff/75 = 873,8 */ | ||
627 | tuner->signal = (tuner->signal * 873) + (8 * tuner->signal / 10); | ||
628 | |||
629 | /* automatic frequency control: -1: freq to low, 1 freq to high */ | ||
630 | /* AFCRL does only indicate that freq. differs, not if too low/high */ | ||
631 | tuner->afc = (radio->registers[STATUSRSSI] & STATUSRSSI_AFCRL) ? 1 : 0; | ||
632 | |||
633 | done: | ||
634 | if (retval < 0) | ||
635 | printk(KERN_WARNING DRIVER_NAME | ||
636 | ": get tuner failed with %d\n", retval); | ||
637 | return retval; | ||
638 | } | ||
639 | |||
640 | |||
641 | /* | ||
642 | * si470x_vidioc_s_tuner - set tuner attributes | ||
643 | */ | ||
644 | static int si470x_vidioc_s_tuner(struct file *file, void *priv, | ||
645 | struct v4l2_tuner *tuner) | ||
646 | { | ||
647 | struct si470x_device *radio = video_drvdata(file); | ||
648 | int retval = -EINVAL; | ||
649 | |||
650 | /* safety checks */ | ||
651 | if (radio->disconnected) { | ||
652 | retval = -EIO; | ||
653 | goto done; | ||
654 | } | ||
655 | if (tuner->index != 0) | ||
656 | goto done; | ||
657 | |||
658 | /* mono/stereo selector */ | ||
659 | switch (tuner->audmode) { | ||
660 | case V4L2_TUNER_MODE_MONO: | ||
661 | radio->registers[POWERCFG] |= POWERCFG_MONO; /* force mono */ | ||
662 | break; | ||
663 | case V4L2_TUNER_MODE_STEREO: | ||
664 | radio->registers[POWERCFG] &= ~POWERCFG_MONO; /* try stereo */ | ||
665 | break; | ||
666 | default: | ||
667 | goto done; | ||
668 | } | ||
669 | |||
670 | retval = si470x_set_register(radio, POWERCFG); | ||
671 | |||
672 | done: | ||
673 | if (retval < 0) | ||
674 | printk(KERN_WARNING DRIVER_NAME | ||
675 | ": set tuner failed with %d\n", retval); | ||
676 | return retval; | ||
677 | } | ||
678 | |||
679 | |||
680 | /* | ||
681 | * si470x_vidioc_g_frequency - get tuner or modulator radio frequency | ||
682 | */ | ||
683 | static int si470x_vidioc_g_frequency(struct file *file, void *priv, | ||
684 | struct v4l2_frequency *freq) | ||
685 | { | ||
686 | struct si470x_device *radio = video_drvdata(file); | ||
687 | int retval = 0; | ||
688 | |||
689 | /* safety checks */ | ||
690 | if (radio->disconnected) { | ||
691 | retval = -EIO; | ||
692 | goto done; | ||
693 | } | ||
694 | if (freq->tuner != 0) { | ||
695 | retval = -EINVAL; | ||
696 | goto done; | ||
697 | } | ||
698 | |||
699 | freq->type = V4L2_TUNER_RADIO; | ||
700 | retval = si470x_get_freq(radio, &freq->frequency); | ||
701 | |||
702 | done: | ||
703 | if (retval < 0) | ||
704 | printk(KERN_WARNING DRIVER_NAME | ||
705 | ": get frequency failed with %d\n", retval); | ||
706 | return retval; | ||
707 | } | ||
708 | |||
709 | |||
710 | /* | ||
711 | * si470x_vidioc_s_frequency - set tuner or modulator radio frequency | ||
712 | */ | ||
713 | static int si470x_vidioc_s_frequency(struct file *file, void *priv, | ||
714 | struct v4l2_frequency *freq) | ||
715 | { | ||
716 | struct si470x_device *radio = video_drvdata(file); | ||
717 | int retval = 0; | ||
718 | |||
719 | /* safety checks */ | ||
720 | if (radio->disconnected) { | ||
721 | retval = -EIO; | ||
722 | goto done; | ||
723 | } | ||
724 | if (freq->tuner != 0) { | ||
725 | retval = -EINVAL; | ||
726 | goto done; | ||
727 | } | ||
728 | |||
729 | retval = si470x_set_freq(radio, freq->frequency); | ||
730 | |||
731 | done: | ||
732 | if (retval < 0) | ||
733 | printk(KERN_WARNING DRIVER_NAME | ||
734 | ": set frequency failed with %d\n", retval); | ||
735 | return retval; | ||
736 | } | ||
737 | |||
738 | |||
739 | /* | ||
740 | * si470x_vidioc_s_hw_freq_seek - set hardware frequency seek | ||
741 | */ | ||
742 | static int si470x_vidioc_s_hw_freq_seek(struct file *file, void *priv, | ||
743 | struct v4l2_hw_freq_seek *seek) | ||
744 | { | ||
745 | struct si470x_device *radio = video_drvdata(file); | ||
746 | int retval = 0; | ||
747 | |||
748 | /* safety checks */ | ||
749 | if (radio->disconnected) { | ||
750 | retval = -EIO; | ||
751 | goto done; | ||
752 | } | ||
753 | if (seek->tuner != 0) { | ||
754 | retval = -EINVAL; | ||
755 | goto done; | ||
756 | } | ||
757 | |||
758 | retval = si470x_set_seek(radio, seek->wrap_around, seek->seek_upward); | ||
759 | |||
760 | done: | ||
761 | if (retval < 0) | ||
762 | printk(KERN_WARNING DRIVER_NAME | ||
763 | ": set hardware frequency seek failed with %d\n", | ||
764 | retval); | ||
765 | return retval; | ||
766 | } | ||
767 | |||
768 | |||
769 | /* | ||
770 | * si470x_ioctl_ops - video device ioctl operations | ||
771 | */ | ||
772 | static const struct v4l2_ioctl_ops si470x_ioctl_ops = { | ||
773 | .vidioc_querycap = si470x_vidioc_querycap, | ||
774 | .vidioc_queryctrl = si470x_vidioc_queryctrl, | ||
775 | .vidioc_g_ctrl = si470x_vidioc_g_ctrl, | ||
776 | .vidioc_s_ctrl = si470x_vidioc_s_ctrl, | ||
777 | .vidioc_g_audio = si470x_vidioc_g_audio, | ||
778 | .vidioc_g_tuner = si470x_vidioc_g_tuner, | ||
779 | .vidioc_s_tuner = si470x_vidioc_s_tuner, | ||
780 | .vidioc_g_frequency = si470x_vidioc_g_frequency, | ||
781 | .vidioc_s_frequency = si470x_vidioc_s_frequency, | ||
782 | .vidioc_s_hw_freq_seek = si470x_vidioc_s_hw_freq_seek, | ||
783 | }; | ||
784 | |||
785 | |||
786 | /* | ||
787 | * si470x_viddev_template - video device interface | ||
788 | */ | ||
789 | struct video_device si470x_viddev_template = { | ||
790 | .fops = &si470x_fops, | ||
791 | .name = DRIVER_NAME, | ||
792 | .release = video_device_release, | ||
793 | .ioctl_ops = &si470x_ioctl_ops, | ||
794 | }; | ||
diff --git a/drivers/media/radio/si470x/radio-si470x-usb.c b/drivers/media/radio/si470x/radio-si470x-usb.c new file mode 100644 index 000000000000..f3d805f70996 --- /dev/null +++ b/drivers/media/radio/si470x/radio-si470x-usb.c | |||
@@ -0,0 +1,958 @@ | |||
1 | /* | ||
2 | * drivers/media/radio/si470x/radio-si470x-usb.c | ||
3 | * | ||
4 | * USB driver for radios with Silicon Labs Si470x FM Radio Receivers | ||
5 | * | ||
6 | * Copyright (c) 2009 Tobias Lorenz <tobias.lorenz@gmx.net> | ||
7 | * | ||
8 | * This program is free software; you can redistribute it and/or modify | ||
9 | * it under the terms of the GNU General Public License as published by | ||
10 | * the Free Software Foundation; either version 2 of the License, or | ||
11 | * (at your option) any later version. | ||
12 | * | ||
13 | * This program is distributed in the hope that it will be useful, | ||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
16 | * GNU General Public License for more details. | ||
17 | * | ||
18 | * You should have received a copy of the GNU General Public License | ||
19 | * along with this program; if not, write to the Free Software | ||
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
21 | */ | ||
22 | |||
23 | |||
24 | /* | ||
25 | * ToDo: | ||
26 | * - add firmware download/update support | ||
27 | */ | ||
28 | |||
29 | |||
30 | /* driver definitions */ | ||
31 | #define DRIVER_AUTHOR "Tobias Lorenz <tobias.lorenz@gmx.net>" | ||
32 | #define DRIVER_KERNEL_VERSION KERNEL_VERSION(1, 0, 10) | ||
33 | #define DRIVER_CARD "Silicon Labs Si470x FM Radio Receiver" | ||
34 | #define DRIVER_DESC "USB radio driver for Si470x FM Radio Receivers" | ||
35 | #define DRIVER_VERSION "1.0.10" | ||
36 | |||
37 | /* kernel includes */ | ||
38 | #include <linux/usb.h> | ||
39 | #include <linux/hid.h> | ||
40 | |||
41 | #include "radio-si470x.h" | ||
42 | |||
43 | |||
44 | /* USB Device ID List */ | ||
45 | static struct usb_device_id si470x_usb_driver_id_table[] = { | ||
46 | /* Silicon Labs USB FM Radio Reference Design */ | ||
47 | { USB_DEVICE_AND_INTERFACE_INFO(0x10c4, 0x818a, USB_CLASS_HID, 0, 0) }, | ||
48 | /* ADS/Tech FM Radio Receiver (formerly Instant FM Music) */ | ||
49 | { USB_DEVICE_AND_INTERFACE_INFO(0x06e1, 0xa155, USB_CLASS_HID, 0, 0) }, | ||
50 | /* KWorld USB FM Radio SnapMusic Mobile 700 (FM700) */ | ||
51 | { USB_DEVICE_AND_INTERFACE_INFO(0x1b80, 0xd700, USB_CLASS_HID, 0, 0) }, | ||
52 | /* Sanei Electric, Inc. FM USB Radio (sold as DealExtreme.com PCear) */ | ||
53 | { USB_DEVICE_AND_INTERFACE_INFO(0x10c5, 0x819a, USB_CLASS_HID, 0, 0) }, | ||
54 | /* Terminating entry */ | ||
55 | { } | ||
56 | }; | ||
57 | MODULE_DEVICE_TABLE(usb, si470x_usb_driver_id_table); | ||
58 | |||
59 | |||
60 | |||
61 | /************************************************************************** | ||
62 | * Module Parameters | ||
63 | **************************************************************************/ | ||
64 | |||
65 | /* Radio Nr */ | ||
66 | static int radio_nr = -1; | ||
67 | module_param(radio_nr, int, 0444); | ||
68 | MODULE_PARM_DESC(radio_nr, "Radio Nr"); | ||
69 | |||
70 | /* USB timeout */ | ||
71 | static unsigned int usb_timeout = 500; | ||
72 | module_param(usb_timeout, uint, 0644); | ||
73 | MODULE_PARM_DESC(usb_timeout, "USB timeout (ms): *500*"); | ||
74 | |||
75 | /* RDS buffer blocks */ | ||
76 | static unsigned int rds_buf = 100; | ||
77 | module_param(rds_buf, uint, 0444); | ||
78 | MODULE_PARM_DESC(rds_buf, "RDS buffer entries: *100*"); | ||
79 | |||
80 | /* RDS maximum block errors */ | ||
81 | static unsigned short max_rds_errors = 1; | ||
82 | /* 0 means 0 errors requiring correction */ | ||
83 | /* 1 means 1-2 errors requiring correction (used by original USBRadio.exe) */ | ||
84 | /* 2 means 3-5 errors requiring correction */ | ||
85 | /* 3 means 6+ errors or errors in checkword, correction not possible */ | ||
86 | module_param(max_rds_errors, ushort, 0644); | ||
87 | MODULE_PARM_DESC(max_rds_errors, "RDS maximum block errors: *1*"); | ||
88 | |||
89 | |||
90 | |||
91 | /************************************************************************** | ||
92 | * USB HID Reports | ||
93 | **************************************************************************/ | ||
94 | |||
95 | /* Reports 1-16 give direct read/write access to the 16 Si470x registers */ | ||
96 | /* with the (REPORT_ID - 1) corresponding to the register address across USB */ | ||
97 | /* endpoint 0 using GET_REPORT and SET_REPORT */ | ||
98 | #define REGISTER_REPORT_SIZE (RADIO_REGISTER_SIZE + 1) | ||
99 | #define REGISTER_REPORT(reg) ((reg) + 1) | ||
100 | |||
101 | /* Report 17 gives direct read/write access to the entire Si470x register */ | ||
102 | /* map across endpoint 0 using GET_REPORT and SET_REPORT */ | ||
103 | #define ENTIRE_REPORT_SIZE (RADIO_REGISTER_NUM * RADIO_REGISTER_SIZE + 1) | ||
104 | #define ENTIRE_REPORT 17 | ||
105 | |||
106 | /* Report 18 is used to send the lowest 6 Si470x registers up the HID */ | ||
107 | /* interrupt endpoint 1 to Windows every 20 milliseconds for status */ | ||
108 | #define RDS_REPORT_SIZE (RDS_REGISTER_NUM * RADIO_REGISTER_SIZE + 1) | ||
109 | #define RDS_REPORT 18 | ||
110 | |||
111 | /* Report 19: LED state */ | ||
112 | #define LED_REPORT_SIZE 3 | ||
113 | #define LED_REPORT 19 | ||
114 | |||
115 | /* Report 19: stream */ | ||
116 | #define STREAM_REPORT_SIZE 3 | ||
117 | #define STREAM_REPORT 19 | ||
118 | |||
119 | /* Report 20: scratch */ | ||
120 | #define SCRATCH_PAGE_SIZE 63 | ||
121 | #define SCRATCH_REPORT_SIZE (SCRATCH_PAGE_SIZE + 1) | ||
122 | #define SCRATCH_REPORT 20 | ||
123 | |||
124 | /* Reports 19-22: flash upgrade of the C8051F321 */ | ||
125 | #define WRITE_REPORT_SIZE 4 | ||
126 | #define WRITE_REPORT 19 | ||
127 | #define FLASH_REPORT_SIZE 64 | ||
128 | #define FLASH_REPORT 20 | ||
129 | #define CRC_REPORT_SIZE 3 | ||
130 | #define CRC_REPORT 21 | ||
131 | #define RESPONSE_REPORT_SIZE 2 | ||
132 | #define RESPONSE_REPORT 22 | ||
133 | |||
134 | /* Report 23: currently unused, but can accept 60 byte reports on the HID */ | ||
135 | /* interrupt out endpoint 2 every 1 millisecond */ | ||
136 | #define UNUSED_REPORT 23 | ||
137 | |||
138 | |||
139 | |||
140 | /************************************************************************** | ||
141 | * Software/Hardware Versions | ||
142 | **************************************************************************/ | ||
143 | #define RADIO_SW_VERSION_NOT_BOOTLOADABLE 6 | ||
144 | #define RADIO_SW_VERSION 7 | ||
145 | #define RADIO_SW_VERSION_CURRENT 15 | ||
146 | #define RADIO_HW_VERSION 1 | ||
147 | |||
148 | #define SCRATCH_PAGE_SW_VERSION 1 | ||
149 | #define SCRATCH_PAGE_HW_VERSION 2 | ||
150 | |||
151 | |||
152 | |||
153 | /************************************************************************** | ||
154 | * LED State Definitions | ||
155 | **************************************************************************/ | ||
156 | #define LED_COMMAND 0x35 | ||
157 | |||
158 | #define NO_CHANGE_LED 0x00 | ||
159 | #define ALL_COLOR_LED 0x01 /* streaming state */ | ||
160 | #define BLINK_GREEN_LED 0x02 /* connect state */ | ||
161 | #define BLINK_RED_LED 0x04 | ||
162 | #define BLINK_ORANGE_LED 0x10 /* disconnect state */ | ||
163 | #define SOLID_GREEN_LED 0x20 /* tuning/seeking state */ | ||
164 | #define SOLID_RED_LED 0x40 /* bootload state */ | ||
165 | #define SOLID_ORANGE_LED 0x80 | ||
166 | |||
167 | |||
168 | |||
169 | /************************************************************************** | ||
170 | * Stream State Definitions | ||
171 | **************************************************************************/ | ||
172 | #define STREAM_COMMAND 0x36 | ||
173 | #define STREAM_VIDPID 0x00 | ||
174 | #define STREAM_AUDIO 0xff | ||
175 | |||
176 | |||
177 | |||
178 | /************************************************************************** | ||
179 | * Bootloader / Flash Commands | ||
180 | **************************************************************************/ | ||
181 | |||
182 | /* unique id sent to bootloader and required to put into a bootload state */ | ||
183 | #define UNIQUE_BL_ID 0x34 | ||
184 | |||
185 | /* mask for the flash data */ | ||
186 | #define FLASH_DATA_MASK 0x55 | ||
187 | |||
188 | /* bootloader commands */ | ||
189 | #define GET_SW_VERSION_COMMAND 0x00 | ||
190 | #define SET_PAGE_COMMAND 0x01 | ||
191 | #define ERASE_PAGE_COMMAND 0x02 | ||
192 | #define WRITE_PAGE_COMMAND 0x03 | ||
193 | #define CRC_ON_PAGE_COMMAND 0x04 | ||
194 | #define READ_FLASH_BYTE_COMMAND 0x05 | ||
195 | #define RESET_DEVICE_COMMAND 0x06 | ||
196 | #define GET_HW_VERSION_COMMAND 0x07 | ||
197 | #define BLANK 0xff | ||
198 | |||
199 | /* bootloader command responses */ | ||
200 | #define COMMAND_OK 0x01 | ||
201 | #define COMMAND_FAILED 0x02 | ||
202 | #define COMMAND_PENDING 0x03 | ||
203 | |||
204 | |||
205 | |||
206 | /************************************************************************** | ||
207 | * General Driver Functions - REGISTER_REPORTs | ||
208 | **************************************************************************/ | ||
209 | |||
210 | /* | ||
211 | * si470x_get_report - receive a HID report | ||
212 | */ | ||
213 | static int si470x_get_report(struct si470x_device *radio, void *buf, int size) | ||
214 | { | ||
215 | unsigned char *report = (unsigned char *) buf; | ||
216 | int retval; | ||
217 | |||
218 | retval = usb_control_msg(radio->usbdev, | ||
219 | usb_rcvctrlpipe(radio->usbdev, 0), | ||
220 | HID_REQ_GET_REPORT, | ||
221 | USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN, | ||
222 | report[0], 2, | ||
223 | buf, size, usb_timeout); | ||
224 | |||
225 | if (retval < 0) | ||
226 | printk(KERN_WARNING DRIVER_NAME | ||
227 | ": si470x_get_report: usb_control_msg returned %d\n", | ||
228 | retval); | ||
229 | return retval; | ||
230 | } | ||
231 | |||
232 | |||
233 | /* | ||
234 | * si470x_set_report - send a HID report | ||
235 | */ | ||
236 | static int si470x_set_report(struct si470x_device *radio, void *buf, int size) | ||
237 | { | ||
238 | unsigned char *report = (unsigned char *) buf; | ||
239 | int retval; | ||
240 | |||
241 | retval = usb_control_msg(radio->usbdev, | ||
242 | usb_sndctrlpipe(radio->usbdev, 0), | ||
243 | HID_REQ_SET_REPORT, | ||
244 | USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT, | ||
245 | report[0], 2, | ||
246 | buf, size, usb_timeout); | ||
247 | |||
248 | if (retval < 0) | ||
249 | printk(KERN_WARNING DRIVER_NAME | ||
250 | ": si470x_set_report: usb_control_msg returned %d\n", | ||
251 | retval); | ||
252 | return retval; | ||
253 | } | ||
254 | |||
255 | |||
256 | /* | ||
257 | * si470x_get_register - read register | ||
258 | */ | ||
259 | int si470x_get_register(struct si470x_device *radio, int regnr) | ||
260 | { | ||
261 | unsigned char buf[REGISTER_REPORT_SIZE]; | ||
262 | int retval; | ||
263 | |||
264 | buf[0] = REGISTER_REPORT(regnr); | ||
265 | |||
266 | retval = si470x_get_report(radio, (void *) &buf, sizeof(buf)); | ||
267 | |||
268 | if (retval >= 0) | ||
269 | radio->registers[regnr] = get_unaligned_be16(&buf[1]); | ||
270 | |||
271 | return (retval < 0) ? -EINVAL : 0; | ||
272 | } | ||
273 | |||
274 | |||
275 | /* | ||
276 | * si470x_set_register - write register | ||
277 | */ | ||
278 | int si470x_set_register(struct si470x_device *radio, int regnr) | ||
279 | { | ||
280 | unsigned char buf[REGISTER_REPORT_SIZE]; | ||
281 | int retval; | ||
282 | |||
283 | buf[0] = REGISTER_REPORT(regnr); | ||
284 | put_unaligned_be16(radio->registers[regnr], &buf[1]); | ||
285 | |||
286 | retval = si470x_set_report(radio, (void *) &buf, sizeof(buf)); | ||
287 | |||
288 | return (retval < 0) ? -EINVAL : 0; | ||
289 | } | ||
290 | |||
291 | |||
292 | |||
293 | /************************************************************************** | ||
294 | * General Driver Functions - ENTIRE_REPORT | ||
295 | **************************************************************************/ | ||
296 | |||
297 | /* | ||
298 | * si470x_get_all_registers - read entire registers | ||
299 | */ | ||
300 | static int si470x_get_all_registers(struct si470x_device *radio) | ||
301 | { | ||
302 | unsigned char buf[ENTIRE_REPORT_SIZE]; | ||
303 | int retval; | ||
304 | unsigned char regnr; | ||
305 | |||
306 | buf[0] = ENTIRE_REPORT; | ||
307 | |||
308 | retval = si470x_get_report(radio, (void *) &buf, sizeof(buf)); | ||
309 | |||
310 | if (retval >= 0) | ||
311 | for (regnr = 0; regnr < RADIO_REGISTER_NUM; regnr++) | ||
312 | radio->registers[regnr] = get_unaligned_be16( | ||
313 | &buf[regnr * RADIO_REGISTER_SIZE + 1]); | ||
314 | |||
315 | return (retval < 0) ? -EINVAL : 0; | ||
316 | } | ||
317 | |||
318 | |||
319 | |||
320 | /************************************************************************** | ||
321 | * General Driver Functions - LED_REPORT | ||
322 | **************************************************************************/ | ||
323 | |||
324 | /* | ||
325 | * si470x_set_led_state - sets the led state | ||
326 | */ | ||
327 | static int si470x_set_led_state(struct si470x_device *radio, | ||
328 | unsigned char led_state) | ||
329 | { | ||
330 | unsigned char buf[LED_REPORT_SIZE]; | ||
331 | int retval; | ||
332 | |||
333 | buf[0] = LED_REPORT; | ||
334 | buf[1] = LED_COMMAND; | ||
335 | buf[2] = led_state; | ||
336 | |||
337 | retval = si470x_set_report(radio, (void *) &buf, sizeof(buf)); | ||
338 | |||
339 | return (retval < 0) ? -EINVAL : 0; | ||
340 | } | ||
341 | |||
342 | |||
343 | |||
344 | /************************************************************************** | ||
345 | * General Driver Functions - SCRATCH_REPORT | ||
346 | **************************************************************************/ | ||
347 | |||
348 | /* | ||
349 | * si470x_get_scratch_versions - gets the scratch page and version infos | ||
350 | */ | ||
351 | static int si470x_get_scratch_page_versions(struct si470x_device *radio) | ||
352 | { | ||
353 | unsigned char buf[SCRATCH_REPORT_SIZE]; | ||
354 | int retval; | ||
355 | |||
356 | buf[0] = SCRATCH_REPORT; | ||
357 | |||
358 | retval = si470x_get_report(radio, (void *) &buf, sizeof(buf)); | ||
359 | |||
360 | if (retval < 0) | ||
361 | printk(KERN_WARNING DRIVER_NAME ": si470x_get_scratch: " | ||
362 | "si470x_get_report returned %d\n", retval); | ||
363 | else { | ||
364 | radio->software_version = buf[1]; | ||
365 | radio->hardware_version = buf[2]; | ||
366 | } | ||
367 | |||
368 | return (retval < 0) ? -EINVAL : 0; | ||
369 | } | ||
370 | |||
371 | |||
372 | |||
373 | /************************************************************************** | ||
374 | * RDS Driver Functions | ||
375 | **************************************************************************/ | ||
376 | |||
377 | /* | ||
378 | * si470x_int_in_callback - rds callback and processing function | ||
379 | * | ||
380 | * TODO: do we need to use mutex locks in some sections? | ||
381 | */ | ||
382 | static void si470x_int_in_callback(struct urb *urb) | ||
383 | { | ||
384 | struct si470x_device *radio = urb->context; | ||
385 | unsigned char buf[RDS_REPORT_SIZE]; | ||
386 | int retval; | ||
387 | unsigned char regnr; | ||
388 | unsigned char blocknum; | ||
389 | unsigned short bler; /* rds block errors */ | ||
390 | unsigned short rds; | ||
391 | unsigned char tmpbuf[3]; | ||
392 | |||
393 | if (urb->status) { | ||
394 | if (urb->status == -ENOENT || | ||
395 | urb->status == -ECONNRESET || | ||
396 | urb->status == -ESHUTDOWN) { | ||
397 | return; | ||
398 | } else { | ||
399 | printk(KERN_WARNING DRIVER_NAME | ||
400 | ": non-zero urb status (%d)\n", urb->status); | ||
401 | goto resubmit; /* Maybe we can recover. */ | ||
402 | } | ||
403 | } | ||
404 | |||
405 | /* safety checks */ | ||
406 | if (radio->disconnected) | ||
407 | return; | ||
408 | if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) | ||
409 | goto resubmit; | ||
410 | |||
411 | if (urb->actual_length > 0) { | ||
412 | /* Update RDS registers with URB data */ | ||
413 | buf[0] = RDS_REPORT; | ||
414 | for (regnr = 0; regnr < RDS_REGISTER_NUM; regnr++) | ||
415 | radio->registers[STATUSRSSI + regnr] = | ||
416 | get_unaligned_be16(&radio->int_in_buffer[ | ||
417 | regnr * RADIO_REGISTER_SIZE + 1]); | ||
418 | /* get rds blocks */ | ||
419 | if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0) { | ||
420 | /* No RDS group ready, better luck next time */ | ||
421 | goto resubmit; | ||
422 | } | ||
423 | if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSS) == 0) { | ||
424 | /* RDS decoder not synchronized */ | ||
425 | goto resubmit; | ||
426 | } | ||
427 | for (blocknum = 0; blocknum < 4; blocknum++) { | ||
428 | switch (blocknum) { | ||
429 | default: | ||
430 | bler = (radio->registers[STATUSRSSI] & | ||
431 | STATUSRSSI_BLERA) >> 9; | ||
432 | rds = radio->registers[RDSA]; | ||
433 | break; | ||
434 | case 1: | ||
435 | bler = (radio->registers[READCHAN] & | ||
436 | READCHAN_BLERB) >> 14; | ||
437 | rds = radio->registers[RDSB]; | ||
438 | break; | ||
439 | case 2: | ||
440 | bler = (radio->registers[READCHAN] & | ||
441 | READCHAN_BLERC) >> 12; | ||
442 | rds = radio->registers[RDSC]; | ||
443 | break; | ||
444 | case 3: | ||
445 | bler = (radio->registers[READCHAN] & | ||
446 | READCHAN_BLERD) >> 10; | ||
447 | rds = radio->registers[RDSD]; | ||
448 | break; | ||
449 | }; | ||
450 | |||
451 | /* Fill the V4L2 RDS buffer */ | ||
452 | put_unaligned_le16(rds, &tmpbuf); | ||
453 | tmpbuf[2] = blocknum; /* offset name */ | ||
454 | tmpbuf[2] |= blocknum << 3; /* received offset */ | ||
455 | if (bler > max_rds_errors) | ||
456 | tmpbuf[2] |= 0x80; /* uncorrectable errors */ | ||
457 | else if (bler > 0) | ||
458 | tmpbuf[2] |= 0x40; /* corrected error(s) */ | ||
459 | |||
460 | /* copy RDS block to internal buffer */ | ||
461 | memcpy(&radio->buffer[radio->wr_index], &tmpbuf, 3); | ||
462 | radio->wr_index += 3; | ||
463 | |||
464 | /* wrap write pointer */ | ||
465 | if (radio->wr_index >= radio->buf_size) | ||
466 | radio->wr_index = 0; | ||
467 | |||
468 | /* check for overflow */ | ||
469 | if (radio->wr_index == radio->rd_index) { | ||
470 | /* increment and wrap read pointer */ | ||
471 | radio->rd_index += 3; | ||
472 | if (radio->rd_index >= radio->buf_size) | ||
473 | radio->rd_index = 0; | ||
474 | } | ||
475 | } | ||
476 | if (radio->wr_index != radio->rd_index) | ||
477 | wake_up_interruptible(&radio->read_queue); | ||
478 | } | ||
479 | |||
480 | resubmit: | ||
481 | /* Resubmit if we're still running. */ | ||
482 | if (radio->int_in_running && radio->usbdev) { | ||
483 | retval = usb_submit_urb(radio->int_in_urb, GFP_ATOMIC); | ||
484 | if (retval) { | ||
485 | printk(KERN_WARNING DRIVER_NAME | ||
486 | ": resubmitting urb failed (%d)", retval); | ||
487 | radio->int_in_running = 0; | ||
488 | } | ||
489 | } | ||
490 | } | ||
491 | |||
492 | |||
493 | |||
494 | /************************************************************************** | ||
495 | * File Operations Interface | ||
496 | **************************************************************************/ | ||
497 | |||
498 | /* | ||
499 | * si470x_fops_read - read RDS data | ||
500 | */ | ||
501 | static ssize_t si470x_fops_read(struct file *file, char __user *buf, | ||
502 | size_t count, loff_t *ppos) | ||
503 | { | ||
504 | struct si470x_device *radio = video_drvdata(file); | ||
505 | int retval = 0; | ||
506 | unsigned int block_count = 0; | ||
507 | |||
508 | /* switch on rds reception */ | ||
509 | if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) | ||
510 | si470x_rds_on(radio); | ||
511 | |||
512 | /* block if no new data available */ | ||
513 | while (radio->wr_index == radio->rd_index) { | ||
514 | if (file->f_flags & O_NONBLOCK) { | ||
515 | retval = -EWOULDBLOCK; | ||
516 | goto done; | ||
517 | } | ||
518 | if (wait_event_interruptible(radio->read_queue, | ||
519 | radio->wr_index != radio->rd_index) < 0) { | ||
520 | retval = -EINTR; | ||
521 | goto done; | ||
522 | } | ||
523 | } | ||
524 | |||
525 | /* calculate block count from byte count */ | ||
526 | count /= 3; | ||
527 | |||
528 | /* copy RDS block out of internal buffer and to user buffer */ | ||
529 | mutex_lock(&radio->lock); | ||
530 | while (block_count < count) { | ||
531 | if (radio->rd_index == radio->wr_index) | ||
532 | break; | ||
533 | |||
534 | /* always transfer rds complete blocks */ | ||
535 | if (copy_to_user(buf, &radio->buffer[radio->rd_index], 3)) | ||
536 | /* retval = -EFAULT; */ | ||
537 | break; | ||
538 | |||
539 | /* increment and wrap read pointer */ | ||
540 | radio->rd_index += 3; | ||
541 | if (radio->rd_index >= radio->buf_size) | ||
542 | radio->rd_index = 0; | ||
543 | |||
544 | /* increment counters */ | ||
545 | block_count++; | ||
546 | buf += 3; | ||
547 | retval += 3; | ||
548 | } | ||
549 | mutex_unlock(&radio->lock); | ||
550 | |||
551 | done: | ||
552 | return retval; | ||
553 | } | ||
554 | |||
555 | |||
556 | /* | ||
557 | * si470x_fops_poll - poll RDS data | ||
558 | */ | ||
559 | static unsigned int si470x_fops_poll(struct file *file, | ||
560 | struct poll_table_struct *pts) | ||
561 | { | ||
562 | struct si470x_device *radio = video_drvdata(file); | ||
563 | int retval = 0; | ||
564 | |||
565 | /* switch on rds reception */ | ||
566 | if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) | ||
567 | si470x_rds_on(radio); | ||
568 | |||
569 | poll_wait(file, &radio->read_queue, pts); | ||
570 | |||
571 | if (radio->rd_index != radio->wr_index) | ||
572 | retval = POLLIN | POLLRDNORM; | ||
573 | |||
574 | return retval; | ||
575 | } | ||
576 | |||
577 | |||
578 | /* | ||
579 | * si470x_fops_open - file open | ||
580 | */ | ||
581 | static int si470x_fops_open(struct file *file) | ||
582 | { | ||
583 | struct si470x_device *radio = video_drvdata(file); | ||
584 | int retval; | ||
585 | |||
586 | lock_kernel(); | ||
587 | radio->users++; | ||
588 | |||
589 | retval = usb_autopm_get_interface(radio->intf); | ||
590 | if (retval < 0) { | ||
591 | radio->users--; | ||
592 | retval = -EIO; | ||
593 | goto done; | ||
594 | } | ||
595 | |||
596 | if (radio->users == 1) { | ||
597 | /* start radio */ | ||
598 | retval = si470x_start(radio); | ||
599 | if (retval < 0) { | ||
600 | usb_autopm_put_interface(radio->intf); | ||
601 | goto done; | ||
602 | } | ||
603 | |||
604 | /* initialize interrupt urb */ | ||
605 | usb_fill_int_urb(radio->int_in_urb, radio->usbdev, | ||
606 | usb_rcvintpipe(radio->usbdev, | ||
607 | radio->int_in_endpoint->bEndpointAddress), | ||
608 | radio->int_in_buffer, | ||
609 | le16_to_cpu(radio->int_in_endpoint->wMaxPacketSize), | ||
610 | si470x_int_in_callback, | ||
611 | radio, | ||
612 | radio->int_in_endpoint->bInterval); | ||
613 | |||
614 | radio->int_in_running = 1; | ||
615 | mb(); | ||
616 | |||
617 | retval = usb_submit_urb(radio->int_in_urb, GFP_KERNEL); | ||
618 | if (retval) { | ||
619 | printk(KERN_INFO DRIVER_NAME | ||
620 | ": submitting int urb failed (%d)\n", retval); | ||
621 | radio->int_in_running = 0; | ||
622 | usb_autopm_put_interface(radio->intf); | ||
623 | } | ||
624 | } | ||
625 | |||
626 | done: | ||
627 | unlock_kernel(); | ||
628 | return retval; | ||
629 | } | ||
630 | |||
631 | |||
632 | /* | ||
633 | * si470x_fops_release - file release | ||
634 | */ | ||
635 | static int si470x_fops_release(struct file *file) | ||
636 | { | ||
637 | struct si470x_device *radio = video_drvdata(file); | ||
638 | int retval = 0; | ||
639 | |||
640 | /* safety check */ | ||
641 | if (!radio) { | ||
642 | retval = -ENODEV; | ||
643 | goto done; | ||
644 | } | ||
645 | |||
646 | mutex_lock(&radio->disconnect_lock); | ||
647 | radio->users--; | ||
648 | if (radio->users == 0) { | ||
649 | /* shutdown interrupt handler */ | ||
650 | if (radio->int_in_running) { | ||
651 | radio->int_in_running = 0; | ||
652 | if (radio->int_in_urb) | ||
653 | usb_kill_urb(radio->int_in_urb); | ||
654 | } | ||
655 | |||
656 | if (radio->disconnected) { | ||
657 | video_unregister_device(radio->videodev); | ||
658 | kfree(radio->int_in_buffer); | ||
659 | kfree(radio->buffer); | ||
660 | kfree(radio); | ||
661 | goto unlock; | ||
662 | } | ||
663 | |||
664 | /* cancel read processes */ | ||
665 | wake_up_interruptible(&radio->read_queue); | ||
666 | |||
667 | /* stop radio */ | ||
668 | retval = si470x_stop(radio); | ||
669 | usb_autopm_put_interface(radio->intf); | ||
670 | } | ||
671 | unlock: | ||
672 | mutex_unlock(&radio->disconnect_lock); | ||
673 | done: | ||
674 | return retval; | ||
675 | } | ||
676 | |||
677 | |||
678 | /* | ||
679 | * si470x_fops - file operations interface | ||
680 | */ | ||
681 | const struct v4l2_file_operations si470x_fops = { | ||
682 | .owner = THIS_MODULE, | ||
683 | .read = si470x_fops_read, | ||
684 | .poll = si470x_fops_poll, | ||
685 | .ioctl = video_ioctl2, | ||
686 | .open = si470x_fops_open, | ||
687 | .release = si470x_fops_release, | ||
688 | }; | ||
689 | |||
690 | |||
691 | |||
692 | /************************************************************************** | ||
693 | * Video4Linux Interface | ||
694 | **************************************************************************/ | ||
695 | |||
696 | /* | ||
697 | * si470x_vidioc_querycap - query device capabilities | ||
698 | */ | ||
699 | int si470x_vidioc_querycap(struct file *file, void *priv, | ||
700 | struct v4l2_capability *capability) | ||
701 | { | ||
702 | struct si470x_device *radio = video_drvdata(file); | ||
703 | |||
704 | strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver)); | ||
705 | strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card)); | ||
706 | usb_make_path(radio->usbdev, capability->bus_info, | ||
707 | sizeof(capability->bus_info)); | ||
708 | capability->version = DRIVER_KERNEL_VERSION; | ||
709 | capability->capabilities = V4L2_CAP_HW_FREQ_SEEK | | ||
710 | V4L2_CAP_TUNER | V4L2_CAP_RADIO | V4L2_CAP_RDS_CAPTURE; | ||
711 | |||
712 | return 0; | ||
713 | } | ||
714 | |||
715 | |||
716 | |||
717 | /************************************************************************** | ||
718 | * USB Interface | ||
719 | **************************************************************************/ | ||
720 | |||
721 | /* | ||
722 | * si470x_usb_driver_probe - probe for the device | ||
723 | */ | ||
724 | static int si470x_usb_driver_probe(struct usb_interface *intf, | ||
725 | const struct usb_device_id *id) | ||
726 | { | ||
727 | struct si470x_device *radio; | ||
728 | struct usb_host_interface *iface_desc; | ||
729 | struct usb_endpoint_descriptor *endpoint; | ||
730 | int i, int_end_size, retval = 0; | ||
731 | |||
732 | /* private data allocation and initialization */ | ||
733 | radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL); | ||
734 | if (!radio) { | ||
735 | retval = -ENOMEM; | ||
736 | goto err_initial; | ||
737 | } | ||
738 | radio->users = 0; | ||
739 | radio->disconnected = 0; | ||
740 | radio->usbdev = interface_to_usbdev(intf); | ||
741 | radio->intf = intf; | ||
742 | mutex_init(&radio->disconnect_lock); | ||
743 | mutex_init(&radio->lock); | ||
744 | |||
745 | iface_desc = intf->cur_altsetting; | ||
746 | |||
747 | /* Set up interrupt endpoint information. */ | ||
748 | for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { | ||
749 | endpoint = &iface_desc->endpoint[i].desc; | ||
750 | if (((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == | ||
751 | USB_DIR_IN) && ((endpoint->bmAttributes & | ||
752 | USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)) | ||
753 | radio->int_in_endpoint = endpoint; | ||
754 | } | ||
755 | if (!radio->int_in_endpoint) { | ||
756 | printk(KERN_INFO DRIVER_NAME | ||
757 | ": could not find interrupt in endpoint\n"); | ||
758 | retval = -EIO; | ||
759 | goto err_radio; | ||
760 | } | ||
761 | |||
762 | int_end_size = le16_to_cpu(radio->int_in_endpoint->wMaxPacketSize); | ||
763 | |||
764 | radio->int_in_buffer = kmalloc(int_end_size, GFP_KERNEL); | ||
765 | if (!radio->int_in_buffer) { | ||
766 | printk(KERN_INFO DRIVER_NAME | ||
767 | "could not allocate int_in_buffer"); | ||
768 | retval = -ENOMEM; | ||
769 | goto err_radio; | ||
770 | } | ||
771 | |||
772 | radio->int_in_urb = usb_alloc_urb(0, GFP_KERNEL); | ||
773 | if (!radio->int_in_urb) { | ||
774 | printk(KERN_INFO DRIVER_NAME "could not allocate int_in_urb"); | ||
775 | retval = -ENOMEM; | ||
776 | goto err_intbuffer; | ||
777 | } | ||
778 | |||
779 | /* video device allocation and initialization */ | ||
780 | radio->videodev = video_device_alloc(); | ||
781 | if (!radio->videodev) { | ||
782 | retval = -ENOMEM; | ||
783 | goto err_intbuffer; | ||
784 | } | ||
785 | memcpy(radio->videodev, &si470x_viddev_template, | ||
786 | sizeof(si470x_viddev_template)); | ||
787 | video_set_drvdata(radio->videodev, radio); | ||
788 | |||
789 | /* show some infos about the specific si470x device */ | ||
790 | if (si470x_get_all_registers(radio) < 0) { | ||
791 | retval = -EIO; | ||
792 | goto err_video; | ||
793 | } | ||
794 | printk(KERN_INFO DRIVER_NAME ": DeviceID=0x%4.4hx ChipID=0x%4.4hx\n", | ||
795 | radio->registers[DEVICEID], radio->registers[CHIPID]); | ||
796 | |||
797 | /* get software and hardware versions */ | ||
798 | if (si470x_get_scratch_page_versions(radio) < 0) { | ||
799 | retval = -EIO; | ||
800 | goto err_video; | ||
801 | } | ||
802 | printk(KERN_INFO DRIVER_NAME | ||
803 | ": software version %d, hardware version %d\n", | ||
804 | radio->software_version, radio->hardware_version); | ||
805 | |||
806 | /* check if device and firmware is current */ | ||
807 | if ((radio->registers[CHIPID] & CHIPID_FIRMWARE) | ||
808 | < RADIO_SW_VERSION_CURRENT) { | ||
809 | printk(KERN_WARNING DRIVER_NAME | ||
810 | ": This driver is known to work with " | ||
811 | "firmware version %hu,\n", RADIO_SW_VERSION_CURRENT); | ||
812 | printk(KERN_WARNING DRIVER_NAME | ||
813 | ": but the device has firmware version %hu.\n", | ||
814 | radio->registers[CHIPID] & CHIPID_FIRMWARE); | ||
815 | printk(KERN_WARNING DRIVER_NAME | ||
816 | ": If you have some trouble using this driver,\n"); | ||
817 | printk(KERN_WARNING DRIVER_NAME | ||
818 | ": please report to V4L ML at " | ||
819 | "linux-media@vger.kernel.org\n"); | ||
820 | } | ||
821 | |||
822 | /* set initial frequency */ | ||
823 | si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */ | ||
824 | |||
825 | /* set led to connect state */ | ||
826 | si470x_set_led_state(radio, BLINK_GREEN_LED); | ||
827 | |||
828 | /* rds buffer allocation */ | ||
829 | radio->buf_size = rds_buf * 3; | ||
830 | radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL); | ||
831 | if (!radio->buffer) { | ||
832 | retval = -EIO; | ||
833 | goto err_video; | ||
834 | } | ||
835 | |||
836 | /* rds buffer configuration */ | ||
837 | radio->wr_index = 0; | ||
838 | radio->rd_index = 0; | ||
839 | init_waitqueue_head(&radio->read_queue); | ||
840 | |||
841 | /* register video device */ | ||
842 | retval = video_register_device(radio->videodev, VFL_TYPE_RADIO, | ||
843 | radio_nr); | ||
844 | if (retval) { | ||
845 | printk(KERN_WARNING DRIVER_NAME | ||
846 | ": Could not register video device\n"); | ||
847 | goto err_all; | ||
848 | } | ||
849 | usb_set_intfdata(intf, radio); | ||
850 | |||
851 | return 0; | ||
852 | err_all: | ||
853 | kfree(radio->buffer); | ||
854 | err_video: | ||
855 | video_device_release(radio->videodev); | ||
856 | err_intbuffer: | ||
857 | kfree(radio->int_in_buffer); | ||
858 | err_radio: | ||
859 | kfree(radio); | ||
860 | err_initial: | ||
861 | return retval; | ||
862 | } | ||
863 | |||
864 | |||
865 | /* | ||
866 | * si470x_usb_driver_suspend - suspend the device | ||
867 | */ | ||
868 | static int si470x_usb_driver_suspend(struct usb_interface *intf, | ||
869 | pm_message_t message) | ||
870 | { | ||
871 | printk(KERN_INFO DRIVER_NAME ": suspending now...\n"); | ||
872 | |||
873 | return 0; | ||
874 | } | ||
875 | |||
876 | |||
877 | /* | ||
878 | * si470x_usb_driver_resume - resume the device | ||
879 | */ | ||
880 | static int si470x_usb_driver_resume(struct usb_interface *intf) | ||
881 | { | ||
882 | printk(KERN_INFO DRIVER_NAME ": resuming now...\n"); | ||
883 | |||
884 | return 0; | ||
885 | } | ||
886 | |||
887 | |||
888 | /* | ||
889 | * si470x_usb_driver_disconnect - disconnect the device | ||
890 | */ | ||
891 | static void si470x_usb_driver_disconnect(struct usb_interface *intf) | ||
892 | { | ||
893 | struct si470x_device *radio = usb_get_intfdata(intf); | ||
894 | |||
895 | mutex_lock(&radio->disconnect_lock); | ||
896 | radio->disconnected = 1; | ||
897 | usb_set_intfdata(intf, NULL); | ||
898 | if (radio->users == 0) { | ||
899 | /* set led to disconnect state */ | ||
900 | si470x_set_led_state(radio, BLINK_ORANGE_LED); | ||
901 | |||
902 | /* Free data structures. */ | ||
903 | usb_free_urb(radio->int_in_urb); | ||
904 | |||
905 | kfree(radio->int_in_buffer); | ||
906 | video_unregister_device(radio->videodev); | ||
907 | kfree(radio->buffer); | ||
908 | kfree(radio); | ||
909 | } | ||
910 | mutex_unlock(&radio->disconnect_lock); | ||
911 | } | ||
912 | |||
913 | |||
914 | /* | ||
915 | * si470x_usb_driver - usb driver interface | ||
916 | */ | ||
917 | static struct usb_driver si470x_usb_driver = { | ||
918 | .name = DRIVER_NAME, | ||
919 | .probe = si470x_usb_driver_probe, | ||
920 | .disconnect = si470x_usb_driver_disconnect, | ||
921 | .suspend = si470x_usb_driver_suspend, | ||
922 | .resume = si470x_usb_driver_resume, | ||
923 | .id_table = si470x_usb_driver_id_table, | ||
924 | .supports_autosuspend = 1, | ||
925 | }; | ||
926 | |||
927 | |||
928 | |||
929 | /************************************************************************** | ||
930 | * Module Interface | ||
931 | **************************************************************************/ | ||
932 | |||
933 | /* | ||
934 | * si470x_module_init - module init | ||
935 | */ | ||
936 | static int __init si470x_module_init(void) | ||
937 | { | ||
938 | printk(KERN_INFO DRIVER_DESC ", Version " DRIVER_VERSION "\n"); | ||
939 | return usb_register(&si470x_usb_driver); | ||
940 | } | ||
941 | |||
942 | |||
943 | /* | ||
944 | * si470x_module_exit - module exit | ||
945 | */ | ||
946 | static void __exit si470x_module_exit(void) | ||
947 | { | ||
948 | usb_deregister(&si470x_usb_driver); | ||
949 | } | ||
950 | |||
951 | |||
952 | module_init(si470x_module_init); | ||
953 | module_exit(si470x_module_exit); | ||
954 | |||
955 | MODULE_LICENSE("GPL"); | ||
956 | MODULE_AUTHOR(DRIVER_AUTHOR); | ||
957 | MODULE_DESCRIPTION(DRIVER_DESC); | ||
958 | MODULE_VERSION(DRIVER_VERSION); | ||
diff --git a/drivers/media/radio/si470x/radio-si470x.h b/drivers/media/radio/si470x/radio-si470x.h new file mode 100644 index 000000000000..dd71985c4113 --- /dev/null +++ b/drivers/media/radio/si470x/radio-si470x.h | |||
@@ -0,0 +1,201 @@ | |||
1 | /* | ||
2 | * drivers/media/radio/si470x/radio-si470x.h | ||
3 | * | ||
4 | * Driver for radios with Silicon Labs Si470x FM Radio Receivers | ||
5 | * | ||
6 | * Copyright (c) 2009 Tobias Lorenz <tobias.lorenz@gmx.net> | ||
7 | * | ||
8 | * This program is free software; you can redistribute it and/or modify | ||
9 | * it under the terms of the GNU General Public License as published by | ||
10 | * the Free Software Foundation; either version 2 of the License, or | ||
11 | * (at your option) any later version. | ||
12 | * | ||
13 | * This program is distributed in the hope that it will be useful, | ||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
16 | * GNU General Public License for more details. | ||
17 | * | ||
18 | * You should have received a copy of the GNU General Public License | ||
19 | * along with this program; if not, write to the Free Software | ||
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
21 | */ | ||
22 | |||
23 | |||
24 | /* driver definitions */ | ||
25 | #define DRIVER_NAME "radio-si470x" | ||
26 | |||
27 | |||
28 | /* kernel includes */ | ||
29 | #include <linux/kernel.h> | ||
30 | #include <linux/module.h> | ||
31 | #include <linux/init.h> | ||
32 | #include <linux/slab.h> | ||
33 | #include <linux/smp_lock.h> | ||
34 | #include <linux/input.h> | ||
35 | #include <linux/version.h> | ||
36 | #include <linux/videodev2.h> | ||
37 | #include <linux/mutex.h> | ||
38 | #include <media/v4l2-common.h> | ||
39 | #include <media/v4l2-ioctl.h> | ||
40 | #include <media/rds.h> | ||
41 | #include <asm/unaligned.h> | ||
42 | |||
43 | |||
44 | /************************************************************************** | ||
45 | * Register Definitions | ||
46 | **************************************************************************/ | ||
47 | #define RADIO_REGISTER_SIZE 2 /* 16 register bit width */ | ||
48 | #define RADIO_REGISTER_NUM 16 /* DEVICEID ... RDSD */ | ||
49 | #define RDS_REGISTER_NUM 6 /* STATUSRSSI ... RDSD */ | ||
50 | |||
51 | #define DEVICEID 0 /* Device ID */ | ||
52 | #define DEVICEID_PN 0xf000 /* bits 15..12: Part Number */ | ||
53 | #define DEVICEID_MFGID 0x0fff /* bits 11..00: Manufacturer ID */ | ||
54 | |||
55 | #define CHIPID 1 /* Chip ID */ | ||
56 | #define CHIPID_REV 0xfc00 /* bits 15..10: Chip Version */ | ||
57 | #define CHIPID_DEV 0x0200 /* bits 09..09: Device */ | ||
58 | #define CHIPID_FIRMWARE 0x01ff /* bits 08..00: Firmware Version */ | ||
59 | |||
60 | #define POWERCFG 2 /* Power Configuration */ | ||
61 | #define POWERCFG_DSMUTE 0x8000 /* bits 15..15: Softmute Disable */ | ||
62 | #define POWERCFG_DMUTE 0x4000 /* bits 14..14: Mute Disable */ | ||
63 | #define POWERCFG_MONO 0x2000 /* bits 13..13: Mono Select */ | ||
64 | #define POWERCFG_RDSM 0x0800 /* bits 11..11: RDS Mode (Si4701 only) */ | ||
65 | #define POWERCFG_SKMODE 0x0400 /* bits 10..10: Seek Mode */ | ||
66 | #define POWERCFG_SEEKUP 0x0200 /* bits 09..09: Seek Direction */ | ||
67 | #define POWERCFG_SEEK 0x0100 /* bits 08..08: Seek */ | ||
68 | #define POWERCFG_DISABLE 0x0040 /* bits 06..06: Powerup Disable */ | ||
69 | #define POWERCFG_ENABLE 0x0001 /* bits 00..00: Powerup Enable */ | ||
70 | |||
71 | #define CHANNEL 3 /* Channel */ | ||
72 | #define CHANNEL_TUNE 0x8000 /* bits 15..15: Tune */ | ||
73 | #define CHANNEL_CHAN 0x03ff /* bits 09..00: Channel Select */ | ||
74 | |||
75 | #define SYSCONFIG1 4 /* System Configuration 1 */ | ||
76 | #define SYSCONFIG1_RDSIEN 0x8000 /* bits 15..15: RDS Interrupt Enable (Si4701 only) */ | ||
77 | #define SYSCONFIG1_STCIEN 0x4000 /* bits 14..14: Seek/Tune Complete Interrupt Enable */ | ||
78 | #define SYSCONFIG1_RDS 0x1000 /* bits 12..12: RDS Enable (Si4701 only) */ | ||
79 | #define SYSCONFIG1_DE 0x0800 /* bits 11..11: De-emphasis (0=75us 1=50us) */ | ||
80 | #define SYSCONFIG1_AGCD 0x0400 /* bits 10..10: AGC Disable */ | ||
81 | #define SYSCONFIG1_BLNDADJ 0x00c0 /* bits 07..06: Stereo/Mono Blend Level Adjustment */ | ||
82 | #define SYSCONFIG1_GPIO3 0x0030 /* bits 05..04: General Purpose I/O 3 */ | ||
83 | #define SYSCONFIG1_GPIO2 0x000c /* bits 03..02: General Purpose I/O 2 */ | ||
84 | #define SYSCONFIG1_GPIO1 0x0003 /* bits 01..00: General Purpose I/O 1 */ | ||
85 | |||
86 | #define SYSCONFIG2 5 /* System Configuration 2 */ | ||
87 | #define SYSCONFIG2_SEEKTH 0xff00 /* bits 15..08: RSSI Seek Threshold */ | ||
88 | #define SYSCONFIG2_BAND 0x0080 /* bits 07..06: Band Select */ | ||
89 | #define SYSCONFIG2_SPACE 0x0030 /* bits 05..04: Channel Spacing */ | ||
90 | #define SYSCONFIG2_VOLUME 0x000f /* bits 03..00: Volume */ | ||
91 | |||
92 | #define SYSCONFIG3 6 /* System Configuration 3 */ | ||
93 | #define SYSCONFIG3_SMUTER 0xc000 /* bits 15..14: Softmute Attack/Recover Rate */ | ||
94 | #define SYSCONFIG3_SMUTEA 0x3000 /* bits 13..12: Softmute Attenuation */ | ||
95 | #define SYSCONFIG3_SKSNR 0x00f0 /* bits 07..04: Seek SNR Threshold */ | ||
96 | #define SYSCONFIG3_SKCNT 0x000f /* bits 03..00: Seek FM Impulse Detection Threshold */ | ||
97 | |||
98 | #define TEST1 7 /* Test 1 */ | ||
99 | #define TEST1_AHIZEN 0x4000 /* bits 14..14: Audio High-Z Enable */ | ||
100 | |||
101 | #define TEST2 8 /* Test 2 */ | ||
102 | /* TEST2 only contains reserved bits */ | ||
103 | |||
104 | #define BOOTCONFIG 9 /* Boot Configuration */ | ||
105 | /* BOOTCONFIG only contains reserved bits */ | ||
106 | |||
107 | #define STATUSRSSI 10 /* Status RSSI */ | ||
108 | #define STATUSRSSI_RDSR 0x8000 /* bits 15..15: RDS Ready (Si4701 only) */ | ||
109 | #define STATUSRSSI_STC 0x4000 /* bits 14..14: Seek/Tune Complete */ | ||
110 | #define STATUSRSSI_SF 0x2000 /* bits 13..13: Seek Fail/Band Limit */ | ||
111 | #define STATUSRSSI_AFCRL 0x1000 /* bits 12..12: AFC Rail */ | ||
112 | #define STATUSRSSI_RDSS 0x0800 /* bits 11..11: RDS Synchronized (Si4701 only) */ | ||
113 | #define STATUSRSSI_BLERA 0x0600 /* bits 10..09: RDS Block A Errors (Si4701 only) */ | ||
114 | #define STATUSRSSI_ST 0x0100 /* bits 08..08: Stereo Indicator */ | ||
115 | #define STATUSRSSI_RSSI 0x00ff /* bits 07..00: RSSI (Received Signal Strength Indicator) */ | ||
116 | |||
117 | #define READCHAN 11 /* Read Channel */ | ||
118 | #define READCHAN_BLERB 0xc000 /* bits 15..14: RDS Block D Errors (Si4701 only) */ | ||
119 | #define READCHAN_BLERC 0x3000 /* bits 13..12: RDS Block C Errors (Si4701 only) */ | ||
120 | #define READCHAN_BLERD 0x0c00 /* bits 11..10: RDS Block B Errors (Si4701 only) */ | ||
121 | #define READCHAN_READCHAN 0x03ff /* bits 09..00: Read Channel */ | ||
122 | |||
123 | #define RDSA 12 /* RDSA */ | ||
124 | #define RDSA_RDSA 0xffff /* bits 15..00: RDS Block A Data (Si4701 only) */ | ||
125 | |||
126 | #define RDSB 13 /* RDSB */ | ||
127 | #define RDSB_RDSB 0xffff /* bits 15..00: RDS Block B Data (Si4701 only) */ | ||
128 | |||
129 | #define RDSC 14 /* RDSC */ | ||
130 | #define RDSC_RDSC 0xffff /* bits 15..00: RDS Block C Data (Si4701 only) */ | ||
131 | |||
132 | #define RDSD 15 /* RDSD */ | ||
133 | #define RDSD_RDSD 0xffff /* bits 15..00: RDS Block D Data (Si4701 only) */ | ||
134 | |||
135 | |||
136 | /************************************************************************** | ||
137 | * General Driver Definitions | ||
138 | **************************************************************************/ | ||
139 | |||
140 | /* | ||
141 | * si470x_device - private data | ||
142 | */ | ||
143 | struct si470x_device { | ||
144 | struct video_device *videodev; | ||
145 | |||
146 | /* reference to USB and video device */ | ||
147 | struct usb_device *usbdev; | ||
148 | struct usb_interface *intf; | ||
149 | |||
150 | /* Interrupt endpoint handling */ | ||
151 | char *int_in_buffer; | ||
152 | struct usb_endpoint_descriptor *int_in_endpoint; | ||
153 | struct urb *int_in_urb; | ||
154 | int int_in_running; | ||
155 | |||
156 | /* scratch page */ | ||
157 | unsigned char software_version; | ||
158 | unsigned char hardware_version; | ||
159 | |||
160 | /* driver management */ | ||
161 | unsigned char disconnected; | ||
162 | struct mutex disconnect_lock; | ||
163 | unsigned int users; | ||
164 | |||
165 | /* Silabs internal registers (0..15) */ | ||
166 | unsigned short registers[RADIO_REGISTER_NUM]; | ||
167 | |||
168 | /* RDS receive buffer */ | ||
169 | wait_queue_head_t read_queue; | ||
170 | struct mutex lock; /* buffer locking */ | ||
171 | unsigned char *buffer; /* size is always multiple of three */ | ||
172 | unsigned int buf_size; | ||
173 | unsigned int rd_index; | ||
174 | unsigned int wr_index; | ||
175 | }; | ||
176 | |||
177 | |||
178 | /* | ||
179 | * The frequency is set in units of 62.5 Hz when using V4L2_TUNER_CAP_LOW, | ||
180 | * 62.5 kHz otherwise. | ||
181 | * The tuner is able to have a channel spacing of 50, 100 or 200 kHz. | ||
182 | * tuner->capability is therefore set to V4L2_TUNER_CAP_LOW | ||
183 | * The FREQ_MUL is then: 1 MHz / 62.5 Hz = 16000 | ||
184 | */ | ||
185 | #define FREQ_MUL (1000000 / 62.5) | ||
186 | |||
187 | |||
188 | |||
189 | /************************************************************************** | ||
190 | * Common Functions | ||
191 | **************************************************************************/ | ||
192 | extern const struct v4l2_file_operations si470x_fops; | ||
193 | extern struct video_device si470x_viddev_template; | ||
194 | int si470x_get_register(struct si470x_device *radio, int regnr); | ||
195 | int si470x_set_register(struct si470x_device *radio, int regnr); | ||
196 | int si470x_set_freq(struct si470x_device *radio, unsigned int freq); | ||
197 | int si470x_start(struct si470x_device *radio); | ||
198 | int si470x_stop(struct si470x_device *radio); | ||
199 | int si470x_rds_on(struct si470x_device *radio); | ||
200 | int si470x_vidioc_querycap(struct file *file, void *priv, | ||
201 | struct v4l2_capability *capability); | ||