3 * @author Nicolas VIVIEN
7 * @brief Driver for Syntek USB video camera
9 * @note Copyright (C) Nicolas VIVIEN
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 #include <linux/module.h>
35 #include <linux/init.h>
36 #include <linux/kernel.h>
37 #include <linux/version.h>
38 #include <linux/errno.h>
39 #include <linux/slab.h>
40 #include <linux/kref.h>
41 #include <linux/vmalloc.h>
45 #include <linux/usb.h>
46 #include <media/v4l2-common.h>
47 #include <media/v4l2-ioctl.h>
52 static struct v4l2_file_operations v4l_stk11xx_fops
;
56 * @var stk11xx_image_sizes
57 * List of all resolutions supported by the driver
59 const struct stk11xx_coord stk11xx_image_sizes
[STK11XX_NBR_SIZES
] = {
74 * @var stk11xx_controls
75 * List of all V4Lv2 controls supported by the driver
76 * Default_value field will be overriden at runtime
78 static struct v4l2_queryctrl stk11xx_controls
[] = {
80 .id
= V4L2_CID_BRIGHTNESS
,
81 .type
= V4L2_CTRL_TYPE_INTEGER
,
86 .default_value
= 0x7f00,
89 .id
= V4L2_CID_WHITENESS
,
90 .type
= V4L2_CTRL_TYPE_INTEGER
,
95 .default_value
= 0x7f00,
98 .id
= V4L2_CID_SATURATION
,
99 .type
= V4L2_CTRL_TYPE_INTEGER
,
100 .name
= "Saturation",
104 .default_value
= 0x7f00,
107 .id
= V4L2_CID_CONTRAST
,
108 .type
= V4L2_CTRL_TYPE_INTEGER
,
113 .default_value
= 0x7f00,
117 .type
= V4L2_CTRL_TYPE_INTEGER
,
122 .default_value
= 0x7f00,
125 .id
= V4L2_CID_HFLIP
,
126 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
127 .name
= "Flip Horizontally",
131 .default_value
= 0, // will be actually set later
134 .id
= V4L2_CID_VFLIP
,
135 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
136 .name
= "Flip Vertically",
140 .default_value
= 0, // will be actually set later
147 * @param width Width of wished resolution
148 * @param height Height of wished resolution
150 * @returns 0 if all is OK
152 * @brief Select a video mode
154 * This function permits to check and select a video mode.
156 int v4l_stk11xx_select_video_mode(struct usb_stk11xx
*dev
, int width
, int height
)
162 // Check width and height
163 // Notice : this test is usefull for the Kopete application !
165 // Driver can't build an image smaller than the minimal resolution !
166 if ((width
< stk11xx_image_sizes
[0].x
)
167 || (height
< stk11xx_image_sizes
[0].y
)) {
168 width
= stk11xx_image_sizes
[0].x
;
169 height
= stk11xx_image_sizes
[0].y
;
172 // Driver can't build an image bigger than the maximal resolution !
173 switch (dev
->webcam_type
) {
175 if ((width
> stk11xx_image_sizes
[STK11XX_1280x1024
].x
)
176 || (height
> stk11xx_image_sizes
[STK11XX_1280x1024
].y
)) {
177 width
= stk11xx_image_sizes
[STK11XX_1280x1024
].x
;
178 height
= stk11xx_image_sizes
[STK11XX_1280x1024
].y
;
183 if ((width
> stk11xx_image_sizes
[STK11XX_640x480
].x
)
184 || (height
> stk11xx_image_sizes
[STK11XX_640x480
].y
)) {
185 width
= stk11xx_image_sizes
[STK11XX_640x480
].x
;
186 height
= stk11xx_image_sizes
[STK11XX_640x480
].y
;
191 if (! (((width
== 720) && (height
==576))
192 || ((width
== 720) && (height
==480))
193 || ((width
== 640) && (height
==480)))) {
204 // Seek the best resolution
205 switch (dev
->webcam_type
) {
207 for (i
=0, find
=0; i
<=STK11XX_1280x1024
; i
++) {
208 if (stk11xx_image_sizes
[i
].x
<= width
&& stk11xx_image_sizes
[i
].y
<= height
)
214 for (i
=0, find
=0; i
<=STK11XX_640x480
; i
++) {
215 if (stk11xx_image_sizes
[i
].x
<= width
&& stk11xx_image_sizes
[i
].y
<= height
)
221 for (i
=0, find
=0; i
<=STK11XX_720x576
; i
++) {
222 if (stk11xx_image_sizes
[i
].x
<= width
&& stk11xx_image_sizes
[i
].y
<= height
)
231 // Save the new resolution
232 dev
->resolution
= find
;
234 STK_DEBUG("Set mode %d [%dx%d]\n", dev
->resolution
,
235 stk11xx_image_sizes
[dev
->resolution
].x
, stk11xx_image_sizes
[dev
->resolution
].y
);
239 dev
->view
.y
= height
;
242 // Calculate the frame size
243 if (dev
->webcam_type
== STK11XX_PAL
) {
244 // Here, dev->resolution equals : 640x480 || 720x576
245 dev
->image
.x
= stk11xx_image_sizes
[dev
->resolution
].x
;
246 dev
->image
.y
= stk11xx_image_sizes
[dev
->resolution
].y
;
247 dev
->frame_size
= dev
->image
.x
* dev
->image
.y
;
250 switch (dev
->resolution
) {
253 case STK11XX_160x120
:
254 case STK11XX_213x160
:
255 case STK11XX_320x240
:
256 case STK11XX_640x480
:
257 dev
->image
.x
= stk11xx_image_sizes
[STK11XX_640x480
].x
;
258 dev
->image
.y
= stk11xx_image_sizes
[STK11XX_640x480
].y
;
259 dev
->frame_size
= dev
->image
.x
* dev
->image
.y
;
262 case STK11XX_720x576
:
263 case STK11XX_800x600
:
264 case STK11XX_1024x768
:
265 case STK11XX_1280x1024
:
266 dev
->image
.x
= stk11xx_image_sizes
[STK11XX_1280x1024
].x
;
267 dev
->image
.y
= stk11xx_image_sizes
[STK11XX_1280x1024
].y
;
268 dev
->frame_size
= dev
->image
.x
* dev
->image
.y
;
274 // Calculate the image size
275 switch (dev
->vsettings
.palette
) {
276 case STK11XX_PALETTE_RGB24
:
277 case STK11XX_PALETTE_BGR24
:
278 dev
->view_size
= 3 * dev
->view
.x
* dev
->view
.y
;
279 dev
->image_size
= 3 * dev
->frame_size
;
282 case STK11XX_PALETTE_RGB32
:
283 case STK11XX_PALETTE_BGR32
:
284 dev
->view_size
= 3 * dev
->view
.x
* dev
->view
.y
;
285 dev
->image_size
= 4 * dev
->frame_size
;
288 case STK11XX_PALETTE_UYVY
:
289 case STK11XX_PALETTE_YUYV
:
290 dev
->view_size
= 2 * dev
->view
.x
* dev
->view
.y
;
291 dev
->image_size
= 2 * dev
->frame_size
;
300 * @param fp File pointer
302 * @returns 0 if all is OK
304 * @brief Open the video device
306 * This function permits to open a video device (/dev/videoX)
308 static int v4l_stk11xx_open(struct file
*fp
)
312 struct usb_stk11xx
*dev
;
313 struct video_device
*vdev
;
315 vdev
= video_devdata(fp
);
316 dev
= video_get_drvdata(video_devdata(fp
));
319 STK_ERROR("Device not initialized !!!\n");
323 mutex_lock(&dev
->modlock
);
326 STK_DEBUG("Device is busy, someone is using the device\n");
327 mutex_unlock(&dev
->modlock
);
332 err
= stk11xx_allocate_buffers(dev
);
335 STK_ERROR("Failed to allocate buffer memory !\n");
336 mutex_unlock(&dev
->modlock
);
340 // Reset buffers and parameters
341 stk11xx_reset_buffers(dev
);
346 dev
->error_status
= 0;
347 dev
->visoc_errors
= 0;
348 dev
->vframes_error
= 0;
349 dev
->vframes_dumped
= 0;
350 dev
->vsettings
.hue
= 0xffff;
351 dev
->vsettings
.whiteness
= 0xffff;
352 dev
->vsettings
.depth
= 24;
353 dev
->vsettings
.palette
= STK11XX_PALETTE_BGR24
;
355 // Select the resolution by default
356 v4l_stk11xx_select_video_mode(dev
, 640, 480);
358 // Initialize the device
359 dev_stk11xx_init_camera(dev
);
360 dev_stk11xx_camera_on(dev
);
361 dev_stk11xx_reconf_camera(dev
);
364 err
= usb_stk11xx_isoc_init(dev
);
367 STK_ERROR("Failed to init ISOC stuff !\n");
368 usb_stk11xx_isoc_cleanup(dev
);
369 stk11xx_free_buffers(dev
);
370 mutex_unlock(&dev
->modlock
);
374 // Start the video stream
375 dev_stk11xx_start_stream(dev
);
378 dev_stk11xx_camera_settings(dev
);
380 // Register interface on power management
381 // usb_autopm_get_interface(dev->interface);
384 fp
->private_data
= vdev
;
386 mutex_unlock(&dev
->modlock
);
393 * @param fp File pointer
395 * @returns 0 if all is OK
397 * @brief Release an opened file.
399 * This function permits to release an opened file with the 'open' method.
401 static int v4l_stk11xx_release(struct file
*fp
)
403 struct usb_stk11xx
*dev
;
405 dev
= video_get_drvdata(video_devdata(fp
));
408 STK_ERROR("v4l_release called on closed device\n");
410 // Stop the video stream
411 dev_stk11xx_stop_stream(dev
);
413 // ISOC and URB cleanup
414 usb_stk11xx_isoc_cleanup(dev
);
417 stk11xx_free_buffers(dev
);
419 // Switch off the camera
420 dev_stk11xx_camera_off(dev
);
422 dev_stk11xx_camera_asleep(dev
);
424 // Unregister interface on power management
425 // usb_autopm_put_interface(dev->interface);
434 * @param fp File pointer
436 * @retval buf Buffer in user space
440 * @returns Count value
442 * @brief Read the video device
444 * This function is called by the application is reading the video device.
446 static ssize_t
v4l_stk11xx_read(struct file
*fp
, char __user
*buf
,
447 size_t count
, loff_t
*f_pos
)
449 int noblock
= fp
->f_flags
& O_NONBLOCK
;
451 struct usb_stk11xx
*dev
;
452 struct video_device
*vdev
;
455 void *image_buffer_addr
;
457 DECLARE_WAITQUEUE(wait
, current
);
459 vdev
= video_devdata(fp
);
460 dev
= video_get_drvdata(video_devdata(fp
));
462 STK_STREAM("Read vdev=0x%p, buf=0x%p, count=%zd\n", vdev
, buf
, count
);
470 mutex_lock(&dev
->modlock
);
472 if (dev
->image_read_pos
== 0) {
473 add_wait_queue(&dev
->wait_frame
, &wait
);
475 while (dev
->full_frames
== NULL
) {
476 if (dev
->error_status
) {
477 remove_wait_queue(&dev
->wait_frame
, &wait
);
478 set_current_state(TASK_RUNNING
);
479 mutex_unlock(&dev
->modlock
);
480 return -dev
->error_status
;
484 remove_wait_queue(&dev
->wait_frame
, &wait
);
485 set_current_state(TASK_RUNNING
);
486 mutex_unlock(&dev
->modlock
);
490 if (signal_pending(current
)) {
491 remove_wait_queue(&dev
->wait_frame
, &wait
);
492 set_current_state(TASK_RUNNING
);
493 mutex_unlock(&dev
->modlock
);
498 set_current_state(TASK_INTERRUPTIBLE
);
501 remove_wait_queue(&dev
->wait_frame
, &wait
);
502 set_current_state(TASK_RUNNING
);
504 if (stk11xx_handle_frame(dev
)) {
505 mutex_unlock(&dev
->modlock
);
510 bytes_to_read
= dev
->view_size
;
512 if (count
+ dev
->image_read_pos
> bytes_to_read
)
513 count
= bytes_to_read
- dev
->image_read_pos
;
515 image_buffer_addr
= dev
->image_data
;
516 image_buffer_addr
+= dev
->images
[dev
->fill_image
].offset
;
517 image_buffer_addr
+= dev
->image_read_pos
;
519 if (copy_to_user(buf
, image_buffer_addr
, count
)) {
520 mutex_unlock(&dev
->modlock
);
524 dev
->image_read_pos
+= count
;
526 if (dev
->image_read_pos
>= bytes_to_read
) {
527 dev
->image_read_pos
= 0;
528 stk11xx_next_image(dev
);
531 mutex_unlock(&dev
->modlock
);
538 * @param fp File pointer
541 * @returns 0 if all is OK
543 * @brief Polling function
545 static unsigned int v4l_stk11xx_poll(struct file
*fp
, poll_table
*wait
)
547 struct usb_stk11xx
*dev
;
548 struct video_device
*vdev
;
550 vdev
= video_devdata(fp
);
551 dev
= video_get_drvdata(video_devdata(fp
));
553 STK_STREAM("Poll\n");
561 poll_wait(fp
, &dev
->wait_frame
, wait
);
563 if (dev
->error_status
)
566 if (dev
->full_frames
!= NULL
)
567 return (POLLIN
| POLLRDNORM
);
574 * @param fp File pointer
575 * @param vma VMA structure
577 * @returns 0 if all is OK
581 * This function permits to map a memory space.
583 static int v4l_stk11xx_mmap(struct file
*fp
, struct vm_area_struct
*vma
)
592 struct usb_stk11xx
*dev
;
594 dev
= video_get_drvdata(video_devdata(fp
));
596 STK_STREAM("mmap\n");
598 start
= vma
->vm_start
;
599 size
= vma
->vm_end
- vma
->vm_start
;
601 // Find the buffer for this mapping...
602 for (i
=0; i
<dev
->nbuffers
; i
++) {
603 pos
= dev
->images
[i
].offset
;
605 if ((pos
>> PAGE_SHIFT
) == vma
->vm_pgoff
)
609 // If no buffer found !
610 if (i
== STK11XX_MAX_IMAGES
) {
611 STK_ERROR("mmap no buffer found !\n");
616 unsigned long total_size
;
618 total_size
= dev
->nbuffers
* dev
->len_per_image
;
620 if (size
!= dev
->len_per_image
&& size
!= total_size
) {
621 STK_ERROR("Wrong size (%lu) needed to be len_per_image=%d or total_size=%lu\n",
622 size
, dev
->len_per_image
, total_size
);
627 else if (size
> dev
->len_per_image
)
630 vma
->vm_flags
|= VM_IO
;
632 pos
= (unsigned long) dev
->image_data
;
635 page
= vmalloc_to_pfn((void *) pos
);
637 if (remap_pfn_range(vma
, start
, page
, PAGE_SIZE
, PAGE_SHARED
))
643 if (size
> PAGE_SIZE
)
654 * @param fp File pointer
656 * @param arg Arguments of the command
658 * @returns 0 if all is OK
660 * @brief Manage IOCTL
662 * This function permits to manage all the IOCTL from the application.
664 static long v4l_stk11xx_do_ioctl(struct file
*fp
,
665 unsigned int cmd
, void __user
*arg
)
667 struct usb_stk11xx
*dev
;
669 DECLARE_WAITQUEUE(wait
, current
);
671 dev
= video_get_drvdata(video_devdata(fp
));
673 #if (CONFIG_STK11XX_DEBUG == 1)
674 v4l_printk_ioctl(cmd
);
678 case VIDIOC_QUERYCAP
:
680 struct v4l2_capability
*cap
= arg
;
682 STK_DEBUG("VIDIOC_QUERYCAP\n");
684 memset(cap
, 0, sizeof(*cap
));
685 strlcpy(cap
->driver
, "stk11xx", sizeof(cap
->driver
));
687 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_READWRITE
| V4L2_CAP_STREAMING
;
688 cap
->version
= (__u32
) DRIVER_VERSION_NUM
, strlcpy(cap
->card
, dev
->vdev
->name
, sizeof(cap
->card
));
690 if (usb_make_path(dev
->udev
, cap
->bus_info
, sizeof(cap
->bus_info
)) < 0)
691 strlcpy(cap
->bus_info
, dev
->vdev
->name
, sizeof(cap
->bus_info
));
695 case VIDIOC_ENUMINPUT
:
697 struct v4l2_input
*i
= arg
;
699 STK_DEBUG("VIDIOC_ENUMINPUT %d\n", i
->index
);
701 if (dev
->webcam_model
!= SYNTEK_STK_0408
) {
704 strlcpy(i
->name
, "USB", sizeof(i
->name
));
712 strlcpy(i
->name
, "Input1", sizeof(i
->name
));
715 strlcpy(i
->name
, "Input2", sizeof(i
->name
));
718 strlcpy(i
->name
, "Input3", sizeof(i
->name
));
721 strlcpy(i
->name
, "Input4", sizeof(i
->name
));
726 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
732 STK_DEBUG("GET INPUT\n");
734 return dev
->vsettings
.input
;
740 struct v4l2_input
*i
= arg
;
742 STK_DEBUG("SET INPUT %d\n", i
->index
);
744 // TODO add input switching
749 dev
->vsettings
.input
= i
->index
+ 1;
751 dev_stk11xx_camera_settings(dev
);
755 case VIDIOC_QUERYCTRL
:
759 struct v4l2_queryctrl
*c
= arg
;
761 STK_DEBUG("VIDIOC_QUERYCTRL id = %d\n", c
->id
);
763 nbr
= sizeof(stk11xx_controls
)/sizeof(struct v4l2_queryctrl
);
765 for (i
=0; i
<nbr
; i
++) {
766 if (stk11xx_controls
[i
].id
== c
->id
) {
767 STK_DEBUG("VIDIOC_QUERYCTRL found\n");
768 memcpy(c
, &stk11xx_controls
[i
], sizeof(struct v4l2_queryctrl
));
771 case V4L2_CID_BRIGHTNESS
:
772 c
->default_value
= dev
->vsettings
.default_brightness
;
774 case V4L2_CID_WHITENESS
:
775 c
->default_value
= dev
->vsettings
.default_whiteness
;
777 case V4L2_CID_SATURATION
:
778 c
->default_value
= dev
->vsettings
.default_colour
;
780 case V4L2_CID_CONTRAST
:
781 c
->default_value
= dev
->vsettings
.default_contrast
;
784 c
->default_value
= dev
->vsettings
.default_hflip
;
787 c
->default_value
= dev
->vsettings
.default_vflip
;
801 struct v4l2_control
*c
= arg
;
803 STK_DEBUG("GET CTRL id=%d\n", c
->id
);
806 case V4L2_CID_BRIGHTNESS
:
807 c
->value
= dev
->vsettings
.brightness
;
810 case V4L2_CID_WHITENESS
:
811 c
->value
= dev
->vsettings
.whiteness
;
815 c
->value
= dev
->vsettings
.hue
;
818 case V4L2_CID_SATURATION
:
819 c
->value
= dev
->vsettings
.colour
;
822 case V4L2_CID_CONTRAST
:
823 c
->value
= dev
->vsettings
.contrast
;
827 c
->value
= dev
->vsettings
.hflip
;
831 c
->value
= dev
->vsettings
.vflip
;
843 struct v4l2_control
*c
= arg
;
845 STK_DEBUG("SET CTRL id=%d value=%d\n", c
->id
, c
->value
);
848 case V4L2_CID_BRIGHTNESS
:
849 dev
->vsettings
.brightness
= (0xff00 & c
->value
);
853 dev
->vsettings
.hue
= (0xff00 & c
->value
);
856 case V4L2_CID_SATURATION
:
857 dev
->vsettings
.colour
= (0xff00 & c
->value
);
860 case V4L2_CID_CONTRAST
:
861 dev
->vsettings
.contrast
= (0xff00 & c
->value
);
865 dev
->vsettings
.hflip
= c
->value
? 1: 0;
869 dev
->vsettings
.vflip
= c
->value
? 1: 0;
876 dev_stk11xx_camera_settings(dev
);
880 case VIDIOC_ENUM_FMT
:
883 struct v4l2_fmtdesc
*fmtd
= arg
;
885 STK_DEBUG("VIDIOC_ENUM_FMT %d\n", fmtd
->index
);
889 memset(fmtd
, 0, sizeof(*fmtd
));
891 fmtd
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
897 fmtd
->pixelformat
= V4L2_PIX_FMT_RGB24
;
899 strcpy(fmtd
->description
, "rgb24");
904 fmtd
->pixelformat
= V4L2_PIX_FMT_RGB32
;
906 strcpy(fmtd
->description
, "rgb32");
911 fmtd
->pixelformat
= V4L2_PIX_FMT_BGR24
;
913 strcpy(fmtd
->description
, "bgr24");
918 fmtd
->pixelformat
= V4L2_PIX_FMT_BGR32
;
920 strcpy(fmtd
->description
, "bgr32");
925 fmtd
->pixelformat
= V4L2_PIX_FMT_UYVY
;
927 strcpy(fmtd
->description
, "uyvy");
932 fmtd
->pixelformat
= V4L2_PIX_FMT_YUYV
;
934 strcpy(fmtd
->description
, "yuyv");
945 struct v4l2_format
*fmtd
= arg
;
946 struct v4l2_pix_format pix_format
;
948 STK_DEBUG("GET FMT %d\n", fmtd
->type
);
950 if (fmtd
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
953 pix_format
.width
= dev
->view
.x
;
954 pix_format
.height
= dev
->view
.y
;
955 pix_format
.field
= V4L2_FIELD_NONE
;
956 pix_format
.colorspace
= V4L2_COLORSPACE_SRGB
;
959 switch (dev
->vsettings
.palette
) {
960 case STK11XX_PALETTE_RGB24
:
961 pix_format
.pixelformat
= V4L2_PIX_FMT_RGB24
;
962 pix_format
.sizeimage
= pix_format
.width
* pix_format
.height
* 3;
963 pix_format
.bytesperline
= 3 * pix_format
.width
;
966 case STK11XX_PALETTE_RGB32
:
967 pix_format
.pixelformat
= V4L2_PIX_FMT_RGB32
;
968 pix_format
.sizeimage
= pix_format
.width
* pix_format
.height
* 4;
969 pix_format
.bytesperline
= 4 * pix_format
.width
;
972 case STK11XX_PALETTE_BGR24
:
973 pix_format
.pixelformat
= V4L2_PIX_FMT_BGR24
;
974 pix_format
.sizeimage
= pix_format
.width
* pix_format
.height
* 3;
975 pix_format
.bytesperline
= 3 * pix_format
.width
;
978 case STK11XX_PALETTE_BGR32
:
979 pix_format
.pixelformat
= V4L2_PIX_FMT_BGR32
;
980 pix_format
.sizeimage
= pix_format
.width
* pix_format
.height
* 4;
981 pix_format
.bytesperline
= 4 * pix_format
.width
;
984 case STK11XX_PALETTE_UYVY
:
985 pix_format
.pixelformat
= V4L2_PIX_FMT_UYVY
;
986 pix_format
.sizeimage
= pix_format
.width
* pix_format
.height
* 2;
987 pix_format
.bytesperline
= 2 * pix_format
.width
;
990 case STK11XX_PALETTE_YUYV
:
991 pix_format
.pixelformat
= V4L2_PIX_FMT_YUYV
;
992 pix_format
.sizeimage
= pix_format
.width
* pix_format
.height
* 2;
993 pix_format
.bytesperline
= 2 * pix_format
.width
;
997 pix_format
.pixelformat
= 0;
998 pix_format
.sizeimage
= 0;
999 pix_format
.bytesperline
= 0;
1002 memcpy(&(fmtd
->fmt
.pix
), &pix_format
, sizeof(pix_format
));
1006 case VIDIOC_TRY_FMT
:
1008 struct v4l2_format
*fmtd
= arg
;
1010 STK_DEBUG("TRY FMT %d\n", fmtd
->type
);
1012 if (fmtd
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1015 switch (dev
->webcam_type
) {
1017 if (fmtd
->fmt
.pix
.width
> stk11xx_image_sizes
[STK11XX_1280x1024
].x
)
1018 fmtd
->fmt
.pix
.width
= stk11xx_image_sizes
[STK11XX_1280x1024
].x
;
1019 else if (fmtd
->fmt
.pix
.width
< stk11xx_image_sizes
[0].x
)
1020 fmtd
->fmt
.pix
.width
= stk11xx_image_sizes
[0].x
;
1022 if (fmtd
->fmt
.pix
.height
> stk11xx_image_sizes
[STK11XX_1280x1024
].y
)
1023 fmtd
->fmt
.pix
.height
= stk11xx_image_sizes
[STK11XX_1280x1024
].y
;
1024 else if (fmtd
->fmt
.pix
.height
< stk11xx_image_sizes
[0].y
)
1025 fmtd
->fmt
.pix
.height
= stk11xx_image_sizes
[0].y
;
1029 if (fmtd
->fmt
.pix
.width
> stk11xx_image_sizes
[STK11XX_720x576
].x
)
1030 fmtd
->fmt
.pix
.width
= stk11xx_image_sizes
[STK11XX_720x576
].x
;
1031 else if (fmtd
->fmt
.pix
.width
< stk11xx_image_sizes
[0].x
)
1032 fmtd
->fmt
.pix
.width
= stk11xx_image_sizes
[0].x
;
1034 if (fmtd
->fmt
.pix
.height
> stk11xx_image_sizes
[STK11XX_720x576
].y
)
1035 fmtd
->fmt
.pix
.height
= stk11xx_image_sizes
[STK11XX_720x576
].y
;
1036 else if (fmtd
->fmt
.pix
.height
< stk11xx_image_sizes
[0].y
)
1037 fmtd
->fmt
.pix
.height
= stk11xx_image_sizes
[0].y
;
1041 if (fmtd
->fmt
.pix
.width
> stk11xx_image_sizes
[STK11XX_640x480
].x
)
1042 fmtd
->fmt
.pix
.width
= stk11xx_image_sizes
[STK11XX_640x480
].x
;
1043 else if (fmtd
->fmt
.pix
.width
< stk11xx_image_sizes
[0].x
)
1044 fmtd
->fmt
.pix
.width
= stk11xx_image_sizes
[0].x
;
1046 if (fmtd
->fmt
.pix
.height
> stk11xx_image_sizes
[STK11XX_640x480
].y
)
1047 fmtd
->fmt
.pix
.height
= stk11xx_image_sizes
[STK11XX_640x480
].y
;
1048 else if (fmtd
->fmt
.pix
.height
< stk11xx_image_sizes
[0].y
)
1049 fmtd
->fmt
.pix
.height
= stk11xx_image_sizes
[0].y
;
1054 fmtd
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
1055 fmtd
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SRGB
;
1056 fmtd
->fmt
.pix
.priv
= 0;
1058 switch (fmtd
->fmt
.pix
.pixelformat
) {
1059 case V4L2_PIX_FMT_RGB24
:
1060 case V4L2_PIX_FMT_BGR24
:
1061 dev
->vsettings
.depth
= 24;
1062 fmtd
->fmt
.pix
.sizeimage
= fmtd
->fmt
.pix
.width
* fmtd
->fmt
.pix
.height
* 3;
1063 fmtd
->fmt
.pix
.bytesperline
= 3 * fmtd
->fmt
.pix
.width
;
1066 case V4L2_PIX_FMT_RGB32
:
1067 case V4L2_PIX_FMT_BGR32
:
1068 dev
->vsettings
.depth
= 32;
1069 fmtd
->fmt
.pix
.sizeimage
= fmtd
->fmt
.pix
.width
* fmtd
->fmt
.pix
.height
* 4;
1070 fmtd
->fmt
.pix
.bytesperline
= 4 * fmtd
->fmt
.pix
.width
;
1073 case V4L2_PIX_FMT_UYVY
:
1074 case V4L2_PIX_FMT_YUYV
:
1075 dev
->vsettings
.depth
= 16;
1076 fmtd
->fmt
.pix
.sizeimage
= fmtd
->fmt
.pix
.width
* fmtd
->fmt
.pix
.height
* 2;
1077 fmtd
->fmt
.pix
.bytesperline
= 2 * fmtd
->fmt
.pix
.width
;
1088 struct v4l2_format
*fmtd
= arg
;
1090 STK_DEBUG("SET FMT %d : %d\n", fmtd
->type
, fmtd
->fmt
.pix
.pixelformat
);
1092 if (fmtd
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1095 fmtd
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
1096 fmtd
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SRGB
;
1097 fmtd
->fmt
.pix
.priv
= 0;
1099 switch (fmtd
->fmt
.pix
.pixelformat
) {
1100 case V4L2_PIX_FMT_RGB24
:
1101 dev
->vsettings
.depth
= 24;
1102 dev
->vsettings
.palette
= STK11XX_PALETTE_RGB24
;
1103 fmtd
->fmt
.pix
.sizeimage
= fmtd
->fmt
.pix
.width
* fmtd
->fmt
.pix
.height
* 3;
1104 fmtd
->fmt
.pix
.bytesperline
= 3 * fmtd
->fmt
.pix
.width
;
1107 case V4L2_PIX_FMT_RGB32
:
1108 dev
->vsettings
.depth
= 32;
1109 dev
->vsettings
.palette
= STK11XX_PALETTE_RGB32
;
1110 fmtd
->fmt
.pix
.sizeimage
= fmtd
->fmt
.pix
.width
* fmtd
->fmt
.pix
.height
* 4;
1111 fmtd
->fmt
.pix
.bytesperline
= 4 * fmtd
->fmt
.pix
.width
;
1114 case V4L2_PIX_FMT_BGR24
:
1115 dev
->vsettings
.depth
= 24;
1116 dev
->vsettings
.palette
= STK11XX_PALETTE_BGR24
;
1117 fmtd
->fmt
.pix
.sizeimage
= fmtd
->fmt
.pix
.width
* fmtd
->fmt
.pix
.height
* 3;
1118 fmtd
->fmt
.pix
.bytesperline
= 3 * fmtd
->fmt
.pix
.width
;
1121 case V4L2_PIX_FMT_BGR32
:
1122 dev
->vsettings
.depth
= 32;
1123 dev
->vsettings
.palette
= STK11XX_PALETTE_BGR32
;
1124 fmtd
->fmt
.pix
.sizeimage
= fmtd
->fmt
.pix
.width
* fmtd
->fmt
.pix
.height
* 4;
1125 fmtd
->fmt
.pix
.bytesperline
= 4 * fmtd
->fmt
.pix
.width
;
1128 case V4L2_PIX_FMT_UYVY
:
1129 dev
->vsettings
.depth
= 16;
1130 dev
->vsettings
.palette
= STK11XX_PALETTE_UYVY
;
1131 fmtd
->fmt
.pix
.sizeimage
= fmtd
->fmt
.pix
.width
* fmtd
->fmt
.pix
.height
* 2;
1132 fmtd
->fmt
.pix
.bytesperline
= 2 * fmtd
->fmt
.pix
.width
;
1135 case V4L2_PIX_FMT_YUYV
:
1136 dev
->vsettings
.depth
= 16;
1137 dev
->vsettings
.palette
= STK11XX_PALETTE_YUYV
;
1138 fmtd
->fmt
.pix
.sizeimage
= fmtd
->fmt
.pix
.width
* fmtd
->fmt
.pix
.height
* 2;
1139 fmtd
->fmt
.pix
.bytesperline
= 2 * fmtd
->fmt
.pix
.width
;
1146 STK_DEBUG("Set width=%d, height=%d\n", fmtd
->fmt
.pix
.width
, fmtd
->fmt
.pix
.height
);
1148 // Stop the video stream
1149 dev_stk11xx_stop_stream(dev
);
1151 // ISOC and URB cleanup
1152 usb_stk11xx_isoc_cleanup(dev
);
1154 // Switch off the camera
1155 dev_stk11xx_camera_off(dev
);
1157 dev_stk11xx_camera_asleep(dev
);
1159 // Select the new video mode
1160 if (v4l_stk11xx_select_video_mode(dev
, fmtd
->fmt
.pix
.width
, fmtd
->fmt
.pix
.height
)) {
1161 STK_ERROR("Select video mode failed !\n");
1165 // Clear the buffers
1166 stk11xx_clear_buffers(dev
);
1168 // Initialize the device
1169 dev_stk11xx_init_camera(dev
);
1170 dev_stk11xx_camera_on(dev
);
1171 dev_stk11xx_reconf_camera(dev
);
1173 // ISOC and URB init
1174 usb_stk11xx_isoc_init(dev
);
1176 // Re-start the stream
1177 dev_stk11xx_start_stream(dev
);
1180 dev_stk11xx_camera_settings(dev
);
1184 case VIDIOC_QUERYSTD
:
1186 STK_DEBUG("QUERY STD\n");
1193 v4l2_std_id
*std
= arg
;
1195 STK_DEBUG("GET STD\n");
1197 *std
= V4L2_STD_UNKNOWN
;
1203 v4l2_std_id
*std
= arg
;
1205 STK_DEBUG("SET STD\n");
1207 if (*std
!= V4L2_STD_UNKNOWN
)
1212 case VIDIOC_ENUMSTD
:
1214 struct v4l2_standard
*std
= arg
;
1216 STK_DEBUG("VIDIOC_ENUMSTD\n");
1218 if (std
->index
!= 0)
1221 std
->id
= V4L2_STD_UNKNOWN
;
1222 strncpy(std
->name
, "webcam", sizeof(std
->name
));
1227 case VIDIOC_REQBUFS
:
1230 struct v4l2_requestbuffers
*rb
= arg
;
1232 if (rb
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1235 if (rb
->memory
!= V4L2_MEMORY_MMAP
)
1238 nbuffers
= rb
->count
;
1242 else if (nbuffers
> dev
->nbuffers
)
1243 nbuffers
= dev
->nbuffers
;
1245 rb
->count
= dev
->nbuffers
;
1249 case VIDIOC_QUERYBUF
:
1252 struct v4l2_buffer
*buf
= arg
;
1254 STK_DEBUG("QUERY BUFFERS %d %d\n", buf
->index
, dev
->nbuffers
);
1256 if (buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1259 if (buf
->memory
!= V4L2_MEMORY_MMAP
)
1264 if (index
< 0 || index
>= dev
->nbuffers
)
1267 memset(buf
, 0, sizeof(struct v4l2_buffer
));
1269 buf
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1271 buf
->m
.offset
= index
* dev
->len_per_image
;
1272 buf
->bytesused
= dev
->view_size
;
1273 buf
->field
= V4L2_FIELD_NONE
;
1274 buf
->memory
= V4L2_MEMORY_MMAP
;
1275 buf
->length
= dev
->len_per_image
;
1281 struct v4l2_buffer
*buf
= arg
;
1283 STK_DEBUG("VIDIOC_QBUF\n");
1285 if (buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1288 if (buf
->memory
!= V4L2_MEMORY_MMAP
)
1291 if (buf
->index
< 0 || buf
->index
>= dev
->nbuffers
)
1294 buf
->flags
|= V4L2_BUF_FLAG_QUEUED
;
1295 buf
->flags
&= ~V4L2_BUF_FLAG_DONE
;
1302 struct v4l2_buffer
*buf
= arg
;
1304 STK_DEBUG("VIDIOC_DQBUF\n");
1306 if (buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1309 add_wait_queue(&dev
->wait_frame
, &wait
);
1311 while (dev
->full_frames
== NULL
) {
1312 if (dev
->error_status
) {
1313 remove_wait_queue(&dev
->wait_frame
, &wait
);
1314 set_current_state(TASK_RUNNING
);
1316 return -dev
->error_status
;
1319 if (signal_pending(current
)) {
1320 remove_wait_queue(&dev
->wait_frame
, &wait
);
1321 set_current_state(TASK_RUNNING
);
1323 return -ERESTARTSYS
;
1327 set_current_state(TASK_INTERRUPTIBLE
);
1330 remove_wait_queue(&dev
->wait_frame
, &wait
);
1331 set_current_state(TASK_RUNNING
);
1333 STK_DEBUG("VIDIOC_DQBUF : frame ready.\n");
1335 ret
= stk11xx_handle_frame(dev
);
1340 buf
->index
= dev
->fill_image
;
1341 buf
->bytesused
= dev
->view_size
;
1342 buf
->flags
= V4L2_BUF_FLAG_MAPPED
;
1343 buf
->field
= V4L2_FIELD_NONE
;
1344 do_gettimeofday(&buf
->timestamp
);
1346 buf
->memory
= V4L2_MEMORY_MMAP
;
1347 buf
->m
.offset
= dev
->fill_image
* dev
->len_per_image
;
1348 buf
->length
= dev
->len_per_image
; //buf->bytesused;
1350 stk11xx_next_image(dev
);
1354 case VIDIOC_STREAMON
:
1356 STK_DEBUG("VIDIOC_STREAMON\n");
1358 usb_stk11xx_isoc_init(dev
);
1362 case VIDIOC_STREAMOFF
:
1364 STK_DEBUG("VIDIOC_STREAMOFF\n");
1366 usb_stk11xx_isoc_cleanup(dev
);
1372 struct v4l2_streamparm
*sp
= arg
;
1374 STK_DEBUG("GET PARM %d\n", sp
->type
);
1376 if (sp
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1379 sp
->parm
.capture
.capability
= 0;
1380 sp
->parm
.capture
.capturemode
= 0;
1381 sp
->parm
.capture
.timeperframe
.numerator
= 1;
1382 sp
->parm
.capture
.timeperframe
.denominator
= 30;
1383 sp
->parm
.capture
.readbuffers
= 2;
1384 sp
->parm
.capture
.extendedmode
= 0;
1389 case VIDIOC_G_AUDIO
:
1390 STK_DEBUG("GET AUDIO\n");
1394 case VIDIOC_S_AUDIO
:
1395 STK_DEBUG("SET AUDIO\n");
1399 case VIDIOC_S_TUNER
:
1400 STK_DEBUG("SET TUNER\n");
1406 case VIDIOC_OVERLAY
:
1410 case VIDIOC_G_TUNER
:
1411 case VIDIOC_G_FREQUENCY
:
1412 case VIDIOC_S_FREQUENCY
:
1416 case VIDIOC_QUERYMENU
:
1420 case VIDIOC_CROPCAP:
1422 struct v4l2_cropcap cc;
1424 cc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1425 cc.pixelaspect.numerator = 1;
1426 cc.pixelaspect.denominator = 1;
1429 cc.bounds.width = 640;
1430 cc.bounds.height = 480;
1432 cc.defrect.left = 0;
1433 cc.defrect.width = 640;
1434 cc.defrect.height = 480;
1436 memcpy(arg, &cc, sizeof(cc));
1441 STK_DEBUG("IOCTL unknown !\n");
1442 return -ENOIOCTLCMD
;
1450 * @param fp File pointer
1451 * @param cmd Command
1452 * @param arg Arguements of the command
1454 * @returns 0 if all is OK
1456 * @brief Manage IOCTL
1458 * This function permits to manage all the IOCTL from the application.
1460 static long v4l_stk11xx_ioctl(struct file
*fp
,
1461 unsigned int cmd
, unsigned long arg
)
1464 struct usb_stk11xx
*dev
;
1465 struct video_device
*vdev
;
1467 vdev
= video_devdata(fp
);
1468 dev
= video_get_drvdata(video_devdata(fp
));
1470 STK_DEBUG("v4l_stk11xx_ioctl %02X\n", (unsigned char) cmd
);
1478 mutex_lock(&dev
->modlock
);
1480 err
= video_usercopy(fp
, cmd
, arg
, v4l_stk11xx_do_ioctl
);
1482 mutex_unlock(&dev
->modlock
);
1489 * @param dev Device structure
1491 * @returns 0 if all is OK
1493 * @brief Register the video device
1495 * This function permits to register the USB device to the video device.
1497 int v4l_stk11xx_register_video_device(struct usb_stk11xx
*dev
)
1501 strcpy(dev
->vdev
->name
, DRIVER_DESC
);
1503 dev
->vdev
->parent
= &dev
->interface
->dev
;
1504 dev
->vdev
->fops
= &v4l_stk11xx_fops
;
1505 dev
->vdev
->release
= video_device_release
;
1506 dev
->vdev
->minor
= -1;
1508 video_set_drvdata(dev
->vdev
, dev
);
1510 err
= video_register_device(dev
->vdev
, VFL_TYPE_GRABBER
, -1);
1513 STK_ERROR("Video register fail !\n");
1515 STK_INFO("Syntek USB2.0 Camera is now controlling video device /dev/video%d\n", dev
->vdev
->minor
);
1522 * @param dev Device structure
1524 * @returns 0 if all is OK
1526 * @brief Unregister the video device
1528 * This function permits to unregister the video device.
1530 int v4l_stk11xx_unregister_video_device(struct usb_stk11xx
*dev
)
1532 STK_INFO("Syntek USB2.0 Camera release resources video device /dev/video%d\n", dev
->vdev
->minor
);
1534 video_set_drvdata(dev
->vdev
, NULL
);
1535 video_unregister_device(dev
->vdev
);
1542 * @var v4l_stk11xx_fops
1544 * This variable contains some callback
1546 static struct v4l2_file_operations v4l_stk11xx_fops
= {
1547 .owner
= THIS_MODULE
,
1548 .open
= v4l_stk11xx_open
,
1549 .release
= v4l_stk11xx_release
,
1550 .read
= v4l_stk11xx_read
,
1551 .poll
= v4l_stk11xx_poll
,
1552 .mmap
= v4l_stk11xx_mmap
,
1553 .ioctl
= v4l_stk11xx_ioctl
,
1554 #if defined(CONFIG_COMPAT) && defined(v4l_compat_ioctl32)
1555 .compat_ioctl
= v4l_compat_ioctl32
,