Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Zoran 364xx based USB webcam module version 0.73
0004  *
0005  * Allows you to use your USB webcam with V4L2 applications
0006  * This is still in heavy development !
0007  *
0008  * Copyright (C) 2004  Antoine Jacquet <royale@zerezo.com>
0009  * http://royale.zerezo.com/zr364xx/
0010  *
0011  * Heavily inspired by usb-skeleton.c, vicam.c, cpia.c and spca50x.c drivers
0012  * V4L2 version inspired by meye.c driver
0013  *
0014  * Some video buffer code by Lamarque based on s2255drv.c and vivi.c drivers.
0015  */
0016 
0017 
0018 #include <linux/module.h>
0019 #include <linux/init.h>
0020 #include <linux/usb.h>
0021 #include <linux/vmalloc.h>
0022 #include <linux/slab.h>
0023 #include <linux/highmem.h>
0024 #include <media/v4l2-common.h>
0025 #include <media/v4l2-ioctl.h>
0026 #include <media/v4l2-device.h>
0027 #include <media/v4l2-ctrls.h>
0028 #include <media/v4l2-fh.h>
0029 #include <media/v4l2-event.h>
0030 #include <media/videobuf-vmalloc.h>
0031 
0032 
0033 /* Version Information */
0034 #define DRIVER_VERSION "0.7.4"
0035 #define DRIVER_AUTHOR "Antoine Jacquet, http://royale.zerezo.com/"
0036 #define DRIVER_DESC "Zoran 364xx"
0037 
0038 
0039 /* Camera */
0040 #define FRAMES 1
0041 #define MAX_FRAME_SIZE 200000
0042 #define BUFFER_SIZE 0x1000
0043 #define CTRL_TIMEOUT 500
0044 
0045 #define ZR364XX_DEF_BUFS    4
0046 #define ZR364XX_READ_IDLE   0
0047 #define ZR364XX_READ_FRAME  1
0048 
0049 /* Debug macro */
0050 #define DBG(fmt, args...) \
0051     do { \
0052         if (debug) { \
0053             printk(KERN_INFO KBUILD_MODNAME " " fmt, ##args); \
0054         } \
0055     } while (0)
0056 
0057 /*#define FULL_DEBUG 1*/
0058 #ifdef FULL_DEBUG
0059 #define _DBG DBG
0060 #else
0061 #define _DBG(fmt, args...)
0062 #endif
0063 
0064 /* Init methods, need to find nicer names for these
0065  * the exact names of the chipsets would be the best if someone finds it */
0066 #define METHOD0 0
0067 #define METHOD1 1
0068 #define METHOD2 2
0069 #define METHOD3 3
0070 
0071 
0072 /* Module parameters */
0073 static int debug;
0074 static int mode;
0075 
0076 
0077 /* Module parameters interface */
0078 module_param(debug, int, 0644);
0079 MODULE_PARM_DESC(debug, "Debug level");
0080 module_param(mode, int, 0644);
0081 MODULE_PARM_DESC(mode, "0 = 320x240, 1 = 160x120, 2 = 640x480");
0082 
0083 
0084 /* Devices supported by this driver
0085  * .driver_info contains the init method used by the camera */
0086 static const struct usb_device_id device_table[] = {
0087     {USB_DEVICE(0x08ca, 0x0109), .driver_info = METHOD0 },
0088     {USB_DEVICE(0x041e, 0x4024), .driver_info = METHOD0 },
0089     {USB_DEVICE(0x0d64, 0x0108), .driver_info = METHOD0 },
0090     {USB_DEVICE(0x0546, 0x3187), .driver_info = METHOD0 },
0091     {USB_DEVICE(0x0d64, 0x3108), .driver_info = METHOD0 },
0092     {USB_DEVICE(0x0595, 0x4343), .driver_info = METHOD0 },
0093     {USB_DEVICE(0x0bb0, 0x500d), .driver_info = METHOD0 },
0094     {USB_DEVICE(0x0feb, 0x2004), .driver_info = METHOD0 },
0095     {USB_DEVICE(0x055f, 0xb500), .driver_info = METHOD0 },
0096     {USB_DEVICE(0x08ca, 0x2062), .driver_info = METHOD2 },
0097     {USB_DEVICE(0x052b, 0x1a18), .driver_info = METHOD1 },
0098     {USB_DEVICE(0x04c8, 0x0729), .driver_info = METHOD0 },
0099     {USB_DEVICE(0x04f2, 0xa208), .driver_info = METHOD0 },
0100     {USB_DEVICE(0x0784, 0x0040), .driver_info = METHOD1 },
0101     {USB_DEVICE(0x06d6, 0x0034), .driver_info = METHOD0 },
0102     {USB_DEVICE(0x0a17, 0x0062), .driver_info = METHOD2 },
0103     {USB_DEVICE(0x06d6, 0x003b), .driver_info = METHOD0 },
0104     {USB_DEVICE(0x0a17, 0x004e), .driver_info = METHOD2 },
0105     {USB_DEVICE(0x041e, 0x405d), .driver_info = METHOD2 },
0106     {USB_DEVICE(0x08ca, 0x2102), .driver_info = METHOD3 },
0107     {USB_DEVICE(0x06d6, 0x003d), .driver_info = METHOD0 },
0108     {}          /* Terminating entry */
0109 };
0110 
0111 MODULE_DEVICE_TABLE(usb, device_table);
0112 
0113 /* frame structure */
0114 struct zr364xx_framei {
0115     unsigned long ulState;  /* ulState:ZR364XX_READ_IDLE,
0116                        ZR364XX_READ_FRAME */
0117     void *lpvbits;      /* image data */
0118     unsigned long cur_size; /* current data copied to it */
0119 };
0120 
0121 /* image buffer structure */
0122 struct zr364xx_bufferi {
0123     unsigned long dwFrames;         /* number of frames in buffer */
0124     struct zr364xx_framei frame[FRAMES];    /* array of FRAME structures */
0125 };
0126 
0127 struct zr364xx_dmaqueue {
0128     struct list_head    active;
0129     struct zr364xx_camera   *cam;
0130 };
0131 
0132 struct zr364xx_pipeinfo {
0133     u32 transfer_size;
0134     u8 *transfer_buffer;
0135     u32 state;
0136     void *stream_urb;
0137     void *cam;  /* back pointer to zr364xx_camera struct */
0138     u32 err_count;
0139     u32 idx;
0140 };
0141 
0142 struct zr364xx_fmt {
0143     u32 fourcc;
0144     int depth;
0145 };
0146 
0147 /* image formats.  */
0148 static const struct zr364xx_fmt formats[] = {
0149     {
0150         .fourcc = V4L2_PIX_FMT_JPEG,
0151         .depth = 24
0152     }
0153 };
0154 
0155 /* Camera stuff */
0156 struct zr364xx_camera {
0157     struct usb_device *udev;    /* save off the usb device pointer */
0158     struct usb_interface *interface;/* the interface for this device */
0159     struct v4l2_device v4l2_dev;
0160     struct v4l2_ctrl_handler ctrl_handler;
0161     struct video_device vdev;   /* v4l video device */
0162     struct v4l2_fh *owner;      /* owns the streaming */
0163     int nb;
0164     struct zr364xx_bufferi      buffer;
0165     int skip;
0166     int width;
0167     int height;
0168     int method;
0169     struct mutex lock;
0170 
0171     spinlock_t      slock;
0172     struct zr364xx_dmaqueue vidq;
0173     int         last_frame;
0174     int         cur_frame;
0175     unsigned long       frame_count;
0176     int         b_acquire;
0177     struct zr364xx_pipeinfo pipe[1];
0178 
0179     u8          read_endpoint;
0180 
0181     const struct zr364xx_fmt *fmt;
0182     struct videobuf_queue   vb_vidq;
0183     bool was_streaming;
0184 };
0185 
0186 /* buffer for one video frame */
0187 struct zr364xx_buffer {
0188     /* common v4l buffer stuff -- must be first */
0189     struct videobuf_buffer vb;
0190     const struct zr364xx_fmt *fmt;
0191 };
0192 
0193 /* function used to send initialisation commands to the camera */
0194 static int send_control_msg(struct usb_device *udev, u8 request, u16 value,
0195                 u16 index, unsigned char *cp, u16 size)
0196 {
0197     int status;
0198 
0199     unsigned char *transfer_buffer = kmemdup(cp, size, GFP_KERNEL);
0200     if (!transfer_buffer)
0201         return -ENOMEM;
0202 
0203     status = usb_control_msg(udev,
0204                  usb_sndctrlpipe(udev, 0),
0205                  request,
0206                  USB_DIR_OUT | USB_TYPE_VENDOR |
0207                  USB_RECIP_DEVICE, value, index,
0208                  transfer_buffer, size, CTRL_TIMEOUT);
0209 
0210     kfree(transfer_buffer);
0211     return status;
0212 }
0213 
0214 
0215 /* Control messages sent to the camera to initialize it
0216  * and launch the capture */
0217 typedef struct {
0218     unsigned int value;
0219     unsigned int size;
0220     unsigned char *bytes;
0221 } message;
0222 
0223 /* method 0 */
0224 static unsigned char m0d1[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
0225 static unsigned char m0d2[] = { 0, 0, 0, 0, 0, 0 };
0226 static unsigned char m0d3[] = { 0, 0 };
0227 static message m0[] = {
0228     {0x1f30, 0, NULL},
0229     {0xd000, 0, NULL},
0230     {0x3370, sizeof(m0d1), m0d1},
0231     {0x2000, 0, NULL},
0232     {0x2f0f, 0, NULL},
0233     {0x2610, sizeof(m0d2), m0d2},
0234     {0xe107, 0, NULL},
0235     {0x2502, 0, NULL},
0236     {0x1f70, 0, NULL},
0237     {0xd000, 0, NULL},
0238     {0x9a01, sizeof(m0d3), m0d3},
0239     {-1, -1, NULL}
0240 };
0241 
0242 /* method 1 */
0243 static unsigned char m1d1[] = { 0xff, 0xff };
0244 static unsigned char m1d2[] = { 0x00, 0x00 };
0245 static message m1[] = {
0246     {0x1f30, 0, NULL},
0247     {0xd000, 0, NULL},
0248     {0xf000, 0, NULL},
0249     {0x2000, 0, NULL},
0250     {0x2f0f, 0, NULL},
0251     {0x2650, 0, NULL},
0252     {0xe107, 0, NULL},
0253     {0x2502, sizeof(m1d1), m1d1},
0254     {0x1f70, 0, NULL},
0255     {0xd000, 0, NULL},
0256     {0xd000, 0, NULL},
0257     {0xd000, 0, NULL},
0258     {0x9a01, sizeof(m1d2), m1d2},
0259     {-1, -1, NULL}
0260 };
0261 
0262 /* method 2 */
0263 static unsigned char m2d1[] = { 0xff, 0xff };
0264 static message m2[] = {
0265     {0x1f30, 0, NULL},
0266     {0xf000, 0, NULL},
0267     {0x2000, 0, NULL},
0268     {0x2f0f, 0, NULL},
0269     {0x2650, 0, NULL},
0270     {0xe107, 0, NULL},
0271     {0x2502, sizeof(m2d1), m2d1},
0272     {0x1f70, 0, NULL},
0273     {-1, -1, NULL}
0274 };
0275 
0276 /* init table */
0277 static message *init[4] = { m0, m1, m2, m2 };
0278 
0279 
0280 /* JPEG static data in header (Huffman table, etc) */
0281 static unsigned char header1[] = {
0282     0xFF, 0xD8,
0283     /*
0284     0xFF, 0xE0, 0x00, 0x10, 'J', 'F', 'I', 'F',
0285     0x00, 0x01, 0x01, 0x00, 0x33, 0x8A, 0x00, 0x00, 0x33, 0x88,
0286     */
0287     0xFF, 0xDB, 0x00, 0x84
0288 };
0289 static unsigned char header2[] = {
0290     0xFF, 0xC4, 0x00, 0x1F, 0x00, 0x00, 0x01, 0x05, 0x01, 0x01, 0x01,
0291     0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0292     0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
0293     0xFF, 0xC4, 0x00, 0xB5, 0x10, 0x00, 0x02, 0x01, 0x03, 0x03, 0x02,
0294     0x04, 0x03, 0x05, 0x05, 0x04, 0x04, 0x00, 0x00, 0x01, 0x7D, 0x01,
0295     0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06,
0296     0x13, 0x51, 0x61, 0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xA1,
0297     0x08, 0x23, 0x42, 0xB1, 0xC1, 0x15, 0x52, 0xD1, 0xF0, 0x24, 0x33,
0298     0x62, 0x72, 0x82, 0x09, 0x0A, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x25,
0299     0x26, 0x27, 0x28, 0x29, 0x2A, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
0300     0x3A, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x53, 0x54,
0301     0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67,
0302     0x68, 0x69, 0x6A, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A,
0303     0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x92, 0x93, 0x94,
0304     0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6,
0305     0xA7, 0xA8, 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8,
0306     0xB9, 0xBA, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA,
0307     0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xE1, 0xE2,
0308     0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xF1, 0xF2, 0xF3,
0309     0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFF, 0xC4, 0x00, 0x1F,
0310     0x01, 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0311     0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04,
0312     0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0xFF, 0xC4, 0x00, 0xB5,
0313     0x11, 0x00, 0x02, 0x01, 0x02, 0x04, 0x04, 0x03, 0x04, 0x07, 0x05,
0314     0x04, 0x04, 0x00, 0x01, 0x02, 0x77, 0x00, 0x01, 0x02, 0x03, 0x11,
0315     0x04, 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
0316     0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xA1, 0xB1, 0xC1,
0317     0x09, 0x23, 0x33, 0x52, 0xF0, 0x15, 0x62, 0x72, 0xD1, 0x0A, 0x16,
0318     0x24, 0x34, 0xE1, 0x25, 0xF1, 0x17, 0x18, 0x19, 0x1A, 0x26, 0x27,
0319     0x28, 0x29, 0x2A, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x43, 0x44,
0320     0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x53, 0x54, 0x55, 0x56, 0x57,
0321     0x58, 0x59, 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A,
0322     0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x82, 0x83, 0x84,
0323     0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x92, 0x93, 0x94, 0x95, 0x96,
0324     0x97, 0x98, 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8,
0325     0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA,
0326     0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xD2, 0xD3,
0327     0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xE2, 0xE3, 0xE4, 0xE5,
0328     0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7,
0329     0xF8, 0xF9, 0xFA, 0xFF, 0xC0, 0x00, 0x11, 0x08, 0x00, 0xF0, 0x01,
0330     0x40, 0x03, 0x01, 0x21, 0x00, 0x02, 0x11, 0x01, 0x03, 0x11, 0x01,
0331     0xFF, 0xDA, 0x00, 0x0C, 0x03, 0x01, 0x00, 0x02, 0x11, 0x03, 0x11,
0332     0x00, 0x3F, 0x00
0333 };
0334 static unsigned char header3;
0335 
0336 /* ------------------------------------------------------------------
0337    Videobuf operations
0338    ------------------------------------------------------------------*/
0339 
0340 static int buffer_setup(struct videobuf_queue *vq, unsigned int *count,
0341             unsigned int *size)
0342 {
0343     struct zr364xx_camera *cam = vq->priv_data;
0344 
0345     *size = cam->width * cam->height * (cam->fmt->depth >> 3);
0346 
0347     if (*count == 0)
0348         *count = ZR364XX_DEF_BUFS;
0349 
0350     if (*size * *count > ZR364XX_DEF_BUFS * 1024 * 1024)
0351         *count = (ZR364XX_DEF_BUFS * 1024 * 1024) / *size;
0352 
0353     return 0;
0354 }
0355 
0356 static void free_buffer(struct videobuf_queue *vq, struct zr364xx_buffer *buf)
0357 {
0358     _DBG("%s\n", __func__);
0359 
0360     videobuf_vmalloc_free(&buf->vb);
0361     buf->vb.state = VIDEOBUF_NEEDS_INIT;
0362 }
0363 
0364 static int buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
0365               enum v4l2_field field)
0366 {
0367     struct zr364xx_camera *cam = vq->priv_data;
0368     struct zr364xx_buffer *buf = container_of(vb, struct zr364xx_buffer,
0369                           vb);
0370     int rc;
0371 
0372     DBG("%s, field=%d\n", __func__, field);
0373     if (!cam->fmt)
0374         return -EINVAL;
0375 
0376     buf->vb.size = cam->width * cam->height * (cam->fmt->depth >> 3);
0377 
0378     if (buf->vb.baddr != 0 && buf->vb.bsize < buf->vb.size) {
0379         DBG("invalid buffer prepare\n");
0380         return -EINVAL;
0381     }
0382 
0383     buf->fmt = cam->fmt;
0384     buf->vb.width = cam->width;
0385     buf->vb.height = cam->height;
0386     buf->vb.field = field;
0387 
0388     if (buf->vb.state == VIDEOBUF_NEEDS_INIT) {
0389         rc = videobuf_iolock(vq, &buf->vb, NULL);
0390         if (rc < 0)
0391             goto fail;
0392     }
0393 
0394     buf->vb.state = VIDEOBUF_PREPARED;
0395     return 0;
0396 fail:
0397     free_buffer(vq, buf);
0398     return rc;
0399 }
0400 
0401 static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
0402 {
0403     struct zr364xx_buffer *buf = container_of(vb, struct zr364xx_buffer,
0404                           vb);
0405     struct zr364xx_camera *cam = vq->priv_data;
0406 
0407     _DBG("%s\n", __func__);
0408 
0409     buf->vb.state = VIDEOBUF_QUEUED;
0410     list_add_tail(&buf->vb.queue, &cam->vidq.active);
0411 }
0412 
0413 static void buffer_release(struct videobuf_queue *vq,
0414                struct videobuf_buffer *vb)
0415 {
0416     struct zr364xx_buffer *buf = container_of(vb, struct zr364xx_buffer,
0417                           vb);
0418 
0419     _DBG("%s\n", __func__);
0420     free_buffer(vq, buf);
0421 }
0422 
0423 static const struct videobuf_queue_ops zr364xx_video_qops = {
0424     .buf_setup = buffer_setup,
0425     .buf_prepare = buffer_prepare,
0426     .buf_queue = buffer_queue,
0427     .buf_release = buffer_release,
0428 };
0429 
0430 /********************/
0431 /* V4L2 integration */
0432 /********************/
0433 static int zr364xx_vidioc_streamon(struct file *file, void *priv,
0434                    enum v4l2_buf_type type);
0435 
0436 static ssize_t zr364xx_read(struct file *file, char __user *buf, size_t count,
0437                 loff_t * ppos)
0438 {
0439     struct zr364xx_camera *cam = video_drvdata(file);
0440     int err = 0;
0441 
0442     _DBG("%s\n", __func__);
0443 
0444     if (!buf)
0445         return -EINVAL;
0446 
0447     if (!count)
0448         return -EINVAL;
0449 
0450     if (mutex_lock_interruptible(&cam->lock))
0451         return -ERESTARTSYS;
0452 
0453     err = zr364xx_vidioc_streamon(file, file->private_data,
0454                 V4L2_BUF_TYPE_VIDEO_CAPTURE);
0455     if (err == 0) {
0456         DBG("%s: reading %d bytes at pos %d.\n", __func__,
0457                 (int) count, (int) *ppos);
0458 
0459         /* NoMan Sux ! */
0460         err = videobuf_read_one(&cam->vb_vidq, buf, count, ppos,
0461                     file->f_flags & O_NONBLOCK);
0462     }
0463     mutex_unlock(&cam->lock);
0464     return err;
0465 }
0466 
0467 /* video buffer vmalloc implementation based partly on VIVI driver which is
0468  *          Copyright (c) 2006 by
0469  *                  Mauro Carvalho Chehab <mchehab--a.t--infradead.org>
0470  *                  Ted Walther <ted--a.t--enumera.com>
0471  *                  John Sokol <sokol--a.t--videotechnology.com>
0472  *                  http://v4l.videotechnology.com/
0473  *
0474  */
0475 static void zr364xx_fillbuff(struct zr364xx_camera *cam,
0476                  struct zr364xx_buffer *buf,
0477                  int jpgsize)
0478 {
0479     int pos = 0;
0480     const char *tmpbuf;
0481     char *vbuf = videobuf_to_vmalloc(&buf->vb);
0482     unsigned long last_frame;
0483 
0484     if (!vbuf)
0485         return;
0486 
0487     last_frame = cam->last_frame;
0488     if (last_frame != -1) {
0489         tmpbuf = (const char *)cam->buffer.frame[last_frame].lpvbits;
0490         switch (buf->fmt->fourcc) {
0491         case V4L2_PIX_FMT_JPEG:
0492             buf->vb.size = jpgsize;
0493             memcpy(vbuf, tmpbuf, buf->vb.size);
0494             break;
0495         default:
0496             printk(KERN_DEBUG KBUILD_MODNAME ": unknown format?\n");
0497         }
0498         cam->last_frame = -1;
0499     } else {
0500         printk(KERN_ERR KBUILD_MODNAME ": =======no frame\n");
0501         return;
0502     }
0503     DBG("%s: Buffer %p size= %d\n", __func__, vbuf, pos);
0504     /* tell v4l buffer was filled */
0505 
0506     buf->vb.field_count = cam->frame_count * 2;
0507     buf->vb.ts = ktime_get_ns();
0508     buf->vb.state = VIDEOBUF_DONE;
0509 }
0510 
0511 static int zr364xx_got_frame(struct zr364xx_camera *cam, int jpgsize)
0512 {
0513     struct zr364xx_dmaqueue *dma_q = &cam->vidq;
0514     struct zr364xx_buffer *buf;
0515     unsigned long flags = 0;
0516     int rc = 0;
0517 
0518     DBG("wakeup: %p\n", &dma_q);
0519     spin_lock_irqsave(&cam->slock, flags);
0520 
0521     if (list_empty(&dma_q->active)) {
0522         DBG("No active queue to serve\n");
0523         rc = -1;
0524         goto unlock;
0525     }
0526     buf = list_entry(dma_q->active.next,
0527              struct zr364xx_buffer, vb.queue);
0528 
0529     if (!waitqueue_active(&buf->vb.done)) {
0530         /* no one active */
0531         rc = -1;
0532         goto unlock;
0533     }
0534     list_del(&buf->vb.queue);
0535     buf->vb.ts = ktime_get_ns();
0536     DBG("[%p/%d] wakeup\n", buf, buf->vb.i);
0537     zr364xx_fillbuff(cam, buf, jpgsize);
0538     wake_up(&buf->vb.done);
0539     DBG("wakeup [buf/i] [%p/%d]\n", buf, buf->vb.i);
0540 unlock:
0541     spin_unlock_irqrestore(&cam->slock, flags);
0542     return rc;
0543 }
0544 
0545 /* this function moves the usb stream read pipe data
0546  * into the system buffers.
0547  * returns 0 on success, EAGAIN if more data to process (call this
0548  * function again).
0549  */
0550 static int zr364xx_read_video_callback(struct zr364xx_camera *cam,
0551                     struct zr364xx_pipeinfo *pipe_info,
0552                     struct urb *purb)
0553 {
0554     unsigned char *pdest;
0555     unsigned char *psrc;
0556     s32 idx = cam->cur_frame;
0557     struct zr364xx_framei *frm = &cam->buffer.frame[idx];
0558     int i = 0;
0559     unsigned char *ptr = NULL;
0560 
0561     _DBG("buffer to user\n");
0562 
0563     /* swap bytes if camera needs it */
0564     if (cam->method == METHOD0) {
0565         u16 *buf = (u16 *)pipe_info->transfer_buffer;
0566         for (i = 0; i < purb->actual_length/2; i++)
0567             swab16s(buf + i);
0568     }
0569 
0570     /* search done.  now find out if should be acquiring */
0571     if (!cam->b_acquire) {
0572         /* we found a frame, but this channel is turned off */
0573         frm->ulState = ZR364XX_READ_IDLE;
0574         return -EINVAL;
0575     }
0576 
0577     psrc = (u8 *)pipe_info->transfer_buffer;
0578     ptr = pdest = frm->lpvbits;
0579 
0580     if (frm->ulState == ZR364XX_READ_IDLE) {
0581         if (purb->actual_length < 128) {
0582             /* header incomplete */
0583             dev_info(&cam->udev->dev,
0584                  "%s: buffer (%d bytes) too small to hold jpeg header. Discarding.\n",
0585                  __func__, purb->actual_length);
0586             return -EINVAL;
0587         }
0588 
0589         frm->ulState = ZR364XX_READ_FRAME;
0590         frm->cur_size = 0;
0591 
0592         _DBG("jpeg header, ");
0593         memcpy(ptr, header1, sizeof(header1));
0594         ptr += sizeof(header1);
0595         header3 = 0;
0596         memcpy(ptr, &header3, 1);
0597         ptr++;
0598         memcpy(ptr, psrc, 64);
0599         ptr += 64;
0600         header3 = 1;
0601         memcpy(ptr, &header3, 1);
0602         ptr++;
0603         memcpy(ptr, psrc + 64, 64);
0604         ptr += 64;
0605         memcpy(ptr, header2, sizeof(header2));
0606         ptr += sizeof(header2);
0607         memcpy(ptr, psrc + 128,
0608                purb->actual_length - 128);
0609         ptr += purb->actual_length - 128;
0610         _DBG("header : %d %d %d %d %d %d %d %d %d\n",
0611             psrc[0], psrc[1], psrc[2],
0612             psrc[3], psrc[4], psrc[5],
0613             psrc[6], psrc[7], psrc[8]);
0614         frm->cur_size = ptr - pdest;
0615     } else {
0616         if (frm->cur_size + purb->actual_length > MAX_FRAME_SIZE) {
0617             dev_info(&cam->udev->dev,
0618                  "%s: buffer (%d bytes) too small to hold frame data. Discarding frame data.\n",
0619                  __func__, MAX_FRAME_SIZE);
0620         } else {
0621             pdest += frm->cur_size;
0622             memcpy(pdest, psrc, purb->actual_length);
0623             frm->cur_size += purb->actual_length;
0624         }
0625     }
0626     /*_DBG("cur_size %lu urb size %d\n", frm->cur_size,
0627         purb->actual_length);*/
0628 
0629     if (purb->actual_length < pipe_info->transfer_size) {
0630         _DBG("****************Buffer[%d]full*************\n", idx);
0631         cam->last_frame = cam->cur_frame;
0632         cam->cur_frame++;
0633         /* end of system frame ring buffer, start at zero */
0634         if (cam->cur_frame == cam->buffer.dwFrames)
0635             cam->cur_frame = 0;
0636 
0637         /* frame ready */
0638         /* go back to find the JPEG EOI marker */
0639         ptr = pdest = frm->lpvbits;
0640         ptr += frm->cur_size - 2;
0641         while (ptr > pdest) {
0642             if (*ptr == 0xFF && *(ptr + 1) == 0xD9
0643                 && *(ptr + 2) == 0xFF)
0644                 break;
0645             ptr--;
0646         }
0647         if (ptr == pdest)
0648             DBG("No EOI marker\n");
0649 
0650         /* Sometimes there is junk data in the middle of the picture,
0651          * we want to skip this bogus frames */
0652         while (ptr > pdest) {
0653             if (*ptr == 0xFF && *(ptr + 1) == 0xFF
0654                 && *(ptr + 2) == 0xFF)
0655                 break;
0656             ptr--;
0657         }
0658         if (ptr != pdest) {
0659             DBG("Bogus frame ? %d\n", ++(cam->nb));
0660         } else if (cam->b_acquire) {
0661             /* we skip the 2 first frames which are usually buggy */
0662             if (cam->skip)
0663                 cam->skip--;
0664             else {
0665                 _DBG("jpeg(%lu): %d %d %d %d %d %d %d %d\n",
0666                     frm->cur_size,
0667                     pdest[0], pdest[1], pdest[2], pdest[3],
0668                     pdest[4], pdest[5], pdest[6], pdest[7]);
0669 
0670                 zr364xx_got_frame(cam, frm->cur_size);
0671             }
0672         }
0673         cam->frame_count++;
0674         frm->ulState = ZR364XX_READ_IDLE;
0675         frm->cur_size = 0;
0676     }
0677     /* done successfully */
0678     return 0;
0679 }
0680 
0681 static int zr364xx_vidioc_querycap(struct file *file, void *priv,
0682                    struct v4l2_capability *cap)
0683 {
0684     struct zr364xx_camera *cam = video_drvdata(file);
0685 
0686     strscpy(cap->driver, DRIVER_DESC, sizeof(cap->driver));
0687     if (cam->udev->product)
0688         strscpy(cap->card, cam->udev->product, sizeof(cap->card));
0689     strscpy(cap->bus_info, dev_name(&cam->udev->dev),
0690         sizeof(cap->bus_info));
0691     return 0;
0692 }
0693 
0694 static int zr364xx_vidioc_enum_input(struct file *file, void *priv,
0695                      struct v4l2_input *i)
0696 {
0697     if (i->index != 0)
0698         return -EINVAL;
0699     strscpy(i->name, DRIVER_DESC " Camera", sizeof(i->name));
0700     i->type = V4L2_INPUT_TYPE_CAMERA;
0701     return 0;
0702 }
0703 
0704 static int zr364xx_vidioc_g_input(struct file *file, void *priv,
0705                   unsigned int *i)
0706 {
0707     *i = 0;
0708     return 0;
0709 }
0710 
0711 static int zr364xx_vidioc_s_input(struct file *file, void *priv,
0712                   unsigned int i)
0713 {
0714     if (i != 0)
0715         return -EINVAL;
0716     return 0;
0717 }
0718 
0719 static int zr364xx_s_ctrl(struct v4l2_ctrl *ctrl)
0720 {
0721     struct zr364xx_camera *cam =
0722         container_of(ctrl->handler, struct zr364xx_camera, ctrl_handler);
0723     int temp;
0724 
0725     switch (ctrl->id) {
0726     case V4L2_CID_BRIGHTNESS:
0727         /* hardware brightness */
0728         send_control_msg(cam->udev, 1, 0x2001, 0, NULL, 0);
0729         temp = (0x60 << 8) + 127 - ctrl->val;
0730         send_control_msg(cam->udev, 1, temp, 0, NULL, 0);
0731         break;
0732     default:
0733         return -EINVAL;
0734     }
0735 
0736     return 0;
0737 }
0738 
0739 static int zr364xx_vidioc_enum_fmt_vid_cap(struct file *file,
0740                        void *priv, struct v4l2_fmtdesc *f)
0741 {
0742     if (f->index > 0)
0743         return -EINVAL;
0744     f->pixelformat = formats[0].fourcc;
0745     return 0;
0746 }
0747 
0748 static char *decode_fourcc(__u32 pixelformat, char *buf)
0749 {
0750     buf[0] = pixelformat & 0xff;
0751     buf[1] = (pixelformat >> 8) & 0xff;
0752     buf[2] = (pixelformat >> 16) & 0xff;
0753     buf[3] = (pixelformat >> 24) & 0xff;
0754     buf[4] = '\0';
0755     return buf;
0756 }
0757 
0758 static int zr364xx_vidioc_try_fmt_vid_cap(struct file *file, void *priv,
0759                       struct v4l2_format *f)
0760 {
0761     struct zr364xx_camera *cam = video_drvdata(file);
0762     char pixelformat_name[5];
0763 
0764     if (!cam)
0765         return -ENODEV;
0766 
0767     if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_JPEG) {
0768         DBG("%s: unsupported pixelformat V4L2_PIX_FMT_%s\n", __func__,
0769             decode_fourcc(f->fmt.pix.pixelformat, pixelformat_name));
0770         return -EINVAL;
0771     }
0772 
0773     if (!(f->fmt.pix.width == 160 && f->fmt.pix.height == 120) &&
0774         !(f->fmt.pix.width == 640 && f->fmt.pix.height == 480)) {
0775         f->fmt.pix.width = 320;
0776         f->fmt.pix.height = 240;
0777     }
0778 
0779     f->fmt.pix.field = V4L2_FIELD_NONE;
0780     f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
0781     f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
0782     f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
0783     DBG("%s: V4L2_PIX_FMT_%s (%d) ok!\n", __func__,
0784         decode_fourcc(f->fmt.pix.pixelformat, pixelformat_name),
0785         f->fmt.pix.field);
0786     return 0;
0787 }
0788 
0789 static int zr364xx_vidioc_g_fmt_vid_cap(struct file *file, void *priv,
0790                     struct v4l2_format *f)
0791 {
0792     struct zr364xx_camera *cam;
0793 
0794     if (!file)
0795         return -ENODEV;
0796     cam = video_drvdata(file);
0797 
0798     f->fmt.pix.pixelformat = formats[0].fourcc;
0799     f->fmt.pix.field = V4L2_FIELD_NONE;
0800     f->fmt.pix.width = cam->width;
0801     f->fmt.pix.height = cam->height;
0802     f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
0803     f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
0804     f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
0805     return 0;
0806 }
0807 
0808 static int zr364xx_vidioc_s_fmt_vid_cap(struct file *file, void *priv,
0809                     struct v4l2_format *f)
0810 {
0811     struct zr364xx_camera *cam = video_drvdata(file);
0812     struct videobuf_queue *q = &cam->vb_vidq;
0813     char pixelformat_name[5];
0814     int ret = zr364xx_vidioc_try_fmt_vid_cap(file, cam, f);
0815     int i;
0816 
0817     if (ret < 0)
0818         return ret;
0819 
0820     mutex_lock(&q->vb_lock);
0821 
0822     if (videobuf_queue_is_busy(&cam->vb_vidq)) {
0823         DBG("%s queue busy\n", __func__);
0824         ret = -EBUSY;
0825         goto out;
0826     }
0827 
0828     if (cam->owner) {
0829         DBG("%s can't change format after started\n", __func__);
0830         ret = -EBUSY;
0831         goto out;
0832     }
0833 
0834     cam->width = f->fmt.pix.width;
0835     cam->height = f->fmt.pix.height;
0836     DBG("%s: %dx%d mode selected\n", __func__,
0837          cam->width, cam->height);
0838     f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
0839     f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
0840     f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
0841     cam->vb_vidq.field = f->fmt.pix.field;
0842 
0843     if (f->fmt.pix.width == 160 && f->fmt.pix.height == 120)
0844         mode = 1;
0845     else if (f->fmt.pix.width == 640 && f->fmt.pix.height == 480)
0846         mode = 2;
0847     else
0848         mode = 0;
0849 
0850     m0d1[0] = mode;
0851     m1[2].value = 0xf000 + mode;
0852     m2[1].value = 0xf000 + mode;
0853 
0854     /* special case for METHOD3, the modes are different */
0855     if (cam->method == METHOD3) {
0856         switch (mode) {
0857         case 1:
0858             m2[1].value = 0xf000 + 4;
0859             break;
0860         case 2:
0861             m2[1].value = 0xf000 + 0;
0862             break;
0863         default:
0864             m2[1].value = 0xf000 + 1;
0865             break;
0866         }
0867     }
0868 
0869     header2[437] = cam->height / 256;
0870     header2[438] = cam->height % 256;
0871     header2[439] = cam->width / 256;
0872     header2[440] = cam->width % 256;
0873 
0874     for (i = 0; init[cam->method][i].size != -1; i++) {
0875         ret =
0876             send_control_msg(cam->udev, 1, init[cam->method][i].value,
0877                      0, init[cam->method][i].bytes,
0878                      init[cam->method][i].size);
0879         if (ret < 0) {
0880             dev_err(&cam->udev->dev,
0881                "error during resolution change sequence: %d\n", i);
0882             goto out;
0883         }
0884     }
0885 
0886     /* Added some delay here, since opening/closing the camera quickly,
0887      * like Ekiga does during its startup, can crash the webcam
0888      */
0889     mdelay(100);
0890     cam->skip = 2;
0891     ret = 0;
0892 
0893 out:
0894     mutex_unlock(&q->vb_lock);
0895 
0896     DBG("%s: V4L2_PIX_FMT_%s (%d) ok!\n", __func__,
0897         decode_fourcc(f->fmt.pix.pixelformat, pixelformat_name),
0898         f->fmt.pix.field);
0899     return ret;
0900 }
0901 
0902 static int zr364xx_vidioc_reqbufs(struct file *file, void *priv,
0903               struct v4l2_requestbuffers *p)
0904 {
0905     struct zr364xx_camera *cam = video_drvdata(file);
0906 
0907     if (cam->owner && cam->owner != priv)
0908         return -EBUSY;
0909     return videobuf_reqbufs(&cam->vb_vidq, p);
0910 }
0911 
0912 static int zr364xx_vidioc_querybuf(struct file *file,
0913                 void *priv,
0914                 struct v4l2_buffer *p)
0915 {
0916     int rc;
0917     struct zr364xx_camera *cam = video_drvdata(file);
0918     rc = videobuf_querybuf(&cam->vb_vidq, p);
0919     return rc;
0920 }
0921 
0922 static int zr364xx_vidioc_qbuf(struct file *file,
0923                 void *priv,
0924                 struct v4l2_buffer *p)
0925 {
0926     int rc;
0927     struct zr364xx_camera *cam = video_drvdata(file);
0928     _DBG("%s\n", __func__);
0929     if (cam->owner && cam->owner != priv)
0930         return -EBUSY;
0931     rc = videobuf_qbuf(&cam->vb_vidq, p);
0932     return rc;
0933 }
0934 
0935 static int zr364xx_vidioc_dqbuf(struct file *file,
0936                 void *priv,
0937                 struct v4l2_buffer *p)
0938 {
0939     int rc;
0940     struct zr364xx_camera *cam = video_drvdata(file);
0941     _DBG("%s\n", __func__);
0942     if (cam->owner && cam->owner != priv)
0943         return -EBUSY;
0944     rc = videobuf_dqbuf(&cam->vb_vidq, p, file->f_flags & O_NONBLOCK);
0945     return rc;
0946 }
0947 
0948 static void read_pipe_completion(struct urb *purb)
0949 {
0950     struct zr364xx_pipeinfo *pipe_info;
0951     struct zr364xx_camera *cam;
0952     int pipe;
0953 
0954     pipe_info = purb->context;
0955     _DBG("%s %p, status %d\n", __func__, purb, purb->status);
0956     if (!pipe_info) {
0957         printk(KERN_ERR KBUILD_MODNAME ": no context!\n");
0958         return;
0959     }
0960 
0961     cam = pipe_info->cam;
0962     if (!cam) {
0963         printk(KERN_ERR KBUILD_MODNAME ": no context!\n");
0964         return;
0965     }
0966 
0967     /* if shutting down, do not resubmit, exit immediately */
0968     if (purb->status == -ESHUTDOWN) {
0969         DBG("%s, err shutdown\n", __func__);
0970         pipe_info->err_count++;
0971         return;
0972     }
0973 
0974     if (pipe_info->state == 0) {
0975         DBG("exiting USB pipe\n");
0976         return;
0977     }
0978 
0979     if (purb->actual_length > pipe_info->transfer_size) {
0980         dev_err(&cam->udev->dev, "wrong number of bytes\n");
0981         return;
0982     }
0983 
0984     if (purb->status == 0)
0985         zr364xx_read_video_callback(cam, pipe_info, purb);
0986     else {
0987         pipe_info->err_count++;
0988         DBG("%s: failed URB %d\n", __func__, purb->status);
0989     }
0990 
0991     pipe = usb_rcvbulkpipe(cam->udev, cam->read_endpoint);
0992 
0993     /* reuse urb */
0994     usb_fill_bulk_urb(pipe_info->stream_urb, cam->udev,
0995               pipe,
0996               pipe_info->transfer_buffer,
0997               pipe_info->transfer_size,
0998               read_pipe_completion, pipe_info);
0999 
1000     if (pipe_info->state != 0) {
1001         purb->status = usb_submit_urb(pipe_info->stream_urb,
1002                           GFP_ATOMIC);
1003 
1004         if (purb->status)
1005             dev_err(&cam->udev->dev,
1006                 "error submitting urb (error=%i)\n",
1007                 purb->status);
1008     } else
1009         DBG("read pipe complete state 0\n");
1010 }
1011 
1012 static int zr364xx_start_readpipe(struct zr364xx_camera *cam)
1013 {
1014     int pipe;
1015     int retval;
1016     struct zr364xx_pipeinfo *pipe_info = cam->pipe;
1017     pipe = usb_rcvbulkpipe(cam->udev, cam->read_endpoint);
1018     DBG("%s: start pipe IN x%x\n", __func__, cam->read_endpoint);
1019 
1020     pipe_info->state = 1;
1021     pipe_info->err_count = 0;
1022     pipe_info->stream_urb = usb_alloc_urb(0, GFP_KERNEL);
1023     if (!pipe_info->stream_urb)
1024         return -ENOMEM;
1025     /* transfer buffer allocated in board_init */
1026     usb_fill_bulk_urb(pipe_info->stream_urb, cam->udev,
1027               pipe,
1028               pipe_info->transfer_buffer,
1029               pipe_info->transfer_size,
1030               read_pipe_completion, pipe_info);
1031 
1032     DBG("submitting URB %p\n", pipe_info->stream_urb);
1033     retval = usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL);
1034     if (retval) {
1035         usb_free_urb(pipe_info->stream_urb);
1036         printk(KERN_ERR KBUILD_MODNAME ": start read pipe failed\n");
1037         return retval;
1038     }
1039 
1040     return 0;
1041 }
1042 
1043 static void zr364xx_stop_readpipe(struct zr364xx_camera *cam)
1044 {
1045     struct zr364xx_pipeinfo *pipe_info;
1046 
1047     if (!cam) {
1048         printk(KERN_ERR KBUILD_MODNAME ": invalid device\n");
1049         return;
1050     }
1051     DBG("stop read pipe\n");
1052     pipe_info = cam->pipe;
1053     if (pipe_info) {
1054         if (pipe_info->state != 0)
1055             pipe_info->state = 0;
1056 
1057         if (pipe_info->stream_urb) {
1058             /* cancel urb */
1059             usb_kill_urb(pipe_info->stream_urb);
1060             usb_free_urb(pipe_info->stream_urb);
1061             pipe_info->stream_urb = NULL;
1062         }
1063     }
1064     return;
1065 }
1066 
1067 /* starts acquisition process */
1068 static int zr364xx_start_acquire(struct zr364xx_camera *cam)
1069 {
1070     int j;
1071 
1072     DBG("start acquire\n");
1073 
1074     cam->last_frame = -1;
1075     cam->cur_frame = 0;
1076     for (j = 0; j < FRAMES; j++) {
1077         cam->buffer.frame[j].ulState = ZR364XX_READ_IDLE;
1078         cam->buffer.frame[j].cur_size = 0;
1079     }
1080     cam->b_acquire = 1;
1081     return 0;
1082 }
1083 
1084 static inline int zr364xx_stop_acquire(struct zr364xx_camera *cam)
1085 {
1086     cam->b_acquire = 0;
1087     return 0;
1088 }
1089 
1090 static int zr364xx_prepare(struct zr364xx_camera *cam)
1091 {
1092     int res;
1093     int i, j;
1094 
1095     for (i = 0; init[cam->method][i].size != -1; i++) {
1096         res = send_control_msg(cam->udev, 1, init[cam->method][i].value,
1097                      0, init[cam->method][i].bytes,
1098                      init[cam->method][i].size);
1099         if (res < 0) {
1100             dev_err(&cam->udev->dev,
1101                 "error during open sequence: %d\n", i);
1102             return res;
1103         }
1104     }
1105 
1106     cam->skip = 2;
1107     cam->last_frame = -1;
1108     cam->cur_frame = 0;
1109     cam->frame_count = 0;
1110     for (j = 0; j < FRAMES; j++) {
1111         cam->buffer.frame[j].ulState = ZR364XX_READ_IDLE;
1112         cam->buffer.frame[j].cur_size = 0;
1113     }
1114     v4l2_ctrl_handler_setup(&cam->ctrl_handler);
1115     return 0;
1116 }
1117 
1118 static int zr364xx_vidioc_streamon(struct file *file, void *priv,
1119                    enum v4l2_buf_type type)
1120 {
1121     struct zr364xx_camera *cam = video_drvdata(file);
1122     int res;
1123 
1124     DBG("%s\n", __func__);
1125 
1126     if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1127         return -EINVAL;
1128 
1129     if (cam->owner && cam->owner != priv)
1130         return -EBUSY;
1131 
1132     res = zr364xx_prepare(cam);
1133     if (res)
1134         return res;
1135     res = videobuf_streamon(&cam->vb_vidq);
1136     if (res == 0) {
1137         zr364xx_start_acquire(cam);
1138         cam->owner = file->private_data;
1139     }
1140     return res;
1141 }
1142 
1143 static int zr364xx_vidioc_streamoff(struct file *file, void *priv,
1144                     enum v4l2_buf_type type)
1145 {
1146     struct zr364xx_camera *cam = video_drvdata(file);
1147 
1148     DBG("%s\n", __func__);
1149     if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1150         return -EINVAL;
1151     if (cam->owner && cam->owner != priv)
1152         return -EBUSY;
1153     zr364xx_stop_acquire(cam);
1154     return videobuf_streamoff(&cam->vb_vidq);
1155 }
1156 
1157 
1158 /* open the camera */
1159 static int zr364xx_open(struct file *file)
1160 {
1161     struct zr364xx_camera *cam = video_drvdata(file);
1162     int err;
1163 
1164     DBG("%s\n", __func__);
1165 
1166     if (mutex_lock_interruptible(&cam->lock))
1167         return -ERESTARTSYS;
1168 
1169     err = v4l2_fh_open(file);
1170     if (err)
1171         goto out;
1172 
1173     /* Added some delay here, since opening/closing the camera quickly,
1174      * like Ekiga does during its startup, can crash the webcam
1175      */
1176     mdelay(100);
1177     err = 0;
1178 
1179 out:
1180     mutex_unlock(&cam->lock);
1181     DBG("%s: %d\n", __func__, err);
1182     return err;
1183 }
1184 
1185 static void zr364xx_board_uninit(struct zr364xx_camera *cam)
1186 {
1187     unsigned long i;
1188 
1189     zr364xx_stop_readpipe(cam);
1190 
1191     /* release sys buffers */
1192     for (i = 0; i < FRAMES; i++) {
1193         if (cam->buffer.frame[i].lpvbits) {
1194             DBG("vfree %p\n", cam->buffer.frame[i].lpvbits);
1195             vfree(cam->buffer.frame[i].lpvbits);
1196         }
1197         cam->buffer.frame[i].lpvbits = NULL;
1198     }
1199 
1200     /* release transfer buffer */
1201     kfree(cam->pipe->transfer_buffer);
1202 }
1203 
1204 static void zr364xx_release(struct v4l2_device *v4l2_dev)
1205 {
1206     struct zr364xx_camera *cam =
1207         container_of(v4l2_dev, struct zr364xx_camera, v4l2_dev);
1208 
1209     videobuf_mmap_free(&cam->vb_vidq);
1210     v4l2_ctrl_handler_free(&cam->ctrl_handler);
1211     zr364xx_board_uninit(cam);
1212     v4l2_device_unregister(&cam->v4l2_dev);
1213     kfree(cam);
1214 }
1215 
1216 /* release the camera */
1217 static int zr364xx_close(struct file *file)
1218 {
1219     struct zr364xx_camera *cam;
1220     struct usb_device *udev;
1221     int i;
1222 
1223     DBG("%s\n", __func__);
1224     cam = video_drvdata(file);
1225 
1226     mutex_lock(&cam->lock);
1227     udev = cam->udev;
1228 
1229     if (file->private_data == cam->owner) {
1230         /* turn off stream */
1231         if (cam->b_acquire)
1232             zr364xx_stop_acquire(cam);
1233         videobuf_streamoff(&cam->vb_vidq);
1234 
1235         for (i = 0; i < 2; i++) {
1236             send_control_msg(udev, 1, init[cam->method][i].value,
1237                     0, init[cam->method][i].bytes,
1238                     init[cam->method][i].size);
1239         }
1240         cam->owner = NULL;
1241     }
1242 
1243     /* Added some delay here, since opening/closing the camera quickly,
1244      * like Ekiga does during its startup, can crash the webcam
1245      */
1246     mdelay(100);
1247     mutex_unlock(&cam->lock);
1248     return v4l2_fh_release(file);
1249 }
1250 
1251 
1252 static int zr364xx_mmap(struct file *file, struct vm_area_struct *vma)
1253 {
1254     struct zr364xx_camera *cam = video_drvdata(file);
1255     int ret;
1256 
1257     if (!cam) {
1258         DBG("%s: cam == NULL\n", __func__);
1259         return -ENODEV;
1260     }
1261     DBG("mmap called, vma=%p\n", vma);
1262 
1263     ret = videobuf_mmap_mapper(&cam->vb_vidq, vma);
1264 
1265     DBG("vma start=0x%08lx, size=%ld, ret=%d\n",
1266         (unsigned long)vma->vm_start,
1267         (unsigned long)vma->vm_end - (unsigned long)vma->vm_start, ret);
1268     return ret;
1269 }
1270 
1271 static __poll_t zr364xx_poll(struct file *file,
1272                    struct poll_table_struct *wait)
1273 {
1274     struct zr364xx_camera *cam = video_drvdata(file);
1275     struct videobuf_queue *q = &cam->vb_vidq;
1276     __poll_t res = v4l2_ctrl_poll(file, wait);
1277 
1278     _DBG("%s\n", __func__);
1279 
1280     return res | videobuf_poll_stream(file, q, wait);
1281 }
1282 
1283 static const struct v4l2_ctrl_ops zr364xx_ctrl_ops = {
1284     .s_ctrl = zr364xx_s_ctrl,
1285 };
1286 
1287 static const struct v4l2_file_operations zr364xx_fops = {
1288     .owner = THIS_MODULE,
1289     .open = zr364xx_open,
1290     .release = zr364xx_close,
1291     .read = zr364xx_read,
1292     .mmap = zr364xx_mmap,
1293     .unlocked_ioctl = video_ioctl2,
1294     .poll = zr364xx_poll,
1295 };
1296 
1297 static const struct v4l2_ioctl_ops zr364xx_ioctl_ops = {
1298     .vidioc_querycap    = zr364xx_vidioc_querycap,
1299     .vidioc_enum_fmt_vid_cap = zr364xx_vidioc_enum_fmt_vid_cap,
1300     .vidioc_try_fmt_vid_cap = zr364xx_vidioc_try_fmt_vid_cap,
1301     .vidioc_s_fmt_vid_cap   = zr364xx_vidioc_s_fmt_vid_cap,
1302     .vidioc_g_fmt_vid_cap   = zr364xx_vidioc_g_fmt_vid_cap,
1303     .vidioc_enum_input  = zr364xx_vidioc_enum_input,
1304     .vidioc_g_input     = zr364xx_vidioc_g_input,
1305     .vidioc_s_input     = zr364xx_vidioc_s_input,
1306     .vidioc_streamon    = zr364xx_vidioc_streamon,
1307     .vidioc_streamoff   = zr364xx_vidioc_streamoff,
1308     .vidioc_reqbufs         = zr364xx_vidioc_reqbufs,
1309     .vidioc_querybuf        = zr364xx_vidioc_querybuf,
1310     .vidioc_qbuf            = zr364xx_vidioc_qbuf,
1311     .vidioc_dqbuf           = zr364xx_vidioc_dqbuf,
1312     .vidioc_log_status      = v4l2_ctrl_log_status,
1313     .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1314     .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1315 };
1316 
1317 static const struct video_device zr364xx_template = {
1318     .name = DRIVER_DESC,
1319     .fops = &zr364xx_fops,
1320     .ioctl_ops = &zr364xx_ioctl_ops,
1321     .release = video_device_release_empty,
1322     .device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
1323                V4L2_CAP_STREAMING,
1324 };
1325 
1326 
1327 
1328 /*******************/
1329 /* USB integration */
1330 /*******************/
1331 static int zr364xx_board_init(struct zr364xx_camera *cam)
1332 {
1333     struct zr364xx_pipeinfo *pipe = cam->pipe;
1334     unsigned long i;
1335     int err;
1336 
1337     DBG("board init: %p\n", cam);
1338     memset(pipe, 0, sizeof(*pipe));
1339     pipe->cam = cam;
1340     pipe->transfer_size = BUFFER_SIZE;
1341 
1342     pipe->transfer_buffer = kzalloc(pipe->transfer_size,
1343                     GFP_KERNEL);
1344     if (!pipe->transfer_buffer) {
1345         DBG("out of memory!\n");
1346         return -ENOMEM;
1347     }
1348 
1349     cam->b_acquire = 0;
1350     cam->frame_count = 0;
1351 
1352     /*** start create system buffers ***/
1353     for (i = 0; i < FRAMES; i++) {
1354         /* always allocate maximum size for system buffers */
1355         cam->buffer.frame[i].lpvbits = vmalloc(MAX_FRAME_SIZE);
1356 
1357         DBG("valloc %p, idx %lu, pdata %p\n",
1358             &cam->buffer.frame[i], i,
1359             cam->buffer.frame[i].lpvbits);
1360         if (!cam->buffer.frame[i].lpvbits) {
1361             printk(KERN_INFO KBUILD_MODNAME ": out of memory. Using less frames\n");
1362             break;
1363         }
1364     }
1365 
1366     if (i == 0) {
1367         printk(KERN_INFO KBUILD_MODNAME ": out of memory. Aborting\n");
1368         err = -ENOMEM;
1369         goto err_free;
1370     } else
1371         cam->buffer.dwFrames = i;
1372 
1373     /* make sure internal states are set */
1374     for (i = 0; i < FRAMES; i++) {
1375         cam->buffer.frame[i].ulState = ZR364XX_READ_IDLE;
1376         cam->buffer.frame[i].cur_size = 0;
1377     }
1378 
1379     cam->cur_frame = 0;
1380     cam->last_frame = -1;
1381     /*** end create system buffers ***/
1382 
1383     /* start read pipe */
1384     err = zr364xx_start_readpipe(cam);
1385     if (err)
1386         goto err_free_frames;
1387 
1388     DBG(": board initialized\n");
1389     return 0;
1390 
1391 err_free_frames:
1392     for (i = 0; i < FRAMES; i++)
1393         vfree(cam->buffer.frame[i].lpvbits);
1394 err_free:
1395     kfree(cam->pipe->transfer_buffer);
1396     cam->pipe->transfer_buffer = NULL;
1397     return err;
1398 }
1399 
1400 static int zr364xx_probe(struct usb_interface *intf,
1401              const struct usb_device_id *id)
1402 {
1403     struct usb_device *udev = interface_to_usbdev(intf);
1404     struct zr364xx_camera *cam = NULL;
1405     struct usb_host_interface *iface_desc;
1406     struct usb_endpoint_descriptor *endpoint;
1407     struct v4l2_ctrl_handler *hdl;
1408     int err;
1409     int i;
1410 
1411     DBG("probing...\n");
1412 
1413     dev_info(&intf->dev, DRIVER_DESC " compatible webcam plugged\n");
1414     dev_info(&intf->dev, "model %04x:%04x detected\n",
1415          le16_to_cpu(udev->descriptor.idVendor),
1416          le16_to_cpu(udev->descriptor.idProduct));
1417 
1418     cam = kzalloc(sizeof(*cam), GFP_KERNEL);
1419     if (!cam)
1420         return -ENOMEM;
1421 
1422     err = v4l2_device_register(&intf->dev, &cam->v4l2_dev);
1423     if (err < 0) {
1424         dev_err(&udev->dev, "couldn't register v4l2_device\n");
1425         goto free_cam;
1426     }
1427     hdl = &cam->ctrl_handler;
1428     v4l2_ctrl_handler_init(hdl, 1);
1429     v4l2_ctrl_new_std(hdl, &zr364xx_ctrl_ops,
1430               V4L2_CID_BRIGHTNESS, 0, 127, 1, 64);
1431     if (hdl->error) {
1432         err = hdl->error;
1433         dev_err(&udev->dev, "couldn't register control\n");
1434         goto free_hdlr_and_unreg_dev;
1435     }
1436     /* save the init method used by this camera */
1437     cam->method = id->driver_info;
1438     mutex_init(&cam->lock);
1439     cam->vdev = zr364xx_template;
1440     cam->vdev.lock = &cam->lock;
1441     cam->vdev.v4l2_dev = &cam->v4l2_dev;
1442     cam->vdev.ctrl_handler = &cam->ctrl_handler;
1443     video_set_drvdata(&cam->vdev, cam);
1444 
1445     cam->udev = udev;
1446 
1447     switch (mode) {
1448     case 1:
1449         dev_info(&udev->dev, "160x120 mode selected\n");
1450         cam->width = 160;
1451         cam->height = 120;
1452         break;
1453     case 2:
1454         dev_info(&udev->dev, "640x480 mode selected\n");
1455         cam->width = 640;
1456         cam->height = 480;
1457         break;
1458     default:
1459         dev_info(&udev->dev, "320x240 mode selected\n");
1460         cam->width = 320;
1461         cam->height = 240;
1462         break;
1463     }
1464 
1465     m0d1[0] = mode;
1466     m1[2].value = 0xf000 + mode;
1467     m2[1].value = 0xf000 + mode;
1468 
1469     /* special case for METHOD3, the modes are different */
1470     if (cam->method == METHOD3) {
1471         switch (mode) {
1472         case 1:
1473             m2[1].value = 0xf000 + 4;
1474             break;
1475         case 2:
1476             m2[1].value = 0xf000 + 0;
1477             break;
1478         default:
1479             m2[1].value = 0xf000 + 1;
1480             break;
1481         }
1482     }
1483 
1484     header2[437] = cam->height / 256;
1485     header2[438] = cam->height % 256;
1486     header2[439] = cam->width / 256;
1487     header2[440] = cam->width % 256;
1488 
1489     cam->nb = 0;
1490 
1491     DBG("dev: %p, udev %p interface %p\n", cam, cam->udev, intf);
1492 
1493     /* set up the endpoint information  */
1494     iface_desc = intf->cur_altsetting;
1495     DBG("num endpoints %d\n", iface_desc->desc.bNumEndpoints);
1496     for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1497         endpoint = &iface_desc->endpoint[i].desc;
1498         if (!cam->read_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
1499             /* we found the bulk in endpoint */
1500             cam->read_endpoint = endpoint->bEndpointAddress;
1501         }
1502     }
1503 
1504     if (!cam->read_endpoint) {
1505         err = -ENOMEM;
1506         dev_err(&intf->dev, "Could not find bulk-in endpoint\n");
1507         goto free_hdlr_and_unreg_dev;
1508     }
1509 
1510     /* v4l */
1511     INIT_LIST_HEAD(&cam->vidq.active);
1512     cam->vidq.cam = cam;
1513 
1514     usb_set_intfdata(intf, cam);
1515 
1516     /* load zr364xx board specific */
1517     err = zr364xx_board_init(cam);
1518     if (err)
1519         goto free_hdlr_and_unreg_dev;
1520     err = v4l2_ctrl_handler_setup(hdl);
1521     if (err)
1522         goto board_uninit;
1523 
1524     spin_lock_init(&cam->slock);
1525 
1526     cam->fmt = formats;
1527 
1528     videobuf_queue_vmalloc_init(&cam->vb_vidq, &zr364xx_video_qops,
1529                     NULL, &cam->slock,
1530                     V4L2_BUF_TYPE_VIDEO_CAPTURE,
1531                     V4L2_FIELD_NONE,
1532                     sizeof(struct zr364xx_buffer), cam, &cam->lock);
1533 
1534     err = video_register_device(&cam->vdev, VFL_TYPE_VIDEO, -1);
1535     if (err) {
1536         dev_err(&udev->dev, "video_register_device failed\n");
1537         goto board_uninit;
1538     }
1539     cam->v4l2_dev.release = zr364xx_release;
1540 
1541     dev_info(&udev->dev, DRIVER_DESC " controlling device %s\n",
1542          video_device_node_name(&cam->vdev));
1543     return 0;
1544 
1545 board_uninit:
1546     zr364xx_board_uninit(cam);
1547 free_hdlr_and_unreg_dev:
1548     v4l2_ctrl_handler_free(hdl);
1549     v4l2_device_unregister(&cam->v4l2_dev);
1550 free_cam:
1551     kfree(cam);
1552     return err;
1553 }
1554 
1555 
1556 static void zr364xx_disconnect(struct usb_interface *intf)
1557 {
1558     struct zr364xx_camera *cam = usb_get_intfdata(intf);
1559 
1560     mutex_lock(&cam->lock);
1561     usb_set_intfdata(intf, NULL);
1562     dev_info(&intf->dev, DRIVER_DESC " webcam unplugged\n");
1563     video_unregister_device(&cam->vdev);
1564     v4l2_device_disconnect(&cam->v4l2_dev);
1565 
1566     /* stops the read pipe if it is running */
1567     if (cam->b_acquire)
1568         zr364xx_stop_acquire(cam);
1569 
1570     zr364xx_stop_readpipe(cam);
1571     mutex_unlock(&cam->lock);
1572     v4l2_device_put(&cam->v4l2_dev);
1573 }
1574 
1575 
1576 #ifdef CONFIG_PM
1577 static int zr364xx_suspend(struct usb_interface *intf, pm_message_t message)
1578 {
1579     struct zr364xx_camera *cam = usb_get_intfdata(intf);
1580 
1581     cam->was_streaming = cam->b_acquire;
1582     if (!cam->was_streaming)
1583         return 0;
1584     zr364xx_stop_acquire(cam);
1585     zr364xx_stop_readpipe(cam);
1586     return 0;
1587 }
1588 
1589 static int zr364xx_resume(struct usb_interface *intf)
1590 {
1591     struct zr364xx_camera *cam = usb_get_intfdata(intf);
1592     int res;
1593 
1594     if (!cam->was_streaming)
1595         return 0;
1596 
1597     res = zr364xx_start_readpipe(cam);
1598     if (res)
1599         return res;
1600 
1601     res = zr364xx_prepare(cam);
1602     if (res)
1603         goto err_prepare;
1604 
1605     zr364xx_start_acquire(cam);
1606     return 0;
1607 
1608 err_prepare:
1609     zr364xx_stop_readpipe(cam);
1610     return res;
1611 }
1612 #endif
1613 
1614 /**********************/
1615 /* Module integration */
1616 /**********************/
1617 
1618 static struct usb_driver zr364xx_driver = {
1619     .name = "zr364xx",
1620     .probe = zr364xx_probe,
1621     .disconnect = zr364xx_disconnect,
1622 #ifdef CONFIG_PM
1623     .suspend = zr364xx_suspend,
1624     .resume = zr364xx_resume,
1625     .reset_resume = zr364xx_resume,
1626 #endif
1627     .id_table = device_table
1628 };
1629 
1630 module_usb_driver(zr364xx_driver);
1631 
1632 MODULE_AUTHOR(DRIVER_AUTHOR);
1633 MODULE_DESCRIPTION(DRIVER_DESC);
1634 MODULE_LICENSE("GPL");
1635 MODULE_VERSION(DRIVER_VERSION);