Release 1.2.1
[syntekdriver.git] / stk11xx-usb.c
blob325d492cd8fb512ebfe7a19f717166522469658c
1 /**
2 * @file stk11xx-usb.c
3 * @author Nicolas VIVIEN
4 * @date 2006-10-23
5 * @version v1.2.0
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>
42 #include <linux/usb.h>
43 #include <media/v4l2-common.h>
45 #include "stk11xx.h"
48 /**
49 * @var default_fps
50 * Number of frame per second by default
52 static int default_fps = -1;
54 /**
55 * @var default_hflip
56 * Enable / Disable horizontal flip image
58 static int default_hflip = -1;
60 /**
61 * @var default_vflip
62 * Enable / Disable vertical flip image
64 static int default_vflip = -1;
66 /**
67 * @var default_brightness
68 * Set brightness
70 static int default_brightness = -1;
72 /**
73 * @var default_whiteness
74 * Set whiteness
76 static int default_whiteness = -1;
78 /**
79 * @var default_contrast
80 * Set contrast
82 static int default_contrast = -1;
84 /**
85 * @var default_colour
86 * Set colour
88 static int default_colour = -1;
91 /**
92 * @var stk11xx_table
93 * Define all the hotplug supported devices by this driver
95 static struct usb_device_id stk11xx_table[] = {
96 { USB_DEVICE(USB_SYNTEK1_VENDOR_ID, USB_STK_A311_PRODUCT_ID) },
97 { USB_DEVICE(USB_SYNTEK1_VENDOR_ID, USB_STK_A821_PRODUCT_ID) },
98 { USB_DEVICE(USB_SYNTEK1_VENDOR_ID, USB_STK_6A31_PRODUCT_ID) },
99 { USB_DEVICE(USB_SYNTEK1_VENDOR_ID, USB_STK_6A33_PRODUCT_ID) },
101 { USB_DEVICE(USB_SYNTEK2_VENDOR_ID, USB_STK_0501_PRODUCT_ID) },
106 MODULE_DEVICE_TABLE(usb, stk11xx_table); /**< Define the supported devices */
110 /**
111 * @param dev Device structure
113 * @returns 0 if all is OK
115 * @brief Initilize an isochronous pipe.
117 * This function permits to initialize an URB transfert (or isochronous pipe).
119 int usb_stk11xx_isoc_init(struct usb_stk11xx *dev)
121 int i, j;
122 int ret = 0;
123 struct urb *urb;
124 struct usb_device *udev;
126 if (dev == NULL)
127 return -EFAULT;
129 if (dev->isoc_init_ok)
130 return 0;
132 udev = dev->udev;
134 STK_DEBUG("usb_stk11xx_isoc_init()\n");
136 // Allocate URB structure
137 for (i=0; i<MAX_ISO_BUFS; i++) {
138 urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
140 if (urb == NULL) {
141 STK_ERROR("Failed to allocate URB %d\n", i);
142 ret = -ENOMEM;
143 break;
146 dev->isobuf[i].urb = urb;
149 if (ret) {
150 while (i >= 0) {
151 if (dev->isobuf[i].urb != NULL)
152 usb_free_urb(dev->isobuf[i].urb);
154 dev->isobuf[i].urb = NULL;
155 i--;
158 return ret;
161 // Init URB structure
162 for (i=0; i<MAX_ISO_BUFS; i++) {
163 urb = dev->isobuf[i].urb;
165 urb->interval = 1;
166 urb->dev = udev;
167 urb->pipe = usb_rcvisocpipe(udev, dev->isoc_in_endpointAddr);
168 urb->transfer_flags = URB_ISO_ASAP;
169 urb->transfer_buffer = dev->isobuf[i].data;
170 urb->transfer_buffer_length = ISO_BUFFER_SIZE;
171 urb->complete = usb_stk11xx_isoc_handler;
172 urb->context = dev;
173 urb->start_frame = 0;
174 urb->number_of_packets = ISO_FRAMES_PER_DESC;
176 for (j=0; j<ISO_FRAMES_PER_DESC; j++) {
177 urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
178 urb->iso_frame_desc[j].length = ISO_MAX_FRAME_SIZE; //dev->isoc_in_size;
182 STK_DEBUG("dev->isoc_in_size = %X\n", dev->isoc_in_size);
183 STK_DEBUG("dev->isoc_in_endpointAddr = %X\n", dev->isoc_in_endpointAddr);
185 // Link
186 for (i=0; i<MAX_ISO_BUFS; i++) {
187 ret = usb_submit_urb(dev->isobuf[i].urb, GFP_KERNEL);
189 if (ret)
190 STK_ERROR("isoc_init() submit_urb %d failed with error %d\n", i, ret);
191 else
192 STK_DEBUG("URB 0x%p submitted.\n", dev->isobuf[i].urb);
194 switch (ret) {
195 case -ENOMEM:
196 STK_ERROR("ENOMEM\n");
197 break;
198 case -ENODEV:
199 STK_ERROR("ENODEV\n");
200 break;
201 case -ENXIO:
202 STK_ERROR("ENXIO\n");
203 break;
204 case -EINVAL:
205 STK_ERROR("EINVAL\n");
206 break;
207 case -EAGAIN:
208 STK_ERROR("EAGAIN\n");
209 break;
210 case -EFBIG:
211 STK_ERROR("EFBIG\n");
212 break;
213 case -EPIPE:
214 STK_ERROR("EPIPE\n");
215 break;
216 case -EMSGSIZE:
217 STK_ERROR("EMSGSIZE\n");
218 break;
222 // All is done
223 dev->isoc_init_ok = 1;
225 return 0;
229 /**
230 * @param urb URB structure
232 * @brief ISOC handler
234 * This function is called as an URB transfert is complete (Isochronous pipe).
235 * So, the traitement is done in interrupt time, so it has be fast, not crash,
236 * ans not stall. Neat.
238 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
239 void usb_stk11xx_isoc_handler(struct urb *urb, struct pt_regs *regs)
240 #else
241 void usb_stk11xx_isoc_handler(struct urb *urb)
242 #endif
244 int i;
245 int ret;
246 int skip;
248 int awake = 0;
249 int framestatus;
250 int framelen;
252 unsigned char *fill = NULL;
253 unsigned char *iso_buf = NULL;
255 struct usb_stk11xx *dev;
256 struct stk11xx_frame_buf *framebuf;
258 STK_STREAM("Isoc handler\n");
260 dev = (struct usb_stk11xx *) urb->context;
262 if (dev == NULL) {
263 STK_ERROR("isoc_handler called with NULL device !\n");
264 return;
267 if (urb->status == -ENOENT || urb->status == -ECONNRESET) {
268 STK_DEBUG("URB unlinked synchronuously !\n");
269 return;
272 if (urb->status != -EINPROGRESS && urb->status != 0) {
273 const char *errmsg;
275 errmsg = "Unknown";
277 switch(urb->status) {
278 case -ENOSR:
279 errmsg = "Buffer error (overrun)";
280 break;
282 case -EPIPE:
283 errmsg = "Stalled (device not responding)";
284 break;
286 case -EOVERFLOW:
287 errmsg = "Babble (bad cable?)";
288 break;
290 case -EPROTO:
291 errmsg = "Bit-stuff error (bad cable?)";
292 break;
294 case -EILSEQ:
295 errmsg = "CRC/Timeout (could be anything)";
296 break;
298 case -ETIMEDOUT:
299 errmsg = "NAK (device does not respond)";
300 break;
303 STK_ERROR("isoc_handler() called with status %d [%s].\n", urb->status, errmsg);
305 dev->visoc_errors++;
307 wake_up_interruptible(&dev->wait_frame);
309 urb->dev = dev->udev;
310 ret = usb_submit_urb(urb, GFP_ATOMIC);
312 if (ret != 0) {
313 STK_ERROR("Error (%d) re-submitting urb in stk11xx_isoc_handler.\n", ret);
316 return;
319 framebuf = dev->fill_frame;
321 if (framebuf == NULL) {
322 STK_ERROR("isoc_handler without valid fill frame !\n");
324 wake_up_interruptible(&dev->wait_frame);
326 urb->dev = dev->udev;
327 ret = usb_submit_urb(urb, GFP_ATOMIC);
329 if (ret != 0) {
330 STK_ERROR("Error (%d) re-submitting urb in stk11xx_isoc_handler.\n", ret);
333 return;
335 else {
336 fill = framebuf->data + framebuf->filled;
339 // Reset ISOC error counter
340 dev->visoc_errors = 0;
342 // Compact data
343 for (i=0; i<urb->number_of_packets; i++) {
344 framestatus = urb->iso_frame_desc[i].status;
345 framelen = urb->iso_frame_desc[i].actual_length;
346 iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
348 if (framestatus == 0) {
349 skip = 4;
351 if (framelen > 4) {
352 // we found something informational from there
353 // the isoc frames have to type of headers
354 // type1: 00 xx 00 00 or 20 xx 00 00
355 // type2: 80 xx 00 00 00 00 00 00 or a0 xx 00 00 00 00 00 00
356 // xx is a sequencer which has never been seen over 0x3f
358 // imho data written down looks like bayer, i see similarities after
359 // every 640 bytes
360 if (*iso_buf & 0x80) {
361 skip = 8;
364 // Our buffer is full !!!
365 if (framelen - skip + framebuf->filled > dev->frame_size) {
366 STK_ERROR("Frame buffer overflow !\n");
367 framebuf->errors++;
369 // All is OK
370 else {
371 memcpy(fill, iso_buf + skip, framelen - skip);
372 fill += framelen - skip;
375 // New size of our buffer
376 framebuf->filled += framelen - skip;
379 STK_STREAM("URB : Length = %d - Skip = %d - Buffer size = %d\n",
380 framelen, skip, framebuf->filled);
382 // Data is always follow by a frame with a length '4'
383 if (framelen == 4) {
384 if (framebuf->filled > 0) {
385 // Our buffer has enough data ?
386 if (framebuf->filled < dev->frame_size)
387 framebuf->errors++;
389 // If there are errors, we skip a frame...
390 if (framebuf->errors == 0) {
391 if (stk11xx_next_frame(dev))
392 dev->vframes_dumped++;
394 else
395 dev->vframes_error++;
397 awake = 1;
398 framebuf = dev->fill_frame;
399 framebuf->filled = 0;
400 framebuf->errors = 0;
401 fill = framebuf->data;
405 else {
406 STK_ERROR("Iso frame %d of USB has error %d\n", i, framestatus);
410 if (awake == 1)
411 wake_up_interruptible(&dev->wait_frame);
413 urb->dev = dev->udev;
415 ret = usb_submit_urb(urb, GFP_ATOMIC);
417 if (ret != 0) {
418 STK_ERROR("Error (%d) re-submitting urb in stk11xx_isoc_handler.\n", ret);
423 /**
424 * @param dev Device structure
426 * @brief Clean-up all the ISOC buffers
428 * This function permits to clean-up all the ISOC buffers.
430 void usb_stk11xx_isoc_cleanup(struct usb_stk11xx *dev)
432 int i;
434 STK_DEBUG("Isoc cleanup\n");
436 if (dev == NULL)
437 return;
439 if (dev->isoc_init_ok == 0)
440 return;
442 // Unlinking ISOC buffers
443 for (i=0; i<MAX_ISO_BUFS; i++) {
444 struct urb *urb;
446 urb = dev->isobuf[i].urb;
448 if (urb != 0) {
449 if (dev->isoc_init_ok)
450 usb_kill_urb(urb);
452 usb_free_urb(urb);
453 dev->isobuf[i].urb = NULL;
457 // All is done
458 dev->isoc_init_ok = 0;
463 /**
464 * @param dev Device structure
465 * @param index Choice of the interface
467 * @returns 0 if all is OK
469 * @brief Send the message SET_FEATURE and choose the interface
471 * This function permits to send the message SET_FEATURE on the USB bus.
473 int usb_stk11xx_set_feature(struct usb_stk11xx *dev, int index)
475 int result;
476 struct usb_device *udev = dev->udev;
478 result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
479 USB_REQ_SET_FEATURE,
480 USB_TYPE_STANDARD | USB_DIR_OUT | USB_RECIP_DEVICE,
481 USB_DEVICE_REMOTE_WAKEUP,
482 index,
483 NULL,
485 500);
487 if (result < 0)
488 STK_ERROR("SET FEATURE fail !\n");
489 else
490 STK_DEBUG("SET FEATURE\n");
492 return result;
496 /**
497 * @param dev Device structure
499 * @returns 0 if all is OK
501 * @brief Send the message SET_CONFIGURATION
503 * This function permits to send the message SET_CONFIGURATION on the USB bus.
505 int usb_stk11xx_set_configuration(struct usb_stk11xx *dev)
507 int result;
508 struct usb_device *udev = dev->udev;
510 result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
511 USB_REQ_SET_CONFIGURATION,
512 USB_TYPE_STANDARD | USB_DIR_OUT | USB_RECIP_DEVICE,
514 udev->config[0].desc.bConfigurationValue,
515 NULL,
517 500);
519 if (result < 0)
520 STK_ERROR("SET CONFIGURATION fail !\n");
521 else
522 STK_DEBUG("SET CONFIGURATION %d\n", udev->config[0].desc.bConfigurationValue);
524 return result;
528 /**
529 * @param dev
530 * @param index
531 * @param value
533 * @returns 0 if all is OK
535 * @brief Write a 16-bits value to a 16-bits register
537 * This function permits to write a 16-bits value to a 16-bits register on the USB bus.
539 int usb_stk11xx_write_registry(struct usb_stk11xx *dev, __u16 index, __u16 value)
541 int result;
542 struct usb_device *udev = dev->udev;
544 result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
545 0x01,
546 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
547 value,
548 index,
549 NULL,
551 500);
553 if (result < 0)
554 STK_ERROR("Write registry fails %02X = %02X", index, value);
556 return result;
560 /**
561 * @param dev
562 * @param index
563 * @param value
565 * @returns 0 if all is OK
567 * @brief Read a 16-bits value from a 16-bits register
569 * This function permits to read a 16-bits value from a 16-bits register on the USB bus.
571 int usb_stk11xx_read_registry(struct usb_stk11xx *dev, __u16 index, int *value)
573 int result;
575 struct usb_device *udev = dev->udev;
577 *value = 0;
579 result = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
580 0x00,
581 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
582 0x00,
583 index,
584 (__u8 *) value,
585 sizeof(__u8),
586 500);
588 if (result < 0)
589 STK_ERROR("Read registry fails %02X", index);
591 return result;
595 /**
596 * @param dev
598 * @returns 0 if all is OK
600 * @brief Set the default value about the video settings.
602 * This function permits to set the video settings for each video camera model.
605 static int usb_stk11xx_default_settings(struct usb_stk11xx *dev)
607 switch (dev->webcam_model) {
608 case SYNTEK_STK_M811:
609 case SYNTEK_STK_A311:
610 dev->vsettings.fps = (default_fps == -1) ? 25 : default_fps;
611 dev->vsettings.vflip = (default_vflip == -1) ? 1 : default_vflip;
612 dev->vsettings.hflip = (default_hflip == -1) ? 1 : default_hflip;
614 dev->vsettings.brightness = (default_brightness == -1) ? STK11XX_PERCENT(50, 0xFFFF) : default_brightness;
615 dev->vsettings.whiteness = (default_whiteness == -1) ? STK11XX_PERCENT(50, 0xFFFF) : default_whiteness;
616 dev->vsettings.contrast = (default_contrast == -1) ? STK11XX_PERCENT(50, 0xFFFF) : default_contrast;
617 dev->vsettings.colour = (default_colour == -1) ? STK11XX_PERCENT(50, 0xFFFF) : default_colour;
618 break;
620 case SYNTEK_STK_A821:
621 dev->vsettings.fps = (default_fps == -1) ? 25 : default_fps;
622 dev->vsettings.vflip = (default_vflip == -1) ? 0 : default_vflip;
623 dev->vsettings.hflip = (default_hflip == -1) ? 0 : default_hflip;
625 dev->vsettings.brightness = (default_brightness == -1) ? STK11XX_PERCENT(50, 0xFFFF) : default_brightness;
626 dev->vsettings.whiteness = (default_whiteness == -1) ? STK11XX_PERCENT(50, 0xFFFF) : default_whiteness;
627 dev->vsettings.contrast = (default_contrast == -1) ? STK11XX_PERCENT(50, 0xFFFF) : default_contrast;
628 dev->vsettings.colour = (default_colour == -1) ? STK11XX_PERCENT(50, 0xFFFF) : default_colour;
629 break;
631 case SYNTEK_STK_6A31:
632 case SYNTEK_STK_6A33:
633 dev->vsettings.fps = (default_fps == -1) ? 25 : default_fps;
634 dev->vsettings.vflip = (default_vflip == -1) ? 0 : default_vflip;
635 dev->vsettings.hflip = (default_hflip == -1) ? 0 : default_hflip;
637 dev->vsettings.brightness = (default_brightness == -1) ? STK11XX_PERCENT(50, 0xFFFF) : default_brightness;
638 dev->vsettings.whiteness = (default_whiteness == -1) ? STK11XX_PERCENT(50, 0xFFFF) : default_whiteness;
639 dev->vsettings.contrast = (default_contrast == -1) ? STK11XX_PERCENT(50, 0xFFFF) : default_contrast;
640 dev->vsettings.colour = (default_colour == -1) ? STK11XX_PERCENT(50, 0xFFFF) : default_colour;
641 break;
643 case SYNTEK_STK_6A54:
644 dev->vsettings.fps = (default_fps == -1) ? 25 : default_fps;
645 dev->vsettings.vflip = (default_vflip == -1) ? 0 : default_vflip;
646 dev->vsettings.hflip = (default_hflip == -1) ? 0 : default_hflip;
648 dev->vsettings.brightness = (default_brightness == -1) ? STK11XX_PERCENT(50, 0xFFFF) : default_brightness;
649 dev->vsettings.whiteness = (default_whiteness == -1) ? STK11XX_PERCENT(50, 0xFFFF) : default_whiteness;
650 dev->vsettings.contrast = (default_contrast == -1) ? STK11XX_PERCENT(50, 0xFFFF) : default_contrast;
651 dev->vsettings.colour = (default_colour == -1) ? STK11XX_PERCENT(50, 0xFFFF) : default_colour;
652 break;
654 default:
655 return -1;
658 return 0;
662 /**
663 * @param interface
664 * @param id
666 * @returns 0 if all is OK
668 * @brief Load the driver
670 * This function detects the device and allocate the buffers for the device
671 * and the video interface.
673 static int usb_stk11xx_probe(struct usb_interface *interface, const struct usb_device_id *id)
675 int i;
676 int err;
677 size_t buffer_size;
679 int vendor_id;
680 int product_id;
681 int bNumInterfaces;
682 int webcam_model;
683 int webcam_type;
685 struct usb_stk11xx *dev = NULL;
686 struct usb_device *udev = interface_to_usbdev(interface);
687 struct usb_host_interface *iface_desc;
688 struct usb_endpoint_descriptor *endpoint;
691 // Get USB VendorID and ProductID
692 vendor_id = le16_to_cpu(udev->descriptor.idVendor);
693 product_id = le16_to_cpu(udev->descriptor.idProduct);
695 // Check if we can handle this device
696 STK_DEBUG("Probe function called with VendorID=%04X, ProductID=%04X and InterfaceNumber=%d\n",
697 vendor_id, product_id, interface->cur_altsetting->desc.bInterfaceNumber);
699 // The interface are probed one by one.
700 // We are interested in the video interface (always the interface '0')
701 // The interfaces '1' or '2' (if presents) are the audio control.
702 if (interface->cur_altsetting->desc.bInterfaceNumber > 0)
703 return -ENODEV;
705 // Detect device
706 if (vendor_id == USB_SYNTEK1_VENDOR_ID) {
707 switch (product_id) {
708 case USB_STK_A311_PRODUCT_ID:
709 STK_INFO("Syntek USB2.0 - STK-1135 based webcam found.\n");
710 STK_INFO("Syntek AVStream USB2.0 1.3M WebCam - Product ID 0xA311.\n");
711 webcam_model = SYNTEK_STK_A311;
712 webcam_type = STK11XX_SXGA;
713 break;
715 case USB_STK_A821_PRODUCT_ID:
716 STK_INFO("Syntek USB2.0 - STK-1135 based webcam found.\n");
717 STK_INFO("Syntek AVStream USB2.0 VGA WebCam - Product ID 0xA821.\n");
718 webcam_model = SYNTEK_STK_A821;
719 webcam_type = STK11XX_VGA;
720 break;
722 case USB_STK_6A31_PRODUCT_ID:
723 STK_INFO("Syntek USB2.0 - STK-1135 based webcam found.\n");
724 STK_INFO("Syntek AVStream USB2.0 1.3M WebCam - Product ID 0x6A31.\n");
725 webcam_model = SYNTEK_STK_6A31;
726 webcam_type = STK11XX_VGA;
727 break;
729 case USB_STK_6A33_PRODUCT_ID:
730 STK_INFO("Syntek USB2.0 - STK-1135 based webcam found.\n");
731 STK_INFO("Syntek AVStream USB2.0 1.3M WebCam - Product ID 0x6A33.\n");
732 webcam_model = SYNTEK_STK_6A33;
733 webcam_type = STK11XX_VGA;
734 break;
736 case USB_STK_6A54_PRODUCT_ID:
737 STK_INFO("Syntek USB2.0 - STK-1135 based webcam found.\n");
738 STK_INFO("Syntek AVStream USB2.0 1.3M WebCam - Product ID 0x6A54.\n");
739 webcam_model = SYNTEK_STK_6A54;
740 webcam_type = STK11XX_VGA;
741 break;
743 default:
744 STK_ERROR("usb_stk11xx_probe failed ! Camera product 0x%04X is not supported.\n",
745 le16_to_cpu(udev->descriptor.idProduct));
746 return -ENODEV;
749 else if (vendor_id == USB_SYNTEK2_VENDOR_ID) {
750 switch (product_id) {
751 case USB_STK_0501_PRODUCT_ID:
752 STK_INFO("Syntek USB2.0 - STK-1135 based webcam found.\n");
753 STK_INFO("Syntek AVStream USB2.0 1.3M WebCam - Product ID 0x0501.\n");
754 webcam_model = SYNTEK_STK_M811;
755 webcam_type = STK11XX_SXGA;
756 break;
758 default:
759 STK_ERROR("usb_stk11xx_probe failed ! Camera product 0x%04X is not supported.\n",
760 le16_to_cpu(udev->descriptor.idProduct));
761 return -ENODEV;
764 else
765 return -ENODEV;
767 // Allocate structure, initialize pointers, mutexes, etc. and link it to the usb_device
768 dev = kzalloc(sizeof(struct usb_stk11xx), GFP_KERNEL);
770 if (dev == NULL) {
771 STK_ERROR("Out of memory !\n");
772 return -ENOMEM;
775 // Init mutexes, spinlock, etc.
776 init_MUTEX(&dev->mutex);
777 spin_lock_init(&dev->spinlock);
778 init_waitqueue_head(&dev->wait_frame);
780 // Save pointers
781 dev->webcam_model = webcam_model;
782 dev->webcam_type = webcam_type;
783 dev->udev = udev;
784 dev->interface = interface;
786 // Read the product release
787 dev->release = le16_to_cpu(udev->descriptor.bcdDevice);
788 STK_INFO("Release: %04x\n", dev->release);
790 // How many interfaces (1 or 3) ?
791 bNumInterfaces = udev->config->desc.bNumInterfaces;
792 STK_INFO("Number of interfaces : %d\n", bNumInterfaces);
795 // Constructor
796 dev->nbuffers = 2;
797 dev->len_per_image = PAGE_ALIGN((1280 * 1024 * 4));
800 // Switch on the camera (to detect size of buffers)
801 dev_stk11xx_camera_on(dev);
804 // Set up the endpoint information
805 // use only the first int-in and isoc-in endpoints
806 // for the current alternate setting
807 iface_desc = interface->cur_altsetting;
809 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
810 endpoint = &iface_desc->endpoint[i].desc;
812 if (!dev->int_in_endpointAddr
813 && ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
814 && ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)) {
815 // we found an interrupt in endpoint
816 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
818 dev->int_in_size = buffer_size;
819 dev->int_in_endpointAddr = (endpoint->bEndpointAddress & 0xf);
822 if (!dev->isoc_in_endpointAddr
823 && ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
824 && ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_ISOC)) {
825 // we found an isoc in endpoint
826 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
828 dev->isoc_in_size = buffer_size;
829 dev->isoc_in_endpointAddr = (endpoint->bEndpointAddress & 0xf);
833 if (!(dev->int_in_endpointAddr && dev->isoc_in_endpointAddr)) {
834 STK_ERROR("Could not find both int-in and isoc-in endpoints");
836 kfree(dev);
838 return -ENODEV;
842 // Switch off camera
843 dev_stk11xx_camera_off(dev);
845 // Initialize the video device
846 dev->vdev = video_device_alloc();
848 if (!dev->vdev) {
849 kfree(dev);
850 return -ENOMEM;
853 // Initialize the camera
854 dev_stk11xx_initialize_device(dev);
856 // Register the video device
857 err = v4l_stk11xx_register_video_device(dev);
859 if (err) {
860 kfree(dev);
861 return err;
864 // Create the entries in the sys filesystem
865 stk11xx_create_sysfs_files(dev->vdev);
867 // Save our data pointer in this interface device
868 usb_set_intfdata(interface, dev);
870 // Default settings video device
871 usb_stk11xx_default_settings(dev);
874 return 0;
878 /**
879 * @param interface
881 * @brief This function is called when the device is disconnected
882 * or when the kernel module is unloaded.
884 static void usb_stk11xx_disconnect(struct usb_interface *interface)
886 struct usb_stk11xx *dev = usb_get_intfdata(interface);
888 STK_INFO("Syntek USB2.0 Camera disconnected\n");
890 usb_set_intfdata(interface, NULL);
892 // We got unplugged; this is signalled by an EPIPE error code
893 if (dev->vopen) {
894 STK_INFO("Disconnected while webcam is in use !\n");
895 dev->error_status = EPIPE;
898 // Alert waiting processes
899 wake_up_interruptible(&dev->wait_frame);
901 // Wait until device is closed
902 while (dev->vopen)
903 schedule();
905 // Unregister the video device
906 v4l_stk11xx_unregister_video_device(dev);
908 // Remove the entries in the sys filesystem
909 stk11xx_remove_sysfs_files(dev->vdev);
914 * @var usb_stk11xx_driver
916 * This variable contains some callback
918 static struct usb_driver usb_stk11xx_driver = {
919 .name = "usb_stk11xx_driver",
920 .probe = usb_stk11xx_probe,
921 .disconnect = usb_stk11xx_disconnect,
922 .id_table = stk11xx_table,
927 * @var fps
928 * Module parameter to set frame per second
930 static int fps;
933 * @var hflip
934 * Module parameter to enable/disable the horizontal flip process
936 static int hflip = -1;
939 * @var vflip
940 * Module parameter to enable/disable the vertical flip process
942 static int vflip = -1;
945 * @var brightness
946 * Module parameter to set the brightness
948 static int brightness = -1;
951 * @var whiteness
952 * Module parameter to set the whiteness
954 static int whiteness = -1;
957 * @var contrast
958 * Module parameter to set the contrast
960 static int contrast = -1;
963 * @var colour
964 * Module parameter to set the colour
966 static int colour = -1;
969 module_param(fps, int, 0444); /**< @brief Module frame per second parameter */
970 module_param(hflip, int, 0444); /**< @brief Module horizontal flip process */
971 module_param(vflip, int, 0444); /**< @brief Module vertical flip process */
973 module_param(brightness, int, 0444); /**< @brief Module brightness */
974 module_param(whiteness, int, 0444); /**< @brief Module whiteness */
975 module_param(contrast, int, 0444); /**< @brief Module contrast */
976 module_param(colour, int, 0444); /**< @brief Module colour */
979 /**
980 * @returns 0 if all is OK
982 * @brief Initialize the driver.
984 * This function is called at first.
985 * This function permits to define the default values from the command line.
987 static int __init usb_stk11xx_init(void)
989 int result;
992 STK_INFO("Syntek USB2.0 webcam driver startup\n");
994 // Frame per second parameter
995 if (fps) {
996 if (fps < 9 || fps > 30) {
997 STK_ERROR("Framerate out of bounds [10-30] !\n");
998 return -EINVAL;
1001 default_fps = fps;
1004 // Horizontal flip value
1005 if ((hflip == 0) || (hflip == 1)) {
1006 STK_DEBUG("Set horizontal flip = %d\n", hflip);
1008 default_hflip = hflip;
1011 // Vertical flip value
1012 if ((vflip == 0) || (vflip == 1)) {
1013 STK_DEBUG("Set vertical flip = %d\n", vflip);
1015 default_vflip = vflip;
1018 // Brightness value
1019 if (brightness > -1) {
1020 STK_DEBUG("Set brightness = 0x%X\n", brightness);
1022 default_brightness = 0xffff & brightness;
1025 // Whiteness value
1026 if (whiteness > -1) {
1027 STK_DEBUG("Set whiteness = 0x%X\n", whiteness);
1029 default_whiteness = 0xffff & whiteness;
1032 // Contrast value
1033 if (contrast > -1) {
1034 STK_DEBUG("Set contrast = 0x%X\n", contrast);
1036 default_contrast = 0xffff & contrast;
1039 // Colour value
1040 if (colour > -1) {
1041 STK_DEBUG("Set colour = 0x%X\n", colour);
1043 default_colour = 0xffff & colour;
1047 // Register the driver with the USB subsystem
1048 result = usb_register(&usb_stk11xx_driver);
1050 if (result)
1051 STK_ERROR("usb_register failed ! Error number %d\n", result);
1053 STK_INFO(DRIVER_VERSION " : " DRIVER_DESC "\n");
1055 return result;
1059 /**
1060 * @brief Close the driver
1062 * This function is called at last when you unload the driver.
1064 static void __exit usb_stk11xx_exit(void)
1066 STK_INFO("usb_stk11xx_exit: Syntek USB2.0 webcam driver shutdown\n");
1068 // Deregister this driver with the USB subsystem
1069 usb_deregister(&usb_stk11xx_driver);
1073 module_init(usb_stk11xx_init); /**< @brief Module initialize */
1074 module_exit(usb_stk11xx_exit); /**< @brief Module exit */
1077 MODULE_PARM_DESC(fps, "Frames per second [5-30]"); /**< @brief Description of 'fps' parameter */
1078 MODULE_PARM_DESC(hflip, "Horizontal image flip"); /**< @brief Description of 'hflip' parameter */
1079 MODULE_PARM_DESC(vflip, "Vertical image flip"); /**< @brief Description of 'vflip' parameter */
1080 MODULE_PARM_DESC(brightness, "Brightness setting"); /**< @brief Description of 'brightness' parameter */
1081 MODULE_PARM_DESC(whiteness, "Whiteness setting"); /**< @brief Description of 'whiteness' parameter */
1082 MODULE_PARM_DESC(colour, "Colour setting"); /**< @brief Description of 'colour' parameter */
1083 MODULE_PARM_DESC(contrast, "Contrast setting"); /**< @brief Description of 'contrast' parameter */
1086 MODULE_LICENSE("GPL"); /**< @brief Driver is under licence GPL */
1087 MODULE_AUTHOR(DRIVER_AUTHOR); /**< @brief Driver is written by Nicolas VIVIEN */
1088 MODULE_DESCRIPTION(DRIVER_DESC); /**< @brief Define the description of the driver */
1089 MODULE_SUPPORTED_DEVICE(DRIVER_SUPPORT); /**< @brief List of supported device */