]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/media/video/s2255drv.c
Merge mainline v2.6.27-rc2 tree into linux-omap tree
[linux-2.6-omap-h63xx.git] / drivers / media / video / s2255drv.c
index 6d5fbad95c29beadd62866322e24624eb2f9cc2f..b1d09d8e2b8572c35c88be39100cf35dbc7e21ba 100644 (file)
 #include <linux/mutex.h>
 #include <linux/videodev2.h>
 #include <linux/version.h>
+#include <linux/mm.h>
 #include <media/videobuf-vmalloc.h>
 #include <media/v4l2-common.h>
+#include <media/v4l2-ioctl.h>
 #include <linux/vmalloc.h>
 #include <linux/usb.h>
 
 /* maximum time for DSP to start responding after last FW word loaded(ms) */
 #define S2255_DSP_BOOTTIME      400
 /* maximum time to wait for firmware to load (ms) */
-#define S2255_LOAD_TIMEOUT      (5000+S2255_DSP_BOOTTIME)
+#define S2255_LOAD_TIMEOUT      (5000 + S2255_DSP_BOOTTIME)
 #define S2255_DEF_BUFS          16
 #define MAX_CHANNELS           4
 #define FRAME_MARKER           0x2255DA4AL
-#define MAX_PIPE_USBBLOCK      (40*1024)
-#define DEFAULT_PIPE_USBBLOCK  (16*1024)
+#define MAX_PIPE_USBBLOCK      (40 * 1024)
+#define DEFAULT_PIPE_USBBLOCK  (16 * 1024)
 #define MAX_CHANNELS           4
 #define MAX_PIPE_BUFFERS       1
 #define SYS_FRAMES             4
 /* maximum size is PAL full size plus room for the marker header(s) */
-#define SYS_FRAMES_MAXSIZE     (720*288*2*2 + 4096)
+#define SYS_FRAMES_MAXSIZE     (720 * 288 * 2 * 2 + 4096)
 #define DEF_USB_BLOCK          (4096)
 #define LINE_SZ_4CIFS_NTSC     640
 #define LINE_SZ_2CIFS_NTSC     640
