V4L/DVB (7939): pvrusb2: Remove sysfs interface hackery
[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  * History:
28  * 2008-01-12   Tobias Lorenz <tobias.lorenz@gmx.net>
29  *              Version 1.0.0
30  *              - First working version
31  * 2008-01-13   Tobias Lorenz <tobias.lorenz@gmx.net>
32  *              Version 1.0.1
33  *              - Improved error handling, every function now returns errno
34  *              - Improved multi user access (start/mute/stop)
35  *              - Channel doesn't get lost anymore after start/mute/stop
36  *              - RDS support added (polling mode via interrupt EP 1)
37  *              - marked default module parameters with *value*
38  *              - switched from bit structs to bit masks
39  *              - header file cleaned and integrated
40  * 2008-01-14   Tobias Lorenz <tobias.lorenz@gmx.net>
41  *              Version 1.0.2
42  *              - hex values are now lower case
43  *              - commented USB ID for ADS/Tech moved on todo list
44  *              - blacklisted si470x in hid-quirks.c
45  *              - rds buffer handling functions integrated into *_work, *_read
46  *              - rds_command in si470x_poll exchanged against simple retval
47  *              - check for firmware version 15
48  *              - code order and prototypes still remain the same
49  *              - spacing and bottom of band codes remain the same
50  * 2008-01-16   Tobias Lorenz <tobias.lorenz@gmx.net>
51  *              Version 1.0.3
52  *              - code reordered to avoid function prototypes
53  *              - switch/case defaults are now more user-friendly
54  *              - unified comment style
55  *              - applied all checkpatch.pl v1.12 suggestions
56  *                except the warning about the too long lines with bit comments
57  *              - renamed FMRADIO to RADIO to cut line length (checkpatch.pl)
58  * 2008-01-22   Tobias Lorenz <tobias.lorenz@gmx.net>
59  *              Version 1.0.4
60  *              - avoid poss. locking when doing copy_to_user which may sleep
61  *              - RDS is automatically activated on read now
62  *              - code cleaned of unnecessary rds_commands
63  *              - USB Vendor/Product ID for ADS/Tech FM Radio Receiver verified
64  *                (thanks to Guillaume RAMOUSSE)
65  * 2008-01-27   Tobias Lorenz <tobias.lorenz@gmx.net>
66  *              Version 1.0.5
67  *              - number of seek_retries changed to tune_timeout
68  *              - fixed problem with incomplete tune operations by own buffers
69  *              - optimization of variables and printf types
70  *              - improved error logging
71  * 2008-01-31   Tobias Lorenz <tobias.lorenz@gmx.net>
72  *              Oliver Neukum <oliver@neukum.org>
73  *              Version 1.0.6
74  *              - fixed coverity checker warnings in *_usb_driver_disconnect
75  *              - probe()/open() race by correct ordering in probe()
76  *              - DMA coherency rules by separate allocation of all buffers
77  *              - use of endianness macros
78  *              - abuse of spinlock, replaced by mutex
79  *              - racy handling of timer in disconnect,
80  *                replaced by delayed_work
81  *              - racy interruptible_sleep_on(),
82  *                replaced with wait_event_interruptible()
83  *              - handle signals in read()
84  * 2008-02-08   Tobias Lorenz <tobias.lorenz@gmx.net>
85  *              Oliver Neukum <oliver@neukum.org>
86  *              Version 1.0.7
87  *              - usb autosuspend support
88  *             - unplugging fixed
89  *
90  * ToDo:
91  * - add seeking support
92  * - add firmware download/update support
93  * - RDS support: interrupt mode, instead of polling
94  * - add LED status output (check if that's not already done in firmware)
95  */
96
97
98 /* driver definitions */
99 #define DRIVER_AUTHOR "Tobias Lorenz <tobias.lorenz@gmx.net>"
100 #define DRIVER_NAME "radio-si470x"
101 #define DRIVER_KERNEL_VERSION KERNEL_VERSION(1, 0, 7)
102 #define DRIVER_CARD "Silicon Labs Si470x FM Radio Receiver"
103 #define DRIVER_DESC "USB radio driver for Si470x FM Radio Receivers"
104 #define DRIVER_VERSION "1.0.7"
105
106
107 /* kernel includes */
108 #include <linux/kernel.h>
109 #include <linux/module.h>
110 #include <linux/init.h>
111 #include <linux/slab.h>
112 #include <linux/input.h>
113 #include <linux/usb.h>
114 #include <linux/hid.h>
115 #include <linux/version.h>
116 #include <linux/videodev2.h>
117 #include <linux/mutex.h>
118 #include <media/v4l2-common.h>
119 #include <media/rds.h>
120 #include <asm/unaligned.h>
121
122
123 /* USB Device ID List */
124 static struct usb_device_id si470x_usb_driver_id_table[] = {
125         /* Silicon Labs USB FM Radio Reference Design */
126         { USB_DEVICE_AND_INTERFACE_INFO(0x10c4, 0x818a, USB_CLASS_HID, 0, 0) },
127         /* ADS/Tech FM Radio Receiver (formerly Instant FM Music) */
128         { USB_DEVICE_AND_INTERFACE_INFO(0x06e1, 0xa155, USB_CLASS_HID, 0, 0) },
129         /* Terminating entry */
130         { }
131 };
132 MODULE_DEVICE_TABLE(usb, si470x_usb_driver_id_table);
133
134
135
136 /**************************************************************************
137  * Module Parameters
138  **************************************************************************/
139
140 /* Radio Nr */
141 static int radio_nr = -1;
142 module_param(radio_nr, int, 0);
143 MODULE_PARM_DESC(radio_nr, "Radio Nr");
144
145 /* Spacing (kHz) */
146 /* 0: 200 kHz (USA, Australia) */
147 /* 1: 100 kHz (Europe, Japan) */
148 /* 2:  50 kHz */
149 static unsigned short space = 2;
150 module_param(space, ushort, 0);
151 MODULE_PARM_DESC(radio_nr, "Spacing: 0=200kHz 1=100kHz *2=50kHz*");
152
153 /* Bottom of Band (MHz) */
154 /* 0: 87.5 - 108 MHz (USA, Europe)*/
155 /* 1: 76   - 108 MHz (Japan wide band) */
156 /* 2: 76   -  90 MHz (Japan) */
157 static unsigned short band = 1;
158 module_param(band, ushort, 0);
159 MODULE_PARM_DESC(radio_nr, "Band: 0=87.5..108MHz *1=76..108MHz* 2=76..90MHz");
160
161 /* De-emphasis */
162 /* 0: 75 us (USA) */
163 /* 1: 50 us (Europe, Australia, Japan) */
164 static unsigned short de = 1;
165 module_param(de, ushort, 0);
166 MODULE_PARM_DESC(radio_nr, "De-emphasis: 0=75us *1=50us*");
167
168 /* USB timeout */
169 static unsigned int usb_timeout = 500;
170 module_param(usb_timeout, uint, 0);
171 MODULE_PARM_DESC(usb_timeout, "USB timeout (ms): *500*");
172
173 /* Tune timeout */
174 static unsigned int tune_timeout = 3000;
175 module_param(tune_timeout, uint, 0);
176 MODULE_PARM_DESC(tune_timeout, "Tune timeout: *3000*");
177
178 /* RDS buffer blocks */
179 static unsigned int rds_buf = 100;
180 module_param(rds_buf, uint, 0);
181 MODULE_PARM_DESC(rds_buf, "RDS buffer entries: *100*");
182
183 /* RDS maximum block errors */
184 static unsigned short max_rds_errors = 1;
185 /* 0 means   0  errors requiring correction */
186 /* 1 means 1-2  errors requiring correction (used by original USBRadio.exe) */
187 /* 2 means 3-5  errors requiring correction */
188 /* 3 means   6+ errors or errors in checkword, correction not possible */
189 module_param(max_rds_errors, ushort, 0);
190 MODULE_PARM_DESC(max_rds_errors, "RDS maximum block errors: *1*");
191
192 /* RDS poll frequency */
193 static unsigned int rds_poll_time = 40;
194 /* 40 is used by the original USBRadio.exe */
195 /* 50 is used by radio-cadet */
196 /* 75 should be okay */
197 /* 80 is the usual RDS receive interval */
198 module_param(rds_poll_time, uint, 0);
199 MODULE_PARM_DESC(rds_poll_time, "RDS poll time (ms): *40*");
200
201
202
203 /**************************************************************************
204  * Register Definitions
205  **************************************************************************/
206 #define RADIO_REGISTER_SIZE     2       /* 16 register bit width */
207 #define RADIO_REGISTER_NUM      16      /* DEVICEID   ... RDSD */
208 #define RDS_REGISTER_NUM        6       /* STATUSRSSI ... RDSD */
209
210 #define DEVICEID                0       /* Device ID */
211 #define DEVICEID_PN             0xf000  /* bits 15..12: Part Number */
212 #define DEVICEID_MFGID          0x0fff  /* bits 11..00: Manufacturer ID */
213
214 #define CHIPID                  1       /* Chip ID */
215 #define CHIPID_REV              0xfc00  /* bits 15..10: Chip Version */
216 #define CHIPID_DEV              0x0200  /* bits 09..09: Device */
217 #define CHIPID_FIRMWARE         0x01ff  /* bits 08..00: Firmware Version */
218
219 #define POWERCFG                2       /* Power Configuration */
220 #define POWERCFG_DSMUTE         0x8000  /* bits 15..15: Softmute Disable */
221 #define POWERCFG_DMUTE          0x4000  /* bits 14..14: Mute Disable */
222 #define POWERCFG_MONO           0x2000  /* bits 13..13: Mono Select */
223 #define POWERCFG_RDSM           0x0800  /* bits 11..11: RDS Mode (Si4701 only) */
224 #define POWERCFG_SKMODE         0x0400  /* bits 10..10: Seek Mode */
225 #define POWERCFG_SEEKUP         0x0200  /* bits 09..09: Seek Direction */
226 #define POWERCFG_SEEK           0x0100  /* bits 08..08: Seek */
227 #define POWERCFG_DISABLE        0x0040  /* bits 06..06: Powerup Disable */
228 #define POWERCFG_ENABLE         0x0001  /* bits 00..00: Powerup Enable */
229
230 #define CHANNEL                 3       /* Channel */
231 #define CHANNEL_TUNE            0x8000  /* bits 15..15: Tune */
232 #define CHANNEL_CHAN            0x03ff  /* bits 09..00: Channel Select */
233
234 #define SYSCONFIG1              4       /* System Configuration 1 */
235 #define SYSCONFIG1_RDSIEN       0x8000  /* bits 15..15: RDS Interrupt Enable (Si4701 only) */
236 #define SYSCONFIG1_STCIEN       0x4000  /* bits 14..14: Seek/Tune Complete Interrupt Enable */
237 #define SYSCONFIG1_RDS          0x1000  /* bits 12..12: RDS Enable (Si4701 only) */
238 #define SYSCONFIG1_DE           0x0800  /* bits 11..11: De-emphasis (0=75us 1=50us) */
239 #define SYSCONFIG1_AGCD         0x0400  /* bits 10..10: AGC Disable */
240 #define SYSCONFIG1_BLNDADJ      0x00c0  /* bits 07..06: Stereo/Mono Blend Level Adjustment */
241 #define SYSCONFIG1_GPIO3        0x0030  /* bits 05..04: General Purpose I/O 3 */
242 #define SYSCONFIG1_GPIO2        0x000c  /* bits 03..02: General Purpose I/O 2 */
243 #define SYSCONFIG1_GPIO1        0x0003  /* bits 01..00: General Purpose I/O 1 */
244
245 #define SYSCONFIG2              5       /* System Configuration 2 */
246 #define SYSCONFIG2_SEEKTH       0xff00  /* bits 15..08: RSSI Seek Threshold */
247 #define SYSCONFIG2_BAND         0x0080  /* bits 07..06: Band Select */
248 #define SYSCONFIG2_SPACE        0x0030  /* bits 05..04: Channel Spacing */
249 #define SYSCONFIG2_VOLUME       0x000f  /* bits 03..00: Volume */
250
251 #define SYSCONFIG3              6       /* System Configuration 3 */
252 #define SYSCONFIG3_SMUTER       0xc000  /* bits 15..14: Softmute Attack/Recover Rate */
253 #define SYSCONFIG3_SMUTEA       0x3000  /* bits 13..12: Softmute Attenuation */
254 #define SYSCONFIG3_SKSNR        0x00f0  /* bits 07..04: Seek SNR Threshold */
255 #define SYSCONFIG3_SKCNT        0x000f  /* bits 03..00: Seek FM Impulse Detection Threshold */
256
257 #define TEST1                   7       /* Test 1 */
258 #define TEST1_AHIZEN            0x4000  /* bits 14..14: Audio High-Z Enable */
259
260 #define TEST2                   8       /* Test 2 */
261 /* TEST2 only contains reserved bits */
262
263 #define BOOTCONFIG              9       /* Boot Configuration */
264 /* BOOTCONFIG only contains reserved bits */
265
266 #define STATUSRSSI              10      /* Status RSSI */
267 #define STATUSRSSI_RDSR         0x8000  /* bits 15..15: RDS Ready (Si4701 only) */
268 #define STATUSRSSI_STC          0x4000  /* bits 14..14: Seek/Tune Complete */
269 #define STATUSRSSI_SF           0x2000  /* bits 13..13: Seek Fail/Band Limit */
270 #define STATUSRSSI_AFCRL        0x1000  /* bits 12..12: AFC Rail */
271 #define STATUSRSSI_RDSS         0x0800  /* bits 11..11: RDS Synchronized (Si4701 only) */
272 #define STATUSRSSI_BLERA        0x0600  /* bits 10..09: RDS Block A Errors (Si4701 only) */
273 #define STATUSRSSI_ST           0x0100  /* bits 08..08: Stereo Indicator */
274 #define STATUSRSSI_RSSI         0x00ff  /* bits 07..00: RSSI (Received Signal Strength Indicator) */
275
276 #define READCHAN                11      /* Read Channel */
277 #define READCHAN_BLERB          0xc000  /* bits 15..14: RDS Block D Errors (Si4701 only) */
278 #define READCHAN_BLERC          0x3000  /* bits 13..12: RDS Block C Errors (Si4701 only) */
279 #define READCHAN_BLERD          0x0c00  /* bits 11..10: RDS Block B Errors (Si4701 only) */
280 #define READCHAN_READCHAN       0x03ff  /* bits 09..00: Read Channel */
281
282 #define RDSA                    12      /* RDSA */
283 #define RDSA_RDSA               0xffff  /* bits 15..00: RDS Block A Data (Si4701 only) */
284
285 #define RDSB                    13      /* RDSB */
286 #define RDSB_RDSB               0xffff  /* bits 15..00: RDS Block B Data (Si4701 only) */
287
288 #define RDSC                    14      /* RDSC */
289 #define RDSC_RDSC               0xffff  /* bits 15..00: RDS Block C Data (Si4701 only) */
290
291 #define RDSD                    15      /* RDSD */
292 #define RDSD_RDSD               0xffff  /* bits 15..00: RDS Block D Data (Si4701 only) */
293
294
295
296 /**************************************************************************
297  * USB HID Reports
298  **************************************************************************/
299
300 /* Reports 1-16 give direct read/write access to the 16 Si470x registers */
301 /* with the (REPORT_ID - 1) corresponding to the register address across USB */
302 /* endpoint 0 using GET_REPORT and SET_REPORT */
303 #define REGISTER_REPORT_SIZE    (RADIO_REGISTER_SIZE + 1)
304 #define REGISTER_REPORT(reg)    ((reg) + 1)
305
306 /* Report 17 gives direct read/write access to the entire Si470x register */
307 /* map across endpoint 0 using GET_REPORT and SET_REPORT */
308 #define ENTIRE_REPORT_SIZE      (RADIO_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
309 #define ENTIRE_REPORT           17
310
311 /* Report 18 is used to send the lowest 6 Si470x registers up the HID */
312 /* interrupt endpoint 1 to Windows every 20 milliseconds for status */
313 #define RDS_REPORT_SIZE         (RDS_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
314 #define RDS_REPORT              18
315
316 /* Report 19: LED state */
317 #define LED_REPORT_SIZE         3
318 #define LED_REPORT              19
319
320 /* Report 19: stream */
321 #define STREAM_REPORT_SIZE      3
322 #define STREAM_REPORT           19
323
324 /* Report 20: scratch */
325 #define SCRATCH_PAGE_SIZE       63
326 #define SCRATCH_REPORT_SIZE     (SCRATCH_PAGE_SIZE + 1)
327 #define SCRATCH_REPORT          20
328
329 /* Reports 19-22: flash upgrade of the C8051F321 */
330 #define WRITE_REPORT            19
331 #define FLASH_REPORT            20
332 #define CRC_REPORT              21
333 #define RESPONSE_REPORT         22
334
335 /* Report 23: currently unused, but can accept 60 byte reports on the HID */
336 /* interrupt out endpoint 2 every 1 millisecond */
337 #define UNUSED_REPORT           23
338
339
340
341 /**************************************************************************
342  * Software/Hardware Versions
343  **************************************************************************/
344 #define RADIO_SW_VERSION_NOT_BOOTLOADABLE       6
345 #define RADIO_SW_VERSION                        7
346 #define RADIO_SW_VERSION_CURRENT                15
347 #define RADIO_HW_VERSION                        1
348
349 #define SCRATCH_PAGE_SW_VERSION 1
350 #define SCRATCH_PAGE_HW_VERSION 2
351
352
353
354 /**************************************************************************
355  * LED State Definitions
356  **************************************************************************/
357 #define LED_COMMAND             0x35
358
359 #define NO_CHANGE_LED           0x00
360 #define ALL_COLOR_LED           0x01    /* streaming state */
361 #define BLINK_GREEN_LED         0x02    /* connect state */
362 #define BLINK_RED_LED           0x04
363 #define BLINK_ORANGE_LED        0x10    /* disconnect state */
364 #define SOLID_GREEN_LED         0x20    /* tuning/seeking state */
365 #define SOLID_RED_LED           0x40    /* bootload state */
366 #define SOLID_ORANGE_LED        0x80
367
368
369
370 /**************************************************************************
371  * Stream State Definitions
372  **************************************************************************/
373 #define STREAM_COMMAND  0x36
374 #define STREAM_VIDPID   0x00
375 #define STREAM_AUDIO    0xff
376
377
378
379 /**************************************************************************
380  * Bootloader / Flash Commands
381  **************************************************************************/
382
383 /* unique id sent to bootloader and required to put into a bootload state */
384 #define UNIQUE_BL_ID            0x34
385
386 /* mask for the flash data */
387 #define FLASH_DATA_MASK         0x55
388
389 /* bootloader commands */
390 #define GET_SW_VERSION_COMMAND  0x00
391 #define SET_PAGE_COMMAND        0x01
392 #define ERASE_PAGE_COMMAND      0x02
393 #define WRITE_PAGE_COMMAND      0x03
394 #define CRC_ON_PAGE_COMMAND     0x04
395 #define READ_FLASH_BYTE_COMMAND 0x05
396 #define RESET_DEVICE_COMMAND    0x06
397 #define GET_HW_VERSION_COMMAND  0x07
398 #define BLANK                   0xff
399
400 /* bootloader command responses */
401 #define COMMAND_OK              0x01
402 #define COMMAND_FAILED          0x02
403 #define COMMAND_PENDING         0x03
404
405 /* buffer sizes */
406 #define COMMAND_BUFFER_SIZE     4
407 #define RESPONSE_BUFFER_SIZE    2
408 #define FLASH_BUFFER_SIZE       64
409 #define CRC_BUFFER_SIZE         3
410
411
412
413 /**************************************************************************
414  * General Driver Definitions
415  **************************************************************************/
416
417 /*
418  * si470x_device - private data
419  */
420 struct si470x_device {
421         /* reference to USB and video device */
422         struct usb_device *usbdev;
423         struct usb_interface *intf;
424         struct video_device *videodev;
425
426         /* driver management */
427         unsigned int users;
428        unsigned char disconnected;
429
430         /* Silabs internal registers (0..15) */
431         unsigned short registers[RADIO_REGISTER_NUM];
432
433         /* RDS receive buffer */
434         struct delayed_work work;
435         wait_queue_head_t read_queue;
436         struct mutex lock;              /* buffer locking */
437         unsigned char *buffer;          /* size is always multiple of three */
438         unsigned int buf_size;
439         unsigned int rd_index;
440         unsigned int wr_index;
441 };
442
443
444 /*
445  * Lock to prevent kfree of data before all users have releases the device.
446  */
447 static DEFINE_MUTEX(open_close_lock);
448
449
450 /*
451  * The frequency is set in units of 62.5 Hz when using V4L2_TUNER_CAP_LOW,
452  * 62.5 kHz otherwise.
453  * The tuner is able to have a channel spacing of 50, 100 or 200 kHz.
454  * tuner->capability is therefore set to V4L2_TUNER_CAP_LOW
455  * The FREQ_MUL is then: 1 MHz / 62.5 Hz = 16000
456  */
457 #define FREQ_MUL (1000000 / 62.5)
458
459
460
461 /**************************************************************************
462  * General Driver Functions
463  **************************************************************************/
464
465 /*
466  * si470x_get_report - receive a HID report
467  */
468 static int si470x_get_report(struct si470x_device *radio, void *buf, int size)
469 {
470         unsigned char *report = (unsigned char *) buf;
471         int retval;
472
473         retval = usb_control_msg(radio->usbdev,
474                 usb_rcvctrlpipe(radio->usbdev, 0),
475                 HID_REQ_GET_REPORT,
476                 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
477                 report[0], 2,
478                 buf, size, usb_timeout);
479         if (retval < 0)
480                 printk(KERN_WARNING DRIVER_NAME
481                         ": si470x_get_report: usb_control_msg returned %d\n",
482                         retval);
483
484         return retval;
485 }
486
487
488 /*
489  * si470x_set_report - send a HID report
490  */
491 static int si470x_set_report(struct si470x_device *radio, void *buf, int size)
492 {
493         unsigned char *report = (unsigned char *) buf;
494         int retval;
495
496         retval = usb_control_msg(radio->usbdev,
497                 usb_sndctrlpipe(radio->usbdev, 0),
498                 HID_REQ_SET_REPORT,
499                 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
500                 report[0], 2,
501                 buf, size, usb_timeout);
502         if (retval < 0)
503                 printk(KERN_WARNING DRIVER_NAME
504                         ": si470x_set_report: usb_control_msg returned %d\n",
505                         retval);
506
507         return retval;
508 }
509
510
511 /*
512  * si470x_get_register - read register
513  */
514 static int si470x_get_register(struct si470x_device *radio, int regnr)
515 {
516         unsigned char buf[REGISTER_REPORT_SIZE];
517         int retval;
518
519         buf[0] = REGISTER_REPORT(regnr);
520
521         retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
522
523         if (retval >= 0)
524                 radio->registers[regnr] = get_unaligned_be16(&buf[1]);
525
526         return (retval < 0) ? -EINVAL : 0;
527 }
528
529
530 /*
531  * si470x_set_register - write register
532  */
533 static int si470x_set_register(struct si470x_device *radio, int regnr)
534 {
535         unsigned char buf[REGISTER_REPORT_SIZE];
536         int retval;
537
538         buf[0] = REGISTER_REPORT(regnr);
539         put_unaligned_be16(radio->registers[regnr], &buf[1]);
540
541         retval = si470x_set_report(radio, (void *) &buf, sizeof(buf));
542
543         return (retval < 0) ? -EINVAL : 0;
544 }
545
546
547 /*
548  * si470x_get_all_registers - read entire registers
549  */
550 static int si470x_get_all_registers(struct si470x_device *radio)
551 {
552         unsigned char buf[ENTIRE_REPORT_SIZE];
553         int retval;
554         unsigned char regnr;
555
556         buf[0] = ENTIRE_REPORT;
557
558         retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
559
560         if (retval >= 0)
561                 for (regnr = 0; regnr < RADIO_REGISTER_NUM; regnr++)
562                         radio->registers[regnr] = get_unaligned_be16(
563                                 &buf[regnr * RADIO_REGISTER_SIZE + 1]);
564
565         return (retval < 0) ? -EINVAL : 0;
566 }
567
568
569 /*
570  * si470x_get_rds_registers - read rds registers
571  */
572 static int si470x_get_rds_registers(struct si470x_device *radio)
573 {
574         unsigned char buf[RDS_REPORT_SIZE];
575         int retval;
576         int size;
577         unsigned char regnr;
578
579         buf[0] = RDS_REPORT;
580
581         retval = usb_interrupt_msg(radio->usbdev,
582                 usb_rcvintpipe(radio->usbdev, 1),
583                 (void *) &buf, sizeof(buf), &size, usb_timeout);
584         if (size != sizeof(buf))
585                printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: "
586                         "return size differs: %d != %zu\n", size, sizeof(buf));
587         if (retval < 0)
588                 printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: "
589                         "usb_interrupt_msg returned %d\n", retval);
590
591         if (retval >= 0)
592                 for (regnr = 0; regnr < RDS_REGISTER_NUM; regnr++)
593                         radio->registers[STATUSRSSI + regnr] =
594                                 get_unaligned_be16(&buf[regnr * RADIO_REGISTER_SIZE + 1]);
595
596         return (retval < 0) ? -EINVAL : 0;
597 }
598
599
600 /*
601  * si470x_set_chan - set the channel
602  */
603 static int si470x_set_chan(struct si470x_device *radio, unsigned short chan)
604 {
605         int retval;
606         unsigned long timeout;
607         bool timed_out = 0;
608
609         /* start tuning */
610         radio->registers[CHANNEL] &= ~CHANNEL_CHAN;
611         radio->registers[CHANNEL] |= CHANNEL_TUNE | chan;
612         retval = si470x_set_register(radio, CHANNEL);
613         if (retval < 0)
614                 return retval;
615
616         /* wait till seek operation has completed */
617         timeout = jiffies + msecs_to_jiffies(tune_timeout);
618         do {
619                 retval = si470x_get_register(radio, STATUSRSSI);
620                 if (retval < 0)
621                         return retval;
622                 timed_out = time_after(jiffies, timeout);
623         } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
624                 (!timed_out));
625         if (timed_out)
626                 printk(KERN_WARNING DRIVER_NAME
627                         ": seek does not finish after %u ms\n", tune_timeout);
628
629         /* stop tuning */
630         radio->registers[CHANNEL] &= ~CHANNEL_TUNE;
631         return si470x_set_register(radio, CHANNEL);
632 }
633
634
635 /*
636  * si470x_get_freq - get the frequency
637  */
638 static unsigned int si470x_get_freq(struct si470x_device *radio)
639 {
640         unsigned int spacing, band_bottom, freq;
641         unsigned short chan;
642         int retval;
643
644         /* Spacing (kHz) */
645         switch (space) {
646         /* 0: 200 kHz (USA, Australia) */
647         case 0 : spacing = 0.200 * FREQ_MUL; break;
648         /* 1: 100 kHz (Europe, Japan) */
649         case 1 : spacing = 0.100 * FREQ_MUL; break;
650         /* 2:  50 kHz */
651         default: spacing = 0.050 * FREQ_MUL; break;
652         };
653
654         /* Bottom of Band (MHz) */
655         switch (band) {
656         /* 0: 87.5 - 108 MHz (USA, Europe) */
657         case 0 : band_bottom = 87.5 * FREQ_MUL; break;
658         /* 1: 76   - 108 MHz (Japan wide band) */
659         default: band_bottom = 76   * FREQ_MUL; break;
660         /* 2: 76   -  90 MHz (Japan) */
661         case 2 : band_bottom = 76   * FREQ_MUL; break;
662         };
663
664         /* read channel */
665         retval = si470x_get_register(radio, READCHAN);
666         if (retval < 0)
667                 return retval;
668         chan = radio->registers[READCHAN] & READCHAN_READCHAN;
669
670         /* Frequency (MHz) = Spacing (kHz) x Channel + Bottom of Band (MHz) */
671         freq = chan * spacing + band_bottom;
672
673         return freq;
674 }
675
676
677 /*
678  * si470x_set_freq - set the frequency
679  */
680 static int si470x_set_freq(struct si470x_device *radio, unsigned int freq)
681 {
682         unsigned int spacing, band_bottom;
683         unsigned short chan;
684
685         /* Spacing (kHz) */
686         switch (space) {
687         /* 0: 200 kHz (USA, Australia) */
688         case 0 : spacing = 0.200 * FREQ_MUL; break;
689         /* 1: 100 kHz (Europe, Japan) */
690         case 1 : spacing = 0.100 * FREQ_MUL; break;
691         /* 2:  50 kHz */
692         default: spacing = 0.050 * FREQ_MUL; break;
693         };
694
695         /* Bottom of Band (MHz) */
696         switch (band) {
697         /* 0: 87.5 - 108 MHz (USA, Europe) */
698         case 0 : band_bottom = 87.5 * FREQ_MUL; break;
699         /* 1: 76   - 108 MHz (Japan wide band) */
700         default: band_bottom = 76   * FREQ_MUL; break;
701         /* 2: 76   -  90 MHz (Japan) */
702         case 2 : band_bottom = 76   * FREQ_MUL; break;
703         };
704
705         /* Chan = [ Freq (Mhz) - Bottom of Band (MHz) ] / Spacing (kHz) */
706         chan = (freq - band_bottom) / spacing;
707
708         return si470x_set_chan(radio, chan);
709 }
710
711
712 /*
713  * si470x_start - switch on radio
714  */
715 static int si470x_start(struct si470x_device *radio)
716 {
717         int retval;
718
719         /* powercfg */
720         radio->registers[POWERCFG] =
721                 POWERCFG_DMUTE | POWERCFG_ENABLE | POWERCFG_RDSM;
722         retval = si470x_set_register(radio, POWERCFG);
723         if (retval < 0)
724                 return retval;
725
726         /* sysconfig 1 */
727         radio->registers[SYSCONFIG1] = SYSCONFIG1_DE;
728         retval = si470x_set_register(radio, SYSCONFIG1);
729         if (retval < 0)
730                 return retval;
731
732         /* sysconfig 2 */
733         radio->registers[SYSCONFIG2] =
734                 (0x3f  << 8) |  /* SEEKTH */
735                 (band  << 6) |  /* BAND */
736                 (space << 4) |  /* SPACE */
737                 15;             /* VOLUME (max) */
738         retval = si470x_set_register(radio, SYSCONFIG2);
739         if (retval < 0)
740                 return retval;
741
742         /* reset last channel */
743         return si470x_set_chan(radio,
744                 radio->registers[CHANNEL] & CHANNEL_CHAN);
745 }
746
747
748 /*
749  * si470x_stop - switch off radio
750  */
751 static int si470x_stop(struct si470x_device *radio)
752 {
753         int retval;
754
755         /* sysconfig 1 */
756         radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
757         retval = si470x_set_register(radio, SYSCONFIG1);
758         if (retval < 0)
759                 return retval;
760
761         /* powercfg */
762         radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
763         /* POWERCFG_ENABLE has to automatically go low */
764         radio->registers[POWERCFG] |= POWERCFG_ENABLE | POWERCFG_DISABLE;
765         return si470x_set_register(radio, POWERCFG);
766 }
767
768
769 /*
770  * si470x_rds_on - switch on rds reception
771  */
772 static int si470x_rds_on(struct si470x_device *radio)
773 {
774         int retval;
775
776         /* sysconfig 1 */
777         mutex_lock(&radio->lock);
778         radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDS;
779         retval = si470x_set_register(radio, SYSCONFIG1);
780         if (retval < 0)
781                 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
782         mutex_unlock(&radio->lock);
783
784         return retval;
785 }
786
787
788
789 /**************************************************************************
790  * RDS Driver Functions
791  **************************************************************************/
792
793 /*
794  * si470x_rds - rds processing function
795  */
796 static void si470x_rds(struct si470x_device *radio)
797 {
798         unsigned char blocknum;
799         unsigned short bler; /* rds block errors */
800         unsigned short rds;
801         unsigned char tmpbuf[3];
802
803         /* get rds blocks */
804         if (si470x_get_rds_registers(radio) < 0)
805                 return;
806         if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0) {
807                 /* No RDS group ready */
808                 return;
809         }
810         if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSS) == 0) {
811                 /* RDS decoder not synchronized */
812                 return;
813         }
814
815         /* copy all four RDS blocks to internal buffer */
816         mutex_lock(&radio->lock);
817         for (blocknum = 0; blocknum < 4; blocknum++) {
818                 switch (blocknum) {
819                 default:
820                         bler = (radio->registers[STATUSRSSI] &
821                                         STATUSRSSI_BLERA) >> 9;
822                         rds = radio->registers[RDSA];
823                         break;
824                 case 1:
825                         bler = (radio->registers[READCHAN] &
826                                         READCHAN_BLERB) >> 14;
827                         rds = radio->registers[RDSB];
828                         break;
829                 case 2:
830                         bler = (radio->registers[READCHAN] &
831                                         READCHAN_BLERC) >> 12;
832                         rds = radio->registers[RDSC];
833                         break;
834                 case 3:
835                         bler = (radio->registers[READCHAN] &
836                                         READCHAN_BLERD) >> 10;
837                         rds = radio->registers[RDSD];
838                         break;
839                 };
840
841                 /* Fill the V4L2 RDS buffer */
842                 put_unaligned_le16(rds, &tmpbuf);
843                 tmpbuf[2] = blocknum;           /* offset name */
844                 tmpbuf[2] |= blocknum << 3;     /* received offset */
845                 if (bler > max_rds_errors)
846                         tmpbuf[2] |= 0x80; /* uncorrectable errors */
847                 else if (bler > 0)
848                         tmpbuf[2] |= 0x40; /* corrected error(s) */
849
850                 /* copy RDS block to internal buffer */
851                 memcpy(&radio->buffer[radio->wr_index], &tmpbuf, 3);
852                 radio->wr_index += 3;
853
854                 /* wrap write pointer */
855                 if (radio->wr_index >= radio->buf_size)
856                         radio->wr_index = 0;
857
858                 /* check for overflow */
859                 if (radio->wr_index == radio->rd_index) {
860                         /* increment and wrap read pointer */
861                         radio->rd_index += 3;
862                         if (radio->rd_index >= radio->buf_size)
863                                 radio->rd_index = 0;
864                 }
865         }
866         mutex_unlock(&radio->lock);
867
868         /* wake up read queue */
869         if (radio->wr_index != radio->rd_index)
870                 wake_up_interruptible(&radio->read_queue);
871 }
872
873
874 /*
875  * si470x_work - rds work function
876  */
877 static void si470x_work(struct work_struct *work)
878 {
879         struct si470x_device *radio = container_of(work, struct si470x_device,
880                 work.work);
881
882        if (radio->disconnected)
883                return;
884         if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
885                 return;
886
887         si470x_rds(radio);
888         schedule_delayed_work(&radio->work, msecs_to_jiffies(rds_poll_time));
889 }
890
891
892
893 /**************************************************************************
894  * File Operations Interface
895  **************************************************************************/
896
897 /*
898  * si470x_fops_read - read RDS data
899  */
900 static ssize_t si470x_fops_read(struct file *file, char __user *buf,
901                 size_t count, loff_t *ppos)
902 {
903         struct si470x_device *radio = video_get_drvdata(video_devdata(file));
904         int retval = 0;
905         unsigned int block_count = 0;
906
907         /* switch on rds reception */
908         if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
909                 si470x_rds_on(radio);
910                 schedule_delayed_work(&radio->work,
911                         msecs_to_jiffies(rds_poll_time));
912         }
913
914         /* block if no new data available */
915         while (radio->wr_index == radio->rd_index) {
916                 if (file->f_flags & O_NONBLOCK)
917                         return -EWOULDBLOCK;
918                 if (wait_event_interruptible(radio->read_queue,
919                         radio->wr_index != radio->rd_index) < 0)
920                         return -EINTR;
921         }
922
923         /* calculate block count from byte count */
924         count /= 3;
925
926         /* copy RDS block out of internal buffer and to user buffer */
927         mutex_lock(&radio->lock);
928         while (block_count < count) {
929                 if (radio->rd_index == radio->wr_index)
930                         break;
931
932                 /* always transfer rds complete blocks */
933                 if (copy_to_user(buf, &radio->buffer[radio->rd_index], 3))
934                         /* retval = -EFAULT; */
935                         break;
936
937                 /* increment and wrap read pointer */
938                 radio->rd_index += 3;
939                 if (radio->rd_index >= radio->buf_size)
940                         radio->rd_index = 0;
941
942                 /* increment counters */
943                 block_count++;
944                 buf += 3;
945                 retval += 3;
946         }
947         mutex_unlock(&radio->lock);
948
949         return retval;
950 }
951
952
953 /*
954  * si470x_fops_poll - poll RDS data
955  */
956 static unsigned int si470x_fops_poll(struct file *file,
957                 struct poll_table_struct *pts)
958 {
959         struct si470x_device *radio = video_get_drvdata(video_devdata(file));
960
961         /* switch on rds reception */
962         if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
963                 si470x_rds_on(radio);
964                 schedule_delayed_work(&radio->work,
965                         msecs_to_jiffies(rds_poll_time));
966         }
967
968         poll_wait(file, &radio->read_queue, pts);
969
970         if (radio->rd_index != radio->wr_index)
971                 return POLLIN | POLLRDNORM;
972
973         return 0;
974 }
975
976
977 /*
978  * si470x_fops_open - file open
979  */
980 static int si470x_fops_open(struct inode *inode, struct file *file)
981 {
982         struct si470x_device *radio = video_get_drvdata(video_devdata(file));
983         int retval;
984
985         radio->users++;
986
987         retval = usb_autopm_get_interface(radio->intf);
988         if (retval < 0) {
989                 radio->users--;
990                 return -EIO;
991         }
992
993         if (radio->users == 1) {
994                 retval = si470x_start(radio);
995                 if (retval < 0)
996                         usb_autopm_put_interface(radio->intf);
997                 return retval;
998         }
999
1000         return 0;
1001 }
1002
1003
1004 /*
1005  * si470x_fops_release - file release
1006  */
1007 static int si470x_fops_release(struct inode *inode, struct file *file)
1008 {
1009         struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1010        int retval = 0;
1011
1012         if (!radio)
1013                 return -ENODEV;
1014
1015        mutex_lock(&open_close_lock);
1016         radio->users--;
1017         if (radio->users == 0) {
1018                if (radio->disconnected) {
1019                        video_unregister_device(radio->videodev);
1020                        kfree(radio->buffer);
1021                        kfree(radio);
1022                        goto done;
1023                }
1024
1025                 /* stop rds reception */
1026                 cancel_delayed_work_sync(&radio->work);
1027
1028                 /* cancel read processes */
1029                 wake_up_interruptible(&radio->read_queue);
1030
1031                 retval = si470x_stop(radio);
1032                 usb_autopm_put_interface(radio->intf);
1033         }
1034
1035 done:
1036        mutex_unlock(&open_close_lock);
1037        return retval;
1038 }
1039
1040
1041 /*
1042  * si470x_fops - file operations interface
1043  */
1044 static const struct file_operations si470x_fops = {
1045         .owner          = THIS_MODULE,
1046         .llseek         = no_llseek,
1047         .read           = si470x_fops_read,
1048         .poll           = si470x_fops_poll,
1049         .ioctl          = video_ioctl2,
1050 #ifdef CONFIG_COMPAT
1051         .compat_ioctl   = v4l_compat_ioctl32,
1052 #endif
1053         .open           = si470x_fops_open,
1054         .release        = si470x_fops_release,
1055 };
1056
1057
1058
1059 /**************************************************************************
1060  * Video4Linux Interface
1061  **************************************************************************/
1062
1063 /*
1064  * si470x_v4l2_queryctrl - query control
1065  */
1066 static struct v4l2_queryctrl si470x_v4l2_queryctrl[] = {
1067 /* HINT: the disabled controls are only here to satify kradio and such apps */
1068         {
1069                 .id             = V4L2_CID_AUDIO_VOLUME,
1070                 .type           = V4L2_CTRL_TYPE_INTEGER,
1071                 .name           = "Volume",
1072                 .minimum        = 0,
1073                 .maximum        = 15,
1074                 .step           = 1,
1075                 .default_value  = 15,
1076         },
1077         {
1078                 .id             = V4L2_CID_AUDIO_BALANCE,
1079                 .flags          = V4L2_CTRL_FLAG_DISABLED,
1080         },
1081         {
1082                 .id             = V4L2_CID_AUDIO_BASS,
1083                 .flags          = V4L2_CTRL_FLAG_DISABLED,
1084         },
1085         {
1086                 .id             = V4L2_CID_AUDIO_TREBLE,
1087                 .flags          = V4L2_CTRL_FLAG_DISABLED,
1088         },
1089         {
1090                 .id             = V4L2_CID_AUDIO_MUTE,
1091                 .type           = V4L2_CTRL_TYPE_BOOLEAN,
1092                 .name           = "Mute",
1093                 .minimum        = 0,
1094                 .maximum        = 1,
1095                 .step           = 1,
1096                 .default_value  = 1,
1097         },
1098         {
1099                 .id             = V4L2_CID_AUDIO_LOUDNESS,
1100                 .flags          = V4L2_CTRL_FLAG_DISABLED,
1101         },
1102 };
1103
1104
1105 /*
1106  * si470x_vidioc_querycap - query device capabilities
1107  */
1108 static int si470x_vidioc_querycap(struct file *file, void *priv,
1109                 struct v4l2_capability *capability)
1110 {
1111         strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver));
1112         strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
1113         sprintf(capability->bus_info, "USB");
1114         capability->version = DRIVER_KERNEL_VERSION;
1115         capability->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
1116
1117         return 0;
1118 }
1119
1120
1121 /*
1122  * si470x_vidioc_g_input - get input
1123  */
1124 static int si470x_vidioc_g_input(struct file *filp, void *priv,
1125                 unsigned int *i)
1126 {
1127         *i = 0;
1128
1129         return 0;
1130 }
1131
1132
1133 /*
1134  * si470x_vidioc_s_input - set input
1135  */
1136 static int si470x_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
1137 {
1138         if (i != 0)
1139                 return -EINVAL;
1140
1141         return 0;
1142 }
1143
1144
1145 /*
1146  * si470x_vidioc_queryctrl - enumerate control items
1147  */
1148 static int si470x_vidioc_queryctrl(struct file *file, void *priv,
1149                 struct v4l2_queryctrl *qc)
1150 {
1151         unsigned char i;
1152         int retval = -EINVAL;
1153
1154         for (i = 0; i < ARRAY_SIZE(si470x_v4l2_queryctrl); i++) {
1155                 if (qc->id && qc->id == si470x_v4l2_queryctrl[i].id) {
1156                         memcpy(qc, &(si470x_v4l2_queryctrl[i]), sizeof(*qc));
1157                         retval = 0;
1158                         break;
1159                 }
1160         }
1161         if (retval < 0)
1162                 printk(KERN_WARNING DRIVER_NAME
1163                         ": query control failed with %d\n", retval);
1164
1165         return retval;
1166 }
1167
1168
1169 /*
1170  * si470x_vidioc_g_ctrl - get the value of a control
1171  */
1172 static int si470x_vidioc_g_ctrl(struct file *file, void *priv,
1173                 struct v4l2_control *ctrl)
1174 {
1175         struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1176
1177        if (radio->disconnected)
1178                return -EIO;
1179
1180         switch (ctrl->id) {
1181         case V4L2_CID_AUDIO_VOLUME:
1182                 ctrl->value = radio->registers[SYSCONFIG2] &
1183                                 SYSCONFIG2_VOLUME;
1184                 break;
1185         case V4L2_CID_AUDIO_MUTE:
1186                 ctrl->value = ((radio->registers[POWERCFG] &
1187                                 POWERCFG_DMUTE) == 0) ? 1 : 0;
1188                 break;
1189         }
1190
1191         return 0;
1192 }
1193
1194
1195 /*
1196  * si470x_vidioc_s_ctrl - set the value of a control
1197  */
1198 static int si470x_vidioc_s_ctrl(struct file *file, void *priv,
1199                 struct v4l2_control *ctrl)
1200 {
1201         struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1202         int retval;
1203
1204        if (radio->disconnected)
1205                return -EIO;
1206
1207         switch (ctrl->id) {
1208         case V4L2_CID_AUDIO_VOLUME:
1209                 radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_VOLUME;
1210                 radio->registers[SYSCONFIG2] |= ctrl->value;
1211                 retval = si470x_set_register(radio, SYSCONFIG2);
1212                 break;
1213         case V4L2_CID_AUDIO_MUTE:
1214                 if (ctrl->value == 1)
1215                         radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
1216                 else
1217                         radio->registers[POWERCFG] |= POWERCFG_DMUTE;
1218                 retval = si470x_set_register(radio, POWERCFG);
1219                 break;
1220         default:
1221                 retval = -EINVAL;
1222         }
1223         if (retval < 0)
1224                 printk(KERN_WARNING DRIVER_NAME
1225                         ": set control failed with %d\n", retval);
1226
1227         return retval;
1228 }
1229
1230
1231 /*
1232  * si470x_vidioc_g_audio - get audio attributes
1233  */
1234 static int si470x_vidioc_g_audio(struct file *file, void *priv,
1235                 struct v4l2_audio *audio)
1236 {
1237         if (audio->index > 1)
1238                 return -EINVAL;
1239
1240         strcpy(audio->name, "Radio");
1241         audio->capability = V4L2_AUDCAP_STEREO;
1242
1243         return 0;
1244 }
1245
1246
1247 /*
1248  * si470x_vidioc_s_audio - set audio attributes
1249  */
1250 static int si470x_vidioc_s_audio(struct file *file, void *priv,
1251                 struct v4l2_audio *audio)
1252 {
1253         if (audio->index != 0)
1254                 return -EINVAL;
1255
1256         return 0;
1257 }
1258
1259
1260 /*
1261  * si470x_vidioc_g_tuner - get tuner attributes
1262  */
1263 static int si470x_vidioc_g_tuner(struct file *file, void *priv,
1264                 struct v4l2_tuner *tuner)
1265 {
1266         struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1267         int retval;
1268
1269        if (radio->disconnected)
1270                return -EIO;
1271         if (tuner->index > 0)
1272                 return -EINVAL;
1273
1274         /* read status rssi */
1275         retval = si470x_get_register(radio, STATUSRSSI);
1276         if (retval < 0)
1277                 return retval;
1278
1279         strcpy(tuner->name, "FM");
1280         tuner->type = V4L2_TUNER_RADIO;
1281         switch (band) {
1282         /* 0: 87.5 - 108 MHz (USA, Europe, default) */
1283         default:
1284                 tuner->rangelow  =  87.5 * FREQ_MUL;
1285                 tuner->rangehigh = 108   * FREQ_MUL;
1286                 break;
1287         /* 1: 76   - 108 MHz (Japan wide band) */
1288         case 1 :
1289                 tuner->rangelow  =  76   * FREQ_MUL;
1290                 tuner->rangehigh = 108   * FREQ_MUL;
1291                 break;
1292         /* 2: 76   -  90 MHz (Japan) */
1293         case 2 :
1294                 tuner->rangelow  =  76   * FREQ_MUL;
1295                 tuner->rangehigh =  90   * FREQ_MUL;
1296                 break;
1297         };
1298         tuner->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
1299         tuner->capability = V4L2_TUNER_CAP_LOW;
1300
1301         /* Stereo indicator == Stereo (instead of Mono) */
1302         if ((radio->registers[STATUSRSSI] & STATUSRSSI_ST) == 1)
1303                 tuner->audmode = V4L2_TUNER_MODE_STEREO;
1304         else
1305                 tuner->audmode = V4L2_TUNER_MODE_MONO;
1306
1307         /* min is worst, max is best; signal:0..0xffff; rssi: 0..0xff */
1308         tuner->signal = (radio->registers[STATUSRSSI] & STATUSRSSI_RSSI)
1309                                 * 0x0101;
1310
1311         /* automatic frequency control: -1: freq to low, 1 freq to high */
1312         tuner->afc = 0;
1313
1314         return 0;
1315 }
1316
1317
1318 /*
1319  * si470x_vidioc_s_tuner - set tuner attributes
1320  */
1321 static int si470x_vidioc_s_tuner(struct file *file, void *priv,
1322                 struct v4l2_tuner *tuner)
1323 {
1324         struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1325         int retval;
1326
1327        if (radio->disconnected)
1328                return -EIO;
1329         if (tuner->index > 0)
1330                 return -EINVAL;
1331
1332         if (tuner->audmode == V4L2_TUNER_MODE_MONO)
1333                 radio->registers[POWERCFG] |= POWERCFG_MONO;  /* force mono */
1334         else
1335                 radio->registers[POWERCFG] &= ~POWERCFG_MONO; /* try stereo */
1336
1337         retval = si470x_set_register(radio, POWERCFG);
1338         if (retval < 0)
1339                 printk(KERN_WARNING DRIVER_NAME
1340                         ": set tuner failed with %d\n", retval);
1341
1342         return retval;
1343 }
1344
1345
1346 /*
1347  * si470x_vidioc_g_frequency - get tuner or modulator radio frequency
1348  */
1349 static int si470x_vidioc_g_frequency(struct file *file, void *priv,
1350                 struct v4l2_frequency *freq)
1351 {
1352         struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1353
1354        if (radio->disconnected)
1355                return -EIO;
1356
1357         freq->type = V4L2_TUNER_RADIO;
1358         freq->frequency = si470x_get_freq(radio);
1359
1360         return 0;
1361 }
1362
1363
1364 /*
1365  * si470x_vidioc_s_frequency - set tuner or modulator radio frequency
1366  */
1367 static int si470x_vidioc_s_frequency(struct file *file, void *priv,
1368                 struct v4l2_frequency *freq)
1369 {
1370         struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1371         int retval;
1372
1373        if (radio->disconnected)
1374                return -EIO;
1375         if (freq->type != V4L2_TUNER_RADIO)
1376                 return -EINVAL;
1377
1378         retval = si470x_set_freq(radio, freq->frequency);
1379         if (retval < 0)
1380                 printk(KERN_WARNING DRIVER_NAME
1381                         ": set frequency failed with %d\n", retval);
1382
1383         return 0;
1384 }
1385
1386
1387 /*
1388  * si470x_viddev_tamples - video device interface
1389  */
1390 static struct video_device si470x_viddev_template = {
1391         .fops                   = &si470x_fops,
1392         .name                   = DRIVER_NAME,
1393         .type                   = VID_TYPE_TUNER,
1394         .release                = video_device_release,
1395         .vidioc_querycap        = si470x_vidioc_querycap,
1396         .vidioc_g_input         = si470x_vidioc_g_input,
1397         .vidioc_s_input         = si470x_vidioc_s_input,
1398         .vidioc_queryctrl       = si470x_vidioc_queryctrl,
1399         .vidioc_g_ctrl          = si470x_vidioc_g_ctrl,
1400         .vidioc_s_ctrl          = si470x_vidioc_s_ctrl,
1401         .vidioc_g_audio         = si470x_vidioc_g_audio,
1402         .vidioc_s_audio         = si470x_vidioc_s_audio,
1403         .vidioc_g_tuner         = si470x_vidioc_g_tuner,
1404         .vidioc_s_tuner         = si470x_vidioc_s_tuner,
1405         .vidioc_g_frequency     = si470x_vidioc_g_frequency,
1406         .vidioc_s_frequency     = si470x_vidioc_s_frequency,
1407         .owner                  = THIS_MODULE,
1408 };
1409
1410
1411
1412 /**************************************************************************
1413  * USB Interface
1414  **************************************************************************/
1415
1416 /*
1417  * si470x_usb_driver_probe - probe for the device
1418  */
1419 static int si470x_usb_driver_probe(struct usb_interface *intf,
1420                 const struct usb_device_id *id)
1421 {
1422         struct si470x_device *radio;
1423         int retval = -ENOMEM;
1424
1425         /* private data allocation */
1426         radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL);
1427         if (!radio)
1428                 goto err_initial;
1429
1430         /* video device allocation */
1431         radio->videodev = video_device_alloc();
1432         if (!radio->videodev)
1433                 goto err_radio;
1434
1435         /* initial configuration */
1436         memcpy(radio->videodev, &si470x_viddev_template,
1437                         sizeof(si470x_viddev_template));
1438         radio->users = 0;
1439         radio->usbdev = interface_to_usbdev(intf);
1440         radio->intf = intf;
1441         mutex_init(&radio->lock);
1442         video_set_drvdata(radio->videodev, radio);
1443
1444         /* show some infos about the specific device */
1445         retval = -EIO;
1446         if (si470x_get_all_registers(radio) < 0)
1447                 goto err_all;
1448         printk(KERN_INFO DRIVER_NAME ": DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",
1449                         radio->registers[DEVICEID], radio->registers[CHIPID]);
1450
1451         /* check if firmware is current */
1452         if ((radio->registers[CHIPID] & CHIPID_FIRMWARE)
1453                         < RADIO_SW_VERSION_CURRENT) {
1454                 printk(KERN_WARNING DRIVER_NAME
1455                         ": This driver is known to work with "
1456                         "firmware version %hu,\n", RADIO_SW_VERSION_CURRENT);
1457                 printk(KERN_WARNING DRIVER_NAME
1458                         ": but the device has firmware version %hu.\n",
1459                         radio->registers[CHIPID] & CHIPID_FIRMWARE);
1460                 printk(KERN_WARNING DRIVER_NAME
1461                         ": If you have some trouble using this driver,\n");
1462                 printk(KERN_WARNING DRIVER_NAME
1463                         ": please report to V4L ML at "
1464                         "video4linux-list@redhat.com\n");
1465         }
1466
1467         /* set initial frequency */
1468         si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
1469
1470         /* rds buffer allocation */
1471         radio->buf_size = rds_buf * 3;
1472         radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
1473         if (!radio->buffer)
1474                 goto err_all;
1475
1476         /* rds buffer configuration */
1477         radio->wr_index = 0;
1478         radio->rd_index = 0;
1479         init_waitqueue_head(&radio->read_queue);
1480
1481         /* prepare rds work function */
1482         INIT_DELAYED_WORK(&radio->work, si470x_work);
1483
1484         /* register video device */
1485         if (video_register_device(radio->videodev, VFL_TYPE_RADIO, radio_nr)) {
1486                 printk(KERN_WARNING DRIVER_NAME
1487                                 ": Could not register video device\n");
1488                 goto err_all;
1489         }
1490         usb_set_intfdata(intf, radio);
1491
1492         return 0;
1493 err_all:
1494         video_device_release(radio->videodev);
1495         kfree(radio->buffer);
1496 err_radio:
1497         kfree(radio);
1498 err_initial:
1499         return retval;
1500 }
1501
1502
1503 /*
1504  * si470x_usb_driver_suspend - suspend the device
1505  */
1506 static int si470x_usb_driver_suspend(struct usb_interface *intf,
1507                 pm_message_t message)
1508 {
1509         struct si470x_device *radio = usb_get_intfdata(intf);
1510
1511         printk(KERN_INFO DRIVER_NAME ": suspending now...\n");
1512
1513         cancel_delayed_work_sync(&radio->work);
1514
1515         return 0;
1516 }
1517
1518
1519 /*
1520  * si470x_usb_driver_resume - resume the device
1521  */
1522 static int si470x_usb_driver_resume(struct usb_interface *intf)
1523 {
1524         struct si470x_device *radio = usb_get_intfdata(intf);
1525
1526         printk(KERN_INFO DRIVER_NAME ": resuming now...\n");
1527
1528         mutex_lock(&radio->lock);
1529         if (radio->users && radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS)
1530                 schedule_delayed_work(&radio->work,
1531                         msecs_to_jiffies(rds_poll_time));
1532         mutex_unlock(&radio->lock);
1533
1534         return 0;
1535 }
1536
1537
1538 /*
1539  * si470x_usb_driver_disconnect - disconnect the device
1540  */
1541 static void si470x_usb_driver_disconnect(struct usb_interface *intf)
1542 {
1543         struct si470x_device *radio = usb_get_intfdata(intf);
1544
1545        mutex_lock(&open_close_lock);
1546        radio->disconnected = 1;
1547         cancel_delayed_work_sync(&radio->work);
1548         usb_set_intfdata(intf, NULL);
1549        if (radio->users == 0) {
1550                video_unregister_device(radio->videodev);
1551                kfree(radio->buffer);
1552                kfree(radio);
1553        }
1554        mutex_unlock(&open_close_lock);
1555 }
1556
1557
1558 /*
1559  * si470x_usb_driver - usb driver interface
1560  */
1561 static struct usb_driver si470x_usb_driver = {
1562         .name                   = DRIVER_NAME,
1563         .probe                  = si470x_usb_driver_probe,
1564         .disconnect             = si470x_usb_driver_disconnect,
1565         .suspend                = si470x_usb_driver_suspend,
1566         .resume                 = si470x_usb_driver_resume,
1567         .id_table               = si470x_usb_driver_id_table,
1568         .supports_autosuspend   = 1,
1569 };
1570
1571
1572
1573 /**************************************************************************
1574  * Module Interface
1575  **************************************************************************/
1576
1577 /*
1578  * si470x_module_init - module init
1579  */
1580 static int __init si470x_module_init(void)
1581 {
1582         printk(KERN_INFO DRIVER_DESC ", Version " DRIVER_VERSION "\n");
1583         return usb_register(&si470x_usb_driver);
1584 }
1585
1586
1587 /*
1588  * si470x_module_exit - module exit
1589  */
1590 static void __exit si470x_module_exit(void)
1591 {
1592         usb_deregister(&si470x_usb_driver);
1593 }
1594
1595
1596 module_init(si470x_module_init);
1597 module_exit(si470x_module_exit);
1598
1599 MODULE_LICENSE("GPL");
1600 MODULE_AUTHOR(DRIVER_AUTHOR);
1601 MODULE_DESCRIPTION(DRIVER_DESC);
1602 MODULE_VERSION(DRIVER_VERSION);