blob: dc93a882b38595114169bb575820cf023155a460 [file] [log] [blame]
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001/*
2 * drivers/media/radio/radio-si470x.c
3 *
4 * Driver for USB radios for the Silicon Labs Si470x FM Radio Receivers:
5 * - Silicon Labs USB FM Radio Reference Design
6 * - ADS/Tech FM Radio Receiver (formerly Instant FM Music) (RDX-155-EF)
7 *
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03008 * Copyright (c) 2008 Tobias Lorenz <tobias.lorenz@gmx.net>
Tobias Lorenz78656ac2008-01-14 21:55:27 -03009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24
25
26/*
Tobias Lorenz6cc72652008-05-31 15:06:50 -030027 * User Notes:
28 * - USB Audio is provided by the alsa snd_usb_audio module.
29 * For listing you have to redirect the sound, for example using:
30 * arecord -D hw:1,0 -r96000 -c2 -f S16_LE | artsdsp aplay -B -
31 * - regarding module parameters in /sys/module/radio_si470x/parameters:
32 * the contents of read-only files (0444) are not updated, even if
33 * space, band and de are changed using private video controls
34 * - increase tune_timeout, if you often get -EIO errors
35 * - hw_freq_seek returns -EAGAIN, when timed out or band limit is reached
36 */
37
38
39/*
Tobias Lorenz78656ac2008-01-14 21:55:27 -030040 * History:
41 * 2008-01-12 Tobias Lorenz <tobias.lorenz@gmx.net>
42 * Version 1.0.0
43 * - First working version
44 * 2008-01-13 Tobias Lorenz <tobias.lorenz@gmx.net>
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -030045 * Version 1.0.1
Tobias Lorenz78656ac2008-01-14 21:55:27 -030046 * - Improved error handling, every function now returns errno
47 * - Improved multi user access (start/mute/stop)
48 * - Channel doesn't get lost anymore after start/mute/stop
49 * - RDS support added (polling mode via interrupt EP 1)
50 * - marked default module parameters with *value*
51 * - switched from bit structs to bit masks
52 * - header file cleaned and integrated
53 * 2008-01-14 Tobias Lorenz <tobias.lorenz@gmx.net>
54 * Version 1.0.2
55 * - hex values are now lower case
56 * - commented USB ID for ADS/Tech moved on todo list
57 * - blacklisted si470x in hid-quirks.c
58 * - rds buffer handling functions integrated into *_work, *_read
59 * - rds_command in si470x_poll exchanged against simple retval
60 * - check for firmware version 15
61 * - code order and prototypes still remain the same
62 * - spacing and bottom of band codes remain the same
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -030063 * 2008-01-16 Tobias Lorenz <tobias.lorenz@gmx.net>
64 * Version 1.0.3
65 * - code reordered to avoid function prototypes
66 * - switch/case defaults are now more user-friendly
67 * - unified comment style
68 * - applied all checkpatch.pl v1.12 suggestions
69 * except the warning about the too long lines with bit comments
70 * - renamed FMRADIO to RADIO to cut line length (checkpatch.pl)
Tobias Lorenz2fb88402008-01-25 05:14:57 -030071 * 2008-01-22 Tobias Lorenz <tobias.lorenz@gmx.net>
72 * Version 1.0.4
73 * - avoid poss. locking when doing copy_to_user which may sleep
74 * - RDS is automatically activated on read now
75 * - code cleaned of unnecessary rds_commands
76 * - USB Vendor/Product ID for ADS/Tech FM Radio Receiver verified
77 * (thanks to Guillaume RAMOUSSE)
Tobias Lorenz0e3301e2008-01-27 14:54:07 -030078 * 2008-01-27 Tobias Lorenz <tobias.lorenz@gmx.net>
79 * Version 1.0.5
80 * - number of seek_retries changed to tune_timeout
81 * - fixed problem with incomplete tune operations by own buffers
Tobias Lorenz5caf5132008-02-04 22:26:08 -030082 * - optimization of variables and printf types
Tobias Lorenz0e3301e2008-01-27 14:54:07 -030083 * - improved error logging
Tobias Lorenz5caf5132008-02-04 22:26:08 -030084 * 2008-01-31 Tobias Lorenz <tobias.lorenz@gmx.net>
85 * Oliver Neukum <oliver@neukum.org>
86 * Version 1.0.6
87 * - fixed coverity checker warnings in *_usb_driver_disconnect
88 * - probe()/open() race by correct ordering in probe()
89 * - DMA coherency rules by separate allocation of all buffers
90 * - use of endianness macros
91 * - abuse of spinlock, replaced by mutex
92 * - racy handling of timer in disconnect,
93 * replaced by delayed_work
94 * - racy interruptible_sleep_on(),
95 * replaced with wait_event_interruptible()
96 * - handle signals in read()
Tobias Lorenz57566ad2008-02-09 16:08:24 -030097 * 2008-02-08 Tobias Lorenz <tobias.lorenz@gmx.net>
98 * Oliver Neukum <oliver@neukum.org>
99 * Version 1.0.7
100 * - usb autosuspend support
Tobias Lorenzce5829e2008-05-31 15:04:32 -0300101 * - unplugging fixed
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300102 * 2008-05-07 Tobias Lorenz <tobias.lorenz@gmx.net>
103 * Version 1.0.8
Tobias Lorenzbbc89952008-05-31 15:11:32 -0300104 * - hardware frequency seek support
Tobias Lorenzace7d4b2008-05-31 15:09:07 -0300105 * - afc indication
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300106 * - more safety checks, let si470x_get_freq return errno
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300107 *
108 * ToDo:
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300109 * - add firmware download/update support
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300110 * - RDS support: interrupt mode, instead of polling
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300111 * - add LED status output (check if that's not already done in firmware)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300112 */
113
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300114
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300115/* driver definitions */
116#define DRIVER_AUTHOR "Tobias Lorenz <tobias.lorenz@gmx.net>"
117#define DRIVER_NAME "radio-si470x"
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300118#define DRIVER_KERNEL_VERSION KERNEL_VERSION(1, 0, 8)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300119#define DRIVER_CARD "Silicon Labs Si470x FM Radio Receiver"
120#define DRIVER_DESC "USB radio driver for Si470x FM Radio Receivers"
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300121#define DRIVER_VERSION "1.0.8"
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300122
123
124/* kernel includes */
125#include <linux/kernel.h>
126#include <linux/module.h>
127#include <linux/init.h>
128#include <linux/slab.h>
129#include <linux/input.h>
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300130#include <linux/usb.h>
131#include <linux/hid.h>
132#include <linux/version.h>
Mauro Carvalho Chehab387d4472008-01-15 11:25:10 -0300133#include <linux/videodev2.h>
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300134#include <linux/mutex.h>
Mauro Carvalho Chehab387d4472008-01-15 11:25:10 -0300135#include <media/v4l2-common.h>
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300136#include <media/rds.h>
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300137#include <asm/unaligned.h>
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300138
139
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300140/* USB Device ID List */
141static struct usb_device_id si470x_usb_driver_id_table[] = {
142 /* Silicon Labs USB FM Radio Reference Design */
143 { USB_DEVICE_AND_INTERFACE_INFO(0x10c4, 0x818a, USB_CLASS_HID, 0, 0) },
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300144 /* ADS/Tech FM Radio Receiver (formerly Instant FM Music) */
145 { USB_DEVICE_AND_INTERFACE_INFO(0x06e1, 0xa155, USB_CLASS_HID, 0, 0) },
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300146 /* Terminating entry */
147 { }
148};
149MODULE_DEVICE_TABLE(usb, si470x_usb_driver_id_table);
150
151
152
153/**************************************************************************
154 * Module Parameters
155 **************************************************************************/
156
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300157/* Radio Nr */
158static int radio_nr = -1;
159module_param(radio_nr, int, 0);
160MODULE_PARM_DESC(radio_nr, "Radio Nr");
161
162/* Spacing (kHz) */
163/* 0: 200 kHz (USA, Australia) */
164/* 1: 100 kHz (Europe, Japan) */
165/* 2: 50 kHz */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300166static unsigned short space = 2;
167module_param(space, ushort, 0);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300168MODULE_PARM_DESC(radio_nr, "Spacing: 0=200kHz 1=100kHz *2=50kHz*");
169
170/* Bottom of Band (MHz) */
171/* 0: 87.5 - 108 MHz (USA, Europe)*/
172/* 1: 76 - 108 MHz (Japan wide band) */
173/* 2: 76 - 90 MHz (Japan) */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300174static unsigned short band = 1;
175module_param(band, ushort, 0);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300176MODULE_PARM_DESC(radio_nr, "Band: 0=87.5..108MHz *1=76..108MHz* 2=76..90MHz");
177
178/* De-emphasis */
179/* 0: 75 us (USA) */
180/* 1: 50 us (Europe, Australia, Japan) */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300181static unsigned short de = 1;
182module_param(de, ushort, 0);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300183MODULE_PARM_DESC(radio_nr, "De-emphasis: 0=75us *1=50us*");
184
185/* USB timeout */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300186static unsigned int usb_timeout = 500;
187module_param(usb_timeout, uint, 0);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300188MODULE_PARM_DESC(usb_timeout, "USB timeout (ms): *500*");
189
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300190/* Tune timeout */
191static unsigned int tune_timeout = 3000;
192module_param(tune_timeout, uint, 0);
193MODULE_PARM_DESC(tune_timeout, "Tune timeout: *3000*");
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300194
Tobias Lorenzbbc89952008-05-31 15:11:32 -0300195/* Seek timeout */
196static unsigned int seek_timeout = 5000;
197module_param(seek_timeout, uint, 0);
198MODULE_PARM_DESC(seek_timeout, "Seek timeout: *5000*");
199
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300200/* RDS buffer blocks */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300201static unsigned int rds_buf = 100;
202module_param(rds_buf, uint, 0);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300203MODULE_PARM_DESC(rds_buf, "RDS buffer entries: *100*");
204
205/* RDS maximum block errors */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300206static unsigned short max_rds_errors = 1;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300207/* 0 means 0 errors requiring correction */
208/* 1 means 1-2 errors requiring correction (used by original USBRadio.exe) */
209/* 2 means 3-5 errors requiring correction */
210/* 3 means 6+ errors or errors in checkword, correction not possible */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300211module_param(max_rds_errors, ushort, 0);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300212MODULE_PARM_DESC(max_rds_errors, "RDS maximum block errors: *1*");
213
214/* RDS poll frequency */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300215static unsigned int rds_poll_time = 40;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300216/* 40 is used by the original USBRadio.exe */
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300217/* 50 is used by radio-cadet */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300218/* 75 should be okay */
219/* 80 is the usual RDS receive interval */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300220module_param(rds_poll_time, uint, 0);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300221MODULE_PARM_DESC(rds_poll_time, "RDS poll time (ms): *40*");
222
223
224
225/**************************************************************************
226 * Register Definitions
227 **************************************************************************/
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300228#define RADIO_REGISTER_SIZE 2 /* 16 register bit width */
229#define RADIO_REGISTER_NUM 16 /* DEVICEID ... RDSD */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300230#define RDS_REGISTER_NUM 6 /* STATUSRSSI ... RDSD */
231
232#define DEVICEID 0 /* Device ID */
233#define DEVICEID_PN 0xf000 /* bits 15..12: Part Number */
234#define DEVICEID_MFGID 0x0fff /* bits 11..00: Manufacturer ID */
235
236#define CHIPID 1 /* Chip ID */
237#define CHIPID_REV 0xfc00 /* bits 15..10: Chip Version */
238#define CHIPID_DEV 0x0200 /* bits 09..09: Device */
239#define CHIPID_FIRMWARE 0x01ff /* bits 08..00: Firmware Version */
240
241#define POWERCFG 2 /* Power Configuration */
242#define POWERCFG_DSMUTE 0x8000 /* bits 15..15: Softmute Disable */
243#define POWERCFG_DMUTE 0x4000 /* bits 14..14: Mute Disable */
244#define POWERCFG_MONO 0x2000 /* bits 13..13: Mono Select */
245#define POWERCFG_RDSM 0x0800 /* bits 11..11: RDS Mode (Si4701 only) */
246#define POWERCFG_SKMODE 0x0400 /* bits 10..10: Seek Mode */
247#define POWERCFG_SEEKUP 0x0200 /* bits 09..09: Seek Direction */
248#define POWERCFG_SEEK 0x0100 /* bits 08..08: Seek */
249#define POWERCFG_DISABLE 0x0040 /* bits 06..06: Powerup Disable */
250#define POWERCFG_ENABLE 0x0001 /* bits 00..00: Powerup Enable */
251
252#define CHANNEL 3 /* Channel */
253#define CHANNEL_TUNE 0x8000 /* bits 15..15: Tune */
254#define CHANNEL_CHAN 0x03ff /* bits 09..00: Channel Select */
255
256#define SYSCONFIG1 4 /* System Configuration 1 */
257#define SYSCONFIG1_RDSIEN 0x8000 /* bits 15..15: RDS Interrupt Enable (Si4701 only) */
258#define SYSCONFIG1_STCIEN 0x4000 /* bits 14..14: Seek/Tune Complete Interrupt Enable */
259#define SYSCONFIG1_RDS 0x1000 /* bits 12..12: RDS Enable (Si4701 only) */
260#define SYSCONFIG1_DE 0x0800 /* bits 11..11: De-emphasis (0=75us 1=50us) */
261#define SYSCONFIG1_AGCD 0x0400 /* bits 10..10: AGC Disable */
262#define SYSCONFIG1_BLNDADJ 0x00c0 /* bits 07..06: Stereo/Mono Blend Level Adjustment */
263#define SYSCONFIG1_GPIO3 0x0030 /* bits 05..04: General Purpose I/O 3 */
264#define SYSCONFIG1_GPIO2 0x000c /* bits 03..02: General Purpose I/O 2 */
265#define SYSCONFIG1_GPIO1 0x0003 /* bits 01..00: General Purpose I/O 1 */
266
267#define SYSCONFIG2 5 /* System Configuration 2 */
268#define SYSCONFIG2_SEEKTH 0xff00 /* bits 15..08: RSSI Seek Threshold */
269#define SYSCONFIG2_BAND 0x0080 /* bits 07..06: Band Select */
270#define SYSCONFIG2_SPACE 0x0030 /* bits 05..04: Channel Spacing */
271#define SYSCONFIG2_VOLUME 0x000f /* bits 03..00: Volume */
272
273#define SYSCONFIG3 6 /* System Configuration 3 */
274#define SYSCONFIG3_SMUTER 0xc000 /* bits 15..14: Softmute Attack/Recover Rate */
275#define SYSCONFIG3_SMUTEA 0x3000 /* bits 13..12: Softmute Attenuation */
276#define SYSCONFIG3_SKSNR 0x00f0 /* bits 07..04: Seek SNR Threshold */
277#define SYSCONFIG3_SKCNT 0x000f /* bits 03..00: Seek FM Impulse Detection Threshold */
278
279#define TEST1 7 /* Test 1 */
280#define TEST1_AHIZEN 0x4000 /* bits 14..14: Audio High-Z Enable */
281
282#define TEST2 8 /* Test 2 */
283/* TEST2 only contains reserved bits */
284
285#define BOOTCONFIG 9 /* Boot Configuration */
286/* BOOTCONFIG only contains reserved bits */
287
288#define STATUSRSSI 10 /* Status RSSI */
289#define STATUSRSSI_RDSR 0x8000 /* bits 15..15: RDS Ready (Si4701 only) */
290#define STATUSRSSI_STC 0x4000 /* bits 14..14: Seek/Tune Complete */
291#define STATUSRSSI_SF 0x2000 /* bits 13..13: Seek Fail/Band Limit */
292#define STATUSRSSI_AFCRL 0x1000 /* bits 12..12: AFC Rail */
293#define STATUSRSSI_RDSS 0x0800 /* bits 11..11: RDS Synchronized (Si4701 only) */
294#define STATUSRSSI_BLERA 0x0600 /* bits 10..09: RDS Block A Errors (Si4701 only) */
295#define STATUSRSSI_ST 0x0100 /* bits 08..08: Stereo Indicator */
296#define STATUSRSSI_RSSI 0x00ff /* bits 07..00: RSSI (Received Signal Strength Indicator) */
297
298#define READCHAN 11 /* Read Channel */
299#define READCHAN_BLERB 0xc000 /* bits 15..14: RDS Block D Errors (Si4701 only) */
300#define READCHAN_BLERC 0x3000 /* bits 13..12: RDS Block C Errors (Si4701 only) */
301#define READCHAN_BLERD 0x0c00 /* bits 11..10: RDS Block B Errors (Si4701 only) */
302#define READCHAN_READCHAN 0x03ff /* bits 09..00: Read Channel */
303
304#define RDSA 12 /* RDSA */
305#define RDSA_RDSA 0xffff /* bits 15..00: RDS Block A Data (Si4701 only) */
306
307#define RDSB 13 /* RDSB */
308#define RDSB_RDSB 0xffff /* bits 15..00: RDS Block B Data (Si4701 only) */
309
310#define RDSC 14 /* RDSC */
311#define RDSC_RDSC 0xffff /* bits 15..00: RDS Block C Data (Si4701 only) */
312
313#define RDSD 15 /* RDSD */
314#define RDSD_RDSD 0xffff /* bits 15..00: RDS Block D Data (Si4701 only) */
315
316
317
318/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300319 * USB HID Reports
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300320 **************************************************************************/
321
322/* Reports 1-16 give direct read/write access to the 16 Si470x registers */
323/* with the (REPORT_ID - 1) corresponding to the register address across USB */
324/* endpoint 0 using GET_REPORT and SET_REPORT */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300325#define REGISTER_REPORT_SIZE (RADIO_REGISTER_SIZE + 1)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300326#define REGISTER_REPORT(reg) ((reg) + 1)
327
328/* Report 17 gives direct read/write access to the entire Si470x register */
329/* map across endpoint 0 using GET_REPORT and SET_REPORT */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300330#define ENTIRE_REPORT_SIZE (RADIO_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300331#define ENTIRE_REPORT 17
332
333/* Report 18 is used to send the lowest 6 Si470x registers up the HID */
334/* interrupt endpoint 1 to Windows every 20 milliseconds for status */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300335#define RDS_REPORT_SIZE (RDS_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300336#define RDS_REPORT 18
337
338/* Report 19: LED state */
339#define LED_REPORT_SIZE 3
340#define LED_REPORT 19
341
342/* Report 19: stream */
343#define STREAM_REPORT_SIZE 3
344#define STREAM_REPORT 19
345
346/* Report 20: scratch */
347#define SCRATCH_PAGE_SIZE 63
348#define SCRATCH_REPORT_SIZE (SCRATCH_PAGE_SIZE + 1)
349#define SCRATCH_REPORT 20
350
351/* Reports 19-22: flash upgrade of the C8051F321 */
352#define WRITE_REPORT 19
353#define FLASH_REPORT 20
354#define CRC_REPORT 21
355#define RESPONSE_REPORT 22
356
357/* Report 23: currently unused, but can accept 60 byte reports on the HID */
358/* interrupt out endpoint 2 every 1 millisecond */
359#define UNUSED_REPORT 23
360
361
362
363/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300364 * Software/Hardware Versions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300365 **************************************************************************/
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300366#define RADIO_SW_VERSION_NOT_BOOTLOADABLE 6
367#define RADIO_SW_VERSION 7
368#define RADIO_SW_VERSION_CURRENT 15
369#define RADIO_HW_VERSION 1
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300370
371#define SCRATCH_PAGE_SW_VERSION 1
372#define SCRATCH_PAGE_HW_VERSION 2
373
374
375
376/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300377 * LED State Definitions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300378 **************************************************************************/
379#define LED_COMMAND 0x35
380
381#define NO_CHANGE_LED 0x00
382#define ALL_COLOR_LED 0x01 /* streaming state */
383#define BLINK_GREEN_LED 0x02 /* connect state */
384#define BLINK_RED_LED 0x04
385#define BLINK_ORANGE_LED 0x10 /* disconnect state */
386#define SOLID_GREEN_LED 0x20 /* tuning/seeking state */
387#define SOLID_RED_LED 0x40 /* bootload state */
388#define SOLID_ORANGE_LED 0x80
389
390
391
392/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300393 * Stream State Definitions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300394 **************************************************************************/
395#define STREAM_COMMAND 0x36
396#define STREAM_VIDPID 0x00
397#define STREAM_AUDIO 0xff
398
399
400
401/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300402 * Bootloader / Flash Commands
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300403 **************************************************************************/
404
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300405/* unique id sent to bootloader and required to put into a bootload state */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300406#define UNIQUE_BL_ID 0x34
407
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300408/* mask for the flash data */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300409#define FLASH_DATA_MASK 0x55
410
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300411/* bootloader commands */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300412#define GET_SW_VERSION_COMMAND 0x00
413#define SET_PAGE_COMMAND 0x01
414#define ERASE_PAGE_COMMAND 0x02
415#define WRITE_PAGE_COMMAND 0x03
416#define CRC_ON_PAGE_COMMAND 0x04
417#define READ_FLASH_BYTE_COMMAND 0x05
418#define RESET_DEVICE_COMMAND 0x06
419#define GET_HW_VERSION_COMMAND 0x07
420#define BLANK 0xff
421
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300422/* bootloader command responses */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300423#define COMMAND_OK 0x01
424#define COMMAND_FAILED 0x02
425#define COMMAND_PENDING 0x03
426
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300427/* buffer sizes */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300428#define COMMAND_BUFFER_SIZE 4
429#define RESPONSE_BUFFER_SIZE 2
430#define FLASH_BUFFER_SIZE 64
431#define CRC_BUFFER_SIZE 3
432
433
434
435/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300436 * General Driver Definitions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300437 **************************************************************************/
438
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300439/*
440 * si470x_device - private data
441 */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300442struct si470x_device {
443 /* reference to USB and video device */
444 struct usb_device *usbdev;
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300445 struct usb_interface *intf;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300446 struct video_device *videodev;
447
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300448 /* driver management */
449 unsigned int users;
Tobias Lorenzce5829e2008-05-31 15:04:32 -0300450 unsigned char disconnected;
451 struct mutex disconnect_lock;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300452
453 /* Silabs internal registers (0..15) */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300454 unsigned short registers[RADIO_REGISTER_NUM];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300455
456 /* RDS receive buffer */
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300457 struct delayed_work work;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300458 wait_queue_head_t read_queue;
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300459 struct mutex lock; /* buffer locking */
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300460 unsigned char *buffer; /* size is always multiple of three */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300461 unsigned int buf_size;
462 unsigned int rd_index;
463 unsigned int wr_index;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300464};
465
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300466
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300467/*
468 * The frequency is set in units of 62.5 Hz when using V4L2_TUNER_CAP_LOW,
469 * 62.5 kHz otherwise.
470 * The tuner is able to have a channel spacing of 50, 100 or 200 kHz.
471 * tuner->capability is therefore set to V4L2_TUNER_CAP_LOW
472 * The FREQ_MUL is then: 1 MHz / 62.5 Hz = 16000
473 */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300474#define FREQ_MUL (1000000 / 62.5)
475
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300476
477
478/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300479 * General Driver Functions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300480 **************************************************************************/
481
482/*
483 * si470x_get_report - receive a HID report
484 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300485static int si470x_get_report(struct si470x_device *radio, void *buf, int size)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300486{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300487 unsigned char *report = (unsigned char *) buf;
488 int retval;
489
490 retval = usb_control_msg(radio->usbdev,
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300491 usb_rcvctrlpipe(radio->usbdev, 0),
492 HID_REQ_GET_REPORT,
493 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300494 report[0], 2,
495 buf, size, usb_timeout);
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300496
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300497 if (retval < 0)
498 printk(KERN_WARNING DRIVER_NAME
499 ": si470x_get_report: usb_control_msg returned %d\n",
500 retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300501 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300502}
503
504
505/*
506 * si470x_set_report - send a HID report
507 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300508static int si470x_set_report(struct si470x_device *radio, void *buf, int size)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300509{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300510 unsigned char *report = (unsigned char *) buf;
511 int retval;
512
513 retval = usb_control_msg(radio->usbdev,
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300514 usb_sndctrlpipe(radio->usbdev, 0),
515 HID_REQ_SET_REPORT,
516 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300517 report[0], 2,
518 buf, size, usb_timeout);
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300519
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300520 if (retval < 0)
521 printk(KERN_WARNING DRIVER_NAME
522 ": si470x_set_report: usb_control_msg returned %d\n",
523 retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300524 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300525}
526
527
528/*
529 * si470x_get_register - read register
530 */
531static int si470x_get_register(struct si470x_device *radio, int regnr)
532{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300533 unsigned char buf[REGISTER_REPORT_SIZE];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300534 int retval;
535
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300536 buf[0] = REGISTER_REPORT(regnr);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300537
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300538 retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
539
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300540 if (retval >= 0)
Harvey Harrison5ad6b812008-04-15 22:22:11 -0300541 radio->registers[regnr] = get_unaligned_be16(&buf[1]);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300542
543 return (retval < 0) ? -EINVAL : 0;
544}
545
546
547/*
548 * si470x_set_register - write register
549 */
550static int si470x_set_register(struct si470x_device *radio, int regnr)
551{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300552 unsigned char buf[REGISTER_REPORT_SIZE];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300553 int retval;
554
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300555 buf[0] = REGISTER_REPORT(regnr);
Harvey Harrison5ad6b812008-04-15 22:22:11 -0300556 put_unaligned_be16(radio->registers[regnr], &buf[1]);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300557
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300558 retval = si470x_set_report(radio, (void *) &buf, sizeof(buf));
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300559
560 return (retval < 0) ? -EINVAL : 0;
561}
562
563
564/*
565 * si470x_get_all_registers - read entire registers
566 */
567static int si470x_get_all_registers(struct si470x_device *radio)
568{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300569 unsigned char buf[ENTIRE_REPORT_SIZE];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300570 int retval;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300571 unsigned char regnr;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300572
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300573 buf[0] = ENTIRE_REPORT;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300574
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300575 retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300576
577 if (retval >= 0)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300578 for (regnr = 0; regnr < RADIO_REGISTER_NUM; regnr++)
Harvey Harrison5ad6b812008-04-15 22:22:11 -0300579 radio->registers[regnr] = get_unaligned_be16(
580 &buf[regnr * RADIO_REGISTER_SIZE + 1]);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300581
582 return (retval < 0) ? -EINVAL : 0;
583}
584
585
586/*
587 * si470x_get_rds_registers - read rds registers
588 */
589static int si470x_get_rds_registers(struct si470x_device *radio)
590{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300591 unsigned char buf[RDS_REPORT_SIZE];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300592 int retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300593 int size;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300594 unsigned char regnr;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300595
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300596 buf[0] = RDS_REPORT;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300597
598 retval = usb_interrupt_msg(radio->usbdev,
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300599 usb_rcvintpipe(radio->usbdev, 1),
600 (void *) &buf, sizeof(buf), &size, usb_timeout);
601 if (size != sizeof(buf))
Tobias Lorenzce5829e2008-05-31 15:04:32 -0300602 printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: "
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300603 "return size differs: %d != %zu\n", size, sizeof(buf));
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300604 if (retval < 0)
605 printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: "
606 "usb_interrupt_msg returned %d\n", retval);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300607
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300608 if (retval >= 0)
609 for (regnr = 0; regnr < RDS_REGISTER_NUM; regnr++)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300610 radio->registers[STATUSRSSI + regnr] =
Tobias Lorenzce5829e2008-05-31 15:04:32 -0300611 get_unaligned_be16(
612 &buf[regnr * RADIO_REGISTER_SIZE + 1]);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300613
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300614 return (retval < 0) ? -EINVAL : 0;
615}
616
617
618/*
619 * si470x_set_chan - set the channel
620 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300621static int si470x_set_chan(struct si470x_device *radio, unsigned short chan)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300622{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300623 int retval;
624 unsigned long timeout;
625 bool timed_out = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300626
627 /* start tuning */
628 radio->registers[CHANNEL] &= ~CHANNEL_CHAN;
629 radio->registers[CHANNEL] |= CHANNEL_TUNE | chan;
630 retval = si470x_set_register(radio, CHANNEL);
631 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300632 goto done;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300633
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300634 /* wait till tune operation has completed */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300635 timeout = jiffies + msecs_to_jiffies(tune_timeout);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300636 do {
637 retval = si470x_get_register(radio, STATUSRSSI);
638 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300639 goto stop;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300640 timed_out = time_after(jiffies, timeout);
641 } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
642 (!timed_out));
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300643 if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
644 printk(KERN_WARNING DRIVER_NAME ": tune does not complete\n");
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300645 if (timed_out)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300646 printk(KERN_WARNING DRIVER_NAME
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300647 ": tune timed out after %u ms\n", tune_timeout);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300648
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300649stop:
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300650 /* stop tuning */
651 radio->registers[CHANNEL] &= ~CHANNEL_TUNE;
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300652 retval = si470x_set_register(radio, CHANNEL);
653
654done:
655 return retval;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300656}
657
658
659/*
660 * si470x_get_freq - get the frequency
661 */
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300662static int si470x_get_freq(struct si470x_device *radio, unsigned int *freq)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300663{
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300664 unsigned int spacing, band_bottom;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300665 unsigned short chan;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300666 int retval;
667
668 /* Spacing (kHz) */
669 switch (space) {
670 /* 0: 200 kHz (USA, Australia) */
671 case 0 : spacing = 0.200 * FREQ_MUL; break;
672 /* 1: 100 kHz (Europe, Japan) */
673 case 1 : spacing = 0.100 * FREQ_MUL; break;
674 /* 2: 50 kHz */
675 default: spacing = 0.050 * FREQ_MUL; break;
676 };
677
678 /* Bottom of Band (MHz) */
679 switch (band) {
680 /* 0: 87.5 - 108 MHz (USA, Europe) */
681 case 0 : band_bottom = 87.5 * FREQ_MUL; break;
682 /* 1: 76 - 108 MHz (Japan wide band) */
683 default: band_bottom = 76 * FREQ_MUL; break;
684 /* 2: 76 - 90 MHz (Japan) */
685 case 2 : band_bottom = 76 * FREQ_MUL; break;
686 };
687
688 /* read channel */
689 retval = si470x_get_register(radio, READCHAN);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300690 chan = radio->registers[READCHAN] & READCHAN_READCHAN;
691
692 /* Frequency (MHz) = Spacing (kHz) x Channel + Bottom of Band (MHz) */
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300693 *freq = chan * spacing + band_bottom;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300694
Tobias Lorenz6cc72652008-05-31 15:06:50 -0300695 return retval;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300696}
697
698
699/*
700 * si470x_set_freq - set the frequency
701 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300702static int si470x_set_freq(struct si470x_device *radio, unsigned int freq)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300703{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300704 unsigned int spacing, band_bottom;
705 unsigned short chan;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300706
707 /* Spacing (kHz) */
708 switch (space) {
709 /* 0: 200 kHz (USA, Australia) */
710 case 0 : spacing = 0.200 * FREQ_MUL; break;
711 /* 1: 100 kHz (Europe, Japan) */
712 case 1 : spacing = 0.100 * FREQ_MUL; break;
713 /* 2: 50 kHz */
714 default: spacing = 0.050 * FREQ_MUL; break;
715 };
716
717 /* Bottom of Band (MHz) */
718 switch (band) {
719 /* 0: 87.5 - 108 MHz (USA, Europe) */
720 case 0 : band_bottom = 87.5 * FREQ_MUL; break;
721 /* 1: 76 - 108 MHz (Japan wide band) */
722 default: band_bottom = 76 * FREQ_MUL; break;
723 /* 2: 76 - 90 MHz (Japan) */
724 case 2 : band_bottom = 76 * FREQ_MUL; break;
725 };
726
727 /* Chan = [ Freq (Mhz) - Bottom of Band (MHz) ] / Spacing (kHz) */
728 chan = (freq - band_bottom) / spacing;
729
730 return si470x_set_chan(radio, chan);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300731}
732
733
734/*
Tobias Lorenzbbc89952008-05-31 15:11:32 -0300735 * si470x_set_seek - set seek
736 */
737static int si470x_set_seek(struct si470x_device *radio,
738 unsigned int wrap_around, unsigned int seek_upward)
739{
740 int retval = 0;
741 unsigned long timeout;
742 bool timed_out = 0;
743
744 /* start seeking */
745 radio->registers[POWERCFG] |= POWERCFG_SEEK;
746 if (wrap_around == 1)
747 radio->registers[POWERCFG] &= ~POWERCFG_SKMODE;
748 else
749 radio->registers[POWERCFG] |= POWERCFG_SKMODE;
750 if (seek_upward == 1)
751 radio->registers[POWERCFG] |= POWERCFG_SEEKUP;
752 else
753 radio->registers[POWERCFG] &= ~POWERCFG_SEEKUP;
754 retval = si470x_set_register(radio, POWERCFG);
755 if (retval < 0)
756 goto done;
757
758 /* wait till seek operation has completed */
759 timeout = jiffies + msecs_to_jiffies(seek_timeout);
760 do {
761 retval = si470x_get_register(radio, STATUSRSSI);
762 if (retval < 0)
763 goto stop;
764 timed_out = time_after(jiffies, timeout);
765 } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
766 (!timed_out));
767 if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
768 printk(KERN_WARNING DRIVER_NAME ": seek does not complete\n");
769 if (radio->registers[STATUSRSSI] & STATUSRSSI_SF)
770 printk(KERN_WARNING DRIVER_NAME
771 ": seek failed / band limit reached\n");
772 if (timed_out)
773 printk(KERN_WARNING DRIVER_NAME
774 ": seek timed out after %u ms\n", seek_timeout);
775
776stop:
777 /* stop seeking */
778 radio->registers[POWERCFG] &= ~POWERCFG_SEEK;
779 retval = si470x_set_register(radio, POWERCFG);
780
781done:
782 /* try again, if timed out */
783 if ((retval == 0) && timed_out)
784 retval = -EAGAIN;
785
786 return retval;
787}
788
789
790/*
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300791 * si470x_start - switch on radio
792 */
793static int si470x_start(struct si470x_device *radio)
794{
795 int retval;
796
797 /* powercfg */
798 radio->registers[POWERCFG] =
799 POWERCFG_DMUTE | POWERCFG_ENABLE | POWERCFG_RDSM;
800 retval = si470x_set_register(radio, POWERCFG);
801 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300802 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300803
804 /* sysconfig 1 */
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300805 radio->registers[SYSCONFIG1] = SYSCONFIG1_DE;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300806 retval = si470x_set_register(radio, SYSCONFIG1);
807 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300808 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300809
810 /* sysconfig 2 */
811 radio->registers[SYSCONFIG2] =
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300812 (0x3f << 8) | /* SEEKTH */
813 ((band << 6) & SYSCONFIG2_BAND) | /* BAND */
814 ((space << 4) & SYSCONFIG2_SPACE) | /* SPACE */
815 15; /* VOLUME (max) */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300816 retval = si470x_set_register(radio, SYSCONFIG2);
817 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300818 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300819
820 /* reset last channel */
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300821 retval = si470x_set_chan(radio,
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300822 radio->registers[CHANNEL] & CHANNEL_CHAN);
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300823
824done:
825 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300826}
827
828
829/*
830 * si470x_stop - switch off radio
831 */
832static int si470x_stop(struct si470x_device *radio)
833{
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300834 int retval;
835
836 /* sysconfig 1 */
837 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
838 retval = si470x_set_register(radio, SYSCONFIG1);
839 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300840 goto done;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300841
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300842 /* powercfg */
843 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
844 /* POWERCFG_ENABLE has to automatically go low */
845 radio->registers[POWERCFG] |= POWERCFG_ENABLE | POWERCFG_DISABLE;
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300846 retval = si470x_set_register(radio, POWERCFG);
847
848done:
849 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300850}
851
852
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300853/*
854 * si470x_rds_on - switch on rds reception
855 */
856static int si470x_rds_on(struct si470x_device *radio)
857{
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300858 int retval;
859
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300860 /* sysconfig 1 */
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300861 mutex_lock(&radio->lock);
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300862 radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDS;
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300863 retval = si470x_set_register(radio, SYSCONFIG1);
864 if (retval < 0)
865 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
866 mutex_unlock(&radio->lock);
867
868 return retval;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300869}
870
871
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300872
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300873/**************************************************************************
874 * RDS Driver Functions
875 **************************************************************************/
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300876
877/*
878 * si470x_rds - rds processing function
879 */
880static void si470x_rds(struct si470x_device *radio)
881{
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300882 unsigned char blocknum;
883 unsigned short bler; /* rds block errors */
884 unsigned short rds;
885 unsigned char tmpbuf[3];
886
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300887 /* get rds blocks */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300888 if (si470x_get_rds_registers(radio) < 0)
889 return;
890 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0) {
891 /* No RDS group ready */
892 return;
893 }
894 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSS) == 0) {
895 /* RDS decoder not synchronized */
896 return;
897 }
898
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300899 /* copy all four RDS blocks to internal buffer */
900 mutex_lock(&radio->lock);
901 for (blocknum = 0; blocknum < 4; blocknum++) {
902 switch (blocknum) {
903 default:
904 bler = (radio->registers[STATUSRSSI] &
905 STATUSRSSI_BLERA) >> 9;
906 rds = radio->registers[RDSA];
907 break;
908 case 1:
909 bler = (radio->registers[READCHAN] &
910 READCHAN_BLERB) >> 14;
911 rds = radio->registers[RDSB];
912 break;
913 case 2:
914 bler = (radio->registers[READCHAN] &
915 READCHAN_BLERC) >> 12;
916 rds = radio->registers[RDSC];
917 break;
918 case 3:
919 bler = (radio->registers[READCHAN] &
920 READCHAN_BLERD) >> 10;
921 rds = radio->registers[RDSD];
922 break;
923 };
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300924
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300925 /* Fill the V4L2 RDS buffer */
Harvey Harrison5ad6b812008-04-15 22:22:11 -0300926 put_unaligned_le16(rds, &tmpbuf);
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300927 tmpbuf[2] = blocknum; /* offset name */
928 tmpbuf[2] |= blocknum << 3; /* received offset */
929 if (bler > max_rds_errors)
930 tmpbuf[2] |= 0x80; /* uncorrectable errors */
931 else if (bler > 0)
932 tmpbuf[2] |= 0x40; /* corrected error(s) */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300933
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300934 /* copy RDS block to internal buffer */
935 memcpy(&radio->buffer[radio->wr_index], &tmpbuf, 3);
936 radio->wr_index += 3;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300937
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300938 /* wrap write pointer */
939 if (radio->wr_index >= radio->buf_size)
940 radio->wr_index = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300941
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300942 /* check for overflow */
943 if (radio->wr_index == radio->rd_index) {
944 /* increment and wrap read pointer */
945 radio->rd_index += 3;
946 if (radio->rd_index >= radio->buf_size)
947 radio->rd_index = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300948 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300949 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300950 mutex_unlock(&radio->lock);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300951
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300952 /* wake up read queue */
953 if (radio->wr_index != radio->rd_index)
954 wake_up_interruptible(&radio->read_queue);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300955}
956
957
958/*
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300959 * si470x_work - rds work function
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300960 */
961static void si470x_work(struct work_struct *work)
962{
963 struct si470x_device *radio = container_of(work, struct si470x_device,
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300964 work.work);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300965
Tobias Lorenza7c850a2008-05-31 15:07:52 -0300966 /* safety checks */
Tobias Lorenzce5829e2008-05-31 15:04:32 -0300967 if (radio->disconnected)
968 return;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300969 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300970 return;
971
972 si470x_rds(radio);
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300973 schedule_delayed_work(&radio->work, msecs_to_jiffies(rds_poll_time));
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300974}
975
976
977
978/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300979 * File Operations Interface
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300980 **************************************************************************/
981
982/*
983 * si470x_fops_read - read RDS data
984 */
985static ssize_t si470x_fops_read(struct file *file, char __user *buf,
986 size_t count, loff_t *ppos)
987{
988 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300989 int retval = 0;
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300990 unsigned int block_count = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300991
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300992 /* switch on rds reception */
993 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
994 si470x_rds_on(radio);
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300995 schedule_delayed_work(&radio->work,
996 msecs_to_jiffies(rds_poll_time));
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300997 }
998
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300999 /* block if no new data available */
1000 while (radio->wr_index == radio->rd_index) {
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001001 if (file->f_flags & O_NONBLOCK) {
1002 retval = -EWOULDBLOCK;
1003 goto done;
1004 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001005 if (wait_event_interruptible(radio->read_queue,
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001006 radio->wr_index != radio->rd_index) < 0) {
1007 retval = -EINTR;
1008 goto done;
1009 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001010 }
1011
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001012 /* calculate block count from byte count */
1013 count /= 3;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001014
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001015 /* copy RDS block out of internal buffer and to user buffer */
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001016 mutex_lock(&radio->lock);
1017 while (block_count < count) {
1018 if (radio->rd_index == radio->wr_index)
1019 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001020
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001021 /* always transfer rds complete blocks */
1022 if (copy_to_user(buf, &radio->buffer[radio->rd_index], 3))
1023 /* retval = -EFAULT; */
1024 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001025
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001026 /* increment and wrap read pointer */
1027 radio->rd_index += 3;
1028 if (radio->rd_index >= radio->buf_size)
1029 radio->rd_index = 0;
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001030
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001031 /* increment counters */
1032 block_count++;
1033 buf += 3;
1034 retval += 3;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001035 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001036 mutex_unlock(&radio->lock);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001037
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001038done:
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001039 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001040}
1041
1042
1043/*
1044 * si470x_fops_poll - poll RDS data
1045 */
1046static unsigned int si470x_fops_poll(struct file *file,
1047 struct poll_table_struct *pts)
1048{
1049 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001050 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001051
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001052 /* switch on rds reception */
1053 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
1054 si470x_rds_on(radio);
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001055 schedule_delayed_work(&radio->work,
1056 msecs_to_jiffies(rds_poll_time));
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001057 }
1058
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001059 poll_wait(file, &radio->read_queue, pts);
1060
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001061 if (radio->rd_index != radio->wr_index)
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001062 retval = POLLIN | POLLRDNORM;
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001063
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001064 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001065}
1066
1067
1068/*
1069 * si470x_fops_open - file open
1070 */
1071static int si470x_fops_open(struct inode *inode, struct file *file)
1072{
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001073 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001074 int retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001075
1076 radio->users++;
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001077
1078 retval = usb_autopm_get_interface(radio->intf);
1079 if (retval < 0) {
1080 radio->users--;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001081 retval = -EIO;
1082 goto done;
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001083 }
1084
1085 if (radio->users == 1) {
1086 retval = si470x_start(radio);
1087 if (retval < 0)
1088 usb_autopm_put_interface(radio->intf);
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001089 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001090
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001091done:
1092 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001093}
1094
1095
1096/*
1097 * si470x_fops_release - file release
1098 */
1099static int si470x_fops_release(struct inode *inode, struct file *file)
1100{
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001101 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001102 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001103
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001104 /* safety check */
1105 if (!radio) {
1106 retval = -ENODEV;
1107 goto done;
1108 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001109
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001110 mutex_lock(&radio->disconnect_lock);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001111 radio->users--;
1112 if (radio->users == 0) {
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001113 if (radio->disconnected) {
1114 video_unregister_device(radio->videodev);
1115 kfree(radio->buffer);
1116 kfree(radio);
1117 goto unlock;
1118 }
Tobias Lorenz03dea862008-04-22 14:46:11 -03001119
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001120 /* stop rds reception */
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001121 cancel_delayed_work_sync(&radio->work);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001122
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001123 /* cancel read processes */
1124 wake_up_interruptible(&radio->read_queue);
1125
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001126 retval = si470x_stop(radio);
1127 usb_autopm_put_interface(radio->intf);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001128 }
1129
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001130unlock:
1131 mutex_unlock(&radio->disconnect_lock);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001132
1133done:
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001134 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001135}
1136
1137
1138/*
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001139 * si470x_fops - file operations interface
1140 */
1141static const struct file_operations si470x_fops = {
1142 .owner = THIS_MODULE,
1143 .llseek = no_llseek,
1144 .read = si470x_fops_read,
1145 .poll = si470x_fops_poll,
1146 .ioctl = video_ioctl2,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -03001147#ifdef CONFIG_COMPAT
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001148 .compat_ioctl = v4l_compat_ioctl32,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -03001149#endif
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001150 .open = si470x_fops_open,
1151 .release = si470x_fops_release,
1152};
1153
1154
1155
1156/**************************************************************************
1157 * Video4Linux Interface
1158 **************************************************************************/
1159
1160/*
1161 * si470x_v4l2_queryctrl - query control
1162 */
1163static struct v4l2_queryctrl si470x_v4l2_queryctrl[] = {
1164/* HINT: the disabled controls are only here to satify kradio and such apps */
1165 {
1166 .id = V4L2_CID_AUDIO_VOLUME,
1167 .type = V4L2_CTRL_TYPE_INTEGER,
1168 .name = "Volume",
1169 .minimum = 0,
1170 .maximum = 15,
1171 .step = 1,
1172 .default_value = 15,
1173 },
1174 {
1175 .id = V4L2_CID_AUDIO_BALANCE,
1176 .flags = V4L2_CTRL_FLAG_DISABLED,
1177 },
1178 {
1179 .id = V4L2_CID_AUDIO_BASS,
1180 .flags = V4L2_CTRL_FLAG_DISABLED,
1181 },
1182 {
1183 .id = V4L2_CID_AUDIO_TREBLE,
1184 .flags = V4L2_CTRL_FLAG_DISABLED,
1185 },
1186 {
1187 .id = V4L2_CID_AUDIO_MUTE,
1188 .type = V4L2_CTRL_TYPE_BOOLEAN,
1189 .name = "Mute",
1190 .minimum = 0,
1191 .maximum = 1,
1192 .step = 1,
1193 .default_value = 1,
1194 },
1195 {
1196 .id = V4L2_CID_AUDIO_LOUDNESS,
1197 .flags = V4L2_CTRL_FLAG_DISABLED,
1198 },
1199};
1200
1201
1202/*
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001203 * si470x_vidioc_querycap - query device capabilities
1204 */
1205static int si470x_vidioc_querycap(struct file *file, void *priv,
1206 struct v4l2_capability *capability)
1207{
1208 strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver));
1209 strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
1210 sprintf(capability->bus_info, "USB");
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001211 capability->version = DRIVER_KERNEL_VERSION;
Tobias Lorenzbbc89952008-05-31 15:11:32 -03001212 capability->capabilities = V4L2_CAP_HW_FREQ_SEEK |
1213 V4L2_CAP_TUNER | V4L2_CAP_RADIO;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001214
1215 return 0;
1216}
1217
1218
1219/*
1220 * si470x_vidioc_g_input - get input
1221 */
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001222static int si470x_vidioc_g_input(struct file *file, void *priv,
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001223 unsigned int *i)
1224{
1225 *i = 0;
1226
1227 return 0;
1228}
1229
1230
1231/*
1232 * si470x_vidioc_s_input - set input
1233 */
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001234static int si470x_vidioc_s_input(struct file *file, void *priv, unsigned int i)
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001235{
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001236 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001237
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001238 /* safety checks */
1239 if (i != 0)
1240 retval = -EINVAL;
1241
1242 if (retval < 0)
1243 printk(KERN_WARNING DRIVER_NAME
1244 ": set input failed with %d\n", retval);
1245 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001246}
1247
1248
1249/*
1250 * si470x_vidioc_queryctrl - enumerate control items
1251 */
1252static int si470x_vidioc_queryctrl(struct file *file, void *priv,
1253 struct v4l2_queryctrl *qc)
1254{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001255 unsigned char i;
1256 int retval = -EINVAL;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001257
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001258 /* safety checks */
1259 if (!qc->id)
1260 goto done;
1261
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001262 for (i = 0; i < ARRAY_SIZE(si470x_v4l2_queryctrl); i++) {
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001263 if (qc->id == si470x_v4l2_queryctrl[i].id) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001264 memcpy(qc, &(si470x_v4l2_queryctrl[i]), sizeof(*qc));
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001265 retval = 0;
1266 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001267 }
1268 }
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001269
1270done:
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001271 if (retval < 0)
1272 printk(KERN_WARNING DRIVER_NAME
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001273 ": query controls failed with %d\n", retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001274 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001275}
1276
1277
1278/*
1279 * si470x_vidioc_g_ctrl - get the value of a control
1280 */
1281static int si470x_vidioc_g_ctrl(struct file *file, void *priv,
1282 struct v4l2_control *ctrl)
1283{
1284 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001285 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001286
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001287 /* safety checks */
1288 if (radio->disconnected) {
1289 retval = -EIO;
1290 goto done;
1291 }
Tobias Lorenz03dea862008-04-22 14:46:11 -03001292
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001293 switch (ctrl->id) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001294 case V4L2_CID_AUDIO_VOLUME:
1295 ctrl->value = radio->registers[SYSCONFIG2] &
1296 SYSCONFIG2_VOLUME;
1297 break;
1298 case V4L2_CID_AUDIO_MUTE:
1299 ctrl->value = ((radio->registers[POWERCFG] &
1300 POWERCFG_DMUTE) == 0) ? 1 : 0;
1301 break;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001302 default:
1303 retval = -EINVAL;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001304 }
1305
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001306done:
1307 if (retval < 0)
1308 printk(KERN_WARNING DRIVER_NAME
1309 ": get control failed with %d\n", retval);
1310 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001311}
1312
1313
1314/*
1315 * si470x_vidioc_s_ctrl - set the value of a control
1316 */
1317static int si470x_vidioc_s_ctrl(struct file *file, void *priv,
1318 struct v4l2_control *ctrl)
1319{
1320 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001321 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001322
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001323 /* safety checks */
1324 if (radio->disconnected) {
1325 retval = -EIO;
1326 goto done;
1327 }
Tobias Lorenz03dea862008-04-22 14:46:11 -03001328
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001329 switch (ctrl->id) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001330 case V4L2_CID_AUDIO_VOLUME:
1331 radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_VOLUME;
1332 radio->registers[SYSCONFIG2] |= ctrl->value;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001333 retval = si470x_set_register(radio, SYSCONFIG2);
1334 break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001335 case V4L2_CID_AUDIO_MUTE:
1336 if (ctrl->value == 1)
1337 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
1338 else
1339 radio->registers[POWERCFG] |= POWERCFG_DMUTE;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001340 retval = si470x_set_register(radio, POWERCFG);
1341 break;
1342 default:
1343 retval = -EINVAL;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001344 }
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001345
1346done:
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001347 if (retval < 0)
1348 printk(KERN_WARNING DRIVER_NAME
1349 ": set control failed with %d\n", retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001350 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001351}
1352
1353
1354/*
1355 * si470x_vidioc_g_audio - get audio attributes
1356 */
1357static int si470x_vidioc_g_audio(struct file *file, void *priv,
1358 struct v4l2_audio *audio)
1359{
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001360 int retval = 0;
1361
1362 /* safety checks */
1363 if (audio->index != 0) {
1364 retval = -EINVAL;
1365 goto done;
1366 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001367
1368 strcpy(audio->name, "Radio");
1369 audio->capability = V4L2_AUDCAP_STEREO;
1370
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001371done:
1372 if (retval < 0)
1373 printk(KERN_WARNING DRIVER_NAME
1374 ": get audio failed with %d\n", retval);
1375 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001376}
1377
1378
1379/*
1380 * si470x_vidioc_s_audio - set audio attributes
1381 */
1382static int si470x_vidioc_s_audio(struct file *file, void *priv,
1383 struct v4l2_audio *audio)
1384{
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001385 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001386
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001387 /* safety checks */
1388 if (audio->index != 0) {
1389 retval = -EINVAL;
1390 goto done;
1391 }
1392
1393done:
1394 if (retval < 0)
1395 printk(KERN_WARNING DRIVER_NAME
1396 ": set audio failed with %d\n", retval);
1397 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001398}
1399
1400
1401/*
1402 * si470x_vidioc_g_tuner - get tuner attributes
1403 */
1404static int si470x_vidioc_g_tuner(struct file *file, void *priv,
1405 struct v4l2_tuner *tuner)
1406{
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001407 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001408 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001409
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001410 /* safety checks */
1411 if (radio->disconnected) {
1412 retval = -EIO;
1413 goto done;
1414 }
1415 if ((tuner->index != 0) && (tuner->type != V4L2_TUNER_RADIO)) {
1416 retval = -EINVAL;
1417 goto done;
1418 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001419
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001420 retval = si470x_get_register(radio, STATUSRSSI);
1421 if (retval < 0)
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001422 goto done;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001423
1424 strcpy(tuner->name, "FM");
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001425 switch (band) {
1426 /* 0: 87.5 - 108 MHz (USA, Europe, default) */
1427 default:
1428 tuner->rangelow = 87.5 * FREQ_MUL;
1429 tuner->rangehigh = 108 * FREQ_MUL;
1430 break;
1431 /* 1: 76 - 108 MHz (Japan wide band) */
1432 case 1 :
1433 tuner->rangelow = 76 * FREQ_MUL;
1434 tuner->rangehigh = 108 * FREQ_MUL;
1435 break;
1436 /* 2: 76 - 90 MHz (Japan) */
1437 case 2 :
1438 tuner->rangelow = 76 * FREQ_MUL;
1439 tuner->rangehigh = 90 * FREQ_MUL;
1440 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001441 };
1442 tuner->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
1443 tuner->capability = V4L2_TUNER_CAP_LOW;
1444
1445 /* Stereo indicator == Stereo (instead of Mono) */
1446 if ((radio->registers[STATUSRSSI] & STATUSRSSI_ST) == 1)
1447 tuner->audmode = V4L2_TUNER_MODE_STEREO;
1448 else
1449 tuner->audmode = V4L2_TUNER_MODE_MONO;
1450
1451 /* min is worst, max is best; signal:0..0xffff; rssi: 0..0xff */
1452 tuner->signal = (radio->registers[STATUSRSSI] & STATUSRSSI_RSSI)
1453 * 0x0101;
1454
1455 /* automatic frequency control: -1: freq to low, 1 freq to high */
Tobias Lorenzace7d4b2008-05-31 15:09:07 -03001456 /* AFCRL does only indicate that freq. differs, not if too low/high */
1457 tuner->afc = (radio->registers[STATUSRSSI] & STATUSRSSI_AFCRL) ? 1 : 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001458
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001459done:
1460 if (retval < 0)
1461 printk(KERN_WARNING DRIVER_NAME
1462 ": get tuner failed with %d\n", retval);
1463 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001464}
1465
1466
1467/*
1468 * si470x_vidioc_s_tuner - set tuner attributes
1469 */
1470static int si470x_vidioc_s_tuner(struct file *file, void *priv,
1471 struct v4l2_tuner *tuner)
1472{
1473 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001474 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001475
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001476 /* safety checks */
1477 if (radio->disconnected) {
1478 retval = -EIO;
1479 goto done;
1480 }
1481 if ((tuner->index != 0) && (tuner->type != V4L2_TUNER_RADIO)) {
1482 retval = -EINVAL;
1483 goto done;
1484 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001485
1486 if (tuner->audmode == V4L2_TUNER_MODE_MONO)
1487 radio->registers[POWERCFG] |= POWERCFG_MONO; /* force mono */
1488 else
1489 radio->registers[POWERCFG] &= ~POWERCFG_MONO; /* try stereo */
1490
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001491 retval = si470x_set_register(radio, POWERCFG);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001492
1493done:
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001494 if (retval < 0)
1495 printk(KERN_WARNING DRIVER_NAME
1496 ": set tuner failed with %d\n", retval);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001497 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001498}
1499
1500
1501/*
1502 * si470x_vidioc_g_frequency - get tuner or modulator radio frequency
1503 */
1504static int si470x_vidioc_g_frequency(struct file *file, void *priv,
1505 struct v4l2_frequency *freq)
1506{
1507 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001508 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001509
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001510 /* safety checks */
1511 if (radio->disconnected) {
1512 retval = -EIO;
1513 goto done;
1514 }
1515 if ((freq->tuner != 0) && (freq->type != V4L2_TUNER_RADIO)) {
1516 retval = -EINVAL;
1517 goto done;
1518 }
Tobias Lorenz03dea862008-04-22 14:46:11 -03001519
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001520 retval = si470x_get_freq(radio, &freq->frequency);
1521
1522done:
1523 if (retval < 0)
1524 printk(KERN_WARNING DRIVER_NAME
1525 ": get frequency failed with %d\n", retval);
1526 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001527}
1528
1529
1530/*
1531 * si470x_vidioc_s_frequency - set tuner or modulator radio frequency
1532 */
1533static int si470x_vidioc_s_frequency(struct file *file, void *priv,
1534 struct v4l2_frequency *freq)
1535{
1536 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001537 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001538
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001539 /* safety checks */
1540 if (radio->disconnected) {
1541 retval = -EIO;
1542 goto done;
1543 }
1544 if ((freq->tuner != 0) && (freq->type != V4L2_TUNER_RADIO)) {
1545 retval = -EINVAL;
1546 goto done;
1547 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001548
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001549 retval = si470x_set_freq(radio, freq->frequency);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001550
1551done:
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001552 if (retval < 0)
1553 printk(KERN_WARNING DRIVER_NAME
1554 ": set frequency failed with %d\n", retval);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001555 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001556}
1557
1558
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001559/*
Tobias Lorenzbbc89952008-05-31 15:11:32 -03001560 * si470x_vidioc_s_hw_freq_seek - set hardware frequency seek
1561 */
1562static int si470x_vidioc_s_hw_freq_seek(struct file *file, void *priv,
1563 struct v4l2_hw_freq_seek *seek)
1564{
1565 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1566 int retval = 0;
1567
1568 /* safety checks */
1569 if (radio->disconnected) {
1570 retval = -EIO;
1571 goto done;
1572 }
1573 if ((seek->tuner != 0) && (seek->type != V4L2_TUNER_RADIO)) {
1574 retval = -EINVAL;
1575 goto done;
1576 }
1577
1578 retval = si470x_set_seek(radio, seek->wrap_around, seek->seek_upward);
1579
1580done:
1581 if (retval < 0)
1582 printk(KERN_WARNING DRIVER_NAME
1583 ": set hardware frequency seek failed with %d\n",
1584 retval);
1585 return retval;
1586}
1587
1588
1589/*
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001590 * si470x_viddev_tamples - video device interface
1591 */
1592static struct video_device si470x_viddev_template = {
1593 .fops = &si470x_fops,
1594 .name = DRIVER_NAME,
1595 .type = VID_TYPE_TUNER,
1596 .release = video_device_release,
1597 .vidioc_querycap = si470x_vidioc_querycap,
1598 .vidioc_g_input = si470x_vidioc_g_input,
1599 .vidioc_s_input = si470x_vidioc_s_input,
1600 .vidioc_queryctrl = si470x_vidioc_queryctrl,
1601 .vidioc_g_ctrl = si470x_vidioc_g_ctrl,
1602 .vidioc_s_ctrl = si470x_vidioc_s_ctrl,
1603 .vidioc_g_audio = si470x_vidioc_g_audio,
1604 .vidioc_s_audio = si470x_vidioc_s_audio,
1605 .vidioc_g_tuner = si470x_vidioc_g_tuner,
1606 .vidioc_s_tuner = si470x_vidioc_s_tuner,
1607 .vidioc_g_frequency = si470x_vidioc_g_frequency,
1608 .vidioc_s_frequency = si470x_vidioc_s_frequency,
Tobias Lorenzbbc89952008-05-31 15:11:32 -03001609 .vidioc_s_hw_freq_seek = si470x_vidioc_s_hw_freq_seek,
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001610 .owner = THIS_MODULE,
1611};
1612
1613
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001614
1615/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001616 * USB Interface
1617 **************************************************************************/
1618
1619/*
1620 * si470x_usb_driver_probe - probe for the device
1621 */
1622static int si470x_usb_driver_probe(struct usb_interface *intf,
1623 const struct usb_device_id *id)
1624{
1625 struct si470x_device *radio;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001626 int retval = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001627
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001628 /* private data allocation and initialization */
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001629 radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001630 if (!radio) {
1631 retval = -ENOMEM;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001632 goto err_initial;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001633 }
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001634 radio->users = 0;
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001635 radio->disconnected = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001636 radio->usbdev = interface_to_usbdev(intf);
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001637 radio->intf = intf;
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001638 mutex_init(&radio->disconnect_lock);
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001639 mutex_init(&radio->lock);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001640
1641 /* video device allocation and initialization */
1642 radio->videodev = video_device_alloc();
1643 if (!radio->videodev) {
1644 retval = -ENOMEM;
1645 goto err_radio;
1646 }
1647 memcpy(radio->videodev, &si470x_viddev_template,
1648 sizeof(si470x_viddev_template));
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001649 video_set_drvdata(radio->videodev, radio);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001650
1651 /* show some infos about the specific device */
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001652 if (si470x_get_all_registers(radio) < 0) {
1653 retval = -EIO;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001654 goto err_all;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001655 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001656 printk(KERN_INFO DRIVER_NAME ": DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001657 radio->registers[DEVICEID], radio->registers[CHIPID]);
1658
1659 /* check if firmware is current */
1660 if ((radio->registers[CHIPID] & CHIPID_FIRMWARE)
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001661 < RADIO_SW_VERSION_CURRENT) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001662 printk(KERN_WARNING DRIVER_NAME
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001663 ": This driver is known to work with "
1664 "firmware version %hu,\n", RADIO_SW_VERSION_CURRENT);
1665 printk(KERN_WARNING DRIVER_NAME
1666 ": but the device has firmware version %hu.\n",
1667 radio->registers[CHIPID] & CHIPID_FIRMWARE);
1668 printk(KERN_WARNING DRIVER_NAME
1669 ": If you have some trouble using this driver,\n");
1670 printk(KERN_WARNING DRIVER_NAME
1671 ": please report to V4L ML at "
1672 "video4linux-list@redhat.com\n");
1673 }
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001674
1675 /* set initial frequency */
1676 si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
1677
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001678 /* rds buffer allocation */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001679 radio->buf_size = rds_buf * 3;
1680 radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001681 if (!radio->buffer) {
1682 retval = -EIO;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001683 goto err_all;
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001684 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001685
1686 /* rds buffer configuration */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001687 radio->wr_index = 0;
1688 radio->rd_index = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001689 init_waitqueue_head(&radio->read_queue);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001690
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001691 /* prepare rds work function */
1692 INIT_DELAYED_WORK(&radio->work, si470x_work);
1693
1694 /* register video device */
1695 if (video_register_device(radio->videodev, VFL_TYPE_RADIO, radio_nr)) {
Tobias Lorenza7c850a2008-05-31 15:07:52 -03001696 retval = -EIO;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001697 printk(KERN_WARNING DRIVER_NAME
1698 ": Could not register video device\n");
1699 goto err_all;
1700 }
1701 usb_set_intfdata(intf, radio);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001702
1703 return 0;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001704err_all:
1705 video_device_release(radio->videodev);
1706 kfree(radio->buffer);
1707err_radio:
1708 kfree(radio);
1709err_initial:
1710 return retval;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001711}
1712
1713
1714/*
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001715 * si470x_usb_driver_suspend - suspend the device
1716 */
1717static int si470x_usb_driver_suspend(struct usb_interface *intf,
1718 pm_message_t message)
1719{
1720 struct si470x_device *radio = usb_get_intfdata(intf);
1721
1722 printk(KERN_INFO DRIVER_NAME ": suspending now...\n");
1723
1724 cancel_delayed_work_sync(&radio->work);
1725
1726 return 0;
1727}
1728
1729
1730/*
1731 * si470x_usb_driver_resume - resume the device
1732 */
1733static int si470x_usb_driver_resume(struct usb_interface *intf)
1734{
1735 struct si470x_device *radio = usb_get_intfdata(intf);
1736
1737 printk(KERN_INFO DRIVER_NAME ": resuming now...\n");
1738
1739 mutex_lock(&radio->lock);
1740 if (radio->users && radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS)
1741 schedule_delayed_work(&radio->work,
1742 msecs_to_jiffies(rds_poll_time));
1743 mutex_unlock(&radio->lock);
1744
1745 return 0;
1746}
1747
1748
1749/*
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001750 * si470x_usb_driver_disconnect - disconnect the device
1751 */
1752static void si470x_usb_driver_disconnect(struct usb_interface *intf)
1753{
1754 struct si470x_device *radio = usb_get_intfdata(intf);
1755
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001756 mutex_lock(&radio->disconnect_lock);
1757 radio->disconnected = 1;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001758 cancel_delayed_work_sync(&radio->work);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001759 usb_set_intfdata(intf, NULL);
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001760 if (radio->users == 0) {
1761 video_unregister_device(radio->videodev);
1762 kfree(radio->buffer);
1763 kfree(radio);
1764 }
1765 mutex_unlock(&radio->disconnect_lock);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001766}
1767
1768
1769/*
1770 * si470x_usb_driver - usb driver interface
1771 */
1772static struct usb_driver si470x_usb_driver = {
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001773 .name = DRIVER_NAME,
1774 .probe = si470x_usb_driver_probe,
1775 .disconnect = si470x_usb_driver_disconnect,
1776 .suspend = si470x_usb_driver_suspend,
1777 .resume = si470x_usb_driver_resume,
1778 .id_table = si470x_usb_driver_id_table,
1779 .supports_autosuspend = 1,
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001780};
1781
1782
1783
1784/**************************************************************************
1785 * Module Interface
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001786 **************************************************************************/
1787
1788/*
1789 * si470x_module_init - module init
1790 */
1791static int __init si470x_module_init(void)
1792{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001793 printk(KERN_INFO DRIVER_DESC ", Version " DRIVER_VERSION "\n");
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001794 return usb_register(&si470x_usb_driver);
1795}
1796
1797
1798/*
1799 * si470x_module_exit - module exit
1800 */
1801static void __exit si470x_module_exit(void)
1802{
1803 usb_deregister(&si470x_usb_driver);
1804}
1805
1806
1807module_init(si470x_module_init);
1808module_exit(si470x_module_exit);
1809
1810MODULE_LICENSE("GPL");
1811MODULE_AUTHOR(DRIVER_AUTHOR);
1812MODULE_DESCRIPTION(DRIVER_DESC);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001813MODULE_VERSION(DRIVER_VERSION);