1 /***************************************************************************
2 * V4L2 driver for ET61X[12]51 PC Camera Controllers *
4 * Copyright (C) 2006-2007 by Luca Risolia <luca.risolia@studio.unibo.it> *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
19 ***************************************************************************/
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/kernel.h>
24 #include <linux/param.h>
25 #include <linux/errno.h>
26 #include <linux/slab.h>
27 #include <linux/device.h>
29 #include <linux/delay.h>
30 #include <linux/compiler.h>
31 #include <linux/ioctl.h>
32 #include <linux/poll.h>
33 #include <linux/stat.h>
35 #include <linux/vmalloc.h>
36 #include <linux/page-flags.h>
37 #include <linux/byteorder/generic.h>
39 #include <asm/uaccess.h>
43 /*****************************************************************************/
45 #define ET61X251_MODULE_NAME "V4L2 driver for ET61X[12]51 " \
46 "PC Camera Controllers"
47 #define ET61X251_MODULE_AUTHOR "(C) 2006-2007 Luca Risolia"
48 #define ET61X251_AUTHOR_EMAIL "<luca.risolia@studio.unibo.it>"
49 #define ET61X251_MODULE_LICENSE "GPL"
50 #define ET61X251_MODULE_VERSION "1:1.09"
51 #define ET61X251_MODULE_VERSION_CODE KERNEL_VERSION(1, 1, 9)
53 /*****************************************************************************/
55 MODULE_DEVICE_TABLE(usb, et61x251_id_table);
57 MODULE_AUTHOR(ET61X251_MODULE_AUTHOR " " ET61X251_AUTHOR_EMAIL);
58 MODULE_DESCRIPTION(ET61X251_MODULE_NAME);
59 MODULE_VERSION(ET61X251_MODULE_VERSION);
60 MODULE_LICENSE(ET61X251_MODULE_LICENSE);
62 static short video_nr[] = {[0 ... ET61X251_MAX_DEVICES-1] = -1};
63 module_param_array(video_nr, short, NULL, 0444);
64 MODULE_PARM_DESC(video_nr,
65 "\n<-1|n[,...]> Specify V4L2 minor mode number."
66 "\n -1 = use next available (default)"
67 "\n n = use minor number n (integer >= 0)"
68 "\nYou can specify up to "
69 __MODULE_STRING(ET61X251_MAX_DEVICES) " cameras this way."
71 "\nvideo_nr=-1,2,-1 would assign minor number 2 to"
72 "\nthe second registered camera and use auto for the first"
73 "\none and for every other camera."
76 static short force_munmap[] = {[0 ... ET61X251_MAX_DEVICES-1] =
77 ET61X251_FORCE_MUNMAP};
78 module_param_array(force_munmap, bool, NULL, 0444);
79 MODULE_PARM_DESC(force_munmap,
80 "\n<0|1[,...]> Force the application to unmap previously"
81 "\nmapped buffer memory before calling any VIDIOC_S_CROP or"
82 "\nVIDIOC_S_FMT ioctl's. Not all the applications support"
83 "\nthis feature. This parameter is specific for each"
85 "\n 0 = do not force memory unmapping"
86 "\n 1 = force memory unmapping (save memory)"
87 "\nDefault value is "__MODULE_STRING(ET61X251_FORCE_MUNMAP)"."
90 static unsigned int frame_timeout[] = {[0 ... ET61X251_MAX_DEVICES-1] =
91 ET61X251_FRAME_TIMEOUT};
92 module_param_array(frame_timeout, uint, NULL, 0644);
93 MODULE_PARM_DESC(frame_timeout,
94 "\n<n[,...]> Timeout for a video frame in seconds."
95 "\nThis parameter is specific for each detected camera."
97 __MODULE_STRING(ET61X251_FRAME_TIMEOUT)"."
100 #ifdef ET61X251_DEBUG
101 static unsigned short debug = ET61X251_DEBUG_LEVEL;
102 module_param(debug, ushort, 0644);
103 MODULE_PARM_DESC(debug,
104 "\n<n> Debugging information level, from 0 to 3:"
105 "\n0 = none (use carefully)"
106 "\n1 = critical errors"
107 "\n2 = significant informations"
108 "\n3 = more verbose messages"
109 "\nLevel 3 is useful for testing only, when only "
110 "one device is used."
111 "\nDefault value is "__MODULE_STRING(ET61X251_DEBUG_LEVEL)"."
115 /*****************************************************************************/
118 et61x251_request_buffers(struct et61x251_device* cam, u32 count,
119 enum et61x251_io_method io)
121 struct v4l2_pix_format* p = &(cam->sensor.pix_format);
122 struct v4l2_rect* r = &(cam->sensor.cropcap.bounds);
123 const size_t imagesize = cam->module_param.force_munmap ||
125 (p->width * p->height * p->priv) / 8 :
126 (r->width * r->height * p->priv) / 8;
130 if (count > ET61X251_MAX_FRAMES)
131 count = ET61X251_MAX_FRAMES;
133 cam->nbuffers = count;
134 while (cam->nbuffers > 0) {
135 if ((buff = vmalloc_32_user(cam->nbuffers *
136 PAGE_ALIGN(imagesize))))
141 for (i = 0; i < cam->nbuffers; i++) {
142 cam->frame[i].bufmem = buff + i*PAGE_ALIGN(imagesize);
143 cam->frame[i].buf.index = i;
144 cam->frame[i].buf.m.offset = i*PAGE_ALIGN(imagesize);
145 cam->frame[i].buf.length = imagesize;
146 cam->frame[i].buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
147 cam->frame[i].buf.sequence = 0;
148 cam->frame[i].buf.field = V4L2_FIELD_NONE;
149 cam->frame[i].buf.memory = V4L2_MEMORY_MMAP;
150 cam->frame[i].buf.flags = 0;
153 return cam->nbuffers;
157 static void et61x251_release_buffers(struct et61x251_device* cam)
160 vfree(cam->frame[0].bufmem);
163 cam->frame_current = NULL;
167 static void et61x251_empty_framequeues(struct et61x251_device* cam)
171 INIT_LIST_HEAD(&cam->inqueue);
172 INIT_LIST_HEAD(&cam->outqueue);
174 for (i = 0; i < ET61X251_MAX_FRAMES; i++) {
175 cam->frame[i].state = F_UNUSED;
176 cam->frame[i].buf.bytesused = 0;
181 static void et61x251_requeue_outqueue(struct et61x251_device* cam)
183 struct et61x251_frame_t *i;
185 list_for_each_entry(i, &cam->outqueue, frame) {
187 list_add(&i->frame, &cam->inqueue);
190 INIT_LIST_HEAD(&cam->outqueue);
194 static void et61x251_queue_unusedframes(struct et61x251_device* cam)
196 unsigned long lock_flags;
199 for (i = 0; i < cam->nbuffers; i++)
200 if (cam->frame[i].state == F_UNUSED) {
201 cam->frame[i].state = F_QUEUED;
202 spin_lock_irqsave(&cam->queue_lock, lock_flags);
203 list_add_tail(&cam->frame[i].frame, &cam->inqueue);
204 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
208 /*****************************************************************************/
210 int et61x251_write_reg(struct et61x251_device* cam, u8 value, u16 index)
212 struct usb_device* udev = cam->usbdev;
213 u8* buff = cam->control_buffer;
218 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
219 0, index, buff, 1, ET61X251_CTRL_TIMEOUT);
221 DBG(3, "Failed to write a register (value 0x%02X, index "
222 "0x%02X, error %d)", value, index, res);
230 int et61x251_read_reg(struct et61x251_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), 0x00, 0xc1,
237 0, index, buff, 1, ET61X251_CTRL_TIMEOUT);
239 DBG(3, "Failed to read a register (index 0x%02X, error %d)",
242 return (res >= 0) ? (int)(*buff) : -1;
247 et61x251_i2c_wait(struct et61x251_device* cam,
248 const struct et61x251_sensor* sensor)
252 for (i = 1; i <= 8; i++) {
253 if (sensor->interface == ET61X251_I2C_3WIRES) {
254 r = et61x251_read_reg(cam, 0x8e);
255 if (!(r & 0x02) && (r >= 0))
258 r = et61x251_read_reg(cam, 0x8b);
259 if (!(r & 0x01) && (r >= 0))
264 udelay(8*8); /* minimum for sensors at 400kHz */
272 et61x251_i2c_try_read(struct et61x251_device* cam,
273 const struct et61x251_sensor* sensor, u8 address)
275 struct usb_device* udev = cam->usbdev;
276 u8* data = cam->control_buffer;
280 data[1] = cam->sensor.i2c_slave_id;
281 data[2] = cam->sensor.rsta | 0x10;
282 data[3] = !(et61x251_read_reg(cam, 0x8b) & 0x02);
283 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
284 0, 0x88, data, 4, ET61X251_CTRL_TIMEOUT);
288 err += et61x251_i2c_wait(cam, sensor);
290 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1,
291 0, 0x80, data, 8, ET61X251_CTRL_TIMEOUT);
296 DBG(3, "I2C read failed for %s image sensor", sensor->name);
298 PDBGG("I2C read: address 0x%02X, value: 0x%02X", address, data[0]);
300 return err ? -1 : (int)data[0];
305 et61x251_i2c_try_write(struct et61x251_device* cam,
306 const struct et61x251_sensor* sensor, u8 address,
309 struct usb_device* udev = cam->usbdev;
310 u8* data = cam->control_buffer;
314 data[1] = cam->sensor.i2c_slave_id;
315 data[2] = cam->sensor.rsta | 0x12;
316 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
317 0, 0x88, data, 3, ET61X251_CTRL_TIMEOUT);
322 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
323 0, 0x80, data, 1, ET61X251_CTRL_TIMEOUT);
327 err += et61x251_i2c_wait(cam, sensor);
330 DBG(3, "I2C write failed for %s image sensor", sensor->name);
332 PDBGG("I2C write: address 0x%02X, value: 0x%02X", address, value);
339 et61x251_i2c_raw_write(struct et61x251_device* cam, u8 n, u8 data1, u8 data2,
340 u8 data3, u8 data4, u8 data5, u8 data6, u8 data7,
341 u8 data8, u8 address)
343 struct usb_device* udev = cam->usbdev;
344 u8* data = cam->control_buffer;
354 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
355 0, 0x81, data, n-1, ET61X251_CTRL_TIMEOUT);
360 data[1] = cam->sensor.i2c_slave_id;
361 data[2] = cam->sensor.rsta | 0x02 | (n << 4);
362 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
363 0, 0x88, data, 3, ET61X251_CTRL_TIMEOUT);
367 /* Start writing through the serial interface */
369 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
370 0, 0x80, data, 1, ET61X251_CTRL_TIMEOUT);
374 err += et61x251_i2c_wait(cam, &cam->sensor);
377 DBG(3, "I2C raw write failed for %s image sensor",
380 PDBGG("I2C raw write: %u bytes, address = 0x%02X, data1 = 0x%02X, "
381 "data2 = 0x%02X, data3 = 0x%02X, data4 = 0x%02X, data5 = 0x%02X,"
382 " data6 = 0x%02X, data7 = 0x%02X, data8 = 0x%02X", n, address,
383 data1, data2, data3, data4, data5, data6, data7, data8);
390 int et61x251_i2c_read(struct et61x251_device* cam, u8 address)
392 return et61x251_i2c_try_read(cam, &cam->sensor, address);
396 int et61x251_i2c_write(struct et61x251_device* cam, u8 address, u8 value)
398 return et61x251_i2c_try_write(cam, &cam->sensor, address, value);
401 /*****************************************************************************/
403 static void et61x251_urb_complete(struct urb *urb)
405 struct et61x251_device* cam = urb->context;
406 struct et61x251_frame_t** f;
411 if (urb->status == -ENOENT)
414 f = &cam->frame_current;
416 if (cam->stream == STREAM_INTERRUPT) {
417 cam->stream = STREAM_OFF;
419 (*f)->state = F_QUEUED;
420 DBG(3, "Stream interrupted");
421 wake_up(&cam->wait_stream);
424 if (cam->state & DEV_DISCONNECTED)
427 if (cam->state & DEV_MISCONFIGURED) {
428 wake_up_interruptible(&cam->wait_frame);
432 if (cam->stream == STREAM_OFF || list_empty(&cam->inqueue))
436 (*f) = list_entry(cam->inqueue.next, struct et61x251_frame_t,
439 imagesize = (cam->sensor.pix_format.width *
440 cam->sensor.pix_format.height *
441 cam->sensor.pix_format.priv) / 8;
443 for (i = 0; i < urb->number_of_packets; i++) {
444 unsigned int len, status;
447 const u8 VOID_BYTES = 6;
450 len = urb->iso_frame_desc[i].actual_length;
451 status = urb->iso_frame_desc[i].status;
452 pos = urb->iso_frame_desc[i].offset + urb->transfer_buffer;
455 DBG(3, "Error in isochronous frame");
456 (*f)->state = F_ERROR;
462 sof = ((*b1 & 0x3f) == 63);
463 imglen = ((*b1 & 0xc0) << 2) | *b2;
465 PDBGG("Isochrnous frame: length %u, #%u i, image length %zu",
468 if ((*f)->state == F_QUEUED || (*f)->state == F_ERROR)
471 (*f)->state = F_GRABBING;
472 (*f)->buf.bytesused = 0;
473 do_gettimeofday(&(*f)->buf.timestamp);
475 DBG(3, "SOF detected: new video frame");
478 if ((*f)->state == F_GRABBING) {
479 if (sof && (*f)->buf.bytesused) {
480 if (cam->sensor.pix_format.pixelformat ==
481 V4L2_PIX_FMT_ET61X251)
484 DBG(3, "Not expected SOF detected "
486 (unsigned long)(*f)->buf.bytesused);
487 (*f)->state = F_ERROR;
492 if ((*f)->buf.bytesused + imglen > imagesize) {
493 DBG(3, "Video frame size exceeded");
494 (*f)->state = F_ERROR;
500 memcpy((*f)->bufmem+(*f)->buf.bytesused, pos, imglen);
501 (*f)->buf.bytesused += imglen;
503 if ((*f)->buf.bytesused == imagesize) {
506 b = (*f)->buf.bytesused;
507 (*f)->state = F_DONE;
508 (*f)->buf.sequence= ++cam->frame_count;
509 spin_lock(&cam->queue_lock);
510 list_move_tail(&(*f)->frame, &cam->outqueue);
511 if (!list_empty(&cam->inqueue))
512 (*f) = list_entry(cam->inqueue.next,
513 struct et61x251_frame_t,
517 spin_unlock(&cam->queue_lock);
518 DBG(3, "Video frame captured: : %lu bytes",
525 cam->sensor.pix_format.pixelformat ==
526 V4L2_PIX_FMT_ET61X251)
533 urb->dev = cam->usbdev;
534 err = usb_submit_urb(urb, GFP_ATOMIC);
535 if (err < 0 && err != -EPERM) {
536 cam->state |= DEV_MISCONFIGURED;
537 DBG(1, "usb_submit_urb() failed");
540 wake_up_interruptible(&cam->wait_frame);
544 static int et61x251_start_transfer(struct et61x251_device* cam)
546 struct usb_device *udev = cam->usbdev;
548 struct usb_host_interface* altsetting = usb_altnum_to_altsetting(
549 usb_ifnum_to_if(udev, 0),
550 ET61X251_ALTERNATE_SETTING);
551 const unsigned int psz = le16_to_cpu(altsetting->
552 endpoint[0].desc.wMaxPacketSize);
556 for (i = 0; i < ET61X251_URBS; i++) {
557 cam->transfer_buffer[i] = kzalloc(ET61X251_ISO_PACKETS * psz,
559 if (!cam->transfer_buffer[i]) {
561 DBG(1, "Not enough memory");
566 for (i = 0; i < ET61X251_URBS; i++) {
567 urb = usb_alloc_urb(ET61X251_ISO_PACKETS, GFP_KERNEL);
571 DBG(1, "usb_alloc_urb() failed");
576 urb->pipe = usb_rcvisocpipe(udev, 1);
577 urb->transfer_flags = URB_ISO_ASAP;
578 urb->number_of_packets = ET61X251_ISO_PACKETS;
579 urb->complete = et61x251_urb_complete;
580 urb->transfer_buffer = cam->transfer_buffer[i];
581 urb->transfer_buffer_length = psz * ET61X251_ISO_PACKETS;
583 for (j = 0; j < ET61X251_ISO_PACKETS; j++) {
584 urb->iso_frame_desc[j].offset = psz * j;
585 urb->iso_frame_desc[j].length = psz;
589 err = et61x251_write_reg(cam, 0x01, 0x03);
590 err = et61x251_write_reg(cam, 0x00, 0x03);
591 err = et61x251_write_reg(cam, 0x08, 0x03);
594 DBG(1, "I/O hardware error");
598 err = usb_set_interface(udev, 0, ET61X251_ALTERNATE_SETTING);
600 DBG(1, "usb_set_interface() failed");
604 cam->frame_current = NULL;
606 for (i = 0; i < ET61X251_URBS; i++) {
607 err = usb_submit_urb(cam->urb[i], GFP_KERNEL);
609 for (j = i-1; j >= 0; j--)
610 usb_kill_urb(cam->urb[j]);
611 DBG(1, "usb_submit_urb() failed, error %d", err);
619 for (i = 0; (i < ET61X251_URBS) && cam->urb[i]; i++)
620 usb_free_urb(cam->urb[i]);
623 for (i = 0; (i < ET61X251_URBS) && cam->transfer_buffer[i]; i++)
624 kfree(cam->transfer_buffer[i]);
630 static int et61x251_stop_transfer(struct et61x251_device* cam)
632 struct usb_device *udev = cam->usbdev;
636 if (cam->state & DEV_DISCONNECTED)
639 for (i = ET61X251_URBS-1; i >= 0; i--) {
640 usb_kill_urb(cam->urb[i]);
641 usb_free_urb(cam->urb[i]);
642 kfree(cam->transfer_buffer[i]);
645 err = usb_set_interface(udev, 0, 0); /* 0 Mb/s */
647 DBG(3, "usb_set_interface() failed");
653 static int et61x251_stream_interrupt(struct et61x251_device* cam)
657 cam->stream = STREAM_INTERRUPT;
658 timeout = wait_event_timeout(cam->wait_stream,
659 (cam->stream == STREAM_OFF) ||
660 (cam->state & DEV_DISCONNECTED),
661 ET61X251_URB_TIMEOUT);
662 if (cam->state & DEV_DISCONNECTED)
664 else if (cam->stream != STREAM_OFF) {
665 cam->state |= DEV_MISCONFIGURED;
666 DBG(1, "URB timeout reached. The camera is misconfigured. To "
667 "use it, close and open /dev/video%d again.",
675 /*****************************************************************************/
677 #ifdef CONFIG_VIDEO_ADV_DEBUG
678 static u8 et61x251_strtou8(const char* buff, size_t len, ssize_t* count)
685 strncpy(str, buff, len);
688 strncpy(str, buff, 4);
692 val = simple_strtoul(str, &endp, 0);
696 *count = (ssize_t)(endp - str);
697 if ((*count) && (len == *count+1) && (buff[*count] == '\n'))
704 NOTE 1: being inside one of the following methods implies that the v4l
705 device exists for sure (see kobjects and reference counters)
706 NOTE 2: buffers are PAGE_SIZE long
709 static ssize_t et61x251_show_reg(struct class_device* cd, char* buf)
711 struct et61x251_device* cam;
714 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
717 cam = video_get_drvdata(to_video_device(cd));
719 mutex_unlock(&et61x251_sysfs_lock);
723 count = sprintf(buf, "%u\n", cam->sysfs.reg);
725 mutex_unlock(&et61x251_sysfs_lock);
732 et61x251_store_reg(struct class_device* cd, const char* buf, size_t len)
734 struct et61x251_device* cam;
738 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
741 cam = video_get_drvdata(to_video_device(cd));
743 mutex_unlock(&et61x251_sysfs_lock);
747 index = et61x251_strtou8(buf, len, &count);
748 if (index > 0x8e || !count) {
749 mutex_unlock(&et61x251_sysfs_lock);
753 cam->sysfs.reg = index;
755 DBG(2, "Moved ET61X[12]51 register index to 0x%02X", cam->sysfs.reg);
756 DBG(3, "Written bytes: %zd", count);
758 mutex_unlock(&et61x251_sysfs_lock);
764 static ssize_t et61x251_show_val(struct class_device* cd, char* buf)
766 struct et61x251_device* cam;
770 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
773 cam = video_get_drvdata(to_video_device(cd));
775 mutex_unlock(&et61x251_sysfs_lock);
779 if ((val = et61x251_read_reg(cam, cam->sysfs.reg)) < 0) {
780 mutex_unlock(&et61x251_sysfs_lock);
784 count = sprintf(buf, "%d\n", val);
786 DBG(3, "Read bytes: %zd", count);
788 mutex_unlock(&et61x251_sysfs_lock);
795 et61x251_store_val(struct class_device* cd, const char* buf, size_t len)
797 struct et61x251_device* cam;
802 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
805 cam = video_get_drvdata(to_video_device(cd));
807 mutex_unlock(&et61x251_sysfs_lock);
811 value = et61x251_strtou8(buf, len, &count);
813 mutex_unlock(&et61x251_sysfs_lock);
817 err = et61x251_write_reg(cam, value, cam->sysfs.reg);
819 mutex_unlock(&et61x251_sysfs_lock);
823 DBG(2, "Written ET61X[12]51 reg. 0x%02X, val. 0x%02X",
824 cam->sysfs.reg, value);
825 DBG(3, "Written bytes: %zd", count);
827 mutex_unlock(&et61x251_sysfs_lock);
833 static ssize_t et61x251_show_i2c_reg(struct class_device* cd, char* buf)
835 struct et61x251_device* cam;
838 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
841 cam = video_get_drvdata(to_video_device(cd));
843 mutex_unlock(&et61x251_sysfs_lock);
847 count = sprintf(buf, "%u\n", cam->sysfs.i2c_reg);
849 DBG(3, "Read bytes: %zd", count);
851 mutex_unlock(&et61x251_sysfs_lock);
858 et61x251_store_i2c_reg(struct class_device* cd, const char* buf, size_t len)
860 struct et61x251_device* cam;
864 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
867 cam = video_get_drvdata(to_video_device(cd));
869 mutex_unlock(&et61x251_sysfs_lock);
873 index = et61x251_strtou8(buf, len, &count);
875 mutex_unlock(&et61x251_sysfs_lock);
879 cam->sysfs.i2c_reg = index;
881 DBG(2, "Moved sensor register index to 0x%02X", cam->sysfs.i2c_reg);
882 DBG(3, "Written bytes: %zd", count);
884 mutex_unlock(&et61x251_sysfs_lock);
890 static ssize_t et61x251_show_i2c_val(struct class_device* cd, char* buf)
892 struct et61x251_device* cam;
896 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
899 cam = video_get_drvdata(to_video_device(cd));
901 mutex_unlock(&et61x251_sysfs_lock);
905 if (!(cam->sensor.sysfs_ops & ET61X251_I2C_READ)) {
906 mutex_unlock(&et61x251_sysfs_lock);
910 if ((val = et61x251_i2c_read(cam, cam->sysfs.i2c_reg)) < 0) {
911 mutex_unlock(&et61x251_sysfs_lock);
915 count = sprintf(buf, "%d\n", val);
917 DBG(3, "Read bytes: %zd", count);
919 mutex_unlock(&et61x251_sysfs_lock);
926 et61x251_store_i2c_val(struct class_device* cd, const char* buf, size_t len)
928 struct et61x251_device* cam;
933 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
936 cam = video_get_drvdata(to_video_device(cd));
938 mutex_unlock(&et61x251_sysfs_lock);
942 if (!(cam->sensor.sysfs_ops & ET61X251_I2C_READ)) {
943 mutex_unlock(&et61x251_sysfs_lock);
947 value = et61x251_strtou8(buf, len, &count);
949 mutex_unlock(&et61x251_sysfs_lock);
953 err = et61x251_i2c_write(cam, cam->sysfs.i2c_reg, value);
955 mutex_unlock(&et61x251_sysfs_lock);
959 DBG(2, "Written sensor reg. 0x%02X, val. 0x%02X",
960 cam->sysfs.i2c_reg, value);
961 DBG(3, "Written bytes: %zd", count);
963 mutex_unlock(&et61x251_sysfs_lock);
969 static CLASS_DEVICE_ATTR(reg, S_IRUGO | S_IWUSR,
970 et61x251_show_reg, et61x251_store_reg);
971 static CLASS_DEVICE_ATTR(val, S_IRUGO | S_IWUSR,
972 et61x251_show_val, et61x251_store_val);
973 static CLASS_DEVICE_ATTR(i2c_reg, S_IRUGO | S_IWUSR,
974 et61x251_show_i2c_reg, et61x251_store_i2c_reg);
975 static CLASS_DEVICE_ATTR(i2c_val, S_IRUGO | S_IWUSR,
976 et61x251_show_i2c_val, et61x251_store_i2c_val);
979 static int et61x251_create_sysfs(struct et61x251_device* cam)
981 struct class_device *classdev = &(cam->v4ldev->class_dev);
984 if ((err = class_device_create_file(classdev, &class_device_attr_reg)))
986 if ((err = class_device_create_file(classdev, &class_device_attr_val)))
989 if (cam->sensor.sysfs_ops) {
990 if ((err = class_device_create_file(classdev,
991 &class_device_attr_i2c_reg)))
993 if ((err = class_device_create_file(classdev,
994 &class_device_attr_i2c_val)))
999 if (cam->sensor.sysfs_ops)
1000 class_device_remove_file(classdev, &class_device_attr_i2c_reg);
1002 class_device_remove_file(classdev, &class_device_attr_val);
1004 class_device_remove_file(classdev, &class_device_attr_reg);
1008 #endif /* CONFIG_VIDEO_ADV_DEBUG */
1010 /*****************************************************************************/
1013 et61x251_set_pix_format(struct et61x251_device* cam,
1014 struct v4l2_pix_format* pix)
1018 if ((r = et61x251_read_reg(cam, 0x12)) < 0)
1020 if (pix->pixelformat == V4L2_PIX_FMT_ET61X251)
1021 err += et61x251_write_reg(cam, r & 0xfd, 0x12);
1023 err += et61x251_write_reg(cam, r | 0x02, 0x12);
1025 return err ? -EIO : 0;
1030 et61x251_set_compression(struct et61x251_device* cam,
1031 struct v4l2_jpegcompression* compression)
1035 if ((r = et61x251_read_reg(cam, 0x12)) < 0)
1037 if (compression->quality == 0)
1038 err += et61x251_write_reg(cam, r & 0xfb, 0x12);
1040 err += et61x251_write_reg(cam, r | 0x04, 0x12);
1042 return err ? -EIO : 0;
1046 static int et61x251_set_scale(struct et61x251_device* cam, u8 scale)
1050 r = et61x251_read_reg(cam, 0x12);
1055 err += et61x251_write_reg(cam, r & ~0x01, 0x12);
1056 else if (scale == 2)
1057 err += et61x251_write_reg(cam, r | 0x01, 0x12);
1062 PDBGG("Scaling factor: %u", scale);
1069 et61x251_set_crop(struct et61x251_device* cam, struct v4l2_rect* rect)
1071 struct et61x251_sensor* s = &cam->sensor;
1072 u16 fmw_sx = (u16)(rect->left - s->cropcap.bounds.left +
1073 s->active_pixel.left),
1074 fmw_sy = (u16)(rect->top - s->cropcap.bounds.top +
1075 s->active_pixel.top),
1076 fmw_length = (u16)(rect->width),
1077 fmw_height = (u16)(rect->height);
1080 err += et61x251_write_reg(cam, fmw_sx & 0xff, 0x69);
1081 err += et61x251_write_reg(cam, fmw_sy & 0xff, 0x6a);
1082 err += et61x251_write_reg(cam, fmw_length & 0xff, 0x6b);
1083 err += et61x251_write_reg(cam, fmw_height & 0xff, 0x6c);
1084 err += et61x251_write_reg(cam, (fmw_sx >> 8) | ((fmw_sy & 0x300) >> 6)
1085 | ((fmw_length & 0x300) >> 4)
1086 | ((fmw_height & 0x300) >> 2), 0x6d);
1090 PDBGG("fmw_sx, fmw_sy, fmw_length, fmw_height: %u %u %u %u",
1091 fmw_sx, fmw_sy, fmw_length, fmw_height);
1097 static int et61x251_init(struct et61x251_device* cam)
1099 struct et61x251_sensor* s = &cam->sensor;
1100 struct v4l2_control ctrl;
1101 struct v4l2_queryctrl *qctrl;
1102 struct v4l2_rect* rect;
1106 if (!(cam->state & DEV_INITIALIZED)) {
1107 mutex_init(&cam->open_mutex);
1108 init_waitqueue_head(&cam->wait_open);
1110 rect = &(s->cropcap.defrect);
1111 cam->compression.quality = ET61X251_COMPRESSION_QUALITY;
1112 } else { /* use current values */
1117 err += et61x251_set_scale(cam, rect->width / s->pix_format.width);
1118 err += et61x251_set_crop(cam, rect);
1125 DBG(3, "Sensor initialization failed");
1130 err += et61x251_set_compression(cam, &cam->compression);
1131 err += et61x251_set_pix_format(cam, &s->pix_format);
1132 if (s->set_pix_format)
1133 err += s->set_pix_format(cam, &s->pix_format);
1137 if (s->pix_format.pixelformat == V4L2_PIX_FMT_ET61X251)
1138 DBG(3, "Compressed video format is active, quality %d",
1139 cam->compression.quality);
1141 DBG(3, "Uncompressed video format is active");
1144 if ((err = s->set_crop(cam, rect))) {
1145 DBG(3, "set_crop() failed");
1150 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1151 if (s->qctrl[i].id != 0 &&
1152 !(s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)) {
1153 ctrl.id = s->qctrl[i].id;
1154 ctrl.value = qctrl[i].default_value;
1155 err = s->set_ctrl(cam, &ctrl);
1157 DBG(3, "Set %s control failed",
1161 DBG(3, "Image sensor supports '%s' control",
1166 if (!(cam->state & DEV_INITIALIZED)) {
1167 mutex_init(&cam->fileop_mutex);
1168 spin_lock_init(&cam->queue_lock);
1169 init_waitqueue_head(&cam->wait_frame);
1170 init_waitqueue_head(&cam->wait_stream);
1171 cam->nreadbuffers = 2;
1172 memcpy(s->_qctrl, s->qctrl, sizeof(s->qctrl));
1173 memcpy(&(s->_rect), &(s->cropcap.defrect),
1174 sizeof(struct v4l2_rect));
1175 cam->state |= DEV_INITIALIZED;
1178 DBG(2, "Initialization succeeded");
1182 /*****************************************************************************/
1184 static void et61x251_release_resources(struct kref *kref)
1186 struct et61x251_device *cam;
1188 mutex_lock(&et61x251_sysfs_lock);
1190 cam = container_of(kref, struct et61x251_device, kref);
1192 DBG(2, "V4L2 device /dev/video%d deregistered", cam->v4ldev->minor);
1193 video_set_drvdata(cam->v4ldev, NULL);
1194 video_unregister_device(cam->v4ldev);
1195 usb_put_dev(cam->usbdev);
1196 kfree(cam->control_buffer);
1199 mutex_unlock(&et61x251_sysfs_lock);
1203 static int et61x251_open(struct inode* inode, struct file* filp)
1205 struct et61x251_device* cam;
1208 if (!down_read_trylock(&et61x251_dev_lock))
1209 return -ERESTARTSYS;
1211 cam = video_get_drvdata(video_devdata(filp));
1213 if (wait_for_completion_interruptible(&cam->probe)) {
1214 up_read(&et61x251_dev_lock);
1215 return -ERESTARTSYS;
1218 kref_get(&cam->kref);
1220 if (mutex_lock_interruptible(&cam->open_mutex)) {
1221 kref_put(&cam->kref, et61x251_release_resources);
1222 up_read(&et61x251_dev_lock);
1223 return -ERESTARTSYS;
1226 if (cam->state & DEV_DISCONNECTED) {
1227 DBG(1, "Device not present");
1233 DBG(2, "Device /dev/video%d is already in use",
1234 cam->v4ldev->minor);
1235 DBG(3, "Simultaneous opens are not supported");
1236 if ((filp->f_flags & O_NONBLOCK) ||
1237 (filp->f_flags & O_NDELAY)) {
1241 DBG(2, "A blocking open() has been requested. Wait for the "
1242 "device to be released...");
1243 up_read(&et61x251_dev_lock);
1244 err = wait_event_interruptible_exclusive(cam->wait_open,
1245 (cam->state & DEV_DISCONNECTED)
1247 down_read(&et61x251_dev_lock);
1250 if (cam->state & DEV_DISCONNECTED) {
1256 if (cam->state & DEV_MISCONFIGURED) {
1257 err = et61x251_init(cam);
1259 DBG(1, "Initialization failed again. "
1260 "I will retry on next open().");
1263 cam->state &= ~DEV_MISCONFIGURED;
1266 if ((err = et61x251_start_transfer(cam)))
1269 filp->private_data = cam;
1272 cam->stream = STREAM_OFF;
1274 cam->frame_count = 0;
1275 et61x251_empty_framequeues(cam);
1277 DBG(3, "Video device /dev/video%d is open", cam->v4ldev->minor);
1280 mutex_unlock(&cam->open_mutex);
1282 kref_put(&cam->kref, et61x251_release_resources);
1283 up_read(&et61x251_dev_lock);
1288 static int et61x251_release(struct inode* inode, struct file* filp)
1290 struct et61x251_device* cam;
1292 down_write(&et61x251_dev_lock);
1294 cam = video_get_drvdata(video_devdata(filp));
1296 et61x251_stop_transfer(cam);
1297 et61x251_release_buffers(cam);
1299 wake_up_interruptible_nr(&cam->wait_open, 1);
1301 DBG(3, "Video device /dev/video%d closed", cam->v4ldev->minor);
1303 kref_put(&cam->kref, et61x251_release_resources);
1305 up_write(&et61x251_dev_lock);
1312 et61x251_read(struct file* filp, char __user * buf,
1313 size_t count, loff_t* f_pos)
1315 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp));
1316 struct et61x251_frame_t* f, * i;
1317 unsigned long lock_flags;
1321 if (mutex_lock_interruptible(&cam->fileop_mutex))
1322 return -ERESTARTSYS;
1324 if (cam->state & DEV_DISCONNECTED) {
1325 DBG(1, "Device not present");
1326 mutex_unlock(&cam->fileop_mutex);
1330 if (cam->state & DEV_MISCONFIGURED) {
1331 DBG(1, "The camera is misconfigured. Close and open it "
1333 mutex_unlock(&cam->fileop_mutex);
1337 if (cam->io == IO_MMAP) {
1338 DBG(3, "Close and open the device again to choose the read "
1340 mutex_unlock(&cam->fileop_mutex);
1344 if (cam->io == IO_NONE) {
1345 if (!et61x251_request_buffers(cam, cam->nreadbuffers,
1347 DBG(1, "read() failed, not enough memory");
1348 mutex_unlock(&cam->fileop_mutex);
1352 cam->stream = STREAM_ON;
1355 if (list_empty(&cam->inqueue)) {
1356 if (!list_empty(&cam->outqueue))
1357 et61x251_empty_framequeues(cam);
1358 et61x251_queue_unusedframes(cam);
1362 mutex_unlock(&cam->fileop_mutex);
1366 if (list_empty(&cam->outqueue)) {
1367 if (filp->f_flags & O_NONBLOCK) {
1368 mutex_unlock(&cam->fileop_mutex);
1371 timeout = wait_event_interruptible_timeout
1373 (!list_empty(&cam->outqueue)) ||
1374 (cam->state & DEV_DISCONNECTED) ||
1375 (cam->state & DEV_MISCONFIGURED),
1376 cam->module_param.frame_timeout *
1377 1000 * msecs_to_jiffies(1) );
1379 mutex_unlock(&cam->fileop_mutex);
1382 if (cam->state & DEV_DISCONNECTED) {
1383 mutex_unlock(&cam->fileop_mutex);
1386 if (!timeout || (cam->state & DEV_MISCONFIGURED)) {
1387 mutex_unlock(&cam->fileop_mutex);
1392 f = list_entry(cam->outqueue.prev, struct et61x251_frame_t, frame);
1394 if (count > f->buf.bytesused)
1395 count = f->buf.bytesused;
1397 if (copy_to_user(buf, f->bufmem, count)) {
1404 spin_lock_irqsave(&cam->queue_lock, lock_flags);
1405 list_for_each_entry(i, &cam->outqueue, frame)
1406 i->state = F_UNUSED;
1407 INIT_LIST_HEAD(&cam->outqueue);
1408 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1410 et61x251_queue_unusedframes(cam);
1412 PDBGG("Frame #%lu, bytes read: %zu",
1413 (unsigned long)f->buf.index, count);
1415 mutex_unlock(&cam->fileop_mutex);
1417 return err ? err : count;
1421 static unsigned int et61x251_poll(struct file *filp, poll_table *wait)
1423 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp));
1424 struct et61x251_frame_t* f;
1425 unsigned long lock_flags;
1426 unsigned int mask = 0;
1428 if (mutex_lock_interruptible(&cam->fileop_mutex))
1431 if (cam->state & DEV_DISCONNECTED) {
1432 DBG(1, "Device not present");
1436 if (cam->state & DEV_MISCONFIGURED) {
1437 DBG(1, "The camera is misconfigured. Close and open it "
1442 if (cam->io == IO_NONE) {
1443 if (!et61x251_request_buffers(cam, cam->nreadbuffers,
1445 DBG(1, "poll() failed, not enough memory");
1449 cam->stream = STREAM_ON;
1452 if (cam->io == IO_READ) {
1453 spin_lock_irqsave(&cam->queue_lock, lock_flags);
1454 list_for_each_entry(f, &cam->outqueue, frame)
1455 f->state = F_UNUSED;
1456 INIT_LIST_HEAD(&cam->outqueue);
1457 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1458 et61x251_queue_unusedframes(cam);
1461 poll_wait(filp, &cam->wait_frame, wait);
1463 if (!list_empty(&cam->outqueue))
1464 mask |= POLLIN | POLLRDNORM;
1466 mutex_unlock(&cam->fileop_mutex);
1471 mutex_unlock(&cam->fileop_mutex);
1476 static void et61x251_vm_open(struct vm_area_struct* vma)
1478 struct et61x251_frame_t* f = vma->vm_private_data;
1483 static void et61x251_vm_close(struct vm_area_struct* vma)
1485 /* NOTE: buffers are not freed here */
1486 struct et61x251_frame_t* f = vma->vm_private_data;
1491 static struct vm_operations_struct et61x251_vm_ops = {
1492 .open = et61x251_vm_open,
1493 .close = et61x251_vm_close,
1497 static int et61x251_mmap(struct file* filp, struct vm_area_struct *vma)
1499 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp));
1500 unsigned long size = vma->vm_end - vma->vm_start,
1501 start = vma->vm_start;
1505 if (mutex_lock_interruptible(&cam->fileop_mutex))
1506 return -ERESTARTSYS;
1508 if (cam->state & DEV_DISCONNECTED) {
1509 DBG(1, "Device not present");
1510 mutex_unlock(&cam->fileop_mutex);
1514 if (cam->state & DEV_MISCONFIGURED) {
1515 DBG(1, "The camera is misconfigured. Close and open it "
1517 mutex_unlock(&cam->fileop_mutex);
1521 if (!(vma->vm_flags & (VM_WRITE | VM_READ))) {
1522 mutex_unlock(&cam->fileop_mutex);
1526 if (cam->io != IO_MMAP ||
1527 size != PAGE_ALIGN(cam->frame[0].buf.length)) {
1528 mutex_unlock(&cam->fileop_mutex);
1532 for (i = 0; i < cam->nbuffers; i++) {
1533 if ((cam->frame[i].buf.m.offset>>PAGE_SHIFT) == vma->vm_pgoff)
1536 if (i == cam->nbuffers) {
1537 mutex_unlock(&cam->fileop_mutex);
1541 vma->vm_flags |= VM_IO;
1542 vma->vm_flags |= VM_RESERVED;
1544 pos = cam->frame[i].bufmem;
1545 while (size > 0) { /* size is page-aligned */
1546 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
1547 mutex_unlock(&cam->fileop_mutex);
1555 vma->vm_ops = &et61x251_vm_ops;
1556 vma->vm_private_data = &cam->frame[i];
1557 et61x251_vm_open(vma);
1559 mutex_unlock(&cam->fileop_mutex);
1564 /*****************************************************************************/
1567 et61x251_vidioc_querycap(struct et61x251_device* cam, void __user * arg)
1569 struct v4l2_capability cap = {
1570 .driver = "et61x251",
1571 .version = ET61X251_MODULE_VERSION_CODE,
1572 .capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
1576 strlcpy(cap.card, cam->v4ldev->name, sizeof(cap.card));
1577 if (usb_make_path(cam->usbdev, cap.bus_info, sizeof(cap.bus_info)) < 0)
1578 strlcpy(cap.bus_info, cam->usbdev->dev.bus_id,
1579 sizeof(cap.bus_info));
1581 if (copy_to_user(arg, &cap, sizeof(cap)))
1589 et61x251_vidioc_enuminput(struct et61x251_device* cam, void __user * arg)
1591 struct v4l2_input i;
1593 if (copy_from_user(&i, arg, sizeof(i)))
1599 memset(&i, 0, sizeof(i));
1600 strcpy(i.name, "Camera");
1601 i.type = V4L2_INPUT_TYPE_CAMERA;
1603 if (copy_to_user(arg, &i, sizeof(i)))
1611 et61x251_vidioc_g_input(struct et61x251_device* cam, void __user * arg)
1615 if (copy_to_user(arg, &index, sizeof(index)))
1623 et61x251_vidioc_s_input(struct et61x251_device* cam, void __user * arg)
1627 if (copy_from_user(&index, arg, sizeof(index)))
1638 et61x251_vidioc_query_ctrl(struct et61x251_device* cam, void __user * arg)
1640 struct et61x251_sensor* s = &cam->sensor;
1641 struct v4l2_queryctrl qc;
1644 if (copy_from_user(&qc, arg, sizeof(qc)))
1647 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1648 if (qc.id && qc.id == s->qctrl[i].id) {
1649 memcpy(&qc, &(s->qctrl[i]), sizeof(qc));
1650 if (copy_to_user(arg, &qc, sizeof(qc)))
1660 et61x251_vidioc_g_ctrl(struct et61x251_device* cam, void __user * arg)
1662 struct et61x251_sensor* s = &cam->sensor;
1663 struct v4l2_control ctrl;
1667 if (!s->get_ctrl && !s->set_ctrl)
1670 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1674 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1675 if (ctrl.id == s->qctrl[i].id) {
1676 ctrl.value = s->_qctrl[i].default_value;
1681 err = s->get_ctrl(cam, &ctrl);
1684 if (copy_to_user(arg, &ctrl, sizeof(ctrl)))
1692 et61x251_vidioc_s_ctrl(struct et61x251_device* cam, void __user * arg)
1694 struct et61x251_sensor* s = &cam->sensor;
1695 struct v4l2_control ctrl;
1702 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1705 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1706 if (ctrl.id == s->qctrl[i].id) {
1707 if (s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)
1709 if (ctrl.value < s->qctrl[i].minimum ||
1710 ctrl.value > s->qctrl[i].maximum)
1712 ctrl.value -= ctrl.value % s->qctrl[i].step;
1716 if ((err = s->set_ctrl(cam, &ctrl)))
1719 s->_qctrl[i].default_value = ctrl.value;
1726 et61x251_vidioc_cropcap(struct et61x251_device* cam, void __user * arg)
1728 struct v4l2_cropcap* cc = &(cam->sensor.cropcap);
1730 cc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1731 cc->pixelaspect.numerator = 1;
1732 cc->pixelaspect.denominator = 1;
1734 if (copy_to_user(arg, cc, sizeof(*cc)))
1742 et61x251_vidioc_g_crop(struct et61x251_device* cam, void __user * arg)
1744 struct et61x251_sensor* s = &cam->sensor;
1745 struct v4l2_crop crop = {
1746 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
1749 memcpy(&(crop.c), &(s->_rect), sizeof(struct v4l2_rect));
1751 if (copy_to_user(arg, &crop, sizeof(crop)))
1759 et61x251_vidioc_s_crop(struct et61x251_device* cam, void __user * arg)
1761 struct et61x251_sensor* s = &cam->sensor;
1762 struct v4l2_crop crop;
1763 struct v4l2_rect* rect;
1764 struct v4l2_rect* bounds = &(s->cropcap.bounds);
1765 struct v4l2_pix_format* pix_format = &(s->pix_format);
1767 const enum et61x251_stream_state stream = cam->stream;
1768 const u32 nbuffers = cam->nbuffers;
1772 if (copy_from_user(&crop, arg, sizeof(crop)))
1777 if (crop.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1780 if (cam->module_param.force_munmap)
1781 for (i = 0; i < cam->nbuffers; i++)
1782 if (cam->frame[i].vma_use_count) {
1783 DBG(3, "VIDIOC_S_CROP failed. "
1784 "Unmap the buffers first.");
1788 /* Preserve R,G or B origin */
1789 rect->left = (s->_rect.left & 1L) ? rect->left | 1L : rect->left & ~1L;
1790 rect->top = (s->_rect.top & 1L) ? rect->top | 1L : rect->top & ~1L;
1792 if (rect->width < 16)
1794 if (rect->height < 16)
1796 if (rect->width > bounds->width)
1797 rect->width = bounds->width;
1798 if (rect->height > bounds->height)
1799 rect->height = bounds->height;
1800 if (rect->left < bounds->left)
1801 rect->left = bounds->left;
1802 if (rect->top < bounds->top)
1803 rect->top = bounds->top;
1804 if (rect->left + rect->width > bounds->left + bounds->width)
1805 rect->left = bounds->left+bounds->width - rect->width;
1806 if (rect->top + rect->height > bounds->top + bounds->height)
1807 rect->top = bounds->top+bounds->height - rect->height;
1809 rect->width &= ~15L;
1810 rect->height &= ~15L;
1812 if (ET61X251_PRESERVE_IMGSCALE) {
1813 /* Calculate the actual scaling factor */
1815 a = rect->width * rect->height;
1816 b = pix_format->width * pix_format->height;
1817 scale = b ? (u8)((a / b) < 4 ? 1 : 2) : 1;
1821 if (cam->stream == STREAM_ON)
1822 if ((err = et61x251_stream_interrupt(cam)))
1825 if (copy_to_user(arg, &crop, sizeof(crop))) {
1826 cam->stream = stream;
1830 if (cam->module_param.force_munmap || cam->io == IO_READ)
1831 et61x251_release_buffers(cam);
1833 err = et61x251_set_crop(cam, rect);
1835 err += s->set_crop(cam, rect);
1836 err += et61x251_set_scale(cam, scale);
1838 if (err) { /* atomic, no rollback in ioctl() */
1839 cam->state |= DEV_MISCONFIGURED;
1840 DBG(1, "VIDIOC_S_CROP failed because of hardware problems. To "
1841 "use the camera, close and open /dev/video%d again.",
1842 cam->v4ldev->minor);
1846 s->pix_format.width = rect->width/scale;
1847 s->pix_format.height = rect->height/scale;
1848 memcpy(&(s->_rect), rect, sizeof(*rect));
1850 if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
1851 nbuffers != et61x251_request_buffers(cam, nbuffers, cam->io)) {
1852 cam->state |= DEV_MISCONFIGURED;
1853 DBG(1, "VIDIOC_S_CROP failed because of not enough memory. To "
1854 "use the camera, close and open /dev/video%d again.",
1855 cam->v4ldev->minor);
1859 if (cam->io == IO_READ)
1860 et61x251_empty_framequeues(cam);
1861 else if (cam->module_param.force_munmap)
1862 et61x251_requeue_outqueue(cam);
1864 cam->stream = stream;
1871 et61x251_vidioc_enum_framesizes(struct et61x251_device* cam, void __user * arg)
1873 struct v4l2_frmsizeenum frmsize;
1875 if (copy_from_user(&frmsize, arg, sizeof(frmsize)))
1878 if (frmsize.index != 0)
1881 if (frmsize.pixel_format != V4L2_PIX_FMT_ET61X251 &&
1882 frmsize.pixel_format != V4L2_PIX_FMT_SBGGR8)
1885 frmsize.type = V4L2_FRMSIZE_TYPE_STEPWISE;
1886 frmsize.stepwise.min_width = frmsize.stepwise.step_width = 16;
1887 frmsize.stepwise.min_height = frmsize.stepwise.step_height = 16;
1888 frmsize.stepwise.max_width = cam->sensor.cropcap.bounds.width;
1889 frmsize.stepwise.max_height = cam->sensor.cropcap.bounds.height;
1890 memset(&frmsize.reserved, 0, sizeof(frmsize.reserved));
1892 if (copy_to_user(arg, &frmsize, sizeof(frmsize)))
1900 et61x251_vidioc_enum_fmt(struct et61x251_device* cam, void __user * arg)
1902 struct v4l2_fmtdesc fmtd;
1904 if (copy_from_user(&fmtd, arg, sizeof(fmtd)))
1907 if (fmtd.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1910 if (fmtd.index == 0) {
1911 strcpy(fmtd.description, "bayer rgb");
1912 fmtd.pixelformat = V4L2_PIX_FMT_SBGGR8;
1913 } else if (fmtd.index == 1) {
1914 strcpy(fmtd.description, "compressed");
1915 fmtd.pixelformat = V4L2_PIX_FMT_ET61X251;
1916 fmtd.flags = V4L2_FMT_FLAG_COMPRESSED;
1920 fmtd.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1921 memset(&fmtd.reserved, 0, sizeof(fmtd.reserved));
1923 if (copy_to_user(arg, &fmtd, sizeof(fmtd)))
1931 et61x251_vidioc_g_fmt(struct et61x251_device* cam, void __user * arg)
1933 struct v4l2_format format;
1934 struct v4l2_pix_format* pfmt = &(cam->sensor.pix_format);
1936 if (copy_from_user(&format, arg, sizeof(format)))
1939 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1942 pfmt->colorspace = (pfmt->pixelformat == V4L2_PIX_FMT_ET61X251) ?
1943 0 : V4L2_COLORSPACE_SRGB;
1944 pfmt->bytesperline = (pfmt->pixelformat==V4L2_PIX_FMT_ET61X251)
1945 ? 0 : (pfmt->width * pfmt->priv) / 8;
1946 pfmt->sizeimage = pfmt->height * ((pfmt->width*pfmt->priv)/8);
1947 pfmt->field = V4L2_FIELD_NONE;
1948 memcpy(&(format.fmt.pix), pfmt, sizeof(*pfmt));
1950 if (copy_to_user(arg, &format, sizeof(format)))
1958 et61x251_vidioc_try_s_fmt(struct et61x251_device* cam, unsigned int cmd,
1961 struct et61x251_sensor* s = &cam->sensor;
1962 struct v4l2_format format;
1963 struct v4l2_pix_format* pix;
1964 struct v4l2_pix_format* pfmt = &(s->pix_format);
1965 struct v4l2_rect* bounds = &(s->cropcap.bounds);
1966 struct v4l2_rect rect;
1968 const enum et61x251_stream_state stream = cam->stream;
1969 const u32 nbuffers = cam->nbuffers;
1973 if (copy_from_user(&format, arg, sizeof(format)))
1976 pix = &(format.fmt.pix);
1978 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1981 memcpy(&rect, &(s->_rect), sizeof(rect));
1983 { /* calculate the actual scaling factor */
1985 a = rect.width * rect.height;
1986 b = pix->width * pix->height;
1987 scale = b ? (u8)((a / b) < 4 ? 1 : 2) : 1;
1990 rect.width = scale * pix->width;
1991 rect.height = scale * pix->height;
1993 if (rect.width < 16)
1995 if (rect.height < 16)
1997 if (rect.width > bounds->left + bounds->width - rect.left)
1998 rect.width = bounds->left + bounds->width - rect.left;
1999 if (rect.height > bounds->top + bounds->height - rect.top)
2000 rect.height = bounds->top + bounds->height - rect.top;
2003 rect.height &= ~15L;
2005 { /* adjust the scaling factor */
2007 a = rect.width * rect.height;
2008 b = pix->width * pix->height;
2009 scale = b ? (u8)((a / b) < 4 ? 1 : 2) : 1;
2012 pix->width = rect.width / scale;
2013 pix->height = rect.height / scale;
2015 if (pix->pixelformat != V4L2_PIX_FMT_ET61X251 &&
2016 pix->pixelformat != V4L2_PIX_FMT_SBGGR8)
2017 pix->pixelformat = pfmt->pixelformat;
2018 pix->priv = pfmt->priv; /* bpp */
2019 pix->colorspace = (pix->pixelformat == V4L2_PIX_FMT_ET61X251) ?
2020 0 : V4L2_COLORSPACE_SRGB;
2021 pix->colorspace = pfmt->colorspace;
2022 pix->bytesperline = (pix->pixelformat == V4L2_PIX_FMT_ET61X251)
2023 ? 0 : (pix->width * pix->priv) / 8;
2024 pix->sizeimage = pix->height * ((pix->width * pix->priv) / 8);
2025 pix->field = V4L2_FIELD_NONE;
2027 if (cmd == VIDIOC_TRY_FMT) {
2028 if (copy_to_user(arg, &format, sizeof(format)))
2033 if (cam->module_param.force_munmap)
2034 for (i = 0; i < cam->nbuffers; i++)
2035 if (cam->frame[i].vma_use_count) {
2036 DBG(3, "VIDIOC_S_FMT failed. "
2037 "Unmap the buffers first.");
2041 if (cam->stream == STREAM_ON)
2042 if ((err = et61x251_stream_interrupt(cam)))
2045 if (copy_to_user(arg, &format, sizeof(format))) {
2046 cam->stream = stream;
2050 if (cam->module_param.force_munmap || cam->io == IO_READ)
2051 et61x251_release_buffers(cam);
2053 err += et61x251_set_pix_format(cam, pix);
2054 err += et61x251_set_crop(cam, &rect);
2055 if (s->set_pix_format)
2056 err += s->set_pix_format(cam, pix);
2058 err += s->set_crop(cam, &rect);
2059 err += et61x251_set_scale(cam, scale);
2061 if (err) { /* atomic, no rollback in ioctl() */
2062 cam->state |= DEV_MISCONFIGURED;
2063 DBG(1, "VIDIOC_S_FMT failed because of hardware problems. To "
2064 "use the camera, close and open /dev/video%d again.",
2065 cam->v4ldev->minor);
2069 memcpy(pfmt, pix, sizeof(*pix));
2070 memcpy(&(s->_rect), &rect, sizeof(rect));
2072 if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
2073 nbuffers != et61x251_request_buffers(cam, nbuffers, cam->io)) {
2074 cam->state |= DEV_MISCONFIGURED;
2075 DBG(1, "VIDIOC_S_FMT failed because of not enough memory. To "
2076 "use the camera, close and open /dev/video%d again.",
2077 cam->v4ldev->minor);
2081 if (cam->io == IO_READ)
2082 et61x251_empty_framequeues(cam);
2083 else if (cam->module_param.force_munmap)
2084 et61x251_requeue_outqueue(cam);
2086 cam->stream = stream;
2093 et61x251_vidioc_g_jpegcomp(struct et61x251_device* cam, void __user * arg)
2095 if (copy_to_user(arg, &cam->compression,
2096 sizeof(cam->compression)))
2104 et61x251_vidioc_s_jpegcomp(struct et61x251_device* cam, void __user * arg)
2106 struct v4l2_jpegcompression jc;
2107 const enum et61x251_stream_state stream = cam->stream;
2110 if (copy_from_user(&jc, arg, sizeof(jc)))
2113 if (jc.quality != 0 && jc.quality != 1)
2116 if (cam->stream == STREAM_ON)
2117 if ((err = et61x251_stream_interrupt(cam)))
2120 err += et61x251_set_compression(cam, &jc);
2121 if (err) { /* atomic, no rollback in ioctl() */
2122 cam->state |= DEV_MISCONFIGURED;
2123 DBG(1, "VIDIOC_S_JPEGCOMP failed because of hardware "
2124 "problems. To use the camera, close and open "
2125 "/dev/video%d again.", cam->v4ldev->minor);
2129 cam->compression.quality = jc.quality;
2131 cam->stream = stream;
2138 et61x251_vidioc_reqbufs(struct et61x251_device* cam, void __user * arg)
2140 struct v4l2_requestbuffers rb;
2144 if (copy_from_user(&rb, arg, sizeof(rb)))
2147 if (rb.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2148 rb.memory != V4L2_MEMORY_MMAP)
2151 if (cam->io == IO_READ) {
2152 DBG(3, "Close and open the device again to choose the mmap "
2157 for (i = 0; i < cam->nbuffers; i++)
2158 if (cam->frame[i].vma_use_count) {
2159 DBG(3, "VIDIOC_REQBUFS failed. "
2160 "Previous buffers are still mapped.");
2164 if (cam->stream == STREAM_ON)
2165 if ((err = et61x251_stream_interrupt(cam)))
2168 et61x251_empty_framequeues(cam);
2170 et61x251_release_buffers(cam);
2172 rb.count = et61x251_request_buffers(cam, rb.count, IO_MMAP);
2174 if (copy_to_user(arg, &rb, sizeof(rb))) {
2175 et61x251_release_buffers(cam);
2180 cam->io = rb.count ? IO_MMAP : IO_NONE;
2187 et61x251_vidioc_querybuf(struct et61x251_device* cam, void __user * arg)
2189 struct v4l2_buffer b;
2191 if (copy_from_user(&b, arg, sizeof(b)))
2194 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2195 b.index >= cam->nbuffers || cam->io != IO_MMAP)
2198 memcpy(&b, &cam->frame[b.index].buf, sizeof(b));
2200 if (cam->frame[b.index].vma_use_count)
2201 b.flags |= V4L2_BUF_FLAG_MAPPED;
2203 if (cam->frame[b.index].state == F_DONE)
2204 b.flags |= V4L2_BUF_FLAG_DONE;
2205 else if (cam->frame[b.index].state != F_UNUSED)
2206 b.flags |= V4L2_BUF_FLAG_QUEUED;
2208 if (copy_to_user(arg, &b, sizeof(b)))
2216 et61x251_vidioc_qbuf(struct et61x251_device* cam, void __user * arg)
2218 struct v4l2_buffer b;
2219 unsigned long lock_flags;
2221 if (copy_from_user(&b, arg, sizeof(b)))
2224 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2225 b.index >= cam->nbuffers || cam->io != IO_MMAP)
2228 if (cam->frame[b.index].state != F_UNUSED)
2231 cam->frame[b.index].state = F_QUEUED;
2233 spin_lock_irqsave(&cam->queue_lock, lock_flags);
2234 list_add_tail(&cam->frame[b.index].frame, &cam->inqueue);
2235 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2237 PDBGG("Frame #%lu queued", (unsigned long)b.index);
2244 et61x251_vidioc_dqbuf(struct et61x251_device* cam, struct file* filp,
2247 struct v4l2_buffer b;
2248 struct et61x251_frame_t *f;
2249 unsigned long lock_flags;
2252 if (copy_from_user(&b, arg, sizeof(b)))
2255 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io!= IO_MMAP)
2258 if (list_empty(&cam->outqueue)) {
2259 if (cam->stream == STREAM_OFF)
2261 if (filp->f_flags & O_NONBLOCK)
2263 timeout = wait_event_interruptible_timeout
2265 (!list_empty(&cam->outqueue)) ||
2266 (cam->state & DEV_DISCONNECTED) ||
2267 (cam->state & DEV_MISCONFIGURED),
2268 cam->module_param.frame_timeout *
2269 1000 * msecs_to_jiffies(1) );
2272 if (cam->state & DEV_DISCONNECTED)
2274 if (!timeout || (cam->state & DEV_MISCONFIGURED))
2278 spin_lock_irqsave(&cam->queue_lock, lock_flags);
2279 f = list_entry(cam->outqueue.next, struct et61x251_frame_t, frame);
2280 list_del(cam->outqueue.next);
2281 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2283 f->state = F_UNUSED;
2285 memcpy(&b, &f->buf, sizeof(b));
2286 if (f->vma_use_count)
2287 b.flags |= V4L2_BUF_FLAG_MAPPED;
2289 if (copy_to_user(arg, &b, sizeof(b)))
2292 PDBGG("Frame #%lu dequeued", (unsigned long)f->buf.index);
2299 et61x251_vidioc_streamon(struct et61x251_device* cam, void __user * arg)
2303 if (copy_from_user(&type, arg, sizeof(type)))
2306 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2309 cam->stream = STREAM_ON;
2311 DBG(3, "Stream on");
2318 et61x251_vidioc_streamoff(struct et61x251_device* cam, void __user * arg)
2322 if (copy_from_user(&type, arg, sizeof(type)))
2325 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2328 if (cam->stream == STREAM_ON)
2329 if ((err = et61x251_stream_interrupt(cam)))
2332 et61x251_empty_framequeues(cam);
2334 DBG(3, "Stream off");
2341 et61x251_vidioc_g_parm(struct et61x251_device* cam, void __user * arg)
2343 struct v4l2_streamparm sp;
2345 if (copy_from_user(&sp, arg, sizeof(sp)))
2348 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2351 sp.parm.capture.extendedmode = 0;
2352 sp.parm.capture.readbuffers = cam->nreadbuffers;
2354 if (copy_to_user(arg, &sp, sizeof(sp)))
2362 et61x251_vidioc_s_parm(struct et61x251_device* cam, void __user * arg)
2364 struct v4l2_streamparm sp;
2366 if (copy_from_user(&sp, arg, sizeof(sp)))
2369 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2372 sp.parm.capture.extendedmode = 0;
2374 if (sp.parm.capture.readbuffers == 0)
2375 sp.parm.capture.readbuffers = cam->nreadbuffers;
2377 if (sp.parm.capture.readbuffers > ET61X251_MAX_FRAMES)
2378 sp.parm.capture.readbuffers = ET61X251_MAX_FRAMES;
2380 if (copy_to_user(arg, &sp, sizeof(sp)))
2383 cam->nreadbuffers = sp.parm.capture.readbuffers;
2389 static int et61x251_ioctl_v4l2(struct inode* inode, struct file* filp,
2390 unsigned int cmd, void __user * arg)
2392 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp));
2396 case VIDIOC_QUERYCAP:
2397 return et61x251_vidioc_querycap(cam, arg);
2399 case VIDIOC_ENUMINPUT:
2400 return et61x251_vidioc_enuminput(cam, arg);
2402 case VIDIOC_G_INPUT:
2403 return et61x251_vidioc_g_input(cam, arg);
2405 case VIDIOC_S_INPUT:
2406 return et61x251_vidioc_s_input(cam, arg);
2408 case VIDIOC_QUERYCTRL:
2409 return et61x251_vidioc_query_ctrl(cam, arg);
2412 return et61x251_vidioc_g_ctrl(cam, arg);
2415 return et61x251_vidioc_s_ctrl(cam, arg);
2417 case VIDIOC_CROPCAP:
2418 return et61x251_vidioc_cropcap(cam, arg);
2421 return et61x251_vidioc_g_crop(cam, arg);
2424 return et61x251_vidioc_s_crop(cam, arg);
2426 case VIDIOC_ENUM_FMT:
2427 return et61x251_vidioc_enum_fmt(cam, arg);
2430 return et61x251_vidioc_g_fmt(cam, arg);
2432 case VIDIOC_TRY_FMT:
2434 return et61x251_vidioc_try_s_fmt(cam, cmd, arg);
2436 case VIDIOC_ENUM_FRAMESIZES:
2437 return et61x251_vidioc_enum_framesizes(cam, arg);
2439 case VIDIOC_G_JPEGCOMP:
2440 return et61x251_vidioc_g_jpegcomp(cam, arg);
2442 case VIDIOC_S_JPEGCOMP:
2443 return et61x251_vidioc_s_jpegcomp(cam, arg);
2445 case VIDIOC_REQBUFS:
2446 return et61x251_vidioc_reqbufs(cam, arg);
2448 case VIDIOC_QUERYBUF:
2449 return et61x251_vidioc_querybuf(cam, arg);
2452 return et61x251_vidioc_qbuf(cam, arg);
2455 return et61x251_vidioc_dqbuf(cam, filp, arg);
2457 case VIDIOC_STREAMON:
2458 return et61x251_vidioc_streamon(cam, arg);
2460 case VIDIOC_STREAMOFF:
2461 return et61x251_vidioc_streamoff(cam, arg);
2464 return et61x251_vidioc_g_parm(cam, arg);
2467 return et61x251_vidioc_s_parm(cam, arg);
2471 case VIDIOC_QUERYSTD:
2472 case VIDIOC_ENUMSTD:
2473 case VIDIOC_QUERYMENU:
2474 case VIDIOC_ENUM_FRAMEINTERVALS:
2484 static int et61x251_ioctl(struct inode* inode, struct file* filp,
2485 unsigned int cmd, unsigned long arg)
2487 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp));
2490 if (mutex_lock_interruptible(&cam->fileop_mutex))
2491 return -ERESTARTSYS;
2493 if (cam->state & DEV_DISCONNECTED) {
2494 DBG(1, "Device not present");
2495 mutex_unlock(&cam->fileop_mutex);
2499 if (cam->state & DEV_MISCONFIGURED) {
2500 DBG(1, "The camera is misconfigured. Close and open it "
2502 mutex_unlock(&cam->fileop_mutex);
2506 V4LDBG(3, "et61x251", cmd);
2508 err = et61x251_ioctl_v4l2(inode, filp, cmd, (void __user *)arg);
2510 mutex_unlock(&cam->fileop_mutex);
2516 static const struct file_operations et61x251_fops = {
2517 .owner = THIS_MODULE,
2518 .open = et61x251_open,
2519 .release = et61x251_release,
2520 .ioctl = et61x251_ioctl,
2521 .compat_ioctl = v4l_compat_ioctl32,
2522 .read = et61x251_read,
2523 .poll = et61x251_poll,
2524 .mmap = et61x251_mmap,
2525 .llseek = no_llseek,
2528 /*****************************************************************************/
2530 /* It exists a single interface only. We do not need to validate anything. */
2532 et61x251_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
2534 struct usb_device *udev = interface_to_usbdev(intf);
2535 struct et61x251_device* cam;
2536 static unsigned int dev_nr = 0;
2540 if (!(cam = kzalloc(sizeof(struct et61x251_device), GFP_KERNEL)))
2545 if (!(cam->control_buffer = kzalloc(8, GFP_KERNEL))) {
2546 DBG(1, "kmalloc() failed");
2551 if (!(cam->v4ldev = video_device_alloc())) {
2552 DBG(1, "video_device_alloc() failed");
2557 DBG(2, "ET61X[12]51 PC Camera Controller detected "
2558 "(vid/pid 0x%04X:0x%04X)",id->idVendor, id->idProduct);
2560 for (i = 0; et61x251_sensor_table[i]; i++) {
2561 err = et61x251_sensor_table[i](cam);
2567 DBG(2, "%s image sensor detected", cam->sensor.name);
2569 DBG(1, "No supported image sensor detected");
2574 if (et61x251_init(cam)) {
2575 DBG(1, "Initialization failed. I will retry on open().");
2576 cam->state |= DEV_MISCONFIGURED;
2579 strcpy(cam->v4ldev->name, "ET61X[12]51 PC Camera");
2580 cam->v4ldev->owner = THIS_MODULE;
2581 cam->v4ldev->type = VID_TYPE_CAPTURE | VID_TYPE_SCALES;
2582 cam->v4ldev->hardware = 0;
2583 cam->v4ldev->fops = &et61x251_fops;
2584 cam->v4ldev->minor = video_nr[dev_nr];
2585 cam->v4ldev->release = video_device_release;
2586 video_set_drvdata(cam->v4ldev, cam);
2588 init_completion(&cam->probe);
2590 err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER,
2593 DBG(1, "V4L2 device registration failed");
2594 if (err == -ENFILE && video_nr[dev_nr] == -1)
2595 DBG(1, "Free /dev/videoX node not found");
2596 video_nr[dev_nr] = -1;
2597 dev_nr = (dev_nr < ET61X251_MAX_DEVICES-1) ? dev_nr+1 : 0;
2598 complete_all(&cam->probe);
2602 DBG(2, "V4L2 device registered as /dev/video%d", cam->v4ldev->minor);
2604 cam->module_param.force_munmap = force_munmap[dev_nr];
2605 cam->module_param.frame_timeout = frame_timeout[dev_nr];
2607 dev_nr = (dev_nr < ET61X251_MAX_DEVICES-1) ? dev_nr+1 : 0;
2609 #ifdef CONFIG_VIDEO_ADV_DEBUG
2610 err = et61x251_create_sysfs(cam);
2612 DBG(2, "Optional device control through 'sysfs' "
2615 DBG(2, "Failed to create 'sysfs' interface for optional "
2616 "device controlling. Error #%d", err);
2618 DBG(2, "Optional device control through 'sysfs' interface disabled");
2619 DBG(3, "Compile the kernel with the 'CONFIG_VIDEO_ADV_DEBUG' "
2620 "configuration option to enable it.");
2623 usb_set_intfdata(intf, cam);
2624 kref_init(&cam->kref);
2625 usb_get_dev(cam->usbdev);
2627 complete_all(&cam->probe);
2633 kfree(cam->control_buffer);
2635 video_device_release(cam->v4ldev);
2642 static void et61x251_usb_disconnect(struct usb_interface* intf)
2644 struct et61x251_device* cam;
2646 down_write(&et61x251_dev_lock);
2648 cam = usb_get_intfdata(intf);
2650 DBG(2, "Disconnecting %s...", cam->v4ldev->name);
2653 DBG(2, "Device /dev/video%d is open! Deregistration and "
2654 "memory deallocation are deferred.",
2655 cam->v4ldev->minor);
2656 cam->state |= DEV_MISCONFIGURED;
2657 et61x251_stop_transfer(cam);
2658 cam->state |= DEV_DISCONNECTED;
2659 wake_up_interruptible(&cam->wait_frame);
2660 wake_up(&cam->wait_stream);
2662 cam->state |= DEV_DISCONNECTED;
2664 wake_up_interruptible_all(&cam->wait_open);
2666 kref_put(&cam->kref, et61x251_release_resources);
2668 up_write(&et61x251_dev_lock);
2672 static struct usb_driver et61x251_usb_driver = {
2674 .id_table = et61x251_id_table,
2675 .probe = et61x251_usb_probe,
2676 .disconnect = et61x251_usb_disconnect,
2679 /*****************************************************************************/
2681 static int __init et61x251_module_init(void)
2685 KDBG(2, ET61X251_MODULE_NAME " v" ET61X251_MODULE_VERSION);
2686 KDBG(3, ET61X251_MODULE_AUTHOR);
2688 if ((err = usb_register(&et61x251_usb_driver)))
2689 KDBG(1, "usb_register() failed");
2695 static void __exit et61x251_module_exit(void)
2697 usb_deregister(&et61x251_usb_driver);
2701 module_init(et61x251_module_init);
2702 module_exit(et61x251_module_exit);