blob: a20583c13b684768837dcbeb11159df9c6639bf5 [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)
Alexey Klimov69df96c2008-10-23 09:20:27 -03007 * - KWorld USB FM Radio SnapMusic Mobile 700 (FM700)
Tobias Lorenz78656ac2008-01-14 21:55:27 -03008 *
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03009 * Copyright (c) 2008 Tobias Lorenz <tobias.lorenz@gmx.net>
Tobias Lorenz78656ac2008-01-14 21:55:27 -030010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25
26
27/*
Tobias Lorenz6cc72652008-05-31 15:06:50 -030028 * User Notes:
29 * - USB Audio is provided by the alsa snd_usb_audio module.
30 * For listing you have to redirect the sound, for example using:
31 * arecord -D hw:1,0 -r96000 -c2 -f S16_LE | artsdsp aplay -B -
32 * - regarding module parameters in /sys/module/radio_si470x/parameters:
33 * the contents of read-only files (0444) are not updated, even if
34 * space, band and de are changed using private video controls
35 * - increase tune_timeout, if you often get -EIO errors
36 * - hw_freq_seek returns -EAGAIN, when timed out or band limit is reached
37 */
38
39
40/*
Tobias Lorenz78656ac2008-01-14 21:55:27 -030041 * History:
42 * 2008-01-12 Tobias Lorenz <tobias.lorenz@gmx.net>
43 * Version 1.0.0
44 * - First working version
45 * 2008-01-13 Tobias Lorenz <tobias.lorenz@gmx.net>
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -030046 * Version 1.0.1
Tobias Lorenz78656ac2008-01-14 21:55:27 -030047 * - Improved error handling, every function now returns errno
48 * - Improved multi user access (start/mute/stop)
49 * - Channel doesn't get lost anymore after start/mute/stop
50 * - RDS support added (polling mode via interrupt EP 1)
51 * - marked default module parameters with *value*
52 * - switched from bit structs to bit masks
53 * - header file cleaned and integrated
54 * 2008-01-14 Tobias Lorenz <tobias.lorenz@gmx.net>
55 * Version 1.0.2
56 * - hex values are now lower case
57 * - commented USB ID for ADS/Tech moved on todo list
58 * - blacklisted si470x in hid-quirks.c
59 * - rds buffer handling functions integrated into *_work, *_read
60 * - rds_command in si470x_poll exchanged against simple retval
61 * - check for firmware version 15
62 * - code order and prototypes still remain the same
63 * - spacing and bottom of band codes remain the same
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -030064 * 2008-01-16 Tobias Lorenz <tobias.lorenz@gmx.net>
65 * Version 1.0.3
66 * - code reordered to avoid function prototypes
67 * - switch/case defaults are now more user-friendly
68 * - unified comment style
69 * - applied all checkpatch.pl v1.12 suggestions
70 * except the warning about the too long lines with bit comments
71 * - renamed FMRADIO to RADIO to cut line length (checkpatch.pl)
Tobias Lorenz2fb88402008-01-25 05:14:57 -030072 * 2008-01-22 Tobias Lorenz <tobias.lorenz@gmx.net>
73 * Version 1.0.4
74 * - avoid poss. locking when doing copy_to_user which may sleep
75 * - RDS is automatically activated on read now
76 * - code cleaned of unnecessary rds_commands
77 * - USB Vendor/Product ID for ADS/Tech FM Radio Receiver verified
78 * (thanks to Guillaume RAMOUSSE)
Tobias Lorenz0e3301e2008-01-27 14:54:07 -030079 * 2008-01-27 Tobias Lorenz <tobias.lorenz@gmx.net>
80 * Version 1.0.5
81 * - number of seek_retries changed to tune_timeout
82 * - fixed problem with incomplete tune operations by own buffers
Tobias Lorenz5caf5132008-02-04 22:26:08 -030083 * - optimization of variables and printf types
Tobias Lorenz0e3301e2008-01-27 14:54:07 -030084 * - improved error logging
Tobias Lorenz5caf5132008-02-04 22:26:08 -030085 * 2008-01-31 Tobias Lorenz <tobias.lorenz@gmx.net>
86 * Oliver Neukum <oliver@neukum.org>
87 * Version 1.0.6
88 * - fixed coverity checker warnings in *_usb_driver_disconnect
89 * - probe()/open() race by correct ordering in probe()
90 * - DMA coherency rules by separate allocation of all buffers
91 * - use of endianness macros
92 * - abuse of spinlock, replaced by mutex
93 * - racy handling of timer in disconnect,
94 * replaced by delayed_work
95 * - racy interruptible_sleep_on(),
96 * replaced with wait_event_interruptible()
97 * - handle signals in read()
Tobias Lorenz57566ad2008-02-09 16:08:24 -030098 * 2008-02-08 Tobias Lorenz <tobias.lorenz@gmx.net>
99 * Oliver Neukum <oliver@neukum.org>
100 * Version 1.0.7
101 * - usb autosuspend support
Tobias Lorenzce5829e2008-05-31 15:04:32 -0300102 * - unplugging fixed
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300103 * 2008-05-07 Tobias Lorenz <tobias.lorenz@gmx.net>
104 * Version 1.0.8
Tobias Lorenzbbc89952008-05-31 15:11:32 -0300105 * - hardware frequency seek support
Tobias Lorenzace7d4b2008-05-31 15:09:07 -0300106 * - afc indication
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300107 * - more safety checks, let si470x_get_freq return errno
Tobias Lorenz33632d42008-09-24 19:30:26 -0300108 * - vidioc behavior corrected according to v4l2 spec
Alexey Klimov69df96c2008-10-23 09:20:27 -0300109 * 2008-10-20 Alexey Klimov <klimov.linux@gmail.com>
110 * - add support for KWorld USB FM Radio FM700
111 * - blacklisted KWorld radio in hid-core.c and hid-ids.h
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300112 *
113 * ToDo:
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300114 * - add firmware download/update support
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300115 * - RDS support: interrupt mode, instead of polling
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300116 * - add LED status output (check if that's not already done in firmware)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300117 */
118
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300119
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300120/* driver definitions */
121#define DRIVER_AUTHOR "Tobias Lorenz <tobias.lorenz@gmx.net>"
122#define DRIVER_NAME "radio-si470x"
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300123#define DRIVER_KERNEL_VERSION KERNEL_VERSION(1, 0, 8)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300124#define DRIVER_CARD "Silicon Labs Si470x FM Radio Receiver"
125#define DRIVER_DESC "USB radio driver for Si470x FM Radio Receivers"
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300126#define DRIVER_VERSION "1.0.8"
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300127
128
129/* kernel includes */
130#include <linux/kernel.h>
131#include <linux/module.h>
132#include <linux/init.h>
133#include <linux/slab.h>
134#include <linux/input.h>
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300135#include <linux/usb.h>
136#include <linux/hid.h>
137#include <linux/version.h>
Mauro Carvalho Chehab387d4472008-01-15 11:25:10 -0300138#include <linux/videodev2.h>
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300139#include <linux/mutex.h>
Mauro Carvalho Chehab387d4472008-01-15 11:25:10 -0300140#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300141#include <media/v4l2-ioctl.h>
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300142#include <media/rds.h>
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300143#include <asm/unaligned.h>
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300144
145
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300146/* USB Device ID List */
147static struct usb_device_id si470x_usb_driver_id_table[] = {
148 /* Silicon Labs USB FM Radio Reference Design */
Tobias Lorenz374ab682008-09-24 19:17:54 -0300149 { USB_DEVICE_AND_INTERFACE_INFO(0x10c4, 0x818a, USB_CLASS_HID, 0, 0) },
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300150 /* ADS/Tech FM Radio Receiver (formerly Instant FM Music) */
Tobias Lorenz374ab682008-09-24 19:17:54 -0300151 { USB_DEVICE_AND_INTERFACE_INFO(0x06e1, 0xa155, USB_CLASS_HID, 0, 0) },
Alexey Klimov69df96c2008-10-23 09:20:27 -0300152 /* KWorld USB FM Radio SnapMusic Mobile 700 (FM700) */
153 { USB_DEVICE_AND_INTERFACE_INFO(0x1b80, 0xd700, USB_CLASS_HID, 0, 0) },
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300154 /* Terminating entry */
155 { }
156};
157MODULE_DEVICE_TABLE(usb, si470x_usb_driver_id_table);
158
159
160
161/**************************************************************************
162 * Module Parameters
163 **************************************************************************/
164
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300165/* Radio Nr */
166static int radio_nr = -1;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300167module_param(radio_nr, int, 0444);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300168MODULE_PARM_DESC(radio_nr, "Radio Nr");
169
170/* Spacing (kHz) */
171/* 0: 200 kHz (USA, Australia) */
172/* 1: 100 kHz (Europe, Japan) */
173/* 2: 50 kHz */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300174static unsigned short space = 2;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300175module_param(space, ushort, 0444);
176MODULE_PARM_DESC(space, "Spacing: 0=200kHz 1=100kHz *2=50kHz*");
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300177
178/* Bottom of Band (MHz) */
179/* 0: 87.5 - 108 MHz (USA, Europe)*/
180/* 1: 76 - 108 MHz (Japan wide band) */
181/* 2: 76 - 90 MHz (Japan) */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300182static unsigned short band = 1;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300183module_param(band, ushort, 0444);
184MODULE_PARM_DESC(band, "Band: 0=87.5..108MHz *1=76..108MHz* 2=76..90MHz");
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300185
186/* De-emphasis */
187/* 0: 75 us (USA) */
188/* 1: 50 us (Europe, Australia, Japan) */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300189static unsigned short de = 1;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300190module_param(de, ushort, 0444);
191MODULE_PARM_DESC(de, "De-emphasis: 0=75us *1=50us*");
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300192
193/* USB timeout */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300194static unsigned int usb_timeout = 500;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300195module_param(usb_timeout, uint, 0644);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300196MODULE_PARM_DESC(usb_timeout, "USB timeout (ms): *500*");
197
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300198/* Tune timeout */
199static unsigned int tune_timeout = 3000;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300200module_param(tune_timeout, uint, 0644);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300201MODULE_PARM_DESC(tune_timeout, "Tune timeout: *3000*");
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300202
Tobias Lorenzbbc89952008-05-31 15:11:32 -0300203/* Seek timeout */
204static unsigned int seek_timeout = 5000;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300205module_param(seek_timeout, uint, 0644);
Tobias Lorenzbbc89952008-05-31 15:11:32 -0300206MODULE_PARM_DESC(seek_timeout, "Seek timeout: *5000*");
207
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300208/* RDS buffer blocks */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300209static unsigned int rds_buf = 100;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300210module_param(rds_buf, uint, 0444);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300211MODULE_PARM_DESC(rds_buf, "RDS buffer entries: *100*");
212
213/* RDS maximum block errors */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300214static unsigned short max_rds_errors = 1;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300215/* 0 means 0 errors requiring correction */
216/* 1 means 1-2 errors requiring correction (used by original USBRadio.exe) */
217/* 2 means 3-5 errors requiring correction */
218/* 3 means 6+ errors or errors in checkword, correction not possible */
Tobias Lorenz374ab682008-09-24 19:17:54 -0300219module_param(max_rds_errors, ushort, 0644);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300220MODULE_PARM_DESC(max_rds_errors, "RDS maximum block errors: *1*");
221
222/* RDS poll frequency */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300223static unsigned int rds_poll_time = 40;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300224/* 40 is used by the original USBRadio.exe */
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300225/* 50 is used by radio-cadet */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300226/* 75 should be okay */
227/* 80 is the usual RDS receive interval */
Tobias Lorenz374ab682008-09-24 19:17:54 -0300228module_param(rds_poll_time, uint, 0644);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300229MODULE_PARM_DESC(rds_poll_time, "RDS poll time (ms): *40*");
230
231
232
233/**************************************************************************
234 * Register Definitions
235 **************************************************************************/
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300236#define RADIO_REGISTER_SIZE 2 /* 16 register bit width */
237#define RADIO_REGISTER_NUM 16 /* DEVICEID ... RDSD */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300238#define RDS_REGISTER_NUM 6 /* STATUSRSSI ... RDSD */
239
240#define DEVICEID 0 /* Device ID */
241#define DEVICEID_PN 0xf000 /* bits 15..12: Part Number */
242#define DEVICEID_MFGID 0x0fff /* bits 11..00: Manufacturer ID */
243
244#define CHIPID 1 /* Chip ID */
245#define CHIPID_REV 0xfc00 /* bits 15..10: Chip Version */
246#define CHIPID_DEV 0x0200 /* bits 09..09: Device */
247#define CHIPID_FIRMWARE 0x01ff /* bits 08..00: Firmware Version */
248
249#define POWERCFG 2 /* Power Configuration */
250#define POWERCFG_DSMUTE 0x8000 /* bits 15..15: Softmute Disable */
251#define POWERCFG_DMUTE 0x4000 /* bits 14..14: Mute Disable */
252#define POWERCFG_MONO 0x2000 /* bits 13..13: Mono Select */
253#define POWERCFG_RDSM 0x0800 /* bits 11..11: RDS Mode (Si4701 only) */
254#define POWERCFG_SKMODE 0x0400 /* bits 10..10: Seek Mode */
255#define POWERCFG_SEEKUP 0x0200 /* bits 09..09: Seek Direction */
256#define POWERCFG_SEEK 0x0100 /* bits 08..08: Seek */
257#define POWERCFG_DISABLE 0x0040 /* bits 06..06: Powerup Disable */
258#define POWERCFG_ENABLE 0x0001 /* bits 00..00: Powerup Enable */
259
260#define CHANNEL 3 /* Channel */
261#define CHANNEL_TUNE 0x8000 /* bits 15..15: Tune */
262#define CHANNEL_CHAN 0x03ff /* bits 09..00: Channel Select */
263
264#define SYSCONFIG1 4 /* System Configuration 1 */
265#define SYSCONFIG1_RDSIEN 0x8000 /* bits 15..15: RDS Interrupt Enable (Si4701 only) */
266#define SYSCONFIG1_STCIEN 0x4000 /* bits 14..14: Seek/Tune Complete Interrupt Enable */
267#define SYSCONFIG1_RDS 0x1000 /* bits 12..12: RDS Enable (Si4701 only) */
268#define SYSCONFIG1_DE 0x0800 /* bits 11..11: De-emphasis (0=75us 1=50us) */
269#define SYSCONFIG1_AGCD 0x0400 /* bits 10..10: AGC Disable */
270#define SYSCONFIG1_BLNDADJ 0x00c0 /* bits 07..06: Stereo/Mono Blend Level Adjustment */
271#define SYSCONFIG1_GPIO3 0x0030 /* bits 05..04: General Purpose I/O 3 */
272#define SYSCONFIG1_GPIO2 0x000c /* bits 03..02: General Purpose I/O 2 */
273#define SYSCONFIG1_GPIO1 0x0003 /* bits 01..00: General Purpose I/O 1 */
274
275#define SYSCONFIG2 5 /* System Configuration 2 */
276#define SYSCONFIG2_SEEKTH 0xff00 /* bits 15..08: RSSI Seek Threshold */
277#define SYSCONFIG2_BAND 0x0080 /* bits 07..06: Band Select */
278#define SYSCONFIG2_SPACE 0x0030 /* bits 05..04: Channel Spacing */
279#define SYSCONFIG2_VOLUME 0x000f /* bits 03..00: Volume */
280
281#define SYSCONFIG3 6 /* System Configuration 3 */
282#define SYSCONFIG3_SMUTER 0xc000 /* bits 15..14: Softmute Attack/Recover Rate */
283#define SYSCONFIG3_SMUTEA 0x3000 /* bits 13..12: Softmute Attenuation */
284#define SYSCONFIG3_SKSNR 0x00f0 /* bits 07..04: Seek SNR Threshold */
285#define SYSCONFIG3_SKCNT 0x000f /* bits 03..00: Seek FM Impulse Detection Threshold */
286
287#define TEST1 7 /* Test 1 */
288#define TEST1_AHIZEN 0x4000 /* bits 14..14: Audio High-Z Enable */
289
290#define TEST2 8 /* Test 2 */
291/* TEST2 only contains reserved bits */
292
293#define BOOTCONFIG 9 /* Boot Configuration */
294/* BOOTCONFIG only contains reserved bits */
295
296#define STATUSRSSI 10 /* Status RSSI */
297#define STATUSRSSI_RDSR 0x8000 /* bits 15..15: RDS Ready (Si4701 only) */
298#define STATUSRSSI_STC 0x4000 /* bits 14..14: Seek/Tune Complete */
299#define STATUSRSSI_SF 0x2000 /* bits 13..13: Seek Fail/Band Limit */
300#define STATUSRSSI_AFCRL 0x1000 /* bits 12..12: AFC Rail */
301#define STATUSRSSI_RDSS 0x0800 /* bits 11..11: RDS Synchronized (Si4701 only) */
302#define STATUSRSSI_BLERA 0x0600 /* bits 10..09: RDS Block A Errors (Si4701 only) */
303#define STATUSRSSI_ST 0x0100 /* bits 08..08: Stereo Indicator */
304#define STATUSRSSI_RSSI 0x00ff /* bits 07..00: RSSI (Received Signal Strength Indicator) */
305
306#define READCHAN 11 /* Read Channel */
307#define READCHAN_BLERB 0xc000 /* bits 15..14: RDS Block D Errors (Si4701 only) */
308#define READCHAN_BLERC 0x3000 /* bits 13..12: RDS Block C Errors (Si4701 only) */
309#define READCHAN_BLERD 0x0c00 /* bits 11..10: RDS Block B Errors (Si4701 only) */
310#define READCHAN_READCHAN 0x03ff /* bits 09..00: Read Channel */
311
312#define RDSA 12 /* RDSA */
313#define RDSA_RDSA 0xffff /* bits 15..00: RDS Block A Data (Si4701 only) */
314
315#define RDSB 13 /* RDSB */
316#define RDSB_RDSB 0xffff /* bits 15..00: RDS Block B Data (Si4701 only) */
317
318#define RDSC 14 /* RDSC */
319#define RDSC_RDSC 0xffff /* bits 15..00: RDS Block C Data (Si4701 only) */
320
321#define RDSD 15 /* RDSD */
322#define RDSD_RDSD 0xffff /* bits 15..00: RDS Block D Data (Si4701 only) */
323
324
325
326/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300327 * USB HID Reports
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300328 **************************************************************************/
329
330/* Reports 1-16 give direct read/write access to the 16 Si470x registers */
331/* with the (REPORT_ID - 1) corresponding to the register address across USB */
332/* endpoint 0 using GET_REPORT and SET_REPORT */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300333#define REGISTER_REPORT_SIZE (RADIO_REGISTER_SIZE + 1)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300334#define REGISTER_REPORT(reg) ((reg) + 1)
335
336/* Report 17 gives direct read/write access to the entire Si470x register */
337/* map across endpoint 0 using GET_REPORT and SET_REPORT */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300338#define ENTIRE_REPORT_SIZE (RADIO_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300339#define ENTIRE_REPORT 17
340
341/* Report 18 is used to send the lowest 6 Si470x registers up the HID */
342/* interrupt endpoint 1 to Windows every 20 milliseconds for status */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300343#define RDS_REPORT_SIZE (RDS_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300344#define RDS_REPORT 18
345
346/* Report 19: LED state */
347#define LED_REPORT_SIZE 3
348#define LED_REPORT 19
349
350/* Report 19: stream */
351#define STREAM_REPORT_SIZE 3
352#define STREAM_REPORT 19
353
354/* Report 20: scratch */
355#define SCRATCH_PAGE_SIZE 63
356#define SCRATCH_REPORT_SIZE (SCRATCH_PAGE_SIZE + 1)
357#define SCRATCH_REPORT 20
358
359/* Reports 19-22: flash upgrade of the C8051F321 */
360#define WRITE_REPORT 19
361#define FLASH_REPORT 20
362#define CRC_REPORT 21
363#define RESPONSE_REPORT 22
364
365/* Report 23: currently unused, but can accept 60 byte reports on the HID */
366/* interrupt out endpoint 2 every 1 millisecond */
367#define UNUSED_REPORT 23
368
369
370
371/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300372 * Software/Hardware Versions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300373 **************************************************************************/
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300374#define RADIO_SW_VERSION_NOT_BOOTLOADABLE 6
375#define RADIO_SW_VERSION 7
376#define RADIO_SW_VERSION_CURRENT 15
377#define RADIO_HW_VERSION 1
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300378
379#define SCRATCH_PAGE_SW_VERSION 1
380#define SCRATCH_PAGE_HW_VERSION 2
381
382
383
384/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300385 * LED State Definitions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300386 **************************************************************************/
387#define LED_COMMAND 0x35
388
389#define NO_CHANGE_LED 0x00
390#define ALL_COLOR_LED 0x01 /* streaming state */
391#define BLINK_GREEN_LED 0x02 /* connect state */
392#define BLINK_RED_LED 0x04
393#define BLINK_ORANGE_LED 0x10 /* disconnect state */
394#define SOLID_GREEN_LED 0x20 /* tuning/seeking state */
395#define SOLID_RED_LED 0x40 /* bootload state */
396#define SOLID_ORANGE_LED 0x80
397
398
399
400/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300401 * Stream State Definitions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300402 **************************************************************************/
403#define STREAM_COMMAND 0x36
404#define STREAM_VIDPID 0x00
405#define STREAM_AUDIO 0xff
406
407
408
409/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300410 * Bootloader / Flash Commands
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300411 **************************************************************************/
412
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300413/* unique id sent to bootloader and required to put into a bootload state */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300414#define UNIQUE_BL_ID 0x34
415
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300416/* mask for the flash data */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300417#define FLASH_DATA_MASK 0x55
418
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300419/* bootloader commands */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300420#define GET_SW_VERSION_COMMAND 0x00
421#define SET_PAGE_COMMAND 0x01
422#define ERASE_PAGE_COMMAND 0x02
423#define WRITE_PAGE_COMMAND 0x03
424#define CRC_ON_PAGE_COMMAND 0x04
425#define READ_FLASH_BYTE_COMMAND 0x05
426#define RESET_DEVICE_COMMAND 0x06
427#define GET_HW_VERSION_COMMAND 0x07
428#define BLANK 0xff
429
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300430/* bootloader command responses */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300431#define COMMAND_OK 0x01
432#define COMMAND_FAILED 0x02
433#define COMMAND_PENDING 0x03
434
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300435/* buffer sizes */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300436#define COMMAND_BUFFER_SIZE 4
437#define RESPONSE_BUFFER_SIZE 2
438#define FLASH_BUFFER_SIZE 64
439#define CRC_BUFFER_SIZE 3
440
441
442
443/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300444 * General Driver Definitions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300445 **************************************************************************/
446
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300447/*
448 * si470x_device - private data
449 */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300450struct si470x_device {
451 /* reference to USB and video device */
452 struct usb_device *usbdev;
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300453 struct usb_interface *intf;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300454 struct video_device *videodev;
455
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300456 /* driver management */
457 unsigned int users;
Tobias Lorenzce5829e2008-05-31 15:04:32 -0300458 unsigned char disconnected;
459 struct mutex disconnect_lock;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300460
461 /* Silabs internal registers (0..15) */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300462 unsigned short registers[RADIO_REGISTER_NUM];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300463
464 /* RDS receive buffer */
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300465 struct delayed_work work;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300466 wait_queue_head_t read_queue;
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300467 struct mutex lock; /* buffer locking */
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300468 unsigned char *buffer; /* size is always multiple of three */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300469 unsigned int buf_size;
470 unsigned int rd_index;
471 unsigned int wr_index;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300472};
473
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300474
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300475/*
476 * The frequency is set in units of 62.5 Hz when using V4L2_TUNER_CAP_LOW,
477 * 62.5 kHz otherwise.
478 * The tuner is able to have a channel spacing of 50, 100 or 200 kHz.
479 * tuner->capability is therefore set to V4L2_TUNER_CAP_LOW
480 * The FREQ_MUL is then: 1 MHz / 62.5 Hz = 16000
481 */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300482#define FREQ_MUL (1000000 / 62.5)
483
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300484
485
486/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300487 * General Driver Functions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300488 **************************************************************************/
489
490/*
491 * si470x_get_report - receive a HID report
492 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300493static int si470x_get_report(struct si470x_device *radio, void *buf, int size)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300494{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300495 unsigned char *report = (unsigned char *) buf;
496 int retval;
497
498 retval = usb_control_msg(radio->usbdev,
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300499 usb_rcvctrlpipe(radio->usbdev, 0),
500 HID_REQ_GET_REPORT,
501 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300502 report[0], 2,
503 buf, size, usb_timeout);
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300504
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300505 if (retval < 0)
506 printk(KERN_WARNING DRIVER_NAME
507 ": si470x_get_report: usb_control_msg returned %d\n",
508 retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300509 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300510}
511
512
513/*
514 * si470x_set_report - send a HID report
515 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300516static int si470x_set_report(struct si470x_device *radio, void *buf, int size)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300517{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300518 unsigned char *report = (unsigned char *) buf;
519 int retval;
520
521 retval = usb_control_msg(radio->usbdev,
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300522 usb_sndctrlpipe(radio->usbdev, 0),
523 HID_REQ_SET_REPORT,
524 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300525 report[0], 2,
526 buf, size, usb_timeout);
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300527
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300528 if (retval < 0)
529 printk(KERN_WARNING DRIVER_NAME
530 ": si470x_set_report: usb_control_msg returned %d\n",
531 retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300532 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300533}
534
535
536/*
537 * si470x_get_register - read register
538 */
539static int si470x_get_register(struct si470x_device *radio, int regnr)
540{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300541 unsigned char buf[REGISTER_REPORT_SIZE];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300542 int retval;
543
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300544 buf[0] = REGISTER_REPORT(regnr);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300545
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300546 retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
547
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300548 if (retval >= 0)
Harvey Harrison5ad6b812008-04-15 22:22:11 -0300549 radio->registers[regnr] = get_unaligned_be16(&buf[1]);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300550
551 return (retval < 0) ? -EINVAL : 0;
552}
553
554
555/*
556 * si470x_set_register - write register
557 */
558static int si470x_set_register(struct si470x_device *radio, int regnr)
559{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300560 unsigned char buf[REGISTER_REPORT_SIZE];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300561 int retval;
562
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300563 buf[0] = REGISTER_REPORT(regnr);
Harvey Harrison5ad6b812008-04-15 22:22:11 -0300564 put_unaligned_be16(radio->registers[regnr], &buf[1]);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300565
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300566 retval = si470x_set_report(radio, (void *) &buf, sizeof(buf));
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300567
568 return (retval < 0) ? -EINVAL : 0;
569}
570
571
572/*
573 * si470x_get_all_registers - read entire registers
574 */
575static int si470x_get_all_registers(struct si470x_device *radio)
576{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300577 unsigned char buf[ENTIRE_REPORT_SIZE];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300578 int retval;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300579 unsigned char regnr;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300580
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300581 buf[0] = ENTIRE_REPORT;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300582
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300583 retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300584
585 if (retval >= 0)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300586 for (regnr = 0; regnr < RADIO_REGISTER_NUM; regnr++)
Harvey Harrison5ad6b812008-04-15 22:22:11 -0300587 radio->registers[regnr] = get_unaligned_be16(
588 &buf[regnr * RADIO_REGISTER_SIZE + 1]);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300589
590 return (retval < 0) ? -EINVAL : 0;
591}
592
593
594/*
595 * si470x_get_rds_registers - read rds registers
596 */
597static int si470x_get_rds_registers(struct si470x_device *radio)
598{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300599 unsigned char buf[RDS_REPORT_SIZE];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300600 int retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300601 int size;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300602 unsigned char regnr;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300603
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300604 buf[0] = RDS_REPORT;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300605
606 retval = usb_interrupt_msg(radio->usbdev,
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300607 usb_rcvintpipe(radio->usbdev, 1),
608 (void *) &buf, sizeof(buf), &size, usb_timeout);
609 if (size != sizeof(buf))
Tobias Lorenzce5829e2008-05-31 15:04:32 -0300610 printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: "
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300611 "return size differs: %d != %zu\n", size, sizeof(buf));
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300612 if (retval < 0)
613 printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: "
614 "usb_interrupt_msg returned %d\n", retval);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300615
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300616 if (retval >= 0)
617 for (regnr = 0; regnr < RDS_REGISTER_NUM; regnr++)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300618 radio->registers[STATUSRSSI + regnr] =
Tobias Lorenzce5829e2008-05-31 15:04:32 -0300619 get_unaligned_be16(
620 &buf[regnr * RADIO_REGISTER_SIZE + 1]);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300621
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300622 return (retval < 0) ? -EINVAL : 0;
623}
624
625
626/*
627 * si470x_set_chan - set the channel
628 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300629static int si470x_set_chan(struct si470x_device *radio, unsigned short chan)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300630{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300631 int retval;
632 unsigned long timeout;
633 bool timed_out = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300634
635 /* start tuning */
636 radio->registers[CHANNEL] &= ~CHANNEL_CHAN;
637 radio->registers[CHANNEL] |= CHANNEL_TUNE | chan;
638 retval = si470x_set_register(radio, CHANNEL);
639 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300640 goto done;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300641
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300642 /* wait till tune operation has completed */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300643 timeout = jiffies + msecs_to_jiffies(tune_timeout);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300644 do {
645 retval = si470x_get_register(radio, STATUSRSSI);
646 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300647 goto stop;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300648 timed_out = time_after(jiffies, timeout);
649 } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
650 (!timed_out));
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300651 if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
652 printk(KERN_WARNING DRIVER_NAME ": tune does not complete\n");
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300653 if (timed_out)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300654 printk(KERN_WARNING DRIVER_NAME
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300655 ": tune timed out after %u ms\n", tune_timeout);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300656
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300657stop:
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300658 /* stop tuning */
659 radio->registers[CHANNEL] &= ~CHANNEL_TUNE;
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300660 retval = si470x_set_register(radio, CHANNEL);
661
662done:
663 return retval;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300664}
665
666
667/*
668 * si470x_get_freq - get the frequency
669 */
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300670static int si470x_get_freq(struct si470x_device *radio, unsigned int *freq)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300671{
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300672 unsigned int spacing, band_bottom;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300673 unsigned short chan;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300674 int retval;
675
676 /* Spacing (kHz) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300677 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300678 /* 0: 200 kHz (USA, Australia) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300679 case 0:
680 spacing = 0.200 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300681 /* 1: 100 kHz (Europe, Japan) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300682 case 1:
683 spacing = 0.100 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300684 /* 2: 50 kHz */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300685 default:
686 spacing = 0.050 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300687 };
688
689 /* Bottom of Band (MHz) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300690 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300691 /* 0: 87.5 - 108 MHz (USA, Europe) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300692 case 0:
693 band_bottom = 87.5 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300694 /* 1: 76 - 108 MHz (Japan wide band) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300695 default:
696 band_bottom = 76 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300697 /* 2: 76 - 90 MHz (Japan) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300698 case 2:
699 band_bottom = 76 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300700 };
701
702 /* read channel */
703 retval = si470x_get_register(radio, READCHAN);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300704 chan = radio->registers[READCHAN] & READCHAN_READCHAN;
705
706 /* Frequency (MHz) = Spacing (kHz) x Channel + Bottom of Band (MHz) */
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300707 *freq = chan * spacing + band_bottom;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300708
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300709 return retval;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300710}
711
712
713/*
714 * si470x_set_freq - set the frequency
715 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300716static int si470x_set_freq(struct si470x_device *radio, unsigned int freq)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300717{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300718 unsigned int spacing, band_bottom;
719 unsigned short chan;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300720
721 /* Spacing (kHz) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300722 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300723 /* 0: 200 kHz (USA, Australia) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300724 case 0:
725 spacing = 0.200 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300726 /* 1: 100 kHz (Europe, Japan) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300727 case 1:
728 spacing = 0.100 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300729 /* 2: 50 kHz */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300730 default:
731 spacing = 0.050 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300732 };
733
734 /* Bottom of Band (MHz) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300735 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300736 /* 0: 87.5 - 108 MHz (USA, Europe) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300737 case 0:
738 band_bottom = 87.5 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300739 /* 1: 76 - 108 MHz (Japan wide band) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300740 default:
741 band_bottom = 76 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300742 /* 2: 76 - 90 MHz (Japan) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300743 case 2:
744 band_bottom = 76 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300745 };
746
747 /* Chan = [ Freq (Mhz) - Bottom of Band (MHz) ] / Spacing (kHz) */
748 chan = (freq - band_bottom) / spacing;
749
750 return si470x_set_chan(radio, chan);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300751}
752
753
754/*
Tobias Lorenzbbc89952008-05-31 15:11:32 -0300755 * si470x_set_seek - set seek
756 */
757static int si470x_set_seek(struct si470x_device *radio,
758 unsigned int wrap_around, unsigned int seek_upward)
759{
760 int retval = 0;
761 unsigned long timeout;
762 bool timed_out = 0;
763
764 /* start seeking */
765 radio->registers[POWERCFG] |= POWERCFG_SEEK;
766 if (wrap_around == 1)
767 radio->registers[POWERCFG] &= ~POWERCFG_SKMODE;
768 else
769 radio->registers[POWERCFG] |= POWERCFG_SKMODE;
770 if (seek_upward == 1)
771 radio->registers[POWERCFG] |= POWERCFG_SEEKUP;
772 else
773 radio->registers[POWERCFG] &= ~POWERCFG_SEEKUP;
774 retval = si470x_set_register(radio, POWERCFG);
775 if (retval < 0)
776 goto done;
777
778 /* wait till seek operation has completed */
779 timeout = jiffies + msecs_to_jiffies(seek_timeout);
780 do {
781 retval = si470x_get_register(radio, STATUSRSSI);
782 if (retval < 0)
783 goto stop;
784 timed_out = time_after(jiffies, timeout);
785 } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
786 (!timed_out));
787 if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
788 printk(KERN_WARNING DRIVER_NAME ": seek does not complete\n");
789 if (radio->registers[STATUSRSSI] & STATUSRSSI_SF)
790 printk(KERN_WARNING DRIVER_NAME
791 ": seek failed / band limit reached\n");
792 if (timed_out)
793 printk(KERN_WARNING DRIVER_NAME
794 ": seek timed out after %u ms\n", seek_timeout);
795
796stop:
797 /* stop seeking */
798 radio->registers[POWERCFG] &= ~POWERCFG_SEEK;
799 retval = si470x_set_register(radio, POWERCFG);
800
801done:
802 /* try again, if timed out */
803 if ((retval == 0) && timed_out)
804 retval = -EAGAIN;
805
806 return retval;
807}
808
809
810/*
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300811 * si470x_start - switch on radio
812 */
813static int si470x_start(struct si470x_device *radio)
814{
815 int retval;
816
817 /* powercfg */
818 radio->registers[POWERCFG] =
819 POWERCFG_DMUTE | POWERCFG_ENABLE | POWERCFG_RDSM;
820 retval = si470x_set_register(radio, POWERCFG);
821 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300822 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300823
824 /* sysconfig 1 */
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300825 radio->registers[SYSCONFIG1] = SYSCONFIG1_DE;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300826 retval = si470x_set_register(radio, SYSCONFIG1);
827 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300828 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300829
830 /* sysconfig 2 */
831 radio->registers[SYSCONFIG2] =
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300832 (0x3f << 8) | /* SEEKTH */
833 ((band << 6) & SYSCONFIG2_BAND) | /* BAND */
834 ((space << 4) & SYSCONFIG2_SPACE) | /* SPACE */
835 15; /* VOLUME (max) */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300836 retval = si470x_set_register(radio, SYSCONFIG2);
837 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300838 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300839
840 /* reset last channel */
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300841 retval = si470x_set_chan(radio,
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300842 radio->registers[CHANNEL] & CHANNEL_CHAN);
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300843
844done:
845 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300846}
847
848
849/*
850 * si470x_stop - switch off radio
851 */
852static int si470x_stop(struct si470x_device *radio)
853{
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300854 int retval;
855
856 /* sysconfig 1 */
857 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
858 retval = si470x_set_register(radio, SYSCONFIG1);
859 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300860 goto done;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300861
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300862 /* powercfg */
863 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
864 /* POWERCFG_ENABLE has to automatically go low */
865 radio->registers[POWERCFG] |= POWERCFG_ENABLE | POWERCFG_DISABLE;
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300866 retval = si470x_set_register(radio, POWERCFG);
867
868done:
869 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300870}
871
872
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300873/*
874 * si470x_rds_on - switch on rds reception
875 */
876static int si470x_rds_on(struct si470x_device *radio)
877{
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300878 int retval;
879
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300880 /* sysconfig 1 */
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300881 mutex_lock(&radio->lock);
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300882 radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDS;
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300883 retval = si470x_set_register(radio, SYSCONFIG1);
884 if (retval < 0)
885 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
886 mutex_unlock(&radio->lock);
887
888 return retval;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300889}
890
891
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300892
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300893/**************************************************************************
894 * RDS Driver Functions
895 **************************************************************************/
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300896
897/*
898 * si470x_rds - rds processing function
899 */
900static void si470x_rds(struct si470x_device *radio)
901{
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300902 unsigned char blocknum;
903 unsigned short bler; /* rds block errors */
904 unsigned short rds;
905 unsigned char tmpbuf[3];
906
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300907 /* get rds blocks */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300908 if (si470x_get_rds_registers(radio) < 0)
909 return;
910 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0) {
911 /* No RDS group ready */
912 return;
913 }
914 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSS) == 0) {
915 /* RDS decoder not synchronized */
916 return;
917 }
918
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300919 /* copy all four RDS blocks to internal buffer */
920 mutex_lock(&radio->lock);
921 for (blocknum = 0; blocknum < 4; blocknum++) {
922 switch (blocknum) {
923 default:
924 bler = (radio->registers[STATUSRSSI] &
925 STATUSRSSI_BLERA) >> 9;
926 rds = radio->registers[RDSA];
927 break;
928 case 1:
929 bler = (radio->registers[READCHAN] &
930 READCHAN_BLERB) >> 14;
931 rds = radio->registers[RDSB];
932 break;
933 case 2:
934 bler = (radio->registers[READCHAN] &
935 READCHAN_BLERC) >> 12;
936 rds = radio->registers[RDSC];
937 break;
938 case 3:
939 bler = (radio->registers[READCHAN] &
940 READCHAN_BLERD) >> 10;
941 rds = radio->registers[RDSD];
942 break;
943 };
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300944
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300945 /* Fill the V4L2 RDS buffer */
Harvey Harrison5ad6b812008-04-15 22:22:11 -0300946 put_unaligned_le16(rds, &tmpbuf);
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300947 tmpbuf[2] = blocknum; /* offset name */
948 tmpbuf[2] |= blocknum << 3; /* received offset */
949 if (bler > max_rds_errors)
950 tmpbuf[2] |= 0x80; /* uncorrectable errors */
951 else if (bler > 0)
952 tmpbuf[2] |= 0x40; /* corrected error(s) */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300953
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300954 /* copy RDS block to internal buffer */
955 memcpy(&radio->buffer[radio->wr_index], &tmpbuf, 3);
956 radio->wr_index += 3;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300957
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300958 /* wrap write pointer */
959 if (radio->wr_index >= radio->buf_size)
960 radio->wr_index = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300961
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300962 /* check for overflow */
963 if (radio->wr_index == radio->rd_index) {
964 /* increment and wrap read pointer */
965 radio->rd_index += 3;
966 if (radio->rd_index >= radio->buf_size)
967 radio->rd_index = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300968 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300969 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300970 mutex_unlock(&radio->lock);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300971
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300972 /* wake up read queue */
973 if (radio->wr_index != radio->rd_index)
974 wake_up_interruptible(&radio->read_queue);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300975}
976
977
978/*
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300979 * si470x_work - rds work function
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300980 */
981static void si470x_work(struct work_struct *work)
982{
983 struct si470x_device *radio = container_of(work, struct si470x_device,
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300984 work.work);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300985
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300986 /* safety checks */
Tobias Lorenzce5829e2008-05-31 15:04:32 -0300987 if (radio->disconnected)
988 return;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300989 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300990 return;
991
992 si470x_rds(radio);
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300993 schedule_delayed_work(&radio->work, msecs_to_jiffies(rds_poll_time));
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300994}
995
996
997
998/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300999 * File Operations Interface
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001000 **************************************************************************/
1001
1002/*
1003 * si470x_fops_read - read RDS data
1004 */
1005static ssize_t si470x_fops_read(struct file *file, char __user *buf,
1006 size_t count, loff_t *ppos)
1007{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001008 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001009 int retval = 0;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001010 unsigned int block_count = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001011
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001012 /* switch on rds reception */
1013 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
1014 si470x_rds_on(radio);
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001015 schedule_delayed_work(&radio->work,
1016 msecs_to_jiffies(rds_poll_time));
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001017 }
1018
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001019 /* block if no new data available */
1020 while (radio->wr_index == radio->rd_index) {
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001021 if (file->f_flags & O_NONBLOCK) {
1022 retval = -EWOULDBLOCK;
1023 goto done;
1024 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001025 if (wait_event_interruptible(radio->read_queue,
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001026 radio->wr_index != radio->rd_index) < 0) {
1027 retval = -EINTR;
1028 goto done;
1029 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001030 }
1031
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001032 /* calculate block count from byte count */
1033 count /= 3;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001034
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001035 /* copy RDS block out of internal buffer and to user buffer */
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001036 mutex_lock(&radio->lock);
1037 while (block_count < count) {
1038 if (radio->rd_index == radio->wr_index)
1039 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001040
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001041 /* always transfer rds complete blocks */
1042 if (copy_to_user(buf, &radio->buffer[radio->rd_index], 3))
1043 /* retval = -EFAULT; */
1044 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001045
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001046 /* increment and wrap read pointer */
1047 radio->rd_index += 3;
1048 if (radio->rd_index >= radio->buf_size)
1049 radio->rd_index = 0;
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001050
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001051 /* increment counters */
1052 block_count++;
1053 buf += 3;
1054 retval += 3;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001055 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001056 mutex_unlock(&radio->lock);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001057
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001058done:
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001059 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001060}
1061
1062
1063/*
1064 * si470x_fops_poll - poll RDS data
1065 */
1066static unsigned int si470x_fops_poll(struct file *file,
1067 struct poll_table_struct *pts)
1068{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001069 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001070 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001071
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001072 /* switch on rds reception */
1073 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
1074 si470x_rds_on(radio);
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001075 schedule_delayed_work(&radio->work,
1076 msecs_to_jiffies(rds_poll_time));
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001077 }
1078
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001079 poll_wait(file, &radio->read_queue, pts);
1080
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001081 if (radio->rd_index != radio->wr_index)
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001082 retval = POLLIN | POLLRDNORM;
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001083
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001084 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001085}
1086
1087
1088/*
1089 * si470x_fops_open - file open
1090 */
1091static int si470x_fops_open(struct inode *inode, struct file *file)
1092{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001093 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001094 int retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001095
Hans Verkuild56dc612008-07-30 08:43:36 -03001096 lock_kernel();
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001097 radio->users++;
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001098
1099 retval = usb_autopm_get_interface(radio->intf);
1100 if (retval < 0) {
1101 radio->users--;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001102 retval = -EIO;
1103 goto done;
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001104 }
1105
1106 if (radio->users == 1) {
1107 retval = si470x_start(radio);
1108 if (retval < 0)
1109 usb_autopm_put_interface(radio->intf);
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001110 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001111
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001112done:
Hans Verkuild56dc612008-07-30 08:43:36 -03001113 unlock_kernel();
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001114 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001115}
1116
1117
1118/*
1119 * si470x_fops_release - file release
1120 */
1121static int si470x_fops_release(struct inode *inode, struct file *file)
1122{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001123 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001124 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001125
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001126 /* safety check */
1127 if (!radio) {
1128 retval = -ENODEV;
1129 goto done;
1130 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001131
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001132 mutex_lock(&radio->disconnect_lock);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001133 radio->users--;
1134 if (radio->users == 0) {
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001135 if (radio->disconnected) {
1136 video_unregister_device(radio->videodev);
1137 kfree(radio->buffer);
1138 kfree(radio);
1139 goto unlock;
1140 }
Tobias Lorenz03dea862008-04-22 14:46:11 -03001141
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001142 /* stop rds reception */
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001143 cancel_delayed_work_sync(&radio->work);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001144
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001145 /* cancel read processes */
1146 wake_up_interruptible(&radio->read_queue);
1147
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001148 retval = si470x_stop(radio);
1149 usb_autopm_put_interface(radio->intf);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001150 }
1151
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001152unlock:
1153 mutex_unlock(&radio->disconnect_lock);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001154
1155done:
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001156 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001157}
1158
1159
1160/*
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001161 * si470x_fops - file operations interface
1162 */
1163static const struct file_operations si470x_fops = {
1164 .owner = THIS_MODULE,
1165 .llseek = no_llseek,
1166 .read = si470x_fops_read,
1167 .poll = si470x_fops_poll,
1168 .ioctl = video_ioctl2,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -03001169#ifdef CONFIG_COMPAT
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001170 .compat_ioctl = v4l_compat_ioctl32,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -03001171#endif
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001172 .open = si470x_fops_open,
1173 .release = si470x_fops_release,
1174};
1175
1176
1177
1178/**************************************************************************
1179 * Video4Linux Interface
1180 **************************************************************************/
1181
1182/*
1183 * si470x_v4l2_queryctrl - query control
1184 */
1185static struct v4l2_queryctrl si470x_v4l2_queryctrl[] = {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001186 {
1187 .id = V4L2_CID_AUDIO_VOLUME,
1188 .type = V4L2_CTRL_TYPE_INTEGER,
1189 .name = "Volume",
1190 .minimum = 0,
1191 .maximum = 15,
1192 .step = 1,
1193 .default_value = 15,
1194 },
1195 {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001196 .id = V4L2_CID_AUDIO_MUTE,
1197 .type = V4L2_CTRL_TYPE_BOOLEAN,
1198 .name = "Mute",
1199 .minimum = 0,
1200 .maximum = 1,
1201 .step = 1,
1202 .default_value = 1,
1203 },
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001204};
1205
1206
1207/*
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001208 * si470x_vidioc_querycap - query device capabilities
1209 */
1210static int si470x_vidioc_querycap(struct file *file, void *priv,
1211 struct v4l2_capability *capability)
1212{
1213 strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver));
1214 strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
1215 sprintf(capability->bus_info, "USB");
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001216 capability->version = DRIVER_KERNEL_VERSION;
Tobias Lorenzbbc89952008-05-31 15:11:32 -03001217 capability->capabilities = V4L2_CAP_HW_FREQ_SEEK |
1218 V4L2_CAP_TUNER | V4L2_CAP_RADIO;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001219
1220 return 0;
1221}
1222
1223
1224/*
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001225 * si470x_vidioc_queryctrl - enumerate control items
1226 */
1227static int si470x_vidioc_queryctrl(struct file *file, void *priv,
1228 struct v4l2_queryctrl *qc)
1229{
Tobias Lorenzeb6e3b82008-09-24 19:25:39 -03001230 unsigned char i = 0;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001231 int retval = -EINVAL;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001232
Tobias Lorenzeb6e3b82008-09-24 19:25:39 -03001233 /* abort if qc->id is below V4L2_CID_BASE */
1234 if (qc->id < V4L2_CID_BASE)
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001235 goto done;
1236
Tobias Lorenzeb6e3b82008-09-24 19:25:39 -03001237 /* search video control */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001238 for (i = 0; i < ARRAY_SIZE(si470x_v4l2_queryctrl); i++) {
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001239 if (qc->id == si470x_v4l2_queryctrl[i].id) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001240 memcpy(qc, &(si470x_v4l2_queryctrl[i]), sizeof(*qc));
Tobias Lorenzeb6e3b82008-09-24 19:25:39 -03001241 retval = 0; /* found */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001242 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001243 }
1244 }
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001245
Tobias Lorenzeb6e3b82008-09-24 19:25:39 -03001246 /* disable unsupported base controls */
1247 /* to satisfy kradio and such apps */
1248 if ((retval == -EINVAL) && (qc->id < V4L2_CID_LASTP1)) {
1249 qc->flags = V4L2_CTRL_FLAG_DISABLED;
1250 retval = 0;
1251 }
1252
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001253done:
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001254 if (retval < 0)
1255 printk(KERN_WARNING DRIVER_NAME
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001256 ": query controls failed with %d\n", retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001257 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001258}
1259
1260
1261/*
1262 * si470x_vidioc_g_ctrl - get the value of a control
1263 */
1264static int si470x_vidioc_g_ctrl(struct file *file, void *priv,
1265 struct v4l2_control *ctrl)
1266{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001267 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001268 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001269
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001270 /* safety checks */
1271 if (radio->disconnected) {
1272 retval = -EIO;
1273 goto done;
1274 }
Tobias Lorenz03dea862008-04-22 14:46:11 -03001275
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001276 switch (ctrl->id) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001277 case V4L2_CID_AUDIO_VOLUME:
1278 ctrl->value = radio->registers[SYSCONFIG2] &
1279 SYSCONFIG2_VOLUME;
1280 break;
1281 case V4L2_CID_AUDIO_MUTE:
1282 ctrl->value = ((radio->registers[POWERCFG] &
1283 POWERCFG_DMUTE) == 0) ? 1 : 0;
1284 break;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001285 default:
1286 retval = -EINVAL;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001287 }
1288
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001289done:
1290 if (retval < 0)
1291 printk(KERN_WARNING DRIVER_NAME
1292 ": get control failed with %d\n", retval);
1293 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001294}
1295
1296
1297/*
1298 * si470x_vidioc_s_ctrl - set the value of a control
1299 */
1300static int si470x_vidioc_s_ctrl(struct file *file, void *priv,
1301 struct v4l2_control *ctrl)
1302{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001303 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001304 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001305
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001306 /* safety checks */
1307 if (radio->disconnected) {
1308 retval = -EIO;
1309 goto done;
1310 }
Tobias Lorenz03dea862008-04-22 14:46:11 -03001311
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001312 switch (ctrl->id) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001313 case V4L2_CID_AUDIO_VOLUME:
1314 radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_VOLUME;
1315 radio->registers[SYSCONFIG2] |= ctrl->value;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001316 retval = si470x_set_register(radio, SYSCONFIG2);
1317 break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001318 case V4L2_CID_AUDIO_MUTE:
1319 if (ctrl->value == 1)
1320 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
1321 else
1322 radio->registers[POWERCFG] |= POWERCFG_DMUTE;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001323 retval = si470x_set_register(radio, POWERCFG);
1324 break;
1325 default:
1326 retval = -EINVAL;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001327 }
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001328
1329done:
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001330 if (retval < 0)
1331 printk(KERN_WARNING DRIVER_NAME
1332 ": set control failed with %d\n", retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001333 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001334}
1335
1336
1337/*
1338 * si470x_vidioc_g_audio - get audio attributes
1339 */
1340static int si470x_vidioc_g_audio(struct file *file, void *priv,
1341 struct v4l2_audio *audio)
1342{
Tobias Lorenz617f5332008-09-24 19:33:09 -03001343 /* driver constants */
1344 audio->index = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001345 strcpy(audio->name, "Radio");
1346 audio->capability = V4L2_AUDCAP_STEREO;
Tobias Lorenz617f5332008-09-24 19:33:09 -03001347 audio->mode = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001348
Tobias Lorenz617f5332008-09-24 19:33:09 -03001349 return 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001350}
1351
1352
1353/*
1354 * si470x_vidioc_g_tuner - get tuner attributes
1355 */
1356static int si470x_vidioc_g_tuner(struct file *file, void *priv,
1357 struct v4l2_tuner *tuner)
1358{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001359 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001360 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001361
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001362 /* safety checks */
1363 if (radio->disconnected) {
1364 retval = -EIO;
1365 goto done;
1366 }
Tobias Lorenz33632d42008-09-24 19:30:26 -03001367 if (tuner->index != 0) {
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001368 retval = -EINVAL;
1369 goto done;
1370 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001371
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001372 retval = si470x_get_register(radio, STATUSRSSI);
1373 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001374 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001375
Tobias Lorenz33632d42008-09-24 19:30:26 -03001376 /* driver constants */
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001377 strcpy(tuner->name, "FM");
Tobias Lorenz33632d42008-09-24 19:30:26 -03001378 tuner->type = V4L2_TUNER_RADIO;
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001379 tuner->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
Tobias Lorenz33632d42008-09-24 19:30:26 -03001380
Tobias Lorenza17c0012008-09-24 19:21:50 -03001381 /* range limits */
1382 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001383 /* 0: 87.5 - 108 MHz (USA, Europe, default) */
1384 default:
1385 tuner->rangelow = 87.5 * FREQ_MUL;
1386 tuner->rangehigh = 108 * FREQ_MUL;
1387 break;
1388 /* 1: 76 - 108 MHz (Japan wide band) */
1389 case 1 :
1390 tuner->rangelow = 76 * FREQ_MUL;
1391 tuner->rangehigh = 108 * FREQ_MUL;
1392 break;
1393 /* 2: 76 - 90 MHz (Japan) */
1394 case 2 :
1395 tuner->rangelow = 76 * FREQ_MUL;
1396 tuner->rangehigh = 90 * FREQ_MUL;
1397 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001398 };
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001399
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001400 /* stereo indicator == stereo (instead of mono) */
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001401 if ((radio->registers[STATUSRSSI] & STATUSRSSI_ST) == 1)
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001402 tuner->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001403 else
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001404 tuner->rxsubchans = V4L2_TUNER_SUB_MONO;
1405
1406 /* mono/stereo selector */
1407 if ((radio->registers[POWERCFG] & POWERCFG_MONO) == 1)
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001408 tuner->audmode = V4L2_TUNER_MODE_MONO;
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001409 else
1410 tuner->audmode = V4L2_TUNER_MODE_STEREO;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001411
1412 /* min is worst, max is best; signal:0..0xffff; rssi: 0..0xff */
1413 tuner->signal = (radio->registers[STATUSRSSI] & STATUSRSSI_RSSI)
1414 * 0x0101;
1415
1416 /* automatic frequency control: -1: freq to low, 1 freq to high */
Tobias Lorenzace7d4b2008-05-31 15:09:07 -03001417 /* AFCRL does only indicate that freq. differs, not if too low/high */
1418 tuner->afc = (radio->registers[STATUSRSSI] & STATUSRSSI_AFCRL) ? 1 : 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001419
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001420done:
1421 if (retval < 0)
1422 printk(KERN_WARNING DRIVER_NAME
1423 ": get tuner failed with %d\n", retval);
1424 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001425}
1426
1427
1428/*
1429 * si470x_vidioc_s_tuner - set tuner attributes
1430 */
1431static int si470x_vidioc_s_tuner(struct file *file, void *priv,
1432 struct v4l2_tuner *tuner)
1433{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001434 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenz33632d42008-09-24 19:30:26 -03001435 int retval = -EINVAL;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001436
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001437 /* safety checks */
1438 if (radio->disconnected) {
1439 retval = -EIO;
1440 goto done;
1441 }
Tobias Lorenz33632d42008-09-24 19:30:26 -03001442 if (tuner->index != 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001443 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001444
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001445 /* mono/stereo selector */
1446 switch (tuner->audmode) {
1447 case V4L2_TUNER_MODE_MONO:
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001448 radio->registers[POWERCFG] |= POWERCFG_MONO; /* force mono */
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001449 break;
1450 case V4L2_TUNER_MODE_STEREO:
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001451 radio->registers[POWERCFG] &= ~POWERCFG_MONO; /* try stereo */
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001452 break;
1453 default:
1454 goto done;
1455 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001456
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001457 retval = si470x_set_register(radio, POWERCFG);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001458
1459done:
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001460 if (retval < 0)
1461 printk(KERN_WARNING DRIVER_NAME
1462 ": set tuner failed with %d\n", retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001463 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001464}
1465
1466
1467/*
1468 * si470x_vidioc_g_frequency - get tuner or modulator radio frequency
1469 */
1470static int si470x_vidioc_g_frequency(struct file *file, void *priv,
1471 struct v4l2_frequency *freq)
1472{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001473 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001474 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001475
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001476 /* safety checks */
1477 if (radio->disconnected) {
1478 retval = -EIO;
1479 goto done;
1480 }
Tobias Lorenz33632d42008-09-24 19:30:26 -03001481 if (freq->tuner != 0) {
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001482 retval = -EINVAL;
1483 goto done;
1484 }
Tobias Lorenz03dea862008-04-22 14:46:11 -03001485
Tobias Lorenz33632d42008-09-24 19:30:26 -03001486 freq->type = V4L2_TUNER_RADIO;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001487 retval = si470x_get_freq(radio, &freq->frequency);
1488
1489done:
1490 if (retval < 0)
1491 printk(KERN_WARNING DRIVER_NAME
1492 ": get frequency failed with %d\n", retval);
1493 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001494}
1495
1496
1497/*
1498 * si470x_vidioc_s_frequency - set tuner or modulator radio frequency
1499 */
1500static int si470x_vidioc_s_frequency(struct file *file, void *priv,
1501 struct v4l2_frequency *freq)
1502{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001503 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001504 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001505
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001506 /* safety checks */
1507 if (radio->disconnected) {
1508 retval = -EIO;
1509 goto done;
1510 }
Tobias Lorenz33632d42008-09-24 19:30:26 -03001511 if (freq->tuner != 0) {
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001512 retval = -EINVAL;
1513 goto done;
1514 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001515
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001516 retval = si470x_set_freq(radio, freq->frequency);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001517
1518done:
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001519 if (retval < 0)
1520 printk(KERN_WARNING DRIVER_NAME
1521 ": set frequency failed with %d\n", retval);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001522 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001523}
1524
1525
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001526/*
Tobias Lorenzbbc89952008-05-31 15:11:32 -03001527 * si470x_vidioc_s_hw_freq_seek - set hardware frequency seek
1528 */
1529static int si470x_vidioc_s_hw_freq_seek(struct file *file, void *priv,
1530 struct v4l2_hw_freq_seek *seek)
1531{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001532 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenzbbc89952008-05-31 15:11:32 -03001533 int retval = 0;
1534
1535 /* safety checks */
1536 if (radio->disconnected) {
1537 retval = -EIO;
1538 goto done;
1539 }
Tobias Lorenz33632d42008-09-24 19:30:26 -03001540 if (seek->tuner != 0) {
Tobias Lorenzbbc89952008-05-31 15:11:32 -03001541 retval = -EINVAL;
1542 goto done;
1543 }
1544
1545 retval = si470x_set_seek(radio, seek->wrap_around, seek->seek_upward);
1546
1547done:
1548 if (retval < 0)
1549 printk(KERN_WARNING DRIVER_NAME
1550 ": set hardware frequency seek failed with %d\n",
1551 retval);
1552 return retval;
1553}
1554
Tobias Lorenz374ab682008-09-24 19:17:54 -03001555
1556/*
1557 * si470x_ioctl_ops - video device ioctl operations
1558 */
Hans Verkuila3998102008-07-21 02:57:38 -03001559static const struct v4l2_ioctl_ops si470x_ioctl_ops = {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001560 .vidioc_querycap = si470x_vidioc_querycap,
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001561 .vidioc_queryctrl = si470x_vidioc_queryctrl,
1562 .vidioc_g_ctrl = si470x_vidioc_g_ctrl,
1563 .vidioc_s_ctrl = si470x_vidioc_s_ctrl,
1564 .vidioc_g_audio = si470x_vidioc_g_audio,
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001565 .vidioc_g_tuner = si470x_vidioc_g_tuner,
1566 .vidioc_s_tuner = si470x_vidioc_s_tuner,
1567 .vidioc_g_frequency = si470x_vidioc_g_frequency,
1568 .vidioc_s_frequency = si470x_vidioc_s_frequency,
Tobias Lorenzbbc89952008-05-31 15:11:32 -03001569 .vidioc_s_hw_freq_seek = si470x_vidioc_s_hw_freq_seek,
Hans Verkuila3998102008-07-21 02:57:38 -03001570};
1571
Tobias Lorenz374ab682008-09-24 19:17:54 -03001572
Hans Verkuila3998102008-07-21 02:57:38 -03001573/*
Tobias Lorenz374ab682008-09-24 19:17:54 -03001574 * si470x_viddev_template - video device interface
Hans Verkuila3998102008-07-21 02:57:38 -03001575 */
1576static struct video_device si470x_viddev_template = {
1577 .fops = &si470x_fops,
Hans Verkuila3998102008-07-21 02:57:38 -03001578 .name = DRIVER_NAME,
Hans Verkuila3998102008-07-21 02:57:38 -03001579 .release = video_device_release,
Tobias Lorenz374ab682008-09-24 19:17:54 -03001580 .ioctl_ops = &si470x_ioctl_ops,
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001581};
1582
1583
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001584
1585/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001586 * USB Interface
1587 **************************************************************************/
1588
1589/*
1590 * si470x_usb_driver_probe - probe for the device
1591 */
1592static int si470x_usb_driver_probe(struct usb_interface *intf,
1593 const struct usb_device_id *id)
1594{
1595 struct si470x_device *radio;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001596 int retval = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001597
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001598 /* private data allocation and initialization */
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001599 radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001600 if (!radio) {
1601 retval = -ENOMEM;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001602 goto err_initial;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001603 }
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001604 radio->users = 0;
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001605 radio->disconnected = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001606 radio->usbdev = interface_to_usbdev(intf);
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001607 radio->intf = intf;
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001608 mutex_init(&radio->disconnect_lock);
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001609 mutex_init(&radio->lock);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001610
1611 /* video device allocation and initialization */
1612 radio->videodev = video_device_alloc();
1613 if (!radio->videodev) {
1614 retval = -ENOMEM;
1615 goto err_radio;
1616 }
1617 memcpy(radio->videodev, &si470x_viddev_template,
1618 sizeof(si470x_viddev_template));
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001619 video_set_drvdata(radio->videodev, radio);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001620
1621 /* show some infos about the specific device */
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001622 if (si470x_get_all_registers(radio) < 0) {
1623 retval = -EIO;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001624 goto err_all;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001625 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001626 printk(KERN_INFO DRIVER_NAME ": DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001627 radio->registers[DEVICEID], radio->registers[CHIPID]);
1628
1629 /* check if firmware is current */
1630 if ((radio->registers[CHIPID] & CHIPID_FIRMWARE)
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001631 < RADIO_SW_VERSION_CURRENT) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001632 printk(KERN_WARNING DRIVER_NAME
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001633 ": This driver is known to work with "
1634 "firmware version %hu,\n", RADIO_SW_VERSION_CURRENT);
1635 printk(KERN_WARNING DRIVER_NAME
1636 ": but the device has firmware version %hu.\n",
1637 radio->registers[CHIPID] & CHIPID_FIRMWARE);
1638 printk(KERN_WARNING DRIVER_NAME
1639 ": If you have some trouble using this driver,\n");
1640 printk(KERN_WARNING DRIVER_NAME
1641 ": please report to V4L ML at "
1642 "video4linux-list@redhat.com\n");
1643 }
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001644
1645 /* set initial frequency */
1646 si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
1647
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001648 /* rds buffer allocation */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001649 radio->buf_size = rds_buf * 3;
1650 radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001651 if (!radio->buffer) {
1652 retval = -EIO;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001653 goto err_all;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001654 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001655
1656 /* rds buffer configuration */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001657 radio->wr_index = 0;
1658 radio->rd_index = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001659 init_waitqueue_head(&radio->read_queue);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001660
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001661 /* prepare rds work function */
1662 INIT_DELAYED_WORK(&radio->work, si470x_work);
1663
1664 /* register video device */
Hans Verkuilcba99ae2008-09-03 17:11:58 -03001665 retval = video_register_device(radio->videodev, VFL_TYPE_RADIO, radio_nr);
1666 if (retval) {
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001667 printk(KERN_WARNING DRIVER_NAME
1668 ": Could not register video device\n");
1669 goto err_all;
1670 }
1671 usb_set_intfdata(intf, radio);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001672
1673 return 0;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001674err_all:
1675 video_device_release(radio->videodev);
1676 kfree(radio->buffer);
1677err_radio:
1678 kfree(radio);
1679err_initial:
1680 return retval;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001681}
1682
1683
1684/*
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001685 * si470x_usb_driver_suspend - suspend the device
1686 */
1687static int si470x_usb_driver_suspend(struct usb_interface *intf,
1688 pm_message_t message)
1689{
1690 struct si470x_device *radio = usb_get_intfdata(intf);
1691
1692 printk(KERN_INFO DRIVER_NAME ": suspending now...\n");
1693
1694 cancel_delayed_work_sync(&radio->work);
1695
1696 return 0;
1697}
1698
1699
1700/*
1701 * si470x_usb_driver_resume - resume the device
1702 */
1703static int si470x_usb_driver_resume(struct usb_interface *intf)
1704{
1705 struct si470x_device *radio = usb_get_intfdata(intf);
1706
1707 printk(KERN_INFO DRIVER_NAME ": resuming now...\n");
1708
1709 mutex_lock(&radio->lock);
1710 if (radio->users && radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS)
1711 schedule_delayed_work(&radio->work,
1712 msecs_to_jiffies(rds_poll_time));
1713 mutex_unlock(&radio->lock);
1714
1715 return 0;
1716}
1717
1718
1719/*
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001720 * si470x_usb_driver_disconnect - disconnect the device
1721 */
1722static void si470x_usb_driver_disconnect(struct usb_interface *intf)
1723{
1724 struct si470x_device *radio = usb_get_intfdata(intf);
1725
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001726 mutex_lock(&radio->disconnect_lock);
1727 radio->disconnected = 1;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001728 cancel_delayed_work_sync(&radio->work);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001729 usb_set_intfdata(intf, NULL);
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001730 if (radio->users == 0) {
1731 video_unregister_device(radio->videodev);
1732 kfree(radio->buffer);
1733 kfree(radio);
1734 }
1735 mutex_unlock(&radio->disconnect_lock);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001736}
1737
1738
1739/*
1740 * si470x_usb_driver - usb driver interface
1741 */
1742static struct usb_driver si470x_usb_driver = {
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001743 .name = DRIVER_NAME,
1744 .probe = si470x_usb_driver_probe,
1745 .disconnect = si470x_usb_driver_disconnect,
1746 .suspend = si470x_usb_driver_suspend,
1747 .resume = si470x_usb_driver_resume,
1748 .id_table = si470x_usb_driver_id_table,
1749 .supports_autosuspend = 1,
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001750};
1751
1752
1753
1754/**************************************************************************
1755 * Module Interface
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001756 **************************************************************************/
1757
1758/*
1759 * si470x_module_init - module init
1760 */
1761static int __init si470x_module_init(void)
1762{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001763 printk(KERN_INFO DRIVER_DESC ", Version " DRIVER_VERSION "\n");
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001764 return usb_register(&si470x_usb_driver);
1765}
1766
1767
1768/*
1769 * si470x_module_exit - module exit
1770 */
1771static void __exit si470x_module_exit(void)
1772{
1773 usb_deregister(&si470x_usb_driver);
1774}
1775
1776
1777module_init(si470x_module_init);
1778module_exit(si470x_module_exit);
1779
1780MODULE_LICENSE("GPL");
1781MODULE_AUTHOR(DRIVER_AUTHOR);
1782MODULE_DESCRIPTION(DRIVER_DESC);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001783MODULE_VERSION(DRIVER_VERSION);