@@ -168,7 +170,7 @@ struct s2255_bufferi {
 
 #define DEF_MODEI_NTSC_CONT    {FORMAT_NTSC, DEF_SCALE, DEF_COLOR,     \
                        DEF_FDEC, DEF_BRIGHT, DEF_CONTRAST, DEF_SATURATION, \
-                       DEF_HUE, 0, DEF_USB_BLOCK, 0 }
+                       DEF_HUE, 0, DEF_USB_BLOCK, 0}
 
 struct s2255_dmaqueue {
        struct list_head        active;
@@ -184,6 +186,7 @@ struct s2255_dmaqueue {
 #define S2255_FW_LOADED_DSPWAIT        1
 #define S2255_FW_SUCCESS       2
 #define S2255_FW_FAILED                3
+#define S2255_FW_DISCONNECTING  4
 
 struct s2255_fw {
        int                   fw_loaded;
@@ -263,7 +266,6 @@ struct s2255_buffer {
 
 struct s2255_fh {
        struct s2255_dev        *dev;
-       unsigned int            resources;
        const struct s2255_fmt  *fmt;
        unsigned int            width;
        unsigned int            height;
@@ -273,10 +275,9 @@ struct s2255_fh {
        /* mode below is the desired mode.
           mode in s2255_dev is the current mode that was last set */
        struct s2255_mode       mode;
+       int                     resources[MAX_CHANNELS];
 };
 
-#define S2255_MAX_USERS         1
-
 #define CUR_USB_FWVER  774     /* current cypress EEPROM firmware version */
 #define S2255_MAJOR_VERSION    1
 #define S2255_MINOR_VERSION    13
@@ -293,7 +294,7 @@ struct s2255_fh {
 #define PREFIX_SIZE            512
 
 /* Channels on box are in reverse order */
-static unsigned long G_chnmap[MAX_CHANNELS] = { 3, 2, 1, 0 };
+static unsigned long G_chnmap[MAX_CHANNELS] = {3, 2, 1, 0};
 
 static LIST_HEAD(s2255_devlist);
 
@@ -329,11 +330,11 @@ static unsigned int vid_limit = 16;       /* Video memory limit, in Mb */
 /* start video number */
 static int video_nr = -1;      /* /dev/videoN, -1 for autodetect */
 
-module_param(debug, int, 0);
+module_param(debug, int, 0644);
 MODULE_PARM_DESC(debug, "Debug level(0-100) default 0");
-module_param(vid_limit, int, 0);
+module_param(vid_limit, int, 0644);
 MODULE_PARM_DESC(vid_limit, "video memory limit(Mb)");
-module_param(video_nr, int, 0);
+module_param(video_nr, int, 0644);
 MODULE_PARM_DESC(video_nr, "start video minor(-1 default autodetect)");
 
 /* USB device table */
@@ -437,7 +438,10 @@ static int norm_minh(struct video_device *vdev)
 }
 
 
-/* converts 2255 planar format to yuyv or uyvy */
+/*
+ * TODO: fixme: move YUV reordering to hardware
+ * converts 2255 planar format to yuyv or uyvy
+ */
 static void planar422p_to_yuv_packed(const unsigned char *in,
                                     unsigned char *out,
                                     int width, int height,
@@ -469,10 +473,9 @@ static void s2255_timer(unsigned long user_data)
        dprintk(100, "s2255 timer\n");
        if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) {
                printk(KERN_ERR "s2255: can't submit urb\n");
-               if (data->fw) {
-                       release_firmware(data->fw);
-                       data->fw = NULL;
-               }
+               atomic_set(&data->fw_state, S2255_FW_FAILED);
+               /* wake up anything waiting for the firmware */
+               wake_up(&data->wait_fw);
                return;
        }
 }
@@ -502,13 +505,18 @@ static void s2255_fwchunk_complete(struct urb *urb)
        struct usb_device *udev = urb->dev;
        int len;
        dprintk(100, "udev %p urb %p", udev, urb);
-
        if (urb->status) {
                dev_err(&udev->dev, "URB failed with status %d", urb->status);
+               atomic_set(&data->fw_state, S2255_FW_FAILED);
+               /* wake up anything waiting for the firmware */
+               wake_up(&data->wait_fw);
                return;
        }
        if (data->fw_urb == NULL) {
-               dev_err(&udev->dev, "early disconncect\n");
+               dev_err(&udev->dev, "s2255 disconnected\n");
+               atomic_set(&data->fw_state, S2255_FW_FAILED);
+               /* wake up anything waiting for the firmware */
+               wake_up(&data->wait_fw);
                return;
        }
 #define CHUNK_SIZE 512
@@ -682,7 +690,7 @@ static int buffer_setup(struct videobuf_queue *vq, unsigned int *count,
        if (0 == *count)
                *count = S2255_DEF_BUFS;
 
-       while (*size * *count > vid_limit * 1024 * 1024)
+       while (*size * (*count) > vid_limit * 1024 * 1024)
                (*count)--;
 
        return 0;
@@ -782,19 +790,29 @@ static int res_get(struct s2255_dev *dev, struct s2255_fh *fh)
        }
        /* it's free, grab it */
        dev->resources[fh->channel] = 1;
-       dprintk(1, "res: get\n");
+       fh->resources[fh->channel] = 1;
+       dprintk(1, "s2255: res: get\n");
        mutex_unlock(&dev->lock);
        return 1;
 }
 
 static int res_locked(struct s2255_dev *dev, struct s2255_fh *fh)
 {
-       return (dev->resources[fh->channel]);
+       return dev->resources[fh->channel];
+}
+
+static int res_check(struct s2255_fh *fh)
+{
+       return fh->resources[fh->channel];
 }
 
+
 static void res_free(struct s2255_dev *dev, struct s2255_fh *fh)
 {
+       mutex_lock(&dev->lock);
        dev->resources[fh->channel] = 0;
+       fh->resources[fh->channel] = 0;
+       mutex_unlock(&dev->lock);
        dprintk(1, "res: put\n");
 }
 
@@ -806,7 +824,8 @@ static int vidioc_querycap(struct file *file, void *priv,
        struct s2255_dev *dev = fh->dev;
        strlcpy(cap->driver, "s2255", sizeof(cap->driver));
        strlcpy(cap->card, "s2255", sizeof(cap->card));
-       strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info));
+       strlcpy(cap->bus_info, dev_name(&dev->udev->dev),
+               sizeof(cap->bus_info));
        cap->version = S2255_VERSION;
        cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
        return 0;
@@ -839,8 +858,7 @@ static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
        f->fmt.pix.pixelformat = fh->fmt->fourcc;
        f->fmt.pix.bytesperline = f->fmt.pix.width * (fh->fmt->depth >> 3);
        f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
-
-       return (0);
+       return 0;
 }
 
 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
@@ -961,7 +979,7 @@ static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
        ret = vidioc_try_fmt_vid_cap(file, fh, f);
 
        if (ret < 0)
-               return (ret);
+               return ret;
 
        fmt = format_by_fourcc(f->fmt.pix.pixelformat);
 
@@ -1225,7 +1243,7 @@ static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
        }
 
        if (!res_get(dev, fh)) {
-               dev_err(&dev->udev->dev, "res get busy\n");
+               dev_err(&dev->udev->dev, "s2255: stream busy\n");
                return -EBUSY;
        }
 
