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
109 * 2009-06-16 Edouard Lafargue <edouard@lafargue.name>
111 * - add support for interrupt mode for RDS endpoint,
112 * instead of polling.
113 * Improves RDS reception significantly
116 * - add firmware download/update support
120 /* driver definitions */
121 #define DRIVER_AUTHOR "Tobias Lorenz <tobias.lorenz@gmx.net>"
122 #define DRIVER_NAME "radio-si470x"
123 #define DRIVER_KERNEL_VERSION KERNEL_VERSION(1, 0, 10)
124 #define DRIVER_CARD "Silicon Labs Si470x FM Radio Receiver"
125 #define DRIVER_DESC "USB radio driver for Si470x FM Radio Receivers"
126 #define DRIVER_VERSION "1.0.10"
129 /* kernel includes */
130 #include <linux/kernel.h>
131 #include <linux/module.h>
132 #include <linux/init.h>
133 #include <linux/slab.h>
134 #include <linux/smp_lock.h>
135 #include <linux/input.h>
136 #include <linux/usb.h>
137 #include <linux/hid.h>
138 #include <linux/version.h>
139 #include <linux/videodev2.h>
140 #include <linux/mutex.h>
141 #include <media/v4l2-common.h>
142 #include <media/v4l2-ioctl.h>
143 #include <media/rds.h>
144 #include <asm/unaligned.h>
147 /* USB Device ID List */
148 static struct usb_device_id si470x_usb_driver_id_table[] = {
149 /* Silicon Labs USB FM Radio Reference Design */
150 { USB_DEVICE_AND_INTERFACE_INFO(0x10c4, 0x818a, USB_CLASS_HID, 0, 0) },
151 /* ADS/Tech FM Radio Receiver (formerly Instant FM Music) */
152 { USB_DEVICE_AND_INTERFACE_INFO(0x06e1, 0xa155, USB_CLASS_HID, 0, 0) },
153 /* KWorld USB FM Radio SnapMusic Mobile 700 (FM700) */
154 { USB_DEVICE_AND_INTERFACE_INFO(0x1b80, 0xd700, USB_CLASS_HID, 0, 0) },
155 /* Sanei Electric, Inc. FM USB Radio (sold as DealExtreme.com PCear) */
156 { USB_DEVICE_AND_INTERFACE_INFO(0x10c5, 0x819a, USB_CLASS_HID, 0, 0) },
157 /* Terminating entry */
160 MODULE_DEVICE_TABLE(usb, si470x_usb_driver_id_table);
164 /**************************************************************************
166 **************************************************************************/
169 static int radio_nr = -1;
170 module_param(radio_nr, int, 0444);
171 MODULE_PARM_DESC(radio_nr, "Radio Nr");
174 /* 0: 200 kHz (USA, Australia) */
175 /* 1: 100 kHz (Europe, Japan) */
177 static unsigned short space = 2;
178 module_param(space, ushort, 0444);
179 MODULE_PARM_DESC(space, "Spacing: 0=200kHz 1=100kHz *2=50kHz*");
181 /* Bottom of Band (MHz) */
182 /* 0: 87.5 - 108 MHz (USA, Europe)*/
183 /* 1: 76 - 108 MHz (Japan wide band) */
184 /* 2: 76 - 90 MHz (Japan) */
185 static unsigned short band = 1;
186 module_param(band, ushort, 0444);
187 MODULE_PARM_DESC(band, "Band: 0=87.5..108MHz *1=76..108MHz* 2=76..90MHz");
191 /* 1: 50 us (Europe, Australia, Japan) */
192 static unsigned short de = 1;
193 module_param(de, ushort, 0444);
194 MODULE_PARM_DESC(de, "De-emphasis: 0=75us *1=50us*");
197 static unsigned int usb_timeout = 500;
198 module_param(usb_timeout, uint, 0644);
199 MODULE_PARM_DESC(usb_timeout, "USB timeout (ms): *500*");
202 static unsigned int tune_timeout = 3000;
203 module_param(tune_timeout, uint, 0644);
204 MODULE_PARM_DESC(tune_timeout, "Tune timeout: *3000*");
207 static unsigned int seek_timeout = 5000;
208 module_param(seek_timeout, uint, 0644);
209 MODULE_PARM_DESC(seek_timeout, "Seek timeout: *5000*");
211 /* RDS buffer blocks */
212 static unsigned int rds_buf = 100;
213 module_param(rds_buf, uint, 0444);
214 MODULE_PARM_DESC(rds_buf, "RDS buffer entries: *100*");
216 /* RDS maximum block errors */
217 static unsigned short max_rds_errors = 1;
218 /* 0 means 0 errors requiring correction */
219 /* 1 means 1-2 errors requiring correction (used by original USBRadio.exe) */
220 /* 2 means 3-5 errors requiring correction */
221 /* 3 means 6+ errors or errors in checkword, correction not possible */
222 module_param(max_rds_errors, ushort, 0644);
223 MODULE_PARM_DESC(max_rds_errors, "RDS maximum block errors: *1*");
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_SIZE 4
354 #define WRITE_REPORT 19
355 #define FLASH_REPORT_SIZE 64
356 #define FLASH_REPORT 20
357 #define CRC_REPORT_SIZE 3
358 #define CRC_REPORT 21
359 #define RESPONSE_REPORT_SIZE 2
360 #define RESPONSE_REPORT 22
362 /* Report 23: currently unused, but can accept 60 byte reports on the HID */
363 /* interrupt out endpoint 2 every 1 millisecond */
364 #define UNUSED_REPORT 23
368 /**************************************************************************
369 * Software/Hardware Versions
370 **************************************************************************/
371 #define RADIO_SW_VERSION_NOT_BOOTLOADABLE 6
372 #define RADIO_SW_VERSION 7
373 #define RADIO_SW_VERSION_CURRENT 15
374 #define RADIO_HW_VERSION 1
376 #define SCRATCH_PAGE_SW_VERSION 1
377 #define SCRATCH_PAGE_HW_VERSION 2
381 /**************************************************************************
382 * LED State Definitions
383 **************************************************************************/
384 #define LED_COMMAND 0x35
386 #define NO_CHANGE_LED 0x00
387 #define ALL_COLOR_LED 0x01 /* streaming state */
388 #define BLINK_GREEN_LED 0x02 /* connect state */
389 #define BLINK_RED_LED 0x04
390 #define BLINK_ORANGE_LED 0x10 /* disconnect state */
391 #define SOLID_GREEN_LED 0x20 /* tuning/seeking state */
392 #define SOLID_RED_LED 0x40 /* bootload state */
393 #define SOLID_ORANGE_LED 0x80
397 /**************************************************************************
398 * Stream State Definitions
399 **************************************************************************/
400 #define STREAM_COMMAND 0x36
401 #define STREAM_VIDPID 0x00
402 #define STREAM_AUDIO 0xff
406 /**************************************************************************
407 * Bootloader / Flash Commands
408 **************************************************************************/
410 /* unique id sent to bootloader and required to put into a bootload state */
411 #define UNIQUE_BL_ID 0x34
413 /* mask for the flash data */
414 #define FLASH_DATA_MASK 0x55
416 /* bootloader commands */
417 #define GET_SW_VERSION_COMMAND 0x00
418 #define SET_PAGE_COMMAND 0x01
419 #define ERASE_PAGE_COMMAND 0x02
420 #define WRITE_PAGE_COMMAND 0x03
421 #define CRC_ON_PAGE_COMMAND 0x04
422 #define READ_FLASH_BYTE_COMMAND 0x05
423 #define RESET_DEVICE_COMMAND 0x06
424 #define GET_HW_VERSION_COMMAND 0x07
427 /* bootloader command responses */
428 #define COMMAND_OK 0x01
429 #define COMMAND_FAILED 0x02
430 #define COMMAND_PENDING 0x03
434 /**************************************************************************
435 * General Driver Definitions
436 **************************************************************************/
439 * si470x_device - private data
441 struct si470x_device {
442 /* reference to USB and video device */
443 struct usb_device *usbdev;
444 struct usb_interface *intf;
445 struct video_device *videodev;
447 /* Interrupt endpoint handling */
449 struct usb_endpoint_descriptor *int_in_endpoint;
450 struct urb *int_in_urb;
453 /* driver management */
455 unsigned char disconnected;
456 struct mutex disconnect_lock;
458 /* Silabs internal registers (0..15) */
459 unsigned short registers[RADIO_REGISTER_NUM];
461 /* RDS receive buffer */
462 wait_queue_head_t read_queue;
463 struct mutex lock; /* buffer locking */
464 unsigned char *buffer; /* size is always multiple of three */
465 unsigned int buf_size;
466 unsigned int rd_index;
467 unsigned int wr_index;
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 - LED_REPORT
868 **************************************************************************/
871 * si470x_set_led_state - sets the led state
873 static int si470x_set_led_state(struct si470x_device *radio,
874 unsigned char led_state)
876 unsigned char buf[LED_REPORT_SIZE];
880 buf[1] = LED_COMMAND;
883 retval = si470x_set_report(radio, (void *) &buf, sizeof(buf));
885 return (retval < 0) ? -EINVAL : 0;
890 /**************************************************************************
891 * General Driver Functions - SCRATCH_REPORT
892 **************************************************************************/
895 * si470x_get_scratch_versions - gets the scratch page and version infos
897 static int si470x_get_scratch_page_versions(struct si470x_device *radio)
899 unsigned char buf[SCRATCH_REPORT_SIZE];
902 buf[0] = SCRATCH_REPORT;
904 retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
907 printk(KERN_WARNING DRIVER_NAME ": si470x_get_scratch: "
908 "si470x_get_report returned %d\n", retval);
910 radio->software_version = buf[1];
911 radio->hardware_version = buf[2];
914 return (retval < 0) ? -EINVAL : 0;
919 /**************************************************************************
920 * RDS Driver Functions
921 **************************************************************************/
924 * si470x_int_in_callback - rds callback and processing function
926 * TODO: do we need to use mutex locks in some sections?
928 static void si470x_int_in_callback(struct urb *urb)
930 struct si470x_device *radio = urb->context;
931 unsigned char buf[RDS_REPORT_SIZE];
934 unsigned char blocknum;
935 unsigned short bler; /* rds block errors */
937 unsigned char tmpbuf[3];
940 if (urb->status == -ENOENT ||
941 urb->status == -ECONNRESET ||
942 urb->status == -ESHUTDOWN) {
945 printk(KERN_WARNING DRIVER_NAME
946 ": non-zero urb status (%d)\n", urb->status);
947 goto resubmit; /* Maybe we can recover. */
952 if (radio->disconnected)
954 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
957 if (urb->actual_length > 0) {
958 /* Update RDS registers with URB data */
960 for (regnr = 0; regnr < RDS_REGISTER_NUM; regnr++)
961 radio->registers[STATUSRSSI + regnr] =
962 get_unaligned_be16(&radio->int_in_buffer[
963 regnr * RADIO_REGISTER_SIZE + 1]);
965 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0) {
966 /* No RDS group ready, better luck next time */
969 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSS) == 0) {
970 /* RDS decoder not synchronized */
973 for (blocknum = 0; blocknum < 4; blocknum++) {
976 bler = (radio->registers[STATUSRSSI] &
977 STATUSRSSI_BLERA) >> 9;
978 rds = radio->registers[RDSA];
981 bler = (radio->registers[READCHAN] &
982 READCHAN_BLERB) >> 14;
983 rds = radio->registers[RDSB];
986 bler = (radio->registers[READCHAN] &
987 READCHAN_BLERC) >> 12;
988 rds = radio->registers[RDSC];
991 bler = (radio->registers[READCHAN] &
992 READCHAN_BLERD) >> 10;
993 rds = radio->registers[RDSD];
997 /* Fill the V4L2 RDS buffer */
998 put_unaligned_le16(rds, &tmpbuf);
999 tmpbuf[2] = blocknum; /* offset name */
1000 tmpbuf[2] |= blocknum << 3; /* received offset */
1001 if (bler > max_rds_errors)
1002 tmpbuf[2] |= 0x80; /* uncorrectable errors */
1004 tmpbuf[2] |= 0x40; /* corrected error(s) */
1006 /* copy RDS block to internal buffer */
1007 memcpy(&radio->buffer[radio->wr_index], &tmpbuf, 3);
1008 radio->wr_index += 3;
1010 /* wrap write pointer */
1011 if (radio->wr_index >= radio->buf_size)
1012 radio->wr_index = 0;
1014 /* check for overflow */
1015 if (radio->wr_index == radio->rd_index) {
1016 /* increment and wrap read pointer */
1017 radio->rd_index += 3;
1018 if (radio->rd_index >= radio->buf_size)
1019 radio->rd_index = 0;
1022 if (radio->wr_index != radio->rd_index)
1023 wake_up_interruptible(&radio->read_queue);
1027 /* Resubmit if we're still running. */
1028 if (radio->int_in_running && radio->usbdev) {
1029 retval = usb_submit_urb(radio->int_in_urb, GFP_ATOMIC);
1031 printk(KERN_WARNING DRIVER_NAME
1032 ": resubmitting urb failed (%d)", retval);
1033 radio->int_in_running = 0;
1040 /**************************************************************************
1041 * File Operations Interface
1042 **************************************************************************/
1045 * si470x_fops_read - read RDS data
1047 static ssize_t si470x_fops_read(struct file *file, char __user *buf,
1048 size_t count, loff_t *ppos)
1050 struct si470x_device *radio = video_drvdata(file);
1052 unsigned int block_count = 0;
1054 /* switch on rds reception */
1055 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
1056 si470x_rds_on(radio);
1059 /* block if no new data available */
1060 while (radio->wr_index == radio->rd_index) {
1061 if (file->f_flags & O_NONBLOCK) {
1062 retval = -EWOULDBLOCK;
1065 if (wait_event_interruptible(radio->read_queue,
1066 radio->wr_index != radio->rd_index) < 0) {
1072 /* calculate block count from byte count */
1075 /* copy RDS block out of internal buffer and to user buffer */
1076 mutex_lock(&radio->lock);
1077 while (block_count < count) {
1078 if (radio->rd_index == radio->wr_index)
1081 /* always transfer rds complete blocks */
1082 if (copy_to_user(buf, &radio->buffer[radio->rd_index], 3))
1083 /* retval = -EFAULT; */
1086 /* increment and wrap read pointer */
1087 radio->rd_index += 3;
1088 if (radio->rd_index >= radio->buf_size)
1089 radio->rd_index = 0;
1091 /* increment counters */
1096 mutex_unlock(&radio->lock);
1104 * si470x_fops_poll - poll RDS data
1106 static unsigned int si470x_fops_poll(struct file *file,
1107 struct poll_table_struct *pts)
1109 struct si470x_device *radio = video_drvdata(file);
1112 /* switch on rds reception */
1113 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
1114 si470x_rds_on(radio);
1117 poll_wait(file, &radio->read_queue, pts);
1119 if (radio->rd_index != radio->wr_index)
1120 retval = POLLIN | POLLRDNORM;
1127 * si470x_fops_open - file open
1129 static int si470x_fops_open(struct file *file)
1131 struct si470x_device *radio = video_drvdata(file);
1137 retval = usb_autopm_get_interface(radio->intf);
1144 if (radio->users == 1) {
1146 retval = si470x_start(radio);
1148 usb_autopm_put_interface(radio->intf);
1152 /* initialize interrupt urb */
1153 usb_fill_int_urb(radio->int_in_urb, radio->usbdev,
1154 usb_rcvintpipe(radio->usbdev,
1155 radio->int_in_endpoint->bEndpointAddress),
1156 radio->int_in_buffer,
1157 le16_to_cpu(radio->int_in_endpoint->wMaxPacketSize),
1158 si470x_int_in_callback,
1160 radio->int_in_endpoint->bInterval);
1162 radio->int_in_running = 1;
1165 retval = usb_submit_urb(radio->int_in_urb, GFP_KERNEL);
1167 printk(KERN_INFO DRIVER_NAME
1168 ": submitting int urb failed (%d)\n", retval);
1169 radio->int_in_running = 0;
1170 usb_autopm_put_interface(radio->intf);
1181 * si470x_fops_release - file release
1183 static int si470x_fops_release(struct file *file)
1185 struct si470x_device *radio = video_drvdata(file);
1194 mutex_lock(&radio->disconnect_lock);
1196 if (radio->users == 0) {
1197 /* shutdown interrupt handler */
1198 if (radio->int_in_running) {
1199 radio->int_in_running = 0;
1200 if (radio->int_in_urb)
1201 usb_kill_urb(radio->int_in_urb);
1204 if (radio->disconnected) {
1205 video_unregister_device(radio->videodev);
1206 kfree(radio->int_in_buffer);
1207 kfree(radio->buffer);
1212 /* cancel read processes */
1213 wake_up_interruptible(&radio->read_queue);
1216 retval = si470x_stop(radio);
1217 usb_autopm_put_interface(radio->intf);
1220 mutex_unlock(&radio->disconnect_lock);
1227 * si470x_fops - file operations interface
1229 static const struct v4l2_file_operations si470x_fops = {
1230 .owner = THIS_MODULE,
1231 .read = si470x_fops_read,
1232 .poll = si470x_fops_poll,
1233 .ioctl = video_ioctl2,
1234 .open = si470x_fops_open,
1235 .release = si470x_fops_release,
1240 /**************************************************************************
1241 * Video4Linux Interface
1242 **************************************************************************/
1245 * si470x_vidioc_querycap - query device capabilities
1247 static int si470x_vidioc_querycap(struct file *file, void *priv,
1248 struct v4l2_capability *capability)
1250 struct si470x_device *radio = video_drvdata(file);
1252 strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver));
1253 strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
1254 usb_make_path(radio->usbdev, capability->bus_info, sizeof(capability->bus_info));
1255 capability->version = DRIVER_KERNEL_VERSION;
1256 capability->capabilities = V4L2_CAP_HW_FREQ_SEEK |
1257 V4L2_CAP_TUNER | V4L2_CAP_RADIO | V4L2_CAP_RDS_CAPTURE;
1264 * si470x_vidioc_queryctrl - enumerate control items
1266 static int si470x_vidioc_queryctrl(struct file *file, void *priv,
1267 struct v4l2_queryctrl *qc)
1269 int retval = -EINVAL;
1271 /* abort if qc->id is below V4L2_CID_BASE */
1272 if (qc->id < V4L2_CID_BASE)
1275 /* search video control */
1277 case V4L2_CID_AUDIO_VOLUME:
1278 return v4l2_ctrl_query_fill(qc, 0, 15, 1, 15);
1279 case V4L2_CID_AUDIO_MUTE:
1280 return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
1283 /* disable unsupported base controls */
1284 /* to satisfy kradio and such apps */
1285 if ((retval == -EINVAL) && (qc->id < V4L2_CID_LASTP1)) {
1286 qc->flags = V4L2_CTRL_FLAG_DISABLED;
1292 printk(KERN_WARNING DRIVER_NAME
1293 ": query controls failed with %d\n", retval);
1299 * si470x_vidioc_g_ctrl - get the value of a control
1301 static int si470x_vidioc_g_ctrl(struct file *file, void *priv,
1302 struct v4l2_control *ctrl)
1304 struct si470x_device *radio = video_drvdata(file);
1308 if (radio->disconnected) {
1314 case V4L2_CID_AUDIO_VOLUME:
1315 ctrl->value = radio->registers[SYSCONFIG2] &
1318 case V4L2_CID_AUDIO_MUTE:
1319 ctrl->value = ((radio->registers[POWERCFG] &
1320 POWERCFG_DMUTE) == 0) ? 1 : 0;
1328 printk(KERN_WARNING DRIVER_NAME
1329 ": get control failed with %d\n", retval);
1335 * si470x_vidioc_s_ctrl - set the value of a control
1337 static int si470x_vidioc_s_ctrl(struct file *file, void *priv,
1338 struct v4l2_control *ctrl)
1340 struct si470x_device *radio = video_drvdata(file);
1344 if (radio->disconnected) {
1350 case V4L2_CID_AUDIO_VOLUME:
1351 radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_VOLUME;
1352 radio->registers[SYSCONFIG2] |= ctrl->value;
1353 retval = si470x_set_register(radio, SYSCONFIG2);
1355 case V4L2_CID_AUDIO_MUTE:
1356 if (ctrl->value == 1)
1357 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
1359 radio->registers[POWERCFG] |= POWERCFG_DMUTE;
1360 retval = si470x_set_register(radio, POWERCFG);
1368 printk(KERN_WARNING DRIVER_NAME
1369 ": set control failed with %d\n", retval);
1375 * si470x_vidioc_g_audio - get audio attributes
1377 static int si470x_vidioc_g_audio(struct file *file, void *priv,
1378 struct v4l2_audio *audio)
1380 /* driver constants */
1382 strcpy(audio->name, "Radio");
1383 audio->capability = V4L2_AUDCAP_STEREO;
1391 * si470x_vidioc_g_tuner - get tuner attributes
1393 static int si470x_vidioc_g_tuner(struct file *file, void *priv,
1394 struct v4l2_tuner *tuner)
1396 struct si470x_device *radio = video_drvdata(file);
1400 if (radio->disconnected) {
1404 if (tuner->index != 0) {
1409 retval = si470x_get_register(radio, STATUSRSSI);
1413 /* driver constants */
1414 strcpy(tuner->name, "FM");
1415 tuner->type = V4L2_TUNER_RADIO;
1416 tuner->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
1420 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
1421 /* 0: 87.5 - 108 MHz (USA, Europe, default) */
1423 tuner->rangelow = 87.5 * FREQ_MUL;
1424 tuner->rangehigh = 108 * FREQ_MUL;
1426 /* 1: 76 - 108 MHz (Japan wide band) */
1428 tuner->rangelow = 76 * FREQ_MUL;
1429 tuner->rangehigh = 108 * FREQ_MUL;
1431 /* 2: 76 - 90 MHz (Japan) */
1433 tuner->rangelow = 76 * FREQ_MUL;
1434 tuner->rangehigh = 90 * FREQ_MUL;
1438 /* stereo indicator == stereo (instead of mono) */
1439 if ((radio->registers[STATUSRSSI] & STATUSRSSI_ST) == 0)
1440 tuner->rxsubchans = V4L2_TUNER_SUB_MONO;
1442 tuner->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
1443 /* If there is a reliable method of detecting an RDS channel,
1444 then this code should check for that before setting this
1446 tuner->rxsubchans |= V4L2_TUNER_SUB_RDS;
1448 /* mono/stereo selector */
1449 if ((radio->registers[POWERCFG] & POWERCFG_MONO) == 0)
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 /* measured in units of dbµV in 1 db increments (max at ~75 dbµV) */
1456 tuner->signal = (radio->registers[STATUSRSSI] & STATUSRSSI_RSSI);
1457 /* the ideal factor is 0xffff/75 = 873,8 */
1458 tuner->signal = (tuner->signal * 873) + (8 * tuner->signal / 10);
1460 /* automatic frequency control: -1: freq to low, 1 freq to high */
1461 /* AFCRL does only indicate that freq. differs, not if too low/high */
1462 tuner->afc = (radio->registers[STATUSRSSI] & STATUSRSSI_AFCRL) ? 1 : 0;
1466 printk(KERN_WARNING DRIVER_NAME
1467 ": get tuner failed with %d\n", retval);
1473 * si470x_vidioc_s_tuner - set tuner attributes
1475 static int si470x_vidioc_s_tuner(struct file *file, void *priv,
1476 struct v4l2_tuner *tuner)
1478 struct si470x_device *radio = video_drvdata(file);
1479 int retval = -EINVAL;
1482 if (radio->disconnected) {
1486 if (tuner->index != 0)
1489 /* mono/stereo selector */
1490 switch (tuner->audmode) {
1491 case V4L2_TUNER_MODE_MONO:
1492 radio->registers[POWERCFG] |= POWERCFG_MONO; /* force mono */
1494 case V4L2_TUNER_MODE_STEREO:
1495 radio->registers[POWERCFG] &= ~POWERCFG_MONO; /* try stereo */
1501 retval = si470x_set_register(radio, POWERCFG);
1505 printk(KERN_WARNING DRIVER_NAME
1506 ": set tuner failed with %d\n", retval);
1512 * si470x_vidioc_g_frequency - get tuner or modulator radio frequency
1514 static int si470x_vidioc_g_frequency(struct file *file, void *priv,
1515 struct v4l2_frequency *freq)
1517 struct si470x_device *radio = video_drvdata(file);
1521 if (radio->disconnected) {
1525 if (freq->tuner != 0) {
1530 freq->type = V4L2_TUNER_RADIO;
1531 retval = si470x_get_freq(radio, &freq->frequency);
1535 printk(KERN_WARNING DRIVER_NAME
1536 ": get frequency failed with %d\n", retval);
1542 * si470x_vidioc_s_frequency - set tuner or modulator radio frequency
1544 static int si470x_vidioc_s_frequency(struct file *file, void *priv,
1545 struct v4l2_frequency *freq)
1547 struct si470x_device *radio = video_drvdata(file);
1551 if (radio->disconnected) {
1555 if (freq->tuner != 0) {
1560 retval = si470x_set_freq(radio, freq->frequency);
1564 printk(KERN_WARNING DRIVER_NAME
1565 ": set frequency failed with %d\n", retval);
1571 * si470x_vidioc_s_hw_freq_seek - set hardware frequency seek
1573 static int si470x_vidioc_s_hw_freq_seek(struct file *file, void *priv,
1574 struct v4l2_hw_freq_seek *seek)
1576 struct si470x_device *radio = video_drvdata(file);
1580 if (radio->disconnected) {
1584 if (seek->tuner != 0) {
1589 retval = si470x_set_seek(radio, seek->wrap_around, seek->seek_upward);
1593 printk(KERN_WARNING DRIVER_NAME
1594 ": set hardware frequency seek failed with %d\n",
1601 * si470x_ioctl_ops - video device ioctl operations
1603 static const struct v4l2_ioctl_ops si470x_ioctl_ops = {
1604 .vidioc_querycap = si470x_vidioc_querycap,
1605 .vidioc_queryctrl = si470x_vidioc_queryctrl,
1606 .vidioc_g_ctrl = si470x_vidioc_g_ctrl,
1607 .vidioc_s_ctrl = si470x_vidioc_s_ctrl,
1608 .vidioc_g_audio = si470x_vidioc_g_audio,
1609 .vidioc_g_tuner = si470x_vidioc_g_tuner,
1610 .vidioc_s_tuner = si470x_vidioc_s_tuner,
1611 .vidioc_g_frequency = si470x_vidioc_g_frequency,
1612 .vidioc_s_frequency = si470x_vidioc_s_frequency,
1613 .vidioc_s_hw_freq_seek = si470x_vidioc_s_hw_freq_seek,
1618 * si470x_viddev_template - video device interface
1620 static struct video_device si470x_viddev_template = {
1621 .fops = &si470x_fops,
1622 .name = DRIVER_NAME,
1623 .release = video_device_release,
1624 .ioctl_ops = &si470x_ioctl_ops,
1629 /**************************************************************************
1631 **************************************************************************/
1634 * si470x_usb_driver_probe - probe for the device
1636 static int si470x_usb_driver_probe(struct usb_interface *intf,
1637 const struct usb_device_id *id)
1639 struct si470x_device *radio;
1640 struct usb_host_interface *iface_desc;
1641 struct usb_endpoint_descriptor *endpoint;
1642 int i, int_end_size, retval = 0;
1644 /* private data allocation and initialization */
1645 radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL);
1651 radio->disconnected = 0;
1652 radio->usbdev = interface_to_usbdev(intf);
1654 mutex_init(&radio->disconnect_lock);
1655 mutex_init(&radio->lock);
1657 iface_desc = intf->cur_altsetting;
1659 /* Set up interrupt endpoint information. */
1660 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1661 endpoint = &iface_desc->endpoint[i].desc;
1662 if (((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
1663 USB_DIR_IN) && ((endpoint->bmAttributes &
1664 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT))
1665 radio->int_in_endpoint = endpoint;
1667 if (!radio->int_in_endpoint) {
1668 printk(KERN_INFO DRIVER_NAME
1669 ": could not find interrupt in endpoint\n");
1674 int_end_size = le16_to_cpu(radio->int_in_endpoint->wMaxPacketSize);
1676 radio->int_in_buffer = kmalloc(int_end_size, GFP_KERNEL);
1677 if (!radio->int_in_buffer) {
1678 printk(KERN_INFO DRIVER_NAME
1679 "could not allocate int_in_buffer");
1684 radio->int_in_urb = usb_alloc_urb(0, GFP_KERNEL);
1685 if (!radio->int_in_urb) {
1686 printk(KERN_INFO DRIVER_NAME "could not allocate int_in_urb");
1691 /* video device allocation and initialization */
1692 radio->videodev = video_device_alloc();
1693 if (!radio->videodev) {
1697 memcpy(radio->videodev, &si470x_viddev_template,
1698 sizeof(si470x_viddev_template));
1699 video_set_drvdata(radio->videodev, radio);
1701 /* show some infos about the specific si470x device */
1702 if (si470x_get_all_registers(radio) < 0) {
1706 printk(KERN_INFO DRIVER_NAME ": DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",
1707 radio->registers[DEVICEID], radio->registers[CHIPID]);
1709 /* get software and hardware versions */
1710 if (si470x_get_scratch_page_versions(radio) < 0) {
1714 printk(KERN_INFO DRIVER_NAME
1715 ": software version %d, hardware version %d\n",
1716 radio->software_version, radio->hardware_version);
1718 /* check if device and firmware is current */
1719 if ((radio->registers[CHIPID] & CHIPID_FIRMWARE)
1720 < RADIO_SW_VERSION_CURRENT) {
1721 printk(KERN_WARNING DRIVER_NAME
1722 ": This driver is known to work with "
1723 "firmware version %hu,\n", RADIO_SW_VERSION_CURRENT);
1724 printk(KERN_WARNING DRIVER_NAME
1725 ": but the device has firmware version %hu.\n",
1726 radio->registers[CHIPID] & CHIPID_FIRMWARE);
1727 printk(KERN_WARNING DRIVER_NAME
1728 ": If you have some trouble using this driver,\n");
1729 printk(KERN_WARNING DRIVER_NAME
1730 ": please report to V4L ML at "
1731 "linux-media@vger.kernel.org\n");
1734 /* set initial frequency */
1735 si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
1737 /* set led to connect state */
1738 si470x_set_led_state(radio, BLINK_GREEN_LED);
1740 /* rds buffer allocation */
1741 radio->buf_size = rds_buf * 3;
1742 radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
1743 if (!radio->buffer) {
1748 /* rds buffer configuration */
1749 radio->wr_index = 0;
1750 radio->rd_index = 0;
1751 init_waitqueue_head(&radio->read_queue);
1753 /* register video device */
1754 retval = video_register_device(radio->videodev, VFL_TYPE_RADIO, radio_nr);
1756 printk(KERN_WARNING DRIVER_NAME
1757 ": Could not register video device\n");
1760 usb_set_intfdata(intf, radio);
1764 kfree(radio->buffer);
1766 video_device_release(radio->videodev);
1768 kfree(radio->int_in_buffer);
1777 * si470x_usb_driver_suspend - suspend the device
1779 static int si470x_usb_driver_suspend(struct usb_interface *intf,
1780 pm_message_t message)
1782 printk(KERN_INFO DRIVER_NAME ": suspending now...\n");
1789 * si470x_usb_driver_resume - resume the device
1791 static int si470x_usb_driver_resume(struct usb_interface *intf)
1793 printk(KERN_INFO DRIVER_NAME ": resuming now...\n");
1800 * si470x_usb_driver_disconnect - disconnect the device
1802 static void si470x_usb_driver_disconnect(struct usb_interface *intf)
1804 struct si470x_device *radio = usb_get_intfdata(intf);
1806 mutex_lock(&radio->disconnect_lock);
1807 radio->disconnected = 1;
1808 usb_set_intfdata(intf, NULL);
1809 if (radio->users == 0) {
1810 /* set led to disconnect state */
1811 si470x_set_led_state(radio, BLINK_ORANGE_LED);
1813 /* Free data structures. */
1814 usb_free_urb(radio->int_in_urb);
1816 kfree(radio->int_in_buffer);
1817 video_unregister_device(radio->videodev);
1818 kfree(radio->buffer);
1821 mutex_unlock(&radio->disconnect_lock);
1826 * si470x_usb_driver - usb driver interface
1828 static struct usb_driver si470x_usb_driver = {
1829 .name = DRIVER_NAME,
1830 .probe = si470x_usb_driver_probe,
1831 .disconnect = si470x_usb_driver_disconnect,
1832 .suspend = si470x_usb_driver_suspend,
1833 .resume = si470x_usb_driver_resume,
1834 .id_table = si470x_usb_driver_id_table,
1835 .supports_autosuspend = 1,
1840 /**************************************************************************
1842 **************************************************************************/
1845 * si470x_module_init - module init
1847 static int __init si470x_module_init(void)
1849 printk(KERN_INFO DRIVER_DESC ", Version " DRIVER_VERSION "\n");
1850 return usb_register(&si470x_usb_driver);
1855 * si470x_module_exit - module exit
1857 static void __exit si470x_module_exit(void)
1859 usb_deregister(&si470x_usb_driver);
1863 module_init(si470x_module_init);
1864 module_exit(si470x_module_exit);
1866 MODULE_LICENSE("GPL");
1867 MODULE_AUTHOR(DRIVER_AUTHOR);
1868 MODULE_DESCRIPTION(DRIVER_DESC);
1869 MODULE_VERSION(DRIVER_VERSION);