]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/media/video/gspca/gspca.c
V4L/DVB (9712): gspca:Subdriver ov534 added.
[linux-2.6-omap-h63xx.git] / drivers / media / video / gspca / gspca.c
index 342a0f39e5d50b3f71226fb2ea408cb970d3b291..0d233c7248750e6d2ee2e336c7eb32ea4603daef 100644 (file)
@@ -21,6 +21,7 @@
 #define MODULE_NAME "gspca"
 
 #include <linux/init.h>
+#include <linux/version.h>
 #include <linux/fs.h>
 #include <linux/vmalloc.h>
 #include <linux/sched.h>
@@ -29,7 +30,6 @@
 #include <linux/string.h>
 #include <linux/pagemap.h>
 #include <linux/io.h>
-#include <linux/kref.h>
 #include <asm/page.h>
 #include <linux/uaccess.h>
 #include <linux/jiffies.h>
@@ -44,7 +44,7 @@ MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
 MODULE_DESCRIPTION("GSPCA USB Camera Driver");
 MODULE_LICENSE("GPL");
 
-#define DRIVER_VERSION_NUMBER  KERNEL_VERSION(2, 3, 0)
+#define DRIVER_VERSION_NUMBER  KERNEL_VERSION(2, 4, 0)
 
 static int video_nr = -1;
 
