blob: 707988edc1b1e133eda85b8e6dc9440f202ec58a [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 Lorenz57566ad2008-02-09 16:08:24 -030084 * 2008-02-08 Tobias Lorenz <tobias.lorenz@gmx.net>
85 * Oliver Neukum <oliver@neukum.org>
86 * Version 1.0.7
87 * - usb autosuspend support
Tobias Lorenzce5829e2008-05-31 15:04:32 -030088 * - unplugging fixed
Tobias Lorenz78656ac2008-01-14 21:55:27 -030089 *
90 * ToDo:
Tobias Lorenz78656ac2008-01-14 21:55:27 -030091 * - add seeking support
92 * - add firmware download/update support
Tobias Lorenz78656ac2008-01-14 21:55:27 -030093 * - RDS support: interrupt mode, instead of polling
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -030094 * - add LED status output (check if that's not already done in firmware)
Tobias Lorenz78656ac2008-01-14 21:55:27 -030095 */
96
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -030097
Tobias Lorenz78656ac2008-01-14 21:55:27 -030098/* driver definitions */
99#define DRIVER_AUTHOR "Tobias Lorenz <tobias.lorenz@gmx.net>"
100#define DRIVER_NAME "radio-si470x"
Tobias Lorenz03dea862008-04-22 14:46:11 -0300101#define DRIVER_KERNEL_VERSION KERNEL_VERSION(1, 0, 7)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300102#define DRIVER_CARD "Silicon Labs Si470x FM Radio Receiver"
103#define DRIVER_DESC "USB radio driver for Si470x FM Radio Receivers"
Tobias Lorenz03dea862008-04-22 14:46:11 -0300104#define DRIVER_VERSION "1.0.7"
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300105
106
107/* kernel includes */
108#include <linux/kernel.h>
109#include <linux/module.h>
110#include <linux/init.h>
111#include <linux/slab.h>
112#include <linux/input.h>
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300113#include <linux/usb.h>
114#include <linux/hid.h>
115#include <linux/version.h>
Mauro Carvalho Chehab387d4472008-01-15 11:25:10 -0300116#include <linux/videodev2.h>
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300117#include <linux/mutex.h>
Mauro Carvalho Chehab387d4472008-01-15 11:25:10 -0300118#include <media/v4l2-common.h>
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300119#include <media/rds.h>
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300120#include <asm/unaligned.h>
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300121
122
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300123/* USB Device ID List */
124static struct usb_device_id si470x_usb_driver_id_table[] = {
125 /* Silicon Labs USB FM Radio Reference Design */
126 { USB_DEVICE_AND_INTERFACE_INFO(0x10c4, 0x818a, USB_CLASS_HID, 0, 0) },
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300127 /* ADS/Tech FM Radio Receiver (formerly Instant FM Music) */
128 { USB_DEVICE_AND_INTERFACE_INFO(0x06e1, 0xa155, USB_CLASS_HID, 0, 0) },
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300129 /* Terminating entry */
130 { }
131};
132MODULE_DEVICE_TABLE(usb, si470x_usb_driver_id_table);
133
134
135
136/**************************************************************************
137 * Module Parameters
138 **************************************************************************/
139
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300140/* Radio Nr */
141static int radio_nr = -1;
142module_param(radio_nr, int, 0);
143MODULE_PARM_DESC(radio_nr, "Radio Nr");
144
145/* Spacing (kHz) */
146/* 0: 200 kHz (USA, Australia) */
147/* 1: 100 kHz (Europe, Japan) */
148/* 2: 50 kHz */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300149static unsigned short space = 2;
150module_param(space, ushort, 0);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300151MODULE_PARM_DESC(radio_nr, "Spacing: 0=200kHz 1=100kHz *2=50kHz*");
152
153/* Bottom of Band (MHz) */
154/* 0: 87.5 - 108 MHz (USA, Europe)*/
155/* 1: 76 - 108 MHz (Japan wide band) */
156/* 2: 76 - 90 MHz (Japan) */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300157static unsigned short band = 1;
158module_param(band, ushort, 0);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300159MODULE_PARM_DESC(radio_nr, "Band: 0=87.5..108MHz *1=76..108MHz* 2=76..90MHz");
160
161/* De-emphasis */
162/* 0: 75 us (USA) */
163/* 1: 50 us (Europe, Australia, Japan) */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300164static unsigned short de = 1;
165module_param(de, ushort, 0);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300166MODULE_PARM_DESC(radio_nr, "De-emphasis: 0=75us *1=50us*");
167
168/* USB timeout */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300169static unsigned int usb_timeout = 500;
170module_param(usb_timeout, uint, 0);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300171MODULE_PARM_DESC(usb_timeout, "USB timeout (ms): *500*");
172
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300173/* Tune timeout */
174static unsigned int tune_timeout = 3000;
175module_param(tune_timeout, uint, 0);
176MODULE_PARM_DESC(tune_timeout, "Tune timeout: *3000*");
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300177
178/* RDS buffer blocks */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300179static unsigned int rds_buf = 100;
180module_param(rds_buf, uint, 0);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300181MODULE_PARM_DESC(rds_buf, "RDS buffer entries: *100*");
182
183/* RDS maximum block errors */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300184static unsigned short max_rds_errors = 1;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300185/* 0 means 0 errors requiring correction */
186/* 1 means 1-2 errors requiring correction (used by original USBRadio.exe) */
187/* 2 means 3-5 errors requiring correction */
188/* 3 means 6+ errors or errors in checkword, correction not possible */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300189module_param(max_rds_errors, ushort, 0);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300190MODULE_PARM_DESC(max_rds_errors, "RDS maximum block errors: *1*");
191
192/* RDS poll frequency */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300193static unsigned int rds_poll_time = 40;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300194/* 40 is used by the original USBRadio.exe */
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300195/* 50 is used by radio-cadet */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300196/* 75 should be okay */
197/* 80 is the usual RDS receive interval */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300198module_param(rds_poll_time, uint, 0);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300199MODULE_PARM_DESC(rds_poll_time, "RDS poll time (ms): *40*");
200
201
202
203/**************************************************************************
204 * Register Definitions
205 **************************************************************************/
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300206#define RADIO_REGISTER_SIZE 2 /* 16 register bit width */
207#define RADIO_REGISTER_NUM 16 /* DEVICEID ... RDSD */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300208#define RDS_REGISTER_NUM 6 /* STATUSRSSI ... RDSD */
209
210#define DEVICEID 0 /* Device ID */
211#define DEVICEID_PN 0xf000 /* bits 15..12: Part Number */
212#define DEVICEID_MFGID 0x0fff /* bits 11..00: Manufacturer ID */
213
214#define CHIPID 1 /* Chip ID */
215#define CHIPID_REV 0xfc00 /* bits 15..10: Chip Version */
216#define CHIPID_DEV 0x0200 /* bits 09..09: Device */
217#define CHIPID_FIRMWARE 0x01ff /* bits 08..00: Firmware Version */
218
219#define POWERCFG 2 /* Power Configuration */
220#define POWERCFG_DSMUTE 0x8000 /* bits 15..15: Softmute Disable */
221#define POWERCFG_DMUTE 0x4000 /* bits 14..14: Mute Disable */
222#define POWERCFG_MONO 0x2000 /* bits 13..13: Mono Select */
223#define POWERCFG_RDSM 0x0800 /* bits 11..11: RDS Mode (Si4701 only) */
224#define POWERCFG_SKMODE 0x0400 /* bits 10..10: Seek Mode */
225#define POWERCFG_SEEKUP 0x0200 /* bits 09..09: Seek Direction */
226#define POWERCFG_SEEK 0x0100 /* bits 08..08: Seek */
227#define POWERCFG_DISABLE 0x0040 /* bits 06..06: Powerup Disable */
228#define POWERCFG_ENABLE 0x0001 /* bits 00..00: Powerup Enable */
229
230#define CHANNEL 3 /* Channel */
231#define CHANNEL_TUNE 0x8000 /* bits 15..15: Tune */
232#define CHANNEL_CHAN 0x03ff /* bits 09..00: Channel Select */
233
234#define SYSCONFIG1 4 /* System Configuration 1 */
235#define SYSCONFIG1_RDSIEN 0x8000 /* bits 15..15: RDS Interrupt Enable (Si4701 only) */
236#define SYSCONFIG1_STCIEN 0x4000 /* bits 14..14: Seek/Tune Complete Interrupt Enable */
237#define SYSCONFIG1_RDS 0x1000 /* bits 12..12: RDS Enable (Si4701 only) */
238#define SYSCONFIG1_DE 0x0800 /* bits 11..11: De-emphasis (0=75us 1=50us) */
239#define SYSCONFIG1_AGCD 0x0400 /* bits 10..10: AGC Disable */
240#define SYSCONFIG1_BLNDADJ 0x00c0 /* bits 07..06: Stereo/Mono Blend Level Adjustment */
241#define SYSCONFIG1_GPIO3 0x0030 /* bits 05..04: General Purpose I/O 3 */
242#define SYSCONFIG1_GPIO2 0x000c /* bits 03..02: General Purpose I/O 2 */
243#define SYSCONFIG1_GPIO1 0x0003 /* bits 01..00: General Purpose I/O 1 */
244
245#define SYSCONFIG2 5 /* System Configuration 2 */
246#define SYSCONFIG2_SEEKTH 0xff00 /* bits 15..08: RSSI Seek Threshold */
247#define SYSCONFIG2_BAND 0x0080 /* bits 07..06: Band Select */
248#define SYSCONFIG2_SPACE 0x0030 /* bits 05..04: Channel Spacing */
249#define SYSCONFIG2_VOLUME 0x000f /* bits 03..00: Volume */
250
251#define SYSCONFIG3 6 /* System Configuration 3 */
252#define SYSCONFIG3_SMUTER 0xc000 /* bits 15..14: Softmute Attack/Recover Rate */
253#define SYSCONFIG3_SMUTEA 0x3000 /* bits 13..12: Softmute Attenuation */
254#define SYSCONFIG3_SKSNR 0x00f0 /* bits 07..04: Seek SNR Threshold */
255#define SYSCONFIG3_SKCNT 0x000f /* bits 03..00: Seek FM Impulse Detection Threshold */
256
257#define TEST1 7 /* Test 1 */
258#define TEST1_AHIZEN 0x4000 /* bits 14..14: Audio High-Z Enable */
259
260#define TEST2 8 /* Test 2 */
261/* TEST2 only contains reserved bits */
262
263#define BOOTCONFIG 9 /* Boot Configuration */
264/* BOOTCONFIG only contains reserved bits */
265
266#define STATUSRSSI 10 /* Status RSSI */
267#define STATUSRSSI_RDSR 0x8000 /* bits 15..15: RDS Ready (Si4701 only) */
268#define STATUSRSSI_STC 0x4000 /* bits 14..14: Seek/Tune Complete */
269#define STATUSRSSI_SF 0x2000 /* bits 13..13: Seek Fail/Band Limit */
270#define STATUSRSSI_AFCRL 0x1000 /* bits 12..12: AFC Rail */
271#define STATUSRSSI_RDSS 0x0800 /* bits 11..11: RDS Synchronized (Si4701 only) */
272#define STATUSRSSI_BLERA 0x0600 /* bits 10..09: RDS Block A Errors (Si4701 only) */
273#define STATUSRSSI_ST 0x0100 /* bits 08..08: Stereo Indicator */
274#define STATUSRSSI_RSSI 0x00ff /* bits 07..00: RSSI (Received Signal Strength Indicator) */
275
276#define READCHAN 11 /* Read Channel */
277#define READCHAN_BLERB 0xc000 /* bits 15..14: RDS Block D Errors (Si4701 only) */
278#define READCHAN_BLERC 0x3000 /* bits 13..12: RDS Block C Errors (Si4701 only) */
279#define READCHAN_BLERD 0x0c00 /* bits 11..10: RDS Block B Errors (Si4701 only) */
280#define READCHAN_READCHAN 0x03ff /* bits 09..00: Read Channel */
281
282#define RDSA 12 /* RDSA */
283#define RDSA_RDSA 0xffff /* bits 15..00: RDS Block A Data (Si4701 only) */
284
285#define RDSB 13 /* RDSB */
286#define RDSB_RDSB 0xffff /* bits 15..00: RDS Block B Data (Si4701 only) */
287
288#define RDSC 14 /* RDSC */
289#define RDSC_RDSC 0xffff /* bits 15..00: RDS Block C Data (Si4701 only) */
290
291#define RDSD 15 /* RDSD */
292#define RDSD_RDSD 0xffff /* bits 15..00: RDS Block D Data (Si4701 only) */
293
294
295
296/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300297 * USB HID Reports
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300298 **************************************************************************/
299
300/* Reports 1-16 give direct read/write access to the 16 Si470x registers */
301/* with the (REPORT_ID - 1) corresponding to the register address across USB */
302/* endpoint 0 using GET_REPORT and SET_REPORT */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300303#define REGISTER_REPORT_SIZE (RADIO_REGISTER_SIZE + 1)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300304#define REGISTER_REPORT(reg) ((reg) + 1)
305
306/* Report 17 gives direct read/write access to the entire Si470x register */
307/* map across endpoint 0 using GET_REPORT and SET_REPORT */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300308#define ENTIRE_REPORT_SIZE (RADIO_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300309#define ENTIRE_REPORT 17
310
311/* Report 18 is used to send the lowest 6 Si470x registers up the HID */
312/* interrupt endpoint 1 to Windows every 20 milliseconds for status */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300313#define RDS_REPORT_SIZE (RDS_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300314#define RDS_REPORT 18
315
316/* Report 19: LED state */
317#define LED_REPORT_SIZE 3
318#define LED_REPORT 19
319
320/* Report 19: stream */
321#define STREAM_REPORT_SIZE 3
322#define STREAM_REPORT 19
323
324/* Report 20: scratch */
325#define SCRATCH_PAGE_SIZE 63
326#define SCRATCH_REPORT_SIZE (SCRATCH_PAGE_SIZE + 1)
327#define SCRATCH_REPORT 20
328
329/* Reports 19-22: flash upgrade of the C8051F321 */
330#define WRITE_REPORT 19
331#define FLASH_REPORT 20
332#define CRC_REPORT 21
333#define RESPONSE_REPORT 22
334
335/* Report 23: currently unused, but can accept 60 byte reports on the HID */
336/* interrupt out endpoint 2 every 1 millisecond */
337#define UNUSED_REPORT 23
338
339
340
341/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300342 * Software/Hardware Versions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300343 **************************************************************************/
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300344#define RADIO_SW_VERSION_NOT_BOOTLOADABLE 6
345#define RADIO_SW_VERSION 7
346#define RADIO_SW_VERSION_CURRENT 15
347#define RADIO_HW_VERSION 1
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300348
349#define SCRATCH_PAGE_SW_VERSION 1
350#define SCRATCH_PAGE_HW_VERSION 2
351
352
353
354/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300355 * LED State Definitions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300356 **************************************************************************/
357#define LED_COMMAND 0x35
358
359#define NO_CHANGE_LED 0x00
360#define ALL_COLOR_LED 0x01 /* streaming state */
361#define BLINK_GREEN_LED 0x02 /* connect state */
362#define BLINK_RED_LED 0x04
363#define BLINK_ORANGE_LED 0x10 /* disconnect state */
364#define SOLID_GREEN_LED 0x20 /* tuning/seeking state */
365#define SOLID_RED_LED 0x40 /* bootload state */
366#define SOLID_ORANGE_LED 0x80
367
368
369
370/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300371 * Stream State Definitions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300372 **************************************************************************/
373#define STREAM_COMMAND 0x36
374#define STREAM_VIDPID 0x00
375#define STREAM_AUDIO 0xff
376
377
378
379/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300380 * Bootloader / Flash Commands
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300381 **************************************************************************/
382
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300383/* unique id sent to bootloader and required to put into a bootload state */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300384#define UNIQUE_BL_ID 0x34
385
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300386/* mask for the flash data */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300387#define FLASH_DATA_MASK 0x55
388
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300389/* bootloader commands */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300390#define GET_SW_VERSION_COMMAND 0x00
391#define SET_PAGE_COMMAND 0x01
392#define ERASE_PAGE_COMMAND 0x02
393#define WRITE_PAGE_COMMAND 0x03
394#define CRC_ON_PAGE_COMMAND 0x04
395#define READ_FLASH_BYTE_COMMAND 0x05
396#define RESET_DEVICE_COMMAND 0x06
397#define GET_HW_VERSION_COMMAND 0x07
398#define BLANK 0xff
399
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300400/* bootloader command responses */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300401#define COMMAND_OK 0x01
402#define COMMAND_FAILED 0x02
403#define COMMAND_PENDING 0x03
404
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300405/* buffer sizes */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300406#define COMMAND_BUFFER_SIZE 4
407#define RESPONSE_BUFFER_SIZE 2
408#define FLASH_BUFFER_SIZE 64
409#define CRC_BUFFER_SIZE 3
410
411
412
413/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300414 * General Driver Definitions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300415 **************************************************************************/
416
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300417/*
418 * si470x_device - private data
419 */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300420struct si470x_device {
421 /* reference to USB and video device */
422 struct usb_device *usbdev;
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300423 struct usb_interface *intf;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300424 struct video_device *videodev;
425
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300426 /* driver management */
427 unsigned int users;
Tobias Lorenzce5829e2008-05-31 15:04:32 -0300428 unsigned char disconnected;
429 struct mutex disconnect_lock;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300430
431 /* Silabs internal registers (0..15) */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300432 unsigned short registers[RADIO_REGISTER_NUM];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300433
434 /* RDS receive buffer */
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300435 struct delayed_work work;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300436 wait_queue_head_t read_queue;
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300437 struct mutex lock; /* buffer locking */
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300438 unsigned char *buffer; /* size is always multiple of three */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300439 unsigned int buf_size;
440 unsigned int rd_index;
441 unsigned int wr_index;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300442};
443
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300444
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300445/*
446 * The frequency is set in units of 62.5 Hz when using V4L2_TUNER_CAP_LOW,
447 * 62.5 kHz otherwise.
448 * The tuner is able to have a channel spacing of 50, 100 or 200 kHz.
449 * tuner->capability is therefore set to V4L2_TUNER_CAP_LOW
450 * The FREQ_MUL is then: 1 MHz / 62.5 Hz = 16000
451 */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300452#define FREQ_MUL (1000000 / 62.5)
453
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300454
455
456/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300457 * General Driver Functions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300458 **************************************************************************/
459
460/*
461 * si470x_get_report - receive a HID report
462 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300463static int si470x_get_report(struct si470x_device *radio, void *buf, int size)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300464{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300465 unsigned char *report = (unsigned char *) buf;
466 int retval;
467
468 retval = usb_control_msg(radio->usbdev,
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300469 usb_rcvctrlpipe(radio->usbdev, 0),
470 HID_REQ_GET_REPORT,
471 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300472 report[0], 2,
473 buf, size, usb_timeout);
474 if (retval < 0)
475 printk(KERN_WARNING DRIVER_NAME
476 ": si470x_get_report: usb_control_msg returned %d\n",
477 retval);
478
479 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300480}
481
482
483/*
484 * si470x_set_report - send a HID report
485 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300486static int si470x_set_report(struct si470x_device *radio, void *buf, int size)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300487{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300488 unsigned char *report = (unsigned char *) buf;
489 int retval;
490
491 retval = usb_control_msg(radio->usbdev,
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300492 usb_sndctrlpipe(radio->usbdev, 0),
493 HID_REQ_SET_REPORT,
494 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300495 report[0], 2,
496 buf, size, usb_timeout);
497 if (retval < 0)
498 printk(KERN_WARNING DRIVER_NAME
499 ": si470x_set_report: usb_control_msg returned %d\n",
500 retval);
501
502 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300503}
504
505
506/*
507 * si470x_get_register - read register
508 */
509static int si470x_get_register(struct si470x_device *radio, int regnr)
510{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300511 unsigned char buf[REGISTER_REPORT_SIZE];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300512 int retval;
513
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300514 buf[0] = REGISTER_REPORT(regnr);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300515
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300516 retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
517
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300518 if (retval >= 0)
Harvey Harrison5ad6b812008-04-15 22:22:11 -0300519 radio->registers[regnr] = get_unaligned_be16(&buf[1]);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300520
521 return (retval < 0) ? -EINVAL : 0;
522}
523
524
525/*
526 * si470x_set_register - write register
527 */
528static int si470x_set_register(struct si470x_device *radio, int regnr)
529{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300530 unsigned char buf[REGISTER_REPORT_SIZE];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300531 int retval;
532
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300533 buf[0] = REGISTER_REPORT(regnr);
Harvey Harrison5ad6b812008-04-15 22:22:11 -0300534 put_unaligned_be16(radio->registers[regnr], &buf[1]);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300535
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300536 retval = si470x_set_report(radio, (void *) &buf, sizeof(buf));
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300537
538 return (retval < 0) ? -EINVAL : 0;
539}
540
541
542/*
543 * si470x_get_all_registers - read entire registers
544 */
545static int si470x_get_all_registers(struct si470x_device *radio)
546{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300547 unsigned char buf[ENTIRE_REPORT_SIZE];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300548 int retval;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300549 unsigned char regnr;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300550
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300551 buf[0] = ENTIRE_REPORT;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300552
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300553 retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300554
555 if (retval >= 0)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300556 for (regnr = 0; regnr < RADIO_REGISTER_NUM; regnr++)
Harvey Harrison5ad6b812008-04-15 22:22:11 -0300557 radio->registers[regnr] = get_unaligned_be16(
558 &buf[regnr * RADIO_REGISTER_SIZE + 1]);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300559
560 return (retval < 0) ? -EINVAL : 0;
561}
562
563
564/*
565 * si470x_get_rds_registers - read rds registers
566 */
567static int si470x_get_rds_registers(struct si470x_device *radio)
568{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300569 unsigned char buf[RDS_REPORT_SIZE];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300570 int retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300571 int size;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300572 unsigned char regnr;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300573
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300574 buf[0] = RDS_REPORT;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300575
576 retval = usb_interrupt_msg(radio->usbdev,
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300577 usb_rcvintpipe(radio->usbdev, 1),
578 (void *) &buf, sizeof(buf), &size, usb_timeout);
579 if (size != sizeof(buf))
Tobias Lorenzce5829e2008-05-31 15:04:32 -0300580 printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: "
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300581 "return size differs: %d != %zu\n", size, sizeof(buf));
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300582 if (retval < 0)
583 printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: "
584 "usb_interrupt_msg returned %d\n", retval);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300585
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300586 if (retval >= 0)
587 for (regnr = 0; regnr < RDS_REGISTER_NUM; regnr++)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300588 radio->registers[STATUSRSSI + regnr] =
Tobias Lorenzce5829e2008-05-31 15:04:32 -0300589 get_unaligned_be16(
590 &buf[regnr * RADIO_REGISTER_SIZE + 1]);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300591
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300592 return (retval < 0) ? -EINVAL : 0;
593}
594
595
596/*
597 * si470x_set_chan - set the channel
598 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300599static int si470x_set_chan(struct si470x_device *radio, unsigned short chan)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300600{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300601 int retval;
602 unsigned long timeout;
603 bool timed_out = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300604
605 /* start tuning */
606 radio->registers[CHANNEL] &= ~CHANNEL_CHAN;
607 radio->registers[CHANNEL] |= CHANNEL_TUNE | chan;
608 retval = si470x_set_register(radio, CHANNEL);
609 if (retval < 0)
610 return retval;
611
612 /* wait till seek operation has completed */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300613 timeout = jiffies + msecs_to_jiffies(tune_timeout);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300614 do {
615 retval = si470x_get_register(radio, STATUSRSSI);
616 if (retval < 0)
617 return retval;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300618 timed_out = time_after(jiffies, timeout);
619 } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
620 (!timed_out));
621 if (timed_out)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300622 printk(KERN_WARNING DRIVER_NAME
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300623 ": seek does not finish after %u ms\n", tune_timeout);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300624
625 /* stop tuning */
626 radio->registers[CHANNEL] &= ~CHANNEL_TUNE;
627 return si470x_set_register(radio, CHANNEL);
628}
629
630
631/*
632 * si470x_get_freq - get the frequency
633 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300634static unsigned int si470x_get_freq(struct si470x_device *radio)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300635{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300636 unsigned int spacing, band_bottom, freq;
637 unsigned short chan;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300638 int retval;
639
640 /* Spacing (kHz) */
641 switch (space) {
642 /* 0: 200 kHz (USA, Australia) */
643 case 0 : spacing = 0.200 * FREQ_MUL; break;
644 /* 1: 100 kHz (Europe, Japan) */
645 case 1 : spacing = 0.100 * FREQ_MUL; break;
646 /* 2: 50 kHz */
647 default: spacing = 0.050 * FREQ_MUL; break;
648 };
649
650 /* Bottom of Band (MHz) */
651 switch (band) {
652 /* 0: 87.5 - 108 MHz (USA, Europe) */
653 case 0 : band_bottom = 87.5 * FREQ_MUL; break;
654 /* 1: 76 - 108 MHz (Japan wide band) */
655 default: band_bottom = 76 * FREQ_MUL; break;
656 /* 2: 76 - 90 MHz (Japan) */
657 case 2 : band_bottom = 76 * FREQ_MUL; break;
658 };
659
660 /* read channel */
661 retval = si470x_get_register(radio, READCHAN);
662 if (retval < 0)
663 return retval;
664 chan = radio->registers[READCHAN] & READCHAN_READCHAN;
665
666 /* Frequency (MHz) = Spacing (kHz) x Channel + Bottom of Band (MHz) */
667 freq = chan * spacing + band_bottom;
668
669 return freq;
670}
671
672
673/*
674 * si470x_set_freq - set the frequency
675 */
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300676static int si470x_set_freq(struct si470x_device *radio, unsigned int freq)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300677{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300678 unsigned int spacing, band_bottom;
679 unsigned short chan;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300680
681 /* Spacing (kHz) */
682 switch (space) {
683 /* 0: 200 kHz (USA, Australia) */
684 case 0 : spacing = 0.200 * FREQ_MUL; break;
685 /* 1: 100 kHz (Europe, Japan) */
686 case 1 : spacing = 0.100 * FREQ_MUL; break;
687 /* 2: 50 kHz */
688 default: spacing = 0.050 * FREQ_MUL; break;
689 };
690
691 /* Bottom of Band (MHz) */
692 switch (band) {
693 /* 0: 87.5 - 108 MHz (USA, Europe) */
694 case 0 : band_bottom = 87.5 * FREQ_MUL; break;
695 /* 1: 76 - 108 MHz (Japan wide band) */
696 default: band_bottom = 76 * FREQ_MUL; break;
697 /* 2: 76 - 90 MHz (Japan) */
698 case 2 : band_bottom = 76 * FREQ_MUL; break;
699 };
700
701 /* Chan = [ Freq (Mhz) - Bottom of Band (MHz) ] / Spacing (kHz) */
702 chan = (freq - band_bottom) / spacing;
703
704 return si470x_set_chan(radio, chan);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300705}
706
707
708/*
709 * si470x_start - switch on radio
710 */
711static int si470x_start(struct si470x_device *radio)
712{
713 int retval;
714
715 /* powercfg */
716 radio->registers[POWERCFG] =
717 POWERCFG_DMUTE | POWERCFG_ENABLE | POWERCFG_RDSM;
718 retval = si470x_set_register(radio, POWERCFG);
719 if (retval < 0)
720 return retval;
721
722 /* sysconfig 1 */
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300723 radio->registers[SYSCONFIG1] = SYSCONFIG1_DE;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300724 retval = si470x_set_register(radio, SYSCONFIG1);
725 if (retval < 0)
726 return retval;
727
728 /* sysconfig 2 */
729 radio->registers[SYSCONFIG2] =
730 (0x3f << 8) | /* SEEKTH */
731 (band << 6) | /* BAND */
732 (space << 4) | /* SPACE */
733 15; /* VOLUME (max) */
734 retval = si470x_set_register(radio, SYSCONFIG2);
735 if (retval < 0)
736 return retval;
737
738 /* reset last channel */
739 return si470x_set_chan(radio,
740 radio->registers[CHANNEL] & CHANNEL_CHAN);
741}
742
743
744/*
745 * si470x_stop - switch off radio
746 */
747static int si470x_stop(struct si470x_device *radio)
748{
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300749 int retval;
750
751 /* sysconfig 1 */
752 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
753 retval = si470x_set_register(radio, SYSCONFIG1);
754 if (retval < 0)
755 return retval;
756
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300757 /* powercfg */
758 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
759 /* POWERCFG_ENABLE has to automatically go low */
760 radio->registers[POWERCFG] |= POWERCFG_ENABLE | POWERCFG_DISABLE;
761 return si470x_set_register(radio, POWERCFG);
762}
763
764
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300765/*
766 * si470x_rds_on - switch on rds reception
767 */
768static int si470x_rds_on(struct si470x_device *radio)
769{
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300770 int retval;
771
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300772 /* sysconfig 1 */
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300773 mutex_lock(&radio->lock);
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300774 radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDS;
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300775 retval = si470x_set_register(radio, SYSCONFIG1);
776 if (retval < 0)
777 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
778 mutex_unlock(&radio->lock);
779
780 return retval;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300781}
782
783
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300784
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300785/**************************************************************************
786 * RDS Driver Functions
787 **************************************************************************/
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300788
789/*
790 * si470x_rds - rds processing function
791 */
792static void si470x_rds(struct si470x_device *radio)
793{
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300794 unsigned char blocknum;
795 unsigned short bler; /* rds block errors */
796 unsigned short rds;
797 unsigned char tmpbuf[3];
798
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300799 /* get rds blocks */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300800 if (si470x_get_rds_registers(radio) < 0)
801 return;
802 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0) {
803 /* No RDS group ready */
804 return;
805 }
806 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSS) == 0) {
807 /* RDS decoder not synchronized */
808 return;
809 }
810
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300811 /* copy all four RDS blocks to internal buffer */
812 mutex_lock(&radio->lock);
813 for (blocknum = 0; blocknum < 4; blocknum++) {
814 switch (blocknum) {
815 default:
816 bler = (radio->registers[STATUSRSSI] &
817 STATUSRSSI_BLERA) >> 9;
818 rds = radio->registers[RDSA];
819 break;
820 case 1:
821 bler = (radio->registers[READCHAN] &
822 READCHAN_BLERB) >> 14;
823 rds = radio->registers[RDSB];
824 break;
825 case 2:
826 bler = (radio->registers[READCHAN] &
827 READCHAN_BLERC) >> 12;
828 rds = radio->registers[RDSC];
829 break;
830 case 3:
831 bler = (radio->registers[READCHAN] &
832 READCHAN_BLERD) >> 10;
833 rds = radio->registers[RDSD];
834 break;
835 };
Tobias Lorenz0e3301e2008-01-27 14:54:07 -0300836
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300837 /* Fill the V4L2 RDS buffer */
Harvey Harrison5ad6b812008-04-15 22:22:11 -0300838 put_unaligned_le16(rds, &tmpbuf);
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300839 tmpbuf[2] = blocknum; /* offset name */
840 tmpbuf[2] |= blocknum << 3; /* received offset */
841 if (bler > max_rds_errors)
842 tmpbuf[2] |= 0x80; /* uncorrectable errors */
843 else if (bler > 0)
844 tmpbuf[2] |= 0x40; /* corrected error(s) */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300845
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300846 /* copy RDS block to internal buffer */
847 memcpy(&radio->buffer[radio->wr_index], &tmpbuf, 3);
848 radio->wr_index += 3;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300849
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300850 /* wrap write pointer */
851 if (radio->wr_index >= radio->buf_size)
852 radio->wr_index = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300853
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300854 /* check for overflow */
855 if (radio->wr_index == radio->rd_index) {
856 /* increment and wrap read pointer */
857 radio->rd_index += 3;
858 if (radio->rd_index >= radio->buf_size)
859 radio->rd_index = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300860 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300861 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300862 mutex_unlock(&radio->lock);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300863
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300864 /* wake up read queue */
865 if (radio->wr_index != radio->rd_index)
866 wake_up_interruptible(&radio->read_queue);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300867}
868
869
870/*
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300871 * si470x_work - rds work function
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300872 */
873static void si470x_work(struct work_struct *work)
874{
875 struct si470x_device *radio = container_of(work, struct si470x_device,
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300876 work.work);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300877
Tobias Lorenzce5829e2008-05-31 15:04:32 -0300878 if (radio->disconnected)
879 return;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300880 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300881 return;
882
883 si470x_rds(radio);
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300884 schedule_delayed_work(&radio->work, msecs_to_jiffies(rds_poll_time));
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300885}
886
887
888
889/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300890 * File Operations Interface
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300891 **************************************************************************/
892
893/*
894 * si470x_fops_read - read RDS data
895 */
896static ssize_t si470x_fops_read(struct file *file, char __user *buf,
897 size_t count, loff_t *ppos)
898{
899 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300900 int retval = 0;
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300901 unsigned int block_count = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300902
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300903 /* switch on rds reception */
904 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
905 si470x_rds_on(radio);
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300906 schedule_delayed_work(&radio->work,
907 msecs_to_jiffies(rds_poll_time));
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300908 }
909
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300910 /* block if no new data available */
911 while (radio->wr_index == radio->rd_index) {
912 if (file->f_flags & O_NONBLOCK)
913 return -EWOULDBLOCK;
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300914 if (wait_event_interruptible(radio->read_queue,
915 radio->wr_index != radio->rd_index) < 0)
916 return -EINTR;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300917 }
918
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300919 /* calculate block count from byte count */
920 count /= 3;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300921
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300922 /* copy RDS block out of internal buffer and to user buffer */
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300923 mutex_lock(&radio->lock);
924 while (block_count < count) {
925 if (radio->rd_index == radio->wr_index)
926 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300927
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300928 /* always transfer rds complete blocks */
929 if (copy_to_user(buf, &radio->buffer[radio->rd_index], 3))
930 /* retval = -EFAULT; */
931 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300932
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300933 /* increment and wrap read pointer */
934 radio->rd_index += 3;
935 if (radio->rd_index >= radio->buf_size)
936 radio->rd_index = 0;
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300937
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300938 /* increment counters */
939 block_count++;
940 buf += 3;
941 retval += 3;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300942 }
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300943 mutex_unlock(&radio->lock);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300944
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300945 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300946}
947
948
949/*
950 * si470x_fops_poll - poll RDS data
951 */
952static unsigned int si470x_fops_poll(struct file *file,
953 struct poll_table_struct *pts)
954{
955 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300956
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300957 /* switch on rds reception */
958 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
959 si470x_rds_on(radio);
Tobias Lorenz5caf5132008-02-04 22:26:08 -0300960 schedule_delayed_work(&radio->work,
961 msecs_to_jiffies(rds_poll_time));
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300962 }
963
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300964 poll_wait(file, &radio->read_queue, pts);
965
Tobias Lorenz2fb88402008-01-25 05:14:57 -0300966 if (radio->rd_index != radio->wr_index)
967 return POLLIN | POLLRDNORM;
968
969 return 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300970}
971
972
973/*
974 * si470x_fops_open - file open
975 */
976static int si470x_fops_open(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));
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300979 int retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300980
981 radio->users++;
Tobias Lorenz57566ad2008-02-09 16:08:24 -0300982
983 retval = usb_autopm_get_interface(radio->intf);
984 if (retval < 0) {
985 radio->users--;
986 return -EIO;
987 }
988
989 if (radio->users == 1) {
990 retval = si470x_start(radio);
991 if (retval < 0)
992 usb_autopm_put_interface(radio->intf);
993 return retval;
994 }
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300995
996 return 0;
997}
998
999
1000/*
1001 * si470x_fops_release - file release
1002 */
1003static int si470x_fops_release(struct inode *inode, struct file *file)
1004{
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001005 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001006 int retval = 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001007
1008 if (!radio)
1009 return -ENODEV;
1010
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001011 mutex_lock(&radio->disconnect_lock);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001012 radio->users--;
1013 if (radio->users == 0) {
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001014 if (radio->disconnected) {
1015 video_unregister_device(radio->videodev);
1016 kfree(radio->buffer);
1017 kfree(radio);
1018 goto unlock;
1019 }
Tobias Lorenz03dea862008-04-22 14:46:11 -03001020
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001021 /* stop rds reception */
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001022 cancel_delayed_work_sync(&radio->work);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001023
Tobias Lorenz2fb88402008-01-25 05:14:57 -03001024 /* cancel read processes */
1025 wake_up_interruptible(&radio->read_queue);
1026
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001027 retval = si470x_stop(radio);
1028 usb_autopm_put_interface(radio->intf);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001029 }
1030
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001031unlock:
1032 mutex_unlock(&radio->disconnect_lock);
1033 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001034}
1035
1036
1037/*
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001038 * si470x_fops - file operations interface
1039 */
1040static const struct file_operations si470x_fops = {
1041 .owner = THIS_MODULE,
1042 .llseek = no_llseek,
1043 .read = si470x_fops_read,
1044 .poll = si470x_fops_poll,
1045 .ioctl = video_ioctl2,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -03001046#ifdef CONFIG_COMPAT
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001047 .compat_ioctl = v4l_compat_ioctl32,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -03001048#endif
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001049 .open = si470x_fops_open,
1050 .release = si470x_fops_release,
1051};
1052
1053
1054
1055/**************************************************************************
1056 * Video4Linux Interface
1057 **************************************************************************/
1058
1059/*
1060 * si470x_v4l2_queryctrl - query control
1061 */
1062static struct v4l2_queryctrl si470x_v4l2_queryctrl[] = {
1063/* HINT: the disabled controls are only here to satify kradio and such apps */
1064 {
1065 .id = V4L2_CID_AUDIO_VOLUME,
1066 .type = V4L2_CTRL_TYPE_INTEGER,
1067 .name = "Volume",
1068 .minimum = 0,
1069 .maximum = 15,
1070 .step = 1,
1071 .default_value = 15,
1072 },
1073 {
1074 .id = V4L2_CID_AUDIO_BALANCE,
1075 .flags = V4L2_CTRL_FLAG_DISABLED,
1076 },
1077 {
1078 .id = V4L2_CID_AUDIO_BASS,
1079 .flags = V4L2_CTRL_FLAG_DISABLED,
1080 },
1081 {
1082 .id = V4L2_CID_AUDIO_TREBLE,
1083 .flags = V4L2_CTRL_FLAG_DISABLED,
1084 },
1085 {
1086 .id = V4L2_CID_AUDIO_MUTE,
1087 .type = V4L2_CTRL_TYPE_BOOLEAN,
1088 .name = "Mute",
1089 .minimum = 0,
1090 .maximum = 1,
1091 .step = 1,
1092 .default_value = 1,
1093 },
1094 {
1095 .id = V4L2_CID_AUDIO_LOUDNESS,
1096 .flags = V4L2_CTRL_FLAG_DISABLED,
1097 },
1098};
1099
1100
1101/*
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001102 * si470x_vidioc_querycap - query device capabilities
1103 */
1104static int si470x_vidioc_querycap(struct file *file, void *priv,
1105 struct v4l2_capability *capability)
1106{
1107 strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver));
1108 strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
1109 sprintf(capability->bus_info, "USB");
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001110 capability->version = DRIVER_KERNEL_VERSION;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001111 capability->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
1112
1113 return 0;
1114}
1115
1116
1117/*
1118 * si470x_vidioc_g_input - get input
1119 */
1120static int si470x_vidioc_g_input(struct file *filp, void *priv,
1121 unsigned int *i)
1122{
1123 *i = 0;
1124
1125 return 0;
1126}
1127
1128
1129/*
1130 * si470x_vidioc_s_input - set input
1131 */
1132static int si470x_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
1133{
1134 if (i != 0)
1135 return -EINVAL;
1136
1137 return 0;
1138}
1139
1140
1141/*
1142 * si470x_vidioc_queryctrl - enumerate control items
1143 */
1144static int si470x_vidioc_queryctrl(struct file *file, void *priv,
1145 struct v4l2_queryctrl *qc)
1146{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001147 unsigned char i;
1148 int retval = -EINVAL;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001149
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001150 for (i = 0; i < ARRAY_SIZE(si470x_v4l2_queryctrl); i++) {
1151 if (qc->id && qc->id == si470x_v4l2_queryctrl[i].id) {
1152 memcpy(qc, &(si470x_v4l2_queryctrl[i]), sizeof(*qc));
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001153 retval = 0;
1154 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001155 }
1156 }
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001157 if (retval < 0)
1158 printk(KERN_WARNING DRIVER_NAME
1159 ": query control failed with %d\n", retval);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001160
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001161 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001162}
1163
1164
1165/*
1166 * si470x_vidioc_g_ctrl - get the value of a control
1167 */
1168static int si470x_vidioc_g_ctrl(struct file *file, void *priv,
1169 struct v4l2_control *ctrl)
1170{
1171 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1172
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001173 if (radio->disconnected)
1174 return -EIO;
Tobias Lorenz03dea862008-04-22 14:46:11 -03001175
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001176 switch (ctrl->id) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001177 case V4L2_CID_AUDIO_VOLUME:
1178 ctrl->value = radio->registers[SYSCONFIG2] &
1179 SYSCONFIG2_VOLUME;
1180 break;
1181 case V4L2_CID_AUDIO_MUTE:
1182 ctrl->value = ((radio->registers[POWERCFG] &
1183 POWERCFG_DMUTE) == 0) ? 1 : 0;
1184 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001185 }
1186
1187 return 0;
1188}
1189
1190
1191/*
1192 * si470x_vidioc_s_ctrl - set the value of a control
1193 */
1194static int si470x_vidioc_s_ctrl(struct file *file, void *priv,
1195 struct v4l2_control *ctrl)
1196{
1197 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001198 int retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001199
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001200 if (radio->disconnected)
1201 return -EIO;
Tobias Lorenz03dea862008-04-22 14:46:11 -03001202
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001203 switch (ctrl->id) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001204 case V4L2_CID_AUDIO_VOLUME:
1205 radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_VOLUME;
1206 radio->registers[SYSCONFIG2] |= ctrl->value;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001207 retval = si470x_set_register(radio, SYSCONFIG2);
1208 break;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001209 case V4L2_CID_AUDIO_MUTE:
1210 if (ctrl->value == 1)
1211 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
1212 else
1213 radio->registers[POWERCFG] |= POWERCFG_DMUTE;
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001214 retval = si470x_set_register(radio, POWERCFG);
1215 break;
1216 default:
1217 retval = -EINVAL;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001218 }
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001219 if (retval < 0)
1220 printk(KERN_WARNING DRIVER_NAME
1221 ": set control failed with %d\n", retval);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001222
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001223 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001224}
1225
1226
1227/*
1228 * si470x_vidioc_g_audio - get audio attributes
1229 */
1230static int si470x_vidioc_g_audio(struct file *file, void *priv,
1231 struct v4l2_audio *audio)
1232{
1233 if (audio->index > 1)
1234 return -EINVAL;
1235
1236 strcpy(audio->name, "Radio");
1237 audio->capability = V4L2_AUDCAP_STEREO;
1238
1239 return 0;
1240}
1241
1242
1243/*
1244 * si470x_vidioc_s_audio - set audio attributes
1245 */
1246static int si470x_vidioc_s_audio(struct file *file, void *priv,
1247 struct v4l2_audio *audio)
1248{
1249 if (audio->index != 0)
1250 return -EINVAL;
1251
1252 return 0;
1253}
1254
1255
1256/*
1257 * si470x_vidioc_g_tuner - get tuner attributes
1258 */
1259static int si470x_vidioc_g_tuner(struct file *file, void *priv,
1260 struct v4l2_tuner *tuner)
1261{
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001262 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001263 int retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001264
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001265 if (radio->disconnected)
1266 return -EIO;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001267 if (tuner->index > 0)
1268 return -EINVAL;
1269
1270 /* read status rssi */
1271 retval = si470x_get_register(radio, STATUSRSSI);
1272 if (retval < 0)
1273 return retval;
1274
1275 strcpy(tuner->name, "FM");
1276 tuner->type = V4L2_TUNER_RADIO;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001277 switch (band) {
1278 /* 0: 87.5 - 108 MHz (USA, Europe, default) */
1279 default:
1280 tuner->rangelow = 87.5 * FREQ_MUL;
1281 tuner->rangehigh = 108 * FREQ_MUL;
1282 break;
1283 /* 1: 76 - 108 MHz (Japan wide band) */
1284 case 1 :
1285 tuner->rangelow = 76 * FREQ_MUL;
1286 tuner->rangehigh = 108 * FREQ_MUL;
1287 break;
1288 /* 2: 76 - 90 MHz (Japan) */
1289 case 2 :
1290 tuner->rangelow = 76 * FREQ_MUL;
1291 tuner->rangehigh = 90 * FREQ_MUL;
1292 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001293 };
1294 tuner->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
1295 tuner->capability = V4L2_TUNER_CAP_LOW;
1296
1297 /* Stereo indicator == Stereo (instead of Mono) */
1298 if ((radio->registers[STATUSRSSI] & STATUSRSSI_ST) == 1)
1299 tuner->audmode = V4L2_TUNER_MODE_STEREO;
1300 else
1301 tuner->audmode = V4L2_TUNER_MODE_MONO;
1302
1303 /* min is worst, max is best; signal:0..0xffff; rssi: 0..0xff */
1304 tuner->signal = (radio->registers[STATUSRSSI] & STATUSRSSI_RSSI)
1305 * 0x0101;
1306
1307 /* automatic frequency control: -1: freq to low, 1 freq to high */
1308 tuner->afc = 0;
1309
1310 return 0;
1311}
1312
1313
1314/*
1315 * si470x_vidioc_s_tuner - set tuner attributes
1316 */
1317static int si470x_vidioc_s_tuner(struct file *file, void *priv,
1318 struct v4l2_tuner *tuner)
1319{
1320 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001321 int retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001322
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001323 if (radio->disconnected)
1324 return -EIO;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001325 if (tuner->index > 0)
1326 return -EINVAL;
1327
1328 if (tuner->audmode == V4L2_TUNER_MODE_MONO)
1329 radio->registers[POWERCFG] |= POWERCFG_MONO; /* force mono */
1330 else
1331 radio->registers[POWERCFG] &= ~POWERCFG_MONO; /* try stereo */
1332
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001333 retval = si470x_set_register(radio, POWERCFG);
1334 if (retval < 0)
1335 printk(KERN_WARNING DRIVER_NAME
1336 ": set tuner failed with %d\n", retval);
1337
1338 return retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001339}
1340
1341
1342/*
1343 * si470x_vidioc_g_frequency - get tuner or modulator radio frequency
1344 */
1345static int si470x_vidioc_g_frequency(struct file *file, void *priv,
1346 struct v4l2_frequency *freq)
1347{
1348 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1349
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001350 if (radio->disconnected)
1351 return -EIO;
Tobias Lorenz03dea862008-04-22 14:46:11 -03001352
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001353 freq->type = V4L2_TUNER_RADIO;
1354 freq->frequency = si470x_get_freq(radio);
1355
1356 return 0;
1357}
1358
1359
1360/*
1361 * si470x_vidioc_s_frequency - set tuner or modulator radio frequency
1362 */
1363static int si470x_vidioc_s_frequency(struct file *file, void *priv,
1364 struct v4l2_frequency *freq)
1365{
1366 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001367 int retval;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001368
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001369 if (radio->disconnected)
1370 return -EIO;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001371 if (freq->type != V4L2_TUNER_RADIO)
1372 return -EINVAL;
1373
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001374 retval = si470x_set_freq(radio, freq->frequency);
1375 if (retval < 0)
1376 printk(KERN_WARNING DRIVER_NAME
1377 ": set frequency failed with %d\n", retval);
1378
1379 return 0;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001380}
1381
1382
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001383/*
1384 * si470x_viddev_tamples - video device interface
1385 */
1386static struct video_device si470x_viddev_template = {
1387 .fops = &si470x_fops,
1388 .name = DRIVER_NAME,
1389 .type = VID_TYPE_TUNER,
1390 .release = video_device_release,
1391 .vidioc_querycap = si470x_vidioc_querycap,
1392 .vidioc_g_input = si470x_vidioc_g_input,
1393 .vidioc_s_input = si470x_vidioc_s_input,
1394 .vidioc_queryctrl = si470x_vidioc_queryctrl,
1395 .vidioc_g_ctrl = si470x_vidioc_g_ctrl,
1396 .vidioc_s_ctrl = si470x_vidioc_s_ctrl,
1397 .vidioc_g_audio = si470x_vidioc_g_audio,
1398 .vidioc_s_audio = si470x_vidioc_s_audio,
1399 .vidioc_g_tuner = si470x_vidioc_g_tuner,
1400 .vidioc_s_tuner = si470x_vidioc_s_tuner,
1401 .vidioc_g_frequency = si470x_vidioc_g_frequency,
1402 .vidioc_s_frequency = si470x_vidioc_s_frequency,
1403 .owner = THIS_MODULE,
1404};
1405
1406
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001407
1408/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001409 * USB Interface
1410 **************************************************************************/
1411
1412/*
1413 * si470x_usb_driver_probe - probe for the device
1414 */
1415static int si470x_usb_driver_probe(struct usb_interface *intf,
1416 const struct usb_device_id *id)
1417{
1418 struct si470x_device *radio;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001419 int retval = -ENOMEM;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001420
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001421 /* private data allocation */
1422 radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001423 if (!radio)
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001424 goto err_initial;
1425
1426 /* video device allocation */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001427 radio->videodev = video_device_alloc();
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001428 if (!radio->videodev)
1429 goto err_radio;
1430
1431 /* initial configuration */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001432 memcpy(radio->videodev, &si470x_viddev_template,
1433 sizeof(si470x_viddev_template));
1434 radio->users = 0;
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001435 radio->disconnected = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001436 radio->usbdev = interface_to_usbdev(intf);
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001437 radio->intf = intf;
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001438 mutex_init(&radio->disconnect_lock);
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001439 mutex_init(&radio->lock);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001440 video_set_drvdata(radio->videodev, radio);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001441
1442 /* show some infos about the specific device */
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001443 retval = -EIO;
1444 if (si470x_get_all_registers(radio) < 0)
1445 goto err_all;
1446 printk(KERN_INFO DRIVER_NAME ": DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001447 radio->registers[DEVICEID], radio->registers[CHIPID]);
1448
1449 /* check if firmware is current */
1450 if ((radio->registers[CHIPID] & CHIPID_FIRMWARE)
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001451 < RADIO_SW_VERSION_CURRENT) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001452 printk(KERN_WARNING DRIVER_NAME
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001453 ": This driver is known to work with "
1454 "firmware version %hu,\n", RADIO_SW_VERSION_CURRENT);
1455 printk(KERN_WARNING DRIVER_NAME
1456 ": but the device has firmware version %hu.\n",
1457 radio->registers[CHIPID] & CHIPID_FIRMWARE);
1458 printk(KERN_WARNING DRIVER_NAME
1459 ": If you have some trouble using this driver,\n");
1460 printk(KERN_WARNING DRIVER_NAME
1461 ": please report to V4L ML at "
1462 "video4linux-list@redhat.com\n");
1463 }
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001464
1465 /* set initial frequency */
1466 si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
1467
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001468 /* rds buffer allocation */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001469 radio->buf_size = rds_buf * 3;
1470 radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001471 if (!radio->buffer)
1472 goto err_all;
1473
1474 /* rds buffer configuration */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001475 radio->wr_index = 0;
1476 radio->rd_index = 0;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001477 init_waitqueue_head(&radio->read_queue);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001478
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001479 /* prepare rds work function */
1480 INIT_DELAYED_WORK(&radio->work, si470x_work);
1481
1482 /* register video device */
1483 if (video_register_device(radio->videodev, VFL_TYPE_RADIO, radio_nr)) {
1484 printk(KERN_WARNING DRIVER_NAME
1485 ": Could not register video device\n");
1486 goto err_all;
1487 }
1488 usb_set_intfdata(intf, radio);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001489
1490 return 0;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001491err_all:
1492 video_device_release(radio->videodev);
1493 kfree(radio->buffer);
1494err_radio:
1495 kfree(radio);
1496err_initial:
1497 return retval;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001498}
1499
1500
1501/*
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001502 * si470x_usb_driver_suspend - suspend the device
1503 */
1504static int si470x_usb_driver_suspend(struct usb_interface *intf,
1505 pm_message_t message)
1506{
1507 struct si470x_device *radio = usb_get_intfdata(intf);
1508
1509 printk(KERN_INFO DRIVER_NAME ": suspending now...\n");
1510
1511 cancel_delayed_work_sync(&radio->work);
1512
1513 return 0;
1514}
1515
1516
1517/*
1518 * si470x_usb_driver_resume - resume the device
1519 */
1520static int si470x_usb_driver_resume(struct usb_interface *intf)
1521{
1522 struct si470x_device *radio = usb_get_intfdata(intf);
1523
1524 printk(KERN_INFO DRIVER_NAME ": resuming now...\n");
1525
1526 mutex_lock(&radio->lock);
1527 if (radio->users && radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS)
1528 schedule_delayed_work(&radio->work,
1529 msecs_to_jiffies(rds_poll_time));
1530 mutex_unlock(&radio->lock);
1531
1532 return 0;
1533}
1534
1535
1536/*
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001537 * si470x_usb_driver_disconnect - disconnect the device
1538 */
1539static void si470x_usb_driver_disconnect(struct usb_interface *intf)
1540{
1541 struct si470x_device *radio = usb_get_intfdata(intf);
1542
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001543 mutex_lock(&radio->disconnect_lock);
1544 radio->disconnected = 1;
Tobias Lorenz5caf5132008-02-04 22:26:08 -03001545 cancel_delayed_work_sync(&radio->work);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001546 usb_set_intfdata(intf, NULL);
Tobias Lorenzce5829e2008-05-31 15:04:32 -03001547 if (radio->users == 0) {
1548 video_unregister_device(radio->videodev);
1549 kfree(radio->buffer);
1550 kfree(radio);
1551 }
1552 mutex_unlock(&radio->disconnect_lock);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001553}
1554
1555
1556/*
1557 * si470x_usb_driver - usb driver interface
1558 */
1559static struct usb_driver si470x_usb_driver = {
Tobias Lorenz57566ad2008-02-09 16:08:24 -03001560 .name = DRIVER_NAME,
1561 .probe = si470x_usb_driver_probe,
1562 .disconnect = si470x_usb_driver_disconnect,
1563 .suspend = si470x_usb_driver_suspend,
1564 .resume = si470x_usb_driver_resume,
1565 .id_table = si470x_usb_driver_id_table,
1566 .supports_autosuspend = 1,
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001567};
1568
1569
1570
1571/**************************************************************************
1572 * Module Interface
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001573 **************************************************************************/
1574
1575/*
1576 * si470x_module_init - module init
1577 */
1578static int __init si470x_module_init(void)
1579{
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001580 printk(KERN_INFO DRIVER_DESC ", Version " DRIVER_VERSION "\n");
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001581 return usb_register(&si470x_usb_driver);
1582}
1583
1584
1585/*
1586 * si470x_module_exit - module exit
1587 */
1588static void __exit si470x_module_exit(void)
1589{
1590 usb_deregister(&si470x_usb_driver);
1591}
1592
1593
1594module_init(si470x_module_init);
1595module_exit(si470x_module_exit);
1596
1597MODULE_LICENSE("GPL");
1598MODULE_AUTHOR(DRIVER_AUTHOR);
1599MODULE_DESCRIPTION(DRIVER_DESC);
Tobias Lorenz0e3301e2008-01-27 14:54:07 -03001600MODULE_VERSION(DRIVER_VERSION);