2 * drivers/media/radio/radio-si470x.c
4 * Driver for USB radios for the Silicon Labs Si470x FM Radio Receivers:
5 * - Silicon Labs USB FM Radio Reference Design
6 * - ADS/Tech FM Radio Receiver (formerly Instant FM Music) (RDX-155-EF)
8 * Copyright (c) 2008 Tobias Lorenz <tobias.lorenz@gmx.net>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 * - USB Audio is provided by the alsa snd_usb_audio module.
29 * For listing you have to redirect the sound, for example using:
30 * arecord -D hw:1,0 -r96000 -c2 -f S16_LE | artsdsp aplay -B -
31 * - regarding module parameters in /sys/module/radio_si470x/parameters:
32 * the contents of read-only files (0444) are not updated, even if
33 * space, band and de are changed using private video controls
34 * - increase tune_timeout, if you often get -EIO errors
35 * - hw_freq_seek returns -EAGAIN, when timed out or band limit is reached
41 * 2008-01-12 Tobias Lorenz <tobias.lorenz@gmx.net>
43 * - First working version
44 * 2008-01-13 Tobias Lorenz <tobias.lorenz@gmx.net>
46 * - Improved error handling, every function now returns errno
47 * - Improved multi user access (start/mute/stop)
48 * - Channel doesn't get lost anymore after start/mute/stop
49 * - RDS support added (polling mode via interrupt EP 1)
50 * - marked default module parameters with *value*
51 * - switched from bit structs to bit masks
52 * - header file cleaned and integrated
53 * 2008-01-14 Tobias Lorenz <tobias.lorenz@gmx.net>
55 * - hex values are now lower case
56 * - commented USB ID for ADS/Tech moved on todo list
57 * - blacklisted si470x in hid-quirks.c
58 * - rds buffer handling functions integrated into *_work, *_read
59 * - rds_command in si470x_poll exchanged against simple retval
60 * - check for firmware version 15
61 * - code order and prototypes still remain the same
62 * - spacing and bottom of band codes remain the same
63 * 2008-01-16 Tobias Lorenz <tobias.lorenz@gmx.net>
65 * - code reordered to avoid function prototypes
66 * - switch/case defaults are now more user-friendly
67 * - unified comment style
68 * - applied all checkpatch.pl v1.12 suggestions
69 * except the warning about the too long lines with bit comments
70 * - renamed FMRADIO to RADIO to cut line length (checkpatch.pl)
71 * 2008-01-22 Tobias Lorenz <tobias.lorenz@gmx.net>
73 * - avoid poss. locking when doing copy_to_user which may sleep
74 * - RDS is automatically activated on read now
75 * - code cleaned of unnecessary rds_commands
76 * - USB Vendor/Product ID for ADS/Tech FM Radio Receiver verified
77 * (thanks to Guillaume RAMOUSSE)
78 * 2008-01-27 Tobias Lorenz <tobias.lorenz@gmx.net>
80 * - number of seek_retries changed to tune_timeout
81 * - fixed problem with incomplete tune operations by own buffers
82 * - optimization of variables and printf types
83 * - improved error logging
84 * 2008-01-31 Tobias Lorenz <tobias.lorenz@gmx.net>
85 * Oliver Neukum <oliver@neukum.org>
87 * - fixed coverity checker warnings in *_usb_driver_disconnect
88 * - probe()/open() race by correct ordering in probe()
89 * - DMA coherency rules by separate allocation of all buffers
90 * - use of endianness macros
91 * - abuse of spinlock, replaced by mutex
92 * - racy handling of timer in disconnect,
93 * replaced by delayed_work
94 * - racy interruptible_sleep_on(),
95 * replaced with wait_event_interruptible()
96 * - handle signals in read()
97 * 2008-02-08 Tobias Lorenz <tobias.lorenz@gmx.net>
98 * Oliver Neukum <oliver@neukum.org>
100 * - usb autosuspend support
102 * 2008-05-07 Tobias Lorenz <tobias.lorenz@gmx.net>
104 * - hardware frequency seek support
106 * - more safety checks, let si470x_get_freq return errno
107 * - vidioc behavior corrected according to v4l2 spec
110 * - add firmware download/update support
111 * - RDS support: interrupt mode, instead of polling
112 * - add LED status output (check if that's not already done in firmware)
116 /* driver definitions */
117 #define DRIVER_AUTHOR "Tobias Lorenz <tobias.lorenz@gmx.net>"
118 #define DRIVER_NAME "radio-si470x"
119 #define DRIVER_KERNEL_VERSION KERNEL_VERSION(1, 0, 8)
120 #define DRIVER_CARD "Silicon Labs Si470x FM Radio Receiver"
121 #define DRIVER_DESC "USB radio driver for Si470x FM Radio Receivers"
122 #define DRIVER_VERSION "1.0.8"
125 /* kernel includes */
126 #include <linux/kernel.h>
127 #include <linux/module.h>
128 #include <linux/init.h>
129 #include <linux/slab.h>
130 #include <linux/input.h>
131 #include <linux/usb.h>
132 #include <linux/hid.h>
133 #include <linux/version.h>
134 #include <linux/videodev2.h>
135 #include <linux/mutex.h>
136 #include <media/v4l2-common.h>
137 #include <media/v4l2-ioctl.h>
138 #include <media/rds.h>
139 #include <asm/unaligned.h>
142 /* USB Device ID List */
143 static struct usb_device_id si470x_usb_driver_id_table[] = {
144 /* Silicon Labs USB FM Radio Reference Design */
145 { USB_DEVICE_AND_INTERFACE_INFO(0x10c4, 0x818a, USB_CLASS_HID, 0, 0) },
146 /* ADS/Tech FM Radio Receiver (formerly Instant FM Music) */
147 { USB_DEVICE_AND_INTERFACE_INFO(0x06e1, 0xa155, USB_CLASS_HID, 0, 0) },
148 /* Terminating entry */
151 MODULE_DEVICE_TABLE(usb, si470x_usb_driver_id_table);
155 /**************************************************************************
157 **************************************************************************/
160 static int radio_nr = -1;
161 module_param(radio_nr, int, 0444);
162 MODULE_PARM_DESC(radio_nr, "Radio Nr");
165 /* 0: 200 kHz (USA, Australia) */
166 /* 1: 100 kHz (Europe, Japan) */
168 static unsigned short space = 2;
169 module_param(space, ushort, 0444);
170 MODULE_PARM_DESC(space, "Spacing: 0=200kHz 1=100kHz *2=50kHz*");
172 /* Bottom of Band (MHz) */
173 /* 0: 87.5 - 108 MHz (USA, Europe)*/
174 /* 1: 76 - 108 MHz (Japan wide band) */
175 /* 2: 76 - 90 MHz (Japan) */
176 static unsigned short band = 1;
177 module_param(band, ushort, 0444);
178 MODULE_PARM_DESC(band, "Band: 0=87.5..108MHz *1=76..108MHz* 2=76..90MHz");
182 /* 1: 50 us (Europe, Australia, Japan) */
183 static unsigned short de = 1;
184 module_param(de, ushort, 0444);
185 MODULE_PARM_DESC(de, "De-emphasis: 0=75us *1=50us*");
188 static unsigned int usb_timeout = 500;
189 module_param(usb_timeout, uint, 0644);
190 MODULE_PARM_DESC(usb_timeout, "USB timeout (ms): *500*");
193 static unsigned int tune_timeout = 3000;
194 module_param(tune_timeout, uint, 0644);
195 MODULE_PARM_DESC(tune_timeout, "Tune timeout: *3000*");
198 static unsigned int seek_timeout = 5000;
199 module_param(seek_timeout, uint, 0644);
200 MODULE_PARM_DESC(seek_timeout, "Seek timeout: *5000*");
202 /* RDS buffer blocks */
203 static unsigned int rds_buf = 100;
204 module_param(rds_buf, uint, 0444);
205 MODULE_PARM_DESC(rds_buf, "RDS buffer entries: *100*");
207 /* RDS maximum block errors */
208 static unsigned short max_rds_errors = 1;
209 /* 0 means 0 errors requiring correction */
210 /* 1 means 1-2 errors requiring correction (used by original USBRadio.exe) */
211 /* 2 means 3-5 errors requiring correction */
212 /* 3 means 6+ errors or errors in checkword, correction not possible */
213 module_param(max_rds_errors, ushort, 0644);
214 MODULE_PARM_DESC(max_rds_errors, "RDS maximum block errors: *1*");
216 /* RDS poll frequency */
217 static unsigned int rds_poll_time = 40;
218 /* 40 is used by the original USBRadio.exe */
219 /* 50 is used by radio-cadet */
220 /* 75 should be okay */
221 /* 80 is the usual RDS receive interval */
222 module_param(rds_poll_time, uint, 0644);
223 MODULE_PARM_DESC(rds_poll_time, "RDS poll time (ms): *40*");
227 /**************************************************************************
228 * Register Definitions
229 **************************************************************************/
230 #define RADIO_REGISTER_SIZE 2 /* 16 register bit width */
231 #define RADIO_REGISTER_NUM 16 /* DEVICEID ... RDSD */
232 #define RDS_REGISTER_NUM 6 /* STATUSRSSI ... RDSD */
234 #define DEVICEID 0 /* Device ID */
235 #define DEVICEID_PN 0xf000 /* bits 15..12: Part Number */
236 #define DEVICEID_MFGID 0x0fff /* bits 11..00: Manufacturer ID */
238 #define CHIPID 1 /* Chip ID */
239 #define CHIPID_REV 0xfc00 /* bits 15..10: Chip Version */
240 #define CHIPID_DEV 0x0200 /* bits 09..09: Device */
241 #define CHIPID_FIRMWARE 0x01ff /* bits 08..00: Firmware Version */
243 #define POWERCFG 2 /* Power Configuration */
244 #define POWERCFG_DSMUTE 0x8000 /* bits 15..15: Softmute Disable */
245 #define POWERCFG_DMUTE 0x4000 /* bits 14..14: Mute Disable */
246 #define POWERCFG_MONO 0x2000 /* bits 13..13: Mono Select */
247 #define POWERCFG_RDSM 0x0800 /* bits 11..11: RDS Mode (Si4701 only) */
248 #define POWERCFG_SKMODE 0x0400 /* bits 10..10: Seek Mode */
249 #define POWERCFG_SEEKUP 0x0200 /* bits 09..09: Seek Direction */
250 #define POWERCFG_SEEK 0x0100 /* bits 08..08: Seek */
251 #define POWERCFG_DISABLE 0x0040 /* bits 06..06: Powerup Disable */
252 #define POWERCFG_ENABLE 0x0001 /* bits 00..00: Powerup Enable */
254 #define CHANNEL 3 /* Channel */
255 #define CHANNEL_TUNE 0x8000 /* bits 15..15: Tune */
256 #define CHANNEL_CHAN 0x03ff /* bits 09..00: Channel Select */
258 #define SYSCONFIG1 4 /* System Configuration 1 */
259 #define SYSCONFIG1_RDSIEN 0x8000 /* bits 15..15: RDS Interrupt Enable (Si4701 only) */
260 #define SYSCONFIG1_STCIEN 0x4000 /* bits 14..14: Seek/Tune Complete Interrupt Enable */
261 #define SYSCONFIG1_RDS 0x1000 /* bits 12..12: RDS Enable (Si4701 only) */
262 #define SYSCONFIG1_DE 0x0800 /* bits 11..11: De-emphasis (0=75us 1=50us) */
263 #define SYSCONFIG1_AGCD 0x0400 /* bits 10..10: AGC Disable */
264 #define SYSCONFIG1_BLNDADJ 0x00c0 /* bits 07..06: Stereo/Mono Blend Level Adjustment */
265 #define SYSCONFIG1_GPIO3 0x0030 /* bits 05..04: General Purpose I/O 3 */
266 #define SYSCONFIG1_GPIO2 0x000c /* bits 03..02: General Purpose I/O 2 */
267 #define SYSCONFIG1_GPIO1 0x0003 /* bits 01..00: General Purpose I/O 1 */
269 #define SYSCONFIG2 5 /* System Configuration 2 */
270 #define SYSCONFIG2_SEEKTH 0xff00 /* bits 15..08: RSSI Seek Threshold */
271 #define SYSCONFIG2_BAND 0x0080 /* bits 07..06: Band Select */
272 #define SYSCONFIG2_SPACE 0x0030 /* bits 05..04: Channel Spacing */
273 #define SYSCONFIG2_VOLUME 0x000f /* bits 03..00: Volume */
275 #define SYSCONFIG3 6 /* System Configuration 3 */
276 #define SYSCONFIG3_SMUTER 0xc000 /* bits 15..14: Softmute Attack/Recover Rate */
277 #define SYSCONFIG3_SMUTEA 0x3000 /* bits 13..12: Softmute Attenuation */
278 #define SYSCONFIG3_SKSNR 0x00f0 /* bits 07..04: Seek SNR Threshold */
279 #define SYSCONFIG3_SKCNT 0x000f /* bits 03..00: Seek FM Impulse Detection Threshold */
281 #define TEST1 7 /* Test 1 */
282 #define TEST1_AHIZEN 0x4000 /* bits 14..14: Audio High-Z Enable */
284 #define TEST2 8 /* Test 2 */
285 /* TEST2 only contains reserved bits */
287 #define BOOTCONFIG 9 /* Boot Configuration */
288 /* BOOTCONFIG only contains reserved bits */
290 #define STATUSRSSI 10 /* Status RSSI */
291 #define STATUSRSSI_RDSR 0x8000 /* bits 15..15: RDS Ready (Si4701 only) */
292 #define STATUSRSSI_STC 0x4000 /* bits 14..14: Seek/Tune Complete */
293 #define STATUSRSSI_SF 0x2000 /* bits 13..13: Seek Fail/Band Limit */
294 #define STATUSRSSI_AFCRL 0x1000 /* bits 12..12: AFC Rail */
295 #define STATUSRSSI_RDSS 0x0800 /* bits 11..11: RDS Synchronized (Si4701 only) */
296 #define STATUSRSSI_BLERA 0x0600 /* bits 10..09: RDS Block A Errors (Si4701 only) */
297 #define STATUSRSSI_ST 0x0100 /* bits 08..08: Stereo Indicator */
298 #define STATUSRSSI_RSSI 0x00ff /* bits 07..00: RSSI (Received Signal Strength Indicator) */
300 #define READCHAN 11 /* Read Channel */
301 #define READCHAN_BLERB 0xc000 /* bits 15..14: RDS Block D Errors (Si4701 only) */
302 #define READCHAN_BLERC 0x3000 /* bits 13..12: RDS Block C Errors (Si4701 only) */
303 #define READCHAN_BLERD 0x0c00 /* bits 11..10: RDS Block B Errors (Si4701 only) */
304 #define READCHAN_READCHAN 0x03ff /* bits 09..00: Read Channel */
306 #define RDSA 12 /* RDSA */
307 #define RDSA_RDSA 0xffff /* bits 15..00: RDS Block A Data (Si4701 only) */
309 #define RDSB 13 /* RDSB */
310 #define RDSB_RDSB 0xffff /* bits 15..00: RDS Block B Data (Si4701 only) */
312 #define RDSC 14 /* RDSC */
313 #define RDSC_RDSC 0xffff /* bits 15..00: RDS Block C Data (Si4701 only) */
315 #define RDSD 15 /* RDSD */
316 #define RDSD_RDSD 0xffff /* bits 15..00: RDS Block D Data (Si4701 only) */
320 /**************************************************************************
322 **************************************************************************/
324 /* Reports 1-16 give direct read/write access to the 16 Si470x registers */
325 /* with the (REPORT_ID - 1) corresponding to the register address across USB */
326 /* endpoint 0 using GET_REPORT and SET_REPORT */
327 #define REGISTER_REPORT_SIZE (RADIO_REGISTER_SIZE + 1)
328 #define REGISTER_REPORT(reg) ((reg) + 1)
330 /* Report 17 gives direct read/write access to the entire Si470x register */
331 /* map across endpoint 0 using GET_REPORT and SET_REPORT */
332 #define ENTIRE_REPORT_SIZE (RADIO_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
333 #define ENTIRE_REPORT 17
335 /* Report 18 is used to send the lowest 6 Si470x registers up the HID */
336 /* interrupt endpoint 1 to Windows every 20 milliseconds for status */
337 #define RDS_REPORT_SIZE (RDS_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
338 #define RDS_REPORT 18
340 /* Report 19: LED state */
341 #define LED_REPORT_SIZE 3
342 #define LED_REPORT 19
344 /* Report 19: stream */
345 #define STREAM_REPORT_SIZE 3
346 #define STREAM_REPORT 19
348 /* Report 20: scratch */
349 #define SCRATCH_PAGE_SIZE 63
350 #define SCRATCH_REPORT_SIZE (SCRATCH_PAGE_SIZE + 1)
351 #define SCRATCH_REPORT 20
353 /* Reports 19-22: flash upgrade of the C8051F321 */
354 #define WRITE_REPORT 19
355 #define FLASH_REPORT 20
356 #define CRC_REPORT 21
357 #define RESPONSE_REPORT 22
359 /* Report 23: currently unused, but can accept 60 byte reports on the HID */
360 /* interrupt out endpoint 2 every 1 millisecond */
361 #define UNUSED_REPORT 23
365 /**************************************************************************
366 * Software/Hardware Versions
367 **************************************************************************/
368 #define RADIO_SW_VERSION_NOT_BOOTLOADABLE 6
369 #define RADIO_SW_VERSION 7
370 #define RADIO_SW_VERSION_CURRENT 15
371 #define RADIO_HW_VERSION 1
373 #define SCRATCH_PAGE_SW_VERSION 1
374 #define SCRATCH_PAGE_HW_VERSION 2
378 /**************************************************************************
379 * LED State Definitions
380 **************************************************************************/
381 #define LED_COMMAND 0x35
383 #define NO_CHANGE_LED 0x00
384 #define ALL_COLOR_LED 0x01 /* streaming state */
385 #define BLINK_GREEN_LED 0x02 /* connect state */
386 #define BLINK_RED_LED 0x04
387 #define BLINK_ORANGE_LED 0x10 /* disconnect state */
388 #define SOLID_GREEN_LED 0x20 /* tuning/seeking state */
389 #define SOLID_RED_LED 0x40 /* bootload state */
390 #define SOLID_ORANGE_LED 0x80
394 /**************************************************************************
395 * Stream State Definitions
396 **************************************************************************/
397 #define STREAM_COMMAND 0x36
398 #define STREAM_VIDPID 0x00
399 #define STREAM_AUDIO 0xff
403 /**************************************************************************
404 * Bootloader / Flash Commands
405 **************************************************************************/
407 /* unique id sent to bootloader and required to put into a bootload state */
408 #define UNIQUE_BL_ID 0x34
410 /* mask for the flash data */
411 #define FLASH_DATA_MASK 0x55
413 /* bootloader commands */
414 #define GET_SW_VERSION_COMMAND 0x00
415 #define SET_PAGE_COMMAND 0x01
416 #define ERASE_PAGE_COMMAND 0x02
417 #define WRITE_PAGE_COMMAND 0x03
418 #define CRC_ON_PAGE_COMMAND 0x04
419 #define READ_FLASH_BYTE_COMMAND 0x05
420 #define RESET_DEVICE_COMMAND 0x06
421 #define GET_HW_VERSION_COMMAND 0x07
424 /* bootloader command responses */
425 #define COMMAND_OK 0x01
426 #define COMMAND_FAILED 0x02
427 #define COMMAND_PENDING 0x03
430 #define COMMAND_BUFFER_SIZE 4
431 #define RESPONSE_BUFFER_SIZE 2
432 #define FLASH_BUFFER_SIZE 64
433 #define CRC_BUFFER_SIZE 3
437 /**************************************************************************
438 * General Driver Definitions
439 **************************************************************************/
442 * si470x_device - private data
444 struct si470x_device {
445 /* reference to USB and video device */
446 struct usb_device *usbdev;
447 struct usb_interface *intf;
448 struct video_device *videodev;
450 /* driver management */
452 unsigned char disconnected;
453 struct mutex disconnect_lock;
455 /* Silabs internal registers (0..15) */
456 unsigned short registers[RADIO_REGISTER_NUM];
458 /* RDS receive buffer */
459 struct delayed_work work;
460 wait_queue_head_t read_queue;
461 struct mutex lock; /* buffer locking */
462 unsigned char *buffer; /* size is always multiple of three */
463 unsigned int buf_size;
464 unsigned int rd_index;
465 unsigned int wr_index;
470 * The frequency is set in units of 62.5 Hz when using V4L2_TUNER_CAP_LOW,
471 * 62.5 kHz otherwise.
472 * The tuner is able to have a channel spacing of 50, 100 or 200 kHz.
473 * tuner->capability is therefore set to V4L2_TUNER_CAP_LOW
474 * The FREQ_MUL is then: 1 MHz / 62.5 Hz = 16000
476 #define FREQ_MUL (1000000 / 62.5)
480 /**************************************************************************
481 * General Driver Functions
482 **************************************************************************/
485 * si470x_get_report - receive a HID report
487 static int si470x_get_report(struct si470x_device *radio, void *buf, int size)
489 unsigned char *report = (unsigned char *) buf;
492 retval = usb_control_msg(radio->usbdev,
493 usb_rcvctrlpipe(radio->usbdev, 0),
495 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
497 buf, size, usb_timeout);
500 printk(KERN_WARNING DRIVER_NAME
501 ": si470x_get_report: usb_control_msg returned %d\n",
508 * si470x_set_report - send a HID report
510 static int si470x_set_report(struct si470x_device *radio, void *buf, int size)
512 unsigned char *report = (unsigned char *) buf;
515 retval = usb_control_msg(radio->usbdev,
516 usb_sndctrlpipe(radio->usbdev, 0),
518 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
520 buf, size, usb_timeout);
523 printk(KERN_WARNING DRIVER_NAME
524 ": si470x_set_report: usb_control_msg returned %d\n",
531 * si470x_get_register - read register
533 static int si470x_get_register(struct si470x_device *radio, int regnr)
535 unsigned char buf[REGISTER_REPORT_SIZE];
538 buf[0] = REGISTER_REPORT(regnr);
540 retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
543 radio->registers[regnr] = get_unaligned_be16(&buf[1]);
545 return (retval < 0) ? -EINVAL : 0;
550 * si470x_set_register - write register
552 static int si470x_set_register(struct si470x_device *radio, int regnr)
554 unsigned char buf[REGISTER_REPORT_SIZE];
557 buf[0] = REGISTER_REPORT(regnr);
558 put_unaligned_be16(radio->registers[regnr], &buf[1]);
560 retval = si470x_set_report(radio, (void *) &buf, sizeof(buf));
562 return (retval < 0) ? -EINVAL : 0;
567 * si470x_get_all_registers - read entire registers
569 static int si470x_get_all_registers(struct si470x_device *radio)
571 unsigned char buf[ENTIRE_REPORT_SIZE];
575 buf[0] = ENTIRE_REPORT;
577 retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
580 for (regnr = 0; regnr < RADIO_REGISTER_NUM; regnr++)
581 radio->registers[regnr] = get_unaligned_be16(
582 &buf[regnr * RADIO_REGISTER_SIZE + 1]);
584 return (retval < 0) ? -EINVAL : 0;
589 * si470x_get_rds_registers - read rds registers
591 static int si470x_get_rds_registers(struct si470x_device *radio)
593 unsigned char buf[RDS_REPORT_SIZE];
600 retval = usb_interrupt_msg(radio->usbdev,
601 usb_rcvintpipe(radio->usbdev, 1),
602 (void *) &buf, sizeof(buf), &size, usb_timeout);
603 if (size != sizeof(buf))
604 printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: "
605 "return size differs: %d != %zu\n", size, sizeof(buf));
607 printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: "
608 "usb_interrupt_msg returned %d\n", retval);
611 for (regnr = 0; regnr < RDS_REGISTER_NUM; regnr++)
612 radio->registers[STATUSRSSI + regnr] =
614 &buf[regnr * RADIO_REGISTER_SIZE + 1]);
616 return (retval < 0) ? -EINVAL : 0;
621 * si470x_set_chan - set the channel
623 static int si470x_set_chan(struct si470x_device *radio, unsigned short chan)
626 unsigned long timeout;
630 radio->registers[CHANNEL] &= ~CHANNEL_CHAN;
631 radio->registers[CHANNEL] |= CHANNEL_TUNE | chan;
632 retval = si470x_set_register(radio, CHANNEL);
636 /* wait till tune operation has completed */
637 timeout = jiffies + msecs_to_jiffies(tune_timeout);
639 retval = si470x_get_register(radio, STATUSRSSI);
642 timed_out = time_after(jiffies, timeout);
643 } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
645 if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
646 printk(KERN_WARNING DRIVER_NAME ": tune does not complete\n");
648 printk(KERN_WARNING DRIVER_NAME
649 ": tune timed out after %u ms\n", tune_timeout);
653 radio->registers[CHANNEL] &= ~CHANNEL_TUNE;
654 retval = si470x_set_register(radio, CHANNEL);
662 * si470x_get_freq - get the frequency
664 static int si470x_get_freq(struct si470x_device *radio, unsigned int *freq)
666 unsigned int spacing, band_bottom;
671 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) {
672 /* 0: 200 kHz (USA, Australia) */
674 spacing = 0.200 * FREQ_MUL; break;
675 /* 1: 100 kHz (Europe, Japan) */
677 spacing = 0.100 * FREQ_MUL; break;
680 spacing = 0.050 * FREQ_MUL; break;
683 /* Bottom of Band (MHz) */
684 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
685 /* 0: 87.5 - 108 MHz (USA, Europe) */
687 band_bottom = 87.5 * FREQ_MUL; break;
688 /* 1: 76 - 108 MHz (Japan wide band) */
690 band_bottom = 76 * FREQ_MUL; break;
691 /* 2: 76 - 90 MHz (Japan) */
693 band_bottom = 76 * FREQ_MUL; break;
697 retval = si470x_get_register(radio, READCHAN);
698 chan = radio->registers[READCHAN] & READCHAN_READCHAN;
700 /* Frequency (MHz) = Spacing (kHz) x Channel + Bottom of Band (MHz) */
701 *freq = chan * spacing + band_bottom;
708 * si470x_set_freq - set the frequency
710 static int si470x_set_freq(struct si470x_device *radio, unsigned int freq)
712 unsigned int spacing, band_bottom;
716 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) {
717 /* 0: 200 kHz (USA, Australia) */
719 spacing = 0.200 * FREQ_MUL; break;
720 /* 1: 100 kHz (Europe, Japan) */
722 spacing = 0.100 * FREQ_MUL; break;
725 spacing = 0.050 * FREQ_MUL; break;
728 /* Bottom of Band (MHz) */
729 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
730 /* 0: 87.5 - 108 MHz (USA, Europe) */
732 band_bottom = 87.5 * FREQ_MUL; break;
733 /* 1: 76 - 108 MHz (Japan wide band) */
735 band_bottom = 76 * FREQ_MUL; break;
736 /* 2: 76 - 90 MHz (Japan) */
738 band_bottom = 76 * FREQ_MUL; break;
741 /* Chan = [ Freq (Mhz) - Bottom of Band (MHz) ] / Spacing (kHz) */
742 chan = (freq - band_bottom) / spacing;
744 return si470x_set_chan(radio, chan);
749 * si470x_set_seek - set seek
751 static int si470x_set_seek(struct si470x_device *radio,
752 unsigned int wrap_around, unsigned int seek_upward)
755 unsigned long timeout;
759 radio->registers[POWERCFG] |= POWERCFG_SEEK;
760 if (wrap_around == 1)
761 radio->registers[POWERCFG] &= ~POWERCFG_SKMODE;
763 radio->registers[POWERCFG] |= POWERCFG_SKMODE;
764 if (seek_upward == 1)
765 radio->registers[POWERCFG] |= POWERCFG_SEEKUP;
767 radio->registers[POWERCFG] &= ~POWERCFG_SEEKUP;
768 retval = si470x_set_register(radio, POWERCFG);
772 /* wait till seek operation has completed */
773 timeout = jiffies + msecs_to_jiffies(seek_timeout);
775 retval = si470x_get_register(radio, STATUSRSSI);
778 timed_out = time_after(jiffies, timeout);
779 } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
781 if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
782 printk(KERN_WARNING DRIVER_NAME ": seek does not complete\n");
783 if (radio->registers[STATUSRSSI] & STATUSRSSI_SF)
784 printk(KERN_WARNING DRIVER_NAME
785 ": seek failed / band limit reached\n");
787 printk(KERN_WARNING DRIVER_NAME
788 ": seek timed out after %u ms\n", seek_timeout);
792 radio->registers[POWERCFG] &= ~POWERCFG_SEEK;
793 retval = si470x_set_register(radio, POWERCFG);
796 /* try again, if timed out */
797 if ((retval == 0) && timed_out)
805 * si470x_start - switch on radio
807 static int si470x_start(struct si470x_device *radio)
812 radio->registers[POWERCFG] =
813 POWERCFG_DMUTE | POWERCFG_ENABLE | POWERCFG_RDSM;
814 retval = si470x_set_register(radio, POWERCFG);
819 radio->registers[SYSCONFIG1] = SYSCONFIG1_DE;
820 retval = si470x_set_register(radio, SYSCONFIG1);
825 radio->registers[SYSCONFIG2] =
826 (0x3f << 8) | /* SEEKTH */
827 ((band << 6) & SYSCONFIG2_BAND) | /* BAND */
828 ((space << 4) & SYSCONFIG2_SPACE) | /* SPACE */
829 15; /* VOLUME (max) */
830 retval = si470x_set_register(radio, SYSCONFIG2);
834 /* reset last channel */
835 retval = si470x_set_chan(radio,
836 radio->registers[CHANNEL] & CHANNEL_CHAN);
844 * si470x_stop - switch off radio
846 static int si470x_stop(struct si470x_device *radio)
851 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
852 retval = si470x_set_register(radio, SYSCONFIG1);
857 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
858 /* POWERCFG_ENABLE has to automatically go low */
859 radio->registers[POWERCFG] |= POWERCFG_ENABLE | POWERCFG_DISABLE;
860 retval = si470x_set_register(radio, POWERCFG);
868 * si470x_rds_on - switch on rds reception
870 static int si470x_rds_on(struct si470x_device *radio)
875 mutex_lock(&radio->lock);
876 radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDS;
877 retval = si470x_set_register(radio, SYSCONFIG1);
879 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
880 mutex_unlock(&radio->lock);
887 /**************************************************************************
888 * RDS Driver Functions
889 **************************************************************************/
892 * si470x_rds - rds processing function
894 static void si470x_rds(struct si470x_device *radio)
896 unsigned char blocknum;
897 unsigned short bler; /* rds block errors */
899 unsigned char tmpbuf[3];
902 if (si470x_get_rds_registers(radio) < 0)
904 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0) {
905 /* No RDS group ready */
908 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSS) == 0) {
909 /* RDS decoder not synchronized */
913 /* copy all four RDS blocks to internal buffer */
914 mutex_lock(&radio->lock);
915 for (blocknum = 0; blocknum < 4; blocknum++) {
918 bler = (radio->registers[STATUSRSSI] &
919 STATUSRSSI_BLERA) >> 9;
920 rds = radio->registers[RDSA];
923 bler = (radio->registers[READCHAN] &
924 READCHAN_BLERB) >> 14;
925 rds = radio->registers[RDSB];
928 bler = (radio->registers[READCHAN] &
929 READCHAN_BLERC) >> 12;
930 rds = radio->registers[RDSC];
933 bler = (radio->registers[READCHAN] &
934 READCHAN_BLERD) >> 10;
935 rds = radio->registers[RDSD];
939 /* Fill the V4L2 RDS buffer */
940 put_unaligned_le16(rds, &tmpbuf);
941 tmpbuf[2] = blocknum; /* offset name */
942 tmpbuf[2] |= blocknum << 3; /* received offset */
943 if (bler > max_rds_errors)
944 tmpbuf[2] |= 0x80; /* uncorrectable errors */
946 tmpbuf[2] |= 0x40; /* corrected error(s) */
948 /* copy RDS block to internal buffer */
949 memcpy(&radio->buffer[radio->wr_index], &tmpbuf, 3);
950 radio->wr_index += 3;
952 /* wrap write pointer */
953 if (radio->wr_index >= radio->buf_size)
956 /* check for overflow */
957 if (radio->wr_index == radio->rd_index) {
958 /* increment and wrap read pointer */
959 radio->rd_index += 3;
960 if (radio->rd_index >= radio->buf_size)
964 mutex_unlock(&radio->lock);
966 /* wake up read queue */
967 if (radio->wr_index != radio->rd_index)
968 wake_up_interruptible(&radio->read_queue);
973 * si470x_work - rds work function
975 static void si470x_work(struct work_struct *work)
977 struct si470x_device *radio = container_of(work, struct si470x_device,
981 if (radio->disconnected)
983 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
987 schedule_delayed_work(&radio->work, msecs_to_jiffies(rds_poll_time));
992 /**************************************************************************
993 * File Operations Interface
994 **************************************************************************/
997 * si470x_fops_read - read RDS data
999 static ssize_t si470x_fops_read(struct file *file, char __user *buf,
1000 size_t count, loff_t *ppos)
1002 struct si470x_device *radio = video_drvdata(file);
1004 unsigned int block_count = 0;
1006 /* switch on rds reception */
1007 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
1008 si470x_rds_on(radio);
1009 schedule_delayed_work(&radio->work,
1010 msecs_to_jiffies(rds_poll_time));
1013 /* block if no new data available */
1014 while (radio->wr_index == radio->rd_index) {
1015 if (file->f_flags & O_NONBLOCK) {
1016 retval = -EWOULDBLOCK;
1019 if (wait_event_interruptible(radio->read_queue,
1020 radio->wr_index != radio->rd_index) < 0) {
1026 /* calculate block count from byte count */
1029 /* copy RDS block out of internal buffer and to user buffer */
1030 mutex_lock(&radio->lock);
1031 while (block_count < count) {
1032 if (radio->rd_index == radio->wr_index)
1035 /* always transfer rds complete blocks */
1036 if (copy_to_user(buf, &radio->buffer[radio->rd_index], 3))
1037 /* retval = -EFAULT; */
1040 /* increment and wrap read pointer */
1041 radio->rd_index += 3;
1042 if (radio->rd_index >= radio->buf_size)
1043 radio->rd_index = 0;
1045 /* increment counters */
1050 mutex_unlock(&radio->lock);
1058 * si470x_fops_poll - poll RDS data
1060 static unsigned int si470x_fops_poll(struct file *file,
1061 struct poll_table_struct *pts)
1063 struct si470x_device *radio = video_drvdata(file);
1066 /* switch on rds reception */
1067 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
1068 si470x_rds_on(radio);
1069 schedule_delayed_work(&radio->work,
1070 msecs_to_jiffies(rds_poll_time));
1073 poll_wait(file, &radio->read_queue, pts);
1075 if (radio->rd_index != radio->wr_index)
1076 retval = POLLIN | POLLRDNORM;
1083 * si470x_fops_open - file open
1085 static int si470x_fops_open(struct inode *inode, struct file *file)
1087 struct si470x_device *radio = video_drvdata(file);
1093 retval = usb_autopm_get_interface(radio->intf);
1100 if (radio->users == 1) {
1101 retval = si470x_start(radio);
1103 usb_autopm_put_interface(radio->intf);
1113 * si470x_fops_release - file release
1115 static int si470x_fops_release(struct inode *inode, struct file *file)
1117 struct si470x_device *radio = video_drvdata(file);
1126 mutex_lock(&radio->disconnect_lock);
1128 if (radio->users == 0) {
1129 if (radio->disconnected) {
1130 video_unregister_device(radio->videodev);
1131 kfree(radio->buffer);
1136 /* stop rds reception */
1137 cancel_delayed_work_sync(&radio->work);
1139 /* cancel read processes */
1140 wake_up_interruptible(&radio->read_queue);
1142 retval = si470x_stop(radio);
1143 usb_autopm_put_interface(radio->intf);
1147 mutex_unlock(&radio->disconnect_lock);
1155 * si470x_fops - file operations interface
1157 static const struct file_operations si470x_fops = {
1158 .owner = THIS_MODULE,
1159 .llseek = no_llseek,
1160 .read = si470x_fops_read,
1161 .poll = si470x_fops_poll,
1162 .ioctl = video_ioctl2,
1163 #ifdef CONFIG_COMPAT
1164 .compat_ioctl = v4l_compat_ioctl32,
1166 .open = si470x_fops_open,
1167 .release = si470x_fops_release,
1172 /**************************************************************************
1173 * Video4Linux Interface
1174 **************************************************************************/
1177 * si470x_v4l2_queryctrl - query control
1179 static struct v4l2_queryctrl si470x_v4l2_queryctrl[] = {
1181 .id = V4L2_CID_AUDIO_VOLUME,
1182 .type = V4L2_CTRL_TYPE_INTEGER,
1187 .default_value = 15,
1190 .id = V4L2_CID_AUDIO_MUTE,
1191 .type = V4L2_CTRL_TYPE_BOOLEAN,
1202 * si470x_vidioc_querycap - query device capabilities
1204 static int si470x_vidioc_querycap(struct file *file, void *priv,
1205 struct v4l2_capability *capability)
1207 strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver));
1208 strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
1209 sprintf(capability->bus_info, "USB");
1210 capability->version = DRIVER_KERNEL_VERSION;
1211 capability->capabilities = V4L2_CAP_HW_FREQ_SEEK |
1212 V4L2_CAP_TUNER | V4L2_CAP_RADIO;
1219 * si470x_vidioc_queryctrl - enumerate control items
1221 static int si470x_vidioc_queryctrl(struct file *file, void *priv,
1222 struct v4l2_queryctrl *qc)
1224 unsigned char i = 0;
1225 int retval = -EINVAL;
1227 /* abort if qc->id is below V4L2_CID_BASE */
1228 if (qc->id < V4L2_CID_BASE)
1231 /* search video control */
1232 for (i = 0; i < ARRAY_SIZE(si470x_v4l2_queryctrl); i++) {
1233 if (qc->id == si470x_v4l2_queryctrl[i].id) {
1234 memcpy(qc, &(si470x_v4l2_queryctrl[i]), sizeof(*qc));
1235 retval = 0; /* found */
1240 /* disable unsupported base controls */
1241 /* to satisfy kradio and such apps */
1242 if ((retval == -EINVAL) && (qc->id < V4L2_CID_LASTP1)) {
1243 qc->flags = V4L2_CTRL_FLAG_DISABLED;
1249 printk(KERN_WARNING DRIVER_NAME
1250 ": query controls failed with %d\n", retval);
1256 * si470x_vidioc_g_ctrl - get the value of a control
1258 static int si470x_vidioc_g_ctrl(struct file *file, void *priv,
1259 struct v4l2_control *ctrl)
1261 struct si470x_device *radio = video_drvdata(file);
1265 if (radio->disconnected) {
1271 case V4L2_CID_AUDIO_VOLUME:
1272 ctrl->value = radio->registers[SYSCONFIG2] &
1275 case V4L2_CID_AUDIO_MUTE:
1276 ctrl->value = ((radio->registers[POWERCFG] &
1277 POWERCFG_DMUTE) == 0) ? 1 : 0;
1285 printk(KERN_WARNING DRIVER_NAME
1286 ": get control failed with %d\n", retval);
1292 * si470x_vidioc_s_ctrl - set the value of a control
1294 static int si470x_vidioc_s_ctrl(struct file *file, void *priv,
1295 struct v4l2_control *ctrl)
1297 struct si470x_device *radio = video_drvdata(file);
1301 if (radio->disconnected) {
1307 case V4L2_CID_AUDIO_VOLUME:
1308 radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_VOLUME;
1309 radio->registers[SYSCONFIG2] |= ctrl->value;
1310 retval = si470x_set_register(radio, SYSCONFIG2);
1312 case V4L2_CID_AUDIO_MUTE:
1313 if (ctrl->value == 1)
1314 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
1316 radio->registers[POWERCFG] |= POWERCFG_DMUTE;
1317 retval = si470x_set_register(radio, POWERCFG);
1325 printk(KERN_WARNING DRIVER_NAME
1326 ": set control failed with %d\n", retval);
1332 * si470x_vidioc_g_audio - get audio attributes
1334 static int si470x_vidioc_g_audio(struct file *file, void *priv,
1335 struct v4l2_audio *audio)
1337 /* driver constants */
1339 strcpy(audio->name, "Radio");
1340 audio->capability = V4L2_AUDCAP_STEREO;
1348 * si470x_vidioc_g_tuner - get tuner attributes
1350 static int si470x_vidioc_g_tuner(struct file *file, void *priv,
1351 struct v4l2_tuner *tuner)
1353 struct si470x_device *radio = video_drvdata(file);
1357 if (radio->disconnected) {
1361 if (tuner->index != 0) {
1366 retval = si470x_get_register(radio, STATUSRSSI);
1370 /* driver constants */
1371 strcpy(tuner->name, "FM");
1372 tuner->type = V4L2_TUNER_RADIO;
1373 tuner->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
1376 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
1377 /* 0: 87.5 - 108 MHz (USA, Europe, default) */
1379 tuner->rangelow = 87.5 * FREQ_MUL;
1380 tuner->rangehigh = 108 * FREQ_MUL;
1382 /* 1: 76 - 108 MHz (Japan wide band) */
1384 tuner->rangelow = 76 * FREQ_MUL;
1385 tuner->rangehigh = 108 * FREQ_MUL;
1387 /* 2: 76 - 90 MHz (Japan) */
1389 tuner->rangelow = 76 * FREQ_MUL;
1390 tuner->rangehigh = 90 * FREQ_MUL;
1394 /* stereo indicator == stereo (instead of mono) */
1395 if ((radio->registers[STATUSRSSI] & STATUSRSSI_ST) == 1)
1396 tuner->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
1398 tuner->rxsubchans = V4L2_TUNER_SUB_MONO;
1400 /* mono/stereo selector */
1401 if ((radio->registers[POWERCFG] & POWERCFG_MONO) == 1)
1402 tuner->audmode = V4L2_TUNER_MODE_MONO;
1404 tuner->audmode = V4L2_TUNER_MODE_STEREO;
1406 /* min is worst, max is best; signal:0..0xffff; rssi: 0..0xff */
1407 tuner->signal = (radio->registers[STATUSRSSI] & STATUSRSSI_RSSI)
1410 /* automatic frequency control: -1: freq to low, 1 freq to high */
1411 /* AFCRL does only indicate that freq. differs, not if too low/high */
1412 tuner->afc = (radio->registers[STATUSRSSI] & STATUSRSSI_AFCRL) ? 1 : 0;
1416 printk(KERN_WARNING DRIVER_NAME
1417 ": get tuner failed with %d\n", retval);
1423 * si470x_vidioc_s_tuner - set tuner attributes
1425 static int si470x_vidioc_s_tuner(struct file *file, void *priv,
1426 struct v4l2_tuner *tuner)
1428 struct si470x_device *radio = video_drvdata(file);
1429 int retval = -EINVAL;
1432 if (radio->disconnected) {
1436 if (tuner->index != 0)
1439 /* mono/stereo selector */
1440 switch (tuner->audmode) {
1441 case V4L2_TUNER_MODE_MONO:
1442 radio->registers[POWERCFG] |= POWERCFG_MONO; /* force mono */
1444 case V4L2_TUNER_MODE_STEREO:
1445 radio->registers[POWERCFG] &= ~POWERCFG_MONO; /* try stereo */
1451 retval = si470x_set_register(radio, POWERCFG);
1455 printk(KERN_WARNING DRIVER_NAME
1456 ": set tuner failed with %d\n", retval);
1462 * si470x_vidioc_g_frequency - get tuner or modulator radio frequency
1464 static int si470x_vidioc_g_frequency(struct file *file, void *priv,
1465 struct v4l2_frequency *freq)
1467 struct si470x_device *radio = video_drvdata(file);
1471 if (radio->disconnected) {
1475 if (freq->tuner != 0) {
1480 freq->type = V4L2_TUNER_RADIO;
1481 retval = si470x_get_freq(radio, &freq->frequency);
1485 printk(KERN_WARNING DRIVER_NAME
1486 ": get frequency failed with %d\n", retval);
1492 * si470x_vidioc_s_frequency - set tuner or modulator radio frequency
1494 static int si470x_vidioc_s_frequency(struct file *file, void *priv,
1495 struct v4l2_frequency *freq)
1497 struct si470x_device *radio = video_drvdata(file);
1501 if (radio->disconnected) {
1505 if (freq->tuner != 0) {
1510 retval = si470x_set_freq(radio, freq->frequency);
1514 printk(KERN_WARNING DRIVER_NAME
1515 ": set frequency failed with %d\n", retval);
1521 * si470x_vidioc_s_hw_freq_seek - set hardware frequency seek
1523 static int si470x_vidioc_s_hw_freq_seek(struct file *file, void *priv,
1524 struct v4l2_hw_freq_seek *seek)
1526 struct si470x_device *radio = video_drvdata(file);
1530 if (radio->disconnected) {
1534 if (seek->tuner != 0) {
1539 retval = si470x_set_seek(radio, seek->wrap_around, seek->seek_upward);
1543 printk(KERN_WARNING DRIVER_NAME
1544 ": set hardware frequency seek failed with %d\n",
1551 * si470x_ioctl_ops - video device ioctl operations
1553 static const struct v4l2_ioctl_ops si470x_ioctl_ops = {
1554 .vidioc_querycap = si470x_vidioc_querycap,
1555 .vidioc_queryctrl = si470x_vidioc_queryctrl,
1556 .vidioc_g_ctrl = si470x_vidioc_g_ctrl,
1557 .vidioc_s_ctrl = si470x_vidioc_s_ctrl,
1558 .vidioc_g_audio = si470x_vidioc_g_audio,
1559 .vidioc_g_tuner = si470x_vidioc_g_tuner,
1560 .vidioc_s_tuner = si470x_vidioc_s_tuner,
1561 .vidioc_g_frequency = si470x_vidioc_g_frequency,
1562 .vidioc_s_frequency = si470x_vidioc_s_frequency,
1563 .vidioc_s_hw_freq_seek = si470x_vidioc_s_hw_freq_seek,
1568 * si470x_viddev_template - video device interface
1570 static struct video_device si470x_viddev_template = {
1571 .fops = &si470x_fops,
1572 .name = DRIVER_NAME,
1573 .release = video_device_release,
1574 .ioctl_ops = &si470x_ioctl_ops,
1579 /**************************************************************************
1581 **************************************************************************/
1584 * si470x_usb_driver_probe - probe for the device
1586 static int si470x_usb_driver_probe(struct usb_interface *intf,
1587 const struct usb_device_id *id)
1589 struct si470x_device *radio;
1592 /* private data allocation and initialization */
1593 radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL);
1599 radio->disconnected = 0;
1600 radio->usbdev = interface_to_usbdev(intf);
1602 mutex_init(&radio->disconnect_lock);
1603 mutex_init(&radio->lock);
1605 /* video device allocation and initialization */
1606 radio->videodev = video_device_alloc();
1607 if (!radio->videodev) {
1611 memcpy(radio->videodev, &si470x_viddev_template,
1612 sizeof(si470x_viddev_template));
1613 video_set_drvdata(radio->videodev, radio);
1615 /* show some infos about the specific device */
1616 if (si470x_get_all_registers(radio) < 0) {
1620 printk(KERN_INFO DRIVER_NAME ": DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",
1621 radio->registers[DEVICEID], radio->registers[CHIPID]);
1623 /* check if firmware is current */
1624 if ((radio->registers[CHIPID] & CHIPID_FIRMWARE)
1625 < RADIO_SW_VERSION_CURRENT) {
1626 printk(KERN_WARNING DRIVER_NAME
1627 ": This driver is known to work with "
1628 "firmware version %hu,\n", RADIO_SW_VERSION_CURRENT);
1629 printk(KERN_WARNING DRIVER_NAME
1630 ": but the device has firmware version %hu.\n",
1631 radio->registers[CHIPID] & CHIPID_FIRMWARE);
1632 printk(KERN_WARNING DRIVER_NAME
1633 ": If you have some trouble using this driver,\n");
1634 printk(KERN_WARNING DRIVER_NAME
1635 ": please report to V4L ML at "
1636 "video4linux-list@redhat.com\n");
1639 /* set initial frequency */
1640 si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
1642 /* rds buffer allocation */
1643 radio->buf_size = rds_buf * 3;
1644 radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
1645 if (!radio->buffer) {
1650 /* rds buffer configuration */
1651 radio->wr_index = 0;
1652 radio->rd_index = 0;
1653 init_waitqueue_head(&radio->read_queue);
1655 /* prepare rds work function */
1656 INIT_DELAYED_WORK(&radio->work, si470x_work);
1658 /* register video device */
1659 retval = video_register_device(radio->videodev, VFL_TYPE_RADIO, radio_nr);
1661 printk(KERN_WARNING DRIVER_NAME
1662 ": Could not register video device\n");
1665 usb_set_intfdata(intf, radio);
1669 video_device_release(radio->videodev);
1670 kfree(radio->buffer);
1679 * si470x_usb_driver_suspend - suspend the device
1681 static int si470x_usb_driver_suspend(struct usb_interface *intf,
1682 pm_message_t message)
1684 struct si470x_device *radio = usb_get_intfdata(intf);
1686 printk(KERN_INFO DRIVER_NAME ": suspending now...\n");
1688 cancel_delayed_work_sync(&radio->work);
1695 * si470x_usb_driver_resume - resume the device
1697 static int si470x_usb_driver_resume(struct usb_interface *intf)
1699 struct si470x_device *radio = usb_get_intfdata(intf);
1701 printk(KERN_INFO DRIVER_NAME ": resuming now...\n");
1703 mutex_lock(&radio->lock);
1704 if (radio->users && radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS)
1705 schedule_delayed_work(&radio->work,
1706 msecs_to_jiffies(rds_poll_time));
1707 mutex_unlock(&radio->lock);
1714 * si470x_usb_driver_disconnect - disconnect the device
1716 static void si470x_usb_driver_disconnect(struct usb_interface *intf)
1718 struct si470x_device *radio = usb_get_intfdata(intf);
1720 mutex_lock(&radio->disconnect_lock);
1721 radio->disconnected = 1;
1722 cancel_delayed_work_sync(&radio->work);
1723 usb_set_intfdata(intf, NULL);
1724 if (radio->users == 0) {
1725 video_unregister_device(radio->videodev);
1726 kfree(radio->buffer);
1729 mutex_unlock(&radio->disconnect_lock);
1734 * si470x_usb_driver - usb driver interface
1736 static struct usb_driver si470x_usb_driver = {
1737 .name = DRIVER_NAME,
1738 .probe = si470x_usb_driver_probe,
1739 .disconnect = si470x_usb_driver_disconnect,
1740 .suspend = si470x_usb_driver_suspend,
1741 .resume = si470x_usb_driver_resume,
1742 .id_table = si470x_usb_driver_id_table,
1743 .supports_autosuspend = 1,
1748 /**************************************************************************
1750 **************************************************************************/
1753 * si470x_module_init - module init
1755 static int __init si470x_module_init(void)
1757 printk(KERN_INFO DRIVER_DESC ", Version " DRIVER_VERSION "\n");
1758 return usb_register(&si470x_usb_driver);
1763 * si470x_module_exit - module exit
1765 static void __exit si470x_module_exit(void)
1767 usb_deregister(&si470x_usb_driver);
1771 module_init(si470x_module_init);
1772 module_exit(si470x_module_exit);
1774 MODULE_LICENSE("GPL");
1775 MODULE_AUTHOR(DRIVER_AUTHOR);
1776 MODULE_DESCRIPTION(DRIVER_DESC);
1777 MODULE_VERSION(DRIVER_VERSION);