1 /***************************************************************************
2 * V4L2 driver for ET61X[12]51 PC Camera Controllers *
4 * Copyright (C) 2006-2007 by Luca Risolia <luca.risolia@studio.unibo.it> *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
19 ***************************************************************************/
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/kernel.h>
24 #include <linux/param.h>
25 #include <linux/errno.h>
26 #include <linux/slab.h>
27 #include <linux/device.h>
29 #include <linux/delay.h>
30 #include <linux/compiler.h>
31 #include <linux/ioctl.h>
32 #include <linux/poll.h>
33 #include <linux/stat.h>
35 #include <linux/vmalloc.h>
36 #include <linux/page-flags.h>
37 #include <media/v4l2-ioctl.h>
38 #include <asm/byteorder.h>
40 #include <asm/uaccess.h>
44 /*****************************************************************************/
46 #define ET61X251_MODULE_NAME "V4L2 driver for ET61X[12]51 " \
47 "PC Camera Controllers"
48 #define ET61X251_MODULE_AUTHOR "(C) 2006-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."
72 "\nvideo_nr=-1,2,-1 would assign minor number 2 to"
73 "\nthe second registered camera and use auto for the first"
74 "\none and for every other camera."
77 static short force_munmap
[] = {[0 ... ET61X251_MAX_DEVICES
-1] =
78 ET61X251_FORCE_MUNMAP
};
79 module_param_array(force_munmap
, bool, NULL
, 0444);
80 MODULE_PARM_DESC(force_munmap
,
81 "\n<0|1[,...]> Force the application to unmap previously"
82 "\nmapped buffer memory before calling any VIDIOC_S_CROP or"
83 "\nVIDIOC_S_FMT ioctl's. Not all the applications support"
84 "\nthis feature. This parameter is specific for each"
86 "\n 0 = do not force memory unmapping"
87 "\n 1 = force memory unmapping (save memory)"
88 "\nDefault value is "__MODULE_STRING(ET61X251_FORCE_MUNMAP
)"."
91 static unsigned int frame_timeout
[] = {[0 ... ET61X251_MAX_DEVICES
-1] =
92 ET61X251_FRAME_TIMEOUT
};
93 module_param_array(frame_timeout
, uint
, NULL
, 0644);
94 MODULE_PARM_DESC(frame_timeout
,
95 "\n<n[,...]> Timeout for a video frame in seconds."
96 "\nThis parameter is specific for each detected camera."
98 __MODULE_STRING(ET61X251_FRAME_TIMEOUT
)"."
101 #ifdef ET61X251_DEBUG
102 static unsigned short debug
= ET61X251_DEBUG_LEVEL
;
103 module_param(debug
, ushort
, 0644);
104 MODULE_PARM_DESC(debug
,
105 "\n<n> Debugging information level, from 0 to 3:"
106 "\n0 = none (use carefully)"
107 "\n1 = critical errors"
108 "\n2 = significant informations"
109 "\n3 = more verbose messages"
110 "\nLevel 3 is useful for testing only, when only "
111 "one device is used."
112 "\nDefault value is "__MODULE_STRING(ET61X251_DEBUG_LEVEL
)"."
116 /*****************************************************************************/
119 et61x251_request_buffers(struct et61x251_device
* cam
, u32 count
,
120 enum et61x251_io_method io
)
122 struct v4l2_pix_format
* p
= &(cam
->sensor
.pix_format
);
123 struct v4l2_rect
* r
= &(cam
->sensor
.cropcap
.bounds
);
124 const size_t imagesize
= cam
->module_param
.force_munmap
||
126 (p
->width
* p
->height
* p
->priv
) / 8 :
127 (r
->width
* r
->height
* p
->priv
) / 8;
131 if (count
> ET61X251_MAX_FRAMES
)
132 count
= ET61X251_MAX_FRAMES
;
134 cam
->nbuffers
= count
;
135 while (cam
->nbuffers
> 0) {
136 if ((buff
= vmalloc_32_user(cam
->nbuffers
*
137 PAGE_ALIGN(imagesize
))))
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
)
161 vfree(cam
->frame
[0].bufmem
);
164 cam
->frame_current
= NULL
;
168 static void et61x251_empty_framequeues(struct et61x251_device
* cam
)
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
) {
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
;
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
;
219 res
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0), 0x00, 0x41,
220 0, index
, buff
, 1, ET61X251_CTRL_TIMEOUT
);
222 DBG(3, "Failed to write a register (value 0x%02X, index "
223 "0x%02X, error %d)", value
, index
, res
);
231 static int et61x251_read_reg(struct et61x251_device
* cam
, u16 index
)
233 struct usb_device
* udev
= cam
->usbdev
;
234 u8
* buff
= cam
->control_buffer
;
237 res
= usb_control_msg(udev
, usb_rcvctrlpipe(udev
, 0), 0x00, 0xc1,
238 0, index
, buff
, 1, ET61X251_CTRL_TIMEOUT
);
240 DBG(3, "Failed to read a register (index 0x%02X, error %d)",
243 return (res
>= 0) ? (int)(*buff
) : -1;
248 et61x251_i2c_wait(struct et61x251_device
* cam
,
249 const struct et61x251_sensor
* sensor
)
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))
259 r
= et61x251_read_reg(cam
, 0x8b);
260 if (!(r
& 0x01) && (r
>= 0))
265 udelay(8*8); /* minimum for sensors at 400kHz */
273 et61x251_i2c_raw_write(struct et61x251_device
* cam
, u8 n
, u8 data1
, u8 data2
,
274 u8 data3
, u8 data4
, u8 data5
, u8 data6
, u8 data7
,
275 u8 data8
, u8 address
)
277 struct usb_device
* udev
= cam
->usbdev
;
278 u8
* data
= cam
->control_buffer
;
288 res
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0), 0x00, 0x41,
289 0, 0x81, data
, n
-1, ET61X251_CTRL_TIMEOUT
);
294 data
[1] = cam
->sensor
.i2c_slave_id
;
295 data
[2] = cam
->sensor
.rsta
| 0x02 | (n
<< 4);
296 res
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0), 0x00, 0x41,
297 0, 0x88, data
, 3, ET61X251_CTRL_TIMEOUT
);
301 /* Start writing through the serial interface */
303 res
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0), 0x00, 0x41,
304 0, 0x80, data
, 1, ET61X251_CTRL_TIMEOUT
);
308 err
+= et61x251_i2c_wait(cam
, &cam
->sensor
);
311 DBG(3, "I2C raw write failed for %s image sensor",
314 PDBGG("I2C raw write: %u bytes, address = 0x%02X, data1 = 0x%02X, "
315 "data2 = 0x%02X, data3 = 0x%02X, data4 = 0x%02X, data5 = 0x%02X,"
316 " data6 = 0x%02X, data7 = 0x%02X, data8 = 0x%02X", n
, address
,
317 data1
, data2
, data3
, data4
, data5
, data6
, data7
, data8
);
324 /*****************************************************************************/
326 static void et61x251_urb_complete(struct urb
*urb
)
328 struct et61x251_device
* cam
= urb
->context
;
329 struct et61x251_frame_t
** f
;
334 if (urb
->status
== -ENOENT
)
337 f
= &cam
->frame_current
;
339 if (cam
->stream
== STREAM_INTERRUPT
) {
340 cam
->stream
= STREAM_OFF
;
342 (*f
)->state
= F_QUEUED
;
343 DBG(3, "Stream interrupted");
344 wake_up(&cam
->wait_stream
);
347 if (cam
->state
& DEV_DISCONNECTED
)
350 if (cam
->state
& DEV_MISCONFIGURED
) {
351 wake_up_interruptible(&cam
->wait_frame
);
355 if (cam
->stream
== STREAM_OFF
|| list_empty(&cam
->inqueue
))
359 (*f
) = list_entry(cam
->inqueue
.next
, struct et61x251_frame_t
,
362 imagesize
= (cam
->sensor
.pix_format
.width
*
363 cam
->sensor
.pix_format
.height
*
364 cam
->sensor
.pix_format
.priv
) / 8;
366 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
367 unsigned int len
, status
;
370 const u8 VOID_BYTES
= 6;
373 len
= urb
->iso_frame_desc
[i
].actual_length
;
374 status
= urb
->iso_frame_desc
[i
].status
;
375 pos
= urb
->iso_frame_desc
[i
].offset
+ urb
->transfer_buffer
;
378 DBG(3, "Error in isochronous frame");
379 (*f
)->state
= F_ERROR
;
385 sof
= ((*b1
& 0x3f) == 63);
386 imglen
= ((*b1
& 0xc0) << 2) | *b2
;
388 PDBGG("Isochrnous frame: length %u, #%u i, image length %zu",
391 if ((*f
)->state
== F_QUEUED
|| (*f
)->state
== F_ERROR
)
394 (*f
)->state
= F_GRABBING
;
395 (*f
)->buf
.bytesused
= 0;
396 do_gettimeofday(&(*f
)->buf
.timestamp
);
398 DBG(3, "SOF detected: new video frame");
401 if ((*f
)->state
== F_GRABBING
) {
402 if (sof
&& (*f
)->buf
.bytesused
) {
403 if (cam
->sensor
.pix_format
.pixelformat
==
404 V4L2_PIX_FMT_ET61X251
)
407 DBG(3, "Not expected SOF detected "
409 (unsigned long)(*f
)->buf
.bytesused
);
410 (*f
)->state
= F_ERROR
;
415 if ((*f
)->buf
.bytesused
+ imglen
> imagesize
) {
416 DBG(3, "Video frame size exceeded");
417 (*f
)->state
= F_ERROR
;
423 memcpy((*f
)->bufmem
+(*f
)->buf
.bytesused
, pos
, imglen
);
424 (*f
)->buf
.bytesused
+= imglen
;
426 if ((*f
)->buf
.bytesused
== imagesize
) {
429 b
= (*f
)->buf
.bytesused
;
430 (*f
)->state
= F_DONE
;
431 (*f
)->buf
.sequence
= ++cam
->frame_count
;
432 spin_lock(&cam
->queue_lock
);
433 list_move_tail(&(*f
)->frame
, &cam
->outqueue
);
434 if (!list_empty(&cam
->inqueue
))
435 (*f
) = list_entry(cam
->inqueue
.next
,
436 struct et61x251_frame_t
,
440 spin_unlock(&cam
->queue_lock
);
441 DBG(3, "Video frame captured: : %lu bytes",
448 cam
->sensor
.pix_format
.pixelformat
==
449 V4L2_PIX_FMT_ET61X251
)
456 urb
->dev
= cam
->usbdev
;
457 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
458 if (err
< 0 && err
!= -EPERM
) {
459 cam
->state
|= DEV_MISCONFIGURED
;
460 DBG(1, "usb_submit_urb() failed");
463 wake_up_interruptible(&cam
->wait_frame
);
467 static int et61x251_start_transfer(struct et61x251_device
* cam
)
469 struct usb_device
*udev
= cam
->usbdev
;
471 struct usb_host_interface
* altsetting
= usb_altnum_to_altsetting(
472 usb_ifnum_to_if(udev
, 0),
473 ET61X251_ALTERNATE_SETTING
);
474 const unsigned int psz
= le16_to_cpu(altsetting
->
475 endpoint
[0].desc
.wMaxPacketSize
);
479 for (i
= 0; i
< ET61X251_URBS
; i
++) {
480 cam
->transfer_buffer
[i
] = kzalloc(ET61X251_ISO_PACKETS
* psz
,
482 if (!cam
->transfer_buffer
[i
]) {
484 DBG(1, "Not enough memory");
489 for (i
= 0; i
< ET61X251_URBS
; i
++) {
490 urb
= usb_alloc_urb(ET61X251_ISO_PACKETS
, GFP_KERNEL
);
494 DBG(1, "usb_alloc_urb() failed");
499 urb
->pipe
= usb_rcvisocpipe(udev
, 1);
500 urb
->transfer_flags
= URB_ISO_ASAP
;
501 urb
->number_of_packets
= ET61X251_ISO_PACKETS
;
502 urb
->complete
= et61x251_urb_complete
;
503 urb
->transfer_buffer
= cam
->transfer_buffer
[i
];
504 urb
->transfer_buffer_length
= psz
* ET61X251_ISO_PACKETS
;
506 for (j
= 0; j
< ET61X251_ISO_PACKETS
; j
++) {
507 urb
->iso_frame_desc
[j
].offset
= psz
* j
;
508 urb
->iso_frame_desc
[j
].length
= psz
;
512 err
= et61x251_write_reg(cam
, 0x01, 0x03);
513 err
= et61x251_write_reg(cam
, 0x00, 0x03);
514 err
= et61x251_write_reg(cam
, 0x08, 0x03);
517 DBG(1, "I/O hardware error");
521 err
= usb_set_interface(udev
, 0, ET61X251_ALTERNATE_SETTING
);
523 DBG(1, "usb_set_interface() failed");
527 cam
->frame_current
= NULL
;
529 for (i
= 0; i
< ET61X251_URBS
; i
++) {
530 err
= usb_submit_urb(cam
->urb
[i
], GFP_KERNEL
);
532 for (j
= i
-1; j
>= 0; j
--)
533 usb_kill_urb(cam
->urb
[j
]);
534 DBG(1, "usb_submit_urb() failed, error %d", err
);
542 for (i
= 0; (i
< ET61X251_URBS
) && cam
->urb
[i
]; i
++)
543 usb_free_urb(cam
->urb
[i
]);
546 for (i
= 0; (i
< ET61X251_URBS
) && cam
->transfer_buffer
[i
]; i
++)
547 kfree(cam
->transfer_buffer
[i
]);
553 static int et61x251_stop_transfer(struct et61x251_device
* cam
)
555 struct usb_device
*udev
= cam
->usbdev
;
559 if (cam
->state
& DEV_DISCONNECTED
)
562 for (i
= ET61X251_URBS
-1; i
>= 0; i
--) {
563 usb_kill_urb(cam
->urb
[i
]);
564 usb_free_urb(cam
->urb
[i
]);
565 kfree(cam
->transfer_buffer
[i
]);
568 err
= usb_set_interface(udev
, 0, 0); /* 0 Mb/s */
570 DBG(3, "usb_set_interface() failed");
576 static int et61x251_stream_interrupt(struct et61x251_device
* cam
)
580 cam
->stream
= STREAM_INTERRUPT
;
581 timeout
= wait_event_timeout(cam
->wait_stream
,
582 (cam
->stream
== STREAM_OFF
) ||
583 (cam
->state
& DEV_DISCONNECTED
),
584 ET61X251_URB_TIMEOUT
);
585 if (cam
->state
& DEV_DISCONNECTED
)
587 else if (cam
->stream
!= STREAM_OFF
) {
588 cam
->state
|= DEV_MISCONFIGURED
;
589 DBG(1, "URB timeout reached. The camera is misconfigured. To "
590 "use it, close and open %s again.",
591 video_device_node_name(cam
->v4ldev
));
598 /*****************************************************************************/
600 #ifdef CONFIG_VIDEO_ADV_DEBUG
602 static int et61x251_i2c_try_read(struct et61x251_device
* cam
,
603 const struct et61x251_sensor
* sensor
,
606 struct usb_device
* udev
= cam
->usbdev
;
607 u8
* data
= cam
->control_buffer
;
611 data
[1] = cam
->sensor
.i2c_slave_id
;
612 data
[2] = cam
->sensor
.rsta
| 0x10;
613 data
[3] = !(et61x251_read_reg(cam
, 0x8b) & 0x02);
614 res
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0), 0x00, 0x41,
615 0, 0x88, data
, 4, ET61X251_CTRL_TIMEOUT
);
619 err
+= et61x251_i2c_wait(cam
, sensor
);
621 res
= usb_control_msg(udev
, usb_rcvctrlpipe(udev
, 0), 0x00, 0xc1,
622 0, 0x80, data
, 8, ET61X251_CTRL_TIMEOUT
);
627 DBG(3, "I2C read failed for %s image sensor", sensor
->name
);
629 PDBGG("I2C read: address 0x%02X, value: 0x%02X", address
, data
[0]);
631 return err
? -1 : (int)data
[0];
635 static int et61x251_i2c_try_write(struct et61x251_device
* cam
,
636 const struct et61x251_sensor
* sensor
,
637 u8 address
, u8 value
)
639 struct usb_device
* udev
= cam
->usbdev
;
640 u8
* data
= cam
->control_buffer
;
644 data
[1] = cam
->sensor
.i2c_slave_id
;
645 data
[2] = cam
->sensor
.rsta
| 0x12;
646 res
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0), 0x00, 0x41,
647 0, 0x88, data
, 3, ET61X251_CTRL_TIMEOUT
);
652 res
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0), 0x00, 0x41,
653 0, 0x80, data
, 1, ET61X251_CTRL_TIMEOUT
);
657 err
+= et61x251_i2c_wait(cam
, sensor
);
660 DBG(3, "I2C write failed for %s image sensor", sensor
->name
);
662 PDBGG("I2C write: address 0x%02X, value: 0x%02X", address
, value
);
667 static int et61x251_i2c_read(struct et61x251_device
* cam
, u8 address
)
669 return et61x251_i2c_try_read(cam
, &cam
->sensor
, address
);
672 static int et61x251_i2c_write(struct et61x251_device
* cam
,
673 u8 address
, u8 value
)
675 return et61x251_i2c_try_write(cam
, &cam
->sensor
, address
, value
);
678 static u8
et61x251_strtou8(const char* buff
, size_t len
, ssize_t
* count
)
685 strncpy(str
, buff
, len
);
688 strncpy(str
, buff
, 4);
692 val
= simple_strtoul(str
, &endp
, 0);
696 *count
= (ssize_t
)(endp
- str
);
697 if ((*count
) && (len
== *count
+1) && (buff
[*count
] == '\n'))
704 NOTE 1: being inside one of the following methods implies that the v4l
705 device exists for sure (see kobjects and reference counters)
706 NOTE 2: buffers are PAGE_SIZE long
709 static ssize_t
et61x251_show_reg(struct device
* cd
,
710 struct device_attribute
*attr
, char* buf
)
712 struct et61x251_device
* cam
;
715 if (mutex_lock_interruptible(&et61x251_sysfs_lock
))
718 cam
= video_get_drvdata(to_video_device(cd
));
720 mutex_unlock(&et61x251_sysfs_lock
);
724 count
= sprintf(buf
, "%u\n", cam
->sysfs
.reg
);
726 mutex_unlock(&et61x251_sysfs_lock
);
733 et61x251_store_reg(struct device
* cd
,
734 struct device_attribute
*attr
, const char* buf
, size_t len
)
736 struct et61x251_device
* cam
;
740 if (mutex_lock_interruptible(&et61x251_sysfs_lock
))
743 cam
= video_get_drvdata(to_video_device(cd
));
745 mutex_unlock(&et61x251_sysfs_lock
);
749 index
= et61x251_strtou8(buf
, len
, &count
);
750 if (index
> 0x8e || !count
) {
751 mutex_unlock(&et61x251_sysfs_lock
);
755 cam
->sysfs
.reg
= index
;
757 DBG(2, "Moved ET61X[12]51 register index to 0x%02X", cam
->sysfs
.reg
);
758 DBG(3, "Written bytes: %zd", count
);
760 mutex_unlock(&et61x251_sysfs_lock
);
766 static ssize_t
et61x251_show_val(struct device
* cd
,
767 struct device_attribute
*attr
, char* buf
)
769 struct et61x251_device
* cam
;
773 if (mutex_lock_interruptible(&et61x251_sysfs_lock
))
776 cam
= video_get_drvdata(to_video_device(cd
));
778 mutex_unlock(&et61x251_sysfs_lock
);
782 if ((val
= et61x251_read_reg(cam
, cam
->sysfs
.reg
)) < 0) {
783 mutex_unlock(&et61x251_sysfs_lock
);
787 count
= sprintf(buf
, "%d\n", val
);
789 DBG(3, "Read bytes: %zd", count
);
791 mutex_unlock(&et61x251_sysfs_lock
);
798 et61x251_store_val(struct device
* cd
, struct device_attribute
*attr
,
799 const char* buf
, size_t len
)
801 struct et61x251_device
* cam
;
806 if (mutex_lock_interruptible(&et61x251_sysfs_lock
))
809 cam
= video_get_drvdata(to_video_device(cd
));
811 mutex_unlock(&et61x251_sysfs_lock
);
815 value
= et61x251_strtou8(buf
, len
, &count
);
817 mutex_unlock(&et61x251_sysfs_lock
);
821 err
= et61x251_write_reg(cam
, value
, cam
->sysfs
.reg
);
823 mutex_unlock(&et61x251_sysfs_lock
);
827 DBG(2, "Written ET61X[12]51 reg. 0x%02X, val. 0x%02X",
828 cam
->sysfs
.reg
, value
);
829 DBG(3, "Written bytes: %zd", count
);
831 mutex_unlock(&et61x251_sysfs_lock
);
837 static ssize_t
et61x251_show_i2c_reg(struct device
* cd
,
838 struct device_attribute
*attr
, char* buf
)
840 struct et61x251_device
* cam
;
843 if (mutex_lock_interruptible(&et61x251_sysfs_lock
))
846 cam
= video_get_drvdata(to_video_device(cd
));
848 mutex_unlock(&et61x251_sysfs_lock
);
852 count
= sprintf(buf
, "%u\n", cam
->sysfs
.i2c_reg
);
854 DBG(3, "Read bytes: %zd", count
);
856 mutex_unlock(&et61x251_sysfs_lock
);
863 et61x251_store_i2c_reg(struct device
* cd
, struct device_attribute
*attr
,
864 const char* buf
, size_t len
)
866 struct et61x251_device
* cam
;
870 if (mutex_lock_interruptible(&et61x251_sysfs_lock
))
873 cam
= video_get_drvdata(to_video_device(cd
));
875 mutex_unlock(&et61x251_sysfs_lock
);
879 index
= et61x251_strtou8(buf
, len
, &count
);
881 mutex_unlock(&et61x251_sysfs_lock
);
885 cam
->sysfs
.i2c_reg
= index
;
887 DBG(2, "Moved sensor register index to 0x%02X", cam
->sysfs
.i2c_reg
);
888 DBG(3, "Written bytes: %zd", count
);
890 mutex_unlock(&et61x251_sysfs_lock
);
896 static ssize_t
et61x251_show_i2c_val(struct device
* cd
,
897 struct device_attribute
*attr
, char* buf
)
899 struct et61x251_device
* cam
;
903 if (mutex_lock_interruptible(&et61x251_sysfs_lock
))
906 cam
= video_get_drvdata(to_video_device(cd
));
908 mutex_unlock(&et61x251_sysfs_lock
);
912 if (!(cam
->sensor
.sysfs_ops
& ET61X251_I2C_READ
)) {
913 mutex_unlock(&et61x251_sysfs_lock
);
917 if ((val
= et61x251_i2c_read(cam
, cam
->sysfs
.i2c_reg
)) < 0) {
918 mutex_unlock(&et61x251_sysfs_lock
);
922 count
= sprintf(buf
, "%d\n", val
);
924 DBG(3, "Read bytes: %zd", count
);
926 mutex_unlock(&et61x251_sysfs_lock
);
933 et61x251_store_i2c_val(struct device
* cd
, struct device_attribute
*attr
,
934 const char* buf
, size_t len
)
936 struct et61x251_device
* cam
;
941 if (mutex_lock_interruptible(&et61x251_sysfs_lock
))
944 cam
= video_get_drvdata(to_video_device(cd
));
946 mutex_unlock(&et61x251_sysfs_lock
);
950 if (!(cam
->sensor
.sysfs_ops
& ET61X251_I2C_READ
)) {
951 mutex_unlock(&et61x251_sysfs_lock
);
955 value
= et61x251_strtou8(buf
, len
, &count
);
957 mutex_unlock(&et61x251_sysfs_lock
);
961 err
= et61x251_i2c_write(cam
, cam
->sysfs
.i2c_reg
, value
);
963 mutex_unlock(&et61x251_sysfs_lock
);
967 DBG(2, "Written sensor reg. 0x%02X, val. 0x%02X",
968 cam
->sysfs
.i2c_reg
, value
);
969 DBG(3, "Written bytes: %zd", count
);
971 mutex_unlock(&et61x251_sysfs_lock
);
977 static DEVICE_ATTR(reg
, S_IRUGO
| S_IWUSR
,
978 et61x251_show_reg
, et61x251_store_reg
);
979 static DEVICE_ATTR(val
, S_IRUGO
| S_IWUSR
,
980 et61x251_show_val
, et61x251_store_val
);
981 static DEVICE_ATTR(i2c_reg
, S_IRUGO
| S_IWUSR
,
982 et61x251_show_i2c_reg
, et61x251_store_i2c_reg
);
983 static DEVICE_ATTR(i2c_val
, S_IRUGO
| S_IWUSR
,
984 et61x251_show_i2c_val
, et61x251_store_i2c_val
);
987 static int et61x251_create_sysfs(struct et61x251_device
* cam
)
989 struct device
*classdev
= &(cam
->v4ldev
->dev
);
992 if ((err
= device_create_file(classdev
, &dev_attr_reg
)))
994 if ((err
= device_create_file(classdev
, &dev_attr_val
)))
997 if (cam
->sensor
.sysfs_ops
) {
998 if ((err
= device_create_file(classdev
, &dev_attr_i2c_reg
)))
1000 if ((err
= device_create_file(classdev
, &dev_attr_i2c_val
)))
1005 if (cam
->sensor
.sysfs_ops
)
1006 device_remove_file(classdev
, &dev_attr_i2c_reg
);
1008 device_remove_file(classdev
, &dev_attr_val
);
1010 device_remove_file(classdev
, &dev_attr_reg
);
1014 #endif /* CONFIG_VIDEO_ADV_DEBUG */
1016 /*****************************************************************************/
1019 et61x251_set_pix_format(struct et61x251_device
* cam
,
1020 struct v4l2_pix_format
* pix
)
1024 if ((r
= et61x251_read_reg(cam
, 0x12)) < 0)
1026 if (pix
->pixelformat
== V4L2_PIX_FMT_ET61X251
)
1027 err
+= et61x251_write_reg(cam
, r
& 0xfd, 0x12);
1029 err
+= et61x251_write_reg(cam
, r
| 0x02, 0x12);
1031 return err
? -EIO
: 0;
1036 et61x251_set_compression(struct et61x251_device
* cam
,
1037 struct v4l2_jpegcompression
* compression
)
1041 if ((r
= et61x251_read_reg(cam
, 0x12)) < 0)
1043 if (compression
->quality
== 0)
1044 err
+= et61x251_write_reg(cam
, r
& 0xfb, 0x12);
1046 err
+= et61x251_write_reg(cam
, r
| 0x04, 0x12);
1048 return err
? -EIO
: 0;
1052 static int et61x251_set_scale(struct et61x251_device
* cam
, u8 scale
)
1056 r
= et61x251_read_reg(cam
, 0x12);
1061 err
+= et61x251_write_reg(cam
, r
& ~0x01, 0x12);
1062 else if (scale
== 2)
1063 err
+= et61x251_write_reg(cam
, r
| 0x01, 0x12);
1068 PDBGG("Scaling factor: %u", scale
);
1075 et61x251_set_crop(struct et61x251_device
* cam
, struct v4l2_rect
* rect
)
1077 struct et61x251_sensor
* s
= &cam
->sensor
;
1078 u16 fmw_sx
= (u16
)(rect
->left
- s
->cropcap
.bounds
.left
+
1079 s
->active_pixel
.left
),
1080 fmw_sy
= (u16
)(rect
->top
- s
->cropcap
.bounds
.top
+
1081 s
->active_pixel
.top
),
1082 fmw_length
= (u16
)(rect
->width
),
1083 fmw_height
= (u16
)(rect
->height
);
1086 err
+= et61x251_write_reg(cam
, fmw_sx
& 0xff, 0x69);
1087 err
+= et61x251_write_reg(cam
, fmw_sy
& 0xff, 0x6a);
1088 err
+= et61x251_write_reg(cam
, fmw_length
& 0xff, 0x6b);
1089 err
+= et61x251_write_reg(cam
, fmw_height
& 0xff, 0x6c);
1090 err
+= et61x251_write_reg(cam
, (fmw_sx
>> 8) | ((fmw_sy
& 0x300) >> 6)
1091 | ((fmw_length
& 0x300) >> 4)
1092 | ((fmw_height
& 0x300) >> 2), 0x6d);
1096 PDBGG("fmw_sx, fmw_sy, fmw_length, fmw_height: %u %u %u %u",
1097 fmw_sx
, fmw_sy
, fmw_length
, fmw_height
);
1103 static int et61x251_init(struct et61x251_device
* cam
)
1105 struct et61x251_sensor
* s
= &cam
->sensor
;
1106 struct v4l2_control ctrl
;
1107 struct v4l2_queryctrl
*qctrl
;
1108 struct v4l2_rect
* rect
;
1112 if (!(cam
->state
& DEV_INITIALIZED
)) {
1113 mutex_init(&cam
->open_mutex
);
1114 init_waitqueue_head(&cam
->wait_open
);
1116 rect
= &(s
->cropcap
.defrect
);
1117 cam
->compression
.quality
= ET61X251_COMPRESSION_QUALITY
;
1118 } else { /* use current values */
1123 err
+= et61x251_set_scale(cam
, rect
->width
/ s
->pix_format
.width
);
1124 err
+= et61x251_set_crop(cam
, rect
);
1131 DBG(3, "Sensor initialization failed");
1136 err
+= et61x251_set_compression(cam
, &cam
->compression
);
1137 err
+= et61x251_set_pix_format(cam
, &s
->pix_format
);
1138 if (s
->set_pix_format
)
1139 err
+= s
->set_pix_format(cam
, &s
->pix_format
);
1143 if (s
->pix_format
.pixelformat
== V4L2_PIX_FMT_ET61X251
)
1144 DBG(3, "Compressed video format is active, quality %d",
1145 cam
->compression
.quality
);
1147 DBG(3, "Uncompressed video format is active");
1150 if ((err
= s
->set_crop(cam
, rect
))) {
1151 DBG(3, "set_crop() failed");
1156 for (i
= 0; i
< ARRAY_SIZE(s
->qctrl
); i
++)
1157 if (s
->qctrl
[i
].id
!= 0 &&
1158 !(s
->qctrl
[i
].flags
& V4L2_CTRL_FLAG_DISABLED
)) {
1159 ctrl
.id
= s
->qctrl
[i
].id
;
1160 ctrl
.value
= qctrl
[i
].default_value
;
1161 err
= s
->set_ctrl(cam
, &ctrl
);
1163 DBG(3, "Set %s control failed",
1167 DBG(3, "Image sensor supports '%s' control",
1172 if (!(cam
->state
& DEV_INITIALIZED
)) {
1173 mutex_init(&cam
->fileop_mutex
);
1174 spin_lock_init(&cam
->queue_lock
);
1175 init_waitqueue_head(&cam
->wait_frame
);
1176 init_waitqueue_head(&cam
->wait_stream
);
1177 cam
->nreadbuffers
= 2;
1178 memcpy(s
->_qctrl
, s
->qctrl
, sizeof(s
->qctrl
));
1179 memcpy(&(s
->_rect
), &(s
->cropcap
.defrect
),
1180 sizeof(struct v4l2_rect
));
1181 cam
->state
|= DEV_INITIALIZED
;
1184 DBG(2, "Initialization succeeded");
1188 /*****************************************************************************/
1190 static void et61x251_release_resources(struct kref
*kref
)
1192 struct et61x251_device
*cam
;
1194 mutex_lock(&et61x251_sysfs_lock
);
1196 cam
= container_of(kref
, struct et61x251_device
, kref
);
1198 DBG(2, "V4L2 device %s deregistered",
1199 video_device_node_name(cam
->v4ldev
));
1200 video_set_drvdata(cam
->v4ldev
, NULL
);
1201 video_unregister_device(cam
->v4ldev
);
1202 usb_put_dev(cam
->usbdev
);
1203 kfree(cam
->control_buffer
);
1206 mutex_unlock(&et61x251_sysfs_lock
);
1210 static int et61x251_open(struct file
*filp
)
1212 struct et61x251_device
* cam
;
1215 if (!down_read_trylock(&et61x251_dev_lock
))
1216 return -ERESTARTSYS
;
1218 cam
= video_drvdata(filp
);
1220 if (wait_for_completion_interruptible(&cam
->probe
)) {
1221 up_read(&et61x251_dev_lock
);
1222 return -ERESTARTSYS
;
1225 kref_get(&cam
->kref
);
1227 if (mutex_lock_interruptible(&cam
->open_mutex
)) {
1228 kref_put(&cam
->kref
, et61x251_release_resources
);
1229 up_read(&et61x251_dev_lock
);
1230 return -ERESTARTSYS
;
1233 if (cam
->state
& DEV_DISCONNECTED
) {
1234 DBG(1, "Device not present");
1240 DBG(2, "Device %s is already in use",
1241 video_device_node_name(cam
->v4ldev
));
1242 DBG(3, "Simultaneous opens are not supported");
1243 if ((filp
->f_flags
& O_NONBLOCK
) ||
1244 (filp
->f_flags
& O_NDELAY
)) {
1248 DBG(2, "A blocking open() has been requested. Wait for the "
1249 "device to be released...");
1250 up_read(&et61x251_dev_lock
);
1251 err
= wait_event_interruptible_exclusive(cam
->wait_open
,
1252 (cam
->state
& DEV_DISCONNECTED
)
1254 down_read(&et61x251_dev_lock
);
1257 if (cam
->state
& DEV_DISCONNECTED
) {
1263 if (cam
->state
& DEV_MISCONFIGURED
) {
1264 err
= et61x251_init(cam
);
1266 DBG(1, "Initialization failed again. "
1267 "I will retry on next open().");
1270 cam
->state
&= ~DEV_MISCONFIGURED
;
1273 if ((err
= et61x251_start_transfer(cam
)))
1276 filp
->private_data
= cam
;
1279 cam
->stream
= STREAM_OFF
;
1281 cam
->frame_count
= 0;
1282 et61x251_empty_framequeues(cam
);
1284 DBG(3, "Video device %s is open",
1285 video_device_node_name(cam
->v4ldev
));
1288 mutex_unlock(&cam
->open_mutex
);
1290 kref_put(&cam
->kref
, et61x251_release_resources
);
1291 up_read(&et61x251_dev_lock
);
1296 static int et61x251_release(struct file
*filp
)
1298 struct et61x251_device
* cam
;
1300 down_write(&et61x251_dev_lock
);
1302 cam
= video_drvdata(filp
);
1304 et61x251_stop_transfer(cam
);
1305 et61x251_release_buffers(cam
);
1307 wake_up_interruptible_nr(&cam
->wait_open
, 1);
1309 DBG(3, "Video device %s closed",
1310 video_device_node_name(cam
->v4ldev
));
1312 kref_put(&cam
->kref
, et61x251_release_resources
);
1314 up_write(&et61x251_dev_lock
);
1321 et61x251_read(struct file
* filp
, char __user
* buf
,
1322 size_t count
, loff_t
* f_pos
)
1324 struct et61x251_device
*cam
= video_drvdata(filp
);
1325 struct et61x251_frame_t
* f
, * i
;
1326 unsigned long lock_flags
;
1330 if (mutex_lock_interruptible(&cam
->fileop_mutex
))
1331 return -ERESTARTSYS
;
1333 if (cam
->state
& DEV_DISCONNECTED
) {
1334 DBG(1, "Device not present");
1335 mutex_unlock(&cam
->fileop_mutex
);
1339 if (cam
->state
& DEV_MISCONFIGURED
) {
1340 DBG(1, "The camera is misconfigured. Close and open it "
1342 mutex_unlock(&cam
->fileop_mutex
);
1346 if (cam
->io
== IO_MMAP
) {
1347 DBG(3, "Close and open the device again to choose the read "
1349 mutex_unlock(&cam
->fileop_mutex
);
1353 if (cam
->io
== IO_NONE
) {
1354 if (!et61x251_request_buffers(cam
, cam
->nreadbuffers
,
1356 DBG(1, "read() failed, not enough memory");
1357 mutex_unlock(&cam
->fileop_mutex
);
1361 cam
->stream
= STREAM_ON
;
1364 if (list_empty(&cam
->inqueue
)) {
1365 if (!list_empty(&cam
->outqueue
))
1366 et61x251_empty_framequeues(cam
);
1367 et61x251_queue_unusedframes(cam
);
1371 mutex_unlock(&cam
->fileop_mutex
);
1375 if (list_empty(&cam
->outqueue
)) {
1376 if (filp
->f_flags
& O_NONBLOCK
) {
1377 mutex_unlock(&cam
->fileop_mutex
);
1380 timeout
= wait_event_interruptible_timeout
1382 (!list_empty(&cam
->outqueue
)) ||
1383 (cam
->state
& DEV_DISCONNECTED
) ||
1384 (cam
->state
& DEV_MISCONFIGURED
),
1386 cam
->module_param
.frame_timeout
* 1000
1390 mutex_unlock(&cam
->fileop_mutex
);
1393 if (cam
->state
& DEV_DISCONNECTED
) {
1394 mutex_unlock(&cam
->fileop_mutex
);
1397 if (!timeout
|| (cam
->state
& DEV_MISCONFIGURED
)) {
1398 mutex_unlock(&cam
->fileop_mutex
);
1403 f
= list_entry(cam
->outqueue
.prev
, struct et61x251_frame_t
, frame
);
1405 if (count
> f
->buf
.bytesused
)
1406 count
= f
->buf
.bytesused
;
1408 if (copy_to_user(buf
, f
->bufmem
, count
)) {
1415 spin_lock_irqsave(&cam
->queue_lock
, lock_flags
);
1416 list_for_each_entry(i
, &cam
->outqueue
, frame
)
1417 i
->state
= F_UNUSED
;
1418 INIT_LIST_HEAD(&cam
->outqueue
);
1419 spin_unlock_irqrestore(&cam
->queue_lock
, lock_flags
);
1421 et61x251_queue_unusedframes(cam
);
1423 PDBGG("Frame #%lu, bytes read: %zu",
1424 (unsigned long)f
->buf
.index
, count
);
1426 mutex_unlock(&cam
->fileop_mutex
);
1428 return err
? err
: count
;
1432 static unsigned int et61x251_poll(struct file
*filp
, poll_table
*wait
)
1434 struct et61x251_device
*cam
= video_drvdata(filp
);
1435 struct et61x251_frame_t
* f
;
1436 unsigned long lock_flags
;
1437 unsigned int mask
= 0;
1439 if (mutex_lock_interruptible(&cam
->fileop_mutex
))
1442 if (cam
->state
& DEV_DISCONNECTED
) {
1443 DBG(1, "Device not present");
1447 if (cam
->state
& DEV_MISCONFIGURED
) {
1448 DBG(1, "The camera is misconfigured. Close and open it "
1453 if (cam
->io
== IO_NONE
) {
1454 if (!et61x251_request_buffers(cam
, cam
->nreadbuffers
,
1456 DBG(1, "poll() failed, not enough memory");
1460 cam
->stream
= STREAM_ON
;
1463 if (cam
->io
== IO_READ
) {
1464 spin_lock_irqsave(&cam
->queue_lock
, lock_flags
);
1465 list_for_each_entry(f
, &cam
->outqueue
, frame
)
1466 f
->state
= F_UNUSED
;
1467 INIT_LIST_HEAD(&cam
->outqueue
);
1468 spin_unlock_irqrestore(&cam
->queue_lock
, lock_flags
);
1469 et61x251_queue_unusedframes(cam
);
1472 poll_wait(filp
, &cam
->wait_frame
, wait
);
1474 if (!list_empty(&cam
->outqueue
))
1475 mask
|= POLLIN
| POLLRDNORM
;
1477 mutex_unlock(&cam
->fileop_mutex
);
1482 mutex_unlock(&cam
->fileop_mutex
);
1487 static void et61x251_vm_open(struct vm_area_struct
* vma
)
1489 struct et61x251_frame_t
* f
= vma
->vm_private_data
;
1494 static void et61x251_vm_close(struct vm_area_struct
* vma
)
1496 /* NOTE: buffers are not freed here */
1497 struct et61x251_frame_t
* f
= vma
->vm_private_data
;
1502 static const struct vm_operations_struct et61x251_vm_ops
= {
1503 .open
= et61x251_vm_open
,
1504 .close
= et61x251_vm_close
,
1508 static int et61x251_mmap(struct file
* filp
, struct vm_area_struct
*vma
)
1510 struct et61x251_device
*cam
= video_drvdata(filp
);
1511 unsigned long size
= vma
->vm_end
- vma
->vm_start
,
1512 start
= vma
->vm_start
;
1516 if (mutex_lock_interruptible(&cam
->fileop_mutex
))
1517 return -ERESTARTSYS
;
1519 if (cam
->state
& DEV_DISCONNECTED
) {
1520 DBG(1, "Device not present");
1521 mutex_unlock(&cam
->fileop_mutex
);
1525 if (cam
->state
& DEV_MISCONFIGURED
) {
1526 DBG(1, "The camera is misconfigured. Close and open it "
1528 mutex_unlock(&cam
->fileop_mutex
);
1532 if (!(vma
->vm_flags
& (VM_WRITE
| VM_READ
))) {
1533 mutex_unlock(&cam
->fileop_mutex
);
1537 if (cam
->io
!= IO_MMAP
||
1538 size
!= PAGE_ALIGN(cam
->frame
[0].buf
.length
)) {
1539 mutex_unlock(&cam
->fileop_mutex
);
1543 for (i
= 0; i
< cam
->nbuffers
; i
++) {
1544 if ((cam
->frame
[i
].buf
.m
.offset
>>PAGE_SHIFT
) == vma
->vm_pgoff
)
1547 if (i
== cam
->nbuffers
) {
1548 mutex_unlock(&cam
->fileop_mutex
);
1552 vma
->vm_flags
|= VM_IO
;
1553 vma
->vm_flags
|= VM_RESERVED
;
1555 pos
= cam
->frame
[i
].bufmem
;
1556 while (size
> 0) { /* size is page-aligned */
1557 if (vm_insert_page(vma
, start
, vmalloc_to_page(pos
))) {
1558 mutex_unlock(&cam
->fileop_mutex
);
1566 vma
->vm_ops
= &et61x251_vm_ops
;
1567 vma
->vm_private_data
= &cam
->frame
[i
];
1568 et61x251_vm_open(vma
);
1570 mutex_unlock(&cam
->fileop_mutex
);
1575 /*****************************************************************************/
1578 et61x251_vidioc_querycap(struct et61x251_device
* cam
, void __user
* arg
)
1580 struct v4l2_capability cap
= {
1581 .driver
= "et61x251",
1582 .version
= ET61X251_MODULE_VERSION_CODE
,
1583 .capabilities
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_READWRITE
|
1587 strlcpy(cap
.card
, cam
->v4ldev
->name
, sizeof(cap
.card
));
1588 if (usb_make_path(cam
->usbdev
, cap
.bus_info
, sizeof(cap
.bus_info
)) < 0)
1589 strlcpy(cap
.bus_info
, dev_name(&cam
->usbdev
->dev
),
1590 sizeof(cap
.bus_info
));
1592 if (copy_to_user(arg
, &cap
, sizeof(cap
)))
1600 et61x251_vidioc_enuminput(struct et61x251_device
* cam
, void __user
* arg
)
1602 struct v4l2_input i
;
1604 if (copy_from_user(&i
, arg
, sizeof(i
)))
1610 memset(&i
, 0, sizeof(i
));
1611 strcpy(i
.name
, "Camera");
1612 i
.type
= V4L2_INPUT_TYPE_CAMERA
;
1613 i
.capabilities
= V4L2_IN_CAP_STD
;
1615 if (copy_to_user(arg
, &i
, sizeof(i
)))
1623 et61x251_vidioc_g_input(struct et61x251_device
* cam
, void __user
* arg
)
1627 if (copy_to_user(arg
, &index
, sizeof(index
)))
1635 et61x251_vidioc_s_input(struct et61x251_device
* cam
, void __user
* arg
)
1639 if (copy_from_user(&index
, arg
, sizeof(index
)))
1650 et61x251_vidioc_query_ctrl(struct et61x251_device
* cam
, void __user
* arg
)
1652 struct et61x251_sensor
* s
= &cam
->sensor
;
1653 struct v4l2_queryctrl qc
;
1656 if (copy_from_user(&qc
, arg
, sizeof(qc
)))
1659 for (i
= 0; i
< ARRAY_SIZE(s
->qctrl
); i
++)
1660 if (qc
.id
&& qc
.id
== s
->qctrl
[i
].id
) {
1661 memcpy(&qc
, &(s
->qctrl
[i
]), sizeof(qc
));
1662 if (copy_to_user(arg
, &qc
, sizeof(qc
)))
1672 et61x251_vidioc_g_ctrl(struct et61x251_device
* cam
, void __user
* arg
)
1674 struct et61x251_sensor
* s
= &cam
->sensor
;
1675 struct v4l2_control ctrl
;
1679 if (!s
->get_ctrl
&& !s
->set_ctrl
)
1682 if (copy_from_user(&ctrl
, arg
, sizeof(ctrl
)))
1686 for (i
= 0; i
< ARRAY_SIZE(s
->qctrl
); i
++)
1687 if (ctrl
.id
== s
->qctrl
[i
].id
) {
1688 ctrl
.value
= s
->_qctrl
[i
].default_value
;
1693 err
= s
->get_ctrl(cam
, &ctrl
);
1696 if (copy_to_user(arg
, &ctrl
, sizeof(ctrl
)))
1704 et61x251_vidioc_s_ctrl(struct et61x251_device
* cam
, void __user
* arg
)
1706 struct et61x251_sensor
* s
= &cam
->sensor
;
1707 struct v4l2_control ctrl
;
1714 if (copy_from_user(&ctrl
, arg
, sizeof(ctrl
)))
1717 for (i
= 0; i
< ARRAY_SIZE(s
->qctrl
); i
++) {
1718 if (ctrl
.id
== s
->qctrl
[i
].id
) {
1719 if (s
->qctrl
[i
].flags
& V4L2_CTRL_FLAG_DISABLED
)
1721 if (ctrl
.value
< s
->qctrl
[i
].minimum
||
1722 ctrl
.value
> s
->qctrl
[i
].maximum
)
1724 ctrl
.value
-= ctrl
.value
% s
->qctrl
[i
].step
;
1728 if (i
== ARRAY_SIZE(s
->qctrl
))
1730 if ((err
= s
->set_ctrl(cam
, &ctrl
)))
1733 s
->_qctrl
[i
].default_value
= ctrl
.value
;
1740 et61x251_vidioc_cropcap(struct et61x251_device
* cam
, void __user
* arg
)
1742 struct v4l2_cropcap
* cc
= &(cam
->sensor
.cropcap
);
1744 cc
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1745 cc
->pixelaspect
.numerator
= 1;
1746 cc
->pixelaspect
.denominator
= 1;
1748 if (copy_to_user(arg
, cc
, sizeof(*cc
)))
1756 et61x251_vidioc_g_crop(struct et61x251_device
* cam
, void __user
* arg
)
1758 struct et61x251_sensor
* s
= &cam
->sensor
;
1759 struct v4l2_crop crop
= {
1760 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
1763 memcpy(&(crop
.c
), &(s
->_rect
), sizeof(struct v4l2_rect
));
1765 if (copy_to_user(arg
, &crop
, sizeof(crop
)))
1773 et61x251_vidioc_s_crop(struct et61x251_device
* cam
, void __user
* arg
)
1775 struct et61x251_sensor
* s
= &cam
->sensor
;
1776 struct v4l2_crop crop
;
1777 struct v4l2_rect
* rect
;
1778 struct v4l2_rect
* bounds
= &(s
->cropcap
.bounds
);
1779 struct v4l2_pix_format
* pix_format
= &(s
->pix_format
);
1781 const enum et61x251_stream_state stream
= cam
->stream
;
1782 const u32 nbuffers
= cam
->nbuffers
;
1786 if (copy_from_user(&crop
, arg
, sizeof(crop
)))
1791 if (crop
.type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1794 if (cam
->module_param
.force_munmap
)
1795 for (i
= 0; i
< cam
->nbuffers
; i
++)
1796 if (cam
->frame
[i
].vma_use_count
) {
1797 DBG(3, "VIDIOC_S_CROP failed. "
1798 "Unmap the buffers first.");
1802 /* Preserve R,G or B origin */
1803 rect
->left
= (s
->_rect
.left
& 1L) ? rect
->left
| 1L : rect
->left
& ~1L;
1804 rect
->top
= (s
->_rect
.top
& 1L) ? rect
->top
| 1L : rect
->top
& ~1L;
1806 if (rect
->width
< 16)
1808 if (rect
->height
< 16)
1810 if (rect
->width
> bounds
->width
)
1811 rect
->width
= bounds
->width
;
1812 if (rect
->height
> bounds
->height
)
1813 rect
->height
= bounds
->height
;
1814 if (rect
->left
< bounds
->left
)
1815 rect
->left
= bounds
->left
;
1816 if (rect
->top
< bounds
->top
)
1817 rect
->top
= bounds
->top
;
1818 if (rect
->left
+ rect
->width
> bounds
->left
+ bounds
->width
)
1819 rect
->left
= bounds
->left
+bounds
->width
- rect
->width
;
1820 if (rect
->top
+ rect
->height
> bounds
->top
+ bounds
->height
)
1821 rect
->top
= bounds
->top
+bounds
->height
- rect
->height
;
1823 rect
->width
&= ~15L;
1824 rect
->height
&= ~15L;
1826 if (ET61X251_PRESERVE_IMGSCALE
) {
1827 /* Calculate the actual scaling factor */
1829 a
= rect
->width
* rect
->height
;
1830 b
= pix_format
->width
* pix_format
->height
;
1831 scale
= b
? (u8
)((a
/ b
) < 4 ? 1 : 2) : 1;
1835 if (cam
->stream
== STREAM_ON
)
1836 if ((err
= et61x251_stream_interrupt(cam
)))
1839 if (copy_to_user(arg
, &crop
, sizeof(crop
))) {
1840 cam
->stream
= stream
;
1844 if (cam
->module_param
.force_munmap
|| cam
->io
== IO_READ
)
1845 et61x251_release_buffers(cam
);
1847 err
= et61x251_set_crop(cam
, rect
);
1849 err
+= s
->set_crop(cam
, rect
);
1850 err
+= et61x251_set_scale(cam
, scale
);
1852 if (err
) { /* atomic, no rollback in ioctl() */
1853 cam
->state
|= DEV_MISCONFIGURED
;
1854 DBG(1, "VIDIOC_S_CROP failed because of hardware problems. To "
1855 "use the camera, close and open %s again.",
1856 video_device_node_name(cam
->v4ldev
));
1860 s
->pix_format
.width
= rect
->width
/scale
;
1861 s
->pix_format
.height
= rect
->height
/scale
;
1862 memcpy(&(s
->_rect
), rect
, sizeof(*rect
));
1864 if ((cam
->module_param
.force_munmap
|| cam
->io
== IO_READ
) &&
1865 nbuffers
!= et61x251_request_buffers(cam
, nbuffers
, cam
->io
)) {
1866 cam
->state
|= DEV_MISCONFIGURED
;
1867 DBG(1, "VIDIOC_S_CROP failed because of not enough memory. To "
1868 "use the camera, close and open %s again.",
1869 video_device_node_name(cam
->v4ldev
));
1873 if (cam
->io
== IO_READ
)
1874 et61x251_empty_framequeues(cam
);
1875 else if (cam
->module_param
.force_munmap
)
1876 et61x251_requeue_outqueue(cam
);
1878 cam
->stream
= stream
;
1885 et61x251_vidioc_enum_framesizes(struct et61x251_device
* cam
, void __user
* arg
)
1887 struct v4l2_frmsizeenum frmsize
;
1889 if (copy_from_user(&frmsize
, arg
, sizeof(frmsize
)))
1892 if (frmsize
.index
!= 0)
1895 if (frmsize
.pixel_format
!= V4L2_PIX_FMT_ET61X251
&&
1896 frmsize
.pixel_format
!= V4L2_PIX_FMT_SBGGR8
)
1899 frmsize
.type
= V4L2_FRMSIZE_TYPE_STEPWISE
;
1900 frmsize
.stepwise
.min_width
= frmsize
.stepwise
.step_width
= 16;
1901 frmsize
.stepwise
.min_height
= frmsize
.stepwise
.step_height
= 16;
1902 frmsize
.stepwise
.max_width
= cam
->sensor
.cropcap
.bounds
.width
;
1903 frmsize
.stepwise
.max_height
= cam
->sensor
.cropcap
.bounds
.height
;
1904 memset(&frmsize
.reserved
, 0, sizeof(frmsize
.reserved
));
1906 if (copy_to_user(arg
, &frmsize
, sizeof(frmsize
)))
1914 et61x251_vidioc_enum_fmt(struct et61x251_device
* cam
, void __user
* arg
)
1916 struct v4l2_fmtdesc fmtd
;
1918 if (copy_from_user(&fmtd
, arg
, sizeof(fmtd
)))
1921 if (fmtd
.type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1924 if (fmtd
.index
== 0) {
1925 strcpy(fmtd
.description
, "bayer rgb");
1926 fmtd
.pixelformat
= V4L2_PIX_FMT_SBGGR8
;
1927 } else if (fmtd
.index
== 1) {
1928 strcpy(fmtd
.description
, "compressed");
1929 fmtd
.pixelformat
= V4L2_PIX_FMT_ET61X251
;
1930 fmtd
.flags
= V4L2_FMT_FLAG_COMPRESSED
;
1934 fmtd
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1935 memset(&fmtd
.reserved
, 0, sizeof(fmtd
.reserved
));
1937 if (copy_to_user(arg
, &fmtd
, sizeof(fmtd
)))
1945 et61x251_vidioc_g_fmt(struct et61x251_device
* cam
, void __user
* arg
)
1947 struct v4l2_format format
;
1948 struct v4l2_pix_format
* pfmt
= &(cam
->sensor
.pix_format
);
1950 if (copy_from_user(&format
, arg
, sizeof(format
)))
1953 if (format
.type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1956 pfmt
->colorspace
= (pfmt
->pixelformat
== V4L2_PIX_FMT_ET61X251
) ?
1957 0 : V4L2_COLORSPACE_SRGB
;
1958 pfmt
->bytesperline
= (pfmt
->pixelformat
==V4L2_PIX_FMT_ET61X251
)
1959 ? 0 : (pfmt
->width
* pfmt
->priv
) / 8;
1960 pfmt
->sizeimage
= pfmt
->height
* ((pfmt
->width
*pfmt
->priv
)/8);
1961 pfmt
->field
= V4L2_FIELD_NONE
;
1962 memcpy(&(format
.fmt
.pix
), pfmt
, sizeof(*pfmt
));
1964 if (copy_to_user(arg
, &format
, sizeof(format
)))
1972 et61x251_vidioc_try_s_fmt(struct et61x251_device
* cam
, unsigned int cmd
,
1975 struct et61x251_sensor
* s
= &cam
->sensor
;
1976 struct v4l2_format format
;
1977 struct v4l2_pix_format
* pix
;
1978 struct v4l2_pix_format
* pfmt
= &(s
->pix_format
);
1979 struct v4l2_rect
* bounds
= &(s
->cropcap
.bounds
);
1980 struct v4l2_rect rect
;
1982 const enum et61x251_stream_state stream
= cam
->stream
;
1983 const u32 nbuffers
= cam
->nbuffers
;
1987 if (copy_from_user(&format
, arg
, sizeof(format
)))
1990 pix
= &(format
.fmt
.pix
);
1992 if (format
.type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1995 memcpy(&rect
, &(s
->_rect
), sizeof(rect
));
1997 { /* calculate the actual scaling factor */
1999 a
= rect
.width
* rect
.height
;
2000 b
= pix
->width
* pix
->height
;
2001 scale
= b
? (u8
)((a
/ b
) < 4 ? 1 : 2) : 1;
2004 rect
.width
= scale
* pix
->width
;
2005 rect
.height
= scale
* pix
->height
;
2007 if (rect
.width
< 16)
2009 if (rect
.height
< 16)
2011 if (rect
.width
> bounds
->left
+ bounds
->width
- rect
.left
)
2012 rect
.width
= bounds
->left
+ bounds
->width
- rect
.left
;
2013 if (rect
.height
> bounds
->top
+ bounds
->height
- rect
.top
)
2014 rect
.height
= bounds
->top
+ bounds
->height
- rect
.top
;
2017 rect
.height
&= ~15L;
2019 { /* adjust the scaling factor */
2021 a
= rect
.width
* rect
.height
;
2022 b
= pix
->width
* pix
->height
;
2023 scale
= b
? (u8
)((a
/ b
) < 4 ? 1 : 2) : 1;
2026 pix
->width
= rect
.width
/ scale
;
2027 pix
->height
= rect
.height
/ scale
;
2029 if (pix
->pixelformat
!= V4L2_PIX_FMT_ET61X251
&&
2030 pix
->pixelformat
!= V4L2_PIX_FMT_SBGGR8
)
2031 pix
->pixelformat
= pfmt
->pixelformat
;
2032 pix
->priv
= pfmt
->priv
; /* bpp */
2033 pix
->colorspace
= (pix
->pixelformat
== V4L2_PIX_FMT_ET61X251
) ?
2034 0 : V4L2_COLORSPACE_SRGB
;
2035 pix
->colorspace
= pfmt
->colorspace
;
2036 pix
->bytesperline
= (pix
->pixelformat
== V4L2_PIX_FMT_ET61X251
)
2037 ? 0 : (pix
->width
* pix
->priv
) / 8;
2038 pix
->sizeimage
= pix
->height
* ((pix
->width
* pix
->priv
) / 8);
2039 pix
->field
= V4L2_FIELD_NONE
;
2041 if (cmd
== VIDIOC_TRY_FMT
) {
2042 if (copy_to_user(arg
, &format
, sizeof(format
)))
2047 if (cam
->module_param
.force_munmap
)
2048 for (i
= 0; i
< cam
->nbuffers
; i
++)
2049 if (cam
->frame
[i
].vma_use_count
) {
2050 DBG(3, "VIDIOC_S_FMT failed. "
2051 "Unmap the buffers first.");
2055 if (cam
->stream
== STREAM_ON
)
2056 if ((err
= et61x251_stream_interrupt(cam
)))
2059 if (copy_to_user(arg
, &format
, sizeof(format
))) {
2060 cam
->stream
= stream
;
2064 if (cam
->module_param
.force_munmap
|| cam
->io
== IO_READ
)
2065 et61x251_release_buffers(cam
);
2067 err
+= et61x251_set_pix_format(cam
, pix
);
2068 err
+= et61x251_set_crop(cam
, &rect
);
2069 if (s
->set_pix_format
)
2070 err
+= s
->set_pix_format(cam
, pix
);
2072 err
+= s
->set_crop(cam
, &rect
);
2073 err
+= et61x251_set_scale(cam
, scale
);
2075 if (err
) { /* atomic, no rollback in ioctl() */
2076 cam
->state
|= DEV_MISCONFIGURED
;
2077 DBG(1, "VIDIOC_S_FMT failed because of hardware problems. To "
2078 "use the camera, close and open %s again.",
2079 video_device_node_name(cam
->v4ldev
));
2083 memcpy(pfmt
, pix
, sizeof(*pix
));
2084 memcpy(&(s
->_rect
), &rect
, sizeof(rect
));
2086 if ((cam
->module_param
.force_munmap
|| cam
->io
== IO_READ
) &&
2087 nbuffers
!= et61x251_request_buffers(cam
, nbuffers
, cam
->io
)) {
2088 cam
->state
|= DEV_MISCONFIGURED
;
2089 DBG(1, "VIDIOC_S_FMT failed because of not enough memory. To "
2090 "use the camera, close and open %s again.",
2091 video_device_node_name(cam
->v4ldev
));
2095 if (cam
->io
== IO_READ
)
2096 et61x251_empty_framequeues(cam
);
2097 else if (cam
->module_param
.force_munmap
)
2098 et61x251_requeue_outqueue(cam
);
2100 cam
->stream
= stream
;
2107 et61x251_vidioc_g_jpegcomp(struct et61x251_device
* cam
, void __user
* arg
)
2109 if (copy_to_user(arg
, &cam
->compression
,
2110 sizeof(cam
->compression
)))
2118 et61x251_vidioc_s_jpegcomp(struct et61x251_device
* cam
, void __user
* arg
)
2120 struct v4l2_jpegcompression jc
;
2121 const enum et61x251_stream_state stream
= cam
->stream
;
2124 if (copy_from_user(&jc
, arg
, sizeof(jc
)))
2127 if (jc
.quality
!= 0 && jc
.quality
!= 1)
2130 if (cam
->stream
== STREAM_ON
)
2131 if ((err
= et61x251_stream_interrupt(cam
)))
2134 err
+= et61x251_set_compression(cam
, &jc
);
2135 if (err
) { /* atomic, no rollback in ioctl() */
2136 cam
->state
|= DEV_MISCONFIGURED
;
2137 DBG(1, "VIDIOC_S_JPEGCOMP failed because of hardware "
2138 "problems. To use the camera, close and open "
2139 "%s again.", video_device_node_name(cam
->v4ldev
));
2143 cam
->compression
.quality
= jc
.quality
;
2145 cam
->stream
= stream
;
2152 et61x251_vidioc_reqbufs(struct et61x251_device
* cam
, void __user
* arg
)
2154 struct v4l2_requestbuffers rb
;
2158 if (copy_from_user(&rb
, arg
, sizeof(rb
)))
2161 if (rb
.type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
2162 rb
.memory
!= V4L2_MEMORY_MMAP
)
2165 if (cam
->io
== IO_READ
) {
2166 DBG(3, "Close and open the device again to choose the mmap "
2171 for (i
= 0; i
< cam
->nbuffers
; i
++)
2172 if (cam
->frame
[i
].vma_use_count
) {
2173 DBG(3, "VIDIOC_REQBUFS failed. "
2174 "Previous buffers are still mapped.");
2178 if (cam
->stream
== STREAM_ON
)
2179 if ((err
= et61x251_stream_interrupt(cam
)))
2182 et61x251_empty_framequeues(cam
);
2184 et61x251_release_buffers(cam
);
2186 rb
.count
= et61x251_request_buffers(cam
, rb
.count
, IO_MMAP
);
2188 if (copy_to_user(arg
, &rb
, sizeof(rb
))) {
2189 et61x251_release_buffers(cam
);
2194 cam
->io
= rb
.count
? IO_MMAP
: IO_NONE
;
2201 et61x251_vidioc_querybuf(struct et61x251_device
* cam
, void __user
* arg
)
2203 struct v4l2_buffer b
;
2205 if (copy_from_user(&b
, arg
, sizeof(b
)))
2208 if (b
.type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
2209 b
.index
>= cam
->nbuffers
|| cam
->io
!= IO_MMAP
)
2212 memcpy(&b
, &cam
->frame
[b
.index
].buf
, sizeof(b
));
2214 if (cam
->frame
[b
.index
].vma_use_count
)
2215 b
.flags
|= V4L2_BUF_FLAG_MAPPED
;
2217 if (cam
->frame
[b
.index
].state
== F_DONE
)
2218 b
.flags
|= V4L2_BUF_FLAG_DONE
;
2219 else if (cam
->frame
[b
.index
].state
!= F_UNUSED
)
2220 b
.flags
|= V4L2_BUF_FLAG_QUEUED
;
2222 if (copy_to_user(arg
, &b
, sizeof(b
)))
2230 et61x251_vidioc_qbuf(struct et61x251_device
* cam
, void __user
* arg
)
2232 struct v4l2_buffer b
;
2233 unsigned long lock_flags
;
2235 if (copy_from_user(&b
, arg
, sizeof(b
)))
2238 if (b
.type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
2239 b
.index
>= cam
->nbuffers
|| cam
->io
!= IO_MMAP
)
2242 if (cam
->frame
[b
.index
].state
!= F_UNUSED
)
2245 cam
->frame
[b
.index
].state
= F_QUEUED
;
2247 spin_lock_irqsave(&cam
->queue_lock
, lock_flags
);
2248 list_add_tail(&cam
->frame
[b
.index
].frame
, &cam
->inqueue
);
2249 spin_unlock_irqrestore(&cam
->queue_lock
, lock_flags
);
2251 PDBGG("Frame #%lu queued", (unsigned long)b
.index
);
2258 et61x251_vidioc_dqbuf(struct et61x251_device
* cam
, struct file
* filp
,
2261 struct v4l2_buffer b
;
2262 struct et61x251_frame_t
*f
;
2263 unsigned long lock_flags
;
2266 if (copy_from_user(&b
, arg
, sizeof(b
)))
2269 if (b
.type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
|| cam
->io
!= IO_MMAP
)
2272 if (list_empty(&cam
->outqueue
)) {
2273 if (cam
->stream
== STREAM_OFF
)
2275 if (filp
->f_flags
& O_NONBLOCK
)
2277 timeout
= wait_event_interruptible_timeout
2279 (!list_empty(&cam
->outqueue
)) ||
2280 (cam
->state
& DEV_DISCONNECTED
) ||
2281 (cam
->state
& DEV_MISCONFIGURED
),
2282 cam
->module_param
.frame_timeout
*
2283 1000 * msecs_to_jiffies(1) );
2286 if (cam
->state
& DEV_DISCONNECTED
)
2288 if (!timeout
|| (cam
->state
& DEV_MISCONFIGURED
))
2292 spin_lock_irqsave(&cam
->queue_lock
, lock_flags
);
2293 f
= list_entry(cam
->outqueue
.next
, struct et61x251_frame_t
, frame
);
2294 list_del(cam
->outqueue
.next
);
2295 spin_unlock_irqrestore(&cam
->queue_lock
, lock_flags
);
2297 f
->state
= F_UNUSED
;
2299 memcpy(&b
, &f
->buf
, sizeof(b
));
2300 if (f
->vma_use_count
)
2301 b
.flags
|= V4L2_BUF_FLAG_MAPPED
;
2303 if (copy_to_user(arg
, &b
, sizeof(b
)))
2306 PDBGG("Frame #%lu dequeued", (unsigned long)f
->buf
.index
);
2313 et61x251_vidioc_streamon(struct et61x251_device
* cam
, void __user
* arg
)
2317 if (copy_from_user(&type
, arg
, sizeof(type
)))
2320 if (type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
|| cam
->io
!= IO_MMAP
)
2323 cam
->stream
= STREAM_ON
;
2325 DBG(3, "Stream on");
2332 et61x251_vidioc_streamoff(struct et61x251_device
* cam
, void __user
* arg
)
2336 if (copy_from_user(&type
, arg
, sizeof(type
)))
2339 if (type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
|| cam
->io
!= IO_MMAP
)
2342 if (cam
->stream
== STREAM_ON
)
2343 if ((err
= et61x251_stream_interrupt(cam
)))
2346 et61x251_empty_framequeues(cam
);
2348 DBG(3, "Stream off");
2355 et61x251_vidioc_g_parm(struct et61x251_device
* cam
, void __user
* arg
)
2357 struct v4l2_streamparm sp
;
2359 if (copy_from_user(&sp
, arg
, sizeof(sp
)))
2362 if (sp
.type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
2365 sp
.parm
.capture
.extendedmode
= 0;
2366 sp
.parm
.capture
.readbuffers
= cam
->nreadbuffers
;
2368 if (copy_to_user(arg
, &sp
, sizeof(sp
)))
2376 et61x251_vidioc_s_parm(struct et61x251_device
* cam
, void __user
* arg
)
2378 struct v4l2_streamparm sp
;
2380 if (copy_from_user(&sp
, arg
, sizeof(sp
)))
2383 if (sp
.type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
2386 sp
.parm
.capture
.extendedmode
= 0;
2388 if (sp
.parm
.capture
.readbuffers
== 0)
2389 sp
.parm
.capture
.readbuffers
= cam
->nreadbuffers
;
2391 if (sp
.parm
.capture
.readbuffers
> ET61X251_MAX_FRAMES
)
2392 sp
.parm
.capture
.readbuffers
= ET61X251_MAX_FRAMES
;
2394 if (copy_to_user(arg
, &sp
, sizeof(sp
)))
2397 cam
->nreadbuffers
= sp
.parm
.capture
.readbuffers
;
2403 static long et61x251_ioctl_v4l2(struct file
*filp
,
2404 unsigned int cmd
, void __user
*arg
)
2406 struct et61x251_device
*cam
= video_drvdata(filp
);
2410 case VIDIOC_QUERYCAP
:
2411 return et61x251_vidioc_querycap(cam
, arg
);
2413 case VIDIOC_ENUMINPUT
:
2414 return et61x251_vidioc_enuminput(cam
, arg
);
2416 case VIDIOC_G_INPUT
:
2417 return et61x251_vidioc_g_input(cam
, arg
);
2419 case VIDIOC_S_INPUT
:
2420 return et61x251_vidioc_s_input(cam
, arg
);
2422 case VIDIOC_QUERYCTRL
:
2423 return et61x251_vidioc_query_ctrl(cam
, arg
);
2426 return et61x251_vidioc_g_ctrl(cam
, arg
);
2429 return et61x251_vidioc_s_ctrl(cam
, arg
);
2431 case VIDIOC_CROPCAP
:
2432 return et61x251_vidioc_cropcap(cam
, arg
);
2435 return et61x251_vidioc_g_crop(cam
, arg
);
2438 return et61x251_vidioc_s_crop(cam
, arg
);
2440 case VIDIOC_ENUM_FMT
:
2441 return et61x251_vidioc_enum_fmt(cam
, arg
);
2444 return et61x251_vidioc_g_fmt(cam
, arg
);
2446 case VIDIOC_TRY_FMT
:
2448 return et61x251_vidioc_try_s_fmt(cam
, cmd
, arg
);
2450 case VIDIOC_ENUM_FRAMESIZES
:
2451 return et61x251_vidioc_enum_framesizes(cam
, arg
);
2453 case VIDIOC_G_JPEGCOMP
:
2454 return et61x251_vidioc_g_jpegcomp(cam
, arg
);
2456 case VIDIOC_S_JPEGCOMP
:
2457 return et61x251_vidioc_s_jpegcomp(cam
, arg
);
2459 case VIDIOC_REQBUFS
:
2460 return et61x251_vidioc_reqbufs(cam
, arg
);
2462 case VIDIOC_QUERYBUF
:
2463 return et61x251_vidioc_querybuf(cam
, arg
);
2466 return et61x251_vidioc_qbuf(cam
, arg
);
2469 return et61x251_vidioc_dqbuf(cam
, filp
, arg
);
2471 case VIDIOC_STREAMON
:
2472 return et61x251_vidioc_streamon(cam
, arg
);
2474 case VIDIOC_STREAMOFF
:
2475 return et61x251_vidioc_streamoff(cam
, arg
);
2478 return et61x251_vidioc_g_parm(cam
, arg
);
2481 return et61x251_vidioc_s_parm(cam
, arg
);
2485 case VIDIOC_QUERYSTD
:
2486 case VIDIOC_ENUMSTD
:
2487 case VIDIOC_QUERYMENU
:
2488 case VIDIOC_ENUM_FRAMEINTERVALS
:
2498 static long et61x251_ioctl(struct file
*filp
,
2499 unsigned int cmd
, unsigned long arg
)
2501 struct et61x251_device
*cam
= video_drvdata(filp
);
2504 if (mutex_lock_interruptible(&cam
->fileop_mutex
))
2505 return -ERESTARTSYS
;
2507 if (cam
->state
& DEV_DISCONNECTED
) {
2508 DBG(1, "Device not present");
2509 mutex_unlock(&cam
->fileop_mutex
);
2513 if (cam
->state
& DEV_MISCONFIGURED
) {
2514 DBG(1, "The camera is misconfigured. Close and open it "
2516 mutex_unlock(&cam
->fileop_mutex
);
2520 V4LDBG(3, "et61x251", cmd
);
2522 err
= et61x251_ioctl_v4l2(filp
, cmd
, (void __user
*)arg
);
2524 mutex_unlock(&cam
->fileop_mutex
);
2530 static const struct v4l2_file_operations et61x251_fops
= {
2531 .owner
= THIS_MODULE
,
2532 .open
= et61x251_open
,
2533 .release
= et61x251_release
,
2534 .unlocked_ioctl
= et61x251_ioctl
,
2535 .read
= et61x251_read
,
2536 .poll
= et61x251_poll
,
2537 .mmap
= et61x251_mmap
,
2540 /*****************************************************************************/
2542 /* It exists a single interface only. We do not need to validate anything. */
2544 et61x251_usb_probe(struct usb_interface
* intf
, const struct usb_device_id
* id
)
2546 struct usb_device
*udev
= interface_to_usbdev(intf
);
2547 struct et61x251_device
* cam
;
2548 static unsigned int dev_nr
;
2552 if (!(cam
= kzalloc(sizeof(struct et61x251_device
), GFP_KERNEL
)))
2557 if (!(cam
->control_buffer
= kzalloc(8, GFP_KERNEL
))) {
2558 DBG(1, "kmalloc() failed");
2563 if (!(cam
->v4ldev
= video_device_alloc())) {
2564 DBG(1, "video_device_alloc() failed");
2569 DBG(2, "ET61X[12]51 PC Camera Controller detected "
2570 "(vid/pid 0x%04X:0x%04X)",id
->idVendor
, id
->idProduct
);
2572 for (i
= 0; et61x251_sensor_table
[i
]; i
++) {
2573 err
= et61x251_sensor_table
[i
](cam
);
2579 DBG(2, "%s image sensor detected", cam
->sensor
.name
);
2581 DBG(1, "No supported image sensor detected");
2586 if (et61x251_init(cam
)) {
2587 DBG(1, "Initialization failed. I will retry on open().");
2588 cam
->state
|= DEV_MISCONFIGURED
;
2591 strcpy(cam
->v4ldev
->name
, "ET61X[12]51 PC Camera");
2592 cam
->v4ldev
->fops
= &et61x251_fops
;
2593 cam
->v4ldev
->release
= video_device_release
;
2594 cam
->v4ldev
->parent
= &udev
->dev
;
2595 video_set_drvdata(cam
->v4ldev
, cam
);
2597 init_completion(&cam
->probe
);
2599 err
= video_register_device(cam
->v4ldev
, VFL_TYPE_GRABBER
,
2602 DBG(1, "V4L2 device registration failed");
2603 if (err
== -ENFILE
&& video_nr
[dev_nr
] == -1)
2604 DBG(1, "Free /dev/videoX node not found");
2605 video_nr
[dev_nr
] = -1;
2606 dev_nr
= (dev_nr
< ET61X251_MAX_DEVICES
-1) ? dev_nr
+1 : 0;
2607 complete_all(&cam
->probe
);
2611 DBG(2, "V4L2 device registered as %s",
2612 video_device_node_name(cam
->v4ldev
));
2614 cam
->module_param
.force_munmap
= force_munmap
[dev_nr
];
2615 cam
->module_param
.frame_timeout
= frame_timeout
[dev_nr
];
2617 dev_nr
= (dev_nr
< ET61X251_MAX_DEVICES
-1) ? dev_nr
+1 : 0;
2619 #ifdef CONFIG_VIDEO_ADV_DEBUG
2620 err
= et61x251_create_sysfs(cam
);
2622 DBG(2, "Optional device control through 'sysfs' "
2625 DBG(2, "Failed to create 'sysfs' interface for optional "
2626 "device controlling. Error #%d", err
);
2628 DBG(2, "Optional device control through 'sysfs' interface disabled");
2629 DBG(3, "Compile the kernel with the 'CONFIG_VIDEO_ADV_DEBUG' "
2630 "configuration option to enable it.");
2633 usb_set_intfdata(intf
, cam
);
2634 kref_init(&cam
->kref
);
2635 usb_get_dev(cam
->usbdev
);
2637 complete_all(&cam
->probe
);
2643 kfree(cam
->control_buffer
);
2645 video_device_release(cam
->v4ldev
);
2652 static void et61x251_usb_disconnect(struct usb_interface
* intf
)
2654 struct et61x251_device
* cam
;
2656 down_write(&et61x251_dev_lock
);
2658 cam
= usb_get_intfdata(intf
);
2660 DBG(2, "Disconnecting %s...", cam
->v4ldev
->name
);
2663 DBG(2, "Device %s is open! Deregistration and memory "
2664 "deallocation are deferred.",
2665 video_device_node_name(cam
->v4ldev
));
2666 cam
->state
|= DEV_MISCONFIGURED
;
2667 et61x251_stop_transfer(cam
);
2668 cam
->state
|= DEV_DISCONNECTED
;
2669 wake_up_interruptible(&cam
->wait_frame
);
2670 wake_up(&cam
->wait_stream
);
2672 cam
->state
|= DEV_DISCONNECTED
;
2674 wake_up_interruptible_all(&cam
->wait_open
);
2676 kref_put(&cam
->kref
, et61x251_release_resources
);
2678 up_write(&et61x251_dev_lock
);
2682 static struct usb_driver et61x251_usb_driver
= {
2684 .id_table
= et61x251_id_table
,
2685 .probe
= et61x251_usb_probe
,
2686 .disconnect
= et61x251_usb_disconnect
,
2689 /*****************************************************************************/
2691 static int __init
et61x251_module_init(void)
2695 KDBG(2, ET61X251_MODULE_NAME
" v" ET61X251_MODULE_VERSION
);
2696 KDBG(3, ET61X251_MODULE_AUTHOR
);
2698 if ((err
= usb_register(&et61x251_usb_driver
)))
2699 KDBG(1, "usb_register() failed");
2705 static void __exit
et61x251_module_exit(void)
2707 usb_deregister(&et61x251_usb_driver
);
2711 module_init(et61x251_module_init
);
2712 module_exit(et61x251_module_exit
);