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)
9 * Copyright (c) 2008 Tobias Lorenz <tobias.lorenz@gmx.net>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 * 2008-01-12 Tobias Lorenz <tobias.lorenz@gmx.net>
31 * - First working version
32 * 2008-01-13 Tobias Lorenz <tobias.lorenz@gmx.net>
34 * - Improved error handling, every function now returns errno
35 * - Improved multi user access (start/mute/stop)
36 * - Channel doesn't get lost anymore after start/mute/stop
37 * - RDS support added (polling mode via interrupt EP 1)
38 * - marked default module parameters with *value*
39 * - switched from bit structs to bit masks
40 * - header file cleaned and integrated
41 * 2008-01-14 Tobias Lorenz <tobias.lorenz@gmx.net>
43 * - hex values are now lower case
44 * - commented USB ID for ADS/Tech moved on todo list
45 * - blacklisted si470x in hid-quirks.c
46 * - rds buffer handling functions integrated into *_work, *_read
47 * - rds_command in si470x_poll exchanged against simple retval
48 * - check for firmware version 15
49 * - code order and prototypes still remain the same
50 * - spacing and bottom of band codes remain the same
51 * 2008-01-16 Tobias Lorenz <tobias.lorenz@gmx.net>
53 * - code reordered to avoid function prototypes
54 * - switch/case defaults are now more user-friendly
55 * - unified comment style
56 * - applied all checkpatch.pl v1.12 suggestions
57 * except the warning about the too long lines with bit comments
58 * - renamed FMRADIO to RADIO to cut line length (checkpatch.pl)
59 * 2008-01-22 Tobias Lorenz <tobias.lorenz@gmx.net>
61 * - avoid poss. locking when doing copy_to_user which may sleep
62 * - RDS is automatically activated on read now
63 * - code cleaned of unnecessary rds_commands
64 * - USB Vendor/Product ID for ADS/Tech FM Radio Receiver verified
65 * (thanks to Guillaume RAMOUSSE)
66 * 2008-01-27 Tobias Lorenz <tobias.lorenz@gmx.net>
68 * - number of seek_retries changed to tune_timeout
69 * - fixed problem with incomplete tune operations by own buffers
70 * - optimization of variables and printf types
71 * - improved error logging
72 * 2008-01-31 Tobias Lorenz <tobias.lorenz@gmx.net>
73 * Oliver Neukum <oliver@neukum.org>
75 * - fixed coverity checker warnings in *_usb_driver_disconnect
76 * - probe()/open() race by correct ordering in probe()
77 * - DMA coherency rules by separate allocation of all buffers
78 * - use of endianness macros
79 * - abuse of spinlock, replaced by mutex
80 * - racy handling of timer in disconnect,
81 * replaced by delayed_work
82 * - racy interruptible_sleep_on(),
83 * replaced with wait_event_interruptible()
84 * - handle signals in read()
85 * 2008-02-08 Tobias Lorenz <tobias.lorenz@gmx.net>
86 * Oliver Neukum <oliver@neukum.org>
88 * - usb autosuspend support
90 * 2008-05-07 Tobias Lorenz <tobias.lorenz@gmx.net>
92 * - hardware frequency seek support
94 * - more safety checks, let si470x_get_freq return errno
95 * - vidioc behavior corrected according to v4l2 spec
96 * 2008-10-20 Alexey Klimov <klimov.linux@gmail.com>
97 * - add support for KWorld USB FM Radio FM700
98 * - blacklisted KWorld radio in hid-core.c and hid-ids.h
101 * - add firmware download/update support
102 * - RDS support: interrupt mode, instead of polling
103 * - add LED status output (check if that's not already done in firmware)
107 /* driver definitions */
108 #define DRIVER_AUTHOR "Tobias Lorenz <tobias.lorenz@gmx.net>"
109 #define DRIVER_NAME "radio-si470x"
110 #define DRIVER_KERNEL_VERSION KERNEL_VERSION(1, 0, 8)
111 #define DRIVER_CARD "Silicon Labs Si470x FM Radio Receiver"
112 #define DRIVER_DESC "USB radio driver for Si470x FM Radio Receivers"
113 #define DRIVER_VERSION "1.0.8"
116 /* kernel includes */
117 #include <linux/kernel.h>
118 #include <linux/module.h>
119 #include <linux/init.h>
120 #include <linux/slab.h>
121 #include <linux/input.h>
122 #include <linux/usb.h>
123 #include <linux/hid.h>
124 #include <linux/version.h>
125 #include <linux/videodev2.h>
126 #include <linux/mutex.h>
127 #include <media/v4l2-common.h>
128 #include <media/v4l2-ioctl.h>
129 #include <media/rds.h>
130 #include <asm/unaligned.h>
133 /* USB Device ID List */
134 static struct usb_device_id si470x_usb_driver_id_table[] = {
135 /* Silicon Labs USB FM Radio Reference Design */
136 { USB_DEVICE_AND_INTERFACE_INFO(0x10c4, 0x818a, USB_CLASS_HID, 0, 0) },
137 /* ADS/Tech FM Radio Receiver (formerly Instant FM Music) */
138 { USB_DEVICE_AND_INTERFACE_INFO(0x06e1, 0xa155, USB_CLASS_HID, 0, 0) },
139 /* KWorld USB FM Radio SnapMusic Mobile 700 (FM700) */
140 { USB_DEVICE_AND_INTERFACE_INFO(0x1b80, 0xd700, USB_CLASS_HID, 0, 0) },
141 /* Terminating entry */
144 MODULE_DEVICE_TABLE(usb, si470x_usb_driver_id_table);
148 /**************************************************************************
150 **************************************************************************/
153 static int radio_nr = -1;
154 module_param(radio_nr, int, 0444);
155 MODULE_PARM_DESC(radio_nr, "Radio Nr");
158 /* 0: 200 kHz (USA, Australia) */
159 /* 1: 100 kHz (Europe, Japan) */
161 static unsigned short space = 2;
162 module_param(space, ushort, 0444);
163 MODULE_PARM_DESC(space, "Spacing: 0=200kHz 1=100kHz *2=50kHz*");
165 /* Bottom of Band (MHz) */
166 /* 0: 87.5 - 108 MHz (USA, Europe)*/
167 /* 1: 76 - 108 MHz (Japan wide band) */
168 /* 2: 76 - 90 MHz (Japan) */
169 static unsigned short band = 1;
170 module_param(band, ushort, 0444);
171 MODULE_PARM_DESC(band, "Band: 0=87.5..108MHz *1=76..108MHz* 2=76..90MHz");
175 /* 1: 50 us (Europe, Australia, Japan) */
176 static unsigned short de = 1;
177 module_param(de, ushort, 0444);
178 MODULE_PARM_DESC(de, "De-emphasis: 0=75us *1=50us*");
181 static unsigned int usb_timeout = 500;
182 module_param(usb_timeout, uint, 0644);
183 MODULE_PARM_DESC(usb_timeout, "USB timeout (ms): *500*");
186 static unsigned int tune_timeout = 3000;
187 module_param(tune_timeout, uint, 0644);
188 MODULE_PARM_DESC(tune_timeout, "Tune timeout: *3000*");
191 static unsigned int seek_timeout = 5000;
192 module_param(seek_timeout, uint, 0644);
193 MODULE_PARM_DESC(seek_timeout, "Seek timeout: *5000*");
195 /* RDS buffer blocks */
196 static unsigned int rds_buf = 100;
197 module_param(rds_buf, uint, 0444);
198 MODULE_PARM_DESC(rds_buf, "RDS buffer entries: *100*");
200 /* RDS maximum block errors */
201 static unsigned short max_rds_errors = 1;
202 /* 0 means 0 errors requiring correction */
203 /* 1 means 1-2 errors requiring correction (used by original USBRadio.exe) */
204 /* 2 means 3-5 errors requiring correction */
205 /* 3 means 6+ errors or errors in checkword, correction not possible */
206 module_param(max_rds_errors, ushort, 0644);
207 MODULE_PARM_DESC(max_rds_errors, "RDS maximum block errors: *1*");
209 /* RDS poll frequency */
210 static unsigned int rds_poll_time = 40;
211 /* 40 is used by the original USBRadio.exe */
212 /* 50 is used by radio-cadet */
213 /* 75 should be okay */
214 /* 80 is the usual RDS receive interval */
215 module_param(rds_poll_time, uint, 0644);
216 MODULE_PARM_DESC(rds_poll_time, "RDS poll time (ms): *40*");
220 /**************************************************************************
221 * Register Definitions
222 **************************************************************************/
223 #define RADIO_REGISTER_SIZE 2 /* 16 register bit width */
224 #define RADIO_REGISTER_NUM 16 /* DEVICEID ... RDSD */
225 #define RDS_REGISTER_NUM 6 /* STATUSRSSI ... RDSD */
227 #define DEVICEID 0 /* Device ID */
228 #define DEVICEID_PN 0xf000 /* bits 15..12: Part Number */
229 #define DEVICEID_MFGID 0x0fff /* bits 11..00: Manufacturer ID */
231 #define CHIPID 1 /* Chip ID */
232 #define CHIPID_REV 0xfc00 /* bits 15..10: Chip Version */
233 #define CHIPID_DEV 0x0200 /* bits 09..09: Device */
234 #define CHIPID_FIRMWARE 0x01ff /* bits 08..00: Firmware Version */
236 #define POWERCFG 2 /* Power Configuration */
237 #define POWERCFG_DSMUTE 0x8000 /* bits 15..15: Softmute Disable */
238 #define POWERCFG_DMUTE 0x4000 /* bits 14..14: Mute Disable */
239 #define POWERCFG_MONO 0x2000 /* bits 13..13: Mono Select */
240 #define POWERCFG_RDSM 0x0800 /* bits 11..11: RDS Mode (Si4701 only) */
241 #define POWERCFG_SKMODE 0x0400 /* bits 10..10: Seek Mode */
242 #define POWERCFG_SEEKUP 0x0200 /* bits 09..09: Seek Direction */
243 #define POWERCFG_SEEK 0x0100 /* bits 08..08: Seek */
244 #define POWERCFG_DISABLE 0x0040 /* bits 06..06: Powerup Disable */
245 #define POWERCFG_ENABLE 0x0001 /* bits 00..00: Powerup Enable */
247 #define CHANNEL 3 /* Channel */
248 #define CHANNEL_TUNE 0x8000 /* bits 15..15: Tune */
249 #define CHANNEL_CHAN 0x03ff /* bits 09..00: Channel Select */
251 #define SYSCONFIG1 4 /* System Configuration 1 */
252 #define SYSCONFIG1_RDSIEN 0x8000 /* bits 15..15: RDS Interrupt Enable (Si4701 only) */
253 #define SYSCONFIG1_STCIEN 0x4000 /* bits 14..14: Seek/Tune Complete Interrupt Enable */
254 #define SYSCONFIG1_RDS 0x1000 /* bits 12..12: RDS Enable (Si4701 only) */
255 #define SYSCONFIG1_DE 0x0800 /* bits 11..11: De-emphasis (0=75us 1=50us) */
256 #define SYSCONFIG1_AGCD 0x0400 /* bits 10..10: AGC Disable */
257 #define SYSCONFIG1_BLNDADJ 0x00c0 /* bits 07..06: Stereo/Mono Blend Level Adjustment */
258 #define SYSCONFIG1_GPIO3 0x0030 /* bits 05..04: General Purpose I/O 3 */
259 #define SYSCONFIG1_GPIO2 0x000c /* bits 03..02: General Purpose I/O 2 */
260 #define SYSCONFIG1_GPIO1 0x0003 /* bits 01..00: General Purpose I/O 1 */
262 #define SYSCONFIG2 5 /* System Configuration 2 */
263 #define SYSCONFIG2_SEEKTH 0xff00 /* bits 15..08: RSSI Seek Threshold */
264 #define SYSCONFIG2_BAND 0x0080 /* bits 07..06: Band Select */
265 #define SYSCONFIG2_SPACE 0x0030 /* bits 05..04: Channel Spacing */
266 #define SYSCONFIG2_VOLUME 0x000f /* bits 03..00: Volume */
268 #define SYSCONFIG3 6 /* System Configuration 3 */
269 #define SYSCONFIG3_SMUTER 0xc000 /* bits 15..14: Softmute Attack/Recover Rate */
270 #define SYSCONFIG3_SMUTEA 0x3000 /* bits 13..12: Softmute Attenuation */
271 #define SYSCONFIG3_SKSNR 0x00f0 /* bits 07..04: Seek SNR Threshold */
272 #define SYSCONFIG3_SKCNT 0x000f /* bits 03..00: Seek FM Impulse Detection Threshold */
274 #define TEST1 7 /* Test 1 */
275 #define TEST1_AHIZEN 0x4000 /* bits 14..14: Audio High-Z Enable */
277 #define TEST2 8 /* Test 2 */
278 /* TEST2 only contains reserved bits */
280 #define BOOTCONFIG 9 /* Boot Configuration */
281 /* BOOTCONFIG only contains reserved bits */
283 #define STATUSRSSI 10 /* Status RSSI */
284 #define STATUSRSSI_RDSR 0x8000 /* bits 15..15: RDS Ready (Si4701 only) */
285 #define STATUSRSSI_STC 0x4000 /* bits 14..14: Seek/Tune Complete */
286 #define STATUSRSSI_SF 0x2000 /* bits 13..13: Seek Fail/Band Limit */
287 #define STATUSRSSI_AFCRL 0x1000 /* bits 12..12: AFC Rail */
288 #define STATUSRSSI_RDSS 0x0800 /* bits 11..11: RDS Synchronized (Si4701 only) */
289 #define STATUSRSSI_BLERA 0x0600 /* bits 10..09: RDS Block A Errors (Si4701 only) */
290 #define STATUSRSSI_ST 0x0100 /* bits 08..08: Stereo Indicator */
291 #define STATUSRSSI_RSSI 0x00ff /* bits 07..00: RSSI (Received Signal Strength Indicator) */
293 #define READCHAN 11 /* Read Channel */
294 #define READCHAN_BLERB 0xc000 /* bits 15..14: RDS Block D Errors (Si4701 only) */
295 #define READCHAN_BLERC 0x3000 /* bits 13..12: RDS Block C Errors (Si4701 only) */
296 #define READCHAN_BLERD 0x0c00 /* bits 11..10: RDS Block B Errors (Si4701 only) */
297 #define READCHAN_READCHAN 0x03ff /* bits 09..00: Read Channel */
299 #define RDSA 12 /* RDSA */
300 #define RDSA_RDSA 0xffff /* bits 15..00: RDS Block A Data (Si4701 only) */
302 #define RDSB 13 /* RDSB */
303 #define RDSB_RDSB 0xffff /* bits 15..00: RDS Block B Data (Si4701 only) */
305 #define RDSC 14 /* RDSC */
306 #define RDSC_RDSC 0xffff /* bits 15..00: RDS Block C Data (Si4701 only) */
308 #define RDSD 15 /* RDSD */
309 #define RDSD_RDSD 0xffff /* bits 15..00: RDS Block D Data (Si4701 only) */
313 /**************************************************************************
315 **************************************************************************/
317 /* Reports 1-16 give direct read/write access to the 16 Si470x registers */
318 /* with the (REPORT_ID - 1) corresponding to the register address across USB */
319 /* endpoint 0 using GET_REPORT and SET_REPORT */
320 #define REGISTER_REPORT_SIZE (RADIO_REGISTER_SIZE + 1)
321 #define REGISTER_REPORT(reg) ((reg) + 1)
323 /* Report 17 gives direct read/write access to the entire Si470x register */
324 /* map across endpoint 0 using GET_REPORT and SET_REPORT */
325 #define ENTIRE_REPORT_SIZE (RADIO_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
326 #define ENTIRE_REPORT 17
328 /* Report 18 is used to send the lowest 6 Si470x registers up the HID */
329 /* interrupt endpoint 1 to Windows every 20 milliseconds for status */
330 #define RDS_REPORT_SIZE (RDS_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
331 #define RDS_REPORT 18
333 /* Report 19: LED state */
334 #define LED_REPORT_SIZE 3
335 #define LED_REPORT 19
337 /* Report 19: stream */
338 #define STREAM_REPORT_SIZE 3
339 #define STREAM_REPORT 19
341 /* Report 20: scratch */
342 #define SCRATCH_PAGE_SIZE 63
343 #define SCRATCH_REPORT_SIZE (SCRATCH_PAGE_SIZE + 1)
344 #define SCRATCH_REPORT 20
346 /* Reports 19-22: flash upgrade of the C8051F321 */
347 #define WRITE_REPORT 19
348 #define FLASH_REPORT 20
349 #define CRC_REPORT 21
350 #define RESPONSE_REPORT 22
352 /* Report 23: currently unused, but can accept 60 byte reports on the HID */
353 /* interrupt out endpoint 2 every 1 millisecond */
354 #define UNUSED_REPORT 23
358 /**************************************************************************
359 * Software/Hardware Versions
360 **************************************************************************/
361 #define RADIO_SW_VERSION_NOT_BOOTLOADABLE 6
362 #define RADIO_SW_VERSION 7
363 #define RADIO_SW_VERSION_CURRENT 15
364 #define RADIO_HW_VERSION 1
366 #define SCRATCH_PAGE_SW_VERSION 1
367 #define SCRATCH_PAGE_HW_VERSION 2
371 /**************************************************************************
372 * LED State Definitions
373 **************************************************************************/
374 #define LED_COMMAND 0x35
376 #define NO_CHANGE_LED 0x00
377 #define ALL_COLOR_LED 0x01 /* streaming state */
378 #define BLINK_GREEN_LED 0x02 /* connect state */
379 #define BLINK_RED_LED 0x04
380 #define BLINK_ORANGE_LED 0x10 /* disconnect state */
381 #define SOLID_GREEN_LED 0x20 /* tuning/seeking state */
382 #define SOLID_RED_LED 0x40 /* bootload state */
383 #define SOLID_ORANGE_LED 0x80
387 /**************************************************************************
388 * Stream State Definitions
389 **************************************************************************/
390 #define STREAM_COMMAND 0x36
391 #define STREAM_VIDPID 0x00
392 #define STREAM_AUDIO 0xff
396 /**************************************************************************
397 * Bootloader / Flash Commands
398 **************************************************************************/
400 /* unique id sent to bootloader and required to put into a bootload state */
401 #define UNIQUE_BL_ID 0x34
403 /* mask for the flash data */
404 #define FLASH_DATA_MASK 0x55
406 /* bootloader commands */
407 #define GET_SW_VERSION_COMMAND 0x00
408 #define SET_PAGE_COMMAND 0x01
409 #define ERASE_PAGE_COMMAND 0x02
410 #define WRITE_PAGE_COMMAND 0x03
411 #define CRC_ON_PAGE_COMMAND 0x04
412 #define READ_FLASH_BYTE_COMMAND 0x05
413 #define RESET_DEVICE_COMMAND 0x06
414 #define GET_HW_VERSION_COMMAND 0x07
417 /* bootloader command responses */
418 #define COMMAND_OK 0x01
419 #define COMMAND_FAILED 0x02
420 #define COMMAND_PENDING 0x03
423 #define COMMAND_BUFFER_SIZE 4
424 #define RESPONSE_BUFFER_SIZE 2
425 #define FLASH_BUFFER_SIZE 64
426 #define CRC_BUFFER_SIZE 3
430 /**************************************************************************
431 * General Driver Definitions
432 **************************************************************************/
435 * si470x_device - private data
437 struct si470x_device {
438 /* reference to USB and video device */
439 struct usb_device *usbdev;
440 struct usb_interface *intf;
441 struct video_device *videodev;
443 /* driver management */
445 unsigned char disconnected;
446 struct mutex disconnect_lock;
448 /* Silabs internal registers (0..15) */
449 unsigned short registers[RADIO_REGISTER_NUM];
451 /* RDS receive buffer */
452 struct delayed_work work;
453 wait_queue_head_t read_queue;
454 struct mutex lock; /* buffer locking */
455 unsigned char *buffer; /* size is always multiple of three */
456 unsigned int buf_size;
457 unsigned int rd_index;
458 unsigned int wr_index;
463 * The frequency is set in units of 62.5 Hz when using V4L2_TUNER_CAP_LOW,
464 * 62.5 kHz otherwise.
465 * The tuner is able to have a channel spacing of 50, 100 or 200 kHz.
466 * tuner->capability is therefore set to V4L2_TUNER_CAP_LOW
467 * The FREQ_MUL is then: 1 MHz / 62.5 Hz = 16000
469 #define FREQ_MUL (1000000 / 62.5)
473 /**************************************************************************
474 * General Driver Functions
475 **************************************************************************/
478 * si470x_get_report - receive a HID report
480 static int si470x_get_report(struct si470x_device *radio, void *buf, int size)
482 unsigned char *report = (unsigned char *) buf;
485 retval = usb_control_msg(radio->usbdev,
486 usb_rcvctrlpipe(radio->usbdev, 0),
488 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
490 buf, size, usb_timeout);
493 printk(KERN_WARNING DRIVER_NAME
494 ": si470x_get_report: usb_control_msg returned %d\n",
501 * si470x_set_report - send a HID report
503 static int si470x_set_report(struct si470x_device *radio, void *buf, int size)
505 unsigned char *report = (unsigned char *) buf;
508 retval = usb_control_msg(radio->usbdev,
509 usb_sndctrlpipe(radio->usbdev, 0),
511 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
513 buf, size, usb_timeout);
516 printk(KERN_WARNING DRIVER_NAME
517 ": si470x_set_report: usb_control_msg returned %d\n",
524 * si470x_get_register - read register
526 static int si470x_get_register(struct si470x_device *radio, int regnr)
528 unsigned char buf[REGISTER_REPORT_SIZE];
531 buf[0] = REGISTER_REPORT(regnr);
533 retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
536 radio->registers[regnr] = get_unaligned_be16(&buf[1]);
538 return (retval < 0) ? -EINVAL : 0;
543 * si470x_set_register - write register
545 static int si470x_set_register(struct si470x_device *radio, int regnr)
547 unsigned char buf[REGISTER_REPORT_SIZE];
550 buf[0] = REGISTER_REPORT(regnr);
551 put_unaligned_be16(radio->registers[regnr], &buf[1]);
553 retval = si470x_set_report(radio, (void *) &buf, sizeof(buf));
555 return (retval < 0) ? -EINVAL : 0;
560 * si470x_get_all_registers - read entire registers
562 static int si470x_get_all_registers(struct si470x_device *radio)
564 unsigned char buf[ENTIRE_REPORT_SIZE];
568 buf[0] = ENTIRE_REPORT;
570 retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
573 for (regnr = 0; regnr < RADIO_REGISTER_NUM; regnr++)
574 radio->registers[regnr] = get_unaligned_be16(
575 &buf[regnr * RADIO_REGISTER_SIZE + 1]);
577 return (retval < 0) ? -EINVAL : 0;
582 * si470x_get_rds_registers - read rds registers
584 static int si470x_get_rds_registers(struct si470x_device *radio)
586 unsigned char buf[RDS_REPORT_SIZE];
593 retval = usb_interrupt_msg(radio->usbdev,
594 usb_rcvintpipe(radio->usbdev, 1),
595 (void *) &buf, sizeof(buf), &size, usb_timeout);
596 if (size != sizeof(buf))
597 printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: "
598 "return size differs: %d != %zu\n", size, sizeof(buf));
600 printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: "
601 "usb_interrupt_msg returned %d\n", retval);
604 for (regnr = 0; regnr < RDS_REGISTER_NUM; regnr++)
605 radio->registers[STATUSRSSI + regnr] =
607 &buf[regnr * RADIO_REGISTER_SIZE + 1]);
609 return (retval < 0) ? -EINVAL : 0;
614 * si470x_set_chan - set the channel
616 static int si470x_set_chan(struct si470x_device *radio, unsigned short chan)
619 unsigned long timeout;
623 radio->registers[CHANNEL] &= ~CHANNEL_CHAN;
624 radio->registers[CHANNEL] |= CHANNEL_TUNE | chan;
625 retval = si470x_set_register(radio, CHANNEL);
629 /* wait till tune operation has completed */
630 timeout = jiffies + msecs_to_jiffies(tune_timeout);
632 retval = si470x_get_register(radio, STATUSRSSI);
635 timed_out = time_after(jiffies, timeout);
636 } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
638 if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
639 printk(KERN_WARNING DRIVER_NAME ": tune does not complete\n");
641 printk(KERN_WARNING DRIVER_NAME
642 ": tune timed out after %u ms\n", tune_timeout);
646 radio->registers[CHANNEL] &= ~CHANNEL_TUNE;
647 retval = si470x_set_register(radio, CHANNEL);
655 * si470x_get_freq - get the frequency
657 static int si470x_get_freq(struct si470x_device *radio, unsigned int *freq)
659 unsigned int spacing, band_bottom;
664 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) {
665 /* 0: 200 kHz (USA, Australia) */
667 spacing = 0.200 * FREQ_MUL; break;
668 /* 1: 100 kHz (Europe, Japan) */
670 spacing = 0.100 * FREQ_MUL; break;
673 spacing = 0.050 * FREQ_MUL; break;
676 /* Bottom of Band (MHz) */
677 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
678 /* 0: 87.5 - 108 MHz (USA, Europe) */
680 band_bottom = 87.5 * FREQ_MUL; break;
681 /* 1: 76 - 108 MHz (Japan wide band) */
683 band_bottom = 76 * FREQ_MUL; break;
684 /* 2: 76 - 90 MHz (Japan) */
686 band_bottom = 76 * FREQ_MUL; break;
690 retval = si470x_get_register(radio, READCHAN);
691 chan = radio->registers[READCHAN] & READCHAN_READCHAN;
693 /* Frequency (MHz) = Spacing (kHz) x Channel + Bottom of Band (MHz) */
694 *freq = chan * spacing + band_bottom;
701 * si470x_set_freq - set the frequency
703 static int si470x_set_freq(struct si470x_device *radio, unsigned int freq)
705 unsigned int spacing, band_bottom;
709 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) {
710 /* 0: 200 kHz (USA, Australia) */
712 spacing = 0.200 * FREQ_MUL; break;
713 /* 1: 100 kHz (Europe, Japan) */
715 spacing = 0.100 * FREQ_MUL; break;
718 spacing = 0.050 * FREQ_MUL; break;
721 /* Bottom of Band (MHz) */
722 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
723 /* 0: 87.5 - 108 MHz (USA, Europe) */
725 band_bottom = 87.5 * FREQ_MUL; break;
726 /* 1: 76 - 108 MHz (Japan wide band) */
728 band_bottom = 76 * FREQ_MUL; break;
729 /* 2: 76 - 90 MHz (Japan) */
731 band_bottom = 76 * FREQ_MUL; break;
734 /* Chan = [ Freq (Mhz) - Bottom of Band (MHz) ] / Spacing (kHz) */
735 chan = (freq - band_bottom) / spacing;
737 return si470x_set_chan(radio, chan);
742 * si470x_set_seek - set seek
744 static int si470x_set_seek(struct si470x_device *radio,
745 unsigned int wrap_around, unsigned int seek_upward)
748 unsigned long timeout;
752 radio->registers[POWERCFG] |= POWERCFG_SEEK;
753 if (wrap_around == 1)
754 radio->registers[POWERCFG] &= ~POWERCFG_SKMODE;
756 radio->registers[POWERCFG] |= POWERCFG_SKMODE;
757 if (seek_upward == 1)
758 radio->registers[POWERCFG] |= POWERCFG_SEEKUP;
760 radio->registers[POWERCFG] &= ~POWERCFG_SEEKUP;
761 retval = si470x_set_register(radio, POWERCFG);
765 /* wait till seek operation has completed */
766 timeout = jiffies + msecs_to_jiffies(seek_timeout);
768 retval = si470x_get_register(radio, STATUSRSSI);
771 timed_out = time_after(jiffies, timeout);
772 } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
774 if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
775 printk(KERN_WARNING DRIVER_NAME ": seek does not complete\n");
776 if (radio->registers[STATUSRSSI] & STATUSRSSI_SF)
777 printk(KERN_WARNING DRIVER_NAME
778 ": seek failed / band limit reached\n");
780 printk(KERN_WARNING DRIVER_NAME
781 ": seek timed out after %u ms\n", seek_timeout);
785 radio->registers[POWERCFG] &= ~POWERCFG_SEEK;
786 retval = si470x_set_register(radio, POWERCFG);
789 /* try again, if timed out */
790 if ((retval == 0) && timed_out)
798 * si470x_start - switch on radio
800 static int si470x_start(struct si470x_device *radio)
805 radio->registers[POWERCFG] =
806 POWERCFG_DMUTE | POWERCFG_ENABLE | POWERCFG_RDSM;
807 retval = si470x_set_register(radio, POWERCFG);
812 radio->registers[SYSCONFIG1] = SYSCONFIG1_DE;
813 retval = si470x_set_register(radio, SYSCONFIG1);
818 radio->registers[SYSCONFIG2] =
819 (0x3f << 8) | /* SEEKTH */
820 ((band << 6) & SYSCONFIG2_BAND) | /* BAND */
821 ((space << 4) & SYSCONFIG2_SPACE) | /* SPACE */
822 15; /* VOLUME (max) */
823 retval = si470x_set_register(radio, SYSCONFIG2);
827 /* reset last channel */
828 retval = si470x_set_chan(radio,
829 radio->registers[CHANNEL] & CHANNEL_CHAN);
837 * si470x_stop - switch off radio
839 static int si470x_stop(struct si470x_device *radio)
844 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
845 retval = si470x_set_register(radio, SYSCONFIG1);
850 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
851 /* POWERCFG_ENABLE has to automatically go low */
852 radio->registers[POWERCFG] |= POWERCFG_ENABLE | POWERCFG_DISABLE;
853 retval = si470x_set_register(radio, POWERCFG);
861 * si470x_rds_on - switch on rds reception
863 static int si470x_rds_on(struct si470x_device *radio)
868 mutex_lock(&radio->lock);
869 radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDS;
870 retval = si470x_set_register(radio, SYSCONFIG1);
872 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
873 mutex_unlock(&radio->lock);
880 /**************************************************************************
881 * RDS Driver Functions
882 **************************************************************************/
885 * si470x_rds - rds processing function
887 static void si470x_rds(struct si470x_device *radio)
889 unsigned char blocknum;
890 unsigned short bler; /* rds block errors */
892 unsigned char tmpbuf[3];
895 if (si470x_get_rds_registers(radio) < 0)
897 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0) {
898 /* No RDS group ready */
901 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSS) == 0) {
902 /* RDS decoder not synchronized */
906 /* copy all four RDS blocks to internal buffer */
907 mutex_lock(&radio->lock);
908 for (blocknum = 0; blocknum < 4; blocknum++) {
911 bler = (radio->registers[STATUSRSSI] &
912 STATUSRSSI_BLERA) >> 9;
913 rds = radio->registers[RDSA];
916 bler = (radio->registers[READCHAN] &
917 READCHAN_BLERB) >> 14;
918 rds = radio->registers[RDSB];
921 bler = (radio->registers[READCHAN] &
922 READCHAN_BLERC) >> 12;
923 rds = radio->registers[RDSC];
926 bler = (radio->registers[READCHAN] &
927 READCHAN_BLERD) >> 10;
928 rds = radio->registers[RDSD];
932 /* Fill the V4L2 RDS buffer */
933 put_unaligned_le16(rds, &tmpbuf);
934 tmpbuf[2] = blocknum; /* offset name */
935 tmpbuf[2] |= blocknum << 3; /* received offset */
936 if (bler > max_rds_errors)
937 tmpbuf[2] |= 0x80; /* uncorrectable errors */
939 tmpbuf[2] |= 0x40; /* corrected error(s) */
941 /* copy RDS block to internal buffer */
942 memcpy(&radio->buffer[radio->wr_index], &tmpbuf, 3);
943 radio->wr_index += 3;
945 /* wrap write pointer */
946 if (radio->wr_index >= radio->buf_size)
949 /* check for overflow */
950 if (radio->wr_index == radio->rd_index) {
951 /* increment and wrap read pointer */
952 radio->rd_index += 3;
953 if (radio->rd_index >= radio->buf_size)
957 mutex_unlock(&radio->lock);
959 /* wake up read queue */
960 if (radio->wr_index != radio->rd_index)
961 wake_up_interruptible(&radio->read_queue);
966 * si470x_work - rds work function
968 static void si470x_work(struct work_struct *work)
970 struct si470x_device *radio = container_of(work, struct si470x_device,
974 if (radio->disconnected)
976 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
980 schedule_delayed_work(&radio->work, msecs_to_jiffies(rds_poll_time));
985 /**************************************************************************
986 * File Operations Interface
987 **************************************************************************/
990 * si470x_fops_read - read RDS data
992 static ssize_t si470x_fops_read(struct file *file, char __user *buf,
993 size_t count, loff_t *ppos)
995 struct si470x_device *radio = video_drvdata(file);
997 unsigned int block_count = 0;
999 /* switch on rds reception */
1000 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
1001 si470x_rds_on(radio);
1002 schedule_delayed_work(&radio->work,
1003 msecs_to_jiffies(rds_poll_time));
1006 /* block if no new data available */
1007 while (radio->wr_index == radio->rd_index) {
1008 if (file->f_flags & O_NONBLOCK) {
1009 retval = -EWOULDBLOCK;
1012 if (wait_event_interruptible(radio->read_queue,
1013 radio->wr_index != radio->rd_index) < 0) {
1019 /* calculate block count from byte count */
1022 /* copy RDS block out of internal buffer and to user buffer */
1023 mutex_lock(&radio->lock);
1024 while (block_count < count) {
1025 if (radio->rd_index == radio->wr_index)
1028 /* always transfer rds complete blocks */
1029 if (copy_to_user(buf, &radio->buffer[radio->rd_index], 3))
1030 /* retval = -EFAULT; */
1033 /* increment and wrap read pointer */
1034 radio->rd_index += 3;
1035 if (radio->rd_index >= radio->buf_size)
1036 radio->rd_index = 0;
1038 /* increment counters */
1043 mutex_unlock(&radio->lock);
1051 * si470x_fops_poll - poll RDS data
1053 static unsigned int si470x_fops_poll(struct file *file,
1054 struct poll_table_struct *pts)
1056 struct si470x_device *radio = video_drvdata(file);
1059 /* switch on rds reception */
1060 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
1061 si470x_rds_on(radio);
1062 schedule_delayed_work(&radio->work,
1063 msecs_to_jiffies(rds_poll_time));
1066 poll_wait(file, &radio->read_queue, pts);
1068 if (radio->rd_index != radio->wr_index)
1069 retval = POLLIN | POLLRDNORM;
1076 * si470x_fops_open - file open
1078 static int si470x_fops_open(struct inode *inode, struct file *file)
1080 struct si470x_device *radio = video_drvdata(file);
1086 retval = usb_autopm_get_interface(radio->intf);
1093 if (radio->users == 1) {
1094 retval = si470x_start(radio);
1096 usb_autopm_put_interface(radio->intf);
1106 * si470x_fops_release - file release
1108 static int si470x_fops_release(struct inode *inode, struct file *file)
1110 struct si470x_device *radio = video_drvdata(file);
1119 mutex_lock(&radio->disconnect_lock);
1121 if (radio->users == 0) {
1122 if (radio->disconnected) {
1123 video_unregister_device(radio->videodev);
1124 kfree(radio->buffer);
1129 /* stop rds reception */
1130 cancel_delayed_work_sync(&radio->work);
1132 /* cancel read processes */
1133 wake_up_interruptible(&radio->read_queue);
1135 retval = si470x_stop(radio);
1136 usb_autopm_put_interface(radio->intf);
1140 mutex_unlock(&radio->disconnect_lock);
1148 * si470x_fops - file operations interface
1150 static const struct file_operations si470x_fops = {
1151 .owner = THIS_MODULE,
1152 .llseek = no_llseek,
1153 .read = si470x_fops_read,
1154 .poll = si470x_fops_poll,
1155 .ioctl = video_ioctl2,
1156 #ifdef CONFIG_COMPAT
1157 .compat_ioctl = v4l_compat_ioctl32,
1159 .open = si470x_fops_open,
1160 .release = si470x_fops_release,
1165 /**************************************************************************
1166 * Video4Linux Interface
1167 **************************************************************************/
1170 * si470x_v4l2_queryctrl - query control
1172 static struct v4l2_queryctrl si470x_v4l2_queryctrl[] = {
1174 .id = V4L2_CID_AUDIO_VOLUME,
1175 .type = V4L2_CTRL_TYPE_INTEGER,
1180 .default_value = 15,
1183 .id = V4L2_CID_AUDIO_MUTE,
1184 .type = V4L2_CTRL_TYPE_BOOLEAN,
1195 * si470x_vidioc_querycap - query device capabilities
1197 static int si470x_vidioc_querycap(struct file *file, void *priv,
1198 struct v4l2_capability *capability)
1200 strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver));
1201 strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
1202 sprintf(capability->bus_info, "USB");
1203 capability->version = DRIVER_KERNEL_VERSION;
1204 capability->capabilities = V4L2_CAP_HW_FREQ_SEEK |
1205 V4L2_CAP_TUNER | V4L2_CAP_RADIO;
1212 * si470x_vidioc_queryctrl - enumerate control items
1214 static int si470x_vidioc_queryctrl(struct file *file, void *priv,
1215 struct v4l2_queryctrl *qc)
1217 unsigned char i = 0;
1218 int retval = -EINVAL;
1220 /* abort if qc->id is below V4L2_CID_BASE */
1221 if (qc->id < V4L2_CID_BASE)
1224 /* search video control */
1225 for (i = 0; i < ARRAY_SIZE(si470x_v4l2_queryctrl); i++) {
1226 if (qc->id == si470x_v4l2_queryctrl[i].id) {
1227 memcpy(qc, &(si470x_v4l2_queryctrl[i]), sizeof(*qc));
1228 retval = 0; /* found */
1233 /* disable unsupported base controls */
1234 /* to satisfy kradio and such apps */
1235 if ((retval == -EINVAL) && (qc->id < V4L2_CID_LASTP1)) {
1236 qc->flags = V4L2_CTRL_FLAG_DISABLED;
1242 printk(KERN_WARNING DRIVER_NAME
1243 ": query controls failed with %d\n", retval);
1249 * si470x_vidioc_g_ctrl - get the value of a control
1251 static int si470x_vidioc_g_ctrl(struct file *file, void *priv,
1252 struct v4l2_control *ctrl)
1254 struct si470x_device *radio = video_drvdata(file);
1258 if (radio->disconnected) {
1264 case V4L2_CID_AUDIO_VOLUME:
1265 ctrl->value = radio->registers[SYSCONFIG2] &
1268 case V4L2_CID_AUDIO_MUTE:
1269 ctrl->value = ((radio->registers[POWERCFG] &
1270 POWERCFG_DMUTE) == 0) ? 1 : 0;
1278 printk(KERN_WARNING DRIVER_NAME
1279 ": get control failed with %d\n", retval);
1285 * si470x_vidioc_s_ctrl - set the value of a control
1287 static int si470x_vidioc_s_ctrl(struct file *file, void *priv,
1288 struct v4l2_control *ctrl)
1290 struct si470x_device *radio = video_drvdata(file);
1294 if (radio->disconnected) {
1300 case V4L2_CID_AUDIO_VOLUME:
1301 radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_VOLUME;
1302 radio->registers[SYSCONFIG2] |= ctrl->value;
1303 retval = si470x_set_register(radio, SYSCONFIG2);
1305 case V4L2_CID_AUDIO_MUTE:
1306 if (ctrl->value == 1)
1307 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
1309 radio->registers[POWERCFG] |= POWERCFG_DMUTE;
1310 retval = si470x_set_register(radio, POWERCFG);
1318 printk(KERN_WARNING DRIVER_NAME
1319 ": set control failed with %d\n", retval);
1325 * si470x_vidioc_g_audio - get audio attributes
1327 static int si470x_vidioc_g_audio(struct file *file, void *priv,
1328 struct v4l2_audio *audio)
1330 /* driver constants */
1332 strcpy(audio->name, "Radio");
1333 audio->capability = V4L2_AUDCAP_STEREO;
1341 * si470x_vidioc_g_tuner - get tuner attributes
1343 static int si470x_vidioc_g_tuner(struct file *file, void *priv,
1344 struct v4l2_tuner *tuner)
1346 struct si470x_device *radio = video_drvdata(file);
1350 if (radio->disconnected) {
1354 if (tuner->index != 0) {
1359 retval = si470x_get_register(radio, STATUSRSSI);
1363 /* driver constants */
1364 strcpy(tuner->name, "FM");
1365 tuner->type = V4L2_TUNER_RADIO;
1366 tuner->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
1369 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
1370 /* 0: 87.5 - 108 MHz (USA, Europe, default) */
1372 tuner->rangelow = 87.5 * FREQ_MUL;
1373 tuner->rangehigh = 108 * FREQ_MUL;
1375 /* 1: 76 - 108 MHz (Japan wide band) */
1377 tuner->rangelow = 76 * FREQ_MUL;
1378 tuner->rangehigh = 108 * FREQ_MUL;
1380 /* 2: 76 - 90 MHz (Japan) */
1382 tuner->rangelow = 76 * FREQ_MUL;
1383 tuner->rangehigh = 90 * FREQ_MUL;
1387 /* stereo indicator == stereo (instead of mono) */
1388 if ((radio->registers[STATUSRSSI] & STATUSRSSI_ST) == 1)
1389 tuner->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
1391 tuner->rxsubchans = V4L2_TUNER_SUB_MONO;
1393 /* mono/stereo selector */
1394 if ((radio->registers[POWERCFG] & POWERCFG_MONO) == 1)
1395 tuner->audmode = V4L2_TUNER_MODE_MONO;
1397 tuner->audmode = V4L2_TUNER_MODE_STEREO;
1399 /* min is worst, max is best; signal:0..0xffff; rssi: 0..0xff */
1400 tuner->signal = (radio->registers[STATUSRSSI] & STATUSRSSI_RSSI)
1403 /* automatic frequency control: -1: freq to low, 1 freq to high */
1404 /* AFCRL does only indicate that freq. differs, not if too low/high */
1405 tuner->afc = (radio->registers[STATUSRSSI] & STATUSRSSI_AFCRL) ? 1 : 0;
1409 printk(KERN_WARNING DRIVER_NAME
1410 ": get tuner failed with %d\n", retval);
1416 * si470x_vidioc_s_tuner - set tuner attributes
1418 static int si470x_vidioc_s_tuner(struct file *file, void *priv,
1419 struct v4l2_tuner *tuner)
1421 struct si470x_device *radio = video_drvdata(file);
1422 int retval = -EINVAL;
1425 if (radio->disconnected) {
1429 if (tuner->index != 0)
1432 /* mono/stereo selector */
1433 switch (tuner->audmode) {
1434 case V4L2_TUNER_MODE_MONO:
1435 radio->registers[POWERCFG] |= POWERCFG_MONO; /* force mono */
1437 case V4L2_TUNER_MODE_STEREO:
1438 radio->registers[POWERCFG] &= ~POWERCFG_MONO; /* try stereo */
1444 retval = si470x_set_register(radio, POWERCFG);
1448 printk(KERN_WARNING DRIVER_NAME
1449 ": set tuner failed with %d\n", retval);
1455 * si470x_vidioc_g_frequency - get tuner or modulator radio frequency
1457 static int si470x_vidioc_g_frequency(struct file *file, void *priv,
1458 struct v4l2_frequency *freq)
1460 struct si470x_device *radio = video_drvdata(file);
1464 if (radio->disconnected) {
1468 if (freq->tuner != 0) {
1473 freq->type = V4L2_TUNER_RADIO;
1474 retval = si470x_get_freq(radio, &freq->frequency);
1478 printk(KERN_WARNING DRIVER_NAME
1479 ": get frequency failed with %d\n", retval);
1485 * si470x_vidioc_s_frequency - set tuner or modulator radio frequency
1487 static int si470x_vidioc_s_frequency(struct file *file, void *priv,
1488 struct v4l2_frequency *freq)
1490 struct si470x_device *radio = video_drvdata(file);
1494 if (radio->disconnected) {
1498 if (freq->tuner != 0) {
1503 retval = si470x_set_freq(radio, freq->frequency);
1507 printk(KERN_WARNING DRIVER_NAME
1508 ": set frequency failed with %d\n", retval);
1514 * si470x_vidioc_s_hw_freq_seek - set hardware frequency seek
1516 static int si470x_vidioc_s_hw_freq_seek(struct file *file, void *priv,
1517 struct v4l2_hw_freq_seek *seek)
1519 struct si470x_device *radio = video_drvdata(file);
1523 if (radio->disconnected) {
1527 if (seek->tuner != 0) {
1532 retval = si470x_set_seek(radio, seek->wrap_around, seek->seek_upward);
1536 printk(KERN_WARNING DRIVER_NAME
1537 ": set hardware frequency seek failed with %d\n",
1544 * si470x_ioctl_ops - video device ioctl operations
1546 static const struct v4l2_ioctl_ops si470x_ioctl_ops = {
1547 .vidioc_querycap = si470x_vidioc_querycap,
1548 .vidioc_queryctrl = si470x_vidioc_queryctrl,
1549 .vidioc_g_ctrl = si470x_vidioc_g_ctrl,
1550 .vidioc_s_ctrl = si470x_vidioc_s_ctrl,
1551 .vidioc_g_audio = si470x_vidioc_g_audio,
1552 .vidioc_g_tuner = si470x_vidioc_g_tuner,
1553 .vidioc_s_tuner = si470x_vidioc_s_tuner,
1554 .vidioc_g_frequency = si470x_vidioc_g_frequency,
1555 .vidioc_s_frequency = si470x_vidioc_s_frequency,
1556 .vidioc_s_hw_freq_seek = si470x_vidioc_s_hw_freq_seek,
1561 * si470x_viddev_template - video device interface
1563 static struct video_device si470x_viddev_template = {
1564 .fops = &si470x_fops,
1565 .name = DRIVER_NAME,
1566 .release = video_device_release,
1567 .ioctl_ops = &si470x_ioctl_ops,
1572 /**************************************************************************
1574 **************************************************************************/
1577 * si470x_usb_driver_probe - probe for the device
1579 static int si470x_usb_driver_probe(struct usb_interface *intf,
1580 const struct usb_device_id *id)
1582 struct si470x_device *radio;
1585 /* private data allocation and initialization */
1586 radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL);
1592 radio->disconnected = 0;
1593 radio->usbdev = interface_to_usbdev(intf);
1595 mutex_init(&radio->disconnect_lock);
1596 mutex_init(&radio->lock);
1598 /* video device allocation and initialization */
1599 radio->videodev = video_device_alloc();
1600 if (!radio->videodev) {
1604 memcpy(radio->videodev, &si470x_viddev_template,
1605 sizeof(si470x_viddev_template));
1606 video_set_drvdata(radio->videodev, radio);
1608 /* show some infos about the specific device */
1609 if (si470x_get_all_registers(radio) < 0) {
1613 printk(KERN_INFO DRIVER_NAME ": DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",
1614 radio->registers[DEVICEID], radio->registers[CHIPID]);
1616 /* check if firmware is current */
1617 if ((radio->registers[CHIPID] & CHIPID_FIRMWARE)
1618 < RADIO_SW_VERSION_CURRENT) {
1619 printk(KERN_WARNING DRIVER_NAME
1620 ": This driver is known to work with "
1621 "firmware version %hu,\n", RADIO_SW_VERSION_CURRENT);
1622 printk(KERN_WARNING DRIVER_NAME
1623 ": but the device has firmware version %hu.\n",
1624 radio->registers[CHIPID] & CHIPID_FIRMWARE);
1625 printk(KERN_WARNING DRIVER_NAME
1626 ": If you have some trouble using this driver,\n");
1627 printk(KERN_WARNING DRIVER_NAME
1628 ": please report to V4L ML at "
1629 "video4linux-list@redhat.com\n");
1632 /* set initial frequency */
1633 si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
1635 /* rds buffer allocation */
1636 radio->buf_size = rds_buf * 3;
1637 radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
1638 if (!radio->buffer) {
1643 /* rds buffer configuration */
1644 radio->wr_index = 0;
1645 radio->rd_index = 0;
1646 init_waitqueue_head(&radio->read_queue);
1648 /* prepare rds work function */
1649 INIT_DELAYED_WORK(&radio->work, si470x_work);
1651 /* register video device */
1652 retval = video_register_device(radio->videodev, VFL_TYPE_RADIO, radio_nr);
1654 printk(KERN_WARNING DRIVER_NAME
1655 ": Could not register video device\n");
1658 usb_set_intfdata(intf, radio);
1662 video_device_release(radio->videodev);
1663 kfree(radio->buffer);
1672 * si470x_usb_driver_suspend - suspend the device
1674 static int si470x_usb_driver_suspend(struct usb_interface *intf,
1675 pm_message_t message)
1677 struct si470x_device *radio = usb_get_intfdata(intf);
1679 printk(KERN_INFO DRIVER_NAME ": suspending now...\n");
1681 cancel_delayed_work_sync(&radio->work);
1688 * si470x_usb_driver_resume - resume the device
1690 static int si470x_usb_driver_resume(struct usb_interface *intf)
1692 struct si470x_device *radio = usb_get_intfdata(intf);
1694 printk(KERN_INFO DRIVER_NAME ": resuming now...\n");
1696 mutex_lock(&radio->lock);
1697 if (radio->users && radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS)
1698 schedule_delayed_work(&radio->work,
1699 msecs_to_jiffies(rds_poll_time));
1700 mutex_unlock(&radio->lock);
1707 * si470x_usb_driver_disconnect - disconnect the device
1709 static void si470x_usb_driver_disconnect(struct usb_interface *intf)
1711 struct si470x_device *radio = usb_get_intfdata(intf);
1713 mutex_lock(&radio->disconnect_lock);
1714 radio->disconnected = 1;
1715 cancel_delayed_work_sync(&radio->work);
1716 usb_set_intfdata(intf, NULL);
1717 if (radio->users == 0) {
1718 video_unregister_device(radio->videodev);
1719 kfree(radio->buffer);
1722 mutex_unlock(&radio->disconnect_lock);
1727 * si470x_usb_driver - usb driver interface
1729 static struct usb_driver si470x_usb_driver = {
1730 .name = DRIVER_NAME,
1731 .probe = si470x_usb_driver_probe,
1732 .disconnect = si470x_usb_driver_disconnect,
1733 .suspend = si470x_usb_driver_suspend,
1734 .resume = si470x_usb_driver_resume,
1735 .id_table = si470x_usb_driver_id_table,
1736 .supports_autosuspend = 1,
1741 /**************************************************************************
1743 **************************************************************************/
1746 * si470x_module_init - module init
1748 static int __init si470x_module_init(void)
1750 printk(KERN_INFO DRIVER_DESC ", Version " DRIVER_VERSION "\n");
1751 return usb_register(&si470x_usb_driver);
1756 * si470x_module_exit - module exit
1758 static void __exit si470x_module_exit(void)
1760 usb_deregister(&si470x_usb_driver);
1764 module_init(si470x_module_init);
1765 module_exit(si470x_module_exit);
1767 MODULE_LICENSE("GPL");
1768 MODULE_AUTHOR(DRIVER_AUTHOR);
1769 MODULE_DESCRIPTION(DRIVER_DESC);
1770 MODULE_VERSION(DRIVER_VERSION);