summaryrefslogtreecommitdiff
path: root/linux/drivers/media/radio/radio-si470x.c
diff options
context:
space:
mode:
Diffstat (limited to 'linux/drivers/media/radio/radio-si470x.c')
-rw-r--r--linux/drivers/media/radio/radio-si470x.c1318
1 files changed, 727 insertions, 591 deletions
diff --git a/linux/drivers/media/radio/radio-si470x.c b/linux/drivers/media/radio/radio-si470x.c
index f34900cd4..e999b2fc3 100644
--- a/linux/drivers/media/radio/radio-si470x.c
+++ b/linux/drivers/media/radio/radio-si470x.c
@@ -5,7 +5,7 @@
* - Silicon Labs USB FM Radio Reference Design
* - ADS/Tech FM Radio Receiver (formerly Instant FM Music) (RDX-155-EF)
*
- * Copyright (c) 2007 Tobias Lorenz <tobias.lorenz@gmx.net>
+ * Copyright (c) 2008 Tobias Lorenz <tobias.lorenz@gmx.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -29,7 +29,7 @@
* Version 1.0.0
* - First working version
* 2008-01-13 Tobias Lorenz <tobias.lorenz@gmx.net>
- * Version 1.0.1
+ * Version 1.0.1
* - Improved error handling, every function now returns errno
* - Improved multi user access (start/mute/stop)
* - Channel doesn't get lost anymore after start/mute/stop
@@ -47,23 +47,60 @@
* - check for firmware version 15
* - code order and prototypes still remain the same
* - spacing and bottom of band codes remain the same
+ * 2008-01-16 Tobias Lorenz <tobias.lorenz@gmx.net>
+ * Version 1.0.3
+ * - code reordered to avoid function prototypes
+ * - switch/case defaults are now more user-friendly
+ * - unified comment style
+ * - applied all checkpatch.pl v1.12 suggestions
+ * except the warning about the too long lines with bit comments
+ * - renamed FMRADIO to RADIO to cut line length (checkpatch.pl)
+ * 2008-01-22 Tobias Lorenz <tobias.lorenz@gmx.net>
+ * Version 1.0.4
+ * - avoid poss. locking when doing copy_to_user which may sleep
+ * - RDS is automatically activated on read now
+ * - code cleaned of unnecessary rds_commands
+ * - USB Vendor/Product ID for ADS/Tech FM Radio Receiver verified
+ * (thanks to Guillaume RAMOUSSE)
+ * 2008-01-27 Tobias Lorenz <tobias.lorenz@gmx.net>
+ * Version 1.0.5
+ * - number of seek_retries changed to tune_timeout
+ * - fixed problem with incomplete tune operations by own buffers
+ * - optimization of variables and printf types
+ * - improved error logging
+ * 2008-01-31 Tobias Lorenz <tobias.lorenz@gmx.net>
+ * Oliver Neukum <oliver@neukum.org>
+ * Version 1.0.6
+ * - fixed coverity checker warnings in *_usb_driver_disconnect
+ * - probe()/open() race by correct ordering in probe()
+ * - DMA coherency rules by separate allocation of all buffers
+ * - use of endianness macros
+ * - abuse of spinlock, replaced by mutex
+ * - racy handling of timer in disconnect,
+ * replaced by delayed_work
+ * - racy interruptible_sleep_on(),
+ * replaced with wait_event_interruptible()
+ * - handle signals in read()
+ * 2008-02-08 Tobias Lorenz <tobias.lorenz@gmx.net>
+ * Oliver Neukum <oliver@neukum.org>
+ * Version 1.0.7
+ * - usb autosuspend support
*
* ToDo:
- * - check USB Vendor/Product ID for ADS/Tech FM Radio Receiver
- * (formerly Instant FM Music) (RDX-155-EF) is 06e1:a155
* - add seeking support
* - add firmware download/update support
- * - add possibility to switch off RDS
* - RDS support: interrupt mode, instead of polling
- * - add LED status output
+ * - add LED status output (check if that's not already done in firmware)
*/
+
/* driver definitions */
#define DRIVER_AUTHOR "Tobias Lorenz <tobias.lorenz@gmx.net>"
#define DRIVER_NAME "radio-si470x"
-#define DRIVER_VERSION KERNEL_VERSION(1, 0, 2)
+#define DRIVER_KERNEL_VERSION KERNEL_VERSION(1, 0, 6)
#define DRIVER_CARD "Silicon Labs Si470x FM Radio Receiver"
#define DRIVER_DESC "USB radio driver for Si470x FM Radio Receivers"
+#define DRIVER_VERSION "1.0.6"
/* kernel includes */
@@ -77,10 +114,31 @@
#include <linux/version.h>
#include "compat.h"
#include <linux/videodev2.h>
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 16)
+#include <linux/mutex.h>
+#endif
#include <media/v4l2-common.h>
#include <media/rds.h>
+#include <asm/unaligned.h>
+
+
+/* USB Device ID List */
+static struct usb_device_id si470x_usb_driver_id_table[] = {
+ /* Silicon Labs USB FM Radio Reference Design */
+ { USB_DEVICE_AND_INTERFACE_INFO(0x10c4, 0x818a, USB_CLASS_HID, 0, 0) },
+ /* ADS/Tech FM Radio Receiver (formerly Instant FM Music) */
+ { USB_DEVICE_AND_INTERFACE_INFO(0x06e1, 0xa155, USB_CLASS_HID, 0, 0) },
+ /* Terminating entry */
+ { }
+};
+MODULE_DEVICE_TABLE(usb, si470x_usb_driver_id_table);
+
+/**************************************************************************
+ * Module Parameters
+ **************************************************************************/
+
/* Radio Nr */
static int radio_nr = -1;
module_param(radio_nr, int, 0);
@@ -90,55 +148,56 @@ MODULE_PARM_DESC(radio_nr, "Radio Nr");
/* 0: 200 kHz (USA, Australia) */
/* 1: 100 kHz (Europe, Japan) */
/* 2: 50 kHz */
-static int space = 2;
-module_param(space, int, 0);
+static unsigned short space = 2;
+module_param(space, ushort, 0);
MODULE_PARM_DESC(radio_nr, "Spacing: 0=200kHz 1=100kHz *2=50kHz*");
/* Bottom of Band (MHz) */
/* 0: 87.5 - 108 MHz (USA, Europe)*/
/* 1: 76 - 108 MHz (Japan wide band) */
/* 2: 76 - 90 MHz (Japan) */
-static int band = 1;
-module_param(band, int, 0);
+static unsigned short band = 1;
+module_param(band, ushort, 0);
MODULE_PARM_DESC(radio_nr, "Band: 0=87.5..108MHz *1=76..108MHz* 2=76..90MHz");
/* De-emphasis */
/* 0: 75 us (USA) */
/* 1: 50 us (Europe, Australia, Japan) */
-static int de = 1;
-module_param(de, int, 0);
+static unsigned short de = 1;
+module_param(de, ushort, 0);
MODULE_PARM_DESC(radio_nr, "De-emphasis: 0=75us *1=50us*");
/* USB timeout */
-static int usb_timeout = 500;
-module_param(usb_timeout, int, 0);
+static unsigned int usb_timeout = 500;
+module_param(usb_timeout, uint, 0);
MODULE_PARM_DESC(usb_timeout, "USB timeout (ms): *500*");
-/* Seek retries */
-static int seek_retries = 100;
-module_param(seek_retries, int, 0);
-MODULE_PARM_DESC(seek_retries, "Seek retries: *100*");
+/* Tune timeout */
+static unsigned int tune_timeout = 3000;
+module_param(tune_timeout, uint, 0);
+MODULE_PARM_DESC(tune_timeout, "Tune timeout: *3000*");
/* RDS buffer blocks */
-static int rds_buf = 100;
-module_param(rds_buf, int, 0);
+static unsigned int rds_buf = 100;
+module_param(rds_buf, uint, 0);
MODULE_PARM_DESC(rds_buf, "RDS buffer entries: *100*");
/* RDS maximum block errors */
-static int max_rds_errors = 1;
+static unsigned short max_rds_errors = 1;
/* 0 means 0 errors requiring correction */
/* 1 means 1-2 errors requiring correction (used by original USBRadio.exe) */
/* 2 means 3-5 errors requiring correction */
/* 3 means 6+ errors or errors in checkword, correction not possible */
-module_param(max_rds_errors, int, 0);
+module_param(max_rds_errors, ushort, 0);
MODULE_PARM_DESC(max_rds_errors, "RDS maximum block errors: *1*");
/* RDS poll frequency */
-static int rds_poll_time = 40;
+static unsigned int rds_poll_time = 40;
/* 40 is used by the original USBRadio.exe */
+/* 50 is used by radio-cadet */
/* 75 should be okay */
/* 80 is the usual RDS receive interval */
-module_param(rds_poll_time, int, 0);
+module_param(rds_poll_time, uint, 0);
MODULE_PARM_DESC(rds_poll_time, "RDS poll time (ms): *40*");
@@ -146,8 +205,8 @@ MODULE_PARM_DESC(rds_poll_time, "RDS poll time (ms): *40*");
/**************************************************************************
* Register Definitions
**************************************************************************/
-#define FMRADIO_REGISTER_SIZE 2 /* 16 register bit width */
-#define FMRADIO_REGISTER_NUM 16 /* DEVICEID ... RDSD */
+#define RADIO_REGISTER_SIZE 2 /* 16 register bit width */
+#define RADIO_REGISTER_NUM 16 /* DEVICEID ... RDSD */
#define RDS_REGISTER_NUM 6 /* STATUSRSSI ... RDSD */
#define DEVICEID 0 /* Device ID */
@@ -237,23 +296,23 @@ MODULE_PARM_DESC(rds_poll_time, "RDS poll time (ms): *40*");
/**************************************************************************
- * USB HID reports
+ * USB HID Reports
**************************************************************************/
/* Reports 1-16 give direct read/write access to the 16 Si470x registers */
/* with the (REPORT_ID - 1) corresponding to the register address across USB */
/* endpoint 0 using GET_REPORT and SET_REPORT */
-#define REGISTER_REPORT_SIZE (FMRADIO_REGISTER_SIZE + 1)
+#define REGISTER_REPORT_SIZE (RADIO_REGISTER_SIZE + 1)
#define REGISTER_REPORT(reg) ((reg) + 1)
/* Report 17 gives direct read/write access to the entire Si470x register */
/* map across endpoint 0 using GET_REPORT and SET_REPORT */
-#define ENTIRE_REPORT_SIZE (FMRADIO_REGISTER_NUM * FMRADIO_REGISTER_SIZE + 1)
+#define ENTIRE_REPORT_SIZE (RADIO_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
#define ENTIRE_REPORT 17
/* Report 18 is used to send the lowest 6 Si470x registers up the HID */
/* interrupt endpoint 1 to Windows every 20 milliseconds for status */
-#define RDS_REPORT_SIZE (RDS_REGISTER_NUM * FMRADIO_REGISTER_SIZE + 1)
+#define RDS_REPORT_SIZE (RDS_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
#define RDS_REPORT 18
/* Report 19: LED state */
@@ -282,12 +341,12 @@ MODULE_PARM_DESC(rds_poll_time, "RDS poll time (ms): *40*");
/**************************************************************************
- * software/hardware versions
+ * Software/Hardware Versions
**************************************************************************/
-#define FMRADIO_SW_VERSION_NOT_BOOTLOADABLE 6
-#define FMRADIO_SW_VERSION 7
-#define FMRADIO_SW_VERSION_CURRENT 15
-#define FMRADIO_HW_VERSION 1
+#define RADIO_SW_VERSION_NOT_BOOTLOADABLE 6
+#define RADIO_SW_VERSION 7
+#define RADIO_SW_VERSION_CURRENT 15
+#define RADIO_HW_VERSION 1
#define SCRATCH_PAGE_SW_VERSION 1
#define SCRATCH_PAGE_HW_VERSION 2
@@ -295,7 +354,7 @@ MODULE_PARM_DESC(rds_poll_time, "RDS poll time (ms): *40*");
/**************************************************************************
- * LED State definitions
+ * LED State Definitions
**************************************************************************/
#define LED_COMMAND 0x35
@@ -311,7 +370,7 @@ MODULE_PARM_DESC(rds_poll_time, "RDS poll time (ms): *40*");
/**************************************************************************
- * Stream State definitions
+ * Stream State Definitions
**************************************************************************/
#define STREAM_COMMAND 0x36
#define STREAM_VIDPID 0x00
@@ -320,16 +379,16 @@ MODULE_PARM_DESC(rds_poll_time, "RDS poll time (ms): *40*");
/**************************************************************************
- * bootloader / flash commands
+ * Bootloader / Flash Commands
**************************************************************************/
-/* Unique ID sent to bootloader and required to put into a bootload state */
+/* unique id sent to bootloader and required to put into a bootload state */
#define UNIQUE_BL_ID 0x34
-/* Mask for the flash data */
+/* mask for the flash data */
#define FLASH_DATA_MASK 0x55
-/* Bootloader commands */
+/* bootloader commands */
#define GET_SW_VERSION_COMMAND 0x00
#define SET_PAGE_COMMAND 0x01
#define ERASE_PAGE_COMMAND 0x02
@@ -340,12 +399,12 @@ MODULE_PARM_DESC(rds_poll_time, "RDS poll time (ms): *40*");
#define GET_HW_VERSION_COMMAND 0x07
#define BLANK 0xff
-/* Bootloader command responses */
+/* bootloader command responses */
#define COMMAND_OK 0x01
#define COMMAND_FAILED 0x02
#define COMMAND_PENDING 0x03
-/* Buffer sizes */
+/* buffer sizes */
#define COMMAND_BUFFER_SIZE 4
#define RESPONSE_BUFFER_SIZE 2
#define FLASH_BUFFER_SIZE 64
@@ -354,236 +413,102 @@ MODULE_PARM_DESC(rds_poll_time, "RDS poll time (ms): *40*");
/**************************************************************************
- * Driver private definitions
+ * General Driver Definitions
**************************************************************************/
-/* private data */
+/*
+ * si470x_device - private data
+ */
struct si470x_device {
/* reference to USB and video device */
struct usb_device *usbdev;
+ struct usb_interface *intf;
struct video_device *videodev;
- /* are these really necessary ? */
- int users;
-
- /* report buffer (maximum 64 bytes) */
- unsigned char buf[64];
+ /* driver management */
+ unsigned int users;
/* Silabs internal registers (0..15) */
- unsigned short registers[FMRADIO_REGISTER_NUM];
+ unsigned short registers[RADIO_REGISTER_NUM];
/* RDS receive buffer */
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
struct work_struct work;
- struct timer_list timer;
- spinlock_t lock;
- unsigned char *buffer;
+#else
+ struct delayed_work work;
+#endif
+
+ wait_queue_head_t read_queue;
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 16)
+ struct mutex lock; /* buffer locking */
+#else
+ struct semaphore lock; /* buffer locking */
+#endif
+ unsigned char *buffer; /* size is always multiple of three */
unsigned int buf_size;
unsigned int rd_index;
unsigned int wr_index;
- unsigned int block_count;
- unsigned char last_blocknum;
- wait_queue_head_t read_queue;
- int data_available_for_read;
-};
-
-/* register acccess functions */
-static int si470x_get_report(struct si470x_device *radio, int size);
-static int si470x_set_report(struct si470x_device *radio, int size);
-static int si470x_get_register(struct si470x_device *radio, int regnr);
-static int si470x_set_register(struct si470x_device *radio, int regnr);
-static int si470x_get_all_registers(struct si470x_device *radio);
-static int si470x_get_rds_registers(struct si470x_device *radio);
-
-/* high-level functions */
-static int si470x_start(struct si470x_device *radio);
-static int si470x_stop(struct si470x_device *radio);
-static int si470x_set_chan(struct si470x_device *radio, int chan);
-static int si470x_get_freq(struct si470x_device *radio);
-static int si470x_set_freq(struct si470x_device *radio, int freq);
-
-/* RDS functions */
-static void si470x_rds(struct si470x_device *radio);
-static void si470x_timer(unsigned long data);
-static void si470x_work(struct work_struct *work);
-
-
-
-/**************************************************************************
- * USB interface definitions
- **************************************************************************/
-
-/* USB device ID list (Vendor, Product, Class, SubClass, Protocol) */
-static struct usb_device_id si470x_usb_driver_id_table[] = {
- /* Silicon Labs USB FM Radio Reference Design */
- { USB_DEVICE_AND_INTERFACE_INFO(0x10c4, 0x818a, USB_CLASS_HID, 0, 0) },
- /* Terminating entry */
- { }
-};
-MODULE_DEVICE_TABLE (usb, si470x_usb_driver_id_table);
-
-/* USB driver functions */
-static int si470x_usb_driver_probe(struct usb_interface *intf,
- const struct usb_device_id *id);
-static void si470x_usb_driver_disconnect(struct usb_interface *intf);
-
-/* USB driver interface */
-static struct usb_driver si470x_usb_driver = {
- .name = DRIVER_NAME,
- .probe = si470x_usb_driver_probe,
- .disconnect = si470x_usb_driver_disconnect,
- .id_table = si470x_usb_driver_id_table,
};
-
-/**************************************************************************
- * Video4Linux interface definitions
- **************************************************************************/
-
-/* The frequency is set in units of 62.5 Hz when using V4L2_TUNER_CAP_LOW, */
-/* 62.5 kHz otherwise. */
-/* The tuner is able to have a channel spacing of 50, 100 or 200 kHz. */
-/* tuner->capability is therefore set to V4L2_TUNER_CAP_LOW */
-/* The FREQ_MUL is then: 1 MHz / 62.5 Hz = 16000 */
+/*
+ * The frequency is set in units of 62.5 Hz when using V4L2_TUNER_CAP_LOW,
+ * 62.5 kHz otherwise.
+ * The tuner is able to have a channel spacing of 50, 100 or 200 kHz.
+ * tuner->capability is therefore set to V4L2_TUNER_CAP_LOW
+ * The FREQ_MUL is then: 1 MHz / 62.5 Hz = 16000
+ */
#define FREQ_MUL (1000000 / 62.5)
-/* File operations functions */
-static ssize_t si470x_fops_read(struct file *file, char __user *buf,
- size_t count, loff_t *ppos);
-static unsigned int si470x_fops_poll(struct file *file,
- struct poll_table_struct *pts);
-static int si470x_fops_open(struct inode *inode, struct file *file);
-static int si470x_fops_release(struct inode *inode, struct file *file);
-
-/* File operations interface */
-static const struct file_operations si470x_fops = {
- .owner = THIS_MODULE,
- .llseek = no_llseek,
- .read = si470x_fops_read,
- .poll = si470x_fops_poll,
- .ioctl = video_ioctl2,
- .compat_ioctl = v4l_compat_ioctl32,
- .open = si470x_fops_open,
- .release = si470x_fops_release,
-};
-
-/* Video device functions */
-static int si470x_vidioc_querycap(struct file *file, void *priv,
- struct v4l2_capability *capability);
-static int si470x_vidioc_g_input(struct file *file, void *priv,
- unsigned int *i);
-static int si470x_vidioc_s_input(struct file *file, void *priv,
- unsigned int i);
-static int si470x_vidioc_queryctrl(struct file *file, void *priv,
- struct v4l2_queryctrl *qc);
-static int si470x_vidioc_g_ctrl(struct file *file, void *priv,
- struct v4l2_control *ctrl);
-static int si470x_vidioc_s_ctrl(struct file *file, void *priv,
- struct v4l2_control *ctrl);
-static int si470x_vidioc_g_audio(struct file *file, void *priv,
- struct v4l2_audio *audio);
-static int si470x_vidioc_s_audio(struct file *file, void *priv,
- struct v4l2_audio *audio);
-static int si470x_vidioc_g_tuner(struct file *file, void *priv,
- struct v4l2_tuner *tuner);
-static int si470x_vidioc_s_tuner(struct file *file, void *priv,
- struct v4l2_tuner *tuner);
-static int si470x_vidioc_g_frequency(struct file *file, void *priv,
- struct v4l2_frequency *freq);
-static int si470x_vidioc_s_frequency(struct file *file, void *priv,
- struct v4l2_frequency *freq);
-
-/* Video device interface */
-static struct video_device si470x_viddev_template = {
- .fops = &si470x_fops,
- .name = DRIVER_NAME,
- .type = VID_TYPE_TUNER,
- .release = video_device_release,
- .vidioc_querycap = si470x_vidioc_querycap,
- .vidioc_g_input = si470x_vidioc_g_input,
- .vidioc_s_input = si470x_vidioc_s_input,
- .vidioc_queryctrl = si470x_vidioc_queryctrl,
- .vidioc_g_ctrl = si470x_vidioc_g_ctrl,
- .vidioc_s_ctrl = si470x_vidioc_s_ctrl,
- .vidioc_g_audio = si470x_vidioc_g_audio,
- .vidioc_s_audio = si470x_vidioc_s_audio,
- .vidioc_g_tuner = si470x_vidioc_g_tuner,
- .vidioc_s_tuner = si470x_vidioc_s_tuner,
- .vidioc_g_frequency = si470x_vidioc_g_frequency,
- .vidioc_s_frequency = si470x_vidioc_s_frequency,
- .owner = THIS_MODULE,
-};
-
-/* Query control */
-static struct v4l2_queryctrl radio_queryctrl[] = {
-/* HINT: the disabled controls are only here to satify kradio and such apps */
- {
- .id = V4L2_CID_AUDIO_VOLUME,
- .type = V4L2_CTRL_TYPE_INTEGER,
- .name = "Volume",
- .minimum = 0,
- .maximum = 15,
- .step = 1,
- .default_value = 15,
- },
- {
- .id = V4L2_CID_AUDIO_BALANCE,
- .flags = V4L2_CTRL_FLAG_DISABLED,
- },
- {
- .id = V4L2_CID_AUDIO_BASS,
- .flags = V4L2_CTRL_FLAG_DISABLED,
- },
- {
- .id = V4L2_CID_AUDIO_TREBLE,
- .flags = V4L2_CTRL_FLAG_DISABLED,
- },
- {
- .id = V4L2_CID_AUDIO_MUTE,
- .type = V4L2_CTRL_TYPE_BOOLEAN,
- .name = "Mute",
- .minimum = 0,
- .maximum = 1,
- .step = 1,
- .default_value = 1,
- },
- {
- .id = V4L2_CID_AUDIO_LOUDNESS,
- .flags = V4L2_CTRL_FLAG_DISABLED,
- },
-};
-
/**************************************************************************
- * Driver private functions
+ * General Driver Functions
**************************************************************************/
/*
* si470x_get_report - receive a HID report
*/
-static int si470x_get_report(struct si470x_device *radio, int size)
+static int si470x_get_report(struct si470x_device *radio, void *buf, int size)
{
- return usb_control_msg(radio->usbdev,
+ unsigned char *report = (unsigned char *) buf;
+ int retval;
+
+ retval = usb_control_msg(radio->usbdev,
usb_rcvctrlpipe(radio->usbdev, 0),
HID_REQ_GET_REPORT,
USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
- radio->buf[0], 2,
- radio->buf, size, usb_timeout);
+ report[0], 2,
+ buf, size, usb_timeout);
+ if (retval < 0)
+ printk(KERN_WARNING DRIVER_NAME
+ ": si470x_get_report: usb_control_msg returned %d\n",
+ retval);
+
+ return retval;
}
/*
* si470x_set_report - send a HID report
*/
-static int si470x_set_report(struct si470x_device *radio, int size)
+static int si470x_set_report(struct si470x_device *radio, void *buf, int size)
{
- return usb_control_msg(radio->usbdev,
+ unsigned char *report = (unsigned char *) buf;
+ int retval;
+
+ retval = usb_control_msg(radio->usbdev,
usb_sndctrlpipe(radio->usbdev, 0),
HID_REQ_SET_REPORT,
USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
- radio->buf[0], 2,
- radio->buf, size, usb_timeout);
+ report[0], 2,
+ buf, size, usb_timeout);
+ if (retval < 0)
+ printk(KERN_WARNING DRIVER_NAME
+ ": si470x_set_report: usb_control_msg returned %d\n",
+ retval);
+
+ return retval;
}
@@ -592,13 +517,16 @@ static int si470x_set_report(struct si470x_device *radio, int size)
*/
static int si470x_get_register(struct si470x_device *radio, int regnr)
{
+ unsigned char buf[REGISTER_REPORT_SIZE];
int retval;
- radio->buf[0] = REGISTER_REPORT(regnr);
+ buf[0] = REGISTER_REPORT(regnr);
+
+ retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
- retval = si470x_get_report(radio, REGISTER_REPORT_SIZE);
if (retval >= 0)
- radio->registers[regnr] = (radio->buf[1] << 8) | radio->buf[2];
+ radio->registers[regnr] = be16_to_cpu(get_unaligned(
+ (unsigned short *) &buf[1]));
return (retval < 0) ? -EINVAL : 0;
}
@@ -609,13 +537,14 @@ static int si470x_get_register(struct si470x_device *radio, int regnr)
*/
static int si470x_set_register(struct si470x_device *radio, int regnr)
{
+ unsigned char buf[REGISTER_REPORT_SIZE];
int retval;
- radio->buf[0] = REGISTER_REPORT(regnr);
- radio->buf[1] = (radio->registers[regnr] & 0xff00) >> 8;
- radio->buf[2] = (radio->registers[regnr] & 0x00ff);
+ buf[0] = REGISTER_REPORT(regnr);
+ put_unaligned(cpu_to_be16(radio->registers[regnr]),
+ (unsigned short *) &buf[1]);
- retval = si470x_set_report(radio, REGISTER_REPORT_SIZE);
+ retval = si470x_set_report(radio, (void *) &buf, sizeof(buf));
return (retval < 0) ? -EINVAL : 0;
}
@@ -626,18 +555,19 @@ static int si470x_set_register(struct si470x_device *radio, int regnr)
*/
static int si470x_get_all_registers(struct si470x_device *radio)
{
+ unsigned char buf[ENTIRE_REPORT_SIZE];
int retval;
- int regnr;
+ unsigned char regnr;
- radio->buf[0] = ENTIRE_REPORT;
+ buf[0] = ENTIRE_REPORT;
- retval = si470x_get_report(radio, ENTIRE_REPORT_SIZE);
+ retval = si470x_get_report(radio, (void *) &buf, sizeof(buf));
if (retval >= 0)
- for (regnr = 0; regnr < FMRADIO_REGISTER_NUM; regnr++)
- radio->registers[regnr] =
- (radio->buf[regnr * FMRADIO_REGISTER_SIZE + 1] << 8) |
- radio->buf[regnr * FMRADIO_REGISTER_SIZE + 2];
+ for (regnr = 0; regnr < RADIO_REGISTER_NUM; regnr++)
+ radio->registers[regnr] = be16_to_cpu(get_unaligned(
+ (unsigned short *)
+ &buf[regnr * RADIO_REGISTER_SIZE + 1]));
return (retval < 0) ? -EINVAL : 0;
}
@@ -648,84 +578,41 @@ static int si470x_get_all_registers(struct si470x_device *radio)
*/
static int si470x_get_rds_registers(struct si470x_device *radio)
{
+ unsigned char buf[RDS_REPORT_SIZE];
int retval;
- int regnr;
int size;
+ unsigned char regnr;
- radio->buf[0] = RDS_REPORT;
+ buf[0] = RDS_REPORT;
retval = usb_interrupt_msg(radio->usbdev,
- usb_rcvctrlpipe(radio->usbdev, 1),
- radio->buf, RDS_REPORT_SIZE, &size, usb_timeout);
-
- if (retval >= 0) {
- for (regnr = 0; regnr < RDS_REGISTER_NUM; regnr++) {
- radio->registers[STATUSRSSI + regnr] =
- (radio->buf[regnr * FMRADIO_REGISTER_SIZE + 1] << 8) |
- radio->buf[regnr * FMRADIO_REGISTER_SIZE + 2];
- }
- }
-
- return (retval < 0) ? - EINVAL : 0;
-}
-
-
-/*
- * si470x_start - switch on radio
- */
-static int si470x_start(struct si470x_device *radio)
-{
- int retval;
-
- /* powercfg */
- radio->registers[POWERCFG] =
- POWERCFG_DMUTE | POWERCFG_ENABLE | POWERCFG_RDSM;
- retval = si470x_set_register(radio, POWERCFG);
- if (retval < 0)
- return retval;
-
- /* sysconfig 1 */
- radio->registers[SYSCONFIG1] =
- SYSCONFIG1_DE | SYSCONFIG1_RDS;
- retval = si470x_set_register(radio, SYSCONFIG1);
+ usb_rcvintpipe(radio->usbdev, 1),
+ (void *) &buf, sizeof(buf), &size, usb_timeout);
+ if (size != sizeof(buf))
+ printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_register: "
+ "return size differs: %d != %zu\n", size, sizeof(buf));
if (retval < 0)
- return retval;
-
- /* sysconfig 2 */
- radio->registers[SYSCONFIG2] =
- (0x3f << 8) | /* SEEKTH */
- (band << 6) | /* BAND */
- (space << 4) | /* SPACE */
- 15; /* VOLUME (max) */
- retval = si470x_set_register(radio, SYSCONFIG2);
- if (retval < 0)
- return retval;
-
- /* reset last channel */
- return si470x_set_chan(radio,
- radio->registers[CHANNEL] & CHANNEL_CHAN);
-}
+ printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: "
+ "usb_interrupt_msg returned %d\n", retval);
+ if (retval >= 0)
+ for (regnr = 0; regnr < RDS_REGISTER_NUM; regnr++)
+ radio->registers[STATUSRSSI + regnr] =
+ be16_to_cpu(get_unaligned((unsigned short *)
+ &buf[regnr * RADIO_REGISTER_SIZE + 1]));
-/*
- * si470x_stop - switch off radio
- */
-static int si470x_stop(struct si470x_device *radio)
-{
- /* powercfg */
- radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
- /* POWERCFG_ENABLE has to automatically go low */
- radio->registers[POWERCFG] |= POWERCFG_ENABLE | POWERCFG_DISABLE;
- return si470x_set_register(radio, POWERCFG);
+ return (retval < 0) ? -EINVAL : 0;
}
/*
* si470x_set_chan - set the channel
*/
-static int si470x_set_chan(struct si470x_device *radio, int chan)
+static int si470x_set_chan(struct si470x_device *radio, unsigned short chan)
{
- int retval, i;
+ int retval;
+ unsigned long timeout;
+ bool timed_out = 0;
/* start tuning */
radio->registers[CHANNEL] &= ~CHANNEL_CHAN;
@@ -735,16 +622,17 @@ static int si470x_set_chan(struct si470x_device *radio, int chan)
return retval;
/* wait till seek operation has completed */
- i = 0;
+ timeout = jiffies + msecs_to_jiffies(tune_timeout);
do {
retval = si470x_get_register(radio, STATUSRSSI);
if (retval < 0)
return retval;
- } while ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) &&
- (++i < seek_retries));
- if (i >= seek_retries)
+ timed_out = time_after(jiffies, timeout);
+ } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
+ (!timed_out));
+ if (timed_out)
printk(KERN_WARNING DRIVER_NAME
- ": seek does not finish after %d tries\n", i);
+ ": seek does not finish after %u ms\n", tune_timeout);
/* stop tuning */
radio->registers[CHANNEL] &= ~CHANNEL_TUNE;
@@ -755,29 +643,30 @@ static int si470x_set_chan(struct si470x_device *radio, int chan)
/*
* si470x_get_freq - get the frequency
*/
-static int si470x_get_freq(struct si470x_device *radio)
+static unsigned int si470x_get_freq(struct si470x_device *radio)
{
- int spacing, band_bottom, chan, freq;
+ unsigned int spacing, band_bottom, freq;
+ unsigned short chan;
int retval;
/* Spacing (kHz) */
switch (space) {
- /* 0: 200 kHz (USA, Australia) */
- default: spacing = 0.200 * FREQ_MUL; break;
- /* 1: 100 kHz (Europe, Japan) */
- case 1 : spacing = 0.100 * FREQ_MUL; break;
- /* 2: 50 kHz */
- case 2 : spacing = 0.050 * FREQ_MUL; break;
+ /* 0: 200 kHz (USA, Australia) */
+ case 0 : spacing = 0.200 * FREQ_MUL; break;
+ /* 1: 100 kHz (Europe, Japan) */
+ case 1 : spacing = 0.100 * FREQ_MUL; break;
+ /* 2: 50 kHz */
+ default: spacing = 0.050 * FREQ_MUL; break;
};
/* Bottom of Band (MHz) */
switch (band) {
- /* 0: 87.5 - 108 MHz (USA, Europe) */
- default: band_bottom = 87.5 * FREQ_MUL; break;
- /* 1: 76 - 108 MHz (Japan wide band) */
- case 1 : band_bottom = 76 * FREQ_MUL; break;
- /* 2: 76 - 90 MHz (Japan) */
- case 2 : band_bottom = 76 * FREQ_MUL; break;
+ /* 0: 87.5 - 108 MHz (USA, Europe) */
+ case 0 : band_bottom = 87.5 * FREQ_MUL; break;
+ /* 1: 76 - 108 MHz (Japan wide band) */
+ default: band_bottom = 76 * FREQ_MUL; break;
+ /* 2: 76 - 90 MHz (Japan) */
+ case 2 : band_bottom = 76 * FREQ_MUL; break;
};
/* read channel */
@@ -796,28 +685,29 @@ static int si470x_get_freq(struct si470x_device *radio)
/*
* si470x_set_freq - set the frequency
*/
-static int si470x_set_freq(struct si470x_device *radio, int freq)
+static int si470x_set_freq(struct si470x_device *radio, unsigned int freq)
{
- int spacing, band_bottom, chan;
+ unsigned int spacing, band_bottom;
+ unsigned short chan;
/* Spacing (kHz) */
switch (space) {
- /* 0: 200 kHz (USA, Australia) */
- default: spacing = 0.200 * FREQ_MUL; break;
- /* 1: 100 kHz (Europe, Japan) */
- case 1 : spacing = 0.100 * FREQ_MUL; break;
- /* 2: 50 kHz */
- case 2 : spacing = 0.050 * FREQ_MUL; break;
+ /* 0: 200 kHz (USA, Australia) */
+ case 0 : spacing = 0.200 * FREQ_MUL; break;
+ /* 1: 100 kHz (Europe, Japan) */
+ case 1 : spacing = 0.100 * FREQ_MUL; break;
+ /* 2: 50 kHz */
+ default: spacing = 0.050 * FREQ_MUL; break;
};
/* Bottom of Band (MHz) */
switch (band) {
- /* 0: 87.5 - 108 MHz (USA, Europe) */
- default: band_bottom = 87.5 * FREQ_MUL; break;
- /* 1: 76 - 108 MHz (Japan wide band) */
- case 1 : band_bottom = 76 * FREQ_MUL; break;
- /* 2: 76 - 90 MHz (Japan) */
- case 2 : band_bottom = 76 * FREQ_MUL; break;
+ /* 0: 87.5 - 108 MHz (USA, Europe) */
+ case 0 : band_bottom = 87.5 * FREQ_MUL; break;
+ /* 1: 76 - 108 MHz (Japan wide band) */
+ default: band_bottom = 76 * FREQ_MUL; break;
+ /* 2: 76 - 90 MHz (Japan) */
+ case 2 : band_bottom = 76 * FREQ_MUL; break;
};
/* Chan = [ Freq (Mhz) - Bottom of Band (MHz) ] / Spacing (kHz) */
@@ -827,21 +717,98 @@ static int si470x_set_freq(struct si470x_device *radio, int freq)
}
+/*
+ * si470x_start - switch on radio
+ */
+static int si470x_start(struct si470x_device *radio)
+{
+ int retval;
+
+ /* powercfg */
+ radio->registers[POWERCFG] =
+ POWERCFG_DMUTE | POWERCFG_ENABLE | POWERCFG_RDSM;
+ retval = si470x_set_register(radio, POWERCFG);
+ if (retval < 0)
+ return retval;
+
+ /* sysconfig 1 */
+ radio->registers[SYSCONFIG1] = SYSCONFIG1_DE;
+ retval = si470x_set_register(radio, SYSCONFIG1);
+ if (retval < 0)
+ return retval;
+
+ /* sysconfig 2 */
+ radio->registers[SYSCONFIG2] =
+ (0x3f << 8) | /* SEEKTH */
+ (band << 6) | /* BAND */
+ (space << 4) | /* SPACE */
+ 15; /* VOLUME (max) */
+ retval = si470x_set_register(radio, SYSCONFIG2);
+ if (retval < 0)
+ return retval;
+
+ /* reset last channel */
+ return si470x_set_chan(radio,
+ radio->registers[CHANNEL] & CHANNEL_CHAN);
+}
+
+
+/*
+ * si470x_stop - switch off radio
+ */
+static int si470x_stop(struct si470x_device *radio)
+{
+ int retval;
+
+ /* sysconfig 1 */
+ radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
+ retval = si470x_set_register(radio, SYSCONFIG1);
+ if (retval < 0)
+ return retval;
+
+ /* powercfg */
+ radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
+ /* POWERCFG_ENABLE has to automatically go low */
+ radio->registers[POWERCFG] |= POWERCFG_ENABLE | POWERCFG_DISABLE;
+ return si470x_set_register(radio, POWERCFG);
+}
+
+
+/*
+ * si470x_rds_on - switch on rds reception
+ */
+static int si470x_rds_on(struct si470x_device *radio)
+{
+ int retval;
+
+ /* sysconfig 1 */
+ mutex_lock(&radio->lock);
+ radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDS;
+ retval = si470x_set_register(radio, SYSCONFIG1);
+ if (retval < 0)
+ radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
+ mutex_unlock(&radio->lock);
+
+ return retval;
+}
+
+
+
+/**************************************************************************
+ * RDS Driver Functions
+ **************************************************************************/
/*
* si470x_rds - rds processing function
*/
static void si470x_rds(struct si470x_device *radio)
{
- unsigned long flags;
- unsigned char tmpbuf[3];
unsigned char blocknum;
- unsigned char bler; /* RDS block errors */
+ unsigned short bler; /* rds block errors */
unsigned short rds;
- unsigned int i;
+ unsigned char tmpbuf[3];
- if (radio->users == 0)
- return;
+ /* get rds blocks */
if (si470x_get_rds_registers(radio) < 0)
return;
if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0) {
@@ -853,196 +820,84 @@ static void si470x_rds(struct si470x_device *radio)
return;
}
+ /* copy all four RDS blocks to internal buffer */
+ mutex_lock(&radio->lock);
for (blocknum = 0; blocknum < 4; blocknum++) {
switch (blocknum) {
- default:
- bler = (radio->registers[STATUSRSSI] &
- STATUSRSSI_BLERA) >> 9;
- rds = radio->registers[RDSA];
- break;
- case 1:
- bler = (radio->registers[READCHAN] &
- READCHAN_BLERB) >> 14;
- rds = radio->registers[RDSB];
- break;
- case 2:
- bler = (radio->registers[READCHAN] &
- READCHAN_BLERC) >> 12;
- rds = radio->registers[RDSC];
- break;
- case 3:
- bler = (radio->registers[READCHAN] &
- READCHAN_BLERD) >> 10;
- rds = radio->registers[RDSD];
- break;
+ default:
+ bler = (radio->registers[STATUSRSSI] &
+ STATUSRSSI_BLERA) >> 9;
+ rds = radio->registers[RDSA];
+ break;
+ case 1:
+ bler = (radio->registers[READCHAN] &
+ READCHAN_BLERB) >> 14;
+ rds = radio->registers[RDSB];
+ break;
+ case 2:
+ bler = (radio->registers[READCHAN] &
+ READCHAN_BLERC) >> 12;
+ rds = radio->registers[RDSC];
+ break;
+ case 3:
+ bler = (radio->registers[READCHAN] &
+ READCHAN_BLERD) >> 10;
+ rds = radio->registers[RDSD];
+ break;
};
/* Fill the V4L2 RDS buffer */
- tmpbuf[0] = rds & 0x00ff; /* LSB */
- tmpbuf[1] = (rds & 0xff00) >> 8;/* MSB */
+ put_unaligned(cpu_to_le16(rds), (unsigned short *) &tmpbuf);
tmpbuf[2] = blocknum; /* offset name */
tmpbuf[2] |= blocknum << 3; /* received offset */
if (bler > max_rds_errors)
- tmpbuf[2] |= 0x80; /* uncorrectable errors */
+ tmpbuf[2] |= 0x80; /* uncorrectable errors */
else if (bler > 0)
- tmpbuf[2] |= 0x40; /* corrected error(s) */
-
- spin_lock_irqsave(&radio->lock, flags);
+ tmpbuf[2] |= 0x40; /* corrected error(s) */
/* copy RDS block to internal buffer */
- for (i = 0; i < 3; i++) {
- radio->buffer[radio->wr_index] = tmpbuf[i];
- radio->wr_index++;
- }
+ memcpy(&radio->buffer[radio->wr_index], &tmpbuf, 3);
+ radio->wr_index += 3;
+ /* wrap write pointer */
if (radio->wr_index >= radio->buf_size)
radio->wr_index = 0;
+ /* check for overflow */
if (radio->wr_index == radio->rd_index) {
+ /* increment and wrap read pointer */
radio->rd_index += 3;
if (radio->rd_index >= radio->buf_size)
radio->rd_index = 0;
- } else
- radio->block_count++;
-
- spin_unlock_irqrestore(&radio->lock, flags);
+ }
}
+ mutex_unlock(&radio->lock);
- radio->data_available_for_read = 1;
- wake_up_interruptible(&radio->read_queue);
+ /* wake up read queue */
+ if (radio->wr_index != radio->rd_index)
+ wake_up_interruptible(&radio->read_queue);
}
/*
- * si470x_timer - rds timer function
- */
-static void si470x_timer(unsigned long data)
-{
- struct si470x_device *radio = (struct si470x_device *) data;
-
- schedule_work(&radio->work);
-}
-
-
-/*
- * si470x_timer - rds work function
+ * si470x_work - rds work function
*/
static void si470x_work(struct work_struct *work)
{
struct si470x_device *radio = container_of(work, struct si470x_device,
- work);
+ work.work);
- if (radio->users == 0)
+ if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
return;
si470x_rds(radio);
- mod_timer(&radio->timer, jiffies + msecs_to_jiffies(rds_poll_time));
+ schedule_delayed_work(&radio->work, msecs_to_jiffies(rds_poll_time));
}
/**************************************************************************
- * USB interface functions
- **************************************************************************/
-
-/*
- * si470x_usb_driver_probe - probe for the device
- */
-static int si470x_usb_driver_probe(struct usb_interface *intf,
- const struct usb_device_id *id)
-{
- struct si470x_device *radio;
-
- if (!(radio = kmalloc(sizeof(struct si470x_device), GFP_KERNEL)))
- return -ENOMEM;
- if (!(radio->videodev = video_device_alloc())) {
- kfree(radio);
- return -ENOMEM;
- }
- memcpy(radio->videodev, &si470x_viddev_template,
- sizeof(si470x_viddev_template));
- radio->users = 0;
- radio->usbdev = interface_to_usbdev(intf);
- video_set_drvdata(radio->videodev, radio);
- if (video_register_device(radio->videodev, VFL_TYPE_RADIO, radio_nr)) {
- printk(KERN_WARNING DRIVER_NAME
- ": Could not register video device\n");
- video_device_release(radio->videodev);
- kfree(radio);
- return -EIO;
- }
- usb_set_intfdata(intf, radio);
-
- /* show some infos about the specific device */
- if (si470x_get_all_registers(radio) < 0) {
- video_device_release(radio->videodev);
- kfree(radio);
- return -EIO;
- }
- printk(KERN_INFO DRIVER_NAME ": DeviceID=0x%4.4x ChipID=0x%4.4x\n",
- radio->registers[DEVICEID], radio->registers[CHIPID]);
-
- /* check if firmware is current */
- if ((radio->registers[CHIPID] & CHIPID_FIRMWARE)
- < FMRADIO_SW_VERSION_CURRENT) {
- printk(KERN_WARNING DRIVER_NAME
- ": This driver is known to work with chip version %d, "
- "but the device has firmware %d. If you have some "
- "trouble using this driver, please report to V4L ML "
- "at video4linux-list@redhat.com\n",
- radio->registers[CHIPID] & CHIPID_FIRMWARE,
- FMRADIO_SW_VERSION_CURRENT);
- }
-
- /* set initial frequency */
- si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
-
- /* rds initialization */
- radio->buf_size = rds_buf * 3;
- if (NULL == (radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL))) {
- video_device_release(radio->videodev);
- kfree(radio);
- return -ENOMEM;
- }
- radio->block_count = 0;
- radio->wr_index = 0;
- radio->rd_index = 0;
- radio->last_blocknum = 0xff;
- init_waitqueue_head(&radio->read_queue);
- radio->data_available_for_read = 0;
-
- /* prepare polling via eventd */
- INIT_WORK(&radio->work, si470x_work);
- init_timer(&radio->timer);
- radio->timer.function = si470x_timer;
- radio->timer.data = (unsigned long) radio;
-
- return 0;
-}
-
-
-/*
- * si470x_usb_driver_disconnect - disconnect the device
- */
-static void si470x_usb_driver_disconnect(struct usb_interface *intf)
-{
- struct si470x_device *radio = usb_get_intfdata(intf);
-
- del_timer_sync(&radio->timer);
- flush_scheduled_work();
-
- usb_set_intfdata(intf, NULL);
- if (radio) {
- video_unregister_device(radio->videodev);
- kfree(radio->buffer);
- kfree(radio);
- }
-}
-
-
-
-/**************************************************************************
- * Video4Linux interface functions
+ * File Operations Interface
**************************************************************************/
/*
@@ -1052,53 +907,52 @@ static ssize_t si470x_fops_read(struct file *file, char __user *buf,
size_t count, loff_t *ppos)
{
struct si470x_device *radio = video_get_drvdata(video_devdata(file));
- struct rds_command cmd;
- unsigned long flags;
- unsigned int i;
- unsigned int rd_blocks;
-
- cmd.block_count = count / 3; /* each RDS block needs 3 bytes */
- cmd.result = 0;
- cmd.buffer = buf;
- cmd.instance = file;
-
- /* copy RDS block out of internal buffer */
- while (!radio->data_available_for_read) {
+ int retval = 0;
+ unsigned int block_count = 0;
+
+ /* switch on rds reception */
+ if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
+ si470x_rds_on(radio);
+ schedule_delayed_work(&radio->work,
+ msecs_to_jiffies(rds_poll_time));
+ }
+
+ /* block if no new data available */
+ while (radio->wr_index == radio->rd_index) {
+ if (file->f_flags & O_NONBLOCK)
+ return -EWOULDBLOCK;
if (wait_event_interruptible(radio->read_queue,
- radio->data_available_for_read) < 0)
+ radio->wr_index != radio->rd_index) < 0)
return -EINTR;
}
- spin_lock_irqsave(&radio->lock, flags);
- rd_blocks = cmd.block_count;
- if (rd_blocks > radio->block_count)
- rd_blocks = radio->block_count;
+ /* calculate block count from byte count */
+ count /= 3;
- if (!rd_blocks) {
- spin_unlock_irqrestore(&radio->lock, flags);
- return cmd.result;
- }
-
- for (i = 0; i < rd_blocks; i++) {
- /* copy RDS block to user buffer */
+ /* copy RDS block out of internal buffer and to user buffer */
+ mutex_lock(&radio->lock);
+ while (block_count < count) {
if (radio->rd_index == radio->wr_index)
break;
+ /* always transfer rds complete blocks */
if (copy_to_user(buf, &radio->buffer[radio->rd_index], 3))
+ /* retval = -EFAULT; */
break;
+ /* increment and wrap read pointer */
radio->rd_index += 3;
if (radio->rd_index >= radio->buf_size)
radio->rd_index = 0;
- radio->block_count--;
+ /* increment counters */
+ block_count++;
buf += 3;
- cmd.result += 3;
+ retval += 3;
}
- radio->data_available_for_read = (radio->block_count > 0);
- spin_unlock_irqrestore(&radio->lock, flags);
+ mutex_unlock(&radio->lock);
- return cmd.result;
+ return retval;
}
@@ -1109,14 +963,20 @@ static unsigned int si470x_fops_poll(struct file *file,
struct poll_table_struct *pts)
{
struct si470x_device *radio = video_get_drvdata(video_devdata(file));
- int retval;
- retval = 0;
- if (radio->data_available_for_read)
- retval = POLLIN | POLLRDNORM;
+ /* switch on rds reception */
+ if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) {
+ si470x_rds_on(radio);
+ schedule_delayed_work(&radio->work,
+ msecs_to_jiffies(rds_poll_time));
+ }
+
poll_wait(file, &radio->read_queue, pts);
- return retval;
+ if (radio->rd_index != radio->wr_index)
+ return POLLIN | POLLRDNORM;
+
+ return 0;
}
@@ -1125,16 +985,22 @@ static unsigned int si470x_fops_poll(struct file *file,
*/
static int si470x_fops_open(struct inode *inode, struct file *file)
{
- int retval;
struct si470x_device *radio = video_get_drvdata(video_devdata(file));
+ int retval;
radio->users++;
+
+ retval = usb_autopm_get_interface(radio->intf);
+ if (retval < 0) {
+ radio->users--;
+ return -EIO;
+ }
+
if (radio->users == 1) {
retval = si470x_start(radio);
if (retval < 0)
- return retval;
-
- schedule_work(&radio->work);
+ usb_autopm_put_interface(radio->intf);
+ return retval;
}
return 0;
@@ -1146,20 +1012,23 @@ static int si470x_fops_open(struct inode *inode, struct file *file)
*/
static int si470x_fops_release(struct inode *inode, struct file *file)
{
- int retval;
struct si470x_device *radio = video_get_drvdata(video_devdata(file));
+ int retval;
if (!radio)
return -ENODEV;
radio->users--;
if (radio->users == 0) {
- radio->data_available_for_read = 1; /* ? */
- wake_up_interruptible(&radio->read_queue); /* ? */
+ /* stop rds reception */
+ cancel_delayed_work_sync(&radio->work);
+
+ /* cancel read processes */
+ wake_up_interruptible(&radio->read_queue);
retval = si470x_stop(radio);
- if (retval < 0)
- return retval;
+ usb_autopm_put_interface(radio->intf);
+ return retval;
}
return 0;
@@ -1167,6 +1036,68 @@ static int si470x_fops_release(struct inode *inode, struct file *file)
/*
+ * si470x_fops - file operations interface
+ */
+static const struct file_operations si470x_fops = {
+ .owner = THIS_MODULE,
+ .llseek = no_llseek,
+ .read = si470x_fops_read,
+ .poll = si470x_fops_poll,
+ .ioctl = video_ioctl2,
+ .compat_ioctl = v4l_compat_ioctl32,
+ .open = si470x_fops_open,
+ .release = si470x_fops_release,
+};
+
+
+
+/**************************************************************************
+ * Video4Linux Interface
+ **************************************************************************/
+
+/*
+ * si470x_v4l2_queryctrl - query control
+ */
+static struct v4l2_queryctrl si470x_v4l2_queryctrl[] = {
+/* HINT: the disabled controls are only here to satify kradio and such apps */
+ {
+ .id = V4L2_CID_AUDIO_VOLUME,
+ .type = V4L2_CTRL_TYPE_INTEGER,
+ .name = "Volume",
+ .minimum = 0,
+ .maximum = 15,
+ .step = 1,
+ .default_value = 15,
+ },
+ {
+ .id = V4L2_CID_AUDIO_BALANCE,
+ .flags = V4L2_CTRL_FLAG_DISABLED,
+ },
+ {
+ .id = V4L2_CID_AUDIO_BASS,
+ .flags = V4L2_CTRL_FLAG_DISABLED,
+ },
+ {
+ .id = V4L2_CID_AUDIO_TREBLE,
+ .flags = V4L2_CTRL_FLAG_DISABLED,
+ },
+ {
+ .id = V4L2_CID_AUDIO_MUTE,
+ .type = V4L2_CTRL_TYPE_BOOLEAN,
+ .name = "Mute",
+ .minimum = 0,
+ .maximum = 1,
+ .step = 1,
+ .default_value = 1,
+ },
+ {
+ .id = V4L2_CID_AUDIO_LOUDNESS,
+ .flags = V4L2_CTRL_FLAG_DISABLED,
+ },
+};
+
+
+/*
* si470x_vidioc_querycap - query device capabilities
*/
static int si470x_vidioc_querycap(struct file *file, void *priv,
@@ -1175,7 +1106,7 @@ static int si470x_vidioc_querycap(struct file *file, void *priv,
strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver));
strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
sprintf(capability->bus_info, "USB");
- capability->version = DRIVER_VERSION;
+ capability->version = DRIVER_KERNEL_VERSION;
capability->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
return 0;
@@ -1212,16 +1143,21 @@ static int si470x_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
static int si470x_vidioc_queryctrl(struct file *file, void *priv,
struct v4l2_queryctrl *qc)
{
- int i;
+ unsigned char i;
+ int retval = -EINVAL;
- for (i = 0; i < ARRAY_SIZE(radio_queryctrl); i++) {
- if (qc->id && qc->id == radio_queryctrl[i].id) {
- memcpy(qc, &(radio_queryctrl[i]), sizeof(*qc));
- return 0;
+ for (i = 0; i < ARRAY_SIZE(si470x_v4l2_queryctrl); i++) {
+ if (qc->id && qc->id == si470x_v4l2_queryctrl[i].id) {
+ memcpy(qc, &(si470x_v4l2_queryctrl[i]), sizeof(*qc));
+ retval = 0;
+ break;
}
}
+ if (retval < 0)
+ printk(KERN_WARNING DRIVER_NAME
+ ": query control failed with %d\n", retval);
- return -EINVAL;
+ return retval;
}
@@ -1234,14 +1170,14 @@ static int si470x_vidioc_g_ctrl(struct file *file, void *priv,
struct si470x_device *radio = video_get_drvdata(video_devdata(file));
switch (ctrl->id) {
- case V4L2_CID_AUDIO_VOLUME:
- ctrl->value = radio->registers[SYSCONFIG2] &
- SYSCONFIG2_VOLUME;
- break;
- case V4L2_CID_AUDIO_MUTE:
- ctrl->value = ((radio->registers[POWERCFG] &
- POWERCFG_DMUTE) == 0) ? 1 : 0;
- break;
+ case V4L2_CID_AUDIO_VOLUME:
+ ctrl->value = radio->registers[SYSCONFIG2] &
+ SYSCONFIG2_VOLUME;
+ break;
+ case V4L2_CID_AUDIO_MUTE:
+ ctrl->value = ((radio->registers[POWERCFG] &
+ POWERCFG_DMUTE) == 0) ? 1 : 0;
+ break;
}
return 0;
@@ -1255,21 +1191,29 @@ static int si470x_vidioc_s_ctrl(struct file *file, void *priv,
struct v4l2_control *ctrl)
{
struct si470x_device *radio = video_get_drvdata(video_devdata(file));
+ int retval;
switch (ctrl->id) {
- case V4L2_CID_AUDIO_VOLUME:
- radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_VOLUME;
- radio->registers[SYSCONFIG2] |= ctrl->value;
- return si470x_set_register(radio, SYSCONFIG2);
- case V4L2_CID_AUDIO_MUTE:
- if (ctrl->value == 1)
- radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
- else
- radio->registers[POWERCFG] |= POWERCFG_DMUTE;
- return si470x_set_register(radio, POWERCFG);
+ case V4L2_CID_AUDIO_VOLUME:
+ radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_VOLUME;
+ radio->registers[SYSCONFIG2] |= ctrl->value;
+ retval = si470x_set_register(radio, SYSCONFIG2);
+ break;
+ case V4L2_CID_AUDIO_MUTE:
+ if (ctrl->value == 1)
+ radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
+ else
+ radio->registers[POWERCFG] |= POWERCFG_DMUTE;
+ retval = si470x_set_register(radio, POWERCFG);
+ break;
+ default:
+ retval = -EINVAL;
}
+ if (retval < 0)
+ printk(KERN_WARNING DRIVER_NAME
+ ": set control failed with %d\n", retval);
- return -EINVAL;
+ return retval;
}
@@ -1308,8 +1252,8 @@ static int si470x_vidioc_s_audio(struct file *file, void *priv,
static int si470x_vidioc_g_tuner(struct file *file, void *priv,
struct v4l2_tuner *tuner)
{
- int retval;
struct si470x_device *radio = video_get_drvdata(video_devdata(file));
+ int retval;
if (tuner->index > 0)
return -EINVAL;
@@ -1321,22 +1265,22 @@ static int si470x_vidioc_g_tuner(struct file *file, void *priv,
strcpy(tuner->name, "FM");
tuner->type = V4L2_TUNER_RADIO;
- switch(band) {
- /* 0: 87.5 - 108 MHz (USA, Europe, default) */
- default:
- tuner->rangelow = 87.5 * FREQ_MUL;
- tuner->rangehigh = 108 * FREQ_MUL;
- break;
- /* 1: 76 - 108 MHz (Japan wide band) */
- case 1 :
- tuner->rangelow = 76 * FREQ_MUL;
- tuner->rangehigh = 108 * FREQ_MUL;
- break;
- /* 2: 76 - 90 MHz (Japan) */
- case 2 :
- tuner->rangelow = 76 * FREQ_MUL;
- tuner->rangehigh = 90 * FREQ_MUL;
- break;
+ switch (band) {
+ /* 0: 87.5 - 108 MHz (USA, Europe, default) */
+ default:
+ tuner->rangelow = 87.5 * FREQ_MUL;
+ tuner->rangehigh = 108 * FREQ_MUL;
+ break;
+ /* 1: 76 - 108 MHz (Japan wide band) */
+ case 1 :
+ tuner->rangelow = 76 * FREQ_MUL;
+ tuner->rangehigh = 108 * FREQ_MUL;
+ break;
+ /* 2: 76 - 90 MHz (Japan) */
+ case 2 :
+ tuner->rangelow = 76 * FREQ_MUL;
+ tuner->rangehigh = 90 * FREQ_MUL;
+ break;
};
tuner->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
tuner->capability = V4L2_TUNER_CAP_LOW;
@@ -1365,6 +1309,7 @@ static int si470x_vidioc_s_tuner(struct file *file, void *priv,
struct v4l2_tuner *tuner)
{
struct si470x_device *radio = video_get_drvdata(video_devdata(file));
+ int retval;
if (tuner->index > 0)
return -EINVAL;
@@ -1374,7 +1319,12 @@ static int si470x_vidioc_s_tuner(struct file *file, void *priv,
else
radio->registers[POWERCFG] &= ~POWERCFG_MONO; /* try stereo */
- return si470x_set_register(radio, POWERCFG);
+ retval = si470x_set_register(radio, POWERCFG);
+ if (retval < 0)
+ printk(KERN_WARNING DRIVER_NAME
+ ": set tuner failed with %d\n", retval);
+
+ return retval;
}
@@ -1400,17 +1350,203 @@ static int si470x_vidioc_s_frequency(struct file *file, void *priv,
struct v4l2_frequency *freq)
{
struct si470x_device *radio = video_get_drvdata(video_devdata(file));
+ int retval;
if (freq->type != V4L2_TUNER_RADIO)
return -EINVAL;
- return si470x_set_freq(radio, freq->frequency);
+ retval = si470x_set_freq(radio, freq->frequency);
+ if (retval < 0)
+ printk(KERN_WARNING DRIVER_NAME
+ ": set frequency failed with %d\n", retval);
+
+ return 0;
}
+/*
+ * si470x_viddev_tamples - video device interface
+ */
+static struct video_device si470x_viddev_template = {
+ .fops = &si470x_fops,
+ .name = DRIVER_NAME,
+ .type = VID_TYPE_TUNER,
+ .release = video_device_release,
+ .vidioc_querycap = si470x_vidioc_querycap,
+ .vidioc_g_input = si470x_vidioc_g_input,
+ .vidioc_s_input = si470x_vidioc_s_input,
+ .vidioc_queryctrl = si470x_vidioc_queryctrl,
+ .vidioc_g_ctrl = si470x_vidioc_g_ctrl,
+ .vidioc_s_ctrl = si470x_vidioc_s_ctrl,
+ .vidioc_g_audio = si470x_vidioc_g_audio,
+ .vidioc_s_audio = si470x_vidioc_s_audio,
+ .vidioc_g_tuner = si470x_vidioc_g_tuner,
+ .vidioc_s_tuner = si470x_vidioc_s_tuner,
+ .vidioc_g_frequency = si470x_vidioc_g_frequency,
+ .vidioc_s_frequency = si470x_vidioc_s_frequency,
+ .owner = THIS_MODULE,
+};
+
+
+
+/**************************************************************************
+ * USB Interface
+ **************************************************************************/
+
+/*
+ * si470x_usb_driver_probe - probe for the device
+ */
+static int si470x_usb_driver_probe(struct usb_interface *intf,
+ const struct usb_device_id *id)
+{
+ struct si470x_device *radio;
+ int retval = -ENOMEM;
+
+ /* private data allocation */
+ radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL);
+ if (!radio)
+ goto err_initial;
+
+ /* video device allocation */
+ radio->videodev = video_device_alloc();
+ if (!radio->videodev)
+ goto err_radio;
+
+ /* initial configuration */
+ memcpy(radio->videodev, &si470x_viddev_template,
+ sizeof(si470x_viddev_template));
+ radio->users = 0;
+ radio->usbdev = interface_to_usbdev(intf);
+ radio->intf = intf;
+ mutex_init(&radio->lock);
+ video_set_drvdata(radio->videodev, radio);
+
+ /* show some infos about the specific device */
+ retval = -EIO;
+ if (si470x_get_all_registers(radio) < 0)
+ goto err_all;
+ printk(KERN_INFO DRIVER_NAME ": DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",
+ radio->registers[DEVICEID], radio->registers[CHIPID]);
+
+ /* check if firmware is current */
+ if ((radio->registers[CHIPID] & CHIPID_FIRMWARE)
+ < RADIO_SW_VERSION_CURRENT) {
+ printk(KERN_WARNING DRIVER_NAME
+ ": This driver is known to work with "
+ "firmware version %hu,\n", RADIO_SW_VERSION_CURRENT);
+ printk(KERN_WARNING DRIVER_NAME
+ ": but the device has firmware version %hu.\n",
+ radio->registers[CHIPID] & CHIPID_FIRMWARE);
+ printk(KERN_WARNING DRIVER_NAME
+ ": If you have some trouble using this driver,\n");
+ printk(KERN_WARNING DRIVER_NAME
+ ": please report to V4L ML at "
+ "video4linux-list@redhat.com\n");
+ }
+
+ /* set initial frequency */
+ si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
+
+ /* rds buffer allocation */
+ radio->buf_size = rds_buf * 3;
+ radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
+ if (!radio->buffer)
+ goto err_all;
+
+ /* rds buffer configuration */
+ radio->wr_index = 0;
+ radio->rd_index = 0;
+ init_waitqueue_head(&radio->read_queue);
+
+ /* prepare rds work function */
+ INIT_DELAYED_WORK(&radio->work, si470x_work);
+
+ /* register video device */
+ if (video_register_device(radio->videodev, VFL_TYPE_RADIO, radio_nr)) {
+ printk(KERN_WARNING DRIVER_NAME
+ ": Could not register video device\n");
+ goto err_all;
+ }
+ usb_set_intfdata(intf, radio);
+
+ return 0;
+err_all:
+ video_device_release(radio->videodev);
+ kfree(radio->buffer);
+err_radio:
+ kfree(radio);
+err_initial:
+ return retval;
+}
+
+
+/*
+ * si470x_usb_driver_suspend - suspend the device
+ */
+static int si470x_usb_driver_suspend(struct usb_interface *intf,
+ pm_message_t message)
+{
+ struct si470x_device *radio = usb_get_intfdata(intf);
+
+ printk(KERN_INFO DRIVER_NAME ": suspending now...\n");
+
+ cancel_delayed_work_sync(&radio->work);
+
+ return 0;
+}
+
+
+/*
+ * si470x_usb_driver_resume - resume the device
+ */
+static int si470x_usb_driver_resume(struct usb_interface *intf)
+{
+ struct si470x_device *radio = usb_get_intfdata(intf);
+
+ printk(KERN_INFO DRIVER_NAME ": resuming now...\n");
+
+ mutex_lock(&radio->lock);
+ if (radio->users && radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS)
+ schedule_delayed_work(&radio->work,
+ msecs_to_jiffies(rds_poll_time));
+ mutex_unlock(&radio->lock);
+
+ return 0;
+}
+
+
+/*
+ * si470x_usb_driver_disconnect - disconnect the device
+ */
+static void si470x_usb_driver_disconnect(struct usb_interface *intf)
+{
+ struct si470x_device *radio = usb_get_intfdata(intf);
+
+ cancel_delayed_work_sync(&radio->work);
+ usb_set_intfdata(intf, NULL);
+ video_unregister_device(radio->videodev);
+ kfree(radio->buffer);
+ kfree(radio);
+}
+
+
+/*
+ * si470x_usb_driver - usb driver interface
+ */
+static struct usb_driver si470x_usb_driver = {
+ .name = DRIVER_NAME,
+ .probe = si470x_usb_driver_probe,
+ .disconnect = si470x_usb_driver_disconnect,
+ .suspend = si470x_usb_driver_suspend,
+ .resume = si470x_usb_driver_resume,
+ .id_table = si470x_usb_driver_id_table,
+ .supports_autosuspend = 1,
+};
+
+
/**************************************************************************
- * Module interface definitions and functions
+ * Module Interface
**************************************************************************/
/*
@@ -1418,7 +1554,7 @@ static int si470x_vidioc_s_frequency(struct file *file, void *priv,
*/
static int __init si470x_module_init(void)
{
- printk(KERN_INFO DRIVER_DESC "\n");
+ printk(KERN_INFO DRIVER_DESC ", Version " DRIVER_VERSION "\n");
return usb_register(&si470x_usb_driver);
}
@@ -1438,4 +1574,4 @@ module_exit(si470x_module_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
-MODULE_VERSION("1.0.2");
+MODULE_VERSION(DRIVER_VERSION);