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)
7 * - KWorld USB FM Radio SnapMusic Mobile 700 (FM700)
8 * - Sanei Electric, Inc. FM USB Radio (sold as DealExtreme.com PCear)
10 * Copyright (c) 2009 Tobias Lorenz <tobias.lorenz@gmx.net>
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.
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.
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
30 * 2008-01-12 Tobias Lorenz <tobias.lorenz@gmx.net>
32 * - First working version
33 * 2008-01-13 Tobias Lorenz <tobias.lorenz@gmx.net>
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>
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>
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>
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>
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>
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>
89 * - usb autosuspend support
91 * 2008-05-07 Tobias Lorenz <tobias.lorenz@gmx.net>
93 * - hardware frequency seek support
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
111 * - add firmware download/update support
112 * - RDS support: interrupt mode, instead of polling
116 /* driver definitions */
117 #define DRIVER_AUTHOR "Tobias Lorenz <tobias.lorenz@gmx.net>"
118 #define DRIVER_NAME "radio-si470x"
119 #define DRIVER_KERNEL_VERSION KERNEL_VERSION(1, 0, 9)
120 #define DRIVER_CARD "Silicon Labs Si470x FM Radio Receiver"
121 #define DRIVER_DESC "USB radio driver for Si470x FM Radio Receivers"
122 #define DRIVER_VERSION "1.0.9"
125 /* kernel includes */
126 #include <linux/kernel.h>
127 #include <linux/module.h>
128 #include <linux/init.h>
129 #include <linux/slab.h>
130 #include <linux/input.h>
131 #include <linux/usb.h>
132 #include <linux/hid.h>
133 #include <linux/version.h>
134 #include <linux/videodev2.h>
135 #include <linux/mutex.h>
136 #include <media/v4l2-common.h>
137 #include <media/v4l2-ioctl.h>
138 #include <media/rds.h>
139 #include <asm/unaligned.h>
142 /* USB Device ID List */
143 static struct usb_device_id si470x_usb_driver_id_table[] = {
144 /* Silicon Labs USB FM Radio Reference Design */
145 { USB_DEVICE_AND_INTERFACE_INFO(0x10c4, 0x818a, USB_CLASS_HID, 0, 0) },
146 /* ADS/Tech FM Radio Receiver (formerly Instant FM Music) */
147 { USB_DEVICE_AND_INTERFACE_INFO(0x06e1, 0xa155, USB_CLASS_HID, 0, 0) },
148 /* KWorld USB FM Radio SnapMusic Mobile 700 (FM700) */
149 { USB_DEVICE_AND_INTERFACE_INFO(0x1b80, 0xd700, USB_CLASS_HID, 0, 0) },
150 /* Sanei Electric, Inc. FM USB Radio (sold as DealExtreme.com PCear) */
151 { USB_DEVICE_AND_INTERFACE_INFO(0x10c5, 0x819a, USB_CLASS_HID, 0, 0) },
152 /* Terminating entry */
155 MODULE_DEVICE_TABLE(usb, si470x_usb_driver_id_table);
159 /**************************************************************************
161 **************************************************************************/
164 static int radio_nr = -1;
165 module_param(radio_nr, int, 0444);
166 MODULE_PARM_DESC(radio_nr, "Radio Nr");
169 /* 0: 200 kHz (USA, Australia) */
170 /* 1: 100 kHz (Europe, Japan) */
172 static unsigned short space = 2;
173 module_param(space, ushort, 0444);
174 MODULE_PARM_DESC(space, "Spacing: 0=200kHz 1=100kHz *2=50kHz*");
176 /* Bottom of Band (MHz) */
177 /* 0: 87.5 - 108 MHz (USA, Europe)*/
178 /* 1: 76 - 108 MHz (Japan wide band) */
179 /* 2: 76 - 90 MHz (Japan) */
180 static unsigned short band = 1;
181 module_param(band, ushort, 0444);
182 MODULE_PARM_DESC(band, "Band: 0=87.5..108MHz *1=76..108MHz* 2=76..90MHz");
186 /* 1: 50 us (Europe, Australia, Japan) */
187 static unsigned short de = 1;
188 module_param(de, ushort, 0444);
189 MODULE_PARM_DESC(de, "De-emphasis: 0=75us *1=50us*");
192 static unsigned int usb_timeout = 500;
193 module_param(usb_timeout, uint, 0644);
194 MODULE_PARM_DESC(usb_timeout, "USB timeout (ms): *500*");
197 static unsigned int tune_timeout = 3000;
198 module_param(tune_timeout, uint, 0644);
199 MODULE_PARM_DESC(tune_timeout, "Tune timeout: *3000*");
202 static unsigned int seek_timeout = 5000;
203 module_param(seek_timeout, uint, 0644);
204 MODULE_PARM_DESC(seek_timeout, "Seek timeout: *5000*");
206 /* RDS buffer blocks */
207 static unsigned int rds_buf = 100;
208 module_param(rds_buf, uint, 0444);
209 MODULE_PARM_DESC(rds_buf, "RDS buffer entries: *100*");
211 /* RDS maximum block errors */
212 static unsigned short max_rds_errors = 1;
213 /* 0 means 0 errors requiring correction */
214 /* 1 means 1-2 errors requiring correction (used by original USBRadio.exe) */
215 /* 2 means 3-5 errors requiring correction */
216 /* 3 means 6+ errors or errors in checkword, correction not possible */
217 module_param(max_rds_errors, ushort, 0644);
218 MODULE_PARM_DESC(max_rds_errors, "RDS maximum block errors: *1*");
220 /* RDS poll frequency */
221 static unsigned int rds_poll_time = 40;
222 /* 40 is used by the original USBRadio.exe */
223 /* 50 is used by radio-cadet */
224 /* 75 should be okay */
225 /* 80 is the usual RDS receive interval */
226 module_param(rds_poll_time, uint, 0644);
227 MODULE_PARM_DESC(rds_poll_time, "RDS poll time (ms): *40*");
231 /**************************************************************************
232 * Register Definitions
233 **************************************************************************/
234 #define RADIO_REGISTER_SIZE 2 /* 16 register bit width */
235 #define RADIO_REGISTER_NUM 16 /* DEVICEID ... RDSD */
236 #define RDS_REGISTER_NUM 6 /* STATUSRSSI ... RDSD */
238 #define DEVICEID 0 /* Device ID */
239 #define DEVICEID_PN 0xf000 /* bits 15..12: Part Number */
240 #define DEVICEID_MFGID 0x0fff /* bits 11..00: Manufacturer ID */
242 #define CHIPID 1 /* Chip ID */
243 #define CHIPID_REV 0xfc00 /* bits 15..10: Chip Version */
244 #define CHIPID_DEV 0x0200 /* bits 09..09: Device */
245 #define CHIPID_FIRMWARE 0x01ff /* bits 08..00: Firmware Version */
247 #define POWERCFG 2 /* Power Configuration */
248 #define POWERCFG_DSMUTE 0x8000 /* bits 15..15: Softmute Disable */
249 #define POWERCFG_DMUTE 0x4000 /* bits 14..14: Mute Disable */
250 #define POWERCFG_MONO 0x2000 /* bits 13..13: Mono Select */
251 #define POWERCFG_RDSM 0x0800 /* bits 11..11: RDS Mode (Si4701 only) */
252 #define POWERCFG_SKMODE 0x0400 /* bits 10..10: Seek Mode */
253 #define POWERCFG_SEEKUP 0x0200 /* bits 09..09: Seek Direction */
254 #define POWERCFG_SEEK 0x0100 /* bits 08..08: Seek */
255 #define POWERCFG_DISABLE 0x0040 /* bits 06..06: Powerup Disable */
256 #define POWERCFG_ENABLE 0x0001 /* bits 00..00: Powerup Enable */
258 #define CHANNEL 3 /* Channel */
259 #define CHANNEL_TUNE 0x8000 /* bits 15..15: Tune */
260 #define CHANNEL_CHAN 0x03ff /* bits 09..00: Channel Select */
262 #define SYSCONFIG1 4 /* System Configuration 1 */
263 #define SYSCONFIG1_RDSIEN 0x8000 /* bits 15..15: RDS Interrupt Enable (Si4701 only) */
264 #define SYSCONFIG1_STCIEN 0x4000 /* bits 14..14: Seek/Tune Complete Interrupt Enable */
265 #define SYSCONFIG1_RDS 0x1000 /* bits 12..12: RDS Enable (Si4701 only) */
266 #define SYSCONFIG1_DE 0x0800 /* bits 11..11: De-emphasis (0=75us 1=50us) */
267 #define SYSCONFIG1_AGCD 0x0400 /* bits 10..10: AGC Disable */
268 #define SYSCONFIG1_BLNDADJ 0x00c0 /* bits 07..06: Stereo/Mono Blend Level Adjustment */
269 #define SYSCONFIG1_GPIO3 0x0030 /* bits 05..04: General Purpose I/O 3 */
270 #define SYSCONFIG1_GPIO2 0x000c /* bits 03..02: General Purpose I/O 2 */
271 #define SYSCONFIG1_GPIO1 0x0003 /* bits 01..00: General Purpose I/O 1 */
273 #define SYSCONFIG2 5 /* System Configuration 2 */
274 #define SYSCONFIG2_SEEKTH 0xff00 /* bits 15..08: RSSI Seek Threshold */
275 #define SYSCONFIG2_BAND 0x0080 /* bits 07..06: Band Select */
276 #define SYSCONFIG2_SPACE 0x0030 /* bits 05..04: Channel Spacing */
277 #define SYSCONFIG2_VOLUME 0x000f /* bits 03..00: Volume */
279 #define SYSCONFIG3 6 /* System Configuration 3 */
280 #define SYSCONFIG3_SMUTER 0xc000 /* bits 15..14: Softmute Attack/Recover Rate */
281 #define SYSCONFIG3_SMUTEA 0x3000 /* bits 13..12: Softmute Attenuation */
282 #define SYSCONFIG3_SKSNR 0x00f0 /* bits 07..04: Seek SNR Threshold */
283 #define SYSCONFIG3_SKCNT 0x000f /* bits 03..00: Seek FM Impulse Detection Threshold */
285 #define TEST1 7 /* Test 1 */
286 #define TEST1_AHIZEN 0x4000 /* bits 14..14: Audio High-Z Enable */
288 #define TEST2 8 /* Test 2 */
289 /* TEST2 only contains reserved bits */
291 #define BOOTCONFIG 9 /* Boot Configuration */
292 /* BOOTCONFIG only contains reserved bits */
294 #define STATUSRSSI 10 /* Status RSSI */
295 #define STATUSRSSI_RDSR 0x8000 /* bits 15..15: RDS Ready (Si4701 only) */
296 #define STATUSRSSI_STC 0x4000 /* bits 14..14: Seek/Tune Complete */
297 #define STATUSRSSI_SF 0x2000 /* bits 13..13: Seek Fail/Band Limit */
298 #define STATUSRSSI_AFCRL 0x1000 /* bits 12..12: AFC Rail */
299 #define STATUSRSSI_RDSS 0x0800 /* bits 11..11: RDS Synchronized (Si4701 only) */
300 #define STATUSRSSI_BLERA 0x0600 /* bits 10..09: RDS Block A Errors (Si4701 only) */
301 #define STATUSRSSI_ST 0x0100 /* bits 08..08: Stereo Indicator */
302 #define STATUSRSSI_RSSI 0x00ff /* bits 07..00: RSSI (Received Signal Strength Indicator) */
304 #define READCHAN 11 /* Read Channel */
305 #define READCHAN_BLERB 0xc000 /* bits 15..14: RDS Block D Errors (Si4701 only) */
306 #define READCHAN_BLERC 0x3000 /* bits 13..12: RDS Block C Errors (Si4701 only) */
307 #define READCHAN_BLERD 0x0c00 /* bits 11..10: RDS Block B Errors (Si4701 only) */
308 #define READCHAN_READCHAN 0x03ff /* bits 09..00: Read Channel */
310 #define RDSA 12 /* RDSA */
311 #define RDSA_RDSA 0xffff /* bits 15..00: RDS Block A Data (Si4701 only) */
313 #define RDSB 13 /* RDSB */
314 #define RDSB_RDSB 0xffff /* bits 15..00: RDS Block B Data (Si4701 only) */
316 #define RDSC 14 /* RDSC */
317 #define RDSC_RDSC 0xffff /* bits 15..00: RDS Block C Data (Si4701 only) */
319 #define RDSD 15 /* RDSD */
320 #define RDSD_RDSD 0xffff /* bits 15..00: RDS Block D Data (Si4701 only) */
324 /**************************************************************************
326 **************************************************************************/
328 /* Reports 1-16 give direct read/write access to the 16 Si470x registers */
329 /* with the (REPORT_ID - 1) corresponding to the register address across USB */
330 /* endpoint 0 using GET_REPORT and SET_REPORT */
331 #define REGISTER_REPORT_SIZE (RADIO_REGISTER_SIZE + 1)
332 #define REGISTER_REPORT(reg) ((reg) + 1)
334 /* Report 17 gives direct read/write access to the entire Si470x register */
335 /* map across endpoint 0 using GET_REPORT and SET_REPORT */
336 #define ENTIRE_REPORT_SIZE (RADIO_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
337 #define ENTIRE_REPORT 17
339 /* Report 18 is used to send the lowest 6 Si470x registers up the HID */
340 /* interrupt endpoint 1 to Windows every 20 milliseconds for status */
341 #define RDS_REPORT_SIZE (RDS_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
342 #define RDS_REPORT 18
344 /* Report 19: LED state */
345 #define LED_REPORT_SIZE 3
346 #define LED_REPORT 19
348 /* Report 19: stream */
349 #define STREAM_REPORT_SIZE 3
350 #define STREAM_REPORT 19
352 /* Report 20: scratch */
353 #define SCRATCH_PAGE_SIZE 63
354 #define SCRATCH_REPORT_SIZE (SCRATCH_PAGE_SIZE + 1)
355 #define SCRATCH_REPORT 20
357 /* Reports 19-22: flash upgrade of the C8051F321 */
358 #define WRITE_REPORT_SIZE 4
359 #define WRITE_REPORT 19
360 #define FLASH_REPORT_SIZE 64
361 #define FLASH_REPORT 20
362 #define CRC_REPORT_SIZE 3
363 #define CRC_REPORT 21
364 #define RESPONSE_REPORT_SIZE 2
365 #define RESPONSE_REPORT 22
367 /* Report 23: currently unused, but can accept 60 byte reports on the HID */
368 /* interrupt out endpoint 2 every 1 millisecond */
369 #define UNUSED_REPORT 23
373 /**************************************************************************
374 * Software/Hardware Versions
375 **************************************************************************/
376 #define RADIO_SW_VERSION_NOT_BOOTLOADABLE 6
377 #define RADIO_SW_VERSION 7
378 #define RADIO_SW_VERSION_CURRENT 15
379 #define RADIO_HW_VERSION 1
381 #define SCRATCH_PAGE_SW_VERSION 1
382 #define SCRATCH_PAGE_HW_VERSION 2
386 /**************************************************************************
387 * LED State Definitions
388 **************************************************************************/
389 #define LED_COMMAND 0x35
391 #define NO_CHANGE_LED 0x00
392 #define ALL_COLOR_LED 0x01 /* streaming state */
393 #define BLINK_GREEN_LED 0x02 /* connect state */
394 #define BLINK_RED_LED 0x04
395 #define BLINK_ORANGE_LED 0x10 /* disconnect state */
396 #define SOLID_GREEN_LED 0x20 /* tuning/seeking state */
397 #define SOLID_RED_LED 0x40 /* bootload state */
398 #define SOLID_ORANGE_LED 0x80
402 /**************************************************************************
403 * Stream State Definitions
404 **************************************************************************/
405 #define STREAM_COMMAND 0x36
406 #define STREAM_VIDPID 0x00
407 #define STREAM_AUDIO 0xff
411 /**************************************************************************
412 * Bootloader / Flash Commands
413 **************************************************************************/
415 /* unique id sent to bootloader and required to put into a bootload state */
416 #define UNIQUE_BL_ID 0x34
418 /* mask for the flash data */
419 #define FLASH_DATA_MASK 0x55
421 /* bootloader commands */
422 #define GET_SW_VERSION_COMMAND 0x00
423 #define SET_PAGE_COMMAND 0x01
424 #define ERASE_PAGE_COMMAND 0x02
425 #define WRITE_PAGE_COMMAND 0x03
426 #define CRC_ON_PAGE_COMMAND 0x04
427 #define READ_FLASH_BYTE_COMMAND 0x05
428 #define RESET_DEVICE_COMMAND 0x06
429 #define GET_HW_VERSION_COMMAND 0x07
432 /* bootloader command responses */
433 #define COMMAND_OK 0x01
434 #define COMMAND_FAILED 0x02
435 #define COMMAND_PENDING 0x03
439 /**************************************************************************
440 * General Driver Definitions
441 **************************************************************************/
444 * si470x_device - private data
446 struct si470x_device {
447 /* reference to USB and video device */
448 struct usb_device *usbdev;
449 struct usb_interface *intf;
450 struct video_device *videodev;
452 /* driver management */
454 unsigned char disconnected;
455 struct mutex disconnect_lock;
457 /* Silabs internal registers (0..15) */
458 unsigned short registers[RADIO_REGISTER_NUM];
460 /* RDS receive buffer */
461 struct delayed_work work;
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;
470 unsigned char software_version;
471 unsigned char hardware_version;
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
482 #define FREQ_MUL (1000000 / 62.5)
486 /**************************************************************************
487 * General Driver Functions - REGISTER_REPORTs
488 **************************************************************************/
491 * si470x_get_report - receive a HID report
493 static int si470x_get_report(struct si470x_device *radio, void *buf, int size)
495 unsigned char *report = (unsigned char *) buf;
498 retval = usb_control_msg(radio->usbdev,
499 usb_rcvctrlpipe(radio->usbdev, 0),
501 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
503 buf, size, usb_timeout);
506 printk(KERN_WARNING DRIVER_NAME
507 ": si470x_get_report: usb_control_msg returned %d\n",
514 * si470x_set_report - send a HID report
516 static int si470x_set_report(struct si470x_device *radio, void *buf, int size)
518 unsigned char *report = (unsigned char *) buf;
521 retval = usb_control_msg(radio->usbdev,
522 usb_sndctrlpipe(radio->usbdev, 0),
524 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
526 buf, size, usb_timeout);
529 printk(KERN_WARNING DRIVER_NAME
530 ": si470x_set_report: usb_control_msg returned %d\n",
537 * si470x_get_register - read register
539 static int si470x_get_register(struct si470x_device *radio, int regnr)
541 unsigned char buf[REGISTER_REPORT_SIZE];
544 buf[0] = REGISTER_REPORT(regnr);
546 retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
549 radio->registers[regnr] = get_unaligned_be16(&buf[1]);
551 return (retval < 0) ? -EINVAL : 0;
556 * si470x_set_register - write register
558 static int si470x_set_register(struct si470x_device *radio, int regnr)
560 unsigned char buf[REGISTER_REPORT_SIZE];
563 buf[0] = REGISTER_REPORT(regnr);
564 put_unaligned_be16(radio->registers[regnr], &buf[1]);
566 retval = si470x_set_report(radio, (void *) &buf, sizeof(buf));
568 return (retval < 0) ? -EINVAL : 0;
573 * si470x_set_chan - set the channel
575 static int si470x_set_chan(struct si470x_device *radio, unsigned short chan)
578 unsigned long timeout;
582 radio->registers[CHANNEL] &= ~CHANNEL_CHAN;
583 radio->registers[CHANNEL] |= CHANNEL_TUNE | chan;
584 retval = si470x_set_register(radio, CHANNEL);
588 /* wait till tune operation has completed */
589 timeout = jiffies + msecs_to_jiffies(tune_timeout);
591 retval = si470x_get_register(radio, STATUSRSSI);
594 timed_out = time_after(jiffies, timeout);
595 } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
597 if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
598 printk(KERN_WARNING DRIVER_NAME ": tune does not complete\n");
600 printk(KERN_WARNING DRIVER_NAME
601 ": tune timed out after %u ms\n", tune_timeout);
605 radio->registers[CHANNEL] &= ~CHANNEL_TUNE;
606 retval = si470x_set_register(radio, CHANNEL);
614 * si470x_get_freq - get the frequency
616 static int si470x_get_freq(struct si470x_device *radio, unsigned int *freq)
618 unsigned int spacing, band_bottom;
623 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) {
624 /* 0: 200 kHz (USA, Australia) */
626 spacing = 0.200 * FREQ_MUL; break;
627 /* 1: 100 kHz (Europe, Japan) */
629 spacing = 0.100 * FREQ_MUL; break;
632 spacing = 0.050 * FREQ_MUL; break;
635 /* Bottom of Band (MHz) */
636 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
637 /* 0: 87.5 - 108 MHz (USA, Europe) */
639 band_bottom = 87.5 * FREQ_MUL; break;
640 /* 1: 76 - 108 MHz (Japan wide band) */
642 band_bottom = 76 * FREQ_MUL; break;
643 /* 2: 76 - 90 MHz (Japan) */
645 band_bottom = 76 * FREQ_MUL; break;
649 retval = si470x_get_register(radio, READCHAN);
650 chan = radio->registers[READCHAN] & READCHAN_READCHAN;
652 /* Frequency (MHz) = Spacing (kHz) x Channel + Bottom of Band (MHz) */
653 *freq = chan * spacing + band_bottom;
660 * si470x_set_freq - set the frequency
662 static int si470x_set_freq(struct si470x_device *radio, unsigned int freq)
664 unsigned int spacing, band_bottom;
668 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) {
669 /* 0: 200 kHz (USA, Australia) */
671 spacing = 0.200 * FREQ_MUL; break;
672 /* 1: 100 kHz (Europe, Japan) */
674 spacing = 0.100 * FREQ_MUL; break;
677 spacing = 0.050 * FREQ_MUL; break;
680 /* Bottom of Band (MHz) */
681 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
682 /* 0: 87.5 - 108 MHz (USA, Europe) */
684 band_bottom = 87.5 * FREQ_MUL; break;
685 /* 1: 76 - 108 MHz (Japan wide band) */
687 band_bottom = 76 * FREQ_MUL; break;
688 /* 2: 76 - 90 MHz (Japan) */
690 band_bottom = 76 * FREQ_MUL; break;
693 /* Chan = [ Freq (Mhz) - Bottom of Band (MHz) ] / Spacing (kHz) */
694 chan = (freq - band_bottom) / spacing;
696 return si470x_set_chan(radio, chan);
701 * si470x_set_seek - set seek
703 static int si470x_set_seek(struct si470x_device *radio,
704 unsigned int wrap_around, unsigned int seek_upward)
707 unsigned long timeout;
711 radio->registers[POWERCFG] |= POWERCFG_SEEK;
712 if (wrap_around == 1)
713 radio->registers[POWERCFG] &= ~POWERCFG_SKMODE;
715 radio->registers[POWERCFG] |= POWERCFG_SKMODE;
716 if (seek_upward == 1)
717 radio->registers[POWERCFG] |= POWERCFG_SEEKUP;
719 radio->registers[POWERCFG] &= ~POWERCFG_SEEKUP;
720 retval = si470x_set_register(radio, POWERCFG);
724 /* wait till seek operation has completed */
725 timeout = jiffies + msecs_to_jiffies(seek_timeout);
727 retval = si470x_get_register(radio, STATUSRSSI);
730 timed_out = time_after(jiffies, timeout);
731 } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
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");
739 printk(KERN_WARNING DRIVER_NAME
740 ": seek timed out after %u ms\n", seek_timeout);
744 radio->registers[POWERCFG] &= ~POWERCFG_SEEK;
745 retval = si470x_set_register(radio, POWERCFG);
748 /* try again, if timed out */
749 if ((retval == 0) && timed_out)
757 * si470x_start - switch on radio
759 static int si470x_start(struct si470x_device *radio)
764 radio->registers[POWERCFG] =
765 POWERCFG_DMUTE | POWERCFG_ENABLE | POWERCFG_RDSM;
766 retval = si470x_set_register(radio, POWERCFG);
771 radio->registers[SYSCONFIG1] = SYSCONFIG1_DE;
772 retval = si470x_set_register(radio, SYSCONFIG1);
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);
786 /* reset last channel */
787 retval = si470x_set_chan(radio,
788 radio->registers[CHANNEL] & CHANNEL_CHAN);
796 * si470x_stop - switch off radio
798 static int si470x_stop(struct si470x_device *radio)
803 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
804 retval = si470x_set_register(radio, SYSCONFIG1);
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);
820 * si470x_rds_on - switch on rds reception
822 static int si470x_rds_on(struct si470x_device *radio)
827 mutex_lock(&radio->lock);
828 radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDS;
829 retval = si470x_set_register(radio, SYSCONFIG1);
831 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
832 mutex_unlock(&radio->lock);
839 /**************************************************************************
840 * General Driver Functions - ENTIRE_REPORT
841 **************************************************************************/
844 * si470x_get_all_registers - read entire registers
846 static int si470x_get_all_registers(struct si470x_device *radio)
848 unsigned char buf[ENTIRE_REPORT_SIZE];
852 buf[0] = ENTIRE_REPORT;
854 retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
857 for (regnr = 0; regnr < RADIO_REGISTER_NUM; regnr++)
858 radio->registers[regnr] = get_unaligned_be16(
859 &buf[regnr * RADIO_REGISTER_SIZE + 1]);
861 return (retval < 0) ? -EINVAL : 0;
866 /**************************************************************************
867 * General Driver Functions - RDS_REPORT
868 **************************************************************************/
871 * si470x_get_rds_registers - read rds registers
873 static int si470x_get_rds_registers(struct si470x_device *radio)
875 unsigned char buf[RDS_REPORT_SIZE];
882 retval = usb_interrupt_msg(radio->usbdev,
883 usb_rcvintpipe(radio->usbdev, 1),
884 (void *) &buf, sizeof(buf), &size, usb_timeout);
885 if (size != sizeof(buf))
886 printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: "
887 "return size differs: %d != %zu\n", size, sizeof(buf));
889 printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: "
890 "usb_interrupt_msg returned %d\n", retval);
893 for (regnr = 0; regnr < RDS_REGISTER_NUM; regnr++)
894 radio->registers[STATUSRSSI + regnr] =
896 &buf[regnr * RADIO_REGISTER_SIZE + 1]);
898 return (retval < 0) ? -EINVAL : 0;
903 /**************************************************************************
904 * General Driver Functions - LED_REPORT
905 **************************************************************************/
908 * si470x_set_led_state - sets the led state
910 static int si470x_set_led_state(struct si470x_device *radio,
911 unsigned char led_state)
913 unsigned char buf[LED_REPORT_SIZE];
917 buf[1] = LED_COMMAND;
920 retval = si470x_set_report(radio, (void *) &buf, sizeof(buf));
922 return (retval < 0) ? -EINVAL : 0;
927 /**************************************************************************
928 * General Driver Functions - SCRATCH_REPORT
929 **************************************************************************/
932 * si470x_get_scratch_versions - gets the scratch page and version infos
934 static int si470x_get_scratch_page_versions(struct si470x_device *radio)
936 unsigned char buf[SCRATCH_REPORT_SIZE];
939 buf[0] = SCRATCH_REPORT;
941 retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
944 printk(KERN_WARNING DRIVER_NAME ": si470x_get_scratch: "
945 "si470x_get_report returned %d\n", retval);
947 radio->software_version = buf[1];
948 radio->hardware_version = buf[2];
951 return (retval < 0) ? -EINVAL : 0;
956 /**************************************************************************
957 * RDS Driver Functions
958 **************************************************************************/
961 * si470x_rds - rds processing function
963 static void si470x_rds(struct si470x_device *radio)
965 unsigned char blocknum;
966 unsigned short bler; /* rds block errors */
968 unsigned char tmpbuf[3];
971 if (si470x_get_rds_registers(radio) < 0)
973 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0) {
974 /* No RDS group ready */
977 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSS) == 0) {
978 /* RDS decoder not synchronized */
982 /* copy all four RDS blocks to internal buffer */
983 mutex_lock(&radio->lock);
984 for (blocknum = 0; blocknum < 4; blocknum++) {
987 bler = (radio->registers[STATUSRSSI] &
988 STATUSRSSI_BLERA) >> 9;
989 rds = radio->registers[RDSA];
992 bler = (radio->registers[READCHAN] &
993 READCHAN_BLERB) >> 14;
994 rds = radio->registers[RDSB];
997 bler = (radio->registers[READCHAN] &
998 READCHAN_BLERC) >> 12;
999 rds = radio->registers[RDSC];
1002 bler = (radio->registers[READCHAN] &
1003 READCHAN_BLERD) >> 10;
1004 rds = radio->registers[RDSD];
1008 /* Fill the V4L2 RDS buffer */
1009 put_unaligned_le16(rds, &tmpbuf);
1010 tmpbuf[2] = blocknum; /* offset name */
1011 tmpbuf[2] |= blocknum << 3; /* received offset */
1012 if (bler > max_rds_errors)
1013 tmpbuf[2] |= 0x80; /* uncorrectable errors */
1015 tmpbuf[2] |= 0x40; /* corrected error(s) */
1017 /* copy RDS block to internal buffer */
1018 memcpy(&radio->buffer[radio->wr_index], &tmpbuf, 3);
1019 radio->wr_index += 3;
1021 /* wrap write pointer */
1022 if (radio->wr_index >= radio->buf_size)
1023 radio->wr_index = 0;
1025 /* check for overflow */
1026 if (radio->wr_index == radio->rd_index) {
1027 /* increment and wrap read pointer */
1028 radio->rd_index += 3;
1029 if (radio->rd_index >= radio->buf_size)
1030 radio->rd_index = 0;
1033 mutex_unlock(&radio->lock);
1035 /* wake up read queue */
1036 if (radio->wr_index != radio->rd_index)
1037 wake_up_interruptible(&radio->read_queue);
1042 * si470x_work - rds work function
1044 static void si470x_work(struct work_struct *work)
1046 struct si470x_device *radio = container_of(work, struct si470x_device,
1050 if (radio->disconnected)
1052 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
1056 schedule_delayed_work(&radio->work, msecs_to_jiffies(rds_poll_time));
1061 /**************************************************************************
1062 * File Operations Interface
1063 **************************************************************************/
1066 * si470x_fops_read - read RDS data
1068 static ssize_t si470x_fops_read(struct file *file, char __user *buf,
1069 size_t count, loff_t *ppos)
1071 struct si470x_device *radio = video_drvdata(file);
1073 unsigned int block_count = 0;
1075 /* switch on rds reception */
1076 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
1077 si470x_rds_on(radio);
1078 schedule_delayed_work(&radio->work,
1079 msecs_to_jiffies(rds_poll_time));
1082 /* block if no new data available */
1083 while (radio->wr_index == radio->rd_index) {
1084 if (file->f_flags & O_NONBLOCK) {
1085 retval = -EWOULDBLOCK;
1088 if (wait_event_interruptible(radio->read_queue,
1089 radio->wr_index != radio->rd_index) < 0) {
1095 /* calculate block count from byte count */
1098 /* copy RDS block out of internal buffer and to user buffer */
1099 mutex_lock(&radio->lock);
1100 while (block_count < count) {
1101 if (radio->rd_index == radio->wr_index)
1104 /* always transfer rds complete blocks */
1105 if (copy_to_user(buf, &radio->buffer[radio->rd_index], 3))
1106 /* retval = -EFAULT; */
1109 /* increment and wrap read pointer */
1110 radio->rd_index += 3;
1111 if (radio->rd_index >= radio->buf_size)
1112 radio->rd_index = 0;
1114 /* increment counters */
1119 mutex_unlock(&radio->lock);
1127 * si470x_fops_poll - poll RDS data
1129 static unsigned int si470x_fops_poll(struct file *file,
1130 struct poll_table_struct *pts)
1132 struct si470x_device *radio = video_drvdata(file);
1135 /* switch on rds reception */
1136 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
1137 si470x_rds_on(radio);
1138 schedule_delayed_work(&radio->work,
1139 msecs_to_jiffies(rds_poll_time));
1142 poll_wait(file, &radio->read_queue, pts);
1144 if (radio->rd_index != radio->wr_index)
1145 retval = POLLIN | POLLRDNORM;
1152 * si470x_fops_open - file open
1154 static int si470x_fops_open(struct file *file)
1156 struct si470x_device *radio = video_drvdata(file);
1162 retval = usb_autopm_get_interface(radio->intf);
1169 if (radio->users == 1) {
1171 retval = si470x_start(radio);
1173 usb_autopm_put_interface(radio->intf);
1183 * si470x_fops_release - file release
1185 static int si470x_fops_release(struct file *file)
1187 struct si470x_device *radio = video_drvdata(file);
1196 mutex_lock(&radio->disconnect_lock);
1198 if (radio->users == 0) {
1199 if (radio->disconnected) {
1200 video_unregister_device(radio->videodev);
1201 kfree(radio->buffer);
1206 /* stop rds reception */
1207 cancel_delayed_work_sync(&radio->work);
1209 /* cancel read processes */
1210 wake_up_interruptible(&radio->read_queue);
1213 retval = si470x_stop(radio);
1214 usb_autopm_put_interface(radio->intf);
1218 mutex_unlock(&radio->disconnect_lock);
1226 * si470x_fops - file operations interface
1228 static const struct v4l2_file_operations si470x_fops = {
1229 .owner = THIS_MODULE,
1230 .read = si470x_fops_read,
1231 .poll = si470x_fops_poll,
1232 .ioctl = video_ioctl2,
1233 .open = si470x_fops_open,
1234 .release = si470x_fops_release,
1239 /**************************************************************************
1240 * Video4Linux Interface
1241 **************************************************************************/
1244 * si470x_v4l2_queryctrl - query control
1246 static struct v4l2_queryctrl si470x_v4l2_queryctrl[] = {
1248 .id = V4L2_CID_AUDIO_VOLUME,
1249 .type = V4L2_CTRL_TYPE_INTEGER,
1254 .default_value = 15,
1257 .id = V4L2_CID_AUDIO_MUTE,
1258 .type = V4L2_CTRL_TYPE_BOOLEAN,
1269 * si470x_vidioc_querycap - query device capabilities
1271 static int si470x_vidioc_querycap(struct file *file, void *priv,
1272 struct v4l2_capability *capability)
1274 struct si470x_device *radio = video_drvdata(file);
1276 strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver));
1277 strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
1278 usb_make_path(radio->usbdev, capability->bus_info, sizeof(capability->bus_info));
1279 capability->version = DRIVER_KERNEL_VERSION;
1280 capability->capabilities = V4L2_CAP_HW_FREQ_SEEK |
1281 V4L2_CAP_TUNER | V4L2_CAP_RADIO;
1288 * si470x_vidioc_queryctrl - enumerate control items
1290 static int si470x_vidioc_queryctrl(struct file *file, void *priv,
1291 struct v4l2_queryctrl *qc)
1293 unsigned char i = 0;
1294 int retval = -EINVAL;
1296 /* abort if qc->id is below V4L2_CID_BASE */
1297 if (qc->id < V4L2_CID_BASE)
1300 /* search video control */
1301 for (i = 0; i < ARRAY_SIZE(si470x_v4l2_queryctrl); i++) {
1302 if (qc->id == si470x_v4l2_queryctrl[i].id) {
1303 memcpy(qc, &(si470x_v4l2_queryctrl[i]), sizeof(*qc));
1304 retval = 0; /* found */
1309 /* disable unsupported base controls */
1310 /* to satisfy kradio and such apps */
1311 if ((retval == -EINVAL) && (qc->id < V4L2_CID_LASTP1)) {
1312 qc->flags = V4L2_CTRL_FLAG_DISABLED;
1318 printk(KERN_WARNING DRIVER_NAME
1319 ": query controls failed with %d\n", retval);
1325 * si470x_vidioc_g_ctrl - get the value of a control
1327 static int si470x_vidioc_g_ctrl(struct file *file, void *priv,
1328 struct v4l2_control *ctrl)
1330 struct si470x_device *radio = video_drvdata(file);
1334 if (radio->disconnected) {
1340 case V4L2_CID_AUDIO_VOLUME:
1341 ctrl->value = radio->registers[SYSCONFIG2] &
1344 case V4L2_CID_AUDIO_MUTE:
1345 ctrl->value = ((radio->registers[POWERCFG] &
1346 POWERCFG_DMUTE) == 0) ? 1 : 0;
1354 printk(KERN_WARNING DRIVER_NAME
1355 ": get control failed with %d\n", retval);
1361 * si470x_vidioc_s_ctrl - set the value of a control
1363 static int si470x_vidioc_s_ctrl(struct file *file, void *priv,
1364 struct v4l2_control *ctrl)
1366 struct si470x_device *radio = video_drvdata(file);
1370 if (radio->disconnected) {
1376 case V4L2_CID_AUDIO_VOLUME:
1377 radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_VOLUME;
1378 radio->registers[SYSCONFIG2] |= ctrl->value;
1379 retval = si470x_set_register(radio, SYSCONFIG2);
1381 case V4L2_CID_AUDIO_MUTE:
1382 if (ctrl->value == 1)
1383 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
1385 radio->registers[POWERCFG] |= POWERCFG_DMUTE;
1386 retval = si470x_set_register(radio, POWERCFG);
1394 printk(KERN_WARNING DRIVER_NAME
1395 ": set control failed with %d\n", retval);
1401 * si470x_vidioc_g_audio - get audio attributes
1403 static int si470x_vidioc_g_audio(struct file *file, void *priv,
1404 struct v4l2_audio *audio)
1406 /* driver constants */
1408 strcpy(audio->name, "Radio");
1409 audio->capability = V4L2_AUDCAP_STEREO;
1417 * si470x_vidioc_g_tuner - get tuner attributes
1419 static int si470x_vidioc_g_tuner(struct file *file, void *priv,
1420 struct v4l2_tuner *tuner)
1422 struct si470x_device *radio = video_drvdata(file);
1426 if (radio->disconnected) {
1430 if (tuner->index != 0) {
1435 retval = si470x_get_register(radio, STATUSRSSI);
1439 /* driver constants */
1440 strcpy(tuner->name, "FM");
1441 tuner->type = V4L2_TUNER_RADIO;
1442 tuner->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
1445 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
1446 /* 0: 87.5 - 108 MHz (USA, Europe, default) */
1448 tuner->rangelow = 87.5 * FREQ_MUL;
1449 tuner->rangehigh = 108 * FREQ_MUL;
1451 /* 1: 76 - 108 MHz (Japan wide band) */
1453 tuner->rangelow = 76 * FREQ_MUL;
1454 tuner->rangehigh = 108 * FREQ_MUL;
1456 /* 2: 76 - 90 MHz (Japan) */
1458 tuner->rangelow = 76 * FREQ_MUL;
1459 tuner->rangehigh = 90 * FREQ_MUL;
1463 /* stereo indicator == stereo (instead of mono) */
1464 if ((radio->registers[STATUSRSSI] & STATUSRSSI_ST) == 0)
1465 tuner->rxsubchans = V4L2_TUNER_SUB_MONO;
1467 tuner->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
1469 /* mono/stereo selector */
1470 if ((radio->registers[POWERCFG] & POWERCFG_MONO) == 0)
1471 tuner->audmode = V4L2_TUNER_MODE_STEREO;
1473 tuner->audmode = V4L2_TUNER_MODE_MONO;
1475 /* min is worst, max is best; signal:0..0xffff; rssi: 0..0xff */
1476 /* measured in units of dbµV in 1 db increments (max at ~75 dbµV) */
1477 tuner->signal = (radio->registers[STATUSRSSI] & STATUSRSSI_RSSI);
1478 /* the ideal factor is 0xffff/75 = 873,8 */
1479 tuner->signal = (tuner->signal * 873) + (8 * tuner->signal / 10);
1481 /* automatic frequency control: -1: freq to low, 1 freq to high */
1482 /* AFCRL does only indicate that freq. differs, not if too low/high */
1483 tuner->afc = (radio->registers[STATUSRSSI] & STATUSRSSI_AFCRL) ? 1 : 0;
1487 printk(KERN_WARNING DRIVER_NAME
1488 ": get tuner failed with %d\n", retval);
1494 * si470x_vidioc_s_tuner - set tuner attributes
1496 static int si470x_vidioc_s_tuner(struct file *file, void *priv,
1497 struct v4l2_tuner *tuner)
1499 struct si470x_device *radio = video_drvdata(file);
1500 int retval = -EINVAL;
1503 if (radio->disconnected) {
1507 if (tuner->index != 0)
1510 /* mono/stereo selector */
1511 switch (tuner->audmode) {
1512 case V4L2_TUNER_MODE_MONO:
1513 radio->registers[POWERCFG] |= POWERCFG_MONO; /* force mono */
1515 case V4L2_TUNER_MODE_STEREO:
1516 radio->registers[POWERCFG] &= ~POWERCFG_MONO; /* try stereo */
1522 retval = si470x_set_register(radio, POWERCFG);
1526 printk(KERN_WARNING DRIVER_NAME
1527 ": set tuner failed with %d\n", retval);
1533 * si470x_vidioc_g_frequency - get tuner or modulator radio frequency
1535 static int si470x_vidioc_g_frequency(struct file *file, void *priv,
1536 struct v4l2_frequency *freq)
1538 struct si470x_device *radio = video_drvdata(file);
1542 if (radio->disconnected) {
1546 if (freq->tuner != 0) {
1551 freq->type = V4L2_TUNER_RADIO;
1552 retval = si470x_get_freq(radio, &freq->frequency);
1556 printk(KERN_WARNING DRIVER_NAME
1557 ": get frequency failed with %d\n", retval);
1563 * si470x_vidioc_s_frequency - set tuner or modulator radio frequency
1565 static int si470x_vidioc_s_frequency(struct file *file, void *priv,
1566 struct v4l2_frequency *freq)
1568 struct si470x_device *radio = video_drvdata(file);
1572 if (radio->disconnected) {
1576 if (freq->tuner != 0) {
1581 retval = si470x_set_freq(radio, freq->frequency);
1585 printk(KERN_WARNING DRIVER_NAME
1586 ": set frequency failed with %d\n", retval);
1592 * si470x_vidioc_s_hw_freq_seek - set hardware frequency seek
1594 static int si470x_vidioc_s_hw_freq_seek(struct file *file, void *priv,
1595 struct v4l2_hw_freq_seek *seek)
1597 struct si470x_device *radio = video_drvdata(file);
1601 if (radio->disconnected) {
1605 if (seek->tuner != 0) {
1610 retval = si470x_set_seek(radio, seek->wrap_around, seek->seek_upward);
1614 printk(KERN_WARNING DRIVER_NAME
1615 ": set hardware frequency seek failed with %d\n",
1622 * si470x_ioctl_ops - video device ioctl operations
1624 static const struct v4l2_ioctl_ops si470x_ioctl_ops = {
1625 .vidioc_querycap = si470x_vidioc_querycap,
1626 .vidioc_queryctrl = si470x_vidioc_queryctrl,
1627 .vidioc_g_ctrl = si470x_vidioc_g_ctrl,
1628 .vidioc_s_ctrl = si470x_vidioc_s_ctrl,
1629 .vidioc_g_audio = si470x_vidioc_g_audio,
1630 .vidioc_g_tuner = si470x_vidioc_g_tuner,
1631 .vidioc_s_tuner = si470x_vidioc_s_tuner,
1632 .vidioc_g_frequency = si470x_vidioc_g_frequency,
1633 .vidioc_s_frequency = si470x_vidioc_s_frequency,
1634 .vidioc_s_hw_freq_seek = si470x_vidioc_s_hw_freq_seek,
1639 * si470x_viddev_template - video device interface
1641 static struct video_device si470x_viddev_template = {
1642 .fops = &si470x_fops,
1643 .name = DRIVER_NAME,
1644 .release = video_device_release,
1645 .ioctl_ops = &si470x_ioctl_ops,
1650 /**************************************************************************
1652 **************************************************************************/
1655 * si470x_usb_driver_probe - probe for the device
1657 static int si470x_usb_driver_probe(struct usb_interface *intf,
1658 const struct usb_device_id *id)
1660 struct si470x_device *radio;
1663 /* private data allocation and initialization */
1664 radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL);
1670 radio->disconnected = 0;
1671 radio->usbdev = interface_to_usbdev(intf);
1673 mutex_init(&radio->disconnect_lock);
1674 mutex_init(&radio->lock);
1676 /* video device allocation and initialization */
1677 radio->videodev = video_device_alloc();
1678 if (!radio->videodev) {
1682 memcpy(radio->videodev, &si470x_viddev_template,
1683 sizeof(si470x_viddev_template));
1684 video_set_drvdata(radio->videodev, radio);
1686 /* show some infos about the specific si470x device */
1687 if (si470x_get_all_registers(radio) < 0) {
1691 printk(KERN_INFO DRIVER_NAME ": DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",
1692 radio->registers[DEVICEID], radio->registers[CHIPID]);
1694 /* get software and hardware versions */
1695 if (si470x_get_scratch_page_versions(radio) < 0) {
1699 printk(KERN_INFO DRIVER_NAME
1700 ": software version %d, hardware version %d\n",
1701 radio->software_version, radio->hardware_version);
1703 /* check if device and firmware is current */
1704 if ((radio->registers[CHIPID] & CHIPID_FIRMWARE)
1705 < RADIO_SW_VERSION_CURRENT) {
1706 printk(KERN_WARNING DRIVER_NAME
1707 ": This driver is known to work with "
1708 "firmware version %hu,\n", RADIO_SW_VERSION_CURRENT);
1709 printk(KERN_WARNING DRIVER_NAME
1710 ": but the device has firmware version %hu.\n",
1711 radio->registers[CHIPID] & CHIPID_FIRMWARE);
1712 printk(KERN_WARNING DRIVER_NAME
1713 ": If you have some trouble using this driver,\n");
1714 printk(KERN_WARNING DRIVER_NAME
1715 ": please report to V4L ML at "
1716 "linux-media@vger.kernel.org\n");
1719 /* set initial frequency */
1720 si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
1722 /* set led to connect state */
1723 si470x_set_led_state(radio, BLINK_GREEN_LED);
1725 /* rds buffer allocation */
1726 radio->buf_size = rds_buf * 3;
1727 radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
1728 if (!radio->buffer) {
1733 /* rds buffer configuration */
1734 radio->wr_index = 0;
1735 radio->rd_index = 0;
1736 init_waitqueue_head(&radio->read_queue);
1738 /* prepare rds work function */
1739 INIT_DELAYED_WORK(&radio->work, si470x_work);
1741 /* register video device */
1742 retval = video_register_device(radio->videodev, VFL_TYPE_RADIO, radio_nr);
1744 printk(KERN_WARNING DRIVER_NAME
1745 ": Could not register video device\n");
1748 usb_set_intfdata(intf, radio);
1752 video_device_release(radio->videodev);
1753 kfree(radio->buffer);
1762 * si470x_usb_driver_suspend - suspend the device
1764 static int si470x_usb_driver_suspend(struct usb_interface *intf,
1765 pm_message_t message)
1767 struct si470x_device *radio = usb_get_intfdata(intf);
1769 printk(KERN_INFO DRIVER_NAME ": suspending now...\n");
1771 cancel_delayed_work_sync(&radio->work);
1778 * si470x_usb_driver_resume - resume the device
1780 static int si470x_usb_driver_resume(struct usb_interface *intf)
1782 struct si470x_device *radio = usb_get_intfdata(intf);
1784 printk(KERN_INFO DRIVER_NAME ": resuming now...\n");
1786 mutex_lock(&radio->lock);
1787 if (radio->users && radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS)
1788 schedule_delayed_work(&radio->work,
1789 msecs_to_jiffies(rds_poll_time));
1790 mutex_unlock(&radio->lock);
1797 * si470x_usb_driver_disconnect - disconnect the device
1799 static void si470x_usb_driver_disconnect(struct usb_interface *intf)
1801 struct si470x_device *radio = usb_get_intfdata(intf);
1803 mutex_lock(&radio->disconnect_lock);
1804 radio->disconnected = 1;
1805 cancel_delayed_work_sync(&radio->work);
1806 usb_set_intfdata(intf, NULL);
1807 if (radio->users == 0) {
1808 /* set led to disconnect state */
1809 si470x_set_led_state(radio, BLINK_ORANGE_LED);
1811 video_unregister_device(radio->videodev);
1812 kfree(radio->buffer);
1815 mutex_unlock(&radio->disconnect_lock);
1820 * si470x_usb_driver - usb driver interface
1822 static struct usb_driver si470x_usb_driver = {
1823 .name = DRIVER_NAME,
1824 .probe = si470x_usb_driver_probe,
1825 .disconnect = si470x_usb_driver_disconnect,
1826 .suspend = si470x_usb_driver_suspend,
1827 .resume = si470x_usb_driver_resume,
1828 .id_table = si470x_usb_driver_id_table,
1829 .supports_autosuspend = 1,
1834 /**************************************************************************
1836 **************************************************************************/
1839 * si470x_module_init - module init
1841 static int __init si470x_module_init(void)
1843 printk(KERN_INFO DRIVER_DESC ", Version " DRIVER_VERSION "\n");
1844 return usb_register(&si470x_usb_driver);
1849 * si470x_module_exit - module exit
1851 static void __exit si470x_module_exit(void)
1853 usb_deregister(&si470x_usb_driver);
1857 module_init(si470x_module_init);
1858 module_exit(si470x_module_exit);
1860 MODULE_LICENSE("GPL");
1861 MODULE_AUTHOR(DRIVER_AUTHOR);
1862 MODULE_DESCRIPTION(DRIVER_DESC);
1863 MODULE_VERSION(DRIVER_VERSION);