blob: 7df1163d709708ad18410d95fec04cc3e05b0753 [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
104 * - 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);
490 if (retval < 0)
491 printk(KERN_WARNING DRIVER_NAME
492 ": si470x_get_report: usb_control_msg returned %d\n",
493 retval);
494
495 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);
513 if (retval < 0)
514 printk(KERN_WARNING DRIVER_NAME
515 ": si470x_set_report: usb_control_msg returned %d\n",
516 retval);
517
518 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)
626 return retval;
627
628 /* wait till seek 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)
633 return retval;
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));
637 if (timed_out)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300638 printk(KERN_WARNING DRIVER_NAME
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300639 ": seek does not finish after %u ms\n", tune_timeout);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300640
641 /* stop tuning */
642 radio->registers[CHANNEL] &= ~CHANNEL_TUNE;
643 return si470x_set_register(radio, CHANNEL);
644}
645
646
647/*
648 * si470x_get_freq - get the frequency
649 */
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300650static int si470x_get_freq(struct si470x_device *radio, unsigned int *freq)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300651{
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300652 unsigned int spacing, band_bottom;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300653 unsigned short chan;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300654 int retval;
655
656 /* Spacing (kHz) */
657 switch (space) {
658 /* 0: 200 kHz (USA, Australia) */
659 case 0 : spacing = 0.200 * FREQ_MUL; break;
660 /* 1: 100 kHz (Europe, Japan) */
661 case 1 : spacing = 0.100 * FREQ_MUL; break;
662 /* 2: 50 kHz */
663 default: spacing = 0.050 * FREQ_MUL; break;
664 };
665
666 /* Bottom of Band (MHz) */
667 switch (band) {
668 /* 0: 87.5 - 108 MHz (USA, Europe) */
669 case 0 : band_bottom = 87.5 * FREQ_MUL; break;
670 /* 1: 76 - 108 MHz (Japan wide band) */
671 default: band_bottom = 76 * FREQ_MUL; break;
672 /* 2: 76 - 90 MHz (Japan) */
673 case 2 : band_bottom = 76 * FREQ_MUL; break;
674 };
675
676 /* read channel */
677 retval = si470x_get_register(radio, READCHAN);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300678 chan = radio->registers[READCHAN] & READCHAN_READCHAN;
679
680 /* Frequency (MHz) = Spacing (kHz) x Channel + Bottom of Band (MHz) */
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300681 *freq = chan * spacing + band_bottom;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300682
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300683 return retval;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300684}
685
686
687/*
688 * si470x_set_freq - set the frequency
689 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300690static int si470x_set_freq(struct si470x_device *radio, unsigned int freq)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300691{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300692 unsigned int spacing, band_bottom;
693 unsigned short chan;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300694
695 /* Spacing (kHz) */
696 switch (space) {
697 /* 0: 200 kHz (USA, Australia) */
698 case 0 : spacing = 0.200 * FREQ_MUL; break;
699 /* 1: 100 kHz (Europe, Japan) */
700 case 1 : spacing = 0.100 * FREQ_MUL; break;
701 /* 2: 50 kHz */
702 default: spacing = 0.050 * FREQ_MUL; break;
703 };
704
705 /* Bottom of Band (MHz) */
706 switch (band) {
707 /* 0: 87.5 - 108 MHz (USA, Europe) */
708 case 0 : band_bottom = 87.5 * FREQ_MUL; break;
709 /* 1: 76 - 108 MHz (Japan wide band) */
710 default: band_bottom = 76 * FREQ_MUL; break;
711 /* 2: 76 - 90 MHz (Japan) */
712 case 2 : band_bottom = 76 * FREQ_MUL; break;
713 };
714
715 /* Chan = [ Freq (Mhz) - Bottom of Band (MHz) ] / Spacing (kHz) */
716 chan = (freq - band_bottom) / spacing;
717
718 return si470x_set_chan(radio, chan);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300719}
720
721
722/*
723 * si470x_start - switch on radio
724 */
725static int si470x_start(struct si470x_device *radio)
726{
727 int retval;
728
729 /* powercfg */
730 radio->registers[POWERCFG] =
731 POWERCFG_DMUTE | POWERCFG_ENABLE | POWERCFG_RDSM;
732 retval = si470x_set_register(radio, POWERCFG);
733 if (retval < 0)
734 return retval;
735
736 /* sysconfig 1 */
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300737 radio->registers[SYSCONFIG1] = SYSCONFIG1_DE;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300738 retval = si470x_set_register(radio, SYSCONFIG1);
739 if (retval < 0)
740 return retval;
741
742 /* sysconfig 2 */
743 radio->registers[SYSCONFIG2] =
744 (0x3f << 8) | /* SEEKTH */
745 (band << 6) | /* BAND */
746 (space << 4) | /* SPACE */
747 15; /* VOLUME (max) */
748 retval = si470x_set_register(radio, SYSCONFIG2);
749 if (retval < 0)
750 return retval;
751
752 /* reset last channel */
753 return si470x_set_chan(radio,
754 radio->registers[CHANNEL] & CHANNEL_CHAN);
755}
756
757
758/*
759 * si470x_stop - switch off radio
760 */
761static int si470x_stop(struct si470x_device *radio)
762{
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300763 int retval;
764
765 /* sysconfig 1 */
766 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
767 retval = si470x_set_register(radio, SYSCONFIG1);
768 if (retval < 0)
769 return retval;
770
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300771 /* powercfg */
772 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
773 /* POWERCFG_ENABLE has to automatically go low */
774 radio->registers[POWERCFG] |= POWERCFG_ENABLE | POWERCFG_DISABLE;
775 return si470x_set_register(radio, POWERCFG);
776}
777
778
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300779/*
780 * si470x_rds_on - switch on rds reception
781 */
782static int si470x_rds_on(struct si470x_device *radio)
783{
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300784 int retval;
785
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300786 /* sysconfig 1 */
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300787 mutex_lock(&radio->lock);
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300788 radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDS;
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300789 retval = si470x_set_register(radio, SYSCONFIG1);
790 if (retval < 0)
791 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
792 mutex_unlock(&radio->lock);
793
794 return retval;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300795}
796
797
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300798
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300799/**************************************************************************
800 * RDS Driver Functions
801 **************************************************************************/
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300802
803/*
804 * si470x_rds - rds processing function
805 */
806static void si470x_rds(struct si470x_device *radio)
807{
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300808 unsigned char blocknum;
809 unsigned short bler; /* rds block errors */
810 unsigned short rds;
811 unsigned char tmpbuf[3];
812
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300813 /* get rds blocks */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300814 if (si470x_get_rds_registers(radio) < 0)
815 return;
816 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0) {
817 /* No RDS group ready */
818 return;
819 }
820 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSS) == 0) {
821 /* RDS decoder not synchronized */
822 return;
823 }
824
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300825 /* copy all four RDS blocks to internal buffer */
826 mutex_lock(&radio->lock);
827 for (blocknum = 0; blocknum < 4; blocknum++) {
828 switch (blocknum) {
829 default:
830 bler = (radio->registers[STATUSRSSI] &
831 STATUSRSSI_BLERA) >> 9;
832 rds = radio->registers[RDSA];
833 break;
834 case 1:
835 bler = (radio->registers[READCHAN] &
836 READCHAN_BLERB) >> 14;
837 rds = radio->registers[RDSB];
838 break;
839 case 2:
840 bler = (radio->registers[READCHAN] &
841 READCHAN_BLERC) >> 12;
842 rds = radio->registers[RDSC];
843 break;
844 case 3:
845 bler = (radio->registers[READCHAN] &
846 READCHAN_BLERD) >> 10;
847 rds = radio->registers[RDSD];
848 break;
849 };
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300850
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300851 /* Fill the V4L2 RDS buffer */
Harvey Harrison5ad6b812008-04-15 22:22:11 -0300852 put_unaligned_le16(rds, &tmpbuf);
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300853 tmpbuf[2] = blocknum; /* offset name */
854 tmpbuf[2] |= blocknum << 3; /* received offset */
855 if (bler > max_rds_errors)
856 tmpbuf[2] |= 0x80; /* uncorrectable errors */
857 else if (bler > 0)
858 tmpbuf[2] |= 0x40; /* corrected error(s) */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300859
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300860 /* copy RDS block to internal buffer */
861 memcpy(&radio->buffer[radio->wr_index], &tmpbuf, 3);
862 radio->wr_index += 3;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300863
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300864 /* wrap write pointer */
865 if (radio->wr_index >= radio->buf_size)
866 radio->wr_index = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300867
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300868 /* check for overflow */
869 if (radio->wr_index == radio->rd_index) {
870 /* increment and wrap read pointer */
871 radio->rd_index += 3;
872 if (radio->rd_index >= radio->buf_size)
873 radio->rd_index = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300874 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300875 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300876 mutex_unlock(&radio->lock);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300877
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300878 /* wake up read queue */
879 if (radio->wr_index != radio->rd_index)
880 wake_up_interruptible(&radio->read_queue);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300881}
882
883
884/*
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300885 * si470x_work - rds work function
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300886 */
887static void si470x_work(struct work_struct *work)
888{
889 struct si470x_device *radio = container_of(work, struct si470x_device,
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300890 work.work);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300891
Tobias Lorenzce5829e2008-05-31 15:04:32 -0300892 if (radio->disconnected)
893 return;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300894 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300895 return;
896
897 si470x_rds(radio);
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300898 schedule_delayed_work(&radio->work, msecs_to_jiffies(rds_poll_time));
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300899}
900
901
902
903/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300904 * File Operations Interface
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300905 **************************************************************************/
906
907/*
908 * si470x_fops_read - read RDS data
909 */
910static ssize_t si470x_fops_read(struct file *file, char __user *buf,
911 size_t count, loff_t *ppos)
912{
913 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300914 int retval = 0;
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300915 unsigned int block_count = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300916
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300917 /* switch on rds reception */
918 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
919 si470x_rds_on(radio);
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300920 schedule_delayed_work(&radio->work,
921 msecs_to_jiffies(rds_poll_time));
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300922 }
923
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300924 /* block if no new data available */
925 while (radio->wr_index == radio->rd_index) {
926 if (file->f_flags & O_NONBLOCK)
927 return -EWOULDBLOCK;
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300928 if (wait_event_interruptible(radio->read_queue,
929 radio->wr_index != radio->rd_index) < 0)
930 return -EINTR;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300931 }
932
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300933 /* calculate block count from byte count */
934 count /= 3;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300935
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300936 /* copy RDS block out of internal buffer and to user buffer */
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300937 mutex_lock(&radio->lock);
938 while (block_count < count) {
939 if (radio->rd_index == radio->wr_index)
940 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300941
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300942 /* always transfer rds complete blocks */
943 if (copy_to_user(buf, &radio->buffer[radio->rd_index], 3))
944 /* retval = -EFAULT; */
945 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300946
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300947 /* increment and wrap read pointer */
948 radio->rd_index += 3;
949 if (radio->rd_index >= radio->buf_size)
950 radio->rd_index = 0;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300951
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300952 /* increment counters */
953 block_count++;
954 buf += 3;
955 retval += 3;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300956 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300957 mutex_unlock(&radio->lock);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300958
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300959 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300960}
961
962
963/*
964 * si470x_fops_poll - poll RDS data
965 */
966static unsigned int si470x_fops_poll(struct file *file,
967 struct poll_table_struct *pts)
968{
969 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300970
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300971 /* switch on rds reception */
972 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
973 si470x_rds_on(radio);
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300974 schedule_delayed_work(&radio->work,
975 msecs_to_jiffies(rds_poll_time));
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300976 }
977
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300978 poll_wait(file, &radio->read_queue, pts);
979
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300980 if (radio->rd_index != radio->wr_index)
981 return POLLIN | POLLRDNORM;
982
983 return 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300984}
985
986
987/*
988 * si470x_fops_open - file open
989 */
990static int si470x_fops_open(struct inode *inode, struct file *file)
991{
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300992 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300993 int retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300994
995 radio->users++;
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300996
997 retval = usb_autopm_get_interface(radio->intf);
998 if (retval < 0) {
999 radio->users--;
1000 return -EIO;
1001 }
1002
1003 if (radio->users == 1) {
1004 retval = si470x_start(radio);
1005 if (retval < 0)
1006 usb_autopm_put_interface(radio->intf);
1007 return retval;
1008 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001009
1010 return 0;
1011}
1012
1013
1014/*
1015 * si470x_fops_release - file release
1016 */
1017static int si470x_fops_release(struct inode *inode, struct file *file)
1018{
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001019 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001020 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001021
1022 if (!radio)
1023 return -ENODEV;
1024
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001025 mutex_lock(&radio->disconnect_lock);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001026 radio->users--;
1027 if (radio->users == 0) {
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001028 if (radio->disconnected) {
1029 video_unregister_device(radio->videodev);
1030 kfree(radio->buffer);
1031 kfree(radio);
1032 goto unlock;
1033 }
Tobias Lorenz03dea862008-04-22 14:46:11 -03001034
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001035 /* stop rds reception */
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001036 cancel_delayed_work_sync(&radio->work);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001037
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001038 /* cancel read processes */
1039 wake_up_interruptible(&radio->read_queue);
1040
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001041 retval = si470x_stop(radio);
1042 usb_autopm_put_interface(radio->intf);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001043 }
1044
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001045unlock:
1046 mutex_unlock(&radio->disconnect_lock);
1047 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001048}
1049
1050
1051/*
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001052 * si470x_fops - file operations interface
1053 */
1054static const struct file_operations si470x_fops = {
1055 .owner = THIS_MODULE,
1056 .llseek = no_llseek,
1057 .read = si470x_fops_read,
1058 .poll = si470x_fops_poll,
1059 .ioctl = video_ioctl2,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -03001060#ifdef CONFIG_COMPAT
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001061 .compat_ioctl = v4l_compat_ioctl32,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -03001062#endif
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001063 .open = si470x_fops_open,
1064 .release = si470x_fops_release,
1065};
1066
1067
1068
1069/**************************************************************************
1070 * Video4Linux Interface
1071 **************************************************************************/
1072
1073/*
1074 * si470x_v4l2_queryctrl - query control
1075 */
1076static struct v4l2_queryctrl si470x_v4l2_queryctrl[] = {
1077/* HINT: the disabled controls are only here to satify kradio and such apps */
1078 {
1079 .id = V4L2_CID_AUDIO_VOLUME,
1080 .type = V4L2_CTRL_TYPE_INTEGER,
1081 .name = "Volume",
1082 .minimum = 0,
1083 .maximum = 15,
1084 .step = 1,
1085 .default_value = 15,
1086 },
1087 {
1088 .id = V4L2_CID_AUDIO_BALANCE,
1089 .flags = V4L2_CTRL_FLAG_DISABLED,
1090 },
1091 {
1092 .id = V4L2_CID_AUDIO_BASS,
1093 .flags = V4L2_CTRL_FLAG_DISABLED,
1094 },
1095 {
1096 .id = V4L2_CID_AUDIO_TREBLE,
1097 .flags = V4L2_CTRL_FLAG_DISABLED,
1098 },
1099 {
1100 .id = V4L2_CID_AUDIO_MUTE,
1101 .type = V4L2_CTRL_TYPE_BOOLEAN,
1102 .name = "Mute",
1103 .minimum = 0,
1104 .maximum = 1,
1105 .step = 1,
1106 .default_value = 1,
1107 },
1108 {
1109 .id = V4L2_CID_AUDIO_LOUDNESS,
1110 .flags = V4L2_CTRL_FLAG_DISABLED,
1111 },
1112};
1113
1114
1115/*
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001116 * si470x_vidioc_querycap - query device capabilities
1117 */
1118static int si470x_vidioc_querycap(struct file *file, void *priv,
1119 struct v4l2_capability *capability)
1120{
1121 strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver));
1122 strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
1123 sprintf(capability->bus_info, "USB");
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001124 capability->version = DRIVER_KERNEL_VERSION;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001125 capability->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
1126
1127 return 0;
1128}
1129
1130
1131/*
1132 * si470x_vidioc_g_input - get input
1133 */
1134static int si470x_vidioc_g_input(struct file *filp, void *priv,
1135 unsigned int *i)
1136{
1137 *i = 0;
1138
1139 return 0;
1140}
1141
1142
1143/*
1144 * si470x_vidioc_s_input - set input
1145 */
1146static int si470x_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
1147{
1148 if (i != 0)
1149 return -EINVAL;
1150
1151 return 0;
1152}
1153
1154
1155/*
1156 * si470x_vidioc_queryctrl - enumerate control items
1157 */
1158static int si470x_vidioc_queryctrl(struct file *file, void *priv,
1159 struct v4l2_queryctrl *qc)
1160{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001161 unsigned char i;
1162 int retval = -EINVAL;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001163
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001164 for (i = 0; i < ARRAY_SIZE(si470x_v4l2_queryctrl); i++) {
1165 if (qc->id && qc->id == si470x_v4l2_queryctrl[i].id) {
1166 memcpy(qc, &(si470x_v4l2_queryctrl[i]), sizeof(*qc));
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001167 retval = 0;
1168 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001169 }
1170 }
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001171 if (retval < 0)
1172 printk(KERN_WARNING DRIVER_NAME
1173 ": query control failed with %d\n", retval);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001174
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001175 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001176}
1177
1178
1179/*
1180 * si470x_vidioc_g_ctrl - get the value of a control
1181 */
1182static int si470x_vidioc_g_ctrl(struct file *file, void *priv,
1183 struct v4l2_control *ctrl)
1184{
1185 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1186
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001187 if (radio->disconnected)
1188 return -EIO;
Tobias Lorenz03dea862008-04-22 14:46:11 -03001189
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001190 switch (ctrl->id) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001191 case V4L2_CID_AUDIO_VOLUME:
1192 ctrl->value = radio->registers[SYSCONFIG2] &
1193 SYSCONFIG2_VOLUME;
1194 break;
1195 case V4L2_CID_AUDIO_MUTE:
1196 ctrl->value = ((radio->registers[POWERCFG] &
1197 POWERCFG_DMUTE) == 0) ? 1 : 0;
1198 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001199 }
1200
1201 return 0;
1202}
1203
1204
1205/*
1206 * si470x_vidioc_s_ctrl - set the value of a control
1207 */
1208static int si470x_vidioc_s_ctrl(struct file *file, void *priv,
1209 struct v4l2_control *ctrl)
1210{
1211 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001212 int retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001213
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001214 if (radio->disconnected)
1215 return -EIO;
Tobias Lorenz03dea862008-04-22 14:46:11 -03001216
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001217 switch (ctrl->id) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001218 case V4L2_CID_AUDIO_VOLUME:
1219 radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_VOLUME;
1220 radio->registers[SYSCONFIG2] |= ctrl->value;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001221 retval = si470x_set_register(radio, SYSCONFIG2);
1222 break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001223 case V4L2_CID_AUDIO_MUTE:
1224 if (ctrl->value == 1)
1225 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
1226 else
1227 radio->registers[POWERCFG] |= POWERCFG_DMUTE;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001228 retval = si470x_set_register(radio, POWERCFG);
1229 break;
1230 default:
1231 retval = -EINVAL;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001232 }
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001233 if (retval < 0)
1234 printk(KERN_WARNING DRIVER_NAME
1235 ": set control failed with %d\n", retval);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001236
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001237 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001238}
1239
1240
1241/*
1242 * si470x_vidioc_g_audio - get audio attributes
1243 */
1244static int si470x_vidioc_g_audio(struct file *file, void *priv,
1245 struct v4l2_audio *audio)
1246{
1247 if (audio->index > 1)
1248 return -EINVAL;
1249
1250 strcpy(audio->name, "Radio");
1251 audio->capability = V4L2_AUDCAP_STEREO;
1252
1253 return 0;
1254}
1255
1256
1257/*
1258 * si470x_vidioc_s_audio - set audio attributes
1259 */
1260static int si470x_vidioc_s_audio(struct file *file, void *priv,
1261 struct v4l2_audio *audio)
1262{
1263 if (audio->index != 0)
1264 return -EINVAL;
1265
1266 return 0;
1267}
1268
1269
1270/*
1271 * si470x_vidioc_g_tuner - get tuner attributes
1272 */
1273static int si470x_vidioc_g_tuner(struct file *file, void *priv,
1274 struct v4l2_tuner *tuner)
1275{
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001276 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001277 int retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001278
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001279 if (radio->disconnected)
1280 return -EIO;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001281 if (tuner->index > 0)
1282 return -EINVAL;
1283
1284 /* read status rssi */
1285 retval = si470x_get_register(radio, STATUSRSSI);
1286 if (retval < 0)
1287 return retval;
1288
1289 strcpy(tuner->name, "FM");
1290 tuner->type = V4L2_TUNER_RADIO;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001291 switch (band) {
1292 /* 0: 87.5 - 108 MHz (USA, Europe, default) */
1293 default:
1294 tuner->rangelow = 87.5 * FREQ_MUL;
1295 tuner->rangehigh = 108 * FREQ_MUL;
1296 break;
1297 /* 1: 76 - 108 MHz (Japan wide band) */
1298 case 1 :
1299 tuner->rangelow = 76 * FREQ_MUL;
1300 tuner->rangehigh = 108 * FREQ_MUL;
1301 break;
1302 /* 2: 76 - 90 MHz (Japan) */
1303 case 2 :
1304 tuner->rangelow = 76 * FREQ_MUL;
1305 tuner->rangehigh = 90 * FREQ_MUL;
1306 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001307 };
1308 tuner->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
1309 tuner->capability = V4L2_TUNER_CAP_LOW;
1310
1311 /* Stereo indicator == Stereo (instead of Mono) */
1312 if ((radio->registers[STATUSRSSI] & STATUSRSSI_ST) == 1)
1313 tuner->audmode = V4L2_TUNER_MODE_STEREO;
1314 else
1315 tuner->audmode = V4L2_TUNER_MODE_MONO;
1316
1317 /* min is worst, max is best; signal:0..0xffff; rssi: 0..0xff */
1318 tuner->signal = (radio->registers[STATUSRSSI] & STATUSRSSI_RSSI)
1319 * 0x0101;
1320
1321 /* automatic frequency control: -1: freq to low, 1 freq to high */
1322 tuner->afc = 0;
1323
1324 return 0;
1325}
1326
1327
1328/*
1329 * si470x_vidioc_s_tuner - set tuner attributes
1330 */
1331static int si470x_vidioc_s_tuner(struct file *file, void *priv,
1332 struct v4l2_tuner *tuner)
1333{
1334 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001335 int retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001336
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001337 if (radio->disconnected)
1338 return -EIO;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001339 if (tuner->index > 0)
1340 return -EINVAL;
1341
1342 if (tuner->audmode == V4L2_TUNER_MODE_MONO)
1343 radio->registers[POWERCFG] |= POWERCFG_MONO; /* force mono */
1344 else
1345 radio->registers[POWERCFG] &= ~POWERCFG_MONO; /* try stereo */
1346
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001347 retval = si470x_set_register(radio, POWERCFG);
1348 if (retval < 0)
1349 printk(KERN_WARNING DRIVER_NAME
1350 ": set tuner failed with %d\n", retval);
1351
1352 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001353}
1354
1355
1356/*
1357 * si470x_vidioc_g_frequency - get tuner or modulator radio frequency
1358 */
1359static int si470x_vidioc_g_frequency(struct file *file, void *priv,
1360 struct v4l2_frequency *freq)
1361{
1362 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1363
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001364 if (radio->disconnected)
1365 return -EIO;
Tobias Lorenz03dea862008-04-22 14:46:11 -03001366
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001367 freq->type = V4L2_TUNER_RADIO;
Tobias Lorenz6cc72652008-05-31 15:06:50 -03001368 return si470x_get_freq(radio, &radio->frequency);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001369}
1370
1371
1372/*
1373 * si470x_vidioc_s_frequency - set tuner or modulator radio frequency
1374 */
1375static int si470x_vidioc_s_frequency(struct file *file, void *priv,
1376 struct v4l2_frequency *freq)
1377{
1378 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001379 int retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001380
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001381 if (radio->disconnected)
1382 return -EIO;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001383 if (freq->type != V4L2_TUNER_RADIO)
1384 return -EINVAL;
1385
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001386 retval = si470x_set_freq(radio, freq->frequency);
1387 if (retval < 0)
1388 printk(KERN_WARNING DRIVER_NAME
1389 ": set frequency failed with %d\n", retval);
1390
1391 return 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001392}
1393
1394
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001395/*
1396 * si470x_viddev_tamples - video device interface
1397 */
1398static struct video_device si470x_viddev_template = {
1399 .fops = &si470x_fops,
1400 .name = DRIVER_NAME,
1401 .type = VID_TYPE_TUNER,
1402 .release = video_device_release,
1403 .vidioc_querycap = si470x_vidioc_querycap,
1404 .vidioc_g_input = si470x_vidioc_g_input,
1405 .vidioc_s_input = si470x_vidioc_s_input,
1406 .vidioc_queryctrl = si470x_vidioc_queryctrl,
1407 .vidioc_g_ctrl = si470x_vidioc_g_ctrl,
1408 .vidioc_s_ctrl = si470x_vidioc_s_ctrl,
1409 .vidioc_g_audio = si470x_vidioc_g_audio,
1410 .vidioc_s_audio = si470x_vidioc_s_audio,
1411 .vidioc_g_tuner = si470x_vidioc_g_tuner,
1412 .vidioc_s_tuner = si470x_vidioc_s_tuner,
1413 .vidioc_g_frequency = si470x_vidioc_g_frequency,
1414 .vidioc_s_frequency = si470x_vidioc_s_frequency,
1415 .owner = THIS_MODULE,
1416};
1417
1418
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001419
1420/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001421 * USB Interface
1422 **************************************************************************/
1423
1424/*
1425 * si470x_usb_driver_probe - probe for the device
1426 */
1427static int si470x_usb_driver_probe(struct usb_interface *intf,
1428 const struct usb_device_id *id)
1429{
1430 struct si470x_device *radio;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001431 int retval = -ENOMEM;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001432
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001433 /* private data allocation */
1434 radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001435 if (!radio)
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001436 goto err_initial;
1437
1438 /* video device allocation */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001439 radio->videodev = video_device_alloc();
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001440 if (!radio->videodev)
1441 goto err_radio;
1442
1443 /* initial configuration */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001444 memcpy(radio->videodev, &si470x_viddev_template,
1445 sizeof(si470x_viddev_template));
1446 radio->users = 0;
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001447 radio->disconnected = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001448 radio->usbdev = interface_to_usbdev(intf);
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001449 radio->intf = intf;
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001450 mutex_init(&radio->disconnect_lock);
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001451 mutex_init(&radio->lock);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001452 video_set_drvdata(radio->videodev, radio);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001453
1454 /* show some infos about the specific device */
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001455 retval = -EIO;
1456 if (si470x_get_all_registers(radio) < 0)
1457 goto err_all;
1458 printk(KERN_INFO DRIVER_NAME ": DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001459 radio->registers[DEVICEID], radio->registers[CHIPID]);
1460
1461 /* check if firmware is current */
1462 if ((radio->registers[CHIPID] & CHIPID_FIRMWARE)
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001463 < RADIO_SW_VERSION_CURRENT) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001464 printk(KERN_WARNING DRIVER_NAME
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001465 ": This driver is known to work with "
1466 "firmware version %hu,\n", RADIO_SW_VERSION_CURRENT);
1467 printk(KERN_WARNING DRIVER_NAME
1468 ": but the device has firmware version %hu.\n",
1469 radio->registers[CHIPID] & CHIPID_FIRMWARE);
1470 printk(KERN_WARNING DRIVER_NAME
1471 ": If you have some trouble using this driver,\n");
1472 printk(KERN_WARNING DRIVER_NAME
1473 ": please report to V4L ML at "
1474 "video4linux-list@redhat.com\n");
1475 }
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001476
1477 /* set initial frequency */
1478 si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
1479
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001480 /* rds buffer allocation */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001481 radio->buf_size = rds_buf * 3;
1482 radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001483 if (!radio->buffer)
1484 goto err_all;
1485
1486 /* rds buffer configuration */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001487 radio->wr_index = 0;
1488 radio->rd_index = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001489 init_waitqueue_head(&radio->read_queue);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001490
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001491 /* prepare rds work function */
1492 INIT_DELAYED_WORK(&radio->work, si470x_work);
1493
1494 /* register video device */
1495 if (video_register_device(radio->videodev, VFL_TYPE_RADIO, radio_nr)) {
1496 printk(KERN_WARNING DRIVER_NAME
1497 ": Could not register video device\n");
1498 goto err_all;
1499 }
1500 usb_set_intfdata(intf, radio);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001501
1502 return 0;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001503err_all:
1504 video_device_release(radio->videodev);
1505 kfree(radio->buffer);
1506err_radio:
1507 kfree(radio);
1508err_initial:
1509 return retval;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001510}
1511
1512
1513/*
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001514 * si470x_usb_driver_suspend - suspend the device
1515 */
1516static int si470x_usb_driver_suspend(struct usb_interface *intf,
1517 pm_message_t message)
1518{
1519 struct si470x_device *radio = usb_get_intfdata(intf);
1520
1521 printk(KERN_INFO DRIVER_NAME ": suspending now...\n");
1522
1523 cancel_delayed_work_sync(&radio->work);
1524
1525 return 0;
1526}
1527
1528
1529/*
1530 * si470x_usb_driver_resume - resume the device
1531 */
1532static int si470x_usb_driver_resume(struct usb_interface *intf)
1533{
1534 struct si470x_device *radio = usb_get_intfdata(intf);
1535
1536 printk(KERN_INFO DRIVER_NAME ": resuming now...\n");
1537
1538 mutex_lock(&radio->lock);
1539 if (radio->users && radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS)
1540 schedule_delayed_work(&radio->work,
1541 msecs_to_jiffies(rds_poll_time));
1542 mutex_unlock(&radio->lock);
1543
1544 return 0;
1545}
1546
1547
1548/*
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001549 * si470x_usb_driver_disconnect - disconnect the device
1550 */
1551static void si470x_usb_driver_disconnect(struct usb_interface *intf)
1552{
1553 struct si470x_device *radio = usb_get_intfdata(intf);
1554
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001555 mutex_lock(&radio->disconnect_lock);
1556 radio->disconnected = 1;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001557 cancel_delayed_work_sync(&radio->work);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001558 usb_set_intfdata(intf, NULL);
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001559 if (radio->users == 0) {
1560 video_unregister_device(radio->videodev);
1561 kfree(radio->buffer);
1562 kfree(radio);
1563 }
1564 mutex_unlock(&radio->disconnect_lock);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001565}
1566
1567
1568/*
1569 * si470x_usb_driver - usb driver interface
1570 */
1571static struct usb_driver si470x_usb_driver = {
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001572 .name = DRIVER_NAME,
1573 .probe = si470x_usb_driver_probe,
1574 .disconnect = si470x_usb_driver_disconnect,
1575 .suspend = si470x_usb_driver_suspend,
1576 .resume = si470x_usb_driver_resume,
1577 .id_table = si470x_usb_driver_id_table,
1578 .supports_autosuspend = 1,
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001579};
1580
1581
1582
1583/**************************************************************************
1584 * Module Interface
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001585 **************************************************************************/
1586
1587/*
1588 * si470x_module_init - module init
1589 */
1590static int __init si470x_module_init(void)
1591{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001592 printk(KERN_INFO DRIVER_DESC ", Version " DRIVER_VERSION "\n");
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001593 return usb_register(&si470x_usb_driver);
1594}
1595
1596
1597/*
1598 * si470x_module_exit - module exit
1599 */
1600static void __exit si470x_module_exit(void)
1601{
1602 usb_deregister(&si470x_usb_driver);
1603}
1604
1605
1606module_init(si470x_module_init);
1607module_exit(si470x_module_exit);
1608
1609MODULE_LICENSE("GPL");
1610MODULE_AUTHOR(DRIVER_AUTHOR);
1611MODULE_DESCRIPTION(DRIVER_DESC);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001612MODULE_VERSION(DRIVER_VERSION);