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 static 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_raw_write(struct et61x251_device* cam, u8 n, u8 data1, u8 data2,
273 u8 data3, u8 data4, u8 data5, u8 data6, u8 data7,
274 u8 data8, u8 address)
276 struct usb_device* udev = cam->usbdev;
277 u8* data = cam->control_buffer;
287 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
288 0, 0x81, data, n-1, ET61X251_CTRL_TIMEOUT);
293 data[1] = cam->sensor.i2c_slave_id;
294 data[2] = cam->sensor.rsta | 0x02 | (n << 4);
295 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
296 0, 0x88, data, 3, ET61X251_CTRL_TIMEOUT);
300 /* Start writing through the serial interface */
302 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
303 0, 0x80, data, 1, ET61X251_CTRL_TIMEOUT);
307 err += et61x251_i2c_wait(cam, &cam->sensor);
310 DBG(3, "I2C raw write failed for %s image sensor",
313 PDBGG("I2C raw write: %u bytes, address = 0x%02X, data1 = 0x%02X, "
314 "data2 = 0x%02X, data3 = 0x%02X, data4 = 0x%02X, data5 = 0x%02X,"
315 " data6 = 0x%02X, data7 = 0x%02X, data8 = 0x%02X", n, address,
316 data1, data2, data3, data4, data5, data6, data7, data8);
323 /*****************************************************************************/
325 static void et61x251_urb_complete(struct urb *urb)
327 struct et61x251_device* cam = urb->context;
328 struct et61x251_frame_t** f;
333 if (urb->status == -ENOENT)
336 f = &cam->frame_current;
338 if (cam->stream == STREAM_INTERRUPT) {
339 cam->stream = STREAM_OFF;
341 (*f)->state = F_QUEUED;
342 DBG(3, "Stream interrupted");
343 wake_up(&cam->wait_stream);
346 if (cam->state & DEV_DISCONNECTED)
349 if (cam->state & DEV_MISCONFIGURED) {
350 wake_up_interruptible(&cam->wait_frame);
354 if (cam->stream == STREAM_OFF || list_empty(&cam->inqueue))
358 (*f) = list_entry(cam->inqueue.next, struct et61x251_frame_t,
361 imagesize = (cam->sensor.pix_format.width *
362 cam->sensor.pix_format.height *
363 cam->sensor.pix_format.priv) / 8;
365 for (i = 0; i < urb->number_of_packets; i++) {
366 unsigned int len, status;
369 const u8 VOID_BYTES = 6;
372 len = urb->iso_frame_desc[i].actual_length;
373 status = urb->iso_frame_desc[i].status;
374 pos = urb->iso_frame_desc[i].offset + urb->transfer_buffer;
377 DBG(3, "Error in isochronous frame");
378 (*f)->state = F_ERROR;
384 sof = ((*b1 & 0x3f) == 63);
385 imglen = ((*b1 & 0xc0) << 2) | *b2;
387 PDBGG("Isochrnous frame: length %u, #%u i, image length %zu",
390 if ((*f)->state == F_QUEUED || (*f)->state == F_ERROR)
393 (*f)->state = F_GRABBING;
394 (*f)->buf.bytesused = 0;
395 do_gettimeofday(&(*f)->buf.timestamp);
397 DBG(3, "SOF detected: new video frame");
400 if ((*f)->state == F_GRABBING) {
401 if (sof && (*f)->buf.bytesused) {
402 if (cam->sensor.pix_format.pixelformat ==
403 V4L2_PIX_FMT_ET61X251)
406 DBG(3, "Not expected SOF detected "
408 (unsigned long)(*f)->buf.bytesused);
409 (*f)->state = F_ERROR;
414 if ((*f)->buf.bytesused + imglen > imagesize) {
415 DBG(3, "Video frame size exceeded");
416 (*f)->state = F_ERROR;
422 memcpy((*f)->bufmem+(*f)->buf.bytesused, pos, imglen);
423 (*f)->buf.bytesused += imglen;
425 if ((*f)->buf.bytesused == imagesize) {
428 b = (*f)->buf.bytesused;
429 (*f)->state = F_DONE;
430 (*f)->buf.sequence= ++cam->frame_count;
431 spin_lock(&cam->queue_lock);
432 list_move_tail(&(*f)->frame, &cam->outqueue);
433 if (!list_empty(&cam->inqueue))
434 (*f) = list_entry(cam->inqueue.next,
435 struct et61x251_frame_t,
439 spin_unlock(&cam->queue_lock);
440 DBG(3, "Video frame captured: : %lu bytes",
447 cam->sensor.pix_format.pixelformat ==
448 V4L2_PIX_FMT_ET61X251)
455 urb->dev = cam->usbdev;
456 err = usb_submit_urb(urb, GFP_ATOMIC);
457 if (err < 0 && err != -EPERM) {
458 cam->state |= DEV_MISCONFIGURED;
459 DBG(1, "usb_submit_urb() failed");
462 wake_up_interruptible(&cam->wait_frame);
466 static int et61x251_start_transfer(struct et61x251_device* cam)
468 struct usb_device *udev = cam->usbdev;
470 struct usb_host_interface* altsetting = usb_altnum_to_altsetting(
471 usb_ifnum_to_if(udev, 0),
472 ET61X251_ALTERNATE_SETTING);
473 const unsigned int psz = le16_to_cpu(altsetting->
474 endpoint[0].desc.wMaxPacketSize);
478 for (i = 0; i < ET61X251_URBS; i++) {
479 cam->transfer_buffer[i] = kzalloc(ET61X251_ISO_PACKETS * psz,
481 if (!cam->transfer_buffer[i]) {
483 DBG(1, "Not enough memory");
488 for (i = 0; i < ET61X251_URBS; i++) {
489 urb = usb_alloc_urb(ET61X251_ISO_PACKETS, GFP_KERNEL);
493 DBG(1, "usb_alloc_urb() failed");
498 urb->pipe = usb_rcvisocpipe(udev, 1);
499 urb->transfer_flags = URB_ISO_ASAP;
500 urb->number_of_packets = ET61X251_ISO_PACKETS;
501 urb->complete = et61x251_urb_complete;
502 urb->transfer_buffer = cam->transfer_buffer[i];
503 urb->transfer_buffer_length = psz * ET61X251_ISO_PACKETS;
505 for (j = 0; j < ET61X251_ISO_PACKETS; j++) {
506 urb->iso_frame_desc[j].offset = psz * j;
507 urb->iso_frame_desc[j].length = psz;
511 err = et61x251_write_reg(cam, 0x01, 0x03);
512 err = et61x251_write_reg(cam, 0x00, 0x03);
513 err = et61x251_write_reg(cam, 0x08, 0x03);
516 DBG(1, "I/O hardware error");
520 err = usb_set_interface(udev, 0, ET61X251_ALTERNATE_SETTING);
522 DBG(1, "usb_set_interface() failed");
526 cam->frame_current = NULL;
528 for (i = 0; i < ET61X251_URBS; i++) {
529 err = usb_submit_urb(cam->urb[i], GFP_KERNEL);
531 for (j = i-1; j >= 0; j--)
532 usb_kill_urb(cam->urb[j]);
533 DBG(1, "usb_submit_urb() failed, error %d", err);
541 for (i = 0; (i < ET61X251_URBS) && cam->urb[i]; i++)
542 usb_free_urb(cam->urb[i]);
545 for (i = 0; (i < ET61X251_URBS) && cam->transfer_buffer[i]; i++)
546 kfree(cam->transfer_buffer[i]);
552 static int et61x251_stop_transfer(struct et61x251_device* cam)
554 struct usb_device *udev = cam->usbdev;
558 if (cam->state & DEV_DISCONNECTED)
561 for (i = ET61X251_URBS-1; i >= 0; i--) {
562 usb_kill_urb(cam->urb[i]);
563 usb_free_urb(cam->urb[i]);
564 kfree(cam->transfer_buffer[i]);
567 err = usb_set_interface(udev, 0, 0); /* 0 Mb/s */
569 DBG(3, "usb_set_interface() failed");
575 static int et61x251_stream_interrupt(struct et61x251_device* cam)
579 cam->stream = STREAM_INTERRUPT;
580 timeout = wait_event_timeout(cam->wait_stream,
581 (cam->stream == STREAM_OFF) ||
582 (cam->state & DEV_DISCONNECTED),
583 ET61X251_URB_TIMEOUT);
584 if (cam->state & DEV_DISCONNECTED)
586 else if (cam->stream != STREAM_OFF) {
587 cam->state |= DEV_MISCONFIGURED;
588 DBG(1, "URB timeout reached. The camera is misconfigured. To "
589 "use it, close and open /dev/video%d again.",
597 /*****************************************************************************/
599 #ifdef CONFIG_VIDEO_ADV_DEBUG
601 static int et61x251_i2c_try_read(struct et61x251_device* cam,
602 const struct et61x251_sensor* sensor,
605 struct usb_device* udev = cam->usbdev;
606 u8* data = cam->control_buffer;
610 data[1] = cam->sensor.i2c_slave_id;
611 data[2] = cam->sensor.rsta | 0x10;
612 data[3] = !(et61x251_read_reg(cam, 0x8b) & 0x02);
613 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
614 0, 0x88, data, 4, ET61X251_CTRL_TIMEOUT);
618 err += et61x251_i2c_wait(cam, sensor);
620 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1,
621 0, 0x80, data, 8, ET61X251_CTRL_TIMEOUT);
626 DBG(3, "I2C read failed for %s image sensor", sensor->name);
628 PDBGG("I2C read: address 0x%02X, value: 0x%02X", address, data[0]);
630 return err ? -1 : (int)data[0];
634 static int et61x251_i2c_try_write(struct et61x251_device* cam,
635 const struct et61x251_sensor* sensor,
636 u8 address, u8 value)
638 struct usb_device* udev = cam->usbdev;
639 u8* data = cam->control_buffer;
643 data[1] = cam->sensor.i2c_slave_id;
644 data[2] = cam->sensor.rsta | 0x12;
645 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
646 0, 0x88, data, 3, ET61X251_CTRL_TIMEOUT);
651 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
652 0, 0x80, data, 1, ET61X251_CTRL_TIMEOUT);
656 err += et61x251_i2c_wait(cam, sensor);
659 DBG(3, "I2C write failed for %s image sensor", sensor->name);
661 PDBGG("I2C write: address 0x%02X, value: 0x%02X", address, value);
666 static int et61x251_i2c_read(struct et61x251_device* cam, u8 address)
668 return et61x251_i2c_try_read(cam, &cam->sensor, address);
671 static int et61x251_i2c_write(struct et61x251_device* cam,
672 u8 address, u8 value)
674 return et61x251_i2c_try_write(cam, &cam->sensor, address, value);
677 static u8 et61x251_strtou8(const char* buff, size_t len, ssize_t* count)
684 strncpy(str, buff, len);
687 strncpy(str, buff, 4);
691 val = simple_strtoul(str, &endp, 0);
695 *count = (ssize_t)(endp - str);
696 if ((*count) && (len == *count+1) && (buff[*count] == '\n'))
703 NOTE 1: being inside one of the following methods implies that the v4l
704 device exists for sure (see kobjects and reference counters)
705 NOTE 2: buffers are PAGE_SIZE long
708 static ssize_t et61x251_show_reg(struct device* cd,
709 struct device_attribute *attr, 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 device* cd,
733 struct device_attribute *attr, const char* buf, size_t len)
735 struct et61x251_device* cam;
739 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
742 cam = video_get_drvdata(to_video_device(cd));
744 mutex_unlock(&et61x251_sysfs_lock);
748 index = et61x251_strtou8(buf, len, &count);
749 if (index > 0x8e || !count) {
750 mutex_unlock(&et61x251_sysfs_lock);
754 cam->sysfs.reg = index;
756 DBG(2, "Moved ET61X[12]51 register index to 0x%02X", cam->sysfs.reg);
757 DBG(3, "Written bytes: %zd", count);
759 mutex_unlock(&et61x251_sysfs_lock);
765 static ssize_t et61x251_show_val(struct device* cd,
766 struct device_attribute *attr, char* buf)
768 struct et61x251_device* cam;
772 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
775 cam = video_get_drvdata(to_video_device(cd));
777 mutex_unlock(&et61x251_sysfs_lock);
781 if ((val = et61x251_read_reg(cam, cam->sysfs.reg)) < 0) {
782 mutex_unlock(&et61x251_sysfs_lock);
786 count = sprintf(buf, "%d\n", val);
788 DBG(3, "Read bytes: %zd", count);
790 mutex_unlock(&et61x251_sysfs_lock);
797 et61x251_store_val(struct device* cd, struct device_attribute *attr,
798 const char* buf, size_t len)
800 struct et61x251_device* cam;
805 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
808 cam = video_get_drvdata(to_video_device(cd));
810 mutex_unlock(&et61x251_sysfs_lock);
814 value = et61x251_strtou8(buf, len, &count);
816 mutex_unlock(&et61x251_sysfs_lock);
820 err = et61x251_write_reg(cam, value, cam->sysfs.reg);
822 mutex_unlock(&et61x251_sysfs_lock);
826 DBG(2, "Written ET61X[12]51 reg. 0x%02X, val. 0x%02X",
827 cam->sysfs.reg, value);
828 DBG(3, "Written bytes: %zd", count);
830 mutex_unlock(&et61x251_sysfs_lock);
836 static ssize_t et61x251_show_i2c_reg(struct device* cd,
837 struct device_attribute *attr, char* buf)
839 struct et61x251_device* cam;
842 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
845 cam = video_get_drvdata(to_video_device(cd));
847 mutex_unlock(&et61x251_sysfs_lock);
851 count = sprintf(buf, "%u\n", cam->sysfs.i2c_reg);
853 DBG(3, "Read bytes: %zd", count);
855 mutex_unlock(&et61x251_sysfs_lock);
862 et61x251_store_i2c_reg(struct device* cd, struct device_attribute *attr,
863 const char* buf, size_t len)
865 struct et61x251_device* cam;
869 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
872 cam = video_get_drvdata(to_video_device(cd));
874 mutex_unlock(&et61x251_sysfs_lock);
878 index = et61x251_strtou8(buf, len, &count);
880 mutex_unlock(&et61x251_sysfs_lock);
884 cam->sysfs.i2c_reg = index;
886 DBG(2, "Moved sensor register index to 0x%02X", cam->sysfs.i2c_reg);
887 DBG(3, "Written bytes: %zd", count);
889 mutex_unlock(&et61x251_sysfs_lock);
895 static ssize_t et61x251_show_i2c_val(struct device* cd,
896 struct device_attribute *attr, char* buf)
898 struct et61x251_device* cam;
902 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
905 cam = video_get_drvdata(to_video_device(cd));
907 mutex_unlock(&et61x251_sysfs_lock);
911 if (!(cam->sensor.sysfs_ops & ET61X251_I2C_READ)) {
912 mutex_unlock(&et61x251_sysfs_lock);
916 if ((val = et61x251_i2c_read(cam, cam->sysfs.i2c_reg)) < 0) {
917 mutex_unlock(&et61x251_sysfs_lock);
921 count = sprintf(buf, "%d\n", val);
923 DBG(3, "Read bytes: %zd", count);
925 mutex_unlock(&et61x251_sysfs_lock);
932 et61x251_store_i2c_val(struct device* cd, struct device_attribute *attr,
933 const char* buf, size_t len)
935 struct et61x251_device* cam;
940 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
943 cam = video_get_drvdata(to_video_device(cd));
945 mutex_unlock(&et61x251_sysfs_lock);
949 if (!(cam->sensor.sysfs_ops & ET61X251_I2C_READ)) {
950 mutex_unlock(&et61x251_sysfs_lock);
954 value = et61x251_strtou8(buf, len, &count);
956 mutex_unlock(&et61x251_sysfs_lock);
960 err = et61x251_i2c_write(cam, cam->sysfs.i2c_reg, value);
962 mutex_unlock(&et61x251_sysfs_lock);
966 DBG(2, "Written sensor reg. 0x%02X, val. 0x%02X",
967 cam->sysfs.i2c_reg, value);
968 DBG(3, "Written bytes: %zd", count);
970 mutex_unlock(&et61x251_sysfs_lock);
976 static DEVICE_ATTR(reg, S_IRUGO | S_IWUSR,
977 et61x251_show_reg, et61x251_store_reg);
978 static DEVICE_ATTR(val, S_IRUGO | S_IWUSR,
979 et61x251_show_val, et61x251_store_val);
980 static DEVICE_ATTR(i2c_reg, S_IRUGO | S_IWUSR,
981 et61x251_show_i2c_reg, et61x251_store_i2c_reg);
982 static DEVICE_ATTR(i2c_val, S_IRUGO | S_IWUSR,
983 et61x251_show_i2c_val, et61x251_store_i2c_val);
986 static int et61x251_create_sysfs(struct et61x251_device* cam)
988 struct device *classdev = &(cam->v4ldev->class_dev);
991 if ((err = device_create_file(classdev, &dev_attr_reg)))
993 if ((err = device_create_file(classdev, &dev_attr_val)))
996 if (cam->sensor.sysfs_ops) {
997 if ((err = device_create_file(classdev, &dev_attr_i2c_reg)))
999 if ((err = device_create_file(classdev, &dev_attr_i2c_val)))
1004 if (cam->sensor.sysfs_ops)
1005 device_remove_file(classdev, &dev_attr_i2c_reg);
1007 device_remove_file(classdev, &dev_attr_val);
1009 device_remove_file(classdev, &dev_attr_reg);
1013 #endif /* CONFIG_VIDEO_ADV_DEBUG */
1015 /*****************************************************************************/
1018 et61x251_set_pix_format(struct et61x251_device* cam,
1019 struct v4l2_pix_format* pix)
1023 if ((r = et61x251_read_reg(cam, 0x12)) < 0)
1025 if (pix->pixelformat == V4L2_PIX_FMT_ET61X251)
1026 err += et61x251_write_reg(cam, r & 0xfd, 0x12);
1028 err += et61x251_write_reg(cam, r | 0x02, 0x12);
1030 return err ? -EIO : 0;
1035 et61x251_set_compression(struct et61x251_device* cam,
1036 struct v4l2_jpegcompression* compression)
1040 if ((r = et61x251_read_reg(cam, 0x12)) < 0)
1042 if (compression->quality == 0)
1043 err += et61x251_write_reg(cam, r & 0xfb, 0x12);
1045 err += et61x251_write_reg(cam, r | 0x04, 0x12);
1047 return err ? -EIO : 0;
1051 static int et61x251_set_scale(struct et61x251_device* cam, u8 scale)
1055 r = et61x251_read_reg(cam, 0x12);
1060 err += et61x251_write_reg(cam, r & ~0x01, 0x12);
1061 else if (scale == 2)
1062 err += et61x251_write_reg(cam, r | 0x01, 0x12);
1067 PDBGG("Scaling factor: %u", scale);
1074 et61x251_set_crop(struct et61x251_device* cam, struct v4l2_rect* rect)
1076 struct et61x251_sensor* s = &cam->sensor;
1077 u16 fmw_sx = (u16)(rect->left - s->cropcap.bounds.left +
1078 s->active_pixel.left),
1079 fmw_sy = (u16)(rect->top - s->cropcap.bounds.top +
1080 s->active_pixel.top),
1081 fmw_length = (u16)(rect->width),
1082 fmw_height = (u16)(rect->height);
1085 err += et61x251_write_reg(cam, fmw_sx & 0xff, 0x69);
1086 err += et61x251_write_reg(cam, fmw_sy & 0xff, 0x6a);
1087 err += et61x251_write_reg(cam, fmw_length & 0xff, 0x6b);
1088 err += et61x251_write_reg(cam, fmw_height & 0xff, 0x6c);
1089 err += et61x251_write_reg(cam, (fmw_sx >> 8) | ((fmw_sy & 0x300) >> 6)
1090 | ((fmw_length & 0x300) >> 4)
1091 | ((fmw_height & 0x300) >> 2), 0x6d);
1095 PDBGG("fmw_sx, fmw_sy, fmw_length, fmw_height: %u %u %u %u",
1096 fmw_sx, fmw_sy, fmw_length, fmw_height);
1102 static int et61x251_init(struct et61x251_device* cam)
1104 struct et61x251_sensor* s = &cam->sensor;
1105 struct v4l2_control ctrl;
1106 struct v4l2_queryctrl *qctrl;
1107 struct v4l2_rect* rect;
1111 if (!(cam->state & DEV_INITIALIZED)) {
1112 mutex_init(&cam->open_mutex);
1113 init_waitqueue_head(&cam->wait_open);
1115 rect = &(s->cropcap.defrect);
1116 cam->compression.quality = ET61X251_COMPRESSION_QUALITY;
1117 } else { /* use current values */
1122 err += et61x251_set_scale(cam, rect->width / s->pix_format.width);
1123 err += et61x251_set_crop(cam, rect);
1130 DBG(3, "Sensor initialization failed");
1135 err += et61x251_set_compression(cam, &cam->compression);
1136 err += et61x251_set_pix_format(cam, &s->pix_format);
1137 if (s->set_pix_format)
1138 err += s->set_pix_format(cam, &s->pix_format);
1142 if (s->pix_format.pixelformat == V4L2_PIX_FMT_ET61X251)
1143 DBG(3, "Compressed video format is active, quality %d",
1144 cam->compression.quality);
1146 DBG(3, "Uncompressed video format is active");
1149 if ((err = s->set_crop(cam, rect))) {
1150 DBG(3, "set_crop() failed");
1155 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1156 if (s->qctrl[i].id != 0 &&
1157 !(s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)) {
1158 ctrl.id = s->qctrl[i].id;
1159 ctrl.value = qctrl[i].default_value;
1160 err = s->set_ctrl(cam, &ctrl);
1162 DBG(3, "Set %s control failed",
1166 DBG(3, "Image sensor supports '%s' control",
1171 if (!(cam->state & DEV_INITIALIZED)) {
1172 mutex_init(&cam->fileop_mutex);
1173 spin_lock_init(&cam->queue_lock);
1174 init_waitqueue_head(&cam->wait_frame);
1175 init_waitqueue_head(&cam->wait_stream);
1176 cam->nreadbuffers = 2;
1177 memcpy(s->_qctrl, s->qctrl, sizeof(s->qctrl));
1178 memcpy(&(s->_rect), &(s->cropcap.defrect),
1179 sizeof(struct v4l2_rect));
1180 cam->state |= DEV_INITIALIZED;
1183 DBG(2, "Initialization succeeded");
1187 /*****************************************************************************/
1189 static void et61x251_release_resources(struct kref *kref)
1191 struct et61x251_device *cam;
1193 mutex_lock(&et61x251_sysfs_lock);
1195 cam = container_of(kref, struct et61x251_device, kref);
1197 DBG(2, "V4L2 device /dev/video%d deregistered", cam->v4ldev->minor);
1198 video_set_drvdata(cam->v4ldev, NULL);
1199 video_unregister_device(cam->v4ldev);
1200 usb_put_dev(cam->usbdev);
1201 kfree(cam->control_buffer);
1204 mutex_unlock(&et61x251_sysfs_lock);
1208 static int et61x251_open(struct inode* inode, struct file* filp)
1210 struct et61x251_device* cam;
1213 if (!down_read_trylock(&et61x251_dev_lock))
1214 return -ERESTARTSYS;
1216 cam = video_get_drvdata(video_devdata(filp));
1218 if (wait_for_completion_interruptible(&cam->probe)) {
1219 up_read(&et61x251_dev_lock);
1220 return -ERESTARTSYS;
1223 kref_get(&cam->kref);
1225 if (mutex_lock_interruptible(&cam->open_mutex)) {
1226 kref_put(&cam->kref, et61x251_release_resources);
1227 up_read(&et61x251_dev_lock);
1228 return -ERESTARTSYS;
1231 if (cam->state & DEV_DISCONNECTED) {
1232 DBG(1, "Device not present");
1238 DBG(2, "Device /dev/video%d is already in use",
1239 cam->v4ldev->minor);
1240 DBG(3, "Simultaneous opens are not supported");
1241 if ((filp->f_flags & O_NONBLOCK) ||
1242 (filp->f_flags & O_NDELAY)) {
1246 DBG(2, "A blocking open() has been requested. Wait for the "
1247 "device to be released...");
1248 up_read(&et61x251_dev_lock);
1249 err = wait_event_interruptible_exclusive(cam->wait_open,
1250 (cam->state & DEV_DISCONNECTED)
1252 down_read(&et61x251_dev_lock);
1255 if (cam->state & DEV_DISCONNECTED) {
1261 if (cam->state & DEV_MISCONFIGURED) {
1262 err = et61x251_init(cam);
1264 DBG(1, "Initialization failed again. "
1265 "I will retry on next open().");
1268 cam->state &= ~DEV_MISCONFIGURED;
1271 if ((err = et61x251_start_transfer(cam)))
1274 filp->private_data = cam;
1277 cam->stream = STREAM_OFF;
1279 cam->frame_count = 0;
1280 et61x251_empty_framequeues(cam);
1282 DBG(3, "Video device /dev/video%d is open", cam->v4ldev->minor);
1285 mutex_unlock(&cam->open_mutex);
1287 kref_put(&cam->kref, et61x251_release_resources);
1288 up_read(&et61x251_dev_lock);
1293 static int et61x251_release(struct inode* inode, struct file* filp)
1295 struct et61x251_device* cam;
1297 down_write(&et61x251_dev_lock);
1299 cam = video_get_drvdata(video_devdata(filp));
1301 et61x251_stop_transfer(cam);
1302 et61x251_release_buffers(cam);
1304 wake_up_interruptible_nr(&cam->wait_open, 1);
1306 DBG(3, "Video device /dev/video%d closed", cam->v4ldev->minor);
1308 kref_put(&cam->kref, et61x251_release_resources);
1310 up_write(&et61x251_dev_lock);
1317 et61x251_read(struct file* filp, char __user * buf,
1318 size_t count, loff_t* f_pos)
1320 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp));
1321 struct et61x251_frame_t* f, * i;
1322 unsigned long lock_flags;
1326 if (mutex_lock_interruptible(&cam->fileop_mutex))
1327 return -ERESTARTSYS;
1329 if (cam->state & DEV_DISCONNECTED) {
1330 DBG(1, "Device not present");
1331 mutex_unlock(&cam->fileop_mutex);
1335 if (cam->state & DEV_MISCONFIGURED) {
1336 DBG(1, "The camera is misconfigured. Close and open it "
1338 mutex_unlock(&cam->fileop_mutex);
1342 if (cam->io == IO_MMAP) {
1343 DBG(3, "Close and open the device again to choose the read "
1345 mutex_unlock(&cam->fileop_mutex);
1349 if (cam->io == IO_NONE) {
1350 if (!et61x251_request_buffers(cam, cam->nreadbuffers,
1352 DBG(1, "read() failed, not enough memory");
1353 mutex_unlock(&cam->fileop_mutex);
1357 cam->stream = STREAM_ON;
1360 if (list_empty(&cam->inqueue)) {
1361 if (!list_empty(&cam->outqueue))
1362 et61x251_empty_framequeues(cam);
1363 et61x251_queue_unusedframes(cam);
1367 mutex_unlock(&cam->fileop_mutex);
1371 if (list_empty(&cam->outqueue)) {
1372 if (filp->f_flags & O_NONBLOCK) {
1373 mutex_unlock(&cam->fileop_mutex);
1376 timeout = wait_event_interruptible_timeout
1378 (!list_empty(&cam->outqueue)) ||
1379 (cam->state & DEV_DISCONNECTED) ||
1380 (cam->state & DEV_MISCONFIGURED),
1381 cam->module_param.frame_timeout *
1382 1000 * msecs_to_jiffies(1) );
1384 mutex_unlock(&cam->fileop_mutex);
1387 if (cam->state & DEV_DISCONNECTED) {
1388 mutex_unlock(&cam->fileop_mutex);
1391 if (!timeout || (cam->state & DEV_MISCONFIGURED)) {
1392 mutex_unlock(&cam->fileop_mutex);
1397 f = list_entry(cam->outqueue.prev, struct et61x251_frame_t, frame);
1399 if (count > f->buf.bytesused)
1400 count = f->buf.bytesused;
1402 if (copy_to_user(buf, f->bufmem, count)) {
1409 spin_lock_irqsave(&cam->queue_lock, lock_flags);
1410 list_for_each_entry(i, &cam->outqueue, frame)
1411 i->state = F_UNUSED;
1412 INIT_LIST_HEAD(&cam->outqueue);
1413 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1415 et61x251_queue_unusedframes(cam);
1417 PDBGG("Frame #%lu, bytes read: %zu",
1418 (unsigned long)f->buf.index, count);
1420 mutex_unlock(&cam->fileop_mutex);
1422 return err ? err : count;
1426 static unsigned int et61x251_poll(struct file *filp, poll_table *wait)
1428 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp));
1429 struct et61x251_frame_t* f;
1430 unsigned long lock_flags;
1431 unsigned int mask = 0;
1433 if (mutex_lock_interruptible(&cam->fileop_mutex))
1436 if (cam->state & DEV_DISCONNECTED) {
1437 DBG(1, "Device not present");
1441 if (cam->state & DEV_MISCONFIGURED) {
1442 DBG(1, "The camera is misconfigured. Close and open it "
1447 if (cam->io == IO_NONE) {
1448 if (!et61x251_request_buffers(cam, cam->nreadbuffers,
1450 DBG(1, "poll() failed, not enough memory");
1454 cam->stream = STREAM_ON;
1457 if (cam->io == IO_READ) {
1458 spin_lock_irqsave(&cam->queue_lock, lock_flags);
1459 list_for_each_entry(f, &cam->outqueue, frame)
1460 f->state = F_UNUSED;
1461 INIT_LIST_HEAD(&cam->outqueue);
1462 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1463 et61x251_queue_unusedframes(cam);
1466 poll_wait(filp, &cam->wait_frame, wait);
1468 if (!list_empty(&cam->outqueue))
1469 mask |= POLLIN | POLLRDNORM;
1471 mutex_unlock(&cam->fileop_mutex);
1476 mutex_unlock(&cam->fileop_mutex);
1481 static void et61x251_vm_open(struct vm_area_struct* vma)
1483 struct et61x251_frame_t* f = vma->vm_private_data;
1488 static void et61x251_vm_close(struct vm_area_struct* vma)
1490 /* NOTE: buffers are not freed here */
1491 struct et61x251_frame_t* f = vma->vm_private_data;
1496 static struct vm_operations_struct et61x251_vm_ops = {
1497 .open = et61x251_vm_open,
1498 .close = et61x251_vm_close,
1502 static int et61x251_mmap(struct file* filp, struct vm_area_struct *vma)
1504 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp));
1505 unsigned long size = vma->vm_end - vma->vm_start,
1506 start = vma->vm_start;
1510 if (mutex_lock_interruptible(&cam->fileop_mutex))
1511 return -ERESTARTSYS;
1513 if (cam->state & DEV_DISCONNECTED) {
1514 DBG(1, "Device not present");
1515 mutex_unlock(&cam->fileop_mutex);
1519 if (cam->state & DEV_MISCONFIGURED) {
1520 DBG(1, "The camera is misconfigured. Close and open it "
1522 mutex_unlock(&cam->fileop_mutex);
1526 if (!(vma->vm_flags & (VM_WRITE | VM_READ))) {
1527 mutex_unlock(&cam->fileop_mutex);
1531 if (cam->io != IO_MMAP ||
1532 size != PAGE_ALIGN(cam->frame[0].buf.length)) {
1533 mutex_unlock(&cam->fileop_mutex);
1537 for (i = 0; i < cam->nbuffers; i++) {
1538 if ((cam->frame[i].buf.m.offset>>PAGE_SHIFT) == vma->vm_pgoff)
1541 if (i == cam->nbuffers) {
1542 mutex_unlock(&cam->fileop_mutex);
1546 vma->vm_flags |= VM_IO;
1547 vma->vm_flags |= VM_RESERVED;
1549 pos = cam->frame[i].bufmem;
1550 while (size > 0) { /* size is page-aligned */
1551 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
1552 mutex_unlock(&cam->fileop_mutex);
1560 vma->vm_ops = &et61x251_vm_ops;
1561 vma->vm_private_data = &cam->frame[i];
1562 et61x251_vm_open(vma);
1564 mutex_unlock(&cam->fileop_mutex);
1569 /*****************************************************************************/
1572 et61x251_vidioc_querycap(struct et61x251_device* cam, void __user * arg)
1574 struct v4l2_capability cap = {
1575 .driver = "et61x251",
1576 .version = ET61X251_MODULE_VERSION_CODE,
1577 .capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
1581 strlcpy(cap.card, cam->v4ldev->name, sizeof(cap.card));
1582 if (usb_make_path(cam->usbdev, cap.bus_info, sizeof(cap.bus_info)) < 0)
1583 strlcpy(cap.bus_info, cam->usbdev->dev.bus_id,
1584 sizeof(cap.bus_info));
1586 if (copy_to_user(arg, &cap, sizeof(cap)))
1594 et61x251_vidioc_enuminput(struct et61x251_device* cam, void __user * arg)
1596 struct v4l2_input i;
1598 if (copy_from_user(&i, arg, sizeof(i)))
1604 memset(&i, 0, sizeof(i));
1605 strcpy(i.name, "Camera");
1606 i.type = V4L2_INPUT_TYPE_CAMERA;
1608 if (copy_to_user(arg, &i, sizeof(i)))
1616 et61x251_vidioc_g_input(struct et61x251_device* cam, void __user * arg)
1620 if (copy_to_user(arg, &index, sizeof(index)))
1628 et61x251_vidioc_s_input(struct et61x251_device* cam, void __user * arg)
1632 if (copy_from_user(&index, arg, sizeof(index)))
1643 et61x251_vidioc_query_ctrl(struct et61x251_device* cam, void __user * arg)
1645 struct et61x251_sensor* s = &cam->sensor;
1646 struct v4l2_queryctrl qc;
1649 if (copy_from_user(&qc, arg, sizeof(qc)))
1652 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1653 if (qc.id && qc.id == s->qctrl[i].id) {
1654 memcpy(&qc, &(s->qctrl[i]), sizeof(qc));
1655 if (copy_to_user(arg, &qc, sizeof(qc)))
1665 et61x251_vidioc_g_ctrl(struct et61x251_device* cam, void __user * arg)
1667 struct et61x251_sensor* s = &cam->sensor;
1668 struct v4l2_control ctrl;
1672 if (!s->get_ctrl && !s->set_ctrl)
1675 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1679 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1680 if (ctrl.id == s->qctrl[i].id) {
1681 ctrl.value = s->_qctrl[i].default_value;
1686 err = s->get_ctrl(cam, &ctrl);
1689 if (copy_to_user(arg, &ctrl, sizeof(ctrl)))
1697 et61x251_vidioc_s_ctrl(struct et61x251_device* cam, void __user * arg)
1699 struct et61x251_sensor* s = &cam->sensor;
1700 struct v4l2_control ctrl;
1707 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1710 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1711 if (ctrl.id == s->qctrl[i].id) {
1712 if (s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)
1714 if (ctrl.value < s->qctrl[i].minimum ||
1715 ctrl.value > s->qctrl[i].maximum)
1717 ctrl.value -= ctrl.value % s->qctrl[i].step;
1721 if ((err = s->set_ctrl(cam, &ctrl)))
1724 s->_qctrl[i].default_value = ctrl.value;
1731 et61x251_vidioc_cropcap(struct et61x251_device* cam, void __user * arg)
1733 struct v4l2_cropcap* cc = &(cam->sensor.cropcap);
1735 cc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1736 cc->pixelaspect.numerator = 1;
1737 cc->pixelaspect.denominator = 1;
1739 if (copy_to_user(arg, cc, sizeof(*cc)))
1747 et61x251_vidioc_g_crop(struct et61x251_device* cam, void __user * arg)
1749 struct et61x251_sensor* s = &cam->sensor;
1750 struct v4l2_crop crop = {
1751 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
1754 memcpy(&(crop.c), &(s->_rect), sizeof(struct v4l2_rect));
1756 if (copy_to_user(arg, &crop, sizeof(crop)))
1764 et61x251_vidioc_s_crop(struct et61x251_device* cam, void __user * arg)
1766 struct et61x251_sensor* s = &cam->sensor;
1767 struct v4l2_crop crop;
1768 struct v4l2_rect* rect;
1769 struct v4l2_rect* bounds = &(s->cropcap.bounds);
1770 struct v4l2_pix_format* pix_format = &(s->pix_format);
1772 const enum et61x251_stream_state stream = cam->stream;
1773 const u32 nbuffers = cam->nbuffers;
1777 if (copy_from_user(&crop, arg, sizeof(crop)))
1782 if (crop.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1785 if (cam->module_param.force_munmap)
1786 for (i = 0; i < cam->nbuffers; i++)
1787 if (cam->frame[i].vma_use_count) {
1788 DBG(3, "VIDIOC_S_CROP failed. "
1789 "Unmap the buffers first.");
1793 /* Preserve R,G or B origin */
1794 rect->left = (s->_rect.left & 1L) ? rect->left | 1L : rect->left & ~1L;
1795 rect->top = (s->_rect.top & 1L) ? rect->top | 1L : rect->top & ~1L;
1797 if (rect->width < 16)
1799 if (rect->height < 16)
1801 if (rect->width > bounds->width)
1802 rect->width = bounds->width;
1803 if (rect->height > bounds->height)
1804 rect->height = bounds->height;
1805 if (rect->left < bounds->left)
1806 rect->left = bounds->left;
1807 if (rect->top < bounds->top)
1808 rect->top = bounds->top;
1809 if (rect->left + rect->width > bounds->left + bounds->width)
1810 rect->left = bounds->left+bounds->width - rect->width;
1811 if (rect->top + rect->height > bounds->top + bounds->height)
1812 rect->top = bounds->top+bounds->height - rect->height;
1814 rect->width &= ~15L;
1815 rect->height &= ~15L;
1817 if (ET61X251_PRESERVE_IMGSCALE) {
1818 /* Calculate the actual scaling factor */
1820 a = rect->width * rect->height;
1821 b = pix_format->width * pix_format->height;
1822 scale = b ? (u8)((a / b) < 4 ? 1 : 2) : 1;
1826 if (cam->stream == STREAM_ON)
1827 if ((err = et61x251_stream_interrupt(cam)))
1830 if (copy_to_user(arg, &crop, sizeof(crop))) {
1831 cam->stream = stream;
1835 if (cam->module_param.force_munmap || cam->io == IO_READ)
1836 et61x251_release_buffers(cam);
1838 err = et61x251_set_crop(cam, rect);
1840 err += s->set_crop(cam, rect);
1841 err += et61x251_set_scale(cam, scale);
1843 if (err) { /* atomic, no rollback in ioctl() */
1844 cam->state |= DEV_MISCONFIGURED;
1845 DBG(1, "VIDIOC_S_CROP failed because of hardware problems. To "
1846 "use the camera, close and open /dev/video%d again.",
1847 cam->v4ldev->minor);
1851 s->pix_format.width = rect->width/scale;
1852 s->pix_format.height = rect->height/scale;
1853 memcpy(&(s->_rect), rect, sizeof(*rect));
1855 if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
1856 nbuffers != et61x251_request_buffers(cam, nbuffers, cam->io)) {
1857 cam->state |= DEV_MISCONFIGURED;
1858 DBG(1, "VIDIOC_S_CROP failed because of not enough memory. To "
1859 "use the camera, close and open /dev/video%d again.",
1860 cam->v4ldev->minor);
1864 if (cam->io == IO_READ)
1865 et61x251_empty_framequeues(cam);
1866 else if (cam->module_param.force_munmap)
1867 et61x251_requeue_outqueue(cam);
1869 cam->stream = stream;
1876 et61x251_vidioc_enum_framesizes(struct et61x251_device* cam, void __user * arg)
1878 struct v4l2_frmsizeenum frmsize;
1880 if (copy_from_user(&frmsize, arg, sizeof(frmsize)))
1883 if (frmsize.index != 0)
1886 if (frmsize.pixel_format != V4L2_PIX_FMT_ET61X251 &&
1887 frmsize.pixel_format != V4L2_PIX_FMT_SBGGR8)
1890 frmsize.type = V4L2_FRMSIZE_TYPE_STEPWISE;
1891 frmsize.stepwise.min_width = frmsize.stepwise.step_width = 16;
1892 frmsize.stepwise.min_height = frmsize.stepwise.step_height = 16;
1893 frmsize.stepwise.max_width = cam->sensor.cropcap.bounds.width;
1894 frmsize.stepwise.max_height = cam->sensor.cropcap.bounds.height;
1895 memset(&frmsize.reserved, 0, sizeof(frmsize.reserved));
1897 if (copy_to_user(arg, &frmsize, sizeof(frmsize)))
1905 et61x251_vidioc_enum_fmt(struct et61x251_device* cam, void __user * arg)
1907 struct v4l2_fmtdesc fmtd;
1909 if (copy_from_user(&fmtd, arg, sizeof(fmtd)))
1912 if (fmtd.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1915 if (fmtd.index == 0) {
1916 strcpy(fmtd.description, "bayer rgb");
1917 fmtd.pixelformat = V4L2_PIX_FMT_SBGGR8;
1918 } else if (fmtd.index == 1) {
1919 strcpy(fmtd.description, "compressed");
1920 fmtd.pixelformat = V4L2_PIX_FMT_ET61X251;
1921 fmtd.flags = V4L2_FMT_FLAG_COMPRESSED;
1925 fmtd.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1926 memset(&fmtd.reserved, 0, sizeof(fmtd.reserved));
1928 if (copy_to_user(arg, &fmtd, sizeof(fmtd)))
1936 et61x251_vidioc_g_fmt(struct et61x251_device* cam, void __user * arg)
1938 struct v4l2_format format;
1939 struct v4l2_pix_format* pfmt = &(cam->sensor.pix_format);
1941 if (copy_from_user(&format, arg, sizeof(format)))
1944 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1947 pfmt->colorspace = (pfmt->pixelformat == V4L2_PIX_FMT_ET61X251) ?
1948 0 : V4L2_COLORSPACE_SRGB;
1949 pfmt->bytesperline = (pfmt->pixelformat==V4L2_PIX_FMT_ET61X251)
1950 ? 0 : (pfmt->width * pfmt->priv) / 8;
1951 pfmt->sizeimage = pfmt->height * ((pfmt->width*pfmt->priv)/8);
1952 pfmt->field = V4L2_FIELD_NONE;
1953 memcpy(&(format.fmt.pix), pfmt, sizeof(*pfmt));
1955 if (copy_to_user(arg, &format, sizeof(format)))
1963 et61x251_vidioc_try_s_fmt(struct et61x251_device* cam, unsigned int cmd,
1966 struct et61x251_sensor* s = &cam->sensor;
1967 struct v4l2_format format;
1968 struct v4l2_pix_format* pix;
1969 struct v4l2_pix_format* pfmt = &(s->pix_format);
1970 struct v4l2_rect* bounds = &(s->cropcap.bounds);
1971 struct v4l2_rect rect;
1973 const enum et61x251_stream_state stream = cam->stream;
1974 const u32 nbuffers = cam->nbuffers;
1978 if (copy_from_user(&format, arg, sizeof(format)))
1981 pix = &(format.fmt.pix);
1983 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1986 memcpy(&rect, &(s->_rect), sizeof(rect));
1988 { /* calculate the actual scaling factor */
1990 a = rect.width * rect.height;
1991 b = pix->width * pix->height;
1992 scale = b ? (u8)((a / b) < 4 ? 1 : 2) : 1;
1995 rect.width = scale * pix->width;
1996 rect.height = scale * pix->height;
1998 if (rect.width < 16)
2000 if (rect.height < 16)
2002 if (rect.width > bounds->left + bounds->width - rect.left)
2003 rect.width = bounds->left + bounds->width - rect.left;
2004 if (rect.height > bounds->top + bounds->height - rect.top)
2005 rect.height = bounds->top + bounds->height - rect.top;
2008 rect.height &= ~15L;
2010 { /* adjust the scaling factor */
2012 a = rect.width * rect.height;
2013 b = pix->width * pix->height;
2014 scale = b ? (u8)((a / b) < 4 ? 1 : 2) : 1;
2017 pix->width = rect.width / scale;
2018 pix->height = rect.height / scale;
2020 if (pix->pixelformat != V4L2_PIX_FMT_ET61X251 &&
2021 pix->pixelformat != V4L2_PIX_FMT_SBGGR8)
2022 pix->pixelformat = pfmt->pixelformat;
2023 pix->priv = pfmt->priv; /* bpp */
2024 pix->colorspace = (pix->pixelformat == V4L2_PIX_FMT_ET61X251) ?
2025 0 : V4L2_COLORSPACE_SRGB;
2026 pix->colorspace = pfmt->colorspace;
2027 pix->bytesperline = (pix->pixelformat == V4L2_PIX_FMT_ET61X251)
2028 ? 0 : (pix->width * pix->priv) / 8;
2029 pix->sizeimage = pix->height * ((pix->width * pix->priv) / 8);
2030 pix->field = V4L2_FIELD_NONE;
2032 if (cmd == VIDIOC_TRY_FMT) {
2033 if (copy_to_user(arg, &format, sizeof(format)))
2038 if (cam->module_param.force_munmap)
2039 for (i = 0; i < cam->nbuffers; i++)
2040 if (cam->frame[i].vma_use_count) {
2041 DBG(3, "VIDIOC_S_FMT failed. "
2042 "Unmap the buffers first.");
2046 if (cam->stream == STREAM_ON)
2047 if ((err = et61x251_stream_interrupt(cam)))
2050 if (copy_to_user(arg, &format, sizeof(format))) {
2051 cam->stream = stream;
2055 if (cam->module_param.force_munmap || cam->io == IO_READ)
2056 et61x251_release_buffers(cam);
2058 err += et61x251_set_pix_format(cam, pix);
2059 err += et61x251_set_crop(cam, &rect);
2060 if (s->set_pix_format)
2061 err += s->set_pix_format(cam, pix);
2063 err += s->set_crop(cam, &rect);
2064 err += et61x251_set_scale(cam, scale);
2066 if (err) { /* atomic, no rollback in ioctl() */
2067 cam->state |= DEV_MISCONFIGURED;
2068 DBG(1, "VIDIOC_S_FMT failed because of hardware problems. To "
2069 "use the camera, close and open /dev/video%d again.",
2070 cam->v4ldev->minor);
2074 memcpy(pfmt, pix, sizeof(*pix));
2075 memcpy(&(s->_rect), &rect, sizeof(rect));
2077 if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
2078 nbuffers != et61x251_request_buffers(cam, nbuffers, cam->io)) {
2079 cam->state |= DEV_MISCONFIGURED;
2080 DBG(1, "VIDIOC_S_FMT failed because of not enough memory. To "
2081 "use the camera, close and open /dev/video%d again.",
2082 cam->v4ldev->minor);
2086 if (cam->io == IO_READ)
2087 et61x251_empty_framequeues(cam);
2088 else if (cam->module_param.force_munmap)
2089 et61x251_requeue_outqueue(cam);
2091 cam->stream = stream;
2098 et61x251_vidioc_g_jpegcomp(struct et61x251_device* cam, void __user * arg)
2100 if (copy_to_user(arg, &cam->compression,
2101 sizeof(cam->compression)))
2109 et61x251_vidioc_s_jpegcomp(struct et61x251_device* cam, void __user * arg)
2111 struct v4l2_jpegcompression jc;
2112 const enum et61x251_stream_state stream = cam->stream;
2115 if (copy_from_user(&jc, arg, sizeof(jc)))
2118 if (jc.quality != 0 && jc.quality != 1)
2121 if (cam->stream == STREAM_ON)
2122 if ((err = et61x251_stream_interrupt(cam)))
2125 err += et61x251_set_compression(cam, &jc);
2126 if (err) { /* atomic, no rollback in ioctl() */
2127 cam->state |= DEV_MISCONFIGURED;
2128 DBG(1, "VIDIOC_S_JPEGCOMP failed because of hardware "
2129 "problems. To use the camera, close and open "
2130 "/dev/video%d again.", cam->v4ldev->minor);
2134 cam->compression.quality = jc.quality;
2136 cam->stream = stream;
2143 et61x251_vidioc_reqbufs(struct et61x251_device* cam, void __user * arg)
2145 struct v4l2_requestbuffers rb;
2149 if (copy_from_user(&rb, arg, sizeof(rb)))
2152 if (rb.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2153 rb.memory != V4L2_MEMORY_MMAP)
2156 if (cam->io == IO_READ) {
2157 DBG(3, "Close and open the device again to choose the mmap "
2162 for (i = 0; i < cam->nbuffers; i++)
2163 if (cam->frame[i].vma_use_count) {
2164 DBG(3, "VIDIOC_REQBUFS failed. "
2165 "Previous buffers are still mapped.");
2169 if (cam->stream == STREAM_ON)
2170 if ((err = et61x251_stream_interrupt(cam)))
2173 et61x251_empty_framequeues(cam);
2175 et61x251_release_buffers(cam);
2177 rb.count = et61x251_request_buffers(cam, rb.count, IO_MMAP);
2179 if (copy_to_user(arg, &rb, sizeof(rb))) {
2180 et61x251_release_buffers(cam);
2185 cam->io = rb.count ? IO_MMAP : IO_NONE;
2192 et61x251_vidioc_querybuf(struct et61x251_device* cam, void __user * arg)
2194 struct v4l2_buffer b;
2196 if (copy_from_user(&b, arg, sizeof(b)))
2199 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2200 b.index >= cam->nbuffers || cam->io != IO_MMAP)
2203 memcpy(&b, &cam->frame[b.index].buf, sizeof(b));
2205 if (cam->frame[b.index].vma_use_count)
2206 b.flags |= V4L2_BUF_FLAG_MAPPED;
2208 if (cam->frame[b.index].state == F_DONE)
2209 b.flags |= V4L2_BUF_FLAG_DONE;
2210 else if (cam->frame[b.index].state != F_UNUSED)
2211 b.flags |= V4L2_BUF_FLAG_QUEUED;
2213 if (copy_to_user(arg, &b, sizeof(b)))
2221 et61x251_vidioc_qbuf(struct et61x251_device* cam, void __user * arg)
2223 struct v4l2_buffer b;
2224 unsigned long lock_flags;
2226 if (copy_from_user(&b, arg, sizeof(b)))
2229 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2230 b.index >= cam->nbuffers || cam->io != IO_MMAP)
2233 if (cam->frame[b.index].state != F_UNUSED)
2236 cam->frame[b.index].state = F_QUEUED;
2238 spin_lock_irqsave(&cam->queue_lock, lock_flags);
2239 list_add_tail(&cam->frame[b.index].frame, &cam->inqueue);
2240 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2242 PDBGG("Frame #%lu queued", (unsigned long)b.index);
2249 et61x251_vidioc_dqbuf(struct et61x251_device* cam, struct file* filp,
2252 struct v4l2_buffer b;
2253 struct et61x251_frame_t *f;
2254 unsigned long lock_flags;
2257 if (copy_from_user(&b, arg, sizeof(b)))
2260 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io!= IO_MMAP)
2263 if (list_empty(&cam->outqueue)) {
2264 if (cam->stream == STREAM_OFF)
2266 if (filp->f_flags & O_NONBLOCK)
2268 timeout = wait_event_interruptible_timeout
2270 (!list_empty(&cam->outqueue)) ||
2271 (cam->state & DEV_DISCONNECTED) ||
2272 (cam->state & DEV_MISCONFIGURED),
2273 cam->module_param.frame_timeout *
2274 1000 * msecs_to_jiffies(1) );
2277 if (cam->state & DEV_DISCONNECTED)
2279 if (!timeout || (cam->state & DEV_MISCONFIGURED))
2283 spin_lock_irqsave(&cam->queue_lock, lock_flags);
2284 f = list_entry(cam->outqueue.next, struct et61x251_frame_t, frame);
2285 list_del(cam->outqueue.next);
2286 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2288 f->state = F_UNUSED;
2290 memcpy(&b, &f->buf, sizeof(b));
2291 if (f->vma_use_count)
2292 b.flags |= V4L2_BUF_FLAG_MAPPED;
2294 if (copy_to_user(arg, &b, sizeof(b)))
2297 PDBGG("Frame #%lu dequeued", (unsigned long)f->buf.index);
2304 et61x251_vidioc_streamon(struct et61x251_device* cam, void __user * arg)
2308 if (copy_from_user(&type, arg, sizeof(type)))
2311 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2314 cam->stream = STREAM_ON;
2316 DBG(3, "Stream on");
2323 et61x251_vidioc_streamoff(struct et61x251_device* cam, void __user * arg)
2327 if (copy_from_user(&type, arg, sizeof(type)))
2330 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2333 if (cam->stream == STREAM_ON)
2334 if ((err = et61x251_stream_interrupt(cam)))
2337 et61x251_empty_framequeues(cam);
2339 DBG(3, "Stream off");
2346 et61x251_vidioc_g_parm(struct et61x251_device* cam, void __user * arg)
2348 struct v4l2_streamparm sp;
2350 if (copy_from_user(&sp, arg, sizeof(sp)))
2353 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2356 sp.parm.capture.extendedmode = 0;
2357 sp.parm.capture.readbuffers = cam->nreadbuffers;
2359 if (copy_to_user(arg, &sp, sizeof(sp)))
2367 et61x251_vidioc_s_parm(struct et61x251_device* cam, void __user * arg)
2369 struct v4l2_streamparm sp;
2371 if (copy_from_user(&sp, arg, sizeof(sp)))
2374 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2377 sp.parm.capture.extendedmode = 0;
2379 if (sp.parm.capture.readbuffers == 0)
2380 sp.parm.capture.readbuffers = cam->nreadbuffers;
2382 if (sp.parm.capture.readbuffers > ET61X251_MAX_FRAMES)
2383 sp.parm.capture.readbuffers = ET61X251_MAX_FRAMES;
2385 if (copy_to_user(arg, &sp, sizeof(sp)))
2388 cam->nreadbuffers = sp.parm.capture.readbuffers;
2394 static int et61x251_ioctl_v4l2(struct inode* inode, struct file* filp,
2395 unsigned int cmd, void __user * arg)
2397 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp));
2401 case VIDIOC_QUERYCAP:
2402 return et61x251_vidioc_querycap(cam, arg);
2404 case VIDIOC_ENUMINPUT:
2405 return et61x251_vidioc_enuminput(cam, arg);
2407 case VIDIOC_G_INPUT:
2408 return et61x251_vidioc_g_input(cam, arg);
2410 case VIDIOC_S_INPUT:
2411 return et61x251_vidioc_s_input(cam, arg);
2413 case VIDIOC_QUERYCTRL:
2414 return et61x251_vidioc_query_ctrl(cam, arg);
2417 return et61x251_vidioc_g_ctrl(cam, arg);
2420 return et61x251_vidioc_s_ctrl(cam, arg);
2422 case VIDIOC_CROPCAP:
2423 return et61x251_vidioc_cropcap(cam, arg);
2426 return et61x251_vidioc_g_crop(cam, arg);
2429 return et61x251_vidioc_s_crop(cam, arg);
2431 case VIDIOC_ENUM_FMT:
2432 return et61x251_vidioc_enum_fmt(cam, arg);
2435 return et61x251_vidioc_g_fmt(cam, arg);
2437 case VIDIOC_TRY_FMT:
2439 return et61x251_vidioc_try_s_fmt(cam, cmd, arg);
2441 case VIDIOC_ENUM_FRAMESIZES:
2442 return et61x251_vidioc_enum_framesizes(cam, arg);
2444 case VIDIOC_G_JPEGCOMP:
2445 return et61x251_vidioc_g_jpegcomp(cam, arg);
2447 case VIDIOC_S_JPEGCOMP:
2448 return et61x251_vidioc_s_jpegcomp(cam, arg);
2450 case VIDIOC_REQBUFS:
2451 return et61x251_vidioc_reqbufs(cam, arg);
2453 case VIDIOC_QUERYBUF:
2454 return et61x251_vidioc_querybuf(cam, arg);
2457 return et61x251_vidioc_qbuf(cam, arg);
2460 return et61x251_vidioc_dqbuf(cam, filp, arg);
2462 case VIDIOC_STREAMON:
2463 return et61x251_vidioc_streamon(cam, arg);
2465 case VIDIOC_STREAMOFF:
2466 return et61x251_vidioc_streamoff(cam, arg);
2469 return et61x251_vidioc_g_parm(cam, arg);
2472 return et61x251_vidioc_s_parm(cam, arg);
2476 case VIDIOC_QUERYSTD:
2477 case VIDIOC_ENUMSTD:
2478 case VIDIOC_QUERYMENU:
2479 case VIDIOC_ENUM_FRAMEINTERVALS:
2489 static int et61x251_ioctl(struct inode* inode, struct file* filp,
2490 unsigned int cmd, unsigned long arg)
2492 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp));
2495 if (mutex_lock_interruptible(&cam->fileop_mutex))
2496 return -ERESTARTSYS;
2498 if (cam->state & DEV_DISCONNECTED) {
2499 DBG(1, "Device not present");
2500 mutex_unlock(&cam->fileop_mutex);
2504 if (cam->state & DEV_MISCONFIGURED) {
2505 DBG(1, "The camera is misconfigured. Close and open it "
2507 mutex_unlock(&cam->fileop_mutex);
2511 V4LDBG(3, "et61x251", cmd);
2513 err = et61x251_ioctl_v4l2(inode, filp, cmd, (void __user *)arg);
2515 mutex_unlock(&cam->fileop_mutex);
2521 static const struct file_operations et61x251_fops = {
2522 .owner = THIS_MODULE,
2523 .open = et61x251_open,
2524 .release = et61x251_release,
2525 .ioctl = et61x251_ioctl,
2526 #ifdef CONFIG_COMPAT
2527 .compat_ioctl = v4l_compat_ioctl32,
2529 .read = et61x251_read,
2530 .poll = et61x251_poll,
2531 .mmap = et61x251_mmap,
2532 .llseek = no_llseek,
2535 /*****************************************************************************/
2537 /* It exists a single interface only. We do not need to validate anything. */
2539 et61x251_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
2541 struct usb_device *udev = interface_to_usbdev(intf);
2542 struct et61x251_device* cam;
2543 static unsigned int dev_nr;
2547 if (!(cam = kzalloc(sizeof(struct et61x251_device), GFP_KERNEL)))
2552 if (!(cam->control_buffer = kzalloc(8, GFP_KERNEL))) {
2553 DBG(1, "kmalloc() failed");
2558 if (!(cam->v4ldev = video_device_alloc())) {
2559 DBG(1, "video_device_alloc() failed");
2564 DBG(2, "ET61X[12]51 PC Camera Controller detected "
2565 "(vid/pid 0x%04X:0x%04X)",id->idVendor, id->idProduct);
2567 for (i = 0; et61x251_sensor_table[i]; i++) {
2568 err = et61x251_sensor_table[i](cam);
2574 DBG(2, "%s image sensor detected", cam->sensor.name);
2576 DBG(1, "No supported image sensor detected");
2581 if (et61x251_init(cam)) {
2582 DBG(1, "Initialization failed. I will retry on open().");
2583 cam->state |= DEV_MISCONFIGURED;
2586 strcpy(cam->v4ldev->name, "ET61X[12]51 PC Camera");
2587 cam->v4ldev->owner = THIS_MODULE;
2588 cam->v4ldev->type = VID_TYPE_CAPTURE | VID_TYPE_SCALES;
2589 cam->v4ldev->fops = &et61x251_fops;
2590 cam->v4ldev->minor = video_nr[dev_nr];
2591 cam->v4ldev->release = video_device_release;
2592 video_set_drvdata(cam->v4ldev, cam);
2594 init_completion(&cam->probe);
2596 err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER,
2599 DBG(1, "V4L2 device registration failed");
2600 if (err == -ENFILE && video_nr[dev_nr] == -1)
2601 DBG(1, "Free /dev/videoX node not found");
2602 video_nr[dev_nr] = -1;
2603 dev_nr = (dev_nr < ET61X251_MAX_DEVICES-1) ? dev_nr+1 : 0;
2604 complete_all(&cam->probe);
2608 DBG(2, "V4L2 device registered as /dev/video%d", cam->v4ldev->minor);
2610 cam->module_param.force_munmap = force_munmap[dev_nr];
2611 cam->module_param.frame_timeout = frame_timeout[dev_nr];
2613 dev_nr = (dev_nr < ET61X251_MAX_DEVICES-1) ? dev_nr+1 : 0;
2615 #ifdef CONFIG_VIDEO_ADV_DEBUG
2616 err = et61x251_create_sysfs(cam);
2618 DBG(2, "Optional device control through 'sysfs' "
2621 DBG(2, "Failed to create 'sysfs' interface for optional "
2622 "device controlling. Error #%d", err);
2624 DBG(2, "Optional device control through 'sysfs' interface disabled");
2625 DBG(3, "Compile the kernel with the 'CONFIG_VIDEO_ADV_DEBUG' "
2626 "configuration option to enable it.");
2629 usb_set_intfdata(intf, cam);
2630 kref_init(&cam->kref);
2631 usb_get_dev(cam->usbdev);
2633 complete_all(&cam->probe);
2639 kfree(cam->control_buffer);
2641 video_device_release(cam->v4ldev);
2648 static void et61x251_usb_disconnect(struct usb_interface* intf)
2650 struct et61x251_device* cam;
2652 down_write(&et61x251_dev_lock);
2654 cam = usb_get_intfdata(intf);
2656 DBG(2, "Disconnecting %s...", cam->v4ldev->name);
2659 DBG(2, "Device /dev/video%d is open! Deregistration and "
2660 "memory deallocation are deferred.",
2661 cam->v4ldev->minor);
2662 cam->state |= DEV_MISCONFIGURED;
2663 et61x251_stop_transfer(cam);
2664 cam->state |= DEV_DISCONNECTED;
2665 wake_up_interruptible(&cam->wait_frame);
2666 wake_up(&cam->wait_stream);
2668 cam->state |= DEV_DISCONNECTED;
2670 wake_up_interruptible_all(&cam->wait_open);
2672 kref_put(&cam->kref, et61x251_release_resources);
2674 up_write(&et61x251_dev_lock);
2678 static struct usb_driver et61x251_usb_driver = {
2680 .id_table = et61x251_id_table,
2681 .probe = et61x251_usb_probe,
2682 .disconnect = et61x251_usb_disconnect,
2685 /*****************************************************************************/
2687 static int __init et61x251_module_init(void)
2691 KDBG(2, ET61X251_MODULE_NAME " v" ET61X251_MODULE_VERSION);
2692 KDBG(3, ET61X251_MODULE_AUTHOR);
2694 if ((err = usb_register(&et61x251_usb_driver)))
2695 KDBG(1, "usb_register() failed");
2701 static void __exit et61x251_module_exit(void)
2703 usb_deregister(&et61x251_usb_driver);
2707 module_init(et61x251_module_init);
2708 module_exit(et61x251_module_exit);