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>
43 #include <linux/usb.h>
44 #include <media/v4l2-common.h>
49 static struct file_operations v4l_stk11xx_fops
;
53 * @var stk11xx_image_sizes
54 * List of all resolutions supported by the driver
56 const struct stk11xx_coord stk11xx_image_sizes
[STK11XX_NBR_SIZES
] = {
70 * @var stk11xx_controls
71 * List of all V4Lv2 controls supported by the driver
73 static struct v4l2_queryctrl stk11xx_controls
[] = {
75 .id
= V4L2_CID_BRIGHTNESS
,
76 .type
= V4L2_CTRL_TYPE_INTEGER
,
81 .default_value
= 0x7f00,
84 .id
= V4L2_CID_WHITENESS
,
85 .type
= V4L2_CTRL_TYPE_INTEGER
,
90 .default_value
= 0x7f00,
93 .id
= V4L2_CID_SATURATION
,
94 .type
= V4L2_CTRL_TYPE_INTEGER
,
99 .default_value
= 0x7f00,
102 .id
= V4L2_CID_CONTRAST
,
103 .type
= V4L2_CTRL_TYPE_INTEGER
,
108 .default_value
= 0x7f00,
115 * @param width Width of wished resolution
116 * @param height Height of wished resolution
118 * @returns 0 if all is OK
120 * @brief Select a video mode
122 * This function permits to check and select a video mode.
124 int v4l_stk11xx_select_video_mode(struct usb_stk11xx
*dev
, int width
, int height
)
130 // Check width and height
131 // Notice : this test is usefull for the Kopete application !
133 // Driver can't build an image smaller than the minimal resolution !
134 if ((width
< stk11xx_image_sizes
[0].x
)
135 || (height
< stk11xx_image_sizes
[0].y
)) {
136 width
= stk11xx_image_sizes
[0].x
;
137 height
= stk11xx_image_sizes
[0].y
;
140 // Driver can't build an image bigger than the maximal resolution !
141 switch (dev
->webcam_type
) {
143 if ((width
> stk11xx_image_sizes
[STK11XX_NBR_SIZES
-1].x
)
144 || (height
> stk11xx_image_sizes
[STK11XX_NBR_SIZES
-1].y
)) {
145 width
= stk11xx_image_sizes
[STK11XX_NBR_SIZES
-1].x
;
146 height
= stk11xx_image_sizes
[STK11XX_NBR_SIZES
-1].y
;
151 if ((width
> stk11xx_image_sizes
[STK11XX_NBR_SIZES
-3-1].x
)
152 || (height
> stk11xx_image_sizes
[STK11XX_NBR_SIZES
-3-1].y
)) {
153 width
= stk11xx_image_sizes
[STK11XX_NBR_SIZES
-3-1].x
;
154 height
= stk11xx_image_sizes
[STK11XX_NBR_SIZES
-3-1].y
;
163 // Seek the best resolution
164 switch (dev
->webcam_type
) {
166 for (i
=0, find
=0; i
<STK11XX_NBR_SIZES
; i
++) {
167 if (stk11xx_image_sizes
[i
].x
<= width
&& stk11xx_image_sizes
[i
].y
<= height
)
173 for (i
=0, find
=0; i
<STK11XX_NBR_SIZES
-3; i
++) {
174 if (stk11xx_image_sizes
[i
].x
<= width
&& stk11xx_image_sizes
[i
].y
<= height
)
183 // Save the new resolution
184 dev
->resolution
= find
;
186 STK_DEBUG("Set mode %d [%dx%d]\n", dev
->resolution
,
187 stk11xx_image_sizes
[dev
->resolution
].x
, stk11xx_image_sizes
[dev
->resolution
].y
);
191 dev
->view
.y
= height
;
194 // Calculate the frame size
195 switch (dev
->resolution
) {
198 case STK11XX_160x120
:
199 case STK11XX_213x160
:
200 case STK11XX_320x240
:
201 case STK11XX_640x480
:
202 dev
->image
.x
= stk11xx_image_sizes
[STK11XX_640x480
].x
;
203 dev
->image
.y
= stk11xx_image_sizes
[STK11XX_640x480
].y
;
204 dev
->frame_size
= dev
->image
.x
* dev
->image
.y
;
207 case STK11XX_800x600
:
208 case STK11XX_1024x768
:
209 case STK11XX_1280x1024
:
210 dev
->image
.x
= stk11xx_image_sizes
[STK11XX_1280x1024
].x
;
211 dev
->image
.y
= stk11xx_image_sizes
[STK11XX_1280x1024
].y
;
212 dev
->frame_size
= dev
->image
.x
* dev
->image
.y
;
217 // Calculate the image size
218 switch (dev
->vsettings
.palette
) {
219 case STK11XX_PALETTE_RGB24
:
220 case STK11XX_PALETTE_BGR24
:
221 dev
->image_size
= 3 * dev
->frame_size
;
224 case STK11XX_PALETTE_RGB32
:
225 case STK11XX_PALETTE_BGR32
:
226 dev
->image_size
= 4 * dev
->frame_size
;
229 case STK11XX_PALETTE_UYVY
:
230 dev
->image_size
= 2 * dev
->frame_size
;
233 case STK11XX_PALETTE_YUYV
:
234 dev
->image_size
= 2 * dev
->frame_size
;
243 * @param inode Pointer on an inode
244 * @param fp File pointer
246 * @returns 0 if all is OK
248 * @brief Open the video device
250 * This function permits to open a video device (/dev/videoX)
252 static int v4l_stk11xx_open(struct inode
*inode
, struct file
*fp
)
256 struct usb_stk11xx
*dev
;
257 struct video_device
*vdev
;
259 vdev
= video_devdata(fp
);
260 dev
= video_get_drvdata(video_devdata(fp
));
263 STK_ERROR("Device not initialized !!!\n");
268 STK_DEBUG("Device is busy, someone is using the device\n");
273 err
= stk11xx_allocate_buffers(dev
);
276 STK_ERROR("Failed to allocate buffer memory !\n");
280 // Reset buffers and parameters
281 stk11xx_reset_buffers(dev
);
284 dev
->error_status
= 0;
285 dev
->visoc_errors
= 0;
286 dev
->vframes_error
= 0;
287 dev
->vframes_dumped
= 0;
288 dev
->vsettings
.hue
= 0xffff;
289 dev
->vsettings
.depth
= 24;
290 dev
->vsettings
.palette
= STK11XX_PALETTE_BGR24
;
292 // Select the resolution by default
293 v4l_stk11xx_select_video_mode(dev
, 640, 480);
295 // Initialize the device
296 dev_stk11xx_init_camera(dev
);
297 dev_stk11xx_camera_on(dev
);
298 dev_stk11xx_reconf_camera(dev
);
301 err
= usb_stk11xx_isoc_init(dev
);
304 STK_ERROR("Failed to init ISOC stuff !\n");
305 usb_stk11xx_isoc_cleanup(dev
);
306 stk11xx_free_buffers(dev
);
310 // Start the video stream
311 dev_stk11xx_start_stream(dev
);
314 dev_stk11xx_camera_settings(dev
);
317 fp
->private_data
= vdev
;
324 * @param inode Pointer on inode
325 * @param fp File pointer
327 * @returns 0 if all is OK
329 * @brief Release an opened file.
331 * This function permits to release an opened file with the 'open' method.
333 static int v4l_stk11xx_release(struct inode
*inode
, struct file
*fp
)
335 struct usb_stk11xx
*dev
;
336 struct video_device
*vdev
;
338 vdev
= video_devdata(fp
);
339 dev
= video_get_drvdata(video_devdata(fp
));
342 STK_ERROR("v4l_release called on closed device\n");
344 // Stop the video stream
345 dev_stk11xx_stop_stream(dev
);
347 // ISOC and URB cleanup
348 usb_stk11xx_isoc_cleanup(dev
);
351 stk11xx_free_buffers(dev
);
353 // Switch off the camera
354 dev_stk11xx_camera_off(dev
);
356 dev_stk11xx_camera_asleep(dev
);
365 * @param fp File pointer
367 * @retval buf Buffer in user space
371 * @returns Count value
373 * @brief Read the video device
375 * This function is called by the application is reading the video device.
377 static ssize_t
v4l_stk11xx_read(struct file
*fp
, char __user
*buf
,
378 size_t count
, loff_t
*f_pos
)
380 int noblock
= fp
->f_flags
& O_NONBLOCK
;
382 struct usb_stk11xx
*dev
;
383 struct video_device
*vdev
;
386 void *image_buffer_addr
;
388 DECLARE_WAITQUEUE(wait
, current
);
390 vdev
= video_devdata(fp
);
391 dev
= video_get_drvdata(video_devdata(fp
));
393 STK_STREAM("Read vdev=0x%p, buf=0x%p, count=%zd\n", vdev
, buf
, count
);
401 if (dev
->image_read_pos
== 0) {
402 add_wait_queue(&dev
->wait_frame
, &wait
);
404 while (dev
->full_frames
== NULL
) {
405 if (dev
->error_status
) {
406 remove_wait_queue(&dev
->wait_frame
, &wait
);
407 set_current_state(TASK_RUNNING
);
408 return -dev
->error_status
;
412 remove_wait_queue(&dev
->wait_frame
, &wait
);
413 set_current_state(TASK_RUNNING
);
417 if (signal_pending(current
)) {
418 remove_wait_queue(&dev
->wait_frame
, &wait
);
419 set_current_state(TASK_RUNNING
);
424 set_current_state(TASK_INTERRUPTIBLE
);
427 remove_wait_queue(&dev
->wait_frame
, &wait
);
428 set_current_state(TASK_RUNNING
);
430 if (stk11xx_handle_frame(dev
))
434 bytes_to_read
= dev
->image_size
;
436 if (count
+ dev
->image_read_pos
> bytes_to_read
)
437 count
= bytes_to_read
- dev
->image_read_pos
;
439 image_buffer_addr
= dev
->image_data
;
440 image_buffer_addr
+= dev
->images
[dev
->fill_image
].offset
;
441 image_buffer_addr
+= dev
->image_read_pos
;
443 if (copy_to_user(buf
, image_buffer_addr
, count
))
446 dev
->image_read_pos
+= count
;
448 if (dev
->image_read_pos
>= bytes_to_read
) {
449 dev
->image_read_pos
= 0;
450 stk11xx_next_image(dev
);
458 * @param fp File pointer
461 * @returns 0 if all is OK
463 * @brief Polling function
465 static unsigned int v4l_stk11xx_poll(struct file
*fp
, poll_table
*wait
)
467 struct usb_stk11xx
*dev
;
468 struct video_device
*vdev
;
470 vdev
= video_devdata(fp
);
471 dev
= video_get_drvdata(video_devdata(fp
));
473 STK_STREAM("Poll\n");
481 poll_wait(fp
, &dev
->wait_frame
, wait
);
483 if (dev
->error_status
)
486 if (dev
->full_frames
!= NULL
)
487 return (POLLIN
| POLLRDNORM
);
494 * @param fp File pointer
495 * @param vma VMA structure
497 * @returns 0 if all is OK
501 * This function permits to map a memory space.
503 static int v4l_stk11xx_mmap(struct file
*fp
, struct vm_area_struct
*vma
)
512 struct usb_stk11xx
*dev
;
514 struct video_device
*vdev
;
516 vdev
= video_devdata(fp
);
517 dev
= video_get_drvdata(video_devdata(fp
));
519 STK_STREAM("mmap\n");
521 start
= vma
->vm_start
;
522 size
= vma
->vm_end
- vma
->vm_start
;
524 // Find the buffer for this mapping...
525 for (i
=0; i
<dev
->nbuffers
; i
++) {
526 pos
= dev
->images
[i
].offset
;
528 if ((pos
>> PAGE_SHIFT
) == vma
->vm_pgoff
)
532 // If no buffer found !
533 if (i
== STK11XX_MAX_IMAGES
) {
534 STK_ERROR("mmap no buffer found !\n");
539 unsigned long total_size
;
541 total_size
= dev
->nbuffers
* dev
->len_per_image
;
543 if (size
!= dev
->len_per_image
&& size
!= total_size
)
546 else if (size
> dev
->len_per_image
)
549 vma
->vm_flags
|= VM_IO
;
551 pos
= (unsigned long) dev
->image_data
;
554 page
= vmalloc_to_pfn((void *) pos
);
556 if (remap_pfn_range(vma
, start
, page
, PAGE_SIZE
, PAGE_SHARED
))
562 if (size
> PAGE_SIZE
)
573 * @param inode Inode pointer
574 * @param fp File pointer
576 * @param arg Arguments of the command
578 * @returns 0 if all is OK
580 * @brief Manage IOCTL
582 * This function permits to manage all the IOCTL from the application.
584 static int v4l_stk11xx_do_ioctl(struct inode
*inode
, struct file
*fp
,
585 unsigned int cmd
, void __user
*arg
)
587 struct usb_stk11xx
*dev
;
588 struct video_device
*vdev
;
590 DECLARE_WAITQUEUE(wait
, current
);
592 vdev
= video_devdata(fp
);
593 dev
= video_get_drvdata(video_devdata(fp
));
595 #if (CONFIG_STK11XX_DEBUG == 1)
596 v4l_printk_ioctl(cmd
);
604 struct video_capability
*cap
= arg
;
606 STK_DEBUG("VIDIOCGCAP\n");
608 memset(cap
, 0, sizeof(*cap
));
609 strlcpy(cap
->name
, "stk11xx", sizeof(cap
->name
));
610 cap
->type
= VID_TYPE_CAPTURE
;
614 switch (dev
->webcam_type
) {
616 cap
->minwidth
= stk11xx_image_sizes
[STK11XX_80x60
].x
;
617 cap
->minheight
= stk11xx_image_sizes
[STK11XX_80x60
].y
;
618 cap
->maxwidth
= stk11xx_image_sizes
[STK11XX_1280x1024
].x
;
619 cap
->maxheight
= stk11xx_image_sizes
[STK11XX_1280x1024
].y
;
623 cap
->minwidth
= stk11xx_image_sizes
[STK11XX_80x60
].x
;
624 cap
->minheight
= stk11xx_image_sizes
[STK11XX_80x60
].y
;
625 cap
->maxwidth
= stk11xx_image_sizes
[STK11XX_640x480
].x
;
626 cap
->maxheight
= stk11xx_image_sizes
[STK11XX_640x480
].y
;
634 struct video_channel
*v
= arg
;
636 STK_DEBUG("VIDIOCGCHAN\n");
643 v
->type
= VIDEO_TYPE_CAMERA
;
644 strcpy(v
->name
, "Webcam");
650 struct video_channel
*v
= arg
;
652 STK_DEBUG("VIDIOCSCHAN\n");
661 struct video_picture
*p
= arg
;
663 STK_DEBUG("VIDIOCGPICT\n");
665 p
->brightness
= dev
->vsettings
.brightness
;
666 p
->contrast
= dev
->vsettings
.contrast
;
667 p
->whiteness
= dev
->vsettings
.whiteness
;
668 p
->colour
= dev
->vsettings
.colour
;
669 p
->depth
= dev
->vsettings
.depth
;
670 p
->palette
= dev
->vsettings
.palette
;
671 p
->hue
= dev
->vsettings
.hue
;
673 switch (dev
->vsettings
.palette
) {
674 case STK11XX_PALETTE_BGR24
:
675 p
->palette
= VIDEO_PALETTE_RGB24
;
678 case STK11XX_PALETTE_BGR32
:
679 p
->palette
= VIDEO_PALETTE_RGB32
;
682 case STK11XX_PALETTE_UYVY
:
683 p
->palette
= VIDEO_PALETTE_UYVY
;
686 case STK11XX_PALETTE_YUYV
:
687 p
->palette
= VIDEO_PALETTE_YUYV
;
695 struct video_picture
*p
= arg
;
697 STK_DEBUG("VIDIOCSPICT\n");
699 dev
->vsettings
.brightness
= p
->brightness
;
700 dev
->vsettings
.contrast
= p
->contrast
;
701 dev
->vsettings
.whiteness
= p
->whiteness
;
702 dev
->vsettings
.colour
= p
->colour
;
703 dev
->vsettings
.hue
= p
->hue
;
705 if (p
->palette
&& p
->palette
!= dev
->vsettings
.palette
) {
706 switch (p
->palette
) {
707 case VIDEO_PALETTE_RGB24
:
708 dev
->vsettings
.depth
= 24;
709 dev
->vsettings
.palette
= STK11XX_PALETTE_BGR24
;
712 case VIDEO_PALETTE_RGB32
:
713 dev
->vsettings
.depth
= 32;
714 dev
->vsettings
.palette
= STK11XX_PALETTE_BGR32
;
717 case VIDEO_PALETTE_UYVY
:
718 dev
->vsettings
.depth
= 16;
719 dev
->vsettings
.palette
= STK11XX_PALETTE_UYVY
;
722 case VIDEO_PALETTE_YUYV
:
723 dev
->vsettings
.depth
= 16;
724 dev
->vsettings
.palette
= STK11XX_PALETTE_YUYV
;
732 dev_stk11xx_camera_settings(dev
);
734 STK_DEBUG("VIDIOCSPICT done\n");
740 struct video_window
*vw
= arg
;
742 STK_DEBUG("VIDIOCGWIN\n");
746 vw
->width
= dev
->view
.x
;
747 vw
->height
= dev
->view
.y
;
754 struct video_window
*vw
= arg
;
756 STK_DEBUG("VIDIOCSWIN\n");
758 STK_DEBUG("Set x=%d, y=%d\n", vw
->x
, vw
->y
);
759 STK_DEBUG("Set width=%d, height=%d\n", vw
->width
, vw
->height
);
760 STK_DEBUG("Flags = %X\n", vw
->flags
);
762 // Stop the video stream
763 dev_stk11xx_stop_stream(dev
);
765 // ISOC and URB cleanup
766 usb_stk11xx_isoc_cleanup(dev
);
768 // Switch off the camera
769 dev_stk11xx_camera_off(dev
);
771 dev_stk11xx_camera_asleep(dev
);
773 // Select the new video mode
774 if (v4l_stk11xx_select_video_mode(dev
, vw
->width
, vw
->height
)) {
775 STK_ERROR("Select video mode failed !\n");
780 stk11xx_clear_buffers(dev
);
782 // Initialize the device
783 dev_stk11xx_init_camera(dev
);
784 dev_stk11xx_camera_on(dev
);
785 dev_stk11xx_reconf_camera(dev
);
788 usb_stk11xx_isoc_init(dev
);
790 // Re-start the stream
791 dev_stk11xx_start_stream(dev
);
794 dev_stk11xx_camera_settings(dev
);
800 struct video_buffer
*vb
= arg
;
802 STK_DEBUG("VIDIOCGFBUF\n");
804 memset(vb
, 0, sizeof(*vb
));
811 struct video_mbuf
*vm
= arg
;
813 STK_DEBUG("VIDIOCGMBUF\n");
815 memset(vm
, 0, sizeof(*vm
));
817 vm
->size
= dev
->nbuffers
* dev
->len_per_image
;
818 vm
->frames
= dev
->nbuffers
;
820 for (i
=0; i
<dev
->nbuffers
; i
++)
821 vm
->offsets
[i
] = i
* dev
->len_per_image
;
827 struct video_mmap
*vm
= arg
;
829 STK_DEBUG("VIDIOCMCAPTURE format=%d\n", vm
->format
);
831 if (vm
->frame
< 0 || vm
->frame
>= dev
->nbuffers
)
835 switch (vm
->format
) {
836 case VIDEO_PALETTE_RGB32
:
839 case VIDEO_PALETTE_RGB24
:
842 case VIDEO_PALETTE_UYVY
:
845 case VIDEO_PALETTE_YUYV
:
853 if ((vm
->width
!= dev
->view
.x
) || (vm
->height
!= dev
->view
.y
))
856 if (dev
->image_used
[vm
->frame
])
859 dev
->image_used
[vm
->frame
] = 1;
861 STK_DEBUG("VIDIOCMCAPTURE done\n");
870 STK_DEBUG("VIDIOCSYNC\n");
872 if (*mbuf
< 0 || *mbuf
>= dev
->nbuffers
)
875 if (dev
->image_used
[*mbuf
] == 0)
878 add_wait_queue(&dev
->wait_frame
, &wait
);
880 while (dev
->full_frames
== NULL
) {
881 if (dev
->error_status
) {
882 remove_wait_queue(&dev
->wait_frame
, &wait
);
883 set_current_state(TASK_RUNNING
);
884 return -dev
->error_status
;
887 if (signal_pending(current
)) {
888 remove_wait_queue(&dev
->wait_frame
, &wait
);
889 set_current_state(TASK_RUNNING
);
894 set_current_state(TASK_INTERRUPTIBLE
);
897 remove_wait_queue(&dev
->wait_frame
, &wait
);
898 set_current_state(TASK_RUNNING
);
900 STK_DEBUG("VIDIOCSYNC: frame ready\n");
902 dev
->fill_image
= *mbuf
;
904 ret
= stk11xx_handle_frame(dev
);
907 STK_ERROR("VIDIOCSYNC error !\n");
909 dev
->image_used
[*mbuf
] = 0;
914 STK_DEBUG("VIDIOCGAUDIO\n");
919 STK_DEBUG("VIDIOCSAUDIO\n");
925 struct video_unit
*vu
= arg
;
927 vu
->video
= dev
->vdev
->minor
& 0x3f;
938 case VIDIOC_QUERYCAP
:
940 struct v4l2_capability
*cap
= arg
;
942 STK_DEBUG("VIDIOC_QUERYCAP\n");
944 memset(cap
, 0, sizeof(*cap
));
945 strlcpy(cap
->driver
, "stk11xx", sizeof(cap
->driver
));
947 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_READWRITE
| V4L2_CAP_STREAMING
;
948 cap
->version
= (__u32
) DRIVER_VERSION_NUM
, strlcpy(cap
->card
, dev
->vdev
->name
, sizeof(cap
->card
));
950 if (usb_make_path(dev
->udev
, cap
->bus_info
, sizeof(cap
->bus_info
)) < 0)
951 strlcpy(cap
->bus_info
, dev
->vdev
->name
, sizeof(cap
->bus_info
));
955 case VIDIOC_ENUMINPUT
:
957 struct v4l2_input
*i
= arg
;
959 STK_DEBUG("VIDIOC_ENUMINPUT %d\n", i
->index
);
964 strlcpy(i
->name
, "USB", sizeof(i
->name
));
965 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
971 struct v4l2_input
*i
= arg
;
973 STK_DEBUG("GET INPUT %d\n", i
->index
);
982 struct v4l2_input
*i
= arg
;
984 STK_DEBUG("SET INPUT %d\n", i
->index
);
991 case VIDIOC_QUERYCTRL
:
995 struct v4l2_queryctrl
*c
= arg
;
997 STK_DEBUG("VIDIOC_QUERYCTRL id = %d\n", c
->id
);
999 nbr
= sizeof(stk11xx_controls
)/sizeof(struct v4l2_queryctrl
);
1001 for (i
=0; i
<nbr
; i
++) {
1002 if (stk11xx_controls
[i
].id
== c
->id
) {
1003 STK_DEBUG("VIDIOC_QUERYCTRL found\n");
1004 memcpy(c
, &stk11xx_controls
[i
], sizeof(struct v4l2_queryctrl
));
1016 struct v4l2_control
*c
= arg
;
1018 STK_DEBUG("GET CTRL id=%d\n", c
->id
);
1021 case V4L2_CID_BRIGHTNESS
:
1022 c
->value
= dev
->vsettings
.brightness
;
1025 case V4L2_CID_WHITENESS
:
1026 c
->value
= dev
->vsettings
.whiteness
;
1029 case V4L2_CID_SATURATION
:
1030 c
->value
= dev
->vsettings
.colour
;
1033 case V4L2_CID_CONTRAST
:
1034 c
->value
= dev
->vsettings
.contrast
;
1045 struct v4l2_control
*c
= arg
;
1047 STK_DEBUG("SET CTRL id=%d value=%d\n", c
->id
, c
->value
);
1050 case V4L2_CID_BRIGHTNESS
:
1051 dev
->vsettings
.brightness
= (0xff00 & c
->value
);
1054 case V4L2_CID_WHITENESS
:
1055 dev
->vsettings
.whiteness
= (0xff00 & c
->value
);
1058 case V4L2_CID_SATURATION
:
1059 dev
->vsettings
.colour
= (0xff00 & c
->value
);
1062 case V4L2_CID_CONTRAST
:
1063 dev
->vsettings
.contrast
= (0xff00 & c
->value
);
1070 dev_stk11xx_camera_settings(dev
);
1074 case VIDIOC_ENUM_FMT
:
1077 struct v4l2_fmtdesc
*fmtd
= arg
;
1079 STK_DEBUG("VIDIOC_ENUM_FMT %d\n", fmtd
->index
);
1081 if (fmtd
->index
!= 0)
1084 index
= fmtd
->index
;
1086 memset(fmtd
, 0, sizeof(*fmtd
));
1088 fmtd
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1089 fmtd
->index
= index
;
1094 fmtd
->pixelformat
= V4L2_PIX_FMT_RGB24
;
1096 strcpy(fmtd
->description
, "rgb24");
1101 fmtd
->pixelformat
= V4L2_PIX_FMT_RGB32
;
1103 strcpy(fmtd
->description
, "rgb32");
1108 fmtd
->pixelformat
= V4L2_PIX_FMT_BGR24
;
1110 strcpy(fmtd
->description
, "bgr24");
1115 fmtd
->pixelformat
= V4L2_PIX_FMT_BGR32
;
1117 strcpy(fmtd
->description
, "bgr32");
1122 fmtd
->pixelformat
= V4L2_PIX_FMT_UYVY
;
1124 strcpy(fmtd
->description
, "uyvy");
1129 fmtd
->pixelformat
= V4L2_PIX_FMT_YUYV
;
1131 strcpy(fmtd
->description
, "yuyv");
1142 struct v4l2_format
*fmtd
= arg
;
1143 struct v4l2_pix_format pix_format
;
1145 STK_DEBUG("GET FMT %d\n", fmtd
->type
);
1147 if (fmtd
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1150 pix_format
.width
= dev
->view
.x
;
1151 pix_format
.height
= dev
->view
.y
;
1152 pix_format
.field
= V4L2_FIELD_NONE
;
1153 pix_format
.colorspace
= V4L2_COLORSPACE_SRGB
;
1154 pix_format
.priv
= 0;
1156 switch (dev
->vsettings
.palette
) {
1157 case STK11XX_PALETTE_RGB24
:
1158 pix_format
.pixelformat
= V4L2_PIX_FMT_RGB24
;
1159 pix_format
.sizeimage
= pix_format
.width
* pix_format
.height
* 3;
1160 pix_format
.bytesperline
= 3 * pix_format
.width
;
1163 case STK11XX_PALETTE_RGB32
:
1164 pix_format
.pixelformat
= V4L2_PIX_FMT_RGB32
;
1165 pix_format
.sizeimage
= pix_format
.width
* pix_format
.height
* 4;
1166 pix_format
.bytesperline
= 4 * pix_format
.width
;
1169 case STK11XX_PALETTE_BGR24
:
1170 pix_format
.pixelformat
= V4L2_PIX_FMT_BGR24
;
1171 pix_format
.sizeimage
= pix_format
.width
* pix_format
.height
* 3;
1172 pix_format
.bytesperline
= 3 * pix_format
.width
;
1175 case STK11XX_PALETTE_BGR32
:
1176 pix_format
.pixelformat
= V4L2_PIX_FMT_BGR32
;
1177 pix_format
.sizeimage
= pix_format
.width
* pix_format
.height
* 4;
1178 pix_format
.bytesperline
= 4 * pix_format
.width
;
1181 case STK11XX_PALETTE_UYVY
:
1182 pix_format
.pixelformat
= V4L2_PIX_FMT_UYVY
;
1183 pix_format
.sizeimage
= pix_format
.width
* pix_format
.height
* 2;
1184 pix_format
.bytesperline
= 2 * pix_format
.width
;
1187 case STK11XX_PALETTE_YUYV
:
1188 pix_format
.pixelformat
= V4L2_PIX_FMT_YUYV
;
1189 pix_format
.sizeimage
= pix_format
.width
* pix_format
.height
* 2;
1190 pix_format
.bytesperline
= 2 * pix_format
.width
;
1194 memcpy(&(fmtd
->fmt
.pix
), &pix_format
, sizeof(pix_format
));
1198 case VIDIOC_TRY_FMT
:
1200 struct v4l2_format
*fmtd
= arg
;
1202 STK_DEBUG("TRY FMT %d\n", fmtd
->type
);
1204 if (fmtd
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1207 switch (fmtd
->fmt
.pix
.pixelformat
) {
1208 case V4L2_PIX_FMT_RGB24
:
1209 case V4L2_PIX_FMT_BGR24
:
1210 dev
->vsettings
.depth
= 24;
1213 case V4L2_PIX_FMT_RGB32
:
1214 case V4L2_PIX_FMT_BGR32
:
1215 dev
->vsettings
.depth
= 32;
1218 case V4L2_PIX_FMT_UYVY
:
1219 case V4L2_PIX_FMT_YUYV
:
1220 dev
->vsettings
.depth
= 16;
1227 switch (dev
->webcam_type
) {
1229 if (fmtd
->fmt
.pix
.width
> stk11xx_image_sizes
[STK11XX_NBR_SIZES
-1].x
)
1230 fmtd
->fmt
.pix
.width
= stk11xx_image_sizes
[STK11XX_NBR_SIZES
-1].x
;
1231 else if (fmtd
->fmt
.pix
.width
< stk11xx_image_sizes
[0].x
)
1232 fmtd
->fmt
.pix
.width
= stk11xx_image_sizes
[0].x
;
1234 if (fmtd
->fmt
.pix
.height
> stk11xx_image_sizes
[STK11XX_NBR_SIZES
-1].y
)
1235 fmtd
->fmt
.pix
.height
= stk11xx_image_sizes
[STK11XX_NBR_SIZES
-1].y
;
1236 else if (fmtd
->fmt
.pix
.height
< stk11xx_image_sizes
[0].y
)
1237 fmtd
->fmt
.pix
.height
= stk11xx_image_sizes
[0].y
;
1241 if (fmtd
->fmt
.pix
.width
> stk11xx_image_sizes
[STK11XX_NBR_SIZES
-1-3].x
)
1242 fmtd
->fmt
.pix
.width
= stk11xx_image_sizes
[STK11XX_NBR_SIZES
-1-3].x
;
1243 else if (fmtd
->fmt
.pix
.width
< stk11xx_image_sizes
[0].x
)
1244 fmtd
->fmt
.pix
.width
= stk11xx_image_sizes
[0].x
;
1246 if (fmtd
->fmt
.pix
.height
> stk11xx_image_sizes
[STK11XX_NBR_SIZES
-1-3].y
)
1247 fmtd
->fmt
.pix
.height
= stk11xx_image_sizes
[STK11XX_NBR_SIZES
-1-3].y
;
1248 else if (fmtd
->fmt
.pix
.height
< stk11xx_image_sizes
[0].y
)
1249 fmtd
->fmt
.pix
.height
= stk11xx_image_sizes
[0].y
;
1258 struct v4l2_format
*fmtd
= arg
;
1260 STK_DEBUG("SET FMT %d : %d\n", fmtd
->type
, fmtd
->fmt
.pix
.pixelformat
);
1262 if (fmtd
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1265 switch (fmtd
->fmt
.pix
.pixelformat
) {
1266 case V4L2_PIX_FMT_RGB24
:
1267 dev
->vsettings
.depth
= 24;
1268 dev
->vsettings
.palette
= STK11XX_PALETTE_RGB24
;
1271 case V4L2_PIX_FMT_RGB32
:
1272 dev
->vsettings
.depth
= 32;
1273 dev
->vsettings
.palette
= STK11XX_PALETTE_RGB32
;
1276 case V4L2_PIX_FMT_BGR24
:
1277 dev
->vsettings
.depth
= 24;
1278 dev
->vsettings
.palette
= STK11XX_PALETTE_BGR24
;
1281 case V4L2_PIX_FMT_BGR32
:
1282 dev
->vsettings
.depth
= 32;
1283 dev
->vsettings
.palette
= STK11XX_PALETTE_BGR32
;
1286 case V4L2_PIX_FMT_UYVY
:
1287 dev
->vsettings
.depth
= 16;
1288 dev
->vsettings
.palette
= STK11XX_PALETTE_UYVY
;
1291 case V4L2_PIX_FMT_YUYV
:
1292 dev
->vsettings
.depth
= 16;
1293 dev
->vsettings
.palette
= STK11XX_PALETTE_YUYV
;
1300 STK_DEBUG("Set width=%d, height=%d\n", fmtd
->fmt
.pix
.width
, fmtd
->fmt
.pix
.height
);
1302 // Stop the video stream
1303 dev_stk11xx_stop_stream(dev
);
1305 // ISOC and URB cleanup
1306 usb_stk11xx_isoc_cleanup(dev
);
1308 // Switch off the camera
1309 dev_stk11xx_camera_off(dev
);
1311 dev_stk11xx_camera_asleep(dev
);
1313 // Select the new video mode
1314 if (v4l_stk11xx_select_video_mode(dev
, fmtd
->fmt
.pix
.width
, fmtd
->fmt
.pix
.height
)) {
1315 STK_ERROR("Select video mode failed !\n");
1319 // Clear the buffers
1320 stk11xx_clear_buffers(dev
);
1322 // Initialize the device
1323 dev_stk11xx_init_camera(dev
);
1324 dev_stk11xx_camera_on(dev
);
1325 dev_stk11xx_reconf_camera(dev
);
1327 // ISOC and URB init
1328 usb_stk11xx_isoc_init(dev
);
1330 // Re-start the stream
1331 dev_stk11xx_start_stream(dev
);
1334 dev_stk11xx_camera_settings(dev
);
1338 case VIDIOC_QUERYSTD
:
1340 STK_DEBUG("QUERY STD\n");
1347 v4l2_std_id
*std
= arg
;
1349 STK_DEBUG("GET STD\n");
1351 *std
= V4L2_STD_UNKNOWN
;
1357 v4l2_std_id
*std
= arg
;
1359 STK_DEBUG("SET STD\n");
1361 if (*std
!= V4L2_STD_UNKNOWN
)
1366 case VIDIOC_ENUMSTD
:
1368 struct v4l2_standard
*std
= arg
;
1370 STK_DEBUG("VIDIOC_ENUMSTD\n");
1372 if (std
->index
!= 0)
1375 std
->id
= V4L2_STD_UNKNOWN
;
1376 strncpy(std
->name
, "webcam", sizeof(std
->name
));
1381 case VIDIOC_REQBUFS
:
1384 struct v4l2_requestbuffers
*rb
= arg
;
1386 if (rb
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1389 if (rb
->memory
!= V4L2_MEMORY_MMAP
)
1392 nbuffers
= rb
->count
;
1396 else if (nbuffers
> dev
->nbuffers
)
1397 nbuffers
= dev
->nbuffers
;
1399 rb
->count
= dev
->nbuffers
;
1403 case VIDIOC_QUERYBUF
:
1406 struct v4l2_buffer
*buf
= arg
;
1408 STK_DEBUG("QUERY BUFFERS %d %d\n", buf
->index
, dev
->nbuffers
);
1410 if (buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1413 if (buf
->memory
!= V4L2_MEMORY_MMAP
)
1418 if (index
< 0 || index
>= dev
->nbuffers
)
1421 memset(buf
, 0, sizeof(struct v4l2_buffer
));
1423 buf
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1425 buf
->m
.offset
= index
* dev
->len_per_image
;
1426 buf
->bytesused
= dev
->image_size
;
1427 buf
->field
= V4L2_FIELD_NONE
;
1428 buf
->memory
= V4L2_MEMORY_MMAP
;
1429 buf
->length
= dev
->len_per_image
;
1435 struct v4l2_buffer
*buf
= arg
;
1437 STK_DEBUG("VIDIOC_QBUF\n");
1439 if (buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1442 if (buf
->memory
!= V4L2_MEMORY_MMAP
)
1445 if (buf
->index
< 0 || buf
->index
>= dev
->nbuffers
)
1448 buf
->flags
|= V4L2_BUF_FLAG_QUEUED
;
1449 buf
->flags
&= ~V4L2_BUF_FLAG_DONE
;
1456 struct v4l2_buffer
*buf
= arg
;
1458 STK_DEBUG("VIDIOC_DQBUF\n");
1460 if (buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1463 add_wait_queue(&dev
->wait_frame
, &wait
);
1465 while (dev
->full_frames
== NULL
) {
1466 if (dev
->error_status
) {
1467 remove_wait_queue(&dev
->wait_frame
, &wait
);
1468 set_current_state(TASK_RUNNING
);
1470 return -dev
->error_status
;
1473 if (signal_pending(current
)) {
1474 remove_wait_queue(&dev
->wait_frame
, &wait
);
1475 set_current_state(TASK_RUNNING
);
1477 return -ERESTARTSYS
;
1481 set_current_state(TASK_INTERRUPTIBLE
);
1484 remove_wait_queue(&dev
->wait_frame
, &wait
);
1485 set_current_state(TASK_RUNNING
);
1487 STK_DEBUG("VIDIOC_DQBUF : frame ready.\n");
1489 ret
= stk11xx_handle_frame(dev
);
1494 buf
->index
= dev
->fill_image
;
1495 buf
->bytesused
= dev
->image_size
;
1496 buf
->flags
= V4L2_BUF_FLAG_MAPPED
;
1497 buf
->field
= V4L2_FIELD_NONE
;
1498 do_gettimeofday(&buf
->timestamp
);
1500 buf
->memory
= V4L2_MEMORY_MMAP
;
1501 buf
->m
.offset
= dev
->fill_image
* dev
->len_per_image
;
1502 buf
->length
= buf
->bytesused
;
1504 stk11xx_next_image(dev
);
1508 case VIDIOC_STREAMON
:
1510 STK_DEBUG("VIDIOC_STREAMON\n");
1512 usb_stk11xx_isoc_init(dev
);
1516 case VIDIOC_STREAMOFF
:
1518 STK_DEBUG("VIDIOC_STREAMOFF\n");
1520 usb_stk11xx_isoc_cleanup(dev
);
1526 struct v4l2_streamparm
*sp
= arg
;
1528 STK_DEBUG("GET PARM %d\n", sp
->type
);
1530 if (sp
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1533 sp
->parm
.capture
.capability
= 0;
1534 sp
->parm
.capture
.capturemode
= 0;
1535 sp
->parm
.capture
.timeperframe
.numerator
= 1;
1536 sp
->parm
.capture
.timeperframe
.denominator
= 30;
1537 sp
->parm
.capture
.readbuffers
= 2;
1538 sp
->parm
.capture
.extendedmode
= 0;
1543 case VIDIOC_G_AUDIO
:
1544 STK_DEBUG("GET AUDIO\n");
1548 case VIDIOC_S_AUDIO
:
1549 STK_DEBUG("SET AUDIO\n");
1553 case VIDIOC_S_TUNER
:
1554 STK_DEBUG("SET TUNER\n");
1560 case VIDIOC_OVERLAY
:
1564 case VIDIOC_G_TUNER
:
1565 case VIDIOC_G_FREQUENCY
:
1566 case VIDIOC_S_FREQUENCY
:
1570 case VIDIOC_QUERYMENU
:
1574 case VIDIOC_CROPCAP:
1576 struct v4l2_cropcap cc;
1578 cc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1579 cc.pixelaspect.numerator = 1;
1580 cc.pixelaspect.denominator = 1;
1583 cc.bounds.width = 640;
1584 cc.bounds.height = 480;
1586 cc.defrect.left = 0;
1587 cc.defrect.width = 640;
1588 cc.defrect.height = 480;
1590 memcpy(arg, &cc, sizeof(cc));
1595 STK_DEBUG("IOCTL unknown !\n");
1596 return -ENOIOCTLCMD
;
1604 * @param inode Inode pointer
1605 * @param fp File pointer
1606 * @param cmd Command
1607 * @param arg Arguements of the command
1609 * @returns 0 if all is OK
1611 * @brief Manage IOCTL
1613 * This function permits to manage all the IOCTL from the application.
1615 static int v4l_stk11xx_ioctl(struct inode
*inode
, struct file
*fp
,
1616 unsigned int cmd
, unsigned long arg
)
1619 struct usb_stk11xx
*dev
;
1620 struct video_device
*vdev
;
1622 vdev
= video_devdata(fp
);
1623 dev
= video_get_drvdata(video_devdata(fp
));
1625 STK_DEBUG("v4l_stk11xx_ioctl %02X\n", (unsigned char) cmd
);
1633 // v4l_printk_ioctl(cmd);
1635 err
= v4l_stk11xx_do_ioctl(inode
, fp
, cmd
, (void __user
*) arg
);
1642 * @param dev Device structure
1644 * @returns 0 if all is OK
1646 * @brief Register the video device
1648 * This function permits to register the USB device to the video device.
1650 int v4l_stk11xx_register_video_device(struct usb_stk11xx
*dev
)
1654 strcpy(dev
->vdev
->name
, DRIVER_DESC
);
1656 dev
->vdev
->owner
= THIS_MODULE
;
1657 dev
->vdev
->type
= VID_TYPE_CAPTURE
;
1658 dev
->vdev
->hardware
= VID_HARDWARE_STK11XX
;
1659 dev
->vdev
->fops
= &v4l_stk11xx_fops
;
1660 dev
->vdev
->release
= video_device_release
;
1661 dev
->vdev
->minor
= -1;
1663 video_set_drvdata(dev
->vdev
, dev
);
1665 err
= video_register_device(dev
->vdev
, VFL_TYPE_GRABBER
, -1);
1668 STK_ERROR("Video register fail !\n");
1670 STK_INFO("Syntek USB2.0 Camera is now controlling video device /dev/video%d\n", dev
->vdev
->minor
);
1677 * @param dev Device structure
1679 * @returns 0 if all is OK
1681 * @brief Unregister the video device
1683 * This function permits to unregister the video device.
1685 int v4l_stk11xx_unregister_video_device(struct usb_stk11xx
*dev
)
1687 STK_INFO("Syntek USB2.0 Camera release resources video device /dev/video%d\n", dev
->vdev
->minor
);
1689 video_set_drvdata(dev
->vdev
, NULL
);
1690 video_unregister_device(dev
->vdev
);
1697 * @var v4l_stk11xx_fops
1699 * This variable contains some callback
1701 static struct file_operations v4l_stk11xx_fops
= {
1702 .owner
= THIS_MODULE
,
1703 .open
= v4l_stk11xx_open
,
1704 .release
= v4l_stk11xx_release
,
1705 .read
= v4l_stk11xx_read
,
1706 .poll
= v4l_stk11xx_poll
,
1707 .mmap
= v4l_stk11xx_mmap
,
1708 .ioctl
= v4l_stk11xx_ioctl
,