V4L/DVB (6006): tuner: move last_div to tuner-simple private data
[wrt350n-kernel.git] / drivers / media / video / et61x251 / et61x251_core.c
blob585bd1fe0765540172114278baad8fd096416492
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-2007 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.09"
52 #define ET61X251_MODULE_VERSION_CODE KERNEL_VERSION(1, 1, 9)
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,
249 const struct et61x251_sensor* sensor)
251 int i, r;
253 for (i = 1; i <= 8; i++) {
254 if (sensor->interface == ET61X251_I2C_3WIRES) {
255 r = et61x251_read_reg(cam, 0x8e);
256 if (!(r & 0x02) && (r >= 0))
257 return 0;
258 } else {
259 r = et61x251_read_reg(cam, 0x8b);
260 if (!(r & 0x01) && (r >= 0))
261 return 0;
263 if (r < 0)
264 return -EIO;
265 udelay(8*8); /* minimum for sensors at 400kHz */
268 return -EBUSY;
273 et61x251_i2c_try_read(struct et61x251_device* cam,
274 const struct et61x251_sensor* sensor, u8 address)
276 struct usb_device* udev = cam->usbdev;
277 u8* data = cam->control_buffer;
278 int err = 0, res;
280 data[0] = address;
281 data[1] = cam->sensor.i2c_slave_id;
282 data[2] = cam->sensor.rsta | 0x10;
283 data[3] = !(et61x251_read_reg(cam, 0x8b) & 0x02);
284 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
285 0, 0x88, data, 4, ET61X251_CTRL_TIMEOUT);
286 if (res < 0)
287 err += res;
289 err += et61x251_i2c_wait(cam, sensor);
291 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1,
292 0, 0x80, data, 8, ET61X251_CTRL_TIMEOUT);
293 if (res < 0)
294 err += res;
296 if (err)
297 DBG(3, "I2C read failed for %s image sensor", sensor->name);
299 PDBGG("I2C read: address 0x%02X, value: 0x%02X", address, data[0]);
301 return err ? -1 : (int)data[0];
306 et61x251_i2c_try_write(struct et61x251_device* cam,
307 const struct et61x251_sensor* sensor, u8 address,
308 u8 value)
310 struct usb_device* udev = cam->usbdev;
311 u8* data = cam->control_buffer;
312 int err = 0, res;
314 data[0] = address;
315 data[1] = cam->sensor.i2c_slave_id;
316 data[2] = cam->sensor.rsta | 0x12;
317 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
318 0, 0x88, data, 3, ET61X251_CTRL_TIMEOUT);
319 if (res < 0)
320 err += res;
322 data[0] = value;
323 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
324 0, 0x80, data, 1, ET61X251_CTRL_TIMEOUT);
325 if (res < 0)
326 err += res;
328 err += et61x251_i2c_wait(cam, sensor);
330 if (err)
331 DBG(3, "I2C write failed for %s image sensor", sensor->name);
333 PDBGG("I2C write: address 0x%02X, value: 0x%02X", address, value);
335 return err ? -1 : 0;
340 et61x251_i2c_raw_write(struct et61x251_device* cam, u8 n, u8 data1, u8 data2,
341 u8 data3, u8 data4, u8 data5, u8 data6, u8 data7,
342 u8 data8, u8 address)
344 struct usb_device* udev = cam->usbdev;
345 u8* data = cam->control_buffer;
346 int err = 0, res;
348 data[0] = data2;
349 data[1] = data3;
350 data[2] = data4;
351 data[3] = data5;
352 data[4] = data6;
353 data[5] = data7;
354 data[6] = data8;
355 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
356 0, 0x81, data, n-1, ET61X251_CTRL_TIMEOUT);
357 if (res < 0)
358 err += res;
360 data[0] = address;
361 data[1] = cam->sensor.i2c_slave_id;
362 data[2] = cam->sensor.rsta | 0x02 | (n << 4);
363 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
364 0, 0x88, data, 3, ET61X251_CTRL_TIMEOUT);
365 if (res < 0)
366 err += res;
368 /* Start writing through the serial interface */
369 data[0] = data1;
370 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
371 0, 0x80, data, 1, ET61X251_CTRL_TIMEOUT);
372 if (res < 0)
373 err += res;
375 err += et61x251_i2c_wait(cam, &cam->sensor);
377 if (err)
378 DBG(3, "I2C raw write failed for %s image sensor",
379 cam->sensor.name);
381 PDBGG("I2C raw write: %u bytes, address = 0x%02X, data1 = 0x%02X, "
382 "data2 = 0x%02X, data3 = 0x%02X, data4 = 0x%02X, data5 = 0x%02X,"
383 " data6 = 0x%02X, data7 = 0x%02X, data8 = 0x%02X", n, address,
384 data1, data2, data3, data4, data5, data6, data7, data8);
386 return err ? -1 : 0;
391 int et61x251_i2c_read(struct et61x251_device* cam, u8 address)
393 return et61x251_i2c_try_read(cam, &cam->sensor, address);
397 int et61x251_i2c_write(struct et61x251_device* cam, u8 address, u8 value)
399 return et61x251_i2c_try_write(cam, &cam->sensor, address, value);
402 /*****************************************************************************/
404 static void et61x251_urb_complete(struct urb *urb)
406 struct et61x251_device* cam = urb->context;
407 struct et61x251_frame_t** f;
408 size_t imagesize;
409 u8 i;
410 int err = 0;
412 if (urb->status == -ENOENT)
413 return;
415 f = &cam->frame_current;
417 if (cam->stream == STREAM_INTERRUPT) {
418 cam->stream = STREAM_OFF;
419 if ((*f))
420 (*f)->state = F_QUEUED;
421 DBG(3, "Stream interrupted");
422 wake_up(&cam->wait_stream);
425 if (cam->state & DEV_DISCONNECTED)
426 return;
428 if (cam->state & DEV_MISCONFIGURED) {
429 wake_up_interruptible(&cam->wait_frame);
430 return;
433 if (cam->stream == STREAM_OFF || list_empty(&cam->inqueue))
434 goto resubmit_urb;
436 if (!(*f))
437 (*f) = list_entry(cam->inqueue.next, struct et61x251_frame_t,
438 frame);
440 imagesize = (cam->sensor.pix_format.width *
441 cam->sensor.pix_format.height *
442 cam->sensor.pix_format.priv) / 8;
444 for (i = 0; i < urb->number_of_packets; i++) {
445 unsigned int len, status;
446 void *pos;
447 u8* b1, * b2, sof;
448 const u8 VOID_BYTES = 6;
449 size_t imglen;
451 len = urb->iso_frame_desc[i].actual_length;
452 status = urb->iso_frame_desc[i].status;
453 pos = urb->iso_frame_desc[i].offset + urb->transfer_buffer;
455 if (status) {
456 DBG(3, "Error in isochronous frame");
457 (*f)->state = F_ERROR;
458 continue;
461 b1 = pos++;
462 b2 = pos++;
463 sof = ((*b1 & 0x3f) == 63);
464 imglen = ((*b1 & 0xc0) << 2) | *b2;
466 PDBGG("Isochrnous frame: length %u, #%u i, image length %zu",
467 len, i, imglen);
469 if ((*f)->state == F_QUEUED || (*f)->state == F_ERROR)
470 start_of_frame:
471 if (sof) {
472 (*f)->state = F_GRABBING;
473 (*f)->buf.bytesused = 0;
474 do_gettimeofday(&(*f)->buf.timestamp);
475 pos += 22;
476 DBG(3, "SOF detected: new video frame");
479 if ((*f)->state == F_GRABBING) {
480 if (sof && (*f)->buf.bytesused) {
481 if (cam->sensor.pix_format.pixelformat ==
482 V4L2_PIX_FMT_ET61X251)
483 goto end_of_frame;
484 else {
485 DBG(3, "Not expected SOF detected "
486 "after %lu bytes",
487 (unsigned long)(*f)->buf.bytesused);
488 (*f)->state = F_ERROR;
489 continue;
493 if ((*f)->buf.bytesused + imglen > imagesize) {
494 DBG(3, "Video frame size exceeded");
495 (*f)->state = F_ERROR;
496 continue;
499 pos += VOID_BYTES;
501 memcpy((*f)->bufmem+(*f)->buf.bytesused, pos, imglen);
502 (*f)->buf.bytesused += imglen;
504 if ((*f)->buf.bytesused == imagesize) {
505 u32 b;
506 end_of_frame:
507 b = (*f)->buf.bytesused;
508 (*f)->state = F_DONE;
509 (*f)->buf.sequence= ++cam->frame_count;
510 spin_lock(&cam->queue_lock);
511 list_move_tail(&(*f)->frame, &cam->outqueue);
512 if (!list_empty(&cam->inqueue))
513 (*f) = list_entry(cam->inqueue.next,
514 struct et61x251_frame_t,
515 frame);
516 else
517 (*f) = NULL;
518 spin_unlock(&cam->queue_lock);
519 DBG(3, "Video frame captured: : %lu bytes",
520 (unsigned long)(b));
522 if (!(*f))
523 goto resubmit_urb;
525 if (sof &&
526 cam->sensor.pix_format.pixelformat ==
527 V4L2_PIX_FMT_ET61X251)
528 goto start_of_frame;
533 resubmit_urb:
534 urb->dev = cam->usbdev;
535 err = usb_submit_urb(urb, GFP_ATOMIC);
536 if (err < 0 && err != -EPERM) {
537 cam->state |= DEV_MISCONFIGURED;
538 DBG(1, "usb_submit_urb() failed");
541 wake_up_interruptible(&cam->wait_frame);
545 static int et61x251_start_transfer(struct et61x251_device* cam)
547 struct usb_device *udev = cam->usbdev;
548 struct urb* urb;
549 struct usb_host_interface* altsetting = usb_altnum_to_altsetting(
550 usb_ifnum_to_if(udev, 0),
551 ET61X251_ALTERNATE_SETTING);
552 const unsigned int psz = le16_to_cpu(altsetting->
553 endpoint[0].desc.wMaxPacketSize);
554 s8 i, j;
555 int err = 0;
557 for (i = 0; i < ET61X251_URBS; i++) {
558 cam->transfer_buffer[i] = kzalloc(ET61X251_ISO_PACKETS * psz,
559 GFP_KERNEL);
560 if (!cam->transfer_buffer[i]) {
561 err = -ENOMEM;
562 DBG(1, "Not enough memory");
563 goto free_buffers;
567 for (i = 0; i < ET61X251_URBS; i++) {
568 urb = usb_alloc_urb(ET61X251_ISO_PACKETS, GFP_KERNEL);
569 cam->urb[i] = urb;
570 if (!urb) {
571 err = -ENOMEM;
572 DBG(1, "usb_alloc_urb() failed");
573 goto free_urbs;
575 urb->dev = udev;
576 urb->context = cam;
577 urb->pipe = usb_rcvisocpipe(udev, 1);
578 urb->transfer_flags = URB_ISO_ASAP;
579 urb->number_of_packets = ET61X251_ISO_PACKETS;
580 urb->complete = et61x251_urb_complete;
581 urb->transfer_buffer = cam->transfer_buffer[i];
582 urb->transfer_buffer_length = psz * ET61X251_ISO_PACKETS;
583 urb->interval = 1;
584 for (j = 0; j < ET61X251_ISO_PACKETS; j++) {
585 urb->iso_frame_desc[j].offset = psz * j;
586 urb->iso_frame_desc[j].length = psz;
590 err = et61x251_write_reg(cam, 0x01, 0x03);
591 err = et61x251_write_reg(cam, 0x00, 0x03);
592 err = et61x251_write_reg(cam, 0x08, 0x03);
593 if (err) {
594 err = -EIO;
595 DBG(1, "I/O hardware error");
596 goto free_urbs;
599 err = usb_set_interface(udev, 0, ET61X251_ALTERNATE_SETTING);
600 if (err) {
601 DBG(1, "usb_set_interface() failed");
602 goto free_urbs;
605 cam->frame_current = NULL;
607 for (i = 0; i < ET61X251_URBS; i++) {
608 err = usb_submit_urb(cam->urb[i], GFP_KERNEL);
609 if (err) {
610 for (j = i-1; j >= 0; j--)
611 usb_kill_urb(cam->urb[j]);
612 DBG(1, "usb_submit_urb() failed, error %d", err);
613 goto free_urbs;
617 return 0;
619 free_urbs:
620 for (i = 0; (i < ET61X251_URBS) && cam->urb[i]; i++)
621 usb_free_urb(cam->urb[i]);
623 free_buffers:
624 for (i = 0; (i < ET61X251_URBS) && cam->transfer_buffer[i]; i++)
625 kfree(cam->transfer_buffer[i]);
627 return err;
631 static int et61x251_stop_transfer(struct et61x251_device* cam)
633 struct usb_device *udev = cam->usbdev;
634 s8 i;
635 int err = 0;
637 if (cam->state & DEV_DISCONNECTED)
638 return 0;
640 for (i = ET61X251_URBS-1; i >= 0; i--) {
641 usb_kill_urb(cam->urb[i]);
642 usb_free_urb(cam->urb[i]);
643 kfree(cam->transfer_buffer[i]);
646 err = usb_set_interface(udev, 0, 0); /* 0 Mb/s */
647 if (err)
648 DBG(3, "usb_set_interface() failed");
650 return err;
654 static int et61x251_stream_interrupt(struct et61x251_device* cam)
656 long timeout;
658 cam->stream = STREAM_INTERRUPT;
659 timeout = wait_event_timeout(cam->wait_stream,
660 (cam->stream == STREAM_OFF) ||
661 (cam->state & DEV_DISCONNECTED),
662 ET61X251_URB_TIMEOUT);
663 if (cam->state & DEV_DISCONNECTED)
664 return -ENODEV;
665 else if (cam->stream != STREAM_OFF) {
666 cam->state |= DEV_MISCONFIGURED;
667 DBG(1, "URB timeout reached. The camera is misconfigured. To "
668 "use it, close and open /dev/video%d again.",
669 cam->v4ldev->minor);
670 return -EIO;
673 return 0;
676 /*****************************************************************************/
678 #ifdef CONFIG_VIDEO_ADV_DEBUG
679 static u8 et61x251_strtou8(const char* buff, size_t len, ssize_t* count)
681 char str[5];
682 char* endp;
683 unsigned long val;
685 if (len < 4) {
686 strncpy(str, buff, len);
687 str[len] = '\0';
688 } else {
689 strncpy(str, buff, 4);
690 str[4] = '\0';
693 val = simple_strtoul(str, &endp, 0);
695 *count = 0;
696 if (val <= 0xff)
697 *count = (ssize_t)(endp - str);
698 if ((*count) && (len == *count+1) && (buff[*count] == '\n'))
699 *count += 1;
701 return (u8)val;
705 NOTE 1: being inside one of the following methods implies that the v4l
706 device exists for sure (see kobjects and reference counters)
707 NOTE 2: buffers are PAGE_SIZE long
710 static ssize_t et61x251_show_reg(struct class_device* cd, char* buf)
712 struct et61x251_device* cam;
713 ssize_t count;
715 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
716 return -ERESTARTSYS;
718 cam = video_get_drvdata(to_video_device(cd));
719 if (!cam) {
720 mutex_unlock(&et61x251_sysfs_lock);
721 return -ENODEV;
724 count = sprintf(buf, "%u\n", cam->sysfs.reg);
726 mutex_unlock(&et61x251_sysfs_lock);
728 return count;
732 static ssize_t
733 et61x251_store_reg(struct class_device* cd, const char* buf, size_t len)
735 struct et61x251_device* cam;
736 u8 index;
737 ssize_t count;
739 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
740 return -ERESTARTSYS;
742 cam = video_get_drvdata(to_video_device(cd));
743 if (!cam) {
744 mutex_unlock(&et61x251_sysfs_lock);
745 return -ENODEV;
748 index = et61x251_strtou8(buf, len, &count);
749 if (index > 0x8e || !count) {
750 mutex_unlock(&et61x251_sysfs_lock);
751 return -EINVAL;
754 cam->sysfs.reg = index;
756 DBG(2, "Moved ET61X[12]51 register index to 0x%02X", cam->sysfs.reg);
757 DBG(3, "Written bytes: %zd", count);
759 mutex_unlock(&et61x251_sysfs_lock);
761 return count;
765 static ssize_t et61x251_show_val(struct class_device* cd, char* buf)
767 struct et61x251_device* cam;
768 ssize_t count;
769 int val;
771 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
772 return -ERESTARTSYS;
774 cam = video_get_drvdata(to_video_device(cd));
775 if (!cam) {
776 mutex_unlock(&et61x251_sysfs_lock);
777 return -ENODEV;
780 if ((val = et61x251_read_reg(cam, cam->sysfs.reg)) < 0) {
781 mutex_unlock(&et61x251_sysfs_lock);
782 return -EIO;
785 count = sprintf(buf, "%d\n", val);
787 DBG(3, "Read bytes: %zd", count);
789 mutex_unlock(&et61x251_sysfs_lock);
791 return count;
795 static ssize_t
796 et61x251_store_val(struct class_device* cd, const char* buf, size_t len)
798 struct et61x251_device* cam;
799 u8 value;
800 ssize_t count;
801 int err;
803 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
804 return -ERESTARTSYS;
806 cam = video_get_drvdata(to_video_device(cd));
807 if (!cam) {
808 mutex_unlock(&et61x251_sysfs_lock);
809 return -ENODEV;
812 value = et61x251_strtou8(buf, len, &count);
813 if (!count) {
814 mutex_unlock(&et61x251_sysfs_lock);
815 return -EINVAL;
818 err = et61x251_write_reg(cam, value, cam->sysfs.reg);
819 if (err) {
820 mutex_unlock(&et61x251_sysfs_lock);
821 return -EIO;
824 DBG(2, "Written ET61X[12]51 reg. 0x%02X, val. 0x%02X",
825 cam->sysfs.reg, value);
826 DBG(3, "Written bytes: %zd", count);
828 mutex_unlock(&et61x251_sysfs_lock);
830 return count;
834 static ssize_t et61x251_show_i2c_reg(struct class_device* cd, char* buf)
836 struct et61x251_device* cam;
837 ssize_t count;
839 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
840 return -ERESTARTSYS;
842 cam = video_get_drvdata(to_video_device(cd));
843 if (!cam) {
844 mutex_unlock(&et61x251_sysfs_lock);
845 return -ENODEV;
848 count = sprintf(buf, "%u\n", cam->sysfs.i2c_reg);
850 DBG(3, "Read bytes: %zd", count);
852 mutex_unlock(&et61x251_sysfs_lock);
854 return count;
858 static ssize_t
859 et61x251_store_i2c_reg(struct class_device* cd, const char* buf, size_t len)
861 struct et61x251_device* cam;
862 u8 index;
863 ssize_t count;
865 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
866 return -ERESTARTSYS;
868 cam = video_get_drvdata(to_video_device(cd));
869 if (!cam) {
870 mutex_unlock(&et61x251_sysfs_lock);
871 return -ENODEV;
874 index = et61x251_strtou8(buf, len, &count);
875 if (!count) {
876 mutex_unlock(&et61x251_sysfs_lock);
877 return -EINVAL;
880 cam->sysfs.i2c_reg = index;
882 DBG(2, "Moved sensor register index to 0x%02X", cam->sysfs.i2c_reg);
883 DBG(3, "Written bytes: %zd", count);
885 mutex_unlock(&et61x251_sysfs_lock);
887 return count;
891 static ssize_t et61x251_show_i2c_val(struct class_device* cd, char* buf)
893 struct et61x251_device* cam;
894 ssize_t count;
895 int val;
897 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
898 return -ERESTARTSYS;
900 cam = video_get_drvdata(to_video_device(cd));
901 if (!cam) {
902 mutex_unlock(&et61x251_sysfs_lock);
903 return -ENODEV;
906 if (!(cam->sensor.sysfs_ops & ET61X251_I2C_READ)) {
907 mutex_unlock(&et61x251_sysfs_lock);
908 return -ENOSYS;
911 if ((val = et61x251_i2c_read(cam, cam->sysfs.i2c_reg)) < 0) {
912 mutex_unlock(&et61x251_sysfs_lock);
913 return -EIO;
916 count = sprintf(buf, "%d\n", val);
918 DBG(3, "Read bytes: %zd", count);
920 mutex_unlock(&et61x251_sysfs_lock);
922 return count;
926 static ssize_t
927 et61x251_store_i2c_val(struct class_device* cd, const char* buf, size_t len)
929 struct et61x251_device* cam;
930 u8 value;
931 ssize_t count;
932 int err;
934 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
935 return -ERESTARTSYS;
937 cam = video_get_drvdata(to_video_device(cd));
938 if (!cam) {
939 mutex_unlock(&et61x251_sysfs_lock);
940 return -ENODEV;
943 if (!(cam->sensor.sysfs_ops & ET61X251_I2C_READ)) {
944 mutex_unlock(&et61x251_sysfs_lock);
945 return -ENOSYS;
948 value = et61x251_strtou8(buf, len, &count);
949 if (!count) {
950 mutex_unlock(&et61x251_sysfs_lock);
951 return -EINVAL;
954 err = et61x251_i2c_write(cam, cam->sysfs.i2c_reg, value);
955 if (err) {
956 mutex_unlock(&et61x251_sysfs_lock);
957 return -EIO;
960 DBG(2, "Written sensor reg. 0x%02X, val. 0x%02X",
961 cam->sysfs.i2c_reg, value);
962 DBG(3, "Written bytes: %zd", count);
964 mutex_unlock(&et61x251_sysfs_lock);
966 return count;
970 static CLASS_DEVICE_ATTR(reg, S_IRUGO | S_IWUSR,
971 et61x251_show_reg, et61x251_store_reg);
972 static CLASS_DEVICE_ATTR(val, S_IRUGO | S_IWUSR,
973 et61x251_show_val, et61x251_store_val);
974 static CLASS_DEVICE_ATTR(i2c_reg, S_IRUGO | S_IWUSR,
975 et61x251_show_i2c_reg, et61x251_store_i2c_reg);
976 static CLASS_DEVICE_ATTR(i2c_val, S_IRUGO | S_IWUSR,
977 et61x251_show_i2c_val, et61x251_store_i2c_val);
980 static int et61x251_create_sysfs(struct et61x251_device* cam)
982 struct class_device *classdev = &(cam->v4ldev->class_dev);
983 int err = 0;
985 if ((err = class_device_create_file(classdev, &class_device_attr_reg)))
986 goto err_out;
987 if ((err = class_device_create_file(classdev, &class_device_attr_val)))
988 goto err_reg;
990 if (cam->sensor.sysfs_ops) {
991 if ((err = class_device_create_file(classdev,
992 &class_device_attr_i2c_reg)))
993 goto err_val;
994 if ((err = class_device_create_file(classdev,
995 &class_device_attr_i2c_val)))
996 goto err_i2c_reg;
999 err_i2c_reg:
1000 if (cam->sensor.sysfs_ops)
1001 class_device_remove_file(classdev, &class_device_attr_i2c_reg);
1002 err_val:
1003 class_device_remove_file(classdev, &class_device_attr_val);
1004 err_reg:
1005 class_device_remove_file(classdev, &class_device_attr_reg);
1006 err_out:
1007 return err;
1009 #endif /* CONFIG_VIDEO_ADV_DEBUG */
1011 /*****************************************************************************/
1013 static int
1014 et61x251_set_pix_format(struct et61x251_device* cam,
1015 struct v4l2_pix_format* pix)
1017 int r, err = 0;
1019 if ((r = et61x251_read_reg(cam, 0x12)) < 0)
1020 err += r;
1021 if (pix->pixelformat == V4L2_PIX_FMT_ET61X251)
1022 err += et61x251_write_reg(cam, r & 0xfd, 0x12);
1023 else
1024 err += et61x251_write_reg(cam, r | 0x02, 0x12);
1026 return err ? -EIO : 0;
1030 static int
1031 et61x251_set_compression(struct et61x251_device* cam,
1032 struct v4l2_jpegcompression* compression)
1034 int r, err = 0;
1036 if ((r = et61x251_read_reg(cam, 0x12)) < 0)
1037 err += r;
1038 if (compression->quality == 0)
1039 err += et61x251_write_reg(cam, r & 0xfb, 0x12);
1040 else
1041 err += et61x251_write_reg(cam, r | 0x04, 0x12);
1043 return err ? -EIO : 0;
1047 static int et61x251_set_scale(struct et61x251_device* cam, u8 scale)
1049 int r = 0, err = 0;
1051 r = et61x251_read_reg(cam, 0x12);
1052 if (r < 0)
1053 err += r;
1055 if (scale == 1)
1056 err += et61x251_write_reg(cam, r & ~0x01, 0x12);
1057 else if (scale == 2)
1058 err += et61x251_write_reg(cam, r | 0x01, 0x12);
1060 if (err)
1061 return -EIO;
1063 PDBGG("Scaling factor: %u", scale);
1065 return 0;
1069 static int
1070 et61x251_set_crop(struct et61x251_device* cam, struct v4l2_rect* rect)
1072 struct et61x251_sensor* s = &cam->sensor;
1073 u16 fmw_sx = (u16)(rect->left - s->cropcap.bounds.left +
1074 s->active_pixel.left),
1075 fmw_sy = (u16)(rect->top - s->cropcap.bounds.top +
1076 s->active_pixel.top),
1077 fmw_length = (u16)(rect->width),
1078 fmw_height = (u16)(rect->height);
1079 int err = 0;
1081 err += et61x251_write_reg(cam, fmw_sx & 0xff, 0x69);
1082 err += et61x251_write_reg(cam, fmw_sy & 0xff, 0x6a);
1083 err += et61x251_write_reg(cam, fmw_length & 0xff, 0x6b);
1084 err += et61x251_write_reg(cam, fmw_height & 0xff, 0x6c);
1085 err += et61x251_write_reg(cam, (fmw_sx >> 8) | ((fmw_sy & 0x300) >> 6)
1086 | ((fmw_length & 0x300) >> 4)
1087 | ((fmw_height & 0x300) >> 2), 0x6d);
1088 if (err)
1089 return -EIO;
1091 PDBGG("fmw_sx, fmw_sy, fmw_length, fmw_height: %u %u %u %u",
1092 fmw_sx, fmw_sy, fmw_length, fmw_height);
1094 return 0;
1098 static int et61x251_init(struct et61x251_device* cam)
1100 struct et61x251_sensor* s = &cam->sensor;
1101 struct v4l2_control ctrl;
1102 struct v4l2_queryctrl *qctrl;
1103 struct v4l2_rect* rect;
1104 u8 i = 0;
1105 int err = 0;
1107 if (!(cam->state & DEV_INITIALIZED)) {
1108 mutex_init(&cam->open_mutex);
1109 init_waitqueue_head(&cam->wait_open);
1110 qctrl = s->qctrl;
1111 rect = &(s->cropcap.defrect);
1112 cam->compression.quality = ET61X251_COMPRESSION_QUALITY;
1113 } else { /* use current values */
1114 qctrl = s->_qctrl;
1115 rect = &(s->_rect);
1118 err += et61x251_set_scale(cam, rect->width / s->pix_format.width);
1119 err += et61x251_set_crop(cam, rect);
1120 if (err)
1121 return err;
1123 if (s->init) {
1124 err = s->init(cam);
1125 if (err) {
1126 DBG(3, "Sensor initialization failed");
1127 return err;
1131 err += et61x251_set_compression(cam, &cam->compression);
1132 err += et61x251_set_pix_format(cam, &s->pix_format);
1133 if (s->set_pix_format)
1134 err += s->set_pix_format(cam, &s->pix_format);
1135 if (err)
1136 return err;
1138 if (s->pix_format.pixelformat == V4L2_PIX_FMT_ET61X251)
1139 DBG(3, "Compressed video format is active, quality %d",
1140 cam->compression.quality);
1141 else
1142 DBG(3, "Uncompressed video format is active");
1144 if (s->set_crop)
1145 if ((err = s->set_crop(cam, rect))) {
1146 DBG(3, "set_crop() failed");
1147 return err;
1150 if (s->set_ctrl) {
1151 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1152 if (s->qctrl[i].id != 0 &&
1153 !(s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)) {
1154 ctrl.id = s->qctrl[i].id;
1155 ctrl.value = qctrl[i].default_value;
1156 err = s->set_ctrl(cam, &ctrl);
1157 if (err) {
1158 DBG(3, "Set %s control failed",
1159 s->qctrl[i].name);
1160 return err;
1162 DBG(3, "Image sensor supports '%s' control",
1163 s->qctrl[i].name);
1167 if (!(cam->state & DEV_INITIALIZED)) {
1168 mutex_init(&cam->fileop_mutex);
1169 spin_lock_init(&cam->queue_lock);
1170 init_waitqueue_head(&cam->wait_frame);
1171 init_waitqueue_head(&cam->wait_stream);
1172 cam->nreadbuffers = 2;
1173 memcpy(s->_qctrl, s->qctrl, sizeof(s->qctrl));
1174 memcpy(&(s->_rect), &(s->cropcap.defrect),
1175 sizeof(struct v4l2_rect));
1176 cam->state |= DEV_INITIALIZED;
1179 DBG(2, "Initialization succeeded");
1180 return 0;
1183 /*****************************************************************************/
1185 static void et61x251_release_resources(struct kref *kref)
1187 struct et61x251_device *cam;
1189 mutex_lock(&et61x251_sysfs_lock);
1191 cam = container_of(kref, struct et61x251_device, kref);
1193 DBG(2, "V4L2 device /dev/video%d deregistered", cam->v4ldev->minor);
1194 video_set_drvdata(cam->v4ldev, NULL);
1195 video_unregister_device(cam->v4ldev);
1196 usb_put_dev(cam->usbdev);
1197 kfree(cam->control_buffer);
1198 kfree(cam);
1200 mutex_unlock(&et61x251_sysfs_lock);
1204 static int et61x251_open(struct inode* inode, struct file* filp)
1206 struct et61x251_device* cam;
1207 int err = 0;
1209 if (!down_read_trylock(&et61x251_dev_lock))
1210 return -ERESTARTSYS;
1212 cam = video_get_drvdata(video_devdata(filp));
1214 if (wait_for_completion_interruptible(&cam->probe)) {
1215 up_read(&et61x251_dev_lock);
1216 return -ERESTARTSYS;
1219 kref_get(&cam->kref);
1221 if (mutex_lock_interruptible(&cam->open_mutex)) {
1222 kref_put(&cam->kref, et61x251_release_resources);
1223 up_read(&et61x251_dev_lock);
1224 return -ERESTARTSYS;
1227 if (cam->state & DEV_DISCONNECTED) {
1228 DBG(1, "Device not present");
1229 err = -ENODEV;
1230 goto out;
1233 if (cam->users) {
1234 DBG(2, "Device /dev/video%d is already in use",
1235 cam->v4ldev->minor);
1236 DBG(3, "Simultaneous opens are not supported");
1237 if ((filp->f_flags & O_NONBLOCK) ||
1238 (filp->f_flags & O_NDELAY)) {
1239 err = -EWOULDBLOCK;
1240 goto out;
1242 DBG(2, "A blocking open() has been requested. Wait for the "
1243 "device to be released...");
1244 up_read(&et61x251_dev_lock);
1245 err = wait_event_interruptible_exclusive(cam->wait_open,
1246 (cam->state & DEV_DISCONNECTED)
1247 || !cam->users);
1248 down_read(&et61x251_dev_lock);
1249 if (err)
1250 goto out;
1251 if (cam->state & DEV_DISCONNECTED) {
1252 err = -ENODEV;
1253 goto out;
1257 if (cam->state & DEV_MISCONFIGURED) {
1258 err = et61x251_init(cam);
1259 if (err) {
1260 DBG(1, "Initialization failed again. "
1261 "I will retry on next open().");
1262 goto out;
1264 cam->state &= ~DEV_MISCONFIGURED;
1267 if ((err = et61x251_start_transfer(cam)))
1268 goto out;
1270 filp->private_data = cam;
1271 cam->users++;
1272 cam->io = IO_NONE;
1273 cam->stream = STREAM_OFF;
1274 cam->nbuffers = 0;
1275 cam->frame_count = 0;
1276 et61x251_empty_framequeues(cam);
1278 DBG(3, "Video device /dev/video%d is open", cam->v4ldev->minor);
1280 out:
1281 mutex_unlock(&cam->open_mutex);
1282 if (err)
1283 kref_put(&cam->kref, et61x251_release_resources);
1284 up_read(&et61x251_dev_lock);
1285 return err;
1289 static int et61x251_release(struct inode* inode, struct file* filp)
1291 struct et61x251_device* cam;
1293 down_write(&et61x251_dev_lock);
1295 cam = video_get_drvdata(video_devdata(filp));
1297 et61x251_stop_transfer(cam);
1298 et61x251_release_buffers(cam);
1299 cam->users--;
1300 wake_up_interruptible_nr(&cam->wait_open, 1);
1302 DBG(3, "Video device /dev/video%d closed", cam->v4ldev->minor);
1304 kref_put(&cam->kref, et61x251_release_resources);
1306 up_write(&et61x251_dev_lock);
1308 return 0;
1312 static ssize_t
1313 et61x251_read(struct file* filp, char __user * buf,
1314 size_t count, loff_t* f_pos)
1316 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp));
1317 struct et61x251_frame_t* f, * i;
1318 unsigned long lock_flags;
1319 long timeout;
1320 int err = 0;
1322 if (mutex_lock_interruptible(&cam->fileop_mutex))
1323 return -ERESTARTSYS;
1325 if (cam->state & DEV_DISCONNECTED) {
1326 DBG(1, "Device not present");
1327 mutex_unlock(&cam->fileop_mutex);
1328 return -ENODEV;
1331 if (cam->state & DEV_MISCONFIGURED) {
1332 DBG(1, "The camera is misconfigured. Close and open it "
1333 "again.");
1334 mutex_unlock(&cam->fileop_mutex);
1335 return -EIO;
1338 if (cam->io == IO_MMAP) {
1339 DBG(3, "Close and open the device again to choose the read "
1340 "method");
1341 mutex_unlock(&cam->fileop_mutex);
1342 return -EBUSY;
1345 if (cam->io == IO_NONE) {
1346 if (!et61x251_request_buffers(cam, cam->nreadbuffers,
1347 IO_READ)) {
1348 DBG(1, "read() failed, not enough memory");
1349 mutex_unlock(&cam->fileop_mutex);
1350 return -ENOMEM;
1352 cam->io = IO_READ;
1353 cam->stream = STREAM_ON;
1356 if (list_empty(&cam->inqueue)) {
1357 if (!list_empty(&cam->outqueue))
1358 et61x251_empty_framequeues(cam);
1359 et61x251_queue_unusedframes(cam);
1362 if (!count) {
1363 mutex_unlock(&cam->fileop_mutex);
1364 return 0;
1367 if (list_empty(&cam->outqueue)) {
1368 if (filp->f_flags & O_NONBLOCK) {
1369 mutex_unlock(&cam->fileop_mutex);
1370 return -EAGAIN;
1372 timeout = wait_event_interruptible_timeout
1373 ( cam->wait_frame,
1374 (!list_empty(&cam->outqueue)) ||
1375 (cam->state & DEV_DISCONNECTED) ||
1376 (cam->state & DEV_MISCONFIGURED),
1377 cam->module_param.frame_timeout *
1378 1000 * msecs_to_jiffies(1) );
1379 if (timeout < 0) {
1380 mutex_unlock(&cam->fileop_mutex);
1381 return timeout;
1383 if (cam->state & DEV_DISCONNECTED) {
1384 mutex_unlock(&cam->fileop_mutex);
1385 return -ENODEV;
1387 if (!timeout || (cam->state & DEV_MISCONFIGURED)) {
1388 mutex_unlock(&cam->fileop_mutex);
1389 return -EIO;
1393 f = list_entry(cam->outqueue.prev, struct et61x251_frame_t, frame);
1395 if (count > f->buf.bytesused)
1396 count = f->buf.bytesused;
1398 if (copy_to_user(buf, f->bufmem, count)) {
1399 err = -EFAULT;
1400 goto exit;
1402 *f_pos += count;
1404 exit:
1405 spin_lock_irqsave(&cam->queue_lock, lock_flags);
1406 list_for_each_entry(i, &cam->outqueue, frame)
1407 i->state = F_UNUSED;
1408 INIT_LIST_HEAD(&cam->outqueue);
1409 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1411 et61x251_queue_unusedframes(cam);
1413 PDBGG("Frame #%lu, bytes read: %zu",
1414 (unsigned long)f->buf.index, count);
1416 mutex_unlock(&cam->fileop_mutex);
1418 return err ? err : count;
1422 static unsigned int et61x251_poll(struct file *filp, poll_table *wait)
1424 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp));
1425 struct et61x251_frame_t* f;
1426 unsigned long lock_flags;
1427 unsigned int mask = 0;
1429 if (mutex_lock_interruptible(&cam->fileop_mutex))
1430 return POLLERR;
1432 if (cam->state & DEV_DISCONNECTED) {
1433 DBG(1, "Device not present");
1434 goto error;
1437 if (cam->state & DEV_MISCONFIGURED) {
1438 DBG(1, "The camera is misconfigured. Close and open it "
1439 "again.");
1440 goto error;
1443 if (cam->io == IO_NONE) {
1444 if (!et61x251_request_buffers(cam, cam->nreadbuffers,
1445 IO_READ)) {
1446 DBG(1, "poll() failed, not enough memory");
1447 goto error;
1449 cam->io = IO_READ;
1450 cam->stream = STREAM_ON;
1453 if (cam->io == IO_READ) {
1454 spin_lock_irqsave(&cam->queue_lock, lock_flags);
1455 list_for_each_entry(f, &cam->outqueue, frame)
1456 f->state = F_UNUSED;
1457 INIT_LIST_HEAD(&cam->outqueue);
1458 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1459 et61x251_queue_unusedframes(cam);
1462 poll_wait(filp, &cam->wait_frame, wait);
1464 if (!list_empty(&cam->outqueue))
1465 mask |= POLLIN | POLLRDNORM;
1467 mutex_unlock(&cam->fileop_mutex);
1469 return mask;
1471 error:
1472 mutex_unlock(&cam->fileop_mutex);
1473 return POLLERR;
1477 static void et61x251_vm_open(struct vm_area_struct* vma)
1479 struct et61x251_frame_t* f = vma->vm_private_data;
1480 f->vma_use_count++;
1484 static void et61x251_vm_close(struct vm_area_struct* vma)
1486 /* NOTE: buffers are not freed here */
1487 struct et61x251_frame_t* f = vma->vm_private_data;
1488 f->vma_use_count--;
1492 static struct vm_operations_struct et61x251_vm_ops = {
1493 .open = et61x251_vm_open,
1494 .close = et61x251_vm_close,
1498 static int et61x251_mmap(struct file* filp, struct vm_area_struct *vma)
1500 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp));
1501 unsigned long size = vma->vm_end - vma->vm_start,
1502 start = vma->vm_start;
1503 void *pos;
1504 u32 i;
1506 if (mutex_lock_interruptible(&cam->fileop_mutex))
1507 return -ERESTARTSYS;
1509 if (cam->state & DEV_DISCONNECTED) {
1510 DBG(1, "Device not present");
1511 mutex_unlock(&cam->fileop_mutex);
1512 return -ENODEV;
1515 if (cam->state & DEV_MISCONFIGURED) {
1516 DBG(1, "The camera is misconfigured. Close and open it "
1517 "again.");
1518 mutex_unlock(&cam->fileop_mutex);
1519 return -EIO;
1522 if (!(vma->vm_flags & (VM_WRITE | VM_READ))) {
1523 mutex_unlock(&cam->fileop_mutex);
1524 return -EACCES;
1527 if (cam->io != IO_MMAP ||
1528 size != PAGE_ALIGN(cam->frame[0].buf.length)) {
1529 mutex_unlock(&cam->fileop_mutex);
1530 return -EINVAL;
1533 for (i = 0; i < cam->nbuffers; i++) {
1534 if ((cam->frame[i].buf.m.offset>>PAGE_SHIFT) == vma->vm_pgoff)
1535 break;
1537 if (i == cam->nbuffers) {
1538 mutex_unlock(&cam->fileop_mutex);
1539 return -EINVAL;
1542 vma->vm_flags |= VM_IO;
1543 vma->vm_flags |= VM_RESERVED;
1545 pos = cam->frame[i].bufmem;
1546 while (size > 0) { /* size is page-aligned */
1547 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
1548 mutex_unlock(&cam->fileop_mutex);
1549 return -EAGAIN;
1551 start += PAGE_SIZE;
1552 pos += PAGE_SIZE;
1553 size -= PAGE_SIZE;
1556 vma->vm_ops = &et61x251_vm_ops;
1557 vma->vm_private_data = &cam->frame[i];
1558 et61x251_vm_open(vma);
1560 mutex_unlock(&cam->fileop_mutex);
1562 return 0;
1565 /*****************************************************************************/
1567 static int
1568 et61x251_vidioc_querycap(struct et61x251_device* cam, void __user * arg)
1570 struct v4l2_capability cap = {
1571 .driver = "et61x251",
1572 .version = ET61X251_MODULE_VERSION_CODE,
1573 .capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
1574 V4L2_CAP_STREAMING,
1577 strlcpy(cap.card, cam->v4ldev->name, sizeof(cap.card));
1578 if (usb_make_path(cam->usbdev, cap.bus_info, sizeof(cap.bus_info)) < 0)
1579 strlcpy(cap.bus_info, cam->usbdev->dev.bus_id,
1580 sizeof(cap.bus_info));
1582 if (copy_to_user(arg, &cap, sizeof(cap)))
1583 return -EFAULT;
1585 return 0;
1589 static int
1590 et61x251_vidioc_enuminput(struct et61x251_device* cam, void __user * arg)
1592 struct v4l2_input i;
1594 if (copy_from_user(&i, arg, sizeof(i)))
1595 return -EFAULT;
1597 if (i.index)
1598 return -EINVAL;
1600 memset(&i, 0, sizeof(i));
1601 strcpy(i.name, "Camera");
1602 i.type = V4L2_INPUT_TYPE_CAMERA;
1604 if (copy_to_user(arg, &i, sizeof(i)))
1605 return -EFAULT;
1607 return 0;
1611 static int
1612 et61x251_vidioc_g_input(struct et61x251_device* cam, void __user * arg)
1614 int index = 0;
1616 if (copy_to_user(arg, &index, sizeof(index)))
1617 return -EFAULT;
1619 return 0;
1623 static int
1624 et61x251_vidioc_s_input(struct et61x251_device* cam, void __user * arg)
1626 int index;
1628 if (copy_from_user(&index, arg, sizeof(index)))
1629 return -EFAULT;
1631 if (index != 0)
1632 return -EINVAL;
1634 return 0;
1638 static int
1639 et61x251_vidioc_query_ctrl(struct et61x251_device* cam, void __user * arg)
1641 struct et61x251_sensor* s = &cam->sensor;
1642 struct v4l2_queryctrl qc;
1643 u8 i;
1645 if (copy_from_user(&qc, arg, sizeof(qc)))
1646 return -EFAULT;
1648 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1649 if (qc.id && qc.id == s->qctrl[i].id) {
1650 memcpy(&qc, &(s->qctrl[i]), sizeof(qc));
1651 if (copy_to_user(arg, &qc, sizeof(qc)))
1652 return -EFAULT;
1653 return 0;
1656 return -EINVAL;
1660 static int
1661 et61x251_vidioc_g_ctrl(struct et61x251_device* cam, void __user * arg)
1663 struct et61x251_sensor* s = &cam->sensor;
1664 struct v4l2_control ctrl;
1665 int err = 0;
1666 u8 i;
1668 if (!s->get_ctrl && !s->set_ctrl)
1669 return -EINVAL;
1671 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1672 return -EFAULT;
1674 if (!s->get_ctrl) {
1675 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1676 if (ctrl.id == s->qctrl[i].id) {
1677 ctrl.value = s->_qctrl[i].default_value;
1678 goto exit;
1680 return -EINVAL;
1681 } else
1682 err = s->get_ctrl(cam, &ctrl);
1684 exit:
1685 if (copy_to_user(arg, &ctrl, sizeof(ctrl)))
1686 return -EFAULT;
1688 return err;
1692 static int
1693 et61x251_vidioc_s_ctrl(struct et61x251_device* cam, void __user * arg)
1695 struct et61x251_sensor* s = &cam->sensor;
1696 struct v4l2_control ctrl;
1697 u8 i;
1698 int err = 0;
1700 if (!s->set_ctrl)
1701 return -EINVAL;
1703 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1704 return -EFAULT;
1706 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1707 if (ctrl.id == s->qctrl[i].id) {
1708 if (s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)
1709 return -EINVAL;
1710 if (ctrl.value < s->qctrl[i].minimum ||
1711 ctrl.value > s->qctrl[i].maximum)
1712 return -ERANGE;
1713 ctrl.value -= ctrl.value % s->qctrl[i].step;
1714 break;
1717 if ((err = s->set_ctrl(cam, &ctrl)))
1718 return err;
1720 s->_qctrl[i].default_value = ctrl.value;
1722 return 0;
1726 static int
1727 et61x251_vidioc_cropcap(struct et61x251_device* cam, void __user * arg)
1729 struct v4l2_cropcap* cc = &(cam->sensor.cropcap);
1731 cc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1732 cc->pixelaspect.numerator = 1;
1733 cc->pixelaspect.denominator = 1;
1735 if (copy_to_user(arg, cc, sizeof(*cc)))
1736 return -EFAULT;
1738 return 0;
1742 static int
1743 et61x251_vidioc_g_crop(struct et61x251_device* cam, void __user * arg)
1745 struct et61x251_sensor* s = &cam->sensor;
1746 struct v4l2_crop crop = {
1747 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
1750 memcpy(&(crop.c), &(s->_rect), sizeof(struct v4l2_rect));
1752 if (copy_to_user(arg, &crop, sizeof(crop)))
1753 return -EFAULT;
1755 return 0;
1759 static int
1760 et61x251_vidioc_s_crop(struct et61x251_device* cam, void __user * arg)
1762 struct et61x251_sensor* s = &cam->sensor;
1763 struct v4l2_crop crop;
1764 struct v4l2_rect* rect;
1765 struct v4l2_rect* bounds = &(s->cropcap.bounds);
1766 struct v4l2_pix_format* pix_format = &(s->pix_format);
1767 u8 scale;
1768 const enum et61x251_stream_state stream = cam->stream;
1769 const u32 nbuffers = cam->nbuffers;
1770 u32 i;
1771 int err = 0;
1773 if (copy_from_user(&crop, arg, sizeof(crop)))
1774 return -EFAULT;
1776 rect = &(crop.c);
1778 if (crop.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1779 return -EINVAL;
1781 if (cam->module_param.force_munmap)
1782 for (i = 0; i < cam->nbuffers; i++)
1783 if (cam->frame[i].vma_use_count) {
1784 DBG(3, "VIDIOC_S_CROP failed. "
1785 "Unmap the buffers first.");
1786 return -EBUSY;
1789 /* Preserve R,G or B origin */
1790 rect->left = (s->_rect.left & 1L) ? rect->left | 1L : rect->left & ~1L;
1791 rect->top = (s->_rect.top & 1L) ? rect->top | 1L : rect->top & ~1L;
1793 if (rect->width < 16)
1794 rect->width = 16;
1795 if (rect->height < 16)
1796 rect->height = 16;
1797 if (rect->width > bounds->width)
1798 rect->width = bounds->width;
1799 if (rect->height > bounds->height)
1800 rect->height = bounds->height;
1801 if (rect->left < bounds->left)
1802 rect->left = bounds->left;
1803 if (rect->top < bounds->top)
1804 rect->top = bounds->top;
1805 if (rect->left + rect->width > bounds->left + bounds->width)
1806 rect->left = bounds->left+bounds->width - rect->width;
1807 if (rect->top + rect->height > bounds->top + bounds->height)
1808 rect->top = bounds->top+bounds->height - rect->height;
1810 rect->width &= ~15L;
1811 rect->height &= ~15L;
1813 if (ET61X251_PRESERVE_IMGSCALE) {
1814 /* Calculate the actual scaling factor */
1815 u32 a, b;
1816 a = rect->width * rect->height;
1817 b = pix_format->width * pix_format->height;
1818 scale = b ? (u8)((a / b) < 4 ? 1 : 2) : 1;
1819 } else
1820 scale = 1;
1822 if (cam->stream == STREAM_ON)
1823 if ((err = et61x251_stream_interrupt(cam)))
1824 return err;
1826 if (copy_to_user(arg, &crop, sizeof(crop))) {
1827 cam->stream = stream;
1828 return -EFAULT;
1831 if (cam->module_param.force_munmap || cam->io == IO_READ)
1832 et61x251_release_buffers(cam);
1834 err = et61x251_set_crop(cam, rect);
1835 if (s->set_crop)
1836 err += s->set_crop(cam, rect);
1837 err += et61x251_set_scale(cam, scale);
1839 if (err) { /* atomic, no rollback in ioctl() */
1840 cam->state |= DEV_MISCONFIGURED;
1841 DBG(1, "VIDIOC_S_CROP failed because of hardware problems. To "
1842 "use the camera, close and open /dev/video%d again.",
1843 cam->v4ldev->minor);
1844 return -EIO;
1847 s->pix_format.width = rect->width/scale;
1848 s->pix_format.height = rect->height/scale;
1849 memcpy(&(s->_rect), rect, sizeof(*rect));
1851 if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
1852 nbuffers != et61x251_request_buffers(cam, nbuffers, cam->io)) {
1853 cam->state |= DEV_MISCONFIGURED;
1854 DBG(1, "VIDIOC_S_CROP failed because of not enough memory. To "
1855 "use the camera, close and open /dev/video%d again.",
1856 cam->v4ldev->minor);
1857 return -ENOMEM;
1860 if (cam->io == IO_READ)
1861 et61x251_empty_framequeues(cam);
1862 else if (cam->module_param.force_munmap)
1863 et61x251_requeue_outqueue(cam);
1865 cam->stream = stream;
1867 return 0;
1871 static int
1872 et61x251_vidioc_enum_framesizes(struct et61x251_device* cam, void __user * arg)
1874 struct v4l2_frmsizeenum frmsize;
1876 if (copy_from_user(&frmsize, arg, sizeof(frmsize)))
1877 return -EFAULT;
1879 if (frmsize.index != 0)
1880 return -EINVAL;
1882 if (frmsize.pixel_format != V4L2_PIX_FMT_ET61X251 &&
1883 frmsize.pixel_format != V4L2_PIX_FMT_SBGGR8)
1884 return -EINVAL;
1886 frmsize.type = V4L2_FRMSIZE_TYPE_STEPWISE;
1887 frmsize.stepwise.min_width = frmsize.stepwise.step_width = 16;
1888 frmsize.stepwise.min_height = frmsize.stepwise.step_height = 16;
1889 frmsize.stepwise.max_width = cam->sensor.cropcap.bounds.width;
1890 frmsize.stepwise.max_height = cam->sensor.cropcap.bounds.height;
1891 memset(&frmsize.reserved, 0, sizeof(frmsize.reserved));
1893 if (copy_to_user(arg, &frmsize, sizeof(frmsize)))
1894 return -EFAULT;
1896 return 0;
1900 static int
1901 et61x251_vidioc_enum_fmt(struct et61x251_device* cam, void __user * arg)
1903 struct v4l2_fmtdesc fmtd;
1905 if (copy_from_user(&fmtd, arg, sizeof(fmtd)))
1906 return -EFAULT;
1908 if (fmtd.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1909 return -EINVAL;
1911 if (fmtd.index == 0) {
1912 strcpy(fmtd.description, "bayer rgb");
1913 fmtd.pixelformat = V4L2_PIX_FMT_SBGGR8;
1914 } else if (fmtd.index == 1) {
1915 strcpy(fmtd.description, "compressed");
1916 fmtd.pixelformat = V4L2_PIX_FMT_ET61X251;
1917 fmtd.flags = V4L2_FMT_FLAG_COMPRESSED;
1918 } else
1919 return -EINVAL;
1921 fmtd.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1922 memset(&fmtd.reserved, 0, sizeof(fmtd.reserved));
1924 if (copy_to_user(arg, &fmtd, sizeof(fmtd)))
1925 return -EFAULT;
1927 return 0;
1931 static int
1932 et61x251_vidioc_g_fmt(struct et61x251_device* cam, void __user * arg)
1934 struct v4l2_format format;
1935 struct v4l2_pix_format* pfmt = &(cam->sensor.pix_format);
1937 if (copy_from_user(&format, arg, sizeof(format)))
1938 return -EFAULT;
1940 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1941 return -EINVAL;
1943 pfmt->colorspace = (pfmt->pixelformat == V4L2_PIX_FMT_ET61X251) ?
1944 0 : V4L2_COLORSPACE_SRGB;
1945 pfmt->bytesperline = (pfmt->pixelformat==V4L2_PIX_FMT_ET61X251)
1946 ? 0 : (pfmt->width * pfmt->priv) / 8;
1947 pfmt->sizeimage = pfmt->height * ((pfmt->width*pfmt->priv)/8);
1948 pfmt->field = V4L2_FIELD_NONE;
1949 memcpy(&(format.fmt.pix), pfmt, sizeof(*pfmt));
1951 if (copy_to_user(arg, &format, sizeof(format)))
1952 return -EFAULT;
1954 return 0;
1958 static int
1959 et61x251_vidioc_try_s_fmt(struct et61x251_device* cam, unsigned int cmd,
1960 void __user * arg)
1962 struct et61x251_sensor* s = &cam->sensor;
1963 struct v4l2_format format;
1964 struct v4l2_pix_format* pix;
1965 struct v4l2_pix_format* pfmt = &(s->pix_format);
1966 struct v4l2_rect* bounds = &(s->cropcap.bounds);
1967 struct v4l2_rect rect;
1968 u8 scale;
1969 const enum et61x251_stream_state stream = cam->stream;
1970 const u32 nbuffers = cam->nbuffers;
1971 u32 i;
1972 int err = 0;
1974 if (copy_from_user(&format, arg, sizeof(format)))
1975 return -EFAULT;
1977 pix = &(format.fmt.pix);
1979 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1980 return -EINVAL;
1982 memcpy(&rect, &(s->_rect), sizeof(rect));
1984 { /* calculate the actual scaling factor */
1985 u32 a, b;
1986 a = rect.width * rect.height;
1987 b = pix->width * pix->height;
1988 scale = b ? (u8)((a / b) < 4 ? 1 : 2) : 1;
1991 rect.width = scale * pix->width;
1992 rect.height = scale * pix->height;
1994 if (rect.width < 16)
1995 rect.width = 16;
1996 if (rect.height < 16)
1997 rect.height = 16;
1998 if (rect.width > bounds->left + bounds->width - rect.left)
1999 rect.width = bounds->left + bounds->width - rect.left;
2000 if (rect.height > bounds->top + bounds->height - rect.top)
2001 rect.height = bounds->top + bounds->height - rect.top;
2003 rect.width &= ~15L;
2004 rect.height &= ~15L;
2006 { /* adjust the scaling factor */
2007 u32 a, b;
2008 a = rect.width * rect.height;
2009 b = pix->width * pix->height;
2010 scale = b ? (u8)((a / b) < 4 ? 1 : 2) : 1;
2013 pix->width = rect.width / scale;
2014 pix->height = rect.height / scale;
2016 if (pix->pixelformat != V4L2_PIX_FMT_ET61X251 &&
2017 pix->pixelformat != V4L2_PIX_FMT_SBGGR8)
2018 pix->pixelformat = pfmt->pixelformat;
2019 pix->priv = pfmt->priv; /* bpp */
2020 pix->colorspace = (pix->pixelformat == V4L2_PIX_FMT_ET61X251) ?
2021 0 : V4L2_COLORSPACE_SRGB;
2022 pix->colorspace = pfmt->colorspace;
2023 pix->bytesperline = (pix->pixelformat == V4L2_PIX_FMT_ET61X251)
2024 ? 0 : (pix->width * pix->priv) / 8;
2025 pix->sizeimage = pix->height * ((pix->width * pix->priv) / 8);
2026 pix->field = V4L2_FIELD_NONE;
2028 if (cmd == VIDIOC_TRY_FMT) {
2029 if (copy_to_user(arg, &format, sizeof(format)))
2030 return -EFAULT;
2031 return 0;
2034 if (cam->module_param.force_munmap)
2035 for (i = 0; i < cam->nbuffers; i++)
2036 if (cam->frame[i].vma_use_count) {
2037 DBG(3, "VIDIOC_S_FMT failed. "
2038 "Unmap the buffers first.");
2039 return -EBUSY;
2042 if (cam->stream == STREAM_ON)
2043 if ((err = et61x251_stream_interrupt(cam)))
2044 return err;
2046 if (copy_to_user(arg, &format, sizeof(format))) {
2047 cam->stream = stream;
2048 return -EFAULT;
2051 if (cam->module_param.force_munmap || cam->io == IO_READ)
2052 et61x251_release_buffers(cam);
2054 err += et61x251_set_pix_format(cam, pix);
2055 err += et61x251_set_crop(cam, &rect);
2056 if (s->set_pix_format)
2057 err += s->set_pix_format(cam, pix);
2058 if (s->set_crop)
2059 err += s->set_crop(cam, &rect);
2060 err += et61x251_set_scale(cam, scale);
2062 if (err) { /* atomic, no rollback in ioctl() */
2063 cam->state |= DEV_MISCONFIGURED;
2064 DBG(1, "VIDIOC_S_FMT failed because of hardware problems. To "
2065 "use the camera, close and open /dev/video%d again.",
2066 cam->v4ldev->minor);
2067 return -EIO;
2070 memcpy(pfmt, pix, sizeof(*pix));
2071 memcpy(&(s->_rect), &rect, sizeof(rect));
2073 if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
2074 nbuffers != et61x251_request_buffers(cam, nbuffers, cam->io)) {
2075 cam->state |= DEV_MISCONFIGURED;
2076 DBG(1, "VIDIOC_S_FMT failed because of not enough memory. To "
2077 "use the camera, close and open /dev/video%d again.",
2078 cam->v4ldev->minor);
2079 return -ENOMEM;
2082 if (cam->io == IO_READ)
2083 et61x251_empty_framequeues(cam);
2084 else if (cam->module_param.force_munmap)
2085 et61x251_requeue_outqueue(cam);
2087 cam->stream = stream;
2089 return 0;
2093 static int
2094 et61x251_vidioc_g_jpegcomp(struct et61x251_device* cam, void __user * arg)
2096 if (copy_to_user(arg, &cam->compression,
2097 sizeof(cam->compression)))
2098 return -EFAULT;
2100 return 0;
2104 static int
2105 et61x251_vidioc_s_jpegcomp(struct et61x251_device* cam, void __user * arg)
2107 struct v4l2_jpegcompression jc;
2108 const enum et61x251_stream_state stream = cam->stream;
2109 int err = 0;
2111 if (copy_from_user(&jc, arg, sizeof(jc)))
2112 return -EFAULT;
2114 if (jc.quality != 0 && jc.quality != 1)
2115 return -EINVAL;
2117 if (cam->stream == STREAM_ON)
2118 if ((err = et61x251_stream_interrupt(cam)))
2119 return err;
2121 err += et61x251_set_compression(cam, &jc);
2122 if (err) { /* atomic, no rollback in ioctl() */
2123 cam->state |= DEV_MISCONFIGURED;
2124 DBG(1, "VIDIOC_S_JPEGCOMP failed because of hardware "
2125 "problems. To use the camera, close and open "
2126 "/dev/video%d again.", cam->v4ldev->minor);
2127 return -EIO;
2130 cam->compression.quality = jc.quality;
2132 cam->stream = stream;
2134 return 0;
2138 static int
2139 et61x251_vidioc_reqbufs(struct et61x251_device* cam, void __user * arg)
2141 struct v4l2_requestbuffers rb;
2142 u32 i;
2143 int err;
2145 if (copy_from_user(&rb, arg, sizeof(rb)))
2146 return -EFAULT;
2148 if (rb.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2149 rb.memory != V4L2_MEMORY_MMAP)
2150 return -EINVAL;
2152 if (cam->io == IO_READ) {
2153 DBG(3, "Close and open the device again to choose the mmap "
2154 "I/O method");
2155 return -EBUSY;
2158 for (i = 0; i < cam->nbuffers; i++)
2159 if (cam->frame[i].vma_use_count) {
2160 DBG(3, "VIDIOC_REQBUFS failed. "
2161 "Previous buffers are still mapped.");
2162 return -EBUSY;
2165 if (cam->stream == STREAM_ON)
2166 if ((err = et61x251_stream_interrupt(cam)))
2167 return err;
2169 et61x251_empty_framequeues(cam);
2171 et61x251_release_buffers(cam);
2172 if (rb.count)
2173 rb.count = et61x251_request_buffers(cam, rb.count, IO_MMAP);
2175 if (copy_to_user(arg, &rb, sizeof(rb))) {
2176 et61x251_release_buffers(cam);
2177 cam->io = IO_NONE;
2178 return -EFAULT;
2181 cam->io = rb.count ? IO_MMAP : IO_NONE;
2183 return 0;
2187 static int
2188 et61x251_vidioc_querybuf(struct et61x251_device* cam, void __user * arg)
2190 struct v4l2_buffer b;
2192 if (copy_from_user(&b, arg, sizeof(b)))
2193 return -EFAULT;
2195 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2196 b.index >= cam->nbuffers || cam->io != IO_MMAP)
2197 return -EINVAL;
2199 memcpy(&b, &cam->frame[b.index].buf, sizeof(b));
2201 if (cam->frame[b.index].vma_use_count)
2202 b.flags |= V4L2_BUF_FLAG_MAPPED;
2204 if (cam->frame[b.index].state == F_DONE)
2205 b.flags |= V4L2_BUF_FLAG_DONE;
2206 else if (cam->frame[b.index].state != F_UNUSED)
2207 b.flags |= V4L2_BUF_FLAG_QUEUED;
2209 if (copy_to_user(arg, &b, sizeof(b)))
2210 return -EFAULT;
2212 return 0;
2216 static int
2217 et61x251_vidioc_qbuf(struct et61x251_device* cam, void __user * arg)
2219 struct v4l2_buffer b;
2220 unsigned long lock_flags;
2222 if (copy_from_user(&b, arg, sizeof(b)))
2223 return -EFAULT;
2225 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2226 b.index >= cam->nbuffers || cam->io != IO_MMAP)
2227 return -EINVAL;
2229 if (cam->frame[b.index].state != F_UNUSED)
2230 return -EINVAL;
2232 cam->frame[b.index].state = F_QUEUED;
2234 spin_lock_irqsave(&cam->queue_lock, lock_flags);
2235 list_add_tail(&cam->frame[b.index].frame, &cam->inqueue);
2236 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2238 PDBGG("Frame #%lu queued", (unsigned long)b.index);
2240 return 0;
2244 static int
2245 et61x251_vidioc_dqbuf(struct et61x251_device* cam, struct file* filp,
2246 void __user * arg)
2248 struct v4l2_buffer b;
2249 struct et61x251_frame_t *f;
2250 unsigned long lock_flags;
2251 long timeout;
2253 if (copy_from_user(&b, arg, sizeof(b)))
2254 return -EFAULT;
2256 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io!= IO_MMAP)
2257 return -EINVAL;
2259 if (list_empty(&cam->outqueue)) {
2260 if (cam->stream == STREAM_OFF)
2261 return -EINVAL;
2262 if (filp->f_flags & O_NONBLOCK)
2263 return -EAGAIN;
2264 timeout = wait_event_interruptible_timeout
2265 ( cam->wait_frame,
2266 (!list_empty(&cam->outqueue)) ||
2267 (cam->state & DEV_DISCONNECTED) ||
2268 (cam->state & DEV_MISCONFIGURED),
2269 cam->module_param.frame_timeout *
2270 1000 * msecs_to_jiffies(1) );
2271 if (timeout < 0)
2272 return timeout;
2273 if (cam->state & DEV_DISCONNECTED)
2274 return -ENODEV;
2275 if (!timeout || (cam->state & DEV_MISCONFIGURED))
2276 return -EIO;
2279 spin_lock_irqsave(&cam->queue_lock, lock_flags);
2280 f = list_entry(cam->outqueue.next, struct et61x251_frame_t, frame);
2281 list_del(cam->outqueue.next);
2282 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2284 f->state = F_UNUSED;
2286 memcpy(&b, &f->buf, sizeof(b));
2287 if (f->vma_use_count)
2288 b.flags |= V4L2_BUF_FLAG_MAPPED;
2290 if (copy_to_user(arg, &b, sizeof(b)))
2291 return -EFAULT;
2293 PDBGG("Frame #%lu dequeued", (unsigned long)f->buf.index);
2295 return 0;
2299 static int
2300 et61x251_vidioc_streamon(struct et61x251_device* cam, void __user * arg)
2302 int type;
2304 if (copy_from_user(&type, arg, sizeof(type)))
2305 return -EFAULT;
2307 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2308 return -EINVAL;
2310 cam->stream = STREAM_ON;
2312 DBG(3, "Stream on");
2314 return 0;
2318 static int
2319 et61x251_vidioc_streamoff(struct et61x251_device* cam, void __user * arg)
2321 int type, err;
2323 if (copy_from_user(&type, arg, sizeof(type)))
2324 return -EFAULT;
2326 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2327 return -EINVAL;
2329 if (cam->stream == STREAM_ON)
2330 if ((err = et61x251_stream_interrupt(cam)))
2331 return err;
2333 et61x251_empty_framequeues(cam);
2335 DBG(3, "Stream off");
2337 return 0;
2341 static int
2342 et61x251_vidioc_g_parm(struct et61x251_device* cam, void __user * arg)
2344 struct v4l2_streamparm sp;
2346 if (copy_from_user(&sp, arg, sizeof(sp)))
2347 return -EFAULT;
2349 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2350 return -EINVAL;
2352 sp.parm.capture.extendedmode = 0;
2353 sp.parm.capture.readbuffers = cam->nreadbuffers;
2355 if (copy_to_user(arg, &sp, sizeof(sp)))
2356 return -EFAULT;
2358 return 0;
2362 static int
2363 et61x251_vidioc_s_parm(struct et61x251_device* cam, void __user * arg)
2365 struct v4l2_streamparm sp;
2367 if (copy_from_user(&sp, arg, sizeof(sp)))
2368 return -EFAULT;
2370 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2371 return -EINVAL;
2373 sp.parm.capture.extendedmode = 0;
2375 if (sp.parm.capture.readbuffers == 0)
2376 sp.parm.capture.readbuffers = cam->nreadbuffers;
2378 if (sp.parm.capture.readbuffers > ET61X251_MAX_FRAMES)
2379 sp.parm.capture.readbuffers = ET61X251_MAX_FRAMES;
2381 if (copy_to_user(arg, &sp, sizeof(sp)))
2382 return -EFAULT;
2384 cam->nreadbuffers = sp.parm.capture.readbuffers;
2386 return 0;
2390 static int et61x251_ioctl_v4l2(struct inode* inode, struct file* filp,
2391 unsigned int cmd, void __user * arg)
2393 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp));
2395 switch (cmd) {
2397 case VIDIOC_QUERYCAP:
2398 return et61x251_vidioc_querycap(cam, arg);
2400 case VIDIOC_ENUMINPUT:
2401 return et61x251_vidioc_enuminput(cam, arg);
2403 case VIDIOC_G_INPUT:
2404 return et61x251_vidioc_g_input(cam, arg);
2406 case VIDIOC_S_INPUT:
2407 return et61x251_vidioc_s_input(cam, arg);
2409 case VIDIOC_QUERYCTRL:
2410 return et61x251_vidioc_query_ctrl(cam, arg);
2412 case VIDIOC_G_CTRL:
2413 return et61x251_vidioc_g_ctrl(cam, arg);
2415 case VIDIOC_S_CTRL:
2416 return et61x251_vidioc_s_ctrl(cam, arg);
2418 case VIDIOC_CROPCAP:
2419 return et61x251_vidioc_cropcap(cam, arg);
2421 case VIDIOC_G_CROP:
2422 return et61x251_vidioc_g_crop(cam, arg);
2424 case VIDIOC_S_CROP:
2425 return et61x251_vidioc_s_crop(cam, arg);
2427 case VIDIOC_ENUM_FMT:
2428 return et61x251_vidioc_enum_fmt(cam, arg);
2430 case VIDIOC_G_FMT:
2431 return et61x251_vidioc_g_fmt(cam, arg);
2433 case VIDIOC_TRY_FMT:
2434 case VIDIOC_S_FMT:
2435 return et61x251_vidioc_try_s_fmt(cam, cmd, arg);
2437 case VIDIOC_ENUM_FRAMESIZES:
2438 return et61x251_vidioc_enum_framesizes(cam, arg);
2440 case VIDIOC_G_JPEGCOMP:
2441 return et61x251_vidioc_g_jpegcomp(cam, arg);
2443 case VIDIOC_S_JPEGCOMP:
2444 return et61x251_vidioc_s_jpegcomp(cam, arg);
2446 case VIDIOC_REQBUFS:
2447 return et61x251_vidioc_reqbufs(cam, arg);
2449 case VIDIOC_QUERYBUF:
2450 return et61x251_vidioc_querybuf(cam, arg);
2452 case VIDIOC_QBUF:
2453 return et61x251_vidioc_qbuf(cam, arg);
2455 case VIDIOC_DQBUF:
2456 return et61x251_vidioc_dqbuf(cam, filp, arg);
2458 case VIDIOC_STREAMON:
2459 return et61x251_vidioc_streamon(cam, arg);
2461 case VIDIOC_STREAMOFF:
2462 return et61x251_vidioc_streamoff(cam, arg);
2464 case VIDIOC_G_PARM:
2465 return et61x251_vidioc_g_parm(cam, arg);
2467 case VIDIOC_S_PARM:
2468 return et61x251_vidioc_s_parm(cam, arg);
2470 case VIDIOC_G_STD:
2471 case VIDIOC_S_STD:
2472 case VIDIOC_QUERYSTD:
2473 case VIDIOC_ENUMSTD:
2474 case VIDIOC_QUERYMENU:
2475 case VIDIOC_ENUM_FRAMEINTERVALS:
2476 return -EINVAL;
2478 default:
2479 return -EINVAL;
2485 static int et61x251_ioctl(struct inode* inode, struct file* filp,
2486 unsigned int cmd, unsigned long arg)
2488 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp));
2489 int err = 0;
2491 if (mutex_lock_interruptible(&cam->fileop_mutex))
2492 return -ERESTARTSYS;
2494 if (cam->state & DEV_DISCONNECTED) {
2495 DBG(1, "Device not present");
2496 mutex_unlock(&cam->fileop_mutex);
2497 return -ENODEV;
2500 if (cam->state & DEV_MISCONFIGURED) {
2501 DBG(1, "The camera is misconfigured. Close and open it "
2502 "again.");
2503 mutex_unlock(&cam->fileop_mutex);
2504 return -EIO;
2507 V4LDBG(3, "et61x251", cmd);
2509 err = et61x251_ioctl_v4l2(inode, filp, cmd, (void __user *)arg);
2511 mutex_unlock(&cam->fileop_mutex);
2513 return err;
2517 static const struct file_operations et61x251_fops = {
2518 .owner = THIS_MODULE,
2519 .open = et61x251_open,
2520 .release = et61x251_release,
2521 .ioctl = et61x251_ioctl,
2522 .compat_ioctl = v4l_compat_ioctl32,
2523 .read = et61x251_read,
2524 .poll = et61x251_poll,
2525 .mmap = et61x251_mmap,
2526 .llseek = no_llseek,
2529 /*****************************************************************************/
2531 /* It exists a single interface only. We do not need to validate anything. */
2532 static int
2533 et61x251_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
2535 struct usb_device *udev = interface_to_usbdev(intf);
2536 struct et61x251_device* cam;
2537 static unsigned int dev_nr = 0;
2538 unsigned int i;
2539 int err = 0;
2541 if (!(cam = kzalloc(sizeof(struct et61x251_device), GFP_KERNEL)))
2542 return -ENOMEM;
2544 cam->usbdev = udev;
2546 if (!(cam->control_buffer = kzalloc(8, GFP_KERNEL))) {
2547 DBG(1, "kmalloc() failed");
2548 err = -ENOMEM;
2549 goto fail;
2552 if (!(cam->v4ldev = video_device_alloc())) {
2553 DBG(1, "video_device_alloc() failed");
2554 err = -ENOMEM;
2555 goto fail;
2558 DBG(2, "ET61X[12]51 PC Camera Controller detected "
2559 "(vid/pid 0x%04X:0x%04X)",id->idVendor, id->idProduct);
2561 for (i = 0; et61x251_sensor_table[i]; i++) {
2562 err = et61x251_sensor_table[i](cam);
2563 if (!err)
2564 break;
2567 if (!err)
2568 DBG(2, "%s image sensor detected", cam->sensor.name);
2569 else {
2570 DBG(1, "No supported image sensor detected");
2571 err = -ENODEV;
2572 goto fail;
2575 if (et61x251_init(cam)) {
2576 DBG(1, "Initialization failed. I will retry on open().");
2577 cam->state |= DEV_MISCONFIGURED;
2580 strcpy(cam->v4ldev->name, "ET61X[12]51 PC Camera");
2581 cam->v4ldev->owner = THIS_MODULE;
2582 cam->v4ldev->type = VID_TYPE_CAPTURE | VID_TYPE_SCALES;
2583 cam->v4ldev->hardware = 0;
2584 cam->v4ldev->fops = &et61x251_fops;
2585 cam->v4ldev->minor = video_nr[dev_nr];
2586 cam->v4ldev->release = video_device_release;
2587 video_set_drvdata(cam->v4ldev, cam);
2589 init_completion(&cam->probe);
2591 err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER,
2592 video_nr[dev_nr]);
2593 if (err) {
2594 DBG(1, "V4L2 device registration failed");
2595 if (err == -ENFILE && video_nr[dev_nr] == -1)
2596 DBG(1, "Free /dev/videoX node not found");
2597 video_nr[dev_nr] = -1;
2598 dev_nr = (dev_nr < ET61X251_MAX_DEVICES-1) ? dev_nr+1 : 0;
2599 complete_all(&cam->probe);
2600 goto fail;
2603 DBG(2, "V4L2 device registered as /dev/video%d", cam->v4ldev->minor);
2605 cam->module_param.force_munmap = force_munmap[dev_nr];
2606 cam->module_param.frame_timeout = frame_timeout[dev_nr];
2608 dev_nr = (dev_nr < ET61X251_MAX_DEVICES-1) ? dev_nr+1 : 0;
2610 #ifdef CONFIG_VIDEO_ADV_DEBUG
2611 err = et61x251_create_sysfs(cam);
2612 if (!err)
2613 DBG(2, "Optional device control through 'sysfs' "
2614 "interface ready");
2615 else
2616 DBG(2, "Failed to create 'sysfs' interface for optional "
2617 "device controlling. Error #%d", err);
2618 #else
2619 DBG(2, "Optional device control through 'sysfs' interface disabled");
2620 DBG(3, "Compile the kernel with the 'CONFIG_VIDEO_ADV_DEBUG' "
2621 "configuration option to enable it.");
2622 #endif
2624 usb_set_intfdata(intf, cam);
2625 kref_init(&cam->kref);
2626 usb_get_dev(cam->usbdev);
2628 complete_all(&cam->probe);
2630 return 0;
2632 fail:
2633 if (cam) {
2634 kfree(cam->control_buffer);
2635 if (cam->v4ldev)
2636 video_device_release(cam->v4ldev);
2637 kfree(cam);
2639 return err;
2643 static void et61x251_usb_disconnect(struct usb_interface* intf)
2645 struct et61x251_device* cam;
2647 down_write(&et61x251_dev_lock);
2649 cam = usb_get_intfdata(intf);
2651 DBG(2, "Disconnecting %s...", cam->v4ldev->name);
2653 if (cam->users) {
2654 DBG(2, "Device /dev/video%d is open! Deregistration and "
2655 "memory deallocation are deferred.",
2656 cam->v4ldev->minor);
2657 cam->state |= DEV_MISCONFIGURED;
2658 et61x251_stop_transfer(cam);
2659 cam->state |= DEV_DISCONNECTED;
2660 wake_up_interruptible(&cam->wait_frame);
2661 wake_up(&cam->wait_stream);
2662 } else
2663 cam->state |= DEV_DISCONNECTED;
2665 wake_up_interruptible_all(&cam->wait_open);
2667 kref_put(&cam->kref, et61x251_release_resources);
2669 up_write(&et61x251_dev_lock);
2673 static struct usb_driver et61x251_usb_driver = {
2674 .name = "et61x251",
2675 .id_table = et61x251_id_table,
2676 .probe = et61x251_usb_probe,
2677 .disconnect = et61x251_usb_disconnect,
2680 /*****************************************************************************/
2682 static int __init et61x251_module_init(void)
2684 int err = 0;
2686 KDBG(2, ET61X251_MODULE_NAME " v" ET61X251_MODULE_VERSION);
2687 KDBG(3, ET61X251_MODULE_AUTHOR);
2689 if ((err = usb_register(&et61x251_usb_driver)))
2690 KDBG(1, "usb_register() failed");
2692 return err;
2696 static void __exit et61x251_module_exit(void)
2698 usb_deregister(&et61x251_usb_driver);
2702 module_init(et61x251_module_init);
2703 module_exit(et61x251_module_exit);