blob: 457445ec7b527bd6c0ffa0e99ce9d0eb187bebb7 [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
Tobias Lorenz78656ac2008-01-14 21:55:27 -030099 *
100 * ToDo:
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300101 * - add firmware download/update support
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300102 * - RDS support: interrupt mode, instead of polling
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300103 * - add LED status output (check if that's not already done in firmware)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300104 */
105
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300106
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300107/* driver definitions */
108#define DRIVER_AUTHOR "Tobias Lorenz <tobias.lorenz@gmx.net>"
109#define DRIVER_NAME "radio-si470x"
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300110#define DRIVER_KERNEL_VERSION KERNEL_VERSION(1, 0, 8)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300111#define DRIVER_CARD "Silicon Labs Si470x FM Radio Receiver"
112#define DRIVER_DESC "USB radio driver for Si470x FM Radio Receivers"
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300113#define DRIVER_VERSION "1.0.8"
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300114
115
116/* kernel includes */
117#include <linux/kernel.h>
118#include <linux/module.h>
119#include <linux/init.h>
120#include <linux/slab.h>
121#include <linux/input.h>
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300122#include <linux/usb.h>
123#include <linux/hid.h>
124#include <linux/version.h>
Mauro Carvalho Chehab387d4472008-01-15 11:25:10 -0300125#include <linux/videodev2.h>
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300126#include <linux/mutex.h>
Mauro Carvalho Chehab387d4472008-01-15 11:25:10 -0300127#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300128#include <media/v4l2-ioctl.h>
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300129#include <media/rds.h>
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300130#include <asm/unaligned.h>
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300131
132
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300133/* USB Device ID List */
134static struct usb_device_id si470x_usb_driver_id_table[] = {
135 /* Silicon Labs USB FM Radio Reference Design */
Tobias Lorenz374ab682008-09-24 19:17:54 -0300136 { USB_DEVICE_AND_INTERFACE_INFO(0x10c4, 0x818a, USB_CLASS_HID, 0, 0) },
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300137 /* ADS/Tech FM Radio Receiver (formerly Instant FM Music) */
Tobias Lorenz374ab682008-09-24 19:17:54 -0300138 { USB_DEVICE_AND_INTERFACE_INFO(0x06e1, 0xa155, USB_CLASS_HID, 0, 0) },
Alexey Klimov69df96c2008-10-23 09:20:27 -0300139 /* KWorld USB FM Radio SnapMusic Mobile 700 (FM700) */
140 { USB_DEVICE_AND_INTERFACE_INFO(0x1b80, 0xd700, USB_CLASS_HID, 0, 0) },
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300141 /* Terminating entry */
142 { }
143};
144MODULE_DEVICE_TABLE(usb, si470x_usb_driver_id_table);
145
146
147
148/**************************************************************************
149 * Module Parameters
150 **************************************************************************/
151
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300152/* Radio Nr */
153static int radio_nr = -1;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300154module_param(radio_nr, int, 0444);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300155MODULE_PARM_DESC(radio_nr, "Radio Nr");
156
157/* Spacing (kHz) */
158/* 0: 200 kHz (USA, Australia) */
159/* 1: 100 kHz (Europe, Japan) */
160/* 2: 50 kHz */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300161static unsigned short space = 2;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300162module_param(space, ushort, 0444);
163MODULE_PARM_DESC(space, "Spacing: 0=200kHz 1=100kHz *2=50kHz*");
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300164
165/* Bottom of Band (MHz) */
166/* 0: 87.5 - 108 MHz (USA, Europe)*/
167/* 1: 76 - 108 MHz (Japan wide band) */
168/* 2: 76 - 90 MHz (Japan) */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300169static unsigned short band = 1;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300170module_param(band, ushort, 0444);
171MODULE_PARM_DESC(band, "Band: 0=87.5..108MHz *1=76..108MHz* 2=76..90MHz");
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300172
173/* De-emphasis */
174/* 0: 75 us (USA) */
175/* 1: 50 us (Europe, Australia, Japan) */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300176static unsigned short de = 1;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300177module_param(de, ushort, 0444);
178MODULE_PARM_DESC(de, "De-emphasis: 0=75us *1=50us*");
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300179
180/* USB timeout */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300181static unsigned int usb_timeout = 500;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300182module_param(usb_timeout, uint, 0644);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300183MODULE_PARM_DESC(usb_timeout, "USB timeout (ms): *500*");
184
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300185/* Tune timeout */
186static unsigned int tune_timeout = 3000;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300187module_param(tune_timeout, uint, 0644);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300188MODULE_PARM_DESC(tune_timeout, "Tune timeout: *3000*");
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300189
Tobias Lorenzbbc89952008-05-31 15:11:32 -0300190/* Seek timeout */
191static unsigned int seek_timeout = 5000;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300192module_param(seek_timeout, uint, 0644);
Tobias Lorenzbbc89952008-05-31 15:11:32 -0300193MODULE_PARM_DESC(seek_timeout, "Seek timeout: *5000*");
194
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300195/* RDS buffer blocks */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300196static unsigned int rds_buf = 100;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300197module_param(rds_buf, uint, 0444);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300198MODULE_PARM_DESC(rds_buf, "RDS buffer entries: *100*");
199
200/* RDS maximum block errors */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300201static unsigned short max_rds_errors = 1;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300202/* 0 means 0 errors requiring correction */
203/* 1 means 1-2 errors requiring correction (used by original USBRadio.exe) */
204/* 2 means 3-5 errors requiring correction */
205/* 3 means 6+ errors or errors in checkword, correction not possible */
Tobias Lorenz374ab682008-09-24 19:17:54 -0300206module_param(max_rds_errors, ushort, 0644);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300207MODULE_PARM_DESC(max_rds_errors, "RDS maximum block errors: *1*");
208
209/* RDS poll frequency */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300210static unsigned int rds_poll_time = 40;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300211/* 40 is used by the original USBRadio.exe */
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300212/* 50 is used by radio-cadet */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300213/* 75 should be okay */
214/* 80 is the usual RDS receive interval */
Tobias Lorenz374ab682008-09-24 19:17:54 -0300215module_param(rds_poll_time, uint, 0644);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300216MODULE_PARM_DESC(rds_poll_time, "RDS poll time (ms): *40*");
217
218
219
220/**************************************************************************
221 * Register Definitions
222 **************************************************************************/
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300223#define RADIO_REGISTER_SIZE 2 /* 16 register bit width */
224#define RADIO_REGISTER_NUM 16 /* DEVICEID ... RDSD */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300225#define RDS_REGISTER_NUM 6 /* STATUSRSSI ... RDSD */
226
227#define DEVICEID 0 /* Device ID */
228#define DEVICEID_PN 0xf000 /* bits 15..12: Part Number */
229#define DEVICEID_MFGID 0x0fff /* bits 11..00: Manufacturer ID */
230
231#define CHIPID 1 /* Chip ID */
232#define CHIPID_REV 0xfc00 /* bits 15..10: Chip Version */
233#define CHIPID_DEV 0x0200 /* bits 09..09: Device */
234#define CHIPID_FIRMWARE 0x01ff /* bits 08..00: Firmware Version */
235
236#define POWERCFG 2 /* Power Configuration */
237#define POWERCFG_DSMUTE 0x8000 /* bits 15..15: Softmute Disable */
238#define POWERCFG_DMUTE 0x4000 /* bits 14..14: Mute Disable */
239#define POWERCFG_MONO 0x2000 /* bits 13..13: Mono Select */
240#define POWERCFG_RDSM 0x0800 /* bits 11..11: RDS Mode (Si4701 only) */
241#define POWERCFG_SKMODE 0x0400 /* bits 10..10: Seek Mode */
242#define POWERCFG_SEEKUP 0x0200 /* bits 09..09: Seek Direction */
243#define POWERCFG_SEEK 0x0100 /* bits 08..08: Seek */
244#define POWERCFG_DISABLE 0x0040 /* bits 06..06: Powerup Disable */
245#define POWERCFG_ENABLE 0x0001 /* bits 00..00: Powerup Enable */
246
247#define CHANNEL 3 /* Channel */
248#define CHANNEL_TUNE 0x8000 /* bits 15..15: Tune */
249#define CHANNEL_CHAN 0x03ff /* bits 09..00: Channel Select */
250
251#define SYSCONFIG1 4 /* System Configuration 1 */
252#define SYSCONFIG1_RDSIEN 0x8000 /* bits 15..15: RDS Interrupt Enable (Si4701 only) */
253#define SYSCONFIG1_STCIEN 0x4000 /* bits 14..14: Seek/Tune Complete Interrupt Enable */
254#define SYSCONFIG1_RDS 0x1000 /* bits 12..12: RDS Enable (Si4701 only) */
255#define SYSCONFIG1_DE 0x0800 /* bits 11..11: De-emphasis (0=75us 1=50us) */
256#define SYSCONFIG1_AGCD 0x0400 /* bits 10..10: AGC Disable */
257#define SYSCONFIG1_BLNDADJ 0x00c0 /* bits 07..06: Stereo/Mono Blend Level Adjustment */
258#define SYSCONFIG1_GPIO3 0x0030 /* bits 05..04: General Purpose I/O 3 */
259#define SYSCONFIG1_GPIO2 0x000c /* bits 03..02: General Purpose I/O 2 */
260#define SYSCONFIG1_GPIO1 0x0003 /* bits 01..00: General Purpose I/O 1 */
261
262#define SYSCONFIG2 5 /* System Configuration 2 */
263#define SYSCONFIG2_SEEKTH 0xff00 /* bits 15..08: RSSI Seek Threshold */
264#define SYSCONFIG2_BAND 0x0080 /* bits 07..06: Band Select */
265#define SYSCONFIG2_SPACE 0x0030 /* bits 05..04: Channel Spacing */
266#define SYSCONFIG2_VOLUME 0x000f /* bits 03..00: Volume */
267
268#define SYSCONFIG3 6 /* System Configuration 3 */
269#define SYSCONFIG3_SMUTER 0xc000 /* bits 15..14: Softmute Attack/Recover Rate */
270#define SYSCONFIG3_SMUTEA 0x3000 /* bits 13..12: Softmute Attenuation */
271#define SYSCONFIG3_SKSNR 0x00f0 /* bits 07..04: Seek SNR Threshold */
272#define SYSCONFIG3_SKCNT 0x000f /* bits 03..00: Seek FM Impulse Detection Threshold */
273
274#define TEST1 7 /* Test 1 */
275#define TEST1_AHIZEN 0x4000 /* bits 14..14: Audio High-Z Enable */
276
277#define TEST2 8 /* Test 2 */
278/* TEST2 only contains reserved bits */
279
280#define BOOTCONFIG 9 /* Boot Configuration */
281/* BOOTCONFIG only contains reserved bits */
282
283#define STATUSRSSI 10 /* Status RSSI */
284#define STATUSRSSI_RDSR 0x8000 /* bits 15..15: RDS Ready (Si4701 only) */
285#define STATUSRSSI_STC 0x4000 /* bits 14..14: Seek/Tune Complete */
286#define STATUSRSSI_SF 0x2000 /* bits 13..13: Seek Fail/Band Limit */
287#define STATUSRSSI_AFCRL 0x1000 /* bits 12..12: AFC Rail */
288#define STATUSRSSI_RDSS 0x0800 /* bits 11..11: RDS Synchronized (Si4701 only) */
289#define STATUSRSSI_BLERA 0x0600 /* bits 10..09: RDS Block A Errors (Si4701 only) */
290#define STATUSRSSI_ST 0x0100 /* bits 08..08: Stereo Indicator */
291#define STATUSRSSI_RSSI 0x00ff /* bits 07..00: RSSI (Received Signal Strength Indicator) */
292
293#define READCHAN 11 /* Read Channel */
294#define READCHAN_BLERB 0xc000 /* bits 15..14: RDS Block D Errors (Si4701 only) */
295#define READCHAN_BLERC 0x3000 /* bits 13..12: RDS Block C Errors (Si4701 only) */
296#define READCHAN_BLERD 0x0c00 /* bits 11..10: RDS Block B Errors (Si4701 only) */
297#define READCHAN_READCHAN 0x03ff /* bits 09..00: Read Channel */
298
299#define RDSA 12 /* RDSA */
300#define RDSA_RDSA 0xffff /* bits 15..00: RDS Block A Data (Si4701 only) */
301
302#define RDSB 13 /* RDSB */
303#define RDSB_RDSB 0xffff /* bits 15..00: RDS Block B Data (Si4701 only) */
304
305#define RDSC 14 /* RDSC */
306#define RDSC_RDSC 0xffff /* bits 15..00: RDS Block C Data (Si4701 only) */
307
308#define RDSD 15 /* RDSD */
309#define RDSD_RDSD 0xffff /* bits 15..00: RDS Block D Data (Si4701 only) */
310
311
312
313/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300314 * USB HID Reports
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300315 **************************************************************************/
316
317/* Reports 1-16 give direct read/write access to the 16 Si470x registers */
318/* with the (REPORT_ID - 1) corresponding to the register address across USB */
319/* endpoint 0 using GET_REPORT and SET_REPORT */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300320#define REGISTER_REPORT_SIZE (RADIO_REGISTER_SIZE + 1)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300321#define REGISTER_REPORT(reg) ((reg) + 1)
322
323/* Report 17 gives direct read/write access to the entire Si470x register */
324/* map across endpoint 0 using GET_REPORT and SET_REPORT */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300325#define ENTIRE_REPORT_SIZE (RADIO_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300326#define ENTIRE_REPORT 17
327
328/* Report 18 is used to send the lowest 6 Si470x registers up the HID */
329/* interrupt endpoint 1 to Windows every 20 milliseconds for status */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300330#define RDS_REPORT_SIZE (RDS_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300331#define RDS_REPORT 18
332
333/* Report 19: LED state */
334#define LED_REPORT_SIZE 3
335#define LED_REPORT 19
336
337/* Report 19: stream */
338#define STREAM_REPORT_SIZE 3
339#define STREAM_REPORT 19
340
341/* Report 20: scratch */
342#define SCRATCH_PAGE_SIZE 63
343#define SCRATCH_REPORT_SIZE (SCRATCH_PAGE_SIZE + 1)
344#define SCRATCH_REPORT 20
345
346/* Reports 19-22: flash upgrade of the C8051F321 */
347#define WRITE_REPORT 19
348#define FLASH_REPORT 20
349#define CRC_REPORT 21
350#define RESPONSE_REPORT 22
351
352/* Report 23: currently unused, but can accept 60 byte reports on the HID */
353/* interrupt out endpoint 2 every 1 millisecond */
354#define UNUSED_REPORT 23
355
356
357
358/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300359 * Software/Hardware Versions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300360 **************************************************************************/
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300361#define RADIO_SW_VERSION_NOT_BOOTLOADABLE 6
362#define RADIO_SW_VERSION 7
363#define RADIO_SW_VERSION_CURRENT 15
364#define RADIO_HW_VERSION 1
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300365
366#define SCRATCH_PAGE_SW_VERSION 1
367#define SCRATCH_PAGE_HW_VERSION 2
368
369
370
371/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300372 * LED State Definitions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300373 **************************************************************************/
374#define LED_COMMAND 0x35
375
376#define NO_CHANGE_LED 0x00
377#define ALL_COLOR_LED 0x01 /* streaming state */
378#define BLINK_GREEN_LED 0x02 /* connect state */
379#define BLINK_RED_LED 0x04
380#define BLINK_ORANGE_LED 0x10 /* disconnect state */
381#define SOLID_GREEN_LED 0x20 /* tuning/seeking state */
382#define SOLID_RED_LED 0x40 /* bootload state */
383#define SOLID_ORANGE_LED 0x80
384
385
386
387/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300388 * Stream State Definitions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300389 **************************************************************************/
390#define STREAM_COMMAND 0x36
391#define STREAM_VIDPID 0x00
392#define STREAM_AUDIO 0xff
393
394
395
396/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300397 * Bootloader / Flash Commands
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300398 **************************************************************************/
399
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300400/* unique id sent to bootloader and required to put into a bootload state */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300401#define UNIQUE_BL_ID 0x34
402
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300403/* mask for the flash data */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300404#define FLASH_DATA_MASK 0x55
405
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300406/* bootloader commands */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300407#define GET_SW_VERSION_COMMAND 0x00
408#define SET_PAGE_COMMAND 0x01
409#define ERASE_PAGE_COMMAND 0x02
410#define WRITE_PAGE_COMMAND 0x03
411#define CRC_ON_PAGE_COMMAND 0x04
412#define READ_FLASH_BYTE_COMMAND 0x05
413#define RESET_DEVICE_COMMAND 0x06
414#define GET_HW_VERSION_COMMAND 0x07
415#define BLANK 0xff
416
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300417/* bootloader command responses */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300418#define COMMAND_OK 0x01
419#define COMMAND_FAILED 0x02
420#define COMMAND_PENDING 0x03
421
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300422/* buffer sizes */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300423#define COMMAND_BUFFER_SIZE 4
424#define RESPONSE_BUFFER_SIZE 2
425#define FLASH_BUFFER_SIZE 64
426#define CRC_BUFFER_SIZE 3
427
428
429
430/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300431 * General Driver Definitions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300432 **************************************************************************/
433
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300434/*
435 * si470x_device - private data
436 */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300437struct si470x_device {
438 /* reference to USB and video device */
439 struct usb_device *usbdev;
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300440 struct usb_interface *intf;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300441 struct video_device *videodev;
442
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300443 /* driver management */
444 unsigned int users;
Tobias Lorenzce5829e2008-05-31 15:04:32 -0300445 unsigned char disconnected;
446 struct mutex disconnect_lock;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300447
448 /* Silabs internal registers (0..15) */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300449 unsigned short registers[RADIO_REGISTER_NUM];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300450
451 /* RDS receive buffer */
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300452 struct delayed_work work;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300453 wait_queue_head_t read_queue;
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300454 struct mutex lock; /* buffer locking */
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300455 unsigned char *buffer; /* size is always multiple of three */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300456 unsigned int buf_size;
457 unsigned int rd_index;
458 unsigned int wr_index;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300459};
460
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300461
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300462/*
463 * The frequency is set in units of 62.5 Hz when using V4L2_TUNER_CAP_LOW,
464 * 62.5 kHz otherwise.
465 * The tuner is able to have a channel spacing of 50, 100 or 200 kHz.
466 * tuner->capability is therefore set to V4L2_TUNER_CAP_LOW
467 * The FREQ_MUL is then: 1 MHz / 62.5 Hz = 16000
468 */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300469#define FREQ_MUL (1000000 / 62.5)
470
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300471
472
473/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300474 * General Driver Functions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300475 **************************************************************************/
476
477/*
478 * si470x_get_report - receive a HID report
479 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300480static int si470x_get_report(struct si470x_device *radio, void *buf, int size)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300481{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300482 unsigned char *report = (unsigned char *) buf;
483 int retval;
484
485 retval = usb_control_msg(radio->usbdev,
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300486 usb_rcvctrlpipe(radio->usbdev, 0),
487 HID_REQ_GET_REPORT,
488 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300489 report[0], 2,
490 buf, size, usb_timeout);
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300491
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300492 if (retval < 0)
493 printk(KERN_WARNING DRIVER_NAME
494 ": si470x_get_report: usb_control_msg returned %d\n",
495 retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300496 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300497}
498
499
500/*
501 * si470x_set_report - send a HID report
502 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300503static int si470x_set_report(struct si470x_device *radio, void *buf, int size)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300504{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300505 unsigned char *report = (unsigned char *) buf;
506 int retval;
507
508 retval = usb_control_msg(radio->usbdev,
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300509 usb_sndctrlpipe(radio->usbdev, 0),
510 HID_REQ_SET_REPORT,
511 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300512 report[0], 2,
513 buf, size, usb_timeout);
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300514
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300515 if (retval < 0)
516 printk(KERN_WARNING DRIVER_NAME
517 ": si470x_set_report: usb_control_msg returned %d\n",
518 retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300519 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300520}
521
522
523/*
524 * si470x_get_register - read register
525 */
526static int si470x_get_register(struct si470x_device *radio, int regnr)
527{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300528 unsigned char buf[REGISTER_REPORT_SIZE];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300529 int retval;
530
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300531 buf[0] = REGISTER_REPORT(regnr);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300532
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300533 retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
534
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300535 if (retval >= 0)
Harvey Harrison5ad6b812008-04-15 22:22:11 -0300536 radio->registers[regnr] = get_unaligned_be16(&buf[1]);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300537
538 return (retval < 0) ? -EINVAL : 0;
539}
540
541
542/*
543 * si470x_set_register - write register
544 */
545static int si470x_set_register(struct si470x_device *radio, int regnr)
546{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300547 unsigned char buf[REGISTER_REPORT_SIZE];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300548 int retval;
549
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300550 buf[0] = REGISTER_REPORT(regnr);
Harvey Harrison5ad6b812008-04-15 22:22:11 -0300551 put_unaligned_be16(radio->registers[regnr], &buf[1]);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300552
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300553 retval = si470x_set_report(radio, (void *) &buf, sizeof(buf));
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300554
555 return (retval < 0) ? -EINVAL : 0;
556}
557
558
559/*
560 * si470x_get_all_registers - read entire registers
561 */
562static int si470x_get_all_registers(struct si470x_device *radio)
563{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300564 unsigned char buf[ENTIRE_REPORT_SIZE];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300565 int retval;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300566 unsigned char regnr;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300567
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300568 buf[0] = ENTIRE_REPORT;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300569
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300570 retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300571
572 if (retval >= 0)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300573 for (regnr = 0; regnr < RADIO_REGISTER_NUM; regnr++)
Harvey Harrison5ad6b812008-04-15 22:22:11 -0300574 radio->registers[regnr] = get_unaligned_be16(
575 &buf[regnr * RADIO_REGISTER_SIZE + 1]);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300576
577 return (retval < 0) ? -EINVAL : 0;
578}
579
580
581/*
582 * si470x_get_rds_registers - read rds registers
583 */
584static int si470x_get_rds_registers(struct si470x_device *radio)
585{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300586 unsigned char buf[RDS_REPORT_SIZE];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300587 int retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300588 int size;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300589 unsigned char regnr;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300590
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300591 buf[0] = RDS_REPORT;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300592
593 retval = usb_interrupt_msg(radio->usbdev,
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300594 usb_rcvintpipe(radio->usbdev, 1),
595 (void *) &buf, sizeof(buf), &size, usb_timeout);
596 if (size != sizeof(buf))
Tobias Lorenzce5829e2008-05-31 15:04:32 -0300597 printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: "
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300598 "return size differs: %d != %zu\n", size, sizeof(buf));
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300599 if (retval < 0)
600 printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: "
601 "usb_interrupt_msg returned %d\n", retval);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300602
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300603 if (retval >= 0)
604 for (regnr = 0; regnr < RDS_REGISTER_NUM; regnr++)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300605 radio->registers[STATUSRSSI + regnr] =
Tobias Lorenzce5829e2008-05-31 15:04:32 -0300606 get_unaligned_be16(
607 &buf[regnr * RADIO_REGISTER_SIZE + 1]);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300608
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300609 return (retval < 0) ? -EINVAL : 0;
610}
611
612
613/*
614 * si470x_set_chan - set the channel
615 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300616static int si470x_set_chan(struct si470x_device *radio, unsigned short chan)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300617{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300618 int retval;
619 unsigned long timeout;
620 bool timed_out = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300621
622 /* start tuning */
623 radio->registers[CHANNEL] &= ~CHANNEL_CHAN;
624 radio->registers[CHANNEL] |= CHANNEL_TUNE | chan;
625 retval = si470x_set_register(radio, CHANNEL);
626 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300627 goto done;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300628
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300629 /* wait till tune operation has completed */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300630 timeout = jiffies + msecs_to_jiffies(tune_timeout);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300631 do {
632 retval = si470x_get_register(radio, STATUSRSSI);
633 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300634 goto stop;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300635 timed_out = time_after(jiffies, timeout);
636 } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
637 (!timed_out));
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300638 if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
639 printk(KERN_WARNING DRIVER_NAME ": tune does not complete\n");
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300640 if (timed_out)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300641 printk(KERN_WARNING DRIVER_NAME
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300642 ": tune timed out after %u ms\n", tune_timeout);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300643
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300644stop:
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300645 /* stop tuning */
646 radio->registers[CHANNEL] &= ~CHANNEL_TUNE;
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300647 retval = si470x_set_register(radio, CHANNEL);
648
649done:
650 return retval;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300651}
652
653
654/*
655 * si470x_get_freq - get the frequency
656 */
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300657static int si470x_get_freq(struct si470x_device *radio, unsigned int *freq)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300658{
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300659 unsigned int spacing, band_bottom;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300660 unsigned short chan;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300661 int retval;
662
663 /* Spacing (kHz) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300664 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300665 /* 0: 200 kHz (USA, Australia) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300666 case 0:
667 spacing = 0.200 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300668 /* 1: 100 kHz (Europe, Japan) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300669 case 1:
670 spacing = 0.100 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300671 /* 2: 50 kHz */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300672 default:
673 spacing = 0.050 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300674 };
675
676 /* Bottom of Band (MHz) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300677 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300678 /* 0: 87.5 - 108 MHz (USA, Europe) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300679 case 0:
680 band_bottom = 87.5 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300681 /* 1: 76 - 108 MHz (Japan wide band) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300682 default:
683 band_bottom = 76 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300684 /* 2: 76 - 90 MHz (Japan) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300685 case 2:
686 band_bottom = 76 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300687 };
688
689 /* read channel */
690 retval = si470x_get_register(radio, READCHAN);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300691 chan = radio->registers[READCHAN] & READCHAN_READCHAN;
692
693 /* Frequency (MHz) = Spacing (kHz) x Channel + Bottom of Band (MHz) */
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300694 *freq = chan * spacing + band_bottom;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300695
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300696 return retval;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300697}
698
699
700/*
701 * si470x_set_freq - set the frequency
702 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300703static int si470x_set_freq(struct si470x_device *radio, unsigned int freq)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300704{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300705 unsigned int spacing, band_bottom;
706 unsigned short chan;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300707
708 /* Spacing (kHz) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300709 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300710 /* 0: 200 kHz (USA, Australia) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300711 case 0:
712 spacing = 0.200 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300713 /* 1: 100 kHz (Europe, Japan) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300714 case 1:
715 spacing = 0.100 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300716 /* 2: 50 kHz */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300717 default:
718 spacing = 0.050 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300719 };
720
721 /* Bottom of Band (MHz) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300722 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300723 /* 0: 87.5 - 108 MHz (USA, Europe) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300724 case 0:
725 band_bottom = 87.5 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300726 /* 1: 76 - 108 MHz (Japan wide band) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300727 default:
728 band_bottom = 76 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300729 /* 2: 76 - 90 MHz (Japan) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300730 case 2:
731 band_bottom = 76 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300732 };
733
734 /* Chan = [ Freq (Mhz) - Bottom of Band (MHz) ] / Spacing (kHz) */
735 chan = (freq - band_bottom) / spacing;
736
737 return si470x_set_chan(radio, chan);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300738}
739
740
741/*
Tobias Lorenzbbc89952008-05-31 15:11:32 -0300742 * si470x_set_seek - set seek
743 */
744static int si470x_set_seek(struct si470x_device *radio,
745 unsigned int wrap_around, unsigned int seek_upward)
746{
747 int retval = 0;
748 unsigned long timeout;
749 bool timed_out = 0;
750
751 /* start seeking */
752 radio->registers[POWERCFG] |= POWERCFG_SEEK;
753 if (wrap_around == 1)
754 radio->registers[POWERCFG] &= ~POWERCFG_SKMODE;
755 else
756 radio->registers[POWERCFG] |= POWERCFG_SKMODE;
757 if (seek_upward == 1)
758 radio->registers[POWERCFG] |= POWERCFG_SEEKUP;
759 else
760 radio->registers[POWERCFG] &= ~POWERCFG_SEEKUP;
761 retval = si470x_set_register(radio, POWERCFG);
762 if (retval < 0)
763 goto done;
764
765 /* wait till seek operation has completed */
766 timeout = jiffies + msecs_to_jiffies(seek_timeout);
767 do {
768 retval = si470x_get_register(radio, STATUSRSSI);
769 if (retval < 0)
770 goto stop;
771 timed_out = time_after(jiffies, timeout);
772 } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
773 (!timed_out));
774 if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
775 printk(KERN_WARNING DRIVER_NAME ": seek does not complete\n");
776 if (radio->registers[STATUSRSSI] & STATUSRSSI_SF)
777 printk(KERN_WARNING DRIVER_NAME
778 ": seek failed / band limit reached\n");
779 if (timed_out)
780 printk(KERN_WARNING DRIVER_NAME
781 ": seek timed out after %u ms\n", seek_timeout);
782
783stop:
784 /* stop seeking */
785 radio->registers[POWERCFG] &= ~POWERCFG_SEEK;
786 retval = si470x_set_register(radio, POWERCFG);
787
788done:
789 /* try again, if timed out */
790 if ((retval == 0) && timed_out)
791 retval = -EAGAIN;
792
793 return retval;
794}
795
796
797/*
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300798 * si470x_start - switch on radio
799 */
800static int si470x_start(struct si470x_device *radio)
801{
802 int retval;
803
804 /* powercfg */
805 radio->registers[POWERCFG] =
806 POWERCFG_DMUTE | POWERCFG_ENABLE | POWERCFG_RDSM;
807 retval = si470x_set_register(radio, POWERCFG);
808 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300809 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300810
811 /* sysconfig 1 */
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300812 radio->registers[SYSCONFIG1] = SYSCONFIG1_DE;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300813 retval = si470x_set_register(radio, SYSCONFIG1);
814 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300815 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300816
817 /* sysconfig 2 */
818 radio->registers[SYSCONFIG2] =
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300819 (0x3f << 8) | /* SEEKTH */
820 ((band << 6) & SYSCONFIG2_BAND) | /* BAND */
821 ((space << 4) & SYSCONFIG2_SPACE) | /* SPACE */
822 15; /* VOLUME (max) */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300823 retval = si470x_set_register(radio, SYSCONFIG2);
824 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300825 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300826
827 /* reset last channel */
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300828 retval = si470x_set_chan(radio,
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300829 radio->registers[CHANNEL] & CHANNEL_CHAN);
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300830
831done:
832 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300833}
834
835
836/*
837 * si470x_stop - switch off radio
838 */
839static int si470x_stop(struct si470x_device *radio)
840{
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300841 int retval;
842
843 /* sysconfig 1 */
844 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
845 retval = si470x_set_register(radio, SYSCONFIG1);
846 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300847 goto done;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300848
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300849 /* powercfg */
850 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
851 /* POWERCFG_ENABLE has to automatically go low */
852 radio->registers[POWERCFG] |= POWERCFG_ENABLE | POWERCFG_DISABLE;
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300853 retval = si470x_set_register(radio, POWERCFG);
854
855done:
856 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300857}
858
859
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300860/*
861 * si470x_rds_on - switch on rds reception
862 */
863static int si470x_rds_on(struct si470x_device *radio)
864{
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300865 int retval;
866
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300867 /* sysconfig 1 */
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300868 mutex_lock(&radio->lock);
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300869 radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDS;
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300870 retval = si470x_set_register(radio, SYSCONFIG1);
871 if (retval < 0)
872 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
873 mutex_unlock(&radio->lock);
874
875 return retval;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300876}
877
878
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300879
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300880/**************************************************************************
881 * RDS Driver Functions
882 **************************************************************************/
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300883
884/*
885 * si470x_rds - rds processing function
886 */
887static void si470x_rds(struct si470x_device *radio)
888{
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300889 unsigned char blocknum;
890 unsigned short bler; /* rds block errors */
891 unsigned short rds;
892 unsigned char tmpbuf[3];
893
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300894 /* get rds blocks */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300895 if (si470x_get_rds_registers(radio) < 0)
896 return;
897 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0) {
898 /* No RDS group ready */
899 return;
900 }
901 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSS) == 0) {
902 /* RDS decoder not synchronized */
903 return;
904 }
905
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300906 /* copy all four RDS blocks to internal buffer */
907 mutex_lock(&radio->lock);
908 for (blocknum = 0; blocknum < 4; blocknum++) {
909 switch (blocknum) {
910 default:
911 bler = (radio->registers[STATUSRSSI] &
912 STATUSRSSI_BLERA) >> 9;
913 rds = radio->registers[RDSA];
914 break;
915 case 1:
916 bler = (radio->registers[READCHAN] &
917 READCHAN_BLERB) >> 14;
918 rds = radio->registers[RDSB];
919 break;
920 case 2:
921 bler = (radio->registers[READCHAN] &
922 READCHAN_BLERC) >> 12;
923 rds = radio->registers[RDSC];
924 break;
925 case 3:
926 bler = (radio->registers[READCHAN] &
927 READCHAN_BLERD) >> 10;
928 rds = radio->registers[RDSD];
929 break;
930 };
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300931
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300932 /* Fill the V4L2 RDS buffer */
Harvey Harrison5ad6b812008-04-15 22:22:11 -0300933 put_unaligned_le16(rds, &tmpbuf);
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300934 tmpbuf[2] = blocknum; /* offset name */
935 tmpbuf[2] |= blocknum << 3; /* received offset */
936 if (bler > max_rds_errors)
937 tmpbuf[2] |= 0x80; /* uncorrectable errors */
938 else if (bler > 0)
939 tmpbuf[2] |= 0x40; /* corrected error(s) */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300940
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300941 /* copy RDS block to internal buffer */
942 memcpy(&radio->buffer[radio->wr_index], &tmpbuf, 3);
943 radio->wr_index += 3;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300944
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300945 /* wrap write pointer */
946 if (radio->wr_index >= radio->buf_size)
947 radio->wr_index = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300948
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300949 /* check for overflow */
950 if (radio->wr_index == radio->rd_index) {
951 /* increment and wrap read pointer */
952 radio->rd_index += 3;
953 if (radio->rd_index >= radio->buf_size)
954 radio->rd_index = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300955 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300956 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300957 mutex_unlock(&radio->lock);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300958
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300959 /* wake up read queue */
960 if (radio->wr_index != radio->rd_index)
961 wake_up_interruptible(&radio->read_queue);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300962}
963
964
965/*
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300966 * si470x_work - rds work function
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300967 */
968static void si470x_work(struct work_struct *work)
969{
970 struct si470x_device *radio = container_of(work, struct si470x_device,
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300971 work.work);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300972
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300973 /* safety checks */
Tobias Lorenzce5829e2008-05-31 15:04:32 -0300974 if (radio->disconnected)
975 return;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300976 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300977 return;
978
979 si470x_rds(radio);
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300980 schedule_delayed_work(&radio->work, msecs_to_jiffies(rds_poll_time));
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300981}
982
983
984
985/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300986 * File Operations Interface
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300987 **************************************************************************/
988
989/*
990 * si470x_fops_read - read RDS data
991 */
992static ssize_t si470x_fops_read(struct file *file, char __user *buf,
993 size_t count, loff_t *ppos)
994{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300995 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300996 int retval = 0;
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300997 unsigned int block_count = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300998
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300999 /* switch on rds reception */
1000 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
1001 si470x_rds_on(radio);
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001002 schedule_delayed_work(&radio->work,
1003 msecs_to_jiffies(rds_poll_time));
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001004 }
1005
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001006 /* block if no new data available */
1007 while (radio->wr_index == radio->rd_index) {
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001008 if (file->f_flags & O_NONBLOCK) {
1009 retval = -EWOULDBLOCK;
1010 goto done;
1011 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001012 if (wait_event_interruptible(radio->read_queue,
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001013 radio->wr_index != radio->rd_index) < 0) {
1014 retval = -EINTR;
1015 goto done;
1016 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001017 }
1018
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001019 /* calculate block count from byte count */
1020 count /= 3;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001021
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001022 /* copy RDS block out of internal buffer and to user buffer */
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001023 mutex_lock(&radio->lock);
1024 while (block_count < count) {
1025 if (radio->rd_index == radio->wr_index)
1026 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001027
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001028 /* always transfer rds complete blocks */
1029 if (copy_to_user(buf, &radio->buffer[radio->rd_index], 3))
1030 /* retval = -EFAULT; */
1031 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001032
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001033 /* increment and wrap read pointer */
1034 radio->rd_index += 3;
1035 if (radio->rd_index >= radio->buf_size)
1036 radio->rd_index = 0;
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001037
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001038 /* increment counters */
1039 block_count++;
1040 buf += 3;
1041 retval += 3;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001042 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001043 mutex_unlock(&radio->lock);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001044
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001045done:
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001046 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001047}
1048
1049
1050/*
1051 * si470x_fops_poll - poll RDS data
1052 */
1053static unsigned int si470x_fops_poll(struct file *file,
1054 struct poll_table_struct *pts)
1055{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001056 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001057 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001058
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001059 /* switch on rds reception */
1060 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
1061 si470x_rds_on(radio);
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001062 schedule_delayed_work(&radio->work,
1063 msecs_to_jiffies(rds_poll_time));
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001064 }
1065
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001066 poll_wait(file, &radio->read_queue, pts);
1067
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001068 if (radio->rd_index != radio->wr_index)
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001069 retval = POLLIN | POLLRDNORM;
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001070
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001071 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001072}
1073
1074
1075/*
1076 * si470x_fops_open - file open
1077 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001078static int si470x_fops_open(struct file *file)
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001079{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001080 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001081 int retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001082
Hans Verkuild56dc612008-07-30 08:43:36 -03001083 lock_kernel();
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001084 radio->users++;
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001085
1086 retval = usb_autopm_get_interface(radio->intf);
1087 if (retval < 0) {
1088 radio->users--;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001089 retval = -EIO;
1090 goto done;
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001091 }
1092
1093 if (radio->users == 1) {
1094 retval = si470x_start(radio);
1095 if (retval < 0)
1096 usb_autopm_put_interface(radio->intf);
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001097 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001098
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001099done:
Hans Verkuild56dc612008-07-30 08:43:36 -03001100 unlock_kernel();
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001101 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001102}
1103
1104
1105/*
1106 * si470x_fops_release - file release
1107 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001108static int si470x_fops_release(struct file *file)
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001109{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001110 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001111 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001112
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001113 /* safety check */
1114 if (!radio) {
1115 retval = -ENODEV;
1116 goto done;
1117 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001118
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001119 mutex_lock(&radio->disconnect_lock);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001120 radio->users--;
1121 if (radio->users == 0) {
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001122 if (radio->disconnected) {
1123 video_unregister_device(radio->videodev);
1124 kfree(radio->buffer);
1125 kfree(radio);
1126 goto unlock;
1127 }
Tobias Lorenz03dea862008-04-22 14:46:11 -03001128
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001129 /* stop rds reception */
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001130 cancel_delayed_work_sync(&radio->work);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001131
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001132 /* cancel read processes */
1133 wake_up_interruptible(&radio->read_queue);
1134
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001135 retval = si470x_stop(radio);
1136 usb_autopm_put_interface(radio->intf);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001137 }
1138
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001139unlock:
1140 mutex_unlock(&radio->disconnect_lock);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001141
1142done:
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001143 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001144}
1145
1146
1147/*
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001148 * si470x_fops - file operations interface
1149 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001150static const struct v4l2_file_operations si470x_fops = {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001151 .owner = THIS_MODULE,
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001152 .read = si470x_fops_read,
1153 .poll = si470x_fops_poll,
1154 .ioctl = video_ioctl2,
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001155 .open = si470x_fops_open,
1156 .release = si470x_fops_release,
1157};
1158
1159
1160
1161/**************************************************************************
1162 * Video4Linux Interface
1163 **************************************************************************/
1164
1165/*
1166 * si470x_v4l2_queryctrl - query control
1167 */
1168static struct v4l2_queryctrl si470x_v4l2_queryctrl[] = {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001169 {
1170 .id = V4L2_CID_AUDIO_VOLUME,
1171 .type = V4L2_CTRL_TYPE_INTEGER,
1172 .name = "Volume",
1173 .minimum = 0,
1174 .maximum = 15,
1175 .step = 1,
1176 .default_value = 15,
1177 },
1178 {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001179 .id = V4L2_CID_AUDIO_MUTE,
1180 .type = V4L2_CTRL_TYPE_BOOLEAN,
1181 .name = "Mute",
1182 .minimum = 0,
1183 .maximum = 1,
1184 .step = 1,
1185 .default_value = 1,
1186 },
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001187};
1188
1189
1190/*
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001191 * si470x_vidioc_querycap - query device capabilities
1192 */
1193static int si470x_vidioc_querycap(struct file *file, void *priv,
1194 struct v4l2_capability *capability)
1195{
1196 strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver));
1197 strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
1198 sprintf(capability->bus_info, "USB");
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001199 capability->version = DRIVER_KERNEL_VERSION;
Tobias Lorenzbbc89952008-05-31 15:11:32 -03001200 capability->capabilities = V4L2_CAP_HW_FREQ_SEEK |
1201 V4L2_CAP_TUNER | V4L2_CAP_RADIO;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001202
1203 return 0;
1204}
1205
1206
1207/*
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001208 * si470x_vidioc_queryctrl - enumerate control items
1209 */
1210static int si470x_vidioc_queryctrl(struct file *file, void *priv,
1211 struct v4l2_queryctrl *qc)
1212{
Tobias Lorenzeb6e3b82008-09-24 19:25:39 -03001213 unsigned char i = 0;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001214 int retval = -EINVAL;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001215
Tobias Lorenzeb6e3b82008-09-24 19:25:39 -03001216 /* abort if qc->id is below V4L2_CID_BASE */
1217 if (qc->id < V4L2_CID_BASE)
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001218 goto done;
1219
Tobias Lorenzeb6e3b82008-09-24 19:25:39 -03001220 /* search video control */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001221 for (i = 0; i < ARRAY_SIZE(si470x_v4l2_queryctrl); i++) {
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001222 if (qc->id == si470x_v4l2_queryctrl[i].id) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001223 memcpy(qc, &(si470x_v4l2_queryctrl[i]), sizeof(*qc));
Tobias Lorenzeb6e3b82008-09-24 19:25:39 -03001224 retval = 0; /* found */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001225 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001226 }
1227 }
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001228
Tobias Lorenzeb6e3b82008-09-24 19:25:39 -03001229 /* disable unsupported base controls */
1230 /* to satisfy kradio and such apps */
1231 if ((retval == -EINVAL) && (qc->id < V4L2_CID_LASTP1)) {
1232 qc->flags = V4L2_CTRL_FLAG_DISABLED;
1233 retval = 0;
1234 }
1235
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001236done:
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001237 if (retval < 0)
1238 printk(KERN_WARNING DRIVER_NAME
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001239 ": query controls failed with %d\n", retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001240 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001241}
1242
1243
1244/*
1245 * si470x_vidioc_g_ctrl - get the value of a control
1246 */
1247static int si470x_vidioc_g_ctrl(struct file *file, void *priv,
1248 struct v4l2_control *ctrl)
1249{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001250 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001251 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001252
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001253 /* safety checks */
1254 if (radio->disconnected) {
1255 retval = -EIO;
1256 goto done;
1257 }
Tobias Lorenz03dea862008-04-22 14:46:11 -03001258
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001259 switch (ctrl->id) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001260 case V4L2_CID_AUDIO_VOLUME:
1261 ctrl->value = radio->registers[SYSCONFIG2] &
1262 SYSCONFIG2_VOLUME;
1263 break;
1264 case V4L2_CID_AUDIO_MUTE:
1265 ctrl->value = ((radio->registers[POWERCFG] &
1266 POWERCFG_DMUTE) == 0) ? 1 : 0;
1267 break;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001268 default:
1269 retval = -EINVAL;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001270 }
1271
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001272done:
1273 if (retval < 0)
1274 printk(KERN_WARNING DRIVER_NAME
1275 ": get control failed with %d\n", retval);
1276 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001277}
1278
1279
1280/*
1281 * si470x_vidioc_s_ctrl - set the value of a control
1282 */
1283static int si470x_vidioc_s_ctrl(struct file *file, void *priv,
1284 struct v4l2_control *ctrl)
1285{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001286 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001287 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001288
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001289 /* safety checks */
1290 if (radio->disconnected) {
1291 retval = -EIO;
1292 goto done;
1293 }
Tobias Lorenz03dea862008-04-22 14:46:11 -03001294
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001295 switch (ctrl->id) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001296 case V4L2_CID_AUDIO_VOLUME:
1297 radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_VOLUME;
1298 radio->registers[SYSCONFIG2] |= ctrl->value;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001299 retval = si470x_set_register(radio, SYSCONFIG2);
1300 break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001301 case V4L2_CID_AUDIO_MUTE:
1302 if (ctrl->value == 1)
1303 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
1304 else
1305 radio->registers[POWERCFG] |= POWERCFG_DMUTE;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001306 retval = si470x_set_register(radio, POWERCFG);
1307 break;
1308 default:
1309 retval = -EINVAL;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001310 }
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001311
1312done:
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001313 if (retval < 0)
1314 printk(KERN_WARNING DRIVER_NAME
1315 ": set control failed with %d\n", retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001316 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001317}
1318
1319
1320/*
1321 * si470x_vidioc_g_audio - get audio attributes
1322 */
1323static int si470x_vidioc_g_audio(struct file *file, void *priv,
1324 struct v4l2_audio *audio)
1325{
Tobias Lorenz617f5332008-09-24 19:33:09 -03001326 /* driver constants */
1327 audio->index = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001328 strcpy(audio->name, "Radio");
1329 audio->capability = V4L2_AUDCAP_STEREO;
Tobias Lorenz617f5332008-09-24 19:33:09 -03001330 audio->mode = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001331
Tobias Lorenz617f5332008-09-24 19:33:09 -03001332 return 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001333}
1334
1335
1336/*
1337 * si470x_vidioc_g_tuner - get tuner attributes
1338 */
1339static int si470x_vidioc_g_tuner(struct file *file, void *priv,
1340 struct v4l2_tuner *tuner)
1341{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001342 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001343 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001344
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001345 /* safety checks */
1346 if (radio->disconnected) {
1347 retval = -EIO;
1348 goto done;
1349 }
Tobias Lorenz33632d42008-09-24 19:30:26 -03001350 if (tuner->index != 0) {
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001351 retval = -EINVAL;
1352 goto done;
1353 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001354
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001355 retval = si470x_get_register(radio, STATUSRSSI);
1356 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001357 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001358
Tobias Lorenz33632d42008-09-24 19:30:26 -03001359 /* driver constants */
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001360 strcpy(tuner->name, "FM");
Tobias Lorenz33632d42008-09-24 19:30:26 -03001361 tuner->type = V4L2_TUNER_RADIO;
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001362 tuner->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
Tobias Lorenz33632d42008-09-24 19:30:26 -03001363
Tobias Lorenza17c0012008-09-24 19:21:50 -03001364 /* range limits */
1365 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001366 /* 0: 87.5 - 108 MHz (USA, Europe, default) */
1367 default:
1368 tuner->rangelow = 87.5 * FREQ_MUL;
1369 tuner->rangehigh = 108 * FREQ_MUL;
1370 break;
1371 /* 1: 76 - 108 MHz (Japan wide band) */
1372 case 1 :
1373 tuner->rangelow = 76 * FREQ_MUL;
1374 tuner->rangehigh = 108 * FREQ_MUL;
1375 break;
1376 /* 2: 76 - 90 MHz (Japan) */
1377 case 2 :
1378 tuner->rangelow = 76 * FREQ_MUL;
1379 tuner->rangehigh = 90 * FREQ_MUL;
1380 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001381 };
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001382
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001383 /* stereo indicator == stereo (instead of mono) */
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001384 if ((radio->registers[STATUSRSSI] & STATUSRSSI_ST) == 1)
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001385 tuner->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001386 else
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001387 tuner->rxsubchans = V4L2_TUNER_SUB_MONO;
1388
1389 /* mono/stereo selector */
1390 if ((radio->registers[POWERCFG] & POWERCFG_MONO) == 1)
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001391 tuner->audmode = V4L2_TUNER_MODE_MONO;
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001392 else
1393 tuner->audmode = V4L2_TUNER_MODE_STEREO;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001394
1395 /* min is worst, max is best; signal:0..0xffff; rssi: 0..0xff */
1396 tuner->signal = (radio->registers[STATUSRSSI] & STATUSRSSI_RSSI)
1397 * 0x0101;
1398
1399 /* automatic frequency control: -1: freq to low, 1 freq to high */
Tobias Lorenzace7d4b2008-05-31 15:09:07 -03001400 /* AFCRL does only indicate that freq. differs, not if too low/high */
1401 tuner->afc = (radio->registers[STATUSRSSI] & STATUSRSSI_AFCRL) ? 1 : 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001402
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001403done:
1404 if (retval < 0)
1405 printk(KERN_WARNING DRIVER_NAME
1406 ": get tuner failed with %d\n", retval);
1407 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001408}
1409
1410
1411/*
1412 * si470x_vidioc_s_tuner - set tuner attributes
1413 */
1414static int si470x_vidioc_s_tuner(struct file *file, void *priv,
1415 struct v4l2_tuner *tuner)
1416{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001417 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenz33632d42008-09-24 19:30:26 -03001418 int retval = -EINVAL;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001419
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001420 /* safety checks */
1421 if (radio->disconnected) {
1422 retval = -EIO;
1423 goto done;
1424 }
Tobias Lorenz33632d42008-09-24 19:30:26 -03001425 if (tuner->index != 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001426 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001427
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001428 /* mono/stereo selector */
1429 switch (tuner->audmode) {
1430 case V4L2_TUNER_MODE_MONO:
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001431 radio->registers[POWERCFG] |= POWERCFG_MONO; /* force mono */
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001432 break;
1433 case V4L2_TUNER_MODE_STEREO:
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001434 radio->registers[POWERCFG] &= ~POWERCFG_MONO; /* try stereo */
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001435 break;
1436 default:
1437 goto done;
1438 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001439
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001440 retval = si470x_set_register(radio, POWERCFG);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001441
1442done:
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001443 if (retval < 0)
1444 printk(KERN_WARNING DRIVER_NAME
1445 ": set tuner failed with %d\n", retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001446 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001447}
1448
1449
1450/*
1451 * si470x_vidioc_g_frequency - get tuner or modulator radio frequency
1452 */
1453static int si470x_vidioc_g_frequency(struct file *file, void *priv,
1454 struct v4l2_frequency *freq)
1455{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001456 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001457 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001458
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001459 /* safety checks */
1460 if (radio->disconnected) {
1461 retval = -EIO;
1462 goto done;
1463 }
Tobias Lorenz33632d42008-09-24 19:30:26 -03001464 if (freq->tuner != 0) {
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001465 retval = -EINVAL;
1466 goto done;
1467 }
Tobias Lorenz03dea862008-04-22 14:46:11 -03001468
Tobias Lorenz33632d42008-09-24 19:30:26 -03001469 freq->type = V4L2_TUNER_RADIO;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001470 retval = si470x_get_freq(radio, &freq->frequency);
1471
1472done:
1473 if (retval < 0)
1474 printk(KERN_WARNING DRIVER_NAME
1475 ": get frequency failed with %d\n", retval);
1476 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001477}
1478
1479
1480/*
1481 * si470x_vidioc_s_frequency - set tuner or modulator radio frequency
1482 */
1483static int si470x_vidioc_s_frequency(struct file *file, void *priv,
1484 struct v4l2_frequency *freq)
1485{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001486 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001487 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001488
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001489 /* safety checks */
1490 if (radio->disconnected) {
1491 retval = -EIO;
1492 goto done;
1493 }
Tobias Lorenz33632d42008-09-24 19:30:26 -03001494 if (freq->tuner != 0) {
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001495 retval = -EINVAL;
1496 goto done;
1497 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001498
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001499 retval = si470x_set_freq(radio, freq->frequency);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001500
1501done:
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001502 if (retval < 0)
1503 printk(KERN_WARNING DRIVER_NAME
1504 ": set frequency failed with %d\n", retval);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001505 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001506}
1507
1508
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001509/*
Tobias Lorenzbbc89952008-05-31 15:11:32 -03001510 * si470x_vidioc_s_hw_freq_seek - set hardware frequency seek
1511 */
1512static int si470x_vidioc_s_hw_freq_seek(struct file *file, void *priv,
1513 struct v4l2_hw_freq_seek *seek)
1514{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001515 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenzbbc89952008-05-31 15:11:32 -03001516 int retval = 0;
1517
1518 /* safety checks */
1519 if (radio->disconnected) {
1520 retval = -EIO;
1521 goto done;
1522 }
Tobias Lorenz33632d42008-09-24 19:30:26 -03001523 if (seek->tuner != 0) {
Tobias Lorenzbbc89952008-05-31 15:11:32 -03001524 retval = -EINVAL;
1525 goto done;
1526 }
1527
1528 retval = si470x_set_seek(radio, seek->wrap_around, seek->seek_upward);
1529
1530done:
1531 if (retval < 0)
1532 printk(KERN_WARNING DRIVER_NAME
1533 ": set hardware frequency seek failed with %d\n",
1534 retval);
1535 return retval;
1536}
1537
Tobias Lorenz374ab682008-09-24 19:17:54 -03001538
1539/*
1540 * si470x_ioctl_ops - video device ioctl operations
1541 */
Hans Verkuila3998102008-07-21 02:57:38 -03001542static const struct v4l2_ioctl_ops si470x_ioctl_ops = {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001543 .vidioc_querycap = si470x_vidioc_querycap,
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001544 .vidioc_queryctrl = si470x_vidioc_queryctrl,
1545 .vidioc_g_ctrl = si470x_vidioc_g_ctrl,
1546 .vidioc_s_ctrl = si470x_vidioc_s_ctrl,
1547 .vidioc_g_audio = si470x_vidioc_g_audio,
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001548 .vidioc_g_tuner = si470x_vidioc_g_tuner,
1549 .vidioc_s_tuner = si470x_vidioc_s_tuner,
1550 .vidioc_g_frequency = si470x_vidioc_g_frequency,
1551 .vidioc_s_frequency = si470x_vidioc_s_frequency,
Tobias Lorenzbbc89952008-05-31 15:11:32 -03001552 .vidioc_s_hw_freq_seek = si470x_vidioc_s_hw_freq_seek,
Hans Verkuila3998102008-07-21 02:57:38 -03001553};
1554
Tobias Lorenz374ab682008-09-24 19:17:54 -03001555
Hans Verkuila3998102008-07-21 02:57:38 -03001556/*
Tobias Lorenz374ab682008-09-24 19:17:54 -03001557 * si470x_viddev_template - video device interface
Hans Verkuila3998102008-07-21 02:57:38 -03001558 */
1559static struct video_device si470x_viddev_template = {
1560 .fops = &si470x_fops,
Hans Verkuila3998102008-07-21 02:57:38 -03001561 .name = DRIVER_NAME,
Hans Verkuila3998102008-07-21 02:57:38 -03001562 .release = video_device_release,
Tobias Lorenz374ab682008-09-24 19:17:54 -03001563 .ioctl_ops = &si470x_ioctl_ops,
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001564};
1565
1566
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001567
1568/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001569 * USB Interface
1570 **************************************************************************/
1571
1572/*
1573 * si470x_usb_driver_probe - probe for the device
1574 */
1575static int si470x_usb_driver_probe(struct usb_interface *intf,
1576 const struct usb_device_id *id)
1577{
1578 struct si470x_device *radio;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001579 int retval = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001580
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001581 /* private data allocation and initialization */
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001582 radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001583 if (!radio) {
1584 retval = -ENOMEM;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001585 goto err_initial;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001586 }
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001587 radio->users = 0;
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001588 radio->disconnected = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001589 radio->usbdev = interface_to_usbdev(intf);
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001590 radio->intf = intf;
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001591 mutex_init(&radio->disconnect_lock);
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001592 mutex_init(&radio->lock);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001593
1594 /* video device allocation and initialization */
1595 radio->videodev = video_device_alloc();
1596 if (!radio->videodev) {
1597 retval = -ENOMEM;
1598 goto err_radio;
1599 }
1600 memcpy(radio->videodev, &si470x_viddev_template,
1601 sizeof(si470x_viddev_template));
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001602 video_set_drvdata(radio->videodev, radio);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001603
1604 /* show some infos about the specific device */
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001605 if (si470x_get_all_registers(radio) < 0) {
1606 retval = -EIO;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001607 goto err_all;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001608 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001609 printk(KERN_INFO DRIVER_NAME ": DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001610 radio->registers[DEVICEID], radio->registers[CHIPID]);
1611
1612 /* check if firmware is current */
1613 if ((radio->registers[CHIPID] & CHIPID_FIRMWARE)
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001614 < RADIO_SW_VERSION_CURRENT) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001615 printk(KERN_WARNING DRIVER_NAME
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001616 ": This driver is known to work with "
1617 "firmware version %hu,\n", RADIO_SW_VERSION_CURRENT);
1618 printk(KERN_WARNING DRIVER_NAME
1619 ": but the device has firmware version %hu.\n",
1620 radio->registers[CHIPID] & CHIPID_FIRMWARE);
1621 printk(KERN_WARNING DRIVER_NAME
1622 ": If you have some trouble using this driver,\n");
1623 printk(KERN_WARNING DRIVER_NAME
1624 ": please report to V4L ML at "
1625 "video4linux-list@redhat.com\n");
1626 }
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001627
1628 /* set initial frequency */
1629 si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
1630
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001631 /* rds buffer allocation */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001632 radio->buf_size = rds_buf * 3;
1633 radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001634 if (!radio->buffer) {
1635 retval = -EIO;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001636 goto err_all;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001637 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001638
1639 /* rds buffer configuration */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001640 radio->wr_index = 0;
1641 radio->rd_index = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001642 init_waitqueue_head(&radio->read_queue);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001643
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001644 /* prepare rds work function */
1645 INIT_DELAYED_WORK(&radio->work, si470x_work);
1646
1647 /* register video device */
Hans Verkuilcba99ae2008-09-03 17:11:58 -03001648 retval = video_register_device(radio->videodev, VFL_TYPE_RADIO, radio_nr);
1649 if (retval) {
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001650 printk(KERN_WARNING DRIVER_NAME
1651 ": Could not register video device\n");
1652 goto err_all;
1653 }
1654 usb_set_intfdata(intf, radio);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001655
1656 return 0;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001657err_all:
1658 video_device_release(radio->videodev);
1659 kfree(radio->buffer);
1660err_radio:
1661 kfree(radio);
1662err_initial:
1663 return retval;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001664}
1665
1666
1667/*
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001668 * si470x_usb_driver_suspend - suspend the device
1669 */
1670static int si470x_usb_driver_suspend(struct usb_interface *intf,
1671 pm_message_t message)
1672{
1673 struct si470x_device *radio = usb_get_intfdata(intf);
1674
1675 printk(KERN_INFO DRIVER_NAME ": suspending now...\n");
1676
1677 cancel_delayed_work_sync(&radio->work);
1678
1679 return 0;
1680}
1681
1682
1683/*
1684 * si470x_usb_driver_resume - resume the device
1685 */
1686static int si470x_usb_driver_resume(struct usb_interface *intf)
1687{
1688 struct si470x_device *radio = usb_get_intfdata(intf);
1689
1690 printk(KERN_INFO DRIVER_NAME ": resuming now...\n");
1691
1692 mutex_lock(&radio->lock);
1693 if (radio->users && radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS)
1694 schedule_delayed_work(&radio->work,
1695 msecs_to_jiffies(rds_poll_time));
1696 mutex_unlock(&radio->lock);
1697
1698 return 0;
1699}
1700
1701
1702/*
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001703 * si470x_usb_driver_disconnect - disconnect the device
1704 */
1705static void si470x_usb_driver_disconnect(struct usb_interface *intf)
1706{
1707 struct si470x_device *radio = usb_get_intfdata(intf);
1708
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001709 mutex_lock(&radio->disconnect_lock);
1710 radio->disconnected = 1;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001711 cancel_delayed_work_sync(&radio->work);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001712 usb_set_intfdata(intf, NULL);
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001713 if (radio->users == 0) {
1714 video_unregister_device(radio->videodev);
1715 kfree(radio->buffer);
1716 kfree(radio);
1717 }
1718 mutex_unlock(&radio->disconnect_lock);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001719}
1720
1721
1722/*
1723 * si470x_usb_driver - usb driver interface
1724 */
1725static struct usb_driver si470x_usb_driver = {
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001726 .name = DRIVER_NAME,
1727 .probe = si470x_usb_driver_probe,
1728 .disconnect = si470x_usb_driver_disconnect,
1729 .suspend = si470x_usb_driver_suspend,
1730 .resume = si470x_usb_driver_resume,
1731 .id_table = si470x_usb_driver_id_table,
1732 .supports_autosuspend = 1,
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001733};
1734
1735
1736
1737/**************************************************************************
1738 * Module Interface
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001739 **************************************************************************/
1740
1741/*
1742 * si470x_module_init - module init
1743 */
1744static int __init si470x_module_init(void)
1745{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001746 printk(KERN_INFO DRIVER_DESC ", Version " DRIVER_VERSION "\n");
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001747 return usb_register(&si470x_usb_driver);
1748}
1749
1750
1751/*
1752 * si470x_module_exit - module exit
1753 */
1754static void __exit si470x_module_exit(void)
1755{
1756 usb_deregister(&si470x_usb_driver);
1757}
1758
1759
1760module_init(si470x_module_init);
1761module_exit(si470x_module_exit);
1762
1763MODULE_LICENSE("GPL");
1764MODULE_AUTHOR(DRIVER_AUTHOR);
1765MODULE_DESCRIPTION(DRIVER_DESC);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001766MODULE_VERSION(DRIVER_VERSION);