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
110 * - add firmware download/update support
111 * - RDS support: interrupt mode, instead of polling
115 /* driver definitions */
116 #define DRIVER_AUTHOR "Tobias Lorenz <tobias.lorenz@gmx.net>"
117 #define DRIVER_NAME "radio-si470x"
118 #define DRIVER_KERNEL_VERSION KERNEL_VERSION(1, 0, 8)
119 #define DRIVER_CARD "Silicon Labs Si470x FM Radio Receiver"
120 #define DRIVER_DESC "USB radio driver for Si470x FM Radio Receivers"
121 #define DRIVER_VERSION "1.0.8"
124 /* kernel includes */
125 #include <linux/kernel.h>
126 #include <linux/module.h>
127 #include <linux/init.h>
128 #include <linux/slab.h>
129 #include <linux/input.h>
130 #include <linux/usb.h>
131 #include <linux/hid.h>
132 #include <linux/version.h>
133 #include <linux/videodev2.h>
134 #include <linux/mutex.h>
135 #include <media/v4l2-common.h>
136 #include <media/v4l2-ioctl.h>
137 #include <media/rds.h>
138 #include <asm/unaligned.h>
141 /* USB Device ID List */
142 static struct usb_device_id si470x_usb_driver_id_table[] = {
143 /* Silicon Labs USB FM Radio Reference Design */
144 { USB_DEVICE_AND_INTERFACE_INFO(0x10c4, 0x818a, USB_CLASS_HID, 0, 0) },
145 /* ADS/Tech FM Radio Receiver (formerly Instant FM Music) */
146 { USB_DEVICE_AND_INTERFACE_INFO(0x06e1, 0xa155, USB_CLASS_HID, 0, 0) },
147 /* KWorld USB FM Radio SnapMusic Mobile 700 (FM700) */
148 { USB_DEVICE_AND_INTERFACE_INFO(0x1b80, 0xd700, USB_CLASS_HID, 0, 0) },
149 /* Sanei Electric, Inc. FM USB Radio (sold as DealExtreme.com PCear) */
150 { USB_DEVICE_AND_INTERFACE_INFO(0x10c5, 0x819a, USB_CLASS_HID, 0, 0) },
151 /* Terminating entry */
154 MODULE_DEVICE_TABLE(usb, si470x_usb_driver_id_table);
158 /**************************************************************************
160 **************************************************************************/
163 static int radio_nr = -1;
164 module_param(radio_nr, int, 0444);
165 MODULE_PARM_DESC(radio_nr, "Radio Nr");
168 /* 0: 200 kHz (USA, Australia) */
169 /* 1: 100 kHz (Europe, Japan) */
171 static unsigned short space = 2;
172 module_param(space, ushort, 0444);
173 MODULE_PARM_DESC(space, "Spacing: 0=200kHz 1=100kHz *2=50kHz*");
175 /* Bottom of Band (MHz) */
176 /* 0: 87.5 - 108 MHz (USA, Europe)*/
177 /* 1: 76 - 108 MHz (Japan wide band) */
178 /* 2: 76 - 90 MHz (Japan) */
179 static unsigned short band = 1;
180 module_param(band, ushort, 0444);
181 MODULE_PARM_DESC(band, "Band: 0=87.5..108MHz *1=76..108MHz* 2=76..90MHz");
185 /* 1: 50 us (Europe, Australia, Japan) */
186 static unsigned short de = 1;
187 module_param(de, ushort, 0444);
188 MODULE_PARM_DESC(de, "De-emphasis: 0=75us *1=50us*");
191 static unsigned int usb_timeout = 500;
192 module_param(usb_timeout, uint, 0644);
193 MODULE_PARM_DESC(usb_timeout, "USB timeout (ms): *500*");
196 static unsigned int tune_timeout = 3000;
197 module_param(tune_timeout, uint, 0644);
198 MODULE_PARM_DESC(tune_timeout, "Tune timeout: *3000*");
201 static unsigned int seek_timeout = 5000;
202 module_param(seek_timeout, uint, 0644);
203 MODULE_PARM_DESC(seek_timeout, "Seek timeout: *5000*");
205 /* RDS buffer blocks */
206 static unsigned int rds_buf = 100;
207 module_param(rds_buf, uint, 0444);
208 MODULE_PARM_DESC(rds_buf, "RDS buffer entries: *100*");
210 /* RDS maximum block errors */
211 static unsigned short max_rds_errors = 1;
212 /* 0 means 0 errors requiring correction */
213 /* 1 means 1-2 errors requiring correction (used by original USBRadio.exe) */
214 /* 2 means 3-5 errors requiring correction */
215 /* 3 means 6+ errors or errors in checkword, correction not possible */
216 module_param(max_rds_errors, ushort, 0644);
217 MODULE_PARM_DESC(max_rds_errors, "RDS maximum block errors: *1*");
219 /* RDS poll frequency */
220 static unsigned int rds_poll_time = 40;
221 /* 40 is used by the original USBRadio.exe */
222 /* 50 is used by radio-cadet */
223 /* 75 should be okay */
224 /* 80 is the usual RDS receive interval */
225 module_param(rds_poll_time, uint, 0644);
226 MODULE_PARM_DESC(rds_poll_time, "RDS poll time (ms): *40*");
230 /**************************************************************************
231 * Register Definitions
232 **************************************************************************/
233 #define RADIO_REGISTER_SIZE 2 /* 16 register bit width */
234 #define RADIO_REGISTER_NUM 16 /* DEVICEID ... RDSD */
235 #define RDS_REGISTER_NUM 6 /* STATUSRSSI ... RDSD */
237 #define DEVICEID 0 /* Device ID */
238 #define DEVICEID_PN 0xf000 /* bits 15..12: Part Number */
239 #define DEVICEID_MFGID 0x0fff /* bits 11..00: Manufacturer ID */
241 #define CHIPID 1 /* Chip ID */
242 #define CHIPID_REV 0xfc00 /* bits 15..10: Chip Version */
243 #define CHIPID_DEV 0x0200 /* bits 09..09: Device */
244 #define CHIPID_FIRMWARE 0x01ff /* bits 08..00: Firmware Version */
246 #define POWERCFG 2 /* Power Configuration */
247 #define POWERCFG_DSMUTE 0x8000 /* bits 15..15: Softmute Disable */
248 #define POWERCFG_DMUTE 0x4000 /* bits 14..14: Mute Disable */
249 #define POWERCFG_MONO 0x2000 /* bits 13..13: Mono Select */
250 #define POWERCFG_RDSM 0x0800 /* bits 11..11: RDS Mode (Si4701 only) */
251 #define POWERCFG_SKMODE 0x0400 /* bits 10..10: Seek Mode */
252 #define POWERCFG_SEEKUP 0x0200 /* bits 09..09: Seek Direction */
253 #define POWERCFG_SEEK 0x0100 /* bits 08..08: Seek */
254 #define POWERCFG_DISABLE 0x0040 /* bits 06..06: Powerup Disable */
255 #define POWERCFG_ENABLE 0x0001 /* bits 00..00: Powerup Enable */
257 #define CHANNEL 3 /* Channel */
258 #define CHANNEL_TUNE 0x8000 /* bits 15..15: Tune */
259 #define CHANNEL_CHAN 0x03ff /* bits 09..00: Channel Select */
261 #define SYSCONFIG1 4 /* System Configuration 1 */
262 #define SYSCONFIG1_RDSIEN 0x8000 /* bits 15..15: RDS Interrupt Enable (Si4701 only) */
263 #define SYSCONFIG1_STCIEN 0x4000 /* bits 14..14: Seek/Tune Complete Interrupt Enable */
264 #define SYSCONFIG1_RDS 0x1000 /* bits 12..12: RDS Enable (Si4701 only) */
265 #define SYSCONFIG1_DE 0x0800 /* bits 11..11: De-emphasis (0=75us 1=50us) */
266 #define SYSCONFIG1_AGCD 0x0400 /* bits 10..10: AGC Disable */
267 #define SYSCONFIG1_BLNDADJ 0x00c0 /* bits 07..06: Stereo/Mono Blend Level Adjustment */
268 #define SYSCONFIG1_GPIO3 0x0030 /* bits 05..04: General Purpose I/O 3 */
269 #define SYSCONFIG1_GPIO2 0x000c /* bits 03..02: General Purpose I/O 2 */
270 #define SYSCONFIG1_GPIO1 0x0003 /* bits 01..00: General Purpose I/O 1 */
272 #define SYSCONFIG2 5 /* System Configuration 2 */
273 #define SYSCONFIG2_SEEKTH 0xff00 /* bits 15..08: RSSI Seek Threshold */
274 #define SYSCONFIG2_BAND 0x0080 /* bits 07..06: Band Select */
275 #define SYSCONFIG2_SPACE 0x0030 /* bits 05..04: Channel Spacing */
276 #define SYSCONFIG2_VOLUME 0x000f /* bits 03..00: Volume */
278 #define SYSCONFIG3 6 /* System Configuration 3 */
279 #define SYSCONFIG3_SMUTER 0xc000 /* bits 15..14: Softmute Attack/Recover Rate */
280 #define SYSCONFIG3_SMUTEA 0x3000 /* bits 13..12: Softmute Attenuation */
281 #define SYSCONFIG3_SKSNR 0x00f0 /* bits 07..04: Seek SNR Threshold */
282 #define SYSCONFIG3_SKCNT 0x000f /* bits 03..00: Seek FM Impulse Detection Threshold */
284 #define TEST1 7 /* Test 1 */
285 #define TEST1_AHIZEN 0x4000 /* bits 14..14: Audio High-Z Enable */
287 #define TEST2 8 /* Test 2 */
288 /* TEST2 only contains reserved bits */
290 #define BOOTCONFIG 9 /* Boot Configuration */
291 /* BOOTCONFIG only contains reserved bits */
293 #define STATUSRSSI 10 /* Status RSSI */
294 #define STATUSRSSI_RDSR 0x8000 /* bits 15..15: RDS Ready (Si4701 only) */
295 #define STATUSRSSI_STC 0x4000 /* bits 14..14: Seek/Tune Complete */
296 #define STATUSRSSI_SF 0x2000 /* bits 13..13: Seek Fail/Band Limit */
297 #define STATUSRSSI_AFCRL 0x1000 /* bits 12..12: AFC Rail */
298 #define STATUSRSSI_RDSS 0x0800 /* bits 11..11: RDS Synchronized (Si4701 only) */
299 #define STATUSRSSI_BLERA 0x0600 /* bits 10..09: RDS Block A Errors (Si4701 only) */
300 #define STATUSRSSI_ST 0x0100 /* bits 08..08: Stereo Indicator */
301 #define STATUSRSSI_RSSI 0x00ff /* bits 07..00: RSSI (Received Signal Strength Indicator) */
303 #define READCHAN 11 /* Read Channel */
304 #define READCHAN_BLERB 0xc000 /* bits 15..14: RDS Block D Errors (Si4701 only) */
305 #define READCHAN_BLERC 0x3000 /* bits 13..12: RDS Block C Errors (Si4701 only) */
306 #define READCHAN_BLERD 0x0c00 /* bits 11..10: RDS Block B Errors (Si4701 only) */
307 #define READCHAN_READCHAN 0x03ff /* bits 09..00: Read Channel */
309 #define RDSA 12 /* RDSA */
310 #define RDSA_RDSA 0xffff /* bits 15..00: RDS Block A Data (Si4701 only) */
312 #define RDSB 13 /* RDSB */
313 #define RDSB_RDSB 0xffff /* bits 15..00: RDS Block B Data (Si4701 only) */
315 #define RDSC 14 /* RDSC */
316 #define RDSC_RDSC 0xffff /* bits 15..00: RDS Block C Data (Si4701 only) */
318 #define RDSD 15 /* RDSD */
319 #define RDSD_RDSD 0xffff /* bits 15..00: RDS Block D Data (Si4701 only) */
323 /**************************************************************************
325 **************************************************************************/
327 /* Reports 1-16 give direct read/write access to the 16 Si470x registers */
328 /* with the (REPORT_ID - 1) corresponding to the register address across USB */
329 /* endpoint 0 using GET_REPORT and SET_REPORT */
330 #define REGISTER_REPORT_SIZE (RADIO_REGISTER_SIZE + 1)
331 #define REGISTER_REPORT(reg) ((reg) + 1)
333 /* Report 17 gives direct read/write access to the entire Si470x register */
334 /* map across endpoint 0 using GET_REPORT and SET_REPORT */
335 #define ENTIRE_REPORT_SIZE (RADIO_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
336 #define ENTIRE_REPORT 17
338 /* Report 18 is used to send the lowest 6 Si470x registers up the HID */
339 /* interrupt endpoint 1 to Windows every 20 milliseconds for status */
340 #define RDS_REPORT_SIZE (RDS_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
341 #define RDS_REPORT 18
343 /* Report 19: LED state */
344 #define LED_REPORT_SIZE 3
345 #define LED_REPORT 19
347 /* Report 19: stream */
348 #define STREAM_REPORT_SIZE 3
349 #define STREAM_REPORT 19
351 /* Report 20: scratch */
352 #define SCRATCH_PAGE_SIZE 63
353 #define SCRATCH_REPORT_SIZE (SCRATCH_PAGE_SIZE + 1)
354 #define SCRATCH_REPORT 20
356 /* Reports 19-22: flash upgrade of the C8051F321 */
357 #define WRITE_REPORT 19
358 #define FLASH_REPORT 20
359 #define CRC_REPORT 21
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
433 #define COMMAND_BUFFER_SIZE 4
434 #define RESPONSE_BUFFER_SIZE 2
435 #define FLASH_BUFFER_SIZE 64
436 #define CRC_BUFFER_SIZE 3
440 /**************************************************************************
441 * General Driver Definitions
442 **************************************************************************/
445 * si470x_device - private data
447 struct si470x_device {
448 /* reference to USB and video device */
449 struct usb_device *usbdev;
450 struct usb_interface *intf;
451 struct video_device *videodev;
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 struct delayed_work work;
463 wait_queue_head_t read_queue;
464 struct mutex lock; /* buffer locking */
465 unsigned char *buffer; /* size is always multiple of three */
466 unsigned int buf_size;
467 unsigned int rd_index;
468 unsigned int wr_index;
473 * The frequency is set in units of 62.5 Hz when using V4L2_TUNER_CAP_LOW,
474 * 62.5 kHz otherwise.
475 * The tuner is able to have a channel spacing of 50, 100 or 200 kHz.
476 * tuner->capability is therefore set to V4L2_TUNER_CAP_LOW
477 * The FREQ_MUL is then: 1 MHz / 62.5 Hz = 16000
479 #define FREQ_MUL (1000000 / 62.5)
483 /**************************************************************************
484 * General Driver Functions
485 **************************************************************************/
488 * si470x_get_report - receive a HID report
490 static int si470x_get_report(struct si470x_device *radio, void *buf, int size)
492 unsigned char *report = (unsigned char *) buf;
495 retval = usb_control_msg(radio->usbdev,
496 usb_rcvctrlpipe(radio->usbdev, 0),
498 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
500 buf, size, usb_timeout);
503 printk(KERN_WARNING DRIVER_NAME
504 ": si470x_get_report: usb_control_msg returned %d\n",
511 * si470x_set_report - send a HID report
513 static int si470x_set_report(struct si470x_device *radio, void *buf, int size)
515 unsigned char *report = (unsigned char *) buf;
518 retval = usb_control_msg(radio->usbdev,
519 usb_sndctrlpipe(radio->usbdev, 0),
521 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
523 buf, size, usb_timeout);
526 printk(KERN_WARNING DRIVER_NAME
527 ": si470x_set_report: usb_control_msg returned %d\n",
534 * si470x_get_register - read register
536 static int si470x_get_register(struct si470x_device *radio, int regnr)
538 unsigned char buf[REGISTER_REPORT_SIZE];
541 buf[0] = REGISTER_REPORT(regnr);
543 retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
546 radio->registers[regnr] = get_unaligned_be16(&buf[1]);
548 return (retval < 0) ? -EINVAL : 0;
553 * si470x_set_register - write register
555 static int si470x_set_register(struct si470x_device *radio, int regnr)
557 unsigned char buf[REGISTER_REPORT_SIZE];
560 buf[0] = REGISTER_REPORT(regnr);
561 put_unaligned_be16(radio->registers[regnr], &buf[1]);
563 retval = si470x_set_report(radio, (void *) &buf, sizeof(buf));
565 return (retval < 0) ? -EINVAL : 0;
570 * si470x_get_all_registers - read entire registers
572 static int si470x_get_all_registers(struct si470x_device *radio)
574 unsigned char buf[ENTIRE_REPORT_SIZE];
578 buf[0] = ENTIRE_REPORT;
580 retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
583 for (regnr = 0; regnr < RADIO_REGISTER_NUM; regnr++)
584 radio->registers[regnr] = get_unaligned_be16(
585 &buf[regnr * RADIO_REGISTER_SIZE + 1]);
587 return (retval < 0) ? -EINVAL : 0;
592 * si470x_get_rds_registers - read rds registers
594 static int si470x_get_rds_registers(struct si470x_device *radio)
596 unsigned char buf[RDS_REPORT_SIZE];
603 retval = usb_interrupt_msg(radio->usbdev,
604 usb_rcvintpipe(radio->usbdev, 1),
605 (void *) &buf, sizeof(buf), &size, usb_timeout);
606 if (size != sizeof(buf))
607 printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: "
608 "return size differs: %d != %zu\n", size, sizeof(buf));
610 printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: "
611 "usb_interrupt_msg returned %d\n", retval);
614 for (regnr = 0; regnr < RDS_REGISTER_NUM; regnr++)
615 radio->registers[STATUSRSSI + regnr] =
617 &buf[regnr * RADIO_REGISTER_SIZE + 1]);
619 return (retval < 0) ? -EINVAL : 0;
624 * si470x_set_chan - set the channel
626 static int si470x_set_chan(struct si470x_device *radio, unsigned short chan)
629 unsigned long timeout;
633 radio->registers[CHANNEL] &= ~CHANNEL_CHAN;
634 radio->registers[CHANNEL] |= CHANNEL_TUNE | chan;
635 retval = si470x_set_register(radio, CHANNEL);
639 /* wait till tune operation has completed */
640 timeout = jiffies + msecs_to_jiffies(tune_timeout);
642 retval = si470x_get_register(radio, STATUSRSSI);
645 timed_out = time_after(jiffies, timeout);
646 } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
648 if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
649 printk(KERN_WARNING DRIVER_NAME ": tune does not complete\n");
651 printk(KERN_WARNING DRIVER_NAME
652 ": tune timed out after %u ms\n", tune_timeout);
656 radio->registers[CHANNEL] &= ~CHANNEL_TUNE;
657 retval = si470x_set_register(radio, CHANNEL);
665 * si470x_get_freq - get the frequency
667 static int si470x_get_freq(struct si470x_device *radio, unsigned int *freq)
669 unsigned int spacing, band_bottom;
674 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) {
675 /* 0: 200 kHz (USA, Australia) */
677 spacing = 0.200 * FREQ_MUL; break;
678 /* 1: 100 kHz (Europe, Japan) */
680 spacing = 0.100 * FREQ_MUL; break;
683 spacing = 0.050 * FREQ_MUL; break;
686 /* Bottom of Band (MHz) */
687 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
688 /* 0: 87.5 - 108 MHz (USA, Europe) */
690 band_bottom = 87.5 * FREQ_MUL; break;
691 /* 1: 76 - 108 MHz (Japan wide band) */
693 band_bottom = 76 * FREQ_MUL; break;
694 /* 2: 76 - 90 MHz (Japan) */
696 band_bottom = 76 * FREQ_MUL; break;
700 retval = si470x_get_register(radio, READCHAN);
701 chan = radio->registers[READCHAN] & READCHAN_READCHAN;
703 /* Frequency (MHz) = Spacing (kHz) x Channel + Bottom of Band (MHz) */
704 *freq = chan * spacing + band_bottom;
711 * si470x_set_freq - set the frequency
713 static int si470x_set_freq(struct si470x_device *radio, unsigned int freq)
715 unsigned int spacing, band_bottom;
719 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) {
720 /* 0: 200 kHz (USA, Australia) */
722 spacing = 0.200 * FREQ_MUL; break;
723 /* 1: 100 kHz (Europe, Japan) */
725 spacing = 0.100 * FREQ_MUL; break;
728 spacing = 0.050 * FREQ_MUL; break;
731 /* Bottom of Band (MHz) */
732 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
733 /* 0: 87.5 - 108 MHz (USA, Europe) */
735 band_bottom = 87.5 * FREQ_MUL; break;
736 /* 1: 76 - 108 MHz (Japan wide band) */
738 band_bottom = 76 * FREQ_MUL; break;
739 /* 2: 76 - 90 MHz (Japan) */
741 band_bottom = 76 * FREQ_MUL; break;
744 /* Chan = [ Freq (Mhz) - Bottom of Band (MHz) ] / Spacing (kHz) */
745 chan = (freq - band_bottom) / spacing;
747 return si470x_set_chan(radio, chan);
752 * si470x_set_seek - set seek
754 static int si470x_set_seek(struct si470x_device *radio,
755 unsigned int wrap_around, unsigned int seek_upward)
758 unsigned long timeout;
762 radio->registers[POWERCFG] |= POWERCFG_SEEK;
763 if (wrap_around == 1)
764 radio->registers[POWERCFG] &= ~POWERCFG_SKMODE;
766 radio->registers[POWERCFG] |= POWERCFG_SKMODE;
767 if (seek_upward == 1)
768 radio->registers[POWERCFG] |= POWERCFG_SEEKUP;
770 radio->registers[POWERCFG] &= ~POWERCFG_SEEKUP;
771 retval = si470x_set_register(radio, POWERCFG);
775 /* wait till seek operation has completed */
776 timeout = jiffies + msecs_to_jiffies(seek_timeout);
778 retval = si470x_get_register(radio, STATUSRSSI);
781 timed_out = time_after(jiffies, timeout);
782 } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
784 if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
785 printk(KERN_WARNING DRIVER_NAME ": seek does not complete\n");
786 if (radio->registers[STATUSRSSI] & STATUSRSSI_SF)
787 printk(KERN_WARNING DRIVER_NAME
788 ": seek failed / band limit reached\n");
790 printk(KERN_WARNING DRIVER_NAME
791 ": seek timed out after %u ms\n", seek_timeout);
795 radio->registers[POWERCFG] &= ~POWERCFG_SEEK;
796 retval = si470x_set_register(radio, POWERCFG);
799 /* try again, if timed out */
800 if ((retval == 0) && timed_out)
808 * si470x_start - switch on radio
810 static int si470x_start(struct si470x_device *radio)
815 radio->registers[POWERCFG] =
816 POWERCFG_DMUTE | POWERCFG_ENABLE | POWERCFG_RDSM;
817 retval = si470x_set_register(radio, POWERCFG);
822 radio->registers[SYSCONFIG1] = SYSCONFIG1_DE;
823 retval = si470x_set_register(radio, SYSCONFIG1);
828 radio->registers[SYSCONFIG2] =
829 (0x3f << 8) | /* SEEKTH */
830 ((band << 6) & SYSCONFIG2_BAND) | /* BAND */
831 ((space << 4) & SYSCONFIG2_SPACE) | /* SPACE */
832 15; /* VOLUME (max) */
833 retval = si470x_set_register(radio, SYSCONFIG2);
837 /* reset last channel */
838 retval = si470x_set_chan(radio,
839 radio->registers[CHANNEL] & CHANNEL_CHAN);
847 * si470x_stop - switch off radio
849 static int si470x_stop(struct si470x_device *radio)
854 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
855 retval = si470x_set_register(radio, SYSCONFIG1);
860 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
861 /* POWERCFG_ENABLE has to automatically go low */
862 radio->registers[POWERCFG] |= POWERCFG_ENABLE | POWERCFG_DISABLE;
863 retval = si470x_set_register(radio, POWERCFG);
871 * si470x_rds_on - switch on rds reception
873 static int si470x_rds_on(struct si470x_device *radio)
878 mutex_lock(&radio->lock);
879 radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDS;
880 retval = si470x_set_register(radio, SYSCONFIG1);
882 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
883 mutex_unlock(&radio->lock);
890 /**************************************************************************
891 * General Driver Functions - LED_REPORT
892 **************************************************************************/
895 * si470x_set_led_state - sets the led state
897 static int si470x_set_led_state(struct si470x_device *radio,
898 unsigned char led_state)
900 unsigned char buf[LED_REPORT_SIZE];
904 buf[1] = LED_COMMAND;
907 retval = si470x_set_report(radio, (void *) &buf, sizeof(buf));
909 return (retval < 0) ? -EINVAL : 0;
914 /**************************************************************************
915 * RDS Driver Functions
916 **************************************************************************/
919 * si470x_rds - rds processing function
921 static void si470x_rds(struct si470x_device *radio)
923 unsigned char blocknum;
924 unsigned short bler; /* rds block errors */
926 unsigned char tmpbuf[3];
929 if (si470x_get_rds_registers(radio) < 0)
931 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0) {
932 /* No RDS group ready */
935 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSS) == 0) {
936 /* RDS decoder not synchronized */
940 /* copy all four RDS blocks to internal buffer */
941 mutex_lock(&radio->lock);
942 for (blocknum = 0; blocknum < 4; blocknum++) {
945 bler = (radio->registers[STATUSRSSI] &
946 STATUSRSSI_BLERA) >> 9;
947 rds = radio->registers[RDSA];
950 bler = (radio->registers[READCHAN] &
951 READCHAN_BLERB) >> 14;
952 rds = radio->registers[RDSB];
955 bler = (radio->registers[READCHAN] &
956 READCHAN_BLERC) >> 12;
957 rds = radio->registers[RDSC];
960 bler = (radio->registers[READCHAN] &
961 READCHAN_BLERD) >> 10;
962 rds = radio->registers[RDSD];
966 /* Fill the V4L2 RDS buffer */
967 put_unaligned_le16(rds, &tmpbuf);
968 tmpbuf[2] = blocknum; /* offset name */
969 tmpbuf[2] |= blocknum << 3; /* received offset */
970 if (bler > max_rds_errors)
971 tmpbuf[2] |= 0x80; /* uncorrectable errors */
973 tmpbuf[2] |= 0x40; /* corrected error(s) */
975 /* copy RDS block to internal buffer */
976 memcpy(&radio->buffer[radio->wr_index], &tmpbuf, 3);
977 radio->wr_index += 3;
979 /* wrap write pointer */
980 if (radio->wr_index >= radio->buf_size)
983 /* check for overflow */
984 if (radio->wr_index == radio->rd_index) {
985 /* increment and wrap read pointer */
986 radio->rd_index += 3;
987 if (radio->rd_index >= radio->buf_size)
991 mutex_unlock(&radio->lock);
993 /* wake up read queue */
994 if (radio->wr_index != radio->rd_index)
995 wake_up_interruptible(&radio->read_queue);
1000 * si470x_work - rds work function
1002 static void si470x_work(struct work_struct *work)
1004 struct si470x_device *radio = container_of(work, struct si470x_device,
1008 if (radio->disconnected)
1010 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
1014 schedule_delayed_work(&radio->work, msecs_to_jiffies(rds_poll_time));
1019 /**************************************************************************
1020 * File Operations Interface
1021 **************************************************************************/
1024 * si470x_fops_read - read RDS data
1026 static ssize_t si470x_fops_read(struct file *file, char __user *buf,
1027 size_t count, loff_t *ppos)
1029 struct si470x_device *radio = video_drvdata(file);
1031 unsigned int block_count = 0;
1033 /* switch on rds reception */
1034 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
1035 si470x_rds_on(radio);
1036 schedule_delayed_work(&radio->work,
1037 msecs_to_jiffies(rds_poll_time));
1040 /* block if no new data available */
1041 while (radio->wr_index == radio->rd_index) {
1042 if (file->f_flags & O_NONBLOCK) {
1043 retval = -EWOULDBLOCK;
1046 if (wait_event_interruptible(radio->read_queue,
1047 radio->wr_index != radio->rd_index) < 0) {
1053 /* calculate block count from byte count */
1056 /* copy RDS block out of internal buffer and to user buffer */
1057 mutex_lock(&radio->lock);
1058 while (block_count < count) {
1059 if (radio->rd_index == radio->wr_index)
1062 /* always transfer rds complete blocks */
1063 if (copy_to_user(buf, &radio->buffer[radio->rd_index], 3))
1064 /* retval = -EFAULT; */
1067 /* increment and wrap read pointer */
1068 radio->rd_index += 3;
1069 if (radio->rd_index >= radio->buf_size)
1070 radio->rd_index = 0;
1072 /* increment counters */
1077 mutex_unlock(&radio->lock);
1085 * si470x_fops_poll - poll RDS data
1087 static unsigned int si470x_fops_poll(struct file *file,
1088 struct poll_table_struct *pts)
1090 struct si470x_device *radio = video_drvdata(file);
1093 /* switch on rds reception */
1094 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
1095 si470x_rds_on(radio);
1096 schedule_delayed_work(&radio->work,
1097 msecs_to_jiffies(rds_poll_time));
1100 poll_wait(file, &radio->read_queue, pts);
1102 if (radio->rd_index != radio->wr_index)
1103 retval = POLLIN | POLLRDNORM;
1110 * si470x_fops_open - file open
1112 static int si470x_fops_open(struct file *file)
1114 struct si470x_device *radio = video_drvdata(file);
1120 retval = usb_autopm_get_interface(radio->intf);
1127 if (radio->users == 1) {
1128 retval = si470x_start(radio);
1130 usb_autopm_put_interface(radio->intf);
1140 * si470x_fops_release - file release
1142 static int si470x_fops_release(struct file *file)
1144 struct si470x_device *radio = video_drvdata(file);
1153 mutex_lock(&radio->disconnect_lock);
1155 if (radio->users == 0) {
1156 if (radio->disconnected) {
1157 video_unregister_device(radio->videodev);
1158 kfree(radio->buffer);
1163 /* stop rds reception */
1164 cancel_delayed_work_sync(&radio->work);
1166 /* cancel read processes */
1167 wake_up_interruptible(&radio->read_queue);
1169 retval = si470x_stop(radio);
1170 usb_autopm_put_interface(radio->intf);
1174 mutex_unlock(&radio->disconnect_lock);
1182 * si470x_fops - file operations interface
1184 static const struct v4l2_file_operations si470x_fops = {
1185 .owner = THIS_MODULE,
1186 .read = si470x_fops_read,
1187 .poll = si470x_fops_poll,
1188 .ioctl = video_ioctl2,
1189 .open = si470x_fops_open,
1190 .release = si470x_fops_release,
1195 /**************************************************************************
1196 * Video4Linux Interface
1197 **************************************************************************/
1200 * si470x_v4l2_queryctrl - query control
1202 static struct v4l2_queryctrl si470x_v4l2_queryctrl[] = {
1204 .id = V4L2_CID_AUDIO_VOLUME,
1205 .type = V4L2_CTRL_TYPE_INTEGER,
1210 .default_value = 15,
1213 .id = V4L2_CID_AUDIO_MUTE,
1214 .type = V4L2_CTRL_TYPE_BOOLEAN,
1225 * si470x_vidioc_querycap - query device capabilities
1227 static int si470x_vidioc_querycap(struct file *file, void *priv,
1228 struct v4l2_capability *capability)
1230 struct si470x_device *radio = video_drvdata(file);
1232 strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver));
1233 strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
1234 usb_make_path(radio->usbdev, capability->bus_info, sizeof(capability->bus_info));
1235 capability->version = DRIVER_KERNEL_VERSION;
1236 capability->capabilities = V4L2_CAP_HW_FREQ_SEEK |
1237 V4L2_CAP_TUNER | V4L2_CAP_RADIO;
1244 * si470x_vidioc_queryctrl - enumerate control items
1246 static int si470x_vidioc_queryctrl(struct file *file, void *priv,
1247 struct v4l2_queryctrl *qc)
1249 unsigned char i = 0;
1250 int retval = -EINVAL;
1252 /* abort if qc->id is below V4L2_CID_BASE */
1253 if (qc->id < V4L2_CID_BASE)
1256 /* search video control */
1257 for (i = 0; i < ARRAY_SIZE(si470x_v4l2_queryctrl); i++) {
1258 if (qc->id == si470x_v4l2_queryctrl[i].id) {
1259 memcpy(qc, &(si470x_v4l2_queryctrl[i]), sizeof(*qc));
1260 retval = 0; /* found */
1265 /* disable unsupported base controls */
1266 /* to satisfy kradio and such apps */
1267 if ((retval == -EINVAL) && (qc->id < V4L2_CID_LASTP1)) {
1268 qc->flags = V4L2_CTRL_FLAG_DISABLED;
1274 printk(KERN_WARNING DRIVER_NAME
1275 ": query controls failed with %d\n", retval);
1281 * si470x_vidioc_g_ctrl - get the value of a control
1283 static int si470x_vidioc_g_ctrl(struct file *file, void *priv,
1284 struct v4l2_control *ctrl)
1286 struct si470x_device *radio = video_drvdata(file);
1290 if (radio->disconnected) {
1296 case V4L2_CID_AUDIO_VOLUME:
1297 ctrl->value = radio->registers[SYSCONFIG2] &
1300 case V4L2_CID_AUDIO_MUTE:
1301 ctrl->value = ((radio->registers[POWERCFG] &
1302 POWERCFG_DMUTE) == 0) ? 1 : 0;
1310 printk(KERN_WARNING DRIVER_NAME
1311 ": get control failed with %d\n", retval);
1317 * si470x_vidioc_s_ctrl - set the value of a control
1319 static int si470x_vidioc_s_ctrl(struct file *file, void *priv,
1320 struct v4l2_control *ctrl)
1322 struct si470x_device *radio = video_drvdata(file);
1326 if (radio->disconnected) {
1332 case V4L2_CID_AUDIO_VOLUME:
1333 radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_VOLUME;
1334 radio->registers[SYSCONFIG2] |= ctrl->value;
1335 retval = si470x_set_register(radio, SYSCONFIG2);
1337 case V4L2_CID_AUDIO_MUTE:
1338 if (ctrl->value == 1)
1339 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
1341 radio->registers[POWERCFG] |= POWERCFG_DMUTE;
1342 retval = si470x_set_register(radio, POWERCFG);
1350 printk(KERN_WARNING DRIVER_NAME
1351 ": set control failed with %d\n", retval);
1357 * si470x_vidioc_g_audio - get audio attributes
1359 static int si470x_vidioc_g_audio(struct file *file, void *priv,
1360 struct v4l2_audio *audio)
1362 /* driver constants */
1364 strcpy(audio->name, "Radio");
1365 audio->capability = V4L2_AUDCAP_STEREO;
1373 * si470x_vidioc_g_tuner - get tuner attributes
1375 static int si470x_vidioc_g_tuner(struct file *file, void *priv,
1376 struct v4l2_tuner *tuner)
1378 struct si470x_device *radio = video_drvdata(file);
1382 if (radio->disconnected) {
1386 if (tuner->index != 0) {
1391 retval = si470x_get_register(radio, STATUSRSSI);
1395 /* driver constants */
1396 strcpy(tuner->name, "FM");
1397 tuner->type = V4L2_TUNER_RADIO;
1398 tuner->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
1401 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
1402 /* 0: 87.5 - 108 MHz (USA, Europe, default) */
1404 tuner->rangelow = 87.5 * FREQ_MUL;
1405 tuner->rangehigh = 108 * FREQ_MUL;
1407 /* 1: 76 - 108 MHz (Japan wide band) */
1409 tuner->rangelow = 76 * FREQ_MUL;
1410 tuner->rangehigh = 108 * FREQ_MUL;
1412 /* 2: 76 - 90 MHz (Japan) */
1414 tuner->rangelow = 76 * FREQ_MUL;
1415 tuner->rangehigh = 90 * FREQ_MUL;
1419 /* stereo indicator == stereo (instead of mono) */
1420 if ((radio->registers[STATUSRSSI] & STATUSRSSI_ST) == 0)
1421 tuner->rxsubchans = V4L2_TUNER_SUB_MONO;
1423 tuner->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
1425 /* mono/stereo selector */
1426 if ((radio->registers[POWERCFG] & POWERCFG_MONO) == 0)
1427 tuner->audmode = V4L2_TUNER_MODE_STEREO;
1429 tuner->audmode = V4L2_TUNER_MODE_MONO;
1431 /* min is worst, max is best; signal:0..0xffff; rssi: 0..0xff */
1432 /* measured in units of dbµV in 1 db increments (max at ~75 dbµV) */
1433 tuner->signal = (radio->registers[STATUSRSSI] & STATUSRSSI_RSSI);
1434 /* the ideal factor is 0xffff/75 = 873,8 */
1435 tuner->signal = (tuner->signal * 873) + (8 * tuner->signal / 10);
1437 /* automatic frequency control: -1: freq to low, 1 freq to high */
1438 /* AFCRL does only indicate that freq. differs, not if too low/high */
1439 tuner->afc = (radio->registers[STATUSRSSI] & STATUSRSSI_AFCRL) ? 1 : 0;
1443 printk(KERN_WARNING DRIVER_NAME
1444 ": get tuner failed with %d\n", retval);
1450 * si470x_vidioc_s_tuner - set tuner attributes
1452 static int si470x_vidioc_s_tuner(struct file *file, void *priv,
1453 struct v4l2_tuner *tuner)
1455 struct si470x_device *radio = video_drvdata(file);
1456 int retval = -EINVAL;
1459 if (radio->disconnected) {
1463 if (tuner->index != 0)
1466 /* mono/stereo selector */
1467 switch (tuner->audmode) {
1468 case V4L2_TUNER_MODE_MONO:
1469 radio->registers[POWERCFG] |= POWERCFG_MONO; /* force mono */
1471 case V4L2_TUNER_MODE_STEREO:
1472 radio->registers[POWERCFG] &= ~POWERCFG_MONO; /* try stereo */
1478 retval = si470x_set_register(radio, POWERCFG);
1482 printk(KERN_WARNING DRIVER_NAME
1483 ": set tuner failed with %d\n", retval);
1489 * si470x_vidioc_g_frequency - get tuner or modulator radio frequency
1491 static int si470x_vidioc_g_frequency(struct file *file, void *priv,
1492 struct v4l2_frequency *freq)
1494 struct si470x_device *radio = video_drvdata(file);
1498 if (radio->disconnected) {
1502 if (freq->tuner != 0) {
1507 freq->type = V4L2_TUNER_RADIO;
1508 retval = si470x_get_freq(radio, &freq->frequency);
1512 printk(KERN_WARNING DRIVER_NAME
1513 ": get frequency failed with %d\n", retval);
1519 * si470x_vidioc_s_frequency - set tuner or modulator radio frequency
1521 static int si470x_vidioc_s_frequency(struct file *file, void *priv,
1522 struct v4l2_frequency *freq)
1524 struct si470x_device *radio = video_drvdata(file);
1528 if (radio->disconnected) {
1532 if (freq->tuner != 0) {
1537 retval = si470x_set_freq(radio, freq->frequency);
1541 printk(KERN_WARNING DRIVER_NAME
1542 ": set frequency failed with %d\n", retval);
1548 * si470x_vidioc_s_hw_freq_seek - set hardware frequency seek
1550 static int si470x_vidioc_s_hw_freq_seek(struct file *file, void *priv,
1551 struct v4l2_hw_freq_seek *seek)
1553 struct si470x_device *radio = video_drvdata(file);
1557 if (radio->disconnected) {
1561 if (seek->tuner != 0) {
1566 retval = si470x_set_seek(radio, seek->wrap_around, seek->seek_upward);
1570 printk(KERN_WARNING DRIVER_NAME
1571 ": set hardware frequency seek failed with %d\n",
1578 * si470x_ioctl_ops - video device ioctl operations
1580 static const struct v4l2_ioctl_ops si470x_ioctl_ops = {
1581 .vidioc_querycap = si470x_vidioc_querycap,
1582 .vidioc_queryctrl = si470x_vidioc_queryctrl,
1583 .vidioc_g_ctrl = si470x_vidioc_g_ctrl,
1584 .vidioc_s_ctrl = si470x_vidioc_s_ctrl,
1585 .vidioc_g_audio = si470x_vidioc_g_audio,
1586 .vidioc_g_tuner = si470x_vidioc_g_tuner,
1587 .vidioc_s_tuner = si470x_vidioc_s_tuner,
1588 .vidioc_g_frequency = si470x_vidioc_g_frequency,
1589 .vidioc_s_frequency = si470x_vidioc_s_frequency,
1590 .vidioc_s_hw_freq_seek = si470x_vidioc_s_hw_freq_seek,
1595 * si470x_viddev_template - video device interface
1597 static struct video_device si470x_viddev_template = {
1598 .fops = &si470x_fops,
1599 .name = DRIVER_NAME,
1600 .release = video_device_release,
1601 .ioctl_ops = &si470x_ioctl_ops,
1606 /**************************************************************************
1608 **************************************************************************/
1611 * si470x_usb_driver_probe - probe for the device
1613 static int si470x_usb_driver_probe(struct usb_interface *intf,
1614 const struct usb_device_id *id)
1616 struct si470x_device *radio;
1619 /* private data allocation and initialization */
1620 radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL);
1626 radio->disconnected = 0;
1627 radio->usbdev = interface_to_usbdev(intf);
1629 mutex_init(&radio->disconnect_lock);
1630 mutex_init(&radio->lock);
1632 /* video device allocation and initialization */
1633 radio->videodev = video_device_alloc();
1634 if (!radio->videodev) {
1638 memcpy(radio->videodev, &si470x_viddev_template,
1639 sizeof(si470x_viddev_template));
1640 video_set_drvdata(radio->videodev, radio);
1642 /* show some infos about the specific device */
1643 if (si470x_get_all_registers(radio) < 0) {
1647 printk(KERN_INFO DRIVER_NAME ": DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",
1648 radio->registers[DEVICEID], radio->registers[CHIPID]);
1650 /* check if firmware is current */
1651 if ((radio->registers[CHIPID] & CHIPID_FIRMWARE)
1652 < RADIO_SW_VERSION_CURRENT) {
1653 printk(KERN_WARNING DRIVER_NAME
1654 ": This driver is known to work with "
1655 "firmware version %hu,\n", RADIO_SW_VERSION_CURRENT);
1656 printk(KERN_WARNING DRIVER_NAME
1657 ": but the device has firmware version %hu.\n",
1658 radio->registers[CHIPID] & CHIPID_FIRMWARE);
1659 printk(KERN_WARNING DRIVER_NAME
1660 ": If you have some trouble using this driver,\n");
1661 printk(KERN_WARNING DRIVER_NAME
1662 ": please report to V4L ML at "
1663 "video4linux-list@redhat.com\n");
1666 /* set initial frequency */
1667 si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
1669 /* set led to connect state */
1670 si470x_set_led_state(radio, BLINK_GREEN_LED);
1672 /* rds buffer allocation */
1673 radio->buf_size = rds_buf * 3;
1674 radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
1675 if (!radio->buffer) {
1680 /* rds buffer configuration */
1681 radio->wr_index = 0;
1682 radio->rd_index = 0;
1683 init_waitqueue_head(&radio->read_queue);
1685 /* prepare rds work function */
1686 INIT_DELAYED_WORK(&radio->work, si470x_work);
1688 /* register video device */
1689 retval = video_register_device(radio->videodev, VFL_TYPE_RADIO, radio_nr);
1691 printk(KERN_WARNING DRIVER_NAME
1692 ": Could not register video device\n");
1695 usb_set_intfdata(intf, radio);
1699 video_device_release(radio->videodev);
1700 kfree(radio->buffer);
1709 * si470x_usb_driver_suspend - suspend the device
1711 static int si470x_usb_driver_suspend(struct usb_interface *intf,
1712 pm_message_t message)
1714 struct si470x_device *radio = usb_get_intfdata(intf);
1716 printk(KERN_INFO DRIVER_NAME ": suspending now...\n");
1718 cancel_delayed_work_sync(&radio->work);
1725 * si470x_usb_driver_resume - resume the device
1727 static int si470x_usb_driver_resume(struct usb_interface *intf)
1729 struct si470x_device *radio = usb_get_intfdata(intf);
1731 printk(KERN_INFO DRIVER_NAME ": resuming now...\n");
1733 mutex_lock(&radio->lock);
1734 if (radio->users && radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS)
1735 schedule_delayed_work(&radio->work,
1736 msecs_to_jiffies(rds_poll_time));
1737 mutex_unlock(&radio->lock);
1744 * si470x_usb_driver_disconnect - disconnect the device
1746 static void si470x_usb_driver_disconnect(struct usb_interface *intf)
1748 struct si470x_device *radio = usb_get_intfdata(intf);
1750 mutex_lock(&radio->disconnect_lock);
1751 radio->disconnected = 1;
1752 cancel_delayed_work_sync(&radio->work);
1753 usb_set_intfdata(intf, NULL);
1754 if (radio->users == 0) {
1755 /* set led to disconnect state */
1756 si470x_set_led_state(radio, BLINK_ORANGE_LED);
1758 video_unregister_device(radio->videodev);
1759 kfree(radio->buffer);
1762 mutex_unlock(&radio->disconnect_lock);
1767 * si470x_usb_driver - usb driver interface
1769 static struct usb_driver si470x_usb_driver = {
1770 .name = DRIVER_NAME,
1771 .probe = si470x_usb_driver_probe,
1772 .disconnect = si470x_usb_driver_disconnect,
1773 .suspend = si470x_usb_driver_suspend,
1774 .resume = si470x_usb_driver_resume,
1775 .id_table = si470x_usb_driver_id_table,
1776 .supports_autosuspend = 1,
1781 /**************************************************************************
1783 **************************************************************************/
1786 * si470x_module_init - module init
1788 static int __init si470x_module_init(void)
1790 printk(KERN_INFO DRIVER_DESC ", Version " DRIVER_VERSION "\n");
1791 return usb_register(&si470x_usb_driver);
1796 * si470x_module_exit - module exit
1798 static void __exit si470x_module_exit(void)
1800 usb_deregister(&si470x_usb_driver);
1804 module_init(si470x_module_init);
1805 module_exit(si470x_module_exit);
1807 MODULE_LICENSE("GPL");
1808 MODULE_AUTHOR(DRIVER_AUTHOR);
1809 MODULE_DESCRIPTION(DRIVER_DESC);
1810 MODULE_VERSION(DRIVER_VERSION);