]> err.no Git - linux-2.6/blobdiff - drivers/media/radio/radio-si470x.c
Merge branch 'for-linus' of master.kernel.org:/home/rmk/linux-2.6-arm
[linux-2.6] / drivers / media / radio / radio-si470x.c
index c72a9e7ad8856c5fd19306093e9cdc118adb28b4..77354ca6e8e928005cfcf631ed97d698bfd9b1ab 100644 (file)
  *             - 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
+ *             - unplugging fixed
  *
  * ToDo:
  * - add seeking support
 /* driver definitions */
 #define DRIVER_AUTHOR "Tobias Lorenz <tobias.lorenz@gmx.net>"
 #define DRIVER_NAME "radio-si470x"
-#define DRIVER_KERNEL_VERSION KERNEL_VERSION(1, 0, 6)
+#define DRIVER_KERNEL_VERSION KERNEL_VERSION(1, 0, 7)
 #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"
+#define DRIVER_VERSION "1.0.7"
 
 
 /* kernel includes */
@@ -415,10 +420,12 @@ MODULE_PARM_DESC(rds_poll_time, "RDS poll time (ms): *40*");
 struct si470x_device {
        /* reference to USB and video device */
        struct usb_device *usbdev;
+       struct usb_interface *intf;
        struct video_device *videodev;
 
        /* driver management */
        unsigned int users;
+       unsigned char disconnected;
 
        /* Silabs internal registers (0..15) */
        unsigned short registers[RADIO_REGISTER_NUM];
@@ -434,6 +441,12 @@ struct si470x_device {
 };
 
 
+/*
+ * Lock to prevent kfree of data before all users have releases the device.
+ */
+static DEFINE_MUTEX(open_close_lock);
+
+
 /*
  * The frequency is set in units of 62.5 Hz when using V4L2_TUNER_CAP_LOW,
  * 62.5 kHz otherwise.
@@ -572,7 +585,7 @@ static int si470x_get_rds_registers(struct si470x_device *radio)
                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: "
+              printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: "
                        "return size differs: %d != %zu\n", size, sizeof(buf));
        if (retval < 0)
                printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: "
@@ -762,9 +775,17 @@ static int si470x_stop(struct si470x_device *radio)
  */
 static int si470x_rds_on(struct si470x_device *radio)
 {
+       int retval;
+
        /* sysconfig 1 */
+       mutex_lock(&radio->lock);
        radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDS;
-       return si470x_set_register(radio, SYSCONFIG1);
+       retval = si470x_set_register(radio, SYSCONFIG1);
+       if (retval < 0)
+               radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
+       mutex_unlock(&radio->lock);
+
+       return retval;
 }
 
 
@@ -862,6 +883,8 @@ static void si470x_work(struct work_struct *work)
        struct si470x_device *radio = container_of(work, struct si470x_device,
                work.work);
 
+       if (radio->disconnected)
+              return;
        if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
                return;
 
@@ -961,10 +984,22 @@ static unsigned int si470x_fops_poll(struct file *file,
 static int si470x_fops_open(struct inode *inode, struct file *file)
 {
        struct si470x_device *radio = video_get_drvdata(video_devdata(file));
+       int retval;
 
        radio->users++;
-       if (radio->users == 1)
-               return si470x_start(radio);
+
+       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)
+                       usb_autopm_put_interface(radio->intf);
+               return retval;
+       }
 
        return 0;
 }
@@ -976,22 +1011,34 @@ static int si470x_fops_open(struct inode *inode, struct file *file)
 static int si470x_fops_release(struct inode *inode, struct file *file)
 {
        struct si470x_device *radio = video_get_drvdata(video_devdata(file));
+       int retval = 0;
 
        if (!radio)
                return -ENODEV;
 
+       mutex_lock(&open_close_lock);
        radio->users--;
        if (radio->users == 0) {
+              if (radio->disconnected) {
+                      video_unregister_device(radio->videodev);
+                      kfree(radio->buffer);
+                      kfree(radio);
+                      goto done;
+              }
+
                /* stop rds reception */
                cancel_delayed_work_sync(&radio->work);
 
                /* cancel read processes */
                wake_up_interruptible(&radio->read_queue);
 
-               return si470x_stop(radio);
+               retval = si470x_stop(radio);
+               usb_autopm_put_interface(radio->intf);
        }
 
-       return 0;
+done:
+       mutex_unlock(&open_close_lock);
+       return retval;
 }
 
 
