Merge branch 'sched-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[pandora-kernel.git] / 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  *              - hardware frequency seek support
105  *              - afc indication
106  *              - more safety checks, let si470x_get_freq return errno
107  *
108  * ToDo:
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 /* Seek timeout */
196 static unsigned int seek_timeout = 5000;
197 module_param(seek_timeout, uint, 0);
198 MODULE_PARM_DESC(seek_timeout, "Seek timeout: *5000*");
199
200 /* RDS buffer blocks */
201 static unsigned int rds_buf = 100;
202 module_param(rds_buf, uint, 0);
203 MODULE_PARM_DESC(rds_buf, "RDS buffer entries: *100*");
204
205 /* RDS maximum block errors */
206 static unsigned short max_rds_errors = 1;
207 /* 0 means   0  errors requiring correction */
208 /* 1 means 1-2  errors requiring correction (used by original USBRadio.exe) */
209 /* 2 means 3-5  errors requiring correction */
210 /* 3 means   6+ errors or errors in checkword, correction not possible */
211 module_param(max_rds_errors, ushort, 0);
212 MODULE_PARM_DESC(max_rds_errors, "RDS maximum block errors: *1*");
213
214 /* RDS poll frequency */
215 static unsigned int rds_poll_time = 40;
216 /* 40 is used by the original USBRadio.exe */
217 /* 50 is used by radio-cadet */
218 /* 75 should be okay */
219 /* 80 is the usual RDS receive interval */
220 module_param(rds_poll_time, uint, 0);
221 MODULE_PARM_DESC(rds_poll_time, "RDS poll time (ms): *40*");
222
223
224
225 /**************************************************************************
226  * Register Definitions
227  **************************************************************************/
228 #define RADIO_REGISTER_SIZE     2       /* 16 register bit width */
229 #define RADIO_REGISTER_NUM      16      /* DEVICEID   ... RDSD */
230 #define RDS_REGISTER_NUM        6       /* STATUSRSSI ... RDSD */
231
232 #define DEVICEID                0       /* Device ID */
233 #define DEVICEID_PN             0xf000  /* bits 15..12: Part Number */
234 #define DEVICEID_MFGID          0x0fff  /* bits 11..00: Manufacturer ID */
235
236 #define CHIPID                  1       /* Chip ID */
237 #define CHIPID_REV              0xfc00  /* bits 15..10: Chip Version */
238 #define CHIPID_DEV              0x0200  /* bits 09..09: Device */
239 #define CHIPID_FIRMWARE         0x01ff  /* bits 08..00: Firmware Version */
240
241 #define POWERCFG                2       /* Power Configuration */
242 #define POWERCFG_DSMUTE         0x8000  /* bits 15..15: Softmute Disable */
243 #define POWERCFG_DMUTE          0x4000  /* bits 14..14: Mute Disable */
244 #define POWERCFG_MONO           0x2000  /* bits 13..13: Mono Select */
245 #define POWERCFG_RDSM           0x0800  /* bits 11..11: RDS Mode (Si4701 only) */
246 #define POWERCFG_SKMODE         0x0400  /* bits 10..10: Seek Mode */
247 #define POWERCFG_SEEKUP         0x0200  /* bits 09..09: Seek Direction */
248 #define POWERCFG_SEEK           0x0100  /* bits 08..08: Seek */
249 #define POWERCFG_DISABLE        0x0040  /* bits 06..06: Powerup Disable */
250 #define POWERCFG_ENABLE         0x0001  /* bits 00..00: Powerup Enable */
251
252 #define CHANNEL                 3       /* Channel */
253 #define CHANNEL_TUNE            0x8000  /* bits 15..15: Tune */
254 #define CHANNEL_CHAN            0x03ff  /* bits 09..00: Channel Select */
255
256 #define SYSCONFIG1              4       /* System Configuration 1 */
257 #define SYSCONFIG1_RDSIEN       0x8000  /* bits 15..15: RDS Interrupt Enable (Si4701 only) */
258 #define SYSCONFIG1_STCIEN       0x4000  /* bits 14..14: Seek/Tune Complete Interrupt Enable */
259 #define SYSCONFIG1_RDS          0x1000  /* bits 12..12: RDS Enable (Si4701 only) */
260 #define SYSCONFIG1_DE           0x0800  /* bits 11..11: De-emphasis (0=75us 1=50us) */
261 #define SYSCONFIG1_AGCD         0x0400  /* bits 10..10: AGC Disable */
262 #define SYSCONFIG1_BLNDADJ      0x00c0  /* bits 07..06: Stereo/Mono Blend Level Adjustment */
263 #define SYSCONFIG1_GPIO3        0x0030  /* bits 05..04: General Purpose I/O 3 */
264 #define SYSCONFIG1_GPIO2        0x000c  /* bits 03..02: General Purpose I/O 2 */
265 #define SYSCONFIG1_GPIO1        0x0003  /* bits 01..00: General Purpose I/O 1 */
266
267 #define SYSCONFIG2              5       /* System Configuration 2 */
268 #define SYSCONFIG2_SEEKTH       0xff00  /* bits 15..08: RSSI Seek Threshold */
269 #define SYSCONFIG2_BAND         0x0080  /* bits 07..06: Band Select */
270 #define SYSCONFIG2_SPACE        0x0030  /* bits 05..04: Channel Spacing */
271 #define SYSCONFIG2_VOLUME       0x000f  /* bits 03..00: Volume */
272
273 #define SYSCONFIG3              6       /* System Configuration 3 */
274 #define SYSCONFIG3_SMUTER       0xc000  /* bits 15..14: Softmute Attack/Recover Rate */
275 #define SYSCONFIG3_SMUTEA       0x3000  /* bits 13..12: Softmute Attenuation */
276 #define SYSCONFIG3_SKSNR        0x00f0  /* bits 07..04: Seek SNR Threshold */
277 #define SYSCONFIG3_SKCNT        0x000f  /* bits 03..00: Seek FM Impulse Detection Threshold */
278
279 #define TEST1                   7       /* Test 1 */
280 #define TEST1_AHIZEN            0x4000  /* bits 14..14: Audio High-Z Enable */
281
282 #define TEST2                   8       /* Test 2 */
283 /* TEST2 only contains reserved bits */
284
285 #define BOOTCONFIG              9       /* Boot Configuration */
286 /* BOOTCONFIG only contains reserved bits */
287
288 #define STATUSRSSI              10      /* Status RSSI */
289 #define STATUSRSSI_RDSR         0x8000  /* bits 15..15: RDS Ready (Si4701 only) */
290 #define STATUSRSSI_STC          0x4000  /* bits 14..14: Seek/Tune Complete */
291 #define STATUSRSSI_SF           0x2000  /* bits 13..13: Seek Fail/Band Limit */
292 #define STATUSRSSI_AFCRL        0x1000  /* bits 12..12: AFC Rail */
293 #define STATUSRSSI_RDSS         0x0800  /* bits 11..11: RDS Synchronized (Si4701 only) */
294 #define STATUSRSSI_BLERA        0x0600  /* bits 10..09: RDS Block A Errors (Si4701 only) */
295 #define STATUSRSSI_ST           0x0100  /* bits 08..08: Stereo Indicator */
296 #define STATUSRSSI_RSSI         0x00ff  /* bits 07..00: RSSI (Received Signal Strength Indicator) */
297
298 #define READCHAN                11      /* Read Channel */
299 #define READCHAN_BLERB          0xc000  /* bits 15..14: RDS Block D Errors (Si4701 only) */
300 #define READCHAN_BLERC          0x3000  /* bits 13..12: RDS Block C Errors (Si4701 only) */
301 #define READCHAN_BLERD          0x0c00  /* bits 11..10: RDS Block B Errors (Si4701 only) */
302 #define READCHAN_READCHAN       0x03ff  /* bits 09..00: Read Channel */
303
304 #define RDSA                    12      /* RDSA */
305 #define RDSA_RDSA               0xffff  /* bits 15..00: RDS Block A Data (Si4701 only) */
306
307 #define RDSB                    13      /* RDSB */
308 #define RDSB_RDSB               0xffff  /* bits 15..00: RDS Block B Data (Si4701 only) */
309
310 #define RDSC                    14      /* RDSC */
311 #define RDSC_RDSC               0xffff  /* bits 15..00: RDS Block C Data (Si4701 only) */
312
313 #define RDSD                    15      /* RDSD */
314 #define RDSD_RDSD               0xffff  /* bits 15..00: RDS Block D Data (Si4701 only) */
315
316
317
318 /**************************************************************************
319  * USB HID Reports
320  **************************************************************************/
321
322 /* Reports 1-16 give direct read/write access to the 16 Si470x registers */
323 /* with the (REPORT_ID - 1) corresponding to the register address across USB */
324 /* endpoint 0 using GET_REPORT and SET_REPORT */
325 #define REGISTER_REPORT_SIZE    (RADIO_REGISTER_SIZE + 1)
326 #define REGISTER_REPORT(reg)    ((reg) + 1)
327
328 /* Report 17 gives direct read/write access to the entire Si470x register */
329 /* map across endpoint 0 using GET_REPORT and SET_REPORT */
330 #define ENTIRE_REPORT_SIZE      (RADIO_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
331 #define ENTIRE_REPORT           17
332
333 /* Report 18 is used to send the lowest 6 Si470x registers up the HID */
334 /* interrupt endpoint 1 to Windows every 20 milliseconds for status */
335 #define RDS_REPORT_SIZE         (RDS_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
336 #define RDS_REPORT              18
337
338 /* Report 19: LED state */
339 #define LED_REPORT_SIZE         3
340 #define LED_REPORT              19
341
342 /* Report 19: stream */
343 #define STREAM_REPORT_SIZE      3
344 #define STREAM_REPORT           19
345
346 /* Report 20: scratch */
347 #define SCRATCH_PAGE_SIZE       63
348 #define SCRATCH_REPORT_SIZE     (SCRATCH_PAGE_SIZE + 1)
349 #define SCRATCH_REPORT          20
350
351 /* Reports 19-22: flash upgrade of the C8051F321 */
352 #define WRITE_REPORT            19
353 #define FLASH_REPORT            20
354 #define CRC_REPORT              21
355 #define RESPONSE_REPORT         22
356
357 /* Report 23: currently unused, but can accept 60 byte reports on the HID */
358 /* interrupt out endpoint 2 every 1 millisecond */
359 #define UNUSED_REPORT           23
360
361
362
363 /**************************************************************************
364  * Software/Hardware Versions
365  **************************************************************************/
366 #define RADIO_SW_VERSION_NOT_BOOTLOADABLE       6
367 #define RADIO_SW_VERSION                        7
368 #define RADIO_SW_VERSION_CURRENT                15
369 #define RADIO_HW_VERSION                        1
370
371 #define SCRATCH_PAGE_SW_VERSION 1
372 #define SCRATCH_PAGE_HW_VERSION 2
373
374
375
376 /**************************************************************************
377  * LED State Definitions
378  **************************************************************************/
379 #define LED_COMMAND             0x35
380
381 #define NO_CHANGE_LED           0x00
382 #define ALL_COLOR_LED           0x01    /* streaming state */
383 #define BLINK_GREEN_LED         0x02    /* connect state */
384 #define BLINK_RED_LED           0x04
385 #define BLINK_ORANGE_LED        0x10    /* disconnect state */
386 #define SOLID_GREEN_LED         0x20    /* tuning/seeking state */
387 #define SOLID_RED_LED           0x40    /* bootload state */
388 #define SOLID_ORANGE_LED        0x80
389
390
391
392 /**************************************************************************
393  * Stream State Definitions
394  **************************************************************************/
395 #define STREAM_COMMAND  0x36
396 #define STREAM_VIDPID   0x00
397 #define STREAM_AUDIO    0xff
398
399
400
401 /**************************************************************************
402  * Bootloader / Flash Commands
403  **************************************************************************/
404
405 /* unique id sent to bootloader and required to put into a bootload state */
406 #define UNIQUE_BL_ID            0x34
407
408 /* mask for the flash data */
409 #define FLASH_DATA_MASK         0x55
410
411 /* bootloader commands */
412 #define GET_SW_VERSION_COMMAND  0x00
413 #define SET_PAGE_COMMAND        0x01
414 #define ERASE_PAGE_COMMAND      0x02
415 #define WRITE_PAGE_COMMAND      0x03
416 #define CRC_ON_PAGE_COMMAND     0x04
417 #define READ_FLASH_BYTE_COMMAND 0x05
418 #define RESET_DEVICE_COMMAND    0x06
419 #define GET_HW_VERSION_COMMAND  0x07
420 #define BLANK                   0xff
421
422 /* bootloader command responses */
423 #define COMMAND_OK              0x01
424 #define COMMAND_FAILED          0x02
425 #define COMMAND_PENDING         0x03
426
427 /* buffer sizes */
428 #define COMMAND_BUFFER_SIZE     4
429 #define RESPONSE_BUFFER_SIZE    2
430 #define FLASH_BUFFER_SIZE       64
431 #define CRC_BUFFER_SIZE         3
432
433
434
435 /**************************************************************************
436  * General Driver Definitions
437  **************************************************************************/
438
439 /*
440  * si470x_device - private data
441  */
442 struct si470x_device {
443         /* reference to USB and video device */
444         struct usb_device *usbdev;
445         struct usb_interface *intf;
446         struct video_device *videodev;
447
448         /* driver management */
449         unsigned int users;
450         unsigned char disconnected;
451         struct mutex disconnect_lock;
452
453         /* Silabs internal registers (0..15) */
454         unsigned short registers[RADIO_REGISTER_NUM];
455
456         /* RDS receive buffer */
457         struct delayed_work work;
458         wait_queue_head_t read_queue;
459         struct mutex lock;              /* buffer locking */
460         unsigned char *buffer;          /* size is always multiple of three */
461         unsigned int buf_size;
462         unsigned int rd_index;
463         unsigned int wr_index;
464 };
465
466
467 /*
468  * The frequency is set in units of 62.5 Hz when using V4L2_TUNER_CAP_LOW,
469  * 62.5 kHz otherwise.
470  * The tuner is able to have a channel spacing of 50, 100 or 200 kHz.
471  * tuner->capability is therefore set to V4L2_TUNER_CAP_LOW
472  * The FREQ_MUL is then: 1 MHz / 62.5 Hz = 16000
473  */
474 #define FREQ_MUL (1000000 / 62.5)
475
476
477
478 /**************************************************************************
479  * General Driver Functions
480  **************************************************************************/
481
482 /*
483  * si470x_get_report - receive a HID report
484  */
485 static int si470x_get_report(struct si470x_device *radio, void *buf, int size)
486 {
487         unsigned char *report = (unsigned char *) buf;
488         int retval;
489
490         retval = usb_control_msg(radio->usbdev,
491                 usb_rcvctrlpipe(radio->usbdev, 0),
492                 HID_REQ_GET_REPORT,
493                 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
494                 report[0], 2,
495                 buf, size, usb_timeout);
496
497         if (retval < 0)
498                 printk(KERN_WARNING DRIVER_NAME
499                         ": si470x_get_report: usb_control_msg returned %d\n",
500                         retval);
501         return retval;
502 }
503
504
505 /*
506  * si470x_set_report - send a HID report
507  */
508 static int si470x_set_report(struct si470x_device *radio, void *buf, int size)
509 {
510         unsigned char *report = (unsigned char *) buf;
511         int retval;
512
513         retval = usb_control_msg(radio->usbdev,
514                 usb_sndctrlpipe(radio->usbdev, 0),
515                 HID_REQ_SET_REPORT,
516                 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
517                 report[0], 2,
518                 buf, size, usb_timeout);
519
520         if (retval < 0)
521                 printk(KERN_WARNING DRIVER_NAME
522                         ": si470x_set_report: usb_control_msg returned %d\n",
523                         retval);
524         return retval;
525 }
526
527
528 /*
529  * si470x_get_register - read register
530  */
531 static int si470x_get_register(struct si470x_device *radio, int regnr)
532 {
533         unsigned char buf[REGISTER_REPORT_SIZE];
534         int retval;
535
536         buf[0] = REGISTER_REPORT(regnr);
537
538         retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
539
540         if (retval >= 0)
541                 radio->registers[regnr] = get_unaligned_be16(&buf[1]);
542
543         return (retval < 0) ? -EINVAL : 0;
544 }
545
546
547 /*
548  * si470x_set_register - write register
549  */
550 static int si470x_set_register(struct si470x_device *radio, int regnr)
551 {
552         unsigned char buf[REGISTER_REPORT_SIZE];
553         int retval;
554
555         buf[0] = REGISTER_REPORT(regnr);
556         put_unaligned_be16(radio->registers[regnr], &buf[1]);
557
558         retval = si470x_set_report(radio, (void *) &buf, sizeof(buf));
559
560         return (retval < 0) ? -EINVAL : 0;
561 }
562
563
564 /*
565  * si470x_get_all_registers - read entire registers
566  */
567 static int si470x_get_all_registers(struct si470x_device *radio)
568 {
569         unsigned char buf[ENTIRE_REPORT_SIZE];
570         int retval;
571         unsigned char regnr;
572
573         buf[0] = ENTIRE_REPORT;
574
575         retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
576
577         if (retval >= 0)
578                 for (regnr = 0; regnr < RADIO_REGISTER_NUM; regnr++)
579                         radio->registers[regnr] = get_unaligned_be16(
580                                 &buf[regnr * RADIO_REGISTER_SIZE + 1]);
581
582         return (retval < 0) ? -EINVAL : 0;
583 }
584
585
586 /*
587  * si470x_get_rds_registers - read rds registers
588  */
589 static int si470x_get_rds_registers(struct si470x_device *radio)
590 {
591         unsigned char buf[RDS_REPORT_SIZE];
592         int retval;
593         int size;
594         unsigned char regnr;
595
596         buf[0] = RDS_REPORT;
597
598         retval = usb_interrupt_msg(radio->usbdev,
599                 usb_rcvintpipe(radio->usbdev, 1),
600                 (void *) &buf, sizeof(buf), &size, usb_timeout);
601         if (size != sizeof(buf))
602                 printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: "
603                         "return size differs: %d != %zu\n", size, sizeof(buf));
604         if (retval < 0)
605                 printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: "
606                         "usb_interrupt_msg returned %d\n", retval);
607
608         if (retval >= 0)
609                 for (regnr = 0; regnr < RDS_REGISTER_NUM; regnr++)
610                         radio->registers[STATUSRSSI + regnr] =
611                                 get_unaligned_be16(
612                                 &buf[regnr * RADIO_REGISTER_SIZE + 1]);
613
614         return (retval < 0) ? -EINVAL : 0;
615 }
616
617
618 /*
619  * si470x_set_chan - set the channel
620  */
621 static int si470x_set_chan(struct si470x_device *radio, unsigned short chan)
622 {
623         int retval;
624         unsigned long timeout;
625         bool timed_out = 0;
626
627         /* start tuning */
628         radio->registers[CHANNEL] &= ~CHANNEL_CHAN;
629         radio->registers[CHANNEL] |= CHANNEL_TUNE | chan;
630         retval = si470x_set_register(radio, CHANNEL);
631         if (retval < 0)
632                 goto done;
633
634         /* wait till tune operation has completed */
635         timeout = jiffies + msecs_to_jiffies(tune_timeout);
636         do {
637                 retval = si470x_get_register(radio, STATUSRSSI);
638                 if (retval < 0)
639                         goto stop;
640                 timed_out = time_after(jiffies, timeout);
641         } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
642                 (!timed_out));
643         if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
644                 printk(KERN_WARNING DRIVER_NAME ": tune does not complete\n");
645         if (timed_out)
646                 printk(KERN_WARNING DRIVER_NAME
647                         ": tune timed out after %u ms\n", tune_timeout);
648
649 stop:
650         /* stop tuning */
651         radio->registers[CHANNEL] &= ~CHANNEL_TUNE;
652         retval = si470x_set_register(radio, CHANNEL);
653
654 done:
655         return retval;
656 }
657
658
659 /*
660  * si470x_get_freq - get the frequency
661  */
662 static int si470x_get_freq(struct si470x_device *radio, unsigned int *freq)
663 {
664         unsigned int spacing, band_bottom;
665         unsigned short chan;
666         int retval;
667
668         /* Spacing (kHz) */
669         switch (space) {
670         /* 0: 200 kHz (USA, Australia) */
671         case 0 : spacing = 0.200 * FREQ_MUL; break;
672         /* 1: 100 kHz (Europe, Japan) */
673         case 1 : spacing = 0.100 * FREQ_MUL; break;
674         /* 2:  50 kHz */
675         default: spacing = 0.050 * FREQ_MUL; break;
676         };
677
678         /* Bottom of Band (MHz) */
679         switch (band) {
680         /* 0: 87.5 - 108 MHz (USA, Europe) */
681         case 0 : band_bottom = 87.5 * FREQ_MUL; break;
682         /* 1: 76   - 108 MHz (Japan wide band) */
683         default: band_bottom = 76   * FREQ_MUL; break;
684         /* 2: 76   -  90 MHz (Japan) */
685         case 2 : band_bottom = 76   * FREQ_MUL; break;
686         };
687
688         /* read channel */
689         retval = si470x_get_register(radio, READCHAN);
690         chan = radio->registers[READCHAN] & READCHAN_READCHAN;
691
692         /* Frequency (MHz) = Spacing (kHz) x Channel + Bottom of Band (MHz) */
693         *freq = chan * spacing + band_bottom;
694
695         return retval;
696 }
697
698
699 /*
700  * si470x_set_freq - set the frequency
701  */
702 static int si470x_set_freq(struct si470x_device *radio, unsigned int freq)
703 {
704         unsigned int spacing, band_bottom;
705         unsigned short chan;
706
707         /* Spacing (kHz) */
708         switch (space) {
709         /* 0: 200 kHz (USA, Australia) */
710         case 0 : spacing = 0.200 * FREQ_MUL; break;
711         /* 1: 100 kHz (Europe, Japan) */
712         case 1 : spacing = 0.100 * FREQ_MUL; break;
713         /* 2:  50 kHz */
714         default: spacing = 0.050 * FREQ_MUL; break;
715         };
716
717         /* Bottom of Band (MHz) */
718         switch (band) {
719         /* 0: 87.5 - 108 MHz (USA, Europe) */
720         case 0 : band_bottom = 87.5 * FREQ_MUL; break;
721         /* 1: 76   - 108 MHz (Japan wide band) */
722         default: band_bottom = 76   * FREQ_MUL; break;
723         /* 2: 76   -  90 MHz (Japan) */
724         case 2 : band_bottom = 76   * FREQ_MUL; break;
725         };
726
727         /* Chan = [ Freq (Mhz) - Bottom of Band (MHz) ] / Spacing (kHz) */
728         chan = (freq - band_bottom) / spacing;
729
730         return si470x_set_chan(radio, chan);
731 }
732
733
734 /*
735  * si470x_set_seek - set seek
736  */
737 static int si470x_set_seek(struct si470x_device *radio,
738                 unsigned int wrap_around, unsigned int seek_upward)
739 {
740         int retval = 0;
741         unsigned long timeout;
742         bool timed_out = 0;
743
744         /* start seeking */
745         radio->registers[POWERCFG] |= POWERCFG_SEEK;
746         if (wrap_around == 1)
747                 radio->registers[POWERCFG] &= ~POWERCFG_SKMODE;
748         else
749                 radio->registers[POWERCFG] |= POWERCFG_SKMODE;
750         if (seek_upward == 1)
751                 radio->registers[POWERCFG] |= POWERCFG_SEEKUP;
752         else
753                 radio->registers[POWERCFG] &= ~POWERCFG_SEEKUP;
754         retval = si470x_set_register(radio, POWERCFG);
755         if (retval < 0)
756                 goto done;
757
758         /* wait till seek operation has completed */
759         timeout = jiffies + msecs_to_jiffies(seek_timeout);
760         do {
761                 retval = si470x_get_register(radio, STATUSRSSI);
762                 if (retval < 0)
763                         goto stop;
764                 timed_out = time_after(jiffies, timeout);
765         } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
766                 (!timed_out));
767         if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
768                 printk(KERN_WARNING DRIVER_NAME ": seek does not complete\n");
769         if (radio->registers[STATUSRSSI] & STATUSRSSI_SF)
770                 printk(KERN_WARNING DRIVER_NAME
771                         ": seek failed / band limit reached\n");
772         if (timed_out)
773                 printk(KERN_WARNING DRIVER_NAME
774                         ": seek timed out after %u ms\n", seek_timeout);
775
776 stop:
777         /* stop seeking */
778         radio->registers[POWERCFG] &= ~POWERCFG_SEEK;
779         retval = si470x_set_register(radio, POWERCFG);
780
781 done:
782         /* try again, if timed out */
783         if ((retval == 0) && timed_out)
784                 retval = -EAGAIN;
785
786         return retval;
787 }
788
789
790 /*
791  * si470x_start - switch on radio
792  */
793 static int si470x_start(struct si470x_device *radio)
794 {
795         int retval;
796
797         /* powercfg */
798         radio->registers[POWERCFG] =
799                 POWERCFG_DMUTE | POWERCFG_ENABLE | POWERCFG_RDSM;
800         retval = si470x_set_register(radio, POWERCFG);
801         if (retval < 0)
802                 goto done;
803
804         /* sysconfig 1 */
805         radio->registers[SYSCONFIG1] = SYSCONFIG1_DE;
806         retval = si470x_set_register(radio, SYSCONFIG1);
807         if (retval < 0)
808                 goto done;
809
810         /* sysconfig 2 */
811         radio->registers[SYSCONFIG2] =
812                 (0x3f  << 8) |                          /* SEEKTH */
813                 ((band  << 6) & SYSCONFIG2_BAND)  |     /* BAND */
814                 ((space << 4) & SYSCONFIG2_SPACE) |     /* SPACE */
815                 15;                                     /* VOLUME (max) */
816         retval = si470x_set_register(radio, SYSCONFIG2);
817         if (retval < 0)
818                 goto done;
819
820         /* reset last channel */
821         retval = si470x_set_chan(radio,
822                 radio->registers[CHANNEL] & CHANNEL_CHAN);
823
824 done:
825         return retval;
826 }
827
828
829 /*
830  * si470x_stop - switch off radio
831  */
832 static int si470x_stop(struct si470x_device *radio)
833 {
834         int retval;
835
836         /* sysconfig 1 */
837         radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
838         retval = si470x_set_register(radio, SYSCONFIG1);
839         if (retval < 0)
840                 goto done;
841
842         /* powercfg */
843         radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
844         /* POWERCFG_ENABLE has to automatically go low */
845         radio->registers[POWERCFG] |= POWERCFG_ENABLE | POWERCFG_DISABLE;
846         retval = si470x_set_register(radio, POWERCFG);
847
848 done:
849         return retval;
850 }
851
852
853 /*
854  * si470x_rds_on - switch on rds reception
855  */
856 static int si470x_rds_on(struct si470x_device *radio)
857 {
858         int retval;
859
860         /* sysconfig 1 */
861         mutex_lock(&radio->lock);
862         radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDS;
863         retval = si470x_set_register(radio, SYSCONFIG1);
864         if (retval < 0)
865                 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
866         mutex_unlock(&radio->lock);
867
868         return retval;
869 }
870
871
872
873 /**************************************************************************
874  * RDS Driver Functions
875  **************************************************************************/
876
877 /*
878  * si470x_rds - rds processing function
879  */
880 static void si470x_rds(struct si470x_device *radio)
881 {
882         unsigned char blocknum;
883         unsigned short bler; /* rds block errors */
884         unsigned short rds;
885         unsigned char tmpbuf[3];
886
887         /* get rds blocks */
888         if (si470x_get_rds_registers(radio) < 0)
889                 return;
890         if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0) {
891                 /* No RDS group ready */
892                 return;
893         }
894         if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSS) == 0) {
895                 /* RDS decoder not synchronized */
896                 return;
897         }
898
899         /* copy all four RDS blocks to internal buffer */
900         mutex_lock(&radio->lock);
901         for (blocknum = 0; blocknum < 4; blocknum++) {
902                 switch (blocknum) {
903                 default:
904                         bler = (radio->registers[STATUSRSSI] &
905                                         STATUSRSSI_BLERA) >> 9;
906                         rds = radio->registers[RDSA];
907                         break;
908                 case 1:
909                         bler = (radio->registers[READCHAN] &
910                                         READCHAN_BLERB) >> 14;
911                         rds = radio->registers[RDSB];
912                         break;
913                 case 2:
914                         bler = (radio->registers[READCHAN] &
915                                         READCHAN_BLERC) >> 12;
916                         rds = radio->registers[RDSC];
917                         break;
918                 case 3:
919                         bler = (radio->registers[READCHAN] &
920                                         READCHAN_BLERD) >> 10;
921                         rds = radio->registers[RDSD];
922                         break;
923                 };
924
925                 /* Fill the V4L2 RDS buffer */
926                 put_unaligned_le16(rds, &tmpbuf);
927                 tmpbuf[2] = blocknum;           /* offset name */
928                 tmpbuf[2] |= blocknum << 3;     /* received offset */
929                 if (bler > max_rds_errors)
930                         tmpbuf[2] |= 0x80; /* uncorrectable errors */
931                 else if (bler > 0)
932                         tmpbuf[2] |= 0x40; /* corrected error(s) */
933
934                 /* copy RDS block to internal buffer */
935                 memcpy(&radio->buffer[radio->wr_index], &tmpbuf, 3);
936                 radio->wr_index += 3;
937
938                 /* wrap write pointer */
939                 if (radio->wr_index >= radio->buf_size)
940                         radio->wr_index = 0;
941
942                 /* check for overflow */
943                 if (radio->wr_index == radio->rd_index) {
944                         /* increment and wrap read pointer */
945                         radio->rd_index += 3;
946                         if (radio->rd_index >= radio->buf_size)
947                                 radio->rd_index = 0;
948                 }
949         }
950         mutex_unlock(&radio->lock);
951
952         /* wake up read queue */
953         if (radio->wr_index != radio->rd_index)
954                 wake_up_interruptible(&radio->read_queue);
955 }
956
957
958 /*
959  * si470x_work - rds work function
960  */
961 static void si470x_work(struct work_struct *work)
962 {
963         struct si470x_device *radio = container_of(work, struct si470x_device,
964                 work.work);
965
966         /* safety checks */
967         if (radio->disconnected)
968                 return;
969         if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
970                 return;
971
972         si470x_rds(radio);
973         schedule_delayed_work(&radio->work, msecs_to_jiffies(rds_poll_time));
974 }
975
976
977
978 /**************************************************************************
979  * File Operations Interface
980  **************************************************************************/
981
982 /*
983  * si470x_fops_read - read RDS data
984  */
985 static ssize_t si470x_fops_read(struct file *file, char __user *buf,
986                 size_t count, loff_t *ppos)
987 {
988         struct si470x_device *radio = video_get_drvdata(video_devdata(file));
989         int retval = 0;
990         unsigned int block_count = 0;
991
992         /* switch on rds reception */
993         if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
994                 si470x_rds_on(radio);
995                 schedule_delayed_work(&radio->work,
996                         msecs_to_jiffies(rds_poll_time));
997         }
998
999         /* block if no new data available */
1000         while (radio->wr_index == radio->rd_index) {
1001                 if (file->f_flags & O_NONBLOCK) {
1002                         retval = -EWOULDBLOCK;
1003                         goto done;
1004                 }
1005                 if (wait_event_interruptible(radio->read_queue,
1006                         radio->wr_index != radio->rd_index) < 0) {
1007                         retval = -EINTR;
1008                         goto done;
1009                 }
1010         }
1011
1012         /* calculate block count from byte count */
1013         count /= 3;
1014
1015         /* copy RDS block out of internal buffer and to user buffer */
1016         mutex_lock(&radio->lock);
1017         while (block_count < count) {
1018                 if (radio->rd_index == radio->wr_index)
1019                         break;
1020
1021                 /* always transfer rds complete blocks */
1022                 if (copy_to_user(buf, &radio->buffer[radio->rd_index], 3))
1023                         /* retval = -EFAULT; */
1024                         break;
1025
1026                 /* increment and wrap read pointer */
1027                 radio->rd_index += 3;
1028                 if (radio->rd_index >= radio->buf_size)
1029                         radio->rd_index = 0;
1030
1031                 /* increment counters */
1032                 block_count++;
1033                 buf += 3;
1034                 retval += 3;
1035         }
1036         mutex_unlock(&radio->lock);
1037
1038 done:
1039         return retval;
1040 }
1041
1042
1043 /*
1044  * si470x_fops_poll - poll RDS data
1045  */
1046 static unsigned int si470x_fops_poll(struct file *file,
1047                 struct poll_table_struct *pts)
1048 {
1049         struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1050         int retval = 0;
1051
1052         /* switch on rds reception */
1053         if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
1054                 si470x_rds_on(radio);
1055                 schedule_delayed_work(&radio->work,
1056                         msecs_to_jiffies(rds_poll_time));
1057         }
1058
1059         poll_wait(file, &radio->read_queue, pts);
1060
1061         if (radio->rd_index != radio->wr_index)
1062                 retval = POLLIN | POLLRDNORM;
1063
1064         return retval;
1065 }
1066
1067
1068 /*
1069  * si470x_fops_open - file open
1070  */
1071 static int si470x_fops_open(struct inode *inode, struct file *file)
1072 {
1073         struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1074         int retval;
1075
1076         radio->users++;
1077
1078         retval = usb_autopm_get_interface(radio->intf);
1079         if (retval < 0) {
1080                 radio->users--;
1081                 retval = -EIO;
1082                 goto done;
1083         }
1084
1085         if (radio->users == 1) {
1086                 retval = si470x_start(radio);
1087                 if (retval < 0)
1088                         usb_autopm_put_interface(radio->intf);
1089         }
1090
1091 done:
1092         return retval;
1093 }
1094
1095
1096 /*
1097  * si470x_fops_release - file release
1098  */
1099 static int si470x_fops_release(struct inode *inode, struct file *file)
1100 {
1101         struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1102         int retval = 0;
1103
1104         /* safety check */
1105         if (!radio) {
1106                 retval = -ENODEV;
1107                 goto done;
1108         }
1109
1110         mutex_lock(&radio->disconnect_lock);
1111         radio->users--;
1112         if (radio->users == 0) {
1113                 if (radio->disconnected) {
1114                         video_unregister_device(radio->videodev);
1115                         kfree(radio->buffer);
1116                         kfree(radio);
1117                         goto unlock;
1118                 }
1119
1120                 /* stop rds reception */
1121                 cancel_delayed_work_sync(&radio->work);
1122
1123                 /* cancel read processes */
1124                 wake_up_interruptible(&radio->read_queue);
1125
1126                 retval = si470x_stop(radio);
1127                 usb_autopm_put_interface(radio->intf);
1128         }
1129
1130 unlock:
1131         mutex_unlock(&radio->disconnect_lock);
1132
1133 done:
1134         return retval;
1135 }
1136
1137
1138 /*
1139  * si470x_fops - file operations interface
1140  */
1141 static const struct file_operations si470x_fops = {
1142         .owner          = THIS_MODULE,
1143         .llseek         = no_llseek,
1144         .read           = si470x_fops_read,
1145         .poll           = si470x_fops_poll,
1146         .ioctl          = video_ioctl2,
1147 #ifdef CONFIG_COMPAT
1148         .compat_ioctl   = v4l_compat_ioctl32,
1149 #endif
1150         .open           = si470x_fops_open,
1151         .release        = si470x_fops_release,
1152 };
1153
1154
1155
1156 /**************************************************************************
1157  * Video4Linux Interface
1158  **************************************************************************/
1159
1160 /*
1161  * si470x_v4l2_queryctrl - query control
1162  */
1163 static struct v4l2_queryctrl si470x_v4l2_queryctrl[] = {
1164 /* HINT: the disabled controls are only here to satify kradio and such apps */
1165         {
1166                 .id             = V4L2_CID_AUDIO_VOLUME,
1167                 .type           = V4L2_CTRL_TYPE_INTEGER,
1168                 .name           = "Volume",
1169                 .minimum        = 0,
1170                 .maximum        = 15,
1171                 .step           = 1,
1172                 .default_value  = 15,
1173         },
1174         {
1175                 .id             = V4L2_CID_AUDIO_BALANCE,
1176                 .flags          = V4L2_CTRL_FLAG_DISABLED,
1177         },
1178         {
1179                 .id             = V4L2_CID_AUDIO_BASS,
1180                 .flags          = V4L2_CTRL_FLAG_DISABLED,
1181         },
1182         {
1183                 .id             = V4L2_CID_AUDIO_TREBLE,
1184                 .flags          = V4L2_CTRL_FLAG_DISABLED,
1185         },
1186         {
1187                 .id             = V4L2_CID_AUDIO_MUTE,
1188                 .type           = V4L2_CTRL_TYPE_BOOLEAN,
1189                 .name           = "Mute",
1190                 .minimum        = 0,
1191                 .maximum        = 1,
1192                 .step           = 1,
1193                 .default_value  = 1,
1194         },
1195         {
1196                 .id             = V4L2_CID_AUDIO_LOUDNESS,
1197                 .flags          = V4L2_CTRL_FLAG_DISABLED,
1198         },
1199 };
1200
1201
1202 /*
1203  * si470x_vidioc_querycap - query device capabilities
1204  */
1205 static int si470x_vidioc_querycap(struct file *file, void *priv,
1206                 struct v4l2_capability *capability)
1207 {
1208         strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver));
1209         strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
1210         sprintf(capability->bus_info, "USB");
1211         capability->version = DRIVER_KERNEL_VERSION;
1212         capability->capabilities = V4L2_CAP_HW_FREQ_SEEK |
1213                 V4L2_CAP_TUNER | V4L2_CAP_RADIO;
1214
1215         return 0;
1216 }
1217
1218
1219 /*
1220  * si470x_vidioc_g_input - get input
1221  */
1222 static int si470x_vidioc_g_input(struct file *file, void *priv,
1223                 unsigned int *i)
1224 {
1225         *i = 0;
1226
1227         return 0;
1228 }
1229
1230
1231 /*
1232  * si470x_vidioc_s_input - set input
1233  */
1234 static int si470x_vidioc_s_input(struct file *file, void *priv, unsigned int i)
1235 {
1236         int retval = 0;
1237
1238         /* safety checks */
1239         if (i != 0)
1240                 retval = -EINVAL;
1241
1242         if (retval < 0)
1243                 printk(KERN_WARNING DRIVER_NAME
1244                         ": set input failed with %d\n", retval);
1245         return retval;
1246 }
1247
1248
1249 /*
1250  * si470x_vidioc_queryctrl - enumerate control items
1251  */
1252 static int si470x_vidioc_queryctrl(struct file *file, void *priv,
1253                 struct v4l2_queryctrl *qc)
1254 {
1255         unsigned char i;
1256         int retval = -EINVAL;
1257
1258         /* safety checks */
1259         if (!qc->id)
1260                 goto done;
1261
1262         for (i = 0; i < ARRAY_SIZE(si470x_v4l2_queryctrl); i++) {
1263                 if (qc->id == si470x_v4l2_queryctrl[i].id) {
1264                         memcpy(qc, &(si470x_v4l2_queryctrl[i]), sizeof(*qc));
1265                         retval = 0;
1266                         break;
1267                 }
1268         }
1269
1270 done:
1271         if (retval < 0)
1272                 printk(KERN_WARNING DRIVER_NAME
1273                         ": query controls failed with %d\n", retval);
1274         return retval;
1275 }
1276
1277
1278 /*
1279  * si470x_vidioc_g_ctrl - get the value of a control
1280  */
1281 static int si470x_vidioc_g_ctrl(struct file *file, void *priv,
1282                 struct v4l2_control *ctrl)
1283 {
1284         struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1285         int retval = 0;
1286
1287         /* safety checks */
1288         if (radio->disconnected) {
1289                 retval = -EIO;
1290                 goto done;
1291         }
1292
1293         switch (ctrl->id) {
1294         case V4L2_CID_AUDIO_VOLUME:
1295                 ctrl->value = radio->registers[SYSCONFIG2] &
1296                                 SYSCONFIG2_VOLUME;
1297                 break;
1298         case V4L2_CID_AUDIO_MUTE:
1299                 ctrl->value = ((radio->registers[POWERCFG] &
1300                                 POWERCFG_DMUTE) == 0) ? 1 : 0;
1301                 break;
1302         default:
1303                 retval = -EINVAL;
1304         }
1305
1306 done:
1307         if (retval < 0)
1308                 printk(KERN_WARNING DRIVER_NAME
1309                         ": get control failed with %d\n", retval);
1310         return retval;
1311 }
1312
1313
1314 /*
1315  * si470x_vidioc_s_ctrl - set the value of a control
1316  */
1317 static int si470x_vidioc_s_ctrl(struct file *file, void *priv,
1318                 struct v4l2_control *ctrl)
1319 {
1320         struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1321         int retval = 0;
1322
1323         /* safety checks */
1324         if (radio->disconnected) {
1325                 retval = -EIO;
1326                 goto done;
1327         }
1328
1329         switch (ctrl->id) {
1330         case V4L2_CID_AUDIO_VOLUME:
1331                 radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_VOLUME;
1332                 radio->registers[SYSCONFIG2] |= ctrl->value;
1333                 retval = si470x_set_register(radio, SYSCONFIG2);
1334                 break;
1335         case V4L2_CID_AUDIO_MUTE:
1336                 if (ctrl->value == 1)
1337                         radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
1338                 else
1339                         radio->registers[POWERCFG] |= POWERCFG_DMUTE;
1340                 retval = si470x_set_register(radio, POWERCFG);
1341                 break;
1342         default:
1343                 retval = -EINVAL;
1344         }
1345
1346 done:
1347         if (retval < 0)
1348                 printk(KERN_WARNING DRIVER_NAME
1349                         ": set control failed with %d\n", retval);
1350         return retval;
1351 }
1352
1353
1354 /*
1355  * si470x_vidioc_g_audio - get audio attributes
1356  */
1357 static int si470x_vidioc_g_audio(struct file *file, void *priv,
1358                 struct v4l2_audio *audio)
1359 {
1360         int retval = 0;
1361
1362         /* safety checks */
1363         if (audio->index != 0) {
1364                 retval = -EINVAL;
1365                 goto done;
1366         }
1367
1368         strcpy(audio->name, "Radio");
1369         audio->capability = V4L2_AUDCAP_STEREO;
1370
1371 done:
1372         if (retval < 0)
1373                 printk(KERN_WARNING DRIVER_NAME
1374                         ": get audio failed with %d\n", retval);
1375         return retval;
1376 }
1377
1378
1379 /*
1380  * si470x_vidioc_s_audio - set audio attributes
1381  */
1382 static int si470x_vidioc_s_audio(struct file *file, void *priv,
1383                 struct v4l2_audio *audio)
1384 {
1385         int retval = 0;
1386
1387         /* safety checks */
1388         if (audio->index != 0) {
1389                 retval = -EINVAL;
1390                 goto done;
1391         }
1392
1393 done:
1394         if (retval < 0)
1395                 printk(KERN_WARNING DRIVER_NAME
1396                         ": set audio failed with %d\n", retval);
1397         return retval;
1398 }
1399
1400
1401 /*
1402  * si470x_vidioc_g_tuner - get tuner attributes
1403  */
1404 static int si470x_vidioc_g_tuner(struct file *file, void *priv,
1405                 struct v4l2_tuner *tuner)
1406 {
1407         struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1408         int retval = 0;
1409
1410         /* safety checks */
1411         if (radio->disconnected) {
1412                 retval = -EIO;
1413                 goto done;
1414         }
1415         if ((tuner->index != 0) && (tuner->type != V4L2_TUNER_RADIO)) {
1416                 retval = -EINVAL;
1417                 goto done;
1418         }
1419
1420         retval = si470x_get_register(radio, STATUSRSSI);
1421         if (retval < 0)
1422                 goto done;
1423
1424         strcpy(tuner->name, "FM");
1425         switch (band) {
1426         /* 0: 87.5 - 108 MHz (USA, Europe, default) */
1427         default:
1428                 tuner->rangelow  =  87.5 * FREQ_MUL;
1429                 tuner->rangehigh = 108   * FREQ_MUL;
1430                 break;
1431         /* 1: 76   - 108 MHz (Japan wide band) */
1432         case 1 :
1433                 tuner->rangelow  =  76   * FREQ_MUL;
1434                 tuner->rangehigh = 108   * FREQ_MUL;
1435                 break;
1436         /* 2: 76   -  90 MHz (Japan) */
1437         case 2 :
1438                 tuner->rangelow  =  76   * FREQ_MUL;
1439                 tuner->rangehigh =  90   * FREQ_MUL;
1440                 break;
1441         };
1442         tuner->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
1443         tuner->capability = V4L2_TUNER_CAP_LOW;
1444
1445         /* Stereo indicator == Stereo (instead of Mono) */
1446         if ((radio->registers[STATUSRSSI] & STATUSRSSI_ST) == 1)
1447                 tuner->audmode = V4L2_TUNER_MODE_STEREO;
1448         else
1449                 tuner->audmode = V4L2_TUNER_MODE_MONO;
1450
1451         /* min is worst, max is best; signal:0..0xffff; rssi: 0..0xff */
1452         tuner->signal = (radio->registers[STATUSRSSI] & STATUSRSSI_RSSI)
1453                                 * 0x0101;
1454
1455         /* automatic frequency control: -1: freq to low, 1 freq to high */
1456         /* AFCRL does only indicate that freq. differs, not if too low/high */
1457         tuner->afc = (radio->registers[STATUSRSSI] & STATUSRSSI_AFCRL) ? 1 : 0;
1458
1459 done:
1460         if (retval < 0)
1461                 printk(KERN_WARNING DRIVER_NAME
1462                         ": get tuner failed with %d\n", retval);
1463         return retval;
1464 }
1465
1466
1467 /*
1468  * si470x_vidioc_s_tuner - set tuner attributes
1469  */
1470 static int si470x_vidioc_s_tuner(struct file *file, void *priv,
1471                 struct v4l2_tuner *tuner)
1472 {
1473         struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1474         int retval = 0;
1475
1476         /* safety checks */
1477         if (radio->disconnected) {
1478                 retval = -EIO;
1479                 goto done;
1480         }
1481         if ((tuner->index != 0) && (tuner->type != V4L2_TUNER_RADIO)) {
1482                 retval = -EINVAL;
1483                 goto done;
1484         }
1485
1486         if (tuner->audmode == V4L2_TUNER_MODE_MONO)
1487                 radio->registers[POWERCFG] |= POWERCFG_MONO;  /* force mono */
1488         else
1489                 radio->registers[POWERCFG] &= ~POWERCFG_MONO; /* try stereo */
1490
1491         retval = si470x_set_register(radio, POWERCFG);
1492
1493 done:
1494         if (retval < 0)
1495                 printk(KERN_WARNING DRIVER_NAME
1496                         ": set tuner failed with %d\n", retval);
1497         return retval;
1498 }
1499
1500
1501 /*
1502  * si470x_vidioc_g_frequency - get tuner or modulator radio frequency
1503  */
1504 static int si470x_vidioc_g_frequency(struct file *file, void *priv,
1505                 struct v4l2_frequency *freq)
1506 {
1507         struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1508         int retval = 0;
1509
1510         /* safety checks */
1511         if (radio->disconnected) {
1512                 retval = -EIO;
1513                 goto done;
1514         }
1515         if ((freq->tuner != 0) && (freq->type != V4L2_TUNER_RADIO)) {
1516                 retval = -EINVAL;
1517                 goto done;
1518         }
1519
1520         retval = si470x_get_freq(radio, &freq->frequency);
1521
1522 done:
1523         if (retval < 0)
1524                 printk(KERN_WARNING DRIVER_NAME
1525                         ": get frequency failed with %d\n", retval);
1526         return retval;
1527 }
1528
1529
1530 /*
1531  * si470x_vidioc_s_frequency - set tuner or modulator radio frequency
1532  */
1533 static int si470x_vidioc_s_frequency(struct file *file, void *priv,
1534                 struct v4l2_frequency *freq)
1535 {
1536         struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1537         int retval = 0;
1538
1539         /* safety checks */
1540         if (radio->disconnected) {
1541                 retval = -EIO;
1542                 goto done;
1543         }
1544         if ((freq->tuner != 0) && (freq->type != V4L2_TUNER_RADIO)) {
1545                 retval = -EINVAL;
1546                 goto done;
1547         }
1548
1549         retval = si470x_set_freq(radio, freq->frequency);
1550
1551 done:
1552         if (retval < 0)
1553                 printk(KERN_WARNING DRIVER_NAME
1554                         ": set frequency failed with %d\n", retval);
1555         return retval;
1556 }
1557
1558
1559 /*
1560  * si470x_vidioc_s_hw_freq_seek - set hardware frequency seek
1561  */
1562 static int si470x_vidioc_s_hw_freq_seek(struct file *file, void *priv,
1563                 struct v4l2_hw_freq_seek *seek)
1564 {
1565         struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1566         int retval = 0;
1567
1568         /* safety checks */
1569         if (radio->disconnected) {
1570                 retval = -EIO;
1571                 goto done;
1572         }
1573         if ((seek->tuner != 0) && (seek->type != V4L2_TUNER_RADIO)) {
1574                 retval = -EINVAL;
1575                 goto done;
1576         }
1577
1578         retval = si470x_set_seek(radio, seek->wrap_around, seek->seek_upward);
1579
1580 done:
1581         if (retval < 0)
1582                 printk(KERN_WARNING DRIVER_NAME
1583                         ": set hardware frequency seek failed with %d\n",
1584                         retval);
1585         return retval;
1586 }
1587
1588
1589 /*
1590  * si470x_viddev_tamples - video device interface
1591  */
1592 static struct video_device si470x_viddev_template = {
1593         .fops                   = &si470x_fops,
1594         .name                   = DRIVER_NAME,
1595         .type                   = VID_TYPE_TUNER,
1596         .release                = video_device_release,
1597         .vidioc_querycap        = si470x_vidioc_querycap,
1598         .vidioc_g_input         = si470x_vidioc_g_input,
1599         .vidioc_s_input         = si470x_vidioc_s_input,
1600         .vidioc_queryctrl       = si470x_vidioc_queryctrl,
1601         .vidioc_g_ctrl          = si470x_vidioc_g_ctrl,
1602         .vidioc_s_ctrl          = si470x_vidioc_s_ctrl,
1603         .vidioc_g_audio         = si470x_vidioc_g_audio,
1604         .vidioc_s_audio         = si470x_vidioc_s_audio,
1605         .vidioc_g_tuner         = si470x_vidioc_g_tuner,
1606         .vidioc_s_tuner         = si470x_vidioc_s_tuner,
1607         .vidioc_g_frequency     = si470x_vidioc_g_frequency,
1608         .vidioc_s_frequency     = si470x_vidioc_s_frequency,
1609         .vidioc_s_hw_freq_seek  = si470x_vidioc_s_hw_freq_seek,
1610         .owner                  = THIS_MODULE,
1611 };
1612
1613
1614
1615 /**************************************************************************
1616  * USB Interface
1617  **************************************************************************/
1618
1619 /*
1620  * si470x_usb_driver_probe - probe for the device
1621  */
1622 static int si470x_usb_driver_probe(struct usb_interface *intf,
1623                 const struct usb_device_id *id)
1624 {
1625         struct si470x_device *radio;
1626         int retval = 0;
1627
1628         /* private data allocation and initialization */
1629         radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL);
1630         if (!radio) {
1631                 retval = -ENOMEM;
1632                 goto err_initial;
1633         }
1634         radio->users = 0;
1635         radio->disconnected = 0;
1636         radio->usbdev = interface_to_usbdev(intf);
1637         radio->intf = intf;
1638         mutex_init(&radio->disconnect_lock);
1639         mutex_init(&radio->lock);
1640
1641         /* video device allocation and initialization */
1642         radio->videodev = video_device_alloc();
1643         if (!radio->videodev) {
1644                 retval = -ENOMEM;
1645                 goto err_radio;
1646         }
1647         memcpy(radio->videodev, &si470x_viddev_template,
1648                         sizeof(si470x_viddev_template));
1649         video_set_drvdata(radio->videodev, radio);
1650
1651         /* show some infos about the specific device */
1652         if (si470x_get_all_registers(radio) < 0) {
1653                 retval = -EIO;
1654                 goto err_all;
1655         }
1656         printk(KERN_INFO DRIVER_NAME ": DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",
1657                         radio->registers[DEVICEID], radio->registers[CHIPID]);
1658
1659         /* check if firmware is current */
1660         if ((radio->registers[CHIPID] & CHIPID_FIRMWARE)
1661                         < RADIO_SW_VERSION_CURRENT) {
1662                 printk(KERN_WARNING DRIVER_NAME
1663                         ": This driver is known to work with "
1664                         "firmware version %hu,\n", RADIO_SW_VERSION_CURRENT);
1665                 printk(KERN_WARNING DRIVER_NAME
1666                         ": but the device has firmware version %hu.\n",
1667                         radio->registers[CHIPID] & CHIPID_FIRMWARE);
1668                 printk(KERN_WARNING DRIVER_NAME
1669                         ": If you have some trouble using this driver,\n");
1670                 printk(KERN_WARNING DRIVER_NAME
1671                         ": please report to V4L ML at "
1672                         "video4linux-list@redhat.com\n");
1673         }
1674
1675         /* set initial frequency */
1676         si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
1677
1678         /* rds buffer allocation */
1679         radio->buf_size = rds_buf * 3;
1680         radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
1681         if (!radio->buffer) {
1682                 retval = -EIO;
1683                 goto err_all;
1684         }
1685
1686         /* rds buffer configuration */
1687         radio->wr_index = 0;
1688         radio->rd_index = 0;
1689         init_waitqueue_head(&radio->read_queue);
1690
1691         /* prepare rds work function */
1692         INIT_DELAYED_WORK(&radio->work, si470x_work);
1693
1694         /* register video device */
1695         if (video_register_device(radio->videodev, VFL_TYPE_RADIO, radio_nr)) {
1696                 retval = -EIO;
1697                 printk(KERN_WARNING DRIVER_NAME
1698                                 ": Could not register video device\n");
1699                 goto err_all;
1700         }
1701         usb_set_intfdata(intf, radio);
1702
1703         return 0;
1704 err_all:
1705         video_device_release(radio->videodev);
1706         kfree(radio->buffer);
1707 err_radio:
1708         kfree(radio);
1709 err_initial:
1710         return retval;
1711 }
1712
1713
1714 /*
1715  * si470x_usb_driver_suspend - suspend the device
1716  */
1717 static int si470x_usb_driver_suspend(struct usb_interface *intf,
1718                 pm_message_t message)
1719 {
1720         struct si470x_device *radio = usb_get_intfdata(intf);
1721
1722         printk(KERN_INFO DRIVER_NAME ": suspending now...\n");
1723
1724         cancel_delayed_work_sync(&radio->work);
1725
1726         return 0;
1727 }
1728
1729
1730 /*
1731  * si470x_usb_driver_resume - resume the device
1732  */
1733 static int si470x_usb_driver_resume(struct usb_interface *intf)
1734 {
1735         struct si470x_device *radio = usb_get_intfdata(intf);
1736
1737         printk(KERN_INFO DRIVER_NAME ": resuming now...\n");
1738
1739         mutex_lock(&radio->lock);
1740         if (radio->users && radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS)
1741                 schedule_delayed_work(&radio->work,
1742                         msecs_to_jiffies(rds_poll_time));
1743         mutex_unlock(&radio->lock);
1744
1745         return 0;
1746 }
1747
1748
1749 /*
1750  * si470x_usb_driver_disconnect - disconnect the device
1751  */
1752 static void si470x_usb_driver_disconnect(struct usb_interface *intf)
1753 {
1754         struct si470x_device *radio = usb_get_intfdata(intf);
1755
1756         mutex_lock(&radio->disconnect_lock);
1757         radio->disconnected = 1;
1758         cancel_delayed_work_sync(&radio->work);
1759         usb_set_intfdata(intf, NULL);
1760         if (radio->users == 0) {
1761                 video_unregister_device(radio->videodev);
1762                 kfree(radio->buffer);
1763                 kfree(radio);
1764         }
1765         mutex_unlock(&radio->disconnect_lock);
1766 }
1767
1768
1769 /*
1770  * si470x_usb_driver - usb driver interface
1771  */
1772 static struct usb_driver si470x_usb_driver = {
1773         .name                   = DRIVER_NAME,
1774         .probe                  = si470x_usb_driver_probe,
1775         .disconnect             = si470x_usb_driver_disconnect,
1776         .suspend                = si470x_usb_driver_suspend,
1777         .resume                 = si470x_usb_driver_resume,
1778         .id_table               = si470x_usb_driver_id_table,
1779         .supports_autosuspend   = 1,
1780 };
1781
1782
1783
1784 /**************************************************************************
1785  * Module Interface
1786  **************************************************************************/
1787
1788 /*
1789  * si470x_module_init - module init
1790  */
1791 static int __init si470x_module_init(void)
1792 {
1793         printk(KERN_INFO DRIVER_DESC ", Version " DRIVER_VERSION "\n");
1794         return usb_register(&si470x_usb_driver);
1795 }
1796
1797
1798 /*
1799  * si470x_module_exit - module exit
1800  */
1801 static void __exit si470x_module_exit(void)
1802 {
1803         usb_deregister(&si470x_usb_driver);
1804 }
1805
1806
1807 module_init(si470x_module_init);
1808 module_exit(si470x_module_exit);
1809
1810 MODULE_LICENSE("GPL");
1811 MODULE_AUTHOR(DRIVER_AUTHOR);
1812 MODULE_DESCRIPTION(DRIVER_DESC);
1813 MODULE_VERSION(DRIVER_VERSION);