@@ -149,8 +149,11 @@ static void fill_frame(struct gspca_dev *gspca_dev,
 
                /* check the packet status and length */
                len = urb->iso_frame_desc[i].actual_length;
-               if (len == 0)
+               if (len == 0) {
+                       if (gspca_dev->empty_packet == 0)
+                               gspca_dev->empty_packet = 1;
                        continue;
+               }
                st = urb->iso_frame_desc[i].status;
                if (st) {
                        PDEBUG(D_ERR,
@@ -169,7 +172,6 @@ static void fill_frame(struct gspca_dev *gspca_dev,
        }
 
        /* resubmit the URB */
-       urb->status = 0;
        st = usb_submit_urb(urb, GFP_ATOMIC);
        if (st < 0)
                PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
@@ -199,11 +201,18 @@ static void bulk_irq(struct urb *urb
 {
        struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
        struct gspca_frame *frame;
+       int st;
 
        PDEBUG(D_PACK, "bulk irq");
        if (!gspca_dev->streaming)
                return;
-       if (urb->status != 0 && urb->status != -ECONNRESET) {
+       switch (urb->status) {
+       case 0:
+               break;
+       case -ECONNRESET:
+               urb->status = 0;
+               break;
+       default:
 #ifdef CONFIG_PM
                if (!gspca_dev->frozen)
 #endif
@@ -222,6 +231,13 @@ static void bulk_irq(struct urb *urb
                                        urb->transfer_buffer,
                                        urb->actual_length);
        }
+
+       /* resubmit the URB */
+       if (gspca_dev->cam.bulk_nurbs != 0) {
+               st = usb_submit_urb(urb, GFP_ATOMIC);
+               if (st < 0)
+                       PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
+       }
 }
 
 /*
@@ -237,7 +253,7 @@ static void bulk_irq(struct urb *urb
  * On LAST_PACKET, a new frame is returned.
  */
 struct gspca_frame *gspca_frame_add(struct gspca_dev *gspca_dev,
-                                   int packet_type,
+                                   enum gspca_packet_type packet_type,
                                    struct gspca_frame *frame,
                                    const __u8 *data,
                                    int len)
@@ -317,7 +333,6 @@ static void *rvmalloc(unsigned long size)
        void *mem;
        unsigned long adr;
 
-/*     size = PAGE_ALIGN(size);        (already done) */
        mem = vmalloc_32(size);
        if (mem != NULL) {
                adr = (unsigned long) mem;
@@ -404,7 +419,7 @@ static void destroy_urbs(struct gspca_dev *gspca_dev)
        unsigned int i;
 
        PDEBUG(D_STREAM, "kill transfer");
-       for (i = 0; i < MAX_NURBS; ++i) {
+       for (i = 0; i < MAX_NURBS; i++) {
                urb = gspca_dev->urb[i];
                if (urb == NULL)
                        break;
@@ -461,25 +476,34 @@ static struct usb_host_endpoint *get_ep(struct gspca_dev *gspca_dev)
        intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
        ep = NULL;
        i = gspca_dev->alt;                     /* previous alt setting */
+
+       /* try isoc */
        while (--i > 0) {                       /* alt 0 is unusable */
                ep = alt_xfer(&intf->altsetting[i],
                                gspca_dev->cam.epaddr,
-                               gspca_dev->bulk
-                                       ? USB_ENDPOINT_XFER_BULK
-                                       : USB_ENDPOINT_XFER_ISOC);
+                               USB_ENDPOINT_XFER_ISOC);
                if (ep)
                        break;
        }
+
+       /* if no isoc, try bulk */
        if (ep == NULL) {
-               err("no transfer endpoint found");
-               return NULL;
+               ep = alt_xfer(&intf->altsetting[0],
+                               gspca_dev->cam.epaddr,
+                               USB_ENDPOINT_XFER_BULK);
+               if (ep == NULL) {
+                       err("no transfer endpoint found");
+                       return NULL;
+               }
        }
        PDEBUG(D_STREAM, "use alt %d ep 0x%02x",
                        i, ep->desc.bEndpointAddress);
-       ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
-       if (ret < 0) {
-               err("set interface err %d", ret);
-               return NULL;
+       if (i > 0) {
+               ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
+               if (ret < 0) {
+                       err("set interface err %d", ret);
+                       return NULL;
+               }
        }
        gspca_dev->alt = i;             /* memorize the current alt setting */
        return ep;
@@ -497,9 +521,10 @@ static int create_urbs(struct gspca_dev *gspca_dev,
        /* calculate the packet size and the number of packets */
        psize = le16_to_cpu(ep->desc.wMaxPacketSize);
 
-       /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
-       psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
-       if (!gspca_dev->bulk) {
+       if (gspca_dev->alt != 0) {              /* isoc */
+
+               /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
+               psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
                npkt = ISO_MAX_SIZE / psize;
                if (npkt > ISO_MAX_PKT)
                        npkt = ISO_MAX_PKT;
@@ -508,11 +533,16 @@ static int create_urbs(struct gspca_dev *gspca_dev,
                        "isoc %d pkts size %d = bsize:%d",
                        npkt, psize, bsize);
                nurbs = DEF_NURBS;
-       } else {
+       } else {                                /* bulk */
                npkt = 0;
-               bsize = psize;
+               bsize = gspca_dev->cam.bulk_size;
+               if (bsize == 0)
+                       bsize = psize;
                PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
-               nurbs = 1;
+               if (gspca_dev->cam.bulk_nurbs != 0)
+                       nurbs = gspca_dev->cam.bulk_nurbs;
+               else
+                       nurbs = 1;
        }
 
        gspca_dev->nurbs = nurbs;
@@ -585,6 +615,12 @@ static int gspca_init_transfer(struct gspca_dev *gspca_dev)
                if (ret < 0)
                        goto out;
 
+               /* clear the bulk endpoint */
+               if (gspca_dev->alt == 0)        /* if bulk transfer */
+                       usb_clear_halt(gspca_dev->dev,
+                                       usb_rcvintpipe(gspca_dev->dev,
+                                                gspca_dev->cam.epaddr));
+
                /* start the cam */
                ret = gspca_dev->sd_desc->start(gspca_dev);
                if (ret < 0) {
@@ -594,8 +630,8 @@ static int gspca_init_transfer(struct gspca_dev *gspca_dev)
                gspca_dev->streaming = 1;
                atomic_set(&gspca_dev->nevent, 0);
 
-               /* bulk transfers are started by the subdriver */
-               if (gspca_dev->bulk)
+               /* some bulk transfers are started by the subdriver */
+               if (gspca_dev->alt == 0 && gspca_dev->cam.bulk_nurbs == 0)
                        break;
 
                /* submit the URBs */
@@ -634,15 +670,14 @@ static void gspca_stream_off(struct gspca_dev *gspca_dev)
 {
        gspca_dev->streaming = 0;
        atomic_set(&gspca_dev->nevent, 0);
-       if (gspca_dev->present) {
-               if (gspca_dev->sd_desc->stopN)
-                       gspca_dev->sd_desc->stopN(gspca_dev);
-               destroy_urbs(gspca_dev);
-               gspca_set_alt0(gspca_dev);
-               if (gspca_dev->sd_desc->stop0)
-                       gspca_dev->sd_desc->stop0(gspca_dev);
-               PDEBUG(D_STREAM, "stream off OK");
-       }
+       if (gspca_dev->present
+           && gspca_dev->sd_desc->stopN)
+               gspca_dev->sd_desc->stopN(gspca_dev);
+       destroy_urbs(gspca_dev);
+       gspca_set_alt0(gspca_dev);
+       if (gspca_dev->sd_desc->stop0)
+               gspca_dev->sd_desc->stop0(gspca_dev);
+       PDEBUG(D_STREAM, "stream off OK");
 }
 
 static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
@@ -716,7 +751,7 @@ static int vidioc_enum_fmt_vid_cap(struct file *file, void  *priv,
                        if (fmtdesc->index == index)
                                break;          /* new format */
                        index++;
-                       if (index >= sizeof fmt_tb / sizeof fmt_tb[0])
+                       if (index >= ARRAY_SIZE(fmt_tb))
                                return -EINVAL;
                }
        }
@@ -835,11 +870,11 @@ out:
        return ret;
 }
 
-static void gspca_delete(struct kref *kref)
+static void gspca_release(struct video_device *vfd)
 {
-       struct gspca_dev *gspca_dev = container_of(kref, struct gspca_dev, kref);
+       struct gspca_dev *gspca_dev = container_of(vfd, struct gspca_dev, vdev);
 
-       PDEBUG(D_STREAM, "device deleted");
+       PDEBUG(D_STREAM, "device released");
 
        kfree(gspca_dev->usb_buf);
        kfree(gspca_dev);
@@ -863,10 +898,14 @@ static int dev_open(struct inode *inode, struct file *file)
                ret = -EBUSY;
                goto out;
        }
-       gspca_dev->users++;
 
-       /* one more user */
-       kref_get(&gspca_dev->kref);
+       /* protect the subdriver against rmmod */
+       if (!try_module_get(gspca_dev->module)) {
+               ret = -ENODEV;
+               goto out;
+       }
+
+       gspca_dev->users++;
 
        file->private_data = gspca_dev;
 #ifdef GSPCA_DEBUG
@@ -909,12 +948,11 @@ static int dev_close(struct inode *inode, struct file *file)
                gspca_dev->memory = GSPCA_MEMORY_NO;
        }
        file->private_data = NULL;
+       module_put(gspca_dev->module);
        mutex_unlock(&gspca_dev->queue_lock);
 
        PDEBUG(D_STREAM, "close done");
 
-       kref_put(&gspca_dev->kref, gspca_delete);
-
        return 0;
 }
 
@@ -925,7 +963,6 @@ static int vidioc_querycap(struct file *file, void  *priv,
 
        memset(cap, 0, sizeof *cap);
        strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver);
-/*     strncpy(cap->card, gspca_dev->cam.dev_name, sizeof cap->card); */
        if (gspca_dev->dev->product != NULL) {
                strncpy(cap->card, gspca_dev->dev->product,
                        sizeof cap->card);
@@ -1246,10 +1283,10 @@ static int vidioc_s_jpegcomp(struct file *file, void *priv,
        struct gspca_dev *gspca_dev = priv;
        int ret;
 
-       if (mutex_lock_interruptible(&gspca_dev->usb_lock))
-               return -ERESTARTSYS;
        if (!gspca_dev->sd_desc->set_jcomp)
                return -EINVAL;
+       if (mutex_lock_interruptible(&gspca_dev->usb_lock))
+               return -ERESTARTSYS;
        ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
        mutex_unlock(&gspca_dev->usb_lock);
        return ret;
@@ -1559,7 +1596,6 @@ static int vidioc_qbuf(struct file *file, void *priv,
        }
 
        frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
-/*     frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE; */
 
        if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
                frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
@@ -1738,11 +1774,6 @@ out:
        return ret;
 }
 
-static void dev_release(struct video_device *vfd)
-{
-       /* nothing */
-}
-
 static struct file_operations dev_fops = {
        .owner = THIS_MODULE,
        .open = dev_open,
@@ -1790,7 +1821,7 @@ static struct video_device gspca_template = {
        .name = "gspca main driver",
        .fops = &dev_fops,
        .ioctl_ops = &dev_ioctl_ops,
-       .release = dev_release,         /* mandatory */
+       .release = gspca_release,
        .minor = -1,
 };
 
@@ -1824,23 +1855,22 @@ int gspca_dev_probe(struct usb_interface *intf,
        if (dev_size < sizeof *gspca_dev)
                dev_size = sizeof *gspca_dev;
        gspca_dev = kzalloc(dev_size, GFP_KERNEL);
-       if (gspca_dev == NULL) {
+       if (!gspca_dev) {
                err("couldn't kzalloc gspca struct");
-               return -EIO;
+               return -ENOMEM;
        }
-       kref_init(&gspca_dev->kref);
        gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
        if (!gspca_dev->usb_buf) {
                err("out of memory");
-               ret = -EIO;
+               ret = -ENOMEM;
                goto out;
        }
        gspca_dev->dev = dev;
        gspca_dev->iface = interface->bInterfaceNumber;
        gspca_dev->nbalt = intf->num_altsetting;
        gspca_dev->sd_desc = sd_desc;
-/*     gspca_dev->users = 0;                   (done by kzalloc) */
        gspca_dev->nbufread = 2;
+       gspca_dev->empty_packet = -1;   /* don't check the empty packets */
 
        /* configure the subdriver and initialize the USB device */
        ret = gspca_dev->sd_desc->config(gspca_dev, id);
@@ -1860,11 +1890,10 @@ int gspca_dev_probe(struct usb_interface *intf,
        init_waitqueue_head(&gspca_dev->wq);
 
        /* init video stuff */
-       memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
-       gspca_dev->vdev.parent = &dev->dev;
-       memcpy(&gspca_dev->fops, &dev_fops, sizeof gspca_dev->fops);
-       gspca_dev->vdev.fops = &gspca_dev->fops;
-       gspca_dev->fops.owner = module;         /* module protection */
+       gspca_dev->vdev = video_device_alloc();
+       memcpy(gspca_dev->vdev, &gspca_template, sizeof gspca_template);
+       gspca_dev->vdev->parent = &dev->dev;
+       gspca_dev->module = module;
        gspca_dev->present = 1;
        ret = video_register_device(&gspca_dev->vdev,
                                  VFL_TYPE_GRABBER,
@@ -1878,7 +1907,8 @@ int gspca_dev_probe(struct usb_interface *intf,
        PDEBUG(D_PROBE, "probe ok");
        return 0;
 out:
-       kref_put(&gspca_dev->kref, gspca_delete);
+       kfree(gspca_dev->usb_buf);
+       kfree(gspca_dev);
        return ret;
 }
 EXPORT_SYMBOL(gspca_dev_probe);
@@ -1895,19 +1925,10 @@ void gspca_disconnect(struct usb_interface *intf)
 
        usb_set_intfdata(intf, NULL);
 
-       gspca_dev->present = 0;
-       mutex_lock(&gspca_dev->queue_lock);
-       mutex_lock(&gspca_dev->usb_lock);
-       gspca_dev->streaming = 0;
-       destroy_urbs(gspca_dev);
-       mutex_unlock(&gspca_dev->usb_lock);
-       mutex_unlock(&gspca_dev->queue_lock);
-
-/* We don't want people trying to open up the device */
+       /* release the device */
+       /* (this will call gspca_release() immediatly or on last close) */
        video_unregister_device(&gspca_dev->vdev);
 
-       kref_put(&gspca_dev->kref, gspca_delete);
-
        PDEBUG(D_PROBE, "disconnect complete");
 }
 EXPORT_SYMBOL(gspca_disconnect);
@@ -1984,7 +2005,7 @@ int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
           desired lumination fast (with the risc of a slight overshoot) */
        steps = abs(desired_avg_lum - avg_lum) / deadzone;
 
-       PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d\n",
+       PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d",
                avg_lum, desired_avg_lum, steps);
 
        for (i = 0; i < steps; i++) {