@@ -1281,8 +1299,10 @@ static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
        }
        s2255_stop_acquire(dev, fh->channel);
        res = videobuf_streamoff(&fh->vb_vidq);
+       if (res < 0)
+               return res;
        res_free(dev, fh);
-       return res;
+       return 0;
 }
 
 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *i)
@@ -1336,7 +1356,7 @@ static int vidioc_enum_input(struct file *file, void *priv,
        inp->type = V4L2_INPUT_TYPE_CAMERA;
        inp->std = S2255_NORMS;
        strlcpy(inp->name, "Camera", sizeof(inp->name));
-       return (0);
+       return 0;
 }
 
 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
@@ -1360,7 +1380,7 @@ static int vidioc_queryctrl(struct file *file, void *priv,
        for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++)
                if (qc->id && qc->id == s2255_qctrl[i].id) {
                        memcpy(qc, &(s2255_qctrl[i]), sizeof(*qc));
-                       return (0);
+                       return 0;
                }
 
        dprintk(4, "query_ctrl -EINVAL %d\n", qc->id);
@@ -1375,7 +1395,7 @@ static int vidioc_g_ctrl(struct file *file, void *priv,
        for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++)
                if (ctrl->id == s2255_qctrl[i].id) {
                        ctrl->value = qctl_regs[i];
-                       return (0);
+                       return 0;
                }
        dprintk(4, "g_ctrl -EINVAL\n");
 
@@ -1395,7 +1415,7 @@ static int vidioc_s_ctrl(struct file *file, void *priv,
                if (ctrl->id == s2255_qctrl[i].id) {
                        if (ctrl->value < s2255_qctrl[i].minimum ||
                            ctrl->value > s2255_qctrl[i].maximum)
-                               return (-ERANGE);
+                               return -ERANGE;
 
                        qctl_regs[i] = ctrl->value;
                        /* update the mode to the corresponding value */
@@ -1455,12 +1475,7 @@ static int s2255_open(struct inode *inode, struct file *file)
        mutex_lock(&dev->open_lock);
 
        dev->users[cur_channel]++;
-       if (dev->users[cur_channel] > S2255_MAX_USERS) {
-               dev->users[cur_channel]--;
-               mutex_unlock(&dev->open_lock);
-               printk(KERN_INFO "s2255drv: too many open handles!\n");
-               return -EBUSY;
-       }
+       dprintk(4, "s2255: open_handles %d\n", dev->users[cur_channel]);
 
        if (atomic_read(&dev->fw_data->fw_state) == S2255_FW_FAILED) {
                err("2255 firmware load failed. retrying.\n");
@@ -1471,7 +1486,8 @@ static int s2255_open(struct inode *inode, struct file *file)
                                   msecs_to_jiffies(S2255_LOAD_TIMEOUT));
                if (atomic_read(&dev->fw_data->fw_state)
                    != S2255_FW_SUCCESS) {
-                       printk(KERN_INFO "2255 FW load failed after 2 tries\n");
+                       printk(KERN_INFO "2255 FW load failed.\n");
+                       dev->users[cur_channel]--;
                        mutex_unlock(&dev->open_lock);
                        return -EFAULT;
                }
@@ -1487,6 +1503,7 @@ static int s2255_open(struct inode *inode, struct file *file)
                    != S2255_FW_SUCCESS) {
                        printk(KERN_INFO "2255 firmware not loaded"
                               "try again\n");
+                       dev->users[cur_channel]--;
                        mutex_unlock(&dev->open_lock);
                        return -EBUSY;
                }
@@ -1495,6 +1512,7 @@ static int s2255_open(struct inode *inode, struct file *file)
        /* allocate + initialize per filehandle data */
        fh = kzalloc(sizeof(*fh), GFP_KERNEL);
        if (NULL == fh) {
+               dev->users[cur_channel]--;
                mutex_unlock(&dev->open_lock);
                return -ENOMEM;
        }
@@ -1554,17 +1572,29 @@ static void s2255_destroy(struct kref *kref)
                printk(KERN_ERR "s2255drv: kref problem\n");
                return;
        }
+
+       /*
+        * Wake up any firmware load waiting (only done in .open,
+        * which holds the open_lock mutex)
+        */
+       atomic_set(&dev->fw_data->fw_state, S2255_FW_DISCONNECTING);
+       wake_up(&dev->fw_data->wait_fw);
+
        /* prevent s2255_disconnect from racing s2255_open */
        mutex_lock(&dev->open_lock);
        s2255_exit_v4l(dev);
