don't manage version it's boring.
[syntekdriver.git] / driver / stk11xx-v4l.c
blob68851511c4b0bd667a8445f1cefffadd01153a96
1 /**
2 * @file stk11xx-v4l.c
3 * @author Nicolas VIVIEN
4 * @date 2006-10-23
5 * @version v2.2.x
7 * @brief Driver for Syntek USB video camera
9 * @note Copyright (C) Nicolas VIVIEN
11 * @par Licences
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
16 * any later version.
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
27 * @par SubVersion
28 * $Date$
29 * $Revision$
30 * $Author$
31 * $HeadURL$
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>
42 #include <linux/mm.h>
45 #include <linux/usb.h>
46 #include <media/v4l2-common.h>
47 #include <media/v4l2-ioctl.h>
49 #include "stk11xx.h"
52 static struct v4l2_file_operations v4l_stk11xx_fops;
55 /**
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] = {
60 { 80, 60 },
61 { 128, 96 },
62 { 160, 120 },
63 { 213, 160 },
64 { 320, 240 },
65 { 640, 480 },
66 { 720, 576 },
67 { 800, 600 },
68 { 1024, 768 },
69 { 1280, 1024 }
73 /**
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,
82 .name = "Brightness",
83 .minimum = 0,
84 .maximum = 0xff00,
85 .step = 1,
86 .default_value = 0x7f00,
89 .id = V4L2_CID_WHITENESS,
90 .type = V4L2_CTRL_TYPE_INTEGER,
91 .name = "Whiteness",
92 .minimum = 0,
93 .maximum = 0xff00,
94 .step = 1,
95 .default_value = 0x7f00,
98 .id = V4L2_CID_SATURATION,
99 .type = V4L2_CTRL_TYPE_INTEGER,
100 .name = "Saturation",
101 .minimum = 0,
102 .maximum = 0xff00,
103 .step = 1,
104 .default_value = 0x7f00,
107 .id = V4L2_CID_CONTRAST,
108 .type = V4L2_CTRL_TYPE_INTEGER,
109 .name = "Contrast",
110 .minimum = 0,
111 .maximum = 0xff00,
112 .step = 1,
113 .default_value = 0x7f00,
116 .id = V4L2_CID_HUE,
117 .type = V4L2_CTRL_TYPE_INTEGER,
118 .name = "Hue",
119 .minimum = 0,
120 .maximum = 0xff00,
121 .step = 1,
122 .default_value = 0x7f00,
125 .id = V4L2_CID_HFLIP,
126 .type = V4L2_CTRL_TYPE_BOOLEAN,
127 .name = "Flip Horizontally",
128 .minimum = 0,
129 .maximum = 1,
130 .step = 1,
131 .default_value = 0, // will be actually set later
134 .id = V4L2_CID_VFLIP,
135 .type = V4L2_CTRL_TYPE_BOOLEAN,
136 .name = "Flip Vertically",
137 .minimum = 0,
138 .maximum = 1,
139 .step = 1,
140 .default_value = 0, // will be actually set later
145 /**
146 * @param dev
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)
158 int i;
159 int find;
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) {
174 case STK11XX_SXGA:
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;
180 break;
182 case STK11XX_VGA:
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;
188 break;
190 case STK11XX_PAL:
191 if (! (((width == 720) && (height==576))
192 || ((width == 720) && (height==480))
193 || ((width == 640) && (height==480)))) {
194 width = 640;
195 height = 480;
197 break;
199 default:
200 return -1;
204 // Seek the best resolution
205 switch (dev->webcam_type) {
206 case STK11XX_SXGA:
207 for (i=0, find=0; i<=STK11XX_1280x1024; i++) {
208 if (stk11xx_image_sizes[i].x <= width && stk11xx_image_sizes[i].y <= height)
209 find = i;
211 break;
213 case STK11XX_VGA:
214 for (i=0, find=0; i<=STK11XX_640x480; i++) {
215 if (stk11xx_image_sizes[i].x <= width && stk11xx_image_sizes[i].y <= height)
216 find = i;
218 break;
220 case STK11XX_PAL:
221 for (i=0, find=0; i<=STK11XX_720x576; i++) {
222 if (stk11xx_image_sizes[i].x <= width && stk11xx_image_sizes[i].y <= height)
223 find = i;
225 break;
227 default:
228 return -1;
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);
237 // Save the new size
238 dev->view.x = width;
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;
249 else {
250 switch (dev->resolution) {
251 case STK11XX_80x60:
252 case STK11XX_128x96:
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;
260 break;
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;
269 break;
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;
280 break;
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;
286 break;
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;
292 break;
295 return 0;
299 /**
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)
310 int err;
312 struct usb_stk11xx *dev;
313 struct video_device *vdev;
315 vdev = video_devdata(fp);
316 dev = video_get_drvdata(video_devdata(fp));
318 if (dev == NULL) {
319 STK_ERROR("Device not initialized !!!\n");
320 BUG();
323 mutex_lock(&dev->modlock);
325 if (dev->vopen) {
326 STK_DEBUG("Device is busy, someone is using the device\n");
327 mutex_unlock(&dev->modlock);
328 return -EBUSY;
331 // Allocate memory
332 err = stk11xx_allocate_buffers(dev);
334 if (err < 0) {
335 STK_ERROR("Failed to allocate buffer memory !\n");
336 mutex_unlock(&dev->modlock);
337 return err;
340 // Reset buffers and parameters
341 stk11xx_reset_buffers(dev);
343 // Settings
344 dev->vsync = 0;
345 dev->v1st_cap = 5;
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);
363 // Init Isoc and URB
364 err = usb_stk11xx_isoc_init(dev);
366 if (err) {
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);
371 return err;
374 // Start the video stream
375 dev_stk11xx_start_stream(dev);
377 // Video settings
378 dev_stk11xx_camera_settings(dev);
380 // Register interface on power management
381 // usb_autopm_get_interface(dev->interface);
383 dev->vopen++;
384 fp->private_data = vdev;
386 mutex_unlock(&dev->modlock);
388 return 0;
392 /**
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));
407 if (dev->vopen == 0)
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);
416 // Free memory
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);
427 dev->vopen--;
429 return 0;
433 /**
434 * @param fp File pointer
436 * @retval buf Buffer in user space
437 * @retval count
438 * @retval f_pos
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;
454 int bytes_to_read;
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);
464 if (dev == NULL)
465 return -EFAULT;
467 if (vdev == NULL)
468 return -EFAULT;
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 ;
483 if (noblock) {
484 remove_wait_queue(&dev->wait_frame, &wait);
485 set_current_state(TASK_RUNNING);
486 mutex_unlock(&dev->modlock);
487 return -EWOULDBLOCK;
490 if (signal_pending(current)) {
491 remove_wait_queue(&dev->wait_frame, &wait);
492 set_current_state(TASK_RUNNING);
493 mutex_unlock(&dev->modlock);
494 return -ERESTARTSYS;
497 schedule();
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);
506 return -EFAULT;
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);
521 return -EFAULT;
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);
533 return count;
537 /**
538 * @param fp File pointer
539 * @param wait
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");
555 if (vdev == NULL)
556 return -EFAULT;
558 if (dev == NULL)
559 return -EFAULT;
561 poll_wait(fp, &dev->wait_frame, wait);
563 if (dev->error_status)
564 return POLLERR;
566 if (dev->full_frames != NULL)
567 return (POLLIN | POLLRDNORM);
569 return 0;
573 /**
574 * @param fp File pointer
575 * @param vma VMA structure
577 * @returns 0 if all is OK
579 * @brief Memory map
581 * This function permits to map a memory space.
583 static int v4l_stk11xx_mmap(struct file *fp, struct vm_area_struct *vma)
585 unsigned int i;
587 unsigned long size;
588 unsigned long start;
589 unsigned long pos;
590 unsigned long page;
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)
606 break;
609 // If no buffer found !
610 if (i == STK11XX_MAX_IMAGES) {
611 STK_ERROR("mmap no buffer found !\n");
612 return -EINVAL;
615 if (i == 0) {
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);
624 return -EINVAL;
627 else if (size > dev->len_per_image)
628 return -EINVAL;
630 vma->vm_flags |= VM_IO;
632 pos = (unsigned long) dev->image_data;
634 while (size > 0) {
635 page = vmalloc_to_pfn((void *) pos);
637 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
638 return -EAGAIN;
640 start += PAGE_SIZE;
641 pos += PAGE_SIZE;
643 if (size > PAGE_SIZE)
644 size -= PAGE_SIZE;
645 else
646 size = 0;
649 return 0;
653 /**
654 * @param fp File pointer
655 * @param cmd Command
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);
675 #endif
677 switch (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));
693 break;
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) {
702 if (i->index)
703 return -EINVAL;
704 strlcpy(i->name, "USB", sizeof(i->name));
706 else {
707 if (i->index > 3)
708 return -EINVAL;
710 switch (i->index) {
711 case 0:
712 strlcpy(i->name, "Input1", sizeof(i->name));
713 break;
714 case 1:
715 strlcpy(i->name, "Input2", sizeof(i->name));
716 break;
717 case 2:
718 strlcpy(i->name, "Input3", sizeof(i->name));
719 break;
720 case 3:
721 strlcpy(i->name, "Input4", sizeof(i->name));
722 break;
726 i->type = V4L2_INPUT_TYPE_CAMERA;
728 break;
730 case VIDIOC_G_INPUT:
732 STK_DEBUG("GET INPUT\n");
734 return dev->vsettings.input;
736 break;
738 case VIDIOC_S_INPUT:
740 struct v4l2_input *i = arg;
742 STK_DEBUG("SET INPUT %d\n", i->index);
744 // TODO add input switching
746 if (i->index > 3)
747 return -EINVAL;
749 dev->vsettings.input = i->index + 1;
751 dev_stk11xx_camera_settings(dev);
753 break;
755 case VIDIOC_QUERYCTRL:
757 int i;
758 int nbr;
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));
769 switch(c->id)
771 case V4L2_CID_BRIGHTNESS:
772 c->default_value = dev->vsettings.default_brightness;
773 break;
774 case V4L2_CID_WHITENESS:
775 c->default_value = dev->vsettings.default_whiteness;
776 break;
777 case V4L2_CID_SATURATION:
778 c->default_value = dev->vsettings.default_colour;
779 break;
780 case V4L2_CID_CONTRAST:
781 c->default_value = dev->vsettings.default_contrast;
782 break;
783 case V4L2_CID_HFLIP:
784 c->default_value = dev->vsettings.default_hflip;
785 break;
786 case V4L2_CID_VFLIP:
787 c->default_value = dev->vsettings.default_vflip;
788 break;
790 break;
794 if (i >= nbr)
795 return -EINVAL;
797 break;
799 case VIDIOC_G_CTRL:
801 struct v4l2_control *c = arg;
803 STK_DEBUG("GET CTRL id=%d\n", c->id);
805 switch (c->id) {
806 case V4L2_CID_BRIGHTNESS:
807 c->value = dev->vsettings.brightness;
808 break;
810 case V4L2_CID_WHITENESS:
811 c->value = dev->vsettings.whiteness;
812 break;
814 case V4L2_CID_HUE:
815 c->value = dev->vsettings.hue;
816 break;
818 case V4L2_CID_SATURATION:
819 c->value = dev->vsettings.colour;
820 break;
822 case V4L2_CID_CONTRAST:
823 c->value = dev->vsettings.contrast;
824 break;
826 case V4L2_CID_HFLIP:
827 c->value = dev->vsettings.hflip;
828 break;
830 case V4L2_CID_VFLIP:
831 c->value = dev->vsettings.vflip;
832 break;
835 default:
836 return -EINVAL;
839 break;
841 case VIDIOC_S_CTRL:
843 struct v4l2_control *c = arg;
845 STK_DEBUG("SET CTRL id=%d value=%d\n", c->id, c->value);
847 switch (c->id) {
848 case V4L2_CID_BRIGHTNESS:
849 dev->vsettings.brightness = (0xff00 & c->value);
850 break;
852 case V4L2_CID_HUE:
853 dev->vsettings.hue = (0xff00 & c->value);
854 break;
856 case V4L2_CID_SATURATION:
857 dev->vsettings.colour = (0xff00 & c->value);
858 break;
860 case V4L2_CID_CONTRAST:
861 dev->vsettings.contrast = (0xff00 & c->value);
862 break;
864 case V4L2_CID_HFLIP:
865 dev->vsettings.hflip = c->value ? 1: 0;
866 break;
868 case V4L2_CID_VFLIP:
869 dev->vsettings.vflip = c->value ? 1: 0;
870 break;
872 default:
873 return -EINVAL;
876 dev_stk11xx_camera_settings(dev);
878 break;
880 case VIDIOC_ENUM_FMT:
882 int index;
883 struct v4l2_fmtdesc *fmtd = arg;
885 STK_DEBUG("VIDIOC_ENUM_FMT %d\n", fmtd->index);
887 index = fmtd->index;
889 memset(fmtd, 0, sizeof(*fmtd));
891 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
892 fmtd->index = index;
894 switch (index) {
895 case 0:
896 fmtd->flags = 0;
897 fmtd->pixelformat = V4L2_PIX_FMT_RGB24;
899 strcpy(fmtd->description, "rgb24");
900 break;
902 case 1:
903 fmtd->flags = 0;
904 fmtd->pixelformat = V4L2_PIX_FMT_RGB32;
906 strcpy(fmtd->description, "rgb32");
907 break;
909 case 2:
910 fmtd->flags = 0;
911 fmtd->pixelformat = V4L2_PIX_FMT_BGR24;
913 strcpy(fmtd->description, "bgr24");
914 break;
916 case 3:
917 fmtd->flags = 0;
918 fmtd->pixelformat = V4L2_PIX_FMT_BGR32;
920 strcpy(fmtd->description, "bgr32");
921 break;
923 case 4:
924 fmtd->flags = 0;
925 fmtd->pixelformat = V4L2_PIX_FMT_UYVY;
927 strcpy(fmtd->description, "uyvy");
928 break;
930 case 5:
931 fmtd->flags = 0;
932 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
934 strcpy(fmtd->description, "yuyv");
935 break;
937 default:
938 return -EINVAL;
941 break;
943 case VIDIOC_G_FMT:
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)
951 return -EINVAL;
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;
957 pix_format.priv = 0;
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;
964 break;
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;
970 break;
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;
976 break;
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;
982 break;
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;
988 break;
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;
994 break;
996 default:
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));
1004 break;
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)
1013 return -EINVAL;
1015 switch (dev->webcam_type) {
1016 case STK11XX_SXGA:
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;
1026 break;
1028 case STK11XX_PAL:
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;
1038 break;
1040 case STK11XX_VGA:
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;
1050 break;
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;
1064 break;
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;
1071 break;
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;
1078 break;
1080 default:
1081 return -EINVAL;
1084 break;
1086 case VIDIOC_S_FMT:
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)
1093 return -EINVAL;
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;
1105 break;
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;
1112 break;
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;
1119 break;
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;
1126 break;
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;
1133 break;
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;
1140 break;
1142 default:
1143 return -EINVAL;
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");
1162 return -EAGAIN;
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);
1179 // Video settings
1180 dev_stk11xx_camera_settings(dev);
1182 break;
1184 case VIDIOC_QUERYSTD:
1186 STK_DEBUG("QUERY STD\n");
1187 return -EINVAL;
1189 break;
1191 case VIDIOC_G_STD:
1193 v4l2_std_id *std = arg;
1195 STK_DEBUG("GET STD\n");
1197 *std = V4L2_STD_UNKNOWN;
1199 break;
1201 case VIDIOC_S_STD:
1203 v4l2_std_id *std = arg;
1205 STK_DEBUG("SET STD\n");
1207 if (*std != V4L2_STD_UNKNOWN)
1208 return -EINVAL;
1210 break;
1212 case VIDIOC_ENUMSTD:
1214 struct v4l2_standard *std = arg;
1216 STK_DEBUG("VIDIOC_ENUMSTD\n");
1218 if (std->index != 0)
1219 return -EINVAL;
1221 std->id = V4L2_STD_UNKNOWN;
1222 strncpy(std->name, "webcam", sizeof(std->name));
1224 break;
1227 case VIDIOC_REQBUFS:
1229 int nbuffers;
1230 struct v4l2_requestbuffers *rb = arg;
1232 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1233 return -EINVAL;
1235 if (rb->memory != V4L2_MEMORY_MMAP)
1236 return -EINVAL;
1238 nbuffers = rb->count;
1240 if (nbuffers < 2)
1241 nbuffers = 2;
1242 else if (nbuffers > dev->nbuffers)
1243 nbuffers = dev->nbuffers;
1245 rb->count = dev->nbuffers;
1247 break;
1249 case VIDIOC_QUERYBUF:
1251 int index;
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)
1257 return -EINVAL;
1259 if (buf->memory != V4L2_MEMORY_MMAP)
1260 return -EINVAL;
1262 index = buf->index;
1264 if (index < 0 || index >= dev->nbuffers)
1265 return -EINVAL;
1267 memset(buf, 0, sizeof(struct v4l2_buffer));
1269 buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1270 buf->index = index;
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;
1277 break;
1279 case VIDIOC_QBUF:
1281 struct v4l2_buffer *buf = arg;
1283 STK_DEBUG("VIDIOC_QBUF\n");
1285 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1286 return -EINVAL;
1288 if (buf->memory != V4L2_MEMORY_MMAP)
1289 return -EINVAL;
1291 if (buf->index < 0 || buf->index >= dev->nbuffers)
1292 return -EINVAL;
1294 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1295 buf->flags &= ~V4L2_BUF_FLAG_DONE;
1297 break;
1299 case VIDIOC_DQBUF:
1301 int ret;
1302 struct v4l2_buffer *buf = arg;
1304 STK_DEBUG("VIDIOC_DQBUF\n");
1306 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1307 return -EINVAL;
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;
1326 schedule();
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);
1337 if (ret)
1338 return -EFAULT;
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);
1345 buf->sequence = 0;
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);
1352 break;
1354 case VIDIOC_STREAMON:
1356 STK_DEBUG("VIDIOC_STREAMON\n");
1358 usb_stk11xx_isoc_init(dev);
1360 break;
1362 case VIDIOC_STREAMOFF:
1364 STK_DEBUG("VIDIOC_STREAMOFF\n");
1366 usb_stk11xx_isoc_cleanup(dev);
1368 break;
1370 case VIDIOC_G_PARM:
1372 struct v4l2_streamparm *sp = arg;
1374 STK_DEBUG("GET PARM %d\n", sp->type);
1376 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1377 return -EINVAL;
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;
1386 break;
1389 case VIDIOC_G_AUDIO:
1390 STK_DEBUG("GET AUDIO\n");
1391 return -EINVAL;
1392 break;
1394 case VIDIOC_S_AUDIO:
1395 STK_DEBUG("SET AUDIO\n");
1396 return -EINVAL;
1397 break;
1399 case VIDIOC_S_TUNER:
1400 STK_DEBUG("SET TUNER\n");
1401 return -EINVAL;
1402 break;
1404 case VIDIOC_G_FBUF:
1405 case VIDIOC_S_FBUF:
1406 case VIDIOC_OVERLAY:
1407 return -EINVAL;
1408 break;
1410 case VIDIOC_G_TUNER:
1411 case VIDIOC_G_FREQUENCY:
1412 case VIDIOC_S_FREQUENCY:
1413 return -EINVAL;
1414 break;
1416 case VIDIOC_QUERYMENU:
1417 return -EINVAL;
1418 break;
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;
1427 cc.bounds.top = 0;
1428 cc.bounds.left = 0;
1429 cc.bounds.width = 640;
1430 cc.bounds.height = 480;
1431 cc.defrect.top = 0;
1432 cc.defrect.left = 0;
1433 cc.defrect.width = 640;
1434 cc.defrect.height = 480;
1436 memcpy(arg, &cc, sizeof(cc));
1438 break;
1440 default:
1441 STK_DEBUG("IOCTL unknown !\n");
1442 return -ENOIOCTLCMD;
1445 return 0;
1449 /**
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)
1463 long err;
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);
1472 if (dev == NULL)
1473 return -EFAULT;
1475 if (vdev == NULL)
1476 return -EFAULT;
1478 mutex_lock(&dev->modlock);
1480 err = video_usercopy(fp, cmd, arg, v4l_stk11xx_do_ioctl);
1482 mutex_unlock(&dev->modlock);
1484 return err;
1488 /**
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)
1499 int err;
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);
1512 if (err)
1513 STK_ERROR("Video register fail !\n");
1514 else
1515 STK_INFO("Syntek USB2.0 Camera is now controlling video device /dev/video%d\n", dev->vdev->minor);
1517 return err;
1521 /**
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);
1537 return 0;
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,
1556 #endif