1 /***************************************************************************
2 * Video4Linux2 driver for ZC0301[P] Image Processor and Control Chip *
4 * Copyright (C) 2006 by Luca Risolia <luca.risolia@studio.unibo.it> *
6 * Informations about the chip internals needed to enable the I2C protocol *
7 * have been taken from the documentation of the ZC030x Video4Linux1 *
8 * driver written by Andrew Birkett <andy@nobugs.org> *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
15 * This program is distributed in the hope that it will be useful, *
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18 * GNU General Public License for more details. *
20 * You should have received a copy of the GNU General Public License *
21 * along with this program; if not, write to the Free Software *
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
23 ***************************************************************************/
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/kernel.h>
28 #include <linux/param.h>
29 #include <linux/moduleparam.h>
30 #include <linux/errno.h>
31 #include <linux/slab.h>
32 #include <linux/device.h>
34 #include <linux/delay.h>
35 #include <linux/compiler.h>
36 #include <linux/ioctl.h>
37 #include <linux/poll.h>
38 #include <linux/stat.h>
40 #include <linux/vmalloc.h>
41 #include <linux/page-flags.h>
42 #include <linux/byteorder/generic.h>
44 #include <asm/uaccess.h>
48 /*****************************************************************************/
50 #define ZC0301_MODULE_NAME "V4L2 driver for ZC0301[P] " \
51 "Image Processor and Control Chip"
52 #define ZC0301_MODULE_AUTHOR "(C) 2006 Luca Risolia"
53 #define ZC0301_AUTHOR_EMAIL "<luca.risolia@studio.unibo.it>"
54 #define ZC0301_MODULE_LICENSE "GPL"
55 #define ZC0301_MODULE_VERSION "1:1.05"
56 #define ZC0301_MODULE_VERSION_CODE KERNEL_VERSION(1, 0, 5)
58 /*****************************************************************************/
60 MODULE_DEVICE_TABLE(usb, zc0301_id_table);
62 MODULE_AUTHOR(ZC0301_MODULE_AUTHOR " " ZC0301_AUTHOR_EMAIL);
63 MODULE_DESCRIPTION(ZC0301_MODULE_NAME);
64 MODULE_VERSION(ZC0301_MODULE_VERSION);
65 MODULE_LICENSE(ZC0301_MODULE_LICENSE);
67 static short video_nr[] = {[0 ... ZC0301_MAX_DEVICES-1] = -1};
68 module_param_array(video_nr, short, NULL, 0444);
69 MODULE_PARM_DESC(video_nr,
70 "\n<-1|n[,...]> Specify V4L2 minor mode number."
71 "\n -1 = use next available (default)"
72 "\n n = use minor number n (integer >= 0)"
73 "\nYou can specify up to "
74 __MODULE_STRING(ZC0301_MAX_DEVICES) " cameras this way."
76 "\nvideo_nr=-1,2,-1 would assign minor number 2 to"
77 "\nthe second registered camera and use auto for the first"
78 "\none and for every other camera."
81 static short force_munmap[] = {[0 ... ZC0301_MAX_DEVICES-1] =
83 module_param_array(force_munmap, bool, NULL, 0444);
84 MODULE_PARM_DESC(force_munmap,
85 "\n<0|1[,...]> Force the application to unmap previously"
86 "\nmapped buffer memory before calling any VIDIOC_S_CROP or"
87 "\nVIDIOC_S_FMT ioctl's. Not all the applications support"
88 "\nthis feature. This parameter is specific for each"
90 "\n 0 = do not force memory unmapping"
91 "\n 1 = force memory unmapping (save memory)"
92 "\nDefault value is "__MODULE_STRING(SN9C102_FORCE_MUNMAP)"."
95 static unsigned int frame_timeout[] = {[0 ... ZC0301_MAX_DEVICES-1] =
96 ZC0301_FRAME_TIMEOUT};
97 module_param_array(frame_timeout, uint, NULL, 0644);
98 MODULE_PARM_DESC(frame_timeout,
99 "\n<n[,...]> Timeout for a video frame in seconds."
100 "\nThis parameter is specific for each detected camera."
101 "\nDefault value is "__MODULE_STRING(ZC0301_FRAME_TIMEOUT)"."
105 static unsigned short debug = ZC0301_DEBUG_LEVEL;
106 module_param(debug, ushort, 0644);
107 MODULE_PARM_DESC(debug,
108 "\n<n> Debugging information level, from 0 to 3:"
109 "\n0 = none (use carefully)"
110 "\n1 = critical errors"
111 "\n2 = significant informations"
112 "\n3 = more verbose messages"
113 "\nLevel 3 is useful for testing only, when only "
114 "one device is used."
115 "\nDefault value is "__MODULE_STRING(ZC0301_DEBUG_LEVEL)"."
119 /*****************************************************************************/
122 zc0301_request_buffers(struct zc0301_device* cam, u32 count,
123 enum zc0301_io_method io)
125 struct v4l2_pix_format* p = &(cam->sensor.pix_format);
126 struct v4l2_rect* r = &(cam->sensor.cropcap.bounds);
127 const size_t imagesize = cam->module_param.force_munmap ||
129 (p->width * p->height * p->priv) / 8 :
130 (r->width * r->height * p->priv) / 8;
134 if (count > ZC0301_MAX_FRAMES)
135 count = ZC0301_MAX_FRAMES;
137 cam->nbuffers = count;
138 while (cam->nbuffers > 0) {
139 if ((buff = vmalloc_32(cam->nbuffers * PAGE_ALIGN(imagesize))))
144 for (i = 0; i < cam->nbuffers; i++) {
145 cam->frame[i].bufmem = buff + i*PAGE_ALIGN(imagesize);
146 cam->frame[i].buf.index = i;
147 cam->frame[i].buf.m.offset = i*PAGE_ALIGN(imagesize);
148 cam->frame[i].buf.length = imagesize;
149 cam->frame[i].buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
150 cam->frame[i].buf.sequence = 0;
151 cam->frame[i].buf.field = V4L2_FIELD_NONE;
152 cam->frame[i].buf.memory = V4L2_MEMORY_MMAP;
153 cam->frame[i].buf.flags = 0;
156 return cam->nbuffers;
160 static void zc0301_release_buffers(struct zc0301_device* cam)
163 vfree(cam->frame[0].bufmem);
166 cam->frame_current = NULL;
170 static void zc0301_empty_framequeues(struct zc0301_device* cam)
174 INIT_LIST_HEAD(&cam->inqueue);
175 INIT_LIST_HEAD(&cam->outqueue);
177 for (i = 0; i < ZC0301_MAX_FRAMES; i++) {
178 cam->frame[i].state = F_UNUSED;
179 cam->frame[i].buf.bytesused = 0;
184 static void zc0301_requeue_outqueue(struct zc0301_device* cam)
186 struct zc0301_frame_t *i;
188 list_for_each_entry(i, &cam->outqueue, frame) {
190 list_add(&i->frame, &cam->inqueue);
193 INIT_LIST_HEAD(&cam->outqueue);
197 static void zc0301_queue_unusedframes(struct zc0301_device* cam)
199 unsigned long lock_flags;
202 for (i = 0; i < cam->nbuffers; i++)
203 if (cam->frame[i].state == F_UNUSED) {
204 cam->frame[i].state = F_QUEUED;
205 spin_lock_irqsave(&cam->queue_lock, lock_flags);
206 list_add_tail(&cam->frame[i].frame, &cam->inqueue);
207 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
211 /*****************************************************************************/
213 int zc0301_write_reg(struct zc0301_device* cam, u16 index, u16 value)
215 struct usb_device* udev = cam->usbdev;
218 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0xa0, 0x40,
219 value, index, NULL, 0, ZC0301_CTRL_TIMEOUT);
221 DBG(3, "Failed to write a register (index 0x%04X, "
222 "value 0x%02X, error %d)",index, value, res);
230 int zc0301_read_reg(struct zc0301_device* cam, u16 index)
232 struct usb_device* udev = cam->usbdev;
233 u8* buff = cam->control_buffer;
236 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0xa1, 0xc0,
237 0x0001, index, buff, 1, ZC0301_CTRL_TIMEOUT);
239 DBG(3, "Failed to read a register (index 0x%04X, error %d)",
242 PDBGG("Read: index 0x%04X, value: 0x%04X", index, (int)(*buff));
244 return (res >= 0) ? (int)(*buff) : -1;
248 int zc0301_i2c_read(struct zc0301_device* cam, u16 address, u8 length)
250 int err = 0, res, r0, r1;
252 err += zc0301_write_reg(cam, 0x0092, address);
253 err += zc0301_write_reg(cam, 0x0090, 0x02);
257 res = zc0301_read_reg(cam, 0x0091);
260 r0 = zc0301_read_reg(cam, 0x0095);
263 r1 = zc0301_read_reg(cam, 0x0096);
267 res = (length <= 1) ? r0 : r0 | (r1 << 8);
270 DBG(3, "I2C read failed at address 0x%04X, value: 0x%04X",
274 PDBGG("I2C read: address 0x%04X, value: 0x%04X", address, res);
276 return err ? -1 : res;
280 int zc0301_i2c_write(struct zc0301_device* cam, u16 address, u16 value)
284 err += zc0301_write_reg(cam, 0x0092, address);
285 err += zc0301_write_reg(cam, 0x0093, value & 0xff);
286 err += zc0301_write_reg(cam, 0x0094, value >> 8);
287 err += zc0301_write_reg(cam, 0x0090, 0x01);
291 res = zc0301_read_reg(cam, 0x0091);
296 DBG(3, "I2C write failed at address 0x%04X, value: 0x%04X",
299 PDBGG("I2C write: address 0x%04X, value: 0x%04X", address, value);
304 /*****************************************************************************/
306 static void zc0301_urb_complete(struct urb *urb, struct pt_regs* regs)
308 struct zc0301_device* cam = urb->context;
309 struct zc0301_frame_t** f;
314 if (urb->status == -ENOENT)
317 f = &cam->frame_current;
319 if (cam->stream == STREAM_INTERRUPT) {
320 cam->stream = STREAM_OFF;
322 (*f)->state = F_QUEUED;
323 DBG(3, "Stream interrupted");
324 wake_up(&cam->wait_stream);
327 if (cam->state & DEV_DISCONNECTED)
330 if (cam->state & DEV_MISCONFIGURED) {
331 wake_up_interruptible(&cam->wait_frame);
335 if (cam->stream == STREAM_OFF || list_empty(&cam->inqueue))
339 (*f) = list_entry(cam->inqueue.next, struct zc0301_frame_t,
342 imagesize = (cam->sensor.pix_format.width *
343 cam->sensor.pix_format.height *
344 cam->sensor.pix_format.priv) / 8;
346 for (i = 0; i < urb->number_of_packets; i++) {
347 unsigned int len, status;
352 len = urb->iso_frame_desc[i].actual_length;
353 status = urb->iso_frame_desc[i].status;
354 pos = urb->iso_frame_desc[i].offset + urb->transfer_buffer;
357 DBG(3, "Error in isochronous frame");
358 (*f)->state = F_ERROR;
362 sof = (*(soi = pos) == 0xd8ff);
364 PDBGG("Isochrnous frame: length %u, #%u i,", len, i);
366 if ((*f)->state == F_QUEUED || (*f)->state == F_ERROR)
369 (*f)->state = F_GRABBING;
370 (*f)->buf.bytesused = 0;
371 do_gettimeofday(&(*f)->buf.timestamp);
372 DBG(3, "SOF detected: new video frame");
375 if ((*f)->state == F_GRABBING) {
376 if (sof && (*f)->buf.bytesused)
379 if ((*f)->buf.bytesused + len > imagesize) {
380 DBG(3, "Video frame size exceeded");
381 (*f)->state = F_ERROR;
385 memcpy((*f)->bufmem+(*f)->buf.bytesused, pos, len);
386 (*f)->buf.bytesused += len;
388 if ((*f)->buf.bytesused == imagesize) {
391 b = (*f)->buf.bytesused;
392 (*f)->state = F_DONE;
393 (*f)->buf.sequence= ++cam->frame_count;
394 spin_lock(&cam->queue_lock);
395 list_move_tail(&(*f)->frame, &cam->outqueue);
396 if (!list_empty(&cam->inqueue))
397 (*f) = list_entry(cam->inqueue.next,
398 struct zc0301_frame_t,
402 spin_unlock(&cam->queue_lock);
403 DBG(3, "Video frame captured: : %lu bytes",
416 urb->dev = cam->usbdev;
417 err = usb_submit_urb(urb, GFP_ATOMIC);
418 if (err < 0 && err != -EPERM) {
419 cam->state |= DEV_MISCONFIGURED;
420 DBG(1, "usb_submit_urb() failed");
423 wake_up_interruptible(&cam->wait_frame);
427 static int zc0301_start_transfer(struct zc0301_device* cam)
429 struct usb_device *udev = cam->usbdev;
430 struct usb_host_interface* altsetting = usb_altnum_to_altsetting(
431 usb_ifnum_to_if(udev, 0),
432 ZC0301_ALTERNATE_SETTING);
433 const unsigned int psz = altsetting->endpoint[0].desc.wMaxPacketSize;
438 for (i = 0; i < ZC0301_URBS; i++) {
439 cam->transfer_buffer[i] = kzalloc(ZC0301_ISO_PACKETS * psz,
441 if (!cam->transfer_buffer[i]) {
443 DBG(1, "Not enough memory");
448 for (i = 0; i < ZC0301_URBS; i++) {
449 urb = usb_alloc_urb(ZC0301_ISO_PACKETS, GFP_KERNEL);
453 DBG(1, "usb_alloc_urb() failed");
458 urb->pipe = usb_rcvisocpipe(udev, 1);
459 urb->transfer_flags = URB_ISO_ASAP;
460 urb->number_of_packets = ZC0301_ISO_PACKETS;
461 urb->complete = zc0301_urb_complete;
462 urb->transfer_buffer = cam->transfer_buffer[i];
463 urb->transfer_buffer_length = psz * ZC0301_ISO_PACKETS;
465 for (j = 0; j < ZC0301_ISO_PACKETS; j++) {
466 urb->iso_frame_desc[j].offset = psz * j;
467 urb->iso_frame_desc[j].length = psz;
471 err = usb_set_interface(udev, 0, ZC0301_ALTERNATE_SETTING);
473 DBG(1, "usb_set_interface() failed");
477 cam->frame_current = NULL;
479 for (i = 0; i < ZC0301_URBS; i++) {
480 err = usb_submit_urb(cam->urb[i], GFP_KERNEL);
482 for (j = i-1; j >= 0; j--)
483 usb_kill_urb(cam->urb[j]);
484 DBG(1, "usb_submit_urb() failed, error %d", err);
492 for (i = 0; (i < ZC0301_URBS) && cam->urb[i]; i++)
493 usb_free_urb(cam->urb[i]);
496 for (i = 0; (i < ZC0301_URBS) && cam->transfer_buffer[i]; i++)
497 kfree(cam->transfer_buffer[i]);
503 static int zc0301_stop_transfer(struct zc0301_device* cam)
505 struct usb_device *udev = cam->usbdev;
509 if (cam->state & DEV_DISCONNECTED)
512 for (i = ZC0301_URBS-1; i >= 0; i--) {
513 usb_kill_urb(cam->urb[i]);
514 usb_free_urb(cam->urb[i]);
515 kfree(cam->transfer_buffer[i]);
518 err = usb_set_interface(udev, 0, 0); /* 0 Mb/s */
520 DBG(3, "usb_set_interface() failed");
526 static int zc0301_stream_interrupt(struct zc0301_device* cam)
530 cam->stream = STREAM_INTERRUPT;
531 timeout = wait_event_timeout(cam->wait_stream,
532 (cam->stream == STREAM_OFF) ||
533 (cam->state & DEV_DISCONNECTED),
535 if (cam->state & DEV_DISCONNECTED)
537 else if (cam->stream != STREAM_OFF) {
538 cam->state |= DEV_MISCONFIGURED;
539 DBG(1, "URB timeout reached. The camera is misconfigured. To "
540 "use it, close and open /dev/video%d again.",
548 /*****************************************************************************/
551 zc0301_set_compression(struct zc0301_device* cam,
552 struct v4l2_jpegcompression* compression)
556 if ((r = zc0301_read_reg(cam, 0x0008)) < 0)
558 err += zc0301_write_reg(cam, 0x0008, r | 0x11 | compression->quality);
560 return err ? -EIO : 0;
564 static int zc0301_init(struct zc0301_device* cam)
566 struct zc0301_sensor* s = &cam->sensor;
567 struct v4l2_control ctrl;
568 struct v4l2_queryctrl *qctrl;
569 struct v4l2_rect* rect;
573 if (!(cam->state & DEV_INITIALIZED)) {
574 init_waitqueue_head(&cam->open);
576 rect = &(s->cropcap.defrect);
577 cam->compression.quality = ZC0301_COMPRESSION_QUALITY;
578 } else { /* use current values */
586 DBG(3, "Sensor initialization failed");
591 if ((err = zc0301_set_compression(cam, &cam->compression))) {
592 DBG(3, "set_compression() failed");
597 if ((err = s->set_crop(cam, rect))) {
598 DBG(3, "set_crop() failed");
603 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
604 if (s->qctrl[i].id != 0 &&
605 !(s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)) {
606 ctrl.id = s->qctrl[i].id;
607 ctrl.value = qctrl[i].default_value;
608 err = s->set_ctrl(cam, &ctrl);
610 DBG(3, "Set %s control failed",
614 DBG(3, "Image sensor supports '%s' control",
619 if (!(cam->state & DEV_INITIALIZED)) {
620 mutex_init(&cam->fileop_mutex);
621 spin_lock_init(&cam->queue_lock);
622 init_waitqueue_head(&cam->wait_frame);
623 init_waitqueue_head(&cam->wait_stream);
624 cam->nreadbuffers = 2;
625 memcpy(s->_qctrl, s->qctrl, sizeof(s->qctrl));
626 memcpy(&(s->_rect), &(s->cropcap.defrect),
627 sizeof(struct v4l2_rect));
628 cam->state |= DEV_INITIALIZED;
631 DBG(2, "Initialization succeeded");
636 static void zc0301_release_resources(struct zc0301_device* cam)
638 DBG(2, "V4L2 device /dev/video%d deregistered", cam->v4ldev->minor);
639 video_set_drvdata(cam->v4ldev, NULL);
640 video_unregister_device(cam->v4ldev);
641 kfree(cam->control_buffer);
644 /*****************************************************************************/
646 static int zc0301_open(struct inode* inode, struct file* filp)
648 struct zc0301_device* cam;
652 This is the only safe way to prevent race conditions with
655 if (!down_read_trylock(&zc0301_disconnect))
658 cam = video_get_drvdata(video_devdata(filp));
660 if (mutex_lock_interruptible(&cam->dev_mutex)) {
661 up_read(&zc0301_disconnect);
666 DBG(2, "Device /dev/video%d is busy...", cam->v4ldev->minor);
667 if ((filp->f_flags & O_NONBLOCK) ||
668 (filp->f_flags & O_NDELAY)) {
672 mutex_unlock(&cam->dev_mutex);
673 err = wait_event_interruptible_exclusive(cam->open,
674 cam->state & DEV_DISCONNECTED
677 up_read(&zc0301_disconnect);
680 if (cam->state & DEV_DISCONNECTED) {
681 up_read(&zc0301_disconnect);
684 mutex_lock(&cam->dev_mutex);
688 if (cam->state & DEV_MISCONFIGURED) {
689 err = zc0301_init(cam);
691 DBG(1, "Initialization failed again. "
692 "I will retry on next open().");
695 cam->state &= ~DEV_MISCONFIGURED;
698 if ((err = zc0301_start_transfer(cam)))
701 filp->private_data = cam;
704 cam->stream = STREAM_OFF;
706 cam->frame_count = 0;
707 zc0301_empty_framequeues(cam);
709 DBG(3, "Video device /dev/video%d is open", cam->v4ldev->minor);
712 mutex_unlock(&cam->dev_mutex);
713 up_read(&zc0301_disconnect);
718 static int zc0301_release(struct inode* inode, struct file* filp)
720 struct zc0301_device* cam = video_get_drvdata(video_devdata(filp));
722 mutex_lock(&cam->dev_mutex); /* prevent disconnect() to be called */
724 zc0301_stop_transfer(cam);
726 zc0301_release_buffers(cam);
728 if (cam->state & DEV_DISCONNECTED) {
729 zc0301_release_resources(cam);
730 usb_put_dev(cam->usbdev);
731 mutex_unlock(&cam->dev_mutex);
737 wake_up_interruptible_nr(&cam->open, 1);
739 DBG(3, "Video device /dev/video%d closed", cam->v4ldev->minor);
741 mutex_unlock(&cam->dev_mutex);
748 zc0301_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
750 struct zc0301_device* cam = video_get_drvdata(video_devdata(filp));
751 struct zc0301_frame_t* f, * i;
752 unsigned long lock_flags;
756 if (mutex_lock_interruptible(&cam->fileop_mutex))
759 if (cam->state & DEV_DISCONNECTED) {
760 DBG(1, "Device not present");
761 mutex_unlock(&cam->fileop_mutex);
765 if (cam->state & DEV_MISCONFIGURED) {
766 DBG(1, "The camera is misconfigured. Close and open it "
768 mutex_unlock(&cam->fileop_mutex);
772 if (cam->io == IO_MMAP) {
773 DBG(3, "Close and open the device again to choose the read "
775 mutex_unlock(&cam->fileop_mutex);
779 if (cam->io == IO_NONE) {
780 if (!zc0301_request_buffers(cam, cam->nreadbuffers, IO_READ)) {
781 DBG(1, "read() failed, not enough memory");
782 mutex_unlock(&cam->fileop_mutex);
786 cam->stream = STREAM_ON;
789 if (list_empty(&cam->inqueue)) {
790 if (!list_empty(&cam->outqueue))
791 zc0301_empty_framequeues(cam);
792 zc0301_queue_unusedframes(cam);
796 mutex_unlock(&cam->fileop_mutex);
800 if (list_empty(&cam->outqueue)) {
801 if (filp->f_flags & O_NONBLOCK) {
802 mutex_unlock(&cam->fileop_mutex);
805 timeout = wait_event_interruptible_timeout
807 (!list_empty(&cam->outqueue)) ||
808 (cam->state & DEV_DISCONNECTED) ||
809 (cam->state & DEV_MISCONFIGURED),
810 cam->module_param.frame_timeout *
811 1000 * msecs_to_jiffies(1) );
813 mutex_unlock(&cam->fileop_mutex);
816 if (cam->state & DEV_DISCONNECTED) {
817 mutex_unlock(&cam->fileop_mutex);
820 if (!timeout || (cam->state & DEV_MISCONFIGURED)) {
821 mutex_unlock(&cam->fileop_mutex);
826 f = list_entry(cam->outqueue.prev, struct zc0301_frame_t, frame);
828 if (count > f->buf.bytesused)
829 count = f->buf.bytesused;
831 if (copy_to_user(buf, f->bufmem, count)) {
838 spin_lock_irqsave(&cam->queue_lock, lock_flags);
839 list_for_each_entry(i, &cam->outqueue, frame)
841 INIT_LIST_HEAD(&cam->outqueue);
842 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
844 zc0301_queue_unusedframes(cam);
846 PDBGG("Frame #%lu, bytes read: %zu",
847 (unsigned long)f->buf.index, count);
849 mutex_unlock(&cam->fileop_mutex);
851 return err ? err : count;
855 static unsigned int zc0301_poll(struct file *filp, poll_table *wait)
857 struct zc0301_device* cam = video_get_drvdata(video_devdata(filp));
858 struct zc0301_frame_t* f;
859 unsigned long lock_flags;
860 unsigned int mask = 0;
862 if (mutex_lock_interruptible(&cam->fileop_mutex))
865 if (cam->state & DEV_DISCONNECTED) {
866 DBG(1, "Device not present");
870 if (cam->state & DEV_MISCONFIGURED) {
871 DBG(1, "The camera is misconfigured. Close and open it "
876 if (cam->io == IO_NONE) {
877 if (!zc0301_request_buffers(cam, cam->nreadbuffers, IO_READ)) {
878 DBG(1, "poll() failed, not enough memory");
882 cam->stream = STREAM_ON;
885 if (cam->io == IO_READ) {
886 spin_lock_irqsave(&cam->queue_lock, lock_flags);
887 list_for_each_entry(f, &cam->outqueue, frame)
889 INIT_LIST_HEAD(&cam->outqueue);
890 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
891 zc0301_queue_unusedframes(cam);
894 poll_wait(filp, &cam->wait_frame, wait);
896 if (!list_empty(&cam->outqueue))
897 mask |= POLLIN | POLLRDNORM;
899 mutex_unlock(&cam->fileop_mutex);
904 mutex_unlock(&cam->fileop_mutex);
909 static void zc0301_vm_open(struct vm_area_struct* vma)
911 struct zc0301_frame_t* f = vma->vm_private_data;
916 static void zc0301_vm_close(struct vm_area_struct* vma)
918 /* NOTE: buffers are not freed here */
919 struct zc0301_frame_t* f = vma->vm_private_data;
924 static struct vm_operations_struct zc0301_vm_ops = {
925 .open = zc0301_vm_open,
926 .close = zc0301_vm_close,
930 static int zc0301_mmap(struct file* filp, struct vm_area_struct *vma)
932 struct zc0301_device* cam = video_get_drvdata(video_devdata(filp));
933 unsigned long size = vma->vm_end - vma->vm_start,
934 start = vma->vm_start;
938 if (mutex_lock_interruptible(&cam->fileop_mutex))
941 if (cam->state & DEV_DISCONNECTED) {
942 DBG(1, "Device not present");
943 mutex_unlock(&cam->fileop_mutex);
947 if (cam->state & DEV_MISCONFIGURED) {
948 DBG(1, "The camera is misconfigured. Close and open it "
950 mutex_unlock(&cam->fileop_mutex);
954 if (cam->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) ||
955 size != PAGE_ALIGN(cam->frame[0].buf.length)) {
956 mutex_unlock(&cam->fileop_mutex);
960 for (i = 0; i < cam->nbuffers; i++) {
961 if ((cam->frame[i].buf.m.offset>>PAGE_SHIFT) == vma->vm_pgoff)
964 if (i == cam->nbuffers) {
965 mutex_unlock(&cam->fileop_mutex);
969 vma->vm_flags |= VM_IO;
970 vma->vm_flags |= VM_RESERVED;
972 pos = cam->frame[i].bufmem;
973 while (size > 0) { /* size is page-aligned */
974 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
975 mutex_unlock(&cam->fileop_mutex);
983 vma->vm_ops = &zc0301_vm_ops;
984 vma->vm_private_data = &cam->frame[i];
988 mutex_unlock(&cam->fileop_mutex);
993 /*****************************************************************************/
996 zc0301_vidioc_querycap(struct zc0301_device* cam, void __user * arg)
998 struct v4l2_capability cap = {
1000 .version = ZC0301_MODULE_VERSION_CODE,
1001 .capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
1005 strlcpy(cap.card, cam->v4ldev->name, sizeof(cap.card));
1006 if (usb_make_path(cam->usbdev, cap.bus_info, sizeof(cap.bus_info)) < 0)
1007 strlcpy(cap.bus_info, cam->usbdev->dev.bus_id,
1008 sizeof(cap.bus_info));
1010 if (copy_to_user(arg, &cap, sizeof(cap)))
1018 zc0301_vidioc_enuminput(struct zc0301_device* cam, void __user * arg)
1020 struct v4l2_input i;
1022 if (copy_from_user(&i, arg, sizeof(i)))
1028 memset(&i, 0, sizeof(i));
1029 strcpy(i.name, "Camera");
1030 i.type = V4L2_INPUT_TYPE_CAMERA;
1032 if (copy_to_user(arg, &i, sizeof(i)))
1040 zc0301_vidioc_g_input(struct zc0301_device* cam, void __user * arg)
1044 if (copy_to_user(arg, &index, sizeof(index)))
1052 zc0301_vidioc_s_input(struct zc0301_device* cam, void __user * arg)
1056 if (copy_from_user(&index, arg, sizeof(index)))
1067 zc0301_vidioc_query_ctrl(struct zc0301_device* cam, void __user * arg)
1069 struct zc0301_sensor* s = &cam->sensor;
1070 struct v4l2_queryctrl qc;
1073 if (copy_from_user(&qc, arg, sizeof(qc)))
1076 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1077 if (qc.id && qc.id == s->qctrl[i].id) {
1078 memcpy(&qc, &(s->qctrl[i]), sizeof(qc));
1079 if (copy_to_user(arg, &qc, sizeof(qc)))
1089 zc0301_vidioc_g_ctrl(struct zc0301_device* cam, void __user * arg)
1091 struct zc0301_sensor* s = &cam->sensor;
1092 struct v4l2_control ctrl;
1096 if (!s->get_ctrl && !s->set_ctrl)
1099 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1103 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1104 if (ctrl.id == s->qctrl[i].id) {
1105 ctrl.value = s->_qctrl[i].default_value;
1110 err = s->get_ctrl(cam, &ctrl);
1113 if (copy_to_user(arg, &ctrl, sizeof(ctrl)))
1121 zc0301_vidioc_s_ctrl(struct zc0301_device* cam, void __user * arg)
1123 struct zc0301_sensor* s = &cam->sensor;
1124 struct v4l2_control ctrl;
1131 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1134 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1135 if (ctrl.id == s->qctrl[i].id) {
1136 if (s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)
1138 if (ctrl.value < s->qctrl[i].minimum ||
1139 ctrl.value > s->qctrl[i].maximum)
1141 ctrl.value -= ctrl.value % s->qctrl[i].step;
1145 if ((err = s->set_ctrl(cam, &ctrl)))
1148 s->_qctrl[i].default_value = ctrl.value;
1155 zc0301_vidioc_cropcap(struct zc0301_device* cam, void __user * arg)
1157 struct v4l2_cropcap* cc = &(cam->sensor.cropcap);
1159 cc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1160 cc->pixelaspect.numerator = 1;
1161 cc->pixelaspect.denominator = 1;
1163 if (copy_to_user(arg, cc, sizeof(*cc)))
1171 zc0301_vidioc_g_crop(struct zc0301_device* cam, void __user * arg)
1173 struct zc0301_sensor* s = &cam->sensor;
1174 struct v4l2_crop crop = {
1175 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
1178 memcpy(&(crop.c), &(s->_rect), sizeof(struct v4l2_rect));
1180 if (copy_to_user(arg, &crop, sizeof(crop)))
1188 zc0301_vidioc_s_crop(struct zc0301_device* cam, void __user * arg)
1190 struct zc0301_sensor* s = &cam->sensor;
1191 struct v4l2_crop crop;
1192 struct v4l2_rect* rect;
1193 struct v4l2_rect* bounds = &(s->cropcap.bounds);
1194 const enum zc0301_stream_state stream = cam->stream;
1195 const u32 nbuffers = cam->nbuffers;
1199 if (copy_from_user(&crop, arg, sizeof(crop)))
1204 if (crop.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1207 if (cam->module_param.force_munmap)
1208 for (i = 0; i < cam->nbuffers; i++)
1209 if (cam->frame[i].vma_use_count) {
1210 DBG(3, "VIDIOC_S_CROP failed. "
1211 "Unmap the buffers first.");
1216 memcpy(rect, &(s->_rect), sizeof(*rect));
1217 if (copy_to_user(arg, &crop, sizeof(crop)))
1224 if (rect->width < 8)
1226 if (rect->height < 8)
1228 if (rect->width > bounds->width)
1229 rect->width = bounds->width;
1230 if (rect->height > bounds->height)
1231 rect->height = bounds->height;
1232 if (rect->left < bounds->left)
1233 rect->left = bounds->left;
1234 if (rect->top < bounds->top)
1235 rect->top = bounds->top;
1236 if (rect->left + rect->width > bounds->left + bounds->width)
1237 rect->left = bounds->left+bounds->width - rect->width;
1238 if (rect->top + rect->height > bounds->top + bounds->height)
1239 rect->top = bounds->top+bounds->height - rect->height;
1241 rect->height &= ~7L;
1243 if (cam->stream == STREAM_ON)
1244 if ((err = zc0301_stream_interrupt(cam)))
1247 if (copy_to_user(arg, &crop, sizeof(crop))) {
1248 cam->stream = stream;
1252 if (cam->module_param.force_munmap || cam->io == IO_READ)
1253 zc0301_release_buffers(cam);
1256 err += s->set_crop(cam, rect);
1258 if (err) { /* atomic, no rollback in ioctl() */
1259 cam->state |= DEV_MISCONFIGURED;
1260 DBG(1, "VIDIOC_S_CROP failed because of hardware problems. To "
1261 "use the camera, close and open /dev/video%d again.",
1262 cam->v4ldev->minor);
1266 s->pix_format.width = rect->width;
1267 s->pix_format.height = rect->height;
1268 memcpy(&(s->_rect), rect, sizeof(*rect));
1270 if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
1271 nbuffers != zc0301_request_buffers(cam, nbuffers, cam->io)) {
1272 cam->state |= DEV_MISCONFIGURED;
1273 DBG(1, "VIDIOC_S_CROP failed because of not enough memory. To "
1274 "use the camera, close and open /dev/video%d again.",
1275 cam->v4ldev->minor);
1279 if (cam->io == IO_READ)
1280 zc0301_empty_framequeues(cam);
1281 else if (cam->module_param.force_munmap)
1282 zc0301_requeue_outqueue(cam);
1284 cam->stream = stream;
1291 zc0301_vidioc_enum_fmt(struct zc0301_device* cam, void __user * arg)
1293 struct v4l2_fmtdesc fmtd;
1295 if (copy_from_user(&fmtd, arg, sizeof(fmtd)))
1298 if (fmtd.index == 0) {
1299 strcpy(fmtd.description, "JPEG");
1300 fmtd.pixelformat = V4L2_PIX_FMT_JPEG;
1301 fmtd.flags = V4L2_FMT_FLAG_COMPRESSED;
1305 fmtd.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1306 memset(&fmtd.reserved, 0, sizeof(fmtd.reserved));
1308 if (copy_to_user(arg, &fmtd, sizeof(fmtd)))
1316 zc0301_vidioc_g_fmt(struct zc0301_device* cam, void __user * arg)
1318 struct v4l2_format format;
1319 struct v4l2_pix_format* pfmt = &(cam->sensor.pix_format);
1321 if (copy_from_user(&format, arg, sizeof(format)))
1324 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1327 pfmt->bytesperline = 0;
1328 pfmt->sizeimage = pfmt->height * ((pfmt->width*pfmt->priv)/8);
1329 pfmt->field = V4L2_FIELD_NONE;
1330 memcpy(&(format.fmt.pix), pfmt, sizeof(*pfmt));
1332 if (copy_to_user(arg, &format, sizeof(format)))
1340 zc0301_vidioc_try_s_fmt(struct zc0301_device* cam, unsigned int cmd,
1343 struct zc0301_sensor* s = &cam->sensor;
1344 struct v4l2_format format;
1345 struct v4l2_pix_format* pix;
1346 struct v4l2_pix_format* pfmt = &(s->pix_format);
1347 struct v4l2_rect* bounds = &(s->cropcap.bounds);
1348 struct v4l2_rect rect;
1349 const enum zc0301_stream_state stream = cam->stream;
1350 const u32 nbuffers = cam->nbuffers;
1354 if (copy_from_user(&format, arg, sizeof(format)))
1357 pix = &(format.fmt.pix);
1359 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1362 memcpy(&rect, &(s->_rect), sizeof(rect));
1365 pix->width = rect.width;
1366 pix->height = rect.height;
1368 rect.width = pix->width;
1369 rect.height = pix->height;
1374 if (rect.height < 8)
1376 if (rect.width > bounds->left + bounds->width - rect.left)
1377 rect.width = bounds->left + bounds->width - rect.left;
1378 if (rect.height > bounds->top + bounds->height - rect.top)
1379 rect.height = bounds->top + bounds->height - rect.top;
1383 pix->width = rect.width;
1384 pix->height = rect.height;
1385 pix->pixelformat = pfmt->pixelformat;
1386 pix->priv = pfmt->priv;
1387 pix->colorspace = pfmt->colorspace;
1388 pix->bytesperline = 0;
1389 pix->sizeimage = pix->height * ((pix->width * pix->priv) / 8);
1390 pix->field = V4L2_FIELD_NONE;
1392 if (cmd == VIDIOC_TRY_FMT) {
1393 if (copy_to_user(arg, &format, sizeof(format)))
1398 if (cam->module_param.force_munmap)
1399 for (i = 0; i < cam->nbuffers; i++)
1400 if (cam->frame[i].vma_use_count) {
1401 DBG(3, "VIDIOC_S_FMT failed. "
1402 "Unmap the buffers first.");
1406 if (cam->stream == STREAM_ON)
1407 if ((err = zc0301_stream_interrupt(cam)))
1410 if (copy_to_user(arg, &format, sizeof(format))) {
1411 cam->stream = stream;
1415 if (cam->module_param.force_munmap || cam->io == IO_READ)
1416 zc0301_release_buffers(cam);
1419 err += s->set_crop(cam, &rect);
1421 if (err) { /* atomic, no rollback in ioctl() */
1422 cam->state |= DEV_MISCONFIGURED;
1423 DBG(1, "VIDIOC_S_FMT failed because of hardware problems. To "
1424 "use the camera, close and open /dev/video%d again.",
1425 cam->v4ldev->minor);
1429 memcpy(pfmt, pix, sizeof(*pix));
1430 memcpy(&(s->_rect), &rect, sizeof(rect));
1432 if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
1433 nbuffers != zc0301_request_buffers(cam, nbuffers, cam->io)) {
1434 cam->state |= DEV_MISCONFIGURED;
1435 DBG(1, "VIDIOC_S_FMT failed because of not enough memory. To "
1436 "use the camera, close and open /dev/video%d again.",
1437 cam->v4ldev->minor);
1441 if (cam->io == IO_READ)
1442 zc0301_empty_framequeues(cam);
1443 else if (cam->module_param.force_munmap)
1444 zc0301_requeue_outqueue(cam);
1446 cam->stream = stream;
1453 zc0301_vidioc_g_jpegcomp(struct zc0301_device* cam, void __user * arg)
1455 if (copy_to_user(arg, &cam->compression, sizeof(cam->compression)))
1463 zc0301_vidioc_s_jpegcomp(struct zc0301_device* cam, void __user * arg)
1465 struct v4l2_jpegcompression jc;
1466 const enum zc0301_stream_state stream = cam->stream;
1469 if (copy_from_user(&jc, arg, sizeof(jc)))
1472 if (jc.quality != 0)
1475 if (cam->stream == STREAM_ON)
1476 if ((err = zc0301_stream_interrupt(cam)))
1479 err += zc0301_set_compression(cam, &jc);
1480 if (err) { /* atomic, no rollback in ioctl() */
1481 cam->state |= DEV_MISCONFIGURED;
1482 DBG(1, "VIDIOC_S_JPEGCOMP failed because of hardware "
1483 "problems. To use the camera, close and open "
1484 "/dev/video%d again.", cam->v4ldev->minor);
1488 cam->compression.quality = jc.quality;
1490 cam->stream = stream;
1497 zc0301_vidioc_reqbufs(struct zc0301_device* cam, void __user * arg)
1499 struct v4l2_requestbuffers rb;
1503 if (copy_from_user(&rb, arg, sizeof(rb)))
1506 if (rb.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1507 rb.memory != V4L2_MEMORY_MMAP)
1510 if (cam->io == IO_READ) {
1511 DBG(3, "Close and open the device again to choose the mmap "
1516 for (i = 0; i < cam->nbuffers; i++)
1517 if (cam->frame[i].vma_use_count) {
1518 DBG(3, "VIDIOC_REQBUFS failed. "
1519 "Previous buffers are still mapped.");
1523 if (cam->stream == STREAM_ON)
1524 if ((err = zc0301_stream_interrupt(cam)))
1527 zc0301_empty_framequeues(cam);
1529 zc0301_release_buffers(cam);
1531 rb.count = zc0301_request_buffers(cam, rb.count, IO_MMAP);
1533 if (copy_to_user(arg, &rb, sizeof(rb))) {
1534 zc0301_release_buffers(cam);
1539 cam->io = rb.count ? IO_MMAP : IO_NONE;
1546 zc0301_vidioc_querybuf(struct zc0301_device* cam, void __user * arg)
1548 struct v4l2_buffer b;
1550 if (copy_from_user(&b, arg, sizeof(b)))
1553 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1554 b.index >= cam->nbuffers || cam->io != IO_MMAP)
1557 memcpy(&b, &cam->frame[b.index].buf, sizeof(b));
1559 if (cam->frame[b.index].vma_use_count)
1560 b.flags |= V4L2_BUF_FLAG_MAPPED;
1562 if (cam->frame[b.index].state == F_DONE)
1563 b.flags |= V4L2_BUF_FLAG_DONE;
1564 else if (cam->frame[b.index].state != F_UNUSED)
1565 b.flags |= V4L2_BUF_FLAG_QUEUED;
1567 if (copy_to_user(arg, &b, sizeof(b)))
1575 zc0301_vidioc_qbuf(struct zc0301_device* cam, void __user * arg)
1577 struct v4l2_buffer b;
1578 unsigned long lock_flags;
1580 if (copy_from_user(&b, arg, sizeof(b)))
1583 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1584 b.index >= cam->nbuffers || cam->io != IO_MMAP)
1587 if (cam->frame[b.index].state != F_UNUSED)
1590 cam->frame[b.index].state = F_QUEUED;
1592 spin_lock_irqsave(&cam->queue_lock, lock_flags);
1593 list_add_tail(&cam->frame[b.index].frame, &cam->inqueue);
1594 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1596 PDBGG("Frame #%lu queued", (unsigned long)b.index);
1603 zc0301_vidioc_dqbuf(struct zc0301_device* cam, struct file* filp,
1606 struct v4l2_buffer b;
1607 struct zc0301_frame_t *f;
1608 unsigned long lock_flags;
1611 if (copy_from_user(&b, arg, sizeof(b)))
1614 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io!= IO_MMAP)
1617 if (list_empty(&cam->outqueue)) {
1618 if (cam->stream == STREAM_OFF)
1620 if (filp->f_flags & O_NONBLOCK)
1622 timeout = wait_event_interruptible_timeout
1624 (!list_empty(&cam->outqueue)) ||
1625 (cam->state & DEV_DISCONNECTED) ||
1626 (cam->state & DEV_MISCONFIGURED),
1627 cam->module_param.frame_timeout *
1628 1000 * msecs_to_jiffies(1) );
1631 if (cam->state & DEV_DISCONNECTED)
1633 if (!timeout || (cam->state & DEV_MISCONFIGURED))
1637 spin_lock_irqsave(&cam->queue_lock, lock_flags);
1638 f = list_entry(cam->outqueue.next, struct zc0301_frame_t, frame);
1639 list_del(cam->outqueue.next);
1640 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1642 f->state = F_UNUSED;
1644 memcpy(&b, &f->buf, sizeof(b));
1645 if (f->vma_use_count)
1646 b.flags |= V4L2_BUF_FLAG_MAPPED;
1648 if (copy_to_user(arg, &b, sizeof(b)))
1651 PDBGG("Frame #%lu dequeued", (unsigned long)f->buf.index);
1658 zc0301_vidioc_streamon(struct zc0301_device* cam, void __user * arg)
1662 if (copy_from_user(&type, arg, sizeof(type)))
1665 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
1668 if (list_empty(&cam->inqueue))
1671 cam->stream = STREAM_ON;
1673 DBG(3, "Stream on");
1680 zc0301_vidioc_streamoff(struct zc0301_device* cam, void __user * arg)
1684 if (copy_from_user(&type, arg, sizeof(type)))
1687 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
1690 if (cam->stream == STREAM_ON)
1691 if ((err = zc0301_stream_interrupt(cam)))
1694 zc0301_empty_framequeues(cam);
1696 DBG(3, "Stream off");
1703 zc0301_vidioc_g_parm(struct zc0301_device* cam, void __user * arg)
1705 struct v4l2_streamparm sp;
1707 if (copy_from_user(&sp, arg, sizeof(sp)))
1710 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1713 sp.parm.capture.extendedmode = 0;
1714 sp.parm.capture.readbuffers = cam->nreadbuffers;
1716 if (copy_to_user(arg, &sp, sizeof(sp)))
1724 zc0301_vidioc_s_parm(struct zc0301_device* cam, void __user * arg)
1726 struct v4l2_streamparm sp;
1728 if (copy_from_user(&sp, arg, sizeof(sp)))
1731 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1734 sp.parm.capture.extendedmode = 0;
1736 if (sp.parm.capture.readbuffers == 0)
1737 sp.parm.capture.readbuffers = cam->nreadbuffers;
1739 if (sp.parm.capture.readbuffers > ZC0301_MAX_FRAMES)
1740 sp.parm.capture.readbuffers = ZC0301_MAX_FRAMES;
1742 if (copy_to_user(arg, &sp, sizeof(sp)))
1745 cam->nreadbuffers = sp.parm.capture.readbuffers;
1751 static int zc0301_ioctl_v4l2(struct inode* inode, struct file* filp,
1752 unsigned int cmd, void __user * arg)
1754 struct zc0301_device* cam = video_get_drvdata(video_devdata(filp));
1758 case VIDIOC_QUERYCAP:
1759 return zc0301_vidioc_querycap(cam, arg);
1761 case VIDIOC_ENUMINPUT:
1762 return zc0301_vidioc_enuminput(cam, arg);
1764 case VIDIOC_G_INPUT:
1765 return zc0301_vidioc_g_input(cam, arg);
1767 case VIDIOC_S_INPUT:
1768 return zc0301_vidioc_s_input(cam, arg);
1770 case VIDIOC_QUERYCTRL:
1771 return zc0301_vidioc_query_ctrl(cam, arg);
1774 return zc0301_vidioc_g_ctrl(cam, arg);
1777 return zc0301_vidioc_s_ctrl(cam, arg);
1779 case VIDIOC_CROPCAP:
1780 return zc0301_vidioc_cropcap(cam, arg);
1783 return zc0301_vidioc_g_crop(cam, arg);
1786 return zc0301_vidioc_s_crop(cam, arg);
1788 case VIDIOC_ENUM_FMT:
1789 return zc0301_vidioc_enum_fmt(cam, arg);
1792 return zc0301_vidioc_g_fmt(cam, arg);
1794 case VIDIOC_TRY_FMT:
1796 return zc0301_vidioc_try_s_fmt(cam, cmd, arg);
1798 case VIDIOC_G_JPEGCOMP:
1799 return zc0301_vidioc_g_jpegcomp(cam, arg);
1801 case VIDIOC_S_JPEGCOMP:
1802 return zc0301_vidioc_s_jpegcomp(cam, arg);
1804 case VIDIOC_REQBUFS:
1805 return zc0301_vidioc_reqbufs(cam, arg);
1807 case VIDIOC_QUERYBUF:
1808 return zc0301_vidioc_querybuf(cam, arg);
1811 return zc0301_vidioc_qbuf(cam, arg);
1814 return zc0301_vidioc_dqbuf(cam, filp, arg);
1816 case VIDIOC_STREAMON:
1817 return zc0301_vidioc_streamon(cam, arg);
1819 case VIDIOC_STREAMOFF:
1820 return zc0301_vidioc_streamoff(cam, arg);
1823 return zc0301_vidioc_g_parm(cam, arg);
1826 return zc0301_vidioc_s_parm(cam, arg);
1830 case VIDIOC_QUERYSTD:
1831 case VIDIOC_ENUMSTD:
1832 case VIDIOC_QUERYMENU:
1842 static int zc0301_ioctl(struct inode* inode, struct file* filp,
1843 unsigned int cmd, unsigned long arg)
1845 struct zc0301_device* cam = video_get_drvdata(video_devdata(filp));
1848 if (mutex_lock_interruptible(&cam->fileop_mutex))
1849 return -ERESTARTSYS;
1851 if (cam->state & DEV_DISCONNECTED) {
1852 DBG(1, "Device not present");
1853 mutex_unlock(&cam->fileop_mutex);
1857 if (cam->state & DEV_MISCONFIGURED) {
1858 DBG(1, "The camera is misconfigured. Close and open it "
1860 mutex_unlock(&cam->fileop_mutex);
1864 V4LDBG(3, "zc0301", cmd);
1866 err = zc0301_ioctl_v4l2(inode, filp, cmd, (void __user *)arg);
1868 mutex_unlock(&cam->fileop_mutex);
1874 static struct file_operations zc0301_fops = {
1875 .owner = THIS_MODULE,
1876 .open = zc0301_open,
1877 .release = zc0301_release,
1878 .ioctl = zc0301_ioctl,
1879 .read = zc0301_read,
1880 .poll = zc0301_poll,
1881 .mmap = zc0301_mmap,
1882 .llseek = no_llseek,
1885 /*****************************************************************************/
1888 zc0301_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
1890 struct usb_device *udev = interface_to_usbdev(intf);
1891 struct zc0301_device* cam;
1892 static unsigned int dev_nr = 0;
1896 if (!(cam = kzalloc(sizeof(struct zc0301_device), GFP_KERNEL)))
1901 if (!(cam->control_buffer = kzalloc(4, GFP_KERNEL))) {
1902 DBG(1, "kmalloc() failed");
1907 if (!(cam->v4ldev = video_device_alloc())) {
1908 DBG(1, "video_device_alloc() failed");
1913 mutex_init(&cam->dev_mutex);
1915 DBG(2, "ZC0301[P] Image Processor and Control Chip detected "
1916 "(vid/pid 0x%04X/0x%04X)",id->idVendor, id->idProduct);
1918 for (i = 0; zc0301_sensor_table[i]; i++) {
1919 err = zc0301_sensor_table[i](cam);
1925 DBG(2, "%s image sensor detected", cam->sensor.name);
1927 DBG(1, "No supported image sensor detected");
1932 if (zc0301_init(cam)) {
1933 DBG(1, "Initialization failed. I will retry on open().");
1934 cam->state |= DEV_MISCONFIGURED;
1937 strcpy(cam->v4ldev->name, "ZC0301[P] PC Camera");
1938 cam->v4ldev->owner = THIS_MODULE;
1939 cam->v4ldev->type = VID_TYPE_CAPTURE | VID_TYPE_SCALES;
1940 cam->v4ldev->hardware = 0;
1941 cam->v4ldev->fops = &zc0301_fops;
1942 cam->v4ldev->minor = video_nr[dev_nr];
1943 cam->v4ldev->release = video_device_release;
1944 video_set_drvdata(cam->v4ldev, cam);
1946 mutex_lock(&cam->dev_mutex);
1948 err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER,
1951 DBG(1, "V4L2 device registration failed");
1952 if (err == -ENFILE && video_nr[dev_nr] == -1)
1953 DBG(1, "Free /dev/videoX node not found");
1954 video_nr[dev_nr] = -1;
1955 dev_nr = (dev_nr < ZC0301_MAX_DEVICES-1) ? dev_nr+1 : 0;
1956 mutex_unlock(&cam->dev_mutex);
1960 DBG(2, "V4L2 device registered as /dev/video%d", cam->v4ldev->minor);
1962 cam->module_param.force_munmap = force_munmap[dev_nr];
1963 cam->module_param.frame_timeout = frame_timeout[dev_nr];
1965 dev_nr = (dev_nr < ZC0301_MAX_DEVICES-1) ? dev_nr+1 : 0;
1967 usb_set_intfdata(intf, cam);
1969 mutex_unlock(&cam->dev_mutex);
1975 kfree(cam->control_buffer);
1977 video_device_release(cam->v4ldev);
1984 static void zc0301_usb_disconnect(struct usb_interface* intf)
1986 struct zc0301_device* cam = usb_get_intfdata(intf);
1991 down_write(&zc0301_disconnect);
1993 mutex_lock(&cam->dev_mutex);
1995 DBG(2, "Disconnecting %s...", cam->v4ldev->name);
1997 wake_up_interruptible_all(&cam->open);
2000 DBG(2, "Device /dev/video%d is open! Deregistration and "
2001 "memory deallocation are deferred on close.",
2002 cam->v4ldev->minor);
2003 cam->state |= DEV_MISCONFIGURED;
2004 zc0301_stop_transfer(cam);
2005 cam->state |= DEV_DISCONNECTED;
2006 wake_up_interruptible(&cam->wait_frame);
2007 wake_up(&cam->wait_stream);
2008 usb_get_dev(cam->usbdev);
2010 cam->state |= DEV_DISCONNECTED;
2011 zc0301_release_resources(cam);
2014 mutex_unlock(&cam->dev_mutex);
2019 up_write(&zc0301_disconnect);
2023 static struct usb_driver zc0301_usb_driver = {
2025 .id_table = zc0301_id_table,
2026 .probe = zc0301_usb_probe,
2027 .disconnect = zc0301_usb_disconnect,
2030 /*****************************************************************************/
2032 static int __init zc0301_module_init(void)
2036 KDBG(2, ZC0301_MODULE_NAME " v" ZC0301_MODULE_VERSION);
2037 KDBG(3, ZC0301_MODULE_AUTHOR);
2039 if ((err = usb_register(&zc0301_usb_driver)))
2040 KDBG(1, "usb_register() failed");
2046 static void __exit zc0301_module_exit(void)
2048 usb_deregister(&zc0301_usb_driver);
2052 module_init(zc0301_module_init);
2053 module_exit(zc0301_module_exit);