blob: d54fe6405351c51edf0d9dba2590318751cf3e53 [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 Lorenz78656ac2008-01-14 21:55:27 -030058 *
59 * ToDo:
60 * - check USB Vendor/Product ID for ADS/Tech FM Radio Receiver
61 * (formerly Instant FM Music) (RDX-155-EF) is 06e1:a155
62 * - add seeking support
63 * - add firmware download/update support
64 * - add possibility to switch off RDS
65 * - RDS support: interrupt mode, instead of polling
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -030066 * - add LED status output (check if that's not already done in firmware)
Tobias Lorenz78656ac2008-01-14 21:55:27 -030067 */
68
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -030069
Tobias Lorenz78656ac2008-01-14 21:55:27 -030070/* driver definitions */
71#define DRIVER_AUTHOR "Tobias Lorenz <tobias.lorenz@gmx.net>"
72#define DRIVER_NAME "radio-si470x"
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -030073#define DRIVER_VERSION KERNEL_VERSION(1, 0, 3)
Tobias Lorenz78656ac2008-01-14 21:55:27 -030074#define DRIVER_CARD "Silicon Labs Si470x FM Radio Receiver"
75#define DRIVER_DESC "USB radio driver for Si470x FM Radio Receivers"
76
77
78/* kernel includes */
79#include <linux/kernel.h>
80#include <linux/module.h>
81#include <linux/init.h>
82#include <linux/slab.h>
83#include <linux/input.h>
Tobias Lorenz78656ac2008-01-14 21:55:27 -030084#include <linux/usb.h>
85#include <linux/hid.h>
86#include <linux/version.h>
Mauro Carvalho Chehab387d4472008-01-15 11:25:10 -030087#include <linux/videodev2.h>
88#include <media/v4l2-common.h>
Tobias Lorenz78656ac2008-01-14 21:55:27 -030089#include <media/rds.h>
90
91
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -030092/* USB Device ID List */
93static struct usb_device_id si470x_usb_driver_id_table[] = {
94 /* Silicon Labs USB FM Radio Reference Design */
95 { USB_DEVICE_AND_INTERFACE_INFO(0x10c4, 0x818a, USB_CLASS_HID, 0, 0) },
96 /* Terminating entry */
97 { }
98};
99MODULE_DEVICE_TABLE(usb, si470x_usb_driver_id_table);
100
101
102
103/**************************************************************************
104 * Module Parameters
105 **************************************************************************/
106
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300107/* Radio Nr */
108static int radio_nr = -1;
109module_param(radio_nr, int, 0);
110MODULE_PARM_DESC(radio_nr, "Radio Nr");
111
112/* Spacing (kHz) */
113/* 0: 200 kHz (USA, Australia) */
114/* 1: 100 kHz (Europe, Japan) */
115/* 2: 50 kHz */
116static int space = 2;
117module_param(space, int, 0);
118MODULE_PARM_DESC(radio_nr, "Spacing: 0=200kHz 1=100kHz *2=50kHz*");
119
120/* Bottom of Band (MHz) */
121/* 0: 87.5 - 108 MHz (USA, Europe)*/
122/* 1: 76 - 108 MHz (Japan wide band) */
123/* 2: 76 - 90 MHz (Japan) */
124static int band = 1;
125module_param(band, int, 0);
126MODULE_PARM_DESC(radio_nr, "Band: 0=87.5..108MHz *1=76..108MHz* 2=76..90MHz");
127
128/* De-emphasis */
129/* 0: 75 us (USA) */
130/* 1: 50 us (Europe, Australia, Japan) */
131static int de = 1;
132module_param(de, int, 0);
133MODULE_PARM_DESC(radio_nr, "De-emphasis: 0=75us *1=50us*");
134
135/* USB timeout */
136static int usb_timeout = 500;
137module_param(usb_timeout, int, 0);
138MODULE_PARM_DESC(usb_timeout, "USB timeout (ms): *500*");
139
140/* Seek retries */
141static int seek_retries = 100;
142module_param(seek_retries, int, 0);
143MODULE_PARM_DESC(seek_retries, "Seek retries: *100*");
144
145/* RDS buffer blocks */
146static int rds_buf = 100;
147module_param(rds_buf, int, 0);
148MODULE_PARM_DESC(rds_buf, "RDS buffer entries: *100*");
149
150/* RDS maximum block errors */
151static int max_rds_errors = 1;
152/* 0 means 0 errors requiring correction */
153/* 1 means 1-2 errors requiring correction (used by original USBRadio.exe) */
154/* 2 means 3-5 errors requiring correction */
155/* 3 means 6+ errors or errors in checkword, correction not possible */
156module_param(max_rds_errors, int, 0);
157MODULE_PARM_DESC(max_rds_errors, "RDS maximum block errors: *1*");
158
159/* RDS poll frequency */
160static int rds_poll_time = 40;
161/* 40 is used by the original USBRadio.exe */
162/* 75 should be okay */
163/* 80 is the usual RDS receive interval */
164module_param(rds_poll_time, int, 0);
165MODULE_PARM_DESC(rds_poll_time, "RDS poll time (ms): *40*");
166
167
168
169/**************************************************************************
170 * Register Definitions
171 **************************************************************************/
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300172#define RADIO_REGISTER_SIZE 2 /* 16 register bit width */
173#define RADIO_REGISTER_NUM 16 /* DEVICEID ... RDSD */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300174#define RDS_REGISTER_NUM 6 /* STATUSRSSI ... RDSD */
175
176#define DEVICEID 0 /* Device ID */
177#define DEVICEID_PN 0xf000 /* bits 15..12: Part Number */
178#define DEVICEID_MFGID 0x0fff /* bits 11..00: Manufacturer ID */
179
180#define CHIPID 1 /* Chip ID */
181#define CHIPID_REV 0xfc00 /* bits 15..10: Chip Version */
182#define CHIPID_DEV 0x0200 /* bits 09..09: Device */
183#define CHIPID_FIRMWARE 0x01ff /* bits 08..00: Firmware Version */
184
185#define POWERCFG 2 /* Power Configuration */
186#define POWERCFG_DSMUTE 0x8000 /* bits 15..15: Softmute Disable */
187#define POWERCFG_DMUTE 0x4000 /* bits 14..14: Mute Disable */
188#define POWERCFG_MONO 0x2000 /* bits 13..13: Mono Select */
189#define POWERCFG_RDSM 0x0800 /* bits 11..11: RDS Mode (Si4701 only) */
190#define POWERCFG_SKMODE 0x0400 /* bits 10..10: Seek Mode */
191#define POWERCFG_SEEKUP 0x0200 /* bits 09..09: Seek Direction */
192#define POWERCFG_SEEK 0x0100 /* bits 08..08: Seek */
193#define POWERCFG_DISABLE 0x0040 /* bits 06..06: Powerup Disable */
194#define POWERCFG_ENABLE 0x0001 /* bits 00..00: Powerup Enable */
195
196#define CHANNEL 3 /* Channel */
197#define CHANNEL_TUNE 0x8000 /* bits 15..15: Tune */
198#define CHANNEL_CHAN 0x03ff /* bits 09..00: Channel Select */
199
200#define SYSCONFIG1 4 /* System Configuration 1 */
201#define SYSCONFIG1_RDSIEN 0x8000 /* bits 15..15: RDS Interrupt Enable (Si4701 only) */
202#define SYSCONFIG1_STCIEN 0x4000 /* bits 14..14: Seek/Tune Complete Interrupt Enable */
203#define SYSCONFIG1_RDS 0x1000 /* bits 12..12: RDS Enable (Si4701 only) */
204#define SYSCONFIG1_DE 0x0800 /* bits 11..11: De-emphasis (0=75us 1=50us) */
205#define SYSCONFIG1_AGCD 0x0400 /* bits 10..10: AGC Disable */
206#define SYSCONFIG1_BLNDADJ 0x00c0 /* bits 07..06: Stereo/Mono Blend Level Adjustment */
207#define SYSCONFIG1_GPIO3 0x0030 /* bits 05..04: General Purpose I/O 3 */
208#define SYSCONFIG1_GPIO2 0x000c /* bits 03..02: General Purpose I/O 2 */
209#define SYSCONFIG1_GPIO1 0x0003 /* bits 01..00: General Purpose I/O 1 */
210
211#define SYSCONFIG2 5 /* System Configuration 2 */
212#define SYSCONFIG2_SEEKTH 0xff00 /* bits 15..08: RSSI Seek Threshold */
213#define SYSCONFIG2_BAND 0x0080 /* bits 07..06: Band Select */
214#define SYSCONFIG2_SPACE 0x0030 /* bits 05..04: Channel Spacing */
215#define SYSCONFIG2_VOLUME 0x000f /* bits 03..00: Volume */
216
217#define SYSCONFIG3 6 /* System Configuration 3 */
218#define SYSCONFIG3_SMUTER 0xc000 /* bits 15..14: Softmute Attack/Recover Rate */
219#define SYSCONFIG3_SMUTEA 0x3000 /* bits 13..12: Softmute Attenuation */
220#define SYSCONFIG3_SKSNR 0x00f0 /* bits 07..04: Seek SNR Threshold */
221#define SYSCONFIG3_SKCNT 0x000f /* bits 03..00: Seek FM Impulse Detection Threshold */
222
223#define TEST1 7 /* Test 1 */
224#define TEST1_AHIZEN 0x4000 /* bits 14..14: Audio High-Z Enable */
225
226#define TEST2 8 /* Test 2 */
227/* TEST2 only contains reserved bits */
228
229#define BOOTCONFIG 9 /* Boot Configuration */
230/* BOOTCONFIG only contains reserved bits */
231
232#define STATUSRSSI 10 /* Status RSSI */
233#define STATUSRSSI_RDSR 0x8000 /* bits 15..15: RDS Ready (Si4701 only) */
234#define STATUSRSSI_STC 0x4000 /* bits 14..14: Seek/Tune Complete */
235#define STATUSRSSI_SF 0x2000 /* bits 13..13: Seek Fail/Band Limit */
236#define STATUSRSSI_AFCRL 0x1000 /* bits 12..12: AFC Rail */
237#define STATUSRSSI_RDSS 0x0800 /* bits 11..11: RDS Synchronized (Si4701 only) */
238#define STATUSRSSI_BLERA 0x0600 /* bits 10..09: RDS Block A Errors (Si4701 only) */
239#define STATUSRSSI_ST 0x0100 /* bits 08..08: Stereo Indicator */
240#define STATUSRSSI_RSSI 0x00ff /* bits 07..00: RSSI (Received Signal Strength Indicator) */
241
242#define READCHAN 11 /* Read Channel */
243#define READCHAN_BLERB 0xc000 /* bits 15..14: RDS Block D Errors (Si4701 only) */
244#define READCHAN_BLERC 0x3000 /* bits 13..12: RDS Block C Errors (Si4701 only) */
245#define READCHAN_BLERD 0x0c00 /* bits 11..10: RDS Block B Errors (Si4701 only) */
246#define READCHAN_READCHAN 0x03ff /* bits 09..00: Read Channel */
247
248#define RDSA 12 /* RDSA */
249#define RDSA_RDSA 0xffff /* bits 15..00: RDS Block A Data (Si4701 only) */
250
251#define RDSB 13 /* RDSB */
252#define RDSB_RDSB 0xffff /* bits 15..00: RDS Block B Data (Si4701 only) */
253
254#define RDSC 14 /* RDSC */
255#define RDSC_RDSC 0xffff /* bits 15..00: RDS Block C Data (Si4701 only) */
256
257#define RDSD 15 /* RDSD */
258#define RDSD_RDSD 0xffff /* bits 15..00: RDS Block D Data (Si4701 only) */
259
260
261
262/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300263 * USB HID Reports
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300264 **************************************************************************/
265
266/* Reports 1-16 give direct read/write access to the 16 Si470x registers */
267/* with the (REPORT_ID - 1) corresponding to the register address across USB */
268/* endpoint 0 using GET_REPORT and SET_REPORT */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300269#define REGISTER_REPORT_SIZE (RADIO_REGISTER_SIZE + 1)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300270#define REGISTER_REPORT(reg) ((reg) + 1)
271
272/* Report 17 gives direct read/write access to the entire Si470x register */
273/* map across endpoint 0 using GET_REPORT and SET_REPORT */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300274#define ENTIRE_REPORT_SIZE (RADIO_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300275#define ENTIRE_REPORT 17
276
277/* Report 18 is used to send the lowest 6 Si470x registers up the HID */
278/* interrupt endpoint 1 to Windows every 20 milliseconds for status */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300279#define RDS_REPORT_SIZE (RDS_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300280#define RDS_REPORT 18
281
282/* Report 19: LED state */
283#define LED_REPORT_SIZE 3
284#define LED_REPORT 19
285
286/* Report 19: stream */
287#define STREAM_REPORT_SIZE 3
288#define STREAM_REPORT 19
289
290/* Report 20: scratch */
291#define SCRATCH_PAGE_SIZE 63
292#define SCRATCH_REPORT_SIZE (SCRATCH_PAGE_SIZE + 1)
293#define SCRATCH_REPORT 20
294
295/* Reports 19-22: flash upgrade of the C8051F321 */
296#define WRITE_REPORT 19
297#define FLASH_REPORT 20
298#define CRC_REPORT 21
299#define RESPONSE_REPORT 22
300
301/* Report 23: currently unused, but can accept 60 byte reports on the HID */
302/* interrupt out endpoint 2 every 1 millisecond */
303#define UNUSED_REPORT 23
304
305
306
307/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300308 * Software/Hardware Versions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300309 **************************************************************************/
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300310#define RADIO_SW_VERSION_NOT_BOOTLOADABLE 6
311#define RADIO_SW_VERSION 7
312#define RADIO_SW_VERSION_CURRENT 15
313#define RADIO_HW_VERSION 1
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300314
315#define SCRATCH_PAGE_SW_VERSION 1
316#define SCRATCH_PAGE_HW_VERSION 2
317
318
319
320/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300321 * LED State Definitions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300322 **************************************************************************/
323#define LED_COMMAND 0x35
324
325#define NO_CHANGE_LED 0x00
326#define ALL_COLOR_LED 0x01 /* streaming state */
327#define BLINK_GREEN_LED 0x02 /* connect state */
328#define BLINK_RED_LED 0x04
329#define BLINK_ORANGE_LED 0x10 /* disconnect state */
330#define SOLID_GREEN_LED 0x20 /* tuning/seeking state */
331#define SOLID_RED_LED 0x40 /* bootload state */
332#define SOLID_ORANGE_LED 0x80
333
334
335
336/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300337 * Stream State Definitions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300338 **************************************************************************/
339#define STREAM_COMMAND 0x36
340#define STREAM_VIDPID 0x00
341#define STREAM_AUDIO 0xff
342
343
344
345/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300346 * Bootloader / Flash Commands
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300347 **************************************************************************/
348
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300349/* unique id sent to bootloader and required to put into a bootload state */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300350#define UNIQUE_BL_ID 0x34
351
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300352/* mask for the flash data */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300353#define FLASH_DATA_MASK 0x55
354
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300355/* bootloader commands */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300356#define GET_SW_VERSION_COMMAND 0x00
357#define SET_PAGE_COMMAND 0x01
358#define ERASE_PAGE_COMMAND 0x02
359#define WRITE_PAGE_COMMAND 0x03
360#define CRC_ON_PAGE_COMMAND 0x04
361#define READ_FLASH_BYTE_COMMAND 0x05
362#define RESET_DEVICE_COMMAND 0x06
363#define GET_HW_VERSION_COMMAND 0x07
364#define BLANK 0xff
365
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300366/* bootloader command responses */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300367#define COMMAND_OK 0x01
368#define COMMAND_FAILED 0x02
369#define COMMAND_PENDING 0x03
370
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300371/* buffer sizes */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300372#define COMMAND_BUFFER_SIZE 4
373#define RESPONSE_BUFFER_SIZE 2
374#define FLASH_BUFFER_SIZE 64
375#define CRC_BUFFER_SIZE 3
376
377
378
379/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300380 * General Driver Definitions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300381 **************************************************************************/
382
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300383/*
384 * si470x_device - private data
385 */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300386struct si470x_device {
387 /* reference to USB and video device */
388 struct usb_device *usbdev;
389 struct video_device *videodev;
390
391 /* are these really necessary ? */
392 int users;
393
394 /* report buffer (maximum 64 bytes) */
395 unsigned char buf[64];
396
397 /* Silabs internal registers (0..15) */
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300398 unsigned short registers[RADIO_REGISTER_NUM];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300399
400 /* RDS receive buffer */
401 struct work_struct work;
402 struct timer_list timer;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300403 spinlock_t lock; /* buffer locking */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300404 unsigned char *buffer;
405 unsigned int buf_size;
406 unsigned int rd_index;
407 unsigned int wr_index;
408 unsigned int block_count;
409 unsigned char last_blocknum;
410 wait_queue_head_t read_queue;
411 int data_available_for_read;
412};
413
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300414
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300415/*
416 * The frequency is set in units of 62.5 Hz when using V4L2_TUNER_CAP_LOW,
417 * 62.5 kHz otherwise.
418 * The tuner is able to have a channel spacing of 50, 100 or 200 kHz.
419 * tuner->capability is therefore set to V4L2_TUNER_CAP_LOW
420 * The FREQ_MUL is then: 1 MHz / 62.5 Hz = 16000
421 */
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300422#define FREQ_MUL (1000000 / 62.5)
423
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300424
425
426/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300427 * General Driver Functions
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300428 **************************************************************************/
429
430/*
431 * si470x_get_report - receive a HID report
432 */
433static int si470x_get_report(struct si470x_device *radio, int size)
434{
435 return usb_control_msg(radio->usbdev,
436 usb_rcvctrlpipe(radio->usbdev, 0),
437 HID_REQ_GET_REPORT,
438 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
439 radio->buf[0], 2,
440 radio->buf, size, usb_timeout);
441}
442
443
444/*
445 * si470x_set_report - send a HID report
446 */
447static int si470x_set_report(struct si470x_device *radio, int size)
448{
449 return usb_control_msg(radio->usbdev,
450 usb_sndctrlpipe(radio->usbdev, 0),
451 HID_REQ_SET_REPORT,
452 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
453 radio->buf[0], 2,
454 radio->buf, size, usb_timeout);
455}
456
457
458/*
459 * si470x_get_register - read register
460 */
461static int si470x_get_register(struct si470x_device *radio, int regnr)
462{
463 int retval;
464
465 radio->buf[0] = REGISTER_REPORT(regnr);
466
467 retval = si470x_get_report(radio, REGISTER_REPORT_SIZE);
468 if (retval >= 0)
469 radio->registers[regnr] = (radio->buf[1] << 8) | radio->buf[2];
470
471 return (retval < 0) ? -EINVAL : 0;
472}
473
474
475/*
476 * si470x_set_register - write register
477 */
478static int si470x_set_register(struct si470x_device *radio, int regnr)
479{
480 int retval;
481
482 radio->buf[0] = REGISTER_REPORT(regnr);
483 radio->buf[1] = (radio->registers[regnr] & 0xff00) >> 8;
484 radio->buf[2] = (radio->registers[regnr] & 0x00ff);
485
486 retval = si470x_set_report(radio, REGISTER_REPORT_SIZE);
487
488 return (retval < 0) ? -EINVAL : 0;
489}
490
491
492/*
493 * si470x_get_all_registers - read entire registers
494 */
495static int si470x_get_all_registers(struct si470x_device *radio)
496{
497 int retval;
498 int regnr;
499
500 radio->buf[0] = ENTIRE_REPORT;
501
502 retval = si470x_get_report(radio, ENTIRE_REPORT_SIZE);
503
504 if (retval >= 0)
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300505 for (regnr = 0; regnr < RADIO_REGISTER_NUM; regnr++)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300506 radio->registers[regnr] =
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300507 (radio->buf[regnr * RADIO_REGISTER_SIZE + 1] << 8) |
508 radio->buf[regnr * RADIO_REGISTER_SIZE + 2];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300509
510 return (retval < 0) ? -EINVAL : 0;
511}
512
513
514/*
515 * si470x_get_rds_registers - read rds registers
516 */
517static int si470x_get_rds_registers(struct si470x_device *radio)
518{
519 int retval;
520 int regnr;
521 int size;
522
523 radio->buf[0] = RDS_REPORT;
524
525 retval = usb_interrupt_msg(radio->usbdev,
526 usb_rcvctrlpipe(radio->usbdev, 1),
527 radio->buf, RDS_REPORT_SIZE, &size, usb_timeout);
528
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300529 if (retval >= 0)
530 for (regnr = 0; regnr < RDS_REGISTER_NUM; regnr++)
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300531 radio->registers[STATUSRSSI + regnr] =
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300532 (radio->buf[regnr * RADIO_REGISTER_SIZE + 1] << 8) |
533 radio->buf[regnr * RADIO_REGISTER_SIZE + 2];
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300534
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300535 return (retval < 0) ? -EINVAL : 0;
536}
537
538
539/*
540 * si470x_set_chan - set the channel
541 */
542static int si470x_set_chan(struct si470x_device *radio, int chan)
543{
544 int retval, i;
545
546 /* start tuning */
547 radio->registers[CHANNEL] &= ~CHANNEL_CHAN;
548 radio->registers[CHANNEL] |= CHANNEL_TUNE | chan;
549 retval = si470x_set_register(radio, CHANNEL);
550 if (retval < 0)
551 return retval;
552
553 /* wait till seek operation has completed */
554 i = 0;
555 do {
556 retval = si470x_get_register(radio, STATUSRSSI);
557 if (retval < 0)
558 return retval;
559 } while ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) &&
560 (++i < seek_retries));
561 if (i >= seek_retries)
562 printk(KERN_WARNING DRIVER_NAME
563 ": seek does not finish after %d tries\n", i);
564
565 /* stop tuning */
566 radio->registers[CHANNEL] &= ~CHANNEL_TUNE;
567 return si470x_set_register(radio, CHANNEL);
568}
569
570
571/*
572 * si470x_get_freq - get the frequency
573 */
574static int si470x_get_freq(struct si470x_device *radio)
575{
576 int spacing, band_bottom, chan, freq;
577 int retval;
578
579 /* Spacing (kHz) */
580 switch (space) {
581 /* 0: 200 kHz (USA, Australia) */
582 case 0 : spacing = 0.200 * FREQ_MUL; break;
583 /* 1: 100 kHz (Europe, Japan) */
584 case 1 : spacing = 0.100 * FREQ_MUL; break;
585 /* 2: 50 kHz */
586 default: spacing = 0.050 * FREQ_MUL; break;
587 };
588
589 /* Bottom of Band (MHz) */
590 switch (band) {
591 /* 0: 87.5 - 108 MHz (USA, Europe) */
592 case 0 : band_bottom = 87.5 * FREQ_MUL; break;
593 /* 1: 76 - 108 MHz (Japan wide band) */
594 default: band_bottom = 76 * FREQ_MUL; break;
595 /* 2: 76 - 90 MHz (Japan) */
596 case 2 : band_bottom = 76 * FREQ_MUL; break;
597 };
598
599 /* read channel */
600 retval = si470x_get_register(radio, READCHAN);
601 if (retval < 0)
602 return retval;
603 chan = radio->registers[READCHAN] & READCHAN_READCHAN;
604
605 /* Frequency (MHz) = Spacing (kHz) x Channel + Bottom of Band (MHz) */
606 freq = chan * spacing + band_bottom;
607
608 return freq;
609}
610
611
612/*
613 * si470x_set_freq - set the frequency
614 */
615static int si470x_set_freq(struct si470x_device *radio, int freq)
616{
617 int spacing, band_bottom, chan;
618
619 /* Spacing (kHz) */
620 switch (space) {
621 /* 0: 200 kHz (USA, Australia) */
622 case 0 : spacing = 0.200 * FREQ_MUL; break;
623 /* 1: 100 kHz (Europe, Japan) */
624 case 1 : spacing = 0.100 * FREQ_MUL; break;
625 /* 2: 50 kHz */
626 default: spacing = 0.050 * FREQ_MUL; break;
627 };
628
629 /* Bottom of Band (MHz) */
630 switch (band) {
631 /* 0: 87.5 - 108 MHz (USA, Europe) */
632 case 0 : band_bottom = 87.5 * FREQ_MUL; break;
633 /* 1: 76 - 108 MHz (Japan wide band) */
634 default: band_bottom = 76 * FREQ_MUL; break;
635 /* 2: 76 - 90 MHz (Japan) */
636 case 2 : band_bottom = 76 * FREQ_MUL; break;
637 };
638
639 /* Chan = [ Freq (Mhz) - Bottom of Band (MHz) ] / Spacing (kHz) */
640 chan = (freq - band_bottom) / spacing;
641
642 return si470x_set_chan(radio, chan);
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300643}
644
645
646/*
647 * si470x_start - switch on radio
648 */
649static int si470x_start(struct si470x_device *radio)
650{
651 int retval;
652
653 /* powercfg */
654 radio->registers[POWERCFG] =
655 POWERCFG_DMUTE | POWERCFG_ENABLE | POWERCFG_RDSM;
656 retval = si470x_set_register(radio, POWERCFG);
657 if (retval < 0)
658 return retval;
659
660 /* sysconfig 1 */
661 radio->registers[SYSCONFIG1] =
662 SYSCONFIG1_DE | SYSCONFIG1_RDS;
663 retval = si470x_set_register(radio, SYSCONFIG1);
664 if (retval < 0)
665 return retval;
666
667 /* sysconfig 2 */
668 radio->registers[SYSCONFIG2] =
669 (0x3f << 8) | /* SEEKTH */
670 (band << 6) | /* BAND */
671 (space << 4) | /* SPACE */
672 15; /* VOLUME (max) */
673 retval = si470x_set_register(radio, SYSCONFIG2);
674 if (retval < 0)
675 return retval;
676
677 /* reset last channel */
678 return si470x_set_chan(radio,
679 radio->registers[CHANNEL] & CHANNEL_CHAN);
680}
681
682
683/*
684 * si470x_stop - switch off radio
685 */
686static int si470x_stop(struct si470x_device *radio)
687{
688 /* powercfg */
689 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
690 /* POWERCFG_ENABLE has to automatically go low */
691 radio->registers[POWERCFG] |= POWERCFG_ENABLE | POWERCFG_DISABLE;
692 return si470x_set_register(radio, POWERCFG);
693}
694
695
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300696
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300697/**************************************************************************
698 * RDS Driver Functions
699 **************************************************************************/
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300700
701/*
702 * si470x_rds - rds processing function
703 */
704static void si470x_rds(struct si470x_device *radio)
705{
706 unsigned long flags;
707 unsigned char tmpbuf[3];
708 unsigned char blocknum;
709 unsigned char bler; /* RDS block errors */
710 unsigned short rds;
711 unsigned int i;
712
713 if (radio->users == 0)
714 return;
715 if (si470x_get_rds_registers(radio) < 0)
716 return;
717 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0) {
718 /* No RDS group ready */
719 return;
720 }
721 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSS) == 0) {
722 /* RDS decoder not synchronized */
723 return;
724 }
725
726 for (blocknum = 0; blocknum < 4; blocknum++) {
727 switch (blocknum) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300728 default:
729 bler = (radio->registers[STATUSRSSI] &
730 STATUSRSSI_BLERA) >> 9;
731 rds = radio->registers[RDSA];
732 break;
733 case 1:
734 bler = (radio->registers[READCHAN] &
735 READCHAN_BLERB) >> 14;
736 rds = radio->registers[RDSB];
737 break;
738 case 2:
739 bler = (radio->registers[READCHAN] &
740 READCHAN_BLERC) >> 12;
741 rds = radio->registers[RDSC];
742 break;
743 case 3:
744 bler = (radio->registers[READCHAN] &
745 READCHAN_BLERD) >> 10;
746 rds = radio->registers[RDSD];
747 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300748 };
749
750 /* Fill the V4L2 RDS buffer */
751 tmpbuf[0] = rds & 0x00ff; /* LSB */
752 tmpbuf[1] = (rds & 0xff00) >> 8;/* MSB */
753 tmpbuf[2] = blocknum; /* offset name */
754 tmpbuf[2] |= blocknum << 3; /* received offset */
755 if (bler > max_rds_errors)
756 tmpbuf[2] |= 0x80; /* uncorrectable errors */
757 else if (bler > 0)
758 tmpbuf[2] |= 0x40; /* corrected error(s) */
759
760 spin_lock_irqsave(&radio->lock, flags);
761
762 /* copy RDS block to internal buffer */
763 for (i = 0; i < 3; i++) {
764 radio->buffer[radio->wr_index] = tmpbuf[i];
765 radio->wr_index++;
766 }
767
768 if (radio->wr_index >= radio->buf_size)
769 radio->wr_index = 0;
770
771 if (radio->wr_index == radio->rd_index) {
772 radio->rd_index += 3;
773 if (radio->rd_index >= radio->buf_size)
774 radio->rd_index = 0;
775 } else
776 radio->block_count++;
777
778 spin_unlock_irqrestore(&radio->lock, flags);
779 }
780
781 radio->data_available_for_read = 1;
782 wake_up_interruptible(&radio->read_queue);
783}
784
785
786/*
787 * si470x_timer - rds timer function
788 */
789static void si470x_timer(unsigned long data)
790{
791 struct si470x_device *radio = (struct si470x_device *) data;
792
793 schedule_work(&radio->work);
794}
795
796
797/*
798 * si470x_timer - rds work function
799 */
800static void si470x_work(struct work_struct *work)
801{
802 struct si470x_device *radio = container_of(work, struct si470x_device,
803 work);
804
805 if (radio->users == 0)
806 return;
807
808 si470x_rds(radio);
809 mod_timer(&radio->timer, jiffies + msecs_to_jiffies(rds_poll_time));
810}
811
812
813
814/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300815 * File Operations Interface
Tobias Lorenz78656ac2008-01-14 21:55:27 -0300816 **************************************************************************/
817
818/*
819 * si470x_fops_read - read RDS data
820 */
821static ssize_t si470x_fops_read(struct file *file, char __user *buf,
822 size_t count, loff_t *ppos)
823{
824 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
825 struct rds_command cmd;
826 unsigned long flags;
827 unsigned int i;
828 unsigned int rd_blocks;
829
830 cmd.block_count = count / 3; /* each RDS block needs 3 bytes */
831 cmd.result = 0;
832 cmd.buffer = buf;
833 cmd.instance = file;
834
835 /* copy RDS block out of internal buffer */
836 while (!radio->data_available_for_read) {
837 if (wait_event_interruptible(radio->read_queue,
838 radio->data_available_for_read) < 0)
839 return -EINTR;
840 }
841
842 spin_lock_irqsave(&radio->lock, flags);
843 rd_blocks = cmd.block_count;
844 if (rd_blocks > radio->block_count)
845 rd_blocks = radio->block_count;
846
847 if (!rd_blocks) {
848 spin_unlock_irqrestore(&radio->lock, flags);
849 return cmd.result;
850 }
851
852 for (i = 0; i < rd_blocks; i++) {
853 /* copy RDS block to user buffer */
854 if (radio->rd_index == radio->wr_index)
855 break;
856
857 if (copy_to_user(buf, &radio->buffer[radio->rd_index], 3))
858 break;
859
860 radio->rd_index += 3;
861 if (radio->rd_index >= radio->buf_size)
862 radio->rd_index = 0;
863 radio->block_count--;
864
865 buf += 3;
866 cmd.result += 3;
867 }
868 radio->data_available_for_read = (radio->block_count > 0);
869 spin_unlock_irqrestore(&radio->lock, flags);
870
871 return cmd.result;
872}
873
874
875/*
876 * si470x_fops_poll - poll RDS data
877 */
878static unsigned int si470x_fops_poll(struct file *file,
879 struct poll_table_struct *pts)
880{
881 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
882 int retval;
883
884 retval = 0;
885 if (radio->data_available_for_read)
886 retval = POLLIN | POLLRDNORM;
887 poll_wait(file, &radio->read_queue, pts);
888
889 return retval;
890}
891
892
893/*
894 * si470x_fops_open - file open
895 */
896static int si470x_fops_open(struct inode *inode, struct file *file)
897{
898 int retval;
899 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
900
901 radio->users++;
902 if (radio->users == 1) {
903 retval = si470x_start(radio);
904 if (retval < 0)
905 return retval;
906
907 schedule_work(&radio->work);
908 }
909
910 return 0;
911}
912
913
914/*
915 * si470x_fops_release - file release
916 */
917static int si470x_fops_release(struct inode *inode, struct file *file)
918{
919 int retval;
920 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
921
922 if (!radio)
923 return -ENODEV;
924
925 radio->users--;
926 if (radio->users == 0) {
927 radio->data_available_for_read = 1; /* ? */
928 wake_up_interruptible(&radio->read_queue); /* ? */
929
930 retval = si470x_stop(radio);
931 if (retval < 0)
932 return retval;
933 }
934
935 return 0;
936}
937
938
939/*
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -0300940 * si470x_fops - file operations interface
941 */
942static const struct file_operations si470x_fops = {
943 .owner = THIS_MODULE,
944 .llseek = no_llseek,
945 .read = si470x_fops_read,
946 .poll = si470x_fops_poll,
947 .ioctl = video_ioctl2,
948 .compat_ioctl = v4l_compat_ioctl32,
949 .open = si470x_fops_open,
950 .release = si470x_fops_release,
951};
952
953
954
955/**************************************************************************
956 * Video4Linux Interface
957 **************************************************************************/
958
959/*
960 * si470x_v4l2_queryctrl - query control
961 */
962static struct v4l2_queryctrl si470x_v4l2_queryctrl[] = {
963/* HINT: the disabled controls are only here to satify kradio and such apps */
964 {
965 .id = V4L2_CID_AUDIO_VOLUME,
966 .type = V4L2_CTRL_TYPE_INTEGER,
967 .name = "Volume",
968 .minimum = 0,
969 .maximum = 15,
970 .step = 1,
971 .default_value = 15,
972 },
973 {
974 .id = V4L2_CID_AUDIO_BALANCE,
975 .flags = V4L2_CTRL_FLAG_DISABLED,
976 },
977 {
978 .id = V4L2_CID_AUDIO_BASS,
979 .flags = V4L2_CTRL_FLAG_DISABLED,
980 },
981 {
982 .id = V4L2_CID_AUDIO_TREBLE,
983 .flags = V4L2_CTRL_FLAG_DISABLED,
984 },
985 {
986 .id = V4L2_CID_AUDIO_MUTE,
987 .type = V4L2_CTRL_TYPE_BOOLEAN,
988 .name = "Mute",
989 .minimum = 0,
990 .maximum = 1,
991 .step = 1,
992 .default_value = 1,
993 },
994 {
995 .id = V4L2_CID_AUDIO_LOUDNESS,
996 .flags = V4L2_CTRL_FLAG_DISABLED,
997 },
998};
999
1000
1001/*
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001002 * si470x_vidioc_querycap - query device capabilities
1003 */
1004static int si470x_vidioc_querycap(struct file *file, void *priv,
1005 struct v4l2_capability *capability)
1006{
1007 strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver));
1008 strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
1009 sprintf(capability->bus_info, "USB");
1010 capability->version = DRIVER_VERSION;
1011 capability->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
1012
1013 return 0;
1014}
1015
1016
1017/*
1018 * si470x_vidioc_g_input - get input
1019 */
1020static int si470x_vidioc_g_input(struct file *filp, void *priv,
1021 unsigned int *i)
1022{
1023 *i = 0;
1024
1025 return 0;
1026}
1027
1028
1029/*
1030 * si470x_vidioc_s_input - set input
1031 */
1032static int si470x_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
1033{
1034 if (i != 0)
1035 return -EINVAL;
1036
1037 return 0;
1038}
1039
1040
1041/*
1042 * si470x_vidioc_queryctrl - enumerate control items
1043 */
1044static int si470x_vidioc_queryctrl(struct file *file, void *priv,
1045 struct v4l2_queryctrl *qc)
1046{
1047 int i;
1048
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001049 for (i = 0; i < ARRAY_SIZE(si470x_v4l2_queryctrl); i++) {
1050 if (qc->id && qc->id == si470x_v4l2_queryctrl[i].id) {
1051 memcpy(qc, &(si470x_v4l2_queryctrl[i]), sizeof(*qc));
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001052 return 0;
1053 }
1054 }
1055
1056 return -EINVAL;
1057}
1058
1059
1060/*
1061 * si470x_vidioc_g_ctrl - get the value of a control
1062 */
1063static int si470x_vidioc_g_ctrl(struct file *file, void *priv,
1064 struct v4l2_control *ctrl)
1065{
1066 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1067
1068 switch (ctrl->id) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001069 case V4L2_CID_AUDIO_VOLUME:
1070 ctrl->value = radio->registers[SYSCONFIG2] &
1071 SYSCONFIG2_VOLUME;
1072 break;
1073 case V4L2_CID_AUDIO_MUTE:
1074 ctrl->value = ((radio->registers[POWERCFG] &
1075 POWERCFG_DMUTE) == 0) ? 1 : 0;
1076 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001077 }
1078
1079 return 0;
1080}
1081
1082
1083/*
1084 * si470x_vidioc_s_ctrl - set the value of a control
1085 */
1086static int si470x_vidioc_s_ctrl(struct file *file, void *priv,
1087 struct v4l2_control *ctrl)
1088{
1089 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1090
1091 switch (ctrl->id) {
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001092 case V4L2_CID_AUDIO_VOLUME:
1093 radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_VOLUME;
1094 radio->registers[SYSCONFIG2] |= ctrl->value;
1095 return si470x_set_register(radio, SYSCONFIG2);
1096 case V4L2_CID_AUDIO_MUTE:
1097 if (ctrl->value == 1)
1098 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
1099 else
1100 radio->registers[POWERCFG] |= POWERCFG_DMUTE;
1101 return si470x_set_register(radio, POWERCFG);
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001102 }
1103
1104 return -EINVAL;
1105}
1106
1107
1108/*
1109 * si470x_vidioc_g_audio - get audio attributes
1110 */
1111static int si470x_vidioc_g_audio(struct file *file, void *priv,
1112 struct v4l2_audio *audio)
1113{
1114 if (audio->index > 1)
1115 return -EINVAL;
1116
1117 strcpy(audio->name, "Radio");
1118 audio->capability = V4L2_AUDCAP_STEREO;
1119
1120 return 0;
1121}
1122
1123
1124/*
1125 * si470x_vidioc_s_audio - set audio attributes
1126 */
1127static int si470x_vidioc_s_audio(struct file *file, void *priv,
1128 struct v4l2_audio *audio)
1129{
1130 if (audio->index != 0)
1131 return -EINVAL;
1132
1133 return 0;
1134}
1135
1136
1137/*
1138 * si470x_vidioc_g_tuner - get tuner attributes
1139 */
1140static int si470x_vidioc_g_tuner(struct file *file, void *priv,
1141 struct v4l2_tuner *tuner)
1142{
1143 int retval;
1144 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1145
1146 if (tuner->index > 0)
1147 return -EINVAL;
1148
1149 /* read status rssi */
1150 retval = si470x_get_register(radio, STATUSRSSI);
1151 if (retval < 0)
1152 return retval;
1153
1154 strcpy(tuner->name, "FM");
1155 tuner->type = V4L2_TUNER_RADIO;
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001156 switch (band) {
1157 /* 0: 87.5 - 108 MHz (USA, Europe, default) */
1158 default:
1159 tuner->rangelow = 87.5 * FREQ_MUL;
1160 tuner->rangehigh = 108 * FREQ_MUL;
1161 break;
1162 /* 1: 76 - 108 MHz (Japan wide band) */
1163 case 1 :
1164 tuner->rangelow = 76 * FREQ_MUL;
1165 tuner->rangehigh = 108 * FREQ_MUL;
1166 break;
1167 /* 2: 76 - 90 MHz (Japan) */
1168 case 2 :
1169 tuner->rangelow = 76 * FREQ_MUL;
1170 tuner->rangehigh = 90 * FREQ_MUL;
1171 break;
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001172 };
1173 tuner->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
1174 tuner->capability = V4L2_TUNER_CAP_LOW;
1175
1176 /* Stereo indicator == Stereo (instead of Mono) */
1177 if ((radio->registers[STATUSRSSI] & STATUSRSSI_ST) == 1)
1178 tuner->audmode = V4L2_TUNER_MODE_STEREO;
1179 else
1180 tuner->audmode = V4L2_TUNER_MODE_MONO;
1181
1182 /* min is worst, max is best; signal:0..0xffff; rssi: 0..0xff */
1183 tuner->signal = (radio->registers[STATUSRSSI] & STATUSRSSI_RSSI)
1184 * 0x0101;
1185
1186 /* automatic frequency control: -1: freq to low, 1 freq to high */
1187 tuner->afc = 0;
1188
1189 return 0;
1190}
1191
1192
1193/*
1194 * si470x_vidioc_s_tuner - set tuner attributes
1195 */
1196static int si470x_vidioc_s_tuner(struct file *file, void *priv,
1197 struct v4l2_tuner *tuner)
1198{
1199 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1200
1201 if (tuner->index > 0)
1202 return -EINVAL;
1203
1204 if (tuner->audmode == V4L2_TUNER_MODE_MONO)
1205 radio->registers[POWERCFG] |= POWERCFG_MONO; /* force mono */
1206 else
1207 radio->registers[POWERCFG] &= ~POWERCFG_MONO; /* try stereo */
1208
1209 return si470x_set_register(radio, POWERCFG);
1210}
1211
1212
1213/*
1214 * si470x_vidioc_g_frequency - get tuner or modulator radio frequency
1215 */
1216static int si470x_vidioc_g_frequency(struct file *file, void *priv,
1217 struct v4l2_frequency *freq)
1218{
1219 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1220
1221 freq->type = V4L2_TUNER_RADIO;
1222 freq->frequency = si470x_get_freq(radio);
1223
1224 return 0;
1225}
1226
1227
1228/*
1229 * si470x_vidioc_s_frequency - set tuner or modulator radio frequency
1230 */
1231static int si470x_vidioc_s_frequency(struct file *file, void *priv,
1232 struct v4l2_frequency *freq)
1233{
1234 struct si470x_device *radio = video_get_drvdata(video_devdata(file));
1235
1236 if (freq->type != V4L2_TUNER_RADIO)
1237 return -EINVAL;
1238
1239 return si470x_set_freq(radio, freq->frequency);
1240}
1241
1242
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001243/*
1244 * si470x_viddev_tamples - video device interface
1245 */
1246static struct video_device si470x_viddev_template = {
1247 .fops = &si470x_fops,
1248 .name = DRIVER_NAME,
1249 .type = VID_TYPE_TUNER,
1250 .release = video_device_release,
1251 .vidioc_querycap = si470x_vidioc_querycap,
1252 .vidioc_g_input = si470x_vidioc_g_input,
1253 .vidioc_s_input = si470x_vidioc_s_input,
1254 .vidioc_queryctrl = si470x_vidioc_queryctrl,
1255 .vidioc_g_ctrl = si470x_vidioc_g_ctrl,
1256 .vidioc_s_ctrl = si470x_vidioc_s_ctrl,
1257 .vidioc_g_audio = si470x_vidioc_g_audio,
1258 .vidioc_s_audio = si470x_vidioc_s_audio,
1259 .vidioc_g_tuner = si470x_vidioc_g_tuner,
1260 .vidioc_s_tuner = si470x_vidioc_s_tuner,
1261 .vidioc_g_frequency = si470x_vidioc_g_frequency,
1262 .vidioc_s_frequency = si470x_vidioc_s_frequency,
1263 .owner = THIS_MODULE,
1264};
1265
1266
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001267
1268/**************************************************************************
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001269 * USB Interface
1270 **************************************************************************/
1271
1272/*
1273 * si470x_usb_driver_probe - probe for the device
1274 */
1275static int si470x_usb_driver_probe(struct usb_interface *intf,
1276 const struct usb_device_id *id)
1277{
1278 struct si470x_device *radio;
1279
1280 /* memory and interface allocations */
1281 radio = kmalloc(sizeof(struct si470x_device), GFP_KERNEL);
1282 if (!radio)
1283 return -ENOMEM;
1284 radio->videodev = video_device_alloc();
1285 if (!radio->videodev) {
1286 kfree(radio);
1287 return -ENOMEM;
1288 }
1289 memcpy(radio->videodev, &si470x_viddev_template,
1290 sizeof(si470x_viddev_template));
1291 radio->users = 0;
1292 radio->usbdev = interface_to_usbdev(intf);
1293 video_set_drvdata(radio->videodev, radio);
1294 if (video_register_device(radio->videodev, VFL_TYPE_RADIO, radio_nr)) {
1295 printk(KERN_WARNING DRIVER_NAME
1296 ": Could not register video device\n");
1297 video_device_release(radio->videodev);
1298 kfree(radio);
1299 return -EIO;
1300 }
1301 usb_set_intfdata(intf, radio);
1302
1303 /* show some infos about the specific device */
1304 if (si470x_get_all_registers(radio) < 0) {
1305 video_device_release(radio->videodev);
1306 kfree(radio);
1307 return -EIO;
1308 }
1309 printk(KERN_INFO DRIVER_NAME ": DeviceID=0x%4.4x ChipID=0x%4.4x\n",
1310 radio->registers[DEVICEID], radio->registers[CHIPID]);
1311
1312 /* check if firmware is current */
1313 if ((radio->registers[CHIPID] & CHIPID_FIRMWARE)
1314 < RADIO_SW_VERSION_CURRENT)
1315 printk(KERN_WARNING DRIVER_NAME
1316 ": This driver is known to work with chip version %d, "
1317 "but the device has firmware %d. If you have some "
1318 "trouble using this driver, please report to V4L ML "
1319 "at video4linux-list@redhat.com\n",
1320 radio->registers[CHIPID] & CHIPID_FIRMWARE,
1321 RADIO_SW_VERSION_CURRENT);
1322
1323 /* set initial frequency */
1324 si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
1325
1326 /* rds initialization */
1327 radio->buf_size = rds_buf * 3;
1328 radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
1329 if (!radio->buffer) {
1330 video_device_release(radio->videodev);
1331 kfree(radio);
1332 return -ENOMEM;
1333 }
1334 radio->block_count = 0;
1335 radio->wr_index = 0;
1336 radio->rd_index = 0;
1337 radio->last_blocknum = 0xff;
1338 init_waitqueue_head(&radio->read_queue);
1339 radio->data_available_for_read = 0;
1340
1341 /* prepare polling via eventd */
1342 INIT_WORK(&radio->work, si470x_work);
1343 init_timer(&radio->timer);
1344 radio->timer.function = si470x_timer;
1345 radio->timer.data = (unsigned long) radio;
1346
1347 return 0;
1348}
1349
1350
1351/*
1352 * si470x_usb_driver_disconnect - disconnect the device
1353 */
1354static void si470x_usb_driver_disconnect(struct usb_interface *intf)
1355{
1356 struct si470x_device *radio = usb_get_intfdata(intf);
1357
1358 del_timer_sync(&radio->timer);
1359 flush_scheduled_work();
1360
1361 usb_set_intfdata(intf, NULL);
1362 if (radio) {
1363 video_unregister_device(radio->videodev);
1364 kfree(radio->buffer);
1365 kfree(radio);
1366 }
1367}
1368
1369
1370/*
1371 * si470x_usb_driver - usb driver interface
1372 */
1373static struct usb_driver si470x_usb_driver = {
1374 .name = DRIVER_NAME,
1375 .probe = si470x_usb_driver_probe,
1376 .disconnect = si470x_usb_driver_disconnect,
1377 .id_table = si470x_usb_driver_id_table,
1378};
1379
1380
1381
1382/**************************************************************************
1383 * Module Interface
Tobias Lorenz78656ac2008-01-14 21:55:27 -03001384 **************************************************************************/
1385
1386/*
1387 * si470x_module_init - module init
1388 */
1389static int __init si470x_module_init(void)
1390{
1391 printk(KERN_INFO DRIVER_DESC "\n");
1392 return usb_register(&si470x_usb_driver);
1393}
1394
1395
1396/*
1397 * si470x_module_exit - module exit
1398 */
1399static void __exit si470x_module_exit(void)
1400{
1401 usb_deregister(&si470x_usb_driver);
1402}
1403
1404
1405module_init(si470x_module_init);
1406module_exit(si470x_module_exit);
1407
1408MODULE_LICENSE("GPL");
1409MODULE_AUTHOR(DRIVER_AUTHOR);
1410MODULE_DESCRIPTION(DRIVER_DESC);
Tobias Lorenz8bf5e5c2008-01-25 04:19:48 -03001411MODULE_VERSION("1.0.3");