1 /***************************************************************************
2 * Video4Linux2 driver for ZC0301 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 " \
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.03"
56 #define ZC0301_MODULE_VERSION_CODE KERNEL_VERSION(1, 0, 3)
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;
431 const unsigned int wMaxPacketSize[] = {0, 128, 192, 256, 384,
433 const unsigned int psz = wMaxPacketSize[ZC0301_ALTERNATE_SETTING];
437 for (i = 0; i < ZC0301_URBS; i++) {
438 cam->transfer_buffer[i] = kzalloc(ZC0301_ISO_PACKETS * psz,
440 if (!cam->transfer_buffer[i]) {
442 DBG(1, "Not enough memory");
447 for (i = 0; i < ZC0301_URBS; i++) {
448 urb = usb_alloc_urb(ZC0301_ISO_PACKETS, GFP_KERNEL);
452 DBG(1, "usb_alloc_urb() failed");
457 urb->pipe = usb_rcvisocpipe(udev, 1);
458 urb->transfer_flags = URB_ISO_ASAP;
459 urb->number_of_packets = ZC0301_ISO_PACKETS;
460 urb->complete = zc0301_urb_complete;
461 urb->transfer_buffer = cam->transfer_buffer[i];
462 urb->transfer_buffer_length = psz * ZC0301_ISO_PACKETS;
464 for (j = 0; j < ZC0301_ISO_PACKETS; j++) {
465 urb->iso_frame_desc[j].offset = psz * j;
466 urb->iso_frame_desc[j].length = psz;
470 err = usb_set_interface(udev, 0, ZC0301_ALTERNATE_SETTING);
472 DBG(1, "usb_set_interface() failed");
476 cam->frame_current = NULL;
478 for (i = 0; i < ZC0301_URBS; i++) {
479 err = usb_submit_urb(cam->urb[i], GFP_KERNEL);
481 for (j = i-1; j >= 0; j--)
482 usb_kill_urb(cam->urb[j]);
483 DBG(1, "usb_submit_urb() failed, error %d", err);
491 for (i = 0; (i < ZC0301_URBS) && cam->urb[i]; i++)
492 usb_free_urb(cam->urb[i]);
495 for (i = 0; (i < ZC0301_URBS) && cam->transfer_buffer[i]; i++)
496 kfree(cam->transfer_buffer[i]);
502 static int zc0301_stop_transfer(struct zc0301_device* cam)
504 struct usb_device *udev = cam->usbdev;
508 if (cam->state & DEV_DISCONNECTED)
511 for (i = ZC0301_URBS-1; i >= 0; i--) {
512 usb_kill_urb(cam->urb[i]);
513 usb_free_urb(cam->urb[i]);
514 kfree(cam->transfer_buffer[i]);
517 err = usb_set_interface(udev, 0, 0); /* 0 Mb/s */
519 DBG(3, "usb_set_interface() failed");
525 static int zc0301_stream_interrupt(struct zc0301_device* cam)
529 cam->stream = STREAM_INTERRUPT;
530 timeout = wait_event_timeout(cam->wait_stream,
531 (cam->stream == STREAM_OFF) ||
532 (cam->state & DEV_DISCONNECTED),
534 if (cam->state & DEV_DISCONNECTED)
536 else if (cam->stream != STREAM_OFF) {
537 cam->state |= DEV_MISCONFIGURED;
538 DBG(1, "URB timeout reached. The camera is misconfigured. To "
539 "use it, close and open /dev/video%d again.",
547 /*****************************************************************************/
550 zc0301_set_compression(struct zc0301_device* cam,
551 struct v4l2_jpegcompression* compression)
555 if ((r = zc0301_read_reg(cam, 0x0008)) < 0)
557 err += zc0301_write_reg(cam, 0x0008, r | 0x11 | compression->quality);
559 return err ? -EIO : 0;
563 static int zc0301_init(struct zc0301_device* cam)
565 struct zc0301_sensor* s = &cam->sensor;
566 struct v4l2_control ctrl;
567 struct v4l2_queryctrl *qctrl;
568 struct v4l2_rect* rect;
572 if (!(cam->state & DEV_INITIALIZED)) {
573 init_waitqueue_head(&cam->open);
575 rect = &(s->cropcap.defrect);
576 cam->compression.quality = ZC0301_COMPRESSION_QUALITY;
577 } else { /* use current values */
585 DBG(3, "Sensor initialization failed");
590 if ((err = zc0301_set_compression(cam, &cam->compression))) {
591 DBG(3, "set_compression() failed");
596 if ((err = s->set_crop(cam, rect))) {
597 DBG(3, "set_crop() failed");
602 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
603 if (s->qctrl[i].id != 0 &&
604 !(s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)) {
605 ctrl.id = s->qctrl[i].id;
606 ctrl.value = qctrl[i].default_value;
607 err = s->set_ctrl(cam, &ctrl);
609 DBG(3, "Set %s control failed",
613 DBG(3, "Image sensor supports '%s' control",
618 if (!(cam->state & DEV_INITIALIZED)) {
619 mutex_init(&cam->fileop_mutex);
620 spin_lock_init(&cam->queue_lock);
621 init_waitqueue_head(&cam->wait_frame);
622 init_waitqueue_head(&cam->wait_stream);
623 cam->nreadbuffers = 2;
624 memcpy(s->_qctrl, s->qctrl, sizeof(s->qctrl));
625 memcpy(&(s->_rect), &(s->cropcap.defrect),
626 sizeof(struct v4l2_rect));
627 cam->state |= DEV_INITIALIZED;
630 DBG(2, "Initialization succeeded");
635 static void zc0301_release_resources(struct zc0301_device* cam)
637 DBG(2, "V4L2 device /dev/video%d deregistered", cam->v4ldev->minor);
638 video_set_drvdata(cam->v4ldev, NULL);
639 video_unregister_device(cam->v4ldev);
640 kfree(cam->control_buffer);
643 /*****************************************************************************/
645 static int zc0301_open(struct inode* inode, struct file* filp)
647 struct zc0301_device* cam;
651 This is the only safe way to prevent race conditions with
654 if (!down_read_trylock(&zc0301_disconnect))
657 cam = video_get_drvdata(video_devdata(filp));
659 if (mutex_lock_interruptible(&cam->dev_mutex)) {
660 up_read(&zc0301_disconnect);
665 DBG(2, "Device /dev/video%d is busy...", cam->v4ldev->minor);
666 if ((filp->f_flags & O_NONBLOCK) ||
667 (filp->f_flags & O_NDELAY)) {
671 mutex_unlock(&cam->dev_mutex);
672 err = wait_event_interruptible_exclusive(cam->open,
673 cam->state & DEV_DISCONNECTED
676 up_read(&zc0301_disconnect);
679 if (cam->state & DEV_DISCONNECTED) {
680 up_read(&zc0301_disconnect);
683 mutex_lock(&cam->dev_mutex);
687 if (cam->state & DEV_MISCONFIGURED) {
688 err = zc0301_init(cam);
690 DBG(1, "Initialization failed again. "
691 "I will retry on next open().");
694 cam->state &= ~DEV_MISCONFIGURED;
697 if ((err = zc0301_start_transfer(cam)))
700 filp->private_data = cam;
703 cam->stream = STREAM_OFF;
705 cam->frame_count = 0;
706 zc0301_empty_framequeues(cam);
708 DBG(3, "Video device /dev/video%d is open", cam->v4ldev->minor);
711 mutex_unlock(&cam->dev_mutex);
712 up_read(&zc0301_disconnect);
717 static int zc0301_release(struct inode* inode, struct file* filp)
719 struct zc0301_device* cam = video_get_drvdata(video_devdata(filp));
721 mutex_lock(&cam->dev_mutex); /* prevent disconnect() to be called */
723 zc0301_stop_transfer(cam);
725 zc0301_release_buffers(cam);
727 if (cam->state & DEV_DISCONNECTED) {
728 zc0301_release_resources(cam);
729 usb_put_dev(cam->usbdev);
730 mutex_unlock(&cam->dev_mutex);
736 wake_up_interruptible_nr(&cam->open, 1);
738 DBG(3, "Video device /dev/video%d closed", cam->v4ldev->minor);
740 mutex_unlock(&cam->dev_mutex);
747 zc0301_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
749 struct zc0301_device* cam = video_get_drvdata(video_devdata(filp));
750 struct zc0301_frame_t* f, * i;
751 unsigned long lock_flags;
755 if (mutex_lock_interruptible(&cam->fileop_mutex))
758 if (cam->state & DEV_DISCONNECTED) {
759 DBG(1, "Device not present");
760 mutex_unlock(&cam->fileop_mutex);
764 if (cam->state & DEV_MISCONFIGURED) {
765 DBG(1, "The camera is misconfigured. Close and open it "
767 mutex_unlock(&cam->fileop_mutex);
771 if (cam->io == IO_MMAP) {
772 DBG(3, "Close and open the device again to choose the read "
774 mutex_unlock(&cam->fileop_mutex);
778 if (cam->io == IO_NONE) {
779 if (!zc0301_request_buffers(cam, cam->nreadbuffers, IO_READ)) {
780 DBG(1, "read() failed, not enough memory");
781 mutex_unlock(&cam->fileop_mutex);
785 cam->stream = STREAM_ON;
788 if (list_empty(&cam->inqueue)) {
789 if (!list_empty(&cam->outqueue))
790 zc0301_empty_framequeues(cam);
791 zc0301_queue_unusedframes(cam);
795 mutex_unlock(&cam->fileop_mutex);
799 if (list_empty(&cam->outqueue)) {
800 if (filp->f_flags & O_NONBLOCK) {
801 mutex_unlock(&cam->fileop_mutex);
804 timeout = wait_event_interruptible_timeout
806 (!list_empty(&cam->outqueue)) ||
807 (cam->state & DEV_DISCONNECTED) ||
808 (cam->state & DEV_MISCONFIGURED),
809 cam->module_param.frame_timeout *
810 1000 * msecs_to_jiffies(1) );
812 mutex_unlock(&cam->fileop_mutex);
815 if (cam->state & DEV_DISCONNECTED) {
816 mutex_unlock(&cam->fileop_mutex);
819 if (!timeout || (cam->state & DEV_MISCONFIGURED)) {
820 mutex_unlock(&cam->fileop_mutex);
825 f = list_entry(cam->outqueue.prev, struct zc0301_frame_t, frame);
827 if (count > f->buf.bytesused)
828 count = f->buf.bytesused;
830 if (copy_to_user(buf, f->bufmem, count)) {
837 spin_lock_irqsave(&cam->queue_lock, lock_flags);
838 list_for_each_entry(i, &cam->outqueue, frame)
840 INIT_LIST_HEAD(&cam->outqueue);
841 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
843 zc0301_queue_unusedframes(cam);
845 PDBGG("Frame #%lu, bytes read: %zu",
846 (unsigned long)f->buf.index, count);
848 mutex_unlock(&cam->fileop_mutex);
850 return err ? err : count;
854 static unsigned int zc0301_poll(struct file *filp, poll_table *wait)
856 struct zc0301_device* cam = video_get_drvdata(video_devdata(filp));
857 struct zc0301_frame_t* f;
858 unsigned long lock_flags;
859 unsigned int mask = 0;
861 if (mutex_lock_interruptible(&cam->fileop_mutex))
864 if (cam->state & DEV_DISCONNECTED) {
865 DBG(1, "Device not present");
869 if (cam->state & DEV_MISCONFIGURED) {
870 DBG(1, "The camera is misconfigured. Close and open it "
875 if (cam->io == IO_NONE) {
876 if (!zc0301_request_buffers(cam, cam->nreadbuffers, IO_READ)) {
877 DBG(1, "poll() failed, not enough memory");
881 cam->stream = STREAM_ON;
884 if (cam->io == IO_READ) {
885 spin_lock_irqsave(&cam->queue_lock, lock_flags);
886 list_for_each_entry(f, &cam->outqueue, frame)
888 INIT_LIST_HEAD(&cam->outqueue);
889 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
890 zc0301_queue_unusedframes(cam);
893 poll_wait(filp, &cam->wait_frame, wait);
895 if (!list_empty(&cam->outqueue))
896 mask |= POLLIN | POLLRDNORM;
898 mutex_unlock(&cam->fileop_mutex);
903 mutex_unlock(&cam->fileop_mutex);
908 static void zc0301_vm_open(struct vm_area_struct* vma)
910 struct zc0301_frame_t* f = vma->vm_private_data;
915 static void zc0301_vm_close(struct vm_area_struct* vma)
917 /* NOTE: buffers are not freed here */
918 struct zc0301_frame_t* f = vma->vm_private_data;
923 static struct vm_operations_struct zc0301_vm_ops = {
924 .open = zc0301_vm_open,
925 .close = zc0301_vm_close,
929 static int zc0301_mmap(struct file* filp, struct vm_area_struct *vma)
931 struct zc0301_device* cam = video_get_drvdata(video_devdata(filp));
932 unsigned long size = vma->vm_end - vma->vm_start,
933 start = vma->vm_start;
937 if (mutex_lock_interruptible(&cam->fileop_mutex))
940 if (cam->state & DEV_DISCONNECTED) {
941 DBG(1, "Device not present");
942 mutex_unlock(&cam->fileop_mutex);
946 if (cam->state & DEV_MISCONFIGURED) {
947 DBG(1, "The camera is misconfigured. Close and open it "
949 mutex_unlock(&cam->fileop_mutex);
953 if (cam->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) ||
954 size != PAGE_ALIGN(cam->frame[0].buf.length)) {
955 mutex_unlock(&cam->fileop_mutex);
959 for (i = 0; i < cam->nbuffers; i++) {
960 if ((cam->frame[i].buf.m.offset>>PAGE_SHIFT) == vma->vm_pgoff)
963 if (i == cam->nbuffers) {
964 mutex_unlock(&cam->fileop_mutex);
968 vma->vm_flags |= VM_IO;
969 vma->vm_flags |= VM_RESERVED;
971 pos = cam->frame[i].bufmem;
972 while (size > 0) { /* size is page-aligned */
973 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
974 mutex_unlock(&cam->fileop_mutex);
982 vma->vm_ops = &zc0301_vm_ops;
983 vma->vm_private_data = &cam->frame[i];
987 mutex_unlock(&cam->fileop_mutex);
992 /*****************************************************************************/
995 zc0301_vidioc_querycap(struct zc0301_device* cam, void __user * arg)
997 struct v4l2_capability cap = {
999 .version = ZC0301_MODULE_VERSION_CODE,
1000 .capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
1004 strlcpy(cap.card, cam->v4ldev->name, sizeof(cap.card));
1005 if (usb_make_path(cam->usbdev, cap.bus_info, sizeof(cap.bus_info)) < 0)
1006 strlcpy(cap.bus_info, cam->usbdev->dev.bus_id,
1007 sizeof(cap.bus_info));
1009 if (copy_to_user(arg, &cap, sizeof(cap)))
1017 zc0301_vidioc_enuminput(struct zc0301_device* cam, void __user * arg)
1019 struct v4l2_input i;
1021 if (copy_from_user(&i, arg, sizeof(i)))
1027 memset(&i, 0, sizeof(i));
1028 strcpy(i.name, "Camera");
1029 i.type = V4L2_INPUT_TYPE_CAMERA;
1031 if (copy_to_user(arg, &i, sizeof(i)))
1039 zc0301_vidioc_g_input(struct zc0301_device* cam, void __user * arg)
1043 if (copy_to_user(arg, &index, sizeof(index)))
1051 zc0301_vidioc_s_input(struct zc0301_device* cam, void __user * arg)
1055 if (copy_from_user(&index, arg, sizeof(index)))
1066 zc0301_vidioc_query_ctrl(struct zc0301_device* cam, void __user * arg)
1068 struct zc0301_sensor* s = &cam->sensor;
1069 struct v4l2_queryctrl qc;
1072 if (copy_from_user(&qc, arg, sizeof(qc)))
1075 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1076 if (qc.id && qc.id == s->qctrl[i].id) {
1077 memcpy(&qc, &(s->qctrl[i]), sizeof(qc));
1078 if (copy_to_user(arg, &qc, sizeof(qc)))
1088 zc0301_vidioc_g_ctrl(struct zc0301_device* cam, void __user * arg)
1090 struct zc0301_sensor* s = &cam->sensor;
1091 struct v4l2_control ctrl;
1095 if (!s->get_ctrl && !s->set_ctrl)
1098 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1102 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1103 if (ctrl.id == s->qctrl[i].id) {
1104 ctrl.value = s->_qctrl[i].default_value;
1109 err = s->get_ctrl(cam, &ctrl);
1112 if (copy_to_user(arg, &ctrl, sizeof(ctrl)))
1120 zc0301_vidioc_s_ctrl(struct zc0301_device* cam, void __user * arg)
1122 struct zc0301_sensor* s = &cam->sensor;
1123 struct v4l2_control ctrl;
1130 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1133 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1134 if (ctrl.id == s->qctrl[i].id) {
1135 if (s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)
1137 if (ctrl.value < s->qctrl[i].minimum ||
1138 ctrl.value > s->qctrl[i].maximum)
1140 ctrl.value -= ctrl.value % s->qctrl[i].step;
1144 if ((err = s->set_ctrl(cam, &ctrl)))
1147 s->_qctrl[i].default_value = ctrl.value;
1154 zc0301_vidioc_cropcap(struct zc0301_device* cam, void __user * arg)
1156 struct v4l2_cropcap* cc = &(cam->sensor.cropcap);
1158 cc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1159 cc->pixelaspect.numerator = 1;
1160 cc->pixelaspect.denominator = 1;
1162 if (copy_to_user(arg, cc, sizeof(*cc)))
1170 zc0301_vidioc_g_crop(struct zc0301_device* cam, void __user * arg)
1172 struct zc0301_sensor* s = &cam->sensor;
1173 struct v4l2_crop crop = {
1174 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
1177 memcpy(&(crop.c), &(s->_rect), sizeof(struct v4l2_rect));
1179 if (copy_to_user(arg, &crop, sizeof(crop)))
1187 zc0301_vidioc_s_crop(struct zc0301_device* cam, void __user * arg)
1189 struct zc0301_sensor* s = &cam->sensor;
1190 struct v4l2_crop crop;
1191 struct v4l2_rect* rect;
1192 struct v4l2_rect* bounds = &(s->cropcap.bounds);
1193 const enum zc0301_stream_state stream = cam->stream;
1194 const u32 nbuffers = cam->nbuffers;
1198 if (copy_from_user(&crop, arg, sizeof(crop)))
1203 if (crop.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1206 if (cam->module_param.force_munmap)
1207 for (i = 0; i < cam->nbuffers; i++)
1208 if (cam->frame[i].vma_use_count) {
1209 DBG(3, "VIDIOC_S_CROP failed. "
1210 "Unmap the buffers first.");
1215 memcpy(rect, &(s->_rect), sizeof(*rect));
1216 if (copy_to_user(arg, &crop, sizeof(crop)))
1223 if (rect->width < 8)
1225 if (rect->height < 8)
1227 if (rect->width > bounds->width)
1228 rect->width = bounds->width;
1229 if (rect->height > bounds->height)
1230 rect->height = bounds->height;
1231 if (rect->left < bounds->left)
1232 rect->left = bounds->left;
1233 if (rect->top < bounds->top)
1234 rect->top = bounds->top;
1235 if (rect->left + rect->width > bounds->left + bounds->width)
1236 rect->left = bounds->left+bounds->width - rect->width;
1237 if (rect->top + rect->height > bounds->top + bounds->height)
1238 rect->top = bounds->top+bounds->height - rect->height;
1240 rect->height &= ~7L;
1242 if (cam->stream == STREAM_ON)
1243 if ((err = zc0301_stream_interrupt(cam)))
1246 if (copy_to_user(arg, &crop, sizeof(crop))) {
1247 cam->stream = stream;
1251 if (cam->module_param.force_munmap || cam->io == IO_READ)
1252 zc0301_release_buffers(cam);
1255 err += s->set_crop(cam, rect);
1257 if (err) { /* atomic, no rollback in ioctl() */
1258 cam->state |= DEV_MISCONFIGURED;
1259 DBG(1, "VIDIOC_S_CROP failed because of hardware problems. To "
1260 "use the camera, close and open /dev/video%d again.",
1261 cam->v4ldev->minor);
1265 s->pix_format.width = rect->width;
1266 s->pix_format.height = rect->height;
1267 memcpy(&(s->_rect), rect, sizeof(*rect));
1269 if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
1270 nbuffers != zc0301_request_buffers(cam, nbuffers, cam->io)) {
1271 cam->state |= DEV_MISCONFIGURED;
1272 DBG(1, "VIDIOC_S_CROP failed because of not enough memory. To "
1273 "use the camera, close and open /dev/video%d again.",
1274 cam->v4ldev->minor);
1278 if (cam->io == IO_READ)
1279 zc0301_empty_framequeues(cam);
1280 else if (cam->module_param.force_munmap)
1281 zc0301_requeue_outqueue(cam);
1283 cam->stream = stream;
1290 zc0301_vidioc_enum_fmt(struct zc0301_device* cam, void __user * arg)
1292 struct v4l2_fmtdesc fmtd;
1294 if (copy_from_user(&fmtd, arg, sizeof(fmtd)))
1297 if (fmtd.index == 0) {
1298 strcpy(fmtd.description, "JPEG");
1299 fmtd.pixelformat = V4L2_PIX_FMT_JPEG;
1300 fmtd.flags = V4L2_FMT_FLAG_COMPRESSED;
1304 fmtd.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1305 memset(&fmtd.reserved, 0, sizeof(fmtd.reserved));
1307 if (copy_to_user(arg, &fmtd, sizeof(fmtd)))
1315 zc0301_vidioc_g_fmt(struct zc0301_device* cam, void __user * arg)
1317 struct v4l2_format format;
1318 struct v4l2_pix_format* pfmt = &(cam->sensor.pix_format);
1320 if (copy_from_user(&format, arg, sizeof(format)))
1323 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1326 pfmt->bytesperline = 0;
1327 pfmt->sizeimage = pfmt->height * ((pfmt->width*pfmt->priv)/8);
1328 pfmt->field = V4L2_FIELD_NONE;
1329 memcpy(&(format.fmt.pix), pfmt, sizeof(*pfmt));
1331 if (copy_to_user(arg, &format, sizeof(format)))
1339 zc0301_vidioc_try_s_fmt(struct zc0301_device* cam, unsigned int cmd,
1342 struct zc0301_sensor* s = &cam->sensor;
1343 struct v4l2_format format;
1344 struct v4l2_pix_format* pix;
1345 struct v4l2_pix_format* pfmt = &(s->pix_format);
1346 struct v4l2_rect* bounds = &(s->cropcap.bounds);
1347 struct v4l2_rect rect;
1348 const enum zc0301_stream_state stream = cam->stream;
1349 const u32 nbuffers = cam->nbuffers;
1353 if (copy_from_user(&format, arg, sizeof(format)))
1356 pix = &(format.fmt.pix);
1358 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1361 memcpy(&rect, &(s->_rect), sizeof(rect));
1364 pix->width = rect.width;
1365 pix->height = rect.height;
1367 rect.width = pix->width;
1368 rect.height = pix->height;
1373 if (rect.height < 8)
1375 if (rect.width > bounds->left + bounds->width - rect.left)
1376 rect.width = bounds->left + bounds->width - rect.left;
1377 if (rect.height > bounds->top + bounds->height - rect.top)
1378 rect.height = bounds->top + bounds->height - rect.top;
1382 pix->width = rect.width;
1383 pix->height = rect.height;
1384 pix->pixelformat = pfmt->pixelformat;
1385 pix->priv = pfmt->priv;
1386 pix->colorspace = pfmt->colorspace;
1387 pix->bytesperline = 0;
1388 pix->sizeimage = pix->height * ((pix->width * pix->priv) / 8);
1389 pix->field = V4L2_FIELD_NONE;
1391 if (cmd == VIDIOC_TRY_FMT) {
1392 if (copy_to_user(arg, &format, sizeof(format)))
1397 if (cam->module_param.force_munmap)
1398 for (i = 0; i < cam->nbuffers; i++)
1399 if (cam->frame[i].vma_use_count) {
1400 DBG(3, "VIDIOC_S_FMT failed. "
1401 "Unmap the buffers first.");
1405 if (cam->stream == STREAM_ON)
1406 if ((err = zc0301_stream_interrupt(cam)))
1409 if (copy_to_user(arg, &format, sizeof(format))) {
1410 cam->stream = stream;
1414 if (cam->module_param.force_munmap || cam->io == IO_READ)
1415 zc0301_release_buffers(cam);
1418 err += s->set_crop(cam, &rect);
1420 if (err) { /* atomic, no rollback in ioctl() */
1421 cam->state |= DEV_MISCONFIGURED;
1422 DBG(1, "VIDIOC_S_FMT failed because of hardware problems. To "
1423 "use the camera, close and open /dev/video%d again.",
1424 cam->v4ldev->minor);
1428 memcpy(pfmt, pix, sizeof(*pix));
1429 memcpy(&(s->_rect), &rect, sizeof(rect));
1431 if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
1432 nbuffers != zc0301_request_buffers(cam, nbuffers, cam->io)) {
1433 cam->state |= DEV_MISCONFIGURED;
1434 DBG(1, "VIDIOC_S_FMT failed because of not enough memory. To "
1435 "use the camera, close and open /dev/video%d again.",
1436 cam->v4ldev->minor);
1440 if (cam->io == IO_READ)
1441 zc0301_empty_framequeues(cam);
1442 else if (cam->module_param.force_munmap)
1443 zc0301_requeue_outqueue(cam);
1445 cam->stream = stream;
1452 zc0301_vidioc_g_jpegcomp(struct zc0301_device* cam, void __user * arg)
1454 if (copy_to_user(arg, &cam->compression, sizeof(cam->compression)))
1462 zc0301_vidioc_s_jpegcomp(struct zc0301_device* cam, void __user * arg)
1464 struct v4l2_jpegcompression jc;
1465 const enum zc0301_stream_state stream = cam->stream;
1468 if (copy_from_user(&jc, arg, sizeof(jc)))
1471 if (jc.quality != 0)
1474 if (cam->stream == STREAM_ON)
1475 if ((err = zc0301_stream_interrupt(cam)))
1478 err += zc0301_set_compression(cam, &jc);
1479 if (err) { /* atomic, no rollback in ioctl() */
1480 cam->state |= DEV_MISCONFIGURED;
1481 DBG(1, "VIDIOC_S_JPEGCOMP failed because of hardware "
1482 "problems. To use the camera, close and open "
1483 "/dev/video%d again.", cam->v4ldev->minor);
1487 cam->compression.quality = jc.quality;
1489 cam->stream = stream;
1496 zc0301_vidioc_reqbufs(struct zc0301_device* cam, void __user * arg)
1498 struct v4l2_requestbuffers rb;
1502 if (copy_from_user(&rb, arg, sizeof(rb)))
1505 if (rb.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1506 rb.memory != V4L2_MEMORY_MMAP)
1509 if (cam->io == IO_READ) {
1510 DBG(3, "Close and open the device again to choose the mmap "
1515 for (i = 0; i < cam->nbuffers; i++)
1516 if (cam->frame[i].vma_use_count) {
1517 DBG(3, "VIDIOC_REQBUFS failed. "
1518 "Previous buffers are still mapped.");
1522 if (cam->stream == STREAM_ON)
1523 if ((err = zc0301_stream_interrupt(cam)))
1526 zc0301_empty_framequeues(cam);
1528 zc0301_release_buffers(cam);
1530 rb.count = zc0301_request_buffers(cam, rb.count, IO_MMAP);
1532 if (copy_to_user(arg, &rb, sizeof(rb))) {
1533 zc0301_release_buffers(cam);
1538 cam->io = rb.count ? IO_MMAP : IO_NONE;
1545 zc0301_vidioc_querybuf(struct zc0301_device* cam, void __user * arg)
1547 struct v4l2_buffer b;
1549 if (copy_from_user(&b, arg, sizeof(b)))
1552 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1553 b.index >= cam->nbuffers || cam->io != IO_MMAP)
1556 memcpy(&b, &cam->frame[b.index].buf, sizeof(b));
1558 if (cam->frame[b.index].vma_use_count)
1559 b.flags |= V4L2_BUF_FLAG_MAPPED;
1561 if (cam->frame[b.index].state == F_DONE)
1562 b.flags |= V4L2_BUF_FLAG_DONE;
1563 else if (cam->frame[b.index].state != F_UNUSED)
1564 b.flags |= V4L2_BUF_FLAG_QUEUED;
1566 if (copy_to_user(arg, &b, sizeof(b)))
1574 zc0301_vidioc_qbuf(struct zc0301_device* cam, void __user * arg)
1576 struct v4l2_buffer b;
1577 unsigned long lock_flags;
1579 if (copy_from_user(&b, arg, sizeof(b)))
1582 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1583 b.index >= cam->nbuffers || cam->io != IO_MMAP)
1586 if (cam->frame[b.index].state != F_UNUSED)
1589 cam->frame[b.index].state = F_QUEUED;
1591 spin_lock_irqsave(&cam->queue_lock, lock_flags);
1592 list_add_tail(&cam->frame[b.index].frame, &cam->inqueue);
1593 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1595 PDBGG("Frame #%lu queued", (unsigned long)b.index);
1602 zc0301_vidioc_dqbuf(struct zc0301_device* cam, struct file* filp,
1605 struct v4l2_buffer b;
1606 struct zc0301_frame_t *f;
1607 unsigned long lock_flags;
1610 if (copy_from_user(&b, arg, sizeof(b)))
1613 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io!= IO_MMAP)
1616 if (list_empty(&cam->outqueue)) {
1617 if (cam->stream == STREAM_OFF)
1619 if (filp->f_flags & O_NONBLOCK)
1621 timeout = wait_event_interruptible_timeout
1623 (!list_empty(&cam->outqueue)) ||
1624 (cam->state & DEV_DISCONNECTED) ||
1625 (cam->state & DEV_MISCONFIGURED),
1626 cam->module_param.frame_timeout *
1627 1000 * msecs_to_jiffies(1) );
1630 if (cam->state & DEV_DISCONNECTED)
1632 if (!timeout || (cam->state & DEV_MISCONFIGURED))
1636 spin_lock_irqsave(&cam->queue_lock, lock_flags);
1637 f = list_entry(cam->outqueue.next, struct zc0301_frame_t, frame);
1638 list_del(cam->outqueue.next);
1639 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1641 f->state = F_UNUSED;
1643 memcpy(&b, &f->buf, sizeof(b));
1644 if (f->vma_use_count)
1645 b.flags |= V4L2_BUF_FLAG_MAPPED;
1647 if (copy_to_user(arg, &b, sizeof(b)))
1650 PDBGG("Frame #%lu dequeued", (unsigned long)f->buf.index);
1657 zc0301_vidioc_streamon(struct zc0301_device* cam, void __user * arg)
1661 if (copy_from_user(&type, arg, sizeof(type)))
1664 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
1667 if (list_empty(&cam->inqueue))
1670 cam->stream = STREAM_ON;
1672 DBG(3, "Stream on");
1679 zc0301_vidioc_streamoff(struct zc0301_device* cam, void __user * arg)
1683 if (copy_from_user(&type, arg, sizeof(type)))
1686 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
1689 if (cam->stream == STREAM_ON)
1690 if ((err = zc0301_stream_interrupt(cam)))
1693 zc0301_empty_framequeues(cam);
1695 DBG(3, "Stream off");
1702 zc0301_vidioc_g_parm(struct zc0301_device* cam, void __user * arg)
1704 struct v4l2_streamparm sp;
1706 if (copy_from_user(&sp, arg, sizeof(sp)))
1709 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1712 sp.parm.capture.extendedmode = 0;
1713 sp.parm.capture.readbuffers = cam->nreadbuffers;
1715 if (copy_to_user(arg, &sp, sizeof(sp)))
1723 zc0301_vidioc_s_parm(struct zc0301_device* cam, void __user * arg)
1725 struct v4l2_streamparm sp;
1727 if (copy_from_user(&sp, arg, sizeof(sp)))
1730 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1733 sp.parm.capture.extendedmode = 0;
1735 if (sp.parm.capture.readbuffers == 0)
1736 sp.parm.capture.readbuffers = cam->nreadbuffers;
1738 if (sp.parm.capture.readbuffers > ZC0301_MAX_FRAMES)
1739 sp.parm.capture.readbuffers = ZC0301_MAX_FRAMES;
1741 if (copy_to_user(arg, &sp, sizeof(sp)))
1744 cam->nreadbuffers = sp.parm.capture.readbuffers;
1750 static int zc0301_ioctl_v4l2(struct inode* inode, struct file* filp,
1751 unsigned int cmd, void __user * arg)
1753 struct zc0301_device* cam = video_get_drvdata(video_devdata(filp));
1757 case VIDIOC_QUERYCAP:
1758 return zc0301_vidioc_querycap(cam, arg);
1760 case VIDIOC_ENUMINPUT:
1761 return zc0301_vidioc_enuminput(cam, arg);
1763 case VIDIOC_G_INPUT:
1764 return zc0301_vidioc_g_input(cam, arg);
1766 case VIDIOC_S_INPUT:
1767 return zc0301_vidioc_s_input(cam, arg);
1769 case VIDIOC_QUERYCTRL:
1770 return zc0301_vidioc_query_ctrl(cam, arg);
1773 return zc0301_vidioc_g_ctrl(cam, arg);
1775 case VIDIOC_S_CTRL_OLD:
1777 return zc0301_vidioc_s_ctrl(cam, arg);
1779 case VIDIOC_CROPCAP_OLD:
1780 case VIDIOC_CROPCAP:
1781 return zc0301_vidioc_cropcap(cam, arg);
1784 return zc0301_vidioc_g_crop(cam, arg);
1787 return zc0301_vidioc_s_crop(cam, arg);
1789 case VIDIOC_ENUM_FMT:
1790 return zc0301_vidioc_enum_fmt(cam, arg);
1793 return zc0301_vidioc_g_fmt(cam, arg);
1795 case VIDIOC_TRY_FMT:
1797 return zc0301_vidioc_try_s_fmt(cam, cmd, arg);
1799 case VIDIOC_G_JPEGCOMP:
1800 return zc0301_vidioc_g_jpegcomp(cam, arg);
1802 case VIDIOC_S_JPEGCOMP:
1803 return zc0301_vidioc_s_jpegcomp(cam, arg);
1805 case VIDIOC_REQBUFS:
1806 return zc0301_vidioc_reqbufs(cam, arg);
1808 case VIDIOC_QUERYBUF:
1809 return zc0301_vidioc_querybuf(cam, arg);
1812 return zc0301_vidioc_qbuf(cam, arg);
1815 return zc0301_vidioc_dqbuf(cam, filp, arg);
1817 case VIDIOC_STREAMON:
1818 return zc0301_vidioc_streamon(cam, arg);
1820 case VIDIOC_STREAMOFF:
1821 return zc0301_vidioc_streamoff(cam, arg);
1824 return zc0301_vidioc_g_parm(cam, arg);
1826 case VIDIOC_S_PARM_OLD:
1828 return zc0301_vidioc_s_parm(cam, arg);
1832 case VIDIOC_QUERYSTD:
1833 case VIDIOC_ENUMSTD:
1834 case VIDIOC_QUERYMENU:
1844 static int zc0301_ioctl(struct inode* inode, struct file* filp,
1845 unsigned int cmd, unsigned long arg)
1847 struct zc0301_device* cam = video_get_drvdata(video_devdata(filp));
1850 if (mutex_lock_interruptible(&cam->fileop_mutex))
1851 return -ERESTARTSYS;
1853 if (cam->state & DEV_DISCONNECTED) {
1854 DBG(1, "Device not present");
1855 mutex_unlock(&cam->fileop_mutex);
1859 if (cam->state & DEV_MISCONFIGURED) {
1860 DBG(1, "The camera is misconfigured. Close and open it "
1862 mutex_unlock(&cam->fileop_mutex);
1866 V4LDBG(3, "zc0301", cmd);
1868 err = zc0301_ioctl_v4l2(inode, filp, cmd, (void __user *)arg);
1870 mutex_unlock(&cam->fileop_mutex);
1876 static struct file_operations zc0301_fops = {
1877 .owner = THIS_MODULE,
1878 .open = zc0301_open,
1879 .release = zc0301_release,
1880 .ioctl = zc0301_ioctl,
1881 .read = zc0301_read,
1882 .poll = zc0301_poll,
1883 .mmap = zc0301_mmap,
1884 .llseek = no_llseek,
1887 /*****************************************************************************/
1890 zc0301_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
1892 struct usb_device *udev = interface_to_usbdev(intf);
1893 struct zc0301_device* cam;
1894 static unsigned int dev_nr = 0;
1898 if (!(cam = kzalloc(sizeof(struct zc0301_device), GFP_KERNEL)))
1903 if (!(cam->control_buffer = kzalloc(4, GFP_KERNEL))) {
1904 DBG(1, "kmalloc() failed");
1909 if (!(cam->v4ldev = video_device_alloc())) {
1910 DBG(1, "video_device_alloc() failed");
1915 mutex_init(&cam->dev_mutex);
1917 DBG(2, "ZC0301 Image Processor and Control Chip detected "
1918 "(vid/pid 0x%04X/0x%04X)",id->idVendor, id->idProduct);
1920 for (i = 0; zc0301_sensor_table[i]; i++) {
1921 err = zc0301_sensor_table[i](cam);
1927 DBG(2, "%s image sensor detected", cam->sensor.name);
1929 DBG(1, "No supported image sensor detected");
1934 if (zc0301_init(cam)) {
1935 DBG(1, "Initialization failed. I will retry on open().");
1936 cam->state |= DEV_MISCONFIGURED;
1939 strcpy(cam->v4ldev->name, "ZC0301 PC Camera");
1940 cam->v4ldev->owner = THIS_MODULE;
1941 cam->v4ldev->type = VID_TYPE_CAPTURE | VID_TYPE_SCALES;
1942 cam->v4ldev->hardware = 0;
1943 cam->v4ldev->fops = &zc0301_fops;
1944 cam->v4ldev->minor = video_nr[dev_nr];
1945 cam->v4ldev->release = video_device_release;
1946 video_set_drvdata(cam->v4ldev, cam);
1948 mutex_lock(&cam->dev_mutex);
1950 err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER,
1953 DBG(1, "V4L2 device registration failed");
1954 if (err == -ENFILE && video_nr[dev_nr] == -1)
1955 DBG(1, "Free /dev/videoX node not found");
1956 video_nr[dev_nr] = -1;
1957 dev_nr = (dev_nr < ZC0301_MAX_DEVICES-1) ? dev_nr+1 : 0;
1958 mutex_unlock(&cam->dev_mutex);
1962 DBG(2, "V4L2 device registered as /dev/video%d", cam->v4ldev->minor);
1964 cam->module_param.force_munmap = force_munmap[dev_nr];
1965 cam->module_param.frame_timeout = frame_timeout[dev_nr];
1967 dev_nr = (dev_nr < ZC0301_MAX_DEVICES-1) ? dev_nr+1 : 0;
1969 usb_set_intfdata(intf, cam);
1971 mutex_unlock(&cam->dev_mutex);
1977 kfree(cam->control_buffer);
1979 video_device_release(cam->v4ldev);
1986 static void zc0301_usb_disconnect(struct usb_interface* intf)
1988 struct zc0301_device* cam = usb_get_intfdata(intf);
1993 down_write(&zc0301_disconnect);
1995 mutex_lock(&cam->dev_mutex);
1997 DBG(2, "Disconnecting %s...", cam->v4ldev->name);
1999 wake_up_interruptible_all(&cam->open);
2002 DBG(2, "Device /dev/video%d is open! Deregistration and "
2003 "memory deallocation are deferred on close.",
2004 cam->v4ldev->minor);
2005 cam->state |= DEV_MISCONFIGURED;
2006 zc0301_stop_transfer(cam);
2007 cam->state |= DEV_DISCONNECTED;
2008 wake_up_interruptible(&cam->wait_frame);
2009 wake_up(&cam->wait_stream);
2010 usb_get_dev(cam->usbdev);
2012 cam->state |= DEV_DISCONNECTED;
2013 zc0301_release_resources(cam);
2016 mutex_unlock(&cam->dev_mutex);
2021 up_write(&zc0301_disconnect);
2025 static struct usb_driver zc0301_usb_driver = {
2027 .id_table = zc0301_id_table,
2028 .probe = zc0301_usb_probe,
2029 .disconnect = zc0301_usb_disconnect,
2032 /*****************************************************************************/
2034 static int __init zc0301_module_init(void)
2038 KDBG(2, ZC0301_MODULE_NAME " v" ZC0301_MODULE_VERSION);
2039 KDBG(3, ZC0301_MODULE_AUTHOR);
2041 if ((err = usb_register(&zc0301_usb_driver)))
2042 KDBG(1, "usb_register() failed");
2048 static void __exit zc0301_module_exit(void)
2050 usb_deregister(&zc0301_usb_driver);
2054 module_init(zc0301_module_init);
2055 module_exit(zc0301_module_exit);