2 * drivers/media/radio/radio-si470x.c
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)
8 * Copyright (c) 2008 Tobias Lorenz <tobias.lorenz@gmx.net>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 * - USB Audio is provided by the alsa snd_usb_audio module.
29 * For listing you have to redirect the sound, for example using:
30 * arecord -D hw:1,0 -r96000 -c2 -f S16_LE | artsdsp aplay -B -
31 * - regarding module parameters in /sys/module/radio_si470x/parameters:
32 * the contents of read-only files (0444) are not updated, even if
33 * space, band and de are changed using private video controls
34 * - increase tune_timeout, if you often get -EIO errors
35 * - hw_freq_seek returns -EAGAIN, when timed out or band limit is reached
41 * 2008-01-12 Tobias Lorenz <tobias.lorenz@gmx.net>
43 * - First working version
44 * 2008-01-13 Tobias Lorenz <tobias.lorenz@gmx.net>
46 * - Improved error handling, every function now returns errno
47 * - Improved multi user access (start/mute/stop)
48 * - Channel doesn't get lost anymore after start/mute/stop
49 * - RDS support added (polling mode via interrupt EP 1)
50 * - marked default module parameters with *value*
51 * - switched from bit structs to bit masks
52 * - header file cleaned and integrated
53 * 2008-01-14 Tobias Lorenz <tobias.lorenz@gmx.net>
55 * - hex values are now lower case
56 * - commented USB ID for ADS/Tech moved on todo list
57 * - blacklisted si470x in hid-quirks.c
58 * - rds buffer handling functions integrated into *_work, *_read
59 * - rds_command in si470x_poll exchanged against simple retval
60 * - check for firmware version 15
61 * - code order and prototypes still remain the same
62 * - spacing and bottom of band codes remain the same
63 * 2008-01-16 Tobias Lorenz <tobias.lorenz@gmx.net>
65 * - code reordered to avoid function prototypes
66 * - switch/case defaults are now more user-friendly
67 * - unified comment style
68 * - applied all checkpatch.pl v1.12 suggestions
69 * except the warning about the too long lines with bit comments
70 * - renamed FMRADIO to RADIO to cut line length (checkpatch.pl)
71 * 2008-01-22 Tobias Lorenz <tobias.lorenz@gmx.net>
73 * - avoid poss. locking when doing copy_to_user which may sleep
74 * - RDS is automatically activated on read now
75 * - code cleaned of unnecessary rds_commands
76 * - USB Vendor/Product ID for ADS/Tech FM Radio Receiver verified
77 * (thanks to Guillaume RAMOUSSE)
78 * 2008-01-27 Tobias Lorenz <tobias.lorenz@gmx.net>
80 * - number of seek_retries changed to tune_timeout
81 * - fixed problem with incomplete tune operations by own buffers
82 * - optimization of variables and printf types
83 * - improved error logging
84 * 2008-01-31 Tobias Lorenz <tobias.lorenz@gmx.net>
85 * Oliver Neukum <oliver@neukum.org>
87 * - fixed coverity checker warnings in *_usb_driver_disconnect
88 * - probe()/open() race by correct ordering in probe()
89 * - DMA coherency rules by separate allocation of all buffers
90 * - use of endianness macros
91 * - abuse of spinlock, replaced by mutex
92 * - racy handling of timer in disconnect,
93 * replaced by delayed_work
94 * - racy interruptible_sleep_on(),
95 * replaced with wait_event_interruptible()
96 * - handle signals in read()
97 * 2008-02-08 Tobias Lorenz <tobias.lorenz@gmx.net>
98 * Oliver Neukum <oliver@neukum.org>
100 * - usb autosuspend support
102 * 2008-05-07 Tobias Lorenz <tobias.lorenz@gmx.net>
104 * - hardware frequency seek support
106 * - more safety checks, let si470x_get_freq return errno
109 * - add firmware download/update support
110 * - RDS support: interrupt mode, instead of polling
111 * - add LED status output (check if that's not already done in firmware)
115 /* driver definitions */
116 #define DRIVER_AUTHOR "Tobias Lorenz <tobias.lorenz@gmx.net>"
117 #define DRIVER_NAME "radio-si470x"
118 #define DRIVER_KERNEL_VERSION KERNEL_VERSION(1, 0, 8)
119 #define DRIVER_CARD "Silicon Labs Si470x FM Radio Receiver"
120 #define DRIVER_DESC "USB radio driver for Si470x FM Radio Receivers"
121 #define DRIVER_VERSION "1.0.8"
124 /* kernel includes */
125 #include <linux/kernel.h>
126 #include <linux/module.h>
127 #include <linux/init.h>
128 #include <linux/slab.h>
129 #include <linux/input.h>
130 #include <linux/usb.h>
131 #include <linux/hid.h>
132 #include <linux/version.h>
133 #include <linux/videodev2.h>
134 #include <linux/mutex.h>
135 #include <media/v4l2-common.h>
136 #include <media/rds.h>
137 #include <asm/unaligned.h>
140 /* USB Device ID List */
141 static struct usb_device_id si470x_usb_driver_id_table[] = {
142 /* Silicon Labs USB FM Radio Reference Design */
143 { USB_DEVICE_AND_INTERFACE_INFO(0x10c4, 0x818a, USB_CLASS_HID, 0, 0) },
144 /* ADS/Tech FM Radio Receiver (formerly Instant FM Music) */
145 { USB_DEVICE_AND_INTERFACE_INFO(0x06e1, 0xa155, USB_CLASS_HID, 0, 0) },
146 /* Terminating entry */
149 MODULE_DEVICE_TABLE(usb, si470x_usb_driver_id_table);
153 /**************************************************************************
155 **************************************************************************/
158 static int radio_nr = -1;
159 module_param(radio_nr, int, 0);
160 MODULE_PARM_DESC(radio_nr, "Radio Nr");
163 /* 0: 200 kHz (USA, Australia) */
164 /* 1: 100 kHz (Europe, Japan) */
166 static unsigned short space = 2;
167 module_param(space, ushort, 0);
168 MODULE_PARM_DESC(radio_nr, "Spacing: 0=200kHz 1=100kHz *2=50kHz*");
170 /* Bottom of Band (MHz) */
171 /* 0: 87.5 - 108 MHz (USA, Europe)*/
172 /* 1: 76 - 108 MHz (Japan wide band) */
173 /* 2: 76 - 90 MHz (Japan) */
174 static unsigned short band = 1;
175 module_param(band, ushort, 0);
176 MODULE_PARM_DESC(radio_nr, "Band: 0=87.5..108MHz *1=76..108MHz* 2=76..90MHz");
180 /* 1: 50 us (Europe, Australia, Japan) */
181 static unsigned short de = 1;
182 module_param(de, ushort, 0);
183 MODULE_PARM_DESC(radio_nr, "De-emphasis: 0=75us *1=50us*");
186 static unsigned int usb_timeout = 500;
187 module_param(usb_timeout, uint, 0);
188 MODULE_PARM_DESC(usb_timeout, "USB timeout (ms): *500*");
191 static unsigned int tune_timeout = 3000;
192 module_param(tune_timeout, uint, 0);
193 MODULE_PARM_DESC(tune_timeout, "Tune timeout: *3000*");
196 static unsigned int seek_timeout = 5000;
197 module_param(seek_timeout, uint, 0);
198 MODULE_PARM_DESC(seek_timeout, "Seek timeout: *5000*");
200 /* RDS buffer blocks */
201 static unsigned int rds_buf = 100;
202 module_param(rds_buf, uint, 0);
203 MODULE_PARM_DESC(rds_buf, "RDS buffer entries: *100*");
205 /* RDS maximum block errors */
206 static unsigned short max_rds_errors = 1;
207 /* 0 means 0 errors requiring correction */
208 /* 1 means 1-2 errors requiring correction (used by original USBRadio.exe) */
209 /* 2 means 3-5 errors requiring correction */
210 /* 3 means 6+ errors or errors in checkword, correction not possible */
211 module_param(max_rds_errors, ushort, 0);
212 MODULE_PARM_DESC(max_rds_errors, "RDS maximum block errors: *1*");
214 /* RDS poll frequency */
215 static unsigned int rds_poll_time = 40;
216 /* 40 is used by the original USBRadio.exe */
217 /* 50 is used by radio-cadet */
218 /* 75 should be okay */
219 /* 80 is the usual RDS receive interval */
220 module_param(rds_poll_time, uint, 0);
221 MODULE_PARM_DESC(rds_poll_time, "RDS poll time (ms): *40*");
225 /**************************************************************************
226 * Register Definitions
227 **************************************************************************/
228 #define RADIO_REGISTER_SIZE 2 /* 16 register bit width */
229 #define RADIO_REGISTER_NUM 16 /* DEVICEID ... RDSD */
230 #define RDS_REGISTER_NUM 6 /* STATUSRSSI ... RDSD */
232 #define DEVICEID 0 /* Device ID */
233 #define DEVICEID_PN 0xf000 /* bits 15..12: Part Number */
234 #define DEVICEID_MFGID 0x0fff /* bits 11..00: Manufacturer ID */
236 #define CHIPID 1 /* Chip ID */
237 #define CHIPID_REV 0xfc00 /* bits 15..10: Chip Version */
238 #define CHIPID_DEV 0x0200 /* bits 09..09: Device */
239 #define CHIPID_FIRMWARE 0x01ff /* bits 08..00: Firmware Version */
241 #define POWERCFG 2 /* Power Configuration */
242 #define POWERCFG_DSMUTE 0x8000 /* bits 15..15: Softmute Disable */
243 #define POWERCFG_DMUTE 0x4000 /* bits 14..14: Mute Disable */
244 #define POWERCFG_MONO 0x2000 /* bits 13..13: Mono Select */
245 #define POWERCFG_RDSM 0x0800 /* bits 11..11: RDS Mode (Si4701 only) */
246 #define POWERCFG_SKMODE 0x0400 /* bits 10..10: Seek Mode */
247 #define POWERCFG_SEEKUP 0x0200 /* bits 09..09: Seek Direction */
248 #define POWERCFG_SEEK 0x0100 /* bits 08..08: Seek */
249 #define POWERCFG_DISABLE 0x0040 /* bits 06..06: Powerup Disable */
250 #define POWERCFG_ENABLE 0x0001 /* bits 00..00: Powerup Enable */
252 #define CHANNEL 3 /* Channel */
253 #define CHANNEL_TUNE 0x8000 /* bits 15..15: Tune */
254 #define CHANNEL_CHAN 0x03ff /* bits 09..00: Channel Select */
256 #define SYSCONFIG1 4 /* System Configuration 1 */
257 #define SYSCONFIG1_RDSIEN 0x8000 /* bits 15..15: RDS Interrupt Enable (Si4701 only) */
258 #define SYSCONFIG1_STCIEN 0x4000 /* bits 14..14: Seek/Tune Complete Interrupt Enable */
259 #define SYSCONFIG1_RDS 0x1000 /* bits 12..12: RDS Enable (Si4701 only) */
260 #define SYSCONFIG1_DE 0x0800 /* bits 11..11: De-emphasis (0=75us 1=50us) */
261 #define SYSCONFIG1_AGCD 0x0400 /* bits 10..10: AGC Disable */
262 #define SYSCONFIG1_BLNDADJ 0x00c0 /* bits 07..06: Stereo/Mono Blend Level Adjustment */
263 #define SYSCONFIG1_GPIO3 0x0030 /* bits 05..04: General Purpose I/O 3 */
264 #define SYSCONFIG1_GPIO2 0x000c /* bits 03..02: General Purpose I/O 2 */
265 #define SYSCONFIG1_GPIO1 0x0003 /* bits 01..00: General Purpose I/O 1 */
267 #define SYSCONFIG2 5 /* System Configuration 2 */
268 #define SYSCONFIG2_SEEKTH 0xff00 /* bits 15..08: RSSI Seek Threshold */
269 #define SYSCONFIG2_BAND 0x0080 /* bits 07..06: Band Select */
270 #define SYSCONFIG2_SPACE 0x0030 /* bits 05..04: Channel Spacing */
271 #define SYSCONFIG2_VOLUME 0x000f /* bits 03..00: Volume */
273 #define SYSCONFIG3 6 /* System Configuration 3 */
274 #define SYSCONFIG3_SMUTER 0xc000 /* bits 15..14: Softmute Attack/Recover Rate */
275 #define SYSCONFIG3_SMUTEA 0x3000 /* bits 13..12: Softmute Attenuation */
276 #define SYSCONFIG3_SKSNR 0x00f0 /* bits 07..04: Seek SNR Threshold */
277 #define SYSCONFIG3_SKCNT 0x000f /* bits 03..00: Seek FM Impulse Detection Threshold */
279 #define TEST1 7 /* Test 1 */
280 #define TEST1_AHIZEN 0x4000 /* bits 14..14: Audio High-Z Enable */
282 #define TEST2 8 /* Test 2 */
283 /* TEST2 only contains reserved bits */
285 #define BOOTCONFIG 9 /* Boot Configuration */
286 /* BOOTCONFIG only contains reserved bits */
288 #define STATUSRSSI 10 /* Status RSSI */
289 #define STATUSRSSI_RDSR 0x8000 /* bits 15..15: RDS Ready (Si4701 only) */
290 #define STATUSRSSI_STC 0x4000 /* bits 14..14: Seek/Tune Complete */
291 #define STATUSRSSI_SF 0x2000 /* bits 13..13: Seek Fail/Band Limit */
292 #define STATUSRSSI_AFCRL 0x1000 /* bits 12..12: AFC Rail */
293 #define STATUSRSSI_RDSS 0x0800 /* bits 11..11: RDS Synchronized (Si4701 only) */
294 #define STATUSRSSI_BLERA 0x0600 /* bits 10..09: RDS Block A Errors (Si4701 only) */
295 #define STATUSRSSI_ST 0x0100 /* bits 08..08: Stereo Indicator */
296 #define STATUSRSSI_RSSI 0x00ff /* bits 07..00: RSSI (Received Signal Strength Indicator) */
298 #define READCHAN 11 /* Read Channel */
299 #define READCHAN_BLERB 0xc000 /* bits 15..14: RDS Block D Errors (Si4701 only) */
300 #define READCHAN_BLERC 0x3000 /* bits 13..12: RDS Block C Errors (Si4701 only) */
301 #define READCHAN_BLERD 0x0c00 /* bits 11..10: RDS Block B Errors (Si4701 only) */
302 #define READCHAN_READCHAN 0x03ff /* bits 09..00: Read Channel */
304 #define RDSA 12 /* RDSA */
305 #define RDSA_RDSA 0xffff /* bits 15..00: RDS Block A Data (Si4701 only) */
307 #define RDSB 13 /* RDSB */
308 #define RDSB_RDSB 0xffff /* bits 15..00: RDS Block B Data (Si4701 only) */
310 #define RDSC 14 /* RDSC */
311 #define RDSC_RDSC 0xffff /* bits 15..00: RDS Block C Data (Si4701 only) */
313 #define RDSD 15 /* RDSD */
314 #define RDSD_RDSD 0xffff /* bits 15..00: RDS Block D Data (Si4701 only) */
318 /**************************************************************************
320 **************************************************************************/
322 /* Reports 1-16 give direct read/write access to the 16 Si470x registers */
323 /* with the (REPORT_ID - 1) corresponding to the register address across USB */
324 /* endpoint 0 using GET_REPORT and SET_REPORT */
325 #define REGISTER_REPORT_SIZE (RADIO_REGISTER_SIZE + 1)
326 #define REGISTER_REPORT(reg) ((reg) + 1)
328 /* Report 17 gives direct read/write access to the entire Si470x register */
329 /* map across endpoint 0 using GET_REPORT and SET_REPORT */
330 #define ENTIRE_REPORT_SIZE (RADIO_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
331 #define ENTIRE_REPORT 17
333 /* Report 18 is used to send the lowest 6 Si470x registers up the HID */
334 /* interrupt endpoint 1 to Windows every 20 milliseconds for status */
335 #define RDS_REPORT_SIZE (RDS_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
336 #define RDS_REPORT 18
338 /* Report 19: LED state */
339 #define LED_REPORT_SIZE 3
340 #define LED_REPORT 19
342 /* Report 19: stream */
343 #define STREAM_REPORT_SIZE 3
344 #define STREAM_REPORT 19
346 /* Report 20: scratch */
347 #define SCRATCH_PAGE_SIZE 63
348 #define SCRATCH_REPORT_SIZE (SCRATCH_PAGE_SIZE + 1)
349 #define SCRATCH_REPORT 20
351 /* Reports 19-22: flash upgrade of the C8051F321 */
352 #define WRITE_REPORT 19
353 #define FLASH_REPORT 20
354 #define CRC_REPORT 21
355 #define RESPONSE_REPORT 22
357 /* Report 23: currently unused, but can accept 60 byte reports on the HID */
358 /* interrupt out endpoint 2 every 1 millisecond */
359 #define UNUSED_REPORT 23
363 /**************************************************************************
364 * Software/Hardware Versions
365 **************************************************************************/
366 #define RADIO_SW_VERSION_NOT_BOOTLOADABLE 6
367 #define RADIO_SW_VERSION 7
368 #define RADIO_SW_VERSION_CURRENT 15
369 #define RADIO_HW_VERSION 1
371 #define SCRATCH_PAGE_SW_VERSION 1
372 #define SCRATCH_PAGE_HW_VERSION 2
376 /**************************************************************************
377 * LED State Definitions
378 **************************************************************************/
379 #define LED_COMMAND 0x35
381 #define NO_CHANGE_LED 0x00
382 #define ALL_COLOR_LED 0x01 /* streaming state */
383 #define BLINK_GREEN_LED 0x02 /* connect state */
384 #define BLINK_RED_LED 0x04
385 #define BLINK_ORANGE_LED 0x10 /* disconnect state */
386 #define SOLID_GREEN_LED 0x20 /* tuning/seeking state */
387 #define SOLID_RED_LED 0x40 /* bootload state */
388 #define SOLID_ORANGE_LED 0x80
392 /**************************************************************************
393 * Stream State Definitions
394 **************************************************************************/
395 #define STREAM_COMMAND 0x36
396 #define STREAM_VIDPID 0x00
397 #define STREAM_AUDIO 0xff
401 /**************************************************************************
402 * Bootloader / Flash Commands
403 **************************************************************************/
405 /* unique id sent to bootloader and required to put into a bootload state */
406 #define UNIQUE_BL_ID 0x34
408 /* mask for the flash data */
409 #define FLASH_DATA_MASK 0x55
411 /* bootloader commands */
412 #define GET_SW_VERSION_COMMAND 0x00
413 #define SET_PAGE_COMMAND 0x01
414 #define ERASE_PAGE_COMMAND 0x02
415 #define WRITE_PAGE_COMMAND 0x03
416 #define CRC_ON_PAGE_COMMAND 0x04
417 #define READ_FLASH_BYTE_COMMAND 0x05
418 #define RESET_DEVICE_COMMAND 0x06
419 #define GET_HW_VERSION_COMMAND 0x07
422 /* bootloader command responses */
423 #define COMMAND_OK 0x01
424 #define COMMAND_FAILED 0x02
425 #define COMMAND_PENDING 0x03
428 #define COMMAND_BUFFER_SIZE 4
429 #define RESPONSE_BUFFER_SIZE 2
430 #define FLASH_BUFFER_SIZE 64
431 #define CRC_BUFFER_SIZE 3
435 /**************************************************************************
436 * General Driver Definitions
437 **************************************************************************/
440 * si470x_device - private data
442 struct si470x_device {
443 /* reference to USB and video device */
444 struct usb_device *usbdev;
445 struct usb_interface *intf;
446 struct video_device *videodev;
448 /* driver management */
450 unsigned char disconnected;
451 struct mutex disconnect_lock;
453 /* Silabs internal registers (0..15) */
454 unsigned short registers[RADIO_REGISTER_NUM];
456 /* RDS receive buffer */
457 struct delayed_work work;
458 wait_queue_head_t read_queue;
459 struct mutex lock; /* buffer locking */
460 unsigned char *buffer; /* size is always multiple of three */
461 unsigned int buf_size;
462 unsigned int rd_index;
463 unsigned int wr_index;
468 * The frequency is set in units of 62.5 Hz when using V4L2_TUNER_CAP_LOW,
469 * 62.5 kHz otherwise.
470 * The tuner is able to have a channel spacing of 50, 100 or 200 kHz.
471 * tuner->capability is therefore set to V4L2_TUNER_CAP_LOW
472 * The FREQ_MUL is then: 1 MHz / 62.5 Hz = 16000
474 #define FREQ_MUL (1000000 / 62.5)
478 /**************************************************************************
479 * General Driver Functions
480 **************************************************************************/
483 * si470x_get_report - receive a HID report
485 static int si470x_get_report(struct si470x_device *radio, void *buf, int size)
487 unsigned char *report = (unsigned char *) buf;
490 retval = usb_control_msg(radio->usbdev,
491 usb_rcvctrlpipe(radio->usbdev, 0),
493 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
495 buf, size, usb_timeout);
498 printk(KERN_WARNING DRIVER_NAME
499 ": si470x_get_report: usb_control_msg returned %d\n",
506 * si470x_set_report - send a HID report
508 static int si470x_set_report(struct si470x_device *radio, void *buf, int size)
510 unsigned char *report = (unsigned char *) buf;
513 retval = usb_control_msg(radio->usbdev,
514 usb_sndctrlpipe(radio->usbdev, 0),
516 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
518 buf, size, usb_timeout);
521 printk(KERN_WARNING DRIVER_NAME
522 ": si470x_set_report: usb_control_msg returned %d\n",
529 * si470x_get_register - read register
531 static int si470x_get_register(struct si470x_device *radio, int regnr)
533 unsigned char buf[REGISTER_REPORT_SIZE];
536 buf[0] = REGISTER_REPORT(regnr);
538 retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
541 radio->registers[regnr] = get_unaligned_be16(&buf[1]);
543 return (retval < 0) ? -EINVAL : 0;
548 * si470x_set_register - write register
550 static int si470x_set_register(struct si470x_device *radio, int regnr)
552 unsigned char buf[REGISTER_REPORT_SIZE];
555 buf[0] = REGISTER_REPORT(regnr);
556 put_unaligned_be16(radio->registers[regnr], &buf[1]);
558 retval = si470x_set_report(radio, (void *) &buf, sizeof(buf));
560 return (retval < 0) ? -EINVAL : 0;
565 * si470x_get_all_registers - read entire registers
567 static int si470x_get_all_registers(struct si470x_device *radio)
569 unsigned char buf[ENTIRE_REPORT_SIZE];
573 buf[0] = ENTIRE_REPORT;
575 retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
578 for (regnr = 0; regnr < RADIO_REGISTER_NUM; regnr++)
579 radio->registers[regnr] = get_unaligned_be16(
580 &buf[regnr * RADIO_REGISTER_SIZE + 1]);
582 return (retval < 0) ? -EINVAL : 0;
587 * si470x_get_rds_registers - read rds registers
589 static int si470x_get_rds_registers(struct si470x_device *radio)
591 unsigned char buf[RDS_REPORT_SIZE];
598 retval = usb_interrupt_msg(radio->usbdev,
599 usb_rcvintpipe(radio->usbdev, 1),
600 (void *) &buf, sizeof(buf), &size, usb_timeout);
601 if (size != sizeof(buf))
602 printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: "
603 "return size differs: %d != %zu\n", size, sizeof(buf));
605 printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: "
606 "usb_interrupt_msg returned %d\n", retval);
609 for (regnr = 0; regnr < RDS_REGISTER_NUM; regnr++)
610 radio->registers[STATUSRSSI + regnr] =
612 &buf[regnr * RADIO_REGISTER_SIZE + 1]);
614 return (retval < 0) ? -EINVAL : 0;
619 * si470x_set_chan - set the channel
621 static int si470x_set_chan(struct si470x_device *radio, unsigned short chan)
624 unsigned long timeout;
628 radio->registers[CHANNEL] &= ~CHANNEL_CHAN;
629 radio->registers[CHANNEL] |= CHANNEL_TUNE | chan;
630 retval = si470x_set_register(radio, CHANNEL);
634 /* wait till tune operation has completed */
635 timeout = jiffies + msecs_to_jiffies(tune_timeout);
637 retval = si470x_get_register(radio, STATUSRSSI);
640 timed_out = time_after(jiffies, timeout);
641 } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
643 if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
644 printk(KERN_WARNING DRIVER_NAME ": tune does not complete\n");
646 printk(KERN_WARNING DRIVER_NAME
647 ": tune timed out after %u ms\n", tune_timeout);
651 radio->registers[CHANNEL] &= ~CHANNEL_TUNE;
652 retval = si470x_set_register(radio, CHANNEL);
660 * si470x_get_freq - get the frequency
662 static int si470x_get_freq(struct si470x_device *radio, unsigned int *freq)
664 unsigned int spacing, band_bottom;
670 /* 0: 200 kHz (USA, Australia) */
671 case 0 : spacing = 0.200 * FREQ_MUL; break;
672 /* 1: 100 kHz (Europe, Japan) */
673 case 1 : spacing = 0.100 * FREQ_MUL; break;
675 default: spacing = 0.050 * FREQ_MUL; break;
678 /* Bottom of Band (MHz) */
680 /* 0: 87.5 - 108 MHz (USA, Europe) */
681 case 0 : band_bottom = 87.5 * FREQ_MUL; break;
682 /* 1: 76 - 108 MHz (Japan wide band) */
683 default: band_bottom = 76 * FREQ_MUL; break;
684 /* 2: 76 - 90 MHz (Japan) */
685 case 2 : band_bottom = 76 * FREQ_MUL; break;
689 retval = si470x_get_register(radio, READCHAN);
690 chan = radio->registers[READCHAN] & READCHAN_READCHAN;
692 /* Frequency (MHz) = Spacing (kHz) x Channel + Bottom of Band (MHz) */
693 *freq = chan * spacing + band_bottom;
700 * si470x_set_freq - set the frequency
702 static int si470x_set_freq(struct si470x_device *radio, unsigned int freq)
704 unsigned int spacing, band_bottom;
709 /* 0: 200 kHz (USA, Australia) */
710 case 0 : spacing = 0.200 * FREQ_MUL; break;
711 /* 1: 100 kHz (Europe, Japan) */
712 case 1 : spacing = 0.100 * FREQ_MUL; break;
714 default: spacing = 0.050 * FREQ_MUL; break;
717 /* Bottom of Band (MHz) */
719 /* 0: 87.5 - 108 MHz (USA, Europe) */
720 case 0 : band_bottom = 87.5 * FREQ_MUL; break;
721 /* 1: 76 - 108 MHz (Japan wide band) */
722 default: band_bottom = 76 * FREQ_MUL; break;
723 /* 2: 76 - 90 MHz (Japan) */
724 case 2 : band_bottom = 76 * FREQ_MUL; break;
727 /* Chan = [ Freq (Mhz) - Bottom of Band (MHz) ] / Spacing (kHz) */
728 chan = (freq - band_bottom) / spacing;
730 return si470x_set_chan(radio, chan);
735 * si470x_set_seek - set seek
737 static int si470x_set_seek(struct si470x_device *radio,
738 unsigned int wrap_around, unsigned int seek_upward)
741 unsigned long timeout;
745 radio->registers[POWERCFG] |= POWERCFG_SEEK;
746 if (wrap_around == 1)
747 radio->registers[POWERCFG] &= ~POWERCFG_SKMODE;
749 radio->registers[POWERCFG] |= POWERCFG_SKMODE;
750 if (seek_upward == 1)
751 radio->registers[POWERCFG] |= POWERCFG_SEEKUP;
753 radio->registers[POWERCFG] &= ~POWERCFG_SEEKUP;
754 retval = si470x_set_register(radio, POWERCFG);
758 /* wait till seek operation has completed */
759 timeout = jiffies + msecs_to_jiffies(seek_timeout);
761 retval = si470x_get_register(radio, STATUSRSSI);
764 timed_out = time_after(jiffies, timeout);
765 } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
767 if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
768 printk(KERN_WARNING DRIVER_NAME ": seek does not complete\n");
769 if (radio->registers[STATUSRSSI] & STATUSRSSI_SF)
770 printk(KERN_WARNING DRIVER_NAME
771 ": seek failed / band limit reached\n");
773 printk(KERN_WARNING DRIVER_NAME
774 ": seek timed out after %u ms\n", seek_timeout);
778 radio->registers[POWERCFG] &= ~POWERCFG_SEEK;
779 retval = si470x_set_register(radio, POWERCFG);
782 /* try again, if timed out */
783 if ((retval == 0) && timed_out)
791 * si470x_start - switch on radio
793 static int si470x_start(struct si470x_device *radio)
798 radio->registers[POWERCFG] =
799 POWERCFG_DMUTE | POWERCFG_ENABLE | POWERCFG_RDSM;
800 retval = si470x_set_register(radio, POWERCFG);
805 radio->registers[SYSCONFIG1] = SYSCONFIG1_DE;
806 retval = si470x_set_register(radio, SYSCONFIG1);
811 radio->registers[SYSCONFIG2] =
812 (0x3f << 8) | /* SEEKTH */
813 ((band << 6) & SYSCONFIG2_BAND) | /* BAND */
814 ((space << 4) & SYSCONFIG2_SPACE) | /* SPACE */
815 15; /* VOLUME (max) */
816 retval = si470x_set_register(radio, SYSCONFIG2);
820 /* reset last channel */
821 retval = si470x_set_chan(radio,
822 radio->registers[CHANNEL] & CHANNEL_CHAN);
830 * si470x_stop - switch off radio
832 static int si470x_stop(struct si470x_device *radio)
837 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
838 retval = si470x_set_register(radio, SYSCONFIG1);
843 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
844 /* POWERCFG_ENABLE has to automatically go low */
845 radio->registers[POWERCFG] |= POWERCFG_ENABLE | POWERCFG_DISABLE;
846 retval = si470x_set_register(radio, POWERCFG);
854 * si470x_rds_on - switch on rds reception
856 static int si470x_rds_on(struct si470x_device *radio)
861 mutex_lock(&radio->lock);
862 radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDS;
863 retval = si470x_set_register(radio, SYSCONFIG1);
865 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
866 mutex_unlock(&radio->lock);
873 /**************************************************************************
874 * RDS Driver Functions
875 **************************************************************************/
878 * si470x_rds - rds processing function
880 static void si470x_rds(struct si470x_device *radio)
882 unsigned char blocknum;
883 unsigned short bler; /* rds block errors */
885 unsigned char tmpbuf[3];
888 if (si470x_get_rds_registers(radio) < 0)
890 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0) {
891 /* No RDS group ready */
894 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSS) == 0) {
895 /* RDS decoder not synchronized */
899 /* copy all four RDS blocks to internal buffer */
900 mutex_lock(&radio->lock);
901 for (blocknum = 0; blocknum < 4; blocknum++) {
904 bler = (radio->registers[STATUSRSSI] &
905 STATUSRSSI_BLERA) >> 9;
906 rds = radio->registers[RDSA];
909 bler = (radio->registers[READCHAN] &
910 READCHAN_BLERB) >> 14;
911 rds = radio->registers[RDSB];
914 bler = (radio->registers[READCHAN] &
915 READCHAN_BLERC) >> 12;
916 rds = radio->registers[RDSC];
919 bler = (radio->registers[READCHAN] &
920 READCHAN_BLERD) >> 10;
921 rds = radio->registers[RDSD];
925 /* Fill the V4L2 RDS buffer */
926 put_unaligned_le16(rds, &tmpbuf);
927 tmpbuf[2] = blocknum; /* offset name */
928 tmpbuf[2] |= blocknum << 3; /* received offset */
929 if (bler > max_rds_errors)
930 tmpbuf[2] |= 0x80; /* uncorrectable errors */
932 tmpbuf[2] |= 0x40; /* corrected error(s) */
934 /* copy RDS block to internal buffer */
935 memcpy(&radio->buffer[radio->wr_index], &tmpbuf, 3);
936 radio->wr_index += 3;
938 /* wrap write pointer */
939 if (radio->wr_index >= radio->buf_size)
942 /* check for overflow */
943 if (radio->wr_index == radio->rd_index) {
944 /* increment and wrap read pointer */
945 radio->rd_index += 3;
946 if (radio->rd_index >= radio->buf_size)
950 mutex_unlock(&radio->lock);
952 /* wake up read queue */
953 if (radio->wr_index != radio->rd_index)
954 wake_up_interruptible(&radio->read_queue);
959 * si470x_work - rds work function
961 static void si470x_work(struct work_struct *work)
963 struct si470x_device *radio = container_of(work, struct si470x_device,
967 if (radio->disconnected)
969 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
973 schedule_delayed_work(&radio->work, msecs_to_jiffies(rds_poll_time));
978 /**************************************************************************
979 * File Operations Interface
980 **************************************************************************/
983 * si470x_fops_read - read RDS data
985 static ssize_t si470x_fops_read(struct file *file, char __user *buf,
986 size_t count, loff_t *ppos)
988 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
990 unsigned int block_count = 0;
992 /* switch on rds reception */
993 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
994 si470x_rds_on(radio);
995 schedule_delayed_work(&radio->work,
996 msecs_to_jiffies(rds_poll_time));
999 /* block if no new data available */
1000 while (radio->wr_index == radio->rd_index) {
1001 if (file->f_flags & O_NONBLOCK) {
1002 retval = -EWOULDBLOCK;
1005 if (wait_event_interruptible(radio->read_queue,
1006 radio->wr_index != radio->rd_index) < 0) {
1012 /* calculate block count from byte count */
1015 /* copy RDS block out of internal buffer and to user buffer */
1016 mutex_lock(&radio->lock);
1017 while (block_count < count) {
1018 if (radio->rd_index == radio->wr_index)
1021 /* always transfer rds complete blocks */
1022 if (copy_to_user(buf, &radio->buffer[radio->rd_index], 3))
1023 /* retval = -EFAULT; */
1026 /* increment and wrap read pointer */
1027 radio->rd_index += 3;
1028 if (radio->rd_index >= radio->buf_size)
1029 radio->rd_index = 0;
1031 /* increment counters */
1036 mutex_unlock(&radio->lock);
1044 * si470x_fops_poll - poll RDS data
1046 static unsigned int si470x_fops_poll(struct file *file,
1047 struct poll_table_struct *pts)
1049 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1052 /* switch on rds reception */
1053 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
1054 si470x_rds_on(radio);
1055 schedule_delayed_work(&radio->work,
1056 msecs_to_jiffies(rds_poll_time));
1059 poll_wait(file, &radio->read_queue, pts);
1061 if (radio->rd_index != radio->wr_index)
1062 retval = POLLIN | POLLRDNORM;
1069 * si470x_fops_open - file open
1071 static int si470x_fops_open(struct inode *inode, struct file *file)
1073 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1078 retval = usb_autopm_get_interface(radio->intf);
1085 if (radio->users == 1) {
1086 retval = si470x_start(radio);
1088 usb_autopm_put_interface(radio->intf);
1097 * si470x_fops_release - file release
1099 static int si470x_fops_release(struct inode *inode, struct file *file)
1101 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1110 mutex_lock(&radio->disconnect_lock);
1112 if (radio->users == 0) {
1113 if (radio->disconnected) {
1114 video_unregister_device(radio->videodev);
1115 kfree(radio->buffer);
1120 /* stop rds reception */
1121 cancel_delayed_work_sync(&radio->work);
1123 /* cancel read processes */
1124 wake_up_interruptible(&radio->read_queue);
1126 retval = si470x_stop(radio);
1127 usb_autopm_put_interface(radio->intf);
1131 mutex_unlock(&radio->disconnect_lock);
1139 * si470x_fops - file operations interface
1141 static const struct file_operations si470x_fops = {
1142 .owner = THIS_MODULE,
1143 .llseek = no_llseek,
1144 .read = si470x_fops_read,
1145 .poll = si470x_fops_poll,
1146 .ioctl = video_ioctl2,
1147 #ifdef CONFIG_COMPAT
1148 .compat_ioctl = v4l_compat_ioctl32,
1150 .open = si470x_fops_open,
1151 .release = si470x_fops_release,
1156 /**************************************************************************
1157 * Video4Linux Interface
1158 **************************************************************************/
1161 * si470x_v4l2_queryctrl - query control
1163 static struct v4l2_queryctrl si470x_v4l2_queryctrl[] = {
1164 /* HINT: the disabled controls are only here to satify kradio and such apps */
1166 .id = V4L2_CID_AUDIO_VOLUME,
1167 .type = V4L2_CTRL_TYPE_INTEGER,
1172 .default_value = 15,
1175 .id = V4L2_CID_AUDIO_BALANCE,
1176 .flags = V4L2_CTRL_FLAG_DISABLED,
1179 .id = V4L2_CID_AUDIO_BASS,
1180 .flags = V4L2_CTRL_FLAG_DISABLED,
1183 .id = V4L2_CID_AUDIO_TREBLE,
1184 .flags = V4L2_CTRL_FLAG_DISABLED,
1187 .id = V4L2_CID_AUDIO_MUTE,
1188 .type = V4L2_CTRL_TYPE_BOOLEAN,
1196 .id = V4L2_CID_AUDIO_LOUDNESS,
1197 .flags = V4L2_CTRL_FLAG_DISABLED,
1203 * si470x_vidioc_querycap - query device capabilities
1205 static int si470x_vidioc_querycap(struct file *file, void *priv,
1206 struct v4l2_capability *capability)
1208 strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver));
1209 strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
1210 sprintf(capability->bus_info, "USB");
1211 capability->version = DRIVER_KERNEL_VERSION;
1212 capability->capabilities = V4L2_CAP_HW_FREQ_SEEK |
1213 V4L2_CAP_TUNER | V4L2_CAP_RADIO;
1220 * si470x_vidioc_g_input - get input
1222 static int si470x_vidioc_g_input(struct file *file, void *priv,
1232 * si470x_vidioc_s_input - set input
1234 static int si470x_vidioc_s_input(struct file *file, void *priv, unsigned int i)
1243 printk(KERN_WARNING DRIVER_NAME
1244 ": set input failed with %d\n", retval);
1250 * si470x_vidioc_queryctrl - enumerate control items
1252 static int si470x_vidioc_queryctrl(struct file *file, void *priv,
1253 struct v4l2_queryctrl *qc)
1256 int retval = -EINVAL;
1262 for (i = 0; i < ARRAY_SIZE(si470x_v4l2_queryctrl); i++) {
1263 if (qc->id == si470x_v4l2_queryctrl[i].id) {
1264 memcpy(qc, &(si470x_v4l2_queryctrl[i]), sizeof(*qc));
1272 printk(KERN_WARNING DRIVER_NAME
1273 ": query controls failed with %d\n", retval);
1279 * si470x_vidioc_g_ctrl - get the value of a control
1281 static int si470x_vidioc_g_ctrl(struct file *file, void *priv,
1282 struct v4l2_control *ctrl)
1284 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1288 if (radio->disconnected) {
1294 case V4L2_CID_AUDIO_VOLUME:
1295 ctrl->value = radio->registers[SYSCONFIG2] &
1298 case V4L2_CID_AUDIO_MUTE:
1299 ctrl->value = ((radio->registers[POWERCFG] &
1300 POWERCFG_DMUTE) == 0) ? 1 : 0;
1308 printk(KERN_WARNING DRIVER_NAME
1309 ": get control failed with %d\n", retval);
1315 * si470x_vidioc_s_ctrl - set the value of a control
1317 static int si470x_vidioc_s_ctrl(struct file *file, void *priv,
1318 struct v4l2_control *ctrl)
1320 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1324 if (radio->disconnected) {
1330 case V4L2_CID_AUDIO_VOLUME:
1331 radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_VOLUME;
1332 radio->registers[SYSCONFIG2] |= ctrl->value;
1333 retval = si470x_set_register(radio, SYSCONFIG2);
1335 case V4L2_CID_AUDIO_MUTE:
1336 if (ctrl->value == 1)
1337 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
1339 radio->registers[POWERCFG] |= POWERCFG_DMUTE;
1340 retval = si470x_set_register(radio, POWERCFG);
1348 printk(KERN_WARNING DRIVER_NAME
1349 ": set control failed with %d\n", retval);
1355 * si470x_vidioc_g_audio - get audio attributes
1357 static int si470x_vidioc_g_audio(struct file *file, void *priv,
1358 struct v4l2_audio *audio)
1363 if (audio->index != 0) {
1368 strcpy(audio->name, "Radio");
1369 audio->capability = V4L2_AUDCAP_STEREO;
1373 printk(KERN_WARNING DRIVER_NAME
1374 ": get audio failed with %d\n", retval);
1380 * si470x_vidioc_s_audio - set audio attributes
1382 static int si470x_vidioc_s_audio(struct file *file, void *priv,
1383 struct v4l2_audio *audio)
1388 if (audio->index != 0) {
1395 printk(KERN_WARNING DRIVER_NAME
1396 ": set audio failed with %d\n", retval);
1402 * si470x_vidioc_g_tuner - get tuner attributes
1404 static int si470x_vidioc_g_tuner(struct file *file, void *priv,
1405 struct v4l2_tuner *tuner)
1407 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1411 if (radio->disconnected) {
1415 if ((tuner->index != 0) && (tuner->type != V4L2_TUNER_RADIO)) {
1420 retval = si470x_get_register(radio, STATUSRSSI);
1424 strcpy(tuner->name, "FM");
1426 /* 0: 87.5 - 108 MHz (USA, Europe, default) */
1428 tuner->rangelow = 87.5 * FREQ_MUL;
1429 tuner->rangehigh = 108 * FREQ_MUL;
1431 /* 1: 76 - 108 MHz (Japan wide band) */
1433 tuner->rangelow = 76 * FREQ_MUL;
1434 tuner->rangehigh = 108 * FREQ_MUL;
1436 /* 2: 76 - 90 MHz (Japan) */
1438 tuner->rangelow = 76 * FREQ_MUL;
1439 tuner->rangehigh = 90 * FREQ_MUL;
1442 tuner->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
1443 tuner->capability = V4L2_TUNER_CAP_LOW;
1445 /* Stereo indicator == Stereo (instead of Mono) */
1446 if ((radio->registers[STATUSRSSI] & STATUSRSSI_ST) == 1)
1447 tuner->audmode = V4L2_TUNER_MODE_STEREO;
1449 tuner->audmode = V4L2_TUNER_MODE_MONO;
1451 /* min is worst, max is best; signal:0..0xffff; rssi: 0..0xff */
1452 tuner->signal = (radio->registers[STATUSRSSI] & STATUSRSSI_RSSI)
1455 /* automatic frequency control: -1: freq to low, 1 freq to high */
1456 /* AFCRL does only indicate that freq. differs, not if too low/high */
1457 tuner->afc = (radio->registers[STATUSRSSI] & STATUSRSSI_AFCRL) ? 1 : 0;
1461 printk(KERN_WARNING DRIVER_NAME
1462 ": get tuner failed with %d\n", retval);
1468 * si470x_vidioc_s_tuner - set tuner attributes
1470 static int si470x_vidioc_s_tuner(struct file *file, void *priv,
1471 struct v4l2_tuner *tuner)
1473 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1477 if (radio->disconnected) {
1481 if ((tuner->index != 0) && (tuner->type != V4L2_TUNER_RADIO)) {
1486 if (tuner->audmode == V4L2_TUNER_MODE_MONO)
1487 radio->registers[POWERCFG] |= POWERCFG_MONO; /* force mono */
1489 radio->registers[POWERCFG] &= ~POWERCFG_MONO; /* try stereo */
1491 retval = si470x_set_register(radio, POWERCFG);
1495 printk(KERN_WARNING DRIVER_NAME
1496 ": set tuner failed with %d\n", retval);
1502 * si470x_vidioc_g_frequency - get tuner or modulator radio frequency
1504 static int si470x_vidioc_g_frequency(struct file *file, void *priv,
1505 struct v4l2_frequency *freq)
1507 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1511 if (radio->disconnected) {
1515 if ((freq->tuner != 0) && (freq->type != V4L2_TUNER_RADIO)) {
1520 retval = si470x_get_freq(radio, &freq->frequency);
1524 printk(KERN_WARNING DRIVER_NAME
1525 ": get frequency failed with %d\n", retval);
1531 * si470x_vidioc_s_frequency - set tuner or modulator radio frequency
1533 static int si470x_vidioc_s_frequency(struct file *file, void *priv,
1534 struct v4l2_frequency *freq)
1536 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1540 if (radio->disconnected) {
1544 if ((freq->tuner != 0) && (freq->type != V4L2_TUNER_RADIO)) {
1549 retval = si470x_set_freq(radio, freq->frequency);
1553 printk(KERN_WARNING DRIVER_NAME
1554 ": set frequency failed with %d\n", retval);
1560 * si470x_vidioc_s_hw_freq_seek - set hardware frequency seek
1562 static int si470x_vidioc_s_hw_freq_seek(struct file *file, void *priv,
1563 struct v4l2_hw_freq_seek *seek)
1565 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1569 if (radio->disconnected) {
1573 if ((seek->tuner != 0) && (seek->type != V4L2_TUNER_RADIO)) {
1578 retval = si470x_set_seek(radio, seek->wrap_around, seek->seek_upward);
1582 printk(KERN_WARNING DRIVER_NAME
1583 ": set hardware frequency seek failed with %d\n",
1590 * si470x_viddev_tamples - video device interface
1592 static struct video_device si470x_viddev_template = {
1593 .fops = &si470x_fops,
1594 .name = DRIVER_NAME,
1595 .type = VID_TYPE_TUNER,
1596 .release = video_device_release,
1597 .vidioc_querycap = si470x_vidioc_querycap,
1598 .vidioc_g_input = si470x_vidioc_g_input,
1599 .vidioc_s_input = si470x_vidioc_s_input,
1600 .vidioc_queryctrl = si470x_vidioc_queryctrl,
1601 .vidioc_g_ctrl = si470x_vidioc_g_ctrl,
1602 .vidioc_s_ctrl = si470x_vidioc_s_ctrl,
1603 .vidioc_g_audio = si470x_vidioc_g_audio,
1604 .vidioc_s_audio = si470x_vidioc_s_audio,
1605 .vidioc_g_tuner = si470x_vidioc_g_tuner,
1606 .vidioc_s_tuner = si470x_vidioc_s_tuner,
1607 .vidioc_g_frequency = si470x_vidioc_g_frequency,
1608 .vidioc_s_frequency = si470x_vidioc_s_frequency,
1609 .vidioc_s_hw_freq_seek = si470x_vidioc_s_hw_freq_seek,
1610 .owner = THIS_MODULE,
1615 /**************************************************************************
1617 **************************************************************************/
1620 * si470x_usb_driver_probe - probe for the device
1622 static int si470x_usb_driver_probe(struct usb_interface *intf,
1623 const struct usb_device_id *id)
1625 struct si470x_device *radio;
1628 /* private data allocation and initialization */
1629 radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL);
1635 radio->disconnected = 0;
1636 radio->usbdev = interface_to_usbdev(intf);
1638 mutex_init(&radio->disconnect_lock);
1639 mutex_init(&radio->lock);
1641 /* video device allocation and initialization */
1642 radio->videodev = video_device_alloc();
1643 if (!radio->videodev) {
1647 memcpy(radio->videodev, &si470x_viddev_template,
1648 sizeof(si470x_viddev_template));
1649 video_set_drvdata(radio->videodev, radio);
1651 /* show some infos about the specific device */
1652 if (si470x_get_all_registers(radio) < 0) {
1656 printk(KERN_INFO DRIVER_NAME ": DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",
1657 radio->registers[DEVICEID], radio->registers[CHIPID]);
1659 /* check if firmware is current */
1660 if ((radio->registers[CHIPID] & CHIPID_FIRMWARE)
1661 < RADIO_SW_VERSION_CURRENT) {
1662 printk(KERN_WARNING DRIVER_NAME
1663 ": This driver is known to work with "
1664 "firmware version %hu,\n", RADIO_SW_VERSION_CURRENT);
1665 printk(KERN_WARNING DRIVER_NAME
1666 ": but the device has firmware version %hu.\n",
1667 radio->registers[CHIPID] & CHIPID_FIRMWARE);
1668 printk(KERN_WARNING DRIVER_NAME
1669 ": If you have some trouble using this driver,\n");
1670 printk(KERN_WARNING DRIVER_NAME
1671 ": please report to V4L ML at "
1672 "video4linux-list@redhat.com\n");
1675 /* set initial frequency */
1676 si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
1678 /* rds buffer allocation */
1679 radio->buf_size = rds_buf * 3;
1680 radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
1681 if (!radio->buffer) {
1686 /* rds buffer configuration */
1687 radio->wr_index = 0;
1688 radio->rd_index = 0;
1689 init_waitqueue_head(&radio->read_queue);
1691 /* prepare rds work function */
1692 INIT_DELAYED_WORK(&radio->work, si470x_work);
1694 /* register video device */
1695 if (video_register_device(radio->videodev, VFL_TYPE_RADIO, radio_nr)) {
1697 printk(KERN_WARNING DRIVER_NAME
1698 ": Could not register video device\n");
1701 usb_set_intfdata(intf, radio);
1705 video_device_release(radio->videodev);
1706 kfree(radio->buffer);
1715 * si470x_usb_driver_suspend - suspend the device
1717 static int si470x_usb_driver_suspend(struct usb_interface *intf,
1718 pm_message_t message)
1720 struct si470x_device *radio = usb_get_intfdata(intf);
1722 printk(KERN_INFO DRIVER_NAME ": suspending now...\n");
1724 cancel_delayed_work_sync(&radio->work);
1731 * si470x_usb_driver_resume - resume the device
1733 static int si470x_usb_driver_resume(struct usb_interface *intf)
1735 struct si470x_device *radio = usb_get_intfdata(intf);
1737 printk(KERN_INFO DRIVER_NAME ": resuming now...\n");
1739 mutex_lock(&radio->lock);
1740 if (radio->users && radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS)
1741 schedule_delayed_work(&radio->work,
1742 msecs_to_jiffies(rds_poll_time));
1743 mutex_unlock(&radio->lock);
1750 * si470x_usb_driver_disconnect - disconnect the device
1752 static void si470x_usb_driver_disconnect(struct usb_interface *intf)
1754 struct si470x_device *radio = usb_get_intfdata(intf);
1756 mutex_lock(&radio->disconnect_lock);
1757 radio->disconnected = 1;
1758 cancel_delayed_work_sync(&radio->work);
1759 usb_set_intfdata(intf, NULL);
1760 if (radio->users == 0) {
1761 video_unregister_device(radio->videodev);
1762 kfree(radio->buffer);
1765 mutex_unlock(&radio->disconnect_lock);
1770 * si470x_usb_driver - usb driver interface
1772 static struct usb_driver si470x_usb_driver = {
1773 .name = DRIVER_NAME,
1774 .probe = si470x_usb_driver_probe,
1775 .disconnect = si470x_usb_driver_disconnect,
1776 .suspend = si470x_usb_driver_suspend,
1777 .resume = si470x_usb_driver_resume,
1778 .id_table = si470x_usb_driver_id_table,
1779 .supports_autosuspend = 1,
1784 /**************************************************************************
1786 **************************************************************************/
1789 * si470x_module_init - module init
1791 static int __init si470x_module_init(void)
1793 printk(KERN_INFO DRIVER_DESC ", Version " DRIVER_VERSION "\n");
1794 return usb_register(&si470x_usb_driver);
1799 * si470x_module_exit - module exit
1801 static void __exit si470x_module_exit(void)
1803 usb_deregister(&si470x_usb_driver);
1807 module_init(si470x_module_init);
1808 module_exit(si470x_module_exit);
1810 MODULE_LICENSE("GPL");
1811 MODULE_AUTHOR(DRIVER_AUTHOR);
1812 MODULE_DESCRIPTION(DRIVER_DESC);
1813 MODULE_VERSION(DRIVER_VERSION);