-       /* device unregistered so no longer possible to open. open_mutex
-          can be unlocked */
+       /*
+        * device unregistered so no longer possible to open. open_mutex
+        *  can be unlocked and timers deleted afterwards.
+        */
        mutex_unlock(&dev->open_lock);
 
        /* board shutdown stops the read pipe if it is running */
        s2255_board_shutdown(dev);
 
        /* make sure firmware still not trying to load */
+       del_timer(&dev->timer);  /* only started in .probe and .open */
+
        if (dev->fw_data->fw_urb) {
                dprintk(2, "kill fw_urb\n");
                usb_kill_urb(dev->fw_data->fw_urb);
@@ -1572,24 +1602,18 @@ static void s2255_destroy(struct kref *kref)
                dev->fw_data->fw_urb = NULL;
        }
 
-       /* make sure we aren't waiting for the DSP */
-       if (atomic_read(&dev->fw_data->fw_state) == S2255_FW_LOADED_DSPWAIT) {
-               /* if we are, wait for the wakeup for fw_success or timeout */
-               wait_event_timeout(dev->fw_data->wait_fw,
-                                  (atomic_read(&dev->fw_data->fw_state)
-                                  == S2255_FW_SUCCESS),
-                                  msecs_to_jiffies(S2255_LOAD_TIMEOUT));
-       }
+       /*
+        * delete the dsp_wait timer, which sets the firmware
+        * state on completion.  This is done before fw_data
+        * is freed below.
+        */
 
-       if (dev->fw_data) {
-               kfree(dev->fw_data->pfw_data);
-               kfree(dev->fw_data);
-       }
+       del_timer(&dev->fw_data->dsp_wait); /* only started in .open */
 
-       if (dev->fw_data->fw) {
+       if (dev->fw_data->fw)
                release_firmware(dev->fw_data->fw);
-               dev->fw_data->fw = NULL;
-       }
+       kfree(dev->fw_data->pfw_data);
+       kfree(dev->fw_data);
 
        usb_put_dev(dev->udev);
        dprintk(1, "%s", __func__);
@@ -1606,17 +1630,23 @@ static int s2255_close(struct inode *inode, struct file *file)
 
        mutex_lock(&dev->open_lock);
 
-       if (dev->b_acquire[fh->channel])
-               s2255_stop_acquire(dev, fh->channel);
-       res_free(dev, fh);
+       /* turn off stream */
+       if (res_check(fh)) {
+               if (dev->b_acquire[fh->channel])
+                       s2255_stop_acquire(dev, fh->channel);
+               videobuf_streamoff(&fh->vb_vidq);
+               res_free(dev, fh);
+       }
+
        videobuf_mmap_free(&fh->vb_vidq);
-       kfree(fh);
        dev->users[fh->channel]--;
+
        mutex_unlock(&dev->open_lock);
 
        kref_put(&dev->kref, s2255_destroy);
        dprintk(1, "s2255: close called (minor=%d, users=%d)\n",
                minor, dev->users[fh->channel]);
+       kfree(fh);
        return 0;
 }
 
@@ -1649,12 +1679,7 @@ static const struct file_operations s2255_fops_v4l = {
        .llseek = no_llseek,
 };
 
-static struct video_device template = {
-       .name = "s2255v",
-       .type = VID_TYPE_CAPTURE,
-       .fops = &s2255_fops_v4l,
-       .minor = -1,
-       .release = video_device_release,
+static const struct v4l2_ioctl_ops s2255_ioctl_ops = {
        .vidioc_querycap = vidioc_querycap,
        .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
        .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
@@ -1676,6 +1701,14 @@ static struct video_device template = {
 #ifdef CONFIG_VIDEO_V4L1_COMPAT
        .vidiocgmbuf = vidioc_cgmbuf,
 #endif
+};
+
+static struct video_device template = {
+       .name = "s2255v",
+       .fops = &s2255_fops_v4l,
+       .ioctl_ops = &s2255_ioctl_ops,
+       .minor = -1,
+       .release = video_device_release,
        .tvnorms = S2255_NORMS,
        .current_norm = V4L2_STD_NTSC_M,
 };
@@ -1697,7 +1730,7 @@ static int s2255_probe_v4l(struct s2255_dev *dev)
                /* register 4 video devices */
                dev->vdev[i] = video_device_alloc();
                memcpy(dev->vdev[i], &template, sizeof(struct video_device));
-               dev->vdev[i]->dev = &dev->interface->dev;
+               dev->vdev[i]->parent = &dev->interface->dev;
                if (video_nr == -1)
                        ret = video_register_device(dev->vdev[i],
                                                    VFL_TYPE_GRABBER,