blob: 8743a521e4afcb3fa93654d8d4b3a839016db9f1 [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 Lorenz78656ac2008-01-14 21:55:27 -0300108 *
109 * ToDo:
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300110 * - add firmware download/update support
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300111 * - RDS support: interrupt mode, instead of polling
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300112 */
113
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300114
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300115/* driver definitions */
116#define DRIVER_AUTHOR "Tobias Lorenz <tobias.lorenz@gmx.net>"
117#define DRIVER_NAME "radio-si470x"
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300118#define DRIVER_KERNEL_VERSION KERNEL_VERSION(1, 0, 8)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300119#define DRIVER_CARD "Silicon Labs Si470x FM Radio Receiver"
120#define DRIVER_DESC "USB radio driver for Si470x FM Radio Receivers"
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300121#define DRIVER_VERSION "1.0.8"
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300122
123
124/* kernel includes */
125#include <linux/kernel.h>
126#include <linux/module.h>
127#include <linux/init.h>
128#include <linux/slab.h>
129#include <linux/input.h>
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300130#include <linux/usb.h>
131#include <linux/hid.h>
132#include <linux/version.h>
Mauro Carvalho Chehab387d4472008-01-15 11:25:10 -0300133#include <linux/videodev2.h>
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300134#include <linux/mutex.h>
Mauro Carvalho Chehab387d4472008-01-15 11:25:10 -0300135#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300136#include <media/v4l2-ioctl.h>
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300137#include <media/rds.h>
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300138#include <asm/unaligned.h>
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300139
140
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300141/* USB Device ID List */
142static struct usb_device_id si470x_usb_driver_id_table[] = {
143 /* Silicon Labs USB FM Radio Reference Design */
Tobias Lorenz374ab682008-09-24 19:17:54 -0300144 { USB_DEVICE_AND_INTERFACE_INFO(0x10c4, 0x818a, USB_CLASS_HID, 0, 0) },
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300145 /* ADS/Tech FM Radio Receiver (formerly Instant FM Music) */
Tobias Lorenz374ab682008-09-24 19:17:54 -0300146 { USB_DEVICE_AND_INTERFACE_INFO(0x06e1, 0xa155, USB_CLASS_HID, 0, 0) },
Alexey Klimov69df96c2008-10-23 09:20:27 -0300147 /* KWorld USB FM Radio SnapMusic Mobile 700 (FM700) */
148 { USB_DEVICE_AND_INTERFACE_INFO(0x1b80, 0xd700, USB_CLASS_HID, 0, 0) },
Tobias Lorenz00ec8d02009-02-12 14:55:45 -0300149 /* Sanei Electric, Inc. FM USB Radio (sold as DealExtreme.com PCear) */
Mark Lord5e6de7d2008-12-03 15:26:15 -0300150 { USB_DEVICE_AND_INTERFACE_INFO(0x10c5, 0x819a, USB_CLASS_HID, 0, 0) },
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300151 /* Terminating entry */
152 { }
153};
154MODULE_DEVICE_TABLE(usb, si470x_usb_driver_id_table);
155
156
157
158/**************************************************************************
159 * Module Parameters
160 **************************************************************************/
161
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300162/* Radio Nr */
163static int radio_nr = -1;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300164module_param(radio_nr, int, 0444);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300165MODULE_PARM_DESC(radio_nr, "Radio Nr");
166
167/* Spacing (kHz) */
168/* 0: 200 kHz (USA, Australia) */
169/* 1: 100 kHz (Europe, Japan) */
170/* 2: 50 kHz */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300171static unsigned short space = 2;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300172module_param(space, ushort, 0444);
173MODULE_PARM_DESC(space, "Spacing: 0=200kHz 1=100kHz *2=50kHz*");
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300174
175/* Bottom of Band (MHz) */
176/* 0: 87.5 - 108 MHz (USA, Europe)*/
177/* 1: 76 - 108 MHz (Japan wide band) */
178/* 2: 76 - 90 MHz (Japan) */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300179static unsigned short band = 1;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300180module_param(band, ushort, 0444);
181MODULE_PARM_DESC(band, "Band: 0=87.5..108MHz *1=76..108MHz* 2=76..90MHz");
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300182
183/* De-emphasis */
184/* 0: 75 us (USA) */
185/* 1: 50 us (Europe, Australia, Japan) */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300186static unsigned short de = 1;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300187module_param(de, ushort, 0444);
188MODULE_PARM_DESC(de, "De-emphasis: 0=75us *1=50us*");
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300189
190/* USB timeout */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300191static unsigned int usb_timeout = 500;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300192module_param(usb_timeout, uint, 0644);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300193MODULE_PARM_DESC(usb_timeout, "USB timeout (ms): *500*");
194
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300195/* Tune timeout */
196static unsigned int tune_timeout = 3000;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300197module_param(tune_timeout, uint, 0644);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300198MODULE_PARM_DESC(tune_timeout, "Tune timeout: *3000*");
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300199
Tobias Lorenzbbc89952008-05-31 15:11:32 -0300200/* Seek timeout */
201static unsigned int seek_timeout = 5000;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300202module_param(seek_timeout, uint, 0644);
Tobias Lorenzbbc89952008-05-31 15:11:32 -0300203MODULE_PARM_DESC(seek_timeout, "Seek timeout: *5000*");
204
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300205/* RDS buffer blocks */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300206static unsigned int rds_buf = 100;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300207module_param(rds_buf, uint, 0444);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300208MODULE_PARM_DESC(rds_buf, "RDS buffer entries: *100*");
209
210/* RDS maximum block errors */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300211static unsigned short max_rds_errors = 1;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300212/* 0 means 0 errors requiring correction */
213/* 1 means 1-2 errors requiring correction (used by original USBRadio.exe) */
214/* 2 means 3-5 errors requiring correction */
215/* 3 means 6+ errors or errors in checkword, correction not possible */
Tobias Lorenz374ab682008-09-24 19:17:54 -0300216module_param(max_rds_errors, ushort, 0644);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300217MODULE_PARM_DESC(max_rds_errors, "RDS maximum block errors: *1*");
218
219/* RDS poll frequency */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300220static unsigned int rds_poll_time = 40;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300221/* 40 is used by the original USBRadio.exe */
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300222/* 50 is used by radio-cadet */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300223/* 75 should be okay */
224/* 80 is the usual RDS receive interval */
Tobias Lorenz374ab682008-09-24 19:17:54 -0300225module_param(rds_poll_time, uint, 0644);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300226MODULE_PARM_DESC(rds_poll_time, "RDS poll time (ms): *40*");
227
228
229
230/**************************************************************************
231 * Register Definitions
232 **************************************************************************/
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300233#define RADIO_REGISTER_SIZE 2 /* 16 register bit width */
234#define RADIO_REGISTER_NUM 16 /* DEVICEID ... RDSD */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300235#define RDS_REGISTER_NUM 6 /* STATUSRSSI ... RDSD */
236
237#define DEVICEID 0 /* Device ID */
238#define DEVICEID_PN 0xf000 /* bits 15..12: Part Number */
239#define DEVICEID_MFGID 0x0fff /* bits 11..00: Manufacturer ID */
240
241#define CHIPID 1 /* Chip ID */
242#define CHIPID_REV 0xfc00 /* bits 15..10: Chip Version */
243#define CHIPID_DEV 0x0200 /* bits 09..09: Device */
244#define CHIPID_FIRMWARE 0x01ff /* bits 08..00: Firmware Version */
245
246#define POWERCFG 2 /* Power Configuration */
247#define POWERCFG_DSMUTE 0x8000 /* bits 15..15: Softmute Disable */
248#define POWERCFG_DMUTE 0x4000 /* bits 14..14: Mute Disable */
249#define POWERCFG_MONO 0x2000 /* bits 13..13: Mono Select */
250#define POWERCFG_RDSM 0x0800 /* bits 11..11: RDS Mode (Si4701 only) */
251#define POWERCFG_SKMODE 0x0400 /* bits 10..10: Seek Mode */
252#define POWERCFG_SEEKUP 0x0200 /* bits 09..09: Seek Direction */
253#define POWERCFG_SEEK 0x0100 /* bits 08..08: Seek */
254#define POWERCFG_DISABLE 0x0040 /* bits 06..06: Powerup Disable */
255#define POWERCFG_ENABLE 0x0001 /* bits 00..00: Powerup Enable */
256
257#define CHANNEL 3 /* Channel */
258#define CHANNEL_TUNE 0x8000 /* bits 15..15: Tune */
259#define CHANNEL_CHAN 0x03ff /* bits 09..00: Channel Select */
260
261#define SYSCONFIG1 4 /* System Configuration 1 */
262#define SYSCONFIG1_RDSIEN 0x8000 /* bits 15..15: RDS Interrupt Enable (Si4701 only) */
263#define SYSCONFIG1_STCIEN 0x4000 /* bits 14..14: Seek/Tune Complete Interrupt Enable */
264#define SYSCONFIG1_RDS 0x1000 /* bits 12..12: RDS Enable (Si4701 only) */
265#define SYSCONFIG1_DE 0x0800 /* bits 11..11: De-emphasis (0=75us 1=50us) */
266#define SYSCONFIG1_AGCD 0x0400 /* bits 10..10: AGC Disable */
267#define SYSCONFIG1_BLNDADJ 0x00c0 /* bits 07..06: Stereo/Mono Blend Level Adjustment */
268#define SYSCONFIG1_GPIO3 0x0030 /* bits 05..04: General Purpose I/O 3 */
269#define SYSCONFIG1_GPIO2 0x000c /* bits 03..02: General Purpose I/O 2 */
270#define SYSCONFIG1_GPIO1 0x0003 /* bits 01..00: General Purpose I/O 1 */
271
272#define SYSCONFIG2 5 /* System Configuration 2 */
273#define SYSCONFIG2_SEEKTH 0xff00 /* bits 15..08: RSSI Seek Threshold */
274#define SYSCONFIG2_BAND 0x0080 /* bits 07..06: Band Select */
275#define SYSCONFIG2_SPACE 0x0030 /* bits 05..04: Channel Spacing */
276#define SYSCONFIG2_VOLUME 0x000f /* bits 03..00: Volume */
277
278#define SYSCONFIG3 6 /* System Configuration 3 */
279#define SYSCONFIG3_SMUTER 0xc000 /* bits 15..14: Softmute Attack/Recover Rate */
280#define SYSCONFIG3_SMUTEA 0x3000 /* bits 13..12: Softmute Attenuation */
281#define SYSCONFIG3_SKSNR 0x00f0 /* bits 07..04: Seek SNR Threshold */
282#define SYSCONFIG3_SKCNT 0x000f /* bits 03..00: Seek FM Impulse Detection Threshold */
283
284#define TEST1 7 /* Test 1 */
285#define TEST1_AHIZEN 0x4000 /* bits 14..14: Audio High-Z Enable */
286
287#define TEST2 8 /* Test 2 */
288/* TEST2 only contains reserved bits */
289
290#define BOOTCONFIG 9 /* Boot Configuration */
291/* BOOTCONFIG only contains reserved bits */
292
293#define STATUSRSSI 10 /* Status RSSI */
294#define STATUSRSSI_RDSR 0x8000 /* bits 15..15: RDS Ready (Si4701 only) */
295#define STATUSRSSI_STC 0x4000 /* bits 14..14: Seek/Tune Complete */
296#define STATUSRSSI_SF 0x2000 /* bits 13..13: Seek Fail/Band Limit */
297#define STATUSRSSI_AFCRL 0x1000 /* bits 12..12: AFC Rail */
298#define STATUSRSSI_RDSS 0x0800 /* bits 11..11: RDS Synchronized (Si4701 only) */
299#define STATUSRSSI_BLERA 0x0600 /* bits 10..09: RDS Block A Errors (Si4701 only) */
300#define STATUSRSSI_ST 0x0100 /* bits 08..08: Stereo Indicator */
301#define STATUSRSSI_RSSI 0x00ff /* bits 07..00: RSSI (Received Signal Strength Indicator) */
302
303#define READCHAN 11 /* Read Channel */
304#define READCHAN_BLERB 0xc000 /* bits 15..14: RDS Block D Errors (Si4701 only) */
305#define READCHAN_BLERC 0x3000 /* bits 13..12: RDS Block C Errors (Si4701 only) */
306#define READCHAN_BLERD 0x0c00 /* bits 11..10: RDS Block B Errors (Si4701 only) */
307#define READCHAN_READCHAN 0x03ff /* bits 09..00: Read Channel */
308
309#define RDSA 12 /* RDSA */
310#define RDSA_RDSA 0xffff /* bits 15..00: RDS Block A Data (Si4701 only) */
311
312#define RDSB 13 /* RDSB */
313#define RDSB_RDSB 0xffff /* bits 15..00: RDS Block B Data (Si4701 only) */
314
315#define RDSC 14 /* RDSC */
316#define RDSC_RDSC 0xffff /* bits 15..00: RDS Block C Data (Si4701 only) */
317
318#define RDSD 15 /* RDSD */
319#define RDSD_RDSD 0xffff /* bits 15..00: RDS Block D Data (Si4701 only) */
320
321
322
323/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300324 * USB HID Reports
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300325 **************************************************************************/
326
327/* Reports 1-16 give direct read/write access to the 16 Si470x registers */
328/* with the (REPORT_ID - 1) corresponding to the register address across USB */
329/* endpoint 0 using GET_REPORT and SET_REPORT */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300330#define REGISTER_REPORT_SIZE (RADIO_REGISTER_SIZE + 1)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300331#define REGISTER_REPORT(reg) ((reg) + 1)
332
333/* Report 17 gives direct read/write access to the entire Si470x register */
334/* map across endpoint 0 using GET_REPORT and SET_REPORT */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300335#define ENTIRE_REPORT_SIZE (RADIO_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300336#define ENTIRE_REPORT 17
337
338/* Report 18 is used to send the lowest 6 Si470x registers up the HID */
339/* interrupt endpoint 1 to Windows every 20 milliseconds for status */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300340#define RDS_REPORT_SIZE (RDS_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300341#define RDS_REPORT 18
342
343/* Report 19: LED state */
344#define LED_REPORT_SIZE 3
345#define LED_REPORT 19
346
347/* Report 19: stream */
348#define STREAM_REPORT_SIZE 3
Tobias Lorenz00ec8d02009-02-12 14:55:45 -0300349#define STREAM_REPORT 19
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300350
351/* Report 20: scratch */
352#define SCRATCH_PAGE_SIZE 63
353#define SCRATCH_REPORT_SIZE (SCRATCH_PAGE_SIZE + 1)
354#define SCRATCH_REPORT 20
355
356/* Reports 19-22: flash upgrade of the C8051F321 */
357#define WRITE_REPORT 19
358#define FLASH_REPORT 20
359#define CRC_REPORT 21
360#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 Lorenz8bf5e5c2008-01-25 04:19:48 -0300432/* buffer sizes */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300433#define COMMAND_BUFFER_SIZE 4
434#define RESPONSE_BUFFER_SIZE 2
435#define FLASH_BUFFER_SIZE 64
436#define CRC_BUFFER_SIZE 3
437
438
439
440/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300441 * General Driver Definitions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300442 **************************************************************************/
443
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300444/*
445 * si470x_device - private data
446 */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300447struct si470x_device {
448 /* reference to USB and video device */
449 struct usb_device *usbdev;
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300450 struct usb_interface *intf;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300451 struct video_device *videodev;
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 Lorenz5caf5132008-02-04 22:26:08 -0300462 struct delayed_work work;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300463 wait_queue_head_t read_queue;
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300464 struct mutex lock; /* buffer locking */
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300465 unsigned char *buffer; /* size is always multiple of three */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300466 unsigned int buf_size;
467 unsigned int rd_index;
468 unsigned int wr_index;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300469};
470
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300471
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300472/*
473 * The frequency is set in units of 62.5 Hz when using V4L2_TUNER_CAP_LOW,
474 * 62.5 kHz otherwise.
475 * The tuner is able to have a channel spacing of 50, 100 or 200 kHz.
476 * tuner->capability is therefore set to V4L2_TUNER_CAP_LOW
477 * The FREQ_MUL is then: 1 MHz / 62.5 Hz = 16000
478 */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300479#define FREQ_MUL (1000000 / 62.5)
480
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300481
482
483/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300484 * General Driver Functions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300485 **************************************************************************/
486
487/*
488 * si470x_get_report - receive a HID report
489 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300490static int si470x_get_report(struct si470x_device *radio, void *buf, int size)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300491{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300492 unsigned char *report = (unsigned char *) buf;
493 int retval;
494
495 retval = usb_control_msg(radio->usbdev,
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300496 usb_rcvctrlpipe(radio->usbdev, 0),
497 HID_REQ_GET_REPORT,
498 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300499 report[0], 2,
500 buf, size, usb_timeout);
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300501
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300502 if (retval < 0)
503 printk(KERN_WARNING DRIVER_NAME
504 ": si470x_get_report: usb_control_msg returned %d\n",
505 retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300506 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300507}
508
509
510/*
511 * si470x_set_report - send a HID report
512 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300513static int si470x_set_report(struct si470x_device *radio, void *buf, int size)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300514{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300515 unsigned char *report = (unsigned char *) buf;
516 int retval;
517
518 retval = usb_control_msg(radio->usbdev,
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300519 usb_sndctrlpipe(radio->usbdev, 0),
520 HID_REQ_SET_REPORT,
521 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300522 report[0], 2,
523 buf, size, usb_timeout);
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300524
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300525 if (retval < 0)
526 printk(KERN_WARNING DRIVER_NAME
527 ": si470x_set_report: usb_control_msg returned %d\n",
528 retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300529 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300530}
531
532
533/*
534 * si470x_get_register - read register
535 */
536static int si470x_get_register(struct si470x_device *radio, int regnr)
537{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300538 unsigned char buf[REGISTER_REPORT_SIZE];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300539 int retval;
540
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300541 buf[0] = REGISTER_REPORT(regnr);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300542
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300543 retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
544
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300545 if (retval >= 0)
Harvey Harrison5ad6b812008-04-15 22:22:11 -0300546 radio->registers[regnr] = get_unaligned_be16(&buf[1]);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300547
548 return (retval < 0) ? -EINVAL : 0;
549}
550
551
552/*
553 * si470x_set_register - write register
554 */
555static int si470x_set_register(struct si470x_device *radio, int regnr)
556{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300557 unsigned char buf[REGISTER_REPORT_SIZE];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300558 int retval;
559
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300560 buf[0] = REGISTER_REPORT(regnr);
Harvey Harrison5ad6b812008-04-15 22:22:11 -0300561 put_unaligned_be16(radio->registers[regnr], &buf[1]);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300562
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300563 retval = si470x_set_report(radio, (void *) &buf, sizeof(buf));
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300564
565 return (retval < 0) ? -EINVAL : 0;
566}
567
568
569/*
570 * si470x_get_all_registers - read entire registers
571 */
572static int si470x_get_all_registers(struct si470x_device *radio)
573{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300574 unsigned char buf[ENTIRE_REPORT_SIZE];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300575 int retval;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300576 unsigned char regnr;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300577
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300578 buf[0] = ENTIRE_REPORT;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300579
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300580 retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300581
582 if (retval >= 0)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300583 for (regnr = 0; regnr < RADIO_REGISTER_NUM; regnr++)
Harvey Harrison5ad6b812008-04-15 22:22:11 -0300584 radio->registers[regnr] = get_unaligned_be16(
585 &buf[regnr * RADIO_REGISTER_SIZE + 1]);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300586
587 return (retval < 0) ? -EINVAL : 0;
588}
589
590
591/*
592 * si470x_get_rds_registers - read rds registers
593 */
594static int si470x_get_rds_registers(struct si470x_device *radio)
595{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300596 unsigned char buf[RDS_REPORT_SIZE];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300597 int retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300598 int size;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300599 unsigned char regnr;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300600
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300601 buf[0] = RDS_REPORT;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300602
603 retval = usb_interrupt_msg(radio->usbdev,
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300604 usb_rcvintpipe(radio->usbdev, 1),
605 (void *) &buf, sizeof(buf), &size, usb_timeout);
606 if (size != sizeof(buf))
Tobias Lorenzce5829e2008-05-31 15:04:32 -0300607 printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: "
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300608 "return size differs: %d != %zu\n", size, sizeof(buf));
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300609 if (retval < 0)
610 printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: "
611 "usb_interrupt_msg returned %d\n", retval);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300612
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300613 if (retval >= 0)
614 for (regnr = 0; regnr < RDS_REGISTER_NUM; regnr++)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300615 radio->registers[STATUSRSSI + regnr] =
Tobias Lorenzce5829e2008-05-31 15:04:32 -0300616 get_unaligned_be16(
617 &buf[regnr * RADIO_REGISTER_SIZE + 1]);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300618
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300619 return (retval < 0) ? -EINVAL : 0;
620}
621
622
623/*
624 * si470x_set_chan - set the channel
625 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300626static int si470x_set_chan(struct si470x_device *radio, unsigned short chan)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300627{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300628 int retval;
629 unsigned long timeout;
630 bool timed_out = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300631
632 /* start tuning */
633 radio->registers[CHANNEL] &= ~CHANNEL_CHAN;
634 radio->registers[CHANNEL] |= CHANNEL_TUNE | chan;
635 retval = si470x_set_register(radio, CHANNEL);
636 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300637 goto done;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300638
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300639 /* wait till tune operation has completed */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300640 timeout = jiffies + msecs_to_jiffies(tune_timeout);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300641 do {
642 retval = si470x_get_register(radio, STATUSRSSI);
643 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300644 goto stop;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300645 timed_out = time_after(jiffies, timeout);
646 } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
647 (!timed_out));
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300648 if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
649 printk(KERN_WARNING DRIVER_NAME ": tune does not complete\n");
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300650 if (timed_out)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300651 printk(KERN_WARNING DRIVER_NAME
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300652 ": tune timed out after %u ms\n", tune_timeout);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300653
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300654stop:
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300655 /* stop tuning */
656 radio->registers[CHANNEL] &= ~CHANNEL_TUNE;
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300657 retval = si470x_set_register(radio, CHANNEL);
658
659done:
660 return retval;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300661}
662
663
664/*
665 * si470x_get_freq - get the frequency
666 */
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300667static int si470x_get_freq(struct si470x_device *radio, unsigned int *freq)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300668{
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300669 unsigned int spacing, band_bottom;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300670 unsigned short chan;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300671 int retval;
672
673 /* Spacing (kHz) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300674 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300675 /* 0: 200 kHz (USA, Australia) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300676 case 0:
677 spacing = 0.200 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300678 /* 1: 100 kHz (Europe, Japan) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300679 case 1:
680 spacing = 0.100 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300681 /* 2: 50 kHz */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300682 default:
683 spacing = 0.050 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300684 };
685
686 /* Bottom of Band (MHz) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300687 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300688 /* 0: 87.5 - 108 MHz (USA, Europe) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300689 case 0:
690 band_bottom = 87.5 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300691 /* 1: 76 - 108 MHz (Japan wide band) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300692 default:
693 band_bottom = 76 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300694 /* 2: 76 - 90 MHz (Japan) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300695 case 2:
696 band_bottom = 76 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300697 };
698
699 /* read channel */
700 retval = si470x_get_register(radio, READCHAN);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300701 chan = radio->registers[READCHAN] & READCHAN_READCHAN;
702
703 /* Frequency (MHz) = Spacing (kHz) x Channel + Bottom of Band (MHz) */
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300704 *freq = chan * spacing + band_bottom;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300705
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300706 return retval;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300707}
708
709
710/*
711 * si470x_set_freq - set the frequency
712 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300713static int si470x_set_freq(struct si470x_device *radio, unsigned int freq)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300714{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300715 unsigned int spacing, band_bottom;
716 unsigned short chan;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300717
718 /* Spacing (kHz) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300719 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300720 /* 0: 200 kHz (USA, Australia) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300721 case 0:
722 spacing = 0.200 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300723 /* 1: 100 kHz (Europe, Japan) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300724 case 1:
725 spacing = 0.100 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300726 /* 2: 50 kHz */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300727 default:
728 spacing = 0.050 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300729 };
730
731 /* Bottom of Band (MHz) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300732 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300733 /* 0: 87.5 - 108 MHz (USA, Europe) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300734 case 0:
735 band_bottom = 87.5 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300736 /* 1: 76 - 108 MHz (Japan wide band) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300737 default:
738 band_bottom = 76 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300739 /* 2: 76 - 90 MHz (Japan) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300740 case 2:
741 band_bottom = 76 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300742 };
743
744 /* Chan = [ Freq (Mhz) - Bottom of Band (MHz) ] / Spacing (kHz) */
745 chan = (freq - band_bottom) / spacing;
746
747 return si470x_set_chan(radio, chan);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300748}
749
750
751/*
Tobias Lorenzbbc89952008-05-31 15:11:32 -0300752 * si470x_set_seek - set seek
753 */
754static int si470x_set_seek(struct si470x_device *radio,
755 unsigned int wrap_around, unsigned int seek_upward)
756{
757 int retval = 0;
758 unsigned long timeout;
759 bool timed_out = 0;
760
761 /* start seeking */
762 radio->registers[POWERCFG] |= POWERCFG_SEEK;
763 if (wrap_around == 1)
764 radio->registers[POWERCFG] &= ~POWERCFG_SKMODE;
765 else
766 radio->registers[POWERCFG] |= POWERCFG_SKMODE;
767 if (seek_upward == 1)
768 radio->registers[POWERCFG] |= POWERCFG_SEEKUP;
769 else
770 radio->registers[POWERCFG] &= ~POWERCFG_SEEKUP;
771 retval = si470x_set_register(radio, POWERCFG);
772 if (retval < 0)
773 goto done;
774
775 /* wait till seek operation has completed */
776 timeout = jiffies + msecs_to_jiffies(seek_timeout);
777 do {
778 retval = si470x_get_register(radio, STATUSRSSI);
779 if (retval < 0)
780 goto stop;
781 timed_out = time_after(jiffies, timeout);
782 } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
783 (!timed_out));
784 if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
785 printk(KERN_WARNING DRIVER_NAME ": seek does not complete\n");
786 if (radio->registers[STATUSRSSI] & STATUSRSSI_SF)
787 printk(KERN_WARNING DRIVER_NAME
788 ": seek failed / band limit reached\n");
789 if (timed_out)
790 printk(KERN_WARNING DRIVER_NAME
791 ": seek timed out after %u ms\n", seek_timeout);
792
793stop:
794 /* stop seeking */
795 radio->registers[POWERCFG] &= ~POWERCFG_SEEK;
796 retval = si470x_set_register(radio, POWERCFG);
797
798done:
799 /* try again, if timed out */
800 if ((retval == 0) && timed_out)
801 retval = -EAGAIN;
802
803 return retval;
804}
805
806
807/*
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300808 * si470x_start - switch on radio
809 */
810static int si470x_start(struct si470x_device *radio)
811{
812 int retval;
813
814 /* powercfg */
815 radio->registers[POWERCFG] =
816 POWERCFG_DMUTE | POWERCFG_ENABLE | POWERCFG_RDSM;
817 retval = si470x_set_register(radio, POWERCFG);
818 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300819 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300820
821 /* sysconfig 1 */
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300822 radio->registers[SYSCONFIG1] = SYSCONFIG1_DE;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300823 retval = si470x_set_register(radio, SYSCONFIG1);
824 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300825 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300826
827 /* sysconfig 2 */
828 radio->registers[SYSCONFIG2] =
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300829 (0x3f << 8) | /* SEEKTH */
830 ((band << 6) & SYSCONFIG2_BAND) | /* BAND */
831 ((space << 4) & SYSCONFIG2_SPACE) | /* SPACE */
832 15; /* VOLUME (max) */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300833 retval = si470x_set_register(radio, SYSCONFIG2);
834 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300835 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300836
837 /* reset last channel */
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300838 retval = si470x_set_chan(radio,
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300839 radio->registers[CHANNEL] & CHANNEL_CHAN);
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300840
841done:
842 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300843}
844
845
846/*
847 * si470x_stop - switch off radio
848 */
849static int si470x_stop(struct si470x_device *radio)
850{
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300851 int retval;
852
853 /* sysconfig 1 */
854 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
855 retval = si470x_set_register(radio, SYSCONFIG1);
856 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300857 goto done;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300858
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300859 /* powercfg */
860 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
861 /* POWERCFG_ENABLE has to automatically go low */
862 radio->registers[POWERCFG] |= POWERCFG_ENABLE | POWERCFG_DISABLE;
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300863 retval = si470x_set_register(radio, POWERCFG);
864
865done:
866 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300867}
868
869
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300870/*
871 * si470x_rds_on - switch on rds reception
872 */
873static int si470x_rds_on(struct si470x_device *radio)
874{
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300875 int retval;
876
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300877 /* sysconfig 1 */
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300878 mutex_lock(&radio->lock);
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300879 radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDS;
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300880 retval = si470x_set_register(radio, SYSCONFIG1);
881 if (retval < 0)
882 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
883 mutex_unlock(&radio->lock);
884
885 return retval;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300886}
887
888
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300889
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300890/**************************************************************************
Tobias Lorenz2f94fc42009-02-12 14:56:19 -0300891 * General Driver Functions - LED_REPORT
892 **************************************************************************/
893
894/*
895 * si470x_set_led_state - sets the led state
896 */
897static int si470x_set_led_state(struct si470x_device *radio,
898 unsigned char led_state)
899{
900 unsigned char buf[LED_REPORT_SIZE];
901 int retval;
902
903 buf[0] = LED_REPORT;
904 buf[1] = LED_COMMAND;
905 buf[2] = led_state;
906
907 retval = si470x_set_report(radio, (void *) &buf, sizeof(buf));
908
909 return (retval < 0) ? -EINVAL : 0;
910}
911
912
913
914/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300915 * RDS Driver Functions
916 **************************************************************************/
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300917
918/*
919 * si470x_rds - rds processing function
920 */
921static void si470x_rds(struct si470x_device *radio)
922{
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300923 unsigned char blocknum;
924 unsigned short bler; /* rds block errors */
925 unsigned short rds;
926 unsigned char tmpbuf[3];
927
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300928 /* get rds blocks */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300929 if (si470x_get_rds_registers(radio) < 0)
930 return;
931 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0) {
932 /* No RDS group ready */
933 return;
934 }
935 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSS) == 0) {
936 /* RDS decoder not synchronized */
937 return;
938 }
939
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300940 /* copy all four RDS blocks to internal buffer */
941 mutex_lock(&radio->lock);
942 for (blocknum = 0; blocknum < 4; blocknum++) {
943 switch (blocknum) {
944 default:
945 bler = (radio->registers[STATUSRSSI] &
946 STATUSRSSI_BLERA) >> 9;
947 rds = radio->registers[RDSA];
948 break;
949 case 1:
950 bler = (radio->registers[READCHAN] &
951 READCHAN_BLERB) >> 14;
952 rds = radio->registers[RDSB];
953 break;
954 case 2:
955 bler = (radio->registers[READCHAN] &
956 READCHAN_BLERC) >> 12;
957 rds = radio->registers[RDSC];
958 break;
959 case 3:
960 bler = (radio->registers[READCHAN] &
961 READCHAN_BLERD) >> 10;
962 rds = radio->registers[RDSD];
963 break;
964 };
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300965
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300966 /* Fill the V4L2 RDS buffer */
Harvey Harrison5ad6b812008-04-15 22:22:11 -0300967 put_unaligned_le16(rds, &tmpbuf);
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300968 tmpbuf[2] = blocknum; /* offset name */
969 tmpbuf[2] |= blocknum << 3; /* received offset */
970 if (bler > max_rds_errors)
971 tmpbuf[2] |= 0x80; /* uncorrectable errors */
972 else if (bler > 0)
973 tmpbuf[2] |= 0x40; /* corrected error(s) */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300974
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300975 /* copy RDS block to internal buffer */
976 memcpy(&radio->buffer[radio->wr_index], &tmpbuf, 3);
977 radio->wr_index += 3;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300978
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300979 /* wrap write pointer */
980 if (radio->wr_index >= radio->buf_size)
981 radio->wr_index = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300982
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300983 /* check for overflow */
984 if (radio->wr_index == radio->rd_index) {
985 /* increment and wrap read pointer */
986 radio->rd_index += 3;
987 if (radio->rd_index >= radio->buf_size)
988 radio->rd_index = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300989 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300990 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300991 mutex_unlock(&radio->lock);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300992
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300993 /* wake up read queue */
994 if (radio->wr_index != radio->rd_index)
995 wake_up_interruptible(&radio->read_queue);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300996}
997
998
999/*
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001000 * si470x_work - rds work function
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001001 */
1002static void si470x_work(struct work_struct *work)
1003{
1004 struct si470x_device *radio = container_of(work, struct si470x_device,
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001005 work.work);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001006
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001007 /* safety checks */
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001008 if (radio->disconnected)
1009 return;
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001010 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001011 return;
1012
1013 si470x_rds(radio);
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001014 schedule_delayed_work(&radio->work, msecs_to_jiffies(rds_poll_time));
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001015}
1016
1017
1018
1019/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001020 * File Operations Interface
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001021 **************************************************************************/
1022
1023/*
1024 * si470x_fops_read - read RDS data
1025 */
1026static ssize_t si470x_fops_read(struct file *file, char __user *buf,
1027 size_t count, loff_t *ppos)
1028{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001029 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001030 int retval = 0;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001031 unsigned int block_count = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001032
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001033 /* switch on rds reception */
1034 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
1035 si470x_rds_on(radio);
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001036 schedule_delayed_work(&radio->work,
1037 msecs_to_jiffies(rds_poll_time));
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001038 }
1039
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001040 /* block if no new data available */
1041 while (radio->wr_index == radio->rd_index) {
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001042 if (file->f_flags & O_NONBLOCK) {
1043 retval = -EWOULDBLOCK;
1044 goto done;
1045 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001046 if (wait_event_interruptible(radio->read_queue,
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001047 radio->wr_index != radio->rd_index) < 0) {
1048 retval = -EINTR;
1049 goto done;
1050 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001051 }
1052
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001053 /* calculate block count from byte count */
1054 count /= 3;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001055
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001056 /* copy RDS block out of internal buffer and to user buffer */
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001057 mutex_lock(&radio->lock);
1058 while (block_count < count) {
1059 if (radio->rd_index == radio->wr_index)
1060 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001061
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001062 /* always transfer rds complete blocks */
1063 if (copy_to_user(buf, &radio->buffer[radio->rd_index], 3))
1064 /* retval = -EFAULT; */
1065 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001066
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001067 /* increment and wrap read pointer */
1068 radio->rd_index += 3;
1069 if (radio->rd_index >= radio->buf_size)
1070 radio->rd_index = 0;
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001071
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001072 /* increment counters */
1073 block_count++;
1074 buf += 3;
1075 retval += 3;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001076 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001077 mutex_unlock(&radio->lock);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001078
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001079done:
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001080 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001081}
1082
1083
1084/*
1085 * si470x_fops_poll - poll RDS data
1086 */
1087static unsigned int si470x_fops_poll(struct file *file,
1088 struct poll_table_struct *pts)
1089{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001090 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001091 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001092
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001093 /* switch on rds reception */
1094 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
1095 si470x_rds_on(radio);
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001096 schedule_delayed_work(&radio->work,
1097 msecs_to_jiffies(rds_poll_time));
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001098 }
1099
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001100 poll_wait(file, &radio->read_queue, pts);
1101
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001102 if (radio->rd_index != radio->wr_index)
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001103 retval = POLLIN | POLLRDNORM;
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001104
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001105 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001106}
1107
1108
1109/*
1110 * si470x_fops_open - file open
1111 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001112static int si470x_fops_open(struct file *file)
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001113{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001114 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001115 int retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001116
Hans Verkuild56dc612008-07-30 08:43:36 -03001117 lock_kernel();
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001118 radio->users++;
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001119
1120 retval = usb_autopm_get_interface(radio->intf);
1121 if (retval < 0) {
1122 radio->users--;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001123 retval = -EIO;
1124 goto done;
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001125 }
1126
1127 if (radio->users == 1) {
1128 retval = si470x_start(radio);
1129 if (retval < 0)
1130 usb_autopm_put_interface(radio->intf);
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001131 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001132
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001133done:
Hans Verkuild56dc612008-07-30 08:43:36 -03001134 unlock_kernel();
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001135 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001136}
1137
1138
1139/*
1140 * si470x_fops_release - file release
1141 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001142static int si470x_fops_release(struct file *file)
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001143{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001144 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001145 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001146
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001147 /* safety check */
1148 if (!radio) {
1149 retval = -ENODEV;
1150 goto done;
1151 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001152
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001153 mutex_lock(&radio->disconnect_lock);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001154 radio->users--;
1155 if (radio->users == 0) {
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001156 if (radio->disconnected) {
1157 video_unregister_device(radio->videodev);
1158 kfree(radio->buffer);
1159 kfree(radio);
1160 goto unlock;
1161 }
Tobias Lorenz03dea862008-04-22 14:46:11 -03001162
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001163 /* stop rds reception */
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001164 cancel_delayed_work_sync(&radio->work);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001165
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001166 /* cancel read processes */
1167 wake_up_interruptible(&radio->read_queue);
1168
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001169 retval = si470x_stop(radio);
1170 usb_autopm_put_interface(radio->intf);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001171 }
1172
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001173unlock:
1174 mutex_unlock(&radio->disconnect_lock);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001175
1176done:
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001177 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001178}
1179
1180
1181/*
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001182 * si470x_fops - file operations interface
1183 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001184static const struct v4l2_file_operations si470x_fops = {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001185 .owner = THIS_MODULE,
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001186 .read = si470x_fops_read,
1187 .poll = si470x_fops_poll,
1188 .ioctl = video_ioctl2,
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001189 .open = si470x_fops_open,
1190 .release = si470x_fops_release,
1191};
1192
1193
1194
1195/**************************************************************************
1196 * Video4Linux Interface
1197 **************************************************************************/
1198
1199/*
1200 * si470x_v4l2_queryctrl - query control
1201 */
1202static struct v4l2_queryctrl si470x_v4l2_queryctrl[] = {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001203 {
1204 .id = V4L2_CID_AUDIO_VOLUME,
1205 .type = V4L2_CTRL_TYPE_INTEGER,
1206 .name = "Volume",
1207 .minimum = 0,
1208 .maximum = 15,
1209 .step = 1,
1210 .default_value = 15,
1211 },
1212 {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001213 .id = V4L2_CID_AUDIO_MUTE,
1214 .type = V4L2_CTRL_TYPE_BOOLEAN,
1215 .name = "Mute",
1216 .minimum = 0,
1217 .maximum = 1,
1218 .step = 1,
1219 .default_value = 1,
1220 },
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001221};
1222
1223
1224/*
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001225 * si470x_vidioc_querycap - query device capabilities
1226 */
1227static int si470x_vidioc_querycap(struct file *file, void *priv,
1228 struct v4l2_capability *capability)
1229{
Alexey Klimovdc025d42009-02-05 22:52:29 -03001230 struct si470x_device *radio = video_drvdata(file);
1231
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001232 strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver));
1233 strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
Alexey Klimovdc025d42009-02-05 22:52:29 -03001234 usb_make_path(radio->usbdev, capability->bus_info, sizeof(capability->bus_info));
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001235 capability->version = DRIVER_KERNEL_VERSION;
Tobias Lorenzbbc89952008-05-31 15:11:32 -03001236 capability->capabilities = V4L2_CAP_HW_FREQ_SEEK |
1237 V4L2_CAP_TUNER | V4L2_CAP_RADIO;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001238
1239 return 0;
1240}
1241
1242
1243/*
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001244 * si470x_vidioc_queryctrl - enumerate control items
1245 */
1246static int si470x_vidioc_queryctrl(struct file *file, void *priv,
1247 struct v4l2_queryctrl *qc)
1248{
Tobias Lorenzeb6e3b82008-09-24 19:25:39 -03001249 unsigned char i = 0;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001250 int retval = -EINVAL;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001251
Tobias Lorenzeb6e3b82008-09-24 19:25:39 -03001252 /* abort if qc->id is below V4L2_CID_BASE */
1253 if (qc->id < V4L2_CID_BASE)
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001254 goto done;
1255
Tobias Lorenzeb6e3b82008-09-24 19:25:39 -03001256 /* search video control */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001257 for (i = 0; i < ARRAY_SIZE(si470x_v4l2_queryctrl); i++) {
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001258 if (qc->id == si470x_v4l2_queryctrl[i].id) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001259 memcpy(qc, &(si470x_v4l2_queryctrl[i]), sizeof(*qc));
Tobias Lorenzeb6e3b82008-09-24 19:25:39 -03001260 retval = 0; /* found */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001261 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001262 }
1263 }
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001264
Tobias Lorenzeb6e3b82008-09-24 19:25:39 -03001265 /* disable unsupported base controls */
1266 /* to satisfy kradio and such apps */
1267 if ((retval == -EINVAL) && (qc->id < V4L2_CID_LASTP1)) {
1268 qc->flags = V4L2_CTRL_FLAG_DISABLED;
1269 retval = 0;
1270 }
1271
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001272done:
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001273 if (retval < 0)
1274 printk(KERN_WARNING DRIVER_NAME
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001275 ": query controls failed with %d\n", retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001276 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001277}
1278
1279
1280/*
1281 * si470x_vidioc_g_ctrl - get the value of a control
1282 */
1283static int si470x_vidioc_g_ctrl(struct file *file, void *priv,
1284 struct v4l2_control *ctrl)
1285{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001286 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001287 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001288
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001289 /* safety checks */
1290 if (radio->disconnected) {
1291 retval = -EIO;
1292 goto done;
1293 }
Tobias Lorenz03dea862008-04-22 14:46:11 -03001294
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001295 switch (ctrl->id) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001296 case V4L2_CID_AUDIO_VOLUME:
1297 ctrl->value = radio->registers[SYSCONFIG2] &
1298 SYSCONFIG2_VOLUME;
1299 break;
1300 case V4L2_CID_AUDIO_MUTE:
1301 ctrl->value = ((radio->registers[POWERCFG] &
1302 POWERCFG_DMUTE) == 0) ? 1 : 0;
1303 break;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001304 default:
1305 retval = -EINVAL;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001306 }
1307
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001308done:
1309 if (retval < 0)
1310 printk(KERN_WARNING DRIVER_NAME
1311 ": get control failed with %d\n", retval);
1312 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001313}
1314
1315
1316/*
1317 * si470x_vidioc_s_ctrl - set the value of a control
1318 */
1319static int si470x_vidioc_s_ctrl(struct file *file, void *priv,
1320 struct v4l2_control *ctrl)
1321{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001322 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001323 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001324
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001325 /* safety checks */
1326 if (radio->disconnected) {
1327 retval = -EIO;
1328 goto done;
1329 }
Tobias Lorenz03dea862008-04-22 14:46:11 -03001330
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001331 switch (ctrl->id) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001332 case V4L2_CID_AUDIO_VOLUME:
1333 radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_VOLUME;
1334 radio->registers[SYSCONFIG2] |= ctrl->value;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001335 retval = si470x_set_register(radio, SYSCONFIG2);
1336 break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001337 case V4L2_CID_AUDIO_MUTE:
1338 if (ctrl->value == 1)
1339 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
1340 else
1341 radio->registers[POWERCFG] |= POWERCFG_DMUTE;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001342 retval = si470x_set_register(radio, POWERCFG);
1343 break;
1344 default:
1345 retval = -EINVAL;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001346 }
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001347
1348done:
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001349 if (retval < 0)
1350 printk(KERN_WARNING DRIVER_NAME
1351 ": set control failed with %d\n", retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001352 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001353}
1354
1355
1356/*
1357 * si470x_vidioc_g_audio - get audio attributes
1358 */
1359static int si470x_vidioc_g_audio(struct file *file, void *priv,
1360 struct v4l2_audio *audio)
1361{
Tobias Lorenz617f5332008-09-24 19:33:09 -03001362 /* driver constants */
1363 audio->index = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001364 strcpy(audio->name, "Radio");
1365 audio->capability = V4L2_AUDCAP_STEREO;
Tobias Lorenz617f5332008-09-24 19:33:09 -03001366 audio->mode = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001367
Tobias Lorenz617f5332008-09-24 19:33:09 -03001368 return 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001369}
1370
1371
1372/*
1373 * si470x_vidioc_g_tuner - get tuner attributes
1374 */
1375static int si470x_vidioc_g_tuner(struct file *file, void *priv,
1376 struct v4l2_tuner *tuner)
1377{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001378 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001379 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001380
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001381 /* safety checks */
1382 if (radio->disconnected) {
1383 retval = -EIO;
1384 goto done;
1385 }
Tobias Lorenz33632d42008-09-24 19:30:26 -03001386 if (tuner->index != 0) {
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001387 retval = -EINVAL;
1388 goto done;
1389 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001390
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001391 retval = si470x_get_register(radio, STATUSRSSI);
1392 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001393 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001394
Tobias Lorenz33632d42008-09-24 19:30:26 -03001395 /* driver constants */
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001396 strcpy(tuner->name, "FM");
Tobias Lorenz33632d42008-09-24 19:30:26 -03001397 tuner->type = V4L2_TUNER_RADIO;
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001398 tuner->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
Tobias Lorenz33632d42008-09-24 19:30:26 -03001399
Tobias Lorenza17c0012008-09-24 19:21:50 -03001400 /* range limits */
1401 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001402 /* 0: 87.5 - 108 MHz (USA, Europe, default) */
1403 default:
1404 tuner->rangelow = 87.5 * FREQ_MUL;
1405 tuner->rangehigh = 108 * FREQ_MUL;
1406 break;
1407 /* 1: 76 - 108 MHz (Japan wide band) */
1408 case 1 :
1409 tuner->rangelow = 76 * FREQ_MUL;
1410 tuner->rangehigh = 108 * FREQ_MUL;
1411 break;
1412 /* 2: 76 - 90 MHz (Japan) */
1413 case 2 :
1414 tuner->rangelow = 76 * FREQ_MUL;
1415 tuner->rangehigh = 90 * FREQ_MUL;
1416 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001417 };
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001418
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001419 /* stereo indicator == stereo (instead of mono) */
Tobias Lorenzd807dec2009-02-12 14:56:10 -03001420 if ((radio->registers[STATUSRSSI] & STATUSRSSI_ST) == 0)
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001421 tuner->rxsubchans = V4L2_TUNER_SUB_MONO;
Tobias Lorenzd807dec2009-02-12 14:56:10 -03001422 else
1423 tuner->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001424
1425 /* mono/stereo selector */
Tobias Lorenzd807dec2009-02-12 14:56:10 -03001426 if ((radio->registers[POWERCFG] & POWERCFG_MONO) == 0)
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001427 tuner->audmode = V4L2_TUNER_MODE_STEREO;
Tobias Lorenzd807dec2009-02-12 14:56:10 -03001428 else
1429 tuner->audmode = V4L2_TUNER_MODE_MONO;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001430
1431 /* min is worst, max is best; signal:0..0xffff; rssi: 0..0xff */
Tobias Lorenzd807dec2009-02-12 14:56:10 -03001432 /* measured in units of dbµV in 1 db increments (max at ~75 dbµV) */
1433 tuner->signal = (radio->registers[STATUSRSSI] & STATUSRSSI_RSSI);
1434 /* the ideal factor is 0xffff/75 = 873,8 */
1435 tuner->signal = (tuner->signal * 873) + (8 * tuner->signal / 10);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001436
1437 /* automatic frequency control: -1: freq to low, 1 freq to high */
Tobias Lorenzace7d4b2008-05-31 15:09:07 -03001438 /* AFCRL does only indicate that freq. differs, not if too low/high */
1439 tuner->afc = (radio->registers[STATUSRSSI] & STATUSRSSI_AFCRL) ? 1 : 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001440
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001441done:
1442 if (retval < 0)
1443 printk(KERN_WARNING DRIVER_NAME
1444 ": get tuner failed with %d\n", retval);
1445 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001446}
1447
1448
1449/*
1450 * si470x_vidioc_s_tuner - set tuner attributes
1451 */
1452static int si470x_vidioc_s_tuner(struct file *file, void *priv,
1453 struct v4l2_tuner *tuner)
1454{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001455 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenz33632d42008-09-24 19:30:26 -03001456 int retval = -EINVAL;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001457
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001458 /* safety checks */
1459 if (radio->disconnected) {
1460 retval = -EIO;
1461 goto done;
1462 }
Tobias Lorenz33632d42008-09-24 19:30:26 -03001463 if (tuner->index != 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001464 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001465
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001466 /* mono/stereo selector */
1467 switch (tuner->audmode) {
1468 case V4L2_TUNER_MODE_MONO:
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001469 radio->registers[POWERCFG] |= POWERCFG_MONO; /* force mono */
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001470 break;
1471 case V4L2_TUNER_MODE_STEREO:
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001472 radio->registers[POWERCFG] &= ~POWERCFG_MONO; /* try stereo */
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001473 break;
1474 default:
1475 goto done;
1476 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001477
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001478 retval = si470x_set_register(radio, POWERCFG);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001479
1480done:
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001481 if (retval < 0)
1482 printk(KERN_WARNING DRIVER_NAME
1483 ": set tuner failed with %d\n", retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001484 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001485}
1486
1487
1488/*
1489 * si470x_vidioc_g_frequency - get tuner or modulator radio frequency
1490 */
1491static int si470x_vidioc_g_frequency(struct file *file, void *priv,
1492 struct v4l2_frequency *freq)
1493{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001494 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001495 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001496
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001497 /* safety checks */
1498 if (radio->disconnected) {
1499 retval = -EIO;
1500 goto done;
1501 }
Tobias Lorenz33632d42008-09-24 19:30:26 -03001502 if (freq->tuner != 0) {
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001503 retval = -EINVAL;
1504 goto done;
1505 }
Tobias Lorenz03dea862008-04-22 14:46:11 -03001506
Tobias Lorenz33632d42008-09-24 19:30:26 -03001507 freq->type = V4L2_TUNER_RADIO;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001508 retval = si470x_get_freq(radio, &freq->frequency);
1509
1510done:
1511 if (retval < 0)
1512 printk(KERN_WARNING DRIVER_NAME
1513 ": get frequency failed with %d\n", retval);
1514 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001515}
1516
1517
1518/*
1519 * si470x_vidioc_s_frequency - set tuner or modulator radio frequency
1520 */
1521static int si470x_vidioc_s_frequency(struct file *file, void *priv,
1522 struct v4l2_frequency *freq)
1523{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001524 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001525 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001526
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001527 /* safety checks */
1528 if (radio->disconnected) {
1529 retval = -EIO;
1530 goto done;
1531 }
Tobias Lorenz33632d42008-09-24 19:30:26 -03001532 if (freq->tuner != 0) {
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001533 retval = -EINVAL;
1534 goto done;
1535 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001536
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001537 retval = si470x_set_freq(radio, freq->frequency);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001538
1539done:
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001540 if (retval < 0)
1541 printk(KERN_WARNING DRIVER_NAME
1542 ": set frequency failed with %d\n", retval);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001543 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001544}
1545
1546
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001547/*
Tobias Lorenzbbc89952008-05-31 15:11:32 -03001548 * si470x_vidioc_s_hw_freq_seek - set hardware frequency seek
1549 */
1550static int si470x_vidioc_s_hw_freq_seek(struct file *file, void *priv,
1551 struct v4l2_hw_freq_seek *seek)
1552{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001553 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenzbbc89952008-05-31 15:11:32 -03001554 int retval = 0;
1555
1556 /* safety checks */
1557 if (radio->disconnected) {
1558 retval = -EIO;
1559 goto done;
1560 }
Tobias Lorenz33632d42008-09-24 19:30:26 -03001561 if (seek->tuner != 0) {
Tobias Lorenzbbc89952008-05-31 15:11:32 -03001562 retval = -EINVAL;
1563 goto done;
1564 }
1565
1566 retval = si470x_set_seek(radio, seek->wrap_around, seek->seek_upward);
1567
1568done:
1569 if (retval < 0)
1570 printk(KERN_WARNING DRIVER_NAME
1571 ": set hardware frequency seek failed with %d\n",
1572 retval);
1573 return retval;
1574}
1575
Tobias Lorenz374ab682008-09-24 19:17:54 -03001576
1577/*
1578 * si470x_ioctl_ops - video device ioctl operations
1579 */
Hans Verkuila3998102008-07-21 02:57:38 -03001580static const struct v4l2_ioctl_ops si470x_ioctl_ops = {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001581 .vidioc_querycap = si470x_vidioc_querycap,
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001582 .vidioc_queryctrl = si470x_vidioc_queryctrl,
1583 .vidioc_g_ctrl = si470x_vidioc_g_ctrl,
1584 .vidioc_s_ctrl = si470x_vidioc_s_ctrl,
1585 .vidioc_g_audio = si470x_vidioc_g_audio,
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001586 .vidioc_g_tuner = si470x_vidioc_g_tuner,
1587 .vidioc_s_tuner = si470x_vidioc_s_tuner,
1588 .vidioc_g_frequency = si470x_vidioc_g_frequency,
1589 .vidioc_s_frequency = si470x_vidioc_s_frequency,
Tobias Lorenzbbc89952008-05-31 15:11:32 -03001590 .vidioc_s_hw_freq_seek = si470x_vidioc_s_hw_freq_seek,
Hans Verkuila3998102008-07-21 02:57:38 -03001591};
1592
Tobias Lorenz374ab682008-09-24 19:17:54 -03001593
Hans Verkuila3998102008-07-21 02:57:38 -03001594/*
Tobias Lorenz374ab682008-09-24 19:17:54 -03001595 * si470x_viddev_template - video device interface
Hans Verkuila3998102008-07-21 02:57:38 -03001596 */
1597static struct video_device si470x_viddev_template = {
1598 .fops = &si470x_fops,
Hans Verkuila3998102008-07-21 02:57:38 -03001599 .name = DRIVER_NAME,
Hans Verkuila3998102008-07-21 02:57:38 -03001600 .release = video_device_release,
Tobias Lorenz374ab682008-09-24 19:17:54 -03001601 .ioctl_ops = &si470x_ioctl_ops,
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001602};
1603
1604
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001605
1606/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001607 * USB Interface
1608 **************************************************************************/
1609
1610/*
1611 * si470x_usb_driver_probe - probe for the device
1612 */
1613static int si470x_usb_driver_probe(struct usb_interface *intf,
1614 const struct usb_device_id *id)
1615{
1616 struct si470x_device *radio;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001617 int retval = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001618
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001619 /* private data allocation and initialization */
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001620 radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001621 if (!radio) {
1622 retval = -ENOMEM;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001623 goto err_initial;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001624 }
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001625 radio->users = 0;
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001626 radio->disconnected = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001627 radio->usbdev = interface_to_usbdev(intf);
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001628 radio->intf = intf;
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001629 mutex_init(&radio->disconnect_lock);
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001630 mutex_init(&radio->lock);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001631
1632 /* video device allocation and initialization */
1633 radio->videodev = video_device_alloc();
1634 if (!radio->videodev) {
1635 retval = -ENOMEM;
1636 goto err_radio;
1637 }
1638 memcpy(radio->videodev, &si470x_viddev_template,
1639 sizeof(si470x_viddev_template));
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001640 video_set_drvdata(radio->videodev, radio);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001641
1642 /* show some infos about the specific device */
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001643 if (si470x_get_all_registers(radio) < 0) {
1644 retval = -EIO;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001645 goto err_all;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001646 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001647 printk(KERN_INFO DRIVER_NAME ": DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001648 radio->registers[DEVICEID], radio->registers[CHIPID]);
1649
1650 /* check if firmware is current */
1651 if ((radio->registers[CHIPID] & CHIPID_FIRMWARE)
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001652 < RADIO_SW_VERSION_CURRENT) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001653 printk(KERN_WARNING DRIVER_NAME
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001654 ": This driver is known to work with "
1655 "firmware version %hu,\n", RADIO_SW_VERSION_CURRENT);
1656 printk(KERN_WARNING DRIVER_NAME
1657 ": but the device has firmware version %hu.\n",
1658 radio->registers[CHIPID] & CHIPID_FIRMWARE);
1659 printk(KERN_WARNING DRIVER_NAME
1660 ": If you have some trouble using this driver,\n");
1661 printk(KERN_WARNING DRIVER_NAME
1662 ": please report to V4L ML at "
1663 "video4linux-list@redhat.com\n");
1664 }
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001665
1666 /* set initial frequency */
1667 si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
1668
Tobias Lorenz2f94fc42009-02-12 14:56:19 -03001669 /* set led to connect state */
1670 si470x_set_led_state(radio, BLINK_GREEN_LED);
1671
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001672 /* rds buffer allocation */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001673 radio->buf_size = rds_buf * 3;
1674 radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001675 if (!radio->buffer) {
1676 retval = -EIO;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001677 goto err_all;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001678 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001679
1680 /* rds buffer configuration */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001681 radio->wr_index = 0;
1682 radio->rd_index = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001683 init_waitqueue_head(&radio->read_queue);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001684
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001685 /* prepare rds work function */
1686 INIT_DELAYED_WORK(&radio->work, si470x_work);
1687
1688 /* register video device */
Hans Verkuilcba99ae2008-09-03 17:11:58 -03001689 retval = video_register_device(radio->videodev, VFL_TYPE_RADIO, radio_nr);
1690 if (retval) {
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001691 printk(KERN_WARNING DRIVER_NAME
1692 ": Could not register video device\n");
1693 goto err_all;
1694 }
1695 usb_set_intfdata(intf, radio);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001696
1697 return 0;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001698err_all:
1699 video_device_release(radio->videodev);
1700 kfree(radio->buffer);
1701err_radio:
1702 kfree(radio);
1703err_initial:
1704 return retval;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001705}
1706
1707
1708/*
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001709 * si470x_usb_driver_suspend - suspend the device
1710 */
1711static int si470x_usb_driver_suspend(struct usb_interface *intf,
1712 pm_message_t message)
1713{
1714 struct si470x_device *radio = usb_get_intfdata(intf);
1715
1716 printk(KERN_INFO DRIVER_NAME ": suspending now...\n");
1717
1718 cancel_delayed_work_sync(&radio->work);
1719
1720 return 0;
1721}
1722
1723
1724/*
1725 * si470x_usb_driver_resume - resume the device
1726 */
1727static int si470x_usb_driver_resume(struct usb_interface *intf)
1728{
1729 struct si470x_device *radio = usb_get_intfdata(intf);
1730
1731 printk(KERN_INFO DRIVER_NAME ": resuming now...\n");
1732
1733 mutex_lock(&radio->lock);
1734 if (radio->users && radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS)
1735 schedule_delayed_work(&radio->work,
1736 msecs_to_jiffies(rds_poll_time));
1737 mutex_unlock(&radio->lock);
1738
1739 return 0;
1740}
1741
1742
1743/*
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001744 * si470x_usb_driver_disconnect - disconnect the device
1745 */
1746static void si470x_usb_driver_disconnect(struct usb_interface *intf)
1747{
1748 struct si470x_device *radio = usb_get_intfdata(intf);
1749
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001750 mutex_lock(&radio->disconnect_lock);
1751 radio->disconnected = 1;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001752 cancel_delayed_work_sync(&radio->work);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001753 usb_set_intfdata(intf, NULL);
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001754 if (radio->users == 0) {
Tobias Lorenz2f94fc42009-02-12 14:56:19 -03001755 /* set led to disconnect state */
1756 si470x_set_led_state(radio, BLINK_ORANGE_LED);
1757
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001758 video_unregister_device(radio->videodev);
1759 kfree(radio->buffer);
1760 kfree(radio);
1761 }
1762 mutex_unlock(&radio->disconnect_lock);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001763}
1764
1765
1766/*
1767 * si470x_usb_driver - usb driver interface
1768 */
1769static struct usb_driver si470x_usb_driver = {
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001770 .name = DRIVER_NAME,
1771 .probe = si470x_usb_driver_probe,
1772 .disconnect = si470x_usb_driver_disconnect,
1773 .suspend = si470x_usb_driver_suspend,
1774 .resume = si470x_usb_driver_resume,
1775 .id_table = si470x_usb_driver_id_table,
1776 .supports_autosuspend = 1,
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001777};
1778
1779
1780
1781/**************************************************************************
1782 * Module Interface
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001783 **************************************************************************/
1784
1785/*
1786 * si470x_module_init - module init
1787 */
1788static int __init si470x_module_init(void)
1789{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001790 printk(KERN_INFO DRIVER_DESC ", Version " DRIVER_VERSION "\n");
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001791 return usb_register(&si470x_usb_driver);
1792}
1793
1794
1795/*
1796 * si470x_module_exit - module exit
1797 */
1798static void __exit si470x_module_exit(void)
1799{
1800 usb_deregister(&si470x_usb_driver);
1801}
1802
1803
1804module_init(si470x_module_init);
1805module_exit(si470x_module_exit);
1806
1807MODULE_LICENSE("GPL");
1808MODULE_AUTHOR(DRIVER_AUTHOR);
1809MODULE_DESCRIPTION(DRIVER_DESC);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001810MODULE_VERSION(DRIVER_VERSION);