blob: fd81a97a0fdbcecd3e4617ccbf864ea90cf2c545 [file] [log] [blame]
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001/*
2 * drivers/media/radio/radio-si470x.c
3 *
4 * Driver for USB radios for the Silicon Labs Si470x FM Radio Receivers:
5 * - Silicon Labs USB FM Radio Reference Design
6 * - ADS/Tech FM Radio Receiver (formerly Instant FM Music) (RDX-155-EF)
Alexey Klimov69df96c2008-10-23 09:20:27 -03007 * - KWorld USB FM Radio SnapMusic Mobile 700 (FM700)
Tobias Lorenz78656ac2008-01-14 21:55:27 -03008 *
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03009 * Copyright (c) 2008 Tobias Lorenz <tobias.lorenz@gmx.net>
Tobias Lorenz78656ac2008-01-14 21:55:27 -030010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25
26
27/*
28 * History:
29 * 2008-01-12 Tobias Lorenz <tobias.lorenz@gmx.net>
30 * Version 1.0.0
31 * - First working version
32 * 2008-01-13 Tobias Lorenz <tobias.lorenz@gmx.net>
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -030033 * Version 1.0.1
Tobias Lorenz78656ac2008-01-14 21:55:27 -030034 * - Improved error handling, every function now returns errno
35 * - Improved multi user access (start/mute/stop)
36 * - Channel doesn't get lost anymore after start/mute/stop
37 * - RDS support added (polling mode via interrupt EP 1)
38 * - marked default module parameters with *value*
39 * - switched from bit structs to bit masks
40 * - header file cleaned and integrated
41 * 2008-01-14 Tobias Lorenz <tobias.lorenz@gmx.net>
42 * Version 1.0.2
43 * - hex values are now lower case
44 * - commented USB ID for ADS/Tech moved on todo list
45 * - blacklisted si470x in hid-quirks.c
46 * - rds buffer handling functions integrated into *_work, *_read
47 * - rds_command in si470x_poll exchanged against simple retval
48 * - check for firmware version 15
49 * - code order and prototypes still remain the same
50 * - spacing and bottom of band codes remain the same
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -030051 * 2008-01-16 Tobias Lorenz <tobias.lorenz@gmx.net>
52 * Version 1.0.3
53 * - code reordered to avoid function prototypes
54 * - switch/case defaults are now more user-friendly
55 * - unified comment style
56 * - applied all checkpatch.pl v1.12 suggestions
57 * except the warning about the too long lines with bit comments
58 * - renamed FMRADIO to RADIO to cut line length (checkpatch.pl)
Tobias Lorenz2fb88402008-01-25 05:14:57 -030059 * 2008-01-22 Tobias Lorenz <tobias.lorenz@gmx.net>
60 * Version 1.0.4
61 * - avoid poss. locking when doing copy_to_user which may sleep
62 * - RDS is automatically activated on read now
63 * - code cleaned of unnecessary rds_commands
64 * - USB Vendor/Product ID for ADS/Tech FM Radio Receiver verified
65 * (thanks to Guillaume RAMOUSSE)
Tobias Lorenz0e3301e2008-01-27 14:54:07 -030066 * 2008-01-27 Tobias Lorenz <tobias.lorenz@gmx.net>
67 * Version 1.0.5
68 * - number of seek_retries changed to tune_timeout
69 * - fixed problem with incomplete tune operations by own buffers
Tobias Lorenz5caf5132008-02-04 22:26:08 -030070 * - optimization of variables and printf types
Tobias Lorenz0e3301e2008-01-27 14:54:07 -030071 * - improved error logging
Tobias Lorenz5caf5132008-02-04 22:26:08 -030072 * 2008-01-31 Tobias Lorenz <tobias.lorenz@gmx.net>
73 * Oliver Neukum <oliver@neukum.org>
74 * Version 1.0.6
75 * - fixed coverity checker warnings in *_usb_driver_disconnect
76 * - probe()/open() race by correct ordering in probe()
77 * - DMA coherency rules by separate allocation of all buffers
78 * - use of endianness macros
79 * - abuse of spinlock, replaced by mutex
80 * - racy handling of timer in disconnect,
81 * replaced by delayed_work
82 * - racy interruptible_sleep_on(),
83 * replaced with wait_event_interruptible()
84 * - handle signals in read()
Tobias Lorenz57566ad2008-02-09 16:08:24 -030085 * 2008-02-08 Tobias Lorenz <tobias.lorenz@gmx.net>
86 * Oliver Neukum <oliver@neukum.org>
87 * Version 1.0.7
88 * - usb autosuspend support
Tobias Lorenzce5829e2008-05-31 15:04:32 -030089 * - unplugging fixed
Tobias Lorenz6cc72652008-05-31 15:06:50 -030090 * 2008-05-07 Tobias Lorenz <tobias.lorenz@gmx.net>
91 * Version 1.0.8
Tobias Lorenzbbc89952008-05-31 15:11:32 -030092 * - hardware frequency seek support
Tobias Lorenzace7d4b2008-05-31 15:09:07 -030093 * - afc indication
Tobias Lorenza7c850a2008-05-31 15:07:52 -030094 * - more safety checks, let si470x_get_freq return errno
Tobias Lorenz33632d42008-09-24 19:30:26 -030095 * - vidioc behavior corrected according to v4l2 spec
Alexey Klimov69df96c2008-10-23 09:20:27 -030096 * 2008-10-20 Alexey Klimov <klimov.linux@gmail.com>
97 * - add support for KWorld USB FM Radio FM700
98 * - blacklisted KWorld radio in hid-core.c and hid-ids.h
Mark Lord5e6de7d2008-12-03 15:26:15 -030099 * 2008-12-03 Mark Lord <mlord@pobox.com>
100 * - add support for DealExtreme USB Radio
Tobias Lorenzd807dec2009-02-12 14:56:10 -0300101 * 2009-01-31 Bob Ross <pigiron@gmx.com>
102 * - correction of stereo detection/setting
103 * - correction of signal strength indicator scaling
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300104 *
105 * ToDo:
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300106 * - add firmware download/update support
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300107 * - RDS support: interrupt mode, instead of polling
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300108 * - add LED status output (check if that's not already done in firmware)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300109 */
110
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300111
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300112/* driver definitions */
113#define DRIVER_AUTHOR "Tobias Lorenz <tobias.lorenz@gmx.net>"
114#define DRIVER_NAME "radio-si470x"
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300115#define DRIVER_KERNEL_VERSION KERNEL_VERSION(1, 0, 8)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300116#define DRIVER_CARD "Silicon Labs Si470x FM Radio Receiver"
117#define DRIVER_DESC "USB radio driver for Si470x FM Radio Receivers"
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300118#define DRIVER_VERSION "1.0.8"
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300119
120
121/* kernel includes */
122#include <linux/kernel.h>
123#include <linux/module.h>
124#include <linux/init.h>
125#include <linux/slab.h>
126#include <linux/input.h>
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300127#include <linux/usb.h>
128#include <linux/hid.h>
129#include <linux/version.h>
Mauro Carvalho Chehab387d4472008-01-15 11:25:10 -0300130#include <linux/videodev2.h>
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300131#include <linux/mutex.h>
Mauro Carvalho Chehab387d4472008-01-15 11:25:10 -0300132#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300133#include <media/v4l2-ioctl.h>
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300134#include <media/rds.h>
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300135#include <asm/unaligned.h>
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300136
137
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300138/* USB Device ID List */
139static struct usb_device_id si470x_usb_driver_id_table[] = {
140 /* Silicon Labs USB FM Radio Reference Design */
Tobias Lorenz374ab682008-09-24 19:17:54 -0300141 { USB_DEVICE_AND_INTERFACE_INFO(0x10c4, 0x818a, USB_CLASS_HID, 0, 0) },
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300142 /* ADS/Tech FM Radio Receiver (formerly Instant FM Music) */
Tobias Lorenz374ab682008-09-24 19:17:54 -0300143 { USB_DEVICE_AND_INTERFACE_INFO(0x06e1, 0xa155, USB_CLASS_HID, 0, 0) },
Alexey Klimov69df96c2008-10-23 09:20:27 -0300144 /* KWorld USB FM Radio SnapMusic Mobile 700 (FM700) */
145 { USB_DEVICE_AND_INTERFACE_INFO(0x1b80, 0xd700, USB_CLASS_HID, 0, 0) },
Mark Lord5e6de7d2008-12-03 15:26:15 -0300146 /* DealExtreme USB Radio */
147 { USB_DEVICE_AND_INTERFACE_INFO(0x10c5, 0x819a, USB_CLASS_HID, 0, 0) },
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300148 /* Terminating entry */
149 { }
150};
151MODULE_DEVICE_TABLE(usb, si470x_usb_driver_id_table);
152
153
154
155/**************************************************************************
156 * Module Parameters
157 **************************************************************************/
158
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300159/* Radio Nr */
160static int radio_nr = -1;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300161module_param(radio_nr, int, 0444);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300162MODULE_PARM_DESC(radio_nr, "Radio Nr");
163
164/* Spacing (kHz) */
165/* 0: 200 kHz (USA, Australia) */
166/* 1: 100 kHz (Europe, Japan) */
167/* 2: 50 kHz */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300168static unsigned short space = 2;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300169module_param(space, ushort, 0444);
170MODULE_PARM_DESC(space, "Spacing: 0=200kHz 1=100kHz *2=50kHz*");
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300171
172/* Bottom of Band (MHz) */
173/* 0: 87.5 - 108 MHz (USA, Europe)*/
174/* 1: 76 - 108 MHz (Japan wide band) */
175/* 2: 76 - 90 MHz (Japan) */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300176static unsigned short band = 1;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300177module_param(band, ushort, 0444);
178MODULE_PARM_DESC(band, "Band: 0=87.5..108MHz *1=76..108MHz* 2=76..90MHz");
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300179
180/* De-emphasis */
181/* 0: 75 us (USA) */
182/* 1: 50 us (Europe, Australia, Japan) */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300183static unsigned short de = 1;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300184module_param(de, ushort, 0444);
185MODULE_PARM_DESC(de, "De-emphasis: 0=75us *1=50us*");
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300186
187/* USB timeout */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300188static unsigned int usb_timeout = 500;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300189module_param(usb_timeout, uint, 0644);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300190MODULE_PARM_DESC(usb_timeout, "USB timeout (ms): *500*");
191
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300192/* Tune timeout */
193static unsigned int tune_timeout = 3000;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300194module_param(tune_timeout, uint, 0644);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300195MODULE_PARM_DESC(tune_timeout, "Tune timeout: *3000*");
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300196
Tobias Lorenzbbc89952008-05-31 15:11:32 -0300197/* Seek timeout */
198static unsigned int seek_timeout = 5000;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300199module_param(seek_timeout, uint, 0644);
Tobias Lorenzbbc89952008-05-31 15:11:32 -0300200MODULE_PARM_DESC(seek_timeout, "Seek timeout: *5000*");
201
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300202/* RDS buffer blocks */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300203static unsigned int rds_buf = 100;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300204module_param(rds_buf, uint, 0444);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300205MODULE_PARM_DESC(rds_buf, "RDS buffer entries: *100*");
206
207/* RDS maximum block errors */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300208static unsigned short max_rds_errors = 1;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300209/* 0 means 0 errors requiring correction */
210/* 1 means 1-2 errors requiring correction (used by original USBRadio.exe) */
211/* 2 means 3-5 errors requiring correction */
212/* 3 means 6+ errors or errors in checkword, correction not possible */
Tobias Lorenz374ab682008-09-24 19:17:54 -0300213module_param(max_rds_errors, ushort, 0644);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300214MODULE_PARM_DESC(max_rds_errors, "RDS maximum block errors: *1*");
215
216/* RDS poll frequency */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300217static unsigned int rds_poll_time = 40;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300218/* 40 is used by the original USBRadio.exe */
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300219/* 50 is used by radio-cadet */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300220/* 75 should be okay */
221/* 80 is the usual RDS receive interval */
Tobias Lorenz374ab682008-09-24 19:17:54 -0300222module_param(rds_poll_time, uint, 0644);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300223MODULE_PARM_DESC(rds_poll_time, "RDS poll time (ms): *40*");
224
225
226
227/**************************************************************************
228 * Register Definitions
229 **************************************************************************/
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300230#define RADIO_REGISTER_SIZE 2 /* 16 register bit width */
231#define RADIO_REGISTER_NUM 16 /* DEVICEID ... RDSD */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300232#define RDS_REGISTER_NUM 6 /* STATUSRSSI ... RDSD */
233
234#define DEVICEID 0 /* Device ID */
235#define DEVICEID_PN 0xf000 /* bits 15..12: Part Number */
236#define DEVICEID_MFGID 0x0fff /* bits 11..00: Manufacturer ID */
237
238#define CHIPID 1 /* Chip ID */
239#define CHIPID_REV 0xfc00 /* bits 15..10: Chip Version */
240#define CHIPID_DEV 0x0200 /* bits 09..09: Device */
241#define CHIPID_FIRMWARE 0x01ff /* bits 08..00: Firmware Version */
242
243#define POWERCFG 2 /* Power Configuration */
244#define POWERCFG_DSMUTE 0x8000 /* bits 15..15: Softmute Disable */
245#define POWERCFG_DMUTE 0x4000 /* bits 14..14: Mute Disable */
246#define POWERCFG_MONO 0x2000 /* bits 13..13: Mono Select */
247#define POWERCFG_RDSM 0x0800 /* bits 11..11: RDS Mode (Si4701 only) */
248#define POWERCFG_SKMODE 0x0400 /* bits 10..10: Seek Mode */
249#define POWERCFG_SEEKUP 0x0200 /* bits 09..09: Seek Direction */
250#define POWERCFG_SEEK 0x0100 /* bits 08..08: Seek */
251#define POWERCFG_DISABLE 0x0040 /* bits 06..06: Powerup Disable */
252#define POWERCFG_ENABLE 0x0001 /* bits 00..00: Powerup Enable */
253
254#define CHANNEL 3 /* Channel */
255#define CHANNEL_TUNE 0x8000 /* bits 15..15: Tune */
256#define CHANNEL_CHAN 0x03ff /* bits 09..00: Channel Select */
257
258#define SYSCONFIG1 4 /* System Configuration 1 */
259#define SYSCONFIG1_RDSIEN 0x8000 /* bits 15..15: RDS Interrupt Enable (Si4701 only) */
260#define SYSCONFIG1_STCIEN 0x4000 /* bits 14..14: Seek/Tune Complete Interrupt Enable */
261#define SYSCONFIG1_RDS 0x1000 /* bits 12..12: RDS Enable (Si4701 only) */
262#define SYSCONFIG1_DE 0x0800 /* bits 11..11: De-emphasis (0=75us 1=50us) */
263#define SYSCONFIG1_AGCD 0x0400 /* bits 10..10: AGC Disable */
264#define SYSCONFIG1_BLNDADJ 0x00c0 /* bits 07..06: Stereo/Mono Blend Level Adjustment */
265#define SYSCONFIG1_GPIO3 0x0030 /* bits 05..04: General Purpose I/O 3 */
266#define SYSCONFIG1_GPIO2 0x000c /* bits 03..02: General Purpose I/O 2 */
267#define SYSCONFIG1_GPIO1 0x0003 /* bits 01..00: General Purpose I/O 1 */
268
269#define SYSCONFIG2 5 /* System Configuration 2 */
270#define SYSCONFIG2_SEEKTH 0xff00 /* bits 15..08: RSSI Seek Threshold */
271#define SYSCONFIG2_BAND 0x0080 /* bits 07..06: Band Select */
272#define SYSCONFIG2_SPACE 0x0030 /* bits 05..04: Channel Spacing */
273#define SYSCONFIG2_VOLUME 0x000f /* bits 03..00: Volume */
274
275#define SYSCONFIG3 6 /* System Configuration 3 */
276#define SYSCONFIG3_SMUTER 0xc000 /* bits 15..14: Softmute Attack/Recover Rate */
277#define SYSCONFIG3_SMUTEA 0x3000 /* bits 13..12: Softmute Attenuation */
278#define SYSCONFIG3_SKSNR 0x00f0 /* bits 07..04: Seek SNR Threshold */
279#define SYSCONFIG3_SKCNT 0x000f /* bits 03..00: Seek FM Impulse Detection Threshold */
280
281#define TEST1 7 /* Test 1 */
282#define TEST1_AHIZEN 0x4000 /* bits 14..14: Audio High-Z Enable */
283
284#define TEST2 8 /* Test 2 */
285/* TEST2 only contains reserved bits */
286
287#define BOOTCONFIG 9 /* Boot Configuration */
288/* BOOTCONFIG only contains reserved bits */
289
290#define STATUSRSSI 10 /* Status RSSI */
291#define STATUSRSSI_RDSR 0x8000 /* bits 15..15: RDS Ready (Si4701 only) */
292#define STATUSRSSI_STC 0x4000 /* bits 14..14: Seek/Tune Complete */
293#define STATUSRSSI_SF 0x2000 /* bits 13..13: Seek Fail/Band Limit */
294#define STATUSRSSI_AFCRL 0x1000 /* bits 12..12: AFC Rail */
295#define STATUSRSSI_RDSS 0x0800 /* bits 11..11: RDS Synchronized (Si4701 only) */
296#define STATUSRSSI_BLERA 0x0600 /* bits 10..09: RDS Block A Errors (Si4701 only) */
297#define STATUSRSSI_ST 0x0100 /* bits 08..08: Stereo Indicator */
298#define STATUSRSSI_RSSI 0x00ff /* bits 07..00: RSSI (Received Signal Strength Indicator) */
299
300#define READCHAN 11 /* Read Channel */
301#define READCHAN_BLERB 0xc000 /* bits 15..14: RDS Block D Errors (Si4701 only) */
302#define READCHAN_BLERC 0x3000 /* bits 13..12: RDS Block C Errors (Si4701 only) */
303#define READCHAN_BLERD 0x0c00 /* bits 11..10: RDS Block B Errors (Si4701 only) */
304#define READCHAN_READCHAN 0x03ff /* bits 09..00: Read Channel */
305
306#define RDSA 12 /* RDSA */
307#define RDSA_RDSA 0xffff /* bits 15..00: RDS Block A Data (Si4701 only) */
308
309#define RDSB 13 /* RDSB */
310#define RDSB_RDSB 0xffff /* bits 15..00: RDS Block B Data (Si4701 only) */
311
312#define RDSC 14 /* RDSC */
313#define RDSC_RDSC 0xffff /* bits 15..00: RDS Block C Data (Si4701 only) */
314
315#define RDSD 15 /* RDSD */
316#define RDSD_RDSD 0xffff /* bits 15..00: RDS Block D Data (Si4701 only) */
317
318
319
320/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300321 * USB HID Reports
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300322 **************************************************************************/
323
324/* Reports 1-16 give direct read/write access to the 16 Si470x registers */
325/* with the (REPORT_ID - 1) corresponding to the register address across USB */
326/* endpoint 0 using GET_REPORT and SET_REPORT */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300327#define REGISTER_REPORT_SIZE (RADIO_REGISTER_SIZE + 1)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300328#define REGISTER_REPORT(reg) ((reg) + 1)
329
330/* Report 17 gives direct read/write access to the entire Si470x register */
331/* map across endpoint 0 using GET_REPORT and SET_REPORT */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300332#define ENTIRE_REPORT_SIZE (RADIO_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300333#define ENTIRE_REPORT 17
334
335/* Report 18 is used to send the lowest 6 Si470x registers up the HID */
336/* interrupt endpoint 1 to Windows every 20 milliseconds for status */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300337#define RDS_REPORT_SIZE (RDS_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300338#define RDS_REPORT 18
339
340/* Report 19: LED state */
341#define LED_REPORT_SIZE 3
342#define LED_REPORT 19
343
344/* Report 19: stream */
345#define STREAM_REPORT_SIZE 3
346#define STREAM_REPORT 19
347
348/* Report 20: scratch */
349#define SCRATCH_PAGE_SIZE 63
350#define SCRATCH_REPORT_SIZE (SCRATCH_PAGE_SIZE + 1)
351#define SCRATCH_REPORT 20
352
353/* Reports 19-22: flash upgrade of the C8051F321 */
354#define WRITE_REPORT 19
355#define FLASH_REPORT 20
356#define CRC_REPORT 21
357#define RESPONSE_REPORT 22
358
359/* Report 23: currently unused, but can accept 60 byte reports on the HID */
360/* interrupt out endpoint 2 every 1 millisecond */
361#define UNUSED_REPORT 23
362
363
364
365/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300366 * Software/Hardware Versions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300367 **************************************************************************/
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300368#define RADIO_SW_VERSION_NOT_BOOTLOADABLE 6
369#define RADIO_SW_VERSION 7
370#define RADIO_SW_VERSION_CURRENT 15
371#define RADIO_HW_VERSION 1
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300372
373#define SCRATCH_PAGE_SW_VERSION 1
374#define SCRATCH_PAGE_HW_VERSION 2
375
376
377
378/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300379 * LED State Definitions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300380 **************************************************************************/
381#define LED_COMMAND 0x35
382
383#define NO_CHANGE_LED 0x00
384#define ALL_COLOR_LED 0x01 /* streaming state */
385#define BLINK_GREEN_LED 0x02 /* connect state */
386#define BLINK_RED_LED 0x04
387#define BLINK_ORANGE_LED 0x10 /* disconnect state */
388#define SOLID_GREEN_LED 0x20 /* tuning/seeking state */
389#define SOLID_RED_LED 0x40 /* bootload state */
390#define SOLID_ORANGE_LED 0x80
391
392
393
394/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300395 * Stream State Definitions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300396 **************************************************************************/
397#define STREAM_COMMAND 0x36
398#define STREAM_VIDPID 0x00
399#define STREAM_AUDIO 0xff
400
401
402
403/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300404 * Bootloader / Flash Commands
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300405 **************************************************************************/
406
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300407/* unique id sent to bootloader and required to put into a bootload state */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300408#define UNIQUE_BL_ID 0x34
409
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300410/* mask for the flash data */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300411#define FLASH_DATA_MASK 0x55
412
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300413/* bootloader commands */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300414#define GET_SW_VERSION_COMMAND 0x00
415#define SET_PAGE_COMMAND 0x01
416#define ERASE_PAGE_COMMAND 0x02
417#define WRITE_PAGE_COMMAND 0x03
418#define CRC_ON_PAGE_COMMAND 0x04
419#define READ_FLASH_BYTE_COMMAND 0x05
420#define RESET_DEVICE_COMMAND 0x06
421#define GET_HW_VERSION_COMMAND 0x07
422#define BLANK 0xff
423
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300424/* bootloader command responses */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300425#define COMMAND_OK 0x01
426#define COMMAND_FAILED 0x02
427#define COMMAND_PENDING 0x03
428
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300429/* buffer sizes */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300430#define COMMAND_BUFFER_SIZE 4
431#define RESPONSE_BUFFER_SIZE 2
432#define FLASH_BUFFER_SIZE 64
433#define CRC_BUFFER_SIZE 3
434
435
436
437/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300438 * General Driver Definitions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300439 **************************************************************************/
440
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300441/*
442 * si470x_device - private data
443 */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300444struct si470x_device {
445 /* reference to USB and video device */
446 struct usb_device *usbdev;
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300447 struct usb_interface *intf;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300448 struct video_device *videodev;
449
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300450 /* driver management */
451 unsigned int users;
Tobias Lorenzce5829e2008-05-31 15:04:32 -0300452 unsigned char disconnected;
453 struct mutex disconnect_lock;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300454
455 /* Silabs internal registers (0..15) */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300456 unsigned short registers[RADIO_REGISTER_NUM];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300457
458 /* RDS receive buffer */
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300459 struct delayed_work work;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300460 wait_queue_head_t read_queue;
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300461 struct mutex lock; /* buffer locking */
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300462 unsigned char *buffer; /* size is always multiple of three */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300463 unsigned int buf_size;
464 unsigned int rd_index;
465 unsigned int wr_index;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300466};
467
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300468
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300469/*
470 * The frequency is set in units of 62.5 Hz when using V4L2_TUNER_CAP_LOW,
471 * 62.5 kHz otherwise.
472 * The tuner is able to have a channel spacing of 50, 100 or 200 kHz.
473 * tuner->capability is therefore set to V4L2_TUNER_CAP_LOW
474 * The FREQ_MUL is then: 1 MHz / 62.5 Hz = 16000
475 */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300476#define FREQ_MUL (1000000 / 62.5)
477
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300478
479
480/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300481 * General Driver Functions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300482 **************************************************************************/
483
484/*
485 * si470x_get_report - receive a HID report
486 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300487static int si470x_get_report(struct si470x_device *radio, void *buf, int size)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300488{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300489 unsigned char *report = (unsigned char *) buf;
490 int retval;
491
492 retval = usb_control_msg(radio->usbdev,
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300493 usb_rcvctrlpipe(radio->usbdev, 0),
494 HID_REQ_GET_REPORT,
495 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300496 report[0], 2,
497 buf, size, usb_timeout);
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300498
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300499 if (retval < 0)
500 printk(KERN_WARNING DRIVER_NAME
501 ": si470x_get_report: usb_control_msg returned %d\n",
502 retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300503 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300504}
505
506
507/*
508 * si470x_set_report - send a HID report
509 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300510static int si470x_set_report(struct si470x_device *radio, void *buf, int size)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300511{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300512 unsigned char *report = (unsigned char *) buf;
513 int retval;
514
515 retval = usb_control_msg(radio->usbdev,
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300516 usb_sndctrlpipe(radio->usbdev, 0),
517 HID_REQ_SET_REPORT,
518 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300519 report[0], 2,
520 buf, size, usb_timeout);
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300521
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300522 if (retval < 0)
523 printk(KERN_WARNING DRIVER_NAME
524 ": si470x_set_report: usb_control_msg returned %d\n",
525 retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300526 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300527}
528
529
530/*
531 * si470x_get_register - read register
532 */
533static int si470x_get_register(struct si470x_device *radio, int regnr)
534{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300535 unsigned char buf[REGISTER_REPORT_SIZE];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300536 int retval;
537
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300538 buf[0] = REGISTER_REPORT(regnr);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300539
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300540 retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
541
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300542 if (retval >= 0)
Harvey Harrison5ad6b812008-04-15 22:22:11 -0300543 radio->registers[regnr] = get_unaligned_be16(&buf[1]);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300544
545 return (retval < 0) ? -EINVAL : 0;
546}
547
548
549/*
550 * si470x_set_register - write register
551 */
552static int si470x_set_register(struct si470x_device *radio, int regnr)
553{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300554 unsigned char buf[REGISTER_REPORT_SIZE];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300555 int retval;
556
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300557 buf[0] = REGISTER_REPORT(regnr);
Harvey Harrison5ad6b812008-04-15 22:22:11 -0300558 put_unaligned_be16(radio->registers[regnr], &buf[1]);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300559
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300560 retval = si470x_set_report(radio, (void *) &buf, sizeof(buf));
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300561
562 return (retval < 0) ? -EINVAL : 0;
563}
564
565
566/*
567 * si470x_get_all_registers - read entire registers
568 */
569static int si470x_get_all_registers(struct si470x_device *radio)
570{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300571 unsigned char buf[ENTIRE_REPORT_SIZE];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300572 int retval;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300573 unsigned char regnr;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300574
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300575 buf[0] = ENTIRE_REPORT;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300576
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300577 retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300578
579 if (retval >= 0)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300580 for (regnr = 0; regnr < RADIO_REGISTER_NUM; regnr++)
Harvey Harrison5ad6b812008-04-15 22:22:11 -0300581 radio->registers[regnr] = get_unaligned_be16(
582 &buf[regnr * RADIO_REGISTER_SIZE + 1]);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300583
584 return (retval < 0) ? -EINVAL : 0;
585}
586
587
588/*
589 * si470x_get_rds_registers - read rds registers
590 */
591static int si470x_get_rds_registers(struct si470x_device *radio)
592{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300593 unsigned char buf[RDS_REPORT_SIZE];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300594 int retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300595 int size;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300596 unsigned char regnr;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300597
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300598 buf[0] = RDS_REPORT;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300599
600 retval = usb_interrupt_msg(radio->usbdev,
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300601 usb_rcvintpipe(radio->usbdev, 1),
602 (void *) &buf, sizeof(buf), &size, usb_timeout);
603 if (size != sizeof(buf))
Tobias Lorenzce5829e2008-05-31 15:04:32 -0300604 printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: "
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300605 "return size differs: %d != %zu\n", size, sizeof(buf));
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300606 if (retval < 0)
607 printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: "
608 "usb_interrupt_msg returned %d\n", retval);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300609
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300610 if (retval >= 0)
611 for (regnr = 0; regnr < RDS_REGISTER_NUM; regnr++)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300612 radio->registers[STATUSRSSI + regnr] =
Tobias Lorenzce5829e2008-05-31 15:04:32 -0300613 get_unaligned_be16(
614 &buf[regnr * RADIO_REGISTER_SIZE + 1]);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300615
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300616 return (retval < 0) ? -EINVAL : 0;
617}
618
619
620/*
621 * si470x_set_chan - set the channel
622 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300623static int si470x_set_chan(struct si470x_device *radio, unsigned short chan)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300624{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300625 int retval;
626 unsigned long timeout;
627 bool timed_out = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300628
629 /* start tuning */
630 radio->registers[CHANNEL] &= ~CHANNEL_CHAN;
631 radio->registers[CHANNEL] |= CHANNEL_TUNE | chan;
632 retval = si470x_set_register(radio, CHANNEL);
633 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300634 goto done;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300635
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300636 /* wait till tune operation has completed */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300637 timeout = jiffies + msecs_to_jiffies(tune_timeout);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300638 do {
639 retval = si470x_get_register(radio, STATUSRSSI);
640 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300641 goto stop;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300642 timed_out = time_after(jiffies, timeout);
643 } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
644 (!timed_out));
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300645 if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
646 printk(KERN_WARNING DRIVER_NAME ": tune does not complete\n");
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300647 if (timed_out)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300648 printk(KERN_WARNING DRIVER_NAME
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300649 ": tune timed out after %u ms\n", tune_timeout);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300650
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300651stop:
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300652 /* stop tuning */
653 radio->registers[CHANNEL] &= ~CHANNEL_TUNE;
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300654 retval = si470x_set_register(radio, CHANNEL);
655
656done:
657 return retval;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300658}
659
660
661/*
662 * si470x_get_freq - get the frequency
663 */
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300664static int si470x_get_freq(struct si470x_device *radio, unsigned int *freq)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300665{
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300666 unsigned int spacing, band_bottom;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300667 unsigned short chan;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300668 int retval;
669
670 /* Spacing (kHz) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300671 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300672 /* 0: 200 kHz (USA, Australia) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300673 case 0:
674 spacing = 0.200 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300675 /* 1: 100 kHz (Europe, Japan) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300676 case 1:
677 spacing = 0.100 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300678 /* 2: 50 kHz */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300679 default:
680 spacing = 0.050 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300681 };
682
683 /* Bottom of Band (MHz) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300684 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300685 /* 0: 87.5 - 108 MHz (USA, Europe) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300686 case 0:
687 band_bottom = 87.5 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300688 /* 1: 76 - 108 MHz (Japan wide band) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300689 default:
690 band_bottom = 76 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300691 /* 2: 76 - 90 MHz (Japan) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300692 case 2:
693 band_bottom = 76 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300694 };
695
696 /* read channel */
697 retval = si470x_get_register(radio, READCHAN);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300698 chan = radio->registers[READCHAN] & READCHAN_READCHAN;
699
700 /* Frequency (MHz) = Spacing (kHz) x Channel + Bottom of Band (MHz) */
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300701 *freq = chan * spacing + band_bottom;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300702
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300703 return retval;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300704}
705
706
707/*
708 * si470x_set_freq - set the frequency
709 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300710static int si470x_set_freq(struct si470x_device *radio, unsigned int freq)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300711{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300712 unsigned int spacing, band_bottom;
713 unsigned short chan;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300714
715 /* Spacing (kHz) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300716 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300717 /* 0: 200 kHz (USA, Australia) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300718 case 0:
719 spacing = 0.200 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300720 /* 1: 100 kHz (Europe, Japan) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300721 case 1:
722 spacing = 0.100 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300723 /* 2: 50 kHz */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300724 default:
725 spacing = 0.050 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300726 };
727
728 /* Bottom of Band (MHz) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300729 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300730 /* 0: 87.5 - 108 MHz (USA, Europe) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300731 case 0:
732 band_bottom = 87.5 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300733 /* 1: 76 - 108 MHz (Japan wide band) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300734 default:
735 band_bottom = 76 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300736 /* 2: 76 - 90 MHz (Japan) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300737 case 2:
738 band_bottom = 76 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300739 };
740
741 /* Chan = [ Freq (Mhz) - Bottom of Band (MHz) ] / Spacing (kHz) */
742 chan = (freq - band_bottom) / spacing;
743
744 return si470x_set_chan(radio, chan);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300745}
746
747
748/*
Tobias Lorenzbbc89952008-05-31 15:11:32 -0300749 * si470x_set_seek - set seek
750 */
751static int si470x_set_seek(struct si470x_device *radio,
752 unsigned int wrap_around, unsigned int seek_upward)
753{
754 int retval = 0;
755 unsigned long timeout;
756 bool timed_out = 0;
757
758 /* start seeking */
759 radio->registers[POWERCFG] |= POWERCFG_SEEK;
760 if (wrap_around == 1)
761 radio->registers[POWERCFG] &= ~POWERCFG_SKMODE;
762 else
763 radio->registers[POWERCFG] |= POWERCFG_SKMODE;
764 if (seek_upward == 1)
765 radio->registers[POWERCFG] |= POWERCFG_SEEKUP;
766 else
767 radio->registers[POWERCFG] &= ~POWERCFG_SEEKUP;
768 retval = si470x_set_register(radio, POWERCFG);
769 if (retval < 0)
770 goto done;
771
772 /* wait till seek operation has completed */
773 timeout = jiffies + msecs_to_jiffies(seek_timeout);
774 do {
775 retval = si470x_get_register(radio, STATUSRSSI);
776 if (retval < 0)
777 goto stop;
778 timed_out = time_after(jiffies, timeout);
779 } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
780 (!timed_out));
781 if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
782 printk(KERN_WARNING DRIVER_NAME ": seek does not complete\n");
783 if (radio->registers[STATUSRSSI] & STATUSRSSI_SF)
784 printk(KERN_WARNING DRIVER_NAME
785 ": seek failed / band limit reached\n");
786 if (timed_out)
787 printk(KERN_WARNING DRIVER_NAME
788 ": seek timed out after %u ms\n", seek_timeout);
789
790stop:
791 /* stop seeking */
792 radio->registers[POWERCFG] &= ~POWERCFG_SEEK;
793 retval = si470x_set_register(radio, POWERCFG);
794
795done:
796 /* try again, if timed out */
797 if ((retval == 0) && timed_out)
798 retval = -EAGAIN;
799
800 return retval;
801}
802
803
804/*
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300805 * si470x_start - switch on radio
806 */
807static int si470x_start(struct si470x_device *radio)
808{
809 int retval;
810
811 /* powercfg */
812 radio->registers[POWERCFG] =
813 POWERCFG_DMUTE | POWERCFG_ENABLE | POWERCFG_RDSM;
814 retval = si470x_set_register(radio, POWERCFG);
815 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300816 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300817
818 /* sysconfig 1 */
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300819 radio->registers[SYSCONFIG1] = SYSCONFIG1_DE;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300820 retval = si470x_set_register(radio, SYSCONFIG1);
821 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300822 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300823
824 /* sysconfig 2 */
825 radio->registers[SYSCONFIG2] =
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300826 (0x3f << 8) | /* SEEKTH */
827 ((band << 6) & SYSCONFIG2_BAND) | /* BAND */
828 ((space << 4) & SYSCONFIG2_SPACE) | /* SPACE */
829 15; /* VOLUME (max) */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300830 retval = si470x_set_register(radio, SYSCONFIG2);
831 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300832 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300833
834 /* reset last channel */
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300835 retval = si470x_set_chan(radio,
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300836 radio->registers[CHANNEL] & CHANNEL_CHAN);
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300837
838done:
839 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300840}
841
842
843/*
844 * si470x_stop - switch off radio
845 */
846static int si470x_stop(struct si470x_device *radio)
847{
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300848 int retval;
849
850 /* sysconfig 1 */
851 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
852 retval = si470x_set_register(radio, SYSCONFIG1);
853 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300854 goto done;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300855
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300856 /* powercfg */
857 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
858 /* POWERCFG_ENABLE has to automatically go low */
859 radio->registers[POWERCFG] |= POWERCFG_ENABLE | POWERCFG_DISABLE;
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300860 retval = si470x_set_register(radio, POWERCFG);
861
862done:
863 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300864}
865
866
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300867/*
868 * si470x_rds_on - switch on rds reception
869 */
870static int si470x_rds_on(struct si470x_device *radio)
871{
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300872 int retval;
873
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300874 /* sysconfig 1 */
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300875 mutex_lock(&radio->lock);
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300876 radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDS;
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300877 retval = si470x_set_register(radio, SYSCONFIG1);
878 if (retval < 0)
879 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
880 mutex_unlock(&radio->lock);
881
882 return retval;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300883}
884
885
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300886
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300887/**************************************************************************
888 * RDS Driver Functions
889 **************************************************************************/
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300890
891/*
892 * si470x_rds - rds processing function
893 */
894static void si470x_rds(struct si470x_device *radio)
895{
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300896 unsigned char blocknum;
897 unsigned short bler; /* rds block errors */
898 unsigned short rds;
899 unsigned char tmpbuf[3];
900
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300901 /* get rds blocks */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300902 if (si470x_get_rds_registers(radio) < 0)
903 return;
904 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0) {
905 /* No RDS group ready */
906 return;
907 }
908 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSS) == 0) {
909 /* RDS decoder not synchronized */
910 return;
911 }
912
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300913 /* copy all four RDS blocks to internal buffer */
914 mutex_lock(&radio->lock);
915 for (blocknum = 0; blocknum < 4; blocknum++) {
916 switch (blocknum) {
917 default:
918 bler = (radio->registers[STATUSRSSI] &
919 STATUSRSSI_BLERA) >> 9;
920 rds = radio->registers[RDSA];
921 break;
922 case 1:
923 bler = (radio->registers[READCHAN] &
924 READCHAN_BLERB) >> 14;
925 rds = radio->registers[RDSB];
926 break;
927 case 2:
928 bler = (radio->registers[READCHAN] &
929 READCHAN_BLERC) >> 12;
930 rds = radio->registers[RDSC];
931 break;
932 case 3:
933 bler = (radio->registers[READCHAN] &
934 READCHAN_BLERD) >> 10;
935 rds = radio->registers[RDSD];
936 break;
937 };
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300938
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300939 /* Fill the V4L2 RDS buffer */
Harvey Harrison5ad6b812008-04-15 22:22:11 -0300940 put_unaligned_le16(rds, &tmpbuf);
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300941 tmpbuf[2] = blocknum; /* offset name */
942 tmpbuf[2] |= blocknum << 3; /* received offset */
943 if (bler > max_rds_errors)
944 tmpbuf[2] |= 0x80; /* uncorrectable errors */
945 else if (bler > 0)
946 tmpbuf[2] |= 0x40; /* corrected error(s) */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300947
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300948 /* copy RDS block to internal buffer */
949 memcpy(&radio->buffer[radio->wr_index], &tmpbuf, 3);
950 radio->wr_index += 3;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300951
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300952 /* wrap write pointer */
953 if (radio->wr_index >= radio->buf_size)
954 radio->wr_index = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300955
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300956 /* check for overflow */
957 if (radio->wr_index == radio->rd_index) {
958 /* increment and wrap read pointer */
959 radio->rd_index += 3;
960 if (radio->rd_index >= radio->buf_size)
961 radio->rd_index = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300962 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300963 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300964 mutex_unlock(&radio->lock);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300965
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300966 /* wake up read queue */
967 if (radio->wr_index != radio->rd_index)
968 wake_up_interruptible(&radio->read_queue);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300969}
970
971
972/*
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300973 * si470x_work - rds work function
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300974 */
975static void si470x_work(struct work_struct *work)
976{
977 struct si470x_device *radio = container_of(work, struct si470x_device,
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300978 work.work);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300979
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300980 /* safety checks */
Tobias Lorenzce5829e2008-05-31 15:04:32 -0300981 if (radio->disconnected)
982 return;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300983 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300984 return;
985
986 si470x_rds(radio);
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300987 schedule_delayed_work(&radio->work, msecs_to_jiffies(rds_poll_time));
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300988}
989
990
991
992/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300993 * File Operations Interface
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300994 **************************************************************************/
995
996/*
997 * si470x_fops_read - read RDS data
998 */
999static ssize_t si470x_fops_read(struct file *file, char __user *buf,
1000 size_t count, loff_t *ppos)
1001{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001002 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001003 int retval = 0;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001004 unsigned int block_count = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001005
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001006 /* switch on rds reception */
1007 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
1008 si470x_rds_on(radio);
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001009 schedule_delayed_work(&radio->work,
1010 msecs_to_jiffies(rds_poll_time));
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001011 }
1012
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001013 /* block if no new data available */
1014 while (radio->wr_index == radio->rd_index) {
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001015 if (file->f_flags & O_NONBLOCK) {
1016 retval = -EWOULDBLOCK;
1017 goto done;
1018 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001019 if (wait_event_interruptible(radio->read_queue,
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001020 radio->wr_index != radio->rd_index) < 0) {
1021 retval = -EINTR;
1022 goto done;
1023 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001024 }
1025
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001026 /* calculate block count from byte count */
1027 count /= 3;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001028
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001029 /* copy RDS block out of internal buffer and to user buffer */
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001030 mutex_lock(&radio->lock);
1031 while (block_count < count) {
1032 if (radio->rd_index == radio->wr_index)
1033 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001034
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001035 /* always transfer rds complete blocks */
1036 if (copy_to_user(buf, &radio->buffer[radio->rd_index], 3))
1037 /* retval = -EFAULT; */
1038 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001039
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001040 /* increment and wrap read pointer */
1041 radio->rd_index += 3;
1042 if (radio->rd_index >= radio->buf_size)
1043 radio->rd_index = 0;
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001044
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001045 /* increment counters */
1046 block_count++;
1047 buf += 3;
1048 retval += 3;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001049 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001050 mutex_unlock(&radio->lock);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001051
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001052done:
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001053 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001054}
1055
1056
1057/*
1058 * si470x_fops_poll - poll RDS data
1059 */
1060static unsigned int si470x_fops_poll(struct file *file,
1061 struct poll_table_struct *pts)
1062{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001063 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001064 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001065
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001066 /* switch on rds reception */
1067 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
1068 si470x_rds_on(radio);
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001069 schedule_delayed_work(&radio->work,
1070 msecs_to_jiffies(rds_poll_time));
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001071 }
1072
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001073 poll_wait(file, &radio->read_queue, pts);
1074
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001075 if (radio->rd_index != radio->wr_index)
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001076 retval = POLLIN | POLLRDNORM;
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001077
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001078 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001079}
1080
1081
1082/*
1083 * si470x_fops_open - file open
1084 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001085static int si470x_fops_open(struct file *file)
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001086{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001087 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001088 int retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001089
Hans Verkuild56dc612008-07-30 08:43:36 -03001090 lock_kernel();
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001091 radio->users++;
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001092
1093 retval = usb_autopm_get_interface(radio->intf);
1094 if (retval < 0) {
1095 radio->users--;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001096 retval = -EIO;
1097 goto done;
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001098 }
1099
1100 if (radio->users == 1) {
1101 retval = si470x_start(radio);
1102 if (retval < 0)
1103 usb_autopm_put_interface(radio->intf);
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001104 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001105
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001106done:
Hans Verkuild56dc612008-07-30 08:43:36 -03001107 unlock_kernel();
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001108 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001109}
1110
1111
1112/*
1113 * si470x_fops_release - file release
1114 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001115static int si470x_fops_release(struct file *file)
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001116{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001117 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001118 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001119
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001120 /* safety check */
1121 if (!radio) {
1122 retval = -ENODEV;
1123 goto done;
1124 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001125
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001126 mutex_lock(&radio->disconnect_lock);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001127 radio->users--;
1128 if (radio->users == 0) {
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001129 if (radio->disconnected) {
1130 video_unregister_device(radio->videodev);
1131 kfree(radio->buffer);
1132 kfree(radio);
1133 goto unlock;
1134 }
Tobias Lorenz03dea862008-04-22 14:46:11 -03001135
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001136 /* stop rds reception */
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001137 cancel_delayed_work_sync(&radio->work);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001138
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001139 /* cancel read processes */
1140 wake_up_interruptible(&radio->read_queue);
1141
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001142 retval = si470x_stop(radio);
1143 usb_autopm_put_interface(radio->intf);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001144 }
1145
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001146unlock:
1147 mutex_unlock(&radio->disconnect_lock);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001148
1149done:
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001150 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001151}
1152
1153
1154/*
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001155 * si470x_fops - file operations interface
1156 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001157static const struct v4l2_file_operations si470x_fops = {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001158 .owner = THIS_MODULE,
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001159 .read = si470x_fops_read,
1160 .poll = si470x_fops_poll,
1161 .ioctl = video_ioctl2,
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001162 .open = si470x_fops_open,
1163 .release = si470x_fops_release,
1164};
1165
1166
1167
1168/**************************************************************************
1169 * Video4Linux Interface
1170 **************************************************************************/
1171
1172/*
1173 * si470x_v4l2_queryctrl - query control
1174 */
1175static struct v4l2_queryctrl si470x_v4l2_queryctrl[] = {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001176 {
1177 .id = V4L2_CID_AUDIO_VOLUME,
1178 .type = V4L2_CTRL_TYPE_INTEGER,
1179 .name = "Volume",
1180 .minimum = 0,
1181 .maximum = 15,
1182 .step = 1,
1183 .default_value = 15,
1184 },
1185 {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001186 .id = V4L2_CID_AUDIO_MUTE,
1187 .type = V4L2_CTRL_TYPE_BOOLEAN,
1188 .name = "Mute",
1189 .minimum = 0,
1190 .maximum = 1,
1191 .step = 1,
1192 .default_value = 1,
1193 },
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001194};
1195
1196
1197/*
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001198 * si470x_vidioc_querycap - query device capabilities
1199 */
1200static int si470x_vidioc_querycap(struct file *file, void *priv,
1201 struct v4l2_capability *capability)
1202{
1203 strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver));
1204 strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
1205 sprintf(capability->bus_info, "USB");
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001206 capability->version = DRIVER_KERNEL_VERSION;
Tobias Lorenzbbc89952008-05-31 15:11:32 -03001207 capability->capabilities = V4L2_CAP_HW_FREQ_SEEK |
1208 V4L2_CAP_TUNER | V4L2_CAP_RADIO;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001209
1210 return 0;
1211}
1212
1213
1214/*
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001215 * si470x_vidioc_queryctrl - enumerate control items
1216 */
1217static int si470x_vidioc_queryctrl(struct file *file, void *priv,
1218 struct v4l2_queryctrl *qc)
1219{
Tobias Lorenzeb6e3b82008-09-24 19:25:39 -03001220 unsigned char i = 0;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001221 int retval = -EINVAL;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001222
Tobias Lorenzeb6e3b82008-09-24 19:25:39 -03001223 /* abort if qc->id is below V4L2_CID_BASE */
1224 if (qc->id < V4L2_CID_BASE)
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001225 goto done;
1226
Tobias Lorenzeb6e3b82008-09-24 19:25:39 -03001227 /* search video control */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001228 for (i = 0; i < ARRAY_SIZE(si470x_v4l2_queryctrl); i++) {
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001229 if (qc->id == si470x_v4l2_queryctrl[i].id) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001230 memcpy(qc, &(si470x_v4l2_queryctrl[i]), sizeof(*qc));
Tobias Lorenzeb6e3b82008-09-24 19:25:39 -03001231 retval = 0; /* found */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001232 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001233 }
1234 }
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001235
Tobias Lorenzeb6e3b82008-09-24 19:25:39 -03001236 /* disable unsupported base controls */
1237 /* to satisfy kradio and such apps */
1238 if ((retval == -EINVAL) && (qc->id < V4L2_CID_LASTP1)) {
1239 qc->flags = V4L2_CTRL_FLAG_DISABLED;
1240 retval = 0;
1241 }
1242
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001243done:
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001244 if (retval < 0)
1245 printk(KERN_WARNING DRIVER_NAME
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001246 ": query controls failed with %d\n", retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001247 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001248}
1249
1250
1251/*
1252 * si470x_vidioc_g_ctrl - get the value of a control
1253 */
1254static int si470x_vidioc_g_ctrl(struct file *file, void *priv,
1255 struct v4l2_control *ctrl)
1256{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001257 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001258 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001259
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001260 /* safety checks */
1261 if (radio->disconnected) {
1262 retval = -EIO;
1263 goto done;
1264 }
Tobias Lorenz03dea862008-04-22 14:46:11 -03001265
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001266 switch (ctrl->id) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001267 case V4L2_CID_AUDIO_VOLUME:
1268 ctrl->value = radio->registers[SYSCONFIG2] &
1269 SYSCONFIG2_VOLUME;
1270 break;
1271 case V4L2_CID_AUDIO_MUTE:
1272 ctrl->value = ((radio->registers[POWERCFG] &
1273 POWERCFG_DMUTE) == 0) ? 1 : 0;
1274 break;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001275 default:
1276 retval = -EINVAL;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001277 }
1278
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001279done:
1280 if (retval < 0)
1281 printk(KERN_WARNING DRIVER_NAME
1282 ": get control failed with %d\n", retval);
1283 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001284}
1285
1286
1287/*
1288 * si470x_vidioc_s_ctrl - set the value of a control
1289 */
1290static int si470x_vidioc_s_ctrl(struct file *file, void *priv,
1291 struct v4l2_control *ctrl)
1292{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001293 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001294 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001295
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001296 /* safety checks */
1297 if (radio->disconnected) {
1298 retval = -EIO;
1299 goto done;
1300 }
Tobias Lorenz03dea862008-04-22 14:46:11 -03001301
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001302 switch (ctrl->id) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001303 case V4L2_CID_AUDIO_VOLUME:
1304 radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_VOLUME;
1305 radio->registers[SYSCONFIG2] |= ctrl->value;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001306 retval = si470x_set_register(radio, SYSCONFIG2);
1307 break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001308 case V4L2_CID_AUDIO_MUTE:
1309 if (ctrl->value == 1)
1310 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
1311 else
1312 radio->registers[POWERCFG] |= POWERCFG_DMUTE;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001313 retval = si470x_set_register(radio, POWERCFG);
1314 break;
1315 default:
1316 retval = -EINVAL;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001317 }
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001318
1319done:
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001320 if (retval < 0)
1321 printk(KERN_WARNING DRIVER_NAME
1322 ": set control failed with %d\n", retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001323 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001324}
1325
1326
1327/*
1328 * si470x_vidioc_g_audio - get audio attributes
1329 */
1330static int si470x_vidioc_g_audio(struct file *file, void *priv,
1331 struct v4l2_audio *audio)
1332{
Tobias Lorenz617f5332008-09-24 19:33:09 -03001333 /* driver constants */
1334 audio->index = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001335 strcpy(audio->name, "Radio");
1336 audio->capability = V4L2_AUDCAP_STEREO;
Tobias Lorenz617f5332008-09-24 19:33:09 -03001337 audio->mode = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001338
Tobias Lorenz617f5332008-09-24 19:33:09 -03001339 return 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001340}
1341
1342
1343/*
1344 * si470x_vidioc_g_tuner - get tuner attributes
1345 */
1346static int si470x_vidioc_g_tuner(struct file *file, void *priv,
1347 struct v4l2_tuner *tuner)
1348{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001349 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001350 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001351
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001352 /* safety checks */
1353 if (radio->disconnected) {
1354 retval = -EIO;
1355 goto done;
1356 }
Tobias Lorenz33632d42008-09-24 19:30:26 -03001357 if (tuner->index != 0) {
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001358 retval = -EINVAL;
1359 goto done;
1360 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001361
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001362 retval = si470x_get_register(radio, STATUSRSSI);
1363 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001364 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001365
Tobias Lorenz33632d42008-09-24 19:30:26 -03001366 /* driver constants */
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001367 strcpy(tuner->name, "FM");
Tobias Lorenz33632d42008-09-24 19:30:26 -03001368 tuner->type = V4L2_TUNER_RADIO;
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001369 tuner->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
Tobias Lorenz33632d42008-09-24 19:30:26 -03001370
Tobias Lorenza17c0012008-09-24 19:21:50 -03001371 /* range limits */
1372 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001373 /* 0: 87.5 - 108 MHz (USA, Europe, default) */
1374 default:
1375 tuner->rangelow = 87.5 * FREQ_MUL;
1376 tuner->rangehigh = 108 * FREQ_MUL;
1377 break;
1378 /* 1: 76 - 108 MHz (Japan wide band) */
1379 case 1 :
1380 tuner->rangelow = 76 * FREQ_MUL;
1381 tuner->rangehigh = 108 * FREQ_MUL;
1382 break;
1383 /* 2: 76 - 90 MHz (Japan) */
1384 case 2 :
1385 tuner->rangelow = 76 * FREQ_MUL;
1386 tuner->rangehigh = 90 * FREQ_MUL;
1387 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001388 };
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001389
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001390 /* stereo indicator == stereo (instead of mono) */
Tobias Lorenzd807dec2009-02-12 14:56:10 -03001391 if ((radio->registers[STATUSRSSI] & STATUSRSSI_ST) == 0)
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001392 tuner->rxsubchans = V4L2_TUNER_SUB_MONO;
Tobias Lorenzd807dec2009-02-12 14:56:10 -03001393 else
1394 tuner->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001395
1396 /* mono/stereo selector */
Tobias Lorenzd807dec2009-02-12 14:56:10 -03001397 if ((radio->registers[POWERCFG] & POWERCFG_MONO) == 0)
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001398 tuner->audmode = V4L2_TUNER_MODE_STEREO;
Tobias Lorenzd807dec2009-02-12 14:56:10 -03001399 else
1400 tuner->audmode = V4L2_TUNER_MODE_MONO;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001401
1402 /* min is worst, max is best; signal:0..0xffff; rssi: 0..0xff */
Tobias Lorenzd807dec2009-02-12 14:56:10 -03001403 /* measured in units of dbµV in 1 db increments (max at ~75 dbµV) */
1404 tuner->signal = (radio->registers[STATUSRSSI] & STATUSRSSI_RSSI);
1405 /* the ideal factor is 0xffff/75 = 873,8 */
1406 tuner->signal = (tuner->signal * 873) + (8 * tuner->signal / 10);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001407
1408 /* automatic frequency control: -1: freq to low, 1 freq to high */
Tobias Lorenzace7d4b2008-05-31 15:09:07 -03001409 /* AFCRL does only indicate that freq. differs, not if too low/high */
1410 tuner->afc = (radio->registers[STATUSRSSI] & STATUSRSSI_AFCRL) ? 1 : 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001411
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001412done:
1413 if (retval < 0)
1414 printk(KERN_WARNING DRIVER_NAME
1415 ": get tuner failed with %d\n", retval);
1416 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001417}
1418
1419
1420/*
1421 * si470x_vidioc_s_tuner - set tuner attributes
1422 */
1423static int si470x_vidioc_s_tuner(struct file *file, void *priv,
1424 struct v4l2_tuner *tuner)
1425{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001426 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenz33632d42008-09-24 19:30:26 -03001427 int retval = -EINVAL;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001428
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001429 /* safety checks */
1430 if (radio->disconnected) {
1431 retval = -EIO;
1432 goto done;
1433 }
Tobias Lorenz33632d42008-09-24 19:30:26 -03001434 if (tuner->index != 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001435 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001436
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001437 /* mono/stereo selector */
1438 switch (tuner->audmode) {
1439 case V4L2_TUNER_MODE_MONO:
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001440 radio->registers[POWERCFG] |= POWERCFG_MONO; /* force mono */
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001441 break;
1442 case V4L2_TUNER_MODE_STEREO:
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001443 radio->registers[POWERCFG] &= ~POWERCFG_MONO; /* try stereo */
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001444 break;
1445 default:
1446 goto done;
1447 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001448
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001449 retval = si470x_set_register(radio, POWERCFG);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001450
1451done:
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001452 if (retval < 0)
1453 printk(KERN_WARNING DRIVER_NAME
1454 ": set tuner failed with %d\n", retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001455 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001456}
1457
1458
1459/*
1460 * si470x_vidioc_g_frequency - get tuner or modulator radio frequency
1461 */
1462static int si470x_vidioc_g_frequency(struct file *file, void *priv,
1463 struct v4l2_frequency *freq)
1464{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001465 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001466 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001467
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001468 /* safety checks */
1469 if (radio->disconnected) {
1470 retval = -EIO;
1471 goto done;
1472 }
Tobias Lorenz33632d42008-09-24 19:30:26 -03001473 if (freq->tuner != 0) {
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001474 retval = -EINVAL;
1475 goto done;
1476 }
Tobias Lorenz03dea862008-04-22 14:46:11 -03001477
Tobias Lorenz33632d42008-09-24 19:30:26 -03001478 freq->type = V4L2_TUNER_RADIO;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001479 retval = si470x_get_freq(radio, &freq->frequency);
1480
1481done:
1482 if (retval < 0)
1483 printk(KERN_WARNING DRIVER_NAME
1484 ": get frequency failed with %d\n", retval);
1485 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001486}
1487
1488
1489/*
1490 * si470x_vidioc_s_frequency - set tuner or modulator radio frequency
1491 */
1492static int si470x_vidioc_s_frequency(struct file *file, void *priv,
1493 struct v4l2_frequency *freq)
1494{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001495 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001496 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001497
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001498 /* safety checks */
1499 if (radio->disconnected) {
1500 retval = -EIO;
1501 goto done;
1502 }
Tobias Lorenz33632d42008-09-24 19:30:26 -03001503 if (freq->tuner != 0) {
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001504 retval = -EINVAL;
1505 goto done;
1506 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001507
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001508 retval = si470x_set_freq(radio, freq->frequency);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001509
1510done:
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001511 if (retval < 0)
1512 printk(KERN_WARNING DRIVER_NAME
1513 ": set frequency failed with %d\n", retval);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001514 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001515}
1516
1517
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001518/*
Tobias Lorenzbbc89952008-05-31 15:11:32 -03001519 * si470x_vidioc_s_hw_freq_seek - set hardware frequency seek
1520 */
1521static int si470x_vidioc_s_hw_freq_seek(struct file *file, void *priv,
1522 struct v4l2_hw_freq_seek *seek)
1523{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001524 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenzbbc89952008-05-31 15:11:32 -03001525 int retval = 0;
1526
1527 /* safety checks */
1528 if (radio->disconnected) {
1529 retval = -EIO;
1530 goto done;
1531 }
Tobias Lorenz33632d42008-09-24 19:30:26 -03001532 if (seek->tuner != 0) {
Tobias Lorenzbbc89952008-05-31 15:11:32 -03001533 retval = -EINVAL;
1534 goto done;
1535 }
1536
1537 retval = si470x_set_seek(radio, seek->wrap_around, seek->seek_upward);
1538
1539done:
1540 if (retval < 0)
1541 printk(KERN_WARNING DRIVER_NAME
1542 ": set hardware frequency seek failed with %d\n",
1543 retval);
1544 return retval;
1545}
1546
Tobias Lorenz374ab682008-09-24 19:17:54 -03001547
1548/*
1549 * si470x_ioctl_ops - video device ioctl operations
1550 */
Hans Verkuila3998102008-07-21 02:57:38 -03001551static const struct v4l2_ioctl_ops si470x_ioctl_ops = {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001552 .vidioc_querycap = si470x_vidioc_querycap,
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001553 .vidioc_queryctrl = si470x_vidioc_queryctrl,
1554 .vidioc_g_ctrl = si470x_vidioc_g_ctrl,
1555 .vidioc_s_ctrl = si470x_vidioc_s_ctrl,
1556 .vidioc_g_audio = si470x_vidioc_g_audio,
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001557 .vidioc_g_tuner = si470x_vidioc_g_tuner,
1558 .vidioc_s_tuner = si470x_vidioc_s_tuner,
1559 .vidioc_g_frequency = si470x_vidioc_g_frequency,
1560 .vidioc_s_frequency = si470x_vidioc_s_frequency,
Tobias Lorenzbbc89952008-05-31 15:11:32 -03001561 .vidioc_s_hw_freq_seek = si470x_vidioc_s_hw_freq_seek,
Hans Verkuila3998102008-07-21 02:57:38 -03001562};
1563
Tobias Lorenz374ab682008-09-24 19:17:54 -03001564
Hans Verkuila3998102008-07-21 02:57:38 -03001565/*
Tobias Lorenz374ab682008-09-24 19:17:54 -03001566 * si470x_viddev_template - video device interface
Hans Verkuila3998102008-07-21 02:57:38 -03001567 */
1568static struct video_device si470x_viddev_template = {
1569 .fops = &si470x_fops,
Hans Verkuila3998102008-07-21 02:57:38 -03001570 .name = DRIVER_NAME,
Hans Verkuila3998102008-07-21 02:57:38 -03001571 .release = video_device_release,
Tobias Lorenz374ab682008-09-24 19:17:54 -03001572 .ioctl_ops = &si470x_ioctl_ops,
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001573};
1574
1575
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001576
1577/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001578 * USB Interface
1579 **************************************************************************/
1580
1581/*
1582 * si470x_usb_driver_probe - probe for the device
1583 */
1584static int si470x_usb_driver_probe(struct usb_interface *intf,
1585 const struct usb_device_id *id)
1586{
1587 struct si470x_device *radio;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001588 int retval = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001589
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001590 /* private data allocation and initialization */
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001591 radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001592 if (!radio) {
1593 retval = -ENOMEM;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001594 goto err_initial;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001595 }
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001596 radio->users = 0;
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001597 radio->disconnected = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001598 radio->usbdev = interface_to_usbdev(intf);
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001599 radio->intf = intf;
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001600 mutex_init(&radio->disconnect_lock);
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001601 mutex_init(&radio->lock);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001602
1603 /* video device allocation and initialization */
1604 radio->videodev = video_device_alloc();
1605 if (!radio->videodev) {
1606 retval = -ENOMEM;
1607 goto err_radio;
1608 }
1609 memcpy(radio->videodev, &si470x_viddev_template,
1610 sizeof(si470x_viddev_template));
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001611 video_set_drvdata(radio->videodev, radio);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001612
1613 /* show some infos about the specific device */
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001614 if (si470x_get_all_registers(radio) < 0) {
1615 retval = -EIO;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001616 goto err_all;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001617 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001618 printk(KERN_INFO DRIVER_NAME ": DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001619 radio->registers[DEVICEID], radio->registers[CHIPID]);
1620
1621 /* check if firmware is current */
1622 if ((radio->registers[CHIPID] & CHIPID_FIRMWARE)
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001623 < RADIO_SW_VERSION_CURRENT) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001624 printk(KERN_WARNING DRIVER_NAME
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001625 ": This driver is known to work with "
1626 "firmware version %hu,\n", RADIO_SW_VERSION_CURRENT);
1627 printk(KERN_WARNING DRIVER_NAME
1628 ": but the device has firmware version %hu.\n",
1629 radio->registers[CHIPID] & CHIPID_FIRMWARE);
1630 printk(KERN_WARNING DRIVER_NAME
1631 ": If you have some trouble using this driver,\n");
1632 printk(KERN_WARNING DRIVER_NAME
1633 ": please report to V4L ML at "
1634 "video4linux-list@redhat.com\n");
1635 }
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001636
1637 /* set initial frequency */
1638 si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
1639
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001640 /* rds buffer allocation */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001641 radio->buf_size = rds_buf * 3;
1642 radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001643 if (!radio->buffer) {
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
1648 /* rds buffer configuration */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001649 radio->wr_index = 0;
1650 radio->rd_index = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001651 init_waitqueue_head(&radio->read_queue);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001652
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001653 /* prepare rds work function */
1654 INIT_DELAYED_WORK(&radio->work, si470x_work);
1655
1656 /* register video device */
Hans Verkuilcba99ae2008-09-03 17:11:58 -03001657 retval = video_register_device(radio->videodev, VFL_TYPE_RADIO, radio_nr);
1658 if (retval) {
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001659 printk(KERN_WARNING DRIVER_NAME
1660 ": Could not register video device\n");
1661 goto err_all;
1662 }
1663 usb_set_intfdata(intf, radio);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001664
1665 return 0;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001666err_all:
1667 video_device_release(radio->videodev);
1668 kfree(radio->buffer);
1669err_radio:
1670 kfree(radio);
1671err_initial:
1672 return retval;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001673}
1674
1675
1676/*
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001677 * si470x_usb_driver_suspend - suspend the device
1678 */
1679static int si470x_usb_driver_suspend(struct usb_interface *intf,
1680 pm_message_t message)
1681{
1682 struct si470x_device *radio = usb_get_intfdata(intf);
1683
1684 printk(KERN_INFO DRIVER_NAME ": suspending now...\n");
1685
1686 cancel_delayed_work_sync(&radio->work);
1687
1688 return 0;
1689}
1690
1691
1692/*
1693 * si470x_usb_driver_resume - resume the device
1694 */
1695static int si470x_usb_driver_resume(struct usb_interface *intf)
1696{
1697 struct si470x_device *radio = usb_get_intfdata(intf);
1698
1699 printk(KERN_INFO DRIVER_NAME ": resuming now...\n");
1700
1701 mutex_lock(&radio->lock);
1702 if (radio->users && radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS)
1703 schedule_delayed_work(&radio->work,
1704 msecs_to_jiffies(rds_poll_time));
1705 mutex_unlock(&radio->lock);
1706
1707 return 0;
1708}
1709
1710
1711/*
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001712 * si470x_usb_driver_disconnect - disconnect the device
1713 */
1714static void si470x_usb_driver_disconnect(struct usb_interface *intf)
1715{
1716 struct si470x_device *radio = usb_get_intfdata(intf);
1717
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001718 mutex_lock(&radio->disconnect_lock);
1719 radio->disconnected = 1;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001720 cancel_delayed_work_sync(&radio->work);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001721 usb_set_intfdata(intf, NULL);
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001722 if (radio->users == 0) {
1723 video_unregister_device(radio->videodev);
1724 kfree(radio->buffer);
1725 kfree(radio);
1726 }
1727 mutex_unlock(&radio->disconnect_lock);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001728}
1729
1730
1731/*
1732 * si470x_usb_driver - usb driver interface
1733 */
1734static struct usb_driver si470x_usb_driver = {
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001735 .name = DRIVER_NAME,
1736 .probe = si470x_usb_driver_probe,
1737 .disconnect = si470x_usb_driver_disconnect,
1738 .suspend = si470x_usb_driver_suspend,
1739 .resume = si470x_usb_driver_resume,
1740 .id_table = si470x_usb_driver_id_table,
1741 .supports_autosuspend = 1,
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001742};
1743
1744
1745
1746/**************************************************************************
1747 * Module Interface
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001748 **************************************************************************/
1749
1750/*
1751 * si470x_module_init - module init
1752 */
1753static int __init si470x_module_init(void)
1754{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001755 printk(KERN_INFO DRIVER_DESC ", Version " DRIVER_VERSION "\n");
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001756 return usb_register(&si470x_usb_driver);
1757}
1758
1759
1760/*
1761 * si470x_module_exit - module exit
1762 */
1763static void __exit si470x_module_exit(void)
1764{
1765 usb_deregister(&si470x_usb_driver);
1766}
1767
1768
1769module_init(si470x_module_init);
1770module_exit(si470x_module_exit);
1771
1772MODULE_LICENSE("GPL");
1773MODULE_AUTHOR(DRIVER_AUTHOR);
1774MODULE_DESCRIPTION(DRIVER_DESC);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001775MODULE_VERSION(DRIVER_VERSION);