@@ -1004,7 +1051,9 @@ static const struct file_operations si470x_fops = {
        .read           = si470x_fops_read,
        .poll           = si470x_fops_poll,
        .ioctl          = video_ioctl2,
+#ifdef CONFIG_COMPAT
        .compat_ioctl   = v4l_compat_ioctl32,
+#endif
        .open           = si470x_fops_open,
        .release        = si470x_fops_release,
 };
@@ -1129,6 +1178,9 @@ static int si470x_vidioc_g_ctrl(struct file *file, void *priv,
 {
        struct si470x_device *radio = video_get_drvdata(video_devdata(file));
 
+       if (radio->disconnected)
+              return -EIO;
+
        switch (ctrl->id) {
        case V4L2_CID_AUDIO_VOLUME:
                ctrl->value = radio->registers[SYSCONFIG2] &
@@ -1153,6 +1205,9 @@ static int si470x_vidioc_s_ctrl(struct file *file, void *priv,
        struct si470x_device *radio = video_get_drvdata(video_devdata(file));
        int retval;
 
+       if (radio->disconnected)
+              return -EIO;
+
        switch (ctrl->id) {
        case V4L2_CID_AUDIO_VOLUME:
                radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_VOLUME;
@@ -1215,6 +1270,8 @@ static int si470x_vidioc_g_tuner(struct file *file, void *priv,
        struct si470x_device *radio = video_get_drvdata(video_devdata(file));
        int retval;
 
+       if (radio->disconnected)
+              return -EIO;
        if (tuner->index > 0)
                return -EINVAL;
 
@@ -1271,6 +1328,8 @@ static int si470x_vidioc_s_tuner(struct file *file, void *priv,
        struct si470x_device *radio = video_get_drvdata(video_devdata(file));
        int retval;
 
+       if (radio->disconnected)
+              return -EIO;
        if (tuner->index > 0)
                return -EINVAL;
 
@@ -1296,6 +1355,9 @@ static int si470x_vidioc_g_frequency(struct file *file, void *priv,
 {
        struct si470x_device *radio = video_get_drvdata(video_devdata(file));
 
+       if (radio->disconnected)
+              return -EIO;
+
        freq->type = V4L2_TUNER_RADIO;
        freq->frequency = si470x_get_freq(radio);
 
@@ -1312,6 +1374,8 @@ static int si470x_vidioc_s_frequency(struct file *file, void *priv,
        struct si470x_device *radio = video_get_drvdata(video_devdata(file));
        int retval;
 
+       if (radio->disconnected)
+              return -EIO;
        if (freq->type != V4L2_TUNER_RADIO)
                return -EINVAL;
 
@@ -1377,6 +1441,7 @@ static int si470x_usb_driver_probe(struct usb_interface *intf,
                        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);
 
@@ -1439,6 +1504,41 @@ err_initial:
 }
 
 
+/*
+ * 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
  */
@@ -1446,11 +1546,16 @@ static void si470x_usb_driver_disconnect(struct usb_interface *intf)
 {
        struct si470x_device *radio = usb_get_intfdata(intf);
 
+       mutex_lock(&open_close_lock);
+       radio->disconnected = 1;
        cancel_delayed_work_sync(&radio->work);
        usb_set_intfdata(intf, NULL);
-       video_unregister_device(radio->videodev);
-       kfree(radio->buffer);
-       kfree(radio);
+       if (radio->users == 0) {
+              video_unregister_device(radio->videodev);
+              kfree(radio->buffer);
+              kfree(radio);
+       }
+       mutex_unlock(&open_close_lock);
 }
 
 
@@ -1458,10 +1563,13 @@ static void si470x_usb_driver_disconnect(struct usb_interface *intf)
  * 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,
-       .id_table       = si470x_usb_driver_id_table,
+       .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,
 };