blob: c72a9e7ad8856c5fd19306093e9cdc118adb28b4 [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/*
27 * History:
28 * 2008-01-12 Tobias Lorenz <tobias.lorenz@gmx.net>
29 * Version 1.0.0
30 * - First working version
31 * 2008-01-13 Tobias Lorenz <tobias.lorenz@gmx.net>
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -030032 * Version 1.0.1
Tobias Lorenz78656ac2008-01-14 21:55:27 -030033 * - Improved error handling, every function now returns errno
34 * - Improved multi user access (start/mute/stop)
35 * - Channel doesn't get lost anymore after start/mute/stop
36 * - RDS support added (polling mode via interrupt EP 1)
37 * - marked default module parameters with *value*
38 * - switched from bit structs to bit masks
39 * - header file cleaned and integrated
40 * 2008-01-14 Tobias Lorenz <tobias.lorenz@gmx.net>
41 * Version 1.0.2
42 * - hex values are now lower case
43 * - commented USB ID for ADS/Tech moved on todo list
44 * - blacklisted si470x in hid-quirks.c
45 * - rds buffer handling functions integrated into *_work, *_read
46 * - rds_command in si470x_poll exchanged against simple retval
47 * - check for firmware version 15
48 * - code order and prototypes still remain the same
49 * - spacing and bottom of band codes remain the same
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -030050 * 2008-01-16 Tobias Lorenz <tobias.lorenz@gmx.net>
51 * Version 1.0.3
52 * - code reordered to avoid function prototypes
53 * - switch/case defaults are now more user-friendly
54 * - unified comment style
55 * - applied all checkpatch.pl v1.12 suggestions
56 * except the warning about the too long lines with bit comments
57 * - renamed FMRADIO to RADIO to cut line length (checkpatch.pl)
Tobias Lorenz2fb88402008-01-25 05:14:57 -030058 * 2008-01-22 Tobias Lorenz <tobias.lorenz@gmx.net>
59 * Version 1.0.4
60 * - avoid poss. locking when doing copy_to_user which may sleep
61 * - RDS is automatically activated on read now
62 * - code cleaned of unnecessary rds_commands
63 * - USB Vendor/Product ID for ADS/Tech FM Radio Receiver verified
64 * (thanks to Guillaume RAMOUSSE)
Tobias Lorenz0e3301e2008-01-27 14:54:07 -030065 * 2008-01-27 Tobias Lorenz <tobias.lorenz@gmx.net>
66 * Version 1.0.5
67 * - number of seek_retries changed to tune_timeout
68 * - fixed problem with incomplete tune operations by own buffers
Tobias Lorenz5caf5132008-02-04 22:26:08 -030069 * - optimization of variables and printf types
Tobias Lorenz0e3301e2008-01-27 14:54:07 -030070 * - improved error logging
Tobias Lorenz5caf5132008-02-04 22:26:08 -030071 * 2008-01-31 Tobias Lorenz <tobias.lorenz@gmx.net>
72 * Oliver Neukum <oliver@neukum.org>
73 * Version 1.0.6
74 * - fixed coverity checker warnings in *_usb_driver_disconnect
75 * - probe()/open() race by correct ordering in probe()
76 * - DMA coherency rules by separate allocation of all buffers
77 * - use of endianness macros
78 * - abuse of spinlock, replaced by mutex
79 * - racy handling of timer in disconnect,
80 * replaced by delayed_work
81 * - racy interruptible_sleep_on(),
82 * replaced with wait_event_interruptible()
83 * - handle signals in read()
Tobias Lorenz78656ac2008-01-14 21:55:27 -030084 *
85 * ToDo:
Tobias Lorenz78656ac2008-01-14 21:55:27 -030086 * - add seeking support
87 * - add firmware download/update support
Tobias Lorenz78656ac2008-01-14 21:55:27 -030088 * - RDS support: interrupt mode, instead of polling
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -030089 * - add LED status output (check if that's not already done in firmware)
Tobias Lorenz78656ac2008-01-14 21:55:27 -030090 */
91
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -030092
Tobias Lorenz78656ac2008-01-14 21:55:27 -030093/* driver definitions */
94#define DRIVER_AUTHOR "Tobias Lorenz <tobias.lorenz@gmx.net>"
95#define DRIVER_NAME "radio-si470x"
Tobias Lorenz5caf5132008-02-04 22:26:08 -030096#define DRIVER_KERNEL_VERSION KERNEL_VERSION(1, 0, 6)
Tobias Lorenz78656ac2008-01-14 21:55:27 -030097#define DRIVER_CARD "Silicon Labs Si470x FM Radio Receiver"
98#define DRIVER_DESC "USB radio driver for Si470x FM Radio Receivers"
Tobias Lorenz5caf5132008-02-04 22:26:08 -030099#define DRIVER_VERSION "1.0.6"
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300100
101
102/* kernel includes */
103#include <linux/kernel.h>
104#include <linux/module.h>
105#include <linux/init.h>
106#include <linux/slab.h>
107#include <linux/input.h>
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300108#include <linux/usb.h>
109#include <linux/hid.h>
110#include <linux/version.h>
Mauro Carvalho Chehab387d4472008-01-15 11:25:10 -0300111#include <linux/videodev2.h>
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300112#include <linux/mutex.h>
Mauro Carvalho Chehab387d4472008-01-15 11:25:10 -0300113#include <media/v4l2-common.h>
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300114#include <media/rds.h>
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300115#include <asm/unaligned.h>
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300116
117
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300118/* USB Device ID List */
119static struct usb_device_id si470x_usb_driver_id_table[] = {
120 /* Silicon Labs USB FM Radio Reference Design */
121 { USB_DEVICE_AND_INTERFACE_INFO(0x10c4, 0x818a, USB_CLASS_HID, 0, 0) },
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300122 /* ADS/Tech FM Radio Receiver (formerly Instant FM Music) */
123 { USB_DEVICE_AND_INTERFACE_INFO(0x06e1, 0xa155, USB_CLASS_HID, 0, 0) },
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300124 /* Terminating entry */
125 { }
126};
127MODULE_DEVICE_TABLE(usb, si470x_usb_driver_id_table);
128
129
130
131/**************************************************************************
132 * Module Parameters
133 **************************************************************************/
134
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300135/* Radio Nr */
136static int radio_nr = -1;
137module_param(radio_nr, int, 0);
138MODULE_PARM_DESC(radio_nr, "Radio Nr");
139
140/* Spacing (kHz) */
141/* 0: 200 kHz (USA, Australia) */
142/* 1: 100 kHz (Europe, Japan) */
143/* 2: 50 kHz */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300144static unsigned short space = 2;
145module_param(space, ushort, 0);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300146MODULE_PARM_DESC(radio_nr, "Spacing: 0=200kHz 1=100kHz *2=50kHz*");
147
148/* Bottom of Band (MHz) */
149/* 0: 87.5 - 108 MHz (USA, Europe)*/
150/* 1: 76 - 108 MHz (Japan wide band) */
151/* 2: 76 - 90 MHz (Japan) */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300152static unsigned short band = 1;
153module_param(band, ushort, 0);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300154MODULE_PARM_DESC(radio_nr, "Band: 0=87.5..108MHz *1=76..108MHz* 2=76..90MHz");
155
156/* De-emphasis */
157/* 0: 75 us (USA) */
158/* 1: 50 us (Europe, Australia, Japan) */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300159static unsigned short de = 1;
160module_param(de, ushort, 0);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300161MODULE_PARM_DESC(radio_nr, "De-emphasis: 0=75us *1=50us*");
162
163/* USB timeout */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300164static unsigned int usb_timeout = 500;
165module_param(usb_timeout, uint, 0);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300166MODULE_PARM_DESC(usb_timeout, "USB timeout (ms): *500*");
167
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300168/* Tune timeout */
169static unsigned int tune_timeout = 3000;
170module_param(tune_timeout, uint, 0);
171MODULE_PARM_DESC(tune_timeout, "Tune timeout: *3000*");
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300172
173/* RDS buffer blocks */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300174static unsigned int rds_buf = 100;
175module_param(rds_buf, uint, 0);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300176MODULE_PARM_DESC(rds_buf, "RDS buffer entries: *100*");
177
178/* RDS maximum block errors */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300179static unsigned short max_rds_errors = 1;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300180/* 0 means 0 errors requiring correction */
181/* 1 means 1-2 errors requiring correction (used by original USBRadio.exe) */
182/* 2 means 3-5 errors requiring correction */
183/* 3 means 6+ errors or errors in checkword, correction not possible */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300184module_param(max_rds_errors, ushort, 0);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300185MODULE_PARM_DESC(max_rds_errors, "RDS maximum block errors: *1*");
186
187/* RDS poll frequency */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300188static unsigned int rds_poll_time = 40;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300189/* 40 is used by the original USBRadio.exe */
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300190/* 50 is used by radio-cadet */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300191/* 75 should be okay */
192/* 80 is the usual RDS receive interval */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300193module_param(rds_poll_time, uint, 0);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300194MODULE_PARM_DESC(rds_poll_time, "RDS poll time (ms): *40*");
195
196
197
198/**************************************************************************
199 * Register Definitions
200 **************************************************************************/
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300201#define RADIO_REGISTER_SIZE 2 /* 16 register bit width */
202#define RADIO_REGISTER_NUM 16 /* DEVICEID ... RDSD */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300203#define RDS_REGISTER_NUM 6 /* STATUSRSSI ... RDSD */
204
205#define DEVICEID 0 /* Device ID */
206#define DEVICEID_PN 0xf000 /* bits 15..12: Part Number */
207#define DEVICEID_MFGID 0x0fff /* bits 11..00: Manufacturer ID */
208
209#define CHIPID 1 /* Chip ID */
210#define CHIPID_REV 0xfc00 /* bits 15..10: Chip Version */
211#define CHIPID_DEV 0x0200 /* bits 09..09: Device */
212#define CHIPID_FIRMWARE 0x01ff /* bits 08..00: Firmware Version */
213
214#define POWERCFG 2 /* Power Configuration */
215#define POWERCFG_DSMUTE 0x8000 /* bits 15..15: Softmute Disable */
216#define POWERCFG_DMUTE 0x4000 /* bits 14..14: Mute Disable */
217#define POWERCFG_MONO 0x2000 /* bits 13..13: Mono Select */
218#define POWERCFG_RDSM 0x0800 /* bits 11..11: RDS Mode (Si4701 only) */
219#define POWERCFG_SKMODE 0x0400 /* bits 10..10: Seek Mode */
220#define POWERCFG_SEEKUP 0x0200 /* bits 09..09: Seek Direction */
221#define POWERCFG_SEEK 0x0100 /* bits 08..08: Seek */
222#define POWERCFG_DISABLE 0x0040 /* bits 06..06: Powerup Disable */
223#define POWERCFG_ENABLE 0x0001 /* bits 00..00: Powerup Enable */
224
225#define CHANNEL 3 /* Channel */
226#define CHANNEL_TUNE 0x8000 /* bits 15..15: Tune */
227#define CHANNEL_CHAN 0x03ff /* bits 09..00: Channel Select */
228
229#define SYSCONFIG1 4 /* System Configuration 1 */
230#define SYSCONFIG1_RDSIEN 0x8000 /* bits 15..15: RDS Interrupt Enable (Si4701 only) */
231#define SYSCONFIG1_STCIEN 0x4000 /* bits 14..14: Seek/Tune Complete Interrupt Enable */
232#define SYSCONFIG1_RDS 0x1000 /* bits 12..12: RDS Enable (Si4701 only) */
233#define SYSCONFIG1_DE 0x0800 /* bits 11..11: De-emphasis (0=75us 1=50us) */
234#define SYSCONFIG1_AGCD 0x0400 /* bits 10..10: AGC Disable */
235#define SYSCONFIG1_BLNDADJ 0x00c0 /* bits 07..06: Stereo/Mono Blend Level Adjustment */
236#define SYSCONFIG1_GPIO3 0x0030 /* bits 05..04: General Purpose I/O 3 */
237#define SYSCONFIG1_GPIO2 0x000c /* bits 03..02: General Purpose I/O 2 */
238#define SYSCONFIG1_GPIO1 0x0003 /* bits 01..00: General Purpose I/O 1 */
239
240#define SYSCONFIG2 5 /* System Configuration 2 */
241#define SYSCONFIG2_SEEKTH 0xff00 /* bits 15..08: RSSI Seek Threshold */
242#define SYSCONFIG2_BAND 0x0080 /* bits 07..06: Band Select */
243#define SYSCONFIG2_SPACE 0x0030 /* bits 05..04: Channel Spacing */
244#define SYSCONFIG2_VOLUME 0x000f /* bits 03..00: Volume */
245
246#define SYSCONFIG3 6 /* System Configuration 3 */
247#define SYSCONFIG3_SMUTER 0xc000 /* bits 15..14: Softmute Attack/Recover Rate */
248#define SYSCONFIG3_SMUTEA 0x3000 /* bits 13..12: Softmute Attenuation */
249#define SYSCONFIG3_SKSNR 0x00f0 /* bits 07..04: Seek SNR Threshold */
250#define SYSCONFIG3_SKCNT 0x000f /* bits 03..00: Seek FM Impulse Detection Threshold */
251
252#define TEST1 7 /* Test 1 */
253#define TEST1_AHIZEN 0x4000 /* bits 14..14: Audio High-Z Enable */
254
255#define TEST2 8 /* Test 2 */
256/* TEST2 only contains reserved bits */
257
258#define BOOTCONFIG 9 /* Boot Configuration */
259/* BOOTCONFIG only contains reserved bits */
260
261#define STATUSRSSI 10 /* Status RSSI */
262#define STATUSRSSI_RDSR 0x8000 /* bits 15..15: RDS Ready (Si4701 only) */
263#define STATUSRSSI_STC 0x4000 /* bits 14..14: Seek/Tune Complete */
264#define STATUSRSSI_SF 0x2000 /* bits 13..13: Seek Fail/Band Limit */
265#define STATUSRSSI_AFCRL 0x1000 /* bits 12..12: AFC Rail */
266#define STATUSRSSI_RDSS 0x0800 /* bits 11..11: RDS Synchronized (Si4701 only) */
267#define STATUSRSSI_BLERA 0x0600 /* bits 10..09: RDS Block A Errors (Si4701 only) */
268#define STATUSRSSI_ST 0x0100 /* bits 08..08: Stereo Indicator */
269#define STATUSRSSI_RSSI 0x00ff /* bits 07..00: RSSI (Received Signal Strength Indicator) */
270
271#define READCHAN 11 /* Read Channel */
272#define READCHAN_BLERB 0xc000 /* bits 15..14: RDS Block D Errors (Si4701 only) */
273#define READCHAN_BLERC 0x3000 /* bits 13..12: RDS Block C Errors (Si4701 only) */
274#define READCHAN_BLERD 0x0c00 /* bits 11..10: RDS Block B Errors (Si4701 only) */
275#define READCHAN_READCHAN 0x03ff /* bits 09..00: Read Channel */
276
277#define RDSA 12 /* RDSA */
278#define RDSA_RDSA 0xffff /* bits 15..00: RDS Block A Data (Si4701 only) */
279
280#define RDSB 13 /* RDSB */
281#define RDSB_RDSB 0xffff /* bits 15..00: RDS Block B Data (Si4701 only) */
282
283#define RDSC 14 /* RDSC */
284#define RDSC_RDSC 0xffff /* bits 15..00: RDS Block C Data (Si4701 only) */
285
286#define RDSD 15 /* RDSD */
287#define RDSD_RDSD 0xffff /* bits 15..00: RDS Block D Data (Si4701 only) */
288
289
290
291/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300292 * USB HID Reports
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300293 **************************************************************************/
294
295/* Reports 1-16 give direct read/write access to the 16 Si470x registers */
296/* with the (REPORT_ID - 1) corresponding to the register address across USB */
297/* endpoint 0 using GET_REPORT and SET_REPORT */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300298#define REGISTER_REPORT_SIZE (RADIO_REGISTER_SIZE + 1)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300299#define REGISTER_REPORT(reg) ((reg) + 1)
300
301/* Report 17 gives direct read/write access to the entire Si470x register */
302/* map across endpoint 0 using GET_REPORT and SET_REPORT */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300303#define ENTIRE_REPORT_SIZE (RADIO_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300304#define ENTIRE_REPORT 17
305
306/* Report 18 is used to send the lowest 6 Si470x registers up the HID */
307/* interrupt endpoint 1 to Windows every 20 milliseconds for status */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300308#define RDS_REPORT_SIZE (RDS_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300309#define RDS_REPORT 18
310
311/* Report 19: LED state */
312#define LED_REPORT_SIZE 3
313#define LED_REPORT 19
314
315/* Report 19: stream */
316#define STREAM_REPORT_SIZE 3
317#define STREAM_REPORT 19
318
319/* Report 20: scratch */
320#define SCRATCH_PAGE_SIZE 63
321#define SCRATCH_REPORT_SIZE (SCRATCH_PAGE_SIZE + 1)
322#define SCRATCH_REPORT 20
323
324/* Reports 19-22: flash upgrade of the C8051F321 */
325#define WRITE_REPORT 19
326#define FLASH_REPORT 20
327#define CRC_REPORT 21
328#define RESPONSE_REPORT 22
329
330/* Report 23: currently unused, but can accept 60 byte reports on the HID */
331/* interrupt out endpoint 2 every 1 millisecond */
332#define UNUSED_REPORT 23
333
334
335
336/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300337 * Software/Hardware Versions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300338 **************************************************************************/
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300339#define RADIO_SW_VERSION_NOT_BOOTLOADABLE 6
340#define RADIO_SW_VERSION 7
341#define RADIO_SW_VERSION_CURRENT 15
342#define RADIO_HW_VERSION 1
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300343
344#define SCRATCH_PAGE_SW_VERSION 1
345#define SCRATCH_PAGE_HW_VERSION 2
346
347
348
349/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300350 * LED State Definitions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300351 **************************************************************************/
352#define LED_COMMAND 0x35
353
354#define NO_CHANGE_LED 0x00
355#define ALL_COLOR_LED 0x01 /* streaming state */
356#define BLINK_GREEN_LED 0x02 /* connect state */
357#define BLINK_RED_LED 0x04
358#define BLINK_ORANGE_LED 0x10 /* disconnect state */
359#define SOLID_GREEN_LED 0x20 /* tuning/seeking state */
360#define SOLID_RED_LED 0x40 /* bootload state */
361#define SOLID_ORANGE_LED 0x80
362
363
364
365/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300366 * Stream State Definitions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300367 **************************************************************************/
368#define STREAM_COMMAND 0x36
369#define STREAM_VIDPID 0x00
370#define STREAM_AUDIO 0xff
371
372
373
374/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300375 * Bootloader / Flash Commands
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300376 **************************************************************************/
377
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300378/* unique id sent to bootloader and required to put into a bootload state */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300379#define UNIQUE_BL_ID 0x34
380
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300381/* mask for the flash data */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300382#define FLASH_DATA_MASK 0x55
383
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300384/* bootloader commands */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300385#define GET_SW_VERSION_COMMAND 0x00
386#define SET_PAGE_COMMAND 0x01
387#define ERASE_PAGE_COMMAND 0x02
388#define WRITE_PAGE_COMMAND 0x03
389#define CRC_ON_PAGE_COMMAND 0x04
390#define READ_FLASH_BYTE_COMMAND 0x05
391#define RESET_DEVICE_COMMAND 0x06
392#define GET_HW_VERSION_COMMAND 0x07
393#define BLANK 0xff
394
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300395/* bootloader command responses */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300396#define COMMAND_OK 0x01
397#define COMMAND_FAILED 0x02
398#define COMMAND_PENDING 0x03
399
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300400/* buffer sizes */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300401#define COMMAND_BUFFER_SIZE 4
402#define RESPONSE_BUFFER_SIZE 2
403#define FLASH_BUFFER_SIZE 64
404#define CRC_BUFFER_SIZE 3
405
406
407
408/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300409 * General Driver Definitions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300410 **************************************************************************/
411
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300412/*
413 * si470x_device - private data
414 */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300415struct si470x_device {
416 /* reference to USB and video device */
417 struct usb_device *usbdev;
418 struct video_device *videodev;
419
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300420 /* driver management */
421 unsigned int users;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300422
423 /* Silabs internal registers (0..15) */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300424 unsigned short registers[RADIO_REGISTER_NUM];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300425
426 /* RDS receive buffer */
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300427 struct delayed_work work;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300428 wait_queue_head_t read_queue;
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300429 struct mutex lock; /* buffer locking */
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300430 unsigned char *buffer; /* size is always multiple of three */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300431 unsigned int buf_size;
432 unsigned int rd_index;
433 unsigned int wr_index;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300434};
435
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300436
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300437/*
438 * The frequency is set in units of 62.5 Hz when using V4L2_TUNER_CAP_LOW,
439 * 62.5 kHz otherwise.
440 * The tuner is able to have a channel spacing of 50, 100 or 200 kHz.
441 * tuner->capability is therefore set to V4L2_TUNER_CAP_LOW
442 * The FREQ_MUL is then: 1 MHz / 62.5 Hz = 16000
443 */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300444#define FREQ_MUL (1000000 / 62.5)
445
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300446
447
448/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300449 * General Driver Functions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300450 **************************************************************************/
451
452/*
453 * si470x_get_report - receive a HID report
454 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300455static int si470x_get_report(struct si470x_device *radio, void *buf, int size)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300456{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300457 unsigned char *report = (unsigned char *) buf;
458 int retval;
459
460 retval = usb_control_msg(radio->usbdev,
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300461 usb_rcvctrlpipe(radio->usbdev, 0),
462 HID_REQ_GET_REPORT,
463 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300464 report[0], 2,
465 buf, size, usb_timeout);
466 if (retval < 0)
467 printk(KERN_WARNING DRIVER_NAME
468 ": si470x_get_report: usb_control_msg returned %d\n",
469 retval);
470
471 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300472}
473
474
475/*
476 * si470x_set_report - send a HID report
477 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300478static int si470x_set_report(struct si470x_device *radio, void *buf, int size)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300479{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300480 unsigned char *report = (unsigned char *) buf;
481 int retval;
482
483 retval = usb_control_msg(radio->usbdev,
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300484 usb_sndctrlpipe(radio->usbdev, 0),
485 HID_REQ_SET_REPORT,
486 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300487 report[0], 2,
488 buf, size, usb_timeout);
489 if (retval < 0)
490 printk(KERN_WARNING DRIVER_NAME
491 ": si470x_set_report: usb_control_msg returned %d\n",
492 retval);
493
494 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300495}
496
497
498/*
499 * si470x_get_register - read register
500 */
501static int si470x_get_register(struct si470x_device *radio, int regnr)
502{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300503 unsigned char buf[REGISTER_REPORT_SIZE];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300504 int retval;
505
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300506 buf[0] = REGISTER_REPORT(regnr);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300507
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300508 retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
509
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300510 if (retval >= 0)
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300511 radio->registers[regnr] = be16_to_cpu(get_unaligned(
512 (unsigned short *) &buf[1]));
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300513
514 return (retval < 0) ? -EINVAL : 0;
515}
516
517
518/*
519 * si470x_set_register - write register
520 */
521static int si470x_set_register(struct si470x_device *radio, int regnr)
522{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300523 unsigned char buf[REGISTER_REPORT_SIZE];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300524 int retval;
525
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300526 buf[0] = REGISTER_REPORT(regnr);
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300527 put_unaligned(cpu_to_be16(radio->registers[regnr]),
528 (unsigned short *) &buf[1]);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300529
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300530 retval = si470x_set_report(radio, (void *) &buf, sizeof(buf));
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300531
532 return (retval < 0) ? -EINVAL : 0;
533}
534
535
536/*
537 * si470x_get_all_registers - read entire registers
538 */
539static int si470x_get_all_registers(struct si470x_device *radio)
540{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300541 unsigned char buf[ENTIRE_REPORT_SIZE];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300542 int retval;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300543 unsigned char regnr;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300544
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300545 buf[0] = ENTIRE_REPORT;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300546
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300547 retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300548
549 if (retval >= 0)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300550 for (regnr = 0; regnr < RADIO_REGISTER_NUM; regnr++)
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300551 radio->registers[regnr] = be16_to_cpu(get_unaligned(
552 (unsigned short *)
553 &buf[regnr * RADIO_REGISTER_SIZE + 1]));
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300554
555 return (retval < 0) ? -EINVAL : 0;
556}
557
558
559/*
560 * si470x_get_rds_registers - read rds registers
561 */
562static int si470x_get_rds_registers(struct si470x_device *radio)
563{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300564 unsigned char buf[RDS_REPORT_SIZE];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300565 int retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300566 int size;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300567 unsigned char regnr;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300568
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300569 buf[0] = RDS_REPORT;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300570
571 retval = usb_interrupt_msg(radio->usbdev,
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300572 usb_rcvintpipe(radio->usbdev, 1),
573 (void *) &buf, sizeof(buf), &size, usb_timeout);
574 if (size != sizeof(buf))
575 printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_register: "
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300576 "return size differs: %d != %zu\n", size, sizeof(buf));
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300577 if (retval < 0)
578 printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: "
579 "usb_interrupt_msg returned %d\n", retval);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300580
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300581 if (retval >= 0)
582 for (regnr = 0; regnr < RDS_REGISTER_NUM; regnr++)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300583 radio->registers[STATUSRSSI + regnr] =
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300584 be16_to_cpu(get_unaligned((unsigned short *)
585 &buf[regnr * RADIO_REGISTER_SIZE + 1]));
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300586
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300587 return (retval < 0) ? -EINVAL : 0;
588}
589
590
591/*
592 * si470x_set_chan - set the channel
593 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300594static int si470x_set_chan(struct si470x_device *radio, unsigned short chan)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300595{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300596 int retval;
597 unsigned long timeout;
598 bool timed_out = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300599
600 /* start tuning */
601 radio->registers[CHANNEL] &= ~CHANNEL_CHAN;
602 radio->registers[CHANNEL] |= CHANNEL_TUNE | chan;
603 retval = si470x_set_register(radio, CHANNEL);
604 if (retval < 0)
605 return retval;
606
607 /* wait till seek operation has completed */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300608 timeout = jiffies + msecs_to_jiffies(tune_timeout);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300609 do {
610 retval = si470x_get_register(radio, STATUSRSSI);
611 if (retval < 0)
612 return retval;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300613 timed_out = time_after(jiffies, timeout);
614 } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
615 (!timed_out));
616 if (timed_out)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300617 printk(KERN_WARNING DRIVER_NAME
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300618 ": seek does not finish after %u ms\n", tune_timeout);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300619
620 /* stop tuning */
621 radio->registers[CHANNEL] &= ~CHANNEL_TUNE;
622 return si470x_set_register(radio, CHANNEL);
623}
624
625
626/*
627 * si470x_get_freq - get the frequency
628 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300629static unsigned int si470x_get_freq(struct si470x_device *radio)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300630{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300631 unsigned int spacing, band_bottom, freq;
632 unsigned short chan;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300633 int retval;
634
635 /* Spacing (kHz) */
636 switch (space) {
637 /* 0: 200 kHz (USA, Australia) */
638 case 0 : spacing = 0.200 * FREQ_MUL; break;
639 /* 1: 100 kHz (Europe, Japan) */
640 case 1 : spacing = 0.100 * FREQ_MUL; break;
641 /* 2: 50 kHz */
642 default: spacing = 0.050 * FREQ_MUL; break;
643 };
644
645 /* Bottom of Band (MHz) */
646 switch (band) {
647 /* 0: 87.5 - 108 MHz (USA, Europe) */
648 case 0 : band_bottom = 87.5 * FREQ_MUL; break;
649 /* 1: 76 - 108 MHz (Japan wide band) */
650 default: band_bottom = 76 * FREQ_MUL; break;
651 /* 2: 76 - 90 MHz (Japan) */
652 case 2 : band_bottom = 76 * FREQ_MUL; break;
653 };
654
655 /* read channel */
656 retval = si470x_get_register(radio, READCHAN);
657 if (retval < 0)
658 return retval;
659 chan = radio->registers[READCHAN] & READCHAN_READCHAN;
660
661 /* Frequency (MHz) = Spacing (kHz) x Channel + Bottom of Band (MHz) */
662 freq = chan * spacing + band_bottom;
663
664 return freq;
665}
666
667
668/*
669 * si470x_set_freq - set the frequency
670 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300671static int si470x_set_freq(struct si470x_device *radio, unsigned int freq)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300672{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300673 unsigned int spacing, band_bottom;
674 unsigned short chan;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300675
676 /* Spacing (kHz) */
677 switch (space) {
678 /* 0: 200 kHz (USA, Australia) */
679 case 0 : spacing = 0.200 * FREQ_MUL; break;
680 /* 1: 100 kHz (Europe, Japan) */
681 case 1 : spacing = 0.100 * FREQ_MUL; break;
682 /* 2: 50 kHz */
683 default: spacing = 0.050 * FREQ_MUL; break;
684 };
685
686 /* Bottom of Band (MHz) */
687 switch (band) {
688 /* 0: 87.5 - 108 MHz (USA, Europe) */
689 case 0 : band_bottom = 87.5 * FREQ_MUL; break;
690 /* 1: 76 - 108 MHz (Japan wide band) */
691 default: band_bottom = 76 * FREQ_MUL; break;
692 /* 2: 76 - 90 MHz (Japan) */
693 case 2 : band_bottom = 76 * FREQ_MUL; break;
694 };
695
696 /* Chan = [ Freq (Mhz) - Bottom of Band (MHz) ] / Spacing (kHz) */
697 chan = (freq - band_bottom) / spacing;
698
699 return si470x_set_chan(radio, chan);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300700}
701
702
703/*
704 * si470x_start - switch on radio
705 */
706static int si470x_start(struct si470x_device *radio)
707{
708 int retval;
709
710 /* powercfg */
711 radio->registers[POWERCFG] =
712 POWERCFG_DMUTE | POWERCFG_ENABLE | POWERCFG_RDSM;
713 retval = si470x_set_register(radio, POWERCFG);
714 if (retval < 0)
715 return retval;
716
717 /* sysconfig 1 */
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300718 radio->registers[SYSCONFIG1] = SYSCONFIG1_DE;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300719 retval = si470x_set_register(radio, SYSCONFIG1);
720 if (retval < 0)
721 return retval;
722
723 /* sysconfig 2 */
724 radio->registers[SYSCONFIG2] =
725 (0x3f << 8) | /* SEEKTH */
726 (band << 6) | /* BAND */
727 (space << 4) | /* SPACE */
728 15; /* VOLUME (max) */
729 retval = si470x_set_register(radio, SYSCONFIG2);
730 if (retval < 0)
731 return retval;
732
733 /* reset last channel */
734 return si470x_set_chan(radio,
735 radio->registers[CHANNEL] & CHANNEL_CHAN);
736}
737
738
739/*
740 * si470x_stop - switch off radio
741 */
742static int si470x_stop(struct si470x_device *radio)
743{
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300744 int retval;
745
746 /* sysconfig 1 */
747 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
748 retval = si470x_set_register(radio, SYSCONFIG1);
749 if (retval < 0)
750 return retval;
751
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300752 /* powercfg */
753 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
754 /* POWERCFG_ENABLE has to automatically go low */
755 radio->registers[POWERCFG] |= POWERCFG_ENABLE | POWERCFG_DISABLE;
756 return si470x_set_register(radio, POWERCFG);
757}
758
759
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300760/*
761 * si470x_rds_on - switch on rds reception
762 */
763static int si470x_rds_on(struct si470x_device *radio)
764{
765 /* sysconfig 1 */
766 radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDS;
767 return si470x_set_register(radio, SYSCONFIG1);
768}
769
770
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300771
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300772/**************************************************************************
773 * RDS Driver Functions
774 **************************************************************************/
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300775
776/*
777 * si470x_rds - rds processing function
778 */
779static void si470x_rds(struct si470x_device *radio)
780{
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300781 unsigned char blocknum;
782 unsigned short bler; /* rds block errors */
783 unsigned short rds;
784 unsigned char tmpbuf[3];
785
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300786 /* get rds blocks */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300787 if (si470x_get_rds_registers(radio) < 0)
788 return;
789 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0) {
790 /* No RDS group ready */
791 return;
792 }
793 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSS) == 0) {
794 /* RDS decoder not synchronized */
795 return;
796 }
797
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300798 /* copy all four RDS blocks to internal buffer */
799 mutex_lock(&radio->lock);
800 for (blocknum = 0; blocknum < 4; blocknum++) {
801 switch (blocknum) {
802 default:
803 bler = (radio->registers[STATUSRSSI] &
804 STATUSRSSI_BLERA) >> 9;
805 rds = radio->registers[RDSA];
806 break;
807 case 1:
808 bler = (radio->registers[READCHAN] &
809 READCHAN_BLERB) >> 14;
810 rds = radio->registers[RDSB];
811 break;
812 case 2:
813 bler = (radio->registers[READCHAN] &
814 READCHAN_BLERC) >> 12;
815 rds = radio->registers[RDSC];
816 break;
817 case 3:
818 bler = (radio->registers[READCHAN] &
819 READCHAN_BLERD) >> 10;
820 rds = radio->registers[RDSD];
821 break;
822 };
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300823
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300824 /* Fill the V4L2 RDS buffer */
825 put_unaligned(cpu_to_le16(rds), (unsigned short *) &tmpbuf);
826 tmpbuf[2] = blocknum; /* offset name */
827 tmpbuf[2] |= blocknum << 3; /* received offset */
828 if (bler > max_rds_errors)
829 tmpbuf[2] |= 0x80; /* uncorrectable errors */
830 else if (bler > 0)
831 tmpbuf[2] |= 0x40; /* corrected error(s) */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300832
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300833 /* copy RDS block to internal buffer */
834 memcpy(&radio->buffer[radio->wr_index], &tmpbuf, 3);
835 radio->wr_index += 3;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300836
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300837 /* wrap write pointer */
838 if (radio->wr_index >= radio->buf_size)
839 radio->wr_index = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300840
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300841 /* check for overflow */
842 if (radio->wr_index == radio->rd_index) {
843 /* increment and wrap read pointer */
844 radio->rd_index += 3;
845 if (radio->rd_index >= radio->buf_size)
846 radio->rd_index = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300847 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300848 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300849 mutex_unlock(&radio->lock);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300850
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300851 /* wake up read queue */
852 if (radio->wr_index != radio->rd_index)
853 wake_up_interruptible(&radio->read_queue);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300854}
855
856
857/*
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300858 * si470x_work - rds work function
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300859 */
860static void si470x_work(struct work_struct *work)
861{
862 struct si470x_device *radio = container_of(work, struct si470x_device,
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300863 work.work);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300864
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300865 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300866 return;
867
868 si470x_rds(radio);
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300869 schedule_delayed_work(&radio->work, msecs_to_jiffies(rds_poll_time));
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300870}
871
872
873
874/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300875 * File Operations Interface
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300876 **************************************************************************/
877
878/*
879 * si470x_fops_read - read RDS data
880 */
881static ssize_t si470x_fops_read(struct file *file, char __user *buf,
882 size_t count, loff_t *ppos)
883{
884 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300885 int retval = 0;
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300886 unsigned int block_count = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300887
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300888 /* switch on rds reception */
889 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
890 si470x_rds_on(radio);
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300891 schedule_delayed_work(&radio->work,
892 msecs_to_jiffies(rds_poll_time));
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300893 }
894
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300895 /* block if no new data available */
896 while (radio->wr_index == radio->rd_index) {
897 if (file->f_flags & O_NONBLOCK)
898 return -EWOULDBLOCK;
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300899 if (wait_event_interruptible(radio->read_queue,
900 radio->wr_index != radio->rd_index) < 0)
901 return -EINTR;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300902 }
903
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300904 /* calculate block count from byte count */
905 count /= 3;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300906
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300907 /* copy RDS block out of internal buffer and to user buffer */
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300908 mutex_lock(&radio->lock);
909 while (block_count < count) {
910 if (radio->rd_index == radio->wr_index)
911 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300912
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300913 /* always transfer rds complete blocks */
914 if (copy_to_user(buf, &radio->buffer[radio->rd_index], 3))
915 /* retval = -EFAULT; */
916 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300917
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300918 /* increment and wrap read pointer */
919 radio->rd_index += 3;
920 if (radio->rd_index >= radio->buf_size)
921 radio->rd_index = 0;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300922
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300923 /* increment counters */
924 block_count++;
925 buf += 3;
926 retval += 3;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300927 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300928 mutex_unlock(&radio->lock);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300929
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300930 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300931}
932
933
934/*
935 * si470x_fops_poll - poll RDS data
936 */
937static unsigned int si470x_fops_poll(struct file *file,
938 struct poll_table_struct *pts)
939{
940 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300941
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300942 /* switch on rds reception */
943 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
944 si470x_rds_on(radio);
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300945 schedule_delayed_work(&radio->work,
946 msecs_to_jiffies(rds_poll_time));
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300947 }
948
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300949 poll_wait(file, &radio->read_queue, pts);
950
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300951 if (radio->rd_index != radio->wr_index)
952 return POLLIN | POLLRDNORM;
953
954 return 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300955}
956
957
958/*
959 * si470x_fops_open - file open
960 */
961static int si470x_fops_open(struct inode *inode, struct file *file)
962{
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300963 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
964
965 radio->users++;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300966 if (radio->users == 1)
967 return si470x_start(radio);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300968
969 return 0;
970}
971
972
973/*
974 * si470x_fops_release - file release
975 */
976static int si470x_fops_release(struct inode *inode, struct file *file)
977{
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300978 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
979
980 if (!radio)
981 return -ENODEV;
982
983 radio->users--;
984 if (radio->users == 0) {
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300985 /* stop rds reception */
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300986 cancel_delayed_work_sync(&radio->work);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300987
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300988 /* cancel read processes */
989 wake_up_interruptible(&radio->read_queue);
990
991 return si470x_stop(radio);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300992 }
993
994 return 0;
995}
996
997
998/*
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300999 * si470x_fops - file operations interface
1000 */
1001static const struct file_operations si470x_fops = {
1002 .owner = THIS_MODULE,
1003 .llseek = no_llseek,
1004 .read = si470x_fops_read,
1005 .poll = si470x_fops_poll,
1006 .ioctl = video_ioctl2,
1007 .compat_ioctl = v4l_compat_ioctl32,
1008 .open = si470x_fops_open,
1009 .release = si470x_fops_release,
1010};
1011
1012
1013
1014/**************************************************************************
1015 * Video4Linux Interface
1016 **************************************************************************/
1017
1018/*
1019 * si470x_v4l2_queryctrl - query control
1020 */
1021static struct v4l2_queryctrl si470x_v4l2_queryctrl[] = {
1022/* HINT: the disabled controls are only here to satify kradio and such apps */
1023 {
1024 .id = V4L2_CID_AUDIO_VOLUME,
1025 .type = V4L2_CTRL_TYPE_INTEGER,
1026 .name = "Volume",
1027 .minimum = 0,
1028 .maximum = 15,
1029 .step = 1,
1030 .default_value = 15,
1031 },
1032 {
1033 .id = V4L2_CID_AUDIO_BALANCE,
1034 .flags = V4L2_CTRL_FLAG_DISABLED,
1035 },
1036 {
1037 .id = V4L2_CID_AUDIO_BASS,
1038 .flags = V4L2_CTRL_FLAG_DISABLED,
1039 },
1040 {
1041 .id = V4L2_CID_AUDIO_TREBLE,
1042 .flags = V4L2_CTRL_FLAG_DISABLED,
1043 },
1044 {
1045 .id = V4L2_CID_AUDIO_MUTE,
1046 .type = V4L2_CTRL_TYPE_BOOLEAN,
1047 .name = "Mute",
1048 .minimum = 0,
1049 .maximum = 1,
1050 .step = 1,
1051 .default_value = 1,
1052 },
1053 {
1054 .id = V4L2_CID_AUDIO_LOUDNESS,
1055 .flags = V4L2_CTRL_FLAG_DISABLED,
1056 },
1057};
1058
1059
1060/*
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001061 * si470x_vidioc_querycap - query device capabilities
1062 */
1063static int si470x_vidioc_querycap(struct file *file, void *priv,
1064 struct v4l2_capability *capability)
1065{
1066 strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver));
1067 strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
1068 sprintf(capability->bus_info, "USB");
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001069 capability->version = DRIVER_KERNEL_VERSION;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001070 capability->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
1071
1072 return 0;
1073}
1074
1075
1076/*
1077 * si470x_vidioc_g_input - get input
1078 */
1079static int si470x_vidioc_g_input(struct file *filp, void *priv,
1080 unsigned int *i)
1081{
1082 *i = 0;
1083
1084 return 0;
1085}
1086
1087
1088/*
1089 * si470x_vidioc_s_input - set input
1090 */
1091static int si470x_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
1092{
1093 if (i != 0)
1094 return -EINVAL;
1095
1096 return 0;
1097}
1098
1099
1100/*
1101 * si470x_vidioc_queryctrl - enumerate control items
1102 */
1103static int si470x_vidioc_queryctrl(struct file *file, void *priv,
1104 struct v4l2_queryctrl *qc)
1105{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001106 unsigned char i;
1107 int retval = -EINVAL;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001108
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001109 for (i = 0; i < ARRAY_SIZE(si470x_v4l2_queryctrl); i++) {
1110 if (qc->id && qc->id == si470x_v4l2_queryctrl[i].id) {
1111 memcpy(qc, &(si470x_v4l2_queryctrl[i]), sizeof(*qc));
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001112 retval = 0;
1113 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001114 }
1115 }
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001116 if (retval < 0)
1117 printk(KERN_WARNING DRIVER_NAME
1118 ": query control failed with %d\n", retval);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001119
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001120 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001121}
1122
1123
1124/*
1125 * si470x_vidioc_g_ctrl - get the value of a control
1126 */
1127static int si470x_vidioc_g_ctrl(struct file *file, void *priv,
1128 struct v4l2_control *ctrl)
1129{
1130 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1131
1132 switch (ctrl->id) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001133 case V4L2_CID_AUDIO_VOLUME:
1134 ctrl->value = radio->registers[SYSCONFIG2] &
1135 SYSCONFIG2_VOLUME;
1136 break;
1137 case V4L2_CID_AUDIO_MUTE:
1138 ctrl->value = ((radio->registers[POWERCFG] &
1139 POWERCFG_DMUTE) == 0) ? 1 : 0;
1140 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001141 }
1142
1143 return 0;
1144}
1145
1146
1147/*
1148 * si470x_vidioc_s_ctrl - set the value of a control
1149 */
1150static int si470x_vidioc_s_ctrl(struct file *file, void *priv,
1151 struct v4l2_control *ctrl)
1152{
1153 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001154 int retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001155
1156 switch (ctrl->id) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001157 case V4L2_CID_AUDIO_VOLUME:
1158 radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_VOLUME;
1159 radio->registers[SYSCONFIG2] |= ctrl->value;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001160 retval = si470x_set_register(radio, SYSCONFIG2);
1161 break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001162 case V4L2_CID_AUDIO_MUTE:
1163 if (ctrl->value == 1)
1164 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
1165 else
1166 radio->registers[POWERCFG] |= POWERCFG_DMUTE;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001167 retval = si470x_set_register(radio, POWERCFG);
1168 break;
1169 default:
1170 retval = -EINVAL;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001171 }
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001172 if (retval < 0)
1173 printk(KERN_WARNING DRIVER_NAME
1174 ": set control failed with %d\n", retval);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001175
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001176 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001177}
1178
1179
1180/*
1181 * si470x_vidioc_g_audio - get audio attributes
1182 */
1183static int si470x_vidioc_g_audio(struct file *file, void *priv,
1184 struct v4l2_audio *audio)
1185{
1186 if (audio->index > 1)
1187 return -EINVAL;
1188
1189 strcpy(audio->name, "Radio");
1190 audio->capability = V4L2_AUDCAP_STEREO;
1191
1192 return 0;
1193}
1194
1195
1196/*
1197 * si470x_vidioc_s_audio - set audio attributes
1198 */
1199static int si470x_vidioc_s_audio(struct file *file, void *priv,
1200 struct v4l2_audio *audio)
1201{
1202 if (audio->index != 0)
1203 return -EINVAL;
1204
1205 return 0;
1206}
1207
1208
1209/*
1210 * si470x_vidioc_g_tuner - get tuner attributes
1211 */
1212static int si470x_vidioc_g_tuner(struct file *file, void *priv,
1213 struct v4l2_tuner *tuner)
1214{
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001215 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001216 int retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001217
1218 if (tuner->index > 0)
1219 return -EINVAL;
1220
1221 /* read status rssi */
1222 retval = si470x_get_register(radio, STATUSRSSI);
1223 if (retval < 0)
1224 return retval;
1225
1226 strcpy(tuner->name, "FM");
1227 tuner->type = V4L2_TUNER_RADIO;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001228 switch (band) {
1229 /* 0: 87.5 - 108 MHz (USA, Europe, default) */
1230 default:
1231 tuner->rangelow = 87.5 * FREQ_MUL;
1232 tuner->rangehigh = 108 * FREQ_MUL;
1233 break;
1234 /* 1: 76 - 108 MHz (Japan wide band) */
1235 case 1 :
1236 tuner->rangelow = 76 * FREQ_MUL;
1237 tuner->rangehigh = 108 * FREQ_MUL;
1238 break;
1239 /* 2: 76 - 90 MHz (Japan) */
1240 case 2 :
1241 tuner->rangelow = 76 * FREQ_MUL;
1242 tuner->rangehigh = 90 * FREQ_MUL;
1243 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001244 };
1245 tuner->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
1246 tuner->capability = V4L2_TUNER_CAP_LOW;
1247
1248 /* Stereo indicator == Stereo (instead of Mono) */
1249 if ((radio->registers[STATUSRSSI] & STATUSRSSI_ST) == 1)
1250 tuner->audmode = V4L2_TUNER_MODE_STEREO;
1251 else
1252 tuner->audmode = V4L2_TUNER_MODE_MONO;
1253
1254 /* min is worst, max is best; signal:0..0xffff; rssi: 0..0xff */
1255 tuner->signal = (radio->registers[STATUSRSSI] & STATUSRSSI_RSSI)
1256 * 0x0101;
1257
1258 /* automatic frequency control: -1: freq to low, 1 freq to high */
1259 tuner->afc = 0;
1260
1261 return 0;
1262}
1263
1264
1265/*
1266 * si470x_vidioc_s_tuner - set tuner attributes
1267 */
1268static int si470x_vidioc_s_tuner(struct file *file, void *priv,
1269 struct v4l2_tuner *tuner)
1270{
1271 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001272 int retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001273
1274 if (tuner->index > 0)
1275 return -EINVAL;
1276
1277 if (tuner->audmode == V4L2_TUNER_MODE_MONO)
1278 radio->registers[POWERCFG] |= POWERCFG_MONO; /* force mono */
1279 else
1280 radio->registers[POWERCFG] &= ~POWERCFG_MONO; /* try stereo */
1281
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001282 retval = si470x_set_register(radio, POWERCFG);
1283 if (retval < 0)
1284 printk(KERN_WARNING DRIVER_NAME
1285 ": set tuner failed with %d\n", retval);
1286
1287 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001288}
1289
1290
1291/*
1292 * si470x_vidioc_g_frequency - get tuner or modulator radio frequency
1293 */
1294static int si470x_vidioc_g_frequency(struct file *file, void *priv,
1295 struct v4l2_frequency *freq)
1296{
1297 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1298
1299 freq->type = V4L2_TUNER_RADIO;
1300 freq->frequency = si470x_get_freq(radio);
1301
1302 return 0;
1303}
1304
1305
1306/*
1307 * si470x_vidioc_s_frequency - set tuner or modulator radio frequency
1308 */
1309static int si470x_vidioc_s_frequency(struct file *file, void *priv,
1310 struct v4l2_frequency *freq)
1311{
1312 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001313 int retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001314
1315 if (freq->type != V4L2_TUNER_RADIO)
1316 return -EINVAL;
1317
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001318 retval = si470x_set_freq(radio, freq->frequency);
1319 if (retval < 0)
1320 printk(KERN_WARNING DRIVER_NAME
1321 ": set frequency failed with %d\n", retval);
1322
1323 return 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001324}
1325
1326
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001327/*
1328 * si470x_viddev_tamples - video device interface
1329 */
1330static struct video_device si470x_viddev_template = {
1331 .fops = &si470x_fops,
1332 .name = DRIVER_NAME,
1333 .type = VID_TYPE_TUNER,
1334 .release = video_device_release,
1335 .vidioc_querycap = si470x_vidioc_querycap,
1336 .vidioc_g_input = si470x_vidioc_g_input,
1337 .vidioc_s_input = si470x_vidioc_s_input,
1338 .vidioc_queryctrl = si470x_vidioc_queryctrl,
1339 .vidioc_g_ctrl = si470x_vidioc_g_ctrl,
1340 .vidioc_s_ctrl = si470x_vidioc_s_ctrl,
1341 .vidioc_g_audio = si470x_vidioc_g_audio,
1342 .vidioc_s_audio = si470x_vidioc_s_audio,
1343 .vidioc_g_tuner = si470x_vidioc_g_tuner,
1344 .vidioc_s_tuner = si470x_vidioc_s_tuner,
1345 .vidioc_g_frequency = si470x_vidioc_g_frequency,
1346 .vidioc_s_frequency = si470x_vidioc_s_frequency,
1347 .owner = THIS_MODULE,
1348};
1349
1350
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001351
1352/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001353 * USB Interface
1354 **************************************************************************/
1355
1356/*
1357 * si470x_usb_driver_probe - probe for the device
1358 */
1359static int si470x_usb_driver_probe(struct usb_interface *intf,
1360 const struct usb_device_id *id)
1361{
1362 struct si470x_device *radio;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001363 int retval = -ENOMEM;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001364
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001365 /* private data allocation */
1366 radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001367 if (!radio)
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001368 goto err_initial;
1369
1370 /* video device allocation */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001371 radio->videodev = video_device_alloc();
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001372 if (!radio->videodev)
1373 goto err_radio;
1374
1375 /* initial configuration */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001376 memcpy(radio->videodev, &si470x_viddev_template,
1377 sizeof(si470x_viddev_template));
1378 radio->users = 0;
1379 radio->usbdev = interface_to_usbdev(intf);
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001380 mutex_init(&radio->lock);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001381 video_set_drvdata(radio->videodev, radio);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001382
1383 /* show some infos about the specific device */
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001384 retval = -EIO;
1385 if (si470x_get_all_registers(radio) < 0)
1386 goto err_all;
1387 printk(KERN_INFO DRIVER_NAME ": DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001388 radio->registers[DEVICEID], radio->registers[CHIPID]);
1389
1390 /* check if firmware is current */
1391 if ((radio->registers[CHIPID] & CHIPID_FIRMWARE)
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001392 < RADIO_SW_VERSION_CURRENT) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001393 printk(KERN_WARNING DRIVER_NAME
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001394 ": This driver is known to work with "
1395 "firmware version %hu,\n", RADIO_SW_VERSION_CURRENT);
1396 printk(KERN_WARNING DRIVER_NAME
1397 ": but the device has firmware version %hu.\n",
1398 radio->registers[CHIPID] & CHIPID_FIRMWARE);
1399 printk(KERN_WARNING DRIVER_NAME
1400 ": If you have some trouble using this driver,\n");
1401 printk(KERN_WARNING DRIVER_NAME
1402 ": please report to V4L ML at "
1403 "video4linux-list@redhat.com\n");
1404 }
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001405
1406 /* set initial frequency */
1407 si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
1408
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001409 /* rds buffer allocation */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001410 radio->buf_size = rds_buf * 3;
1411 radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001412 if (!radio->buffer)
1413 goto err_all;
1414
1415 /* rds buffer configuration */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001416 radio->wr_index = 0;
1417 radio->rd_index = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001418 init_waitqueue_head(&radio->read_queue);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001419
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001420 /* prepare rds work function */
1421 INIT_DELAYED_WORK(&radio->work, si470x_work);
1422
1423 /* register video device */
1424 if (video_register_device(radio->videodev, VFL_TYPE_RADIO, radio_nr)) {
1425 printk(KERN_WARNING DRIVER_NAME
1426 ": Could not register video device\n");
1427 goto err_all;
1428 }
1429 usb_set_intfdata(intf, radio);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001430
1431 return 0;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001432err_all:
1433 video_device_release(radio->videodev);
1434 kfree(radio->buffer);
1435err_radio:
1436 kfree(radio);
1437err_initial:
1438 return retval;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001439}
1440
1441
1442/*
1443 * si470x_usb_driver_disconnect - disconnect the device
1444 */
1445static void si470x_usb_driver_disconnect(struct usb_interface *intf)
1446{
1447 struct si470x_device *radio = usb_get_intfdata(intf);
1448
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001449 cancel_delayed_work_sync(&radio->work);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001450 usb_set_intfdata(intf, NULL);
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001451 video_unregister_device(radio->videodev);
1452 kfree(radio->buffer);
1453 kfree(radio);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001454}
1455
1456
1457/*
1458 * si470x_usb_driver - usb driver interface
1459 */
1460static struct usb_driver si470x_usb_driver = {
1461 .name = DRIVER_NAME,
1462 .probe = si470x_usb_driver_probe,
1463 .disconnect = si470x_usb_driver_disconnect,
1464 .id_table = si470x_usb_driver_id_table,
1465};
1466
1467
1468
1469/**************************************************************************
1470 * Module Interface
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001471 **************************************************************************/
1472
1473/*
1474 * si470x_module_init - module init
1475 */
1476static int __init si470x_module_init(void)
1477{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001478 printk(KERN_INFO DRIVER_DESC ", Version " DRIVER_VERSION "\n");
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001479 return usb_register(&si470x_usb_driver);
1480}
1481
1482
1483/*
1484 * si470x_module_exit - module exit
1485 */
1486static void __exit si470x_module_exit(void)
1487{
1488 usb_deregister(&si470x_usb_driver);
1489}
1490
1491
1492module_init(si470x_module_init);
1493module_exit(si470x_module_exit);
1494
1495MODULE_LICENSE("GPL");
1496MODULE_AUTHOR(DRIVER_AUTHOR);
1497MODULE_DESCRIPTION(DRIVER_DESC);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001498MODULE_VERSION(DRIVER_VERSION);