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