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/v4l2-ioctl.h>
137 #include <media/rds.h>
138 #include <asm/unaligned.h>
141 /* USB Device ID List */
142 static struct usb_device_id si470x_usb_driver_id_table[] = {
143 /* Silicon Labs USB FM Radio Reference Design */
144 { USB_DEVICE_AND_INTERFACE_INFO(0x10c4, 0x818a, USB_CLASS_HID, 0, 0) },
145 /* ADS/Tech FM Radio Receiver (formerly Instant FM Music) */
146 { USB_DEVICE_AND_INTERFACE_INFO(0x06e1, 0xa155, USB_CLASS_HID, 0, 0) },
147 /* Terminating entry */
150 MODULE_DEVICE_TABLE(usb, si470x_usb_driver_id_table);
154 /**************************************************************************
156 **************************************************************************/
159 static int radio_nr = -1;
160 module_param(radio_nr, int, 0);
161 MODULE_PARM_DESC(radio_nr, "Radio Nr");
164 /* 0: 200 kHz (USA, Australia) */
165 /* 1: 100 kHz (Europe, Japan) */
167 static unsigned short space = 2;
168 module_param(space, ushort, 0);
169 MODULE_PARM_DESC(radio_nr, "Spacing: 0=200kHz 1=100kHz *2=50kHz*");
171 /* Bottom of Band (MHz) */
172 /* 0: 87.5 - 108 MHz (USA, Europe)*/
173 /* 1: 76 - 108 MHz (Japan wide band) */
174 /* 2: 76 - 90 MHz (Japan) */
175 static unsigned short band = 1;
176 module_param(band, ushort, 0);
177 MODULE_PARM_DESC(radio_nr, "Band: 0=87.5..108MHz *1=76..108MHz* 2=76..90MHz");
181 /* 1: 50 us (Europe, Australia, Japan) */
182 static unsigned short de = 1;
183 module_param(de, ushort, 0);
184 MODULE_PARM_DESC(radio_nr, "De-emphasis: 0=75us *1=50us*");
187 static unsigned int usb_timeout = 500;
188 module_param(usb_timeout, uint, 0);
189 MODULE_PARM_DESC(usb_timeout, "USB timeout (ms): *500*");
192 static unsigned int tune_timeout = 3000;
193 module_param(tune_timeout, uint, 0);
194 MODULE_PARM_DESC(tune_timeout, "Tune timeout: *3000*");
197 static unsigned int seek_timeout = 5000;
198 module_param(seek_timeout, uint, 0);
199 MODULE_PARM_DESC(seek_timeout, "Seek timeout: *5000*");
201 /* RDS buffer blocks */
202 static unsigned int rds_buf = 100;
203 module_param(rds_buf, uint, 0);
204 MODULE_PARM_DESC(rds_buf, "RDS buffer entries: *100*");
206 /* RDS maximum block errors */
207 static unsigned short max_rds_errors = 1;
208 /* 0 means 0 errors requiring correction */
209 /* 1 means 1-2 errors requiring correction (used by original USBRadio.exe) */
210 /* 2 means 3-5 errors requiring correction */
211 /* 3 means 6+ errors or errors in checkword, correction not possible */
212 module_param(max_rds_errors, ushort, 0);
213 MODULE_PARM_DESC(max_rds_errors, "RDS maximum block errors: *1*");
215 /* RDS poll frequency */
216 static unsigned int rds_poll_time = 40;
217 /* 40 is used by the original USBRadio.exe */
218 /* 50 is used by radio-cadet */
219 /* 75 should be okay */
220 /* 80 is the usual RDS receive interval */
221 module_param(rds_poll_time, uint, 0);
222 MODULE_PARM_DESC(rds_poll_time, "RDS poll time (ms): *40*");
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 */
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 */
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 */
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 */
253 #define CHANNEL 3 /* Channel */
254 #define CHANNEL_TUNE 0x8000 /* bits 15..15: Tune */
255 #define CHANNEL_CHAN 0x03ff /* bits 09..00: Channel Select */
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 */
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 */
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 */
280 #define TEST1 7 /* Test 1 */
281 #define TEST1_AHIZEN 0x4000 /* bits 14..14: Audio High-Z Enable */
283 #define TEST2 8 /* Test 2 */
284 /* TEST2 only contains reserved bits */
286 #define BOOTCONFIG 9 /* Boot Configuration */
287 /* BOOTCONFIG only contains reserved bits */
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) */
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 */
305 #define RDSA 12 /* RDSA */
306 #define RDSA_RDSA 0xffff /* bits 15..00: RDS Block A Data (Si4701 only) */
308 #define RDSB 13 /* RDSB */
309 #define RDSB_RDSB 0xffff /* bits 15..00: RDS Block B Data (Si4701 only) */
311 #define RDSC 14 /* RDSC */
312 #define RDSC_RDSC 0xffff /* bits 15..00: RDS Block C Data (Si4701 only) */
314 #define RDSD 15 /* RDSD */
315 #define RDSD_RDSD 0xffff /* bits 15..00: RDS Block D Data (Si4701 only) */
319 /**************************************************************************
321 **************************************************************************/
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)
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
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
339 /* Report 19: LED state */
340 #define LED_REPORT_SIZE 3
341 #define LED_REPORT 19
343 /* Report 19: stream */
344 #define STREAM_REPORT_SIZE 3
345 #define STREAM_REPORT 19
347 /* Report 20: scratch */
348 #define SCRATCH_PAGE_SIZE 63
349 #define SCRATCH_REPORT_SIZE (SCRATCH_PAGE_SIZE + 1)
350 #define SCRATCH_REPORT 20
352 /* Reports 19-22: flash upgrade of the C8051F321 */
353 #define WRITE_REPORT 19
354 #define FLASH_REPORT 20
355 #define CRC_REPORT 21
356 #define RESPONSE_REPORT 22
358 /* Report 23: currently unused, but can accept 60 byte reports on the HID */
359 /* interrupt out endpoint 2 every 1 millisecond */
360 #define UNUSED_REPORT 23
364 /**************************************************************************
365 * Software/Hardware Versions
366 **************************************************************************/
367 #define RADIO_SW_VERSION_NOT_BOOTLOADABLE 6
368 #define RADIO_SW_VERSION 7
369 #define RADIO_SW_VERSION_CURRENT 15
370 #define RADIO_HW_VERSION 1
372 #define SCRATCH_PAGE_SW_VERSION 1
373 #define SCRATCH_PAGE_HW_VERSION 2
377 /**************************************************************************
378 * LED State Definitions
379 **************************************************************************/
380 #define LED_COMMAND 0x35
382 #define NO_CHANGE_LED 0x00
383 #define ALL_COLOR_LED 0x01 /* streaming state */
384 #define BLINK_GREEN_LED 0x02 /* connect state */
385 #define BLINK_RED_LED 0x04
386 #define BLINK_ORANGE_LED 0x10 /* disconnect state */
387 #define SOLID_GREEN_LED 0x20 /* tuning/seeking state */
388 #define SOLID_RED_LED 0x40 /* bootload state */
389 #define SOLID_ORANGE_LED 0x80
393 /**************************************************************************
394 * Stream State Definitions
395 **************************************************************************/
396 #define STREAM_COMMAND 0x36
397 #define STREAM_VIDPID 0x00
398 #define STREAM_AUDIO 0xff
402 /**************************************************************************
403 * Bootloader / Flash Commands
404 **************************************************************************/
406 /* unique id sent to bootloader and required to put into a bootload state */
407 #define UNIQUE_BL_ID 0x34
409 /* mask for the flash data */
410 #define FLASH_DATA_MASK 0x55
412 /* bootloader commands */
413 #define GET_SW_VERSION_COMMAND 0x00
414 #define SET_PAGE_COMMAND 0x01
415 #define ERASE_PAGE_COMMAND 0x02
416 #define WRITE_PAGE_COMMAND 0x03
417 #define CRC_ON_PAGE_COMMAND 0x04
418 #define READ_FLASH_BYTE_COMMAND 0x05
419 #define RESET_DEVICE_COMMAND 0x06
420 #define GET_HW_VERSION_COMMAND 0x07
423 /* bootloader command responses */
424 #define COMMAND_OK 0x01
425 #define COMMAND_FAILED 0x02
426 #define COMMAND_PENDING 0x03
429 #define COMMAND_BUFFER_SIZE 4
430 #define RESPONSE_BUFFER_SIZE 2
431 #define FLASH_BUFFER_SIZE 64
432 #define CRC_BUFFER_SIZE 3
436 /**************************************************************************
437 * General Driver Definitions
438 **************************************************************************/
441 * si470x_device - private data
443 struct si470x_device {
444 /* reference to USB and video device */
445 struct usb_device *usbdev;
446 struct usb_interface *intf;
447 struct video_device *videodev;
449 /* driver management */
451 unsigned char disconnected;
452 struct mutex disconnect_lock;
454 /* Silabs internal registers (0..15) */
455 unsigned short registers[RADIO_REGISTER_NUM];
457 /* RDS receive buffer */
458 struct delayed_work work;
459 wait_queue_head_t read_queue;
460 struct mutex lock; /* buffer locking */
461 unsigned char *buffer; /* size is always multiple of three */
462 unsigned int buf_size;
463 unsigned int rd_index;
464 unsigned int wr_index;
469 * The frequency is set in units of 62.5 Hz when using V4L2_TUNER_CAP_LOW,
470 * 62.5 kHz otherwise.
471 * The tuner is able to have a channel spacing of 50, 100 or 200 kHz.
472 * tuner->capability is therefore set to V4L2_TUNER_CAP_LOW
473 * The FREQ_MUL is then: 1 MHz / 62.5 Hz = 16000
475 #define FREQ_MUL (1000000 / 62.5)
479 /**************************************************************************
480 * General Driver Functions
481 **************************************************************************/
484 * si470x_get_report - receive a HID report
486 static int si470x_get_report(struct si470x_device *radio, void *buf, int size)
488 unsigned char *report = (unsigned char *) buf;
491 retval = usb_control_msg(radio->usbdev,
492 usb_rcvctrlpipe(radio->usbdev, 0),
494 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
496 buf, size, usb_timeout);
499 printk(KERN_WARNING DRIVER_NAME
500 ": si470x_get_report: usb_control_msg returned %d\n",
507 * si470x_set_report - send a HID report
509 static int si470x_set_report(struct si470x_device *radio, void *buf, int size)
511 unsigned char *report = (unsigned char *) buf;
514 retval = usb_control_msg(radio->usbdev,
515 usb_sndctrlpipe(radio->usbdev, 0),
517 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
519 buf, size, usb_timeout);
522 printk(KERN_WARNING DRIVER_NAME
523 ": si470x_set_report: usb_control_msg returned %d\n",
530 * si470x_get_register - read register
532 static int si470x_get_register(struct si470x_device *radio, int regnr)
534 unsigned char buf[REGISTER_REPORT_SIZE];
537 buf[0] = REGISTER_REPORT(regnr);
539 retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
542 radio->registers[regnr] = get_unaligned_be16(&buf[1]);
544 return (retval < 0) ? -EINVAL : 0;
549 * si470x_set_register - write register
551 static int si470x_set_register(struct si470x_device *radio, int regnr)
553 unsigned char buf[REGISTER_REPORT_SIZE];
556 buf[0] = REGISTER_REPORT(regnr);
557 put_unaligned_be16(radio->registers[regnr], &buf[1]);
559 retval = si470x_set_report(radio, (void *) &buf, sizeof(buf));
561 return (retval < 0) ? -EINVAL : 0;
566 * si470x_get_all_registers - read entire registers
568 static int si470x_get_all_registers(struct si470x_device *radio)
570 unsigned char buf[ENTIRE_REPORT_SIZE];
574 buf[0] = ENTIRE_REPORT;
576 retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
579 for (regnr = 0; regnr < RADIO_REGISTER_NUM; regnr++)
580 radio->registers[regnr] = get_unaligned_be16(
581 &buf[regnr * RADIO_REGISTER_SIZE + 1]);
583 return (retval < 0) ? -EINVAL : 0;
588 * si470x_get_rds_registers - read rds registers
590 static int si470x_get_rds_registers(struct si470x_device *radio)
592 unsigned char buf[RDS_REPORT_SIZE];
599 retval = usb_interrupt_msg(radio->usbdev,
600 usb_rcvintpipe(radio->usbdev, 1),
601 (void *) &buf, sizeof(buf), &size, usb_timeout);
602 if (size != sizeof(buf))
603 printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: "
604 "return size differs: %d != %zu\n", size, sizeof(buf));
606 printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: "
607 "usb_interrupt_msg returned %d\n", retval);
610 for (regnr = 0; regnr < RDS_REGISTER_NUM; regnr++)
611 radio->registers[STATUSRSSI + regnr] =
613 &buf[regnr * RADIO_REGISTER_SIZE + 1]);
615 return (retval < 0) ? -EINVAL : 0;
620 * si470x_set_chan - set the channel
622 static int si470x_set_chan(struct si470x_device *radio, unsigned short chan)
625 unsigned long timeout;
629 radio->registers[CHANNEL] &= ~CHANNEL_CHAN;
630 radio->registers[CHANNEL] |= CHANNEL_TUNE | chan;
631 retval = si470x_set_register(radio, CHANNEL);
635 /* wait till tune operation has completed */
636 timeout = jiffies + msecs_to_jiffies(tune_timeout);
638 retval = si470x_get_register(radio, STATUSRSSI);
641 timed_out = time_after(jiffies, timeout);
642 } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
644 if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
645 printk(KERN_WARNING DRIVER_NAME ": tune does not complete\n");
647 printk(KERN_WARNING DRIVER_NAME
648 ": tune timed out after %u ms\n", tune_timeout);
652 radio->registers[CHANNEL] &= ~CHANNEL_TUNE;
653 retval = si470x_set_register(radio, CHANNEL);
661 * si470x_get_freq - get the frequency
663 static int si470x_get_freq(struct si470x_device *radio, unsigned int *freq)
665 unsigned int spacing, band_bottom;
671 /* 0: 200 kHz (USA, Australia) */
672 case 0 : spacing = 0.200 * FREQ_MUL; break;
673 /* 1: 100 kHz (Europe, Japan) */
674 case 1 : spacing = 0.100 * FREQ_MUL; break;
676 default: spacing = 0.050 * FREQ_MUL; break;
679 /* Bottom of Band (MHz) */
681 /* 0: 87.5 - 108 MHz (USA, Europe) */
682 case 0 : band_bottom = 87.5 * FREQ_MUL; break;
683 /* 1: 76 - 108 MHz (Japan wide band) */
684 default: band_bottom = 76 * FREQ_MUL; break;
685 /* 2: 76 - 90 MHz (Japan) */
686 case 2 : band_bottom = 76 * FREQ_MUL; break;
690 retval = si470x_get_register(radio, READCHAN);
691 chan = radio->registers[READCHAN] & READCHAN_READCHAN;
693 /* Frequency (MHz) = Spacing (kHz) x Channel + Bottom of Band (MHz) */
694 *freq = chan * spacing + band_bottom;
701 * si470x_set_freq - set the frequency
703 static int si470x_set_freq(struct si470x_device *radio, unsigned int freq)
705 unsigned int spacing, band_bottom;
710 /* 0: 200 kHz (USA, Australia) */
711 case 0 : spacing = 0.200 * FREQ_MUL; break;
712 /* 1: 100 kHz (Europe, Japan) */
713 case 1 : spacing = 0.100 * FREQ_MUL; break;
715 default: spacing = 0.050 * FREQ_MUL; break;
718 /* Bottom of Band (MHz) */
720 /* 0: 87.5 - 108 MHz (USA, Europe) */
721 case 0 : band_bottom = 87.5 * FREQ_MUL; break;
722 /* 1: 76 - 108 MHz (Japan wide band) */
723 default: band_bottom = 76 * FREQ_MUL; break;
724 /* 2: 76 - 90 MHz (Japan) */
725 case 2 : band_bottom = 76 * FREQ_MUL; break;
728 /* Chan = [ Freq (Mhz) - Bottom of Band (MHz) ] / Spacing (kHz) */
729 chan = (freq - band_bottom) / spacing;
731 return si470x_set_chan(radio, chan);
736 * si470x_set_seek - set seek
738 static int si470x_set_seek(struct si470x_device *radio,
739 unsigned int wrap_around, unsigned int seek_upward)
742 unsigned long timeout;
746 radio->registers[POWERCFG] |= POWERCFG_SEEK;
747 if (wrap_around == 1)
748 radio->registers[POWERCFG] &= ~POWERCFG_SKMODE;
750 radio->registers[POWERCFG] |= POWERCFG_SKMODE;
751 if (seek_upward == 1)
752 radio->registers[POWERCFG] |= POWERCFG_SEEKUP;
754 radio->registers[POWERCFG] &= ~POWERCFG_SEEKUP;
755 retval = si470x_set_register(radio, POWERCFG);
759 /* wait till seek operation has completed */
760 timeout = jiffies + msecs_to_jiffies(seek_timeout);
762 retval = si470x_get_register(radio, STATUSRSSI);
765 timed_out = time_after(jiffies, timeout);
766 } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
768 if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
769 printk(KERN_WARNING DRIVER_NAME ": seek does not complete\n");
770 if (radio->registers[STATUSRSSI] & STATUSRSSI_SF)
771 printk(KERN_WARNING DRIVER_NAME
772 ": seek failed / band limit reached\n");
774 printk(KERN_WARNING DRIVER_NAME
775 ": seek timed out after %u ms\n", seek_timeout);
779 radio->registers[POWERCFG] &= ~POWERCFG_SEEK;
780 retval = si470x_set_register(radio, POWERCFG);
783 /* try again, if timed out */
784 if ((retval == 0) && timed_out)
792 * si470x_start - switch on radio
794 static int si470x_start(struct si470x_device *radio)
799 radio->registers[POWERCFG] =
800 POWERCFG_DMUTE | POWERCFG_ENABLE | POWERCFG_RDSM;
801 retval = si470x_set_register(radio, POWERCFG);
806 radio->registers[SYSCONFIG1] = SYSCONFIG1_DE;
807 retval = si470x_set_register(radio, SYSCONFIG1);
812 radio->registers[SYSCONFIG2] =
813 (0x3f << 8) | /* SEEKTH */
814 ((band << 6) & SYSCONFIG2_BAND) | /* BAND */
815 ((space << 4) & SYSCONFIG2_SPACE) | /* SPACE */
816 15; /* VOLUME (max) */
817 retval = si470x_set_register(radio, SYSCONFIG2);
821 /* reset last channel */
822 retval = si470x_set_chan(radio,
823 radio->registers[CHANNEL] & CHANNEL_CHAN);
831 * si470x_stop - switch off radio
833 static int si470x_stop(struct si470x_device *radio)
838 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
839 retval = si470x_set_register(radio, SYSCONFIG1);
844 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
845 /* POWERCFG_ENABLE has to automatically go low */
846 radio->registers[POWERCFG] |= POWERCFG_ENABLE | POWERCFG_DISABLE;
847 retval = si470x_set_register(radio, POWERCFG);
855 * si470x_rds_on - switch on rds reception
857 static int si470x_rds_on(struct si470x_device *radio)
862 mutex_lock(&radio->lock);
863 radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDS;
864 retval = si470x_set_register(radio, SYSCONFIG1);
866 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
867 mutex_unlock(&radio->lock);
874 /**************************************************************************
875 * RDS Driver Functions
876 **************************************************************************/
879 * si470x_rds - rds processing function
881 static void si470x_rds(struct si470x_device *radio)
883 unsigned char blocknum;
884 unsigned short bler; /* rds block errors */
886 unsigned char tmpbuf[3];
889 if (si470x_get_rds_registers(radio) < 0)
891 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0) {
892 /* No RDS group ready */
895 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSS) == 0) {
896 /* RDS decoder not synchronized */
900 /* copy all four RDS blocks to internal buffer */
901 mutex_lock(&radio->lock);
902 for (blocknum = 0; blocknum < 4; blocknum++) {
905 bler = (radio->registers[STATUSRSSI] &
906 STATUSRSSI_BLERA) >> 9;
907 rds = radio->registers[RDSA];
910 bler = (radio->registers[READCHAN] &
911 READCHAN_BLERB) >> 14;
912 rds = radio->registers[RDSB];
915 bler = (radio->registers[READCHAN] &
916 READCHAN_BLERC) >> 12;
917 rds = radio->registers[RDSC];
920 bler = (radio->registers[READCHAN] &
921 READCHAN_BLERD) >> 10;
922 rds = radio->registers[RDSD];
926 /* Fill the V4L2 RDS buffer */
927 put_unaligned_le16(rds, &tmpbuf);
928 tmpbuf[2] = blocknum; /* offset name */
929 tmpbuf[2] |= blocknum << 3; /* received offset */
930 if (bler > max_rds_errors)
931 tmpbuf[2] |= 0x80; /* uncorrectable errors */
933 tmpbuf[2] |= 0x40; /* corrected error(s) */
935 /* copy RDS block to internal buffer */
936 memcpy(&radio->buffer[radio->wr_index], &tmpbuf, 3);
937 radio->wr_index += 3;
939 /* wrap write pointer */
940 if (radio->wr_index >= radio->buf_size)
943 /* check for overflow */
944 if (radio->wr_index == radio->rd_index) {
945 /* increment and wrap read pointer */
946 radio->rd_index += 3;
947 if (radio->rd_index >= radio->buf_size)
951 mutex_unlock(&radio->lock);
953 /* wake up read queue */
954 if (radio->wr_index != radio->rd_index)
955 wake_up_interruptible(&radio->read_queue);
960 * si470x_work - rds work function
962 static void si470x_work(struct work_struct *work)
964 struct si470x_device *radio = container_of(work, struct si470x_device,
968 if (radio->disconnected)
970 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
974 schedule_delayed_work(&radio->work, msecs_to_jiffies(rds_poll_time));
979 /**************************************************************************
980 * File Operations Interface
981 **************************************************************************/
984 * si470x_fops_read - read RDS data
986 static ssize_t si470x_fops_read(struct file *file, char __user *buf,
987 size_t count, loff_t *ppos)
989 struct si470x_device *radio = video_drvdata(file);
991 unsigned int block_count = 0;
993 /* switch on rds reception */
994 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
995 si470x_rds_on(radio);
996 schedule_delayed_work(&radio->work,
997 msecs_to_jiffies(rds_poll_time));
1000 /* block if no new data available */
1001 while (radio->wr_index == radio->rd_index) {
1002 if (file->f_flags & O_NONBLOCK) {
1003 retval = -EWOULDBLOCK;
1006 if (wait_event_interruptible(radio->read_queue,
1007 radio->wr_index != radio->rd_index) < 0) {
1013 /* calculate block count from byte count */
1016 /* copy RDS block out of internal buffer and to user buffer */
1017 mutex_lock(&radio->lock);
1018 while (block_count < count) {
1019 if (radio->rd_index == radio->wr_index)
1022 /* always transfer rds complete blocks */
1023 if (copy_to_user(buf, &radio->buffer[radio->rd_index], 3))
1024 /* retval = -EFAULT; */
1027 /* increment and wrap read pointer */
1028 radio->rd_index += 3;
1029 if (radio->rd_index >= radio->buf_size)
1030 radio->rd_index = 0;
1032 /* increment counters */
1037 mutex_unlock(&radio->lock);
1045 * si470x_fops_poll - poll RDS data
1047 static unsigned int si470x_fops_poll(struct file *file,
1048 struct poll_table_struct *pts)
1050 struct si470x_device *radio = video_drvdata(file);
1053 /* switch on rds reception */
1054 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
1055 si470x_rds_on(radio);
1056 schedule_delayed_work(&radio->work,
1057 msecs_to_jiffies(rds_poll_time));
1060 poll_wait(file, &radio->read_queue, pts);
1062 if (radio->rd_index != radio->wr_index)
1063 retval = POLLIN | POLLRDNORM;
1070 * si470x_fops_open - file open
1072 static int si470x_fops_open(struct inode *inode, struct file *file)
1074 struct si470x_device *radio = video_drvdata(file);
1080 retval = usb_autopm_get_interface(radio->intf);
1087 if (radio->users == 1) {
1088 retval = si470x_start(radio);
1090 usb_autopm_put_interface(radio->intf);
1100 * si470x_fops_release - file release
1102 static int si470x_fops_release(struct inode *inode, struct file *file)
1104 struct si470x_device *radio = video_drvdata(file);
1113 mutex_lock(&radio->disconnect_lock);
1115 if (radio->users == 0) {
1116 if (radio->disconnected) {
1117 video_unregister_device(radio->videodev);
1118 kfree(radio->buffer);
1123 /* stop rds reception */
1124 cancel_delayed_work_sync(&radio->work);
1126 /* cancel read processes */
1127 wake_up_interruptible(&radio->read_queue);
1129 retval = si470x_stop(radio);
1130 usb_autopm_put_interface(radio->intf);
1134 mutex_unlock(&radio->disconnect_lock);
1142 * si470x_fops - file operations interface
1144 static const struct file_operations si470x_fops = {
1145 .owner = THIS_MODULE,
1146 .llseek = no_llseek,
1147 .read = si470x_fops_read,
1148 .poll = si470x_fops_poll,
1149 .ioctl = video_ioctl2,
1150 #ifdef CONFIG_COMPAT
1151 .compat_ioctl = v4l_compat_ioctl32,
1153 .open = si470x_fops_open,
1154 .release = si470x_fops_release,
1159 /**************************************************************************
1160 * Video4Linux Interface
1161 **************************************************************************/
1164 * si470x_v4l2_queryctrl - query control
1166 static struct v4l2_queryctrl si470x_v4l2_queryctrl[] = {
1167 /* HINT: the disabled controls are only here to satify kradio and such apps */
1169 .id = V4L2_CID_AUDIO_VOLUME,
1170 .type = V4L2_CTRL_TYPE_INTEGER,
1175 .default_value = 15,
1178 .id = V4L2_CID_AUDIO_BALANCE,
1179 .flags = V4L2_CTRL_FLAG_DISABLED,
1182 .id = V4L2_CID_AUDIO_BASS,
1183 .flags = V4L2_CTRL_FLAG_DISABLED,
1186 .id = V4L2_CID_AUDIO_TREBLE,
1187 .flags = V4L2_CTRL_FLAG_DISABLED,
1190 .id = V4L2_CID_AUDIO_MUTE,
1191 .type = V4L2_CTRL_TYPE_BOOLEAN,
1199 .id = V4L2_CID_AUDIO_LOUDNESS,
1200 .flags = V4L2_CTRL_FLAG_DISABLED,
1206 * si470x_vidioc_querycap - query device capabilities
1208 static int si470x_vidioc_querycap(struct file *file, void *priv,
1209 struct v4l2_capability *capability)
1211 strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver));
1212 strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
1213 sprintf(capability->bus_info, "USB");
1214 capability->version = DRIVER_KERNEL_VERSION;
1215 capability->capabilities = V4L2_CAP_HW_FREQ_SEEK |
1216 V4L2_CAP_TUNER | V4L2_CAP_RADIO;
1223 * si470x_vidioc_g_input - get input
1225 static int si470x_vidioc_g_input(struct file *file, void *priv,
1235 * si470x_vidioc_s_input - set input
1237 static int si470x_vidioc_s_input(struct file *file, void *priv, unsigned int i)
1246 printk(KERN_WARNING DRIVER_NAME
1247 ": set input failed with %d\n", retval);
1253 * si470x_vidioc_queryctrl - enumerate control items
1255 static int si470x_vidioc_queryctrl(struct file *file, void *priv,
1256 struct v4l2_queryctrl *qc)
1259 int retval = -EINVAL;
1265 for (i = 0; i < ARRAY_SIZE(si470x_v4l2_queryctrl); i++) {
1266 if (qc->id == si470x_v4l2_queryctrl[i].id) {
1267 memcpy(qc, &(si470x_v4l2_queryctrl[i]), sizeof(*qc));
1275 printk(KERN_WARNING DRIVER_NAME
1276 ": query controls failed with %d\n", retval);
1282 * si470x_vidioc_g_ctrl - get the value of a control
1284 static int si470x_vidioc_g_ctrl(struct file *file, void *priv,
1285 struct v4l2_control *ctrl)
1287 struct si470x_device *radio = video_drvdata(file);
1291 if (radio->disconnected) {
1297 case V4L2_CID_AUDIO_VOLUME:
1298 ctrl->value = radio->registers[SYSCONFIG2] &
1301 case V4L2_CID_AUDIO_MUTE:
1302 ctrl->value = ((radio->registers[POWERCFG] &
1303 POWERCFG_DMUTE) == 0) ? 1 : 0;
1311 printk(KERN_WARNING DRIVER_NAME
1312 ": get control failed with %d\n", retval);
1318 * si470x_vidioc_s_ctrl - set the value of a control
1320 static int si470x_vidioc_s_ctrl(struct file *file, void *priv,
1321 struct v4l2_control *ctrl)
1323 struct si470x_device *radio = video_drvdata(file);
1327 if (radio->disconnected) {
1333 case V4L2_CID_AUDIO_VOLUME:
1334 radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_VOLUME;
1335 radio->registers[SYSCONFIG2] |= ctrl->value;
1336 retval = si470x_set_register(radio, SYSCONFIG2);
1338 case V4L2_CID_AUDIO_MUTE:
1339 if (ctrl->value == 1)
1340 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
1342 radio->registers[POWERCFG] |= POWERCFG_DMUTE;
1343 retval = si470x_set_register(radio, POWERCFG);
1351 printk(KERN_WARNING DRIVER_NAME
1352 ": set control failed with %d\n", retval);
1358 * si470x_vidioc_g_audio - get audio attributes
1360 static int si470x_vidioc_g_audio(struct file *file, void *priv,
1361 struct v4l2_audio *audio)
1366 if (audio->index != 0) {
1371 strcpy(audio->name, "Radio");
1372 audio->capability = V4L2_AUDCAP_STEREO;
1376 printk(KERN_WARNING DRIVER_NAME
1377 ": get audio failed with %d\n", retval);
1383 * si470x_vidioc_s_audio - set audio attributes
1385 static int si470x_vidioc_s_audio(struct file *file, void *priv,
1386 struct v4l2_audio *audio)
1391 if (audio->index != 0) {
1398 printk(KERN_WARNING DRIVER_NAME
1399 ": set audio failed with %d\n", retval);
1405 * si470x_vidioc_g_tuner - get tuner attributes
1407 static int si470x_vidioc_g_tuner(struct file *file, void *priv,
1408 struct v4l2_tuner *tuner)
1410 struct si470x_device *radio = video_drvdata(file);
1414 if (radio->disconnected) {
1418 if ((tuner->index != 0) && (tuner->type != V4L2_TUNER_RADIO)) {
1423 retval = si470x_get_register(radio, STATUSRSSI);
1427 strcpy(tuner->name, "FM");
1429 /* 0: 87.5 - 108 MHz (USA, Europe, default) */
1431 tuner->rangelow = 87.5 * FREQ_MUL;
1432 tuner->rangehigh = 108 * FREQ_MUL;
1434 /* 1: 76 - 108 MHz (Japan wide band) */
1436 tuner->rangelow = 76 * FREQ_MUL;
1437 tuner->rangehigh = 108 * FREQ_MUL;
1439 /* 2: 76 - 90 MHz (Japan) */
1441 tuner->rangelow = 76 * FREQ_MUL;
1442 tuner->rangehigh = 90 * FREQ_MUL;
1445 tuner->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
1446 tuner->capability = V4L2_TUNER_CAP_LOW;
1448 /* Stereo indicator == Stereo (instead of Mono) */
1449 if ((radio->registers[STATUSRSSI] & STATUSRSSI_ST) == 1)
1450 tuner->audmode = V4L2_TUNER_MODE_STEREO;
1452 tuner->audmode = V4L2_TUNER_MODE_MONO;
1454 /* min is worst, max is best; signal:0..0xffff; rssi: 0..0xff */
1455 tuner->signal = (radio->registers[STATUSRSSI] & STATUSRSSI_RSSI)
1458 /* automatic frequency control: -1: freq to low, 1 freq to high */
1459 /* AFCRL does only indicate that freq. differs, not if too low/high */
1460 tuner->afc = (radio->registers[STATUSRSSI] & STATUSRSSI_AFCRL) ? 1 : 0;
1464 printk(KERN_WARNING DRIVER_NAME
1465 ": get tuner failed with %d\n", retval);
1471 * si470x_vidioc_s_tuner - set tuner attributes
1473 static int si470x_vidioc_s_tuner(struct file *file, void *priv,
1474 struct v4l2_tuner *tuner)
1476 struct si470x_device *radio = video_drvdata(file);
1480 if (radio->disconnected) {
1484 if ((tuner->index != 0) && (tuner->type != V4L2_TUNER_RADIO)) {
1489 if (tuner->audmode == V4L2_TUNER_MODE_MONO)
1490 radio->registers[POWERCFG] |= POWERCFG_MONO; /* force mono */
1492 radio->registers[POWERCFG] &= ~POWERCFG_MONO; /* try stereo */
1494 retval = si470x_set_register(radio, POWERCFG);
1498 printk(KERN_WARNING DRIVER_NAME
1499 ": set tuner failed with %d\n", retval);
1505 * si470x_vidioc_g_frequency - get tuner or modulator radio frequency
1507 static int si470x_vidioc_g_frequency(struct file *file, void *priv,
1508 struct v4l2_frequency *freq)
1510 struct si470x_device *radio = video_drvdata(file);
1514 if (radio->disconnected) {
1518 if ((freq->tuner != 0) && (freq->type != V4L2_TUNER_RADIO)) {
1523 retval = si470x_get_freq(radio, &freq->frequency);
1527 printk(KERN_WARNING DRIVER_NAME
1528 ": get frequency failed with %d\n", retval);
1534 * si470x_vidioc_s_frequency - set tuner or modulator radio frequency
1536 static int si470x_vidioc_s_frequency(struct file *file, void *priv,
1537 struct v4l2_frequency *freq)
1539 struct si470x_device *radio = video_drvdata(file);
1543 if (radio->disconnected) {
1547 if ((freq->tuner != 0) && (freq->type != V4L2_TUNER_RADIO)) {
1552 retval = si470x_set_freq(radio, freq->frequency);
1556 printk(KERN_WARNING DRIVER_NAME
1557 ": set frequency failed with %d\n", retval);
1563 * si470x_vidioc_s_hw_freq_seek - set hardware frequency seek
1565 static int si470x_vidioc_s_hw_freq_seek(struct file *file, void *priv,
1566 struct v4l2_hw_freq_seek *seek)
1568 struct si470x_device *radio = video_drvdata(file);
1572 if (radio->disconnected) {
1576 if ((seek->tuner != 0) && (seek->type != V4L2_TUNER_RADIO)) {
1581 retval = si470x_set_seek(radio, seek->wrap_around, seek->seek_upward);
1585 printk(KERN_WARNING DRIVER_NAME
1586 ": set hardware frequency seek failed with %d\n",
1591 static const struct v4l2_ioctl_ops si470x_ioctl_ops = {
1592 .vidioc_querycap = si470x_vidioc_querycap,
1593 .vidioc_g_input = si470x_vidioc_g_input,
1594 .vidioc_s_input = si470x_vidioc_s_input,
1595 .vidioc_queryctrl = si470x_vidioc_queryctrl,
1596 .vidioc_g_ctrl = si470x_vidioc_g_ctrl,
1597 .vidioc_s_ctrl = si470x_vidioc_s_ctrl,
1598 .vidioc_g_audio = si470x_vidioc_g_audio,
1599 .vidioc_s_audio = si470x_vidioc_s_audio,
1600 .vidioc_g_tuner = si470x_vidioc_g_tuner,
1601 .vidioc_s_tuner = si470x_vidioc_s_tuner,
1602 .vidioc_g_frequency = si470x_vidioc_g_frequency,
1603 .vidioc_s_frequency = si470x_vidioc_s_frequency,
1604 .vidioc_s_hw_freq_seek = si470x_vidioc_s_hw_freq_seek,
1608 * si470x_viddev_tamples - video device interface
1610 static struct video_device si470x_viddev_template = {
1611 .fops = &si470x_fops,
1612 .ioctl_ops = &si470x_ioctl_ops,
1613 .name = DRIVER_NAME,
1614 .release = video_device_release,
1619 /**************************************************************************
1621 **************************************************************************/
1624 * si470x_usb_driver_probe - probe for the device
1626 static int si470x_usb_driver_probe(struct usb_interface *intf,
1627 const struct usb_device_id *id)
1629 struct si470x_device *radio;
1632 /* private data allocation and initialization */
1633 radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL);
1639 radio->disconnected = 0;
1640 radio->usbdev = interface_to_usbdev(intf);
1642 mutex_init(&radio->disconnect_lock);
1643 mutex_init(&radio->lock);
1645 /* video device allocation and initialization */
1646 radio->videodev = video_device_alloc();
1647 if (!radio->videodev) {
1651 memcpy(radio->videodev, &si470x_viddev_template,
1652 sizeof(si470x_viddev_template));
1653 video_set_drvdata(radio->videodev, radio);
1655 /* show some infos about the specific device */
1656 if (si470x_get_all_registers(radio) < 0) {
1660 printk(KERN_INFO DRIVER_NAME ": DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",
1661 radio->registers[DEVICEID], radio->registers[CHIPID]);
1663 /* check if firmware is current */
1664 if ((radio->registers[CHIPID] & CHIPID_FIRMWARE)
1665 < RADIO_SW_VERSION_CURRENT) {
1666 printk(KERN_WARNING DRIVER_NAME
1667 ": This driver is known to work with "
1668 "firmware version %hu,\n", RADIO_SW_VERSION_CURRENT);
1669 printk(KERN_WARNING DRIVER_NAME
1670 ": but the device has firmware version %hu.\n",
1671 radio->registers[CHIPID] & CHIPID_FIRMWARE);
1672 printk(KERN_WARNING DRIVER_NAME
1673 ": If you have some trouble using this driver,\n");
1674 printk(KERN_WARNING DRIVER_NAME
1675 ": please report to V4L ML at "
1676 "video4linux-list@redhat.com\n");
1679 /* set initial frequency */
1680 si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
1682 /* rds buffer allocation */
1683 radio->buf_size = rds_buf * 3;
1684 radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
1685 if (!radio->buffer) {
1690 /* rds buffer configuration */
1691 radio->wr_index = 0;
1692 radio->rd_index = 0;
1693 init_waitqueue_head(&radio->read_queue);
1695 /* prepare rds work function */
1696 INIT_DELAYED_WORK(&radio->work, si470x_work);
1698 /* register video device */
1699 retval = video_register_device(radio->videodev, VFL_TYPE_RADIO, radio_nr);
1701 printk(KERN_WARNING DRIVER_NAME
1702 ": Could not register video device\n");
1705 usb_set_intfdata(intf, radio);
1709 video_device_release(radio->videodev);
1710 kfree(radio->buffer);
1719 * si470x_usb_driver_suspend - suspend the device
1721 static int si470x_usb_driver_suspend(struct usb_interface *intf,
1722 pm_message_t message)
1724 struct si470x_device *radio = usb_get_intfdata(intf);
1726 printk(KERN_INFO DRIVER_NAME ": suspending now...\n");
1728 cancel_delayed_work_sync(&radio->work);
1735 * si470x_usb_driver_resume - resume the device
1737 static int si470x_usb_driver_resume(struct usb_interface *intf)
1739 struct si470x_device *radio = usb_get_intfdata(intf);
1741 printk(KERN_INFO DRIVER_NAME ": resuming now...\n");
1743 mutex_lock(&radio->lock);
1744 if (radio->users && radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS)
1745 schedule_delayed_work(&radio->work,
1746 msecs_to_jiffies(rds_poll_time));
1747 mutex_unlock(&radio->lock);
1754 * si470x_usb_driver_disconnect - disconnect the device
1756 static void si470x_usb_driver_disconnect(struct usb_interface *intf)
1758 struct si470x_device *radio = usb_get_intfdata(intf);
1760 mutex_lock(&radio->disconnect_lock);
1761 radio->disconnected = 1;
1762 cancel_delayed_work_sync(&radio->work);
1763 usb_set_intfdata(intf, NULL);
1764 if (radio->users == 0) {
1765 video_unregister_device(radio->videodev);
1766 kfree(radio->buffer);
1769 mutex_unlock(&radio->disconnect_lock);
1774 * si470x_usb_driver - usb driver interface
1776 static struct usb_driver si470x_usb_driver = {
1777 .name = DRIVER_NAME,
1778 .probe = si470x_usb_driver_probe,
1779 .disconnect = si470x_usb_driver_disconnect,
1780 .suspend = si470x_usb_driver_suspend,
1781 .resume = si470x_usb_driver_resume,
1782 .id_table = si470x_usb_driver_id_table,
1783 .supports_autosuspend = 1,
1788 /**************************************************************************
1790 **************************************************************************/
1793 * si470x_module_init - module init
1795 static int __init si470x_module_init(void)
1797 printk(KERN_INFO DRIVER_DESC ", Version " DRIVER_VERSION "\n");
1798 return usb_register(&si470x_usb_driver);
1803 * si470x_module_exit - module exit
1805 static void __exit si470x_module_exit(void)
1807 usb_deregister(&si470x_usb_driver);
1811 module_init(si470x_module_init);
1812 module_exit(si470x_module_exit);
1814 MODULE_LICENSE("GPL");
1815 MODULE_AUTHOR(DRIVER_AUTHOR);
1816 MODULE_DESCRIPTION(DRIVER_DESC);
1817 MODULE_VERSION(DRIVER_VERSION);