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/moduleparam.h>
26 #include <linux/errno.h>
27 #include <linux/slab.h>
28 #include <linux/device.h>
30 #include <linux/delay.h>
31 #include <linux/compiler.h>
32 #include <linux/ioctl.h>
33 #include <linux/poll.h>
34 #include <linux/stat.h>
36 #include <linux/vmalloc.h>
37 #include <linux/page-flags.h>
38 #include <linux/byteorder/generic.h>
40 #include <asm/uaccess.h>
44 /*****************************************************************************/
46 #define ET61X251_MODULE_NAME "V4L2 driver for ET61X[12]51 " \
47 "PC Camera Controllers"
48 #define ET61X251_MODULE_AUTHOR "(C) 2006 Luca Risolia"
49 #define ET61X251_AUTHOR_EMAIL "<luca.risolia@studio.unibo.it>"
50 #define ET61X251_MODULE_LICENSE "GPL"
51 #define ET61X251_MODULE_VERSION "1:1.04"
52 #define ET61X251_MODULE_VERSION_CODE KERNEL_VERSION(1, 1, 4)
54 /*****************************************************************************/
56 MODULE_DEVICE_TABLE(usb
, et61x251_id_table
);
58 MODULE_AUTHOR(ET61X251_MODULE_AUTHOR
" " ET61X251_AUTHOR_EMAIL
);
59 MODULE_DESCRIPTION(ET61X251_MODULE_NAME
);
60 MODULE_VERSION(ET61X251_MODULE_VERSION
);
61 MODULE_LICENSE(ET61X251_MODULE_LICENSE
);
63 static short video_nr
[] = {[0 ... ET61X251_MAX_DEVICES
-1] = -1};
64 module_param_array(video_nr
, short, NULL
, 0444);
65 MODULE_PARM_DESC(video_nr
,
66 "\n<-1|n[,...]> Specify V4L2 minor mode number."
67 "\n -1 = use next available (default)"
68 "\n n = use minor number n (integer >= 0)"
69 "\nYou can specify up to "
70 __MODULE_STRING(ET61X251_MAX_DEVICES
) " cameras this way."
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 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
, struct et61x251_sensor
* sensor
)
252 for (i
= 1; i
<= 8; i
++) {
253 if (sensor
->interface
== ET61X251_I2C_3WIRES
) {
254 r
= et61x251_read_reg(cam
, 0x8e);
255 if (!(r
& 0x02) && (r
>= 0))
258 r
= et61x251_read_reg(cam
, 0x8b);
259 if (!(r
& 0x01) && (r
>= 0))
264 udelay(8*8); /* minimum for sensors at 400kHz */
272 et61x251_i2c_try_read(struct et61x251_device
* cam
,
273 struct et61x251_sensor
* sensor
, u8 address
)
275 struct usb_device
* udev
= cam
->usbdev
;
276 u8
* data
= cam
->control_buffer
;
280 data
[1] = cam
->sensor
.i2c_slave_id
;
281 data
[2] = cam
->sensor
.rsta
| 0x10;
282 data
[3] = !(et61x251_read_reg(cam
, 0x8b) & 0x02);
283 res
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0), 0x00, 0x41,
284 0, 0x88, data
, 4, ET61X251_CTRL_TIMEOUT
);
288 err
+= et61x251_i2c_wait(cam
, sensor
);
290 res
= usb_control_msg(udev
, usb_rcvctrlpipe(udev
, 0), 0x00, 0xc1,
291 0, 0x80, data
, 8, ET61X251_CTRL_TIMEOUT
);
296 DBG(3, "I2C read failed for %s image sensor", sensor
->name
);
298 PDBGG("I2C read: address 0x%02X, value: 0x%02X", address
, data
[0]);
300 return err
? -1 : (int)data
[0];
305 et61x251_i2c_try_write(struct et61x251_device
* cam
,
306 struct et61x251_sensor
* sensor
, u8 address
, u8 value
)
308 struct usb_device
* udev
= cam
->usbdev
;
309 u8
* data
= cam
->control_buffer
;
313 data
[1] = cam
->sensor
.i2c_slave_id
;
314 data
[2] = cam
->sensor
.rsta
| 0x12;
315 res
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0), 0x00, 0x41,
316 0, 0x88, data
, 3, ET61X251_CTRL_TIMEOUT
);
321 res
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0), 0x00, 0x41,
322 0, 0x80, data
, 1, ET61X251_CTRL_TIMEOUT
);
326 err
+= et61x251_i2c_wait(cam
, sensor
);
329 DBG(3, "I2C write failed for %s image sensor", sensor
->name
);
331 PDBGG("I2C write: address 0x%02X, value: 0x%02X", address
, value
);
338 et61x251_i2c_raw_write(struct et61x251_device
* cam
, u8 n
, u8 data1
, u8 data2
,
339 u8 data3
, u8 data4
, u8 data5
, u8 data6
, u8 data7
,
340 u8 data8
, u8 address
)
342 struct usb_device
* udev
= cam
->usbdev
;
343 u8
* data
= cam
->control_buffer
;
353 res
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0), 0x00, 0x41,
354 0, 0x81, data
, n
-1, ET61X251_CTRL_TIMEOUT
);
359 data
[1] = cam
->sensor
.i2c_slave_id
;
360 data
[2] = cam
->sensor
.rsta
| 0x02 | (n
<< 4);
361 res
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0), 0x00, 0x41,
362 0, 0x88, data
, 3, ET61X251_CTRL_TIMEOUT
);
366 /* Start writing through the serial interface */
368 res
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0), 0x00, 0x41,
369 0, 0x80, data
, 1, ET61X251_CTRL_TIMEOUT
);
373 err
+= et61x251_i2c_wait(cam
, &cam
->sensor
);
376 DBG(3, "I2C raw write failed for %s image sensor",
379 PDBGG("I2C raw write: %u bytes, address = 0x%02X, data1 = 0x%02X, "
380 "data2 = 0x%02X, data3 = 0x%02X, data4 = 0x%02X, data5 = 0x%02X,"
381 " data6 = 0x%02X, data7 = 0x%02X, data8 = 0x%02X", n
, address
,
382 data1
, data2
, data3
, data4
, data5
, data6
, data7
, data8
);
389 int et61x251_i2c_read(struct et61x251_device
* cam
, u8 address
)
391 return et61x251_i2c_try_read(cam
, &cam
->sensor
, address
);
395 int et61x251_i2c_write(struct et61x251_device
* cam
, u8 address
, u8 value
)
397 return et61x251_i2c_try_write(cam
, &cam
->sensor
, address
, value
);
400 /*****************************************************************************/
402 static void et61x251_urb_complete(struct urb
*urb
)
404 struct et61x251_device
* cam
= urb
->context
;
405 struct et61x251_frame_t
** f
;
410 if (urb
->status
== -ENOENT
)
413 f
= &cam
->frame_current
;
415 if (cam
->stream
== STREAM_INTERRUPT
) {
416 cam
->stream
= STREAM_OFF
;
418 (*f
)->state
= F_QUEUED
;
419 DBG(3, "Stream interrupted");
420 wake_up(&cam
->wait_stream
);
423 if (cam
->state
& DEV_DISCONNECTED
)
426 if (cam
->state
& DEV_MISCONFIGURED
) {
427 wake_up_interruptible(&cam
->wait_frame
);
431 if (cam
->stream
== STREAM_OFF
|| list_empty(&cam
->inqueue
))
435 (*f
) = list_entry(cam
->inqueue
.next
, struct et61x251_frame_t
,
438 imagesize
= (cam
->sensor
.pix_format
.width
*
439 cam
->sensor
.pix_format
.height
*
440 cam
->sensor
.pix_format
.priv
) / 8;
442 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
443 unsigned int len
, status
;
446 const u8 VOID_BYTES
= 6;
449 len
= urb
->iso_frame_desc
[i
].actual_length
;
450 status
= urb
->iso_frame_desc
[i
].status
;
451 pos
= urb
->iso_frame_desc
[i
].offset
+ urb
->transfer_buffer
;
454 DBG(3, "Error in isochronous frame");
455 (*f
)->state
= F_ERROR
;
461 sof
= ((*b1
& 0x3f) == 63);
462 imglen
= ((*b1
& 0xc0) << 2) | *b2
;
464 PDBGG("Isochrnous frame: length %u, #%u i, image length %zu",
467 if ((*f
)->state
== F_QUEUED
|| (*f
)->state
== F_ERROR
)
470 (*f
)->state
= F_GRABBING
;
471 (*f
)->buf
.bytesused
= 0;
472 do_gettimeofday(&(*f
)->buf
.timestamp
);
474 DBG(3, "SOF detected: new video frame");
477 if ((*f
)->state
== F_GRABBING
) {
478 if (sof
&& (*f
)->buf
.bytesused
) {
479 if (cam
->sensor
.pix_format
.pixelformat
==
480 V4L2_PIX_FMT_ET61X251
)
483 DBG(3, "Not expected SOF detected "
485 (unsigned long)(*f
)->buf
.bytesused
);
486 (*f
)->state
= F_ERROR
;
491 if ((*f
)->buf
.bytesused
+ imglen
> imagesize
) {
492 DBG(3, "Video frame size exceeded");
493 (*f
)->state
= F_ERROR
;
499 memcpy((*f
)->bufmem
+(*f
)->buf
.bytesused
, pos
, imglen
);
500 (*f
)->buf
.bytesused
+= imglen
;
502 if ((*f
)->buf
.bytesused
== imagesize
) {
505 b
= (*f
)->buf
.bytesused
;
506 (*f
)->state
= F_DONE
;
507 (*f
)->buf
.sequence
= ++cam
->frame_count
;
508 spin_lock(&cam
->queue_lock
);
509 list_move_tail(&(*f
)->frame
, &cam
->outqueue
);
510 if (!list_empty(&cam
->inqueue
))
511 (*f
) = list_entry(cam
->inqueue
.next
,
512 struct et61x251_frame_t
,
516 spin_unlock(&cam
->queue_lock
);
517 DBG(3, "Video frame captured: : %lu bytes",
524 cam
->sensor
.pix_format
.pixelformat
==
525 V4L2_PIX_FMT_ET61X251
)
532 urb
->dev
= cam
->usbdev
;
533 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
534 if (err
< 0 && err
!= -EPERM
) {
535 cam
->state
|= DEV_MISCONFIGURED
;
536 DBG(1, "usb_submit_urb() failed");
539 wake_up_interruptible(&cam
->wait_frame
);
543 static int et61x251_start_transfer(struct et61x251_device
* cam
)
545 struct usb_device
*udev
= cam
->usbdev
;
547 struct usb_host_interface
* altsetting
= usb_altnum_to_altsetting(
548 usb_ifnum_to_if(udev
, 0),
549 ET61X251_ALTERNATE_SETTING
);
550 const unsigned int psz
= le16_to_cpu(altsetting
->
551 endpoint
[0].desc
.wMaxPacketSize
);
555 for (i
= 0; i
< ET61X251_URBS
; i
++) {
556 cam
->transfer_buffer
[i
] = kzalloc(ET61X251_ISO_PACKETS
* psz
,
558 if (!cam
->transfer_buffer
[i
]) {
560 DBG(1, "Not enough memory");
565 for (i
= 0; i
< ET61X251_URBS
; i
++) {
566 urb
= usb_alloc_urb(ET61X251_ISO_PACKETS
, GFP_KERNEL
);
570 DBG(1, "usb_alloc_urb() failed");
575 urb
->pipe
= usb_rcvisocpipe(udev
, 1);
576 urb
->transfer_flags
= URB_ISO_ASAP
;
577 urb
->number_of_packets
= ET61X251_ISO_PACKETS
;
578 urb
->complete
= et61x251_urb_complete
;
579 urb
->transfer_buffer
= cam
->transfer_buffer
[i
];
580 urb
->transfer_buffer_length
= psz
* ET61X251_ISO_PACKETS
;
582 for (j
= 0; j
< ET61X251_ISO_PACKETS
; j
++) {
583 urb
->iso_frame_desc
[j
].offset
= psz
* j
;
584 urb
->iso_frame_desc
[j
].length
= psz
;
588 err
= et61x251_write_reg(cam
, 0x01, 0x03);
589 err
= et61x251_write_reg(cam
, 0x00, 0x03);
590 err
= et61x251_write_reg(cam
, 0x08, 0x03);
593 DBG(1, "I/O hardware error");
597 err
= usb_set_interface(udev
, 0, ET61X251_ALTERNATE_SETTING
);
599 DBG(1, "usb_set_interface() failed");
603 cam
->frame_current
= NULL
;
605 for (i
= 0; i
< ET61X251_URBS
; i
++) {
606 err
= usb_submit_urb(cam
->urb
[i
], GFP_KERNEL
);
608 for (j
= i
-1; j
>= 0; j
--)
609 usb_kill_urb(cam
->urb
[j
]);
610 DBG(1, "usb_submit_urb() failed, error %d", err
);
618 for (i
= 0; (i
< ET61X251_URBS
) && cam
->urb
[i
]; i
++)
619 usb_free_urb(cam
->urb
[i
]);
622 for (i
= 0; (i
< ET61X251_URBS
) && cam
->transfer_buffer
[i
]; i
++)
623 kfree(cam
->transfer_buffer
[i
]);
629 static int et61x251_stop_transfer(struct et61x251_device
* cam
)
631 struct usb_device
*udev
= cam
->usbdev
;
635 if (cam
->state
& DEV_DISCONNECTED
)
638 for (i
= ET61X251_URBS
-1; i
>= 0; i
--) {
639 usb_kill_urb(cam
->urb
[i
]);
640 usb_free_urb(cam
->urb
[i
]);
641 kfree(cam
->transfer_buffer
[i
]);
644 err
= usb_set_interface(udev
, 0, 0); /* 0 Mb/s */
646 DBG(3, "usb_set_interface() failed");
652 static int et61x251_stream_interrupt(struct et61x251_device
* cam
)
656 cam
->stream
= STREAM_INTERRUPT
;
657 timeout
= wait_event_timeout(cam
->wait_stream
,
658 (cam
->stream
== STREAM_OFF
) ||
659 (cam
->state
& DEV_DISCONNECTED
),
660 ET61X251_URB_TIMEOUT
);
661 if (cam
->state
& DEV_DISCONNECTED
)
663 else if (cam
->stream
!= STREAM_OFF
) {
664 cam
->state
|= DEV_MISCONFIGURED
;
665 DBG(1, "URB timeout reached. The camera is misconfigured. To "
666 "use it, close and open /dev/video%d again.",
674 /*****************************************************************************/
676 #ifdef CONFIG_VIDEO_ADV_DEBUG
677 static u8
et61x251_strtou8(const char* buff
, size_t len
, ssize_t
* count
)
684 strncpy(str
, buff
, len
);
687 strncpy(str
, buff
, 4);
691 val
= simple_strtoul(str
, &endp
, 0);
695 *count
= (ssize_t
)(endp
- str
);
696 if ((*count
) && (len
== *count
+1) && (buff
[*count
] == '\n'))
703 NOTE 1: being inside one of the following methods implies that the v4l
704 device exists for sure (see kobjects and reference counters)
705 NOTE 2: buffers are PAGE_SIZE long
708 static ssize_t
et61x251_show_reg(struct class_device
* cd
, char* buf
)
710 struct et61x251_device
* cam
;
713 if (mutex_lock_interruptible(&et61x251_sysfs_lock
))
716 cam
= video_get_drvdata(to_video_device(cd
));
718 mutex_unlock(&et61x251_sysfs_lock
);
722 count
= sprintf(buf
, "%u\n", cam
->sysfs
.reg
);
724 mutex_unlock(&et61x251_sysfs_lock
);
731 et61x251_store_reg(struct class_device
* cd
, const char* buf
, size_t len
)
733 struct et61x251_device
* cam
;
737 if (mutex_lock_interruptible(&et61x251_sysfs_lock
))
740 cam
= video_get_drvdata(to_video_device(cd
));
742 mutex_unlock(&et61x251_sysfs_lock
);
746 index
= et61x251_strtou8(buf
, len
, &count
);
747 if (index
> 0x8e || !count
) {
748 mutex_unlock(&et61x251_sysfs_lock
);
752 cam
->sysfs
.reg
= index
;
754 DBG(2, "Moved ET61X[12]51 register index to 0x%02X", cam
->sysfs
.reg
);
755 DBG(3, "Written bytes: %zd", count
);
757 mutex_unlock(&et61x251_sysfs_lock
);
763 static ssize_t
et61x251_show_val(struct class_device
* cd
, char* buf
)
765 struct et61x251_device
* cam
;
769 if (mutex_lock_interruptible(&et61x251_sysfs_lock
))
772 cam
= video_get_drvdata(to_video_device(cd
));
774 mutex_unlock(&et61x251_sysfs_lock
);
778 if ((val
= et61x251_read_reg(cam
, cam
->sysfs
.reg
)) < 0) {
779 mutex_unlock(&et61x251_sysfs_lock
);
783 count
= sprintf(buf
, "%d\n", val
);
785 DBG(3, "Read bytes: %zd", count
);
787 mutex_unlock(&et61x251_sysfs_lock
);
794 et61x251_store_val(struct class_device
* cd
, const char* buf
, size_t len
)
796 struct et61x251_device
* cam
;
801 if (mutex_lock_interruptible(&et61x251_sysfs_lock
))
804 cam
= video_get_drvdata(to_video_device(cd
));
806 mutex_unlock(&et61x251_sysfs_lock
);
810 value
= et61x251_strtou8(buf
, len
, &count
);
812 mutex_unlock(&et61x251_sysfs_lock
);
816 err
= et61x251_write_reg(cam
, value
, cam
->sysfs
.reg
);
818 mutex_unlock(&et61x251_sysfs_lock
);
822 DBG(2, "Written ET61X[12]51 reg. 0x%02X, val. 0x%02X",
823 cam
->sysfs
.reg
, value
);
824 DBG(3, "Written bytes: %zd", count
);
826 mutex_unlock(&et61x251_sysfs_lock
);
832 static ssize_t
et61x251_show_i2c_reg(struct class_device
* cd
, char* buf
)
834 struct et61x251_device
* cam
;
837 if (mutex_lock_interruptible(&et61x251_sysfs_lock
))
840 cam
= video_get_drvdata(to_video_device(cd
));
842 mutex_unlock(&et61x251_sysfs_lock
);
846 count
= sprintf(buf
, "%u\n", cam
->sysfs
.i2c_reg
);
848 DBG(3, "Read bytes: %zd", count
);
850 mutex_unlock(&et61x251_sysfs_lock
);
857 et61x251_store_i2c_reg(struct class_device
* cd
, const char* buf
, size_t len
)
859 struct et61x251_device
* cam
;
863 if (mutex_lock_interruptible(&et61x251_sysfs_lock
))
866 cam
= video_get_drvdata(to_video_device(cd
));
868 mutex_unlock(&et61x251_sysfs_lock
);
872 index
= et61x251_strtou8(buf
, len
, &count
);
874 mutex_unlock(&et61x251_sysfs_lock
);
878 cam
->sysfs
.i2c_reg
= index
;
880 DBG(2, "Moved sensor register index to 0x%02X", cam
->sysfs
.i2c_reg
);
881 DBG(3, "Written bytes: %zd", count
);
883 mutex_unlock(&et61x251_sysfs_lock
);
889 static ssize_t
et61x251_show_i2c_val(struct class_device
* cd
, char* buf
)
891 struct et61x251_device
* cam
;
895 if (mutex_lock_interruptible(&et61x251_sysfs_lock
))
898 cam
= video_get_drvdata(to_video_device(cd
));
900 mutex_unlock(&et61x251_sysfs_lock
);
904 if (!(cam
->sensor
.sysfs_ops
& ET61X251_I2C_READ
)) {
905 mutex_unlock(&et61x251_sysfs_lock
);
909 if ((val
= et61x251_i2c_read(cam
, cam
->sysfs
.i2c_reg
)) < 0) {
910 mutex_unlock(&et61x251_sysfs_lock
);
914 count
= sprintf(buf
, "%d\n", val
);
916 DBG(3, "Read bytes: %zd", count
);
918 mutex_unlock(&et61x251_sysfs_lock
);
925 et61x251_store_i2c_val(struct class_device
* cd
, const char* buf
, size_t len
)
927 struct et61x251_device
* cam
;
932 if (mutex_lock_interruptible(&et61x251_sysfs_lock
))
935 cam
= video_get_drvdata(to_video_device(cd
));
937 mutex_unlock(&et61x251_sysfs_lock
);
941 if (!(cam
->sensor
.sysfs_ops
& ET61X251_I2C_READ
)) {
942 mutex_unlock(&et61x251_sysfs_lock
);
946 value
= et61x251_strtou8(buf
, len
, &count
);
948 mutex_unlock(&et61x251_sysfs_lock
);
952 err
= et61x251_i2c_write(cam
, cam
->sysfs
.i2c_reg
, value
);
954 mutex_unlock(&et61x251_sysfs_lock
);
958 DBG(2, "Written sensor reg. 0x%02X, val. 0x%02X",
959 cam
->sysfs
.i2c_reg
, value
);
960 DBG(3, "Written bytes: %zd", count
);
962 mutex_unlock(&et61x251_sysfs_lock
);
968 static CLASS_DEVICE_ATTR(reg
, S_IRUGO
| S_IWUSR
,
969 et61x251_show_reg
, et61x251_store_reg
);
970 static CLASS_DEVICE_ATTR(val
, S_IRUGO
| S_IWUSR
,
971 et61x251_show_val
, et61x251_store_val
);
972 static CLASS_DEVICE_ATTR(i2c_reg
, S_IRUGO
| S_IWUSR
,
973 et61x251_show_i2c_reg
, et61x251_store_i2c_reg
);
974 static CLASS_DEVICE_ATTR(i2c_val
, S_IRUGO
| S_IWUSR
,
975 et61x251_show_i2c_val
, et61x251_store_i2c_val
);
978 static int et61x251_create_sysfs(struct et61x251_device
* cam
)
980 struct video_device
*v4ldev
= cam
->v4ldev
;
983 if ((err
= video_device_create_file(v4ldev
, &class_device_attr_reg
)))
985 if ((err
= video_device_create_file(v4ldev
, &class_device_attr_val
)))
988 if (cam
->sensor
.sysfs_ops
) {
989 if ((err
= video_device_create_file(v4ldev
,
990 &class_device_attr_i2c_reg
)))
992 if ((err
= video_device_create_file(v4ldev
,
993 &class_device_attr_i2c_val
)))
998 if (cam
->sensor
.sysfs_ops
)
999 video_device_remove_file(v4ldev
, &class_device_attr_i2c_reg
);
1001 video_device_remove_file(v4ldev
, &class_device_attr_val
);
1003 video_device_remove_file(v4ldev
, &class_device_attr_reg
);
1007 #endif /* CONFIG_VIDEO_ADV_DEBUG */
1009 /*****************************************************************************/
1012 et61x251_set_pix_format(struct et61x251_device
* cam
,
1013 struct v4l2_pix_format
* pix
)
1017 if ((r
= et61x251_read_reg(cam
, 0x12)) < 0)
1019 if (pix
->pixelformat
== V4L2_PIX_FMT_ET61X251
)
1020 err
+= et61x251_write_reg(cam
, r
& 0xfd, 0x12);
1022 err
+= et61x251_write_reg(cam
, r
| 0x02, 0x12);
1024 return err
? -EIO
: 0;
1029 et61x251_set_compression(struct et61x251_device
* cam
,
1030 struct v4l2_jpegcompression
* compression
)
1034 if ((r
= et61x251_read_reg(cam
, 0x12)) < 0)
1036 if (compression
->quality
== 0)
1037 err
+= et61x251_write_reg(cam
, r
& 0xfb, 0x12);
1039 err
+= et61x251_write_reg(cam
, r
| 0x04, 0x12);
1041 return err
? -EIO
: 0;
1045 static int et61x251_set_scale(struct et61x251_device
* cam
, u8 scale
)
1049 r
= et61x251_read_reg(cam
, 0x12);
1054 err
+= et61x251_write_reg(cam
, r
& ~0x01, 0x12);
1055 else if (scale
== 2)
1056 err
+= et61x251_write_reg(cam
, r
| 0x01, 0x12);
1061 PDBGG("Scaling factor: %u", scale
);
1068 et61x251_set_crop(struct et61x251_device
* cam
, struct v4l2_rect
* rect
)
1070 struct et61x251_sensor
* s
= &cam
->sensor
;
1071 u16 fmw_sx
= (u16
)(rect
->left
- s
->cropcap
.bounds
.left
+
1072 s
->active_pixel
.left
),
1073 fmw_sy
= (u16
)(rect
->top
- s
->cropcap
.bounds
.top
+
1074 s
->active_pixel
.top
),
1075 fmw_length
= (u16
)(rect
->width
),
1076 fmw_height
= (u16
)(rect
->height
);
1079 err
+= et61x251_write_reg(cam
, fmw_sx
& 0xff, 0x69);
1080 err
+= et61x251_write_reg(cam
, fmw_sy
& 0xff, 0x6a);
1081 err
+= et61x251_write_reg(cam
, fmw_length
& 0xff, 0x6b);
1082 err
+= et61x251_write_reg(cam
, fmw_height
& 0xff, 0x6c);
1083 err
+= et61x251_write_reg(cam
, (fmw_sx
>> 8) | ((fmw_sy
& 0x300) >> 6)
1084 | ((fmw_length
& 0x300) >> 4)
1085 | ((fmw_height
& 0x300) >> 2), 0x6d);
1089 PDBGG("fmw_sx, fmw_sy, fmw_length, fmw_height: %u %u %u %u",
1090 fmw_sx
, fmw_sy
, fmw_length
, fmw_height
);
1096 static int et61x251_init(struct et61x251_device
* cam
)
1098 struct et61x251_sensor
* s
= &cam
->sensor
;
1099 struct v4l2_control ctrl
;
1100 struct v4l2_queryctrl
*qctrl
;
1101 struct v4l2_rect
* rect
;
1105 if (!(cam
->state
& DEV_INITIALIZED
)) {
1106 init_waitqueue_head(&cam
->open
);
1108 rect
= &(s
->cropcap
.defrect
);
1109 cam
->compression
.quality
= ET61X251_COMPRESSION_QUALITY
;
1110 } else { /* use current values */
1115 err
+= et61x251_set_scale(cam
, rect
->width
/ s
->pix_format
.width
);
1116 err
+= et61x251_set_crop(cam
, rect
);
1123 DBG(3, "Sensor initialization failed");
1128 err
+= et61x251_set_compression(cam
, &cam
->compression
);
1129 err
+= et61x251_set_pix_format(cam
, &s
->pix_format
);
1130 if (s
->set_pix_format
)
1131 err
+= s
->set_pix_format(cam
, &s
->pix_format
);
1135 if (s
->pix_format
.pixelformat
== V4L2_PIX_FMT_ET61X251
)
1136 DBG(3, "Compressed video format is active, quality %d",
1137 cam
->compression
.quality
);
1139 DBG(3, "Uncompressed video format is active");
1142 if ((err
= s
->set_crop(cam
, rect
))) {
1143 DBG(3, "set_crop() failed");
1148 for (i
= 0; i
< ARRAY_SIZE(s
->qctrl
); i
++)
1149 if (s
->qctrl
[i
].id
!= 0 &&
1150 !(s
->qctrl
[i
].flags
& V4L2_CTRL_FLAG_DISABLED
)) {
1151 ctrl
.id
= s
->qctrl
[i
].id
;
1152 ctrl
.value
= qctrl
[i
].default_value
;
1153 err
= s
->set_ctrl(cam
, &ctrl
);
1155 DBG(3, "Set %s control failed",
1159 DBG(3, "Image sensor supports '%s' control",
1164 if (!(cam
->state
& DEV_INITIALIZED
)) {
1165 mutex_init(&cam
->fileop_mutex
);
1166 spin_lock_init(&cam
->queue_lock
);
1167 init_waitqueue_head(&cam
->wait_frame
);
1168 init_waitqueue_head(&cam
->wait_stream
);
1169 cam
->nreadbuffers
= 2;
1170 memcpy(s
->_qctrl
, s
->qctrl
, sizeof(s
->qctrl
));
1171 memcpy(&(s
->_rect
), &(s
->cropcap
.defrect
),
1172 sizeof(struct v4l2_rect
));
1173 cam
->state
|= DEV_INITIALIZED
;
1176 DBG(2, "Initialization succeeded");
1181 static void et61x251_release_resources(struct et61x251_device
* cam
)
1183 mutex_lock(&et61x251_sysfs_lock
);
1185 DBG(2, "V4L2 device /dev/video%d deregistered", cam
->v4ldev
->minor
);
1186 video_set_drvdata(cam
->v4ldev
, NULL
);
1187 video_unregister_device(cam
->v4ldev
);
1189 mutex_unlock(&et61x251_sysfs_lock
);
1191 kfree(cam
->control_buffer
);
1194 /*****************************************************************************/
1196 static int et61x251_open(struct inode
* inode
, struct file
* filp
)
1198 struct et61x251_device
* cam
;
1202 This is the only safe way to prevent race conditions with
1205 if (!down_read_trylock(&et61x251_disconnect
))
1206 return -ERESTARTSYS
;
1208 cam
= video_get_drvdata(video_devdata(filp
));
1210 if (mutex_lock_interruptible(&cam
->dev_mutex
)) {
1211 up_read(&et61x251_disconnect
);
1212 return -ERESTARTSYS
;
1216 DBG(2, "Device /dev/video%d is busy...", cam
->v4ldev
->minor
);
1217 if ((filp
->f_flags
& O_NONBLOCK
) ||
1218 (filp
->f_flags
& O_NDELAY
)) {
1222 mutex_unlock(&cam
->dev_mutex
);
1223 err
= wait_event_interruptible_exclusive(cam
->open
,
1224 cam
->state
& DEV_DISCONNECTED
1227 up_read(&et61x251_disconnect
);
1230 if (cam
->state
& DEV_DISCONNECTED
) {
1231 up_read(&et61x251_disconnect
);
1234 mutex_lock(&cam
->dev_mutex
);
1238 if (cam
->state
& DEV_MISCONFIGURED
) {
1239 err
= et61x251_init(cam
);
1241 DBG(1, "Initialization failed again. "
1242 "I will retry on next open().");
1245 cam
->state
&= ~DEV_MISCONFIGURED
;
1248 if ((err
= et61x251_start_transfer(cam
)))
1251 filp
->private_data
= cam
;
1254 cam
->stream
= STREAM_OFF
;
1256 cam
->frame_count
= 0;
1257 et61x251_empty_framequeues(cam
);
1259 DBG(3, "Video device /dev/video%d is open", cam
->v4ldev
->minor
);
1262 mutex_unlock(&cam
->dev_mutex
);
1263 up_read(&et61x251_disconnect
);
1268 static int et61x251_release(struct inode
* inode
, struct file
* filp
)
1270 struct et61x251_device
* cam
= video_get_drvdata(video_devdata(filp
));
1272 mutex_lock(&cam
->dev_mutex
); /* prevent disconnect() to be called */
1274 et61x251_stop_transfer(cam
);
1276 et61x251_release_buffers(cam
);
1278 if (cam
->state
& DEV_DISCONNECTED
) {
1279 et61x251_release_resources(cam
);
1280 usb_put_dev(cam
->usbdev
);
1281 mutex_unlock(&cam
->dev_mutex
);
1287 wake_up_interruptible_nr(&cam
->open
, 1);
1289 DBG(3, "Video device /dev/video%d closed", cam
->v4ldev
->minor
);
1291 mutex_unlock(&cam
->dev_mutex
);
1298 et61x251_read(struct file
* filp
, char __user
* buf
,
1299 size_t count
, loff_t
* f_pos
)
1301 struct et61x251_device
* cam
= video_get_drvdata(video_devdata(filp
));
1302 struct et61x251_frame_t
* f
, * i
;
1303 unsigned long lock_flags
;
1307 if (mutex_lock_interruptible(&cam
->fileop_mutex
))
1308 return -ERESTARTSYS
;
1310 if (cam
->state
& DEV_DISCONNECTED
) {
1311 DBG(1, "Device not present");
1312 mutex_unlock(&cam
->fileop_mutex
);
1316 if (cam
->state
& DEV_MISCONFIGURED
) {
1317 DBG(1, "The camera is misconfigured. Close and open it "
1319 mutex_unlock(&cam
->fileop_mutex
);
1323 if (cam
->io
== IO_MMAP
) {
1324 DBG(3, "Close and open the device again to choose the read "
1326 mutex_unlock(&cam
->fileop_mutex
);
1330 if (cam
->io
== IO_NONE
) {
1331 if (!et61x251_request_buffers(cam
, cam
->nreadbuffers
,
1333 DBG(1, "read() failed, not enough memory");
1334 mutex_unlock(&cam
->fileop_mutex
);
1338 cam
->stream
= STREAM_ON
;
1341 if (list_empty(&cam
->inqueue
)) {
1342 if (!list_empty(&cam
->outqueue
))
1343 et61x251_empty_framequeues(cam
);
1344 et61x251_queue_unusedframes(cam
);
1348 mutex_unlock(&cam
->fileop_mutex
);
1352 if (list_empty(&cam
->outqueue
)) {
1353 if (filp
->f_flags
& O_NONBLOCK
) {
1354 mutex_unlock(&cam
->fileop_mutex
);
1357 timeout
= wait_event_interruptible_timeout
1359 (!list_empty(&cam
->outqueue
)) ||
1360 (cam
->state
& DEV_DISCONNECTED
) ||
1361 (cam
->state
& DEV_MISCONFIGURED
),
1362 cam
->module_param
.frame_timeout
*
1363 1000 * msecs_to_jiffies(1) );
1365 mutex_unlock(&cam
->fileop_mutex
);
1368 if (cam
->state
& DEV_DISCONNECTED
) {
1369 mutex_unlock(&cam
->fileop_mutex
);
1372 if (!timeout
|| (cam
->state
& DEV_MISCONFIGURED
)) {
1373 mutex_unlock(&cam
->fileop_mutex
);
1378 f
= list_entry(cam
->outqueue
.prev
, struct et61x251_frame_t
, frame
);
1380 if (count
> f
->buf
.bytesused
)
1381 count
= f
->buf
.bytesused
;
1383 if (copy_to_user(buf
, f
->bufmem
, count
)) {
1390 spin_lock_irqsave(&cam
->queue_lock
, lock_flags
);
1391 list_for_each_entry(i
, &cam
->outqueue
, frame
)
1392 i
->state
= F_UNUSED
;
1393 INIT_LIST_HEAD(&cam
->outqueue
);
1394 spin_unlock_irqrestore(&cam
->queue_lock
, lock_flags
);
1396 et61x251_queue_unusedframes(cam
);
1398 PDBGG("Frame #%lu, bytes read: %zu",
1399 (unsigned long)f
->buf
.index
, count
);
1401 mutex_unlock(&cam
->fileop_mutex
);
1403 return err
? err
: count
;
1407 static unsigned int et61x251_poll(struct file
*filp
, poll_table
*wait
)
1409 struct et61x251_device
* cam
= video_get_drvdata(video_devdata(filp
));
1410 struct et61x251_frame_t
* f
;
1411 unsigned long lock_flags
;
1412 unsigned int mask
= 0;
1414 if (mutex_lock_interruptible(&cam
->fileop_mutex
))
1417 if (cam
->state
& DEV_DISCONNECTED
) {
1418 DBG(1, "Device not present");
1422 if (cam
->state
& DEV_MISCONFIGURED
) {
1423 DBG(1, "The camera is misconfigured. Close and open it "
1428 if (cam
->io
== IO_NONE
) {
1429 if (!et61x251_request_buffers(cam
, cam
->nreadbuffers
,
1431 DBG(1, "poll() failed, not enough memory");
1435 cam
->stream
= STREAM_ON
;
1438 if (cam
->io
== IO_READ
) {
1439 spin_lock_irqsave(&cam
->queue_lock
, lock_flags
);
1440 list_for_each_entry(f
, &cam
->outqueue
, frame
)
1441 f
->state
= F_UNUSED
;
1442 INIT_LIST_HEAD(&cam
->outqueue
);
1443 spin_unlock_irqrestore(&cam
->queue_lock
, lock_flags
);
1444 et61x251_queue_unusedframes(cam
);
1447 poll_wait(filp
, &cam
->wait_frame
, wait
);
1449 if (!list_empty(&cam
->outqueue
))
1450 mask
|= POLLIN
| POLLRDNORM
;
1452 mutex_unlock(&cam
->fileop_mutex
);
1457 mutex_unlock(&cam
->fileop_mutex
);
1462 static void et61x251_vm_open(struct vm_area_struct
* vma
)
1464 struct et61x251_frame_t
* f
= vma
->vm_private_data
;
1469 static void et61x251_vm_close(struct vm_area_struct
* vma
)
1471 /* NOTE: buffers are not freed here */
1472 struct et61x251_frame_t
* f
= vma
->vm_private_data
;
1477 static struct vm_operations_struct et61x251_vm_ops
= {
1478 .open
= et61x251_vm_open
,
1479 .close
= et61x251_vm_close
,
1483 static int et61x251_mmap(struct file
* filp
, struct vm_area_struct
*vma
)
1485 struct et61x251_device
* cam
= video_get_drvdata(video_devdata(filp
));
1486 unsigned long size
= vma
->vm_end
- vma
->vm_start
,
1487 start
= vma
->vm_start
;
1491 if (mutex_lock_interruptible(&cam
->fileop_mutex
))
1492 return -ERESTARTSYS
;
1494 if (cam
->state
& DEV_DISCONNECTED
) {
1495 DBG(1, "Device not present");
1496 mutex_unlock(&cam
->fileop_mutex
);
1500 if (cam
->state
& DEV_MISCONFIGURED
) {
1501 DBG(1, "The camera is misconfigured. Close and open it "
1503 mutex_unlock(&cam
->fileop_mutex
);
1507 if (cam
->io
!= IO_MMAP
|| !(vma
->vm_flags
& VM_WRITE
) ||
1508 size
!= PAGE_ALIGN(cam
->frame
[0].buf
.length
)) {
1509 mutex_unlock(&cam
->fileop_mutex
);
1513 for (i
= 0; i
< cam
->nbuffers
; i
++) {
1514 if ((cam
->frame
[i
].buf
.m
.offset
>>PAGE_SHIFT
) == vma
->vm_pgoff
)
1517 if (i
== cam
->nbuffers
) {
1518 mutex_unlock(&cam
->fileop_mutex
);
1522 vma
->vm_flags
|= VM_IO
;
1523 vma
->vm_flags
|= VM_RESERVED
;
1525 pos
= cam
->frame
[i
].bufmem
;
1526 while (size
> 0) { /* size is page-aligned */
1527 if (vm_insert_page(vma
, start
, vmalloc_to_page(pos
))) {
1528 mutex_unlock(&cam
->fileop_mutex
);
1536 vma
->vm_ops
= &et61x251_vm_ops
;
1537 vma
->vm_private_data
= &cam
->frame
[i
];
1539 et61x251_vm_open(vma
);
1541 mutex_unlock(&cam
->fileop_mutex
);
1546 /*****************************************************************************/
1549 et61x251_vidioc_querycap(struct et61x251_device
* cam
, void __user
* arg
)
1551 struct v4l2_capability cap
= {
1552 .driver
= "et61x251",
1553 .version
= ET61X251_MODULE_VERSION_CODE
,
1554 .capabilities
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_READWRITE
|
1558 strlcpy(cap
.card
, cam
->v4ldev
->name
, sizeof(cap
.card
));
1559 if (usb_make_path(cam
->usbdev
, cap
.bus_info
, sizeof(cap
.bus_info
)) < 0)
1560 strlcpy(cap
.bus_info
, cam
->usbdev
->dev
.bus_id
,
1561 sizeof(cap
.bus_info
));
1563 if (copy_to_user(arg
, &cap
, sizeof(cap
)))
1571 et61x251_vidioc_enuminput(struct et61x251_device
* cam
, void __user
* arg
)
1573 struct v4l2_input i
;
1575 if (copy_from_user(&i
, arg
, sizeof(i
)))
1581 memset(&i
, 0, sizeof(i
));
1582 strcpy(i
.name
, "Camera");
1583 i
.type
= V4L2_INPUT_TYPE_CAMERA
;
1585 if (copy_to_user(arg
, &i
, sizeof(i
)))
1593 et61x251_vidioc_g_input(struct et61x251_device
* cam
, void __user
* arg
)
1597 if (copy_to_user(arg
, &index
, sizeof(index
)))
1605 et61x251_vidioc_s_input(struct et61x251_device
* cam
, void __user
* arg
)
1609 if (copy_from_user(&index
, arg
, sizeof(index
)))
1620 et61x251_vidioc_query_ctrl(struct et61x251_device
* cam
, void __user
* arg
)
1622 struct et61x251_sensor
* s
= &cam
->sensor
;
1623 struct v4l2_queryctrl qc
;
1626 if (copy_from_user(&qc
, arg
, sizeof(qc
)))
1629 for (i
= 0; i
< ARRAY_SIZE(s
->qctrl
); i
++)
1630 if (qc
.id
&& qc
.id
== s
->qctrl
[i
].id
) {
1631 memcpy(&qc
, &(s
->qctrl
[i
]), sizeof(qc
));
1632 if (copy_to_user(arg
, &qc
, sizeof(qc
)))
1642 et61x251_vidioc_g_ctrl(struct et61x251_device
* cam
, void __user
* arg
)
1644 struct et61x251_sensor
* s
= &cam
->sensor
;
1645 struct v4l2_control ctrl
;
1649 if (!s
->get_ctrl
&& !s
->set_ctrl
)
1652 if (copy_from_user(&ctrl
, arg
, sizeof(ctrl
)))
1656 for (i
= 0; i
< ARRAY_SIZE(s
->qctrl
); i
++)
1657 if (ctrl
.id
== s
->qctrl
[i
].id
) {
1658 ctrl
.value
= s
->_qctrl
[i
].default_value
;
1663 err
= s
->get_ctrl(cam
, &ctrl
);
1666 if (copy_to_user(arg
, &ctrl
, sizeof(ctrl
)))
1674 et61x251_vidioc_s_ctrl(struct et61x251_device
* cam
, void __user
* arg
)
1676 struct et61x251_sensor
* s
= &cam
->sensor
;
1677 struct v4l2_control ctrl
;
1684 if (copy_from_user(&ctrl
, arg
, sizeof(ctrl
)))
1687 for (i
= 0; i
< ARRAY_SIZE(s
->qctrl
); i
++)
1688 if (ctrl
.id
== s
->qctrl
[i
].id
) {
1689 if (s
->qctrl
[i
].flags
& V4L2_CTRL_FLAG_DISABLED
)
1691 if (ctrl
.value
< s
->qctrl
[i
].minimum
||
1692 ctrl
.value
> s
->qctrl
[i
].maximum
)
1694 ctrl
.value
-= ctrl
.value
% s
->qctrl
[i
].step
;
1698 if ((err
= s
->set_ctrl(cam
, &ctrl
)))
1701 s
->_qctrl
[i
].default_value
= ctrl
.value
;
1708 et61x251_vidioc_cropcap(struct et61x251_device
* cam
, void __user
* arg
)
1710 struct v4l2_cropcap
* cc
= &(cam
->sensor
.cropcap
);
1712 cc
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1713 cc
->pixelaspect
.numerator
= 1;
1714 cc
->pixelaspect
.denominator
= 1;
1716 if (copy_to_user(arg
, cc
, sizeof(*cc
)))
1724 et61x251_vidioc_g_crop(struct et61x251_device
* cam
, void __user
* arg
)
1726 struct et61x251_sensor
* s
= &cam
->sensor
;
1727 struct v4l2_crop crop
= {
1728 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
1731 memcpy(&(crop
.c
), &(s
->_rect
), sizeof(struct v4l2_rect
));
1733 if (copy_to_user(arg
, &crop
, sizeof(crop
)))
1741 et61x251_vidioc_s_crop(struct et61x251_device
* cam
, void __user
* arg
)
1743 struct et61x251_sensor
* s
= &cam
->sensor
;
1744 struct v4l2_crop crop
;
1745 struct v4l2_rect
* rect
;
1746 struct v4l2_rect
* bounds
= &(s
->cropcap
.bounds
);
1747 struct v4l2_pix_format
* pix_format
= &(s
->pix_format
);
1749 const enum et61x251_stream_state stream
= cam
->stream
;
1750 const u32 nbuffers
= cam
->nbuffers
;
1754 if (copy_from_user(&crop
, arg
, sizeof(crop
)))
1759 if (crop
.type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1762 if (cam
->module_param
.force_munmap
)
1763 for (i
= 0; i
< cam
->nbuffers
; i
++)
1764 if (cam
->frame
[i
].vma_use_count
) {
1765 DBG(3, "VIDIOC_S_CROP failed. "
1766 "Unmap the buffers first.");
1770 /* Preserve R,G or B origin */
1771 rect
->left
= (s
->_rect
.left
& 1L) ? rect
->left
| 1L : rect
->left
& ~1L;
1772 rect
->top
= (s
->_rect
.top
& 1L) ? rect
->top
| 1L : rect
->top
& ~1L;
1774 if (rect
->width
< 16)
1776 if (rect
->height
< 16)
1778 if (rect
->width
> bounds
->width
)
1779 rect
->width
= bounds
->width
;
1780 if (rect
->height
> bounds
->height
)
1781 rect
->height
= bounds
->height
;
1782 if (rect
->left
< bounds
->left
)
1783 rect
->left
= bounds
->left
;
1784 if (rect
->top
< bounds
->top
)
1785 rect
->top
= bounds
->top
;
1786 if (rect
->left
+ rect
->width
> bounds
->left
+ bounds
->width
)
1787 rect
->left
= bounds
->left
+bounds
->width
- rect
->width
;
1788 if (rect
->top
+ rect
->height
> bounds
->top
+ bounds
->height
)
1789 rect
->top
= bounds
->top
+bounds
->height
- rect
->height
;
1791 rect
->width
&= ~15L;
1792 rect
->height
&= ~15L;
1794 if (ET61X251_PRESERVE_IMGSCALE
) {
1795 /* Calculate the actual scaling factor */
1797 a
= rect
->width
* rect
->height
;
1798 b
= pix_format
->width
* pix_format
->height
;
1799 scale
= b
? (u8
)((a
/ b
) < 4 ? 1 : 2) : 1;
1803 if (cam
->stream
== STREAM_ON
)
1804 if ((err
= et61x251_stream_interrupt(cam
)))
1807 if (copy_to_user(arg
, &crop
, sizeof(crop
))) {
1808 cam
->stream
= stream
;
1812 if (cam
->module_param
.force_munmap
|| cam
->io
== IO_READ
)
1813 et61x251_release_buffers(cam
);
1815 err
= et61x251_set_crop(cam
, rect
);
1817 err
+= s
->set_crop(cam
, rect
);
1818 err
+= et61x251_set_scale(cam
, scale
);
1820 if (err
) { /* atomic, no rollback in ioctl() */
1821 cam
->state
|= DEV_MISCONFIGURED
;
1822 DBG(1, "VIDIOC_S_CROP failed because of hardware problems. To "
1823 "use the camera, close and open /dev/video%d again.",
1824 cam
->v4ldev
->minor
);
1828 s
->pix_format
.width
= rect
->width
/scale
;
1829 s
->pix_format
.height
= rect
->height
/scale
;
1830 memcpy(&(s
->_rect
), rect
, sizeof(*rect
));
1832 if ((cam
->module_param
.force_munmap
|| cam
->io
== IO_READ
) &&
1833 nbuffers
!= et61x251_request_buffers(cam
, nbuffers
, cam
->io
)) {
1834 cam
->state
|= DEV_MISCONFIGURED
;
1835 DBG(1, "VIDIOC_S_CROP failed because of not enough memory. To "
1836 "use the camera, close and open /dev/video%d again.",
1837 cam
->v4ldev
->minor
);
1841 if (cam
->io
== IO_READ
)
1842 et61x251_empty_framequeues(cam
);
1843 else if (cam
->module_param
.force_munmap
)
1844 et61x251_requeue_outqueue(cam
);
1846 cam
->stream
= stream
;
1853 et61x251_vidioc_enum_framesizes(struct et61x251_device
* cam
, void __user
* arg
)
1855 struct v4l2_frmsizeenum frmsize
;
1857 if (copy_from_user(&frmsize
, arg
, sizeof(frmsize
)))
1860 if (frmsize
.index
!= 0)
1863 if (frmsize
.pixel_format
!= V4L2_PIX_FMT_ET61X251
&&
1864 frmsize
.pixel_format
!= V4L2_PIX_FMT_SBGGR8
)
1867 frmsize
.type
= V4L2_FRMSIZE_TYPE_STEPWISE
;
1868 frmsize
.stepwise
.min_width
= frmsize
.stepwise
.step_width
= 16;
1869 frmsize
.stepwise
.min_height
= frmsize
.stepwise
.step_height
= 16;
1870 frmsize
.stepwise
.max_width
= cam
->sensor
.cropcap
.bounds
.width
;
1871 frmsize
.stepwise
.max_height
= cam
->sensor
.cropcap
.bounds
.height
;
1872 memset(&frmsize
.reserved
, 0, sizeof(frmsize
.reserved
));
1874 if (copy_to_user(arg
, &frmsize
, sizeof(frmsize
)))
1882 et61x251_vidioc_enum_fmt(struct et61x251_device
* cam
, void __user
* arg
)
1884 struct v4l2_fmtdesc fmtd
;
1886 if (copy_from_user(&fmtd
, arg
, sizeof(fmtd
)))
1889 if (fmtd
.type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1892 if (fmtd
.index
== 0) {
1893 strcpy(fmtd
.description
, "bayer rgb");
1894 fmtd
.pixelformat
= V4L2_PIX_FMT_SBGGR8
;
1895 } else if (fmtd
.index
== 1) {
1896 strcpy(fmtd
.description
, "compressed");
1897 fmtd
.pixelformat
= V4L2_PIX_FMT_ET61X251
;
1898 fmtd
.flags
= V4L2_FMT_FLAG_COMPRESSED
;
1902 fmtd
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1903 memset(&fmtd
.reserved
, 0, sizeof(fmtd
.reserved
));
1905 if (copy_to_user(arg
, &fmtd
, sizeof(fmtd
)))
1913 et61x251_vidioc_g_fmt(struct et61x251_device
* cam
, void __user
* arg
)
1915 struct v4l2_format format
;
1916 struct v4l2_pix_format
* pfmt
= &(cam
->sensor
.pix_format
);
1918 if (copy_from_user(&format
, arg
, sizeof(format
)))
1921 if (format
.type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1924 pfmt
->bytesperline
= (pfmt
->pixelformat
==V4L2_PIX_FMT_ET61X251
)
1925 ? 0 : (pfmt
->width
* pfmt
->priv
) / 8;
1926 pfmt
->sizeimage
= pfmt
->height
* ((pfmt
->width
*pfmt
->priv
)/8);
1927 pfmt
->field
= V4L2_FIELD_NONE
;
1928 memcpy(&(format
.fmt
.pix
), pfmt
, sizeof(*pfmt
));
1930 if (copy_to_user(arg
, &format
, sizeof(format
)))
1938 et61x251_vidioc_try_s_fmt(struct et61x251_device
* cam
, unsigned int cmd
,
1941 struct et61x251_sensor
* s
= &cam
->sensor
;
1942 struct v4l2_format format
;
1943 struct v4l2_pix_format
* pix
;
1944 struct v4l2_pix_format
* pfmt
= &(s
->pix_format
);
1945 struct v4l2_rect
* bounds
= &(s
->cropcap
.bounds
);
1946 struct v4l2_rect rect
;
1948 const enum et61x251_stream_state stream
= cam
->stream
;
1949 const u32 nbuffers
= cam
->nbuffers
;
1953 if (copy_from_user(&format
, arg
, sizeof(format
)))
1956 pix
= &(format
.fmt
.pix
);
1958 if (format
.type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1961 memcpy(&rect
, &(s
->_rect
), sizeof(rect
));
1963 { /* calculate the actual scaling factor */
1965 a
= rect
.width
* rect
.height
;
1966 b
= pix
->width
* pix
->height
;
1967 scale
= b
? (u8
)((a
/ b
) < 4 ? 1 : 2) : 1;
1970 rect
.width
= scale
* pix
->width
;
1971 rect
.height
= scale
* pix
->height
;
1973 if (rect
.width
< 16)
1975 if (rect
.height
< 16)
1977 if (rect
.width
> bounds
->left
+ bounds
->width
- rect
.left
)
1978 rect
.width
= bounds
->left
+ bounds
->width
- rect
.left
;
1979 if (rect
.height
> bounds
->top
+ bounds
->height
- rect
.top
)
1980 rect
.height
= bounds
->top
+ bounds
->height
- rect
.top
;
1983 rect
.height
&= ~15L;
1985 { /* adjust the scaling factor */
1987 a
= rect
.width
* rect
.height
;
1988 b
= pix
->width
* pix
->height
;
1989 scale
= b
? (u8
)((a
/ b
) < 4 ? 1 : 2) : 1;
1992 pix
->width
= rect
.width
/ scale
;
1993 pix
->height
= rect
.height
/ scale
;
1995 if (pix
->pixelformat
!= V4L2_PIX_FMT_ET61X251
&&
1996 pix
->pixelformat
!= V4L2_PIX_FMT_SBGGR8
)
1997 pix
->pixelformat
= pfmt
->pixelformat
;
1998 pix
->priv
= pfmt
->priv
; /* bpp */
1999 pix
->colorspace
= pfmt
->colorspace
;
2000 pix
->bytesperline
= (pix
->pixelformat
== V4L2_PIX_FMT_ET61X251
)
2001 ? 0 : (pix
->width
* pix
->priv
) / 8;
2002 pix
->sizeimage
= pix
->height
* ((pix
->width
* pix
->priv
) / 8);
2003 pix
->field
= V4L2_FIELD_NONE
;
2005 if (cmd
== VIDIOC_TRY_FMT
) {
2006 if (copy_to_user(arg
, &format
, sizeof(format
)))
2011 if (cam
->module_param
.force_munmap
)
2012 for (i
= 0; i
< cam
->nbuffers
; i
++)
2013 if (cam
->frame
[i
].vma_use_count
) {
2014 DBG(3, "VIDIOC_S_FMT failed. "
2015 "Unmap the buffers first.");
2019 if (cam
->stream
== STREAM_ON
)
2020 if ((err
= et61x251_stream_interrupt(cam
)))
2023 if (copy_to_user(arg
, &format
, sizeof(format
))) {
2024 cam
->stream
= stream
;
2028 if (cam
->module_param
.force_munmap
|| cam
->io
== IO_READ
)
2029 et61x251_release_buffers(cam
);
2031 err
+= et61x251_set_pix_format(cam
, pix
);
2032 err
+= et61x251_set_crop(cam
, &rect
);
2033 if (s
->set_pix_format
)
2034 err
+= s
->set_pix_format(cam
, pix
);
2036 err
+= s
->set_crop(cam
, &rect
);
2037 err
+= et61x251_set_scale(cam
, scale
);
2039 if (err
) { /* atomic, no rollback in ioctl() */
2040 cam
->state
|= DEV_MISCONFIGURED
;
2041 DBG(1, "VIDIOC_S_FMT failed because of hardware problems. To "
2042 "use the camera, close and open /dev/video%d again.",
2043 cam
->v4ldev
->minor
);
2047 memcpy(pfmt
, pix
, sizeof(*pix
));
2048 memcpy(&(s
->_rect
), &rect
, sizeof(rect
));
2050 if ((cam
->module_param
.force_munmap
|| cam
->io
== IO_READ
) &&
2051 nbuffers
!= et61x251_request_buffers(cam
, nbuffers
, cam
->io
)) {
2052 cam
->state
|= DEV_MISCONFIGURED
;
2053 DBG(1, "VIDIOC_S_FMT failed because of not enough memory. To "
2054 "use the camera, close and open /dev/video%d again.",
2055 cam
->v4ldev
->minor
);
2059 if (cam
->io
== IO_READ
)
2060 et61x251_empty_framequeues(cam
);
2061 else if (cam
->module_param
.force_munmap
)
2062 et61x251_requeue_outqueue(cam
);
2064 cam
->stream
= stream
;
2071 et61x251_vidioc_g_jpegcomp(struct et61x251_device
* cam
, void __user
* arg
)
2073 if (copy_to_user(arg
, &cam
->compression
,
2074 sizeof(cam
->compression
)))
2082 et61x251_vidioc_s_jpegcomp(struct et61x251_device
* cam
, void __user
* arg
)
2084 struct v4l2_jpegcompression jc
;
2085 const enum et61x251_stream_state stream
= cam
->stream
;
2088 if (copy_from_user(&jc
, arg
, sizeof(jc
)))
2091 if (jc
.quality
!= 0 && jc
.quality
!= 1)
2094 if (cam
->stream
== STREAM_ON
)
2095 if ((err
= et61x251_stream_interrupt(cam
)))
2098 err
+= et61x251_set_compression(cam
, &jc
);
2099 if (err
) { /* atomic, no rollback in ioctl() */
2100 cam
->state
|= DEV_MISCONFIGURED
;
2101 DBG(1, "VIDIOC_S_JPEGCOMP failed because of hardware "
2102 "problems. To use the camera, close and open "
2103 "/dev/video%d again.", cam
->v4ldev
->minor
);
2107 cam
->compression
.quality
= jc
.quality
;
2109 cam
->stream
= stream
;
2116 et61x251_vidioc_reqbufs(struct et61x251_device
* cam
, void __user
* arg
)
2118 struct v4l2_requestbuffers rb
;
2122 if (copy_from_user(&rb
, arg
, sizeof(rb
)))
2125 if (rb
.type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
2126 rb
.memory
!= V4L2_MEMORY_MMAP
)
2129 if (cam
->io
== IO_READ
) {
2130 DBG(3, "Close and open the device again to choose the mmap "
2135 for (i
= 0; i
< cam
->nbuffers
; i
++)
2136 if (cam
->frame
[i
].vma_use_count
) {
2137 DBG(3, "VIDIOC_REQBUFS failed. "
2138 "Previous buffers are still mapped.");
2142 if (cam
->stream
== STREAM_ON
)
2143 if ((err
= et61x251_stream_interrupt(cam
)))
2146 et61x251_empty_framequeues(cam
);
2148 et61x251_release_buffers(cam
);
2150 rb
.count
= et61x251_request_buffers(cam
, rb
.count
, IO_MMAP
);
2152 if (copy_to_user(arg
, &rb
, sizeof(rb
))) {
2153 et61x251_release_buffers(cam
);
2158 cam
->io
= rb
.count
? IO_MMAP
: IO_NONE
;
2165 et61x251_vidioc_querybuf(struct et61x251_device
* cam
, void __user
* arg
)
2167 struct v4l2_buffer b
;
2169 if (copy_from_user(&b
, arg
, sizeof(b
)))
2172 if (b
.type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
2173 b
.index
>= cam
->nbuffers
|| cam
->io
!= IO_MMAP
)
2176 memcpy(&b
, &cam
->frame
[b
.index
].buf
, sizeof(b
));
2178 if (cam
->frame
[b
.index
].vma_use_count
)
2179 b
.flags
|= V4L2_BUF_FLAG_MAPPED
;
2181 if (cam
->frame
[b
.index
].state
== F_DONE
)
2182 b
.flags
|= V4L2_BUF_FLAG_DONE
;
2183 else if (cam
->frame
[b
.index
].state
!= F_UNUSED
)
2184 b
.flags
|= V4L2_BUF_FLAG_QUEUED
;
2186 if (copy_to_user(arg
, &b
, sizeof(b
)))
2194 et61x251_vidioc_qbuf(struct et61x251_device
* cam
, void __user
* arg
)
2196 struct v4l2_buffer b
;
2197 unsigned long lock_flags
;
2199 if (copy_from_user(&b
, arg
, sizeof(b
)))
2202 if (b
.type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
2203 b
.index
>= cam
->nbuffers
|| cam
->io
!= IO_MMAP
)
2206 if (cam
->frame
[b
.index
].state
!= F_UNUSED
)
2209 cam
->frame
[b
.index
].state
= F_QUEUED
;
2211 spin_lock_irqsave(&cam
->queue_lock
, lock_flags
);
2212 list_add_tail(&cam
->frame
[b
.index
].frame
, &cam
->inqueue
);
2213 spin_unlock_irqrestore(&cam
->queue_lock
, lock_flags
);
2215 PDBGG("Frame #%lu queued", (unsigned long)b
.index
);
2222 et61x251_vidioc_dqbuf(struct et61x251_device
* cam
, struct file
* filp
,
2225 struct v4l2_buffer b
;
2226 struct et61x251_frame_t
*f
;
2227 unsigned long lock_flags
;
2230 if (copy_from_user(&b
, arg
, sizeof(b
)))
2233 if (b
.type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
|| cam
->io
!= IO_MMAP
)
2236 if (list_empty(&cam
->outqueue
)) {
2237 if (cam
->stream
== STREAM_OFF
)
2239 if (filp
->f_flags
& O_NONBLOCK
)
2241 timeout
= wait_event_interruptible_timeout
2243 (!list_empty(&cam
->outqueue
)) ||
2244 (cam
->state
& DEV_DISCONNECTED
) ||
2245 (cam
->state
& DEV_MISCONFIGURED
),
2246 cam
->module_param
.frame_timeout
*
2247 1000 * msecs_to_jiffies(1) );
2250 if (cam
->state
& DEV_DISCONNECTED
)
2252 if (!timeout
|| (cam
->state
& DEV_MISCONFIGURED
))
2256 spin_lock_irqsave(&cam
->queue_lock
, lock_flags
);
2257 f
= list_entry(cam
->outqueue
.next
, struct et61x251_frame_t
, frame
);
2258 list_del(cam
->outqueue
.next
);
2259 spin_unlock_irqrestore(&cam
->queue_lock
, lock_flags
);
2261 f
->state
= F_UNUSED
;
2263 memcpy(&b
, &f
->buf
, sizeof(b
));
2264 if (f
->vma_use_count
)
2265 b
.flags
|= V4L2_BUF_FLAG_MAPPED
;
2267 if (copy_to_user(arg
, &b
, sizeof(b
)))
2270 PDBGG("Frame #%lu dequeued", (unsigned long)f
->buf
.index
);
2277 et61x251_vidioc_streamon(struct et61x251_device
* cam
, void __user
* arg
)
2281 if (copy_from_user(&type
, arg
, sizeof(type
)))
2284 if (type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
|| cam
->io
!= IO_MMAP
)
2287 if (list_empty(&cam
->inqueue
))
2290 cam
->stream
= STREAM_ON
;
2292 DBG(3, "Stream on");
2299 et61x251_vidioc_streamoff(struct et61x251_device
* cam
, void __user
* arg
)
2303 if (copy_from_user(&type
, arg
, sizeof(type
)))
2306 if (type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
|| cam
->io
!= IO_MMAP
)
2309 if (cam
->stream
== STREAM_ON
)
2310 if ((err
= et61x251_stream_interrupt(cam
)))
2313 et61x251_empty_framequeues(cam
);
2315 DBG(3, "Stream off");
2322 et61x251_vidioc_g_parm(struct et61x251_device
* cam
, void __user
* arg
)
2324 struct v4l2_streamparm sp
;
2326 if (copy_from_user(&sp
, arg
, sizeof(sp
)))
2329 if (sp
.type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
2332 sp
.parm
.capture
.extendedmode
= 0;
2333 sp
.parm
.capture
.readbuffers
= cam
->nreadbuffers
;
2335 if (copy_to_user(arg
, &sp
, sizeof(sp
)))
2343 et61x251_vidioc_s_parm(struct et61x251_device
* cam
, void __user
* arg
)
2345 struct v4l2_streamparm sp
;
2347 if (copy_from_user(&sp
, arg
, sizeof(sp
)))
2350 if (sp
.type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
2353 sp
.parm
.capture
.extendedmode
= 0;
2355 if (sp
.parm
.capture
.readbuffers
== 0)
2356 sp
.parm
.capture
.readbuffers
= cam
->nreadbuffers
;
2358 if (sp
.parm
.capture
.readbuffers
> ET61X251_MAX_FRAMES
)
2359 sp
.parm
.capture
.readbuffers
= ET61X251_MAX_FRAMES
;
2361 if (copy_to_user(arg
, &sp
, sizeof(sp
)))
2364 cam
->nreadbuffers
= sp
.parm
.capture
.readbuffers
;
2370 static int et61x251_ioctl_v4l2(struct inode
* inode
, struct file
* filp
,
2371 unsigned int cmd
, void __user
* arg
)
2373 struct et61x251_device
* cam
= video_get_drvdata(video_devdata(filp
));
2377 case VIDIOC_QUERYCAP
:
2378 return et61x251_vidioc_querycap(cam
, arg
);
2380 case VIDIOC_ENUMINPUT
:
2381 return et61x251_vidioc_enuminput(cam
, arg
);
2383 case VIDIOC_G_INPUT
:
2384 return et61x251_vidioc_g_input(cam
, arg
);
2386 case VIDIOC_S_INPUT
:
2387 return et61x251_vidioc_s_input(cam
, arg
);
2389 case VIDIOC_QUERYCTRL
:
2390 return et61x251_vidioc_query_ctrl(cam
, arg
);
2393 return et61x251_vidioc_g_ctrl(cam
, arg
);
2396 return et61x251_vidioc_s_ctrl(cam
, arg
);
2398 case VIDIOC_CROPCAP
:
2399 return et61x251_vidioc_cropcap(cam
, arg
);
2402 return et61x251_vidioc_g_crop(cam
, arg
);
2405 return et61x251_vidioc_s_crop(cam
, arg
);
2407 case VIDIOC_ENUM_FMT
:
2408 return et61x251_vidioc_enum_fmt(cam
, arg
);
2411 return et61x251_vidioc_g_fmt(cam
, arg
);
2413 case VIDIOC_TRY_FMT
:
2415 return et61x251_vidioc_try_s_fmt(cam
, cmd
, arg
);
2417 case VIDIOC_ENUM_FRAMESIZES
:
2418 return et61x251_vidioc_enum_framesizes(cam
, arg
);
2420 case VIDIOC_G_JPEGCOMP
:
2421 return et61x251_vidioc_g_jpegcomp(cam
, arg
);
2423 case VIDIOC_S_JPEGCOMP
:
2424 return et61x251_vidioc_s_jpegcomp(cam
, arg
);
2426 case VIDIOC_REQBUFS
:
2427 return et61x251_vidioc_reqbufs(cam
, arg
);
2429 case VIDIOC_QUERYBUF
:
2430 return et61x251_vidioc_querybuf(cam
, arg
);
2433 return et61x251_vidioc_qbuf(cam
, arg
);
2436 return et61x251_vidioc_dqbuf(cam
, filp
, arg
);
2438 case VIDIOC_STREAMON
:
2439 return et61x251_vidioc_streamon(cam
, arg
);
2441 case VIDIOC_STREAMOFF
:
2442 return et61x251_vidioc_streamoff(cam
, arg
);
2445 return et61x251_vidioc_g_parm(cam
, arg
);
2448 return et61x251_vidioc_s_parm(cam
, arg
);
2452 case VIDIOC_QUERYSTD
:
2453 case VIDIOC_ENUMSTD
:
2454 case VIDIOC_QUERYMENU
:
2455 case VIDIOC_ENUM_FRAMEINTERVALS
:
2465 static int et61x251_ioctl(struct inode
* inode
, struct file
* filp
,
2466 unsigned int cmd
, unsigned long arg
)
2468 struct et61x251_device
* cam
= video_get_drvdata(video_devdata(filp
));
2471 if (mutex_lock_interruptible(&cam
->fileop_mutex
))
2472 return -ERESTARTSYS
;
2474 if (cam
->state
& DEV_DISCONNECTED
) {
2475 DBG(1, "Device not present");
2476 mutex_unlock(&cam
->fileop_mutex
);
2480 if (cam
->state
& DEV_MISCONFIGURED
) {
2481 DBG(1, "The camera is misconfigured. Close and open it "
2483 mutex_unlock(&cam
->fileop_mutex
);
2487 V4LDBG(3, "et61x251", cmd
);
2489 err
= et61x251_ioctl_v4l2(inode
, filp
, cmd
, (void __user
*)arg
);
2491 mutex_unlock(&cam
->fileop_mutex
);
2497 static const struct file_operations et61x251_fops
= {
2498 .owner
= THIS_MODULE
,
2499 .open
= et61x251_open
,
2500 .release
= et61x251_release
,
2501 .ioctl
= et61x251_ioctl
,
2502 .compat_ioctl
= v4l_compat_ioctl32
,
2503 .read
= et61x251_read
,
2504 .poll
= et61x251_poll
,
2505 .mmap
= et61x251_mmap
,
2506 .llseek
= no_llseek
,
2509 /*****************************************************************************/
2511 /* It exists a single interface only. We do not need to validate anything. */
2513 et61x251_usb_probe(struct usb_interface
* intf
, const struct usb_device_id
* id
)
2515 struct usb_device
*udev
= interface_to_usbdev(intf
);
2516 struct et61x251_device
* cam
;
2517 static unsigned int dev_nr
= 0;
2521 if (!(cam
= kzalloc(sizeof(struct et61x251_device
), GFP_KERNEL
)))
2526 if (!(cam
->control_buffer
= kzalloc(8, GFP_KERNEL
))) {
2527 DBG(1, "kmalloc() failed");
2532 if (!(cam
->v4ldev
= video_device_alloc())) {
2533 DBG(1, "video_device_alloc() failed");
2538 mutex_init(&cam
->dev_mutex
);
2540 DBG(2, "ET61X[12]51 PC Camera Controller detected "
2541 "(vid/pid 0x%04X:0x%04X)",id
->idVendor
, id
->idProduct
);
2543 for (i
= 0; et61x251_sensor_table
[i
]; i
++) {
2544 err
= et61x251_sensor_table
[i
](cam
);
2550 DBG(2, "%s image sensor detected", cam
->sensor
.name
);
2552 DBG(1, "No supported image sensor detected");
2557 if (et61x251_init(cam
)) {
2558 DBG(1, "Initialization failed. I will retry on open().");
2559 cam
->state
|= DEV_MISCONFIGURED
;
2562 strcpy(cam
->v4ldev
->name
, "ET61X[12]51 PC Camera");
2563 cam
->v4ldev
->owner
= THIS_MODULE
;
2564 cam
->v4ldev
->type
= VID_TYPE_CAPTURE
| VID_TYPE_SCALES
;
2565 cam
->v4ldev
->hardware
= 0;
2566 cam
->v4ldev
->fops
= &et61x251_fops
;
2567 cam
->v4ldev
->minor
= video_nr
[dev_nr
];
2568 cam
->v4ldev
->release
= video_device_release
;
2569 video_set_drvdata(cam
->v4ldev
, cam
);
2571 mutex_lock(&cam
->dev_mutex
);
2573 err
= video_register_device(cam
->v4ldev
, VFL_TYPE_GRABBER
,
2576 DBG(1, "V4L2 device registration failed");
2577 if (err
== -ENFILE
&& video_nr
[dev_nr
] == -1)
2578 DBG(1, "Free /dev/videoX node not found");
2579 video_nr
[dev_nr
] = -1;
2580 dev_nr
= (dev_nr
< ET61X251_MAX_DEVICES
-1) ? dev_nr
+1 : 0;
2581 mutex_unlock(&cam
->dev_mutex
);
2585 DBG(2, "V4L2 device registered as /dev/video%d", cam
->v4ldev
->minor
);
2587 cam
->module_param
.force_munmap
= force_munmap
[dev_nr
];
2588 cam
->module_param
.frame_timeout
= frame_timeout
[dev_nr
];
2590 dev_nr
= (dev_nr
< ET61X251_MAX_DEVICES
-1) ? dev_nr
+1 : 0;
2592 #ifdef CONFIG_VIDEO_ADV_DEBUG
2593 err
= et61x251_create_sysfs(cam
);
2595 DBG(2, "Optional device control through 'sysfs' "
2598 DBG(2, "Failed to create 'sysfs' interface for optional "
2599 "device controlling. Error #%d", err
);
2601 DBG(2, "Optional device control through 'sysfs' interface disabled");
2604 usb_set_intfdata(intf
, cam
);
2606 mutex_unlock(&cam
->dev_mutex
);
2612 kfree(cam
->control_buffer
);
2614 video_device_release(cam
->v4ldev
);
2621 static void et61x251_usb_disconnect(struct usb_interface
* intf
)
2623 struct et61x251_device
* cam
= usb_get_intfdata(intf
);
2628 down_write(&et61x251_disconnect
);
2630 mutex_lock(&cam
->dev_mutex
);
2632 DBG(2, "Disconnecting %s...", cam
->v4ldev
->name
);
2634 wake_up_interruptible_all(&cam
->open
);
2637 DBG(2, "Device /dev/video%d is open! Deregistration and "
2638 "memory deallocation are deferred on close.",
2639 cam
->v4ldev
->minor
);
2640 cam
->state
|= DEV_MISCONFIGURED
;
2641 et61x251_stop_transfer(cam
);
2642 cam
->state
|= DEV_DISCONNECTED
;
2643 wake_up_interruptible(&cam
->wait_frame
);
2644 wake_up(&cam
->wait_stream
);
2645 usb_get_dev(cam
->usbdev
);
2647 cam
->state
|= DEV_DISCONNECTED
;
2648 et61x251_release_resources(cam
);
2651 mutex_unlock(&cam
->dev_mutex
);
2656 up_write(&et61x251_disconnect
);
2660 static struct usb_driver et61x251_usb_driver
= {
2662 .id_table
= et61x251_id_table
,
2663 .probe
= et61x251_usb_probe
,
2664 .disconnect
= et61x251_usb_disconnect
,
2667 /*****************************************************************************/
2669 static int __init
et61x251_module_init(void)
2673 KDBG(2, ET61X251_MODULE_NAME
" v" ET61X251_MODULE_VERSION
);
2674 KDBG(3, ET61X251_MODULE_AUTHOR
);
2676 if ((err
= usb_register(&et61x251_usb_driver
)))
2677 KDBG(1, "usb_register() failed");
2683 static void __exit
et61x251_module_exit(void)
2685 usb_deregister(&et61x251_usb_driver
);
2689 module_init(et61x251_module_init
);
2690 module_exit(et61x251_module_exit
);