blob: 67cbce82cb913a0619f3a72fb8a4e519076cbf96 [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 Lorenz78656ac2008-01-14 21:55:27 -0300101 *
102 * ToDo:
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300103 * - add firmware download/update support
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300104 * - RDS support: interrupt mode, instead of polling
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300105 * - add LED status output (check if that's not already done in firmware)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300106 */
107
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300108
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300109/* driver definitions */
110#define DRIVER_AUTHOR "Tobias Lorenz <tobias.lorenz@gmx.net>"
111#define DRIVER_NAME "radio-si470x"
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300112#define DRIVER_KERNEL_VERSION KERNEL_VERSION(1, 0, 8)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300113#define DRIVER_CARD "Silicon Labs Si470x FM Radio Receiver"
114#define DRIVER_DESC "USB radio driver for Si470x FM Radio Receivers"
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300115#define DRIVER_VERSION "1.0.8"
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300116
117
118/* kernel includes */
119#include <linux/kernel.h>
120#include <linux/module.h>
121#include <linux/init.h>
122#include <linux/slab.h>
123#include <linux/input.h>
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300124#include <linux/usb.h>
125#include <linux/hid.h>
126#include <linux/version.h>
Mauro Carvalho Chehab387d4472008-01-15 11:25:10 -0300127#include <linux/videodev2.h>
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300128#include <linux/mutex.h>
Mauro Carvalho Chehab387d4472008-01-15 11:25:10 -0300129#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300130#include <media/v4l2-ioctl.h>
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300131#include <media/rds.h>
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300132#include <asm/unaligned.h>
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300133
134
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300135/* USB Device ID List */
136static struct usb_device_id si470x_usb_driver_id_table[] = {
137 /* Silicon Labs USB FM Radio Reference Design */
Tobias Lorenz374ab682008-09-24 19:17:54 -0300138 { USB_DEVICE_AND_INTERFACE_INFO(0x10c4, 0x818a, USB_CLASS_HID, 0, 0) },
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300139 /* ADS/Tech FM Radio Receiver (formerly Instant FM Music) */
Tobias Lorenz374ab682008-09-24 19:17:54 -0300140 { USB_DEVICE_AND_INTERFACE_INFO(0x06e1, 0xa155, USB_CLASS_HID, 0, 0) },
Alexey Klimov69df96c2008-10-23 09:20:27 -0300141 /* KWorld USB FM Radio SnapMusic Mobile 700 (FM700) */
142 { USB_DEVICE_AND_INTERFACE_INFO(0x1b80, 0xd700, USB_CLASS_HID, 0, 0) },
Mark Lord5e6de7d2008-12-03 15:26:15 -0300143 /* DealExtreme USB Radio */
144 { USB_DEVICE_AND_INTERFACE_INFO(0x10c5, 0x819a, USB_CLASS_HID, 0, 0) },
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300145 /* Terminating entry */
146 { }
147};
148MODULE_DEVICE_TABLE(usb, si470x_usb_driver_id_table);
149
150
151
152/**************************************************************************
153 * Module Parameters
154 **************************************************************************/
155
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300156/* Radio Nr */
157static int radio_nr = -1;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300158module_param(radio_nr, int, 0444);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300159MODULE_PARM_DESC(radio_nr, "Radio Nr");
160
161/* Spacing (kHz) */
162/* 0: 200 kHz (USA, Australia) */
163/* 1: 100 kHz (Europe, Japan) */
164/* 2: 50 kHz */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300165static unsigned short space = 2;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300166module_param(space, ushort, 0444);
167MODULE_PARM_DESC(space, "Spacing: 0=200kHz 1=100kHz *2=50kHz*");
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300168
169/* Bottom of Band (MHz) */
170/* 0: 87.5 - 108 MHz (USA, Europe)*/
171/* 1: 76 - 108 MHz (Japan wide band) */
172/* 2: 76 - 90 MHz (Japan) */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300173static unsigned short band = 1;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300174module_param(band, ushort, 0444);
175MODULE_PARM_DESC(band, "Band: 0=87.5..108MHz *1=76..108MHz* 2=76..90MHz");
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300176
177/* De-emphasis */
178/* 0: 75 us (USA) */
179/* 1: 50 us (Europe, Australia, Japan) */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300180static unsigned short de = 1;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300181module_param(de, ushort, 0444);
182MODULE_PARM_DESC(de, "De-emphasis: 0=75us *1=50us*");
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300183
184/* USB timeout */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300185static unsigned int usb_timeout = 500;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300186module_param(usb_timeout, uint, 0644);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300187MODULE_PARM_DESC(usb_timeout, "USB timeout (ms): *500*");
188
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300189/* Tune timeout */
190static unsigned int tune_timeout = 3000;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300191module_param(tune_timeout, uint, 0644);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300192MODULE_PARM_DESC(tune_timeout, "Tune timeout: *3000*");
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300193
Tobias Lorenzbbc89952008-05-31 15:11:32 -0300194/* Seek timeout */
195static unsigned int seek_timeout = 5000;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300196module_param(seek_timeout, uint, 0644);
Tobias Lorenzbbc89952008-05-31 15:11:32 -0300197MODULE_PARM_DESC(seek_timeout, "Seek timeout: *5000*");
198
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300199/* RDS buffer blocks */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300200static unsigned int rds_buf = 100;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300201module_param(rds_buf, uint, 0444);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300202MODULE_PARM_DESC(rds_buf, "RDS buffer entries: *100*");
203
204/* RDS maximum block errors */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300205static unsigned short max_rds_errors = 1;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300206/* 0 means 0 errors requiring correction */
207/* 1 means 1-2 errors requiring correction (used by original USBRadio.exe) */
208/* 2 means 3-5 errors requiring correction */
209/* 3 means 6+ errors or errors in checkword, correction not possible */
Tobias Lorenz374ab682008-09-24 19:17:54 -0300210module_param(max_rds_errors, ushort, 0644);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300211MODULE_PARM_DESC(max_rds_errors, "RDS maximum block errors: *1*");
212
213/* RDS poll frequency */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300214static unsigned int rds_poll_time = 40;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300215/* 40 is used by the original USBRadio.exe */
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300216/* 50 is used by radio-cadet */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300217/* 75 should be okay */
218/* 80 is the usual RDS receive interval */
Tobias Lorenz374ab682008-09-24 19:17:54 -0300219module_param(rds_poll_time, uint, 0644);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300220MODULE_PARM_DESC(rds_poll_time, "RDS poll time (ms): *40*");
221
222
223
224/**************************************************************************
225 * Register Definitions
226 **************************************************************************/
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300227#define RADIO_REGISTER_SIZE 2 /* 16 register bit width */
228#define RADIO_REGISTER_NUM 16 /* DEVICEID ... RDSD */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300229#define RDS_REGISTER_NUM 6 /* STATUSRSSI ... RDSD */
230
231#define DEVICEID 0 /* Device ID */
232#define DEVICEID_PN 0xf000 /* bits 15..12: Part Number */
233#define DEVICEID_MFGID 0x0fff /* bits 11..00: Manufacturer ID */
234
235#define CHIPID 1 /* Chip ID */
236#define CHIPID_REV 0xfc00 /* bits 15..10: Chip Version */
237#define CHIPID_DEV 0x0200 /* bits 09..09: Device */
238#define CHIPID_FIRMWARE 0x01ff /* bits 08..00: Firmware Version */
239
240#define POWERCFG 2 /* Power Configuration */
241#define POWERCFG_DSMUTE 0x8000 /* bits 15..15: Softmute Disable */
242#define POWERCFG_DMUTE 0x4000 /* bits 14..14: Mute Disable */
243#define POWERCFG_MONO 0x2000 /* bits 13..13: Mono Select */
244#define POWERCFG_RDSM 0x0800 /* bits 11..11: RDS Mode (Si4701 only) */
245#define POWERCFG_SKMODE 0x0400 /* bits 10..10: Seek Mode */
246#define POWERCFG_SEEKUP 0x0200 /* bits 09..09: Seek Direction */
247#define POWERCFG_SEEK 0x0100 /* bits 08..08: Seek */
248#define POWERCFG_DISABLE 0x0040 /* bits 06..06: Powerup Disable */
249#define POWERCFG_ENABLE 0x0001 /* bits 00..00: Powerup Enable */
250
251#define CHANNEL 3 /* Channel */
252#define CHANNEL_TUNE 0x8000 /* bits 15..15: Tune */
253#define CHANNEL_CHAN 0x03ff /* bits 09..00: Channel Select */
254
255#define SYSCONFIG1 4 /* System Configuration 1 */
256#define SYSCONFIG1_RDSIEN 0x8000 /* bits 15..15: RDS Interrupt Enable (Si4701 only) */
257#define SYSCONFIG1_STCIEN 0x4000 /* bits 14..14: Seek/Tune Complete Interrupt Enable */
258#define SYSCONFIG1_RDS 0x1000 /* bits 12..12: RDS Enable (Si4701 only) */
259#define SYSCONFIG1_DE 0x0800 /* bits 11..11: De-emphasis (0=75us 1=50us) */
260#define SYSCONFIG1_AGCD 0x0400 /* bits 10..10: AGC Disable */
261#define SYSCONFIG1_BLNDADJ 0x00c0 /* bits 07..06: Stereo/Mono Blend Level Adjustment */
262#define SYSCONFIG1_GPIO3 0x0030 /* bits 05..04: General Purpose I/O 3 */
263#define SYSCONFIG1_GPIO2 0x000c /* bits 03..02: General Purpose I/O 2 */
264#define SYSCONFIG1_GPIO1 0x0003 /* bits 01..00: General Purpose I/O 1 */
265
266#define SYSCONFIG2 5 /* System Configuration 2 */
267#define SYSCONFIG2_SEEKTH 0xff00 /* bits 15..08: RSSI Seek Threshold */
268#define SYSCONFIG2_BAND 0x0080 /* bits 07..06: Band Select */
269#define SYSCONFIG2_SPACE 0x0030 /* bits 05..04: Channel Spacing */
270#define SYSCONFIG2_VOLUME 0x000f /* bits 03..00: Volume */
271
272#define SYSCONFIG3 6 /* System Configuration 3 */
273#define SYSCONFIG3_SMUTER 0xc000 /* bits 15..14: Softmute Attack/Recover Rate */
274#define SYSCONFIG3_SMUTEA 0x3000 /* bits 13..12: Softmute Attenuation */
275#define SYSCONFIG3_SKSNR 0x00f0 /* bits 07..04: Seek SNR Threshold */
276#define SYSCONFIG3_SKCNT 0x000f /* bits 03..00: Seek FM Impulse Detection Threshold */
277
278#define TEST1 7 /* Test 1 */
279#define TEST1_AHIZEN 0x4000 /* bits 14..14: Audio High-Z Enable */
280
281#define TEST2 8 /* Test 2 */
282/* TEST2 only contains reserved bits */
283
284#define BOOTCONFIG 9 /* Boot Configuration */
285/* BOOTCONFIG only contains reserved bits */
286
287#define STATUSRSSI 10 /* Status RSSI */
288#define STATUSRSSI_RDSR 0x8000 /* bits 15..15: RDS Ready (Si4701 only) */
289#define STATUSRSSI_STC 0x4000 /* bits 14..14: Seek/Tune Complete */
290#define STATUSRSSI_SF 0x2000 /* bits 13..13: Seek Fail/Band Limit */
291#define STATUSRSSI_AFCRL 0x1000 /* bits 12..12: AFC Rail */
292#define STATUSRSSI_RDSS 0x0800 /* bits 11..11: RDS Synchronized (Si4701 only) */
293#define STATUSRSSI_BLERA 0x0600 /* bits 10..09: RDS Block A Errors (Si4701 only) */
294#define STATUSRSSI_ST 0x0100 /* bits 08..08: Stereo Indicator */
295#define STATUSRSSI_RSSI 0x00ff /* bits 07..00: RSSI (Received Signal Strength Indicator) */
296
297#define READCHAN 11 /* Read Channel */
298#define READCHAN_BLERB 0xc000 /* bits 15..14: RDS Block D Errors (Si4701 only) */
299#define READCHAN_BLERC 0x3000 /* bits 13..12: RDS Block C Errors (Si4701 only) */
300#define READCHAN_BLERD 0x0c00 /* bits 11..10: RDS Block B Errors (Si4701 only) */
301#define READCHAN_READCHAN 0x03ff /* bits 09..00: Read Channel */
302
303#define RDSA 12 /* RDSA */
304#define RDSA_RDSA 0xffff /* bits 15..00: RDS Block A Data (Si4701 only) */
305
306#define RDSB 13 /* RDSB */
307#define RDSB_RDSB 0xffff /* bits 15..00: RDS Block B Data (Si4701 only) */
308
309#define RDSC 14 /* RDSC */
310#define RDSC_RDSC 0xffff /* bits 15..00: RDS Block C Data (Si4701 only) */
311
312#define RDSD 15 /* RDSD */
313#define RDSD_RDSD 0xffff /* bits 15..00: RDS Block D Data (Si4701 only) */
314
315
316
317/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300318 * USB HID Reports
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300319 **************************************************************************/
320
321/* Reports 1-16 give direct read/write access to the 16 Si470x registers */
322/* with the (REPORT_ID - 1) corresponding to the register address across USB */
323/* endpoint 0 using GET_REPORT and SET_REPORT */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300324#define REGISTER_REPORT_SIZE (RADIO_REGISTER_SIZE + 1)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300325#define REGISTER_REPORT(reg) ((reg) + 1)
326
327/* Report 17 gives direct read/write access to the entire Si470x register */
328/* map across endpoint 0 using GET_REPORT and SET_REPORT */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300329#define ENTIRE_REPORT_SIZE (RADIO_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300330#define ENTIRE_REPORT 17
331
332/* Report 18 is used to send the lowest 6 Si470x registers up the HID */
333/* interrupt endpoint 1 to Windows every 20 milliseconds for status */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300334#define RDS_REPORT_SIZE (RDS_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300335#define RDS_REPORT 18
336
337/* Report 19: LED state */
338#define LED_REPORT_SIZE 3
339#define LED_REPORT 19
340
341/* Report 19: stream */
342#define STREAM_REPORT_SIZE 3
343#define STREAM_REPORT 19
344
345/* Report 20: scratch */
346#define SCRATCH_PAGE_SIZE 63
347#define SCRATCH_REPORT_SIZE (SCRATCH_PAGE_SIZE + 1)
348#define SCRATCH_REPORT 20
349
350/* Reports 19-22: flash upgrade of the C8051F321 */
351#define WRITE_REPORT 19
352#define FLASH_REPORT 20
353#define CRC_REPORT 21
354#define RESPONSE_REPORT 22
355
356/* Report 23: currently unused, but can accept 60 byte reports on the HID */
357/* interrupt out endpoint 2 every 1 millisecond */
358#define UNUSED_REPORT 23
359
360
361
362/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300363 * Software/Hardware Versions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300364 **************************************************************************/
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300365#define RADIO_SW_VERSION_NOT_BOOTLOADABLE 6
366#define RADIO_SW_VERSION 7
367#define RADIO_SW_VERSION_CURRENT 15
368#define RADIO_HW_VERSION 1
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300369
370#define SCRATCH_PAGE_SW_VERSION 1
371#define SCRATCH_PAGE_HW_VERSION 2
372
373
374
375/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300376 * LED State Definitions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300377 **************************************************************************/
378#define LED_COMMAND 0x35
379
380#define NO_CHANGE_LED 0x00
381#define ALL_COLOR_LED 0x01 /* streaming state */
382#define BLINK_GREEN_LED 0x02 /* connect state */
383#define BLINK_RED_LED 0x04
384#define BLINK_ORANGE_LED 0x10 /* disconnect state */
385#define SOLID_GREEN_LED 0x20 /* tuning/seeking state */
386#define SOLID_RED_LED 0x40 /* bootload state */
387#define SOLID_ORANGE_LED 0x80
388
389
390
391/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300392 * Stream State Definitions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300393 **************************************************************************/
394#define STREAM_COMMAND 0x36
395#define STREAM_VIDPID 0x00
396#define STREAM_AUDIO 0xff
397
398
399
400/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300401 * Bootloader / Flash Commands
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300402 **************************************************************************/
403
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300404/* unique id sent to bootloader and required to put into a bootload state */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300405#define UNIQUE_BL_ID 0x34
406
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300407/* mask for the flash data */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300408#define FLASH_DATA_MASK 0x55
409
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300410/* bootloader commands */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300411#define GET_SW_VERSION_COMMAND 0x00
412#define SET_PAGE_COMMAND 0x01
413#define ERASE_PAGE_COMMAND 0x02
414#define WRITE_PAGE_COMMAND 0x03
415#define CRC_ON_PAGE_COMMAND 0x04
416#define READ_FLASH_BYTE_COMMAND 0x05
417#define RESET_DEVICE_COMMAND 0x06
418#define GET_HW_VERSION_COMMAND 0x07
419#define BLANK 0xff
420
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300421/* bootloader command responses */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300422#define COMMAND_OK 0x01
423#define COMMAND_FAILED 0x02
424#define COMMAND_PENDING 0x03
425
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300426/* buffer sizes */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300427#define COMMAND_BUFFER_SIZE 4
428#define RESPONSE_BUFFER_SIZE 2
429#define FLASH_BUFFER_SIZE 64
430#define CRC_BUFFER_SIZE 3
431
432
433
434/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300435 * General Driver Definitions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300436 **************************************************************************/
437
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300438/*
439 * si470x_device - private data
440 */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300441struct si470x_device {
442 /* reference to USB and video device */
443 struct usb_device *usbdev;
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300444 struct usb_interface *intf;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300445 struct video_device *videodev;
446
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300447 /* driver management */
448 unsigned int users;
Tobias Lorenzce5829e2008-05-31 15:04:32 -0300449 unsigned char disconnected;
450 struct mutex disconnect_lock;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300451
452 /* Silabs internal registers (0..15) */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300453 unsigned short registers[RADIO_REGISTER_NUM];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300454
455 /* RDS receive buffer */
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300456 struct delayed_work work;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300457 wait_queue_head_t read_queue;
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300458 struct mutex lock; /* buffer locking */
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300459 unsigned char *buffer; /* size is always multiple of three */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300460 unsigned int buf_size;
461 unsigned int rd_index;
462 unsigned int wr_index;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300463};
464
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300465
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300466/*
467 * The frequency is set in units of 62.5 Hz when using V4L2_TUNER_CAP_LOW,
468 * 62.5 kHz otherwise.
469 * The tuner is able to have a channel spacing of 50, 100 or 200 kHz.
470 * tuner->capability is therefore set to V4L2_TUNER_CAP_LOW
471 * The FREQ_MUL is then: 1 MHz / 62.5 Hz = 16000
472 */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300473#define FREQ_MUL (1000000 / 62.5)
474
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300475
476
477/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300478 * General Driver Functions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300479 **************************************************************************/
480
481/*
482 * si470x_get_report - receive a HID report
483 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300484static int si470x_get_report(struct si470x_device *radio, void *buf, int size)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300485{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300486 unsigned char *report = (unsigned char *) buf;
487 int retval;
488
489 retval = usb_control_msg(radio->usbdev,
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300490 usb_rcvctrlpipe(radio->usbdev, 0),
491 HID_REQ_GET_REPORT,
492 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300493 report[0], 2,
494 buf, size, usb_timeout);
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300495
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300496 if (retval < 0)
497 printk(KERN_WARNING DRIVER_NAME
498 ": si470x_get_report: usb_control_msg returned %d\n",
499 retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300500 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300501}
502
503
504/*
505 * si470x_set_report - send a HID report
506 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300507static int si470x_set_report(struct si470x_device *radio, void *buf, int size)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300508{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300509 unsigned char *report = (unsigned char *) buf;
510 int retval;
511
512 retval = usb_control_msg(radio->usbdev,
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300513 usb_sndctrlpipe(radio->usbdev, 0),
514 HID_REQ_SET_REPORT,
515 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300516 report[0], 2,
517 buf, size, usb_timeout);
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300518
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300519 if (retval < 0)
520 printk(KERN_WARNING DRIVER_NAME
521 ": si470x_set_report: usb_control_msg returned %d\n",
522 retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300523 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300524}
525
526
527/*
528 * si470x_get_register - read register
529 */
530static int si470x_get_register(struct si470x_device *radio, int regnr)
531{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300532 unsigned char buf[REGISTER_REPORT_SIZE];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300533 int retval;
534
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300535 buf[0] = REGISTER_REPORT(regnr);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300536
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300537 retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
538
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300539 if (retval >= 0)
Harvey Harrison5ad6b812008-04-15 22:22:11 -0300540 radio->registers[regnr] = get_unaligned_be16(&buf[1]);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300541
542 return (retval < 0) ? -EINVAL : 0;
543}
544
545
546/*
547 * si470x_set_register - write register
548 */
549static int si470x_set_register(struct si470x_device *radio, int regnr)
550{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300551 unsigned char buf[REGISTER_REPORT_SIZE];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300552 int retval;
553
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300554 buf[0] = REGISTER_REPORT(regnr);
Harvey Harrison5ad6b812008-04-15 22:22:11 -0300555 put_unaligned_be16(radio->registers[regnr], &buf[1]);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300556
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300557 retval = si470x_set_report(radio, (void *) &buf, sizeof(buf));
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300558
559 return (retval < 0) ? -EINVAL : 0;
560}
561
562
563/*
564 * si470x_get_all_registers - read entire registers
565 */
566static int si470x_get_all_registers(struct si470x_device *radio)
567{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300568 unsigned char buf[ENTIRE_REPORT_SIZE];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300569 int retval;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300570 unsigned char regnr;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300571
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300572 buf[0] = ENTIRE_REPORT;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300573
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300574 retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300575
576 if (retval >= 0)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300577 for (regnr = 0; regnr < RADIO_REGISTER_NUM; regnr++)
Harvey Harrison5ad6b812008-04-15 22:22:11 -0300578 radio->registers[regnr] = get_unaligned_be16(
579 &buf[regnr * RADIO_REGISTER_SIZE + 1]);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300580
581 return (retval < 0) ? -EINVAL : 0;
582}
583
584
585/*
586 * si470x_get_rds_registers - read rds registers
587 */
588static int si470x_get_rds_registers(struct si470x_device *radio)
589{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300590 unsigned char buf[RDS_REPORT_SIZE];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300591 int retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300592 int size;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300593 unsigned char regnr;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300594
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300595 buf[0] = RDS_REPORT;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300596
597 retval = usb_interrupt_msg(radio->usbdev,
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300598 usb_rcvintpipe(radio->usbdev, 1),
599 (void *) &buf, sizeof(buf), &size, usb_timeout);
600 if (size != sizeof(buf))
Tobias Lorenzce5829e2008-05-31 15:04:32 -0300601 printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: "
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300602 "return size differs: %d != %zu\n", size, sizeof(buf));
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300603 if (retval < 0)
604 printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: "
605 "usb_interrupt_msg returned %d\n", retval);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300606
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300607 if (retval >= 0)
608 for (regnr = 0; regnr < RDS_REGISTER_NUM; regnr++)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300609 radio->registers[STATUSRSSI + regnr] =
Tobias Lorenzce5829e2008-05-31 15:04:32 -0300610 get_unaligned_be16(
611 &buf[regnr * RADIO_REGISTER_SIZE + 1]);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300612
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300613 return (retval < 0) ? -EINVAL : 0;
614}
615
616
617/*
618 * si470x_set_chan - set the channel
619 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300620static int si470x_set_chan(struct si470x_device *radio, unsigned short chan)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300621{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300622 int retval;
623 unsigned long timeout;
624 bool timed_out = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300625
626 /* start tuning */
627 radio->registers[CHANNEL] &= ~CHANNEL_CHAN;
628 radio->registers[CHANNEL] |= CHANNEL_TUNE | chan;
629 retval = si470x_set_register(radio, CHANNEL);
630 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300631 goto done;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300632
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300633 /* wait till tune operation has completed */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300634 timeout = jiffies + msecs_to_jiffies(tune_timeout);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300635 do {
636 retval = si470x_get_register(radio, STATUSRSSI);
637 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300638 goto stop;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300639 timed_out = time_after(jiffies, timeout);
640 } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
641 (!timed_out));
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300642 if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
643 printk(KERN_WARNING DRIVER_NAME ": tune does not complete\n");
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300644 if (timed_out)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300645 printk(KERN_WARNING DRIVER_NAME
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300646 ": tune timed out after %u ms\n", tune_timeout);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300647
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300648stop:
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300649 /* stop tuning */
650 radio->registers[CHANNEL] &= ~CHANNEL_TUNE;
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300651 retval = si470x_set_register(radio, CHANNEL);
652
653done:
654 return retval;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300655}
656
657
658/*
659 * si470x_get_freq - get the frequency
660 */
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300661static int si470x_get_freq(struct si470x_device *radio, unsigned int *freq)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300662{
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300663 unsigned int spacing, band_bottom;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300664 unsigned short chan;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300665 int retval;
666
667 /* Spacing (kHz) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300668 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300669 /* 0: 200 kHz (USA, Australia) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300670 case 0:
671 spacing = 0.200 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300672 /* 1: 100 kHz (Europe, Japan) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300673 case 1:
674 spacing = 0.100 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300675 /* 2: 50 kHz */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300676 default:
677 spacing = 0.050 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300678 };
679
680 /* Bottom of Band (MHz) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300681 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300682 /* 0: 87.5 - 108 MHz (USA, Europe) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300683 case 0:
684 band_bottom = 87.5 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300685 /* 1: 76 - 108 MHz (Japan wide band) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300686 default:
687 band_bottom = 76 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300688 /* 2: 76 - 90 MHz (Japan) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300689 case 2:
690 band_bottom = 76 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300691 };
692
693 /* read channel */
694 retval = si470x_get_register(radio, READCHAN);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300695 chan = radio->registers[READCHAN] & READCHAN_READCHAN;
696
697 /* Frequency (MHz) = Spacing (kHz) x Channel + Bottom of Band (MHz) */
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300698 *freq = chan * spacing + band_bottom;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300699
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300700 return retval;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300701}
702
703
704/*
705 * si470x_set_freq - set the frequency
706 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300707static int si470x_set_freq(struct si470x_device *radio, unsigned int freq)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300708{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300709 unsigned int spacing, band_bottom;
710 unsigned short chan;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300711
712 /* Spacing (kHz) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300713 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300714 /* 0: 200 kHz (USA, Australia) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300715 case 0:
716 spacing = 0.200 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300717 /* 1: 100 kHz (Europe, Japan) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300718 case 1:
719 spacing = 0.100 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300720 /* 2: 50 kHz */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300721 default:
722 spacing = 0.050 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300723 };
724
725 /* Bottom of Band (MHz) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300726 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300727 /* 0: 87.5 - 108 MHz (USA, Europe) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300728 case 0:
729 band_bottom = 87.5 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300730 /* 1: 76 - 108 MHz (Japan wide band) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300731 default:
732 band_bottom = 76 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300733 /* 2: 76 - 90 MHz (Japan) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300734 case 2:
735 band_bottom = 76 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300736 };
737
738 /* Chan = [ Freq (Mhz) - Bottom of Band (MHz) ] / Spacing (kHz) */
739 chan = (freq - band_bottom) / spacing;
740
741 return si470x_set_chan(radio, chan);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300742}
743
744
745/*
Tobias Lorenzbbc89952008-05-31 15:11:32 -0300746 * si470x_set_seek - set seek
747 */
748static int si470x_set_seek(struct si470x_device *radio,
749 unsigned int wrap_around, unsigned int seek_upward)
750{
751 int retval = 0;
752 unsigned long timeout;
753 bool timed_out = 0;
754
755 /* start seeking */
756 radio->registers[POWERCFG] |= POWERCFG_SEEK;
757 if (wrap_around == 1)
758 radio->registers[POWERCFG] &= ~POWERCFG_SKMODE;
759 else
760 radio->registers[POWERCFG] |= POWERCFG_SKMODE;
761 if (seek_upward == 1)
762 radio->registers[POWERCFG] |= POWERCFG_SEEKUP;
763 else
764 radio->registers[POWERCFG] &= ~POWERCFG_SEEKUP;
765 retval = si470x_set_register(radio, POWERCFG);
766 if (retval < 0)
767 goto done;
768
769 /* wait till seek operation has completed */
770 timeout = jiffies + msecs_to_jiffies(seek_timeout);
771 do {
772 retval = si470x_get_register(radio, STATUSRSSI);
773 if (retval < 0)
774 goto stop;
775 timed_out = time_after(jiffies, timeout);
776 } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
777 (!timed_out));
778 if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
779 printk(KERN_WARNING DRIVER_NAME ": seek does not complete\n");
780 if (radio->registers[STATUSRSSI] & STATUSRSSI_SF)
781 printk(KERN_WARNING DRIVER_NAME
782 ": seek failed / band limit reached\n");
783 if (timed_out)
784 printk(KERN_WARNING DRIVER_NAME
785 ": seek timed out after %u ms\n", seek_timeout);
786
787stop:
788 /* stop seeking */
789 radio->registers[POWERCFG] &= ~POWERCFG_SEEK;
790 retval = si470x_set_register(radio, POWERCFG);
791
792done:
793 /* try again, if timed out */
794 if ((retval == 0) && timed_out)
795 retval = -EAGAIN;
796
797 return retval;
798}
799
800
801/*
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300802 * si470x_start - switch on radio
803 */
804static int si470x_start(struct si470x_device *radio)
805{
806 int retval;
807
808 /* powercfg */
809 radio->registers[POWERCFG] =
810 POWERCFG_DMUTE | POWERCFG_ENABLE | POWERCFG_RDSM;
811 retval = si470x_set_register(radio, POWERCFG);
812 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300813 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300814
815 /* sysconfig 1 */
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300816 radio->registers[SYSCONFIG1] = SYSCONFIG1_DE;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300817 retval = si470x_set_register(radio, SYSCONFIG1);
818 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300819 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300820
821 /* sysconfig 2 */
822 radio->registers[SYSCONFIG2] =
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300823 (0x3f << 8) | /* SEEKTH */
824 ((band << 6) & SYSCONFIG2_BAND) | /* BAND */
825 ((space << 4) & SYSCONFIG2_SPACE) | /* SPACE */
826 15; /* VOLUME (max) */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300827 retval = si470x_set_register(radio, SYSCONFIG2);
828 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300829 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300830
831 /* reset last channel */
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300832 retval = si470x_set_chan(radio,
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300833 radio->registers[CHANNEL] & CHANNEL_CHAN);
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300834
835done:
836 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300837}
838
839
840/*
841 * si470x_stop - switch off radio
842 */
843static int si470x_stop(struct si470x_device *radio)
844{
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300845 int retval;
846
847 /* sysconfig 1 */
848 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
849 retval = si470x_set_register(radio, SYSCONFIG1);
850 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300851 goto done;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300852
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300853 /* powercfg */
854 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
855 /* POWERCFG_ENABLE has to automatically go low */
856 radio->registers[POWERCFG] |= POWERCFG_ENABLE | POWERCFG_DISABLE;
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300857 retval = si470x_set_register(radio, POWERCFG);
858
859done:
860 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300861}
862
863
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300864/*
865 * si470x_rds_on - switch on rds reception
866 */
867static int si470x_rds_on(struct si470x_device *radio)
868{
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300869 int retval;
870
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300871 /* sysconfig 1 */
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300872 mutex_lock(&radio->lock);
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300873 radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDS;
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300874 retval = si470x_set_register(radio, SYSCONFIG1);
875 if (retval < 0)
876 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
877 mutex_unlock(&radio->lock);
878
879 return retval;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300880}
881
882
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300883
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300884/**************************************************************************
885 * RDS Driver Functions
886 **************************************************************************/
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300887
888/*
889 * si470x_rds - rds processing function
890 */
891static void si470x_rds(struct si470x_device *radio)
892{
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300893 unsigned char blocknum;
894 unsigned short bler; /* rds block errors */
895 unsigned short rds;
896 unsigned char tmpbuf[3];
897
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300898 /* get rds blocks */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300899 if (si470x_get_rds_registers(radio) < 0)
900 return;
901 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0) {
902 /* No RDS group ready */
903 return;
904 }
905 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSS) == 0) {
906 /* RDS decoder not synchronized */
907 return;
908 }
909
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300910 /* copy all four RDS blocks to internal buffer */
911 mutex_lock(&radio->lock);
912 for (blocknum = 0; blocknum < 4; blocknum++) {
913 switch (blocknum) {
914 default:
915 bler = (radio->registers[STATUSRSSI] &
916 STATUSRSSI_BLERA) >> 9;
917 rds = radio->registers[RDSA];
918 break;
919 case 1:
920 bler = (radio->registers[READCHAN] &
921 READCHAN_BLERB) >> 14;
922 rds = radio->registers[RDSB];
923 break;
924 case 2:
925 bler = (radio->registers[READCHAN] &
926 READCHAN_BLERC) >> 12;
927 rds = radio->registers[RDSC];
928 break;
929 case 3:
930 bler = (radio->registers[READCHAN] &
931 READCHAN_BLERD) >> 10;
932 rds = radio->registers[RDSD];
933 break;
934 };
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300935
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300936 /* Fill the V4L2 RDS buffer */
Harvey Harrison5ad6b812008-04-15 22:22:11 -0300937 put_unaligned_le16(rds, &tmpbuf);
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300938 tmpbuf[2] = blocknum; /* offset name */
939 tmpbuf[2] |= blocknum << 3; /* received offset */
940 if (bler > max_rds_errors)
941 tmpbuf[2] |= 0x80; /* uncorrectable errors */
942 else if (bler > 0)
943 tmpbuf[2] |= 0x40; /* corrected error(s) */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300944
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300945 /* copy RDS block to internal buffer */
946 memcpy(&radio->buffer[radio->wr_index], &tmpbuf, 3);
947 radio->wr_index += 3;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300948
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300949 /* wrap write pointer */
950 if (radio->wr_index >= radio->buf_size)
951 radio->wr_index = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300952
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300953 /* check for overflow */
954 if (radio->wr_index == radio->rd_index) {
955 /* increment and wrap read pointer */
956 radio->rd_index += 3;
957 if (radio->rd_index >= radio->buf_size)
958 radio->rd_index = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300959 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300960 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300961 mutex_unlock(&radio->lock);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300962
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300963 /* wake up read queue */
964 if (radio->wr_index != radio->rd_index)
965 wake_up_interruptible(&radio->read_queue);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300966}
967
968
969/*
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300970 * si470x_work - rds work function
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300971 */
972static void si470x_work(struct work_struct *work)
973{
974 struct si470x_device *radio = container_of(work, struct si470x_device,
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300975 work.work);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300976
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300977 /* safety checks */
Tobias Lorenzce5829e2008-05-31 15:04:32 -0300978 if (radio->disconnected)
979 return;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300980 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300981 return;
982
983 si470x_rds(radio);
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300984 schedule_delayed_work(&radio->work, msecs_to_jiffies(rds_poll_time));
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300985}
986
987
988
989/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300990 * File Operations Interface
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300991 **************************************************************************/
992
993/*
994 * si470x_fops_read - read RDS data
995 */
996static ssize_t si470x_fops_read(struct file *file, char __user *buf,
997 size_t count, loff_t *ppos)
998{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300999 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001000 int retval = 0;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001001 unsigned int block_count = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001002
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001003 /* switch on rds reception */
1004 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
1005 si470x_rds_on(radio);
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001006 schedule_delayed_work(&radio->work,
1007 msecs_to_jiffies(rds_poll_time));
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001008 }
1009
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001010 /* block if no new data available */
1011 while (radio->wr_index == radio->rd_index) {
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001012 if (file->f_flags & O_NONBLOCK) {
1013 retval = -EWOULDBLOCK;
1014 goto done;
1015 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001016 if (wait_event_interruptible(radio->read_queue,
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001017 radio->wr_index != radio->rd_index) < 0) {
1018 retval = -EINTR;
1019 goto done;
1020 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001021 }
1022
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001023 /* calculate block count from byte count */
1024 count /= 3;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001025
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001026 /* copy RDS block out of internal buffer and to user buffer */
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001027 mutex_lock(&radio->lock);
1028 while (block_count < count) {
1029 if (radio->rd_index == radio->wr_index)
1030 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001031
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001032 /* always transfer rds complete blocks */
1033 if (copy_to_user(buf, &radio->buffer[radio->rd_index], 3))
1034 /* retval = -EFAULT; */
1035 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001036
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001037 /* increment and wrap read pointer */
1038 radio->rd_index += 3;
1039 if (radio->rd_index >= radio->buf_size)
1040 radio->rd_index = 0;
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001041
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001042 /* increment counters */
1043 block_count++;
1044 buf += 3;
1045 retval += 3;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001046 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001047 mutex_unlock(&radio->lock);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001048
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001049done:
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001050 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001051}
1052
1053
1054/*
1055 * si470x_fops_poll - poll RDS data
1056 */
1057static unsigned int si470x_fops_poll(struct file *file,
1058 struct poll_table_struct *pts)
1059{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001060 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001061 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001062
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001063 /* switch on rds reception */
1064 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
1065 si470x_rds_on(radio);
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001066 schedule_delayed_work(&radio->work,
1067 msecs_to_jiffies(rds_poll_time));
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001068 }
1069
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001070 poll_wait(file, &radio->read_queue, pts);
1071
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001072 if (radio->rd_index != radio->wr_index)
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001073 retval = POLLIN | POLLRDNORM;
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001074
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001075 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001076}
1077
1078
1079/*
1080 * si470x_fops_open - file open
1081 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001082static int si470x_fops_open(struct file *file)
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001083{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001084 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001085 int retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001086
Hans Verkuild56dc612008-07-30 08:43:36 -03001087 lock_kernel();
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001088 radio->users++;
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001089
1090 retval = usb_autopm_get_interface(radio->intf);
1091 if (retval < 0) {
1092 radio->users--;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001093 retval = -EIO;
1094 goto done;
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001095 }
1096
1097 if (radio->users == 1) {
1098 retval = si470x_start(radio);
1099 if (retval < 0)
1100 usb_autopm_put_interface(radio->intf);
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001101 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001102
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001103done:
Hans Verkuild56dc612008-07-30 08:43:36 -03001104 unlock_kernel();
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001105 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001106}
1107
1108
1109/*
1110 * si470x_fops_release - file release
1111 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001112static int si470x_fops_release(struct file *file)
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001113{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001114 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001115 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001116
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001117 /* safety check */
1118 if (!radio) {
1119 retval = -ENODEV;
1120 goto done;
1121 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001122
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001123 mutex_lock(&radio->disconnect_lock);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001124 radio->users--;
1125 if (radio->users == 0) {
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001126 if (radio->disconnected) {
1127 video_unregister_device(radio->videodev);
1128 kfree(radio->buffer);
1129 kfree(radio);
1130 goto unlock;
1131 }
Tobias Lorenz03dea862008-04-22 14:46:11 -03001132
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001133 /* stop rds reception */
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001134 cancel_delayed_work_sync(&radio->work);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001135
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001136 /* cancel read processes */
1137 wake_up_interruptible(&radio->read_queue);
1138
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001139 retval = si470x_stop(radio);
1140 usb_autopm_put_interface(radio->intf);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001141 }
1142
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001143unlock:
1144 mutex_unlock(&radio->disconnect_lock);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001145
1146done:
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001147 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001148}
1149
1150
1151/*
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001152 * si470x_fops - file operations interface
1153 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001154static const struct v4l2_file_operations si470x_fops = {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001155 .owner = THIS_MODULE,
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001156 .read = si470x_fops_read,
1157 .poll = si470x_fops_poll,
1158 .ioctl = video_ioctl2,
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001159 .open = si470x_fops_open,
1160 .release = si470x_fops_release,
1161};
1162
1163
1164
1165/**************************************************************************
1166 * Video4Linux Interface
1167 **************************************************************************/
1168
1169/*
1170 * si470x_v4l2_queryctrl - query control
1171 */
1172static struct v4l2_queryctrl si470x_v4l2_queryctrl[] = {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001173 {
1174 .id = V4L2_CID_AUDIO_VOLUME,
1175 .type = V4L2_CTRL_TYPE_INTEGER,
1176 .name = "Volume",
1177 .minimum = 0,
1178 .maximum = 15,
1179 .step = 1,
1180 .default_value = 15,
1181 },
1182 {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001183 .id = V4L2_CID_AUDIO_MUTE,
1184 .type = V4L2_CTRL_TYPE_BOOLEAN,
1185 .name = "Mute",
1186 .minimum = 0,
1187 .maximum = 1,
1188 .step = 1,
1189 .default_value = 1,
1190 },
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001191};
1192
1193
1194/*
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001195 * si470x_vidioc_querycap - query device capabilities
1196 */
1197static int si470x_vidioc_querycap(struct file *file, void *priv,
1198 struct v4l2_capability *capability)
1199{
1200 strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver));
1201 strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
1202 sprintf(capability->bus_info, "USB");
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001203 capability->version = DRIVER_KERNEL_VERSION;
Tobias Lorenzbbc89952008-05-31 15:11:32 -03001204 capability->capabilities = V4L2_CAP_HW_FREQ_SEEK |
1205 V4L2_CAP_TUNER | V4L2_CAP_RADIO;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001206
1207 return 0;
1208}
1209
1210
1211/*
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001212 * si470x_vidioc_queryctrl - enumerate control items
1213 */
1214static int si470x_vidioc_queryctrl(struct file *file, void *priv,
1215 struct v4l2_queryctrl *qc)
1216{
Tobias Lorenzeb6e3b82008-09-24 19:25:39 -03001217 unsigned char i = 0;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001218 int retval = -EINVAL;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001219
Tobias Lorenzeb6e3b82008-09-24 19:25:39 -03001220 /* abort if qc->id is below V4L2_CID_BASE */
1221 if (qc->id < V4L2_CID_BASE)
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001222 goto done;
1223
Tobias Lorenzeb6e3b82008-09-24 19:25:39 -03001224 /* search video control */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001225 for (i = 0; i < ARRAY_SIZE(si470x_v4l2_queryctrl); i++) {
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001226 if (qc->id == si470x_v4l2_queryctrl[i].id) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001227 memcpy(qc, &(si470x_v4l2_queryctrl[i]), sizeof(*qc));
Tobias Lorenzeb6e3b82008-09-24 19:25:39 -03001228 retval = 0; /* found */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001229 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001230 }
1231 }
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001232
Tobias Lorenzeb6e3b82008-09-24 19:25:39 -03001233 /* disable unsupported base controls */
1234 /* to satisfy kradio and such apps */
1235 if ((retval == -EINVAL) && (qc->id < V4L2_CID_LASTP1)) {
1236 qc->flags = V4L2_CTRL_FLAG_DISABLED;
1237 retval = 0;
1238 }
1239
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001240done:
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001241 if (retval < 0)
1242 printk(KERN_WARNING DRIVER_NAME
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001243 ": query controls failed with %d\n", retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001244 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001245}
1246
1247
1248/*
1249 * si470x_vidioc_g_ctrl - get the value of a control
1250 */
1251static int si470x_vidioc_g_ctrl(struct file *file, void *priv,
1252 struct v4l2_control *ctrl)
1253{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001254 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001255 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001256
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001257 /* safety checks */
1258 if (radio->disconnected) {
1259 retval = -EIO;
1260 goto done;
1261 }
Tobias Lorenz03dea862008-04-22 14:46:11 -03001262
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001263 switch (ctrl->id) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001264 case V4L2_CID_AUDIO_VOLUME:
1265 ctrl->value = radio->registers[SYSCONFIG2] &
1266 SYSCONFIG2_VOLUME;
1267 break;
1268 case V4L2_CID_AUDIO_MUTE:
1269 ctrl->value = ((radio->registers[POWERCFG] &
1270 POWERCFG_DMUTE) == 0) ? 1 : 0;
1271 break;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001272 default:
1273 retval = -EINVAL;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001274 }
1275
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001276done:
1277 if (retval < 0)
1278 printk(KERN_WARNING DRIVER_NAME
1279 ": get control failed with %d\n", retval);
1280 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001281}
1282
1283
1284/*
1285 * si470x_vidioc_s_ctrl - set the value of a control
1286 */
1287static int si470x_vidioc_s_ctrl(struct file *file, void *priv,
1288 struct v4l2_control *ctrl)
1289{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001290 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001291 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001292
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001293 /* safety checks */
1294 if (radio->disconnected) {
1295 retval = -EIO;
1296 goto done;
1297 }
Tobias Lorenz03dea862008-04-22 14:46:11 -03001298
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001299 switch (ctrl->id) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001300 case V4L2_CID_AUDIO_VOLUME:
1301 radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_VOLUME;
1302 radio->registers[SYSCONFIG2] |= ctrl->value;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001303 retval = si470x_set_register(radio, SYSCONFIG2);
1304 break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001305 case V4L2_CID_AUDIO_MUTE:
1306 if (ctrl->value == 1)
1307 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
1308 else
1309 radio->registers[POWERCFG] |= POWERCFG_DMUTE;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001310 retval = si470x_set_register(radio, POWERCFG);
1311 break;
1312 default:
1313 retval = -EINVAL;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001314 }
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001315
1316done:
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001317 if (retval < 0)
1318 printk(KERN_WARNING DRIVER_NAME
1319 ": set control failed with %d\n", retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001320 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001321}
1322
1323
1324/*
1325 * si470x_vidioc_g_audio - get audio attributes
1326 */
1327static int si470x_vidioc_g_audio(struct file *file, void *priv,
1328 struct v4l2_audio *audio)
1329{
Tobias Lorenz617f5332008-09-24 19:33:09 -03001330 /* driver constants */
1331 audio->index = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001332 strcpy(audio->name, "Radio");
1333 audio->capability = V4L2_AUDCAP_STEREO;
Tobias Lorenz617f5332008-09-24 19:33:09 -03001334 audio->mode = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001335
Tobias Lorenz617f5332008-09-24 19:33:09 -03001336 return 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001337}
1338
1339
1340/*
1341 * si470x_vidioc_g_tuner - get tuner attributes
1342 */
1343static int si470x_vidioc_g_tuner(struct file *file, void *priv,
1344 struct v4l2_tuner *tuner)
1345{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001346 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001347 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001348
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001349 /* safety checks */
1350 if (radio->disconnected) {
1351 retval = -EIO;
1352 goto done;
1353 }
Tobias Lorenz33632d42008-09-24 19:30:26 -03001354 if (tuner->index != 0) {
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001355 retval = -EINVAL;
1356 goto done;
1357 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001358
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001359 retval = si470x_get_register(radio, STATUSRSSI);
1360 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001361 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001362
Tobias Lorenz33632d42008-09-24 19:30:26 -03001363 /* driver constants */
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001364 strcpy(tuner->name, "FM");
Tobias Lorenz33632d42008-09-24 19:30:26 -03001365 tuner->type = V4L2_TUNER_RADIO;
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001366 tuner->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
Tobias Lorenz33632d42008-09-24 19:30:26 -03001367
Tobias Lorenza17c0012008-09-24 19:21:50 -03001368 /* range limits */
1369 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001370 /* 0: 87.5 - 108 MHz (USA, Europe, default) */
1371 default:
1372 tuner->rangelow = 87.5 * FREQ_MUL;
1373 tuner->rangehigh = 108 * FREQ_MUL;
1374 break;
1375 /* 1: 76 - 108 MHz (Japan wide band) */
1376 case 1 :
1377 tuner->rangelow = 76 * FREQ_MUL;
1378 tuner->rangehigh = 108 * FREQ_MUL;
1379 break;
1380 /* 2: 76 - 90 MHz (Japan) */
1381 case 2 :
1382 tuner->rangelow = 76 * FREQ_MUL;
1383 tuner->rangehigh = 90 * FREQ_MUL;
1384 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001385 };
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001386
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001387 /* stereo indicator == stereo (instead of mono) */
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001388 if ((radio->registers[STATUSRSSI] & STATUSRSSI_ST) == 1)
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001389 tuner->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001390 else
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001391 tuner->rxsubchans = V4L2_TUNER_SUB_MONO;
1392
1393 /* mono/stereo selector */
1394 if ((radio->registers[POWERCFG] & POWERCFG_MONO) == 1)
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001395 tuner->audmode = V4L2_TUNER_MODE_MONO;
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001396 else
1397 tuner->audmode = V4L2_TUNER_MODE_STEREO;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001398
1399 /* min is worst, max is best; signal:0..0xffff; rssi: 0..0xff */
1400 tuner->signal = (radio->registers[STATUSRSSI] & STATUSRSSI_RSSI)
1401 * 0x0101;
1402
1403 /* automatic frequency control: -1: freq to low, 1 freq to high */
Tobias Lorenzace7d4b2008-05-31 15:09:07 -03001404 /* AFCRL does only indicate that freq. differs, not if too low/high */
1405 tuner->afc = (radio->registers[STATUSRSSI] & STATUSRSSI_AFCRL) ? 1 : 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001406
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001407done:
1408 if (retval < 0)
1409 printk(KERN_WARNING DRIVER_NAME
1410 ": get tuner failed with %d\n", retval);
1411 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001412}
1413
1414
1415/*
1416 * si470x_vidioc_s_tuner - set tuner attributes
1417 */
1418static int si470x_vidioc_s_tuner(struct file *file, void *priv,
1419 struct v4l2_tuner *tuner)
1420{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001421 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenz33632d42008-09-24 19:30:26 -03001422 int retval = -EINVAL;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001423
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001424 /* safety checks */
1425 if (radio->disconnected) {
1426 retval = -EIO;
1427 goto done;
1428 }
Tobias Lorenz33632d42008-09-24 19:30:26 -03001429 if (tuner->index != 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001430 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001431
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001432 /* mono/stereo selector */
1433 switch (tuner->audmode) {
1434 case V4L2_TUNER_MODE_MONO:
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001435 radio->registers[POWERCFG] |= POWERCFG_MONO; /* force mono */
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001436 break;
1437 case V4L2_TUNER_MODE_STEREO:
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001438 radio->registers[POWERCFG] &= ~POWERCFG_MONO; /* try stereo */
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001439 break;
1440 default:
1441 goto done;
1442 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001443
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001444 retval = si470x_set_register(radio, POWERCFG);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001445
1446done:
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001447 if (retval < 0)
1448 printk(KERN_WARNING DRIVER_NAME
1449 ": set tuner failed with %d\n", retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001450 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001451}
1452
1453
1454/*
1455 * si470x_vidioc_g_frequency - get tuner or modulator radio frequency
1456 */
1457static int si470x_vidioc_g_frequency(struct file *file, void *priv,
1458 struct v4l2_frequency *freq)
1459{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001460 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001461 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001462
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001463 /* safety checks */
1464 if (radio->disconnected) {
1465 retval = -EIO;
1466 goto done;
1467 }
Tobias Lorenz33632d42008-09-24 19:30:26 -03001468 if (freq->tuner != 0) {
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001469 retval = -EINVAL;
1470 goto done;
1471 }
Tobias Lorenz03dea862008-04-22 14:46:11 -03001472
Tobias Lorenz33632d42008-09-24 19:30:26 -03001473 freq->type = V4L2_TUNER_RADIO;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001474 retval = si470x_get_freq(radio, &freq->frequency);
1475
1476done:
1477 if (retval < 0)
1478 printk(KERN_WARNING DRIVER_NAME
1479 ": get frequency failed with %d\n", retval);
1480 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001481}
1482
1483
1484/*
1485 * si470x_vidioc_s_frequency - set tuner or modulator radio frequency
1486 */
1487static int si470x_vidioc_s_frequency(struct file *file, void *priv,
1488 struct v4l2_frequency *freq)
1489{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001490 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001491 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001492
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001493 /* safety checks */
1494 if (radio->disconnected) {
1495 retval = -EIO;
1496 goto done;
1497 }
Tobias Lorenz33632d42008-09-24 19:30:26 -03001498 if (freq->tuner != 0) {
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001499 retval = -EINVAL;
1500 goto done;
1501 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001502
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001503 retval = si470x_set_freq(radio, freq->frequency);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001504
1505done:
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001506 if (retval < 0)
1507 printk(KERN_WARNING DRIVER_NAME
1508 ": set frequency failed with %d\n", retval);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001509 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001510}
1511
1512
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001513/*
Tobias Lorenzbbc89952008-05-31 15:11:32 -03001514 * si470x_vidioc_s_hw_freq_seek - set hardware frequency seek
1515 */
1516static int si470x_vidioc_s_hw_freq_seek(struct file *file, void *priv,
1517 struct v4l2_hw_freq_seek *seek)
1518{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001519 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenzbbc89952008-05-31 15:11:32 -03001520 int retval = 0;
1521
1522 /* safety checks */
1523 if (radio->disconnected) {
1524 retval = -EIO;
1525 goto done;
1526 }
Tobias Lorenz33632d42008-09-24 19:30:26 -03001527 if (seek->tuner != 0) {
Tobias Lorenzbbc89952008-05-31 15:11:32 -03001528 retval = -EINVAL;
1529 goto done;
1530 }
1531
1532 retval = si470x_set_seek(radio, seek->wrap_around, seek->seek_upward);
1533
1534done:
1535 if (retval < 0)
1536 printk(KERN_WARNING DRIVER_NAME
1537 ": set hardware frequency seek failed with %d\n",
1538 retval);
1539 return retval;
1540}
1541
Tobias Lorenz374ab682008-09-24 19:17:54 -03001542
1543/*
1544 * si470x_ioctl_ops - video device ioctl operations
1545 */
Hans Verkuila3998102008-07-21 02:57:38 -03001546static const struct v4l2_ioctl_ops si470x_ioctl_ops = {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001547 .vidioc_querycap = si470x_vidioc_querycap,
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001548 .vidioc_queryctrl = si470x_vidioc_queryctrl,
1549 .vidioc_g_ctrl = si470x_vidioc_g_ctrl,
1550 .vidioc_s_ctrl = si470x_vidioc_s_ctrl,
1551 .vidioc_g_audio = si470x_vidioc_g_audio,
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001552 .vidioc_g_tuner = si470x_vidioc_g_tuner,
1553 .vidioc_s_tuner = si470x_vidioc_s_tuner,
1554 .vidioc_g_frequency = si470x_vidioc_g_frequency,
1555 .vidioc_s_frequency = si470x_vidioc_s_frequency,
Tobias Lorenzbbc89952008-05-31 15:11:32 -03001556 .vidioc_s_hw_freq_seek = si470x_vidioc_s_hw_freq_seek,
Hans Verkuila3998102008-07-21 02:57:38 -03001557};
1558
Tobias Lorenz374ab682008-09-24 19:17:54 -03001559
Hans Verkuila3998102008-07-21 02:57:38 -03001560/*
Tobias Lorenz374ab682008-09-24 19:17:54 -03001561 * si470x_viddev_template - video device interface
Hans Verkuila3998102008-07-21 02:57:38 -03001562 */
1563static struct video_device si470x_viddev_template = {
1564 .fops = &si470x_fops,
Hans Verkuila3998102008-07-21 02:57:38 -03001565 .name = DRIVER_NAME,
Hans Verkuila3998102008-07-21 02:57:38 -03001566 .release = video_device_release,
Tobias Lorenz374ab682008-09-24 19:17:54 -03001567 .ioctl_ops = &si470x_ioctl_ops,
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001568};
1569
1570
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001571
1572/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001573 * USB Interface
1574 **************************************************************************/
1575
1576/*
1577 * si470x_usb_driver_probe - probe for the device
1578 */
1579static int si470x_usb_driver_probe(struct usb_interface *intf,
1580 const struct usb_device_id *id)
1581{
1582 struct si470x_device *radio;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001583 int retval = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001584
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001585 /* private data allocation and initialization */
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001586 radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001587 if (!radio) {
1588 retval = -ENOMEM;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001589 goto err_initial;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001590 }
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001591 radio->users = 0;
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001592 radio->disconnected = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001593 radio->usbdev = interface_to_usbdev(intf);
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001594 radio->intf = intf;
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001595 mutex_init(&radio->disconnect_lock);
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001596 mutex_init(&radio->lock);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001597
1598 /* video device allocation and initialization */
1599 radio->videodev = video_device_alloc();
1600 if (!radio->videodev) {
1601 retval = -ENOMEM;
1602 goto err_radio;
1603 }
1604 memcpy(radio->videodev, &si470x_viddev_template,
1605 sizeof(si470x_viddev_template));
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001606 video_set_drvdata(radio->videodev, radio);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001607
1608 /* show some infos about the specific device */
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001609 if (si470x_get_all_registers(radio) < 0) {
1610 retval = -EIO;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001611 goto err_all;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001612 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001613 printk(KERN_INFO DRIVER_NAME ": DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001614 radio->registers[DEVICEID], radio->registers[CHIPID]);
1615
1616 /* check if firmware is current */
1617 if ((radio->registers[CHIPID] & CHIPID_FIRMWARE)
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001618 < RADIO_SW_VERSION_CURRENT) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001619 printk(KERN_WARNING DRIVER_NAME
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001620 ": This driver is known to work with "
1621 "firmware version %hu,\n", RADIO_SW_VERSION_CURRENT);
1622 printk(KERN_WARNING DRIVER_NAME
1623 ": but the device has firmware version %hu.\n",
1624 radio->registers[CHIPID] & CHIPID_FIRMWARE);
1625 printk(KERN_WARNING DRIVER_NAME
1626 ": If you have some trouble using this driver,\n");
1627 printk(KERN_WARNING DRIVER_NAME
1628 ": please report to V4L ML at "
1629 "video4linux-list@redhat.com\n");
1630 }
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001631
1632 /* set initial frequency */
1633 si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
1634
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001635 /* rds buffer allocation */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001636 radio->buf_size = rds_buf * 3;
1637 radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001638 if (!radio->buffer) {
1639 retval = -EIO;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001640 goto err_all;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001641 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001642
1643 /* rds buffer configuration */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001644 radio->wr_index = 0;
1645 radio->rd_index = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001646 init_waitqueue_head(&radio->read_queue);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001647
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001648 /* prepare rds work function */
1649 INIT_DELAYED_WORK(&radio->work, si470x_work);
1650
1651 /* register video device */
Hans Verkuilcba99ae2008-09-03 17:11:58 -03001652 retval = video_register_device(radio->videodev, VFL_TYPE_RADIO, radio_nr);
1653 if (retval) {
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001654 printk(KERN_WARNING DRIVER_NAME
1655 ": Could not register video device\n");
1656 goto err_all;
1657 }
1658 usb_set_intfdata(intf, radio);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001659
1660 return 0;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001661err_all:
1662 video_device_release(radio->videodev);
1663 kfree(radio->buffer);
1664err_radio:
1665 kfree(radio);
1666err_initial:
1667 return retval;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001668}
1669
1670
1671/*
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001672 * si470x_usb_driver_suspend - suspend the device
1673 */
1674static int si470x_usb_driver_suspend(struct usb_interface *intf,
1675 pm_message_t message)
1676{
1677 struct si470x_device *radio = usb_get_intfdata(intf);
1678
1679 printk(KERN_INFO DRIVER_NAME ": suspending now...\n");
1680
1681 cancel_delayed_work_sync(&radio->work);
1682
1683 return 0;
1684}
1685
1686
1687/*
1688 * si470x_usb_driver_resume - resume the device
1689 */
1690static int si470x_usb_driver_resume(struct usb_interface *intf)
1691{
1692 struct si470x_device *radio = usb_get_intfdata(intf);
1693
1694 printk(KERN_INFO DRIVER_NAME ": resuming now...\n");
1695
1696 mutex_lock(&radio->lock);
1697 if (radio->users && radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS)
1698 schedule_delayed_work(&radio->work,
1699 msecs_to_jiffies(rds_poll_time));
1700 mutex_unlock(&radio->lock);
1701
1702 return 0;
1703}
1704
1705
1706/*
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001707 * si470x_usb_driver_disconnect - disconnect the device
1708 */
1709static void si470x_usb_driver_disconnect(struct usb_interface *intf)
1710{
1711 struct si470x_device *radio = usb_get_intfdata(intf);
1712
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001713 mutex_lock(&radio->disconnect_lock);
1714 radio->disconnected = 1;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001715 cancel_delayed_work_sync(&radio->work);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001716 usb_set_intfdata(intf, NULL);
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001717 if (radio->users == 0) {
1718 video_unregister_device(radio->videodev);
1719 kfree(radio->buffer);
1720 kfree(radio);
1721 }
1722 mutex_unlock(&radio->disconnect_lock);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001723}
1724
1725
1726/*
1727 * si470x_usb_driver - usb driver interface
1728 */
1729static struct usb_driver si470x_usb_driver = {
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001730 .name = DRIVER_NAME,
1731 .probe = si470x_usb_driver_probe,
1732 .disconnect = si470x_usb_driver_disconnect,
1733 .suspend = si470x_usb_driver_suspend,
1734 .resume = si470x_usb_driver_resume,
1735 .id_table = si470x_usb_driver_id_table,
1736 .supports_autosuspend = 1,
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001737};
1738
1739
1740
1741/**************************************************************************
1742 * Module Interface
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001743 **************************************************************************/
1744
1745/*
1746 * si470x_module_init - module init
1747 */
1748static int __init si470x_module_init(void)
1749{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001750 printk(KERN_INFO DRIVER_DESC ", Version " DRIVER_VERSION "\n");
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001751 return usb_register(&si470x_usb_driver);
1752}
1753
1754
1755/*
1756 * si470x_module_exit - module exit
1757 */
1758static void __exit si470x_module_exit(void)
1759{
1760 usb_deregister(&si470x_usb_driver);
1761}
1762
1763
1764module_init(si470x_module_init);
1765module_exit(si470x_module_exit);
1766
1767MODULE_LICENSE("GPL");
1768MODULE_AUTHOR(DRIVER_AUTHOR);
1769MODULE_DESCRIPTION(DRIVER_DESC);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001770MODULE_VERSION(DRIVER_VERSION);