blob: 31da21a38f24948f45458c877984acc6783d1e27 [file] [log] [blame]
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001/*
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 *
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03008 * Copyright (c) 2008 Tobias Lorenz <tobias.lorenz@gmx.net>
Tobias Lorenz78656ac2008-01-14 21:55:27 -03009 *
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/*
Tobias Lorenz6cc72652008-05-31 15:06:50 -030027 * 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/*
Tobias Lorenz78656ac2008-01-14 21:55:27 -030040 * 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>
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -030045 * Version 1.0.1
Tobias Lorenz78656ac2008-01-14 21:55:27 -030046 * - 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
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -030063 * 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)
Tobias Lorenz2fb88402008-01-25 05:14:57 -030071 * 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)
Tobias Lorenz0e3301e2008-01-27 14:54:07 -030078 * 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
Tobias Lorenz5caf5132008-02-04 22:26:08 -030082 * - optimization of variables and printf types
Tobias Lorenz0e3301e2008-01-27 14:54:07 -030083 * - improved error logging
Tobias Lorenz5caf5132008-02-04 22:26:08 -030084 * 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()
Tobias Lorenz57566ad2008-02-09 16:08:24 -030097 * 2008-02-08 Tobias Lorenz <tobias.lorenz@gmx.net>
98 * Oliver Neukum <oliver@neukum.org>
99 * Version 1.0.7
100 * - usb autosuspend support
Tobias Lorenzce5829e2008-05-31 15:04:32 -0300101 * - unplugging fixed
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300102 * 2008-05-07 Tobias Lorenz <tobias.lorenz@gmx.net>
103 * Version 1.0.8
Tobias Lorenzbbc89952008-05-31 15:11:32 -0300104 * - hardware frequency seek support
Tobias Lorenzace7d4b2008-05-31 15:09:07 -0300105 * - afc indication
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300106 * - more safety checks, let si470x_get_freq return errno
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300107 *
108 * ToDo:
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300109 * - add firmware download/update support
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300110 * - RDS support: interrupt mode, instead of polling
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300111 * - add LED status output (check if that's not already done in firmware)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300112 */
113
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300114
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300115/* driver definitions */
116#define DRIVER_AUTHOR "Tobias Lorenz <tobias.lorenz@gmx.net>"
117#define DRIVER_NAME "radio-si470x"
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300118#define DRIVER_KERNEL_VERSION KERNEL_VERSION(1, 0, 8)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300119#define DRIVER_CARD "Silicon Labs Si470x FM Radio Receiver"
120#define DRIVER_DESC "USB radio driver for Si470x FM Radio Receivers"
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300121#define DRIVER_VERSION "1.0.8"
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300122
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>
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300130#include <linux/usb.h>
131#include <linux/hid.h>
132#include <linux/version.h>
Mauro Carvalho Chehab387d4472008-01-15 11:25:10 -0300133#include <linux/videodev2.h>
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300134#include <linux/mutex.h>
Mauro Carvalho Chehab387d4472008-01-15 11:25:10 -0300135#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300136#include <media/v4l2-ioctl.h>
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300137#include <media/rds.h>
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300138#include <asm/unaligned.h>
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300139
140
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300141/* USB Device ID List */
142static struct usb_device_id si470x_usb_driver_id_table[] = {
143 /* Silicon Labs USB FM Radio Reference Design */
Tobias Lorenz374ab682008-09-24 19:17:54 -0300144 { USB_DEVICE_AND_INTERFACE_INFO(0x10c4, 0x818a, USB_CLASS_HID, 0, 0) },
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300145 /* ADS/Tech FM Radio Receiver (formerly Instant FM Music) */
Tobias Lorenz374ab682008-09-24 19:17:54 -0300146 { USB_DEVICE_AND_INTERFACE_INFO(0x06e1, 0xa155, USB_CLASS_HID, 0, 0) },
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300147 /* Terminating entry */
148 { }
149};
150MODULE_DEVICE_TABLE(usb, si470x_usb_driver_id_table);
151
152
153
154/**************************************************************************
155 * Module Parameters
156 **************************************************************************/
157
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300158/* Radio Nr */
159static int radio_nr = -1;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300160module_param(radio_nr, int, 0444);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300161MODULE_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 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300167static unsigned short space = 2;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300168module_param(space, ushort, 0444);
169MODULE_PARM_DESC(space, "Spacing: 0=200kHz 1=100kHz *2=50kHz*");
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300170
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) */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300175static unsigned short band = 1;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300176module_param(band, ushort, 0444);
177MODULE_PARM_DESC(band, "Band: 0=87.5..108MHz *1=76..108MHz* 2=76..90MHz");
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300178
179/* De-emphasis */
180/* 0: 75 us (USA) */
181/* 1: 50 us (Europe, Australia, Japan) */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300182static unsigned short de = 1;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300183module_param(de, ushort, 0444);
184MODULE_PARM_DESC(de, "De-emphasis: 0=75us *1=50us*");
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300185
186/* USB timeout */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300187static unsigned int usb_timeout = 500;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300188module_param(usb_timeout, uint, 0644);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300189MODULE_PARM_DESC(usb_timeout, "USB timeout (ms): *500*");
190
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300191/* Tune timeout */
192static unsigned int tune_timeout = 3000;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300193module_param(tune_timeout, uint, 0644);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300194MODULE_PARM_DESC(tune_timeout, "Tune timeout: *3000*");
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300195
Tobias Lorenzbbc89952008-05-31 15:11:32 -0300196/* Seek timeout */
197static unsigned int seek_timeout = 5000;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300198module_param(seek_timeout, uint, 0644);
Tobias Lorenzbbc89952008-05-31 15:11:32 -0300199MODULE_PARM_DESC(seek_timeout, "Seek timeout: *5000*");
200
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300201/* RDS buffer blocks */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300202static unsigned int rds_buf = 100;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300203module_param(rds_buf, uint, 0444);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300204MODULE_PARM_DESC(rds_buf, "RDS buffer entries: *100*");
205
206/* RDS maximum block errors */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300207static unsigned short max_rds_errors = 1;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300208/* 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 */
Tobias Lorenz374ab682008-09-24 19:17:54 -0300212module_param(max_rds_errors, ushort, 0644);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300213MODULE_PARM_DESC(max_rds_errors, "RDS maximum block errors: *1*");
214
215/* RDS poll frequency */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300216static unsigned int rds_poll_time = 40;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300217/* 40 is used by the original USBRadio.exe */
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300218/* 50 is used by radio-cadet */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300219/* 75 should be okay */
220/* 80 is the usual RDS receive interval */
Tobias Lorenz374ab682008-09-24 19:17:54 -0300221module_param(rds_poll_time, uint, 0644);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300222MODULE_PARM_DESC(rds_poll_time, "RDS poll time (ms): *40*");
223
224
225
226/**************************************************************************
227 * Register Definitions
228 **************************************************************************/
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300229#define RADIO_REGISTER_SIZE 2 /* 16 register bit width */
230#define RADIO_REGISTER_NUM 16 /* DEVICEID ... RDSD */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300231#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/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300320 * USB HID Reports
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300321 **************************************************************************/
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 */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300326#define REGISTER_REPORT_SIZE (RADIO_REGISTER_SIZE + 1)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300327#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 */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300331#define ENTIRE_REPORT_SIZE (RADIO_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300332#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 */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300336#define RDS_REPORT_SIZE (RDS_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300337#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/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300365 * Software/Hardware Versions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300366 **************************************************************************/
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300367#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
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300371
372#define SCRATCH_PAGE_SW_VERSION 1
373#define SCRATCH_PAGE_HW_VERSION 2
374
375
376
377/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300378 * LED State Definitions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300379 **************************************************************************/
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/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300394 * Stream State Definitions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300395 **************************************************************************/
396#define STREAM_COMMAND 0x36
397#define STREAM_VIDPID 0x00
398#define STREAM_AUDIO 0xff
399
400
401
402/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300403 * Bootloader / Flash Commands
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300404 **************************************************************************/
405
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300406/* unique id sent to bootloader and required to put into a bootload state */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300407#define UNIQUE_BL_ID 0x34
408
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300409/* mask for the flash data */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300410#define FLASH_DATA_MASK 0x55
411
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300412/* bootloader commands */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300413#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
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300423/* bootloader command responses */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300424#define COMMAND_OK 0x01
425#define COMMAND_FAILED 0x02
426#define COMMAND_PENDING 0x03
427
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300428/* buffer sizes */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300429#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/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300437 * General Driver Definitions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300438 **************************************************************************/
439
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300440/*
441 * si470x_device - private data
442 */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300443struct si470x_device {
444 /* reference to USB and video device */
445 struct usb_device *usbdev;
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300446 struct usb_interface *intf;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300447 struct video_device *videodev;
448
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300449 /* driver management */
450 unsigned int users;
Tobias Lorenzce5829e2008-05-31 15:04:32 -0300451 unsigned char disconnected;
452 struct mutex disconnect_lock;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300453
454 /* Silabs internal registers (0..15) */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300455 unsigned short registers[RADIO_REGISTER_NUM];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300456
457 /* RDS receive buffer */
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300458 struct delayed_work work;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300459 wait_queue_head_t read_queue;
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300460 struct mutex lock; /* buffer locking */
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300461 unsigned char *buffer; /* size is always multiple of three */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300462 unsigned int buf_size;
463 unsigned int rd_index;
464 unsigned int wr_index;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300465};
466
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300467
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300468/*
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 */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300475#define FREQ_MUL (1000000 / 62.5)
476
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300477
478
479/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300480 * General Driver Functions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300481 **************************************************************************/
482
483/*
484 * si470x_get_report - receive a HID report
485 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300486static int si470x_get_report(struct si470x_device *radio, void *buf, int size)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300487{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300488 unsigned char *report = (unsigned char *) buf;
489 int retval;
490
491 retval = usb_control_msg(radio->usbdev,
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300492 usb_rcvctrlpipe(radio->usbdev, 0),
493 HID_REQ_GET_REPORT,
494 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300495 report[0], 2,
496 buf, size, usb_timeout);
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300497
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300498 if (retval < 0)
499 printk(KERN_WARNING DRIVER_NAME
500 ": si470x_get_report: usb_control_msg returned %d\n",
501 retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300502 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300503}
504
505
506/*
507 * si470x_set_report - send a HID report
508 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300509static int si470x_set_report(struct si470x_device *radio, void *buf, int size)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300510{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300511 unsigned char *report = (unsigned char *) buf;
512 int retval;
513
514 retval = usb_control_msg(radio->usbdev,
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300515 usb_sndctrlpipe(radio->usbdev, 0),
516 HID_REQ_SET_REPORT,
517 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300518 report[0], 2,
519 buf, size, usb_timeout);
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300520
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300521 if (retval < 0)
522 printk(KERN_WARNING DRIVER_NAME
523 ": si470x_set_report: usb_control_msg returned %d\n",
524 retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300525 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300526}
527
528
529/*
530 * si470x_get_register - read register
531 */
532static int si470x_get_register(struct si470x_device *radio, int regnr)
533{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300534 unsigned char buf[REGISTER_REPORT_SIZE];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300535 int retval;
536
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300537 buf[0] = REGISTER_REPORT(regnr);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300538
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300539 retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
540
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300541 if (retval >= 0)
Harvey Harrison5ad6b812008-04-15 22:22:11 -0300542 radio->registers[regnr] = get_unaligned_be16(&buf[1]);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300543
544 return (retval < 0) ? -EINVAL : 0;
545}
546
547
548/*
549 * si470x_set_register - write register
550 */
551static int si470x_set_register(struct si470x_device *radio, int regnr)
552{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300553 unsigned char buf[REGISTER_REPORT_SIZE];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300554 int retval;
555
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300556 buf[0] = REGISTER_REPORT(regnr);
Harvey Harrison5ad6b812008-04-15 22:22:11 -0300557 put_unaligned_be16(radio->registers[regnr], &buf[1]);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300558
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300559 retval = si470x_set_report(radio, (void *) &buf, sizeof(buf));
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300560
561 return (retval < 0) ? -EINVAL : 0;
562}
563
564
565/*
566 * si470x_get_all_registers - read entire registers
567 */
568static int si470x_get_all_registers(struct si470x_device *radio)
569{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300570 unsigned char buf[ENTIRE_REPORT_SIZE];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300571 int retval;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300572 unsigned char regnr;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300573
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300574 buf[0] = ENTIRE_REPORT;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300575
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300576 retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300577
578 if (retval >= 0)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300579 for (regnr = 0; regnr < RADIO_REGISTER_NUM; regnr++)
Harvey Harrison5ad6b812008-04-15 22:22:11 -0300580 radio->registers[regnr] = get_unaligned_be16(
581 &buf[regnr * RADIO_REGISTER_SIZE + 1]);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300582
583 return (retval < 0) ? -EINVAL : 0;
584}
585
586
587/*
588 * si470x_get_rds_registers - read rds registers
589 */
590static int si470x_get_rds_registers(struct si470x_device *radio)
591{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300592 unsigned char buf[RDS_REPORT_SIZE];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300593 int retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300594 int size;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300595 unsigned char regnr;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300596
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300597 buf[0] = RDS_REPORT;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300598
599 retval = usb_interrupt_msg(radio->usbdev,
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300600 usb_rcvintpipe(radio->usbdev, 1),
601 (void *) &buf, sizeof(buf), &size, usb_timeout);
602 if (size != sizeof(buf))
Tobias Lorenzce5829e2008-05-31 15:04:32 -0300603 printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: "
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300604 "return size differs: %d != %zu\n", size, sizeof(buf));
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300605 if (retval < 0)
606 printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: "
607 "usb_interrupt_msg returned %d\n", retval);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300608
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300609 if (retval >= 0)
610 for (regnr = 0; regnr < RDS_REGISTER_NUM; regnr++)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300611 radio->registers[STATUSRSSI + regnr] =
Tobias Lorenzce5829e2008-05-31 15:04:32 -0300612 get_unaligned_be16(
613 &buf[regnr * RADIO_REGISTER_SIZE + 1]);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300614
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300615 return (retval < 0) ? -EINVAL : 0;
616}
617
618
619/*
620 * si470x_set_chan - set the channel
621 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300622static int si470x_set_chan(struct si470x_device *radio, unsigned short chan)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300623{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300624 int retval;
625 unsigned long timeout;
626 bool timed_out = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300627
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)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300633 goto done;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300634
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300635 /* wait till tune operation has completed */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300636 timeout = jiffies + msecs_to_jiffies(tune_timeout);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300637 do {
638 retval = si470x_get_register(radio, STATUSRSSI);
639 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300640 goto stop;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300641 timed_out = time_after(jiffies, timeout);
642 } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
643 (!timed_out));
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300644 if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
645 printk(KERN_WARNING DRIVER_NAME ": tune does not complete\n");
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300646 if (timed_out)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300647 printk(KERN_WARNING DRIVER_NAME
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300648 ": tune timed out after %u ms\n", tune_timeout);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300649
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300650stop:
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300651 /* stop tuning */
652 radio->registers[CHANNEL] &= ~CHANNEL_TUNE;
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300653 retval = si470x_set_register(radio, CHANNEL);
654
655done:
656 return retval;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300657}
658
659
660/*
661 * si470x_get_freq - get the frequency
662 */
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300663static int si470x_get_freq(struct si470x_device *radio, unsigned int *freq)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300664{
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300665 unsigned int spacing, band_bottom;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300666 unsigned short chan;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300667 int retval;
668
669 /* Spacing (kHz) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300670 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300671 /* 0: 200 kHz (USA, Australia) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300672 case 0:
673 spacing = 0.200 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300674 /* 1: 100 kHz (Europe, Japan) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300675 case 1:
676 spacing = 0.100 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300677 /* 2: 50 kHz */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300678 default:
679 spacing = 0.050 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300680 };
681
682 /* Bottom of Band (MHz) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300683 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300684 /* 0: 87.5 - 108 MHz (USA, Europe) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300685 case 0:
686 band_bottom = 87.5 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300687 /* 1: 76 - 108 MHz (Japan wide band) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300688 default:
689 band_bottom = 76 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300690 /* 2: 76 - 90 MHz (Japan) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300691 case 2:
692 band_bottom = 76 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300693 };
694
695 /* read channel */
696 retval = si470x_get_register(radio, READCHAN);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300697 chan = radio->registers[READCHAN] & READCHAN_READCHAN;
698
699 /* Frequency (MHz) = Spacing (kHz) x Channel + Bottom of Band (MHz) */
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300700 *freq = chan * spacing + band_bottom;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300701
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300702 return retval;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300703}
704
705
706/*
707 * si470x_set_freq - set the frequency
708 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300709static int si470x_set_freq(struct si470x_device *radio, unsigned int freq)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300710{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300711 unsigned int spacing, band_bottom;
712 unsigned short chan;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300713
714 /* Spacing (kHz) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300715 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300716 /* 0: 200 kHz (USA, Australia) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300717 case 0:
718 spacing = 0.200 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300719 /* 1: 100 kHz (Europe, Japan) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300720 case 1:
721 spacing = 0.100 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300722 /* 2: 50 kHz */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300723 default:
724 spacing = 0.050 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300725 };
726
727 /* Bottom of Band (MHz) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300728 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300729 /* 0: 87.5 - 108 MHz (USA, Europe) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300730 case 0:
731 band_bottom = 87.5 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300732 /* 1: 76 - 108 MHz (Japan wide band) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300733 default:
734 band_bottom = 76 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300735 /* 2: 76 - 90 MHz (Japan) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300736 case 2:
737 band_bottom = 76 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300738 };
739
740 /* Chan = [ Freq (Mhz) - Bottom of Band (MHz) ] / Spacing (kHz) */
741 chan = (freq - band_bottom) / spacing;
742
743 return si470x_set_chan(radio, chan);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300744}
745
746
747/*
Tobias Lorenzbbc89952008-05-31 15:11:32 -0300748 * si470x_set_seek - set seek
749 */
750static int si470x_set_seek(struct si470x_device *radio,
751 unsigned int wrap_around, unsigned int seek_upward)
752{
753 int retval = 0;
754 unsigned long timeout;
755 bool timed_out = 0;
756
757 /* start seeking */
758 radio->registers[POWERCFG] |= POWERCFG_SEEK;
759 if (wrap_around == 1)
760 radio->registers[POWERCFG] &= ~POWERCFG_SKMODE;
761 else
762 radio->registers[POWERCFG] |= POWERCFG_SKMODE;
763 if (seek_upward == 1)
764 radio->registers[POWERCFG] |= POWERCFG_SEEKUP;
765 else
766 radio->registers[POWERCFG] &= ~POWERCFG_SEEKUP;
767 retval = si470x_set_register(radio, POWERCFG);
768 if (retval < 0)
769 goto done;
770
771 /* wait till seek operation has completed */
772 timeout = jiffies + msecs_to_jiffies(seek_timeout);
773 do {
774 retval = si470x_get_register(radio, STATUSRSSI);
775 if (retval < 0)
776 goto stop;
777 timed_out = time_after(jiffies, timeout);
778 } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
779 (!timed_out));
780 if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
781 printk(KERN_WARNING DRIVER_NAME ": seek does not complete\n");
782 if (radio->registers[STATUSRSSI] & STATUSRSSI_SF)
783 printk(KERN_WARNING DRIVER_NAME
784 ": seek failed / band limit reached\n");
785 if (timed_out)
786 printk(KERN_WARNING DRIVER_NAME
787 ": seek timed out after %u ms\n", seek_timeout);
788
789stop:
790 /* stop seeking */
791 radio->registers[POWERCFG] &= ~POWERCFG_SEEK;
792 retval = si470x_set_register(radio, POWERCFG);
793
794done:
795 /* try again, if timed out */
796 if ((retval == 0) && timed_out)
797 retval = -EAGAIN;
798
799 return retval;
800}
801
802
803/*
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300804 * si470x_start - switch on radio
805 */
806static int si470x_start(struct si470x_device *radio)
807{
808 int retval;
809
810 /* powercfg */
811 radio->registers[POWERCFG] =
812 POWERCFG_DMUTE | POWERCFG_ENABLE | POWERCFG_RDSM;
813 retval = si470x_set_register(radio, POWERCFG);
814 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300815 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300816
817 /* sysconfig 1 */
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300818 radio->registers[SYSCONFIG1] = SYSCONFIG1_DE;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300819 retval = si470x_set_register(radio, SYSCONFIG1);
820 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300821 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300822
823 /* sysconfig 2 */
824 radio->registers[SYSCONFIG2] =
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300825 (0x3f << 8) | /* SEEKTH */
826 ((band << 6) & SYSCONFIG2_BAND) | /* BAND */
827 ((space << 4) & SYSCONFIG2_SPACE) | /* SPACE */
828 15; /* VOLUME (max) */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300829 retval = si470x_set_register(radio, SYSCONFIG2);
830 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300831 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300832
833 /* reset last channel */
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300834 retval = si470x_set_chan(radio,
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300835 radio->registers[CHANNEL] & CHANNEL_CHAN);
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300836
837done:
838 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300839}
840
841
842/*
843 * si470x_stop - switch off radio
844 */
845static int si470x_stop(struct si470x_device *radio)
846{
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300847 int retval;
848
849 /* sysconfig 1 */
850 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
851 retval = si470x_set_register(radio, SYSCONFIG1);
852 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300853 goto done;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300854
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300855 /* powercfg */
856 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
857 /* POWERCFG_ENABLE has to automatically go low */
858 radio->registers[POWERCFG] |= POWERCFG_ENABLE | POWERCFG_DISABLE;
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300859 retval = si470x_set_register(radio, POWERCFG);
860
861done:
862 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300863}
864
865
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300866/*
867 * si470x_rds_on - switch on rds reception
868 */
869static int si470x_rds_on(struct si470x_device *radio)
870{
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300871 int retval;
872
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300873 /* sysconfig 1 */
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300874 mutex_lock(&radio->lock);
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300875 radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDS;
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300876 retval = si470x_set_register(radio, SYSCONFIG1);
877 if (retval < 0)
878 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
879 mutex_unlock(&radio->lock);
880
881 return retval;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300882}
883
884
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300885
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300886/**************************************************************************
887 * RDS Driver Functions
888 **************************************************************************/
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300889
890/*
891 * si470x_rds - rds processing function
892 */
893static void si470x_rds(struct si470x_device *radio)
894{
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300895 unsigned char blocknum;
896 unsigned short bler; /* rds block errors */
897 unsigned short rds;
898 unsigned char tmpbuf[3];
899
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300900 /* get rds blocks */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300901 if (si470x_get_rds_registers(radio) < 0)
902 return;
903 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0) {
904 /* No RDS group ready */
905 return;
906 }
907 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSS) == 0) {
908 /* RDS decoder not synchronized */
909 return;
910 }
911
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300912 /* copy all four RDS blocks to internal buffer */
913 mutex_lock(&radio->lock);
914 for (blocknum = 0; blocknum < 4; blocknum++) {
915 switch (blocknum) {
916 default:
917 bler = (radio->registers[STATUSRSSI] &
918 STATUSRSSI_BLERA) >> 9;
919 rds = radio->registers[RDSA];
920 break;
921 case 1:
922 bler = (radio->registers[READCHAN] &
923 READCHAN_BLERB) >> 14;
924 rds = radio->registers[RDSB];
925 break;
926 case 2:
927 bler = (radio->registers[READCHAN] &
928 READCHAN_BLERC) >> 12;
929 rds = radio->registers[RDSC];
930 break;
931 case 3:
932 bler = (radio->registers[READCHAN] &
933 READCHAN_BLERD) >> 10;
934 rds = radio->registers[RDSD];
935 break;
936 };
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300937
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300938 /* Fill the V4L2 RDS buffer */
Harvey Harrison5ad6b812008-04-15 22:22:11 -0300939 put_unaligned_le16(rds, &tmpbuf);
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300940 tmpbuf[2] = blocknum; /* offset name */
941 tmpbuf[2] |= blocknum << 3; /* received offset */
942 if (bler > max_rds_errors)
943 tmpbuf[2] |= 0x80; /* uncorrectable errors */
944 else if (bler > 0)
945 tmpbuf[2] |= 0x40; /* corrected error(s) */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300946
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300947 /* copy RDS block to internal buffer */
948 memcpy(&radio->buffer[radio->wr_index], &tmpbuf, 3);
949 radio->wr_index += 3;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300950
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300951 /* wrap write pointer */
952 if (radio->wr_index >= radio->buf_size)
953 radio->wr_index = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300954
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300955 /* check for overflow */
956 if (radio->wr_index == radio->rd_index) {
957 /* increment and wrap read pointer */
958 radio->rd_index += 3;
959 if (radio->rd_index >= radio->buf_size)
960 radio->rd_index = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300961 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300962 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300963 mutex_unlock(&radio->lock);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300964
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300965 /* wake up read queue */
966 if (radio->wr_index != radio->rd_index)
967 wake_up_interruptible(&radio->read_queue);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300968}
969
970
971/*
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300972 * si470x_work - rds work function
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300973 */
974static void si470x_work(struct work_struct *work)
975{
976 struct si470x_device *radio = container_of(work, struct si470x_device,
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300977 work.work);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300978
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300979 /* safety checks */
Tobias Lorenzce5829e2008-05-31 15:04:32 -0300980 if (radio->disconnected)
981 return;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300982 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300983 return;
984
985 si470x_rds(radio);
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300986 schedule_delayed_work(&radio->work, msecs_to_jiffies(rds_poll_time));
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300987}
988
989
990
991/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300992 * File Operations Interface
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300993 **************************************************************************/
994
995/*
996 * si470x_fops_read - read RDS data
997 */
998static ssize_t si470x_fops_read(struct file *file, char __user *buf,
999 size_t count, loff_t *ppos)
1000{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001001 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001002 int retval = 0;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001003 unsigned int block_count = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001004
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001005 /* switch on rds reception */
1006 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
1007 si470x_rds_on(radio);
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001008 schedule_delayed_work(&radio->work,
1009 msecs_to_jiffies(rds_poll_time));
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001010 }
1011
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001012 /* block if no new data available */
1013 while (radio->wr_index == radio->rd_index) {
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001014 if (file->f_flags & O_NONBLOCK) {
1015 retval = -EWOULDBLOCK;
1016 goto done;
1017 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001018 if (wait_event_interruptible(radio->read_queue,
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001019 radio->wr_index != radio->rd_index) < 0) {
1020 retval = -EINTR;
1021 goto done;
1022 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001023 }
1024
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001025 /* calculate block count from byte count */
1026 count /= 3;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001027
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001028 /* copy RDS block out of internal buffer and to user buffer */
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001029 mutex_lock(&radio->lock);
1030 while (block_count < count) {
1031 if (radio->rd_index == radio->wr_index)
1032 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001033
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001034 /* always transfer rds complete blocks */
1035 if (copy_to_user(buf, &radio->buffer[radio->rd_index], 3))
1036 /* retval = -EFAULT; */
1037 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001038
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001039 /* increment and wrap read pointer */
1040 radio->rd_index += 3;
1041 if (radio->rd_index >= radio->buf_size)
1042 radio->rd_index = 0;
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001043
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001044 /* increment counters */
1045 block_count++;
1046 buf += 3;
1047 retval += 3;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001048 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001049 mutex_unlock(&radio->lock);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001050
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001051done:
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001052 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001053}
1054
1055
1056/*
1057 * si470x_fops_poll - poll RDS data
1058 */
1059static unsigned int si470x_fops_poll(struct file *file,
1060 struct poll_table_struct *pts)
1061{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001062 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001063 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001064
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001065 /* switch on rds reception */
1066 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
1067 si470x_rds_on(radio);
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001068 schedule_delayed_work(&radio->work,
1069 msecs_to_jiffies(rds_poll_time));
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001070 }
1071
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001072 poll_wait(file, &radio->read_queue, pts);
1073
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001074 if (radio->rd_index != radio->wr_index)
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001075 retval = POLLIN | POLLRDNORM;
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001076
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001077 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001078}
1079
1080
1081/*
1082 * si470x_fops_open - file open
1083 */
1084static int si470x_fops_open(struct inode *inode, struct file *file)
1085{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001086 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001087 int retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001088
Hans Verkuild56dc612008-07-30 08:43:36 -03001089 lock_kernel();
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001090 radio->users++;
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001091
1092 retval = usb_autopm_get_interface(radio->intf);
1093 if (retval < 0) {
1094 radio->users--;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001095 retval = -EIO;
1096 goto done;
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001097 }
1098
1099 if (radio->users == 1) {
1100 retval = si470x_start(radio);
1101 if (retval < 0)
1102 usb_autopm_put_interface(radio->intf);
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001103 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001104
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001105done:
Hans Verkuild56dc612008-07-30 08:43:36 -03001106 unlock_kernel();
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001107 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001108}
1109
1110
1111/*
1112 * si470x_fops_release - file release
1113 */
1114static int si470x_fops_release(struct inode *inode, struct file *file)
1115{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001116 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001117 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001118
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001119 /* safety check */
1120 if (!radio) {
1121 retval = -ENODEV;
1122 goto done;
1123 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001124
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001125 mutex_lock(&radio->disconnect_lock);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001126 radio->users--;
1127 if (radio->users == 0) {
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001128 if (radio->disconnected) {
1129 video_unregister_device(radio->videodev);
1130 kfree(radio->buffer);
1131 kfree(radio);
1132 goto unlock;
1133 }
Tobias Lorenz03dea862008-04-22 14:46:11 -03001134
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001135 /* stop rds reception */
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001136 cancel_delayed_work_sync(&radio->work);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001137
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001138 /* cancel read processes */
1139 wake_up_interruptible(&radio->read_queue);
1140
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001141 retval = si470x_stop(radio);
1142 usb_autopm_put_interface(radio->intf);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001143 }
1144
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001145unlock:
1146 mutex_unlock(&radio->disconnect_lock);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001147
1148done:
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001149 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001150}
1151
1152
1153/*
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001154 * si470x_fops - file operations interface
1155 */
1156static const struct file_operations si470x_fops = {
1157 .owner = THIS_MODULE,
1158 .llseek = no_llseek,
1159 .read = si470x_fops_read,
1160 .poll = si470x_fops_poll,
1161 .ioctl = video_ioctl2,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -03001162#ifdef CONFIG_COMPAT
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001163 .compat_ioctl = v4l_compat_ioctl32,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -03001164#endif
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001165 .open = si470x_fops_open,
1166 .release = si470x_fops_release,
1167};
1168
1169
1170
1171/**************************************************************************
1172 * Video4Linux Interface
1173 **************************************************************************/
1174
1175/*
1176 * si470x_v4l2_queryctrl - query control
1177 */
1178static struct v4l2_queryctrl si470x_v4l2_queryctrl[] = {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001179 {
1180 .id = V4L2_CID_AUDIO_VOLUME,
1181 .type = V4L2_CTRL_TYPE_INTEGER,
1182 .name = "Volume",
1183 .minimum = 0,
1184 .maximum = 15,
1185 .step = 1,
1186 .default_value = 15,
1187 },
1188 {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001189 .id = V4L2_CID_AUDIO_MUTE,
1190 .type = V4L2_CTRL_TYPE_BOOLEAN,
1191 .name = "Mute",
1192 .minimum = 0,
1193 .maximum = 1,
1194 .step = 1,
1195 .default_value = 1,
1196 },
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001197};
1198
1199
1200/*
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001201 * si470x_vidioc_querycap - query device capabilities
1202 */
1203static int si470x_vidioc_querycap(struct file *file, void *priv,
1204 struct v4l2_capability *capability)
1205{
1206 strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver));
1207 strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
1208 sprintf(capability->bus_info, "USB");
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001209 capability->version = DRIVER_KERNEL_VERSION;
Tobias Lorenzbbc89952008-05-31 15:11:32 -03001210 capability->capabilities = V4L2_CAP_HW_FREQ_SEEK |
1211 V4L2_CAP_TUNER | V4L2_CAP_RADIO;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001212
1213 return 0;
1214}
1215
1216
1217/*
1218 * si470x_vidioc_g_input - get input
1219 */
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001220static int si470x_vidioc_g_input(struct file *file, void *priv,
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001221 unsigned int *i)
1222{
1223 *i = 0;
1224
1225 return 0;
1226}
1227
1228
1229/*
1230 * si470x_vidioc_s_input - set input
1231 */
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001232static int si470x_vidioc_s_input(struct file *file, void *priv, unsigned int i)
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001233{
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001234 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001235
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001236 /* safety checks */
1237 if (i != 0)
1238 retval = -EINVAL;
1239
1240 if (retval < 0)
1241 printk(KERN_WARNING DRIVER_NAME
1242 ": set input failed with %d\n", retval);
1243 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001244}
1245
1246
1247/*
1248 * si470x_vidioc_queryctrl - enumerate control items
1249 */
1250static int si470x_vidioc_queryctrl(struct file *file, void *priv,
1251 struct v4l2_queryctrl *qc)
1252{
Tobias Lorenzeb6e3b82008-09-24 19:25:39 -03001253 unsigned char i = 0;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001254 int retval = -EINVAL;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001255
Tobias Lorenzeb6e3b82008-09-24 19:25:39 -03001256 /* abort if qc->id is below V4L2_CID_BASE */
1257 if (qc->id < V4L2_CID_BASE)
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001258 goto done;
1259
Tobias Lorenzeb6e3b82008-09-24 19:25:39 -03001260 /* search video control */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001261 for (i = 0; i < ARRAY_SIZE(si470x_v4l2_queryctrl); i++) {
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001262 if (qc->id == si470x_v4l2_queryctrl[i].id) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001263 memcpy(qc, &(si470x_v4l2_queryctrl[i]), sizeof(*qc));
Tobias Lorenzeb6e3b82008-09-24 19:25:39 -03001264 retval = 0; /* found */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001265 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001266 }
1267 }
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001268
Tobias Lorenzeb6e3b82008-09-24 19:25:39 -03001269 /* disable unsupported base controls */
1270 /* to satisfy kradio and such apps */
1271 if ((retval == -EINVAL) && (qc->id < V4L2_CID_LASTP1)) {
1272 qc->flags = V4L2_CTRL_FLAG_DISABLED;
1273 retval = 0;
1274 }
1275
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001276done:
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001277 if (retval < 0)
1278 printk(KERN_WARNING DRIVER_NAME
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001279 ": query controls failed with %d\n", retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001280 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001281}
1282
1283
1284/*
1285 * si470x_vidioc_g_ctrl - get the value of a control
1286 */
1287static int si470x_vidioc_g_ctrl(struct file *file, void *priv,
1288 struct v4l2_control *ctrl)
1289{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001290 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001291 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001292
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001293 /* safety checks */
1294 if (radio->disconnected) {
1295 retval = -EIO;
1296 goto done;
1297 }
Tobias Lorenz03dea862008-04-22 14:46:11 -03001298
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001299 switch (ctrl->id) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001300 case V4L2_CID_AUDIO_VOLUME:
1301 ctrl->value = radio->registers[SYSCONFIG2] &
1302 SYSCONFIG2_VOLUME;
1303 break;
1304 case V4L2_CID_AUDIO_MUTE:
1305 ctrl->value = ((radio->registers[POWERCFG] &
1306 POWERCFG_DMUTE) == 0) ? 1 : 0;
1307 break;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001308 default:
1309 retval = -EINVAL;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001310 }
1311
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001312done:
1313 if (retval < 0)
1314 printk(KERN_WARNING DRIVER_NAME
1315 ": get control failed with %d\n", retval);
1316 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001317}
1318
1319
1320/*
1321 * si470x_vidioc_s_ctrl - set the value of a control
1322 */
1323static int si470x_vidioc_s_ctrl(struct file *file, void *priv,
1324 struct v4l2_control *ctrl)
1325{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001326 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001327 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001328
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001329 /* safety checks */
1330 if (radio->disconnected) {
1331 retval = -EIO;
1332 goto done;
1333 }
Tobias Lorenz03dea862008-04-22 14:46:11 -03001334
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001335 switch (ctrl->id) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001336 case V4L2_CID_AUDIO_VOLUME:
1337 radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_VOLUME;
1338 radio->registers[SYSCONFIG2] |= ctrl->value;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001339 retval = si470x_set_register(radio, SYSCONFIG2);
1340 break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001341 case V4L2_CID_AUDIO_MUTE:
1342 if (ctrl->value == 1)
1343 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
1344 else
1345 radio->registers[POWERCFG] |= POWERCFG_DMUTE;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001346 retval = si470x_set_register(radio, POWERCFG);
1347 break;
1348 default:
1349 retval = -EINVAL;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001350 }
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001351
1352done:
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001353 if (retval < 0)
1354 printk(KERN_WARNING DRIVER_NAME
1355 ": set control failed with %d\n", retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001356 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001357}
1358
1359
1360/*
1361 * si470x_vidioc_g_audio - get audio attributes
1362 */
1363static int si470x_vidioc_g_audio(struct file *file, void *priv,
1364 struct v4l2_audio *audio)
1365{
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001366 int retval = 0;
1367
1368 /* safety checks */
1369 if (audio->index != 0) {
1370 retval = -EINVAL;
1371 goto done;
1372 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001373
1374 strcpy(audio->name, "Radio");
1375 audio->capability = V4L2_AUDCAP_STEREO;
1376
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001377done:
1378 if (retval < 0)
1379 printk(KERN_WARNING DRIVER_NAME
1380 ": get audio failed with %d\n", retval);
1381 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001382}
1383
1384
1385/*
1386 * si470x_vidioc_s_audio - set audio attributes
1387 */
1388static int si470x_vidioc_s_audio(struct file *file, void *priv,
1389 struct v4l2_audio *audio)
1390{
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001391 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001392
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001393 /* safety checks */
1394 if (audio->index != 0) {
1395 retval = -EINVAL;
1396 goto done;
1397 }
1398
1399done:
1400 if (retval < 0)
1401 printk(KERN_WARNING DRIVER_NAME
1402 ": set audio failed with %d\n", retval);
1403 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001404}
1405
1406
1407/*
1408 * si470x_vidioc_g_tuner - get tuner attributes
1409 */
1410static int si470x_vidioc_g_tuner(struct file *file, void *priv,
1411 struct v4l2_tuner *tuner)
1412{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001413 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001414 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001415
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001416 /* safety checks */
1417 if (radio->disconnected) {
1418 retval = -EIO;
1419 goto done;
1420 }
1421 if ((tuner->index != 0) && (tuner->type != V4L2_TUNER_RADIO)) {
1422 retval = -EINVAL;
1423 goto done;
1424 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001425
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001426 retval = si470x_get_register(radio, STATUSRSSI);
1427 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001428 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001429
1430 strcpy(tuner->name, "FM");
Tobias Lorenza17c0012008-09-24 19:21:50 -03001431 /* range limits */
1432 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001433 /* 0: 87.5 - 108 MHz (USA, Europe, default) */
1434 default:
1435 tuner->rangelow = 87.5 * FREQ_MUL;
1436 tuner->rangehigh = 108 * FREQ_MUL;
1437 break;
1438 /* 1: 76 - 108 MHz (Japan wide band) */
1439 case 1 :
1440 tuner->rangelow = 76 * FREQ_MUL;
1441 tuner->rangehigh = 108 * FREQ_MUL;
1442 break;
1443 /* 2: 76 - 90 MHz (Japan) */
1444 case 2 :
1445 tuner->rangelow = 76 * FREQ_MUL;
1446 tuner->rangehigh = 90 * FREQ_MUL;
1447 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001448 };
1449 tuner->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
1450 tuner->capability = V4L2_TUNER_CAP_LOW;
1451
1452 /* Stereo indicator == Stereo (instead of Mono) */
1453 if ((radio->registers[STATUSRSSI] & STATUSRSSI_ST) == 1)
1454 tuner->audmode = V4L2_TUNER_MODE_STEREO;
1455 else
1456 tuner->audmode = V4L2_TUNER_MODE_MONO;
1457
1458 /* min is worst, max is best; signal:0..0xffff; rssi: 0..0xff */
1459 tuner->signal = (radio->registers[STATUSRSSI] & STATUSRSSI_RSSI)
1460 * 0x0101;
1461
1462 /* automatic frequency control: -1: freq to low, 1 freq to high */
Tobias Lorenzace7d4b2008-05-31 15:09:07 -03001463 /* AFCRL does only indicate that freq. differs, not if too low/high */
1464 tuner->afc = (radio->registers[STATUSRSSI] & STATUSRSSI_AFCRL) ? 1 : 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001465
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001466done:
1467 if (retval < 0)
1468 printk(KERN_WARNING DRIVER_NAME
1469 ": get tuner failed with %d\n", retval);
1470 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001471}
1472
1473
1474/*
1475 * si470x_vidioc_s_tuner - set tuner attributes
1476 */
1477static int si470x_vidioc_s_tuner(struct file *file, void *priv,
1478 struct v4l2_tuner *tuner)
1479{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001480 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001481 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001482
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001483 /* safety checks */
1484 if (radio->disconnected) {
1485 retval = -EIO;
1486 goto done;
1487 }
1488 if ((tuner->index != 0) && (tuner->type != V4L2_TUNER_RADIO)) {
1489 retval = -EINVAL;
1490 goto done;
1491 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001492
1493 if (tuner->audmode == V4L2_TUNER_MODE_MONO)
1494 radio->registers[POWERCFG] |= POWERCFG_MONO; /* force mono */
1495 else
1496 radio->registers[POWERCFG] &= ~POWERCFG_MONO; /* try stereo */
1497
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001498 retval = si470x_set_register(radio, POWERCFG);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001499
1500done:
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001501 if (retval < 0)
1502 printk(KERN_WARNING DRIVER_NAME
1503 ": set tuner failed with %d\n", retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001504 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001505}
1506
1507
1508/*
1509 * si470x_vidioc_g_frequency - get tuner or modulator radio frequency
1510 */
1511static int si470x_vidioc_g_frequency(struct file *file, void *priv,
1512 struct v4l2_frequency *freq)
1513{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001514 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001515 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001516
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001517 /* safety checks */
1518 if (radio->disconnected) {
1519 retval = -EIO;
1520 goto done;
1521 }
1522 if ((freq->tuner != 0) && (freq->type != V4L2_TUNER_RADIO)) {
1523 retval = -EINVAL;
1524 goto done;
1525 }
Tobias Lorenz03dea862008-04-22 14:46:11 -03001526
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001527 retval = si470x_get_freq(radio, &freq->frequency);
1528
1529done:
1530 if (retval < 0)
1531 printk(KERN_WARNING DRIVER_NAME
1532 ": get frequency failed with %d\n", retval);
1533 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001534}
1535
1536
1537/*
1538 * si470x_vidioc_s_frequency - set tuner or modulator radio frequency
1539 */
1540static int si470x_vidioc_s_frequency(struct file *file, void *priv,
1541 struct v4l2_frequency *freq)
1542{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001543 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001544 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001545
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001546 /* safety checks */
1547 if (radio->disconnected) {
1548 retval = -EIO;
1549 goto done;
1550 }
1551 if ((freq->tuner != 0) && (freq->type != V4L2_TUNER_RADIO)) {
1552 retval = -EINVAL;
1553 goto done;
1554 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001555
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001556 retval = si470x_set_freq(radio, freq->frequency);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001557
1558done:
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001559 if (retval < 0)
1560 printk(KERN_WARNING DRIVER_NAME
1561 ": set frequency failed with %d\n", retval);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001562 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001563}
1564
1565
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001566/*
Tobias Lorenzbbc89952008-05-31 15:11:32 -03001567 * si470x_vidioc_s_hw_freq_seek - set hardware frequency seek
1568 */
1569static int si470x_vidioc_s_hw_freq_seek(struct file *file, void *priv,
1570 struct v4l2_hw_freq_seek *seek)
1571{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001572 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenzbbc89952008-05-31 15:11:32 -03001573 int retval = 0;
1574
1575 /* safety checks */
1576 if (radio->disconnected) {
1577 retval = -EIO;
1578 goto done;
1579 }
1580 if ((seek->tuner != 0) && (seek->type != V4L2_TUNER_RADIO)) {
1581 retval = -EINVAL;
1582 goto done;
1583 }
1584
1585 retval = si470x_set_seek(radio, seek->wrap_around, seek->seek_upward);
1586
1587done:
1588 if (retval < 0)
1589 printk(KERN_WARNING DRIVER_NAME
1590 ": set hardware frequency seek failed with %d\n",
1591 retval);
1592 return retval;
1593}
1594
Tobias Lorenz374ab682008-09-24 19:17:54 -03001595
1596/*
1597 * si470x_ioctl_ops - video device ioctl operations
1598 */
Hans Verkuila3998102008-07-21 02:57:38 -03001599static const struct v4l2_ioctl_ops si470x_ioctl_ops = {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001600 .vidioc_querycap = si470x_vidioc_querycap,
1601 .vidioc_g_input = si470x_vidioc_g_input,
1602 .vidioc_s_input = si470x_vidioc_s_input,
1603 .vidioc_queryctrl = si470x_vidioc_queryctrl,
1604 .vidioc_g_ctrl = si470x_vidioc_g_ctrl,
1605 .vidioc_s_ctrl = si470x_vidioc_s_ctrl,
1606 .vidioc_g_audio = si470x_vidioc_g_audio,
1607 .vidioc_s_audio = si470x_vidioc_s_audio,
1608 .vidioc_g_tuner = si470x_vidioc_g_tuner,
1609 .vidioc_s_tuner = si470x_vidioc_s_tuner,
1610 .vidioc_g_frequency = si470x_vidioc_g_frequency,
1611 .vidioc_s_frequency = si470x_vidioc_s_frequency,
Tobias Lorenzbbc89952008-05-31 15:11:32 -03001612 .vidioc_s_hw_freq_seek = si470x_vidioc_s_hw_freq_seek,
Hans Verkuila3998102008-07-21 02:57:38 -03001613};
1614
Tobias Lorenz374ab682008-09-24 19:17:54 -03001615
Hans Verkuila3998102008-07-21 02:57:38 -03001616/*
Tobias Lorenz374ab682008-09-24 19:17:54 -03001617 * si470x_viddev_template - video device interface
Hans Verkuila3998102008-07-21 02:57:38 -03001618 */
1619static struct video_device si470x_viddev_template = {
1620 .fops = &si470x_fops,
Hans Verkuila3998102008-07-21 02:57:38 -03001621 .name = DRIVER_NAME,
Hans Verkuila3998102008-07-21 02:57:38 -03001622 .release = video_device_release,
Tobias Lorenz374ab682008-09-24 19:17:54 -03001623 .ioctl_ops = &si470x_ioctl_ops,
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001624};
1625
1626
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001627
1628/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001629 * USB Interface
1630 **************************************************************************/
1631
1632/*
1633 * si470x_usb_driver_probe - probe for the device
1634 */
1635static int si470x_usb_driver_probe(struct usb_interface *intf,
1636 const struct usb_device_id *id)
1637{
1638 struct si470x_device *radio;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001639 int retval = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001640
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001641 /* private data allocation and initialization */
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001642 radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001643 if (!radio) {
1644 retval = -ENOMEM;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001645 goto err_initial;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001646 }
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001647 radio->users = 0;
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001648 radio->disconnected = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001649 radio->usbdev = interface_to_usbdev(intf);
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001650 radio->intf = intf;
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001651 mutex_init(&radio->disconnect_lock);
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001652 mutex_init(&radio->lock);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001653
1654 /* video device allocation and initialization */
1655 radio->videodev = video_device_alloc();
1656 if (!radio->videodev) {
1657 retval = -ENOMEM;
1658 goto err_radio;
1659 }
1660 memcpy(radio->videodev, &si470x_viddev_template,
1661 sizeof(si470x_viddev_template));
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001662 video_set_drvdata(radio->videodev, radio);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001663
1664 /* show some infos about the specific device */
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001665 if (si470x_get_all_registers(radio) < 0) {
1666 retval = -EIO;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001667 goto err_all;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001668 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001669 printk(KERN_INFO DRIVER_NAME ": DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001670 radio->registers[DEVICEID], radio->registers[CHIPID]);
1671
1672 /* check if firmware is current */
1673 if ((radio->registers[CHIPID] & CHIPID_FIRMWARE)
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001674 < RADIO_SW_VERSION_CURRENT) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001675 printk(KERN_WARNING DRIVER_NAME
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001676 ": This driver is known to work with "
1677 "firmware version %hu,\n", RADIO_SW_VERSION_CURRENT);
1678 printk(KERN_WARNING DRIVER_NAME
1679 ": but the device has firmware version %hu.\n",
1680 radio->registers[CHIPID] & CHIPID_FIRMWARE);
1681 printk(KERN_WARNING DRIVER_NAME
1682 ": If you have some trouble using this driver,\n");
1683 printk(KERN_WARNING DRIVER_NAME
1684 ": please report to V4L ML at "
1685 "video4linux-list@redhat.com\n");
1686 }
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001687
1688 /* set initial frequency */
1689 si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
1690
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001691 /* rds buffer allocation */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001692 radio->buf_size = rds_buf * 3;
1693 radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001694 if (!radio->buffer) {
1695 retval = -EIO;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001696 goto err_all;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001697 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001698
1699 /* rds buffer configuration */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001700 radio->wr_index = 0;
1701 radio->rd_index = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001702 init_waitqueue_head(&radio->read_queue);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001703
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001704 /* prepare rds work function */
1705 INIT_DELAYED_WORK(&radio->work, si470x_work);
1706
1707 /* register video device */
Hans Verkuilcba99ae2008-09-03 17:11:58 -03001708 retval = video_register_device(radio->videodev, VFL_TYPE_RADIO, radio_nr);
1709 if (retval) {
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001710 printk(KERN_WARNING DRIVER_NAME
1711 ": Could not register video device\n");
1712 goto err_all;
1713 }
1714 usb_set_intfdata(intf, radio);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001715
1716 return 0;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001717err_all:
1718 video_device_release(radio->videodev);
1719 kfree(radio->buffer);
1720err_radio:
1721 kfree(radio);
1722err_initial:
1723 return retval;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001724}
1725
1726
1727/*
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001728 * si470x_usb_driver_suspend - suspend the device
1729 */
1730static int si470x_usb_driver_suspend(struct usb_interface *intf,
1731 pm_message_t message)
1732{
1733 struct si470x_device *radio = usb_get_intfdata(intf);
1734
1735 printk(KERN_INFO DRIVER_NAME ": suspending now...\n");
1736
1737 cancel_delayed_work_sync(&radio->work);
1738
1739 return 0;
1740}
1741
1742
1743/*
1744 * si470x_usb_driver_resume - resume the device
1745 */
1746static int si470x_usb_driver_resume(struct usb_interface *intf)
1747{
1748 struct si470x_device *radio = usb_get_intfdata(intf);
1749
1750 printk(KERN_INFO DRIVER_NAME ": resuming now...\n");
1751
1752 mutex_lock(&radio->lock);
1753 if (radio->users && radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS)
1754 schedule_delayed_work(&radio->work,
1755 msecs_to_jiffies(rds_poll_time));
1756 mutex_unlock(&radio->lock);
1757
1758 return 0;
1759}
1760
1761
1762/*
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001763 * si470x_usb_driver_disconnect - disconnect the device
1764 */
1765static void si470x_usb_driver_disconnect(struct usb_interface *intf)
1766{
1767 struct si470x_device *radio = usb_get_intfdata(intf);
1768
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001769 mutex_lock(&radio->disconnect_lock);
1770 radio->disconnected = 1;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001771 cancel_delayed_work_sync(&radio->work);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001772 usb_set_intfdata(intf, NULL);
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001773 if (radio->users == 0) {
1774 video_unregister_device(radio->videodev);
1775 kfree(radio->buffer);
1776 kfree(radio);
1777 }
1778 mutex_unlock(&radio->disconnect_lock);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001779}
1780
1781
1782/*
1783 * si470x_usb_driver - usb driver interface
1784 */
1785static struct usb_driver si470x_usb_driver = {
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001786 .name = DRIVER_NAME,
1787 .probe = si470x_usb_driver_probe,
1788 .disconnect = si470x_usb_driver_disconnect,
1789 .suspend = si470x_usb_driver_suspend,
1790 .resume = si470x_usb_driver_resume,
1791 .id_table = si470x_usb_driver_id_table,
1792 .supports_autosuspend = 1,
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001793};
1794
1795
1796
1797/**************************************************************************
1798 * Module Interface
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001799 **************************************************************************/
1800
1801/*
1802 * si470x_module_init - module init
1803 */
1804static int __init si470x_module_init(void)
1805{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001806 printk(KERN_INFO DRIVER_DESC ", Version " DRIVER_VERSION "\n");
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001807 return usb_register(&si470x_usb_driver);
1808}
1809
1810
1811/*
1812 * si470x_module_exit - module exit
1813 */
1814static void __exit si470x_module_exit(void)
1815{
1816 usb_deregister(&si470x_usb_driver);
1817}
1818
1819
1820module_init(si470x_module_init);
1821module_exit(si470x_module_exit);
1822
1823MODULE_LICENSE("GPL");
1824MODULE_AUTHOR(DRIVER_AUTHOR);
1825MODULE_DESCRIPTION(DRIVER_DESC);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001826MODULE_VERSION(DRIVER_VERSION);