1 /***************************************************************************
2 * V4L2 driver for ET61X[12]51 PC Camera Controllers *
4 * Copyright (C) 2006 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/moduleparam.h>
26 #include <linux/errno.h>
27 #include <linux/slab.h>
28 #include <linux/device.h>
30 #include <linux/delay.h>
31 #include <linux/compiler.h>
32 #include <linux/ioctl.h>
33 #include <linux/poll.h>
34 #include <linux/stat.h>
36 #include <linux/vmalloc.h>
37 #include <linux/page-flags.h>
38 #include <linux/byteorder/generic.h>
40 #include <asm/uaccess.h>
44 /*****************************************************************************/
46 #define ET61X251_MODULE_NAME "V4L2 driver for ET61X[12]51 " \
47 "PC Camera Controllers"
48 #define ET61X251_MODULE_AUTHOR "(C) 2006 Luca Risolia"
49 #define ET61X251_AUTHOR_EMAIL "<luca.risolia@studio.unibo.it>"
50 #define ET61X251_MODULE_LICENSE "GPL"
51 #define ET61X251_MODULE_VERSION "1:1.02"
52 #define ET61X251_MODULE_VERSION_CODE KERNEL_VERSION(1, 0, 2)
54 /*****************************************************************************/
56 MODULE_DEVICE_TABLE(usb, et61x251_id_table);
58 MODULE_AUTHOR(ET61X251_MODULE_AUTHOR " " ET61X251_AUTHOR_EMAIL);
59 MODULE_DESCRIPTION(ET61X251_MODULE_NAME);
60 MODULE_VERSION(ET61X251_MODULE_VERSION);
61 MODULE_LICENSE(ET61X251_MODULE_LICENSE);
63 static short video_nr[] = {[0 ... ET61X251_MAX_DEVICES-1] = -1};
64 module_param_array(video_nr, short, NULL, 0444);
65 MODULE_PARM_DESC(video_nr,
66 "\n<-1|n[,...]> Specify V4L2 minor mode number."
67 "\n -1 = use next available (default)"
68 "\n n = use minor number n (integer >= 0)"
69 "\nYou can specify up to "
70 __MODULE_STRING(ET61X251_MAX_DEVICES) " cameras this way."
72 "\nvideo_nr=-1,2,-1 would assign minor number 2 to"
73 "\nthe second registered camera and use auto for the first"
74 "\none and for every other camera."
77 static short force_munmap[] = {[0 ... ET61X251_MAX_DEVICES-1] =
78 ET61X251_FORCE_MUNMAP};
79 module_param_array(force_munmap, bool, NULL, 0444);
80 MODULE_PARM_DESC(force_munmap,
81 "\n<0|1[,...]> Force the application to unmap previously"
82 "\nmapped buffer memory before calling any VIDIOC_S_CROP or"
83 "\nVIDIOC_S_FMT ioctl's. Not all the applications support"
84 "\nthis feature. This parameter is specific for each"
86 "\n 0 = do not force memory unmapping"
87 "\n 1 = force memory unmapping (save memory)"
88 "\nDefault value is "__MODULE_STRING(SN9C102_FORCE_MUNMAP)"."
91 static unsigned int frame_timeout[] = {[0 ... ET61X251_MAX_DEVICES-1] =
92 ET61X251_FRAME_TIMEOUT};
93 module_param_array(frame_timeout, uint, NULL, 0644);
94 MODULE_PARM_DESC(frame_timeout,
95 "\n<n[,...]> Timeout for a video frame in seconds."
96 "\nThis parameter is specific for each detected camera."
98 __MODULE_STRING(ET61X251_FRAME_TIMEOUT)"."
101 #ifdef ET61X251_DEBUG
102 static unsigned short debug = ET61X251_DEBUG_LEVEL;
103 module_param(debug, ushort, 0644);
104 MODULE_PARM_DESC(debug,
105 "\n<n> Debugging information level, from 0 to 3:"
106 "\n0 = none (use carefully)"
107 "\n1 = critical errors"
108 "\n2 = significant informations"
109 "\n3 = more verbose messages"
110 "\nLevel 3 is useful for testing only, when only "
111 "one device is used."
112 "\nDefault value is "__MODULE_STRING(ET61X251_DEBUG_LEVEL)"."
116 /*****************************************************************************/
119 et61x251_request_buffers(struct et61x251_device* cam, u32 count,
120 enum et61x251_io_method io)
122 struct v4l2_pix_format* p = &(cam->sensor.pix_format);
123 struct v4l2_rect* r = &(cam->sensor.cropcap.bounds);
124 const size_t imagesize = cam->module_param.force_munmap ||
126 (p->width * p->height * p->priv) / 8 :
127 (r->width * r->height * p->priv) / 8;
131 if (count > ET61X251_MAX_FRAMES)
132 count = ET61X251_MAX_FRAMES;
134 cam->nbuffers = count;
135 while (cam->nbuffers > 0) {
136 if ((buff = vmalloc_32(cam->nbuffers * 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, struct et61x251_sensor* sensor)
251 for (i = 1; i <= 8; i++) {
252 if (sensor->interface == ET61X251_I2C_3WIRES) {
253 r = et61x251_read_reg(cam, 0x8e);
254 if (!(r & 0x02) && (r >= 0))
257 r = et61x251_read_reg(cam, 0x8b);
258 if (!(r & 0x01) && (r >= 0))
263 udelay(8*8); /* minimum for sensors at 400kHz */
271 et61x251_i2c_try_read(struct et61x251_device* cam,
272 struct et61x251_sensor* sensor, u8 address)
274 struct usb_device* udev = cam->usbdev;
275 u8* data = cam->control_buffer;
279 data[1] = cam->sensor.i2c_slave_id;
280 data[2] = cam->sensor.rsta | 0x10;
281 data[3] = !(et61x251_read_reg(cam, 0x8b) & 0x02);
282 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
283 0, 0x88, data, 4, ET61X251_CTRL_TIMEOUT);
287 err += et61x251_i2c_wait(cam, sensor);
289 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1,
290 0, 0x80, data, 8, ET61X251_CTRL_TIMEOUT);
295 DBG(3, "I2C read failed for %s image sensor", sensor->name);
297 PDBGG("I2C read: address 0x%02X, value: 0x%02X", address, data[0]);
299 return err ? -1 : (int)data[0];
304 et61x251_i2c_try_write(struct et61x251_device* cam,
305 struct et61x251_sensor* sensor, u8 address, u8 value)
307 struct usb_device* udev = cam->usbdev;
308 u8* data = cam->control_buffer;
312 data[1] = cam->sensor.i2c_slave_id;
313 data[2] = cam->sensor.rsta | 0x12;
314 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
315 0, 0x88, data, 3, ET61X251_CTRL_TIMEOUT);
320 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
321 0, 0x80, data, 1, ET61X251_CTRL_TIMEOUT);
325 err += et61x251_i2c_wait(cam, sensor);
328 DBG(3, "I2C write failed for %s image sensor", sensor->name);
330 PDBGG("I2C write: address 0x%02X, value: 0x%02X", address, value);
337 et61x251_i2c_raw_write(struct et61x251_device* cam, u8 n, u8 data1, u8 data2,
338 u8 data3, u8 data4, u8 data5, u8 data6, u8 data7,
339 u8 data8, u8 address)
341 struct usb_device* udev = cam->usbdev;
342 u8* data = cam->control_buffer;
352 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
353 0, 0x81, data, n-1, ET61X251_CTRL_TIMEOUT);
358 data[1] = cam->sensor.i2c_slave_id;
359 data[2] = cam->sensor.rsta | 0x02 | (n << 4);
360 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
361 0, 0x88, data, 3, ET61X251_CTRL_TIMEOUT);
365 /* Start writing through the serial interface */
367 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
368 0, 0x80, data, 1, ET61X251_CTRL_TIMEOUT);
372 err += et61x251_i2c_wait(cam, &cam->sensor);
375 DBG(3, "I2C raw write failed for %s image sensor",
378 PDBGG("I2C raw write: %u bytes, address = 0x%02X, data1 = 0x%02X, "
379 "data2 = 0x%02X, data3 = 0x%02X, data4 = 0x%02X, data5 = 0x%02X,"
380 " data6 = 0x%02X, data7 = 0x%02X, data8 = 0x%02X", n, address,
381 data1, data2, data3, data4, data5, data6, data7, data8);
388 int et61x251_i2c_read(struct et61x251_device* cam, u8 address)
390 return et61x251_i2c_try_read(cam, &cam->sensor, address);
394 int et61x251_i2c_write(struct et61x251_device* cam, u8 address, u8 value)
396 return et61x251_i2c_try_write(cam, &cam->sensor, address, value);
399 /*****************************************************************************/
401 static void et61x251_urb_complete(struct urb *urb)
403 struct et61x251_device* cam = urb->context;
404 struct et61x251_frame_t** f;
409 if (urb->status == -ENOENT)
412 f = &cam->frame_current;
414 if (cam->stream == STREAM_INTERRUPT) {
415 cam->stream = STREAM_OFF;
417 (*f)->state = F_QUEUED;
418 DBG(3, "Stream interrupted");
419 wake_up(&cam->wait_stream);
422 if (cam->state & DEV_DISCONNECTED)
425 if (cam->state & DEV_MISCONFIGURED) {
426 wake_up_interruptible(&cam->wait_frame);
430 if (cam->stream == STREAM_OFF || list_empty(&cam->inqueue))
434 (*f) = list_entry(cam->inqueue.next, struct et61x251_frame_t,
437 imagesize = (cam->sensor.pix_format.width *
438 cam->sensor.pix_format.height *
439 cam->sensor.pix_format.priv) / 8;
441 for (i = 0; i < urb->number_of_packets; i++) {
442 unsigned int len, status;
445 const u8 VOID_BYTES = 6;
448 len = urb->iso_frame_desc[i].actual_length;
449 status = urb->iso_frame_desc[i].status;
450 pos = urb->iso_frame_desc[i].offset + urb->transfer_buffer;
453 DBG(3, "Error in isochronous frame");
454 (*f)->state = F_ERROR;
460 sof = ((*b1 & 0x3f) == 63);
461 imglen = ((*b1 & 0xc0) << 2) | *b2;
463 PDBGG("Isochrnous frame: length %u, #%u i, image length %zu",
466 if ((*f)->state == F_QUEUED || (*f)->state == F_ERROR)
469 (*f)->state = F_GRABBING;
470 (*f)->buf.bytesused = 0;
471 do_gettimeofday(&(*f)->buf.timestamp);
473 DBG(3, "SOF detected: new video frame");
476 if ((*f)->state == F_GRABBING) {
477 if (sof && (*f)->buf.bytesused) {
478 if (cam->sensor.pix_format.pixelformat ==
479 V4L2_PIX_FMT_ET61X251)
482 DBG(3, "Not expected SOF detected "
484 (unsigned long)(*f)->buf.bytesused);
485 (*f)->state = F_ERROR;
490 if ((*f)->buf.bytesused + imglen > imagesize) {
491 DBG(3, "Video frame size exceeded");
492 (*f)->state = F_ERROR;
498 memcpy((*f)->bufmem+(*f)->buf.bytesused, pos, imglen);
499 (*f)->buf.bytesused += imglen;
501 if ((*f)->buf.bytesused == imagesize) {
504 b = (*f)->buf.bytesused;
505 (*f)->state = F_DONE;
506 (*f)->buf.sequence= ++cam->frame_count;
507 spin_lock(&cam->queue_lock);
508 list_move_tail(&(*f)->frame, &cam->outqueue);
509 if (!list_empty(&cam->inqueue))
510 (*f) = list_entry(cam->inqueue.next,
511 struct et61x251_frame_t,
515 spin_unlock(&cam->queue_lock);
516 DBG(3, "Video frame captured: : %lu bytes",
523 cam->sensor.pix_format.pixelformat ==
524 V4L2_PIX_FMT_ET61X251)
531 urb->dev = cam->usbdev;
532 err = usb_submit_urb(urb, GFP_ATOMIC);
533 if (err < 0 && err != -EPERM) {
534 cam->state |= DEV_MISCONFIGURED;
535 DBG(1, "usb_submit_urb() failed");
538 wake_up_interruptible(&cam->wait_frame);
542 static int et61x251_start_transfer(struct et61x251_device* cam)
544 struct usb_device *udev = cam->usbdev;
546 const unsigned int wMaxPacketSize[] = {0, 256, 384, 512, 640, 768, 832,
547 864, 896, 920, 956, 980, 1000,
549 const unsigned int psz = wMaxPacketSize[ET61X251_ALTERNATE_SETTING];
553 for (i = 0; i < ET61X251_URBS; i++) {
554 cam->transfer_buffer[i] = kzalloc(ET61X251_ISO_PACKETS * psz,
556 if (!cam->transfer_buffer[i]) {
558 DBG(1, "Not enough memory");
563 for (i = 0; i < ET61X251_URBS; i++) {
564 urb = usb_alloc_urb(ET61X251_ISO_PACKETS, GFP_KERNEL);
568 DBG(1, "usb_alloc_urb() failed");
573 urb->pipe = usb_rcvisocpipe(udev, 1);
574 urb->transfer_flags = URB_ISO_ASAP;
575 urb->number_of_packets = ET61X251_ISO_PACKETS;
576 urb->complete = et61x251_urb_complete;
577 urb->transfer_buffer = cam->transfer_buffer[i];
578 urb->transfer_buffer_length = psz * ET61X251_ISO_PACKETS;
580 for (j = 0; j < ET61X251_ISO_PACKETS; j++) {
581 urb->iso_frame_desc[j].offset = psz * j;
582 urb->iso_frame_desc[j].length = psz;
586 err = et61x251_write_reg(cam, 0x01, 0x03);
587 err = et61x251_write_reg(cam, 0x00, 0x03);
588 err = et61x251_write_reg(cam, 0x08, 0x03);
591 DBG(1, "I/O hardware error");
595 err = usb_set_interface(udev, 0, ET61X251_ALTERNATE_SETTING);
597 DBG(1, "usb_set_interface() failed");
601 cam->frame_current = NULL;
603 for (i = 0; i < ET61X251_URBS; i++) {
604 err = usb_submit_urb(cam->urb[i], GFP_KERNEL);
606 for (j = i-1; j >= 0; j--)
607 usb_kill_urb(cam->urb[j]);
608 DBG(1, "usb_submit_urb() failed, error %d", err);
616 for (i = 0; (i < ET61X251_URBS) && cam->urb[i]; i++)
617 usb_free_urb(cam->urb[i]);
620 for (i = 0; (i < ET61X251_URBS) && cam->transfer_buffer[i]; i++)
621 kfree(cam->transfer_buffer[i]);
627 static int et61x251_stop_transfer(struct et61x251_device* cam)
629 struct usb_device *udev = cam->usbdev;
633 if (cam->state & DEV_DISCONNECTED)
636 for (i = ET61X251_URBS-1; i >= 0; i--) {
637 usb_kill_urb(cam->urb[i]);
638 usb_free_urb(cam->urb[i]);
639 kfree(cam->transfer_buffer[i]);
642 err = usb_set_interface(udev, 0, 0); /* 0 Mb/s */
644 DBG(3, "usb_set_interface() failed");
650 static int et61x251_stream_interrupt(struct et61x251_device* cam)
654 cam->stream = STREAM_INTERRUPT;
655 timeout = wait_event_timeout(cam->wait_stream,
656 (cam->stream == STREAM_OFF) ||
657 (cam->state & DEV_DISCONNECTED),
658 ET61X251_URB_TIMEOUT);
659 if (cam->state & DEV_DISCONNECTED)
661 else if (cam->stream != STREAM_OFF) {
662 cam->state |= DEV_MISCONFIGURED;
663 DBG(1, "URB timeout reached. The camera is misconfigured. To "
664 "use it, close and open /dev/video%d again.",
672 /*****************************************************************************/
674 #ifdef CONFIG_VIDEO_ADV_DEBUG
675 static u8 et61x251_strtou8(const char* buff, size_t len, ssize_t* count)
682 strncpy(str, buff, len);
685 strncpy(str, buff, 4);
689 val = simple_strtoul(str, &endp, 0);
693 *count = (ssize_t)(endp - str);
694 if ((*count) && (len == *count+1) && (buff[*count] == '\n'))
701 NOTE 1: being inside one of the following methods implies that the v4l
702 device exists for sure (see kobjects and reference counters)
703 NOTE 2: buffers are PAGE_SIZE long
706 static ssize_t et61x251_show_reg(struct class_device* cd, char* buf)
708 struct et61x251_device* cam;
711 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
714 cam = video_get_drvdata(to_video_device(cd));
716 mutex_unlock(&et61x251_sysfs_lock);
720 count = sprintf(buf, "%u\n", cam->sysfs.reg);
722 mutex_unlock(&et61x251_sysfs_lock);
729 et61x251_store_reg(struct class_device* cd, const char* buf, size_t len)
731 struct et61x251_device* cam;
735 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
738 cam = video_get_drvdata(to_video_device(cd));
740 mutex_unlock(&et61x251_sysfs_lock);
744 index = et61x251_strtou8(buf, len, &count);
745 if (index > 0x8e || !count) {
746 mutex_unlock(&et61x251_sysfs_lock);
750 cam->sysfs.reg = index;
752 DBG(2, "Moved ET61X[12]51 register index to 0x%02X", cam->sysfs.reg);
753 DBG(3, "Written bytes: %zd", count);
755 mutex_unlock(&et61x251_sysfs_lock);
761 static ssize_t et61x251_show_val(struct class_device* cd, char* buf)
763 struct et61x251_device* cam;
767 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
770 cam = video_get_drvdata(to_video_device(cd));
772 mutex_unlock(&et61x251_sysfs_lock);
776 if ((val = et61x251_read_reg(cam, cam->sysfs.reg)) < 0) {
777 mutex_unlock(&et61x251_sysfs_lock);
781 count = sprintf(buf, "%d\n", val);
783 DBG(3, "Read bytes: %zd", count);
785 mutex_unlock(&et61x251_sysfs_lock);
792 et61x251_store_val(struct class_device* cd, const char* buf, size_t len)
794 struct et61x251_device* cam;
799 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
802 cam = video_get_drvdata(to_video_device(cd));
804 mutex_unlock(&et61x251_sysfs_lock);
808 value = et61x251_strtou8(buf, len, &count);
810 mutex_unlock(&et61x251_sysfs_lock);
814 err = et61x251_write_reg(cam, value, cam->sysfs.reg);
816 mutex_unlock(&et61x251_sysfs_lock);
820 DBG(2, "Written ET61X[12]51 reg. 0x%02X, val. 0x%02X",
821 cam->sysfs.reg, value);
822 DBG(3, "Written bytes: %zd", count);
824 mutex_unlock(&et61x251_sysfs_lock);
830 static ssize_t et61x251_show_i2c_reg(struct class_device* cd, char* buf)
832 struct et61x251_device* cam;
835 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
838 cam = video_get_drvdata(to_video_device(cd));
840 mutex_unlock(&et61x251_sysfs_lock);
844 count = sprintf(buf, "%u\n", cam->sysfs.i2c_reg);
846 DBG(3, "Read bytes: %zd", count);
848 mutex_unlock(&et61x251_sysfs_lock);
855 et61x251_store_i2c_reg(struct class_device* cd, const char* buf, size_t len)
857 struct et61x251_device* cam;
861 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
864 cam = video_get_drvdata(to_video_device(cd));
866 mutex_unlock(&et61x251_sysfs_lock);
870 index = et61x251_strtou8(buf, len, &count);
872 mutex_unlock(&et61x251_sysfs_lock);
876 cam->sysfs.i2c_reg = index;
878 DBG(2, "Moved sensor register index to 0x%02X", cam->sysfs.i2c_reg);
879 DBG(3, "Written bytes: %zd", count);
881 mutex_unlock(&et61x251_sysfs_lock);
887 static ssize_t et61x251_show_i2c_val(struct class_device* cd, char* buf)
889 struct et61x251_device* cam;
893 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
896 cam = video_get_drvdata(to_video_device(cd));
898 mutex_unlock(&et61x251_sysfs_lock);
902 if (!(cam->sensor.sysfs_ops & ET61X251_I2C_READ)) {
903 mutex_unlock(&et61x251_sysfs_lock);
907 if ((val = et61x251_i2c_read(cam, cam->sysfs.i2c_reg)) < 0) {
908 mutex_unlock(&et61x251_sysfs_lock);
912 count = sprintf(buf, "%d\n", val);
914 DBG(3, "Read bytes: %zd", count);
916 mutex_unlock(&et61x251_sysfs_lock);
923 et61x251_store_i2c_val(struct class_device* cd, const char* buf, size_t len)
925 struct et61x251_device* cam;
930 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
933 cam = video_get_drvdata(to_video_device(cd));
935 mutex_unlock(&et61x251_sysfs_lock);
939 if (!(cam->sensor.sysfs_ops & ET61X251_I2C_READ)) {
940 mutex_unlock(&et61x251_sysfs_lock);
944 value = et61x251_strtou8(buf, len, &count);
946 mutex_unlock(&et61x251_sysfs_lock);
950 err = et61x251_i2c_write(cam, cam->sysfs.i2c_reg, value);
952 mutex_unlock(&et61x251_sysfs_lock);
956 DBG(2, "Written sensor reg. 0x%02X, val. 0x%02X",
957 cam->sysfs.i2c_reg, value);
958 DBG(3, "Written bytes: %zd", count);
960 mutex_unlock(&et61x251_sysfs_lock);
966 static CLASS_DEVICE_ATTR(reg, S_IRUGO | S_IWUSR,
967 et61x251_show_reg, et61x251_store_reg);
968 static CLASS_DEVICE_ATTR(val, S_IRUGO | S_IWUSR,
969 et61x251_show_val, et61x251_store_val);
970 static CLASS_DEVICE_ATTR(i2c_reg, S_IRUGO | S_IWUSR,
971 et61x251_show_i2c_reg, et61x251_store_i2c_reg);
972 static CLASS_DEVICE_ATTR(i2c_val, S_IRUGO | S_IWUSR,
973 et61x251_show_i2c_val, et61x251_store_i2c_val);
976 static int et61x251_create_sysfs(struct et61x251_device* cam)
978 struct video_device *v4ldev = cam->v4ldev;
981 rc = video_device_create_file(v4ldev, &class_device_attr_reg);
983 rc = video_device_create_file(v4ldev, &class_device_attr_val);
984 if (rc) goto err_reg;
985 if (cam->sensor.sysfs_ops) {
986 rc = video_device_create_file(v4ldev, &class_device_attr_i2c_reg);
987 if (rc) goto err_val;
988 rc = video_device_create_file(v4ldev, &class_device_attr_i2c_val);
989 if (rc) goto err_i2c_reg;
995 video_device_remove_file(v4ldev, &class_device_attr_i2c_reg);
997 video_device_remove_file(v4ldev, &class_device_attr_val);
999 video_device_remove_file(v4ldev, &class_device_attr_reg);
1003 #endif /* CONFIG_VIDEO_ADV_DEBUG */
1005 /*****************************************************************************/
1008 et61x251_set_pix_format(struct et61x251_device* cam,
1009 struct v4l2_pix_format* pix)
1013 if ((r = et61x251_read_reg(cam, 0x12)) < 0)
1015 if (pix->pixelformat == V4L2_PIX_FMT_ET61X251)
1016 err += et61x251_write_reg(cam, r & 0xfd, 0x12);
1018 err += et61x251_write_reg(cam, r | 0x02, 0x12);
1020 return err ? -EIO : 0;
1025 et61x251_set_compression(struct et61x251_device* cam,
1026 struct v4l2_jpegcompression* compression)
1030 if ((r = et61x251_read_reg(cam, 0x12)) < 0)
1032 if (compression->quality == 0)
1033 err += et61x251_write_reg(cam, r & 0xfb, 0x12);
1035 err += et61x251_write_reg(cam, r | 0x04, 0x12);
1037 return err ? -EIO : 0;
1041 static int et61x251_set_scale(struct et61x251_device* cam, u8 scale)
1045 r = et61x251_read_reg(cam, 0x12);
1050 err += et61x251_write_reg(cam, r & ~0x01, 0x12);
1051 else if (scale == 2)
1052 err += et61x251_write_reg(cam, r | 0x01, 0x12);
1057 PDBGG("Scaling factor: %u", scale);
1064 et61x251_set_crop(struct et61x251_device* cam, struct v4l2_rect* rect)
1066 struct et61x251_sensor* s = &cam->sensor;
1067 u16 fmw_sx = (u16)(rect->left - s->cropcap.bounds.left +
1068 s->active_pixel.left),
1069 fmw_sy = (u16)(rect->top - s->cropcap.bounds.top +
1070 s->active_pixel.top),
1071 fmw_length = (u16)(rect->width),
1072 fmw_height = (u16)(rect->height);
1075 err += et61x251_write_reg(cam, fmw_sx & 0xff, 0x69);
1076 err += et61x251_write_reg(cam, fmw_sy & 0xff, 0x6a);
1077 err += et61x251_write_reg(cam, fmw_length & 0xff, 0x6b);
1078 err += et61x251_write_reg(cam, fmw_height & 0xff, 0x6c);
1079 err += et61x251_write_reg(cam, (fmw_sx >> 8) | ((fmw_sy & 0x300) >> 6)
1080 | ((fmw_length & 0x300) >> 4)
1081 | ((fmw_height & 0x300) >> 2), 0x6d);
1085 PDBGG("fmw_sx, fmw_sy, fmw_length, fmw_height: %u %u %u %u",
1086 fmw_sx, fmw_sy, fmw_length, fmw_height);
1092 static int et61x251_init(struct et61x251_device* cam)
1094 struct et61x251_sensor* s = &cam->sensor;
1095 struct v4l2_control ctrl;
1096 struct v4l2_queryctrl *qctrl;
1097 struct v4l2_rect* rect;
1101 if (!(cam->state & DEV_INITIALIZED)) {
1102 init_waitqueue_head(&cam->open);
1104 rect = &(s->cropcap.defrect);
1105 cam->compression.quality = ET61X251_COMPRESSION_QUALITY;
1106 } else { /* use current values */
1111 err += et61x251_set_scale(cam, rect->width / s->pix_format.width);
1112 err += et61x251_set_crop(cam, rect);
1119 DBG(3, "Sensor initialization failed");
1124 err += et61x251_set_compression(cam, &cam->compression);
1125 err += et61x251_set_pix_format(cam, &s->pix_format);
1126 if (s->set_pix_format)
1127 err += s->set_pix_format(cam, &s->pix_format);
1131 if (s->pix_format.pixelformat == V4L2_PIX_FMT_ET61X251)
1132 DBG(3, "Compressed video format is active, quality %d",
1133 cam->compression.quality);
1135 DBG(3, "Uncompressed video format is active");
1138 if ((err = s->set_crop(cam, rect))) {
1139 DBG(3, "set_crop() failed");
1144 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1145 if (s->qctrl[i].id != 0 &&
1146 !(s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)) {
1147 ctrl.id = s->qctrl[i].id;
1148 ctrl.value = qctrl[i].default_value;
1149 err = s->set_ctrl(cam, &ctrl);
1151 DBG(3, "Set %s control failed",
1155 DBG(3, "Image sensor supports '%s' control",
1160 if (!(cam->state & DEV_INITIALIZED)) {
1161 mutex_init(&cam->fileop_mutex);
1162 spin_lock_init(&cam->queue_lock);
1163 init_waitqueue_head(&cam->wait_frame);
1164 init_waitqueue_head(&cam->wait_stream);
1165 cam->nreadbuffers = 2;
1166 memcpy(s->_qctrl, s->qctrl, sizeof(s->qctrl));
1167 memcpy(&(s->_rect), &(s->cropcap.defrect),
1168 sizeof(struct v4l2_rect));
1169 cam->state |= DEV_INITIALIZED;
1172 DBG(2, "Initialization succeeded");
1177 static void et61x251_release_resources(struct et61x251_device* cam)
1179 mutex_lock(&et61x251_sysfs_lock);
1181 DBG(2, "V4L2 device /dev/video%d deregistered", cam->v4ldev->minor);
1182 video_set_drvdata(cam->v4ldev, NULL);
1183 video_unregister_device(cam->v4ldev);
1185 usb_put_dev(cam->usbdev);
1187 mutex_unlock(&et61x251_sysfs_lock);
1189 kfree(cam->control_buffer);
1192 /*****************************************************************************/
1194 static int et61x251_open(struct inode* inode, struct file* filp)
1196 struct et61x251_device* cam;
1200 This is the only safe way to prevent race conditions with
1203 if (!down_read_trylock(&et61x251_disconnect))
1204 return -ERESTARTSYS;
1206 cam = video_get_drvdata(video_devdata(filp));
1208 if (mutex_lock_interruptible(&cam->dev_mutex)) {
1209 up_read(&et61x251_disconnect);
1210 return -ERESTARTSYS;
1214 DBG(2, "Device /dev/video%d is busy...", cam->v4ldev->minor);
1215 if ((filp->f_flags & O_NONBLOCK) ||
1216 (filp->f_flags & O_NDELAY)) {
1220 mutex_unlock(&cam->dev_mutex);
1221 err = wait_event_interruptible_exclusive(cam->open,
1222 cam->state & DEV_DISCONNECTED
1225 up_read(&et61x251_disconnect);
1228 if (cam->state & DEV_DISCONNECTED) {
1229 up_read(&et61x251_disconnect);
1232 mutex_lock(&cam->dev_mutex);
1236 if (cam->state & DEV_MISCONFIGURED) {
1237 err = et61x251_init(cam);
1239 DBG(1, "Initialization failed again. "
1240 "I will retry on next open().");
1243 cam->state &= ~DEV_MISCONFIGURED;
1246 if ((err = et61x251_start_transfer(cam)))
1249 filp->private_data = cam;
1252 cam->stream = STREAM_OFF;
1254 cam->frame_count = 0;
1255 et61x251_empty_framequeues(cam);
1257 DBG(3, "Video device /dev/video%d is open", cam->v4ldev->minor);
1260 mutex_unlock(&cam->dev_mutex);
1261 up_read(&et61x251_disconnect);
1266 static int et61x251_release(struct inode* inode, struct file* filp)
1268 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp));
1270 mutex_lock(&cam->dev_mutex); /* prevent disconnect() to be called */
1272 et61x251_stop_transfer(cam);
1274 et61x251_release_buffers(cam);
1276 if (cam->state & DEV_DISCONNECTED) {
1277 et61x251_release_resources(cam);
1278 mutex_unlock(&cam->dev_mutex);
1284 wake_up_interruptible_nr(&cam->open, 1);
1286 DBG(3, "Video device /dev/video%d closed", cam->v4ldev->minor);
1288 mutex_unlock(&cam->dev_mutex);
1295 et61x251_read(struct file* filp, char __user * buf,
1296 size_t count, loff_t* f_pos)
1298 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp));
1299 struct et61x251_frame_t* f, * i;
1300 unsigned long lock_flags;
1304 if (mutex_lock_interruptible(&cam->fileop_mutex))
1305 return -ERESTARTSYS;
1307 if (cam->state & DEV_DISCONNECTED) {
1308 DBG(1, "Device not present");
1309 mutex_unlock(&cam->fileop_mutex);
1313 if (cam->state & DEV_MISCONFIGURED) {
1314 DBG(1, "The camera is misconfigured. Close and open it "
1316 mutex_unlock(&cam->fileop_mutex);
1320 if (cam->io == IO_MMAP) {
1321 DBG(3, "Close and open the device again to choose the read "
1323 mutex_unlock(&cam->fileop_mutex);
1327 if (cam->io == IO_NONE) {
1328 if (!et61x251_request_buffers(cam, cam->nreadbuffers,
1330 DBG(1, "read() failed, not enough memory");
1331 mutex_unlock(&cam->fileop_mutex);
1335 cam->stream = STREAM_ON;
1338 if (list_empty(&cam->inqueue)) {
1339 if (!list_empty(&cam->outqueue))
1340 et61x251_empty_framequeues(cam);
1341 et61x251_queue_unusedframes(cam);
1345 mutex_unlock(&cam->fileop_mutex);
1349 if (list_empty(&cam->outqueue)) {
1350 if (filp->f_flags & O_NONBLOCK) {
1351 mutex_unlock(&cam->fileop_mutex);
1354 timeout = wait_event_interruptible_timeout
1356 (!list_empty(&cam->outqueue)) ||
1357 (cam->state & DEV_DISCONNECTED) ||
1358 (cam->state & DEV_MISCONFIGURED),
1359 cam->module_param.frame_timeout *
1360 1000 * msecs_to_jiffies(1) );
1362 mutex_unlock(&cam->fileop_mutex);
1365 if (cam->state & DEV_DISCONNECTED) {
1366 mutex_unlock(&cam->fileop_mutex);
1369 if (!timeout || (cam->state & DEV_MISCONFIGURED)) {
1370 mutex_unlock(&cam->fileop_mutex);
1375 f = list_entry(cam->outqueue.prev, struct et61x251_frame_t, frame);
1377 if (count > f->buf.bytesused)
1378 count = f->buf.bytesused;
1380 if (copy_to_user(buf, f->bufmem, count)) {
1387 spin_lock_irqsave(&cam->queue_lock, lock_flags);
1388 list_for_each_entry(i, &cam->outqueue, frame)
1389 i->state = F_UNUSED;
1390 INIT_LIST_HEAD(&cam->outqueue);
1391 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1393 et61x251_queue_unusedframes(cam);
1395 PDBGG("Frame #%lu, bytes read: %zu",
1396 (unsigned long)f->buf.index, count);
1398 mutex_unlock(&cam->fileop_mutex);
1400 return err ? err : count;
1404 static unsigned int et61x251_poll(struct file *filp, poll_table *wait)
1406 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp));
1407 struct et61x251_frame_t* f;
1408 unsigned long lock_flags;
1409 unsigned int mask = 0;
1411 if (mutex_lock_interruptible(&cam->fileop_mutex))
1414 if (cam->state & DEV_DISCONNECTED) {
1415 DBG(1, "Device not present");
1419 if (cam->state & DEV_MISCONFIGURED) {
1420 DBG(1, "The camera is misconfigured. Close and open it "
1425 if (cam->io == IO_NONE) {
1426 if (!et61x251_request_buffers(cam, cam->nreadbuffers,
1428 DBG(1, "poll() failed, not enough memory");
1432 cam->stream = STREAM_ON;
1435 if (cam->io == IO_READ) {
1436 spin_lock_irqsave(&cam->queue_lock, lock_flags);
1437 list_for_each_entry(f, &cam->outqueue, frame)
1438 f->state = F_UNUSED;
1439 INIT_LIST_HEAD(&cam->outqueue);
1440 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1441 et61x251_queue_unusedframes(cam);
1444 poll_wait(filp, &cam->wait_frame, wait);
1446 if (!list_empty(&cam->outqueue))
1447 mask |= POLLIN | POLLRDNORM;
1449 mutex_unlock(&cam->fileop_mutex);
1454 mutex_unlock(&cam->fileop_mutex);
1459 static void et61x251_vm_open(struct vm_area_struct* vma)
1461 struct et61x251_frame_t* f = vma->vm_private_data;
1466 static void et61x251_vm_close(struct vm_area_struct* vma)
1468 /* NOTE: buffers are not freed here */
1469 struct et61x251_frame_t* f = vma->vm_private_data;
1474 static struct vm_operations_struct et61x251_vm_ops = {
1475 .open = et61x251_vm_open,
1476 .close = et61x251_vm_close,
1480 static int et61x251_mmap(struct file* filp, struct vm_area_struct *vma)
1482 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp));
1483 unsigned long size = vma->vm_end - vma->vm_start,
1484 start = vma->vm_start;
1488 if (mutex_lock_interruptible(&cam->fileop_mutex))
1489 return -ERESTARTSYS;
1491 if (cam->state & DEV_DISCONNECTED) {
1492 DBG(1, "Device not present");
1493 mutex_unlock(&cam->fileop_mutex);
1497 if (cam->state & DEV_MISCONFIGURED) {
1498 DBG(1, "The camera is misconfigured. Close and open it "
1500 mutex_unlock(&cam->fileop_mutex);
1504 if (cam->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) ||
1505 size != PAGE_ALIGN(cam->frame[0].buf.length)) {
1506 mutex_unlock(&cam->fileop_mutex);
1510 for (i = 0; i < cam->nbuffers; i++) {
1511 if ((cam->frame[i].buf.m.offset>>PAGE_SHIFT) == vma->vm_pgoff)
1514 if (i == cam->nbuffers) {
1515 mutex_unlock(&cam->fileop_mutex);
1519 vma->vm_flags |= VM_IO;
1520 vma->vm_flags |= VM_RESERVED;
1522 pos = cam->frame[i].bufmem;
1523 while (size > 0) { /* size is page-aligned */
1524 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
1525 mutex_unlock(&cam->fileop_mutex);
1533 vma->vm_ops = &et61x251_vm_ops;
1534 vma->vm_private_data = &cam->frame[i];
1536 et61x251_vm_open(vma);
1538 mutex_unlock(&cam->fileop_mutex);
1543 /*****************************************************************************/
1546 et61x251_vidioc_querycap(struct et61x251_device* cam, void __user * arg)
1548 struct v4l2_capability cap = {
1549 .driver = "et61x251",
1550 .version = ET61X251_MODULE_VERSION_CODE,
1551 .capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
1555 strlcpy(cap.card, cam->v4ldev->name, sizeof(cap.card));
1556 if (usb_make_path(cam->usbdev, cap.bus_info, sizeof(cap.bus_info)) < 0)
1557 strlcpy(cap.bus_info, cam->usbdev->dev.bus_id,
1558 sizeof(cap.bus_info));
1560 if (copy_to_user(arg, &cap, sizeof(cap)))
1568 et61x251_vidioc_enuminput(struct et61x251_device* cam, void __user * arg)
1570 struct v4l2_input i;
1572 if (copy_from_user(&i, arg, sizeof(i)))
1578 memset(&i, 0, sizeof(i));
1579 strcpy(i.name, "Camera");
1580 i.type = V4L2_INPUT_TYPE_CAMERA;
1582 if (copy_to_user(arg, &i, sizeof(i)))
1590 et61x251_vidioc_g_input(struct et61x251_device* cam, void __user * arg)
1594 if (copy_to_user(arg, &index, sizeof(index)))
1602 et61x251_vidioc_s_input(struct et61x251_device* cam, void __user * arg)
1606 if (copy_from_user(&index, arg, sizeof(index)))
1617 et61x251_vidioc_query_ctrl(struct et61x251_device* cam, void __user * arg)
1619 struct et61x251_sensor* s = &cam->sensor;
1620 struct v4l2_queryctrl qc;
1623 if (copy_from_user(&qc, arg, sizeof(qc)))
1626 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1627 if (qc.id && qc.id == s->qctrl[i].id) {
1628 memcpy(&qc, &(s->qctrl[i]), sizeof(qc));
1629 if (copy_to_user(arg, &qc, sizeof(qc)))
1639 et61x251_vidioc_g_ctrl(struct et61x251_device* cam, void __user * arg)
1641 struct et61x251_sensor* s = &cam->sensor;
1642 struct v4l2_control ctrl;
1646 if (!s->get_ctrl && !s->set_ctrl)
1649 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1653 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1654 if (ctrl.id == s->qctrl[i].id) {
1655 ctrl.value = s->_qctrl[i].default_value;
1660 err = s->get_ctrl(cam, &ctrl);
1663 if (copy_to_user(arg, &ctrl, sizeof(ctrl)))
1671 et61x251_vidioc_s_ctrl(struct et61x251_device* cam, void __user * arg)
1673 struct et61x251_sensor* s = &cam->sensor;
1674 struct v4l2_control ctrl;
1681 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1684 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1685 if (ctrl.id == s->qctrl[i].id) {
1686 if (s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)
1688 if (ctrl.value < s->qctrl[i].minimum ||
1689 ctrl.value > s->qctrl[i].maximum)
1691 ctrl.value -= ctrl.value % s->qctrl[i].step;
1695 if ((err = s->set_ctrl(cam, &ctrl)))
1698 s->_qctrl[i].default_value = ctrl.value;
1705 et61x251_vidioc_cropcap(struct et61x251_device* cam, void __user * arg)
1707 struct v4l2_cropcap* cc = &(cam->sensor.cropcap);
1709 cc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1710 cc->pixelaspect.numerator = 1;
1711 cc->pixelaspect.denominator = 1;
1713 if (copy_to_user(arg, cc, sizeof(*cc)))
1721 et61x251_vidioc_g_crop(struct et61x251_device* cam, void __user * arg)
1723 struct et61x251_sensor* s = &cam->sensor;
1724 struct v4l2_crop crop = {
1725 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
1728 memcpy(&(crop.c), &(s->_rect), sizeof(struct v4l2_rect));
1730 if (copy_to_user(arg, &crop, sizeof(crop)))
1738 et61x251_vidioc_s_crop(struct et61x251_device* cam, void __user * arg)
1740 struct et61x251_sensor* s = &cam->sensor;
1741 struct v4l2_crop crop;
1742 struct v4l2_rect* rect;
1743 struct v4l2_rect* bounds = &(s->cropcap.bounds);
1744 struct v4l2_pix_format* pix_format = &(s->pix_format);
1746 const enum et61x251_stream_state stream = cam->stream;
1747 const u32 nbuffers = cam->nbuffers;
1751 if (copy_from_user(&crop, arg, sizeof(crop)))
1756 if (crop.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1759 if (cam->module_param.force_munmap)
1760 for (i = 0; i < cam->nbuffers; i++)
1761 if (cam->frame[i].vma_use_count) {
1762 DBG(3, "VIDIOC_S_CROP failed. "
1763 "Unmap the buffers first.");
1767 /* Preserve R,G or B origin */
1768 rect->left = (s->_rect.left & 1L) ? rect->left | 1L : rect->left & ~1L;
1769 rect->top = (s->_rect.top & 1L) ? rect->top | 1L : rect->top & ~1L;
1771 if (rect->width < 4)
1773 if (rect->height < 4)
1775 if (rect->width > bounds->width)
1776 rect->width = bounds->width;
1777 if (rect->height > bounds->height)
1778 rect->height = bounds->height;
1779 if (rect->left < bounds->left)
1780 rect->left = bounds->left;
1781 if (rect->top < bounds->top)
1782 rect->top = bounds->top;
1783 if (rect->left + rect->width > bounds->left + bounds->width)
1784 rect->left = bounds->left+bounds->width - rect->width;
1785 if (rect->top + rect->height > bounds->top + bounds->height)
1786 rect->top = bounds->top+bounds->height - rect->height;
1789 rect->height &= ~3L;
1791 if (ET61X251_PRESERVE_IMGSCALE) {
1792 /* Calculate the actual scaling factor */
1794 a = rect->width * rect->height;
1795 b = pix_format->width * pix_format->height;
1796 scale = b ? (u8)((a / b) < 4 ? 1 : 2) : 1;
1800 if (cam->stream == STREAM_ON)
1801 if ((err = et61x251_stream_interrupt(cam)))
1804 if (copy_to_user(arg, &crop, sizeof(crop))) {
1805 cam->stream = stream;
1809 if (cam->module_param.force_munmap || cam->io == IO_READ)
1810 et61x251_release_buffers(cam);
1812 err = et61x251_set_crop(cam, rect);
1814 err += s->set_crop(cam, rect);
1815 err += et61x251_set_scale(cam, scale);
1817 if (err) { /* atomic, no rollback in ioctl() */
1818 cam->state |= DEV_MISCONFIGURED;
1819 DBG(1, "VIDIOC_S_CROP failed because of hardware problems. To "
1820 "use the camera, close and open /dev/video%d again.",
1821 cam->v4ldev->minor);
1825 s->pix_format.width = rect->width/scale;
1826 s->pix_format.height = rect->height/scale;
1827 memcpy(&(s->_rect), rect, sizeof(*rect));
1829 if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
1830 nbuffers != et61x251_request_buffers(cam, nbuffers, cam->io)) {
1831 cam->state |= DEV_MISCONFIGURED;
1832 DBG(1, "VIDIOC_S_CROP failed because of not enough memory. To "
1833 "use the camera, close and open /dev/video%d again.",
1834 cam->v4ldev->minor);
1838 if (cam->io == IO_READ)
1839 et61x251_empty_framequeues(cam);
1840 else if (cam->module_param.force_munmap)
1841 et61x251_requeue_outqueue(cam);
1843 cam->stream = stream;
1850 et61x251_vidioc_enum_fmt(struct et61x251_device* cam, void __user * arg)
1852 struct v4l2_fmtdesc fmtd;
1854 if (copy_from_user(&fmtd, arg, sizeof(fmtd)))
1857 if (fmtd.index == 0) {
1858 strcpy(fmtd.description, "bayer rgb");
1859 fmtd.pixelformat = V4L2_PIX_FMT_SBGGR8;
1860 } else if (fmtd.index == 1) {
1861 strcpy(fmtd.description, "compressed");
1862 fmtd.pixelformat = V4L2_PIX_FMT_ET61X251;
1863 fmtd.flags = V4L2_FMT_FLAG_COMPRESSED;
1867 fmtd.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1868 memset(&fmtd.reserved, 0, sizeof(fmtd.reserved));
1870 if (copy_to_user(arg, &fmtd, sizeof(fmtd)))
1878 et61x251_vidioc_g_fmt(struct et61x251_device* cam, void __user * arg)
1880 struct v4l2_format format;
1881 struct v4l2_pix_format* pfmt = &(cam->sensor.pix_format);
1883 if (copy_from_user(&format, arg, sizeof(format)))
1886 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1889 pfmt->bytesperline = (pfmt->pixelformat==V4L2_PIX_FMT_ET61X251)
1890 ? 0 : (pfmt->width * pfmt->priv) / 8;
1891 pfmt->sizeimage = pfmt->height * ((pfmt->width*pfmt->priv)/8);
1892 pfmt->field = V4L2_FIELD_NONE;
1893 memcpy(&(format.fmt.pix), pfmt, sizeof(*pfmt));
1895 if (copy_to_user(arg, &format, sizeof(format)))
1903 et61x251_vidioc_try_s_fmt(struct et61x251_device* cam, unsigned int cmd,
1906 struct et61x251_sensor* s = &cam->sensor;
1907 struct v4l2_format format;
1908 struct v4l2_pix_format* pix;
1909 struct v4l2_pix_format* pfmt = &(s->pix_format);
1910 struct v4l2_rect* bounds = &(s->cropcap.bounds);
1911 struct v4l2_rect rect;
1913 const enum et61x251_stream_state stream = cam->stream;
1914 const u32 nbuffers = cam->nbuffers;
1918 if (copy_from_user(&format, arg, sizeof(format)))
1921 pix = &(format.fmt.pix);
1923 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1926 memcpy(&rect, &(s->_rect), sizeof(rect));
1928 { /* calculate the actual scaling factor */
1930 a = rect.width * rect.height;
1931 b = pix->width * pix->height;
1932 scale = b ? (u8)((a / b) < 4 ? 1 : 2) : 1;
1935 rect.width = scale * pix->width;
1936 rect.height = scale * pix->height;
1940 if (rect.height < 4)
1942 if (rect.width > bounds->left + bounds->width - rect.left)
1943 rect.width = bounds->left + bounds->width - rect.left;
1944 if (rect.height > bounds->top + bounds->height - rect.top)
1945 rect.height = bounds->top + bounds->height - rect.top;
1950 { /* adjust the scaling factor */
1952 a = rect.width * rect.height;
1953 b = pix->width * pix->height;
1954 scale = b ? (u8)((a / b) < 4 ? 1 : 2) : 1;
1957 pix->width = rect.width / scale;
1958 pix->height = rect.height / scale;
1960 if (pix->pixelformat != V4L2_PIX_FMT_ET61X251 &&
1961 pix->pixelformat != V4L2_PIX_FMT_SBGGR8)
1962 pix->pixelformat = pfmt->pixelformat;
1963 pix->priv = pfmt->priv; /* bpp */
1964 pix->colorspace = pfmt->colorspace;
1965 pix->bytesperline = (pix->pixelformat == V4L2_PIX_FMT_ET61X251)
1966 ? 0 : (pix->width * pix->priv) / 8;
1967 pix->sizeimage = pix->height * ((pix->width * pix->priv) / 8);
1968 pix->field = V4L2_FIELD_NONE;
1970 if (cmd == VIDIOC_TRY_FMT) {
1971 if (copy_to_user(arg, &format, sizeof(format)))
1976 if (cam->module_param.force_munmap)
1977 for (i = 0; i < cam->nbuffers; i++)
1978 if (cam->frame[i].vma_use_count) {
1979 DBG(3, "VIDIOC_S_FMT failed. "
1980 "Unmap the buffers first.");
1984 if (cam->stream == STREAM_ON)
1985 if ((err = et61x251_stream_interrupt(cam)))
1988 if (copy_to_user(arg, &format, sizeof(format))) {
1989 cam->stream = stream;
1993 if (cam->module_param.force_munmap || cam->io == IO_READ)
1994 et61x251_release_buffers(cam);
1996 err += et61x251_set_pix_format(cam, pix);
1997 err += et61x251_set_crop(cam, &rect);
1998 if (s->set_pix_format)
1999 err += s->set_pix_format(cam, pix);
2001 err += s->set_crop(cam, &rect);
2002 err += et61x251_set_scale(cam, scale);
2004 if (err) { /* atomic, no rollback in ioctl() */
2005 cam->state |= DEV_MISCONFIGURED;
2006 DBG(1, "VIDIOC_S_FMT failed because of hardware problems. To "
2007 "use the camera, close and open /dev/video%d again.",
2008 cam->v4ldev->minor);
2012 memcpy(pfmt, pix, sizeof(*pix));
2013 memcpy(&(s->_rect), &rect, sizeof(rect));
2015 if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
2016 nbuffers != et61x251_request_buffers(cam, nbuffers, cam->io)) {
2017 cam->state |= DEV_MISCONFIGURED;
2018 DBG(1, "VIDIOC_S_FMT failed because of not enough memory. To "
2019 "use the camera, close and open /dev/video%d again.",
2020 cam->v4ldev->minor);
2024 if (cam->io == IO_READ)
2025 et61x251_empty_framequeues(cam);
2026 else if (cam->module_param.force_munmap)
2027 et61x251_requeue_outqueue(cam);
2029 cam->stream = stream;
2036 et61x251_vidioc_g_jpegcomp(struct et61x251_device* cam, void __user * arg)
2038 if (copy_to_user(arg, &cam->compression,
2039 sizeof(cam->compression)))
2047 et61x251_vidioc_s_jpegcomp(struct et61x251_device* cam, void __user * arg)
2049 struct v4l2_jpegcompression jc;
2050 const enum et61x251_stream_state stream = cam->stream;
2053 if (copy_from_user(&jc, arg, sizeof(jc)))
2056 if (jc.quality != 0 && jc.quality != 1)
2059 if (cam->stream == STREAM_ON)
2060 if ((err = et61x251_stream_interrupt(cam)))
2063 err += et61x251_set_compression(cam, &jc);
2064 if (err) { /* atomic, no rollback in ioctl() */
2065 cam->state |= DEV_MISCONFIGURED;
2066 DBG(1, "VIDIOC_S_JPEGCOMP failed because of hardware "
2067 "problems. To use the camera, close and open "
2068 "/dev/video%d again.", cam->v4ldev->minor);
2072 cam->compression.quality = jc.quality;
2074 cam->stream = stream;
2081 et61x251_vidioc_reqbufs(struct et61x251_device* cam, void __user * arg)
2083 struct v4l2_requestbuffers rb;
2087 if (copy_from_user(&rb, arg, sizeof(rb)))
2090 if (rb.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2091 rb.memory != V4L2_MEMORY_MMAP)
2094 if (cam->io == IO_READ) {
2095 DBG(3, "Close and open the device again to choose the mmap "
2100 for (i = 0; i < cam->nbuffers; i++)
2101 if (cam->frame[i].vma_use_count) {
2102 DBG(3, "VIDIOC_REQBUFS failed. "
2103 "Previous buffers are still mapped.");
2107 if (cam->stream == STREAM_ON)
2108 if ((err = et61x251_stream_interrupt(cam)))
2111 et61x251_empty_framequeues(cam);
2113 et61x251_release_buffers(cam);
2115 rb.count = et61x251_request_buffers(cam, rb.count, IO_MMAP);
2117 if (copy_to_user(arg, &rb, sizeof(rb))) {
2118 et61x251_release_buffers(cam);
2123 cam->io = rb.count ? IO_MMAP : IO_NONE;
2130 et61x251_vidioc_querybuf(struct et61x251_device* cam, void __user * arg)
2132 struct v4l2_buffer b;
2134 if (copy_from_user(&b, arg, sizeof(b)))
2137 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2138 b.index >= cam->nbuffers || cam->io != IO_MMAP)
2141 memcpy(&b, &cam->frame[b.index].buf, sizeof(b));
2143 if (cam->frame[b.index].vma_use_count)
2144 b.flags |= V4L2_BUF_FLAG_MAPPED;
2146 if (cam->frame[b.index].state == F_DONE)
2147 b.flags |= V4L2_BUF_FLAG_DONE;
2148 else if (cam->frame[b.index].state != F_UNUSED)
2149 b.flags |= V4L2_BUF_FLAG_QUEUED;
2151 if (copy_to_user(arg, &b, sizeof(b)))
2159 et61x251_vidioc_qbuf(struct et61x251_device* cam, void __user * arg)
2161 struct v4l2_buffer b;
2162 unsigned long lock_flags;
2164 if (copy_from_user(&b, arg, sizeof(b)))
2167 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2168 b.index >= cam->nbuffers || cam->io != IO_MMAP)
2171 if (cam->frame[b.index].state != F_UNUSED)
2174 cam->frame[b.index].state = F_QUEUED;
2176 spin_lock_irqsave(&cam->queue_lock, lock_flags);
2177 list_add_tail(&cam->frame[b.index].frame, &cam->inqueue);
2178 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2180 PDBGG("Frame #%lu queued", (unsigned long)b.index);
2187 et61x251_vidioc_dqbuf(struct et61x251_device* cam, struct file* filp,
2190 struct v4l2_buffer b;
2191 struct et61x251_frame_t *f;
2192 unsigned long lock_flags;
2195 if (copy_from_user(&b, arg, sizeof(b)))
2198 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io!= IO_MMAP)
2201 if (list_empty(&cam->outqueue)) {
2202 if (cam->stream == STREAM_OFF)
2204 if (filp->f_flags & O_NONBLOCK)
2206 timeout = wait_event_interruptible_timeout
2208 (!list_empty(&cam->outqueue)) ||
2209 (cam->state & DEV_DISCONNECTED) ||
2210 (cam->state & DEV_MISCONFIGURED),
2211 cam->module_param.frame_timeout *
2212 1000 * msecs_to_jiffies(1) );
2215 if (cam->state & DEV_DISCONNECTED)
2217 if (!timeout || (cam->state & DEV_MISCONFIGURED))
2221 spin_lock_irqsave(&cam->queue_lock, lock_flags);
2222 f = list_entry(cam->outqueue.next, struct et61x251_frame_t, frame);
2223 list_del(cam->outqueue.next);
2224 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2226 f->state = F_UNUSED;
2228 memcpy(&b, &f->buf, sizeof(b));
2229 if (f->vma_use_count)
2230 b.flags |= V4L2_BUF_FLAG_MAPPED;
2232 if (copy_to_user(arg, &b, sizeof(b)))
2235 PDBGG("Frame #%lu dequeued", (unsigned long)f->buf.index);
2242 et61x251_vidioc_streamon(struct et61x251_device* cam, void __user * arg)
2246 if (copy_from_user(&type, arg, sizeof(type)))
2249 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2252 if (list_empty(&cam->inqueue))
2255 cam->stream = STREAM_ON;
2257 DBG(3, "Stream on");
2264 et61x251_vidioc_streamoff(struct et61x251_device* cam, void __user * arg)
2268 if (copy_from_user(&type, arg, sizeof(type)))
2271 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2274 if (cam->stream == STREAM_ON)
2275 if ((err = et61x251_stream_interrupt(cam)))
2278 et61x251_empty_framequeues(cam);
2280 DBG(3, "Stream off");
2287 et61x251_vidioc_g_parm(struct et61x251_device* cam, void __user * arg)
2289 struct v4l2_streamparm sp;
2291 if (copy_from_user(&sp, arg, sizeof(sp)))
2294 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2297 sp.parm.capture.extendedmode = 0;
2298 sp.parm.capture.readbuffers = cam->nreadbuffers;
2300 if (copy_to_user(arg, &sp, sizeof(sp)))
2308 et61x251_vidioc_s_parm(struct et61x251_device* cam, void __user * arg)
2310 struct v4l2_streamparm sp;
2312 if (copy_from_user(&sp, arg, sizeof(sp)))
2315 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2318 sp.parm.capture.extendedmode = 0;
2320 if (sp.parm.capture.readbuffers == 0)
2321 sp.parm.capture.readbuffers = cam->nreadbuffers;
2323 if (sp.parm.capture.readbuffers > ET61X251_MAX_FRAMES)
2324 sp.parm.capture.readbuffers = ET61X251_MAX_FRAMES;
2326 if (copy_to_user(arg, &sp, sizeof(sp)))
2329 cam->nreadbuffers = sp.parm.capture.readbuffers;
2335 static int et61x251_ioctl_v4l2(struct inode* inode, struct file* filp,
2336 unsigned int cmd, void __user * arg)
2338 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp));
2342 case VIDIOC_QUERYCAP:
2343 return et61x251_vidioc_querycap(cam, arg);
2345 case VIDIOC_ENUMINPUT:
2346 return et61x251_vidioc_enuminput(cam, arg);
2348 case VIDIOC_G_INPUT:
2349 return et61x251_vidioc_g_input(cam, arg);
2351 case VIDIOC_S_INPUT:
2352 return et61x251_vidioc_s_input(cam, arg);
2354 case VIDIOC_QUERYCTRL:
2355 return et61x251_vidioc_query_ctrl(cam, arg);
2358 return et61x251_vidioc_g_ctrl(cam, arg);
2361 return et61x251_vidioc_s_ctrl(cam, arg);
2363 case VIDIOC_CROPCAP:
2364 return et61x251_vidioc_cropcap(cam, arg);
2367 return et61x251_vidioc_g_crop(cam, arg);
2370 return et61x251_vidioc_s_crop(cam, arg);
2372 case VIDIOC_ENUM_FMT:
2373 return et61x251_vidioc_enum_fmt(cam, arg);
2376 return et61x251_vidioc_g_fmt(cam, arg);
2378 case VIDIOC_TRY_FMT:
2380 return et61x251_vidioc_try_s_fmt(cam, cmd, arg);
2382 case VIDIOC_G_JPEGCOMP:
2383 return et61x251_vidioc_g_jpegcomp(cam, arg);
2385 case VIDIOC_S_JPEGCOMP:
2386 return et61x251_vidioc_s_jpegcomp(cam, arg);
2388 case VIDIOC_REQBUFS:
2389 return et61x251_vidioc_reqbufs(cam, arg);
2391 case VIDIOC_QUERYBUF:
2392 return et61x251_vidioc_querybuf(cam, arg);
2395 return et61x251_vidioc_qbuf(cam, arg);
2398 return et61x251_vidioc_dqbuf(cam, filp, arg);
2400 case VIDIOC_STREAMON:
2401 return et61x251_vidioc_streamon(cam, arg);
2403 case VIDIOC_STREAMOFF:
2404 return et61x251_vidioc_streamoff(cam, arg);
2407 return et61x251_vidioc_g_parm(cam, arg);
2410 return et61x251_vidioc_s_parm(cam, arg);
2414 case VIDIOC_QUERYSTD:
2415 case VIDIOC_ENUMSTD:
2416 case VIDIOC_QUERYMENU:
2426 static int et61x251_ioctl(struct inode* inode, struct file* filp,
2427 unsigned int cmd, unsigned long arg)
2429 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp));
2432 if (mutex_lock_interruptible(&cam->fileop_mutex))
2433 return -ERESTARTSYS;
2435 if (cam->state & DEV_DISCONNECTED) {
2436 DBG(1, "Device not present");
2437 mutex_unlock(&cam->fileop_mutex);
2441 if (cam->state & DEV_MISCONFIGURED) {
2442 DBG(1, "The camera is misconfigured. Close and open it "
2444 mutex_unlock(&cam->fileop_mutex);
2448 V4LDBG(3, "et61x251", cmd);
2450 err = et61x251_ioctl_v4l2(inode, filp, cmd, (void __user *)arg);
2452 mutex_unlock(&cam->fileop_mutex);
2458 static struct file_operations et61x251_fops = {
2459 .owner = THIS_MODULE,
2460 .open = et61x251_open,
2461 .release = et61x251_release,
2462 .ioctl = et61x251_ioctl,
2463 .read = et61x251_read,
2464 .poll = et61x251_poll,
2465 .mmap = et61x251_mmap,
2466 .llseek = no_llseek,
2469 /*****************************************************************************/
2471 /* It exists a single interface only. We do not need to validate anything. */
2473 et61x251_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
2475 struct usb_device *udev = interface_to_usbdev(intf);
2476 struct et61x251_device* cam;
2477 static unsigned int dev_nr = 0;
2481 if (!(cam = kzalloc(sizeof(struct et61x251_device), GFP_KERNEL)))
2486 if (!(cam->control_buffer = kzalloc(8, GFP_KERNEL))) {
2487 DBG(1, "kmalloc() failed");
2492 if (!(cam->v4ldev = video_device_alloc())) {
2493 DBG(1, "video_device_alloc() failed");
2498 mutex_init(&cam->dev_mutex);
2500 DBG(2, "ET61X[12]51 PC Camera Controller detected "
2501 "(vid/pid 0x%04X/0x%04X)",id->idVendor, id->idProduct);
2503 for (i = 0; et61x251_sensor_table[i]; i++) {
2504 err = et61x251_sensor_table[i](cam);
2510 DBG(2, "%s image sensor detected", cam->sensor.name);
2512 DBG(1, "No supported image sensor detected");
2517 if (et61x251_init(cam)) {
2518 DBG(1, "Initialization failed. I will retry on open().");
2519 cam->state |= DEV_MISCONFIGURED;
2522 strcpy(cam->v4ldev->name, "ET61X[12]51 PC Camera");
2523 cam->v4ldev->owner = THIS_MODULE;
2524 cam->v4ldev->type = VID_TYPE_CAPTURE | VID_TYPE_SCALES;
2525 cam->v4ldev->hardware = 0;
2526 cam->v4ldev->fops = &et61x251_fops;
2527 cam->v4ldev->minor = video_nr[dev_nr];
2528 cam->v4ldev->release = video_device_release;
2529 video_set_drvdata(cam->v4ldev, cam);
2531 mutex_lock(&cam->dev_mutex);
2533 err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER,
2536 DBG(1, "V4L2 device registration failed");
2537 if (err == -ENFILE && video_nr[dev_nr] == -1)
2538 DBG(1, "Free /dev/videoX node not found");
2539 video_nr[dev_nr] = -1;
2540 dev_nr = (dev_nr < ET61X251_MAX_DEVICES-1) ? dev_nr+1 : 0;
2541 mutex_unlock(&cam->dev_mutex);
2545 DBG(2, "V4L2 device registered as /dev/video%d", cam->v4ldev->minor);
2547 cam->module_param.force_munmap = force_munmap[dev_nr];
2548 cam->module_param.frame_timeout = frame_timeout[dev_nr];
2550 dev_nr = (dev_nr < ET61X251_MAX_DEVICES-1) ? dev_nr+1 : 0;
2552 #ifdef CONFIG_VIDEO_ADV_DEBUG
2553 err = et61x251_create_sysfs(cam);
2556 DBG(2, "Optional device control through 'sysfs' interface ready");
2559 usb_set_intfdata(intf, cam);
2561 mutex_unlock(&cam->dev_mutex);
2565 #ifdef CONFIG_VIDEO_ADV_DEBUG
2567 video_nr[dev_nr] = -1;
2568 dev_nr = (dev_nr < ET61X251_MAX_DEVICES-1) ? dev_nr+1 : 0;
2569 mutex_unlock(&cam->dev_mutex);
2570 video_unregister_device(cam->v4ldev);
2574 kfree(cam->control_buffer);
2576 video_device_release(cam->v4ldev);
2583 static void et61x251_usb_disconnect(struct usb_interface* intf)
2585 struct et61x251_device* cam = usb_get_intfdata(intf);
2590 down_write(&et61x251_disconnect);
2592 mutex_lock(&cam->dev_mutex);
2594 DBG(2, "Disconnecting %s...", cam->v4ldev->name);
2596 wake_up_interruptible_all(&cam->open);
2599 DBG(2, "Device /dev/video%d is open! Deregistration and "
2600 "memory deallocation are deferred on close.",
2601 cam->v4ldev->minor);
2602 cam->state |= DEV_MISCONFIGURED;
2603 et61x251_stop_transfer(cam);
2604 cam->state |= DEV_DISCONNECTED;
2605 wake_up_interruptible(&cam->wait_frame);
2606 wake_up(&cam->wait_stream);
2607 usb_get_dev(cam->usbdev);
2609 cam->state |= DEV_DISCONNECTED;
2610 et61x251_release_resources(cam);
2613 mutex_unlock(&cam->dev_mutex);
2618 up_write(&et61x251_disconnect);
2622 static struct usb_driver et61x251_usb_driver = {
2624 .id_table = et61x251_id_table,
2625 .probe = et61x251_usb_probe,
2626 .disconnect = et61x251_usb_disconnect,
2629 /*****************************************************************************/
2631 static int __init et61x251_module_init(void)
2635 KDBG(2, ET61X251_MODULE_NAME " v" ET61X251_MODULE_VERSION);
2636 KDBG(3, ET61X251_MODULE_AUTHOR);
2638 if ((err = usb_register(&et61x251_usb_driver)))
2639 KDBG(1, "usb_register() failed");
2645 static void __exit et61x251_module_exit(void)
2647 usb_deregister(&et61x251_usb_driver);
2651 module_init(et61x251_module_init);
2652 module_exit(et61x251_module_exit);