mm/slab.c: proper prototypes
[wrt350n-kernel.git] / drivers / media / video / et61x251 / et61x251_core.c
bloba6525513cd1e12492248ade25f8274949003cfd8
1 /***************************************************************************
2 * V4L2 driver for ET61X[12]51 PC Camera Controllers *
3 * *
4 * Copyright (C) 2006-2007 by Luca Risolia <luca.risolia@studio.unibo.it> *
5 * *
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. *
10 * *
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. *
15 * *
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>
29 #include <linux/fs.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>
35 #include <linux/mm.h>
36 #include <linux/vmalloc.h>
37 #include <linux/page-flags.h>
38 #include <linux/byteorder/generic.h>
39 #include <asm/page.h>
40 #include <asm/uaccess.h>
42 #include "et61x251.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.04"
52 #define ET61X251_MODULE_VERSION_CODE KERNEL_VERSION(1, 1, 4)
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."
71 "\nFor example:"
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."
75 "\n");
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"
85 "\ndetected camera."
86 "\n 0 = do not force memory unmapping"
87 "\n 1 = force memory unmapping (save memory)"
88 "\nDefault value is "__MODULE_STRING(ET61X251_FORCE_MUNMAP)"."
89 "\n");
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."
97 "\nDefault value is "
98 __MODULE_STRING(ET61X251_FRAME_TIMEOUT)"."
99 "\n");
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)"."
113 "\n");
114 #endif
116 /*****************************************************************************/
118 static u32
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 ||
125 io == IO_READ ?
126 (p->width * p->height * p->priv) / 8 :
127 (r->width * r->height * p->priv) / 8;
128 void* buff = NULL;
129 u32 i;
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_user(cam->nbuffers *
137 PAGE_ALIGN(imagesize))))
138 break;
139 cam->nbuffers--;
142 for (i = 0; i < cam->nbuffers; i++) {
143 cam->frame[i].bufmem = buff + i*PAGE_ALIGN(imagesize);
144 cam->frame[i].buf.index = i;
145 cam->frame[i].buf.m.offset = i*PAGE_ALIGN(imagesize);
146 cam->frame[i].buf.length = imagesize;
147 cam->frame[i].buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
148 cam->frame[i].buf.sequence = 0;
149 cam->frame[i].buf.field = V4L2_FIELD_NONE;
150 cam->frame[i].buf.memory = V4L2_MEMORY_MMAP;
151 cam->frame[i].buf.flags = 0;
154 return cam->nbuffers;
158 static void et61x251_release_buffers(struct et61x251_device* cam)
160 if (cam->nbuffers) {
161 vfree(cam->frame[0].bufmem);
162 cam->nbuffers = 0;
164 cam->frame_current = NULL;
168 static void et61x251_empty_framequeues(struct et61x251_device* cam)
170 u32 i;
172 INIT_LIST_HEAD(&cam->inqueue);
173 INIT_LIST_HEAD(&cam->outqueue);
175 for (i = 0; i < ET61X251_MAX_FRAMES; i++) {
176 cam->frame[i].state = F_UNUSED;
177 cam->frame[i].buf.bytesused = 0;
182 static void et61x251_requeue_outqueue(struct et61x251_device* cam)
184 struct et61x251_frame_t *i;
186 list_for_each_entry(i, &cam->outqueue, frame) {
187 i->state = F_QUEUED;
188 list_add(&i->frame, &cam->inqueue);
191 INIT_LIST_HEAD(&cam->outqueue);
195 static void et61x251_queue_unusedframes(struct et61x251_device* cam)
197 unsigned long lock_flags;
198 u32 i;
200 for (i = 0; i < cam->nbuffers; i++)
201 if (cam->frame[i].state == F_UNUSED) {
202 cam->frame[i].state = F_QUEUED;
203 spin_lock_irqsave(&cam->queue_lock, lock_flags);
204 list_add_tail(&cam->frame[i].frame, &cam->inqueue);
205 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
209 /*****************************************************************************/
211 int et61x251_write_reg(struct et61x251_device* cam, u8 value, u16 index)
213 struct usb_device* udev = cam->usbdev;
214 u8* buff = cam->control_buffer;
215 int res;
217 *buff = value;
219 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
220 0, index, buff, 1, ET61X251_CTRL_TIMEOUT);
221 if (res < 0) {
222 DBG(3, "Failed to write a register (value 0x%02X, index "
223 "0x%02X, error %d)", value, index, res);
224 return -1;
227 return 0;
231 int et61x251_read_reg(struct et61x251_device* cam, u16 index)
233 struct usb_device* udev = cam->usbdev;
234 u8* buff = cam->control_buffer;
235 int res;
237 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1,
238 0, index, buff, 1, ET61X251_CTRL_TIMEOUT);
239 if (res < 0)
240 DBG(3, "Failed to read a register (index 0x%02X, error %d)",
241 index, res);
243 return (res >= 0) ? (int)(*buff) : -1;
247 static int
248 et61x251_i2c_wait(struct et61x251_device* cam, struct et61x251_sensor* sensor)
250 int i, r;
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))
256 return 0;
257 } else {
258 r = et61x251_read_reg(cam, 0x8b);
259 if (!(r & 0x01) && (r >= 0))
260 return 0;
262 if (r < 0)
263 return -EIO;
264 udelay(8*8); /* minimum for sensors at 400kHz */
267 return -EBUSY;
272 et61x251_i2c_try_read(struct et61x251_device* cam,
273 struct et61x251_sensor* sensor, u8 address)
275 struct usb_device* udev = cam->usbdev;
276 u8* data = cam->control_buffer;
277 int err = 0, res;
279 data[0] = address;
280 data[1] = cam->sensor.i2c_slave_id;
281 data[2] = cam->sensor.rsta | 0x10;
282 data[3] = !(et61x251_read_reg(cam, 0x8b) & 0x02);
283 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
284 0, 0x88, data, 4, ET61X251_CTRL_TIMEOUT);
285 if (res < 0)
286 err += res;
288 err += et61x251_i2c_wait(cam, sensor);
290 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1,
291 0, 0x80, data, 8, ET61X251_CTRL_TIMEOUT);
292 if (res < 0)
293 err += res;
295 if (err)
296 DBG(3, "I2C read failed for %s image sensor", sensor->name);
298 PDBGG("I2C read: address 0x%02X, value: 0x%02X", address, data[0]);
300 return err ? -1 : (int)data[0];
305 et61x251_i2c_try_write(struct et61x251_device* cam,
306 struct et61x251_sensor* sensor, u8 address, u8 value)
308 struct usb_device* udev = cam->usbdev;
309 u8* data = cam->control_buffer;
310 int err = 0, res;
312 data[0] = address;
313 data[1] = cam->sensor.i2c_slave_id;
314 data[2] = cam->sensor.rsta | 0x12;
315 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
316 0, 0x88, data, 3, ET61X251_CTRL_TIMEOUT);
317 if (res < 0)
318 err += res;
320 data[0] = value;
321 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
322 0, 0x80, data, 1, ET61X251_CTRL_TIMEOUT);
323 if (res < 0)
324 err += res;
326 err += et61x251_i2c_wait(cam, sensor);
328 if (err)
329 DBG(3, "I2C write failed for %s image sensor", sensor->name);
331 PDBGG("I2C write: address 0x%02X, value: 0x%02X", address, value);
333 return err ? -1 : 0;
338 et61x251_i2c_raw_write(struct et61x251_device* cam, u8 n, u8 data1, u8 data2,
339 u8 data3, u8 data4, u8 data5, u8 data6, u8 data7,
340 u8 data8, u8 address)
342 struct usb_device* udev = cam->usbdev;
343 u8* data = cam->control_buffer;
344 int err = 0, res;
346 data[0] = data2;
347 data[1] = data3;
348 data[2] = data4;
349 data[3] = data5;
350 data[4] = data6;
351 data[5] = data7;
352 data[6] = data8;
353 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
354 0, 0x81, data, n-1, ET61X251_CTRL_TIMEOUT);
355 if (res < 0)
356 err += res;
358 data[0] = address;
359 data[1] = cam->sensor.i2c_slave_id;
360 data[2] = cam->sensor.rsta | 0x02 | (n << 4);
361 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
362 0, 0x88, data, 3, ET61X251_CTRL_TIMEOUT);
363 if (res < 0)
364 err += res;
366 /* Start writing through the serial interface */
367 data[0] = data1;
368 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
369 0, 0x80, data, 1, ET61X251_CTRL_TIMEOUT);
370 if (res < 0)
371 err += res;
373 err += et61x251_i2c_wait(cam, &cam->sensor);
375 if (err)
376 DBG(3, "I2C raw write failed for %s image sensor",
377 cam->sensor.name);
379 PDBGG("I2C raw write: %u bytes, address = 0x%02X, data1 = 0x%02X, "
380 "data2 = 0x%02X, data3 = 0x%02X, data4 = 0x%02X, data5 = 0x%02X,"
381 " data6 = 0x%02X, data7 = 0x%02X, data8 = 0x%02X", n, address,
382 data1, data2, data3, data4, data5, data6, data7, data8);
384 return err ? -1 : 0;
389 int et61x251_i2c_read(struct et61x251_device* cam, u8 address)
391 return et61x251_i2c_try_read(cam, &cam->sensor, address);
395 int et61x251_i2c_write(struct et61x251_device* cam, u8 address, u8 value)
397 return et61x251_i2c_try_write(cam, &cam->sensor, address, value);
400 /*****************************************************************************/
402 static void et61x251_urb_complete(struct urb *urb)
404 struct et61x251_device* cam = urb->context;
405 struct et61x251_frame_t** f;
406 size_t imagesize;
407 u8 i;
408 int err = 0;
410 if (urb->status == -ENOENT)
411 return;
413 f = &cam->frame_current;
415 if (cam->stream == STREAM_INTERRUPT) {
416 cam->stream = STREAM_OFF;
417 if ((*f))
418 (*f)->state = F_QUEUED;
419 DBG(3, "Stream interrupted");
420 wake_up(&cam->wait_stream);
423 if (cam->state & DEV_DISCONNECTED)
424 return;
426 if (cam->state & DEV_MISCONFIGURED) {
427 wake_up_interruptible(&cam->wait_frame);
428 return;
431 if (cam->stream == STREAM_OFF || list_empty(&cam->inqueue))
432 goto resubmit_urb;
434 if (!(*f))
435 (*f) = list_entry(cam->inqueue.next, struct et61x251_frame_t,
436 frame);
438 imagesize = (cam->sensor.pix_format.width *
439 cam->sensor.pix_format.height *
440 cam->sensor.pix_format.priv) / 8;
442 for (i = 0; i < urb->number_of_packets; i++) {
443 unsigned int len, status;
444 void *pos;
445 u8* b1, * b2, sof;
446 const u8 VOID_BYTES = 6;
447 size_t imglen;
449 len = urb->iso_frame_desc[i].actual_length;
450 status = urb->iso_frame_desc[i].status;
451 pos = urb->iso_frame_desc[i].offset + urb->transfer_buffer;
453 if (status) {
454 DBG(3, "Error in isochronous frame");
455 (*f)->state = F_ERROR;
456 continue;
459 b1 = pos++;
460 b2 = pos++;
461 sof = ((*b1 & 0x3f) == 63);
462 imglen = ((*b1 & 0xc0) << 2) | *b2;
464 PDBGG("Isochrnous frame: length %u, #%u i, image length %zu",
465 len, i, imglen);
467 if ((*f)->state == F_QUEUED || (*f)->state == F_ERROR)
468 start_of_frame:
469 if (sof) {
470 (*f)->state = F_GRABBING;
471 (*f)->buf.bytesused = 0;
472 do_gettimeofday(&(*f)->buf.timestamp);
473 pos += 22;
474 DBG(3, "SOF detected: new video frame");
477 if ((*f)->state == F_GRABBING) {
478 if (sof && (*f)->buf.bytesused) {
479 if (cam->sensor.pix_format.pixelformat ==
480 V4L2_PIX_FMT_ET61X251)
481 goto end_of_frame;
482 else {
483 DBG(3, "Not expected SOF detected "
484 "after %lu bytes",
485 (unsigned long)(*f)->buf.bytesused);
486 (*f)->state = F_ERROR;
487 continue;
491 if ((*f)->buf.bytesused + imglen > imagesize) {
492 DBG(3, "Video frame size exceeded");
493 (*f)->state = F_ERROR;
494 continue;
497 pos += VOID_BYTES;
499 memcpy((*f)->bufmem+(*f)->buf.bytesused, pos, imglen);
500 (*f)->buf.bytesused += imglen;
502 if ((*f)->buf.bytesused == imagesize) {
503 u32 b;
504 end_of_frame:
505 b = (*f)->buf.bytesused;
506 (*f)->state = F_DONE;
507 (*f)->buf.sequence= ++cam->frame_count;
508 spin_lock(&cam->queue_lock);
509 list_move_tail(&(*f)->frame, &cam->outqueue);
510 if (!list_empty(&cam->inqueue))
511 (*f) = list_entry(cam->inqueue.next,
512 struct et61x251_frame_t,
513 frame);
514 else
515 (*f) = NULL;
516 spin_unlock(&cam->queue_lock);
517 DBG(3, "Video frame captured: : %lu bytes",
518 (unsigned long)(b));
520 if (!(*f))
521 goto resubmit_urb;
523 if (sof &&
524 cam->sensor.pix_format.pixelformat ==
525 V4L2_PIX_FMT_ET61X251)
526 goto start_of_frame;
531 resubmit_urb:
532 urb->dev = cam->usbdev;
533 err = usb_submit_urb(urb, GFP_ATOMIC);
534 if (err < 0 && err != -EPERM) {
535 cam->state |= DEV_MISCONFIGURED;
536 DBG(1, "usb_submit_urb() failed");
539 wake_up_interruptible(&cam->wait_frame);
543 static int et61x251_start_transfer(struct et61x251_device* cam)
545 struct usb_device *udev = cam->usbdev;
546 struct urb* urb;
547 struct usb_host_interface* altsetting = usb_altnum_to_altsetting(
548 usb_ifnum_to_if(udev, 0),
549 ET61X251_ALTERNATE_SETTING);
550 const unsigned int psz = le16_to_cpu(altsetting->
551 endpoint[0].desc.wMaxPacketSize);
552 s8 i, j;
553 int err = 0;
555 for (i = 0; i < ET61X251_URBS; i++) {
556 cam->transfer_buffer[i] = kzalloc(ET61X251_ISO_PACKETS * psz,
557 GFP_KERNEL);
558 if (!cam->transfer_buffer[i]) {
559 err = -ENOMEM;
560 DBG(1, "Not enough memory");
561 goto free_buffers;
565 for (i = 0; i < ET61X251_URBS; i++) {
566 urb = usb_alloc_urb(ET61X251_ISO_PACKETS, GFP_KERNEL);
567 cam->urb[i] = urb;
568 if (!urb) {
569 err = -ENOMEM;
570 DBG(1, "usb_alloc_urb() failed");
571 goto free_urbs;
573 urb->dev = udev;
574 urb->context = cam;
575 urb->pipe = usb_rcvisocpipe(udev, 1);
576 urb->transfer_flags = URB_ISO_ASAP;
577 urb->number_of_packets = ET61X251_ISO_PACKETS;
578 urb->complete = et61x251_urb_complete;
579 urb->transfer_buffer = cam->transfer_buffer[i];
580 urb->transfer_buffer_length = psz * ET61X251_ISO_PACKETS;
581 urb->interval = 1;
582 for (j = 0; j < ET61X251_ISO_PACKETS; j++) {
583 urb->iso_frame_desc[j].offset = psz * j;
584 urb->iso_frame_desc[j].length = psz;
588 err = et61x251_write_reg(cam, 0x01, 0x03);
589 err = et61x251_write_reg(cam, 0x00, 0x03);
590 err = et61x251_write_reg(cam, 0x08, 0x03);
591 if (err) {
592 err = -EIO;
593 DBG(1, "I/O hardware error");
594 goto free_urbs;
597 err = usb_set_interface(udev, 0, ET61X251_ALTERNATE_SETTING);
598 if (err) {
599 DBG(1, "usb_set_interface() failed");
600 goto free_urbs;
603 cam->frame_current = NULL;
605 for (i = 0; i < ET61X251_URBS; i++) {
606 err = usb_submit_urb(cam->urb[i], GFP_KERNEL);
607 if (err) {
608 for (j = i-1; j >= 0; j--)
609 usb_kill_urb(cam->urb[j]);
610 DBG(1, "usb_submit_urb() failed, error %d", err);
611 goto free_urbs;
615 return 0;
617 free_urbs:
618 for (i = 0; (i < ET61X251_URBS) && cam->urb[i]; i++)
619 usb_free_urb(cam->urb[i]);
621 free_buffers:
622 for (i = 0; (i < ET61X251_URBS) && cam->transfer_buffer[i]; i++)
623 kfree(cam->transfer_buffer[i]);
625 return err;
629 static int et61x251_stop_transfer(struct et61x251_device* cam)
631 struct usb_device *udev = cam->usbdev;
632 s8 i;
633 int err = 0;
635 if (cam->state & DEV_DISCONNECTED)
636 return 0;
638 for (i = ET61X251_URBS-1; i >= 0; i--) {
639 usb_kill_urb(cam->urb[i]);
640 usb_free_urb(cam->urb[i]);
641 kfree(cam->transfer_buffer[i]);
644 err = usb_set_interface(udev, 0, 0); /* 0 Mb/s */
645 if (err)
646 DBG(3, "usb_set_interface() failed");
648 return err;
652 static int et61x251_stream_interrupt(struct et61x251_device* cam)
654 long timeout;
656 cam->stream = STREAM_INTERRUPT;
657 timeout = wait_event_timeout(cam->wait_stream,
658 (cam->stream == STREAM_OFF) ||
659 (cam->state & DEV_DISCONNECTED),
660 ET61X251_URB_TIMEOUT);
661 if (cam->state & DEV_DISCONNECTED)
662 return -ENODEV;
663 else if (cam->stream != STREAM_OFF) {
664 cam->state |= DEV_MISCONFIGURED;
665 DBG(1, "URB timeout reached. The camera is misconfigured. To "
666 "use it, close and open /dev/video%d again.",
667 cam->v4ldev->minor);
668 return -EIO;
671 return 0;
674 /*****************************************************************************/
676 #ifdef CONFIG_VIDEO_ADV_DEBUG
677 static u8 et61x251_strtou8(const char* buff, size_t len, ssize_t* count)
679 char str[5];
680 char* endp;
681 unsigned long val;
683 if (len < 4) {
684 strncpy(str, buff, len);
685 str[len+1] = '\0';
686 } else {
687 strncpy(str, buff, 4);
688 str[4] = '\0';
691 val = simple_strtoul(str, &endp, 0);
693 *count = 0;
694 if (val <= 0xff)
695 *count = (ssize_t)(endp - str);
696 if ((*count) && (len == *count+1) && (buff[*count] == '\n'))
697 *count += 1;
699 return (u8)val;
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 class_device* cd, char* buf)
710 struct et61x251_device* cam;
711 ssize_t count;
713 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
714 return -ERESTARTSYS;
716 cam = video_get_drvdata(to_video_device(cd));
717 if (!cam) {
718 mutex_unlock(&et61x251_sysfs_lock);
719 return -ENODEV;
722 count = sprintf(buf, "%u\n", cam->sysfs.reg);
724 mutex_unlock(&et61x251_sysfs_lock);
726 return count;
730 static ssize_t
731 et61x251_store_reg(struct class_device* cd, const char* buf, size_t len)
733 struct et61x251_device* cam;
734 u8 index;
735 ssize_t count;
737 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
738 return -ERESTARTSYS;
740 cam = video_get_drvdata(to_video_device(cd));
741 if (!cam) {
742 mutex_unlock(&et61x251_sysfs_lock);
743 return -ENODEV;
746 index = et61x251_strtou8(buf, len, &count);
747 if (index > 0x8e || !count) {
748 mutex_unlock(&et61x251_sysfs_lock);
749 return -EINVAL;
752 cam->sysfs.reg = index;
754 DBG(2, "Moved ET61X[12]51 register index to 0x%02X", cam->sysfs.reg);
755 DBG(3, "Written bytes: %zd", count);
757 mutex_unlock(&et61x251_sysfs_lock);
759 return count;
763 static ssize_t et61x251_show_val(struct class_device* cd, char* buf)
765 struct et61x251_device* cam;
766 ssize_t count;
767 int val;
769 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
770 return -ERESTARTSYS;
772 cam = video_get_drvdata(to_video_device(cd));
773 if (!cam) {
774 mutex_unlock(&et61x251_sysfs_lock);
775 return -ENODEV;
778 if ((val = et61x251_read_reg(cam, cam->sysfs.reg)) < 0) {
779 mutex_unlock(&et61x251_sysfs_lock);
780 return -EIO;
783 count = sprintf(buf, "%d\n", val);
785 DBG(3, "Read bytes: %zd", count);
787 mutex_unlock(&et61x251_sysfs_lock);
789 return count;
793 static ssize_t
794 et61x251_store_val(struct class_device* cd, const char* buf, size_t len)
796 struct et61x251_device* cam;
797 u8 value;
798 ssize_t count;
799 int err;
801 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
802 return -ERESTARTSYS;
804 cam = video_get_drvdata(to_video_device(cd));
805 if (!cam) {
806 mutex_unlock(&et61x251_sysfs_lock);
807 return -ENODEV;
810 value = et61x251_strtou8(buf, len, &count);
811 if (!count) {
812 mutex_unlock(&et61x251_sysfs_lock);
813 return -EINVAL;
816 err = et61x251_write_reg(cam, value, cam->sysfs.reg);
817 if (err) {
818 mutex_unlock(&et61x251_sysfs_lock);
819 return -EIO;
822 DBG(2, "Written ET61X[12]51 reg. 0x%02X, val. 0x%02X",
823 cam->sysfs.reg, value);
824 DBG(3, "Written bytes: %zd", count);
826 mutex_unlock(&et61x251_sysfs_lock);
828 return count;
832 static ssize_t et61x251_show_i2c_reg(struct class_device* cd, char* buf)
834 struct et61x251_device* cam;
835 ssize_t count;
837 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
838 return -ERESTARTSYS;
840 cam = video_get_drvdata(to_video_device(cd));
841 if (!cam) {
842 mutex_unlock(&et61x251_sysfs_lock);
843 return -ENODEV;
846 count = sprintf(buf, "%u\n", cam->sysfs.i2c_reg);
848 DBG(3, "Read bytes: %zd", count);
850 mutex_unlock(&et61x251_sysfs_lock);
852 return count;
856 static ssize_t
857 et61x251_store_i2c_reg(struct class_device* cd, const char* buf, size_t len)
859 struct et61x251_device* cam;
860 u8 index;
861 ssize_t count;
863 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
864 return -ERESTARTSYS;
866 cam = video_get_drvdata(to_video_device(cd));
867 if (!cam) {
868 mutex_unlock(&et61x251_sysfs_lock);
869 return -ENODEV;
872 index = et61x251_strtou8(buf, len, &count);
873 if (!count) {
874 mutex_unlock(&et61x251_sysfs_lock);
875 return -EINVAL;
878 cam->sysfs.i2c_reg = index;
880 DBG(2, "Moved sensor register index to 0x%02X", cam->sysfs.i2c_reg);
881 DBG(3, "Written bytes: %zd", count);
883 mutex_unlock(&et61x251_sysfs_lock);
885 return count;
889 static ssize_t et61x251_show_i2c_val(struct class_device* cd, char* buf)
891 struct et61x251_device* cam;
892 ssize_t count;
893 int val;
895 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
896 return -ERESTARTSYS;
898 cam = video_get_drvdata(to_video_device(cd));
899 if (!cam) {
900 mutex_unlock(&et61x251_sysfs_lock);
901 return -ENODEV;
904 if (!(cam->sensor.sysfs_ops & ET61X251_I2C_READ)) {
905 mutex_unlock(&et61x251_sysfs_lock);
906 return -ENOSYS;
909 if ((val = et61x251_i2c_read(cam, cam->sysfs.i2c_reg)) < 0) {
910 mutex_unlock(&et61x251_sysfs_lock);
911 return -EIO;
914 count = sprintf(buf, "%d\n", val);
916 DBG(3, "Read bytes: %zd", count);
918 mutex_unlock(&et61x251_sysfs_lock);
920 return count;
924 static ssize_t
925 et61x251_store_i2c_val(struct class_device* cd, const char* buf, size_t len)
927 struct et61x251_device* cam;
928 u8 value;
929 ssize_t count;
930 int err;
932 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
933 return -ERESTARTSYS;
935 cam = video_get_drvdata(to_video_device(cd));
936 if (!cam) {
937 mutex_unlock(&et61x251_sysfs_lock);
938 return -ENODEV;
941 if (!(cam->sensor.sysfs_ops & ET61X251_I2C_READ)) {
942 mutex_unlock(&et61x251_sysfs_lock);
943 return -ENOSYS;
946 value = et61x251_strtou8(buf, len, &count);
947 if (!count) {
948 mutex_unlock(&et61x251_sysfs_lock);
949 return -EINVAL;
952 err = et61x251_i2c_write(cam, cam->sysfs.i2c_reg, value);
953 if (err) {
954 mutex_unlock(&et61x251_sysfs_lock);
955 return -EIO;
958 DBG(2, "Written sensor reg. 0x%02X, val. 0x%02X",
959 cam->sysfs.i2c_reg, value);
960 DBG(3, "Written bytes: %zd", count);
962 mutex_unlock(&et61x251_sysfs_lock);
964 return count;
968 static CLASS_DEVICE_ATTR(reg, S_IRUGO | S_IWUSR,
969 et61x251_show_reg, et61x251_store_reg);
970 static CLASS_DEVICE_ATTR(val, S_IRUGO | S_IWUSR,
971 et61x251_show_val, et61x251_store_val);
972 static CLASS_DEVICE_ATTR(i2c_reg, S_IRUGO | S_IWUSR,
973 et61x251_show_i2c_reg, et61x251_store_i2c_reg);
974 static CLASS_DEVICE_ATTR(i2c_val, S_IRUGO | S_IWUSR,
975 et61x251_show_i2c_val, et61x251_store_i2c_val);
978 static int et61x251_create_sysfs(struct et61x251_device* cam)
980 struct video_device *v4ldev = cam->v4ldev;
981 int err = 0;
983 if ((err = video_device_create_file(v4ldev, &class_device_attr_reg)))
984 goto err_out;
985 if ((err = video_device_create_file(v4ldev, &class_device_attr_val)))
986 goto err_reg;
988 if (cam->sensor.sysfs_ops) {
989 if ((err = video_device_create_file(v4ldev,
990 &class_device_attr_i2c_reg)))
991 goto err_val;
992 if ((err = video_device_create_file(v4ldev,
993 &class_device_attr_i2c_val)))
994 goto err_i2c_reg;
997 err_i2c_reg:
998 if (cam->sensor.sysfs_ops)
999 video_device_remove_file(v4ldev, &class_device_attr_i2c_reg);
1000 err_val:
1001 video_device_remove_file(v4ldev, &class_device_attr_val);
1002 err_reg:
1003 video_device_remove_file(v4ldev, &class_device_attr_reg);
1004 err_out:
1005 return err;
1007 #endif /* CONFIG_VIDEO_ADV_DEBUG */
1009 /*****************************************************************************/
1011 static int
1012 et61x251_set_pix_format(struct et61x251_device* cam,
1013 struct v4l2_pix_format* pix)
1015 int r, err = 0;
1017 if ((r = et61x251_read_reg(cam, 0x12)) < 0)
1018 err += r;
1019 if (pix->pixelformat == V4L2_PIX_FMT_ET61X251)
1020 err += et61x251_write_reg(cam, r & 0xfd, 0x12);
1021 else
1022 err += et61x251_write_reg(cam, r | 0x02, 0x12);
1024 return err ? -EIO : 0;
1028 static int
1029 et61x251_set_compression(struct et61x251_device* cam,
1030 struct v4l2_jpegcompression* compression)
1032 int r, err = 0;
1034 if ((r = et61x251_read_reg(cam, 0x12)) < 0)
1035 err += r;
1036 if (compression->quality == 0)
1037 err += et61x251_write_reg(cam, r & 0xfb, 0x12);
1038 else
1039 err += et61x251_write_reg(cam, r | 0x04, 0x12);
1041 return err ? -EIO : 0;
1045 static int et61x251_set_scale(struct et61x251_device* cam, u8 scale)
1047 int r = 0, err = 0;
1049 r = et61x251_read_reg(cam, 0x12);
1050 if (r < 0)
1051 err += r;
1053 if (scale == 1)
1054 err += et61x251_write_reg(cam, r & ~0x01, 0x12);
1055 else if (scale == 2)
1056 err += et61x251_write_reg(cam, r | 0x01, 0x12);
1058 if (err)
1059 return -EIO;
1061 PDBGG("Scaling factor: %u", scale);
1063 return 0;
1067 static int
1068 et61x251_set_crop(struct et61x251_device* cam, struct v4l2_rect* rect)
1070 struct et61x251_sensor* s = &cam->sensor;
1071 u16 fmw_sx = (u16)(rect->left - s->cropcap.bounds.left +
1072 s->active_pixel.left),
1073 fmw_sy = (u16)(rect->top - s->cropcap.bounds.top +
1074 s->active_pixel.top),
1075 fmw_length = (u16)(rect->width),
1076 fmw_height = (u16)(rect->height);
1077 int err = 0;
1079 err += et61x251_write_reg(cam, fmw_sx & 0xff, 0x69);
1080 err += et61x251_write_reg(cam, fmw_sy & 0xff, 0x6a);
1081 err += et61x251_write_reg(cam, fmw_length & 0xff, 0x6b);
1082 err += et61x251_write_reg(cam, fmw_height & 0xff, 0x6c);
1083 err += et61x251_write_reg(cam, (fmw_sx >> 8) | ((fmw_sy & 0x300) >> 6)
1084 | ((fmw_length & 0x300) >> 4)
1085 | ((fmw_height & 0x300) >> 2), 0x6d);
1086 if (err)
1087 return -EIO;
1089 PDBGG("fmw_sx, fmw_sy, fmw_length, fmw_height: %u %u %u %u",
1090 fmw_sx, fmw_sy, fmw_length, fmw_height);
1092 return 0;
1096 static int et61x251_init(struct et61x251_device* cam)
1098 struct et61x251_sensor* s = &cam->sensor;
1099 struct v4l2_control ctrl;
1100 struct v4l2_queryctrl *qctrl;
1101 struct v4l2_rect* rect;
1102 u8 i = 0;
1103 int err = 0;
1105 if (!(cam->state & DEV_INITIALIZED)) {
1106 init_waitqueue_head(&cam->open);
1107 qctrl = s->qctrl;
1108 rect = &(s->cropcap.defrect);
1109 cam->compression.quality = ET61X251_COMPRESSION_QUALITY;
1110 } else { /* use current values */
1111 qctrl = s->_qctrl;
1112 rect = &(s->_rect);
1115 err += et61x251_set_scale(cam, rect->width / s->pix_format.width);
1116 err += et61x251_set_crop(cam, rect);
1117 if (err)
1118 return err;
1120 if (s->init) {
1121 err = s->init(cam);
1122 if (err) {
1123 DBG(3, "Sensor initialization failed");
1124 return err;
1128 err += et61x251_set_compression(cam, &cam->compression);
1129 err += et61x251_set_pix_format(cam, &s->pix_format);
1130 if (s->set_pix_format)
1131 err += s->set_pix_format(cam, &s->pix_format);
1132 if (err)
1133 return err;
1135 if (s->pix_format.pixelformat == V4L2_PIX_FMT_ET61X251)
1136 DBG(3, "Compressed video format is active, quality %d",
1137 cam->compression.quality);
1138 else
1139 DBG(3, "Uncompressed video format is active");
1141 if (s->set_crop)
1142 if ((err = s->set_crop(cam, rect))) {
1143 DBG(3, "set_crop() failed");
1144 return err;
1147 if (s->set_ctrl) {
1148 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1149 if (s->qctrl[i].id != 0 &&
1150 !(s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)) {
1151 ctrl.id = s->qctrl[i].id;
1152 ctrl.value = qctrl[i].default_value;
1153 err = s->set_ctrl(cam, &ctrl);
1154 if (err) {
1155 DBG(3, "Set %s control failed",
1156 s->qctrl[i].name);
1157 return err;
1159 DBG(3, "Image sensor supports '%s' control",
1160 s->qctrl[i].name);
1164 if (!(cam->state & DEV_INITIALIZED)) {
1165 mutex_init(&cam->fileop_mutex);
1166 spin_lock_init(&cam->queue_lock);
1167 init_waitqueue_head(&cam->wait_frame);
1168 init_waitqueue_head(&cam->wait_stream);
1169 cam->nreadbuffers = 2;
1170 memcpy(s->_qctrl, s->qctrl, sizeof(s->qctrl));
1171 memcpy(&(s->_rect), &(s->cropcap.defrect),
1172 sizeof(struct v4l2_rect));
1173 cam->state |= DEV_INITIALIZED;
1176 DBG(2, "Initialization succeeded");
1177 return 0;
1181 static void et61x251_release_resources(struct et61x251_device* cam)
1183 mutex_lock(&et61x251_sysfs_lock);
1185 DBG(2, "V4L2 device /dev/video%d deregistered", cam->v4ldev->minor);
1186 video_set_drvdata(cam->v4ldev, NULL);
1187 video_unregister_device(cam->v4ldev);
1189 mutex_unlock(&et61x251_sysfs_lock);
1191 kfree(cam->control_buffer);
1194 /*****************************************************************************/
1196 static int et61x251_open(struct inode* inode, struct file* filp)
1198 struct et61x251_device* cam;
1199 int err = 0;
1202 This is the only safe way to prevent race conditions with
1203 disconnect
1205 if (!down_read_trylock(&et61x251_disconnect))
1206 return -ERESTARTSYS;
1208 cam = video_get_drvdata(video_devdata(filp));
1210 if (mutex_lock_interruptible(&cam->dev_mutex)) {
1211 up_read(&et61x251_disconnect);
1212 return -ERESTARTSYS;
1215 if (cam->users) {
1216 DBG(2, "Device /dev/video%d is busy...", cam->v4ldev->minor);
1217 if ((filp->f_flags & O_NONBLOCK) ||
1218 (filp->f_flags & O_NDELAY)) {
1219 err = -EWOULDBLOCK;
1220 goto out;
1222 mutex_unlock(&cam->dev_mutex);
1223 err = wait_event_interruptible_exclusive(cam->open,
1224 cam->state & DEV_DISCONNECTED
1225 || !cam->users);
1226 if (err) {
1227 up_read(&et61x251_disconnect);
1228 return err;
1230 if (cam->state & DEV_DISCONNECTED) {
1231 up_read(&et61x251_disconnect);
1232 return -ENODEV;
1234 mutex_lock(&cam->dev_mutex);
1238 if (cam->state & DEV_MISCONFIGURED) {
1239 err = et61x251_init(cam);
1240 if (err) {
1241 DBG(1, "Initialization failed again. "
1242 "I will retry on next open().");
1243 goto out;
1245 cam->state &= ~DEV_MISCONFIGURED;
1248 if ((err = et61x251_start_transfer(cam)))
1249 goto out;
1251 filp->private_data = cam;
1252 cam->users++;
1253 cam->io = IO_NONE;
1254 cam->stream = STREAM_OFF;
1255 cam->nbuffers = 0;
1256 cam->frame_count = 0;
1257 et61x251_empty_framequeues(cam);
1259 DBG(3, "Video device /dev/video%d is open", cam->v4ldev->minor);
1261 out:
1262 mutex_unlock(&cam->dev_mutex);
1263 up_read(&et61x251_disconnect);
1264 return err;
1268 static int et61x251_release(struct inode* inode, struct file* filp)
1270 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp));
1272 mutex_lock(&cam->dev_mutex); /* prevent disconnect() to be called */
1274 et61x251_stop_transfer(cam);
1276 et61x251_release_buffers(cam);
1278 if (cam->state & DEV_DISCONNECTED) {
1279 et61x251_release_resources(cam);
1280 usb_put_dev(cam->usbdev);
1281 mutex_unlock(&cam->dev_mutex);
1282 kfree(cam);
1283 return 0;
1286 cam->users--;
1287 wake_up_interruptible_nr(&cam->open, 1);
1289 DBG(3, "Video device /dev/video%d closed", cam->v4ldev->minor);
1291 mutex_unlock(&cam->dev_mutex);
1293 return 0;
1297 static ssize_t
1298 et61x251_read(struct file* filp, char __user * buf,
1299 size_t count, loff_t* f_pos)
1301 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp));
1302 struct et61x251_frame_t* f, * i;
1303 unsigned long lock_flags;
1304 long timeout;
1305 int err = 0;
1307 if (mutex_lock_interruptible(&cam->fileop_mutex))
1308 return -ERESTARTSYS;
1310 if (cam->state & DEV_DISCONNECTED) {
1311 DBG(1, "Device not present");
1312 mutex_unlock(&cam->fileop_mutex);
1313 return -ENODEV;
1316 if (cam->state & DEV_MISCONFIGURED) {
1317 DBG(1, "The camera is misconfigured. Close and open it "
1318 "again.");
1319 mutex_unlock(&cam->fileop_mutex);
1320 return -EIO;
1323 if (cam->io == IO_MMAP) {
1324 DBG(3, "Close and open the device again to choose the read "
1325 "method");
1326 mutex_unlock(&cam->fileop_mutex);
1327 return -EINVAL;
1330 if (cam->io == IO_NONE) {
1331 if (!et61x251_request_buffers(cam, cam->nreadbuffers,
1332 IO_READ)) {
1333 DBG(1, "read() failed, not enough memory");
1334 mutex_unlock(&cam->fileop_mutex);
1335 return -ENOMEM;
1337 cam->io = IO_READ;
1338 cam->stream = STREAM_ON;
1341 if (list_empty(&cam->inqueue)) {
1342 if (!list_empty(&cam->outqueue))
1343 et61x251_empty_framequeues(cam);
1344 et61x251_queue_unusedframes(cam);
1347 if (!count) {
1348 mutex_unlock(&cam->fileop_mutex);
1349 return 0;
1352 if (list_empty(&cam->outqueue)) {
1353 if (filp->f_flags & O_NONBLOCK) {
1354 mutex_unlock(&cam->fileop_mutex);
1355 return -EAGAIN;
1357 timeout = wait_event_interruptible_timeout
1358 ( cam->wait_frame,
1359 (!list_empty(&cam->outqueue)) ||
1360 (cam->state & DEV_DISCONNECTED) ||
1361 (cam->state & DEV_MISCONFIGURED),
1362 cam->module_param.frame_timeout *
1363 1000 * msecs_to_jiffies(1) );
1364 if (timeout < 0) {
1365 mutex_unlock(&cam->fileop_mutex);
1366 return timeout;
1368 if (cam->state & DEV_DISCONNECTED) {
1369 mutex_unlock(&cam->fileop_mutex);
1370 return -ENODEV;
1372 if (!timeout || (cam->state & DEV_MISCONFIGURED)) {
1373 mutex_unlock(&cam->fileop_mutex);
1374 return -EIO;
1378 f = list_entry(cam->outqueue.prev, struct et61x251_frame_t, frame);
1380 if (count > f->buf.bytesused)
1381 count = f->buf.bytesused;
1383 if (copy_to_user(buf, f->bufmem, count)) {
1384 err = -EFAULT;
1385 goto exit;
1387 *f_pos += count;
1389 exit:
1390 spin_lock_irqsave(&cam->queue_lock, lock_flags);
1391 list_for_each_entry(i, &cam->outqueue, frame)
1392 i->state = F_UNUSED;
1393 INIT_LIST_HEAD(&cam->outqueue);
1394 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1396 et61x251_queue_unusedframes(cam);
1398 PDBGG("Frame #%lu, bytes read: %zu",
1399 (unsigned long)f->buf.index, count);
1401 mutex_unlock(&cam->fileop_mutex);
1403 return err ? err : count;
1407 static unsigned int et61x251_poll(struct file *filp, poll_table *wait)
1409 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp));
1410 struct et61x251_frame_t* f;
1411 unsigned long lock_flags;
1412 unsigned int mask = 0;
1414 if (mutex_lock_interruptible(&cam->fileop_mutex))
1415 return POLLERR;
1417 if (cam->state & DEV_DISCONNECTED) {
1418 DBG(1, "Device not present");
1419 goto error;
1422 if (cam->state & DEV_MISCONFIGURED) {
1423 DBG(1, "The camera is misconfigured. Close and open it "
1424 "again.");
1425 goto error;
1428 if (cam->io == IO_NONE) {
1429 if (!et61x251_request_buffers(cam, cam->nreadbuffers,
1430 IO_READ)) {
1431 DBG(1, "poll() failed, not enough memory");
1432 goto error;
1434 cam->io = IO_READ;
1435 cam->stream = STREAM_ON;
1438 if (cam->io == IO_READ) {
1439 spin_lock_irqsave(&cam->queue_lock, lock_flags);
1440 list_for_each_entry(f, &cam->outqueue, frame)
1441 f->state = F_UNUSED;
1442 INIT_LIST_HEAD(&cam->outqueue);
1443 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1444 et61x251_queue_unusedframes(cam);
1447 poll_wait(filp, &cam->wait_frame, wait);
1449 if (!list_empty(&cam->outqueue))
1450 mask |= POLLIN | POLLRDNORM;
1452 mutex_unlock(&cam->fileop_mutex);
1454 return mask;
1456 error:
1457 mutex_unlock(&cam->fileop_mutex);
1458 return POLLERR;
1462 static void et61x251_vm_open(struct vm_area_struct* vma)
1464 struct et61x251_frame_t* f = vma->vm_private_data;
1465 f->vma_use_count++;
1469 static void et61x251_vm_close(struct vm_area_struct* vma)
1471 /* NOTE: buffers are not freed here */
1472 struct et61x251_frame_t* f = vma->vm_private_data;
1473 f->vma_use_count--;
1477 static struct vm_operations_struct et61x251_vm_ops = {
1478 .open = et61x251_vm_open,
1479 .close = et61x251_vm_close,
1483 static int et61x251_mmap(struct file* filp, struct vm_area_struct *vma)
1485 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp));
1486 unsigned long size = vma->vm_end - vma->vm_start,
1487 start = vma->vm_start;
1488 void *pos;
1489 u32 i;
1491 if (mutex_lock_interruptible(&cam->fileop_mutex))
1492 return -ERESTARTSYS;
1494 if (cam->state & DEV_DISCONNECTED) {
1495 DBG(1, "Device not present");
1496 mutex_unlock(&cam->fileop_mutex);
1497 return -ENODEV;
1500 if (cam->state & DEV_MISCONFIGURED) {
1501 DBG(1, "The camera is misconfigured. Close and open it "
1502 "again.");
1503 mutex_unlock(&cam->fileop_mutex);
1504 return -EIO;
1507 if (cam->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) ||
1508 size != PAGE_ALIGN(cam->frame[0].buf.length)) {
1509 mutex_unlock(&cam->fileop_mutex);
1510 return -EINVAL;
1513 for (i = 0; i < cam->nbuffers; i++) {
1514 if ((cam->frame[i].buf.m.offset>>PAGE_SHIFT) == vma->vm_pgoff)
1515 break;
1517 if (i == cam->nbuffers) {
1518 mutex_unlock(&cam->fileop_mutex);
1519 return -EINVAL;
1522 vma->vm_flags |= VM_IO;
1523 vma->vm_flags |= VM_RESERVED;
1525 pos = cam->frame[i].bufmem;
1526 while (size > 0) { /* size is page-aligned */
1527 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
1528 mutex_unlock(&cam->fileop_mutex);
1529 return -EAGAIN;
1531 start += PAGE_SIZE;
1532 pos += PAGE_SIZE;
1533 size -= PAGE_SIZE;
1536 vma->vm_ops = &et61x251_vm_ops;
1537 vma->vm_private_data = &cam->frame[i];
1539 et61x251_vm_open(vma);
1541 mutex_unlock(&cam->fileop_mutex);
1543 return 0;
1546 /*****************************************************************************/
1548 static int
1549 et61x251_vidioc_querycap(struct et61x251_device* cam, void __user * arg)
1551 struct v4l2_capability cap = {
1552 .driver = "et61x251",
1553 .version = ET61X251_MODULE_VERSION_CODE,
1554 .capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
1555 V4L2_CAP_STREAMING,
1558 strlcpy(cap.card, cam->v4ldev->name, sizeof(cap.card));
1559 if (usb_make_path(cam->usbdev, cap.bus_info, sizeof(cap.bus_info)) < 0)
1560 strlcpy(cap.bus_info, cam->usbdev->dev.bus_id,
1561 sizeof(cap.bus_info));
1563 if (copy_to_user(arg, &cap, sizeof(cap)))
1564 return -EFAULT;
1566 return 0;
1570 static int
1571 et61x251_vidioc_enuminput(struct et61x251_device* cam, void __user * arg)
1573 struct v4l2_input i;
1575 if (copy_from_user(&i, arg, sizeof(i)))
1576 return -EFAULT;
1578 if (i.index)
1579 return -EINVAL;
1581 memset(&i, 0, sizeof(i));
1582 strcpy(i.name, "Camera");
1583 i.type = V4L2_INPUT_TYPE_CAMERA;
1585 if (copy_to_user(arg, &i, sizeof(i)))
1586 return -EFAULT;
1588 return 0;
1592 static int
1593 et61x251_vidioc_g_input(struct et61x251_device* cam, void __user * arg)
1595 int index = 0;
1597 if (copy_to_user(arg, &index, sizeof(index)))
1598 return -EFAULT;
1600 return 0;
1604 static int
1605 et61x251_vidioc_s_input(struct et61x251_device* cam, void __user * arg)
1607 int index;
1609 if (copy_from_user(&index, arg, sizeof(index)))
1610 return -EFAULT;
1612 if (index != 0)
1613 return -EINVAL;
1615 return 0;
1619 static int
1620 et61x251_vidioc_query_ctrl(struct et61x251_device* cam, void __user * arg)
1622 struct et61x251_sensor* s = &cam->sensor;
1623 struct v4l2_queryctrl qc;
1624 u8 i;
1626 if (copy_from_user(&qc, arg, sizeof(qc)))
1627 return -EFAULT;
1629 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1630 if (qc.id && qc.id == s->qctrl[i].id) {
1631 memcpy(&qc, &(s->qctrl[i]), sizeof(qc));
1632 if (copy_to_user(arg, &qc, sizeof(qc)))
1633 return -EFAULT;
1634 return 0;
1637 return -EINVAL;
1641 static int
1642 et61x251_vidioc_g_ctrl(struct et61x251_device* cam, void __user * arg)
1644 struct et61x251_sensor* s = &cam->sensor;
1645 struct v4l2_control ctrl;
1646 int err = 0;
1647 u8 i;
1649 if (!s->get_ctrl && !s->set_ctrl)
1650 return -EINVAL;
1652 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1653 return -EFAULT;
1655 if (!s->get_ctrl) {
1656 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1657 if (ctrl.id == s->qctrl[i].id) {
1658 ctrl.value = s->_qctrl[i].default_value;
1659 goto exit;
1661 return -EINVAL;
1662 } else
1663 err = s->get_ctrl(cam, &ctrl);
1665 exit:
1666 if (copy_to_user(arg, &ctrl, sizeof(ctrl)))
1667 return -EFAULT;
1669 return err;
1673 static int
1674 et61x251_vidioc_s_ctrl(struct et61x251_device* cam, void __user * arg)
1676 struct et61x251_sensor* s = &cam->sensor;
1677 struct v4l2_control ctrl;
1678 u8 i;
1679 int err = 0;
1681 if (!s->set_ctrl)
1682 return -EINVAL;
1684 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1685 return -EFAULT;
1687 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1688 if (ctrl.id == s->qctrl[i].id) {
1689 if (s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)
1690 return -EINVAL;
1691 if (ctrl.value < s->qctrl[i].minimum ||
1692 ctrl.value > s->qctrl[i].maximum)
1693 return -ERANGE;
1694 ctrl.value -= ctrl.value % s->qctrl[i].step;
1695 break;
1698 if ((err = s->set_ctrl(cam, &ctrl)))
1699 return err;
1701 s->_qctrl[i].default_value = ctrl.value;
1703 return 0;
1707 static int
1708 et61x251_vidioc_cropcap(struct et61x251_device* cam, void __user * arg)
1710 struct v4l2_cropcap* cc = &(cam->sensor.cropcap);
1712 cc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1713 cc->pixelaspect.numerator = 1;
1714 cc->pixelaspect.denominator = 1;
1716 if (copy_to_user(arg, cc, sizeof(*cc)))
1717 return -EFAULT;
1719 return 0;
1723 static int
1724 et61x251_vidioc_g_crop(struct et61x251_device* cam, void __user * arg)
1726 struct et61x251_sensor* s = &cam->sensor;
1727 struct v4l2_crop crop = {
1728 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
1731 memcpy(&(crop.c), &(s->_rect), sizeof(struct v4l2_rect));
1733 if (copy_to_user(arg, &crop, sizeof(crop)))
1734 return -EFAULT;
1736 return 0;
1740 static int
1741 et61x251_vidioc_s_crop(struct et61x251_device* cam, void __user * arg)
1743 struct et61x251_sensor* s = &cam->sensor;
1744 struct v4l2_crop crop;
1745 struct v4l2_rect* rect;
1746 struct v4l2_rect* bounds = &(s->cropcap.bounds);
1747 struct v4l2_pix_format* pix_format = &(s->pix_format);
1748 u8 scale;
1749 const enum et61x251_stream_state stream = cam->stream;
1750 const u32 nbuffers = cam->nbuffers;
1751 u32 i;
1752 int err = 0;
1754 if (copy_from_user(&crop, arg, sizeof(crop)))
1755 return -EFAULT;
1757 rect = &(crop.c);
1759 if (crop.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1760 return -EINVAL;
1762 if (cam->module_param.force_munmap)
1763 for (i = 0; i < cam->nbuffers; i++)
1764 if (cam->frame[i].vma_use_count) {
1765 DBG(3, "VIDIOC_S_CROP failed. "
1766 "Unmap the buffers first.");
1767 return -EINVAL;
1770 /* Preserve R,G or B origin */
1771 rect->left = (s->_rect.left & 1L) ? rect->left | 1L : rect->left & ~1L;
1772 rect->top = (s->_rect.top & 1L) ? rect->top | 1L : rect->top & ~1L;
1774 if (rect->width < 16)
1775 rect->width = 16;
1776 if (rect->height < 16)
1777 rect->height = 16;
1778 if (rect->width > bounds->width)
1779 rect->width = bounds->width;
1780 if (rect->height > bounds->height)
1781 rect->height = bounds->height;
1782 if (rect->left < bounds->left)
1783 rect->left = bounds->left;
1784 if (rect->top < bounds->top)
1785 rect->top = bounds->top;
1786 if (rect->left + rect->width > bounds->left + bounds->width)
1787 rect->left = bounds->left+bounds->width - rect->width;
1788 if (rect->top + rect->height > bounds->top + bounds->height)
1789 rect->top = bounds->top+bounds->height - rect->height;
1791 rect->width &= ~15L;
1792 rect->height &= ~15L;
1794 if (ET61X251_PRESERVE_IMGSCALE) {
1795 /* Calculate the actual scaling factor */
1796 u32 a, b;
1797 a = rect->width * rect->height;
1798 b = pix_format->width * pix_format->height;
1799 scale = b ? (u8)((a / b) < 4 ? 1 : 2) : 1;
1800 } else
1801 scale = 1;
1803 if (cam->stream == STREAM_ON)
1804 if ((err = et61x251_stream_interrupt(cam)))
1805 return err;
1807 if (copy_to_user(arg, &crop, sizeof(crop))) {
1808 cam->stream = stream;
1809 return -EFAULT;
1812 if (cam->module_param.force_munmap || cam->io == IO_READ)
1813 et61x251_release_buffers(cam);
1815 err = et61x251_set_crop(cam, rect);
1816 if (s->set_crop)
1817 err += s->set_crop(cam, rect);
1818 err += et61x251_set_scale(cam, scale);
1820 if (err) { /* atomic, no rollback in ioctl() */
1821 cam->state |= DEV_MISCONFIGURED;
1822 DBG(1, "VIDIOC_S_CROP failed because of hardware problems. To "
1823 "use the camera, close and open /dev/video%d again.",
1824 cam->v4ldev->minor);
1825 return -EIO;
1828 s->pix_format.width = rect->width/scale;
1829 s->pix_format.height = rect->height/scale;
1830 memcpy(&(s->_rect), rect, sizeof(*rect));
1832 if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
1833 nbuffers != et61x251_request_buffers(cam, nbuffers, cam->io)) {
1834 cam->state |= DEV_MISCONFIGURED;
1835 DBG(1, "VIDIOC_S_CROP failed because of not enough memory. To "
1836 "use the camera, close and open /dev/video%d again.",
1837 cam->v4ldev->minor);
1838 return -ENOMEM;
1841 if (cam->io == IO_READ)
1842 et61x251_empty_framequeues(cam);
1843 else if (cam->module_param.force_munmap)
1844 et61x251_requeue_outqueue(cam);
1846 cam->stream = stream;
1848 return 0;
1852 static int
1853 et61x251_vidioc_enum_framesizes(struct et61x251_device* cam, void __user * arg)
1855 struct v4l2_frmsizeenum frmsize;
1857 if (copy_from_user(&frmsize, arg, sizeof(frmsize)))
1858 return -EFAULT;
1860 if (frmsize.index != 0)
1861 return -EINVAL;
1863 if (frmsize.pixel_format != V4L2_PIX_FMT_ET61X251 &&
1864 frmsize.pixel_format != V4L2_PIX_FMT_SBGGR8)
1865 return -EINVAL;
1867 frmsize.type = V4L2_FRMSIZE_TYPE_STEPWISE;
1868 frmsize.stepwise.min_width = frmsize.stepwise.step_width = 16;
1869 frmsize.stepwise.min_height = frmsize.stepwise.step_height = 16;
1870 frmsize.stepwise.max_width = cam->sensor.cropcap.bounds.width;
1871 frmsize.stepwise.max_height = cam->sensor.cropcap.bounds.height;
1872 memset(&frmsize.reserved, 0, sizeof(frmsize.reserved));
1874 if (copy_to_user(arg, &frmsize, sizeof(frmsize)))
1875 return -EFAULT;
1877 return 0;
1881 static int
1882 et61x251_vidioc_enum_fmt(struct et61x251_device* cam, void __user * arg)
1884 struct v4l2_fmtdesc fmtd;
1886 if (copy_from_user(&fmtd, arg, sizeof(fmtd)))
1887 return -EFAULT;
1889 if (fmtd.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1890 return -EINVAL;
1892 if (fmtd.index == 0) {
1893 strcpy(fmtd.description, "bayer rgb");
1894 fmtd.pixelformat = V4L2_PIX_FMT_SBGGR8;
1895 } else if (fmtd.index == 1) {
1896 strcpy(fmtd.description, "compressed");
1897 fmtd.pixelformat = V4L2_PIX_FMT_ET61X251;
1898 fmtd.flags = V4L2_FMT_FLAG_COMPRESSED;
1899 } else
1900 return -EINVAL;
1902 fmtd.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1903 memset(&fmtd.reserved, 0, sizeof(fmtd.reserved));
1905 if (copy_to_user(arg, &fmtd, sizeof(fmtd)))
1906 return -EFAULT;
1908 return 0;
1912 static int
1913 et61x251_vidioc_g_fmt(struct et61x251_device* cam, void __user * arg)
1915 struct v4l2_format format;
1916 struct v4l2_pix_format* pfmt = &(cam->sensor.pix_format);
1918 if (copy_from_user(&format, arg, sizeof(format)))
1919 return -EFAULT;
1921 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1922 return -EINVAL;
1924 pfmt->bytesperline = (pfmt->pixelformat==V4L2_PIX_FMT_ET61X251)
1925 ? 0 : (pfmt->width * pfmt->priv) / 8;
1926 pfmt->sizeimage = pfmt->height * ((pfmt->width*pfmt->priv)/8);
1927 pfmt->field = V4L2_FIELD_NONE;
1928 memcpy(&(format.fmt.pix), pfmt, sizeof(*pfmt));
1930 if (copy_to_user(arg, &format, sizeof(format)))
1931 return -EFAULT;
1933 return 0;
1937 static int
1938 et61x251_vidioc_try_s_fmt(struct et61x251_device* cam, unsigned int cmd,
1939 void __user * arg)
1941 struct et61x251_sensor* s = &cam->sensor;
1942 struct v4l2_format format;
1943 struct v4l2_pix_format* pix;
1944 struct v4l2_pix_format* pfmt = &(s->pix_format);
1945 struct v4l2_rect* bounds = &(s->cropcap.bounds);
1946 struct v4l2_rect rect;
1947 u8 scale;
1948 const enum et61x251_stream_state stream = cam->stream;
1949 const u32 nbuffers = cam->nbuffers;
1950 u32 i;
1951 int err = 0;
1953 if (copy_from_user(&format, arg, sizeof(format)))
1954 return -EFAULT;
1956 pix = &(format.fmt.pix);
1958 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1959 return -EINVAL;
1961 memcpy(&rect, &(s->_rect), sizeof(rect));
1963 { /* calculate the actual scaling factor */
1964 u32 a, b;
1965 a = rect.width * rect.height;
1966 b = pix->width * pix->height;
1967 scale = b ? (u8)((a / b) < 4 ? 1 : 2) : 1;
1970 rect.width = scale * pix->width;
1971 rect.height = scale * pix->height;
1973 if (rect.width < 16)
1974 rect.width = 16;
1975 if (rect.height < 16)
1976 rect.height = 16;
1977 if (rect.width > bounds->left + bounds->width - rect.left)
1978 rect.width = bounds->left + bounds->width - rect.left;
1979 if (rect.height > bounds->top + bounds->height - rect.top)
1980 rect.height = bounds->top + bounds->height - rect.top;
1982 rect.width &= ~15L;
1983 rect.height &= ~15L;
1985 { /* adjust the scaling factor */
1986 u32 a, b;
1987 a = rect.width * rect.height;
1988 b = pix->width * pix->height;
1989 scale = b ? (u8)((a / b) < 4 ? 1 : 2) : 1;
1992 pix->width = rect.width / scale;
1993 pix->height = rect.height / scale;
1995 if (pix->pixelformat != V4L2_PIX_FMT_ET61X251 &&
1996 pix->pixelformat != V4L2_PIX_FMT_SBGGR8)
1997 pix->pixelformat = pfmt->pixelformat;
1998 pix->priv = pfmt->priv; /* bpp */
1999 pix->colorspace = pfmt->colorspace;
2000 pix->bytesperline = (pix->pixelformat == V4L2_PIX_FMT_ET61X251)
2001 ? 0 : (pix->width * pix->priv) / 8;
2002 pix->sizeimage = pix->height * ((pix->width * pix->priv) / 8);
2003 pix->field = V4L2_FIELD_NONE;
2005 if (cmd == VIDIOC_TRY_FMT) {
2006 if (copy_to_user(arg, &format, sizeof(format)))
2007 return -EFAULT;
2008 return 0;
2011 if (cam->module_param.force_munmap)
2012 for (i = 0; i < cam->nbuffers; i++)
2013 if (cam->frame[i].vma_use_count) {
2014 DBG(3, "VIDIOC_S_FMT failed. "
2015 "Unmap the buffers first.");
2016 return -EINVAL;
2019 if (cam->stream == STREAM_ON)
2020 if ((err = et61x251_stream_interrupt(cam)))
2021 return err;
2023 if (copy_to_user(arg, &format, sizeof(format))) {
2024 cam->stream = stream;
2025 return -EFAULT;
2028 if (cam->module_param.force_munmap || cam->io == IO_READ)
2029 et61x251_release_buffers(cam);
2031 err += et61x251_set_pix_format(cam, pix);
2032 err += et61x251_set_crop(cam, &rect);
2033 if (s->set_pix_format)
2034 err += s->set_pix_format(cam, pix);
2035 if (s->set_crop)
2036 err += s->set_crop(cam, &rect);
2037 err += et61x251_set_scale(cam, scale);
2039 if (err) { /* atomic, no rollback in ioctl() */
2040 cam->state |= DEV_MISCONFIGURED;
2041 DBG(1, "VIDIOC_S_FMT failed because of hardware problems. To "
2042 "use the camera, close and open /dev/video%d again.",
2043 cam->v4ldev->minor);
2044 return -EIO;
2047 memcpy(pfmt, pix, sizeof(*pix));
2048 memcpy(&(s->_rect), &rect, sizeof(rect));
2050 if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
2051 nbuffers != et61x251_request_buffers(cam, nbuffers, cam->io)) {
2052 cam->state |= DEV_MISCONFIGURED;
2053 DBG(1, "VIDIOC_S_FMT failed because of not enough memory. To "
2054 "use the camera, close and open /dev/video%d again.",
2055 cam->v4ldev->minor);
2056 return -ENOMEM;
2059 if (cam->io == IO_READ)
2060 et61x251_empty_framequeues(cam);
2061 else if (cam->module_param.force_munmap)
2062 et61x251_requeue_outqueue(cam);
2064 cam->stream = stream;
2066 return 0;
2070 static int
2071 et61x251_vidioc_g_jpegcomp(struct et61x251_device* cam, void __user * arg)
2073 if (copy_to_user(arg, &cam->compression,
2074 sizeof(cam->compression)))
2075 return -EFAULT;
2077 return 0;
2081 static int
2082 et61x251_vidioc_s_jpegcomp(struct et61x251_device* cam, void __user * arg)
2084 struct v4l2_jpegcompression jc;
2085 const enum et61x251_stream_state stream = cam->stream;
2086 int err = 0;
2088 if (copy_from_user(&jc, arg, sizeof(jc)))
2089 return -EFAULT;
2091 if (jc.quality != 0 && jc.quality != 1)
2092 return -EINVAL;
2094 if (cam->stream == STREAM_ON)
2095 if ((err = et61x251_stream_interrupt(cam)))
2096 return err;
2098 err += et61x251_set_compression(cam, &jc);
2099 if (err) { /* atomic, no rollback in ioctl() */
2100 cam->state |= DEV_MISCONFIGURED;
2101 DBG(1, "VIDIOC_S_JPEGCOMP failed because of hardware "
2102 "problems. To use the camera, close and open "
2103 "/dev/video%d again.", cam->v4ldev->minor);
2104 return -EIO;
2107 cam->compression.quality = jc.quality;
2109 cam->stream = stream;
2111 return 0;
2115 static int
2116 et61x251_vidioc_reqbufs(struct et61x251_device* cam, void __user * arg)
2118 struct v4l2_requestbuffers rb;
2119 u32 i;
2120 int err;
2122 if (copy_from_user(&rb, arg, sizeof(rb)))
2123 return -EFAULT;
2125 if (rb.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2126 rb.memory != V4L2_MEMORY_MMAP)
2127 return -EINVAL;
2129 if (cam->io == IO_READ) {
2130 DBG(3, "Close and open the device again to choose the mmap "
2131 "I/O method");
2132 return -EINVAL;
2135 for (i = 0; i < cam->nbuffers; i++)
2136 if (cam->frame[i].vma_use_count) {
2137 DBG(3, "VIDIOC_REQBUFS failed. "
2138 "Previous buffers are still mapped.");
2139 return -EINVAL;
2142 if (cam->stream == STREAM_ON)
2143 if ((err = et61x251_stream_interrupt(cam)))
2144 return err;
2146 et61x251_empty_framequeues(cam);
2148 et61x251_release_buffers(cam);
2149 if (rb.count)
2150 rb.count = et61x251_request_buffers(cam, rb.count, IO_MMAP);
2152 if (copy_to_user(arg, &rb, sizeof(rb))) {
2153 et61x251_release_buffers(cam);
2154 cam->io = IO_NONE;
2155 return -EFAULT;
2158 cam->io = rb.count ? IO_MMAP : IO_NONE;
2160 return 0;
2164 static int
2165 et61x251_vidioc_querybuf(struct et61x251_device* cam, void __user * arg)
2167 struct v4l2_buffer b;
2169 if (copy_from_user(&b, arg, sizeof(b)))
2170 return -EFAULT;
2172 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2173 b.index >= cam->nbuffers || cam->io != IO_MMAP)
2174 return -EINVAL;
2176 memcpy(&b, &cam->frame[b.index].buf, sizeof(b));
2178 if (cam->frame[b.index].vma_use_count)
2179 b.flags |= V4L2_BUF_FLAG_MAPPED;
2181 if (cam->frame[b.index].state == F_DONE)
2182 b.flags |= V4L2_BUF_FLAG_DONE;
2183 else if (cam->frame[b.index].state != F_UNUSED)
2184 b.flags |= V4L2_BUF_FLAG_QUEUED;
2186 if (copy_to_user(arg, &b, sizeof(b)))
2187 return -EFAULT;
2189 return 0;
2193 static int
2194 et61x251_vidioc_qbuf(struct et61x251_device* cam, void __user * arg)
2196 struct v4l2_buffer b;
2197 unsigned long lock_flags;
2199 if (copy_from_user(&b, arg, sizeof(b)))
2200 return -EFAULT;
2202 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2203 b.index >= cam->nbuffers || cam->io != IO_MMAP)
2204 return -EINVAL;
2206 if (cam->frame[b.index].state != F_UNUSED)
2207 return -EINVAL;
2209 cam->frame[b.index].state = F_QUEUED;
2211 spin_lock_irqsave(&cam->queue_lock, lock_flags);
2212 list_add_tail(&cam->frame[b.index].frame, &cam->inqueue);
2213 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2215 PDBGG("Frame #%lu queued", (unsigned long)b.index);
2217 return 0;
2221 static int
2222 et61x251_vidioc_dqbuf(struct et61x251_device* cam, struct file* filp,
2223 void __user * arg)
2225 struct v4l2_buffer b;
2226 struct et61x251_frame_t *f;
2227 unsigned long lock_flags;
2228 long timeout;
2230 if (copy_from_user(&b, arg, sizeof(b)))
2231 return -EFAULT;
2233 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io!= IO_MMAP)
2234 return -EINVAL;
2236 if (list_empty(&cam->outqueue)) {
2237 if (cam->stream == STREAM_OFF)
2238 return -EINVAL;
2239 if (filp->f_flags & O_NONBLOCK)
2240 return -EAGAIN;
2241 timeout = wait_event_interruptible_timeout
2242 ( cam->wait_frame,
2243 (!list_empty(&cam->outqueue)) ||
2244 (cam->state & DEV_DISCONNECTED) ||
2245 (cam->state & DEV_MISCONFIGURED),
2246 cam->module_param.frame_timeout *
2247 1000 * msecs_to_jiffies(1) );
2248 if (timeout < 0)
2249 return timeout;
2250 if (cam->state & DEV_DISCONNECTED)
2251 return -ENODEV;
2252 if (!timeout || (cam->state & DEV_MISCONFIGURED))
2253 return -EIO;
2256 spin_lock_irqsave(&cam->queue_lock, lock_flags);
2257 f = list_entry(cam->outqueue.next, struct et61x251_frame_t, frame);
2258 list_del(cam->outqueue.next);
2259 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2261 f->state = F_UNUSED;
2263 memcpy(&b, &f->buf, sizeof(b));
2264 if (f->vma_use_count)
2265 b.flags |= V4L2_BUF_FLAG_MAPPED;
2267 if (copy_to_user(arg, &b, sizeof(b)))
2268 return -EFAULT;
2270 PDBGG("Frame #%lu dequeued", (unsigned long)f->buf.index);
2272 return 0;
2276 static int
2277 et61x251_vidioc_streamon(struct et61x251_device* cam, void __user * arg)
2279 int type;
2281 if (copy_from_user(&type, arg, sizeof(type)))
2282 return -EFAULT;
2284 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2285 return -EINVAL;
2287 if (list_empty(&cam->inqueue))
2288 return -EINVAL;
2290 cam->stream = STREAM_ON;
2292 DBG(3, "Stream on");
2294 return 0;
2298 static int
2299 et61x251_vidioc_streamoff(struct et61x251_device* cam, void __user * arg)
2301 int type, err;
2303 if (copy_from_user(&type, arg, sizeof(type)))
2304 return -EFAULT;
2306 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2307 return -EINVAL;
2309 if (cam->stream == STREAM_ON)
2310 if ((err = et61x251_stream_interrupt(cam)))
2311 return err;
2313 et61x251_empty_framequeues(cam);
2315 DBG(3, "Stream off");
2317 return 0;
2321 static int
2322 et61x251_vidioc_g_parm(struct et61x251_device* cam, void __user * arg)
2324 struct v4l2_streamparm sp;
2326 if (copy_from_user(&sp, arg, sizeof(sp)))
2327 return -EFAULT;
2329 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2330 return -EINVAL;
2332 sp.parm.capture.extendedmode = 0;
2333 sp.parm.capture.readbuffers = cam->nreadbuffers;
2335 if (copy_to_user(arg, &sp, sizeof(sp)))
2336 return -EFAULT;
2338 return 0;
2342 static int
2343 et61x251_vidioc_s_parm(struct et61x251_device* cam, void __user * arg)
2345 struct v4l2_streamparm sp;
2347 if (copy_from_user(&sp, arg, sizeof(sp)))
2348 return -EFAULT;
2350 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2351 return -EINVAL;
2353 sp.parm.capture.extendedmode = 0;
2355 if (sp.parm.capture.readbuffers == 0)
2356 sp.parm.capture.readbuffers = cam->nreadbuffers;
2358 if (sp.parm.capture.readbuffers > ET61X251_MAX_FRAMES)
2359 sp.parm.capture.readbuffers = ET61X251_MAX_FRAMES;
2361 if (copy_to_user(arg, &sp, sizeof(sp)))
2362 return -EFAULT;
2364 cam->nreadbuffers = sp.parm.capture.readbuffers;
2366 return 0;
2370 static int et61x251_ioctl_v4l2(struct inode* inode, struct file* filp,
2371 unsigned int cmd, void __user * arg)
2373 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp));
2375 switch (cmd) {
2377 case VIDIOC_QUERYCAP:
2378 return et61x251_vidioc_querycap(cam, arg);
2380 case VIDIOC_ENUMINPUT:
2381 return et61x251_vidioc_enuminput(cam, arg);
2383 case VIDIOC_G_INPUT:
2384 return et61x251_vidioc_g_input(cam, arg);
2386 case VIDIOC_S_INPUT:
2387 return et61x251_vidioc_s_input(cam, arg);
2389 case VIDIOC_QUERYCTRL:
2390 return et61x251_vidioc_query_ctrl(cam, arg);
2392 case VIDIOC_G_CTRL:
2393 return et61x251_vidioc_g_ctrl(cam, arg);
2395 case VIDIOC_S_CTRL:
2396 return et61x251_vidioc_s_ctrl(cam, arg);
2398 case VIDIOC_CROPCAP:
2399 return et61x251_vidioc_cropcap(cam, arg);
2401 case VIDIOC_G_CROP:
2402 return et61x251_vidioc_g_crop(cam, arg);
2404 case VIDIOC_S_CROP:
2405 return et61x251_vidioc_s_crop(cam, arg);
2407 case VIDIOC_ENUM_FMT:
2408 return et61x251_vidioc_enum_fmt(cam, arg);
2410 case VIDIOC_G_FMT:
2411 return et61x251_vidioc_g_fmt(cam, arg);
2413 case VIDIOC_TRY_FMT:
2414 case VIDIOC_S_FMT:
2415 return et61x251_vidioc_try_s_fmt(cam, cmd, arg);
2417 case VIDIOC_ENUM_FRAMESIZES:
2418 return et61x251_vidioc_enum_framesizes(cam, arg);
2420 case VIDIOC_G_JPEGCOMP:
2421 return et61x251_vidioc_g_jpegcomp(cam, arg);
2423 case VIDIOC_S_JPEGCOMP:
2424 return et61x251_vidioc_s_jpegcomp(cam, arg);
2426 case VIDIOC_REQBUFS:
2427 return et61x251_vidioc_reqbufs(cam, arg);
2429 case VIDIOC_QUERYBUF:
2430 return et61x251_vidioc_querybuf(cam, arg);
2432 case VIDIOC_QBUF:
2433 return et61x251_vidioc_qbuf(cam, arg);
2435 case VIDIOC_DQBUF:
2436 return et61x251_vidioc_dqbuf(cam, filp, arg);
2438 case VIDIOC_STREAMON:
2439 return et61x251_vidioc_streamon(cam, arg);
2441 case VIDIOC_STREAMOFF:
2442 return et61x251_vidioc_streamoff(cam, arg);
2444 case VIDIOC_G_PARM:
2445 return et61x251_vidioc_g_parm(cam, arg);
2447 case VIDIOC_S_PARM:
2448 return et61x251_vidioc_s_parm(cam, arg);
2450 case VIDIOC_G_STD:
2451 case VIDIOC_S_STD:
2452 case VIDIOC_QUERYSTD:
2453 case VIDIOC_ENUMSTD:
2454 case VIDIOC_QUERYMENU:
2455 case VIDIOC_ENUM_FRAMEINTERVALS:
2456 return -EINVAL;
2458 default:
2459 return -EINVAL;
2465 static int et61x251_ioctl(struct inode* inode, struct file* filp,
2466 unsigned int cmd, unsigned long arg)
2468 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp));
2469 int err = 0;
2471 if (mutex_lock_interruptible(&cam->fileop_mutex))
2472 return -ERESTARTSYS;
2474 if (cam->state & DEV_DISCONNECTED) {
2475 DBG(1, "Device not present");
2476 mutex_unlock(&cam->fileop_mutex);
2477 return -ENODEV;
2480 if (cam->state & DEV_MISCONFIGURED) {
2481 DBG(1, "The camera is misconfigured. Close and open it "
2482 "again.");
2483 mutex_unlock(&cam->fileop_mutex);
2484 return -EIO;
2487 V4LDBG(3, "et61x251", cmd);
2489 err = et61x251_ioctl_v4l2(inode, filp, cmd, (void __user *)arg);
2491 mutex_unlock(&cam->fileop_mutex);
2493 return err;
2497 static const struct file_operations et61x251_fops = {
2498 .owner = THIS_MODULE,
2499 .open = et61x251_open,
2500 .release = et61x251_release,
2501 .ioctl = et61x251_ioctl,
2502 .compat_ioctl = v4l_compat_ioctl32,
2503 .read = et61x251_read,
2504 .poll = et61x251_poll,
2505 .mmap = et61x251_mmap,
2506 .llseek = no_llseek,
2509 /*****************************************************************************/
2511 /* It exists a single interface only. We do not need to validate anything. */
2512 static int
2513 et61x251_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
2515 struct usb_device *udev = interface_to_usbdev(intf);
2516 struct et61x251_device* cam;
2517 static unsigned int dev_nr = 0;
2518 unsigned int i;
2519 int err = 0;
2521 if (!(cam = kzalloc(sizeof(struct et61x251_device), GFP_KERNEL)))
2522 return -ENOMEM;
2524 cam->usbdev = udev;
2526 if (!(cam->control_buffer = kzalloc(8, GFP_KERNEL))) {
2527 DBG(1, "kmalloc() failed");
2528 err = -ENOMEM;
2529 goto fail;
2532 if (!(cam->v4ldev = video_device_alloc())) {
2533 DBG(1, "video_device_alloc() failed");
2534 err = -ENOMEM;
2535 goto fail;
2538 mutex_init(&cam->dev_mutex);
2540 DBG(2, "ET61X[12]51 PC Camera Controller detected "
2541 "(vid/pid 0x%04X:0x%04X)",id->idVendor, id->idProduct);
2543 for (i = 0; et61x251_sensor_table[i]; i++) {
2544 err = et61x251_sensor_table[i](cam);
2545 if (!err)
2546 break;
2549 if (!err)
2550 DBG(2, "%s image sensor detected", cam->sensor.name);
2551 else {
2552 DBG(1, "No supported image sensor detected");
2553 err = -ENODEV;
2554 goto fail;
2557 if (et61x251_init(cam)) {
2558 DBG(1, "Initialization failed. I will retry on open().");
2559 cam->state |= DEV_MISCONFIGURED;
2562 strcpy(cam->v4ldev->name, "ET61X[12]51 PC Camera");
2563 cam->v4ldev->owner = THIS_MODULE;
2564 cam->v4ldev->type = VID_TYPE_CAPTURE | VID_TYPE_SCALES;
2565 cam->v4ldev->hardware = 0;
2566 cam->v4ldev->fops = &et61x251_fops;
2567 cam->v4ldev->minor = video_nr[dev_nr];
2568 cam->v4ldev->release = video_device_release;
2569 video_set_drvdata(cam->v4ldev, cam);
2571 mutex_lock(&cam->dev_mutex);
2573 err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER,
2574 video_nr[dev_nr]);
2575 if (err) {
2576 DBG(1, "V4L2 device registration failed");
2577 if (err == -ENFILE && video_nr[dev_nr] == -1)
2578 DBG(1, "Free /dev/videoX node not found");
2579 video_nr[dev_nr] = -1;
2580 dev_nr = (dev_nr < ET61X251_MAX_DEVICES-1) ? dev_nr+1 : 0;
2581 mutex_unlock(&cam->dev_mutex);
2582 goto fail;
2585 DBG(2, "V4L2 device registered as /dev/video%d", cam->v4ldev->minor);
2587 cam->module_param.force_munmap = force_munmap[dev_nr];
2588 cam->module_param.frame_timeout = frame_timeout[dev_nr];
2590 dev_nr = (dev_nr < ET61X251_MAX_DEVICES-1) ? dev_nr+1 : 0;
2592 #ifdef CONFIG_VIDEO_ADV_DEBUG
2593 err = et61x251_create_sysfs(cam);
2594 if (!err)
2595 DBG(2, "Optional device control through 'sysfs' "
2596 "interface ready");
2597 else
2598 DBG(2, "Failed to create 'sysfs' interface for optional "
2599 "device controlling. Error #%d", err);
2600 #else
2601 DBG(2, "Optional device control through 'sysfs' interface disabled");
2602 #endif
2604 usb_set_intfdata(intf, cam);
2606 mutex_unlock(&cam->dev_mutex);
2608 return 0;
2610 fail:
2611 if (cam) {
2612 kfree(cam->control_buffer);
2613 if (cam->v4ldev)
2614 video_device_release(cam->v4ldev);
2615 kfree(cam);
2617 return err;
2621 static void et61x251_usb_disconnect(struct usb_interface* intf)
2623 struct et61x251_device* cam = usb_get_intfdata(intf);
2625 if (!cam)
2626 return;
2628 down_write(&et61x251_disconnect);
2630 mutex_lock(&cam->dev_mutex);
2632 DBG(2, "Disconnecting %s...", cam->v4ldev->name);
2634 wake_up_interruptible_all(&cam->open);
2636 if (cam->users) {
2637 DBG(2, "Device /dev/video%d is open! Deregistration and "
2638 "memory deallocation are deferred on close.",
2639 cam->v4ldev->minor);
2640 cam->state |= DEV_MISCONFIGURED;
2641 et61x251_stop_transfer(cam);
2642 cam->state |= DEV_DISCONNECTED;
2643 wake_up_interruptible(&cam->wait_frame);
2644 wake_up(&cam->wait_stream);
2645 usb_get_dev(cam->usbdev);
2646 } else {
2647 cam->state |= DEV_DISCONNECTED;
2648 et61x251_release_resources(cam);
2651 mutex_unlock(&cam->dev_mutex);
2653 if (!cam->users)
2654 kfree(cam);
2656 up_write(&et61x251_disconnect);
2660 static struct usb_driver et61x251_usb_driver = {
2661 .name = "et61x251",
2662 .id_table = et61x251_id_table,
2663 .probe = et61x251_usb_probe,
2664 .disconnect = et61x251_usb_disconnect,
2667 /*****************************************************************************/
2669 static int __init et61x251_module_init(void)
2671 int err = 0;
2673 KDBG(2, ET61X251_MODULE_NAME " v" ET61X251_MODULE_VERSION);
2674 KDBG(3, ET61X251_MODULE_AUTHOR);
2676 if ((err = usb_register(&et61x251_usb_driver)))
2677 KDBG(1, "usb_register() failed");
2679 return err;
2683 static void __exit et61x251_module_exit(void)
2685 usb_deregister(&et61x251_usb_driver);
2689 module_init(et61x251_module_init);
2690 module_exit(et61x251_module_exit);