blob: 67d74cecdb67c6679c8269ffc4d82be5b7768d43 [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)
7 *
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03008 * Copyright (c) 2008 Tobias Lorenz <tobias.lorenz@gmx.net>
Tobias Lorenz78656ac2008-01-14 21:55:27 -03009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24
25
26/*
Tobias Lorenz6cc72652008-05-31 15:06:50 -030027 * User Notes:
28 * - USB Audio is provided by the alsa snd_usb_audio module.
29 * For listing you have to redirect the sound, for example using:
30 * arecord -D hw:1,0 -r96000 -c2 -f S16_LE | artsdsp aplay -B -
31 * - regarding module parameters in /sys/module/radio_si470x/parameters:
32 * the contents of read-only files (0444) are not updated, even if
33 * space, band and de are changed using private video controls
34 * - increase tune_timeout, if you often get -EIO errors
35 * - hw_freq_seek returns -EAGAIN, when timed out or band limit is reached
36 */
37
38
39/*
Tobias Lorenz78656ac2008-01-14 21:55:27 -030040 * History:
41 * 2008-01-12 Tobias Lorenz <tobias.lorenz@gmx.net>
42 * Version 1.0.0
43 * - First working version
44 * 2008-01-13 Tobias Lorenz <tobias.lorenz@gmx.net>
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -030045 * Version 1.0.1
Tobias Lorenz78656ac2008-01-14 21:55:27 -030046 * - Improved error handling, every function now returns errno
47 * - Improved multi user access (start/mute/stop)
48 * - Channel doesn't get lost anymore after start/mute/stop
49 * - RDS support added (polling mode via interrupt EP 1)
50 * - marked default module parameters with *value*
51 * - switched from bit structs to bit masks
52 * - header file cleaned and integrated
53 * 2008-01-14 Tobias Lorenz <tobias.lorenz@gmx.net>
54 * Version 1.0.2
55 * - hex values are now lower case
56 * - commented USB ID for ADS/Tech moved on todo list
57 * - blacklisted si470x in hid-quirks.c
58 * - rds buffer handling functions integrated into *_work, *_read
59 * - rds_command in si470x_poll exchanged against simple retval
60 * - check for firmware version 15
61 * - code order and prototypes still remain the same
62 * - spacing and bottom of band codes remain the same
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -030063 * 2008-01-16 Tobias Lorenz <tobias.lorenz@gmx.net>
64 * Version 1.0.3
65 * - code reordered to avoid function prototypes
66 * - switch/case defaults are now more user-friendly
67 * - unified comment style
68 * - applied all checkpatch.pl v1.12 suggestions
69 * except the warning about the too long lines with bit comments
70 * - renamed FMRADIO to RADIO to cut line length (checkpatch.pl)
Tobias Lorenz2fb88402008-01-25 05:14:57 -030071 * 2008-01-22 Tobias Lorenz <tobias.lorenz@gmx.net>
72 * Version 1.0.4
73 * - avoid poss. locking when doing copy_to_user which may sleep
74 * - RDS is automatically activated on read now
75 * - code cleaned of unnecessary rds_commands
76 * - USB Vendor/Product ID for ADS/Tech FM Radio Receiver verified
77 * (thanks to Guillaume RAMOUSSE)
Tobias Lorenz0e3301e2008-01-27 14:54:07 -030078 * 2008-01-27 Tobias Lorenz <tobias.lorenz@gmx.net>
79 * Version 1.0.5
80 * - number of seek_retries changed to tune_timeout
81 * - fixed problem with incomplete tune operations by own buffers
Tobias Lorenz5caf5132008-02-04 22:26:08 -030082 * - optimization of variables and printf types
Tobias Lorenz0e3301e2008-01-27 14:54:07 -030083 * - improved error logging
Tobias Lorenz5caf5132008-02-04 22:26:08 -030084 * 2008-01-31 Tobias Lorenz <tobias.lorenz@gmx.net>
85 * Oliver Neukum <oliver@neukum.org>
86 * Version 1.0.6
87 * - fixed coverity checker warnings in *_usb_driver_disconnect
88 * - probe()/open() race by correct ordering in probe()
89 * - DMA coherency rules by separate allocation of all buffers
90 * - use of endianness macros
91 * - abuse of spinlock, replaced by mutex
92 * - racy handling of timer in disconnect,
93 * replaced by delayed_work
94 * - racy interruptible_sleep_on(),
95 * replaced with wait_event_interruptible()
96 * - handle signals in read()
Tobias Lorenz57566ad2008-02-09 16:08:24 -030097 * 2008-02-08 Tobias Lorenz <tobias.lorenz@gmx.net>
98 * Oliver Neukum <oliver@neukum.org>
99 * Version 1.0.7
100 * - usb autosuspend support
Tobias Lorenzce5829e2008-05-31 15:04:32 -0300101 * - unplugging fixed
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300102 * 2008-05-07 Tobias Lorenz <tobias.lorenz@gmx.net>
103 * Version 1.0.8
Tobias Lorenzbbc89952008-05-31 15:11:32 -0300104 * - hardware frequency seek support
Tobias Lorenzace7d4b2008-05-31 15:09:07 -0300105 * - afc indication
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300106 * - more safety checks, let si470x_get_freq return errno
Tobias Lorenz33632d42008-09-24 19:30:26 -0300107 * - vidioc behavior corrected according to v4l2 spec
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300108 *
109 * ToDo:
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300110 * - add firmware download/update support
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300111 * - RDS support: interrupt mode, instead of polling
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300112 * - add LED status output (check if that's not already done in firmware)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300113 */
114
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300115
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300116/* driver definitions */
117#define DRIVER_AUTHOR "Tobias Lorenz <tobias.lorenz@gmx.net>"
118#define DRIVER_NAME "radio-si470x"
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300119#define DRIVER_KERNEL_VERSION KERNEL_VERSION(1, 0, 8)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300120#define DRIVER_CARD "Silicon Labs Si470x FM Radio Receiver"
121#define DRIVER_DESC "USB radio driver for Si470x FM Radio Receivers"
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300122#define DRIVER_VERSION "1.0.8"
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300123
124
125/* kernel includes */
126#include <linux/kernel.h>
127#include <linux/module.h>
128#include <linux/init.h>
129#include <linux/slab.h>
130#include <linux/input.h>
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300131#include <linux/usb.h>
132#include <linux/hid.h>
133#include <linux/version.h>
Mauro Carvalho Chehab387d4472008-01-15 11:25:10 -0300134#include <linux/videodev2.h>
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300135#include <linux/mutex.h>
Mauro Carvalho Chehab387d4472008-01-15 11:25:10 -0300136#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300137#include <media/v4l2-ioctl.h>
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300138#include <media/rds.h>
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300139#include <asm/unaligned.h>
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300140
141
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300142/* USB Device ID List */
143static struct usb_device_id si470x_usb_driver_id_table[] = {
144 /* Silicon Labs USB FM Radio Reference Design */
Tobias Lorenz374ab682008-09-24 19:17:54 -0300145 { USB_DEVICE_AND_INTERFACE_INFO(0x10c4, 0x818a, USB_CLASS_HID, 0, 0) },
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300146 /* ADS/Tech FM Radio Receiver (formerly Instant FM Music) */
Tobias Lorenz374ab682008-09-24 19:17:54 -0300147 { USB_DEVICE_AND_INTERFACE_INFO(0x06e1, 0xa155, USB_CLASS_HID, 0, 0) },
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300148 /* Terminating entry */
149 { }
150};
151MODULE_DEVICE_TABLE(usb, si470x_usb_driver_id_table);
152
153
154
155/**************************************************************************
156 * Module Parameters
157 **************************************************************************/
158
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300159/* Radio Nr */
160static int radio_nr = -1;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300161module_param(radio_nr, int, 0444);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300162MODULE_PARM_DESC(radio_nr, "Radio Nr");
163
164/* Spacing (kHz) */
165/* 0: 200 kHz (USA, Australia) */
166/* 1: 100 kHz (Europe, Japan) */
167/* 2: 50 kHz */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300168static unsigned short space = 2;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300169module_param(space, ushort, 0444);
170MODULE_PARM_DESC(space, "Spacing: 0=200kHz 1=100kHz *2=50kHz*");
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300171
172/* Bottom of Band (MHz) */
173/* 0: 87.5 - 108 MHz (USA, Europe)*/
174/* 1: 76 - 108 MHz (Japan wide band) */
175/* 2: 76 - 90 MHz (Japan) */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300176static unsigned short band = 1;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300177module_param(band, ushort, 0444);
178MODULE_PARM_DESC(band, "Band: 0=87.5..108MHz *1=76..108MHz* 2=76..90MHz");
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300179
180/* De-emphasis */
181/* 0: 75 us (USA) */
182/* 1: 50 us (Europe, Australia, Japan) */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300183static unsigned short de = 1;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300184module_param(de, ushort, 0444);
185MODULE_PARM_DESC(de, "De-emphasis: 0=75us *1=50us*");
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300186
187/* USB timeout */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300188static unsigned int usb_timeout = 500;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300189module_param(usb_timeout, uint, 0644);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300190MODULE_PARM_DESC(usb_timeout, "USB timeout (ms): *500*");
191
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300192/* Tune timeout */
193static unsigned int tune_timeout = 3000;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300194module_param(tune_timeout, uint, 0644);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300195MODULE_PARM_DESC(tune_timeout, "Tune timeout: *3000*");
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300196
Tobias Lorenzbbc89952008-05-31 15:11:32 -0300197/* Seek timeout */
198static unsigned int seek_timeout = 5000;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300199module_param(seek_timeout, uint, 0644);
Tobias Lorenzbbc89952008-05-31 15:11:32 -0300200MODULE_PARM_DESC(seek_timeout, "Seek timeout: *5000*");
201
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300202/* RDS buffer blocks */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300203static unsigned int rds_buf = 100;
Tobias Lorenz374ab682008-09-24 19:17:54 -0300204module_param(rds_buf, uint, 0444);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300205MODULE_PARM_DESC(rds_buf, "RDS buffer entries: *100*");
206
207/* RDS maximum block errors */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300208static unsigned short max_rds_errors = 1;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300209/* 0 means 0 errors requiring correction */
210/* 1 means 1-2 errors requiring correction (used by original USBRadio.exe) */
211/* 2 means 3-5 errors requiring correction */
212/* 3 means 6+ errors or errors in checkword, correction not possible */
Tobias Lorenz374ab682008-09-24 19:17:54 -0300213module_param(max_rds_errors, ushort, 0644);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300214MODULE_PARM_DESC(max_rds_errors, "RDS maximum block errors: *1*");
215
216/* RDS poll frequency */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300217static unsigned int rds_poll_time = 40;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300218/* 40 is used by the original USBRadio.exe */
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300219/* 50 is used by radio-cadet */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300220/* 75 should be okay */
221/* 80 is the usual RDS receive interval */
Tobias Lorenz374ab682008-09-24 19:17:54 -0300222module_param(rds_poll_time, uint, 0644);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300223MODULE_PARM_DESC(rds_poll_time, "RDS poll time (ms): *40*");
224
225
226
227/**************************************************************************
228 * Register Definitions
229 **************************************************************************/
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300230#define RADIO_REGISTER_SIZE 2 /* 16 register bit width */
231#define RADIO_REGISTER_NUM 16 /* DEVICEID ... RDSD */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300232#define RDS_REGISTER_NUM 6 /* STATUSRSSI ... RDSD */
233
234#define DEVICEID 0 /* Device ID */
235#define DEVICEID_PN 0xf000 /* bits 15..12: Part Number */
236#define DEVICEID_MFGID 0x0fff /* bits 11..00: Manufacturer ID */
237
238#define CHIPID 1 /* Chip ID */
239#define CHIPID_REV 0xfc00 /* bits 15..10: Chip Version */
240#define CHIPID_DEV 0x0200 /* bits 09..09: Device */
241#define CHIPID_FIRMWARE 0x01ff /* bits 08..00: Firmware Version */
242
243#define POWERCFG 2 /* Power Configuration */
244#define POWERCFG_DSMUTE 0x8000 /* bits 15..15: Softmute Disable */
245#define POWERCFG_DMUTE 0x4000 /* bits 14..14: Mute Disable */
246#define POWERCFG_MONO 0x2000 /* bits 13..13: Mono Select */
247#define POWERCFG_RDSM 0x0800 /* bits 11..11: RDS Mode (Si4701 only) */
248#define POWERCFG_SKMODE 0x0400 /* bits 10..10: Seek Mode */
249#define POWERCFG_SEEKUP 0x0200 /* bits 09..09: Seek Direction */
250#define POWERCFG_SEEK 0x0100 /* bits 08..08: Seek */
251#define POWERCFG_DISABLE 0x0040 /* bits 06..06: Powerup Disable */
252#define POWERCFG_ENABLE 0x0001 /* bits 00..00: Powerup Enable */
253
254#define CHANNEL 3 /* Channel */
255#define CHANNEL_TUNE 0x8000 /* bits 15..15: Tune */
256#define CHANNEL_CHAN 0x03ff /* bits 09..00: Channel Select */
257
258#define SYSCONFIG1 4 /* System Configuration 1 */
259#define SYSCONFIG1_RDSIEN 0x8000 /* bits 15..15: RDS Interrupt Enable (Si4701 only) */
260#define SYSCONFIG1_STCIEN 0x4000 /* bits 14..14: Seek/Tune Complete Interrupt Enable */
261#define SYSCONFIG1_RDS 0x1000 /* bits 12..12: RDS Enable (Si4701 only) */
262#define SYSCONFIG1_DE 0x0800 /* bits 11..11: De-emphasis (0=75us 1=50us) */
263#define SYSCONFIG1_AGCD 0x0400 /* bits 10..10: AGC Disable */
264#define SYSCONFIG1_BLNDADJ 0x00c0 /* bits 07..06: Stereo/Mono Blend Level Adjustment */
265#define SYSCONFIG1_GPIO3 0x0030 /* bits 05..04: General Purpose I/O 3 */
266#define SYSCONFIG1_GPIO2 0x000c /* bits 03..02: General Purpose I/O 2 */
267#define SYSCONFIG1_GPIO1 0x0003 /* bits 01..00: General Purpose I/O 1 */
268
269#define SYSCONFIG2 5 /* System Configuration 2 */
270#define SYSCONFIG2_SEEKTH 0xff00 /* bits 15..08: RSSI Seek Threshold */
271#define SYSCONFIG2_BAND 0x0080 /* bits 07..06: Band Select */
272#define SYSCONFIG2_SPACE 0x0030 /* bits 05..04: Channel Spacing */
273#define SYSCONFIG2_VOLUME 0x000f /* bits 03..00: Volume */
274
275#define SYSCONFIG3 6 /* System Configuration 3 */
276#define SYSCONFIG3_SMUTER 0xc000 /* bits 15..14: Softmute Attack/Recover Rate */
277#define SYSCONFIG3_SMUTEA 0x3000 /* bits 13..12: Softmute Attenuation */
278#define SYSCONFIG3_SKSNR 0x00f0 /* bits 07..04: Seek SNR Threshold */
279#define SYSCONFIG3_SKCNT 0x000f /* bits 03..00: Seek FM Impulse Detection Threshold */
280
281#define TEST1 7 /* Test 1 */
282#define TEST1_AHIZEN 0x4000 /* bits 14..14: Audio High-Z Enable */
283
284#define TEST2 8 /* Test 2 */
285/* TEST2 only contains reserved bits */
286
287#define BOOTCONFIG 9 /* Boot Configuration */
288/* BOOTCONFIG only contains reserved bits */
289
290#define STATUSRSSI 10 /* Status RSSI */
291#define STATUSRSSI_RDSR 0x8000 /* bits 15..15: RDS Ready (Si4701 only) */
292#define STATUSRSSI_STC 0x4000 /* bits 14..14: Seek/Tune Complete */
293#define STATUSRSSI_SF 0x2000 /* bits 13..13: Seek Fail/Band Limit */
294#define STATUSRSSI_AFCRL 0x1000 /* bits 12..12: AFC Rail */
295#define STATUSRSSI_RDSS 0x0800 /* bits 11..11: RDS Synchronized (Si4701 only) */
296#define STATUSRSSI_BLERA 0x0600 /* bits 10..09: RDS Block A Errors (Si4701 only) */
297#define STATUSRSSI_ST 0x0100 /* bits 08..08: Stereo Indicator */
298#define STATUSRSSI_RSSI 0x00ff /* bits 07..00: RSSI (Received Signal Strength Indicator) */
299
300#define READCHAN 11 /* Read Channel */
301#define READCHAN_BLERB 0xc000 /* bits 15..14: RDS Block D Errors (Si4701 only) */
302#define READCHAN_BLERC 0x3000 /* bits 13..12: RDS Block C Errors (Si4701 only) */
303#define READCHAN_BLERD 0x0c00 /* bits 11..10: RDS Block B Errors (Si4701 only) */
304#define READCHAN_READCHAN 0x03ff /* bits 09..00: Read Channel */
305
306#define RDSA 12 /* RDSA */
307#define RDSA_RDSA 0xffff /* bits 15..00: RDS Block A Data (Si4701 only) */
308
309#define RDSB 13 /* RDSB */
310#define RDSB_RDSB 0xffff /* bits 15..00: RDS Block B Data (Si4701 only) */
311
312#define RDSC 14 /* RDSC */
313#define RDSC_RDSC 0xffff /* bits 15..00: RDS Block C Data (Si4701 only) */
314
315#define RDSD 15 /* RDSD */
316#define RDSD_RDSD 0xffff /* bits 15..00: RDS Block D Data (Si4701 only) */
317
318
319
320/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300321 * USB HID Reports
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300322 **************************************************************************/
323
324/* Reports 1-16 give direct read/write access to the 16 Si470x registers */
325/* with the (REPORT_ID - 1) corresponding to the register address across USB */
326/* endpoint 0 using GET_REPORT and SET_REPORT */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300327#define REGISTER_REPORT_SIZE (RADIO_REGISTER_SIZE + 1)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300328#define REGISTER_REPORT(reg) ((reg) + 1)
329
330/* Report 17 gives direct read/write access to the entire Si470x register */
331/* map across endpoint 0 using GET_REPORT and SET_REPORT */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300332#define ENTIRE_REPORT_SIZE (RADIO_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300333#define ENTIRE_REPORT 17
334
335/* Report 18 is used to send the lowest 6 Si470x registers up the HID */
336/* interrupt endpoint 1 to Windows every 20 milliseconds for status */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300337#define RDS_REPORT_SIZE (RDS_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300338#define RDS_REPORT 18
339
340/* Report 19: LED state */
341#define LED_REPORT_SIZE 3
342#define LED_REPORT 19
343
344/* Report 19: stream */
345#define STREAM_REPORT_SIZE 3
346#define STREAM_REPORT 19
347
348/* Report 20: scratch */
349#define SCRATCH_PAGE_SIZE 63
350#define SCRATCH_REPORT_SIZE (SCRATCH_PAGE_SIZE + 1)
351#define SCRATCH_REPORT 20
352
353/* Reports 19-22: flash upgrade of the C8051F321 */
354#define WRITE_REPORT 19
355#define FLASH_REPORT 20
356#define CRC_REPORT 21
357#define RESPONSE_REPORT 22
358
359/* Report 23: currently unused, but can accept 60 byte reports on the HID */
360/* interrupt out endpoint 2 every 1 millisecond */
361#define UNUSED_REPORT 23
362
363
364
365/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300366 * Software/Hardware Versions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300367 **************************************************************************/
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300368#define RADIO_SW_VERSION_NOT_BOOTLOADABLE 6
369#define RADIO_SW_VERSION 7
370#define RADIO_SW_VERSION_CURRENT 15
371#define RADIO_HW_VERSION 1
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300372
373#define SCRATCH_PAGE_SW_VERSION 1
374#define SCRATCH_PAGE_HW_VERSION 2
375
376
377
378/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300379 * LED State Definitions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300380 **************************************************************************/
381#define LED_COMMAND 0x35
382
383#define NO_CHANGE_LED 0x00
384#define ALL_COLOR_LED 0x01 /* streaming state */
385#define BLINK_GREEN_LED 0x02 /* connect state */
386#define BLINK_RED_LED 0x04
387#define BLINK_ORANGE_LED 0x10 /* disconnect state */
388#define SOLID_GREEN_LED 0x20 /* tuning/seeking state */
389#define SOLID_RED_LED 0x40 /* bootload state */
390#define SOLID_ORANGE_LED 0x80
391
392
393
394/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300395 * Stream State Definitions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300396 **************************************************************************/
397#define STREAM_COMMAND 0x36
398#define STREAM_VIDPID 0x00
399#define STREAM_AUDIO 0xff
400
401
402
403/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300404 * Bootloader / Flash Commands
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300405 **************************************************************************/
406
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300407/* unique id sent to bootloader and required to put into a bootload state */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300408#define UNIQUE_BL_ID 0x34
409
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300410/* mask for the flash data */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300411#define FLASH_DATA_MASK 0x55
412
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300413/* bootloader commands */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300414#define GET_SW_VERSION_COMMAND 0x00
415#define SET_PAGE_COMMAND 0x01
416#define ERASE_PAGE_COMMAND 0x02
417#define WRITE_PAGE_COMMAND 0x03
418#define CRC_ON_PAGE_COMMAND 0x04
419#define READ_FLASH_BYTE_COMMAND 0x05
420#define RESET_DEVICE_COMMAND 0x06
421#define GET_HW_VERSION_COMMAND 0x07
422#define BLANK 0xff
423
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300424/* bootloader command responses */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300425#define COMMAND_OK 0x01
426#define COMMAND_FAILED 0x02
427#define COMMAND_PENDING 0x03
428
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300429/* buffer sizes */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300430#define COMMAND_BUFFER_SIZE 4
431#define RESPONSE_BUFFER_SIZE 2
432#define FLASH_BUFFER_SIZE 64
433#define CRC_BUFFER_SIZE 3
434
435
436
437/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300438 * General Driver Definitions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300439 **************************************************************************/
440
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300441/*
442 * si470x_device - private data
443 */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300444struct si470x_device {
445 /* reference to USB and video device */
446 struct usb_device *usbdev;
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300447 struct usb_interface *intf;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300448 struct video_device *videodev;
449
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300450 /* driver management */
451 unsigned int users;
Tobias Lorenzce5829e2008-05-31 15:04:32 -0300452 unsigned char disconnected;
453 struct mutex disconnect_lock;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300454
455 /* Silabs internal registers (0..15) */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300456 unsigned short registers[RADIO_REGISTER_NUM];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300457
458 /* RDS receive buffer */
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300459 struct delayed_work work;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300460 wait_queue_head_t read_queue;
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300461 struct mutex lock; /* buffer locking */
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300462 unsigned char *buffer; /* size is always multiple of three */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300463 unsigned int buf_size;
464 unsigned int rd_index;
465 unsigned int wr_index;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300466};
467
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300468
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300469/*
470 * The frequency is set in units of 62.5 Hz when using V4L2_TUNER_CAP_LOW,
471 * 62.5 kHz otherwise.
472 * The tuner is able to have a channel spacing of 50, 100 or 200 kHz.
473 * tuner->capability is therefore set to V4L2_TUNER_CAP_LOW
474 * The FREQ_MUL is then: 1 MHz / 62.5 Hz = 16000
475 */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300476#define FREQ_MUL (1000000 / 62.5)
477
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300478
479
480/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300481 * General Driver Functions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300482 **************************************************************************/
483
484/*
485 * si470x_get_report - receive a HID report
486 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300487static int si470x_get_report(struct si470x_device *radio, void *buf, int size)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300488{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300489 unsigned char *report = (unsigned char *) buf;
490 int retval;
491
492 retval = usb_control_msg(radio->usbdev,
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300493 usb_rcvctrlpipe(radio->usbdev, 0),
494 HID_REQ_GET_REPORT,
495 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300496 report[0], 2,
497 buf, size, usb_timeout);
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300498
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300499 if (retval < 0)
500 printk(KERN_WARNING DRIVER_NAME
501 ": si470x_get_report: usb_control_msg returned %d\n",
502 retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300503 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300504}
505
506
507/*
508 * si470x_set_report - send a HID report
509 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300510static int si470x_set_report(struct si470x_device *radio, void *buf, int size)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300511{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300512 unsigned char *report = (unsigned char *) buf;
513 int retval;
514
515 retval = usb_control_msg(radio->usbdev,
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300516 usb_sndctrlpipe(radio->usbdev, 0),
517 HID_REQ_SET_REPORT,
518 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300519 report[0], 2,
520 buf, size, usb_timeout);
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300521
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300522 if (retval < 0)
523 printk(KERN_WARNING DRIVER_NAME
524 ": si470x_set_report: usb_control_msg returned %d\n",
525 retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300526 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300527}
528
529
530/*
531 * si470x_get_register - read register
532 */
533static int si470x_get_register(struct si470x_device *radio, int regnr)
534{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300535 unsigned char buf[REGISTER_REPORT_SIZE];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300536 int retval;
537
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300538 buf[0] = REGISTER_REPORT(regnr);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300539
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300540 retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
541
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300542 if (retval >= 0)
Harvey Harrison5ad6b812008-04-15 22:22:11 -0300543 radio->registers[regnr] = get_unaligned_be16(&buf[1]);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300544
545 return (retval < 0) ? -EINVAL : 0;
546}
547
548
549/*
550 * si470x_set_register - write register
551 */
552static int si470x_set_register(struct si470x_device *radio, int regnr)
553{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300554 unsigned char buf[REGISTER_REPORT_SIZE];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300555 int retval;
556
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300557 buf[0] = REGISTER_REPORT(regnr);
Harvey Harrison5ad6b812008-04-15 22:22:11 -0300558 put_unaligned_be16(radio->registers[regnr], &buf[1]);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300559
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300560 retval = si470x_set_report(radio, (void *) &buf, sizeof(buf));
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300561
562 return (retval < 0) ? -EINVAL : 0;
563}
564
565
566/*
567 * si470x_get_all_registers - read entire registers
568 */
569static int si470x_get_all_registers(struct si470x_device *radio)
570{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300571 unsigned char buf[ENTIRE_REPORT_SIZE];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300572 int retval;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300573 unsigned char regnr;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300574
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300575 buf[0] = ENTIRE_REPORT;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300576
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300577 retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300578
579 if (retval >= 0)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300580 for (regnr = 0; regnr < RADIO_REGISTER_NUM; regnr++)
Harvey Harrison5ad6b812008-04-15 22:22:11 -0300581 radio->registers[regnr] = get_unaligned_be16(
582 &buf[regnr * RADIO_REGISTER_SIZE + 1]);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300583
584 return (retval < 0) ? -EINVAL : 0;
585}
586
587
588/*
589 * si470x_get_rds_registers - read rds registers
590 */
591static int si470x_get_rds_registers(struct si470x_device *radio)
592{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300593 unsigned char buf[RDS_REPORT_SIZE];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300594 int retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300595 int size;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300596 unsigned char regnr;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300597
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300598 buf[0] = RDS_REPORT;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300599
600 retval = usb_interrupt_msg(radio->usbdev,
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300601 usb_rcvintpipe(radio->usbdev, 1),
602 (void *) &buf, sizeof(buf), &size, usb_timeout);
603 if (size != sizeof(buf))
Tobias Lorenzce5829e2008-05-31 15:04:32 -0300604 printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: "
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300605 "return size differs: %d != %zu\n", size, sizeof(buf));
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300606 if (retval < 0)
607 printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: "
608 "usb_interrupt_msg returned %d\n", retval);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300609
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300610 if (retval >= 0)
611 for (regnr = 0; regnr < RDS_REGISTER_NUM; regnr++)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300612 radio->registers[STATUSRSSI + regnr] =
Tobias Lorenzce5829e2008-05-31 15:04:32 -0300613 get_unaligned_be16(
614 &buf[regnr * RADIO_REGISTER_SIZE + 1]);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300615
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300616 return (retval < 0) ? -EINVAL : 0;
617}
618
619
620/*
621 * si470x_set_chan - set the channel
622 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300623static int si470x_set_chan(struct si470x_device *radio, unsigned short chan)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300624{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300625 int retval;
626 unsigned long timeout;
627 bool timed_out = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300628
629 /* start tuning */
630 radio->registers[CHANNEL] &= ~CHANNEL_CHAN;
631 radio->registers[CHANNEL] |= CHANNEL_TUNE | chan;
632 retval = si470x_set_register(radio, CHANNEL);
633 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300634 goto done;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300635
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300636 /* wait till tune operation has completed */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300637 timeout = jiffies + msecs_to_jiffies(tune_timeout);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300638 do {
639 retval = si470x_get_register(radio, STATUSRSSI);
640 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300641 goto stop;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300642 timed_out = time_after(jiffies, timeout);
643 } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
644 (!timed_out));
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300645 if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
646 printk(KERN_WARNING DRIVER_NAME ": tune does not complete\n");
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300647 if (timed_out)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300648 printk(KERN_WARNING DRIVER_NAME
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300649 ": tune timed out after %u ms\n", tune_timeout);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300650
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300651stop:
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300652 /* stop tuning */
653 radio->registers[CHANNEL] &= ~CHANNEL_TUNE;
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300654 retval = si470x_set_register(radio, CHANNEL);
655
656done:
657 return retval;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300658}
659
660
661/*
662 * si470x_get_freq - get the frequency
663 */
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300664static int si470x_get_freq(struct si470x_device *radio, unsigned int *freq)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300665{
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300666 unsigned int spacing, band_bottom;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300667 unsigned short chan;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300668 int retval;
669
670 /* Spacing (kHz) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300671 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300672 /* 0: 200 kHz (USA, Australia) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300673 case 0:
674 spacing = 0.200 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300675 /* 1: 100 kHz (Europe, Japan) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300676 case 1:
677 spacing = 0.100 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300678 /* 2: 50 kHz */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300679 default:
680 spacing = 0.050 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300681 };
682
683 /* Bottom of Band (MHz) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300684 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300685 /* 0: 87.5 - 108 MHz (USA, Europe) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300686 case 0:
687 band_bottom = 87.5 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300688 /* 1: 76 - 108 MHz (Japan wide band) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300689 default:
690 band_bottom = 76 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300691 /* 2: 76 - 90 MHz (Japan) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300692 case 2:
693 band_bottom = 76 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300694 };
695
696 /* read channel */
697 retval = si470x_get_register(radio, READCHAN);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300698 chan = radio->registers[READCHAN] & READCHAN_READCHAN;
699
700 /* Frequency (MHz) = Spacing (kHz) x Channel + Bottom of Band (MHz) */
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300701 *freq = chan * spacing + band_bottom;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300702
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300703 return retval;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300704}
705
706
707/*
708 * si470x_set_freq - set the frequency
709 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300710static int si470x_set_freq(struct si470x_device *radio, unsigned int freq)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300711{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300712 unsigned int spacing, band_bottom;
713 unsigned short chan;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300714
715 /* Spacing (kHz) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300716 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300717 /* 0: 200 kHz (USA, Australia) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300718 case 0:
719 spacing = 0.200 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300720 /* 1: 100 kHz (Europe, Japan) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300721 case 1:
722 spacing = 0.100 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300723 /* 2: 50 kHz */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300724 default:
725 spacing = 0.050 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300726 };
727
728 /* Bottom of Band (MHz) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300729 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300730 /* 0: 87.5 - 108 MHz (USA, Europe) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300731 case 0:
732 band_bottom = 87.5 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300733 /* 1: 76 - 108 MHz (Japan wide band) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300734 default:
735 band_bottom = 76 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300736 /* 2: 76 - 90 MHz (Japan) */
Tobias Lorenza17c0012008-09-24 19:21:50 -0300737 case 2:
738 band_bottom = 76 * FREQ_MUL; break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300739 };
740
741 /* Chan = [ Freq (Mhz) - Bottom of Band (MHz) ] / Spacing (kHz) */
742 chan = (freq - band_bottom) / spacing;
743
744 return si470x_set_chan(radio, chan);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300745}
746
747
748/*
Tobias Lorenzbbc89952008-05-31 15:11:32 -0300749 * si470x_set_seek - set seek
750 */
751static int si470x_set_seek(struct si470x_device *radio,
752 unsigned int wrap_around, unsigned int seek_upward)
753{
754 int retval = 0;
755 unsigned long timeout;
756 bool timed_out = 0;
757
758 /* start seeking */
759 radio->registers[POWERCFG] |= POWERCFG_SEEK;
760 if (wrap_around == 1)
761 radio->registers[POWERCFG] &= ~POWERCFG_SKMODE;
762 else
763 radio->registers[POWERCFG] |= POWERCFG_SKMODE;
764 if (seek_upward == 1)
765 radio->registers[POWERCFG] |= POWERCFG_SEEKUP;
766 else
767 radio->registers[POWERCFG] &= ~POWERCFG_SEEKUP;
768 retval = si470x_set_register(radio, POWERCFG);
769 if (retval < 0)
770 goto done;
771
772 /* wait till seek operation has completed */
773 timeout = jiffies + msecs_to_jiffies(seek_timeout);
774 do {
775 retval = si470x_get_register(radio, STATUSRSSI);
776 if (retval < 0)
777 goto stop;
778 timed_out = time_after(jiffies, timeout);
779 } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
780 (!timed_out));
781 if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
782 printk(KERN_WARNING DRIVER_NAME ": seek does not complete\n");
783 if (radio->registers[STATUSRSSI] & STATUSRSSI_SF)
784 printk(KERN_WARNING DRIVER_NAME
785 ": seek failed / band limit reached\n");
786 if (timed_out)
787 printk(KERN_WARNING DRIVER_NAME
788 ": seek timed out after %u ms\n", seek_timeout);
789
790stop:
791 /* stop seeking */
792 radio->registers[POWERCFG] &= ~POWERCFG_SEEK;
793 retval = si470x_set_register(radio, POWERCFG);
794
795done:
796 /* try again, if timed out */
797 if ((retval == 0) && timed_out)
798 retval = -EAGAIN;
799
800 return retval;
801}
802
803
804/*
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300805 * si470x_start - switch on radio
806 */
807static int si470x_start(struct si470x_device *radio)
808{
809 int retval;
810
811 /* powercfg */
812 radio->registers[POWERCFG] =
813 POWERCFG_DMUTE | POWERCFG_ENABLE | POWERCFG_RDSM;
814 retval = si470x_set_register(radio, POWERCFG);
815 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300816 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300817
818 /* sysconfig 1 */
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300819 radio->registers[SYSCONFIG1] = SYSCONFIG1_DE;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300820 retval = si470x_set_register(radio, SYSCONFIG1);
821 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300822 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300823
824 /* sysconfig 2 */
825 radio->registers[SYSCONFIG2] =
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300826 (0x3f << 8) | /* SEEKTH */
827 ((band << 6) & SYSCONFIG2_BAND) | /* BAND */
828 ((space << 4) & SYSCONFIG2_SPACE) | /* SPACE */
829 15; /* VOLUME (max) */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300830 retval = si470x_set_register(radio, SYSCONFIG2);
831 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300832 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300833
834 /* reset last channel */
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300835 retval = si470x_set_chan(radio,
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300836 radio->registers[CHANNEL] & CHANNEL_CHAN);
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300837
838done:
839 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300840}
841
842
843/*
844 * si470x_stop - switch off radio
845 */
846static int si470x_stop(struct si470x_device *radio)
847{
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300848 int retval;
849
850 /* sysconfig 1 */
851 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
852 retval = si470x_set_register(radio, SYSCONFIG1);
853 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300854 goto done;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300855
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300856 /* powercfg */
857 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
858 /* POWERCFG_ENABLE has to automatically go low */
859 radio->registers[POWERCFG] |= POWERCFG_ENABLE | POWERCFG_DISABLE;
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300860 retval = si470x_set_register(radio, POWERCFG);
861
862done:
863 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300864}
865
866
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300867/*
868 * si470x_rds_on - switch on rds reception
869 */
870static int si470x_rds_on(struct si470x_device *radio)
871{
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300872 int retval;
873
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300874 /* sysconfig 1 */
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300875 mutex_lock(&radio->lock);
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300876 radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDS;
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300877 retval = si470x_set_register(radio, SYSCONFIG1);
878 if (retval < 0)
879 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
880 mutex_unlock(&radio->lock);
881
882 return retval;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300883}
884
885
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300886
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300887/**************************************************************************
888 * RDS Driver Functions
889 **************************************************************************/
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300890
891/*
892 * si470x_rds - rds processing function
893 */
894static void si470x_rds(struct si470x_device *radio)
895{
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300896 unsigned char blocknum;
897 unsigned short bler; /* rds block errors */
898 unsigned short rds;
899 unsigned char tmpbuf[3];
900
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300901 /* get rds blocks */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300902 if (si470x_get_rds_registers(radio) < 0)
903 return;
904 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0) {
905 /* No RDS group ready */
906 return;
907 }
908 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSS) == 0) {
909 /* RDS decoder not synchronized */
910 return;
911 }
912
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300913 /* copy all four RDS blocks to internal buffer */
914 mutex_lock(&radio->lock);
915 for (blocknum = 0; blocknum < 4; blocknum++) {
916 switch (blocknum) {
917 default:
918 bler = (radio->registers[STATUSRSSI] &
919 STATUSRSSI_BLERA) >> 9;
920 rds = radio->registers[RDSA];
921 break;
922 case 1:
923 bler = (radio->registers[READCHAN] &
924 READCHAN_BLERB) >> 14;
925 rds = radio->registers[RDSB];
926 break;
927 case 2:
928 bler = (radio->registers[READCHAN] &
929 READCHAN_BLERC) >> 12;
930 rds = radio->registers[RDSC];
931 break;
932 case 3:
933 bler = (radio->registers[READCHAN] &
934 READCHAN_BLERD) >> 10;
935 rds = radio->registers[RDSD];
936 break;
937 };
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300938
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300939 /* Fill the V4L2 RDS buffer */
Harvey Harrison5ad6b812008-04-15 22:22:11 -0300940 put_unaligned_le16(rds, &tmpbuf);
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300941 tmpbuf[2] = blocknum; /* offset name */
942 tmpbuf[2] |= blocknum << 3; /* received offset */
943 if (bler > max_rds_errors)
944 tmpbuf[2] |= 0x80; /* uncorrectable errors */
945 else if (bler > 0)
946 tmpbuf[2] |= 0x40; /* corrected error(s) */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300947
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300948 /* copy RDS block to internal buffer */
949 memcpy(&radio->buffer[radio->wr_index], &tmpbuf, 3);
950 radio->wr_index += 3;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300951
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300952 /* wrap write pointer */
953 if (radio->wr_index >= radio->buf_size)
954 radio->wr_index = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300955
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300956 /* check for overflow */
957 if (radio->wr_index == radio->rd_index) {
958 /* increment and wrap read pointer */
959 radio->rd_index += 3;
960 if (radio->rd_index >= radio->buf_size)
961 radio->rd_index = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300962 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300963 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300964 mutex_unlock(&radio->lock);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300965
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300966 /* wake up read queue */
967 if (radio->wr_index != radio->rd_index)
968 wake_up_interruptible(&radio->read_queue);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300969}
970
971
972/*
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300973 * si470x_work - rds work function
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300974 */
975static void si470x_work(struct work_struct *work)
976{
977 struct si470x_device *radio = container_of(work, struct si470x_device,
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300978 work.work);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300979
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300980 /* safety checks */
Tobias Lorenzce5829e2008-05-31 15:04:32 -0300981 if (radio->disconnected)
982 return;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300983 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300984 return;
985
986 si470x_rds(radio);
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300987 schedule_delayed_work(&radio->work, msecs_to_jiffies(rds_poll_time));
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300988}
989
990
991
992/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300993 * File Operations Interface
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300994 **************************************************************************/
995
996/*
997 * si470x_fops_read - read RDS data
998 */
999static ssize_t si470x_fops_read(struct file *file, char __user *buf,
1000 size_t count, loff_t *ppos)
1001{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001002 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001003 int retval = 0;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001004 unsigned int block_count = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001005
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001006 /* switch on rds reception */
1007 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
1008 si470x_rds_on(radio);
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001009 schedule_delayed_work(&radio->work,
1010 msecs_to_jiffies(rds_poll_time));
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001011 }
1012
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001013 /* block if no new data available */
1014 while (radio->wr_index == radio->rd_index) {
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001015 if (file->f_flags & O_NONBLOCK) {
1016 retval = -EWOULDBLOCK;
1017 goto done;
1018 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001019 if (wait_event_interruptible(radio->read_queue,
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001020 radio->wr_index != radio->rd_index) < 0) {
1021 retval = -EINTR;
1022 goto done;
1023 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001024 }
1025
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001026 /* calculate block count from byte count */
1027 count /= 3;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001028
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001029 /* copy RDS block out of internal buffer and to user buffer */
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001030 mutex_lock(&radio->lock);
1031 while (block_count < count) {
1032 if (radio->rd_index == radio->wr_index)
1033 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001034
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001035 /* always transfer rds complete blocks */
1036 if (copy_to_user(buf, &radio->buffer[radio->rd_index], 3))
1037 /* retval = -EFAULT; */
1038 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001039
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001040 /* increment and wrap read pointer */
1041 radio->rd_index += 3;
1042 if (radio->rd_index >= radio->buf_size)
1043 radio->rd_index = 0;
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001044
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001045 /* increment counters */
1046 block_count++;
1047 buf += 3;
1048 retval += 3;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001049 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001050 mutex_unlock(&radio->lock);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001051
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001052done:
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001053 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001054}
1055
1056
1057/*
1058 * si470x_fops_poll - poll RDS data
1059 */
1060static unsigned int si470x_fops_poll(struct file *file,
1061 struct poll_table_struct *pts)
1062{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001063 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001064 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001065
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001066 /* switch on rds reception */
1067 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
1068 si470x_rds_on(radio);
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001069 schedule_delayed_work(&radio->work,
1070 msecs_to_jiffies(rds_poll_time));
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001071 }
1072
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001073 poll_wait(file, &radio->read_queue, pts);
1074
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001075 if (radio->rd_index != radio->wr_index)
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001076 retval = POLLIN | POLLRDNORM;
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001077
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001078 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001079}
1080
1081
1082/*
1083 * si470x_fops_open - file open
1084 */
1085static int si470x_fops_open(struct inode *inode, struct file *file)
1086{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001087 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001088 int retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001089
Hans Verkuild56dc612008-07-30 08:43:36 -03001090 lock_kernel();
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001091 radio->users++;
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001092
1093 retval = usb_autopm_get_interface(radio->intf);
1094 if (retval < 0) {
1095 radio->users--;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001096 retval = -EIO;
1097 goto done;
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001098 }
1099
1100 if (radio->users == 1) {
1101 retval = si470x_start(radio);
1102 if (retval < 0)
1103 usb_autopm_put_interface(radio->intf);
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001104 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001105
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001106done:
Hans Verkuild56dc612008-07-30 08:43:36 -03001107 unlock_kernel();
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001108 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001109}
1110
1111
1112/*
1113 * si470x_fops_release - file release
1114 */
1115static int si470x_fops_release(struct inode *inode, struct file *file)
1116{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001117 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001118 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001119
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001120 /* safety check */
1121 if (!radio) {
1122 retval = -ENODEV;
1123 goto done;
1124 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001125
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001126 mutex_lock(&radio->disconnect_lock);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001127 radio->users--;
1128 if (radio->users == 0) {
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001129 if (radio->disconnected) {
1130 video_unregister_device(radio->videodev);
1131 kfree(radio->buffer);
1132 kfree(radio);
1133 goto unlock;
1134 }
Tobias Lorenz03dea862008-04-22 14:46:11 -03001135
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001136 /* stop rds reception */
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001137 cancel_delayed_work_sync(&radio->work);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001138
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001139 /* cancel read processes */
1140 wake_up_interruptible(&radio->read_queue);
1141
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001142 retval = si470x_stop(radio);
1143 usb_autopm_put_interface(radio->intf);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001144 }
1145
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001146unlock:
1147 mutex_unlock(&radio->disconnect_lock);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001148
1149done:
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001150 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001151}
1152
1153
1154/*
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001155 * si470x_fops - file operations interface
1156 */
1157static const struct file_operations si470x_fops = {
1158 .owner = THIS_MODULE,
1159 .llseek = no_llseek,
1160 .read = si470x_fops_read,
1161 .poll = si470x_fops_poll,
1162 .ioctl = video_ioctl2,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -03001163#ifdef CONFIG_COMPAT
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001164 .compat_ioctl = v4l_compat_ioctl32,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -03001165#endif
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001166 .open = si470x_fops_open,
1167 .release = si470x_fops_release,
1168};
1169
1170
1171
1172/**************************************************************************
1173 * Video4Linux Interface
1174 **************************************************************************/
1175
1176/*
1177 * si470x_v4l2_queryctrl - query control
1178 */
1179static struct v4l2_queryctrl si470x_v4l2_queryctrl[] = {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001180 {
1181 .id = V4L2_CID_AUDIO_VOLUME,
1182 .type = V4L2_CTRL_TYPE_INTEGER,
1183 .name = "Volume",
1184 .minimum = 0,
1185 .maximum = 15,
1186 .step = 1,
1187 .default_value = 15,
1188 },
1189 {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001190 .id = V4L2_CID_AUDIO_MUTE,
1191 .type = V4L2_CTRL_TYPE_BOOLEAN,
1192 .name = "Mute",
1193 .minimum = 0,
1194 .maximum = 1,
1195 .step = 1,
1196 .default_value = 1,
1197 },
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001198};
1199
1200
1201/*
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001202 * si470x_vidioc_querycap - query device capabilities
1203 */
1204static int si470x_vidioc_querycap(struct file *file, void *priv,
1205 struct v4l2_capability *capability)
1206{
1207 strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver));
1208 strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
1209 sprintf(capability->bus_info, "USB");
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001210 capability->version = DRIVER_KERNEL_VERSION;
Tobias Lorenzbbc89952008-05-31 15:11:32 -03001211 capability->capabilities = V4L2_CAP_HW_FREQ_SEEK |
1212 V4L2_CAP_TUNER | V4L2_CAP_RADIO;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001213
1214 return 0;
1215}
1216
1217
1218/*
1219 * si470x_vidioc_g_input - get input
1220 */
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001221static int si470x_vidioc_g_input(struct file *file, void *priv,
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001222 unsigned int *i)
1223{
1224 *i = 0;
1225
1226 return 0;
1227}
1228
1229
1230/*
1231 * si470x_vidioc_s_input - set input
1232 */
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001233static int si470x_vidioc_s_input(struct file *file, void *priv, unsigned int i)
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001234{
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001235 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001236
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001237 /* safety checks */
1238 if (i != 0)
1239 retval = -EINVAL;
1240
1241 if (retval < 0)
1242 printk(KERN_WARNING DRIVER_NAME
1243 ": set input failed with %d\n", retval);
1244 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001245}
1246
1247
1248/*
1249 * si470x_vidioc_queryctrl - enumerate control items
1250 */
1251static int si470x_vidioc_queryctrl(struct file *file, void *priv,
1252 struct v4l2_queryctrl *qc)
1253{
Tobias Lorenzeb6e3b82008-09-24 19:25:39 -03001254 unsigned char i = 0;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001255 int retval = -EINVAL;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001256
Tobias Lorenzeb6e3b82008-09-24 19:25:39 -03001257 /* abort if qc->id is below V4L2_CID_BASE */
1258 if (qc->id < V4L2_CID_BASE)
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001259 goto done;
1260
Tobias Lorenzeb6e3b82008-09-24 19:25:39 -03001261 /* search video control */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001262 for (i = 0; i < ARRAY_SIZE(si470x_v4l2_queryctrl); i++) {
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001263 if (qc->id == si470x_v4l2_queryctrl[i].id) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001264 memcpy(qc, &(si470x_v4l2_queryctrl[i]), sizeof(*qc));
Tobias Lorenzeb6e3b82008-09-24 19:25:39 -03001265 retval = 0; /* found */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001266 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001267 }
1268 }
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001269
Tobias Lorenzeb6e3b82008-09-24 19:25:39 -03001270 /* disable unsupported base controls */
1271 /* to satisfy kradio and such apps */
1272 if ((retval == -EINVAL) && (qc->id < V4L2_CID_LASTP1)) {
1273 qc->flags = V4L2_CTRL_FLAG_DISABLED;
1274 retval = 0;
1275 }
1276
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001277done:
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001278 if (retval < 0)
1279 printk(KERN_WARNING DRIVER_NAME
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001280 ": query controls failed with %d\n", retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001281 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001282}
1283
1284
1285/*
1286 * si470x_vidioc_g_ctrl - get the value of a control
1287 */
1288static int si470x_vidioc_g_ctrl(struct file *file, void *priv,
1289 struct v4l2_control *ctrl)
1290{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001291 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001292 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001293
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001294 /* safety checks */
1295 if (radio->disconnected) {
1296 retval = -EIO;
1297 goto done;
1298 }
Tobias Lorenz03dea862008-04-22 14:46:11 -03001299
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001300 switch (ctrl->id) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001301 case V4L2_CID_AUDIO_VOLUME:
1302 ctrl->value = radio->registers[SYSCONFIG2] &
1303 SYSCONFIG2_VOLUME;
1304 break;
1305 case V4L2_CID_AUDIO_MUTE:
1306 ctrl->value = ((radio->registers[POWERCFG] &
1307 POWERCFG_DMUTE) == 0) ? 1 : 0;
1308 break;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001309 default:
1310 retval = -EINVAL;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001311 }
1312
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001313done:
1314 if (retval < 0)
1315 printk(KERN_WARNING DRIVER_NAME
1316 ": get control failed with %d\n", retval);
1317 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001318}
1319
1320
1321/*
1322 * si470x_vidioc_s_ctrl - set the value of a control
1323 */
1324static int si470x_vidioc_s_ctrl(struct file *file, void *priv,
1325 struct v4l2_control *ctrl)
1326{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001327 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001328 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001329
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001330 /* safety checks */
1331 if (radio->disconnected) {
1332 retval = -EIO;
1333 goto done;
1334 }
Tobias Lorenz03dea862008-04-22 14:46:11 -03001335
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001336 switch (ctrl->id) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001337 case V4L2_CID_AUDIO_VOLUME:
1338 radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_VOLUME;
1339 radio->registers[SYSCONFIG2] |= ctrl->value;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001340 retval = si470x_set_register(radio, SYSCONFIG2);
1341 break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001342 case V4L2_CID_AUDIO_MUTE:
1343 if (ctrl->value == 1)
1344 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
1345 else
1346 radio->registers[POWERCFG] |= POWERCFG_DMUTE;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001347 retval = si470x_set_register(radio, POWERCFG);
1348 break;
1349 default:
1350 retval = -EINVAL;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001351 }
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001352
1353done:
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001354 if (retval < 0)
1355 printk(KERN_WARNING DRIVER_NAME
1356 ": set control failed with %d\n", retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001357 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001358}
1359
1360
1361/*
1362 * si470x_vidioc_g_audio - get audio attributes
1363 */
1364static int si470x_vidioc_g_audio(struct file *file, void *priv,
1365 struct v4l2_audio *audio)
1366{
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001367 int retval = 0;
1368
1369 /* safety checks */
1370 if (audio->index != 0) {
1371 retval = -EINVAL;
1372 goto done;
1373 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001374
1375 strcpy(audio->name, "Radio");
1376 audio->capability = V4L2_AUDCAP_STEREO;
1377
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001378done:
1379 if (retval < 0)
1380 printk(KERN_WARNING DRIVER_NAME
1381 ": get audio failed with %d\n", retval);
1382 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001383}
1384
1385
1386/*
1387 * si470x_vidioc_s_audio - set audio attributes
1388 */
1389static int si470x_vidioc_s_audio(struct file *file, void *priv,
1390 struct v4l2_audio *audio)
1391{
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001392 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001393
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001394 /* safety checks */
1395 if (audio->index != 0) {
1396 retval = -EINVAL;
1397 goto done;
1398 }
1399
1400done:
1401 if (retval < 0)
1402 printk(KERN_WARNING DRIVER_NAME
1403 ": set audio failed with %d\n", retval);
1404 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001405}
1406
1407
1408/*
1409 * si470x_vidioc_g_tuner - get tuner attributes
1410 */
1411static int si470x_vidioc_g_tuner(struct file *file, void *priv,
1412 struct v4l2_tuner *tuner)
1413{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001414 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001415 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001416
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001417 /* safety checks */
1418 if (radio->disconnected) {
1419 retval = -EIO;
1420 goto done;
1421 }
Tobias Lorenz33632d42008-09-24 19:30:26 -03001422 if (tuner->index != 0) {
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001423 retval = -EINVAL;
1424 goto done;
1425 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001426
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001427 retval = si470x_get_register(radio, STATUSRSSI);
1428 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001429 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001430
Tobias Lorenz33632d42008-09-24 19:30:26 -03001431 /* driver constants */
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001432 strcpy(tuner->name, "FM");
Tobias Lorenz33632d42008-09-24 19:30:26 -03001433 tuner->type = V4L2_TUNER_RADIO;
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001434 tuner->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
Tobias Lorenz33632d42008-09-24 19:30:26 -03001435
Tobias Lorenza17c0012008-09-24 19:21:50 -03001436 /* range limits */
1437 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001438 /* 0: 87.5 - 108 MHz (USA, Europe, default) */
1439 default:
1440 tuner->rangelow = 87.5 * FREQ_MUL;
1441 tuner->rangehigh = 108 * FREQ_MUL;
1442 break;
1443 /* 1: 76 - 108 MHz (Japan wide band) */
1444 case 1 :
1445 tuner->rangelow = 76 * FREQ_MUL;
1446 tuner->rangehigh = 108 * FREQ_MUL;
1447 break;
1448 /* 2: 76 - 90 MHz (Japan) */
1449 case 2 :
1450 tuner->rangelow = 76 * FREQ_MUL;
1451 tuner->rangehigh = 90 * FREQ_MUL;
1452 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001453 };
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001454
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001455 /* stereo indicator == stereo (instead of mono) */
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001456 if ((radio->registers[STATUSRSSI] & STATUSRSSI_ST) == 1)
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001457 tuner->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001458 else
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001459 tuner->rxsubchans = V4L2_TUNER_SUB_MONO;
1460
1461 /* mono/stereo selector */
1462 if ((radio->registers[POWERCFG] & POWERCFG_MONO) == 1)
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001463 tuner->audmode = V4L2_TUNER_MODE_MONO;
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001464 else
1465 tuner->audmode = V4L2_TUNER_MODE_STEREO;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001466
1467 /* min is worst, max is best; signal:0..0xffff; rssi: 0..0xff */
1468 tuner->signal = (radio->registers[STATUSRSSI] & STATUSRSSI_RSSI)
1469 * 0x0101;
1470
1471 /* automatic frequency control: -1: freq to low, 1 freq to high */
Tobias Lorenzace7d4b2008-05-31 15:09:07 -03001472 /* AFCRL does only indicate that freq. differs, not if too low/high */
1473 tuner->afc = (radio->registers[STATUSRSSI] & STATUSRSSI_AFCRL) ? 1 : 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001474
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001475done:
1476 if (retval < 0)
1477 printk(KERN_WARNING DRIVER_NAME
1478 ": get tuner failed with %d\n", retval);
1479 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001480}
1481
1482
1483/*
1484 * si470x_vidioc_s_tuner - set tuner attributes
1485 */
1486static int si470x_vidioc_s_tuner(struct file *file, void *priv,
1487 struct v4l2_tuner *tuner)
1488{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001489 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenz33632d42008-09-24 19:30:26 -03001490 int retval = -EINVAL;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001491
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001492 /* safety checks */
1493 if (radio->disconnected) {
1494 retval = -EIO;
1495 goto done;
1496 }
Tobias Lorenz33632d42008-09-24 19:30:26 -03001497 if (tuner->index != 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001498 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001499
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001500 /* mono/stereo selector */
1501 switch (tuner->audmode) {
1502 case V4L2_TUNER_MODE_MONO:
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001503 radio->registers[POWERCFG] |= POWERCFG_MONO; /* force mono */
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001504 break;
1505 case V4L2_TUNER_MODE_STEREO:
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001506 radio->registers[POWERCFG] &= ~POWERCFG_MONO; /* try stereo */
Tobias Lorenz73a529f2008-09-24 19:33:41 -03001507 break;
1508 default:
1509 goto done;
1510 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001511
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001512 retval = si470x_set_register(radio, POWERCFG);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001513
1514done:
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001515 if (retval < 0)
1516 printk(KERN_WARNING DRIVER_NAME
1517 ": set tuner failed with %d\n", retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001518 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001519}
1520
1521
1522/*
1523 * si470x_vidioc_g_frequency - get tuner or modulator radio frequency
1524 */
1525static int si470x_vidioc_g_frequency(struct file *file, void *priv,
1526 struct v4l2_frequency *freq)
1527{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001528 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001529 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001530
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001531 /* safety checks */
1532 if (radio->disconnected) {
1533 retval = -EIO;
1534 goto done;
1535 }
Tobias Lorenz33632d42008-09-24 19:30:26 -03001536 if (freq->tuner != 0) {
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001537 retval = -EINVAL;
1538 goto done;
1539 }
Tobias Lorenz03dea862008-04-22 14:46:11 -03001540
Tobias Lorenz33632d42008-09-24 19:30:26 -03001541 freq->type = V4L2_TUNER_RADIO;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001542 retval = si470x_get_freq(radio, &freq->frequency);
1543
1544done:
1545 if (retval < 0)
1546 printk(KERN_WARNING DRIVER_NAME
1547 ": get frequency failed with %d\n", retval);
1548 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001549}
1550
1551
1552/*
1553 * si470x_vidioc_s_frequency - set tuner or modulator radio frequency
1554 */
1555static int si470x_vidioc_s_frequency(struct file *file, void *priv,
1556 struct v4l2_frequency *freq)
1557{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001558 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001559 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001560
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001561 /* safety checks */
1562 if (radio->disconnected) {
1563 retval = -EIO;
1564 goto done;
1565 }
Tobias Lorenz33632d42008-09-24 19:30:26 -03001566 if (freq->tuner != 0) {
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001567 retval = -EINVAL;
1568 goto done;
1569 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001570
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001571 retval = si470x_set_freq(radio, freq->frequency);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001572
1573done:
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001574 if (retval < 0)
1575 printk(KERN_WARNING DRIVER_NAME
1576 ": set frequency failed with %d\n", retval);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001577 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001578}
1579
1580
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001581/*
Tobias Lorenzbbc89952008-05-31 15:11:32 -03001582 * si470x_vidioc_s_hw_freq_seek - set hardware frequency seek
1583 */
1584static int si470x_vidioc_s_hw_freq_seek(struct file *file, void *priv,
1585 struct v4l2_hw_freq_seek *seek)
1586{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001587 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenzbbc89952008-05-31 15:11:32 -03001588 int retval = 0;
1589
1590 /* safety checks */
1591 if (radio->disconnected) {
1592 retval = -EIO;
1593 goto done;
1594 }
Tobias Lorenz33632d42008-09-24 19:30:26 -03001595 if (seek->tuner != 0) {
Tobias Lorenzbbc89952008-05-31 15:11:32 -03001596 retval = -EINVAL;
1597 goto done;
1598 }
1599
1600 retval = si470x_set_seek(radio, seek->wrap_around, seek->seek_upward);
1601
1602done:
1603 if (retval < 0)
1604 printk(KERN_WARNING DRIVER_NAME
1605 ": set hardware frequency seek failed with %d\n",
1606 retval);
1607 return retval;
1608}
1609
Tobias Lorenz374ab682008-09-24 19:17:54 -03001610
1611/*
1612 * si470x_ioctl_ops - video device ioctl operations
1613 */
Hans Verkuila3998102008-07-21 02:57:38 -03001614static const struct v4l2_ioctl_ops si470x_ioctl_ops = {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001615 .vidioc_querycap = si470x_vidioc_querycap,
1616 .vidioc_g_input = si470x_vidioc_g_input,
1617 .vidioc_s_input = si470x_vidioc_s_input,
1618 .vidioc_queryctrl = si470x_vidioc_queryctrl,
1619 .vidioc_g_ctrl = si470x_vidioc_g_ctrl,
1620 .vidioc_s_ctrl = si470x_vidioc_s_ctrl,
1621 .vidioc_g_audio = si470x_vidioc_g_audio,
1622 .vidioc_s_audio = si470x_vidioc_s_audio,
1623 .vidioc_g_tuner = si470x_vidioc_g_tuner,
1624 .vidioc_s_tuner = si470x_vidioc_s_tuner,
1625 .vidioc_g_frequency = si470x_vidioc_g_frequency,
1626 .vidioc_s_frequency = si470x_vidioc_s_frequency,
Tobias Lorenzbbc89952008-05-31 15:11:32 -03001627 .vidioc_s_hw_freq_seek = si470x_vidioc_s_hw_freq_seek,
Hans Verkuila3998102008-07-21 02:57:38 -03001628};
1629
Tobias Lorenz374ab682008-09-24 19:17:54 -03001630
Hans Verkuila3998102008-07-21 02:57:38 -03001631/*
Tobias Lorenz374ab682008-09-24 19:17:54 -03001632 * si470x_viddev_template - video device interface
Hans Verkuila3998102008-07-21 02:57:38 -03001633 */
1634static struct video_device si470x_viddev_template = {
1635 .fops = &si470x_fops,
Hans Verkuila3998102008-07-21 02:57:38 -03001636 .name = DRIVER_NAME,
Hans Verkuila3998102008-07-21 02:57:38 -03001637 .release = video_device_release,
Tobias Lorenz374ab682008-09-24 19:17:54 -03001638 .ioctl_ops = &si470x_ioctl_ops,
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001639};
1640
1641
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001642
1643/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001644 * USB Interface
1645 **************************************************************************/
1646
1647/*
1648 * si470x_usb_driver_probe - probe for the device
1649 */
1650static int si470x_usb_driver_probe(struct usb_interface *intf,
1651 const struct usb_device_id *id)
1652{
1653 struct si470x_device *radio;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001654 int retval = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001655
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001656 /* private data allocation and initialization */
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001657 radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001658 if (!radio) {
1659 retval = -ENOMEM;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001660 goto err_initial;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001661 }
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001662 radio->users = 0;
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001663 radio->disconnected = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001664 radio->usbdev = interface_to_usbdev(intf);
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001665 radio->intf = intf;
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001666 mutex_init(&radio->disconnect_lock);
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001667 mutex_init(&radio->lock);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001668
1669 /* video device allocation and initialization */
1670 radio->videodev = video_device_alloc();
1671 if (!radio->videodev) {
1672 retval = -ENOMEM;
1673 goto err_radio;
1674 }
1675 memcpy(radio->videodev, &si470x_viddev_template,
1676 sizeof(si470x_viddev_template));
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001677 video_set_drvdata(radio->videodev, radio);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001678
1679 /* show some infos about the specific device */
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001680 if (si470x_get_all_registers(radio) < 0) {
1681 retval = -EIO;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001682 goto err_all;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001683 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001684 printk(KERN_INFO DRIVER_NAME ": DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001685 radio->registers[DEVICEID], radio->registers[CHIPID]);
1686
1687 /* check if firmware is current */
1688 if ((radio->registers[CHIPID] & CHIPID_FIRMWARE)
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001689 < RADIO_SW_VERSION_CURRENT) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001690 printk(KERN_WARNING DRIVER_NAME
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001691 ": This driver is known to work with "
1692 "firmware version %hu,\n", RADIO_SW_VERSION_CURRENT);
1693 printk(KERN_WARNING DRIVER_NAME
1694 ": but the device has firmware version %hu.\n",
1695 radio->registers[CHIPID] & CHIPID_FIRMWARE);
1696 printk(KERN_WARNING DRIVER_NAME
1697 ": If you have some trouble using this driver,\n");
1698 printk(KERN_WARNING DRIVER_NAME
1699 ": please report to V4L ML at "
1700 "video4linux-list@redhat.com\n");
1701 }
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001702
1703 /* set initial frequency */
1704 si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
1705
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001706 /* rds buffer allocation */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001707 radio->buf_size = rds_buf * 3;
1708 radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001709 if (!radio->buffer) {
1710 retval = -EIO;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001711 goto err_all;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001712 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001713
1714 /* rds buffer configuration */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001715 radio->wr_index = 0;
1716 radio->rd_index = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001717 init_waitqueue_head(&radio->read_queue);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001718
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001719 /* prepare rds work function */
1720 INIT_DELAYED_WORK(&radio->work, si470x_work);
1721
1722 /* register video device */
Hans Verkuilcba99ae2008-09-03 17:11:58 -03001723 retval = video_register_device(radio->videodev, VFL_TYPE_RADIO, radio_nr);
1724 if (retval) {
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001725 printk(KERN_WARNING DRIVER_NAME
1726 ": Could not register video device\n");
1727 goto err_all;
1728 }
1729 usb_set_intfdata(intf, radio);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001730
1731 return 0;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001732err_all:
1733 video_device_release(radio->videodev);
1734 kfree(radio->buffer);
1735err_radio:
1736 kfree(radio);
1737err_initial:
1738 return retval;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001739}
1740
1741
1742/*
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001743 * si470x_usb_driver_suspend - suspend the device
1744 */
1745static int si470x_usb_driver_suspend(struct usb_interface *intf,
1746 pm_message_t message)
1747{
1748 struct si470x_device *radio = usb_get_intfdata(intf);
1749
1750 printk(KERN_INFO DRIVER_NAME ": suspending now...\n");
1751
1752 cancel_delayed_work_sync(&radio->work);
1753
1754 return 0;
1755}
1756
1757
1758/*
1759 * si470x_usb_driver_resume - resume the device
1760 */
1761static int si470x_usb_driver_resume(struct usb_interface *intf)
1762{
1763 struct si470x_device *radio = usb_get_intfdata(intf);
1764
1765 printk(KERN_INFO DRIVER_NAME ": resuming now...\n");
1766
1767 mutex_lock(&radio->lock);
1768 if (radio->users && radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS)
1769 schedule_delayed_work(&radio->work,
1770 msecs_to_jiffies(rds_poll_time));
1771 mutex_unlock(&radio->lock);
1772
1773 return 0;
1774}
1775
1776
1777/*
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001778 * si470x_usb_driver_disconnect - disconnect the device
1779 */
1780static void si470x_usb_driver_disconnect(struct usb_interface *intf)
1781{
1782 struct si470x_device *radio = usb_get_intfdata(intf);
1783
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001784 mutex_lock(&radio->disconnect_lock);
1785 radio->disconnected = 1;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001786 cancel_delayed_work_sync(&radio->work);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001787 usb_set_intfdata(intf, NULL);
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001788 if (radio->users == 0) {
1789 video_unregister_device(radio->videodev);
1790 kfree(radio->buffer);
1791 kfree(radio);
1792 }
1793 mutex_unlock(&radio->disconnect_lock);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001794}
1795
1796
1797/*
1798 * si470x_usb_driver - usb driver interface
1799 */
1800static struct usb_driver si470x_usb_driver = {
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001801 .name = DRIVER_NAME,
1802 .probe = si470x_usb_driver_probe,
1803 .disconnect = si470x_usb_driver_disconnect,
1804 .suspend = si470x_usb_driver_suspend,
1805 .resume = si470x_usb_driver_resume,
1806 .id_table = si470x_usb_driver_id_table,
1807 .supports_autosuspend = 1,
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001808};
1809
1810
1811
1812/**************************************************************************
1813 * Module Interface
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001814 **************************************************************************/
1815
1816/*
1817 * si470x_module_init - module init
1818 */
1819static int __init si470x_module_init(void)
1820{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001821 printk(KERN_INFO DRIVER_DESC ", Version " DRIVER_VERSION "\n");
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001822 return usb_register(&si470x_usb_driver);
1823}
1824
1825
1826/*
1827 * si470x_module_exit - module exit
1828 */
1829static void __exit si470x_module_exit(void)
1830{
1831 usb_deregister(&si470x_usb_driver);
1832}
1833
1834
1835module_init(si470x_module_init);
1836module_exit(si470x_module_exit);
1837
1838MODULE_LICENSE("GPL");
1839MODULE_AUTHOR(DRIVER_AUTHOR);
1840MODULE_DESCRIPTION(DRIVER_DESC);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001841MODULE_VERSION(DRIVER_VERSION);