blob: 954ba997895d75d22ca450434231a6e02c82c2a4 [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 Lorenza7c850a2008-05-31 15:07:52 -0300104 * - more safety checks, let si470x_get_freq return errno
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300105 *
106 * ToDo:
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300107 * - add seeking support
108 * - add firmware download/update support
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300109 * - RDS support: interrupt mode, instead of polling
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300110 * - add LED status output (check if that's not already done in firmware)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300111 */
112
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300113
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300114/* driver definitions */
115#define DRIVER_AUTHOR "Tobias Lorenz <tobias.lorenz@gmx.net>"
116#define DRIVER_NAME "radio-si470x"
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300117#define DRIVER_KERNEL_VERSION KERNEL_VERSION(1, 0, 8)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300118#define DRIVER_CARD "Silicon Labs Si470x FM Radio Receiver"
119#define DRIVER_DESC "USB radio driver for Si470x FM Radio Receivers"
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300120#define DRIVER_VERSION "1.0.8"
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300121
122
123/* kernel includes */
124#include <linux/kernel.h>
125#include <linux/module.h>
126#include <linux/init.h>
127#include <linux/slab.h>
128#include <linux/input.h>
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300129#include <linux/usb.h>
130#include <linux/hid.h>
131#include <linux/version.h>
Mauro Carvalho Chehab387d4472008-01-15 11:25:10 -0300132#include <linux/videodev2.h>
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300133#include <linux/mutex.h>
Mauro Carvalho Chehab387d4472008-01-15 11:25:10 -0300134#include <media/v4l2-common.h>
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300135#include <media/rds.h>
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300136#include <asm/unaligned.h>
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300137
138
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300139/* USB Device ID List */
140static struct usb_device_id si470x_usb_driver_id_table[] = {
141 /* Silicon Labs USB FM Radio Reference Design */
142 { USB_DEVICE_AND_INTERFACE_INFO(0x10c4, 0x818a, USB_CLASS_HID, 0, 0) },
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300143 /* ADS/Tech FM Radio Receiver (formerly Instant FM Music) */
144 { USB_DEVICE_AND_INTERFACE_INFO(0x06e1, 0xa155, 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;
158module_param(radio_nr, int, 0);
159MODULE_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;
166module_param(space, ushort, 0);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300167MODULE_PARM_DESC(radio_nr, "Spacing: 0=200kHz 1=100kHz *2=50kHz*");
168
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;
174module_param(band, ushort, 0);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300175MODULE_PARM_DESC(radio_nr, "Band: 0=87.5..108MHz *1=76..108MHz* 2=76..90MHz");
176
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;
181module_param(de, ushort, 0);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300182MODULE_PARM_DESC(radio_nr, "De-emphasis: 0=75us *1=50us*");
183
184/* USB timeout */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300185static unsigned int usb_timeout = 500;
186module_param(usb_timeout, uint, 0);
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;
191module_param(tune_timeout, uint, 0);
192MODULE_PARM_DESC(tune_timeout, "Tune timeout: *3000*");
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300193
194/* RDS buffer blocks */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300195static unsigned int rds_buf = 100;
196module_param(rds_buf, uint, 0);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300197MODULE_PARM_DESC(rds_buf, "RDS buffer entries: *100*");
198
199/* RDS maximum block errors */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300200static unsigned short max_rds_errors = 1;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300201/* 0 means 0 errors requiring correction */
202/* 1 means 1-2 errors requiring correction (used by original USBRadio.exe) */
203/* 2 means 3-5 errors requiring correction */
204/* 3 means 6+ errors or errors in checkword, correction not possible */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300205module_param(max_rds_errors, ushort, 0);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300206MODULE_PARM_DESC(max_rds_errors, "RDS maximum block errors: *1*");
207
208/* RDS poll frequency */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300209static unsigned int rds_poll_time = 40;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300210/* 40 is used by the original USBRadio.exe */
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300211/* 50 is used by radio-cadet */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300212/* 75 should be okay */
213/* 80 is the usual RDS receive interval */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300214module_param(rds_poll_time, uint, 0);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300215MODULE_PARM_DESC(rds_poll_time, "RDS poll time (ms): *40*");
216
217
218
219/**************************************************************************
220 * Register Definitions
221 **************************************************************************/
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300222#define RADIO_REGISTER_SIZE 2 /* 16 register bit width */
223#define RADIO_REGISTER_NUM 16 /* DEVICEID ... RDSD */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300224#define RDS_REGISTER_NUM 6 /* STATUSRSSI ... RDSD */
225
226#define DEVICEID 0 /* Device ID */
227#define DEVICEID_PN 0xf000 /* bits 15..12: Part Number */
228#define DEVICEID_MFGID 0x0fff /* bits 11..00: Manufacturer ID */
229
230#define CHIPID 1 /* Chip ID */
231#define CHIPID_REV 0xfc00 /* bits 15..10: Chip Version */
232#define CHIPID_DEV 0x0200 /* bits 09..09: Device */
233#define CHIPID_FIRMWARE 0x01ff /* bits 08..00: Firmware Version */
234
235#define POWERCFG 2 /* Power Configuration */
236#define POWERCFG_DSMUTE 0x8000 /* bits 15..15: Softmute Disable */
237#define POWERCFG_DMUTE 0x4000 /* bits 14..14: Mute Disable */
238#define POWERCFG_MONO 0x2000 /* bits 13..13: Mono Select */
239#define POWERCFG_RDSM 0x0800 /* bits 11..11: RDS Mode (Si4701 only) */
240#define POWERCFG_SKMODE 0x0400 /* bits 10..10: Seek Mode */
241#define POWERCFG_SEEKUP 0x0200 /* bits 09..09: Seek Direction */
242#define POWERCFG_SEEK 0x0100 /* bits 08..08: Seek */
243#define POWERCFG_DISABLE 0x0040 /* bits 06..06: Powerup Disable */
244#define POWERCFG_ENABLE 0x0001 /* bits 00..00: Powerup Enable */
245
246#define CHANNEL 3 /* Channel */
247#define CHANNEL_TUNE 0x8000 /* bits 15..15: Tune */
248#define CHANNEL_CHAN 0x03ff /* bits 09..00: Channel Select */
249
250#define SYSCONFIG1 4 /* System Configuration 1 */
251#define SYSCONFIG1_RDSIEN 0x8000 /* bits 15..15: RDS Interrupt Enable (Si4701 only) */
252#define SYSCONFIG1_STCIEN 0x4000 /* bits 14..14: Seek/Tune Complete Interrupt Enable */
253#define SYSCONFIG1_RDS 0x1000 /* bits 12..12: RDS Enable (Si4701 only) */
254#define SYSCONFIG1_DE 0x0800 /* bits 11..11: De-emphasis (0=75us 1=50us) */
255#define SYSCONFIG1_AGCD 0x0400 /* bits 10..10: AGC Disable */
256#define SYSCONFIG1_BLNDADJ 0x00c0 /* bits 07..06: Stereo/Mono Blend Level Adjustment */
257#define SYSCONFIG1_GPIO3 0x0030 /* bits 05..04: General Purpose I/O 3 */
258#define SYSCONFIG1_GPIO2 0x000c /* bits 03..02: General Purpose I/O 2 */
259#define SYSCONFIG1_GPIO1 0x0003 /* bits 01..00: General Purpose I/O 1 */
260
261#define SYSCONFIG2 5 /* System Configuration 2 */
262#define SYSCONFIG2_SEEKTH 0xff00 /* bits 15..08: RSSI Seek Threshold */
263#define SYSCONFIG2_BAND 0x0080 /* bits 07..06: Band Select */
264#define SYSCONFIG2_SPACE 0x0030 /* bits 05..04: Channel Spacing */
265#define SYSCONFIG2_VOLUME 0x000f /* bits 03..00: Volume */
266
267#define SYSCONFIG3 6 /* System Configuration 3 */
268#define SYSCONFIG3_SMUTER 0xc000 /* bits 15..14: Softmute Attack/Recover Rate */
269#define SYSCONFIG3_SMUTEA 0x3000 /* bits 13..12: Softmute Attenuation */
270#define SYSCONFIG3_SKSNR 0x00f0 /* bits 07..04: Seek SNR Threshold */
271#define SYSCONFIG3_SKCNT 0x000f /* bits 03..00: Seek FM Impulse Detection Threshold */
272
273#define TEST1 7 /* Test 1 */
274#define TEST1_AHIZEN 0x4000 /* bits 14..14: Audio High-Z Enable */
275
276#define TEST2 8 /* Test 2 */
277/* TEST2 only contains reserved bits */
278
279#define BOOTCONFIG 9 /* Boot Configuration */
280/* BOOTCONFIG only contains reserved bits */
281
282#define STATUSRSSI 10 /* Status RSSI */
283#define STATUSRSSI_RDSR 0x8000 /* bits 15..15: RDS Ready (Si4701 only) */
284#define STATUSRSSI_STC 0x4000 /* bits 14..14: Seek/Tune Complete */
285#define STATUSRSSI_SF 0x2000 /* bits 13..13: Seek Fail/Band Limit */
286#define STATUSRSSI_AFCRL 0x1000 /* bits 12..12: AFC Rail */
287#define STATUSRSSI_RDSS 0x0800 /* bits 11..11: RDS Synchronized (Si4701 only) */
288#define STATUSRSSI_BLERA 0x0600 /* bits 10..09: RDS Block A Errors (Si4701 only) */
289#define STATUSRSSI_ST 0x0100 /* bits 08..08: Stereo Indicator */
290#define STATUSRSSI_RSSI 0x00ff /* bits 07..00: RSSI (Received Signal Strength Indicator) */
291
292#define READCHAN 11 /* Read Channel */
293#define READCHAN_BLERB 0xc000 /* bits 15..14: RDS Block D Errors (Si4701 only) */
294#define READCHAN_BLERC 0x3000 /* bits 13..12: RDS Block C Errors (Si4701 only) */
295#define READCHAN_BLERD 0x0c00 /* bits 11..10: RDS Block B Errors (Si4701 only) */
296#define READCHAN_READCHAN 0x03ff /* bits 09..00: Read Channel */
297
298#define RDSA 12 /* RDSA */
299#define RDSA_RDSA 0xffff /* bits 15..00: RDS Block A Data (Si4701 only) */
300
301#define RDSB 13 /* RDSB */
302#define RDSB_RDSB 0xffff /* bits 15..00: RDS Block B Data (Si4701 only) */
303
304#define RDSC 14 /* RDSC */
305#define RDSC_RDSC 0xffff /* bits 15..00: RDS Block C Data (Si4701 only) */
306
307#define RDSD 15 /* RDSD */
308#define RDSD_RDSD 0xffff /* bits 15..00: RDS Block D Data (Si4701 only) */
309
310
311
312/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300313 * USB HID Reports
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300314 **************************************************************************/
315
316/* Reports 1-16 give direct read/write access to the 16 Si470x registers */
317/* with the (REPORT_ID - 1) corresponding to the register address across USB */
318/* endpoint 0 using GET_REPORT and SET_REPORT */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300319#define REGISTER_REPORT_SIZE (RADIO_REGISTER_SIZE + 1)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300320#define REGISTER_REPORT(reg) ((reg) + 1)
321
322/* Report 17 gives direct read/write access to the entire Si470x register */
323/* map across endpoint 0 using GET_REPORT and SET_REPORT */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300324#define ENTIRE_REPORT_SIZE (RADIO_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300325#define ENTIRE_REPORT 17
326
327/* Report 18 is used to send the lowest 6 Si470x registers up the HID */
328/* interrupt endpoint 1 to Windows every 20 milliseconds for status */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300329#define RDS_REPORT_SIZE (RDS_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300330#define RDS_REPORT 18
331
332/* Report 19: LED state */
333#define LED_REPORT_SIZE 3
334#define LED_REPORT 19
335
336/* Report 19: stream */
337#define STREAM_REPORT_SIZE 3
338#define STREAM_REPORT 19
339
340/* Report 20: scratch */
341#define SCRATCH_PAGE_SIZE 63
342#define SCRATCH_REPORT_SIZE (SCRATCH_PAGE_SIZE + 1)
343#define SCRATCH_REPORT 20
344
345/* Reports 19-22: flash upgrade of the C8051F321 */
346#define WRITE_REPORT 19
347#define FLASH_REPORT 20
348#define CRC_REPORT 21
349#define RESPONSE_REPORT 22
350
351/* Report 23: currently unused, but can accept 60 byte reports on the HID */
352/* interrupt out endpoint 2 every 1 millisecond */
353#define UNUSED_REPORT 23
354
355
356
357/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300358 * Software/Hardware Versions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300359 **************************************************************************/
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300360#define RADIO_SW_VERSION_NOT_BOOTLOADABLE 6
361#define RADIO_SW_VERSION 7
362#define RADIO_SW_VERSION_CURRENT 15
363#define RADIO_HW_VERSION 1
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300364
365#define SCRATCH_PAGE_SW_VERSION 1
366#define SCRATCH_PAGE_HW_VERSION 2
367
368
369
370/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300371 * LED State Definitions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300372 **************************************************************************/
373#define LED_COMMAND 0x35
374
375#define NO_CHANGE_LED 0x00
376#define ALL_COLOR_LED 0x01 /* streaming state */
377#define BLINK_GREEN_LED 0x02 /* connect state */
378#define BLINK_RED_LED 0x04
379#define BLINK_ORANGE_LED 0x10 /* disconnect state */
380#define SOLID_GREEN_LED 0x20 /* tuning/seeking state */
381#define SOLID_RED_LED 0x40 /* bootload state */
382#define SOLID_ORANGE_LED 0x80
383
384
385
386/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300387 * Stream State Definitions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300388 **************************************************************************/
389#define STREAM_COMMAND 0x36
390#define STREAM_VIDPID 0x00
391#define STREAM_AUDIO 0xff
392
393
394
395/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300396 * Bootloader / Flash Commands
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300397 **************************************************************************/
398
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300399/* unique id sent to bootloader and required to put into a bootload state */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300400#define UNIQUE_BL_ID 0x34
401
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300402/* mask for the flash data */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300403#define FLASH_DATA_MASK 0x55
404
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300405/* bootloader commands */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300406#define GET_SW_VERSION_COMMAND 0x00
407#define SET_PAGE_COMMAND 0x01
408#define ERASE_PAGE_COMMAND 0x02
409#define WRITE_PAGE_COMMAND 0x03
410#define CRC_ON_PAGE_COMMAND 0x04
411#define READ_FLASH_BYTE_COMMAND 0x05
412#define RESET_DEVICE_COMMAND 0x06
413#define GET_HW_VERSION_COMMAND 0x07
414#define BLANK 0xff
415
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300416/* bootloader command responses */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300417#define COMMAND_OK 0x01
418#define COMMAND_FAILED 0x02
419#define COMMAND_PENDING 0x03
420
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300421/* buffer sizes */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300422#define COMMAND_BUFFER_SIZE 4
423#define RESPONSE_BUFFER_SIZE 2
424#define FLASH_BUFFER_SIZE 64
425#define CRC_BUFFER_SIZE 3
426
427
428
429/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300430 * General Driver Definitions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300431 **************************************************************************/
432
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300433/*
434 * si470x_device - private data
435 */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300436struct si470x_device {
437 /* reference to USB and video device */
438 struct usb_device *usbdev;
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300439 struct usb_interface *intf;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300440 struct video_device *videodev;
441
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300442 /* driver management */
443 unsigned int users;
Tobias Lorenzce5829e2008-05-31 15:04:32 -0300444 unsigned char disconnected;
445 struct mutex disconnect_lock;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300446
447 /* Silabs internal registers (0..15) */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300448 unsigned short registers[RADIO_REGISTER_NUM];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300449
450 /* RDS receive buffer */
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300451 struct delayed_work work;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300452 wait_queue_head_t read_queue;
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300453 struct mutex lock; /* buffer locking */
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300454 unsigned char *buffer; /* size is always multiple of three */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300455 unsigned int buf_size;
456 unsigned int rd_index;
457 unsigned int wr_index;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300458};
459
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300460
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300461/*
462 * The frequency is set in units of 62.5 Hz when using V4L2_TUNER_CAP_LOW,
463 * 62.5 kHz otherwise.
464 * The tuner is able to have a channel spacing of 50, 100 or 200 kHz.
465 * tuner->capability is therefore set to V4L2_TUNER_CAP_LOW
466 * The FREQ_MUL is then: 1 MHz / 62.5 Hz = 16000
467 */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300468#define FREQ_MUL (1000000 / 62.5)
469
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300470
471
472/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300473 * General Driver Functions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300474 **************************************************************************/
475
476/*
477 * si470x_get_report - receive a HID report
478 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300479static int si470x_get_report(struct si470x_device *radio, void *buf, int size)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300480{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300481 unsigned char *report = (unsigned char *) buf;
482 int retval;
483
484 retval = usb_control_msg(radio->usbdev,
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300485 usb_rcvctrlpipe(radio->usbdev, 0),
486 HID_REQ_GET_REPORT,
487 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300488 report[0], 2,
489 buf, size, usb_timeout);
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300490
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300491 if (retval < 0)
492 printk(KERN_WARNING DRIVER_NAME
493 ": si470x_get_report: usb_control_msg returned %d\n",
494 retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300495 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300496}
497
498
499/*
500 * si470x_set_report - send a HID report
501 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300502static int si470x_set_report(struct si470x_device *radio, void *buf, int size)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300503{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300504 unsigned char *report = (unsigned char *) buf;
505 int retval;
506
507 retval = usb_control_msg(radio->usbdev,
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300508 usb_sndctrlpipe(radio->usbdev, 0),
509 HID_REQ_SET_REPORT,
510 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300511 report[0], 2,
512 buf, size, usb_timeout);
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300513
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300514 if (retval < 0)
515 printk(KERN_WARNING DRIVER_NAME
516 ": si470x_set_report: usb_control_msg returned %d\n",
517 retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300518 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300519}
520
521
522/*
523 * si470x_get_register - read register
524 */
525static int si470x_get_register(struct si470x_device *radio, int regnr)
526{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300527 unsigned char buf[REGISTER_REPORT_SIZE];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300528 int retval;
529
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300530 buf[0] = REGISTER_REPORT(regnr);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300531
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300532 retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
533
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300534 if (retval >= 0)
Harvey Harrison5ad6b812008-04-15 22:22:11 -0300535 radio->registers[regnr] = get_unaligned_be16(&buf[1]);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300536
537 return (retval < 0) ? -EINVAL : 0;
538}
539
540
541/*
542 * si470x_set_register - write register
543 */
544static int si470x_set_register(struct si470x_device *radio, int regnr)
545{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300546 unsigned char buf[REGISTER_REPORT_SIZE];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300547 int retval;
548
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300549 buf[0] = REGISTER_REPORT(regnr);
Harvey Harrison5ad6b812008-04-15 22:22:11 -0300550 put_unaligned_be16(radio->registers[regnr], &buf[1]);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300551
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300552 retval = si470x_set_report(radio, (void *) &buf, sizeof(buf));
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300553
554 return (retval < 0) ? -EINVAL : 0;
555}
556
557
558/*
559 * si470x_get_all_registers - read entire registers
560 */
561static int si470x_get_all_registers(struct si470x_device *radio)
562{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300563 unsigned char buf[ENTIRE_REPORT_SIZE];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300564 int retval;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300565 unsigned char regnr;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300566
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300567 buf[0] = ENTIRE_REPORT;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300568
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300569 retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300570
571 if (retval >= 0)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300572 for (regnr = 0; regnr < RADIO_REGISTER_NUM; regnr++)
Harvey Harrison5ad6b812008-04-15 22:22:11 -0300573 radio->registers[regnr] = get_unaligned_be16(
574 &buf[regnr * RADIO_REGISTER_SIZE + 1]);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300575
576 return (retval < 0) ? -EINVAL : 0;
577}
578
579
580/*
581 * si470x_get_rds_registers - read rds registers
582 */
583static int si470x_get_rds_registers(struct si470x_device *radio)
584{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300585 unsigned char buf[RDS_REPORT_SIZE];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300586 int retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300587 int size;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300588 unsigned char regnr;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300589
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300590 buf[0] = RDS_REPORT;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300591
592 retval = usb_interrupt_msg(radio->usbdev,
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300593 usb_rcvintpipe(radio->usbdev, 1),
594 (void *) &buf, sizeof(buf), &size, usb_timeout);
595 if (size != sizeof(buf))
Tobias Lorenzce5829e2008-05-31 15:04:32 -0300596 printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: "
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300597 "return size differs: %d != %zu\n", size, sizeof(buf));
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300598 if (retval < 0)
599 printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: "
600 "usb_interrupt_msg returned %d\n", retval);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300601
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300602 if (retval >= 0)
603 for (regnr = 0; regnr < RDS_REGISTER_NUM; regnr++)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300604 radio->registers[STATUSRSSI + regnr] =
Tobias Lorenzce5829e2008-05-31 15:04:32 -0300605 get_unaligned_be16(
606 &buf[regnr * RADIO_REGISTER_SIZE + 1]);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300607
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300608 return (retval < 0) ? -EINVAL : 0;
609}
610
611
612/*
613 * si470x_set_chan - set the channel
614 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300615static int si470x_set_chan(struct si470x_device *radio, unsigned short chan)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300616{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300617 int retval;
618 unsigned long timeout;
619 bool timed_out = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300620
621 /* start tuning */
622 radio->registers[CHANNEL] &= ~CHANNEL_CHAN;
623 radio->registers[CHANNEL] |= CHANNEL_TUNE | chan;
624 retval = si470x_set_register(radio, CHANNEL);
625 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300626 goto done;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300627
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300628 /* wait till tune operation has completed */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300629 timeout = jiffies + msecs_to_jiffies(tune_timeout);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300630 do {
631 retval = si470x_get_register(radio, STATUSRSSI);
632 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300633 goto stop;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300634 timed_out = time_after(jiffies, timeout);
635 } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
636 (!timed_out));
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300637 if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
638 printk(KERN_WARNING DRIVER_NAME ": tune does not complete\n");
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300639 if (timed_out)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300640 printk(KERN_WARNING DRIVER_NAME
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300641 ": tune timed out after %u ms\n", tune_timeout);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300642
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300643stop:
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300644 /* stop tuning */
645 radio->registers[CHANNEL] &= ~CHANNEL_TUNE;
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300646 retval = si470x_set_register(radio, CHANNEL);
647
648done:
649 return retval;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300650}
651
652
653/*
654 * si470x_get_freq - get the frequency
655 */
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300656static int si470x_get_freq(struct si470x_device *radio, unsigned int *freq)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300657{
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300658 unsigned int spacing, band_bottom;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300659 unsigned short chan;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300660 int retval;
661
662 /* Spacing (kHz) */
663 switch (space) {
664 /* 0: 200 kHz (USA, Australia) */
665 case 0 : spacing = 0.200 * FREQ_MUL; break;
666 /* 1: 100 kHz (Europe, Japan) */
667 case 1 : spacing = 0.100 * FREQ_MUL; break;
668 /* 2: 50 kHz */
669 default: spacing = 0.050 * FREQ_MUL; break;
670 };
671
672 /* Bottom of Band (MHz) */
673 switch (band) {
674 /* 0: 87.5 - 108 MHz (USA, Europe) */
675 case 0 : band_bottom = 87.5 * FREQ_MUL; break;
676 /* 1: 76 - 108 MHz (Japan wide band) */
677 default: band_bottom = 76 * FREQ_MUL; break;
678 /* 2: 76 - 90 MHz (Japan) */
679 case 2 : band_bottom = 76 * FREQ_MUL; break;
680 };
681
682 /* read channel */
683 retval = si470x_get_register(radio, READCHAN);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300684 chan = radio->registers[READCHAN] & READCHAN_READCHAN;
685
686 /* Frequency (MHz) = Spacing (kHz) x Channel + Bottom of Band (MHz) */
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300687 *freq = chan * spacing + band_bottom;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300688
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300689 return retval;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300690}
691
692
693/*
694 * si470x_set_freq - set the frequency
695 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300696static int si470x_set_freq(struct si470x_device *radio, unsigned int freq)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300697{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300698 unsigned int spacing, band_bottom;
699 unsigned short chan;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300700
701 /* Spacing (kHz) */
702 switch (space) {
703 /* 0: 200 kHz (USA, Australia) */
704 case 0 : spacing = 0.200 * FREQ_MUL; break;
705 /* 1: 100 kHz (Europe, Japan) */
706 case 1 : spacing = 0.100 * FREQ_MUL; break;
707 /* 2: 50 kHz */
708 default: spacing = 0.050 * FREQ_MUL; break;
709 };
710
711 /* Bottom of Band (MHz) */
712 switch (band) {
713 /* 0: 87.5 - 108 MHz (USA, Europe) */
714 case 0 : band_bottom = 87.5 * FREQ_MUL; break;
715 /* 1: 76 - 108 MHz (Japan wide band) */
716 default: band_bottom = 76 * FREQ_MUL; break;
717 /* 2: 76 - 90 MHz (Japan) */
718 case 2 : band_bottom = 76 * FREQ_MUL; break;
719 };
720
721 /* Chan = [ Freq (Mhz) - Bottom of Band (MHz) ] / Spacing (kHz) */
722 chan = (freq - band_bottom) / spacing;
723
724 return si470x_set_chan(radio, chan);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300725}
726
727
728/*
729 * si470x_start - switch on radio
730 */
731static int si470x_start(struct si470x_device *radio)
732{
733 int retval;
734
735 /* powercfg */
736 radio->registers[POWERCFG] =
737 POWERCFG_DMUTE | POWERCFG_ENABLE | POWERCFG_RDSM;
738 retval = si470x_set_register(radio, POWERCFG);
739 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300740 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300741
742 /* sysconfig 1 */
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300743 radio->registers[SYSCONFIG1] = SYSCONFIG1_DE;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300744 retval = si470x_set_register(radio, SYSCONFIG1);
745 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300746 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300747
748 /* sysconfig 2 */
749 radio->registers[SYSCONFIG2] =
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300750 (0x3f << 8) | /* SEEKTH */
751 ((band << 6) & SYSCONFIG2_BAND) | /* BAND */
752 ((space << 4) & SYSCONFIG2_SPACE) | /* SPACE */
753 15; /* VOLUME (max) */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300754 retval = si470x_set_register(radio, SYSCONFIG2);
755 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300756 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300757
758 /* reset last channel */
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300759 retval = si470x_set_chan(radio,
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300760 radio->registers[CHANNEL] & CHANNEL_CHAN);
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300761
762done:
763 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300764}
765
766
767/*
768 * si470x_stop - switch off radio
769 */
770static int si470x_stop(struct si470x_device *radio)
771{
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300772 int retval;
773
774 /* sysconfig 1 */
775 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
776 retval = si470x_set_register(radio, SYSCONFIG1);
777 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300778 goto done;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300779
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300780 /* powercfg */
781 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
782 /* POWERCFG_ENABLE has to automatically go low */
783 radio->registers[POWERCFG] |= POWERCFG_ENABLE | POWERCFG_DISABLE;
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300784 retval = si470x_set_register(radio, POWERCFG);
785
786done:
787 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300788}
789
790
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300791/*
792 * si470x_rds_on - switch on rds reception
793 */
794static int si470x_rds_on(struct si470x_device *radio)
795{
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300796 int retval;
797
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300798 /* sysconfig 1 */
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300799 mutex_lock(&radio->lock);
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300800 radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDS;
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300801 retval = si470x_set_register(radio, SYSCONFIG1);
802 if (retval < 0)
803 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
804 mutex_unlock(&radio->lock);
805
806 return retval;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300807}
808
809
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300810
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300811/**************************************************************************
812 * RDS Driver Functions
813 **************************************************************************/
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300814
815/*
816 * si470x_rds - rds processing function
817 */
818static void si470x_rds(struct si470x_device *radio)
819{
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300820 unsigned char blocknum;
821 unsigned short bler; /* rds block errors */
822 unsigned short rds;
823 unsigned char tmpbuf[3];
824
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300825 /* get rds blocks */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300826 if (si470x_get_rds_registers(radio) < 0)
827 return;
828 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0) {
829 /* No RDS group ready */
830 return;
831 }
832 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSS) == 0) {
833 /* RDS decoder not synchronized */
834 return;
835 }
836
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300837 /* copy all four RDS blocks to internal buffer */
838 mutex_lock(&radio->lock);
839 for (blocknum = 0; blocknum < 4; blocknum++) {
840 switch (blocknum) {
841 default:
842 bler = (radio->registers[STATUSRSSI] &
843 STATUSRSSI_BLERA) >> 9;
844 rds = radio->registers[RDSA];
845 break;
846 case 1:
847 bler = (radio->registers[READCHAN] &
848 READCHAN_BLERB) >> 14;
849 rds = radio->registers[RDSB];
850 break;
851 case 2:
852 bler = (radio->registers[READCHAN] &
853 READCHAN_BLERC) >> 12;
854 rds = radio->registers[RDSC];
855 break;
856 case 3:
857 bler = (radio->registers[READCHAN] &
858 READCHAN_BLERD) >> 10;
859 rds = radio->registers[RDSD];
860 break;
861 };
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300862
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300863 /* Fill the V4L2 RDS buffer */
Harvey Harrison5ad6b812008-04-15 22:22:11 -0300864 put_unaligned_le16(rds, &tmpbuf);
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300865 tmpbuf[2] = blocknum; /* offset name */
866 tmpbuf[2] |= blocknum << 3; /* received offset */
867 if (bler > max_rds_errors)
868 tmpbuf[2] |= 0x80; /* uncorrectable errors */
869 else if (bler > 0)
870 tmpbuf[2] |= 0x40; /* corrected error(s) */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300871
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300872 /* copy RDS block to internal buffer */
873 memcpy(&radio->buffer[radio->wr_index], &tmpbuf, 3);
874 radio->wr_index += 3;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300875
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300876 /* wrap write pointer */
877 if (radio->wr_index >= radio->buf_size)
878 radio->wr_index = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300879
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300880 /* check for overflow */
881 if (radio->wr_index == radio->rd_index) {
882 /* increment and wrap read pointer */
883 radio->rd_index += 3;
884 if (radio->rd_index >= radio->buf_size)
885 radio->rd_index = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300886 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300887 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300888 mutex_unlock(&radio->lock);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300889
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300890 /* wake up read queue */
891 if (radio->wr_index != radio->rd_index)
892 wake_up_interruptible(&radio->read_queue);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300893}
894
895
896/*
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300897 * si470x_work - rds work function
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300898 */
899static void si470x_work(struct work_struct *work)
900{
901 struct si470x_device *radio = container_of(work, struct si470x_device,
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300902 work.work);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300903
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300904 /* safety checks */
Tobias Lorenzce5829e2008-05-31 15:04:32 -0300905 if (radio->disconnected)
906 return;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300907 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300908 return;
909
910 si470x_rds(radio);
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300911 schedule_delayed_work(&radio->work, msecs_to_jiffies(rds_poll_time));
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300912}
913
914
915
916/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300917 * File Operations Interface
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300918 **************************************************************************/
919
920/*
921 * si470x_fops_read - read RDS data
922 */
923static ssize_t si470x_fops_read(struct file *file, char __user *buf,
924 size_t count, loff_t *ppos)
925{
926 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300927 int retval = 0;
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300928 unsigned int block_count = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300929
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300930 /* switch on rds reception */
931 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
932 si470x_rds_on(radio);
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300933 schedule_delayed_work(&radio->work,
934 msecs_to_jiffies(rds_poll_time));
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300935 }
936
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300937 /* block if no new data available */
938 while (radio->wr_index == radio->rd_index) {
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300939 if (file->f_flags & O_NONBLOCK) {
940 retval = -EWOULDBLOCK;
941 goto done;
942 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300943 if (wait_event_interruptible(radio->read_queue,
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300944 radio->wr_index != radio->rd_index) < 0) {
945 retval = -EINTR;
946 goto done;
947 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300948 }
949
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300950 /* calculate block count from byte count */
951 count /= 3;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300952
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300953 /* copy RDS block out of internal buffer and to user buffer */
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300954 mutex_lock(&radio->lock);
955 while (block_count < count) {
956 if (radio->rd_index == radio->wr_index)
957 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300958
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300959 /* always transfer rds complete blocks */
960 if (copy_to_user(buf, &radio->buffer[radio->rd_index], 3))
961 /* retval = -EFAULT; */
962 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300963
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300964 /* increment and wrap read pointer */
965 radio->rd_index += 3;
966 if (radio->rd_index >= radio->buf_size)
967 radio->rd_index = 0;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300968
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300969 /* increment counters */
970 block_count++;
971 buf += 3;
972 retval += 3;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300973 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300974 mutex_unlock(&radio->lock);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300975
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300976done:
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300977 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300978}
979
980
981/*
982 * si470x_fops_poll - poll RDS data
983 */
984static unsigned int si470x_fops_poll(struct file *file,
985 struct poll_table_struct *pts)
986{
987 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300988 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300989
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300990 /* switch on rds reception */
991 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
992 si470x_rds_on(radio);
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300993 schedule_delayed_work(&radio->work,
994 msecs_to_jiffies(rds_poll_time));
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300995 }
996
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300997 poll_wait(file, &radio->read_queue, pts);
998
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300999 if (radio->rd_index != radio->wr_index)
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001000 retval = POLLIN | POLLRDNORM;
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001001
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001002 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001003}
1004
1005
1006/*
1007 * si470x_fops_open - file open
1008 */
1009static int si470x_fops_open(struct inode *inode, struct file *file)
1010{
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001011 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001012 int retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001013
1014 radio->users++;
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001015
1016 retval = usb_autopm_get_interface(radio->intf);
1017 if (retval < 0) {
1018 radio->users--;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001019 retval = -EIO;
1020 goto done;
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001021 }
1022
1023 if (radio->users == 1) {
1024 retval = si470x_start(radio);
1025 if (retval < 0)
1026 usb_autopm_put_interface(radio->intf);
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001027 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001028
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001029done:
1030 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001031}
1032
1033
1034/*
1035 * si470x_fops_release - file release
1036 */
1037static int si470x_fops_release(struct inode *inode, struct file *file)
1038{
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001039 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001040 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001041
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001042 /* safety check */
1043 if (!radio) {
1044 retval = -ENODEV;
1045 goto done;
1046 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001047
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001048 mutex_lock(&radio->disconnect_lock);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001049 radio->users--;
1050 if (radio->users == 0) {
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001051 if (radio->disconnected) {
1052 video_unregister_device(radio->videodev);
1053 kfree(radio->buffer);
1054 kfree(radio);
1055 goto unlock;
1056 }
Tobias Lorenz03dea862008-04-22 14:46:11 -03001057
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001058 /* stop rds reception */
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001059 cancel_delayed_work_sync(&radio->work);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001060
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001061 /* cancel read processes */
1062 wake_up_interruptible(&radio->read_queue);
1063
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001064 retval = si470x_stop(radio);
1065 usb_autopm_put_interface(radio->intf);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001066 }
1067
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001068unlock:
1069 mutex_unlock(&radio->disconnect_lock);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001070
1071done:
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001072 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001073}
1074
1075
1076/*
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001077 * si470x_fops - file operations interface
1078 */
1079static const struct file_operations si470x_fops = {
1080 .owner = THIS_MODULE,
1081 .llseek = no_llseek,
1082 .read = si470x_fops_read,
1083 .poll = si470x_fops_poll,
1084 .ioctl = video_ioctl2,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -03001085#ifdef CONFIG_COMPAT
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001086 .compat_ioctl = v4l_compat_ioctl32,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -03001087#endif
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001088 .open = si470x_fops_open,
1089 .release = si470x_fops_release,
1090};
1091
1092
1093
1094/**************************************************************************
1095 * Video4Linux Interface
1096 **************************************************************************/
1097
1098/*
1099 * si470x_v4l2_queryctrl - query control
1100 */
1101static struct v4l2_queryctrl si470x_v4l2_queryctrl[] = {
1102/* HINT: the disabled controls are only here to satify kradio and such apps */
1103 {
1104 .id = V4L2_CID_AUDIO_VOLUME,
1105 .type = V4L2_CTRL_TYPE_INTEGER,
1106 .name = "Volume",
1107 .minimum = 0,
1108 .maximum = 15,
1109 .step = 1,
1110 .default_value = 15,
1111 },
1112 {
1113 .id = V4L2_CID_AUDIO_BALANCE,
1114 .flags = V4L2_CTRL_FLAG_DISABLED,
1115 },
1116 {
1117 .id = V4L2_CID_AUDIO_BASS,
1118 .flags = V4L2_CTRL_FLAG_DISABLED,
1119 },
1120 {
1121 .id = V4L2_CID_AUDIO_TREBLE,
1122 .flags = V4L2_CTRL_FLAG_DISABLED,
1123 },
1124 {
1125 .id = V4L2_CID_AUDIO_MUTE,
1126 .type = V4L2_CTRL_TYPE_BOOLEAN,
1127 .name = "Mute",
1128 .minimum = 0,
1129 .maximum = 1,
1130 .step = 1,
1131 .default_value = 1,
1132 },
1133 {
1134 .id = V4L2_CID_AUDIO_LOUDNESS,
1135 .flags = V4L2_CTRL_FLAG_DISABLED,
1136 },
1137};
1138
1139
1140/*
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001141 * si470x_vidioc_querycap - query device capabilities
1142 */
1143static int si470x_vidioc_querycap(struct file *file, void *priv,
1144 struct v4l2_capability *capability)
1145{
1146 strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver));
1147 strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
1148 sprintf(capability->bus_info, "USB");
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001149 capability->version = DRIVER_KERNEL_VERSION;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001150 capability->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
1151
1152 return 0;
1153}
1154
1155
1156/*
1157 * si470x_vidioc_g_input - get input
1158 */
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001159static int si470x_vidioc_g_input(struct file *file, void *priv,
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001160 unsigned int *i)
1161{
1162 *i = 0;
1163
1164 return 0;
1165}
1166
1167
1168/*
1169 * si470x_vidioc_s_input - set input
1170 */
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001171static int si470x_vidioc_s_input(struct file *file, void *priv, unsigned int i)
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001172{
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001173 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001174
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001175 /* safety checks */
1176 if (i != 0)
1177 retval = -EINVAL;
1178
1179 if (retval < 0)
1180 printk(KERN_WARNING DRIVER_NAME
1181 ": set input failed with %d\n", retval);
1182 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001183}
1184
1185
1186/*
1187 * si470x_vidioc_queryctrl - enumerate control items
1188 */
1189static int si470x_vidioc_queryctrl(struct file *file, void *priv,
1190 struct v4l2_queryctrl *qc)
1191{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001192 unsigned char i;
1193 int retval = -EINVAL;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001194
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001195 /* safety checks */
1196 if (!qc->id)
1197 goto done;
1198
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001199 for (i = 0; i < ARRAY_SIZE(si470x_v4l2_queryctrl); i++) {
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001200 if (qc->id == si470x_v4l2_queryctrl[i].id) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001201 memcpy(qc, &(si470x_v4l2_queryctrl[i]), sizeof(*qc));
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001202 retval = 0;
1203 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001204 }
1205 }
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001206
1207done:
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001208 if (retval < 0)
1209 printk(KERN_WARNING DRIVER_NAME
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001210 ": query controls failed with %d\n", retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001211 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001212}
1213
1214
1215/*
1216 * si470x_vidioc_g_ctrl - get the value of a control
1217 */
1218static int si470x_vidioc_g_ctrl(struct file *file, void *priv,
1219 struct v4l2_control *ctrl)
1220{
1221 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001222 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001223
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001224 /* safety checks */
1225 if (radio->disconnected) {
1226 retval = -EIO;
1227 goto done;
1228 }
Tobias Lorenz03dea862008-04-22 14:46:11 -03001229
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001230 switch (ctrl->id) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001231 case V4L2_CID_AUDIO_VOLUME:
1232 ctrl->value = radio->registers[SYSCONFIG2] &
1233 SYSCONFIG2_VOLUME;
1234 break;
1235 case V4L2_CID_AUDIO_MUTE:
1236 ctrl->value = ((radio->registers[POWERCFG] &
1237 POWERCFG_DMUTE) == 0) ? 1 : 0;
1238 break;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001239 default:
1240 retval = -EINVAL;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001241 }
1242
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001243done:
1244 if (retval < 0)
1245 printk(KERN_WARNING DRIVER_NAME
1246 ": get control failed with %d\n", retval);
1247 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001248}
1249
1250
1251/*
1252 * si470x_vidioc_s_ctrl - set the value of a control
1253 */
1254static int si470x_vidioc_s_ctrl(struct file *file, void *priv,
1255 struct v4l2_control *ctrl)
1256{
1257 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001258 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001259
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001260 /* safety checks */
1261 if (radio->disconnected) {
1262 retval = -EIO;
1263 goto done;
1264 }
Tobias Lorenz03dea862008-04-22 14:46:11 -03001265
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001266 switch (ctrl->id) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001267 case V4L2_CID_AUDIO_VOLUME:
1268 radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_VOLUME;
1269 radio->registers[SYSCONFIG2] |= ctrl->value;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001270 retval = si470x_set_register(radio, SYSCONFIG2);
1271 break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001272 case V4L2_CID_AUDIO_MUTE:
1273 if (ctrl->value == 1)
1274 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
1275 else
1276 radio->registers[POWERCFG] |= POWERCFG_DMUTE;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001277 retval = si470x_set_register(radio, POWERCFG);
1278 break;
1279 default:
1280 retval = -EINVAL;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001281 }
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001282
1283done:
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001284 if (retval < 0)
1285 printk(KERN_WARNING DRIVER_NAME
1286 ": set control failed with %d\n", retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001287 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001288}
1289
1290
1291/*
1292 * si470x_vidioc_g_audio - get audio attributes
1293 */
1294static int si470x_vidioc_g_audio(struct file *file, void *priv,
1295 struct v4l2_audio *audio)
1296{
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001297 int retval = 0;
1298
1299 /* safety checks */
1300 if (audio->index != 0) {
1301 retval = -EINVAL;
1302 goto done;
1303 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001304
1305 strcpy(audio->name, "Radio");
1306 audio->capability = V4L2_AUDCAP_STEREO;
1307
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001308done:
1309 if (retval < 0)
1310 printk(KERN_WARNING DRIVER_NAME
1311 ": get audio failed with %d\n", retval);
1312 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001313}
1314
1315
1316/*
1317 * si470x_vidioc_s_audio - set audio attributes
1318 */
1319static int si470x_vidioc_s_audio(struct file *file, void *priv,
1320 struct v4l2_audio *audio)
1321{
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001322 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001323
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001324 /* safety checks */
1325 if (audio->index != 0) {
1326 retval = -EINVAL;
1327 goto done;
1328 }
1329
1330done:
1331 if (retval < 0)
1332 printk(KERN_WARNING DRIVER_NAME
1333 ": set audio failed with %d\n", retval);
1334 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001335}
1336
1337
1338/*
1339 * si470x_vidioc_g_tuner - get tuner attributes
1340 */
1341static int si470x_vidioc_g_tuner(struct file *file, void *priv,
1342 struct v4l2_tuner *tuner)
1343{
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001344 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001345 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001346
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001347 /* safety checks */
1348 if (radio->disconnected) {
1349 retval = -EIO;
1350 goto done;
1351 }
1352 if ((tuner->index != 0) && (tuner->type != V4L2_TUNER_RADIO)) {
1353 retval = -EINVAL;
1354 goto done;
1355 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001356
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001357 retval = si470x_get_register(radio, STATUSRSSI);
1358 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001359 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001360
1361 strcpy(tuner->name, "FM");
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001362 switch (band) {
1363 /* 0: 87.5 - 108 MHz (USA, Europe, default) */
1364 default:
1365 tuner->rangelow = 87.5 * FREQ_MUL;
1366 tuner->rangehigh = 108 * FREQ_MUL;
1367 break;
1368 /* 1: 76 - 108 MHz (Japan wide band) */
1369 case 1 :
1370 tuner->rangelow = 76 * FREQ_MUL;
1371 tuner->rangehigh = 108 * FREQ_MUL;
1372 break;
1373 /* 2: 76 - 90 MHz (Japan) */
1374 case 2 :
1375 tuner->rangelow = 76 * FREQ_MUL;
1376 tuner->rangehigh = 90 * FREQ_MUL;
1377 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001378 };
1379 tuner->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
1380 tuner->capability = V4L2_TUNER_CAP_LOW;
1381
1382 /* Stereo indicator == Stereo (instead of Mono) */
1383 if ((radio->registers[STATUSRSSI] & STATUSRSSI_ST) == 1)
1384 tuner->audmode = V4L2_TUNER_MODE_STEREO;
1385 else
1386 tuner->audmode = V4L2_TUNER_MODE_MONO;
1387
1388 /* min is worst, max is best; signal:0..0xffff; rssi: 0..0xff */
1389 tuner->signal = (radio->registers[STATUSRSSI] & STATUSRSSI_RSSI)
1390 * 0x0101;
1391
1392 /* automatic frequency control: -1: freq to low, 1 freq to high */
1393 tuner->afc = 0;
1394
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001395done:
1396 if (retval < 0)
1397 printk(KERN_WARNING DRIVER_NAME
1398 ": get tuner failed with %d\n", retval);
1399 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001400}
1401
1402
1403/*
1404 * si470x_vidioc_s_tuner - set tuner attributes
1405 */
1406static int si470x_vidioc_s_tuner(struct file *file, void *priv,
1407 struct v4l2_tuner *tuner)
1408{
1409 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001410 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001411
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001412 /* safety checks */
1413 if (radio->disconnected) {
1414 retval = -EIO;
1415 goto done;
1416 }
1417 if ((tuner->index != 0) && (tuner->type != V4L2_TUNER_RADIO)) {
1418 retval = -EINVAL;
1419 goto done;
1420 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001421
1422 if (tuner->audmode == V4L2_TUNER_MODE_MONO)
1423 radio->registers[POWERCFG] |= POWERCFG_MONO; /* force mono */
1424 else
1425 radio->registers[POWERCFG] &= ~POWERCFG_MONO; /* try stereo */
1426
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001427 retval = si470x_set_register(radio, POWERCFG);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001428
1429done:
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001430 if (retval < 0)
1431 printk(KERN_WARNING DRIVER_NAME
1432 ": set tuner failed with %d\n", retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001433 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001434}
1435
1436
1437/*
1438 * si470x_vidioc_g_frequency - get tuner or modulator radio frequency
1439 */
1440static int si470x_vidioc_g_frequency(struct file *file, void *priv,
1441 struct v4l2_frequency *freq)
1442{
1443 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001444 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001445
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001446 /* safety checks */
1447 if (radio->disconnected) {
1448 retval = -EIO;
1449 goto done;
1450 }
1451 if ((freq->tuner != 0) && (freq->type != V4L2_TUNER_RADIO)) {
1452 retval = -EINVAL;
1453 goto done;
1454 }
Tobias Lorenz03dea862008-04-22 14:46:11 -03001455
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001456 retval = si470x_get_freq(radio, &freq->frequency);
1457
1458done:
1459 if (retval < 0)
1460 printk(KERN_WARNING DRIVER_NAME
1461 ": get frequency failed with %d\n", retval);
1462 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001463}
1464
1465
1466/*
1467 * si470x_vidioc_s_frequency - set tuner or modulator radio frequency
1468 */
1469static int si470x_vidioc_s_frequency(struct file *file, void *priv,
1470 struct v4l2_frequency *freq)
1471{
1472 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001473 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001474
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001475 /* safety checks */
1476 if (radio->disconnected) {
1477 retval = -EIO;
1478 goto done;
1479 }
1480 if ((freq->tuner != 0) && (freq->type != V4L2_TUNER_RADIO)) {
1481 retval = -EINVAL;
1482 goto done;
1483 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001484
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001485 retval = si470x_set_freq(radio, freq->frequency);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001486
1487done:
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001488 if (retval < 0)
1489 printk(KERN_WARNING DRIVER_NAME
1490 ": set frequency failed with %d\n", retval);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001491 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001492}
1493
1494
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001495/*
1496 * si470x_viddev_tamples - video device interface
1497 */
1498static struct video_device si470x_viddev_template = {
1499 .fops = &si470x_fops,
1500 .name = DRIVER_NAME,
1501 .type = VID_TYPE_TUNER,
1502 .release = video_device_release,
1503 .vidioc_querycap = si470x_vidioc_querycap,
1504 .vidioc_g_input = si470x_vidioc_g_input,
1505 .vidioc_s_input = si470x_vidioc_s_input,
1506 .vidioc_queryctrl = si470x_vidioc_queryctrl,
1507 .vidioc_g_ctrl = si470x_vidioc_g_ctrl,
1508 .vidioc_s_ctrl = si470x_vidioc_s_ctrl,
1509 .vidioc_g_audio = si470x_vidioc_g_audio,
1510 .vidioc_s_audio = si470x_vidioc_s_audio,
1511 .vidioc_g_tuner = si470x_vidioc_g_tuner,
1512 .vidioc_s_tuner = si470x_vidioc_s_tuner,
1513 .vidioc_g_frequency = si470x_vidioc_g_frequency,
1514 .vidioc_s_frequency = si470x_vidioc_s_frequency,
1515 .owner = THIS_MODULE,
1516};
1517
1518
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001519
1520/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001521 * USB Interface
1522 **************************************************************************/
1523
1524/*
1525 * si470x_usb_driver_probe - probe for the device
1526 */
1527static int si470x_usb_driver_probe(struct usb_interface *intf,
1528 const struct usb_device_id *id)
1529{
1530 struct si470x_device *radio;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001531 int retval = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001532
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001533 /* private data allocation and initialization */
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001534 radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001535 if (!radio) {
1536 retval = -ENOMEM;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001537 goto err_initial;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001538 }
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001539 radio->users = 0;
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001540 radio->disconnected = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001541 radio->usbdev = interface_to_usbdev(intf);
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001542 radio->intf = intf;
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001543 mutex_init(&radio->disconnect_lock);
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001544 mutex_init(&radio->lock);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001545
1546 /* video device allocation and initialization */
1547 radio->videodev = video_device_alloc();
1548 if (!radio->videodev) {
1549 retval = -ENOMEM;
1550 goto err_radio;
1551 }
1552 memcpy(radio->videodev, &si470x_viddev_template,
1553 sizeof(si470x_viddev_template));
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001554 video_set_drvdata(radio->videodev, radio);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001555
1556 /* show some infos about the specific device */
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001557 if (si470x_get_all_registers(radio) < 0) {
1558 retval = -EIO;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001559 goto err_all;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001560 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001561 printk(KERN_INFO DRIVER_NAME ": DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001562 radio->registers[DEVICEID], radio->registers[CHIPID]);
1563
1564 /* check if firmware is current */
1565 if ((radio->registers[CHIPID] & CHIPID_FIRMWARE)
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001566 < RADIO_SW_VERSION_CURRENT) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001567 printk(KERN_WARNING DRIVER_NAME
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001568 ": This driver is known to work with "
1569 "firmware version %hu,\n", RADIO_SW_VERSION_CURRENT);
1570 printk(KERN_WARNING DRIVER_NAME
1571 ": but the device has firmware version %hu.\n",
1572 radio->registers[CHIPID] & CHIPID_FIRMWARE);
1573 printk(KERN_WARNING DRIVER_NAME
1574 ": If you have some trouble using this driver,\n");
1575 printk(KERN_WARNING DRIVER_NAME
1576 ": please report to V4L ML at "
1577 "video4linux-list@redhat.com\n");
1578 }
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001579
1580 /* set initial frequency */
1581 si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
1582
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001583 /* rds buffer allocation */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001584 radio->buf_size = rds_buf * 3;
1585 radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001586 if (!radio->buffer) {
1587 retval = -EIO;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001588 goto err_all;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001589 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001590
1591 /* rds buffer configuration */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001592 radio->wr_index = 0;
1593 radio->rd_index = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001594 init_waitqueue_head(&radio->read_queue);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001595
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001596 /* prepare rds work function */
1597 INIT_DELAYED_WORK(&radio->work, si470x_work);
1598
1599 /* register video device */
1600 if (video_register_device(radio->videodev, VFL_TYPE_RADIO, radio_nr)) {
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001601 retval = -EIO;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001602 printk(KERN_WARNING DRIVER_NAME
1603 ": Could not register video device\n");
1604 goto err_all;
1605 }
1606 usb_set_intfdata(intf, radio);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001607
1608 return 0;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001609err_all:
1610 video_device_release(radio->videodev);
1611 kfree(radio->buffer);
1612err_radio:
1613 kfree(radio);
1614err_initial:
1615 return retval;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001616}
1617
1618
1619/*
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001620 * si470x_usb_driver_suspend - suspend the device
1621 */
1622static int si470x_usb_driver_suspend(struct usb_interface *intf,
1623 pm_message_t message)
1624{
1625 struct si470x_device *radio = usb_get_intfdata(intf);
1626
1627 printk(KERN_INFO DRIVER_NAME ": suspending now...\n");
1628
1629 cancel_delayed_work_sync(&radio->work);
1630
1631 return 0;
1632}
1633
1634
1635/*
1636 * si470x_usb_driver_resume - resume the device
1637 */
1638static int si470x_usb_driver_resume(struct usb_interface *intf)
1639{
1640 struct si470x_device *radio = usb_get_intfdata(intf);
1641
1642 printk(KERN_INFO DRIVER_NAME ": resuming now...\n");
1643
1644 mutex_lock(&radio->lock);
1645 if (radio->users && radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS)
1646 schedule_delayed_work(&radio->work,
1647 msecs_to_jiffies(rds_poll_time));
1648 mutex_unlock(&radio->lock);
1649
1650 return 0;
1651}
1652
1653
1654/*
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001655 * si470x_usb_driver_disconnect - disconnect the device
1656 */
1657static void si470x_usb_driver_disconnect(struct usb_interface *intf)
1658{
1659 struct si470x_device *radio = usb_get_intfdata(intf);
1660
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001661 mutex_lock(&radio->disconnect_lock);
1662 radio->disconnected = 1;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001663 cancel_delayed_work_sync(&radio->work);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001664 usb_set_intfdata(intf, NULL);
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001665 if (radio->users == 0) {
1666 video_unregister_device(radio->videodev);
1667 kfree(radio->buffer);
1668 kfree(radio);
1669 }
1670 mutex_unlock(&radio->disconnect_lock);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001671}
1672
1673
1674/*
1675 * si470x_usb_driver - usb driver interface
1676 */
1677static struct usb_driver si470x_usb_driver = {
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001678 .name = DRIVER_NAME,
1679 .probe = si470x_usb_driver_probe,
1680 .disconnect = si470x_usb_driver_disconnect,
1681 .suspend = si470x_usb_driver_suspend,
1682 .resume = si470x_usb_driver_resume,
1683 .id_table = si470x_usb_driver_id_table,
1684 .supports_autosuspend = 1,
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001685};
1686
1687
1688
1689/**************************************************************************
1690 * Module Interface
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001691 **************************************************************************/
1692
1693/*
1694 * si470x_module_init - module init
1695 */
1696static int __init si470x_module_init(void)
1697{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001698 printk(KERN_INFO DRIVER_DESC ", Version " DRIVER_VERSION "\n");
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001699 return usb_register(&si470x_usb_driver);
1700}
1701
1702
1703/*
1704 * si470x_module_exit - module exit
1705 */
1706static void __exit si470x_module_exit(void)
1707{
1708 usb_deregister(&si470x_usb_driver);
1709}
1710
1711
1712module_init(si470x_module_init);
1713module_exit(si470x_module_exit);
1714
1715MODULE_LICENSE("GPL");
1716MODULE_AUTHOR(DRIVER_AUTHOR);
1717MODULE_DESCRIPTION(DRIVER_DESC);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001718MODULE_VERSION(DRIVER_VERSION);