V4L/DVB (7996): si470x: afc indication
[safe/jmp/linux-2.6] / drivers / media / radio / radio-si470x.c
1 /*
2  *  drivers/media/radio/radio-si470x.c
3  *
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  *
8  *  Copyright (c) 2008 Tobias Lorenz <tobias.lorenz@gmx.net>
9  *
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.
14  *
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.
19  *
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
23  */
24
25
26 /*
27  * User Notes:
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
36  */
37
38
39 /*
40  * History:
41  * 2008-01-12   Tobias Lorenz <tobias.lorenz@gmx.net>
42  *              Version 1.0.0
43  *              - First working version
44  * 2008-01-13   Tobias Lorenz <tobias.lorenz@gmx.net>
45  *              Version 1.0.1
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>
54  *              Version 1.0.2
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>
64  *              Version 1.0.3
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>
72  *              Version 1.0.4
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>
79  *              Version 1.0.5
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>
86  *              Version 1.0.6
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>
99  *              Version 1.0.7
100  *              - usb autosuspend support
101  *              - unplugging fixed
102  * 2008-05-07   Tobias Lorenz <tobias.lorenz@gmx.net>
103  *              Version 1.0.8
104  *              - afc indication
105  *              - more safety checks, let si470x_get_freq return errno
106  *
107  * ToDo:
108  * - add seeking support
109  * - add firmware download/update support
110  * - RDS support: interrupt mode, instead of polling
111  * - add LED status output (check if that's not already done in firmware)
112  */
113
114
115 /* driver definitions */
116 #define DRIVER_AUTHOR "Tobias Lorenz <tobias.lorenz@gmx.net>"
117 #define DRIVER_NAME "radio-si470x"
118 #define DRIVER_KERNEL_VERSION KERNEL_VERSION(1, 0, 8)
119 #define DRIVER_CARD "Silicon Labs Si470x FM Radio Receiver"
120 #define DRIVER_DESC "USB radio driver for Si470x FM Radio Receivers"
121 #define DRIVER_VERSION "1.0.8"
122
123
124 /* kernel includes */
125 #include <linux/kernel.h>
126 #include <linux/module.h>
127 #include <linux/init.h>
128 #include <linux/slab.h>
129 #include <linux/input.h>
130 #include <linux/usb.h>
131 #include <linux/hid.h>
132 #include <linux/version.h>
133 #include <linux/videodev2.h>
134 #include <linux/mutex.h>
135 #include <media/v4l2-common.h>
136 #include <media/rds.h>
137 #include <asm/unaligned.h>
138
139
140 /* USB Device ID List */
141 static struct usb_device_id si470x_usb_driver_id_table[] = {
142         /* Silicon Labs USB FM Radio Reference Design */
143         { USB_DEVICE_AND_INTERFACE_INFO(0x10c4, 0x818a, USB_CLASS_HID, 0, 0) },
144         /* ADS/Tech FM Radio Receiver (formerly Instant FM Music) */
145         { USB_DEVICE_AND_INTERFACE_INFO(0x06e1, 0xa155, USB_CLASS_HID, 0, 0) },
146         /* Terminating entry */
147         { }
148 };
149 MODULE_DEVICE_TABLE(usb, si470x_usb_driver_id_table);
150
151
152
153 /**************************************************************************
154  * Module Parameters
155  **************************************************************************/
156
157 /* Radio Nr */
158 static int radio_nr = -1;
159 module_param(radio_nr, int, 0);
160 MODULE_PARM_DESC(radio_nr, "Radio Nr");
161
162 /* Spacing (kHz) */
163 /* 0: 200 kHz (USA, Australia) */
164 /* 1: 100 kHz (Europe, Japan) */
165 /* 2:  50 kHz */
166 static unsigned short space = 2;
167 module_param(space, ushort, 0);
168 MODULE_PARM_DESC(radio_nr, "Spacing: 0=200kHz 1=100kHz *2=50kHz*");
169
170 /* Bottom of Band (MHz) */
171 /* 0: 87.5 - 108 MHz (USA, Europe)*/
172 /* 1: 76   - 108 MHz (Japan wide band) */
173 /* 2: 76   -  90 MHz (Japan) */
174 static unsigned short band = 1;
175 module_param(band, ushort, 0);
176 MODULE_PARM_DESC(radio_nr, "Band: 0=87.5..108MHz *1=76..108MHz* 2=76..90MHz");
177
178 /* De-emphasis */
179 /* 0: 75 us (USA) */
180 /* 1: 50 us (Europe, Australia, Japan) */
181 static unsigned short de = 1;
182 module_param(de, ushort, 0);
183 MODULE_PARM_DESC(radio_nr, "De-emphasis: 0=75us *1=50us*");
184
185 /* USB timeout */
186 static unsigned int usb_timeout = 500;
187 module_param(usb_timeout, uint, 0);
188 MODULE_PARM_DESC(usb_timeout, "USB timeout (ms): *500*");
189
190 /* Tune timeout */
191 static unsigned int tune_timeout = 3000;
192 module_param(tune_timeout, uint, 0);
193 MODULE_PARM_DESC(tune_timeout, "Tune timeout: *3000*");
194
195 /* RDS buffer blocks */
196 static unsigned int rds_buf = 100;
197 module_param(rds_buf, uint, 0);
198 MODULE_PARM_DESC(rds_buf, "RDS buffer entries: *100*");
199
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, 0);
207 MODULE_PARM_DESC(max_rds_errors, "RDS maximum block errors: *1*");
208
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, 0);
216 MODULE_PARM_DESC(rds_poll_time, "RDS poll time (ms): *40*");
217
218
219
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 */
226
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 */
230
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 */
235
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 */
246
247 #define CHANNEL                 3       /* Channel */
248 #define CHANNEL_TUNE            0x8000  /* bits 15..15: Tune */
249 #define CHANNEL_CHAN            0x03ff  /* bits 09..00: Channel Select */
250
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 */
261
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 */
267
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 */
273
274 #define TEST1                   7       /* Test 1 */
275 #define TEST1_AHIZEN            0x4000  /* bits 14..14: Audio High-Z Enable */
276
277 #define TEST2                   8       /* Test 2 */
278 /* TEST2 only contains reserved bits */
279
280 #define BOOTCONFIG              9       /* Boot Configuration */
281 /* BOOTCONFIG only contains reserved bits */
282
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) */
292
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 */
298
299 #define RDSA                    12      /* RDSA */
300 #define RDSA_RDSA               0xffff  /* bits 15..00: RDS Block A Data (Si4701 only) */
301
302 #define RDSB                    13      /* RDSB */
303 #define RDSB_RDSB               0xffff  /* bits 15..00: RDS Block B Data (Si4701 only) */
304
305 #define RDSC                    14      /* RDSC */
306 #define RDSC_RDSC               0xffff  /* bits 15..00: RDS Block C Data (Si4701 only) */
307
308 #define RDSD                    15      /* RDSD */
309 #define RDSD_RDSD               0xffff  /* bits 15..00: RDS Block D Data (Si4701 only) */
310
311
312
313 /**************************************************************************
314  * USB HID Reports
315  **************************************************************************/
316
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)
322
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
327
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
332
333 /* Report 19: LED state */
334 #define LED_REPORT_SIZE         3
335 #define LED_REPORT              19
336
337 /* Report 19: stream */
338 #define STREAM_REPORT_SIZE      3
339 #define STREAM_REPORT           19
340
341 /* Report 20: scratch */
342 #define SCRATCH_PAGE_SIZE       63
343 #define SCRATCH_REPORT_SIZE     (SCRATCH_PAGE_SIZE + 1)
344 #define SCRATCH_REPORT          20
345
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
351
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
355
356
357
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
365
366 #define SCRATCH_PAGE_SW_VERSION 1
367 #define SCRATCH_PAGE_HW_VERSION 2
368
369
370
371 /**************************************************************************
372  * LED State Definitions
373  **************************************************************************/
374 #define LED_COMMAND             0x35
375
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
384
385
386
387 /**************************************************************************
388  * Stream State Definitions
389  **************************************************************************/
390 #define STREAM_COMMAND  0x36
391 #define STREAM_VIDPID   0x00
392 #define STREAM_AUDIO    0xff
393
394
395
396 /**************************************************************************
397  * Bootloader / Flash Commands
398  **************************************************************************/
399
400 /* unique id sent to bootloader and required to put into a bootload state */
401 #define UNIQUE_BL_ID            0x34
402
403 /* mask for the flash data */
404 #define FLASH_DATA_MASK         0x55
405
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
415 #define BLANK                   0xff
416
417 /* bootloader command responses */
418 #define COMMAND_OK              0x01
419 #define COMMAND_FAILED          0x02
420 #define COMMAND_PENDING         0x03
421
422 /* buffer sizes */
423 #define COMMAND_BUFFER_SIZE     4
424 #define RESPONSE_BUFFER_SIZE    2
425 #define FLASH_BUFFER_SIZE       64
426 #define CRC_BUFFER_SIZE         3
427
428
429
430 /**************************************************************************
431  * General Driver Definitions
432  **************************************************************************/
433
434 /*
435  * si470x_device - private data
436  */
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;
442
443         /* driver management */
444         unsigned int users;
445         unsigned char disconnected;
446         struct mutex disconnect_lock;
447
448         /* Silabs internal registers (0..15) */
449         unsigned short registers[RADIO_REGISTER_NUM];
450
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;
459 };
460
461
462 /*
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
468  */
469 #define FREQ_MUL (1000000 / 62.5)
470
471
472
473 /**************************************************************************
474  * General Driver Functions
475  **************************************************************************/
476
477 /*
478  * si470x_get_report - receive a HID report
479  */
480 static int si470x_get_report(struct si470x_device *radio, void *buf, int size)
481 {
482         unsigned char *report = (unsigned char *) buf;
483         int retval;
484
485         retval = usb_control_msg(radio->usbdev,
486                 usb_rcvctrlpipe(radio->usbdev, 0),
487                 HID_REQ_GET_REPORT,
488                 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
489                 report[0], 2,
490                 buf, size, usb_timeout);
491
492         if (retval < 0)
493                 printk(KERN_WARNING DRIVER_NAME
494                         ": si470x_get_report: usb_control_msg returned %d\n",
495                         retval);
496         return retval;
497 }
498
499
500 /*
501  * si470x_set_report - send a HID report
502  */
503 static int si470x_set_report(struct si470x_device *radio, void *buf, int size)
504 {
505         unsigned char *report = (unsigned char *) buf;
506         int retval;
507
508         retval = usb_control_msg(radio->usbdev,
509                 usb_sndctrlpipe(radio->usbdev, 0),
510                 HID_REQ_SET_REPORT,
511                 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
512                 report[0], 2,
513                 buf, size, usb_timeout);
514
515         if (retval < 0)
516                 printk(KERN_WARNING DRIVER_NAME
517                         ": si470x_set_report: usb_control_msg returned %d\n",
518                         retval);
519         return retval;
520 }
521
522
523 /*
524  * si470x_get_register - read register
525  */
526 static int si470x_get_register(struct si470x_device *radio, int regnr)
527 {
528         unsigned char buf[REGISTER_REPORT_SIZE];
529         int retval;
530
531         buf[0] = REGISTER_REPORT(regnr);
532
533         retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
534
535         if (retval >= 0)
536                 radio->registers[regnr] = get_unaligned_be16(&buf[1]);
537
538         return (retval < 0) ? -EINVAL : 0;
539 }
540
541
542 /*
543  * si470x_set_register - write register
544  */
545 static int si470x_set_register(struct si470x_device *radio, int regnr)
546 {
547         unsigned char buf[REGISTER_REPORT_SIZE];
548         int retval;
549
550         buf[0] = REGISTER_REPORT(regnr);
551         put_unaligned_be16(radio->registers[regnr], &buf[1]);
552
553         retval = si470x_set_report(radio, (void *) &buf, sizeof(buf));
554
555         return (retval < 0) ? -EINVAL : 0;
556 }
557
558
559 /*
560  * si470x_get_all_registers - read entire registers
561  */
562 static int si470x_get_all_registers(struct si470x_device *radio)
563 {
564         unsigned char buf[ENTIRE_REPORT_SIZE];
565         int retval;
566         unsigned char regnr;
567
568         buf[0] = ENTIRE_REPORT;
569
570         retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
571
572         if (retval >= 0)
573                 for (regnr = 0; regnr < RADIO_REGISTER_NUM; regnr++)
574                         radio->registers[regnr] = get_unaligned_be16(
575                                 &buf[regnr * RADIO_REGISTER_SIZE + 1]);
576
577         return (retval < 0) ? -EINVAL : 0;
578 }
579
580
581 /*
582  * si470x_get_rds_registers - read rds registers
583  */
584 static int si470x_get_rds_registers(struct si470x_device *radio)
585 {
586         unsigned char buf[RDS_REPORT_SIZE];
587         int retval;
588         int size;
589         unsigned char regnr;
590
591         buf[0] = RDS_REPORT;
592
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));
599         if (retval < 0)
600                 printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: "
601                         "usb_interrupt_msg returned %d\n", retval);
602
603         if (retval >= 0)
604                 for (regnr = 0; regnr < RDS_REGISTER_NUM; regnr++)
605                         radio->registers[STATUSRSSI + regnr] =
606                                 get_unaligned_be16(
607                                 &buf[regnr * RADIO_REGISTER_SIZE + 1]);
608
609         return (retval < 0) ? -EINVAL : 0;
610 }
611
612
613 /*
614  * si470x_set_chan - set the channel
615  */
616 static int si470x_set_chan(struct si470x_device *radio, unsigned short chan)
617 {
618         int retval;
619         unsigned long timeout;
620         bool timed_out = 0;
621
622         /* start tuning */
623         radio->registers[CHANNEL] &= ~CHANNEL_CHAN;
624         radio->registers[CHANNEL] |= CHANNEL_TUNE | chan;
625         retval = si470x_set_register(radio, CHANNEL);
626         if (retval < 0)
627                 goto done;
628
629         /* wait till tune operation has completed */
630         timeout = jiffies + msecs_to_jiffies(tune_timeout);
631         do {
632                 retval = si470x_get_register(radio, STATUSRSSI);
633                 if (retval < 0)
634                         goto stop;
635                 timed_out = time_after(jiffies, timeout);
636         } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
637                 (!timed_out));
638         if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
639                 printk(KERN_WARNING DRIVER_NAME ": tune does not complete\n");
640         if (timed_out)
641                 printk(KERN_WARNING DRIVER_NAME
642                         ": tune timed out after %u ms\n", tune_timeout);
643
644 stop:
645         /* stop tuning */
646         radio->registers[CHANNEL] &= ~CHANNEL_TUNE;
647         retval = si470x_set_register(radio, CHANNEL);
648
649 done:
650         return retval;
651 }
652
653
654 /*
655  * si470x_get_freq - get the frequency
656  */
657 static int si470x_get_freq(struct si470x_device *radio, unsigned int *freq)
658 {
659         unsigned int spacing, band_bottom;
660         unsigned short chan;
661         int retval;
662
663         /* Spacing (kHz) */
664         switch (space) {
665         /* 0: 200 kHz (USA, Australia) */
666         case 0 : spacing = 0.200 * FREQ_MUL; break;
667         /* 1: 100 kHz (Europe, Japan) */
668         case 1 : spacing = 0.100 * FREQ_MUL; break;
669         /* 2:  50 kHz */
670         default: spacing = 0.050 * FREQ_MUL; break;
671         };
672
673         /* Bottom of Band (MHz) */
674         switch (band) {
675         /* 0: 87.5 - 108 MHz (USA, Europe) */
676         case 0 : band_bottom = 87.5 * FREQ_MUL; break;
677         /* 1: 76   - 108 MHz (Japan wide band) */
678         default: band_bottom = 76   * FREQ_MUL; break;
679         /* 2: 76   -  90 MHz (Japan) */
680         case 2 : band_bottom = 76   * FREQ_MUL; break;
681         };
682
683         /* read channel */
684         retval = si470x_get_register(radio, READCHAN);
685         chan = radio->registers[READCHAN] & READCHAN_READCHAN;
686
687         /* Frequency (MHz) = Spacing (kHz) x Channel + Bottom of Band (MHz) */
688         *freq = chan * spacing + band_bottom;
689
690         return retval;
691 }
692
693
694 /*
695  * si470x_set_freq - set the frequency
696  */
697 static int si470x_set_freq(struct si470x_device *radio, unsigned int freq)
698 {
699         unsigned int spacing, band_bottom;
700         unsigned short chan;
701
702         /* Spacing (kHz) */
703         switch (space) {
704         /* 0: 200 kHz (USA, Australia) */
705         case 0 : spacing = 0.200 * FREQ_MUL; break;
706         /* 1: 100 kHz (Europe, Japan) */
707         case 1 : spacing = 0.100 * FREQ_MUL; break;
708         /* 2:  50 kHz */
709         default: spacing = 0.050 * FREQ_MUL; break;
710         };
711
712         /* Bottom of Band (MHz) */
713         switch (band) {
714         /* 0: 87.5 - 108 MHz (USA, Europe) */
715         case 0 : band_bottom = 87.5 * FREQ_MUL; break;
716         /* 1: 76   - 108 MHz (Japan wide band) */
717         default: band_bottom = 76   * FREQ_MUL; break;
718         /* 2: 76   -  90 MHz (Japan) */
719         case 2 : band_bottom = 76   * FREQ_MUL; break;
720         };
721
722         /* Chan = [ Freq (Mhz) - Bottom of Band (MHz) ] / Spacing (kHz) */
723         chan = (freq - band_bottom) / spacing;
724
725         return si470x_set_chan(radio, chan);
726 }
727
728
729 /*
730  * si470x_start - switch on radio
731  */
732 static int si470x_start(struct si470x_device *radio)
733 {
734         int retval;
735
736         /* powercfg */
737         radio->registers[POWERCFG] =
738                 POWERCFG_DMUTE | POWERCFG_ENABLE | POWERCFG_RDSM;
739         retval = si470x_set_register(radio, POWERCFG);
740         if (retval < 0)
741                 goto done;
742
743         /* sysconfig 1 */
744         radio->registers[SYSCONFIG1] = SYSCONFIG1_DE;
745         retval = si470x_set_register(radio, SYSCONFIG1);
746         if (retval < 0)
747                 goto done;
748
749         /* sysconfig 2 */
750         radio->registers[SYSCONFIG2] =
751                 (0x3f  << 8) |                          /* SEEKTH */
752                 ((band  << 6) & SYSCONFIG2_BAND)  |     /* BAND */
753                 ((space << 4) & SYSCONFIG2_SPACE) |     /* SPACE */
754                 15;                                     /* VOLUME (max) */
755         retval = si470x_set_register(radio, SYSCONFIG2);
756         if (retval < 0)
757                 goto done;
758
759         /* reset last channel */
760         retval = si470x_set_chan(radio,
761                 radio->registers[CHANNEL] & CHANNEL_CHAN);
762
763 done:
764         return retval;
765 }
766
767
768 /*
769  * si470x_stop - switch off radio
770  */
771 static int si470x_stop(struct si470x_device *radio)
772 {
773         int retval;
774
775         /* sysconfig 1 */
776         radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
777         retval = si470x_set_register(radio, SYSCONFIG1);
778         if (retval < 0)
779                 goto done;
780
781         /* powercfg */
782         radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
783         /* POWERCFG_ENABLE has to automatically go low */
784         radio->registers[POWERCFG] |= POWERCFG_ENABLE | POWERCFG_DISABLE;
785         retval = si470x_set_register(radio, POWERCFG);
786
787 done:
788         return retval;
789 }
790
791
792 /*
793  * si470x_rds_on - switch on rds reception
794  */
795 static int si470x_rds_on(struct si470x_device *radio)
796 {
797         int retval;
798
799         /* sysconfig 1 */
800         mutex_lock(&radio->lock);
801         radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDS;
802         retval = si470x_set_register(radio, SYSCONFIG1);
803         if (retval < 0)
804                 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
805         mutex_unlock(&radio->lock);
806
807         return retval;
808 }
809
810
811
812 /**************************************************************************
813  * RDS Driver Functions
814  **************************************************************************/
815
816 /*
817  * si470x_rds - rds processing function
818  */
819 static void si470x_rds(struct si470x_device *radio)
820 {
821         unsigned char blocknum;
822         unsigned short bler; /* rds block errors */
823         unsigned short rds;
824         unsigned char tmpbuf[3];
825
826         /* get rds blocks */
827         if (si470x_get_rds_registers(radio) < 0)
828                 return;
829         if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0) {
830                 /* No RDS group ready */
831                 return;
832         }
833         if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSS) == 0) {
834                 /* RDS decoder not synchronized */
835                 return;
836         }
837
838         /* copy all four RDS blocks to internal buffer */
839         mutex_lock(&radio->lock);
840         for (blocknum = 0; blocknum < 4; blocknum++) {
841                 switch (blocknum) {
842                 default:
843                         bler = (radio->registers[STATUSRSSI] &
844                                         STATUSRSSI_BLERA) >> 9;
845                         rds = radio->registers[RDSA];
846                         break;
847                 case 1:
848                         bler = (radio->registers[READCHAN] &
849                                         READCHAN_BLERB) >> 14;
850                         rds = radio->registers[RDSB];
851                         break;
852                 case 2:
853                         bler = (radio->registers[READCHAN] &
854                                         READCHAN_BLERC) >> 12;
855                         rds = radio->registers[RDSC];
856                         break;
857                 case 3:
858                         bler = (radio->registers[READCHAN] &
859                                         READCHAN_BLERD) >> 10;
860                         rds = radio->registers[RDSD];
861                         break;
862                 };
863
864                 /* Fill the V4L2 RDS buffer */
865                 put_unaligned_le16(rds, &tmpbuf);
866                 tmpbuf[2] = blocknum;           /* offset name */
867                 tmpbuf[2] |= blocknum << 3;     /* received offset */
868                 if (bler > max_rds_errors)
869                         tmpbuf[2] |= 0x80; /* uncorrectable errors */
870                 else if (bler > 0)
871                         tmpbuf[2] |= 0x40; /* corrected error(s) */
872
873                 /* copy RDS block to internal buffer */
874                 memcpy(&radio->buffer[radio->wr_index], &tmpbuf, 3);
875                 radio->wr_index += 3;
876
877                 /* wrap write pointer */
878                 if (radio->wr_index >= radio->buf_size)
879                         radio->wr_index = 0;
880
881                 /* check for overflow */
882                 if (radio->wr_index == radio->rd_index) {
883                         /* increment and wrap read pointer */
884                         radio->rd_index += 3;
885                         if (radio->rd_index >= radio->buf_size)
886                                 radio->rd_index = 0;
887                 }
888         }
889         mutex_unlock(&radio->lock);
890
891         /* wake up read queue */
892         if (radio->wr_index != radio->rd_index)
893                 wake_up_interruptible(&radio->read_queue);
894 }
895
896
897 /*
898  * si470x_work - rds work function
899  */
900 static void si470x_work(struct work_struct *work)
901 {
902         struct si470x_device *radio = container_of(work, struct si470x_device,
903                 work.work);
904
905         /* safety checks */
906         if (radio->disconnected)
907                 return;
908         if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
909                 return;
910
911         si470x_rds(radio);
912         schedule_delayed_work(&radio->work, msecs_to_jiffies(rds_poll_time));
913 }
914
915
916
917 /**************************************************************************
918  * File Operations Interface
919  **************************************************************************/
920
921 /*
922  * si470x_fops_read - read RDS data
923  */
924 static ssize_t si470x_fops_read(struct file *file, char __user *buf,
925                 size_t count, loff_t *ppos)
926 {
927         struct si470x_device *radio = video_get_drvdata(video_devdata(file));
928         int retval = 0;
929         unsigned int block_count = 0;
930
931         /* switch on rds reception */
932         if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
933                 si470x_rds_on(radio);
934                 schedule_delayed_work(&radio->work,
935                         msecs_to_jiffies(rds_poll_time));
936         }
937
938         /* block if no new data available */
939         while (radio->wr_index == radio->rd_index) {
940                 if (file->f_flags & O_NONBLOCK) {
941                         retval = -EWOULDBLOCK;
942                         goto done;
943                 }
944                 if (wait_event_interruptible(radio->read_queue,
945                         radio->wr_index != radio->rd_index) < 0) {
946                         retval = -EINTR;
947                         goto done;
948                 }
949         }
950
951         /* calculate block count from byte count */
952         count /= 3;
953
954         /* copy RDS block out of internal buffer and to user buffer */
955         mutex_lock(&radio->lock);
956         while (block_count < count) {
957                 if (radio->rd_index == radio->wr_index)
958                         break;
959
960                 /* always transfer rds complete blocks */
961                 if (copy_to_user(buf, &radio->buffer[radio->rd_index], 3))
962                         /* retval = -EFAULT; */
963                         break;
964
965                 /* increment and wrap read pointer */
966                 radio->rd_index += 3;
967                 if (radio->rd_index >= radio->buf_size)
968                         radio->rd_index = 0;
969
970                 /* increment counters */
971                 block_count++;
972                 buf += 3;
973                 retval += 3;
974         }
975         mutex_unlock(&radio->lock);
976
977 done:
978         return retval;
979 }
980
981
982 /*
983  * si470x_fops_poll - poll RDS data
984  */
985 static unsigned int si470x_fops_poll(struct file *file,
986                 struct poll_table_struct *pts)
987 {
988         struct si470x_device *radio = video_get_drvdata(video_devdata(file));
989         int retval = 0;
990
991         /* switch on rds reception */
992         if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
993                 si470x_rds_on(radio);
994                 schedule_delayed_work(&radio->work,
995                         msecs_to_jiffies(rds_poll_time));
996         }
997
998         poll_wait(file, &radio->read_queue, pts);
999
1000         if (radio->rd_index != radio->wr_index)
1001                 retval = POLLIN | POLLRDNORM;
1002
1003         return retval;
1004 }
1005
1006
1007 /*
1008  * si470x_fops_open - file open
1009  */
1010 static int si470x_fops_open(struct inode *inode, struct file *file)
1011 {
1012         struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1013         int retval;
1014
1015         radio->users++;
1016
1017         retval = usb_autopm_get_interface(radio->intf);
1018         if (retval < 0) {
1019                 radio->users--;
1020                 retval = -EIO;
1021                 goto done;
1022         }
1023
1024         if (radio->users == 1) {
1025                 retval = si470x_start(radio);
1026                 if (retval < 0)
1027                         usb_autopm_put_interface(radio->intf);
1028         }
1029
1030 done:
1031         return retval;
1032 }
1033
1034
1035 /*
1036  * si470x_fops_release - file release
1037  */
1038 static int si470x_fops_release(struct inode *inode, struct file *file)
1039 {
1040         struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1041         int retval = 0;
1042
1043         /* safety check */
1044         if (!radio) {
1045                 retval = -ENODEV;
1046                 goto done;
1047         }
1048
1049         mutex_lock(&radio->disconnect_lock);
1050         radio->users--;
1051         if (radio->users == 0) {
1052                 if (radio->disconnected) {
1053                         video_unregister_device(radio->videodev);
1054                         kfree(radio->buffer);
1055                         kfree(radio);
1056                         goto unlock;
1057                 }
1058
1059                 /* stop rds reception */
1060                 cancel_delayed_work_sync(&radio->work);
1061
1062                 /* cancel read processes */
1063                 wake_up_interruptible(&radio->read_queue);
1064
1065                 retval = si470x_stop(radio);
1066                 usb_autopm_put_interface(radio->intf);
1067         }
1068
1069 unlock:
1070         mutex_unlock(&radio->disconnect_lock);
1071
1072 done:
1073         return retval;
1074 }
1075
1076
1077 /*
1078  * si470x_fops - file operations interface
1079  */
1080 static const struct file_operations si470x_fops = {
1081         .owner          = THIS_MODULE,
1082         .llseek         = no_llseek,
1083         .read           = si470x_fops_read,
1084         .poll           = si470x_fops_poll,
1085         .ioctl          = video_ioctl2,
1086 #ifdef CONFIG_COMPAT
1087         .compat_ioctl   = v4l_compat_ioctl32,
1088 #endif
1089         .open           = si470x_fops_open,
1090         .release        = si470x_fops_release,
1091 };
1092
1093
1094
1095 /**************************************************************************
1096  * Video4Linux Interface
1097  **************************************************************************/
1098
1099 /*
1100  * si470x_v4l2_queryctrl - query control
1101  */
1102 static struct v4l2_queryctrl si470x_v4l2_queryctrl[] = {
1103 /* HINT: the disabled controls are only here to satify kradio and such apps */
1104         {
1105                 .id             = V4L2_CID_AUDIO_VOLUME,
1106                 .type           = V4L2_CTRL_TYPE_INTEGER,
1107                 .name           = "Volume",
1108                 .minimum        = 0,
1109                 .maximum        = 15,
1110                 .step           = 1,
1111                 .default_value  = 15,
1112         },
1113         {
1114                 .id             = V4L2_CID_AUDIO_BALANCE,
1115                 .flags          = V4L2_CTRL_FLAG_DISABLED,
1116         },
1117         {
1118                 .id             = V4L2_CID_AUDIO_BASS,
1119                 .flags          = V4L2_CTRL_FLAG_DISABLED,
1120         },
1121         {
1122                 .id             = V4L2_CID_AUDIO_TREBLE,
1123                 .flags          = V4L2_CTRL_FLAG_DISABLED,
1124         },
1125         {
1126                 .id             = V4L2_CID_AUDIO_MUTE,
1127                 .type           = V4L2_CTRL_TYPE_BOOLEAN,
1128                 .name           = "Mute",
1129                 .minimum        = 0,
1130                 .maximum        = 1,
1131                 .step           = 1,
1132                 .default_value  = 1,
1133         },
1134         {
1135                 .id             = V4L2_CID_AUDIO_LOUDNESS,
1136                 .flags          = V4L2_CTRL_FLAG_DISABLED,
1137         },
1138 };
1139
1140
1141 /*
1142  * si470x_vidioc_querycap - query device capabilities
1143  */
1144 static int si470x_vidioc_querycap(struct file *file, void *priv,
1145                 struct v4l2_capability *capability)
1146 {
1147         strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver));
1148         strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
1149         sprintf(capability->bus_info, "USB");
1150         capability->version = DRIVER_KERNEL_VERSION;
1151         capability->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
1152
1153         return 0;
1154 }
1155
1156
1157 /*
1158  * si470x_vidioc_g_input - get input
1159  */
1160 static int si470x_vidioc_g_input(struct file *file, void *priv,
1161                 unsigned int *i)
1162 {
1163         *i = 0;
1164
1165         return 0;
1166 }
1167
1168
1169 /*
1170  * si470x_vidioc_s_input - set input
1171  */
1172 static int si470x_vidioc_s_input(struct file *file, void *priv, unsigned int i)
1173 {
1174         int retval = 0;
1175
1176         /* safety checks */
1177         if (i != 0)
1178                 retval = -EINVAL;
1179
1180         if (retval < 0)
1181                 printk(KERN_WARNING DRIVER_NAME
1182                         ": set input failed with %d\n", retval);
1183         return retval;
1184 }
1185
1186
1187 /*
1188  * si470x_vidioc_queryctrl - enumerate control items
1189  */
1190 static int si470x_vidioc_queryctrl(struct file *file, void *priv,
1191                 struct v4l2_queryctrl *qc)
1192 {
1193         unsigned char i;
1194         int retval = -EINVAL;
1195
1196         /* safety checks */
1197         if (!qc->id)
1198                 goto done;
1199
1200         for (i = 0; i < ARRAY_SIZE(si470x_v4l2_queryctrl); i++) {
1201                 if (qc->id == si470x_v4l2_queryctrl[i].id) {
1202                         memcpy(qc, &(si470x_v4l2_queryctrl[i]), sizeof(*qc));
1203                         retval = 0;
1204                         break;
1205                 }
1206         }
1207
1208 done:
1209         if (retval < 0)
1210                 printk(KERN_WARNING DRIVER_NAME
1211                         ": query controls failed with %d\n", retval);
1212         return retval;
1213 }
1214
1215
1216 /*
1217  * si470x_vidioc_g_ctrl - get the value of a control
1218  */
1219 static int si470x_vidioc_g_ctrl(struct file *file, void *priv,
1220                 struct v4l2_control *ctrl)
1221 {
1222         struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1223         int retval = 0;
1224
1225         /* safety checks */
1226         if (radio->disconnected) {
1227                 retval = -EIO;
1228                 goto done;
1229         }
1230
1231         switch (ctrl->id) {
1232         case V4L2_CID_AUDIO_VOLUME:
1233                 ctrl->value = radio->registers[SYSCONFIG2] &
1234                                 SYSCONFIG2_VOLUME;
1235                 break;
1236         case V4L2_CID_AUDIO_MUTE:
1237                 ctrl->value = ((radio->registers[POWERCFG] &
1238                                 POWERCFG_DMUTE) == 0) ? 1 : 0;
1239                 break;
1240         default:
1241                 retval = -EINVAL;
1242         }
1243
1244 done:
1245         if (retval < 0)
1246                 printk(KERN_WARNING DRIVER_NAME
1247                         ": get control failed with %d\n", retval);
1248         return retval;
1249 }
1250
1251
1252 /*
1253  * si470x_vidioc_s_ctrl - set the value of a control
1254  */
1255 static int si470x_vidioc_s_ctrl(struct file *file, void *priv,
1256                 struct v4l2_control *ctrl)
1257 {
1258         struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1259         int retval = 0;
1260
1261         /* safety checks */
1262         if (radio->disconnected) {
1263                 retval = -EIO;
1264                 goto done;
1265         }
1266
1267         switch (ctrl->id) {
1268         case V4L2_CID_AUDIO_VOLUME:
1269                 radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_VOLUME;
1270                 radio->registers[SYSCONFIG2] |= ctrl->value;
1271                 retval = si470x_set_register(radio, SYSCONFIG2);
1272                 break;
1273         case V4L2_CID_AUDIO_MUTE:
1274                 if (ctrl->value == 1)
1275                         radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
1276                 else
1277                         radio->registers[POWERCFG] |= POWERCFG_DMUTE;
1278                 retval = si470x_set_register(radio, POWERCFG);
1279                 break;
1280         default:
1281                 retval = -EINVAL;
1282         }
1283
1284 done:
1285         if (retval < 0)
1286                 printk(KERN_WARNING DRIVER_NAME
1287                         ": set control failed with %d\n", retval);
1288         return retval;
1289 }
1290
1291
1292 /*
1293  * si470x_vidioc_g_audio - get audio attributes
1294  */
1295 static int si470x_vidioc_g_audio(struct file *file, void *priv,
1296                 struct v4l2_audio *audio)
1297 {
1298         int retval = 0;
1299
1300         /* safety checks */
1301         if (audio->index != 0) {
1302                 retval = -EINVAL;
1303                 goto done;
1304         }
1305
1306         strcpy(audio->name, "Radio");
1307         audio->capability = V4L2_AUDCAP_STEREO;
1308
1309 done:
1310         if (retval < 0)
1311                 printk(KERN_WARNING DRIVER_NAME
1312                         ": get audio failed with %d\n", retval);
1313         return retval;
1314 }
1315
1316
1317 /*
1318  * si470x_vidioc_s_audio - set audio attributes
1319  */
1320 static int si470x_vidioc_s_audio(struct file *file, void *priv,
1321                 struct v4l2_audio *audio)
1322 {
1323         int retval = 0;
1324
1325         /* safety checks */
1326         if (audio->index != 0) {
1327                 retval = -EINVAL;
1328                 goto done;
1329         }
1330
1331 done:
1332         if (retval < 0)
1333                 printk(KERN_WARNING DRIVER_NAME
1334                         ": set audio failed with %d\n", retval);
1335         return retval;
1336 }
1337
1338
1339 /*
1340  * si470x_vidioc_g_tuner - get tuner attributes
1341  */
1342 static int si470x_vidioc_g_tuner(struct file *file, void *priv,
1343                 struct v4l2_tuner *tuner)
1344 {
1345         struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1346         int retval = 0;
1347
1348         /* safety checks */
1349         if (radio->disconnected) {
1350                 retval = -EIO;
1351                 goto done;
1352         }
1353         if ((tuner->index != 0) && (tuner->type != V4L2_TUNER_RADIO)) {
1354                 retval = -EINVAL;
1355                 goto done;
1356         }
1357
1358         retval = si470x_get_register(radio, STATUSRSSI);
1359         if (retval < 0)
1360                 goto done;
1361
1362         strcpy(tuner->name, "FM");
1363         switch (band) {
1364         /* 0: 87.5 - 108 MHz (USA, Europe, default) */
1365         default:
1366                 tuner->rangelow  =  87.5 * FREQ_MUL;
1367                 tuner->rangehigh = 108   * FREQ_MUL;
1368                 break;
1369         /* 1: 76   - 108 MHz (Japan wide band) */
1370         case 1 :
1371                 tuner->rangelow  =  76   * FREQ_MUL;
1372                 tuner->rangehigh = 108   * FREQ_MUL;
1373                 break;
1374         /* 2: 76   -  90 MHz (Japan) */
1375         case 2 :
1376                 tuner->rangelow  =  76   * FREQ_MUL;
1377                 tuner->rangehigh =  90   * FREQ_MUL;
1378                 break;
1379         };
1380         tuner->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
1381         tuner->capability = V4L2_TUNER_CAP_LOW;
1382
1383         /* Stereo indicator == Stereo (instead of Mono) */
1384         if ((radio->registers[STATUSRSSI] & STATUSRSSI_ST) == 1)
1385                 tuner->audmode = V4L2_TUNER_MODE_STEREO;
1386         else
1387                 tuner->audmode = V4L2_TUNER_MODE_MONO;
1388
1389         /* min is worst, max is best; signal:0..0xffff; rssi: 0..0xff */
1390         tuner->signal = (radio->registers[STATUSRSSI] & STATUSRSSI_RSSI)
1391                                 * 0x0101;
1392
1393         /* automatic frequency control: -1: freq to low, 1 freq to high */
1394         /* AFCRL does only indicate that freq. differs, not if too low/high */
1395         tuner->afc = (radio->registers[STATUSRSSI] & STATUSRSSI_AFCRL) ? 1 : 0;
1396
1397 done:
1398         if (retval < 0)
1399                 printk(KERN_WARNING DRIVER_NAME
1400                         ": get tuner failed with %d\n", retval);
1401         return retval;
1402 }
1403
1404
1405 /*
1406  * si470x_vidioc_s_tuner - set tuner attributes
1407  */
1408 static int si470x_vidioc_s_tuner(struct file *file, void *priv,
1409                 struct v4l2_tuner *tuner)
1410 {
1411         struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1412         int retval = 0;
1413
1414         /* safety checks */
1415         if (radio->disconnected) {
1416                 retval = -EIO;
1417                 goto done;
1418         }
1419         if ((tuner->index != 0) && (tuner->type != V4L2_TUNER_RADIO)) {
1420                 retval = -EINVAL;
1421                 goto done;
1422         }
1423
1424         if (tuner->audmode == V4L2_TUNER_MODE_MONO)
1425                 radio->registers[POWERCFG] |= POWERCFG_MONO;  /* force mono */
1426         else
1427                 radio->registers[POWERCFG] &= ~POWERCFG_MONO; /* try stereo */
1428
1429         retval = si470x_set_register(radio, POWERCFG);
1430
1431 done:
1432         if (retval < 0)
1433                 printk(KERN_WARNING DRIVER_NAME
1434                         ": set tuner failed with %d\n", retval);
1435         return retval;
1436 }
1437
1438
1439 /*
1440  * si470x_vidioc_g_frequency - get tuner or modulator radio frequency
1441  */
1442 static int si470x_vidioc_g_frequency(struct file *file, void *priv,
1443                 struct v4l2_frequency *freq)
1444 {
1445         struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1446         int retval = 0;
1447
1448         /* safety checks */
1449         if (radio->disconnected) {
1450                 retval = -EIO;
1451                 goto done;
1452         }
1453         if ((freq->tuner != 0) && (freq->type != V4L2_TUNER_RADIO)) {
1454                 retval = -EINVAL;
1455                 goto done;
1456         }
1457
1458         retval = si470x_get_freq(radio, &freq->frequency);
1459
1460 done:
1461         if (retval < 0)
1462                 printk(KERN_WARNING DRIVER_NAME
1463                         ": get frequency failed with %d\n", retval);
1464         return retval;
1465 }
1466
1467
1468 /*
1469  * si470x_vidioc_s_frequency - set tuner or modulator radio frequency
1470  */
1471 static int si470x_vidioc_s_frequency(struct file *file, void *priv,
1472                 struct v4l2_frequency *freq)
1473 {
1474         struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1475         int retval = 0;
1476
1477         /* safety checks */
1478         if (radio->disconnected) {
1479                 retval = -EIO;
1480                 goto done;
1481         }
1482         if ((freq->tuner != 0) && (freq->type != V4L2_TUNER_RADIO)) {
1483                 retval = -EINVAL;
1484                 goto done;
1485         }
1486
1487         retval = si470x_set_freq(radio, freq->frequency);
1488
1489 done:
1490         if (retval < 0)
1491                 printk(KERN_WARNING DRIVER_NAME
1492                         ": set frequency failed with %d\n", retval);
1493         return retval;
1494 }
1495
1496
1497 /*
1498  * si470x_viddev_tamples - video device interface
1499  */
1500 static struct video_device si470x_viddev_template = {
1501         .fops                   = &si470x_fops,
1502         .name                   = DRIVER_NAME,
1503         .type                   = VID_TYPE_TUNER,
1504         .release                = video_device_release,
1505         .vidioc_querycap        = si470x_vidioc_querycap,
1506         .vidioc_g_input         = si470x_vidioc_g_input,
1507         .vidioc_s_input         = si470x_vidioc_s_input,
1508         .vidioc_queryctrl       = si470x_vidioc_queryctrl,
1509         .vidioc_g_ctrl          = si470x_vidioc_g_ctrl,
1510         .vidioc_s_ctrl          = si470x_vidioc_s_ctrl,
1511         .vidioc_g_audio         = si470x_vidioc_g_audio,
1512         .vidioc_s_audio         = si470x_vidioc_s_audio,
1513         .vidioc_g_tuner         = si470x_vidioc_g_tuner,
1514         .vidioc_s_tuner         = si470x_vidioc_s_tuner,
1515         .vidioc_g_frequency     = si470x_vidioc_g_frequency,
1516         .vidioc_s_frequency     = si470x_vidioc_s_frequency,
1517         .owner                  = THIS_MODULE,
1518 };
1519
1520
1521
1522 /**************************************************************************
1523  * USB Interface
1524  **************************************************************************/
1525
1526 /*
1527  * si470x_usb_driver_probe - probe for the device
1528  */
1529 static int si470x_usb_driver_probe(struct usb_interface *intf,
1530                 const struct usb_device_id *id)
1531 {
1532         struct si470x_device *radio;
1533         int retval = 0;
1534
1535         /* private data allocation and initialization */
1536         radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL);
1537         if (!radio) {
1538                 retval = -ENOMEM;
1539                 goto err_initial;
1540         }
1541         radio->users = 0;
1542         radio->disconnected = 0;
1543         radio->usbdev = interface_to_usbdev(intf);
1544         radio->intf = intf;
1545         mutex_init(&radio->disconnect_lock);
1546         mutex_init(&radio->lock);
1547
1548         /* video device allocation and initialization */
1549         radio->videodev = video_device_alloc();
1550         if (!radio->videodev) {
1551                 retval = -ENOMEM;
1552                 goto err_radio;
1553         }
1554         memcpy(radio->videodev, &si470x_viddev_template,
1555                         sizeof(si470x_viddev_template));
1556         video_set_drvdata(radio->videodev, radio);
1557
1558         /* show some infos about the specific device */
1559         if (si470x_get_all_registers(radio) < 0) {
1560                 retval = -EIO;
1561                 goto err_all;
1562         }
1563         printk(KERN_INFO DRIVER_NAME ": DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",
1564                         radio->registers[DEVICEID], radio->registers[CHIPID]);
1565
1566         /* check if firmware is current */
1567         if ((radio->registers[CHIPID] & CHIPID_FIRMWARE)
1568                         < RADIO_SW_VERSION_CURRENT) {
1569                 printk(KERN_WARNING DRIVER_NAME
1570                         ": This driver is known to work with "
1571                         "firmware version %hu,\n", RADIO_SW_VERSION_CURRENT);
1572                 printk(KERN_WARNING DRIVER_NAME
1573                         ": but the device has firmware version %hu.\n",
1574                         radio->registers[CHIPID] & CHIPID_FIRMWARE);
1575                 printk(KERN_WARNING DRIVER_NAME
1576                         ": If you have some trouble using this driver,\n");
1577                 printk(KERN_WARNING DRIVER_NAME
1578                         ": please report to V4L ML at "
1579                         "video4linux-list@redhat.com\n");
1580         }
1581
1582         /* set initial frequency */
1583         si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
1584
1585         /* rds buffer allocation */
1586         radio->buf_size = rds_buf * 3;
1587         radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
1588         if (!radio->buffer) {
1589                 retval = -EIO;
1590                 goto err_all;
1591         }
1592
1593         /* rds buffer configuration */
1594         radio->wr_index = 0;
1595         radio->rd_index = 0;
1596         init_waitqueue_head(&radio->read_queue);
1597
1598         /* prepare rds work function */
1599         INIT_DELAYED_WORK(&radio->work, si470x_work);
1600
1601         /* register video device */
1602         if (video_register_device(radio->videodev, VFL_TYPE_RADIO, radio_nr)) {
1603                 retval = -EIO;
1604                 printk(KERN_WARNING DRIVER_NAME
1605                                 ": Could not register video device\n");
1606                 goto err_all;
1607         }
1608         usb_set_intfdata(intf, radio);
1609
1610         return 0;
1611 err_all:
1612         video_device_release(radio->videodev);
1613         kfree(radio->buffer);
1614 err_radio:
1615         kfree(radio);
1616 err_initial:
1617         return retval;
1618 }
1619
1620
1621 /*
1622  * si470x_usb_driver_suspend - suspend the device
1623  */
1624 static int si470x_usb_driver_suspend(struct usb_interface *intf,
1625                 pm_message_t message)
1626 {
1627         struct si470x_device *radio = usb_get_intfdata(intf);
1628
1629         printk(KERN_INFO DRIVER_NAME ": suspending now...\n");
1630
1631         cancel_delayed_work_sync(&radio->work);
1632
1633         return 0;
1634 }
1635
1636
1637 /*
1638  * si470x_usb_driver_resume - resume the device
1639  */
1640 static int si470x_usb_driver_resume(struct usb_interface *intf)
1641 {
1642         struct si470x_device *radio = usb_get_intfdata(intf);
1643
1644         printk(KERN_INFO DRIVER_NAME ": resuming now...\n");
1645
1646         mutex_lock(&radio->lock);
1647         if (radio->users && radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS)
1648                 schedule_delayed_work(&radio->work,
1649                         msecs_to_jiffies(rds_poll_time));
1650         mutex_unlock(&radio->lock);
1651
1652         return 0;
1653 }
1654
1655
1656 /*
1657  * si470x_usb_driver_disconnect - disconnect the device
1658  */
1659 static void si470x_usb_driver_disconnect(struct usb_interface *intf)
1660 {
1661         struct si470x_device *radio = usb_get_intfdata(intf);
1662
1663         mutex_lock(&radio->disconnect_lock);
1664         radio->disconnected = 1;
1665         cancel_delayed_work_sync(&radio->work);
1666         usb_set_intfdata(intf, NULL);
1667         if (radio->users == 0) {
1668                 video_unregister_device(radio->videodev);
1669                 kfree(radio->buffer);
1670                 kfree(radio);
1671         }
1672         mutex_unlock(&radio->disconnect_lock);
1673 }
1674
1675
1676 /*
1677  * si470x_usb_driver - usb driver interface
1678  */
1679 static struct usb_driver si470x_usb_driver = {
1680         .name                   = DRIVER_NAME,
1681         .probe                  = si470x_usb_driver_probe,
1682         .disconnect             = si470x_usb_driver_disconnect,
1683         .suspend                = si470x_usb_driver_suspend,
1684         .resume                 = si470x_usb_driver_resume,
1685         .id_table               = si470x_usb_driver_id_table,
1686         .supports_autosuspend   = 1,
1687 };
1688
1689
1690
1691 /**************************************************************************
1692  * Module Interface
1693  **************************************************************************/
1694
1695 /*
1696  * si470x_module_init - module init
1697  */
1698 static int __init si470x_module_init(void)
1699 {
1700         printk(KERN_INFO DRIVER_DESC ", Version " DRIVER_VERSION "\n");
1701         return usb_register(&si470x_usb_driver);
1702 }
1703
1704
1705 /*
1706  * si470x_module_exit - module exit
1707  */
1708 static void __exit si470x_module_exit(void)
1709 {
1710         usb_deregister(&si470x_usb_driver);
1711 }
1712
1713
1714 module_init(si470x_module_init);
1715 module_exit(si470x_module_exit);
1716
1717 MODULE_LICENSE("GPL");
1718 MODULE_AUTHOR(DRIVER_AUTHOR);
1719 MODULE_DESCRIPTION(DRIVER_DESC);
1720 MODULE_VERSION(DRIVER_VERSION);