iwlwifi: introduce host commands callbacks
[linux/fpc-iii.git] / drivers / media / radio / radio-si470x.c
blob649f14d2c01340e4bdea2bee6d4b1910ee39e496
1 /*
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
27 * History:
28 * 2008-01-12 Tobias Lorenz <tobias.lorenz@gmx.net>
29 * Version 1.0.0
30 * - First working version
31 * 2008-01-13 Tobias Lorenz <tobias.lorenz@gmx.net>
32 * Version 1.0.1
33 * - Improved error handling, every function now returns errno
34 * - Improved multi user access (start/mute/stop)
35 * - Channel doesn't get lost anymore after start/mute/stop
36 * - RDS support added (polling mode via interrupt EP 1)
37 * - marked default module parameters with *value*
38 * - switched from bit structs to bit masks
39 * - header file cleaned and integrated
40 * 2008-01-14 Tobias Lorenz <tobias.lorenz@gmx.net>
41 * Version 1.0.2
42 * - hex values are now lower case
43 * - commented USB ID for ADS/Tech moved on todo list
44 * - blacklisted si470x in hid-quirks.c
45 * - rds buffer handling functions integrated into *_work, *_read
46 * - rds_command in si470x_poll exchanged against simple retval
47 * - check for firmware version 15
48 * - code order and prototypes still remain the same
49 * - spacing and bottom of band codes remain the same
50 * 2008-01-16 Tobias Lorenz <tobias.lorenz@gmx.net>
51 * Version 1.0.3
52 * - code reordered to avoid function prototypes
53 * - switch/case defaults are now more user-friendly
54 * - unified comment style
55 * - applied all checkpatch.pl v1.12 suggestions
56 * except the warning about the too long lines with bit comments
57 * - renamed FMRADIO to RADIO to cut line length (checkpatch.pl)
58 * 2008-01-22 Tobias Lorenz <tobias.lorenz@gmx.net>
59 * Version 1.0.4
60 * - avoid poss. locking when doing copy_to_user which may sleep
61 * - RDS is automatically activated on read now
62 * - code cleaned of unnecessary rds_commands
63 * - USB Vendor/Product ID for ADS/Tech FM Radio Receiver verified
64 * (thanks to Guillaume RAMOUSSE)
65 * 2008-01-27 Tobias Lorenz <tobias.lorenz@gmx.net>
66 * Version 1.0.5
67 * - number of seek_retries changed to tune_timeout
68 * - fixed problem with incomplete tune operations by own buffers
69 * - optimization of variables and printf types
70 * - improved error logging
71 * 2008-01-31 Tobias Lorenz <tobias.lorenz@gmx.net>
72 * Oliver Neukum <oliver@neukum.org>
73 * Version 1.0.6
74 * - fixed coverity checker warnings in *_usb_driver_disconnect
75 * - probe()/open() race by correct ordering in probe()
76 * - DMA coherency rules by separate allocation of all buffers
77 * - use of endianness macros
78 * - abuse of spinlock, replaced by mutex
79 * - racy handling of timer in disconnect,
80 * replaced by delayed_work
81 * - racy interruptible_sleep_on(),
82 * replaced with wait_event_interruptible()
83 * - handle signals in read()
84 * 2008-02-08 Tobias Lorenz <tobias.lorenz@gmx.net>
85 * Oliver Neukum <oliver@neukum.org>
86 * Version 1.0.7
87 * - usb autosuspend support
89 * ToDo:
90 * - add seeking support
91 * - add firmware download/update support
92 * - RDS support: interrupt mode, instead of polling
93 * - add LED status output (check if that's not already done in firmware)
97 /* driver definitions */
98 #define DRIVER_AUTHOR "Tobias Lorenz <tobias.lorenz@gmx.net>"
99 #define DRIVER_NAME "radio-si470x"
100 #define DRIVER_KERNEL_VERSION KERNEL_VERSION(1, 0, 6)
101 #define DRIVER_CARD "Silicon Labs Si470x FM Radio Receiver"
102 #define DRIVER_DESC "USB radio driver for Si470x FM Radio Receivers"
103 #define DRIVER_VERSION "1.0.6"
106 /* kernel includes */
107 #include <linux/kernel.h>
108 #include <linux/module.h>
109 #include <linux/init.h>
110 #include <linux/slab.h>
111 #include <linux/input.h>
112 #include <linux/usb.h>
113 #include <linux/hid.h>
114 #include <linux/version.h>
115 #include <linux/videodev2.h>
116 #include <linux/mutex.h>
117 #include <media/v4l2-common.h>
118 #include <media/rds.h>
119 #include <asm/unaligned.h>
122 /* USB Device ID List */
123 static struct usb_device_id si470x_usb_driver_id_table[] = {
124 /* Silicon Labs USB FM Radio Reference Design */
125 { USB_DEVICE_AND_INTERFACE_INFO(0x10c4, 0x818a, USB_CLASS_HID, 0, 0) },
126 /* ADS/Tech FM Radio Receiver (formerly Instant FM Music) */
127 { USB_DEVICE_AND_INTERFACE_INFO(0x06e1, 0xa155, USB_CLASS_HID, 0, 0) },
128 /* Terminating entry */
131 MODULE_DEVICE_TABLE(usb, si470x_usb_driver_id_table);
135 /**************************************************************************
136 * Module Parameters
137 **************************************************************************/
139 /* Radio Nr */
140 static int radio_nr = -1;
141 module_param(radio_nr, int, 0);
142 MODULE_PARM_DESC(radio_nr, "Radio Nr");
144 /* Spacing (kHz) */
145 /* 0: 200 kHz (USA, Australia) */
146 /* 1: 100 kHz (Europe, Japan) */
147 /* 2: 50 kHz */
148 static unsigned short space = 2;
149 module_param(space, ushort, 0);
150 MODULE_PARM_DESC(radio_nr, "Spacing: 0=200kHz 1=100kHz *2=50kHz*");
152 /* Bottom of Band (MHz) */
153 /* 0: 87.5 - 108 MHz (USA, Europe)*/
154 /* 1: 76 - 108 MHz (Japan wide band) */
155 /* 2: 76 - 90 MHz (Japan) */
156 static unsigned short band = 1;
157 module_param(band, ushort, 0);
158 MODULE_PARM_DESC(radio_nr, "Band: 0=87.5..108MHz *1=76..108MHz* 2=76..90MHz");
160 /* De-emphasis */
161 /* 0: 75 us (USA) */
162 /* 1: 50 us (Europe, Australia, Japan) */
163 static unsigned short de = 1;
164 module_param(de, ushort, 0);
165 MODULE_PARM_DESC(radio_nr, "De-emphasis: 0=75us *1=50us*");
167 /* USB timeout */
168 static unsigned int usb_timeout = 500;
169 module_param(usb_timeout, uint, 0);
170 MODULE_PARM_DESC(usb_timeout, "USB timeout (ms): *500*");
172 /* Tune timeout */
173 static unsigned int tune_timeout = 3000;
174 module_param(tune_timeout, uint, 0);
175 MODULE_PARM_DESC(tune_timeout, "Tune timeout: *3000*");
177 /* RDS buffer blocks */
178 static unsigned int rds_buf = 100;
179 module_param(rds_buf, uint, 0);
180 MODULE_PARM_DESC(rds_buf, "RDS buffer entries: *100*");
182 /* RDS maximum block errors */
183 static unsigned short max_rds_errors = 1;
184 /* 0 means 0 errors requiring correction */
185 /* 1 means 1-2 errors requiring correction (used by original USBRadio.exe) */
186 /* 2 means 3-5 errors requiring correction */
187 /* 3 means 6+ errors or errors in checkword, correction not possible */
188 module_param(max_rds_errors, ushort, 0);
189 MODULE_PARM_DESC(max_rds_errors, "RDS maximum block errors: *1*");
191 /* RDS poll frequency */
192 static unsigned int rds_poll_time = 40;
193 /* 40 is used by the original USBRadio.exe */
194 /* 50 is used by radio-cadet */
195 /* 75 should be okay */
196 /* 80 is the usual RDS receive interval */
197 module_param(rds_poll_time, uint, 0);
198 MODULE_PARM_DESC(rds_poll_time, "RDS poll time (ms): *40*");
202 /**************************************************************************
203 * Register Definitions
204 **************************************************************************/
205 #define RADIO_REGISTER_SIZE 2 /* 16 register bit width */
206 #define RADIO_REGISTER_NUM 16 /* DEVICEID ... RDSD */
207 #define RDS_REGISTER_NUM 6 /* STATUSRSSI ... RDSD */
209 #define DEVICEID 0 /* Device ID */
210 #define DEVICEID_PN 0xf000 /* bits 15..12: Part Number */
211 #define DEVICEID_MFGID 0x0fff /* bits 11..00: Manufacturer ID */
213 #define CHIPID 1 /* Chip ID */
214 #define CHIPID_REV 0xfc00 /* bits 15..10: Chip Version */
215 #define CHIPID_DEV 0x0200 /* bits 09..09: Device */
216 #define CHIPID_FIRMWARE 0x01ff /* bits 08..00: Firmware Version */
218 #define POWERCFG 2 /* Power Configuration */
219 #define POWERCFG_DSMUTE 0x8000 /* bits 15..15: Softmute Disable */
220 #define POWERCFG_DMUTE 0x4000 /* bits 14..14: Mute Disable */
221 #define POWERCFG_MONO 0x2000 /* bits 13..13: Mono Select */
222 #define POWERCFG_RDSM 0x0800 /* bits 11..11: RDS Mode (Si4701 only) */
223 #define POWERCFG_SKMODE 0x0400 /* bits 10..10: Seek Mode */
224 #define POWERCFG_SEEKUP 0x0200 /* bits 09..09: Seek Direction */
225 #define POWERCFG_SEEK 0x0100 /* bits 08..08: Seek */
226 #define POWERCFG_DISABLE 0x0040 /* bits 06..06: Powerup Disable */
227 #define POWERCFG_ENABLE 0x0001 /* bits 00..00: Powerup Enable */
229 #define CHANNEL 3 /* Channel */
230 #define CHANNEL_TUNE 0x8000 /* bits 15..15: Tune */
231 #define CHANNEL_CHAN 0x03ff /* bits 09..00: Channel Select */
233 #define SYSCONFIG1 4 /* System Configuration 1 */
234 #define SYSCONFIG1_RDSIEN 0x8000 /* bits 15..15: RDS Interrupt Enable (Si4701 only) */
235 #define SYSCONFIG1_STCIEN 0x4000 /* bits 14..14: Seek/Tune Complete Interrupt Enable */
236 #define SYSCONFIG1_RDS 0x1000 /* bits 12..12: RDS Enable (Si4701 only) */
237 #define SYSCONFIG1_DE 0x0800 /* bits 11..11: De-emphasis (0=75us 1=50us) */
238 #define SYSCONFIG1_AGCD 0x0400 /* bits 10..10: AGC Disable */
239 #define SYSCONFIG1_BLNDADJ 0x00c0 /* bits 07..06: Stereo/Mono Blend Level Adjustment */
240 #define SYSCONFIG1_GPIO3 0x0030 /* bits 05..04: General Purpose I/O 3 */
241 #define SYSCONFIG1_GPIO2 0x000c /* bits 03..02: General Purpose I/O 2 */
242 #define SYSCONFIG1_GPIO1 0x0003 /* bits 01..00: General Purpose I/O 1 */
244 #define SYSCONFIG2 5 /* System Configuration 2 */
245 #define SYSCONFIG2_SEEKTH 0xff00 /* bits 15..08: RSSI Seek Threshold */
246 #define SYSCONFIG2_BAND 0x0080 /* bits 07..06: Band Select */
247 #define SYSCONFIG2_SPACE 0x0030 /* bits 05..04: Channel Spacing */
248 #define SYSCONFIG2_VOLUME 0x000f /* bits 03..00: Volume */
250 #define SYSCONFIG3 6 /* System Configuration 3 */
251 #define SYSCONFIG3_SMUTER 0xc000 /* bits 15..14: Softmute Attack/Recover Rate */
252 #define SYSCONFIG3_SMUTEA 0x3000 /* bits 13..12: Softmute Attenuation */
253 #define SYSCONFIG3_SKSNR 0x00f0 /* bits 07..04: Seek SNR Threshold */
254 #define SYSCONFIG3_SKCNT 0x000f /* bits 03..00: Seek FM Impulse Detection Threshold */
256 #define TEST1 7 /* Test 1 */
257 #define TEST1_AHIZEN 0x4000 /* bits 14..14: Audio High-Z Enable */
259 #define TEST2 8 /* Test 2 */
260 /* TEST2 only contains reserved bits */
262 #define BOOTCONFIG 9 /* Boot Configuration */
263 /* BOOTCONFIG only contains reserved bits */
265 #define STATUSRSSI 10 /* Status RSSI */
266 #define STATUSRSSI_RDSR 0x8000 /* bits 15..15: RDS Ready (Si4701 only) */
267 #define STATUSRSSI_STC 0x4000 /* bits 14..14: Seek/Tune Complete */
268 #define STATUSRSSI_SF 0x2000 /* bits 13..13: Seek Fail/Band Limit */
269 #define STATUSRSSI_AFCRL 0x1000 /* bits 12..12: AFC Rail */
270 #define STATUSRSSI_RDSS 0x0800 /* bits 11..11: RDS Synchronized (Si4701 only) */
271 #define STATUSRSSI_BLERA 0x0600 /* bits 10..09: RDS Block A Errors (Si4701 only) */
272 #define STATUSRSSI_ST 0x0100 /* bits 08..08: Stereo Indicator */
273 #define STATUSRSSI_RSSI 0x00ff /* bits 07..00: RSSI (Received Signal Strength Indicator) */
275 #define READCHAN 11 /* Read Channel */
276 #define READCHAN_BLERB 0xc000 /* bits 15..14: RDS Block D Errors (Si4701 only) */
277 #define READCHAN_BLERC 0x3000 /* bits 13..12: RDS Block C Errors (Si4701 only) */
278 #define READCHAN_BLERD 0x0c00 /* bits 11..10: RDS Block B Errors (Si4701 only) */
279 #define READCHAN_READCHAN 0x03ff /* bits 09..00: Read Channel */
281 #define RDSA 12 /* RDSA */
282 #define RDSA_RDSA 0xffff /* bits 15..00: RDS Block A Data (Si4701 only) */
284 #define RDSB 13 /* RDSB */
285 #define RDSB_RDSB 0xffff /* bits 15..00: RDS Block B Data (Si4701 only) */
287 #define RDSC 14 /* RDSC */
288 #define RDSC_RDSC 0xffff /* bits 15..00: RDS Block C Data (Si4701 only) */
290 #define RDSD 15 /* RDSD */
291 #define RDSD_RDSD 0xffff /* bits 15..00: RDS Block D Data (Si4701 only) */
295 /**************************************************************************
296 * USB HID Reports
297 **************************************************************************/
299 /* Reports 1-16 give direct read/write access to the 16 Si470x registers */
300 /* with the (REPORT_ID - 1) corresponding to the register address across USB */
301 /* endpoint 0 using GET_REPORT and SET_REPORT */
302 #define REGISTER_REPORT_SIZE (RADIO_REGISTER_SIZE + 1)
303 #define REGISTER_REPORT(reg) ((reg) + 1)
305 /* Report 17 gives direct read/write access to the entire Si470x register */
306 /* map across endpoint 0 using GET_REPORT and SET_REPORT */
307 #define ENTIRE_REPORT_SIZE (RADIO_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
308 #define ENTIRE_REPORT 17
310 /* Report 18 is used to send the lowest 6 Si470x registers up the HID */
311 /* interrupt endpoint 1 to Windows every 20 milliseconds for status */
312 #define RDS_REPORT_SIZE (RDS_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
313 #define RDS_REPORT 18
315 /* Report 19: LED state */
316 #define LED_REPORT_SIZE 3
317 #define LED_REPORT 19
319 /* Report 19: stream */
320 #define STREAM_REPORT_SIZE 3
321 #define STREAM_REPORT 19
323 /* Report 20: scratch */
324 #define SCRATCH_PAGE_SIZE 63
325 #define SCRATCH_REPORT_SIZE (SCRATCH_PAGE_SIZE + 1)
326 #define SCRATCH_REPORT 20
328 /* Reports 19-22: flash upgrade of the C8051F321 */
329 #define WRITE_REPORT 19
330 #define FLASH_REPORT 20
331 #define CRC_REPORT 21
332 #define RESPONSE_REPORT 22
334 /* Report 23: currently unused, but can accept 60 byte reports on the HID */
335 /* interrupt out endpoint 2 every 1 millisecond */
336 #define UNUSED_REPORT 23
340 /**************************************************************************
341 * Software/Hardware Versions
342 **************************************************************************/
343 #define RADIO_SW_VERSION_NOT_BOOTLOADABLE 6
344 #define RADIO_SW_VERSION 7
345 #define RADIO_SW_VERSION_CURRENT 15
346 #define RADIO_HW_VERSION 1
348 #define SCRATCH_PAGE_SW_VERSION 1
349 #define SCRATCH_PAGE_HW_VERSION 2
353 /**************************************************************************
354 * LED State Definitions
355 **************************************************************************/
356 #define LED_COMMAND 0x35
358 #define NO_CHANGE_LED 0x00
359 #define ALL_COLOR_LED 0x01 /* streaming state */
360 #define BLINK_GREEN_LED 0x02 /* connect state */
361 #define BLINK_RED_LED 0x04
362 #define BLINK_ORANGE_LED 0x10 /* disconnect state */
363 #define SOLID_GREEN_LED 0x20 /* tuning/seeking state */
364 #define SOLID_RED_LED 0x40 /* bootload state */
365 #define SOLID_ORANGE_LED 0x80
369 /**************************************************************************
370 * Stream State Definitions
371 **************************************************************************/
372 #define STREAM_COMMAND 0x36
373 #define STREAM_VIDPID 0x00
374 #define STREAM_AUDIO 0xff
378 /**************************************************************************
379 * Bootloader / Flash Commands
380 **************************************************************************/
382 /* unique id sent to bootloader and required to put into a bootload state */
383 #define UNIQUE_BL_ID 0x34
385 /* mask for the flash data */
386 #define FLASH_DATA_MASK 0x55
388 /* bootloader commands */
389 #define GET_SW_VERSION_COMMAND 0x00
390 #define SET_PAGE_COMMAND 0x01
391 #define ERASE_PAGE_COMMAND 0x02
392 #define WRITE_PAGE_COMMAND 0x03
393 #define CRC_ON_PAGE_COMMAND 0x04
394 #define READ_FLASH_BYTE_COMMAND 0x05
395 #define RESET_DEVICE_COMMAND 0x06
396 #define GET_HW_VERSION_COMMAND 0x07
397 #define BLANK 0xff
399 /* bootloader command responses */
400 #define COMMAND_OK 0x01
401 #define COMMAND_FAILED 0x02
402 #define COMMAND_PENDING 0x03
404 /* buffer sizes */
405 #define COMMAND_BUFFER_SIZE 4
406 #define RESPONSE_BUFFER_SIZE 2
407 #define FLASH_BUFFER_SIZE 64
408 #define CRC_BUFFER_SIZE 3
412 /**************************************************************************
413 * General Driver Definitions
414 **************************************************************************/
417 * si470x_device - private data
419 struct si470x_device {
420 /* reference to USB and video device */
421 struct usb_device *usbdev;
422 struct usb_interface *intf;
423 struct video_device *videodev;
425 /* driver management */
426 unsigned int users;
428 /* Silabs internal registers (0..15) */
429 unsigned short registers[RADIO_REGISTER_NUM];
431 /* RDS receive buffer */
432 struct delayed_work work;
433 wait_queue_head_t read_queue;
434 struct mutex lock; /* buffer locking */
435 unsigned char *buffer; /* size is always multiple of three */
436 unsigned int buf_size;
437 unsigned int rd_index;
438 unsigned int wr_index;
443 * The frequency is set in units of 62.5 Hz when using V4L2_TUNER_CAP_LOW,
444 * 62.5 kHz otherwise.
445 * The tuner is able to have a channel spacing of 50, 100 or 200 kHz.
446 * tuner->capability is therefore set to V4L2_TUNER_CAP_LOW
447 * The FREQ_MUL is then: 1 MHz / 62.5 Hz = 16000
449 #define FREQ_MUL (1000000 / 62.5)
453 /**************************************************************************
454 * General Driver Functions
455 **************************************************************************/
458 * si470x_get_report - receive a HID report
460 static int si470x_get_report(struct si470x_device *radio, void *buf, int size)
462 unsigned char *report = (unsigned char *) buf;
463 int retval;
465 retval = usb_control_msg(radio->usbdev,
466 usb_rcvctrlpipe(radio->usbdev, 0),
467 HID_REQ_GET_REPORT,
468 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
469 report[0], 2,
470 buf, size, usb_timeout);
471 if (retval < 0)
472 printk(KERN_WARNING DRIVER_NAME
473 ": si470x_get_report: usb_control_msg returned %d\n",
474 retval);
476 return retval;
481 * si470x_set_report - send a HID report
483 static int si470x_set_report(struct si470x_device *radio, void *buf, int size)
485 unsigned char *report = (unsigned char *) buf;
486 int retval;
488 retval = usb_control_msg(radio->usbdev,
489 usb_sndctrlpipe(radio->usbdev, 0),
490 HID_REQ_SET_REPORT,
491 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
492 report[0], 2,
493 buf, size, usb_timeout);
494 if (retval < 0)
495 printk(KERN_WARNING DRIVER_NAME
496 ": si470x_set_report: usb_control_msg returned %d\n",
497 retval);
499 return retval;
504 * si470x_get_register - read register
506 static int si470x_get_register(struct si470x_device *radio, int regnr)
508 unsigned char buf[REGISTER_REPORT_SIZE];
509 int retval;
511 buf[0] = REGISTER_REPORT(regnr);
513 retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
515 if (retval >= 0)
516 radio->registers[regnr] = be16_to_cpu(get_unaligned(
517 (unsigned short *) &buf[1]));
519 return (retval < 0) ? -EINVAL : 0;
524 * si470x_set_register - write register
526 static int si470x_set_register(struct si470x_device *radio, int regnr)
528 unsigned char buf[REGISTER_REPORT_SIZE];
529 int retval;
531 buf[0] = REGISTER_REPORT(regnr);
532 put_unaligned(cpu_to_be16(radio->registers[regnr]),
533 (unsigned short *) &buf[1]);
535 retval = si470x_set_report(radio, (void *) &buf, sizeof(buf));
537 return (retval < 0) ? -EINVAL : 0;
542 * si470x_get_all_registers - read entire registers
544 static int si470x_get_all_registers(struct si470x_device *radio)
546 unsigned char buf[ENTIRE_REPORT_SIZE];
547 int retval;
548 unsigned char regnr;
550 buf[0] = ENTIRE_REPORT;
552 retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
554 if (retval >= 0)
555 for (regnr = 0; regnr < RADIO_REGISTER_NUM; regnr++)
556 radio->registers[regnr] = be16_to_cpu(get_unaligned(
557 (unsigned short *)
558 &buf[regnr * RADIO_REGISTER_SIZE + 1]));
560 return (retval < 0) ? -EINVAL : 0;
565 * si470x_get_rds_registers - read rds registers
567 static int si470x_get_rds_registers(struct si470x_device *radio)
569 unsigned char buf[RDS_REPORT_SIZE];
570 int retval;
571 int size;
572 unsigned char regnr;
574 buf[0] = RDS_REPORT;
576 retval = usb_interrupt_msg(radio->usbdev,
577 usb_rcvintpipe(radio->usbdev, 1),
578 (void *) &buf, sizeof(buf), &size, usb_timeout);
579 if (size != sizeof(buf))
580 printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_register: "
581 "return size differs: %d != %zu\n", size, sizeof(buf));
582 if (retval < 0)
583 printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: "
584 "usb_interrupt_msg returned %d\n", retval);
586 if (retval >= 0)
587 for (regnr = 0; regnr < RDS_REGISTER_NUM; regnr++)
588 radio->registers[STATUSRSSI + regnr] =
589 be16_to_cpu(get_unaligned((unsigned short *)
590 &buf[regnr * RADIO_REGISTER_SIZE + 1]));
592 return (retval < 0) ? -EINVAL : 0;
597 * si470x_set_chan - set the channel
599 static int si470x_set_chan(struct si470x_device *radio, unsigned short chan)
601 int retval;
602 unsigned long timeout;
603 bool timed_out = 0;
605 /* start tuning */
606 radio->registers[CHANNEL] &= ~CHANNEL_CHAN;
607 radio->registers[CHANNEL] |= CHANNEL_TUNE | chan;
608 retval = si470x_set_register(radio, CHANNEL);
609 if (retval < 0)
610 return retval;
612 /* wait till seek operation has completed */
613 timeout = jiffies + msecs_to_jiffies(tune_timeout);
614 do {
615 retval = si470x_get_register(radio, STATUSRSSI);
616 if (retval < 0)
617 return retval;
618 timed_out = time_after(jiffies, timeout);
619 } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
620 (!timed_out));
621 if (timed_out)
622 printk(KERN_WARNING DRIVER_NAME
623 ": seek does not finish after %u ms\n", tune_timeout);
625 /* stop tuning */
626 radio->registers[CHANNEL] &= ~CHANNEL_TUNE;
627 return si470x_set_register(radio, CHANNEL);
632 * si470x_get_freq - get the frequency
634 static unsigned int si470x_get_freq(struct si470x_device *radio)
636 unsigned int spacing, band_bottom, freq;
637 unsigned short chan;
638 int retval;
640 /* Spacing (kHz) */
641 switch (space) {
642 /* 0: 200 kHz (USA, Australia) */
643 case 0 : spacing = 0.200 * FREQ_MUL; break;
644 /* 1: 100 kHz (Europe, Japan) */
645 case 1 : spacing = 0.100 * FREQ_MUL; break;
646 /* 2: 50 kHz */
647 default: spacing = 0.050 * FREQ_MUL; break;
650 /* Bottom of Band (MHz) */
651 switch (band) {
652 /* 0: 87.5 - 108 MHz (USA, Europe) */
653 case 0 : band_bottom = 87.5 * FREQ_MUL; break;
654 /* 1: 76 - 108 MHz (Japan wide band) */
655 default: band_bottom = 76 * FREQ_MUL; break;
656 /* 2: 76 - 90 MHz (Japan) */
657 case 2 : band_bottom = 76 * FREQ_MUL; break;
660 /* read channel */
661 retval = si470x_get_register(radio, READCHAN);
662 if (retval < 0)
663 return retval;
664 chan = radio->registers[READCHAN] & READCHAN_READCHAN;
666 /* Frequency (MHz) = Spacing (kHz) x Channel + Bottom of Band (MHz) */
667 freq = chan * spacing + band_bottom;
669 return freq;
674 * si470x_set_freq - set the frequency
676 static int si470x_set_freq(struct si470x_device *radio, unsigned int freq)
678 unsigned int spacing, band_bottom;
679 unsigned short chan;
681 /* Spacing (kHz) */
682 switch (space) {
683 /* 0: 200 kHz (USA, Australia) */
684 case 0 : spacing = 0.200 * FREQ_MUL; break;
685 /* 1: 100 kHz (Europe, Japan) */
686 case 1 : spacing = 0.100 * FREQ_MUL; break;
687 /* 2: 50 kHz */
688 default: spacing = 0.050 * FREQ_MUL; break;
691 /* Bottom of Band (MHz) */
692 switch (band) {
693 /* 0: 87.5 - 108 MHz (USA, Europe) */
694 case 0 : band_bottom = 87.5 * FREQ_MUL; break;
695 /* 1: 76 - 108 MHz (Japan wide band) */
696 default: band_bottom = 76 * FREQ_MUL; break;
697 /* 2: 76 - 90 MHz (Japan) */
698 case 2 : band_bottom = 76 * FREQ_MUL; break;
701 /* Chan = [ Freq (Mhz) - Bottom of Band (MHz) ] / Spacing (kHz) */
702 chan = (freq - band_bottom) / spacing;
704 return si470x_set_chan(radio, chan);
709 * si470x_start - switch on radio
711 static int si470x_start(struct si470x_device *radio)
713 int retval;
715 /* powercfg */
716 radio->registers[POWERCFG] =
717 POWERCFG_DMUTE | POWERCFG_ENABLE | POWERCFG_RDSM;
718 retval = si470x_set_register(radio, POWERCFG);
719 if (retval < 0)
720 return retval;
722 /* sysconfig 1 */
723 radio->registers[SYSCONFIG1] = SYSCONFIG1_DE;
724 retval = si470x_set_register(radio, SYSCONFIG1);
725 if (retval < 0)
726 return retval;
728 /* sysconfig 2 */
729 radio->registers[SYSCONFIG2] =
730 (0x3f << 8) | /* SEEKTH */
731 (band << 6) | /* BAND */
732 (space << 4) | /* SPACE */
733 15; /* VOLUME (max) */
734 retval = si470x_set_register(radio, SYSCONFIG2);
735 if (retval < 0)
736 return retval;
738 /* reset last channel */
739 return si470x_set_chan(radio,
740 radio->registers[CHANNEL] & CHANNEL_CHAN);
745 * si470x_stop - switch off radio
747 static int si470x_stop(struct si470x_device *radio)
749 int retval;
751 /* sysconfig 1 */
752 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
753 retval = si470x_set_register(radio, SYSCONFIG1);
754 if (retval < 0)
755 return retval;
757 /* powercfg */
758 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
759 /* POWERCFG_ENABLE has to automatically go low */
760 radio->registers[POWERCFG] |= POWERCFG_ENABLE | POWERCFG_DISABLE;
761 return si470x_set_register(radio, POWERCFG);
766 * si470x_rds_on - switch on rds reception
768 static int si470x_rds_on(struct si470x_device *radio)
770 int retval;
772 /* sysconfig 1 */
773 mutex_lock(&radio->lock);
774 radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDS;
775 retval = si470x_set_register(radio, SYSCONFIG1);
776 if (retval < 0)
777 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
778 mutex_unlock(&radio->lock);
780 return retval;
785 /**************************************************************************
786 * RDS Driver Functions
787 **************************************************************************/
790 * si470x_rds - rds processing function
792 static void si470x_rds(struct si470x_device *radio)
794 unsigned char blocknum;
795 unsigned short bler; /* rds block errors */
796 unsigned short rds;
797 unsigned char tmpbuf[3];
799 /* get rds blocks */
800 if (si470x_get_rds_registers(radio) < 0)
801 return;
802 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0) {
803 /* No RDS group ready */
804 return;
806 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSS) == 0) {
807 /* RDS decoder not synchronized */
808 return;
811 /* copy all four RDS blocks to internal buffer */
812 mutex_lock(&radio->lock);
813 for (blocknum = 0; blocknum < 4; blocknum++) {
814 switch (blocknum) {
815 default:
816 bler = (radio->registers[STATUSRSSI] &
817 STATUSRSSI_BLERA) >> 9;
818 rds = radio->registers[RDSA];
819 break;
820 case 1:
821 bler = (radio->registers[READCHAN] &
822 READCHAN_BLERB) >> 14;
823 rds = radio->registers[RDSB];
824 break;
825 case 2:
826 bler = (radio->registers[READCHAN] &
827 READCHAN_BLERC) >> 12;
828 rds = radio->registers[RDSC];
829 break;
830 case 3:
831 bler = (radio->registers[READCHAN] &
832 READCHAN_BLERD) >> 10;
833 rds = radio->registers[RDSD];
834 break;
837 /* Fill the V4L2 RDS buffer */
838 put_unaligned(cpu_to_le16(rds), (unsigned short *) &tmpbuf);
839 tmpbuf[2] = blocknum; /* offset name */
840 tmpbuf[2] |= blocknum << 3; /* received offset */
841 if (bler > max_rds_errors)
842 tmpbuf[2] |= 0x80; /* uncorrectable errors */
843 else if (bler > 0)
844 tmpbuf[2] |= 0x40; /* corrected error(s) */
846 /* copy RDS block to internal buffer */
847 memcpy(&radio->buffer[radio->wr_index], &tmpbuf, 3);
848 radio->wr_index += 3;
850 /* wrap write pointer */
851 if (radio->wr_index >= radio->buf_size)
852 radio->wr_index = 0;
854 /* check for overflow */
855 if (radio->wr_index == radio->rd_index) {
856 /* increment and wrap read pointer */
857 radio->rd_index += 3;
858 if (radio->rd_index >= radio->buf_size)
859 radio->rd_index = 0;
862 mutex_unlock(&radio->lock);
864 /* wake up read queue */
865 if (radio->wr_index != radio->rd_index)
866 wake_up_interruptible(&radio->read_queue);
871 * si470x_work - rds work function
873 static void si470x_work(struct work_struct *work)
875 struct si470x_device *radio = container_of(work, struct si470x_device,
876 work.work);
878 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
879 return;
881 si470x_rds(radio);
882 schedule_delayed_work(&radio->work, msecs_to_jiffies(rds_poll_time));
887 /**************************************************************************
888 * File Operations Interface
889 **************************************************************************/
892 * si470x_fops_read - read RDS data
894 static ssize_t si470x_fops_read(struct file *file, char __user *buf,
895 size_t count, loff_t *ppos)
897 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
898 int retval = 0;
899 unsigned int block_count = 0;
901 /* switch on rds reception */
902 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
903 si470x_rds_on(radio);
904 schedule_delayed_work(&radio->work,
905 msecs_to_jiffies(rds_poll_time));
908 /* block if no new data available */
909 while (radio->wr_index == radio->rd_index) {
910 if (file->f_flags & O_NONBLOCK)
911 return -EWOULDBLOCK;
912 if (wait_event_interruptible(radio->read_queue,
913 radio->wr_index != radio->rd_index) < 0)
914 return -EINTR;
917 /* calculate block count from byte count */
918 count /= 3;
920 /* copy RDS block out of internal buffer and to user buffer */
921 mutex_lock(&radio->lock);
922 while (block_count < count) {
923 if (radio->rd_index == radio->wr_index)
924 break;
926 /* always transfer rds complete blocks */
927 if (copy_to_user(buf, &radio->buffer[radio->rd_index], 3))
928 /* retval = -EFAULT; */
929 break;
931 /* increment and wrap read pointer */
932 radio->rd_index += 3;
933 if (radio->rd_index >= radio->buf_size)
934 radio->rd_index = 0;
936 /* increment counters */
937 block_count++;
938 buf += 3;
939 retval += 3;
941 mutex_unlock(&radio->lock);
943 return retval;
948 * si470x_fops_poll - poll RDS data
950 static unsigned int si470x_fops_poll(struct file *file,
951 struct poll_table_struct *pts)
953 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
955 /* switch on rds reception */
956 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
957 si470x_rds_on(radio);
958 schedule_delayed_work(&radio->work,
959 msecs_to_jiffies(rds_poll_time));
962 poll_wait(file, &radio->read_queue, pts);
964 if (radio->rd_index != radio->wr_index)
965 return POLLIN | POLLRDNORM;
967 return 0;
972 * si470x_fops_open - file open
974 static int si470x_fops_open(struct inode *inode, struct file *file)
976 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
977 int retval;
979 radio->users++;
981 retval = usb_autopm_get_interface(radio->intf);
982 if (retval < 0) {
983 radio->users--;
984 return -EIO;
987 if (radio->users == 1) {
988 retval = si470x_start(radio);
989 if (retval < 0)
990 usb_autopm_put_interface(radio->intf);
991 return retval;
994 return 0;
999 * si470x_fops_release - file release
1001 static int si470x_fops_release(struct inode *inode, struct file *file)
1003 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1004 int retval;
1006 if (!radio)
1007 return -ENODEV;
1009 radio->users--;
1010 if (radio->users == 0) {
1011 /* stop rds reception */
1012 cancel_delayed_work_sync(&radio->work);
1014 /* cancel read processes */
1015 wake_up_interruptible(&radio->read_queue);
1017 retval = si470x_stop(radio);
1018 usb_autopm_put_interface(radio->intf);
1019 return retval;
1022 return 0;
1027 * si470x_fops - file operations interface
1029 static const struct file_operations si470x_fops = {
1030 .owner = THIS_MODULE,
1031 .llseek = no_llseek,
1032 .read = si470x_fops_read,
1033 .poll = si470x_fops_poll,
1034 .ioctl = video_ioctl2,
1035 .compat_ioctl = v4l_compat_ioctl32,
1036 .open = si470x_fops_open,
1037 .release = si470x_fops_release,
1042 /**************************************************************************
1043 * Video4Linux Interface
1044 **************************************************************************/
1047 * si470x_v4l2_queryctrl - query control
1049 static struct v4l2_queryctrl si470x_v4l2_queryctrl[] = {
1050 /* HINT: the disabled controls are only here to satify kradio and such apps */
1052 .id = V4L2_CID_AUDIO_VOLUME,
1053 .type = V4L2_CTRL_TYPE_INTEGER,
1054 .name = "Volume",
1055 .minimum = 0,
1056 .maximum = 15,
1057 .step = 1,
1058 .default_value = 15,
1061 .id = V4L2_CID_AUDIO_BALANCE,
1062 .flags = V4L2_CTRL_FLAG_DISABLED,
1065 .id = V4L2_CID_AUDIO_BASS,
1066 .flags = V4L2_CTRL_FLAG_DISABLED,
1069 .id = V4L2_CID_AUDIO_TREBLE,
1070 .flags = V4L2_CTRL_FLAG_DISABLED,
1073 .id = V4L2_CID_AUDIO_MUTE,
1074 .type = V4L2_CTRL_TYPE_BOOLEAN,
1075 .name = "Mute",
1076 .minimum = 0,
1077 .maximum = 1,
1078 .step = 1,
1079 .default_value = 1,
1082 .id = V4L2_CID_AUDIO_LOUDNESS,
1083 .flags = V4L2_CTRL_FLAG_DISABLED,
1089 * si470x_vidioc_querycap - query device capabilities
1091 static int si470x_vidioc_querycap(struct file *file, void *priv,
1092 struct v4l2_capability *capability)
1094 strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver));
1095 strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
1096 sprintf(capability->bus_info, "USB");
1097 capability->version = DRIVER_KERNEL_VERSION;
1098 capability->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
1100 return 0;
1105 * si470x_vidioc_g_input - get input
1107 static int si470x_vidioc_g_input(struct file *filp, void *priv,
1108 unsigned int *i)
1110 *i = 0;
1112 return 0;
1117 * si470x_vidioc_s_input - set input
1119 static int si470x_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
1121 if (i != 0)
1122 return -EINVAL;
1124 return 0;
1129 * si470x_vidioc_queryctrl - enumerate control items
1131 static int si470x_vidioc_queryctrl(struct file *file, void *priv,
1132 struct v4l2_queryctrl *qc)
1134 unsigned char i;
1135 int retval = -EINVAL;
1137 for (i = 0; i < ARRAY_SIZE(si470x_v4l2_queryctrl); i++) {
1138 if (qc->id && qc->id == si470x_v4l2_queryctrl[i].id) {
1139 memcpy(qc, &(si470x_v4l2_queryctrl[i]), sizeof(*qc));
1140 retval = 0;
1141 break;
1144 if (retval < 0)
1145 printk(KERN_WARNING DRIVER_NAME
1146 ": query control failed with %d\n", retval);
1148 return retval;
1153 * si470x_vidioc_g_ctrl - get the value of a control
1155 static int si470x_vidioc_g_ctrl(struct file *file, void *priv,
1156 struct v4l2_control *ctrl)
1158 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1160 switch (ctrl->id) {
1161 case V4L2_CID_AUDIO_VOLUME:
1162 ctrl->value = radio->registers[SYSCONFIG2] &
1163 SYSCONFIG2_VOLUME;
1164 break;
1165 case V4L2_CID_AUDIO_MUTE:
1166 ctrl->value = ((radio->registers[POWERCFG] &
1167 POWERCFG_DMUTE) == 0) ? 1 : 0;
1168 break;
1171 return 0;
1176 * si470x_vidioc_s_ctrl - set the value of a control
1178 static int si470x_vidioc_s_ctrl(struct file *file, void *priv,
1179 struct v4l2_control *ctrl)
1181 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1182 int retval;
1184 switch (ctrl->id) {
1185 case V4L2_CID_AUDIO_VOLUME:
1186 radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_VOLUME;
1187 radio->registers[SYSCONFIG2] |= ctrl->value;
1188 retval = si470x_set_register(radio, SYSCONFIG2);
1189 break;
1190 case V4L2_CID_AUDIO_MUTE:
1191 if (ctrl->value == 1)
1192 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
1193 else
1194 radio->registers[POWERCFG] |= POWERCFG_DMUTE;
1195 retval = si470x_set_register(radio, POWERCFG);
1196 break;
1197 default:
1198 retval = -EINVAL;
1200 if (retval < 0)
1201 printk(KERN_WARNING DRIVER_NAME
1202 ": set control failed with %d\n", retval);
1204 return retval;
1209 * si470x_vidioc_g_audio - get audio attributes
1211 static int si470x_vidioc_g_audio(struct file *file, void *priv,
1212 struct v4l2_audio *audio)
1214 if (audio->index > 1)
1215 return -EINVAL;
1217 strcpy(audio->name, "Radio");
1218 audio->capability = V4L2_AUDCAP_STEREO;
1220 return 0;
1225 * si470x_vidioc_s_audio - set audio attributes
1227 static int si470x_vidioc_s_audio(struct file *file, void *priv,
1228 struct v4l2_audio *audio)
1230 if (audio->index != 0)
1231 return -EINVAL;
1233 return 0;
1238 * si470x_vidioc_g_tuner - get tuner attributes
1240 static int si470x_vidioc_g_tuner(struct file *file, void *priv,
1241 struct v4l2_tuner *tuner)
1243 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1244 int retval;
1246 if (tuner->index > 0)
1247 return -EINVAL;
1249 /* read status rssi */
1250 retval = si470x_get_register(radio, STATUSRSSI);
1251 if (retval < 0)
1252 return retval;
1254 strcpy(tuner->name, "FM");
1255 tuner->type = V4L2_TUNER_RADIO;
1256 switch (band) {
1257 /* 0: 87.5 - 108 MHz (USA, Europe, default) */
1258 default:
1259 tuner->rangelow = 87.5 * FREQ_MUL;
1260 tuner->rangehigh = 108 * FREQ_MUL;
1261 break;
1262 /* 1: 76 - 108 MHz (Japan wide band) */
1263 case 1 :
1264 tuner->rangelow = 76 * FREQ_MUL;
1265 tuner->rangehigh = 108 * FREQ_MUL;
1266 break;
1267 /* 2: 76 - 90 MHz (Japan) */
1268 case 2 :
1269 tuner->rangelow = 76 * FREQ_MUL;
1270 tuner->rangehigh = 90 * FREQ_MUL;
1271 break;
1273 tuner->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
1274 tuner->capability = V4L2_TUNER_CAP_LOW;
1276 /* Stereo indicator == Stereo (instead of Mono) */
1277 if ((radio->registers[STATUSRSSI] & STATUSRSSI_ST) == 1)
1278 tuner->audmode = V4L2_TUNER_MODE_STEREO;
1279 else
1280 tuner->audmode = V4L2_TUNER_MODE_MONO;
1282 /* min is worst, max is best; signal:0..0xffff; rssi: 0..0xff */
1283 tuner->signal = (radio->registers[STATUSRSSI] & STATUSRSSI_RSSI)
1284 * 0x0101;
1286 /* automatic frequency control: -1: freq to low, 1 freq to high */
1287 tuner->afc = 0;
1289 return 0;
1294 * si470x_vidioc_s_tuner - set tuner attributes
1296 static int si470x_vidioc_s_tuner(struct file *file, void *priv,
1297 struct v4l2_tuner *tuner)
1299 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1300 int retval;
1302 if (tuner->index > 0)
1303 return -EINVAL;
1305 if (tuner->audmode == V4L2_TUNER_MODE_MONO)
1306 radio->registers[POWERCFG] |= POWERCFG_MONO; /* force mono */
1307 else
1308 radio->registers[POWERCFG] &= ~POWERCFG_MONO; /* try stereo */
1310 retval = si470x_set_register(radio, POWERCFG);
1311 if (retval < 0)
1312 printk(KERN_WARNING DRIVER_NAME
1313 ": set tuner failed with %d\n", retval);
1315 return retval;
1320 * si470x_vidioc_g_frequency - get tuner or modulator radio frequency
1322 static int si470x_vidioc_g_frequency(struct file *file, void *priv,
1323 struct v4l2_frequency *freq)
1325 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1327 freq->type = V4L2_TUNER_RADIO;
1328 freq->frequency = si470x_get_freq(radio);
1330 return 0;
1335 * si470x_vidioc_s_frequency - set tuner or modulator radio frequency
1337 static int si470x_vidioc_s_frequency(struct file *file, void *priv,
1338 struct v4l2_frequency *freq)
1340 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1341 int retval;
1343 if (freq->type != V4L2_TUNER_RADIO)
1344 return -EINVAL;
1346 retval = si470x_set_freq(radio, freq->frequency);
1347 if (retval < 0)
1348 printk(KERN_WARNING DRIVER_NAME
1349 ": set frequency failed with %d\n", retval);
1351 return 0;
1356 * si470x_viddev_tamples - video device interface
1358 static struct video_device si470x_viddev_template = {
1359 .fops = &si470x_fops,
1360 .name = DRIVER_NAME,
1361 .type = VID_TYPE_TUNER,
1362 .release = video_device_release,
1363 .vidioc_querycap = si470x_vidioc_querycap,
1364 .vidioc_g_input = si470x_vidioc_g_input,
1365 .vidioc_s_input = si470x_vidioc_s_input,
1366 .vidioc_queryctrl = si470x_vidioc_queryctrl,
1367 .vidioc_g_ctrl = si470x_vidioc_g_ctrl,
1368 .vidioc_s_ctrl = si470x_vidioc_s_ctrl,
1369 .vidioc_g_audio = si470x_vidioc_g_audio,
1370 .vidioc_s_audio = si470x_vidioc_s_audio,
1371 .vidioc_g_tuner = si470x_vidioc_g_tuner,
1372 .vidioc_s_tuner = si470x_vidioc_s_tuner,
1373 .vidioc_g_frequency = si470x_vidioc_g_frequency,
1374 .vidioc_s_frequency = si470x_vidioc_s_frequency,
1375 .owner = THIS_MODULE,
1380 /**************************************************************************
1381 * USB Interface
1382 **************************************************************************/
1385 * si470x_usb_driver_probe - probe for the device
1387 static int si470x_usb_driver_probe(struct usb_interface *intf,
1388 const struct usb_device_id *id)
1390 struct si470x_device *radio;
1391 int retval = -ENOMEM;
1393 /* private data allocation */
1394 radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL);
1395 if (!radio)
1396 goto err_initial;
1398 /* video device allocation */
1399 radio->videodev = video_device_alloc();
1400 if (!radio->videodev)
1401 goto err_radio;
1403 /* initial configuration */
1404 memcpy(radio->videodev, &si470x_viddev_template,
1405 sizeof(si470x_viddev_template));
1406 radio->users = 0;
1407 radio->usbdev = interface_to_usbdev(intf);
1408 radio->intf = intf;
1409 mutex_init(&radio->lock);
1410 video_set_drvdata(radio->videodev, radio);
1412 /* show some infos about the specific device */
1413 retval = -EIO;
1414 if (si470x_get_all_registers(radio) < 0)
1415 goto err_all;
1416 printk(KERN_INFO DRIVER_NAME ": DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",
1417 radio->registers[DEVICEID], radio->registers[CHIPID]);
1419 /* check if firmware is current */
1420 if ((radio->registers[CHIPID] & CHIPID_FIRMWARE)
1421 < RADIO_SW_VERSION_CURRENT) {
1422 printk(KERN_WARNING DRIVER_NAME
1423 ": This driver is known to work with "
1424 "firmware version %hu,\n", RADIO_SW_VERSION_CURRENT);
1425 printk(KERN_WARNING DRIVER_NAME
1426 ": but the device has firmware version %hu.\n",
1427 radio->registers[CHIPID] & CHIPID_FIRMWARE);
1428 printk(KERN_WARNING DRIVER_NAME
1429 ": If you have some trouble using this driver,\n");
1430 printk(KERN_WARNING DRIVER_NAME
1431 ": please report to V4L ML at "
1432 "video4linux-list@redhat.com\n");
1435 /* set initial frequency */
1436 si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
1438 /* rds buffer allocation */
1439 radio->buf_size = rds_buf * 3;
1440 radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
1441 if (!radio->buffer)
1442 goto err_all;
1444 /* rds buffer configuration */
1445 radio->wr_index = 0;
1446 radio->rd_index = 0;
1447 init_waitqueue_head(&radio->read_queue);
1449 /* prepare rds work function */
1450 INIT_DELAYED_WORK(&radio->work, si470x_work);
1452 /* register video device */
1453 if (video_register_device(radio->videodev, VFL_TYPE_RADIO, radio_nr)) {
1454 printk(KERN_WARNING DRIVER_NAME
1455 ": Could not register video device\n");
1456 goto err_all;
1458 usb_set_intfdata(intf, radio);
1460 return 0;
1461 err_all:
1462 video_device_release(radio->videodev);
1463 kfree(radio->buffer);
1464 err_radio:
1465 kfree(radio);
1466 err_initial:
1467 return retval;
1472 * si470x_usb_driver_suspend - suspend the device
1474 static int si470x_usb_driver_suspend(struct usb_interface *intf,
1475 pm_message_t message)
1477 struct si470x_device *radio = usb_get_intfdata(intf);
1479 printk(KERN_INFO DRIVER_NAME ": suspending now...\n");
1481 cancel_delayed_work_sync(&radio->work);
1483 return 0;
1488 * si470x_usb_driver_resume - resume the device
1490 static int si470x_usb_driver_resume(struct usb_interface *intf)
1492 struct si470x_device *radio = usb_get_intfdata(intf);
1494 printk(KERN_INFO DRIVER_NAME ": resuming now...\n");
1496 mutex_lock(&radio->lock);
1497 if (radio->users && radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS)
1498 schedule_delayed_work(&radio->work,
1499 msecs_to_jiffies(rds_poll_time));
1500 mutex_unlock(&radio->lock);
1502 return 0;
1507 * si470x_usb_driver_disconnect - disconnect the device
1509 static void si470x_usb_driver_disconnect(struct usb_interface *intf)
1511 struct si470x_device *radio = usb_get_intfdata(intf);
1513 cancel_delayed_work_sync(&radio->work);
1514 usb_set_intfdata(intf, NULL);
1515 video_unregister_device(radio->videodev);
1516 kfree(radio->buffer);
1517 kfree(radio);
1522 * si470x_usb_driver - usb driver interface
1524 static struct usb_driver si470x_usb_driver = {
1525 .name = DRIVER_NAME,
1526 .probe = si470x_usb_driver_probe,
1527 .disconnect = si470x_usb_driver_disconnect,
1528 .suspend = si470x_usb_driver_suspend,
1529 .resume = si470x_usb_driver_resume,
1530 .id_table = si470x_usb_driver_id_table,
1531 .supports_autosuspend = 1,
1536 /**************************************************************************
1537 * Module Interface
1538 **************************************************************************/
1541 * si470x_module_init - module init
1543 static int __init si470x_module_init(void)
1545 printk(KERN_INFO DRIVER_DESC ", Version " DRIVER_VERSION "\n");
1546 return usb_register(&si470x_usb_driver);
1551 * si470x_module_exit - module exit
1553 static void __exit si470x_module_exit(void)
1555 usb_deregister(&si470x_usb_driver);
1559 module_init(si470x_module_init);
1560 module_exit(si470x_module_exit);
1562 MODULE_LICENSE("GPL");
1563 MODULE_AUTHOR(DRIVER_AUTHOR);
1564 MODULE_DESCRIPTION(DRIVER_DESC);
1565 MODULE_VERSION(DRIVER_VERSION);