blob: dfaaa467c043a442aa90a22d249498221dbdd162 [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 Lorenz7f53b352009-02-12 14:55:56 -0300484 * General Driver Functions - REGISTER_REPORTs
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/*
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300570 * si470x_set_chan - set the channel
571 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300572static int si470x_set_chan(struct si470x_device *radio, unsigned short chan)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300573{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300574 int retval;
575 unsigned long timeout;
576 bool timed_out = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300577
578 /* start tuning */
579 radio->registers[CHANNEL] &= ~CHANNEL_CHAN;
580 radio->registers[CHANNEL] |= CHANNEL_TUNE | chan;
581 retval = si470x_set_register(radio, CHANNEL);
582 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300583 goto done;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300584
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300585 /* wait till tune operation has completed */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300586 timeout = jiffies + msecs_to_jiffies(tune_timeout);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300587 do {
588 retval = si470x_get_register(radio, STATUSRSSI);
589 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300590 goto stop;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300591 timed_out = time_after(jiffies, timeout);
592 } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
593 (!timed_out));
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300594 if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
595 printk(KERN_WARNING DRIVER_NAME ": tune does not complete\n");
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300596 if (timed_out)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300597 printk(KERN_WARNING DRIVER_NAME
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300598 ": tune timed out after %u ms\n", tune_timeout);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300599
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300600stop:
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300601 /* stop tuning */
602 radio->registers[CHANNEL] &= ~CHANNEL_TUNE;
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300603 retval = si470x_set_register(radio, CHANNEL);
604
605done:
606 return retval;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300607}
608
609
610/*
611 * si470x_get_freq - get the frequency
612 */
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300613static int si470x_get_freq(struct si470x_device *radio, unsigned int *freq)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300614{
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300615 unsigned int spacing, band_bottom;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300616 unsigned short chan;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300617 int retval;
618
619 /* Spacing (kHz) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300620 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300621 /* 0: 200 kHz (USA, Australia) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300622 case 0:
623 spacing = 0.200 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300624 /* 1: 100 kHz (Europe, Japan) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300625 case 1:
626 spacing = 0.100 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300627 /* 2: 50 kHz */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300628 default:
629 spacing = 0.050 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300630 };
631
632 /* Bottom of Band (MHz) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300633 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300634 /* 0: 87.5 - 108 MHz (USA, Europe) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300635 case 0:
636 band_bottom = 87.5 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300637 /* 1: 76 - 108 MHz (Japan wide band) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300638 default:
639 band_bottom = 76 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300640 /* 2: 76 - 90 MHz (Japan) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300641 case 2:
642 band_bottom = 76 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300643 };
644
645 /* read channel */
646 retval = si470x_get_register(radio, READCHAN);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300647 chan = radio->registers[READCHAN] & READCHAN_READCHAN;
648
649 /* Frequency (MHz) = Spacing (kHz) x Channel + Bottom of Band (MHz) */
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300650 *freq = chan * spacing + band_bottom;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300651
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300652 return retval;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300653}
654
655
656/*
657 * si470x_set_freq - set the frequency
658 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300659static int si470x_set_freq(struct si470x_device *radio, unsigned int freq)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300660{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300661 unsigned int spacing, band_bottom;
662 unsigned short chan;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300663
664 /* Spacing (kHz) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300665 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300666 /* 0: 200 kHz (USA, Australia) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300667 case 0:
668 spacing = 0.200 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300669 /* 1: 100 kHz (Europe, Japan) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300670 case 1:
671 spacing = 0.100 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300672 /* 2: 50 kHz */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300673 default:
674 spacing = 0.050 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300675 };
676
677 /* Bottom of Band (MHz) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300678 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300679 /* 0: 87.5 - 108 MHz (USA, Europe) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300680 case 0:
681 band_bottom = 87.5 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300682 /* 1: 76 - 108 MHz (Japan wide band) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300683 default:
684 band_bottom = 76 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300685 /* 2: 76 - 90 MHz (Japan) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300686 case 2:
687 band_bottom = 76 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300688 };
689
690 /* Chan = [ Freq (Mhz) - Bottom of Band (MHz) ] / Spacing (kHz) */
691 chan = (freq - band_bottom) / spacing;
692
693 return si470x_set_chan(radio, chan);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300694}
695
696
697/*
Tobias Lorenzbbc89952008-05-31 15:11:32 -0300698 * si470x_set_seek - set seek
699 */
700static int si470x_set_seek(struct si470x_device *radio,
701 unsigned int wrap_around, unsigned int seek_upward)
702{
703 int retval = 0;
704 unsigned long timeout;
705 bool timed_out = 0;
706
707 /* start seeking */
708 radio->registers[POWERCFG] |= POWERCFG_SEEK;
709 if (wrap_around == 1)
710 radio->registers[POWERCFG] &= ~POWERCFG_SKMODE;
711 else
712 radio->registers[POWERCFG] |= POWERCFG_SKMODE;
713 if (seek_upward == 1)
714 radio->registers[POWERCFG] |= POWERCFG_SEEKUP;
715 else
716 radio->registers[POWERCFG] &= ~POWERCFG_SEEKUP;
717 retval = si470x_set_register(radio, POWERCFG);
718 if (retval < 0)
719 goto done;
720
721 /* wait till seek operation has completed */
722 timeout = jiffies + msecs_to_jiffies(seek_timeout);
723 do {
724 retval = si470x_get_register(radio, STATUSRSSI);
725 if (retval < 0)
726 goto stop;
727 timed_out = time_after(jiffies, timeout);
728 } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
729 (!timed_out));
730 if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
731 printk(KERN_WARNING DRIVER_NAME ": seek does not complete\n");
732 if (radio->registers[STATUSRSSI] & STATUSRSSI_SF)
733 printk(KERN_WARNING DRIVER_NAME
734 ": seek failed / band limit reached\n");
735 if (timed_out)
736 printk(KERN_WARNING DRIVER_NAME
737 ": seek timed out after %u ms\n", seek_timeout);
738
739stop:
740 /* stop seeking */
741 radio->registers[POWERCFG] &= ~POWERCFG_SEEK;
742 retval = si470x_set_register(radio, POWERCFG);
743
744done:
745 /* try again, if timed out */
746 if ((retval == 0) && timed_out)
747 retval = -EAGAIN;
748
749 return retval;
750}
751
752
753/*
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300754 * si470x_start - switch on radio
755 */
756static int si470x_start(struct si470x_device *radio)
757{
758 int retval;
759
760 /* powercfg */
761 radio->registers[POWERCFG] =
762 POWERCFG_DMUTE | POWERCFG_ENABLE | POWERCFG_RDSM;
763 retval = si470x_set_register(radio, POWERCFG);
764 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300765 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300766
767 /* sysconfig 1 */
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300768 radio->registers[SYSCONFIG1] = SYSCONFIG1_DE;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300769 retval = si470x_set_register(radio, SYSCONFIG1);
770 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300771 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300772
773 /* sysconfig 2 */
774 radio->registers[SYSCONFIG2] =
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300775 (0x3f << 8) | /* SEEKTH */
776 ((band << 6) & SYSCONFIG2_BAND) | /* BAND */
777 ((space << 4) & SYSCONFIG2_SPACE) | /* SPACE */
778 15; /* VOLUME (max) */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300779 retval = si470x_set_register(radio, SYSCONFIG2);
780 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300781 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300782
783 /* reset last channel */
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300784 retval = si470x_set_chan(radio,
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300785 radio->registers[CHANNEL] & CHANNEL_CHAN);
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300786
787done:
788 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300789}
790
791
792/*
793 * si470x_stop - switch off radio
794 */
795static int si470x_stop(struct si470x_device *radio)
796{
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300797 int retval;
798
799 /* sysconfig 1 */
800 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
801 retval = si470x_set_register(radio, SYSCONFIG1);
802 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300803 goto done;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300804
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300805 /* powercfg */
806 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
807 /* POWERCFG_ENABLE has to automatically go low */
808 radio->registers[POWERCFG] |= POWERCFG_ENABLE | POWERCFG_DISABLE;
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300809 retval = si470x_set_register(radio, POWERCFG);
810
811done:
812 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300813}
814
815
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300816/*
817 * si470x_rds_on - switch on rds reception
818 */
819static int si470x_rds_on(struct si470x_device *radio)
820{
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300821 int retval;
822
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300823 /* sysconfig 1 */
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300824 mutex_lock(&radio->lock);
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300825 radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDS;
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300826 retval = si470x_set_register(radio, SYSCONFIG1);
827 if (retval < 0)
828 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
829 mutex_unlock(&radio->lock);
830
831 return retval;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300832}
833
834
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300835
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300836/**************************************************************************
Tobias Lorenz2f94fc42009-02-12 14:56:19 -0300837 * General Driver Functions - LED_REPORT
838 **************************************************************************/
839
840/*
841 * si470x_set_led_state - sets the led state
842 */
843static int si470x_set_led_state(struct si470x_device *radio,
844 unsigned char led_state)
845{
846 unsigned char buf[LED_REPORT_SIZE];
847 int retval;
848
849 buf[0] = LED_REPORT;
850 buf[1] = LED_COMMAND;
851 buf[2] = led_state;
852
853 retval = si470x_set_report(radio, (void *) &buf, sizeof(buf));
854
855 return (retval < 0) ? -EINVAL : 0;
856}
857
858
859
860/**************************************************************************
Tobias Lorenz7f53b352009-02-12 14:55:56 -0300861 * General Driver Functions - ENTIRE_REPORT
862 **************************************************************************/
863
864/*
865 * si470x_get_all_registers - read entire registers
866 */
867static int si470x_get_all_registers(struct si470x_device *radio)
868{
869 unsigned char buf[ENTIRE_REPORT_SIZE];
870 int retval;
871 unsigned char regnr;
872
873 buf[0] = ENTIRE_REPORT;
874
875 retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
876
877 if (retval >= 0)
878 for (regnr = 0; regnr < RADIO_REGISTER_NUM; regnr++)
879 radio->registers[regnr] = get_unaligned_be16(
880 &buf[regnr * RADIO_REGISTER_SIZE + 1]);
881
882 return (retval < 0) ? -EINVAL : 0;
883}
884
885
886
887/**************************************************************************
888 * General Driver Functions - RDS_REPORT
889 **************************************************************************/
890
891/*
892 * si470x_get_rds_registers - read rds registers
893 */
894static int si470x_get_rds_registers(struct si470x_device *radio)
895{
896 unsigned char buf[RDS_REPORT_SIZE];
897 int retval;
898 int size;
899 unsigned char regnr;
900
901 buf[0] = RDS_REPORT;
902
903 retval = usb_interrupt_msg(radio->usbdev,
904 usb_rcvintpipe(radio->usbdev, 1),
905 (void *) &buf, sizeof(buf), &size, usb_timeout);
906 if (size != sizeof(buf))
907 printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: "
908 "return size differs: %d != %zu\n", size, sizeof(buf));
909 if (retval < 0)
910 printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: "
911 "usb_interrupt_msg returned %d\n", retval);
912
913 if (retval >= 0)
914 for (regnr = 0; regnr < RDS_REGISTER_NUM; regnr++)
915 radio->registers[STATUSRSSI + regnr] =
916 get_unaligned_be16(
917 &buf[regnr * RADIO_REGISTER_SIZE + 1]);
918
919 return (retval < 0) ? -EINVAL : 0;
920}
921
922
923
924/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300925 * RDS Driver Functions
926 **************************************************************************/
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300927
928/*
929 * si470x_rds - rds processing function
930 */
931static void si470x_rds(struct si470x_device *radio)
932{
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300933 unsigned char blocknum;
934 unsigned short bler; /* rds block errors */
935 unsigned short rds;
936 unsigned char tmpbuf[3];
937
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300938 /* get rds blocks */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300939 if (si470x_get_rds_registers(radio) < 0)
940 return;
941 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0) {
942 /* No RDS group ready */
943 return;
944 }
945 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSS) == 0) {
946 /* RDS decoder not synchronized */
947 return;
948 }
949
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300950 /* copy all four RDS blocks to internal buffer */
951 mutex_lock(&radio->lock);
952 for (blocknum = 0; blocknum < 4; blocknum++) {
953 switch (blocknum) {
954 default:
955 bler = (radio->registers[STATUSRSSI] &
956 STATUSRSSI_BLERA) >> 9;
957 rds = radio->registers[RDSA];
958 break;
959 case 1:
960 bler = (radio->registers[READCHAN] &
961 READCHAN_BLERB) >> 14;
962 rds = radio->registers[RDSB];
963 break;
964 case 2:
965 bler = (radio->registers[READCHAN] &
966 READCHAN_BLERC) >> 12;
967 rds = radio->registers[RDSC];
968 break;
969 case 3:
970 bler = (radio->registers[READCHAN] &
971 READCHAN_BLERD) >> 10;
972 rds = radio->registers[RDSD];
973 break;
974 };
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300975
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300976 /* Fill the V4L2 RDS buffer */
Harvey Harrison5ad6b812008-04-15 22:22:11 -0300977 put_unaligned_le16(rds, &tmpbuf);
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300978 tmpbuf[2] = blocknum; /* offset name */
979 tmpbuf[2] |= blocknum << 3; /* received offset */
980 if (bler > max_rds_errors)
981 tmpbuf[2] |= 0x80; /* uncorrectable errors */
982 else if (bler > 0)
983 tmpbuf[2] |= 0x40; /* corrected error(s) */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300984
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300985 /* copy RDS block to internal buffer */
986 memcpy(&radio->buffer[radio->wr_index], &tmpbuf, 3);
987 radio->wr_index += 3;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300988
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300989 /* wrap write pointer */
990 if (radio->wr_index >= radio->buf_size)
991 radio->wr_index = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300992
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300993 /* check for overflow */
994 if (radio->wr_index == radio->rd_index) {
995 /* increment and wrap read pointer */
996 radio->rd_index += 3;
997 if (radio->rd_index >= radio->buf_size)
998 radio->rd_index = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300999 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001000 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001001 mutex_unlock(&radio->lock);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001002
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001003 /* wake up read queue */
1004 if (radio->wr_index != radio->rd_index)
1005 wake_up_interruptible(&radio->read_queue);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001006}
1007
1008
1009/*
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001010 * si470x_work - rds work function
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001011 */
1012static void si470x_work(struct work_struct *work)
1013{
1014 struct si470x_device *radio = container_of(work, struct si470x_device,
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001015 work.work);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001016
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001017 /* safety checks */
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001018 if (radio->disconnected)
1019 return;
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001020 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001021 return;
1022
1023 si470x_rds(radio);
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001024 schedule_delayed_work(&radio->work, msecs_to_jiffies(rds_poll_time));
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001025}
1026
1027
1028
1029/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001030 * File Operations Interface
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001031 **************************************************************************/
1032
1033/*
1034 * si470x_fops_read - read RDS data
1035 */
1036static ssize_t si470x_fops_read(struct file *file, char __user *buf,
1037 size_t count, loff_t *ppos)
1038{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001039 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001040 int retval = 0;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001041 unsigned int block_count = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001042
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001043 /* switch on rds reception */
1044 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
1045 si470x_rds_on(radio);
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001046 schedule_delayed_work(&radio->work,
1047 msecs_to_jiffies(rds_poll_time));
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001048 }
1049
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001050 /* block if no new data available */
1051 while (radio->wr_index == radio->rd_index) {
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001052 if (file->f_flags & O_NONBLOCK) {
1053 retval = -EWOULDBLOCK;
1054 goto done;
1055 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001056 if (wait_event_interruptible(radio->read_queue,
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001057 radio->wr_index != radio->rd_index) < 0) {
1058 retval = -EINTR;
1059 goto done;
1060 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001061 }
1062
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001063 /* calculate block count from byte count */
1064 count /= 3;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001065
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001066 /* copy RDS block out of internal buffer and to user buffer */
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001067 mutex_lock(&radio->lock);
1068 while (block_count < count) {
1069 if (radio->rd_index == radio->wr_index)
1070 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001071
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001072 /* always transfer rds complete blocks */
1073 if (copy_to_user(buf, &radio->buffer[radio->rd_index], 3))
1074 /* retval = -EFAULT; */
1075 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001076
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001077 /* increment and wrap read pointer */
1078 radio->rd_index += 3;
1079 if (radio->rd_index >= radio->buf_size)
1080 radio->rd_index = 0;
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001081
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001082 /* increment counters */
1083 block_count++;
1084 buf += 3;
1085 retval += 3;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001086 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001087 mutex_unlock(&radio->lock);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001088
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001089done:
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001090 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001091}
1092
1093
1094/*
1095 * si470x_fops_poll - poll RDS data
1096 */
1097static unsigned int si470x_fops_poll(struct file *file,
1098 struct poll_table_struct *pts)
1099{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001100 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001101 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001102
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001103 /* switch on rds reception */
1104 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
1105 si470x_rds_on(radio);
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001106 schedule_delayed_work(&radio->work,
1107 msecs_to_jiffies(rds_poll_time));
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001108 }
1109
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001110 poll_wait(file, &radio->read_queue, pts);
1111
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001112 if (radio->rd_index != radio->wr_index)
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001113 retval = POLLIN | POLLRDNORM;
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001114
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001115 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001116}
1117
1118
1119/*
1120 * si470x_fops_open - file open
1121 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001122static int si470x_fops_open(struct file *file)
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001123{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001124 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001125 int retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001126
Hans Verkuild56dc612008-07-30 08:43:36 -03001127 lock_kernel();
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001128 radio->users++;
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001129
1130 retval = usb_autopm_get_interface(radio->intf);
1131 if (retval < 0) {
1132 radio->users--;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001133 retval = -EIO;
1134 goto done;
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001135 }
1136
1137 if (radio->users == 1) {
Tobias Lorenz7f53b352009-02-12 14:55:56 -03001138 /* start radio */
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001139 retval = si470x_start(radio);
1140 if (retval < 0)
1141 usb_autopm_put_interface(radio->intf);
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001142 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001143
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001144done:
Hans Verkuild56dc612008-07-30 08:43:36 -03001145 unlock_kernel();
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001146 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001147}
1148
1149
1150/*
1151 * si470x_fops_release - file release
1152 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001153static int si470x_fops_release(struct file *file)
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001154{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001155 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001156 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001157
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001158 /* safety check */
1159 if (!radio) {
1160 retval = -ENODEV;
1161 goto done;
1162 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001163
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001164 mutex_lock(&radio->disconnect_lock);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001165 radio->users--;
1166 if (radio->users == 0) {
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001167 if (radio->disconnected) {
1168 video_unregister_device(radio->videodev);
1169 kfree(radio->buffer);
1170 kfree(radio);
1171 goto unlock;
1172 }
Tobias Lorenz03dea862008-04-22 14:46:11 -03001173
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001174 /* stop rds reception */
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001175 cancel_delayed_work_sync(&radio->work);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001176
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001177 /* cancel read processes */
1178 wake_up_interruptible(&radio->read_queue);
1179
Tobias Lorenz7f53b352009-02-12 14:55:56 -03001180 /* stop radio */
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001181 retval = si470x_stop(radio);
1182 usb_autopm_put_interface(radio->intf);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001183 }
1184
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001185unlock:
1186 mutex_unlock(&radio->disconnect_lock);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001187
1188done:
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001189 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001190}
1191
1192
1193/*
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001194 * si470x_fops - file operations interface
1195 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001196static const struct v4l2_file_operations si470x_fops = {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001197 .owner = THIS_MODULE,
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001198 .read = si470x_fops_read,
1199 .poll = si470x_fops_poll,
1200 .ioctl = video_ioctl2,
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001201 .open = si470x_fops_open,
1202 .release = si470x_fops_release,
1203};
1204
1205
1206
1207/**************************************************************************
1208 * Video4Linux Interface
1209 **************************************************************************/
1210
1211/*
1212 * si470x_v4l2_queryctrl - query control
1213 */
1214static struct v4l2_queryctrl si470x_v4l2_queryctrl[] = {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001215 {
1216 .id = V4L2_CID_AUDIO_VOLUME,
1217 .type = V4L2_CTRL_TYPE_INTEGER,
1218 .name = "Volume",
1219 .minimum = 0,
1220 .maximum = 15,
1221 .step = 1,
1222 .default_value = 15,
1223 },
1224 {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001225 .id = V4L2_CID_AUDIO_MUTE,
1226 .type = V4L2_CTRL_TYPE_BOOLEAN,
1227 .name = "Mute",
1228 .minimum = 0,
1229 .maximum = 1,
1230 .step = 1,
1231 .default_value = 1,
1232 },
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001233};
1234
1235
1236/*
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001237 * si470x_vidioc_querycap - query device capabilities
1238 */
1239static int si470x_vidioc_querycap(struct file *file, void *priv,
1240 struct v4l2_capability *capability)
1241{
Alexey Klimovdc025d42009-02-05 22:52:29 -03001242 struct si470x_device *radio = video_drvdata(file);
1243
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001244 strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver));
1245 strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
Alexey Klimovdc025d42009-02-05 22:52:29 -03001246 usb_make_path(radio->usbdev, capability->bus_info, sizeof(capability->bus_info));
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001247 capability->version = DRIVER_KERNEL_VERSION;
Tobias Lorenzbbc89952008-05-31 15:11:32 -03001248 capability->capabilities = V4L2_CAP_HW_FREQ_SEEK |
1249 V4L2_CAP_TUNER | V4L2_CAP_RADIO;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001250
1251 return 0;
1252}
1253
1254
1255/*
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001256 * si470x_vidioc_queryctrl - enumerate control items
1257 */
1258static int si470x_vidioc_queryctrl(struct file *file, void *priv,
1259 struct v4l2_queryctrl *qc)
1260{
Tobias Lorenzeb6e3b82008-09-24 19:25:39 -03001261 unsigned char i = 0;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001262 int retval = -EINVAL;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001263
Tobias Lorenzeb6e3b82008-09-24 19:25:39 -03001264 /* abort if qc->id is below V4L2_CID_BASE */
1265 if (qc->id < V4L2_CID_BASE)
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001266 goto done;
1267
Tobias Lorenzeb6e3b82008-09-24 19:25:39 -03001268 /* search video control */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001269 for (i = 0; i < ARRAY_SIZE(si470x_v4l2_queryctrl); i++) {
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001270 if (qc->id == si470x_v4l2_queryctrl[i].id) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001271 memcpy(qc, &(si470x_v4l2_queryctrl[i]), sizeof(*qc));
Tobias Lorenzeb6e3b82008-09-24 19:25:39 -03001272 retval = 0; /* found */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001273 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001274 }
1275 }
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001276
Tobias Lorenzeb6e3b82008-09-24 19:25:39 -03001277 /* disable unsupported base controls */
1278 /* to satisfy kradio and such apps */
1279 if ((retval == -EINVAL) && (qc->id < V4L2_CID_LASTP1)) {
1280 qc->flags = V4L2_CTRL_FLAG_DISABLED;
1281 retval = 0;
1282 }
1283
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001284done:
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001285 if (retval < 0)
1286 printk(KERN_WARNING DRIVER_NAME
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001287 ": query controls failed with %d\n", retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001288 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001289}
1290
1291
1292/*
1293 * si470x_vidioc_g_ctrl - get the value of a control
1294 */
1295static int si470x_vidioc_g_ctrl(struct file *file, void *priv,
1296 struct v4l2_control *ctrl)
1297{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001298 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001299 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001300
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001301 /* safety checks */
1302 if (radio->disconnected) {
1303 retval = -EIO;
1304 goto done;
1305 }
Tobias Lorenz03dea862008-04-22 14:46:11 -03001306
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001307 switch (ctrl->id) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001308 case V4L2_CID_AUDIO_VOLUME:
1309 ctrl->value = radio->registers[SYSCONFIG2] &
1310 SYSCONFIG2_VOLUME;
1311 break;
1312 case V4L2_CID_AUDIO_MUTE:
1313 ctrl->value = ((radio->registers[POWERCFG] &
1314 POWERCFG_DMUTE) == 0) ? 1 : 0;
1315 break;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001316 default:
1317 retval = -EINVAL;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001318 }
1319
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001320done:
1321 if (retval < 0)
1322 printk(KERN_WARNING DRIVER_NAME
1323 ": get control failed with %d\n", retval);
1324 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001325}
1326
1327
1328/*
1329 * si470x_vidioc_s_ctrl - set the value of a control
1330 */
1331static int si470x_vidioc_s_ctrl(struct file *file, void *priv,
1332 struct v4l2_control *ctrl)
1333{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001334 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001335 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001336
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001337 /* safety checks */
1338 if (radio->disconnected) {
1339 retval = -EIO;
1340 goto done;
1341 }
Tobias Lorenz03dea862008-04-22 14:46:11 -03001342
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001343 switch (ctrl->id) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001344 case V4L2_CID_AUDIO_VOLUME:
1345 radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_VOLUME;
1346 radio->registers[SYSCONFIG2] |= ctrl->value;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001347 retval = si470x_set_register(radio, SYSCONFIG2);
1348 break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001349 case V4L2_CID_AUDIO_MUTE:
1350 if (ctrl->value == 1)
1351 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
1352 else
1353 radio->registers[POWERCFG] |= POWERCFG_DMUTE;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001354 retval = si470x_set_register(radio, POWERCFG);
1355 break;
1356 default:
1357 retval = -EINVAL;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001358 }
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001359
1360done:
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001361 if (retval < 0)
1362 printk(KERN_WARNING DRIVER_NAME
1363 ": set control failed with %d\n", retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001364 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001365}
1366
1367
1368/*
1369 * si470x_vidioc_g_audio - get audio attributes
1370 */
1371static int si470x_vidioc_g_audio(struct file *file, void *priv,
1372 struct v4l2_audio *audio)
1373{
Tobias Lorenz617f5332008-09-24 19:33:09 -03001374 /* driver constants */
1375 audio->index = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001376 strcpy(audio->name, "Radio");
1377 audio->capability = V4L2_AUDCAP_STEREO;
Tobias Lorenz617f5332008-09-24 19:33:09 -03001378 audio->mode = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001379
Tobias Lorenz617f5332008-09-24 19:33:09 -03001380 return 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001381}
1382
1383
1384/*
1385 * si470x_vidioc_g_tuner - get tuner attributes
1386 */
1387static int si470x_vidioc_g_tuner(struct file *file, void *priv,
1388 struct v4l2_tuner *tuner)
1389{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001390 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001391 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001392
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001393 /* safety checks */
1394 if (radio->disconnected) {
1395 retval = -EIO;
1396 goto done;
1397 }
Tobias Lorenz33632d42008-09-24 19:30:26 -03001398 if (tuner->index != 0) {
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001399 retval = -EINVAL;
1400 goto done;
1401 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001402
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001403 retval = si470x_get_register(radio, STATUSRSSI);
1404 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001405 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001406
Tobias Lorenz33632d42008-09-24 19:30:26 -03001407 /* driver constants */
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001408 strcpy(tuner->name, "FM");
Tobias Lorenz33632d42008-09-24 19:30:26 -03001409 tuner->type = V4L2_TUNER_RADIO;
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001410 tuner->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
Tobias Lorenz33632d42008-09-24 19:30:26 -03001411
Tobias Lorenza17c0012008-09-24 19:21:50 -03001412 /* range limits */
1413 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001414 /* 0: 87.5 - 108 MHz (USA, Europe, default) */
1415 default:
1416 tuner->rangelow = 87.5 * FREQ_MUL;
1417 tuner->rangehigh = 108 * FREQ_MUL;
1418 break;
1419 /* 1: 76 - 108 MHz (Japan wide band) */
1420 case 1 :
1421 tuner->rangelow = 76 * FREQ_MUL;
1422 tuner->rangehigh = 108 * FREQ_MUL;
1423 break;
1424 /* 2: 76 - 90 MHz (Japan) */
1425 case 2 :
1426 tuner->rangelow = 76 * FREQ_MUL;
1427 tuner->rangehigh = 90 * FREQ_MUL;
1428 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001429 };
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001430
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001431 /* stereo indicator == stereo (instead of mono) */
Tobias Lorenzd807dec2009-02-12 14:56:10 -03001432 if ((radio->registers[STATUSRSSI] & STATUSRSSI_ST) == 0)
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001433 tuner->rxsubchans = V4L2_TUNER_SUB_MONO;
Tobias Lorenzd807dec2009-02-12 14:56:10 -03001434 else
1435 tuner->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001436
1437 /* mono/stereo selector */
Tobias Lorenzd807dec2009-02-12 14:56:10 -03001438 if ((radio->registers[POWERCFG] & POWERCFG_MONO) == 0)
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001439 tuner->audmode = V4L2_TUNER_MODE_STEREO;
Tobias Lorenzd807dec2009-02-12 14:56:10 -03001440 else
1441 tuner->audmode = V4L2_TUNER_MODE_MONO;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001442
1443 /* min is worst, max is best; signal:0..0xffff; rssi: 0..0xff */
Tobias Lorenzd807dec2009-02-12 14:56:10 -03001444 /* measured in units of dbµV in 1 db increments (max at ~75 dbµV) */
1445 tuner->signal = (radio->registers[STATUSRSSI] & STATUSRSSI_RSSI);
1446 /* the ideal factor is 0xffff/75 = 873,8 */
1447 tuner->signal = (tuner->signal * 873) + (8 * tuner->signal / 10);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001448
1449 /* automatic frequency control: -1: freq to low, 1 freq to high */
Tobias Lorenzace7d4b2008-05-31 15:09:07 -03001450 /* AFCRL does only indicate that freq. differs, not if too low/high */
1451 tuner->afc = (radio->registers[STATUSRSSI] & STATUSRSSI_AFCRL) ? 1 : 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001452
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001453done:
1454 if (retval < 0)
1455 printk(KERN_WARNING DRIVER_NAME
1456 ": get tuner failed with %d\n", retval);
1457 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001458}
1459
1460
1461/*
1462 * si470x_vidioc_s_tuner - set tuner attributes
1463 */
1464static int si470x_vidioc_s_tuner(struct file *file, void *priv,
1465 struct v4l2_tuner *tuner)
1466{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001467 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenz33632d42008-09-24 19:30:26 -03001468 int retval = -EINVAL;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001469
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001470 /* safety checks */
1471 if (radio->disconnected) {
1472 retval = -EIO;
1473 goto done;
1474 }
Tobias Lorenz33632d42008-09-24 19:30:26 -03001475 if (tuner->index != 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001476 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001477
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001478 /* mono/stereo selector */
1479 switch (tuner->audmode) {
1480 case V4L2_TUNER_MODE_MONO:
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001481 radio->registers[POWERCFG] |= POWERCFG_MONO; /* force mono */
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001482 break;
1483 case V4L2_TUNER_MODE_STEREO:
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001484 radio->registers[POWERCFG] &= ~POWERCFG_MONO; /* try stereo */
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001485 break;
1486 default:
1487 goto done;
1488 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001489
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001490 retval = si470x_set_register(radio, POWERCFG);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001491
1492done:
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001493 if (retval < 0)
1494 printk(KERN_WARNING DRIVER_NAME
1495 ": set tuner failed with %d\n", retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001496 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001497}
1498
1499
1500/*
1501 * si470x_vidioc_g_frequency - get tuner or modulator radio frequency
1502 */
1503static int si470x_vidioc_g_frequency(struct file *file, void *priv,
1504 struct v4l2_frequency *freq)
1505{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001506 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001507 int retval = 0;
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 (freq->tuner != 0) {
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001515 retval = -EINVAL;
1516 goto done;
1517 }
Tobias Lorenz03dea862008-04-22 14:46:11 -03001518
Tobias Lorenz33632d42008-09-24 19:30:26 -03001519 freq->type = V4L2_TUNER_RADIO;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001520 retval = si470x_get_freq(radio, &freq->frequency);
1521
1522done:
1523 if (retval < 0)
1524 printk(KERN_WARNING DRIVER_NAME
1525 ": get frequency failed with %d\n", retval);
1526 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001527}
1528
1529
1530/*
1531 * si470x_vidioc_s_frequency - set tuner or modulator radio frequency
1532 */
1533static int si470x_vidioc_s_frequency(struct file *file, void *priv,
1534 struct v4l2_frequency *freq)
1535{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001536 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001537 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001538
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001539 /* safety checks */
1540 if (radio->disconnected) {
1541 retval = -EIO;
1542 goto done;
1543 }
Tobias Lorenz33632d42008-09-24 19:30:26 -03001544 if (freq->tuner != 0) {
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001545 retval = -EINVAL;
1546 goto done;
1547 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001548
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001549 retval = si470x_set_freq(radio, freq->frequency);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001550
1551done:
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001552 if (retval < 0)
1553 printk(KERN_WARNING DRIVER_NAME
1554 ": set frequency failed with %d\n", retval);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001555 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001556}
1557
1558
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001559/*
Tobias Lorenzbbc89952008-05-31 15:11:32 -03001560 * si470x_vidioc_s_hw_freq_seek - set hardware frequency seek
1561 */
1562static int si470x_vidioc_s_hw_freq_seek(struct file *file, void *priv,
1563 struct v4l2_hw_freq_seek *seek)
1564{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001565 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenzbbc89952008-05-31 15:11:32 -03001566 int retval = 0;
1567
1568 /* safety checks */
1569 if (radio->disconnected) {
1570 retval = -EIO;
1571 goto done;
1572 }
Tobias Lorenz33632d42008-09-24 19:30:26 -03001573 if (seek->tuner != 0) {
Tobias Lorenzbbc89952008-05-31 15:11:32 -03001574 retval = -EINVAL;
1575 goto done;
1576 }
1577
1578 retval = si470x_set_seek(radio, seek->wrap_around, seek->seek_upward);
1579
1580done:
1581 if (retval < 0)
1582 printk(KERN_WARNING DRIVER_NAME
1583 ": set hardware frequency seek failed with %d\n",
1584 retval);
1585 return retval;
1586}
1587
Tobias Lorenz374ab682008-09-24 19:17:54 -03001588
1589/*
1590 * si470x_ioctl_ops - video device ioctl operations
1591 */
Hans Verkuila3998102008-07-21 02:57:38 -03001592static const struct v4l2_ioctl_ops si470x_ioctl_ops = {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001593 .vidioc_querycap = si470x_vidioc_querycap,
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001594 .vidioc_queryctrl = si470x_vidioc_queryctrl,
1595 .vidioc_g_ctrl = si470x_vidioc_g_ctrl,
1596 .vidioc_s_ctrl = si470x_vidioc_s_ctrl,
1597 .vidioc_g_audio = si470x_vidioc_g_audio,
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001598 .vidioc_g_tuner = si470x_vidioc_g_tuner,
1599 .vidioc_s_tuner = si470x_vidioc_s_tuner,
1600 .vidioc_g_frequency = si470x_vidioc_g_frequency,
1601 .vidioc_s_frequency = si470x_vidioc_s_frequency,
Tobias Lorenzbbc89952008-05-31 15:11:32 -03001602 .vidioc_s_hw_freq_seek = si470x_vidioc_s_hw_freq_seek,
Hans Verkuila3998102008-07-21 02:57:38 -03001603};
1604
Tobias Lorenz374ab682008-09-24 19:17:54 -03001605
Hans Verkuila3998102008-07-21 02:57:38 -03001606/*
Tobias Lorenz374ab682008-09-24 19:17:54 -03001607 * si470x_viddev_template - video device interface
Hans Verkuila3998102008-07-21 02:57:38 -03001608 */
1609static struct video_device si470x_viddev_template = {
1610 .fops = &si470x_fops,
Hans Verkuila3998102008-07-21 02:57:38 -03001611 .name = DRIVER_NAME,
Hans Verkuila3998102008-07-21 02:57:38 -03001612 .release = video_device_release,
Tobias Lorenz374ab682008-09-24 19:17:54 -03001613 .ioctl_ops = &si470x_ioctl_ops,
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001614};
1615
1616
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001617
1618/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001619 * USB Interface
1620 **************************************************************************/
1621
1622/*
1623 * si470x_usb_driver_probe - probe for the device
1624 */
1625static int si470x_usb_driver_probe(struct usb_interface *intf,
1626 const struct usb_device_id *id)
1627{
1628 struct si470x_device *radio;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001629 int retval = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001630
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001631 /* private data allocation and initialization */
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001632 radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001633 if (!radio) {
1634 retval = -ENOMEM;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001635 goto err_initial;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001636 }
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001637 radio->users = 0;
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001638 radio->disconnected = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001639 radio->usbdev = interface_to_usbdev(intf);
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001640 radio->intf = intf;
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001641 mutex_init(&radio->disconnect_lock);
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001642 mutex_init(&radio->lock);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001643
1644 /* video device allocation and initialization */
1645 radio->videodev = video_device_alloc();
1646 if (!radio->videodev) {
1647 retval = -ENOMEM;
1648 goto err_radio;
1649 }
1650 memcpy(radio->videodev, &si470x_viddev_template,
1651 sizeof(si470x_viddev_template));
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001652 video_set_drvdata(radio->videodev, radio);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001653
1654 /* show some infos about the specific device */
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001655 if (si470x_get_all_registers(radio) < 0) {
1656 retval = -EIO;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001657 goto err_all;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001658 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001659 printk(KERN_INFO DRIVER_NAME ": DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001660 radio->registers[DEVICEID], radio->registers[CHIPID]);
1661
1662 /* check if firmware is current */
1663 if ((radio->registers[CHIPID] & CHIPID_FIRMWARE)
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001664 < RADIO_SW_VERSION_CURRENT) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001665 printk(KERN_WARNING DRIVER_NAME
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001666 ": This driver is known to work with "
1667 "firmware version %hu,\n", RADIO_SW_VERSION_CURRENT);
1668 printk(KERN_WARNING DRIVER_NAME
1669 ": but the device has firmware version %hu.\n",
1670 radio->registers[CHIPID] & CHIPID_FIRMWARE);
1671 printk(KERN_WARNING DRIVER_NAME
1672 ": If you have some trouble using this driver,\n");
1673 printk(KERN_WARNING DRIVER_NAME
1674 ": please report to V4L ML at "
1675 "video4linux-list@redhat.com\n");
1676 }
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001677
1678 /* set initial frequency */
1679 si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
1680
Tobias Lorenz2f94fc42009-02-12 14:56:19 -03001681 /* set led to connect state */
1682 si470x_set_led_state(radio, BLINK_GREEN_LED);
1683
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001684 /* rds buffer allocation */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001685 radio->buf_size = rds_buf * 3;
1686 radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001687 if (!radio->buffer) {
1688 retval = -EIO;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001689 goto err_all;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001690 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001691
1692 /* rds buffer configuration */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001693 radio->wr_index = 0;
1694 radio->rd_index = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001695 init_waitqueue_head(&radio->read_queue);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001696
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001697 /* prepare rds work function */
1698 INIT_DELAYED_WORK(&radio->work, si470x_work);
1699
1700 /* register video device */
Hans Verkuilcba99ae2008-09-03 17:11:58 -03001701 retval = video_register_device(radio->videodev, VFL_TYPE_RADIO, radio_nr);
1702 if (retval) {
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001703 printk(KERN_WARNING DRIVER_NAME
1704 ": Could not register video device\n");
1705 goto err_all;
1706 }
1707 usb_set_intfdata(intf, radio);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001708
1709 return 0;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001710err_all:
1711 video_device_release(radio->videodev);
1712 kfree(radio->buffer);
1713err_radio:
1714 kfree(radio);
1715err_initial:
1716 return retval;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001717}
1718
1719
1720/*
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001721 * si470x_usb_driver_suspend - suspend the device
1722 */
1723static int si470x_usb_driver_suspend(struct usb_interface *intf,
1724 pm_message_t message)
1725{
1726 struct si470x_device *radio = usb_get_intfdata(intf);
1727
1728 printk(KERN_INFO DRIVER_NAME ": suspending now...\n");
1729
1730 cancel_delayed_work_sync(&radio->work);
1731
1732 return 0;
1733}
1734
1735
1736/*
1737 * si470x_usb_driver_resume - resume the device
1738 */
1739static int si470x_usb_driver_resume(struct usb_interface *intf)
1740{
1741 struct si470x_device *radio = usb_get_intfdata(intf);
1742
1743 printk(KERN_INFO DRIVER_NAME ": resuming now...\n");
1744
1745 mutex_lock(&radio->lock);
1746 if (radio->users && radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS)
1747 schedule_delayed_work(&radio->work,
1748 msecs_to_jiffies(rds_poll_time));
1749 mutex_unlock(&radio->lock);
1750
1751 return 0;
1752}
1753
1754
1755/*
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001756 * si470x_usb_driver_disconnect - disconnect the device
1757 */
1758static void si470x_usb_driver_disconnect(struct usb_interface *intf)
1759{
1760 struct si470x_device *radio = usb_get_intfdata(intf);
1761
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001762 mutex_lock(&radio->disconnect_lock);
1763 radio->disconnected = 1;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001764 cancel_delayed_work_sync(&radio->work);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001765 usb_set_intfdata(intf, NULL);
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001766 if (radio->users == 0) {
Tobias Lorenz2f94fc42009-02-12 14:56:19 -03001767 /* set led to disconnect state */
1768 si470x_set_led_state(radio, BLINK_ORANGE_LED);
1769
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001770 video_unregister_device(radio->videodev);
1771 kfree(radio->buffer);
1772 kfree(radio);
1773 }
1774 mutex_unlock(&radio->disconnect_lock);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001775}
1776
1777
1778/*
1779 * si470x_usb_driver - usb driver interface
1780 */
1781static struct usb_driver si470x_usb_driver = {
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001782 .name = DRIVER_NAME,
1783 .probe = si470x_usb_driver_probe,
1784 .disconnect = si470x_usb_driver_disconnect,
1785 .suspend = si470x_usb_driver_suspend,
1786 .resume = si470x_usb_driver_resume,
1787 .id_table = si470x_usb_driver_id_table,
1788 .supports_autosuspend = 1,
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001789};
1790
1791
1792
1793/**************************************************************************
1794 * Module Interface
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001795 **************************************************************************/
1796
1797/*
1798 * si470x_module_init - module init
1799 */
1800static int __init si470x_module_init(void)
1801{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001802 printk(KERN_INFO DRIVER_DESC ", Version " DRIVER_VERSION "\n");
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001803 return usb_register(&si470x_usb_driver);
1804}
1805
1806
1807/*
1808 * si470x_module_exit - module exit
1809 */
1810static void __exit si470x_module_exit(void)
1811{
1812 usb_deregister(&si470x_usb_driver);
1813}
1814
1815
1816module_init(si470x_module_init);
1817module_exit(si470x_module_exit);
1818
1819MODULE_LICENSE("GPL");
1820MODULE_AUTHOR(DRIVER_AUTHOR);
1821MODULE_DESCRIPTION(DRIVER_DESC);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001822MODULE_VERSION(DRIVER_VERSION);