blob: 41f30432507497b2df8fce3fd0c75b7d7589a0b4 [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 Lorenz00ec8d02009-02-12 14:55:45 -03008 * - Sanei Electric, Inc. FM USB Radio (sold as DealExtreme.com PCear)
Tobias Lorenz78656ac2008-01-14 21:55:27 -03009 *
Tobias Lorenz00ec8d02009-02-12 14:55:45 -030010 * Copyright (c) 2009 Tobias Lorenz <tobias.lorenz@gmx.net>
Tobias Lorenz78656ac2008-01-14 21:55:27 -030011 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 */
26
27
28/*
29 * History:
30 * 2008-01-12 Tobias Lorenz <tobias.lorenz@gmx.net>
31 * Version 1.0.0
32 * - First working version
Tobias Lorenz00ec8d02009-02-12 14:55:45 -030033 * 2008-01-13 Tobias Lorenz <tobias.lorenz@gmx.net>
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -030034 * Version 1.0.1
Tobias Lorenz78656ac2008-01-14 21:55:27 -030035 * - Improved error handling, every function now returns errno
36 * - Improved multi user access (start/mute/stop)
37 * - Channel doesn't get lost anymore after start/mute/stop
38 * - RDS support added (polling mode via interrupt EP 1)
39 * - marked default module parameters with *value*
40 * - switched from bit structs to bit masks
41 * - header file cleaned and integrated
42 * 2008-01-14 Tobias Lorenz <tobias.lorenz@gmx.net>
43 * Version 1.0.2
44 * - hex values are now lower case
45 * - commented USB ID for ADS/Tech moved on todo list
46 * - blacklisted si470x in hid-quirks.c
47 * - rds buffer handling functions integrated into *_work, *_read
48 * - rds_command in si470x_poll exchanged against simple retval
49 * - check for firmware version 15
50 * - code order and prototypes still remain the same
51 * - spacing and bottom of band codes remain the same
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -030052 * 2008-01-16 Tobias Lorenz <tobias.lorenz@gmx.net>
53 * Version 1.0.3
54 * - code reordered to avoid function prototypes
55 * - switch/case defaults are now more user-friendly
56 * - unified comment style
57 * - applied all checkpatch.pl v1.12 suggestions
58 * except the warning about the too long lines with bit comments
59 * - renamed FMRADIO to RADIO to cut line length (checkpatch.pl)
Tobias Lorenz2fb88402008-01-25 05:14:57 -030060 * 2008-01-22 Tobias Lorenz <tobias.lorenz@gmx.net>
61 * Version 1.0.4
62 * - avoid poss. locking when doing copy_to_user which may sleep
63 * - RDS is automatically activated on read now
64 * - code cleaned of unnecessary rds_commands
65 * - USB Vendor/Product ID for ADS/Tech FM Radio Receiver verified
66 * (thanks to Guillaume RAMOUSSE)
Tobias Lorenz0e3301e2008-01-27 14:54:07 -030067 * 2008-01-27 Tobias Lorenz <tobias.lorenz@gmx.net>
68 * Version 1.0.5
69 * - number of seek_retries changed to tune_timeout
70 * - fixed problem with incomplete tune operations by own buffers
Tobias Lorenz5caf5132008-02-04 22:26:08 -030071 * - optimization of variables and printf types
Tobias Lorenz0e3301e2008-01-27 14:54:07 -030072 * - improved error logging
Tobias Lorenz5caf5132008-02-04 22:26:08 -030073 * 2008-01-31 Tobias Lorenz <tobias.lorenz@gmx.net>
74 * Oliver Neukum <oliver@neukum.org>
75 * Version 1.0.6
76 * - fixed coverity checker warnings in *_usb_driver_disconnect
77 * - probe()/open() race by correct ordering in probe()
78 * - DMA coherency rules by separate allocation of all buffers
79 * - use of endianness macros
80 * - abuse of spinlock, replaced by mutex
81 * - racy handling of timer in disconnect,
82 * replaced by delayed_work
83 * - racy interruptible_sleep_on(),
84 * replaced with wait_event_interruptible()
85 * - handle signals in read()
Tobias Lorenz57566ad2008-02-09 16:08:24 -030086 * 2008-02-08 Tobias Lorenz <tobias.lorenz@gmx.net>
87 * Oliver Neukum <oliver@neukum.org>
88 * Version 1.0.7
89 * - usb autosuspend support
Tobias Lorenzce5829e2008-05-31 15:04:32 -030090 * - unplugging fixed
Tobias Lorenz6cc72652008-05-31 15:06:50 -030091 * 2008-05-07 Tobias Lorenz <tobias.lorenz@gmx.net>
92 * Version 1.0.8
Tobias Lorenzbbc89952008-05-31 15:11:32 -030093 * - hardware frequency seek support
Tobias Lorenzace7d4b2008-05-31 15:09:07 -030094 * - afc indication
Tobias Lorenza7c850a2008-05-31 15:07:52 -030095 * - more safety checks, let si470x_get_freq return errno
Tobias Lorenz33632d42008-09-24 19:30:26 -030096 * - vidioc behavior corrected according to v4l2 spec
Alexey Klimov69df96c2008-10-23 09:20:27 -030097 * 2008-10-20 Alexey Klimov <klimov.linux@gmail.com>
98 * - add support for KWorld USB FM Radio FM700
99 * - blacklisted KWorld radio in hid-core.c and hid-ids.h
Mark Lord5e6de7d2008-12-03 15:26:15 -0300100 * 2008-12-03 Mark Lord <mlord@pobox.com>
101 * - add support for DealExtreme USB Radio
Tobias Lorenzd807dec2009-02-12 14:56:10 -0300102 * 2009-01-31 Bob Ross <pigiron@gmx.com>
103 * - correction of stereo detection/setting
104 * - correction of signal strength indicator scaling
Tobias Lorenz2f94fc42009-02-12 14:56:19 -0300105 * 2009-01-31 Rick Bronson <rick@efn.org>
106 * Tobias Lorenz <tobias.lorenz@gmx.net>
107 * - add LED status output
Tobias Lorenz10711d02009-02-12 14:56:30 -0300108 * - get HW/SW version from scratchpad
Tobias Lorenz721f59e2009-06-20 19:00:06 -0300109 * 2009-06-16 Edouard Lafargue <edouard@lafargue.name>
110 * Version 1.0.10
111 * - add support for interrupt mode for RDS endpoint,
112 * instead of polling.
113 * Improves RDS reception significantly
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300114 *
115 * ToDo:
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300116 * - add firmware download/update support
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 Lorenz721f59e2009-06-20 19:00:06 -0300123#define DRIVER_KERNEL_VERSION KERNEL_VERSION(1, 0, 10)
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 Lorenz721f59e2009-06-20 19:00:06 -0300126#define DRIVER_VERSION "1.0.10"
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>
Alexey Dobriyan405f5572009-07-11 22:08:37 +0400134#include <linux/smp_lock.h>
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300135#include <linux/input.h>
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300136#include <linux/usb.h>
137#include <linux/hid.h>
138#include <linux/version.h>
Mauro Carvalho Chehab387d4472008-01-15 11:25:10 -0300139#include <linux/videodev2.h>
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300140#include <linux/mutex.h>
Mauro Carvalho Chehab387d4472008-01-15 11:25:10 -0300141#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300142#include <media/v4l2-ioctl.h>
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300143#include <media/rds.h>
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300144#include <asm/unaligned.h>
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300145
146
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300147/* USB Device ID List */
148static struct usb_device_id si470x_usb_driver_id_table[] = {
149 /* Silicon Labs USB FM Radio Reference Design */
Tobias Lorenz374ab682008-09-24 19:17:54 -0300150 { USB_DEVICE_AND_INTERFACE_INFO(0x10c4, 0x818a, USB_CLASS_HID, 0, 0) },
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300151 /* ADS/Tech FM Radio Receiver (formerly Instant FM Music) */
Tobias Lorenz374ab682008-09-24 19:17:54 -0300152 { USB_DEVICE_AND_INTERFACE_INFO(0x06e1, 0xa155, USB_CLASS_HID, 0, 0) },
Alexey Klimov69df96c2008-10-23 09:20:27 -0300153 /* KWorld USB FM Radio SnapMusic Mobile 700 (FM700) */
154 { USB_DEVICE_AND_INTERFACE_INFO(0x1b80, 0xd700, USB_CLASS_HID, 0, 0) },
Tobias Lorenz00ec8d02009-02-12 14:55:45 -0300155 /* Sanei Electric, Inc. FM USB Radio (sold as DealExtreme.com PCear) */
Mark Lord5e6de7d2008-12-03 15:26:15 -0300156 { USB_DEVICE_AND_INTERFACE_INFO(0x10c5, 0x819a, USB_CLASS_HID, 0, 0) },
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300157 /* Terminating entry */
158 { }
159};
160MODULE_DEVICE_TABLE(usb, si470x_usb_driver_id_table);
161
162
163
164/**************************************************************************
165 * Module Parameters
166 **************************************************************************/
167
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300168/* Radio Nr */
169static int radio_nr = -1;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300170module_param(radio_nr, int, 0444);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300171MODULE_PARM_DESC(radio_nr, "Radio Nr");
172
173/* Spacing (kHz) */
174/* 0: 200 kHz (USA, Australia) */
175/* 1: 100 kHz (Europe, Japan) */
176/* 2: 50 kHz */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300177static unsigned short space = 2;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300178module_param(space, ushort, 0444);
179MODULE_PARM_DESC(space, "Spacing: 0=200kHz 1=100kHz *2=50kHz*");
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300180
181/* Bottom of Band (MHz) */
182/* 0: 87.5 - 108 MHz (USA, Europe)*/
183/* 1: 76 - 108 MHz (Japan wide band) */
184/* 2: 76 - 90 MHz (Japan) */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300185static unsigned short band = 1;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300186module_param(band, ushort, 0444);
187MODULE_PARM_DESC(band, "Band: 0=87.5..108MHz *1=76..108MHz* 2=76..90MHz");
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300188
189/* De-emphasis */
190/* 0: 75 us (USA) */
191/* 1: 50 us (Europe, Australia, Japan) */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300192static unsigned short de = 1;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300193module_param(de, ushort, 0444);
194MODULE_PARM_DESC(de, "De-emphasis: 0=75us *1=50us*");
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300195
196/* USB timeout */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300197static unsigned int usb_timeout = 500;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300198module_param(usb_timeout, uint, 0644);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300199MODULE_PARM_DESC(usb_timeout, "USB timeout (ms): *500*");
200
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300201/* Tune timeout */
202static unsigned int tune_timeout = 3000;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300203module_param(tune_timeout, uint, 0644);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300204MODULE_PARM_DESC(tune_timeout, "Tune timeout: *3000*");
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300205
Tobias Lorenzbbc89952008-05-31 15:11:32 -0300206/* Seek timeout */
207static unsigned int seek_timeout = 5000;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300208module_param(seek_timeout, uint, 0644);
Tobias Lorenzbbc89952008-05-31 15:11:32 -0300209MODULE_PARM_DESC(seek_timeout, "Seek timeout: *5000*");
210
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300211/* RDS buffer blocks */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300212static unsigned int rds_buf = 100;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300213module_param(rds_buf, uint, 0444);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300214MODULE_PARM_DESC(rds_buf, "RDS buffer entries: *100*");
215
216/* RDS maximum block errors */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300217static unsigned short max_rds_errors = 1;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300218/* 0 means 0 errors requiring correction */
219/* 1 means 1-2 errors requiring correction (used by original USBRadio.exe) */
220/* 2 means 3-5 errors requiring correction */
221/* 3 means 6+ errors or errors in checkword, correction not possible */
Tobias Lorenz374ab682008-09-24 19:17:54 -0300222module_param(max_rds_errors, ushort, 0644);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300223MODULE_PARM_DESC(max_rds_errors, "RDS maximum block errors: *1*");
224
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300225
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
Tobias Lorenz00ec8d02009-02-12 14:55:45 -0300345#define STREAM_REPORT 19
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300346
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 */
Tobias Lorenz10711d02009-02-12 14:56:30 -0300353#define WRITE_REPORT_SIZE 4
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300354#define WRITE_REPORT 19
Tobias Lorenz10711d02009-02-12 14:56:30 -0300355#define FLASH_REPORT_SIZE 64
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300356#define FLASH_REPORT 20
Tobias Lorenz10711d02009-02-12 14:56:30 -0300357#define CRC_REPORT_SIZE 3
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300358#define CRC_REPORT 21
Tobias Lorenz10711d02009-02-12 14:56:30 -0300359#define RESPONSE_REPORT_SIZE 2
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300360#define RESPONSE_REPORT 22
361
362/* Report 23: currently unused, but can accept 60 byte reports on the HID */
363/* interrupt out endpoint 2 every 1 millisecond */
364#define UNUSED_REPORT 23
365
366
367
368/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300369 * Software/Hardware Versions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300370 **************************************************************************/
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300371#define RADIO_SW_VERSION_NOT_BOOTLOADABLE 6
372#define RADIO_SW_VERSION 7
373#define RADIO_SW_VERSION_CURRENT 15
374#define RADIO_HW_VERSION 1
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300375
376#define SCRATCH_PAGE_SW_VERSION 1
377#define SCRATCH_PAGE_HW_VERSION 2
378
379
380
381/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300382 * LED State Definitions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300383 **************************************************************************/
384#define LED_COMMAND 0x35
385
386#define NO_CHANGE_LED 0x00
387#define ALL_COLOR_LED 0x01 /* streaming state */
388#define BLINK_GREEN_LED 0x02 /* connect state */
389#define BLINK_RED_LED 0x04
390#define BLINK_ORANGE_LED 0x10 /* disconnect state */
391#define SOLID_GREEN_LED 0x20 /* tuning/seeking state */
392#define SOLID_RED_LED 0x40 /* bootload state */
393#define SOLID_ORANGE_LED 0x80
394
395
396
397/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300398 * Stream State Definitions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300399 **************************************************************************/
400#define STREAM_COMMAND 0x36
401#define STREAM_VIDPID 0x00
402#define STREAM_AUDIO 0xff
403
404
405
406/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300407 * Bootloader / Flash Commands
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300408 **************************************************************************/
409
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300410/* unique id sent to bootloader and required to put into a bootload state */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300411#define UNIQUE_BL_ID 0x34
412
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300413/* mask for the flash data */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300414#define FLASH_DATA_MASK 0x55
415
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300416/* bootloader commands */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300417#define GET_SW_VERSION_COMMAND 0x00
Tobias Lorenz00ec8d02009-02-12 14:55:45 -0300418#define SET_PAGE_COMMAND 0x01
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300419#define ERASE_PAGE_COMMAND 0x02
420#define WRITE_PAGE_COMMAND 0x03
421#define CRC_ON_PAGE_COMMAND 0x04
422#define READ_FLASH_BYTE_COMMAND 0x05
423#define RESET_DEVICE_COMMAND 0x06
424#define GET_HW_VERSION_COMMAND 0x07
425#define BLANK 0xff
426
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300427/* bootloader command responses */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300428#define COMMAND_OK 0x01
429#define COMMAND_FAILED 0x02
430#define COMMAND_PENDING 0x03
431
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300432
433
434/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300435 * General Driver Definitions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300436 **************************************************************************/
437
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300438/*
439 * si470x_device - private data
440 */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300441struct si470x_device {
442 /* reference to USB and video device */
443 struct usb_device *usbdev;
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300444 struct usb_interface *intf;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300445 struct video_device *videodev;
446
Tobias Lorenz721f59e2009-06-20 19:00:06 -0300447 /* Interrupt endpoint handling */
448 char *int_in_buffer;
449 struct usb_endpoint_descriptor *int_in_endpoint;
450 struct urb *int_in_urb;
451 int int_in_running;
452
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300453 /* driver management */
454 unsigned int users;
Tobias Lorenzce5829e2008-05-31 15:04:32 -0300455 unsigned char disconnected;
456 struct mutex disconnect_lock;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300457
458 /* Silabs internal registers (0..15) */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300459 unsigned short registers[RADIO_REGISTER_NUM];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300460
461 /* RDS receive buffer */
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300462 wait_queue_head_t read_queue;
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300463 struct mutex lock; /* buffer locking */
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300464 unsigned char *buffer; /* size is always multiple of three */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300465 unsigned int buf_size;
466 unsigned int rd_index;
467 unsigned int wr_index;
Tobias Lorenz10711d02009-02-12 14:56:30 -0300468
469 /* scratch page */
470 unsigned char software_version;
471 unsigned char hardware_version;
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 Lorenz7f53b352009-02-12 14:55:56 -0300487 * General Driver Functions - REGISTER_REPORTs
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/*
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300573 * si470x_set_chan - set the channel
574 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300575static int si470x_set_chan(struct si470x_device *radio, unsigned short chan)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300576{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300577 int retval;
578 unsigned long timeout;
579 bool timed_out = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300580
581 /* start tuning */
582 radio->registers[CHANNEL] &= ~CHANNEL_CHAN;
583 radio->registers[CHANNEL] |= CHANNEL_TUNE | chan;
584 retval = si470x_set_register(radio, CHANNEL);
585 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300586 goto done;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300587
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300588 /* wait till tune operation has completed */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300589 timeout = jiffies + msecs_to_jiffies(tune_timeout);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300590 do {
591 retval = si470x_get_register(radio, STATUSRSSI);
592 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300593 goto stop;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300594 timed_out = time_after(jiffies, timeout);
595 } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
596 (!timed_out));
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300597 if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
598 printk(KERN_WARNING DRIVER_NAME ": tune does not complete\n");
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300599 if (timed_out)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300600 printk(KERN_WARNING DRIVER_NAME
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300601 ": tune timed out after %u ms\n", tune_timeout);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300602
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300603stop:
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300604 /* stop tuning */
605 radio->registers[CHANNEL] &= ~CHANNEL_TUNE;
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300606 retval = si470x_set_register(radio, CHANNEL);
607
608done:
609 return retval;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300610}
611
612
613/*
614 * si470x_get_freq - get the frequency
615 */
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300616static int si470x_get_freq(struct si470x_device *radio, unsigned int *freq)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300617{
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300618 unsigned int spacing, band_bottom;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300619 unsigned short chan;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300620 int retval;
621
622 /* Spacing (kHz) */
Tobias Lorenza17c00192008-09-24 19:21:50 -0300623 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300624 /* 0: 200 kHz (USA, Australia) */
Tobias Lorenza17c00192008-09-24 19:21:50 -0300625 case 0:
626 spacing = 0.200 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300627 /* 1: 100 kHz (Europe, Japan) */
Tobias Lorenza17c00192008-09-24 19:21:50 -0300628 case 1:
629 spacing = 0.100 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300630 /* 2: 50 kHz */
Tobias Lorenza17c00192008-09-24 19:21:50 -0300631 default:
632 spacing = 0.050 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300633 };
634
635 /* Bottom of Band (MHz) */
Tobias Lorenza17c00192008-09-24 19:21:50 -0300636 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300637 /* 0: 87.5 - 108 MHz (USA, Europe) */
Tobias Lorenza17c00192008-09-24 19:21:50 -0300638 case 0:
639 band_bottom = 87.5 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300640 /* 1: 76 - 108 MHz (Japan wide band) */
Tobias Lorenza17c00192008-09-24 19:21:50 -0300641 default:
642 band_bottom = 76 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300643 /* 2: 76 - 90 MHz (Japan) */
Tobias Lorenza17c00192008-09-24 19:21:50 -0300644 case 2:
645 band_bottom = 76 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300646 };
647
648 /* read channel */
649 retval = si470x_get_register(radio, READCHAN);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300650 chan = radio->registers[READCHAN] & READCHAN_READCHAN;
651
652 /* Frequency (MHz) = Spacing (kHz) x Channel + Bottom of Band (MHz) */
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300653 *freq = chan * spacing + band_bottom;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300654
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300655 return retval;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300656}
657
658
659/*
660 * si470x_set_freq - set the frequency
661 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300662static int si470x_set_freq(struct si470x_device *radio, unsigned int freq)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300663{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300664 unsigned int spacing, band_bottom;
665 unsigned short chan;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300666
667 /* Spacing (kHz) */
Tobias Lorenza17c00192008-09-24 19:21:50 -0300668 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300669 /* 0: 200 kHz (USA, Australia) */
Tobias Lorenza17c00192008-09-24 19:21:50 -0300670 case 0:
671 spacing = 0.200 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300672 /* 1: 100 kHz (Europe, Japan) */
Tobias Lorenza17c00192008-09-24 19:21:50 -0300673 case 1:
674 spacing = 0.100 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300675 /* 2: 50 kHz */
Tobias Lorenza17c00192008-09-24 19:21:50 -0300676 default:
677 spacing = 0.050 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300678 };
679
680 /* Bottom of Band (MHz) */
Tobias Lorenza17c00192008-09-24 19:21:50 -0300681 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300682 /* 0: 87.5 - 108 MHz (USA, Europe) */
Tobias Lorenza17c00192008-09-24 19:21:50 -0300683 case 0:
684 band_bottom = 87.5 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300685 /* 1: 76 - 108 MHz (Japan wide band) */
Tobias Lorenza17c00192008-09-24 19:21:50 -0300686 default:
687 band_bottom = 76 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300688 /* 2: 76 - 90 MHz (Japan) */
Tobias Lorenza17c00192008-09-24 19:21:50 -0300689 case 2:
690 band_bottom = 76 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300691 };
692
693 /* Chan = [ Freq (Mhz) - Bottom of Band (MHz) ] / Spacing (kHz) */
694 chan = (freq - band_bottom) / spacing;
695
696 return si470x_set_chan(radio, chan);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300697}
698
699
700/*
Tobias Lorenzbbc89952008-05-31 15:11:32 -0300701 * si470x_set_seek - set seek
702 */
703static int si470x_set_seek(struct si470x_device *radio,
704 unsigned int wrap_around, unsigned int seek_upward)
705{
706 int retval = 0;
707 unsigned long timeout;
708 bool timed_out = 0;
709
710 /* start seeking */
711 radio->registers[POWERCFG] |= POWERCFG_SEEK;
712 if (wrap_around == 1)
713 radio->registers[POWERCFG] &= ~POWERCFG_SKMODE;
714 else
715 radio->registers[POWERCFG] |= POWERCFG_SKMODE;
716 if (seek_upward == 1)
717 radio->registers[POWERCFG] |= POWERCFG_SEEKUP;
718 else
719 radio->registers[POWERCFG] &= ~POWERCFG_SEEKUP;
720 retval = si470x_set_register(radio, POWERCFG);
721 if (retval < 0)
722 goto done;
723
724 /* wait till seek operation has completed */
725 timeout = jiffies + msecs_to_jiffies(seek_timeout);
726 do {
727 retval = si470x_get_register(radio, STATUSRSSI);
728 if (retval < 0)
729 goto stop;
730 timed_out = time_after(jiffies, timeout);
731 } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
732 (!timed_out));
733 if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
734 printk(KERN_WARNING DRIVER_NAME ": seek does not complete\n");
735 if (radio->registers[STATUSRSSI] & STATUSRSSI_SF)
736 printk(KERN_WARNING DRIVER_NAME
737 ": seek failed / band limit reached\n");
738 if (timed_out)
739 printk(KERN_WARNING DRIVER_NAME
740 ": seek timed out after %u ms\n", seek_timeout);
741
742stop:
743 /* stop seeking */
744 radio->registers[POWERCFG] &= ~POWERCFG_SEEK;
745 retval = si470x_set_register(radio, POWERCFG);
746
747done:
748 /* try again, if timed out */
749 if ((retval == 0) && timed_out)
750 retval = -EAGAIN;
751
752 return retval;
753}
754
755
756/*
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300757 * si470x_start - switch on radio
758 */
759static int si470x_start(struct si470x_device *radio)
760{
761 int retval;
762
763 /* powercfg */
764 radio->registers[POWERCFG] =
765 POWERCFG_DMUTE | POWERCFG_ENABLE | POWERCFG_RDSM;
766 retval = si470x_set_register(radio, POWERCFG);
767 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300768 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300769
770 /* sysconfig 1 */
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300771 radio->registers[SYSCONFIG1] = SYSCONFIG1_DE;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300772 retval = si470x_set_register(radio, SYSCONFIG1);
773 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300774 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300775
776 /* sysconfig 2 */
777 radio->registers[SYSCONFIG2] =
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300778 (0x3f << 8) | /* SEEKTH */
779 ((band << 6) & SYSCONFIG2_BAND) | /* BAND */
780 ((space << 4) & SYSCONFIG2_SPACE) | /* SPACE */
781 15; /* VOLUME (max) */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300782 retval = si470x_set_register(radio, SYSCONFIG2);
783 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300784 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300785
786 /* reset last channel */
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300787 retval = si470x_set_chan(radio,
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300788 radio->registers[CHANNEL] & CHANNEL_CHAN);
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300789
790done:
791 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300792}
793
794
795/*
796 * si470x_stop - switch off radio
797 */
798static int si470x_stop(struct si470x_device *radio)
799{
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300800 int retval;
801
802 /* sysconfig 1 */
803 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
804 retval = si470x_set_register(radio, SYSCONFIG1);
805 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300806 goto done;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300807
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300808 /* powercfg */
809 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
810 /* POWERCFG_ENABLE has to automatically go low */
811 radio->registers[POWERCFG] |= POWERCFG_ENABLE | POWERCFG_DISABLE;
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300812 retval = si470x_set_register(radio, POWERCFG);
813
814done:
815 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300816}
817
818
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300819/*
820 * si470x_rds_on - switch on rds reception
821 */
822static int si470x_rds_on(struct si470x_device *radio)
823{
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300824 int retval;
825
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300826 /* sysconfig 1 */
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300827 mutex_lock(&radio->lock);
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300828 radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDS;
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300829 retval = si470x_set_register(radio, SYSCONFIG1);
830 if (retval < 0)
831 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
832 mutex_unlock(&radio->lock);
833
834 return retval;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300835}
836
837
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300838
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300839/**************************************************************************
Tobias Lorenz7f53b352009-02-12 14:55:56 -0300840 * General Driver Functions - ENTIRE_REPORT
841 **************************************************************************/
842
843/*
844 * si470x_get_all_registers - read entire registers
845 */
846static int si470x_get_all_registers(struct si470x_device *radio)
847{
848 unsigned char buf[ENTIRE_REPORT_SIZE];
849 int retval;
850 unsigned char regnr;
851
852 buf[0] = ENTIRE_REPORT;
853
854 retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
855
856 if (retval >= 0)
857 for (regnr = 0; regnr < RADIO_REGISTER_NUM; regnr++)
858 radio->registers[regnr] = get_unaligned_be16(
859 &buf[regnr * RADIO_REGISTER_SIZE + 1]);
860
861 return (retval < 0) ? -EINVAL : 0;
862}
863
864
865
866/**************************************************************************
Tobias Lorenz10711d02009-02-12 14:56:30 -0300867 * General Driver Functions - LED_REPORT
868 **************************************************************************/
869
870/*
871 * si470x_set_led_state - sets the led state
872 */
873static int si470x_set_led_state(struct si470x_device *radio,
874 unsigned char led_state)
875{
876 unsigned char buf[LED_REPORT_SIZE];
877 int retval;
878
879 buf[0] = LED_REPORT;
880 buf[1] = LED_COMMAND;
881 buf[2] = led_state;
882
883 retval = si470x_set_report(radio, (void *) &buf, sizeof(buf));
884
885 return (retval < 0) ? -EINVAL : 0;
886}
887
888
889
890/**************************************************************************
891 * General Driver Functions - SCRATCH_REPORT
892 **************************************************************************/
893
894/*
895 * si470x_get_scratch_versions - gets the scratch page and version infos
896 */
897static int si470x_get_scratch_page_versions(struct si470x_device *radio)
898{
899 unsigned char buf[SCRATCH_REPORT_SIZE];
900 int retval;
901
902 buf[0] = SCRATCH_REPORT;
903
904 retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
905
906 if (retval < 0)
907 printk(KERN_WARNING DRIVER_NAME ": si470x_get_scratch: "
908 "si470x_get_report returned %d\n", retval);
909 else {
910 radio->software_version = buf[1];
911 radio->hardware_version = buf[2];
912 }
913
914 return (retval < 0) ? -EINVAL : 0;
915}
916
917
918
919/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300920 * RDS Driver Functions
921 **************************************************************************/
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300922
923/*
Tobias Lorenz721f59e2009-06-20 19:00:06 -0300924 * si470x_int_in_callback - rds callback and processing function
925 *
926 * TODO: do we need to use mutex locks in some sections?
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300927 */
Tobias Lorenz721f59e2009-06-20 19:00:06 -0300928static void si470x_int_in_callback(struct urb *urb)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300929{
Tobias Lorenz721f59e2009-06-20 19:00:06 -0300930 struct si470x_device *radio = urb->context;
931 unsigned char buf[RDS_REPORT_SIZE];
932 int retval;
933 unsigned char regnr;
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300934 unsigned char blocknum;
935 unsigned short bler; /* rds block errors */
936 unsigned short rds;
937 unsigned char tmpbuf[3];
938
Tobias Lorenz721f59e2009-06-20 19:00:06 -0300939 if (urb->status) {
940 if (urb->status == -ENOENT ||
941 urb->status == -ECONNRESET ||
942 urb->status == -ESHUTDOWN) {
943 return;
944 } else {
945 printk(KERN_WARNING DRIVER_NAME
946 ": non-zero urb status (%d)\n", urb->status);
947 goto resubmit; /* Maybe we can recover. */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300948 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300949 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300950
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300951 /* safety checks */
Tobias Lorenzce5829e2008-05-31 15:04:32 -0300952 if (radio->disconnected)
953 return;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300954 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
Tobias Lorenz721f59e2009-06-20 19:00:06 -0300955 goto resubmit;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300956
Tobias Lorenz721f59e2009-06-20 19:00:06 -0300957 if (urb->actual_length > 0) {
958 /* Update RDS registers with URB data */
959 buf[0] = RDS_REPORT;
960 for (regnr = 0; regnr < RDS_REGISTER_NUM; regnr++)
961 radio->registers[STATUSRSSI + regnr] =
962 get_unaligned_be16(&radio->int_in_buffer[
963 regnr * RADIO_REGISTER_SIZE + 1]);
964 /* get rds blocks */
965 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0) {
966 /* No RDS group ready, better luck next time */
967 goto resubmit;
968 }
969 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSS) == 0) {
970 /* RDS decoder not synchronized */
971 goto resubmit;
972 }
973 for (blocknum = 0; blocknum < 4; blocknum++) {
974 switch (blocknum) {
975 default:
976 bler = (radio->registers[STATUSRSSI] &
977 STATUSRSSI_BLERA) >> 9;
978 rds = radio->registers[RDSA];
979 break;
980 case 1:
981 bler = (radio->registers[READCHAN] &
982 READCHAN_BLERB) >> 14;
983 rds = radio->registers[RDSB];
984 break;
985 case 2:
986 bler = (radio->registers[READCHAN] &
987 READCHAN_BLERC) >> 12;
988 rds = radio->registers[RDSC];
989 break;
990 case 3:
991 bler = (radio->registers[READCHAN] &
992 READCHAN_BLERD) >> 10;
993 rds = radio->registers[RDSD];
994 break;
995 };
996
997 /* Fill the V4L2 RDS buffer */
998 put_unaligned_le16(rds, &tmpbuf);
999 tmpbuf[2] = blocknum; /* offset name */
1000 tmpbuf[2] |= blocknum << 3; /* received offset */
1001 if (bler > max_rds_errors)
1002 tmpbuf[2] |= 0x80; /* uncorrectable errors */
1003 else if (bler > 0)
1004 tmpbuf[2] |= 0x40; /* corrected error(s) */
1005
1006 /* copy RDS block to internal buffer */
1007 memcpy(&radio->buffer[radio->wr_index], &tmpbuf, 3);
1008 radio->wr_index += 3;
1009
1010 /* wrap write pointer */
1011 if (radio->wr_index >= radio->buf_size)
1012 radio->wr_index = 0;
1013
1014 /* check for overflow */
1015 if (radio->wr_index == radio->rd_index) {
1016 /* increment and wrap read pointer */
1017 radio->rd_index += 3;
1018 if (radio->rd_index >= radio->buf_size)
1019 radio->rd_index = 0;
1020 }
1021 }
1022 if (radio->wr_index != radio->rd_index)
1023 wake_up_interruptible(&radio->read_queue);
1024 }
1025
1026resubmit:
1027 /* Resubmit if we're still running. */
1028 if (radio->int_in_running && radio->usbdev) {
1029 retval = usb_submit_urb(radio->int_in_urb, GFP_ATOMIC);
1030 if (retval) {
1031 printk(KERN_WARNING DRIVER_NAME
1032 ": resubmitting urb failed (%d)", retval);
1033 radio->int_in_running = 0;
1034 }
1035 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001036}
1037
1038
1039
1040/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001041 * File Operations Interface
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001042 **************************************************************************/
1043
1044/*
1045 * si470x_fops_read - read RDS data
1046 */
1047static ssize_t si470x_fops_read(struct file *file, char __user *buf,
1048 size_t count, loff_t *ppos)
1049{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001050 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001051 int retval = 0;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001052 unsigned int block_count = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001053
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001054 /* switch on rds reception */
1055 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
1056 si470x_rds_on(radio);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001057 }
1058
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001059 /* block if no new data available */
1060 while (radio->wr_index == radio->rd_index) {
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001061 if (file->f_flags & O_NONBLOCK) {
1062 retval = -EWOULDBLOCK;
1063 goto done;
1064 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001065 if (wait_event_interruptible(radio->read_queue,
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001066 radio->wr_index != radio->rd_index) < 0) {
1067 retval = -EINTR;
1068 goto done;
1069 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001070 }
1071
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001072 /* calculate block count from byte count */
1073 count /= 3;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001074
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001075 /* copy RDS block out of internal buffer and to user buffer */
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001076 mutex_lock(&radio->lock);
1077 while (block_count < count) {
1078 if (radio->rd_index == radio->wr_index)
1079 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001080
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001081 /* always transfer rds complete blocks */
1082 if (copy_to_user(buf, &radio->buffer[radio->rd_index], 3))
1083 /* retval = -EFAULT; */
1084 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001085
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001086 /* increment and wrap read pointer */
1087 radio->rd_index += 3;
1088 if (radio->rd_index >= radio->buf_size)
1089 radio->rd_index = 0;
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001090
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001091 /* increment counters */
1092 block_count++;
1093 buf += 3;
1094 retval += 3;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001095 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001096 mutex_unlock(&radio->lock);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001097
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001098done:
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001099 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001100}
1101
1102
1103/*
1104 * si470x_fops_poll - poll RDS data
1105 */
1106static unsigned int si470x_fops_poll(struct file *file,
1107 struct poll_table_struct *pts)
1108{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001109 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001110 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001111
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001112 /* switch on rds reception */
1113 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
1114 si470x_rds_on(radio);
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001115 }
1116
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001117 poll_wait(file, &radio->read_queue, pts);
1118
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001119 if (radio->rd_index != radio->wr_index)
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001120 retval = POLLIN | POLLRDNORM;
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001121
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001122 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001123}
1124
1125
1126/*
1127 * si470x_fops_open - file open
1128 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001129static int si470x_fops_open(struct file *file)
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001130{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001131 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001132 int retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001133
Hans Verkuild56dc612008-07-30 08:43:36 -03001134 lock_kernel();
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001135 radio->users++;
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001136
1137 retval = usb_autopm_get_interface(radio->intf);
1138 if (retval < 0) {
1139 radio->users--;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001140 retval = -EIO;
1141 goto done;
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001142 }
1143
Tobias Lorenz721f59e2009-06-20 19:00:06 -03001144 printk(KERN_INFO DRIVER_NAME
1145 ": Opened radio (users now: %i)\n", radio->users);
1146
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001147 if (radio->users == 1) {
Tobias Lorenz7f53b352009-02-12 14:55:56 -03001148 /* start radio */
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001149 retval = si470x_start(radio);
Tobias Lorenz721f59e2009-06-20 19:00:06 -03001150 if (retval < 0) {
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001151 usb_autopm_put_interface(radio->intf);
Tobias Lorenz721f59e2009-06-20 19:00:06 -03001152 goto done;
1153 }
1154 /* Initialize interrupt URB. */
1155 usb_fill_int_urb(radio->int_in_urb, radio->usbdev,
1156 usb_rcvintpipe(radio->usbdev,
1157 radio->int_in_endpoint->bEndpointAddress),
1158 radio->int_in_buffer,
1159 le16_to_cpu(radio->int_in_endpoint->wMaxPacketSize),
1160 si470x_int_in_callback,
1161 radio,
1162 radio->int_in_endpoint->bInterval);
1163
1164 radio->int_in_running = 1;
1165 mb();
1166
1167 retval = usb_submit_urb(radio->int_in_urb, GFP_KERNEL);
1168 if (retval) {
1169 printk(KERN_INFO DRIVER_NAME
1170 ": submitting int urb failed (%d)\n", retval);
1171 radio->int_in_running = 0;
1172 usb_autopm_put_interface(radio->intf);
1173 }
1174
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001175 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001176
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001177done:
Hans Verkuild56dc612008-07-30 08:43:36 -03001178 unlock_kernel();
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001179 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001180}
1181
1182
1183/*
1184 * si470x_fops_release - file release
1185 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001186static int si470x_fops_release(struct file *file)
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001187{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001188 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001189 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001190
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001191 /* safety check */
1192 if (!radio) {
1193 retval = -ENODEV;
1194 goto done;
1195 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001196
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001197 mutex_lock(&radio->disconnect_lock);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001198 radio->users--;
Tobias Lorenz721f59e2009-06-20 19:00:06 -03001199 printk(KERN_INFO DRIVER_NAME
1200 ": Closed radio (remaining users:%i)\n", radio->users);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001201 if (radio->users == 0) {
Tobias Lorenz721f59e2009-06-20 19:00:06 -03001202
1203 /* Shutdown Interrupt handler */
1204 if (radio->int_in_running) {
1205 radio->int_in_running = 0;
1206 if (radio->int_in_urb)
1207 usb_kill_urb(radio->int_in_urb);
1208 }
1209
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001210 if (radio->disconnected) {
1211 video_unregister_device(radio->videodev);
Tobias Lorenz721f59e2009-06-20 19:00:06 -03001212 kfree(radio->int_in_buffer);
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001213 kfree(radio->buffer);
1214 kfree(radio);
Jiri Slabya5ca3a12009-07-05 15:44:20 -03001215 goto unlock;
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001216 }
Tobias Lorenz03dea862008-04-22 14:46:11 -03001217
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001218 /* cancel read processes */
1219 wake_up_interruptible(&radio->read_queue);
1220
Tobias Lorenz7f53b352009-02-12 14:55:56 -03001221 /* stop radio */
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001222 retval = si470x_stop(radio);
1223 usb_autopm_put_interface(radio->intf);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001224 }
Jiri Slabya5ca3a12009-07-05 15:44:20 -03001225unlock:
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001226 mutex_unlock(&radio->disconnect_lock);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001227done:
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001228 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001229}
1230
1231
1232/*
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001233 * si470x_fops - file operations interface
1234 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001235static const struct v4l2_file_operations si470x_fops = {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001236 .owner = THIS_MODULE,
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001237 .read = si470x_fops_read,
1238 .poll = si470x_fops_poll,
1239 .ioctl = video_ioctl2,
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001240 .open = si470x_fops_open,
1241 .release = si470x_fops_release,
1242};
1243
1244
1245
1246/**************************************************************************
1247 * Video4Linux Interface
1248 **************************************************************************/
1249
1250/*
1251 * si470x_v4l2_queryctrl - query control
1252 */
1253static struct v4l2_queryctrl si470x_v4l2_queryctrl[] = {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001254 {
1255 .id = V4L2_CID_AUDIO_VOLUME,
1256 .type = V4L2_CTRL_TYPE_INTEGER,
1257 .name = "Volume",
1258 .minimum = 0,
1259 .maximum = 15,
1260 .step = 1,
1261 .default_value = 15,
1262 },
1263 {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001264 .id = V4L2_CID_AUDIO_MUTE,
1265 .type = V4L2_CTRL_TYPE_BOOLEAN,
1266 .name = "Mute",
1267 .minimum = 0,
1268 .maximum = 1,
1269 .step = 1,
1270 .default_value = 1,
1271 },
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001272};
1273
1274
1275/*
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001276 * si470x_vidioc_querycap - query device capabilities
1277 */
1278static int si470x_vidioc_querycap(struct file *file, void *priv,
1279 struct v4l2_capability *capability)
1280{
Alexey Klimovdc025d42009-02-05 22:52:29 -03001281 struct si470x_device *radio = video_drvdata(file);
1282
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001283 strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver));
1284 strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
Alexey Klimovdc025d42009-02-05 22:52:29 -03001285 usb_make_path(radio->usbdev, capability->bus_info, sizeof(capability->bus_info));
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001286 capability->version = DRIVER_KERNEL_VERSION;
Tobias Lorenzbbc89952008-05-31 15:11:32 -03001287 capability->capabilities = V4L2_CAP_HW_FREQ_SEEK |
1288 V4L2_CAP_TUNER | V4L2_CAP_RADIO;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001289
1290 return 0;
1291}
1292
1293
1294/*
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001295 * si470x_vidioc_queryctrl - enumerate control items
1296 */
1297static int si470x_vidioc_queryctrl(struct file *file, void *priv,
1298 struct v4l2_queryctrl *qc)
1299{
Tobias Lorenzeb6e3b82008-09-24 19:25:39 -03001300 unsigned char i = 0;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001301 int retval = -EINVAL;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001302
Tobias Lorenzeb6e3b82008-09-24 19:25:39 -03001303 /* abort if qc->id is below V4L2_CID_BASE */
1304 if (qc->id < V4L2_CID_BASE)
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001305 goto done;
1306
Tobias Lorenzeb6e3b82008-09-24 19:25:39 -03001307 /* search video control */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001308 for (i = 0; i < ARRAY_SIZE(si470x_v4l2_queryctrl); i++) {
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001309 if (qc->id == si470x_v4l2_queryctrl[i].id) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001310 memcpy(qc, &(si470x_v4l2_queryctrl[i]), sizeof(*qc));
Tobias Lorenzeb6e3b82008-09-24 19:25:39 -03001311 retval = 0; /* found */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001312 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001313 }
1314 }
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001315
Tobias Lorenzeb6e3b82008-09-24 19:25:39 -03001316 /* disable unsupported base controls */
1317 /* to satisfy kradio and such apps */
1318 if ((retval == -EINVAL) && (qc->id < V4L2_CID_LASTP1)) {
1319 qc->flags = V4L2_CTRL_FLAG_DISABLED;
1320 retval = 0;
1321 }
1322
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001323done:
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001324 if (retval < 0)
1325 printk(KERN_WARNING DRIVER_NAME
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001326 ": query controls failed with %d\n", retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001327 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001328}
1329
1330
1331/*
1332 * si470x_vidioc_g_ctrl - get the value of a control
1333 */
1334static int si470x_vidioc_g_ctrl(struct file *file, void *priv,
1335 struct v4l2_control *ctrl)
1336{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001337 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001338 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001339
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001340 /* safety checks */
1341 if (radio->disconnected) {
1342 retval = -EIO;
1343 goto done;
1344 }
Tobias Lorenz03dea862008-04-22 14:46:11 -03001345
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001346 switch (ctrl->id) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001347 case V4L2_CID_AUDIO_VOLUME:
1348 ctrl->value = radio->registers[SYSCONFIG2] &
1349 SYSCONFIG2_VOLUME;
1350 break;
1351 case V4L2_CID_AUDIO_MUTE:
1352 ctrl->value = ((radio->registers[POWERCFG] &
1353 POWERCFG_DMUTE) == 0) ? 1 : 0;
1354 break;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001355 default:
1356 retval = -EINVAL;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001357 }
1358
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001359done:
1360 if (retval < 0)
1361 printk(KERN_WARNING DRIVER_NAME
1362 ": get control failed with %d\n", retval);
1363 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001364}
1365
1366
1367/*
1368 * si470x_vidioc_s_ctrl - set the value of a control
1369 */
1370static int si470x_vidioc_s_ctrl(struct file *file, void *priv,
1371 struct v4l2_control *ctrl)
1372{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001373 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001374 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001375
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001376 /* safety checks */
1377 if (radio->disconnected) {
1378 retval = -EIO;
1379 goto done;
1380 }
Tobias Lorenz03dea862008-04-22 14:46:11 -03001381
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001382 switch (ctrl->id) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001383 case V4L2_CID_AUDIO_VOLUME:
1384 radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_VOLUME;
1385 radio->registers[SYSCONFIG2] |= ctrl->value;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001386 retval = si470x_set_register(radio, SYSCONFIG2);
1387 break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001388 case V4L2_CID_AUDIO_MUTE:
1389 if (ctrl->value == 1)
1390 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
1391 else
1392 radio->registers[POWERCFG] |= POWERCFG_DMUTE;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001393 retval = si470x_set_register(radio, POWERCFG);
1394 break;
1395 default:
1396 retval = -EINVAL;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001397 }
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001398
1399done:
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001400 if (retval < 0)
1401 printk(KERN_WARNING DRIVER_NAME
1402 ": set control failed with %d\n", retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001403 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001404}
1405
1406
1407/*
1408 * si470x_vidioc_g_audio - get audio attributes
1409 */
1410static int si470x_vidioc_g_audio(struct file *file, void *priv,
1411 struct v4l2_audio *audio)
1412{
Tobias Lorenz617f5332008-09-24 19:33:09 -03001413 /* driver constants */
1414 audio->index = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001415 strcpy(audio->name, "Radio");
1416 audio->capability = V4L2_AUDCAP_STEREO;
Tobias Lorenz617f5332008-09-24 19:33:09 -03001417 audio->mode = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001418
Tobias Lorenz617f5332008-09-24 19:33:09 -03001419 return 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001420}
1421
1422
1423/*
1424 * si470x_vidioc_g_tuner - get tuner attributes
1425 */
1426static int si470x_vidioc_g_tuner(struct file *file, void *priv,
1427 struct v4l2_tuner *tuner)
1428{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001429 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001430 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001431
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001432 /* safety checks */
1433 if (radio->disconnected) {
1434 retval = -EIO;
1435 goto done;
1436 }
Tobias Lorenz33632d42008-09-24 19:30:26 -03001437 if (tuner->index != 0) {
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001438 retval = -EINVAL;
1439 goto done;
1440 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001441
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001442 retval = si470x_get_register(radio, STATUSRSSI);
1443 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001444 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001445
Tobias Lorenz33632d42008-09-24 19:30:26 -03001446 /* driver constants */
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001447 strcpy(tuner->name, "FM");
Tobias Lorenz33632d42008-09-24 19:30:26 -03001448 tuner->type = V4L2_TUNER_RADIO;
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001449 tuner->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
Tobias Lorenz33632d42008-09-24 19:30:26 -03001450
Tobias Lorenza17c00192008-09-24 19:21:50 -03001451 /* range limits */
1452 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001453 /* 0: 87.5 - 108 MHz (USA, Europe, default) */
1454 default:
1455 tuner->rangelow = 87.5 * FREQ_MUL;
1456 tuner->rangehigh = 108 * FREQ_MUL;
1457 break;
1458 /* 1: 76 - 108 MHz (Japan wide band) */
1459 case 1 :
1460 tuner->rangelow = 76 * FREQ_MUL;
1461 tuner->rangehigh = 108 * FREQ_MUL;
1462 break;
1463 /* 2: 76 - 90 MHz (Japan) */
1464 case 2 :
1465 tuner->rangelow = 76 * FREQ_MUL;
1466 tuner->rangehigh = 90 * FREQ_MUL;
1467 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001468 };
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001469
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001470 /* stereo indicator == stereo (instead of mono) */
Tobias Lorenzd807dec2009-02-12 14:56:10 -03001471 if ((radio->registers[STATUSRSSI] & STATUSRSSI_ST) == 0)
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001472 tuner->rxsubchans = V4L2_TUNER_SUB_MONO;
Tobias Lorenzd807dec2009-02-12 14:56:10 -03001473 else
1474 tuner->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001475
1476 /* mono/stereo selector */
Tobias Lorenzd807dec2009-02-12 14:56:10 -03001477 if ((radio->registers[POWERCFG] & POWERCFG_MONO) == 0)
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001478 tuner->audmode = V4L2_TUNER_MODE_STEREO;
Tobias Lorenzd807dec2009-02-12 14:56:10 -03001479 else
1480 tuner->audmode = V4L2_TUNER_MODE_MONO;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001481
1482 /* min is worst, max is best; signal:0..0xffff; rssi: 0..0xff */
Tobias Lorenzd807dec2009-02-12 14:56:10 -03001483 /* measured in units of dbµV in 1 db increments (max at ~75 dbµV) */
1484 tuner->signal = (radio->registers[STATUSRSSI] & STATUSRSSI_RSSI);
1485 /* the ideal factor is 0xffff/75 = 873,8 */
1486 tuner->signal = (tuner->signal * 873) + (8 * tuner->signal / 10);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001487
1488 /* automatic frequency control: -1: freq to low, 1 freq to high */
Tobias Lorenzace7d4b2008-05-31 15:09:07 -03001489 /* AFCRL does only indicate that freq. differs, not if too low/high */
1490 tuner->afc = (radio->registers[STATUSRSSI] & STATUSRSSI_AFCRL) ? 1 : 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001491
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001492done:
1493 if (retval < 0)
1494 printk(KERN_WARNING DRIVER_NAME
1495 ": get tuner failed with %d\n", retval);
1496 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001497}
1498
1499
1500/*
1501 * si470x_vidioc_s_tuner - set tuner attributes
1502 */
1503static int si470x_vidioc_s_tuner(struct file *file, void *priv,
1504 struct v4l2_tuner *tuner)
1505{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001506 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenz33632d42008-09-24 19:30:26 -03001507 int retval = -EINVAL;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001508
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001509 /* safety checks */
1510 if (radio->disconnected) {
1511 retval = -EIO;
1512 goto done;
1513 }
Tobias Lorenz33632d42008-09-24 19:30:26 -03001514 if (tuner->index != 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001515 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001516
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001517 /* mono/stereo selector */
1518 switch (tuner->audmode) {
1519 case V4L2_TUNER_MODE_MONO:
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001520 radio->registers[POWERCFG] |= POWERCFG_MONO; /* force mono */
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001521 break;
1522 case V4L2_TUNER_MODE_STEREO:
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001523 radio->registers[POWERCFG] &= ~POWERCFG_MONO; /* try stereo */
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001524 break;
1525 default:
1526 goto done;
1527 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001528
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001529 retval = si470x_set_register(radio, POWERCFG);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001530
1531done:
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001532 if (retval < 0)
1533 printk(KERN_WARNING DRIVER_NAME
1534 ": set tuner failed with %d\n", retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001535 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001536}
1537
1538
1539/*
1540 * si470x_vidioc_g_frequency - get tuner or modulator radio frequency
1541 */
1542static int si470x_vidioc_g_frequency(struct file *file, void *priv,
1543 struct v4l2_frequency *freq)
1544{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001545 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001546 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001547
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001548 /* safety checks */
1549 if (radio->disconnected) {
1550 retval = -EIO;
1551 goto done;
1552 }
Tobias Lorenz33632d42008-09-24 19:30:26 -03001553 if (freq->tuner != 0) {
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001554 retval = -EINVAL;
1555 goto done;
1556 }
Tobias Lorenz03dea862008-04-22 14:46:11 -03001557
Tobias Lorenz33632d42008-09-24 19:30:26 -03001558 freq->type = V4L2_TUNER_RADIO;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001559 retval = si470x_get_freq(radio, &freq->frequency);
1560
1561done:
1562 if (retval < 0)
1563 printk(KERN_WARNING DRIVER_NAME
1564 ": get frequency failed with %d\n", retval);
1565 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001566}
1567
1568
1569/*
1570 * si470x_vidioc_s_frequency - set tuner or modulator radio frequency
1571 */
1572static int si470x_vidioc_s_frequency(struct file *file, void *priv,
1573 struct v4l2_frequency *freq)
1574{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001575 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001576 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001577
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001578 /* safety checks */
1579 if (radio->disconnected) {
1580 retval = -EIO;
1581 goto done;
1582 }
Tobias Lorenz33632d42008-09-24 19:30:26 -03001583 if (freq->tuner != 0) {
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001584 retval = -EINVAL;
1585 goto done;
1586 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001587
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001588 retval = si470x_set_freq(radio, freq->frequency);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001589
1590done:
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001591 if (retval < 0)
1592 printk(KERN_WARNING DRIVER_NAME
1593 ": set frequency failed with %d\n", retval);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001594 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001595}
1596
1597
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001598/*
Tobias Lorenzbbc89952008-05-31 15:11:32 -03001599 * si470x_vidioc_s_hw_freq_seek - set hardware frequency seek
1600 */
1601static int si470x_vidioc_s_hw_freq_seek(struct file *file, void *priv,
1602 struct v4l2_hw_freq_seek *seek)
1603{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001604 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenzbbc89952008-05-31 15:11:32 -03001605 int retval = 0;
1606
1607 /* safety checks */
1608 if (radio->disconnected) {
1609 retval = -EIO;
1610 goto done;
1611 }
Tobias Lorenz33632d42008-09-24 19:30:26 -03001612 if (seek->tuner != 0) {
Tobias Lorenzbbc89952008-05-31 15:11:32 -03001613 retval = -EINVAL;
1614 goto done;
1615 }
1616
1617 retval = si470x_set_seek(radio, seek->wrap_around, seek->seek_upward);
1618
1619done:
1620 if (retval < 0)
1621 printk(KERN_WARNING DRIVER_NAME
1622 ": set hardware frequency seek failed with %d\n",
1623 retval);
1624 return retval;
1625}
1626
Tobias Lorenz374ab682008-09-24 19:17:54 -03001627
1628/*
1629 * si470x_ioctl_ops - video device ioctl operations
1630 */
Hans Verkuila3998102008-07-21 02:57:38 -03001631static const struct v4l2_ioctl_ops si470x_ioctl_ops = {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001632 .vidioc_querycap = si470x_vidioc_querycap,
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001633 .vidioc_queryctrl = si470x_vidioc_queryctrl,
1634 .vidioc_g_ctrl = si470x_vidioc_g_ctrl,
1635 .vidioc_s_ctrl = si470x_vidioc_s_ctrl,
1636 .vidioc_g_audio = si470x_vidioc_g_audio,
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001637 .vidioc_g_tuner = si470x_vidioc_g_tuner,
1638 .vidioc_s_tuner = si470x_vidioc_s_tuner,
1639 .vidioc_g_frequency = si470x_vidioc_g_frequency,
1640 .vidioc_s_frequency = si470x_vidioc_s_frequency,
Tobias Lorenzbbc89952008-05-31 15:11:32 -03001641 .vidioc_s_hw_freq_seek = si470x_vidioc_s_hw_freq_seek,
Hans Verkuila3998102008-07-21 02:57:38 -03001642};
1643
Tobias Lorenz374ab682008-09-24 19:17:54 -03001644
Hans Verkuila3998102008-07-21 02:57:38 -03001645/*
Tobias Lorenz374ab682008-09-24 19:17:54 -03001646 * si470x_viddev_template - video device interface
Hans Verkuila3998102008-07-21 02:57:38 -03001647 */
1648static struct video_device si470x_viddev_template = {
1649 .fops = &si470x_fops,
Hans Verkuila3998102008-07-21 02:57:38 -03001650 .name = DRIVER_NAME,
Hans Verkuila3998102008-07-21 02:57:38 -03001651 .release = video_device_release,
Tobias Lorenz374ab682008-09-24 19:17:54 -03001652 .ioctl_ops = &si470x_ioctl_ops,
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001653};
1654
1655
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001656
1657/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001658 * USB Interface
1659 **************************************************************************/
1660
1661/*
1662 * si470x_usb_driver_probe - probe for the device
1663 */
1664static int si470x_usb_driver_probe(struct usb_interface *intf,
1665 const struct usb_device_id *id)
1666{
1667 struct si470x_device *radio;
Tobias Lorenz721f59e2009-06-20 19:00:06 -03001668 struct usb_host_interface *iface_desc;
1669 struct usb_endpoint_descriptor *endpoint;
1670 int i, int_end_size, retval = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001671
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001672 /* private data allocation and initialization */
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001673 radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001674 if (!radio) {
1675 retval = -ENOMEM;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001676 goto err_initial;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001677 }
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001678 radio->users = 0;
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001679 radio->disconnected = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001680 radio->usbdev = interface_to_usbdev(intf);
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001681 radio->intf = intf;
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001682 mutex_init(&radio->disconnect_lock);
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001683 mutex_init(&radio->lock);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001684
Tobias Lorenz721f59e2009-06-20 19:00:06 -03001685 iface_desc = intf->cur_altsetting;
1686
1687 /* Set up interrupt endpoint information. */
1688 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1689 endpoint = &iface_desc->endpoint[i].desc;
1690 if (((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
1691 USB_DIR_IN) && ((endpoint->bmAttributes &
1692 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT))
1693 radio->int_in_endpoint = endpoint;
1694 }
1695 if (!radio->int_in_endpoint) {
1696 printk(KERN_INFO DRIVER_NAME
1697 ": could not find interrupt in endpoint\n");
1698 retval = -EIO;
1699 goto err_radio;
1700 }
1701
1702 int_end_size = le16_to_cpu(radio->int_in_endpoint->wMaxPacketSize);
1703
1704 radio->int_in_buffer = kmalloc(int_end_size, GFP_KERNEL);
1705 if (!radio->int_in_buffer) {
1706 printk(KERN_INFO DRIVER_NAME
1707 "could not allocate int_in_buffer");
1708 retval = -ENOMEM;
1709 goto err_radio;
1710 }
1711
1712 radio->int_in_urb = usb_alloc_urb(0, GFP_KERNEL);
1713 if (!radio->int_in_urb) {
1714 printk(KERN_INFO DRIVER_NAME "could not allocate int_in_urb");
1715 retval = -ENOMEM;
1716 goto err_intbuffer;
1717 }
1718
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001719 /* video device allocation and initialization */
1720 radio->videodev = video_device_alloc();
1721 if (!radio->videodev) {
1722 retval = -ENOMEM;
Tobias Lorenz721f59e2009-06-20 19:00:06 -03001723 goto err_intbuffer;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001724 }
1725 memcpy(radio->videodev, &si470x_viddev_template,
1726 sizeof(si470x_viddev_template));
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001727 video_set_drvdata(radio->videodev, radio);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001728
Tobias Lorenz10711d02009-02-12 14:56:30 -03001729 /* show some infos about the specific si470x device */
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001730 if (si470x_get_all_registers(radio) < 0) {
1731 retval = -EIO;
Alexey Klimov81d1d092009-03-31 21:01:04 -03001732 goto err_video;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001733 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001734 printk(KERN_INFO DRIVER_NAME ": DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001735 radio->registers[DEVICEID], radio->registers[CHIPID]);
1736
Tobias Lorenz10711d02009-02-12 14:56:30 -03001737 /* get software and hardware versions */
1738 if (si470x_get_scratch_page_versions(radio) < 0) {
1739 retval = -EIO;
Alexey Klimov81d1d092009-03-31 21:01:04 -03001740 goto err_video;
Tobias Lorenz10711d02009-02-12 14:56:30 -03001741 }
1742 printk(KERN_INFO DRIVER_NAME
1743 ": software version %d, hardware version %d\n",
1744 radio->software_version, radio->hardware_version);
1745
1746 /* check if device and firmware is current */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001747 if ((radio->registers[CHIPID] & CHIPID_FIRMWARE)
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001748 < RADIO_SW_VERSION_CURRENT) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001749 printk(KERN_WARNING DRIVER_NAME
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001750 ": This driver is known to work with "
1751 "firmware version %hu,\n", RADIO_SW_VERSION_CURRENT);
1752 printk(KERN_WARNING DRIVER_NAME
1753 ": but the device has firmware version %hu.\n",
1754 radio->registers[CHIPID] & CHIPID_FIRMWARE);
1755 printk(KERN_WARNING DRIVER_NAME
1756 ": If you have some trouble using this driver,\n");
1757 printk(KERN_WARNING DRIVER_NAME
1758 ": please report to V4L ML at "
Hans Verkuil15965f02009-03-14 15:06:08 -03001759 "linux-media@vger.kernel.org\n");
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001760 }
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001761
1762 /* set initial frequency */
1763 si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
1764
Tobias Lorenz2f94fc42009-02-12 14:56:19 -03001765 /* set led to connect state */
1766 si470x_set_led_state(radio, BLINK_GREEN_LED);
1767
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001768 /* rds buffer allocation */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001769 radio->buf_size = rds_buf * 3;
1770 radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001771 if (!radio->buffer) {
1772 retval = -EIO;
Alexey Klimov81d1d092009-03-31 21:01:04 -03001773 goto err_video;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001774 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001775
1776 /* rds buffer configuration */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001777 radio->wr_index = 0;
1778 radio->rd_index = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001779 init_waitqueue_head(&radio->read_queue);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001780
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001781 /* register video device */
Hans Verkuilcba99ae2008-09-03 17:11:58 -03001782 retval = video_register_device(radio->videodev, VFL_TYPE_RADIO, radio_nr);
1783 if (retval) {
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001784 printk(KERN_WARNING DRIVER_NAME
1785 ": Could not register video device\n");
1786 goto err_all;
1787 }
1788 usb_set_intfdata(intf, radio);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001789
1790 return 0;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001791err_all:
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001792 kfree(radio->buffer);
Alexey Klimov81d1d092009-03-31 21:01:04 -03001793err_video:
1794 video_device_release(radio->videodev);
Tobias Lorenz721f59e2009-06-20 19:00:06 -03001795err_intbuffer:
1796 kfree(radio->int_in_buffer);
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001797err_radio:
1798 kfree(radio);
1799err_initial:
1800 return retval;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001801}
1802
1803
1804/*
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001805 * si470x_usb_driver_suspend - suspend the device
1806 */
1807static int si470x_usb_driver_suspend(struct usb_interface *intf,
1808 pm_message_t message)
1809{
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001810 printk(KERN_INFO DRIVER_NAME ": suspending now...\n");
1811
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001812 return 0;
1813}
1814
1815
1816/*
1817 * si470x_usb_driver_resume - resume the device
1818 */
1819static int si470x_usb_driver_resume(struct usb_interface *intf)
1820{
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001821 printk(KERN_INFO DRIVER_NAME ": resuming now...\n");
1822
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001823 return 0;
1824}
1825
1826
1827/*
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001828 * si470x_usb_driver_disconnect - disconnect the device
1829 */
1830static void si470x_usb_driver_disconnect(struct usb_interface *intf)
1831{
1832 struct si470x_device *radio = usb_get_intfdata(intf);
1833
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001834 mutex_lock(&radio->disconnect_lock);
1835 radio->disconnected = 1;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001836 usb_set_intfdata(intf, NULL);
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001837 if (radio->users == 0) {
Tobias Lorenz2f94fc42009-02-12 14:56:19 -03001838 /* set led to disconnect state */
1839 si470x_set_led_state(radio, BLINK_ORANGE_LED);
1840
Tobias Lorenz721f59e2009-06-20 19:00:06 -03001841 /* Free data structures. */
1842 usb_free_urb(radio->int_in_urb);
1843
1844 kfree(radio->int_in_buffer);
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001845 video_unregister_device(radio->videodev);
1846 kfree(radio->buffer);
1847 kfree(radio);
1848 }
1849 mutex_unlock(&radio->disconnect_lock);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001850}
1851
1852
1853/*
1854 * si470x_usb_driver - usb driver interface
1855 */
1856static struct usb_driver si470x_usb_driver = {
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001857 .name = DRIVER_NAME,
1858 .probe = si470x_usb_driver_probe,
1859 .disconnect = si470x_usb_driver_disconnect,
1860 .suspend = si470x_usb_driver_suspend,
1861 .resume = si470x_usb_driver_resume,
1862 .id_table = si470x_usb_driver_id_table,
1863 .supports_autosuspend = 1,
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001864};
1865
1866
1867
1868/**************************************************************************
1869 * Module Interface
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001870 **************************************************************************/
1871
1872/*
1873 * si470x_module_init - module init
1874 */
1875static int __init si470x_module_init(void)
1876{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001877 printk(KERN_INFO DRIVER_DESC ", Version " DRIVER_VERSION "\n");
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001878 return usb_register(&si470x_usb_driver);
1879}
1880
1881
1882/*
1883 * si470x_module_exit - module exit
1884 */
1885static void __exit si470x_module_exit(void)
1886{
1887 usb_deregister(&si470x_usb_driver);
1888}
1889
1890
1891module_init(si470x_module_init);
1892module_exit(si470x_module_exit);
1893
1894MODULE_LICENSE("GPL");
1895MODULE_AUTHOR(DRIVER_AUTHOR);
1896MODULE_DESCRIPTION(DRIVER_DESC);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001897MODULE_VERSION(DRIVER_VERSION);