Added support for O_NONBLOCK in DQBUF ioctl
[microdia.git] / microdia-usb.c
blob01d0db128b7ba22ddfc50901b2643caa82599cd1
1 /**
2 * @file microdia-usb.c
3 * @author Nicolas VIVIEN
4 * @date 2008-02-01
5 * @version v0.0.0
7 * @brief Driver for Microdia 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: 2007-12-08 11:34:45 +0100 (sam, 08 déc 2007) $
29 * $Revision: 68 $
30 * $Author: nicklas79 $
31 * $HeadURL: https://syntekdriver.svn.sourceforge.net/svnroot/syntekdriver/trunk/driver/microdia-usb.c $
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/debugfs.h>
42 #include <linux/stat.h>
44 #include <linux/usb.h>
45 #include <media/v4l2-common.h>
47 #include "microdia.h"
49 /**
50 * @var fps
51 * Module parameter to set frame per second
53 static int fps = 25;
55 /**
56 * @var hflip
57 * Module parameter to enable/disable the horizontal flip process
59 static int hflip = 0;
61 /**
62 * @var vflip
63 * Module parameter to enable/disable the vertical flip process
65 static int vflip = 0;
67 /**
68 * @var brightness
69 * Module parameter to set the brightness
71 static int brightness = MICRODIA_PERCENT(50, 0xFFFF);
73 /**
74 * @var whiteness
75 * Module parameter to set the whiteness
77 static int whiteness = MICRODIA_PERCENT(20, 0xFFFF);
79 /**
80 * @var contrast
81 * Module parameter to set the contrast
83 static int contrast = MICRODIA_PERCENT(50, 0xFFFF);
85 /**
86 * @var exposure
87 * Module parameter to set the exposure
89 static int exposure = MICRODIA_PERCENT(20, 0xFFFF);
91 /**
92 * @var debug_dir_name
93 * Name of our directory in debugfs
95 static const char * debug_dir_name = "microdia";
97 /**
98 * @var debug_file_name
99 * Name of our debug file in debugfs
101 static const char * debug_file_name = "debug_level";
104 * @var debug_dir
105 * Dentry for our debug dir (for cleanup)
107 static struct dentry * debug_dir;
110 * @var debug_file
111 * Dentry for our debug file (for cleanup)
113 static struct dentry * debug_file;
116 * @var debug_level
117 * Level at which we do debugging
119 static __u8 debug_level = 0;
123 * @var microdia_table
124 * Define all the hotplug supported devices by this driver
126 static struct usb_device_id microdia_table[] = {
127 { USB_DEVICE(USB_MICRODIA_VENDOR_ID, USB_UDIA_6027_PRODUCT_ID) },
128 { USB_DEVICE(USB_MICRODIA_VENDOR_ID, USB_UDIA_608F_PRODUCT_ID) },
129 { USB_DEVICE(USB_MICRODIA_VENDOR_ID, USB_UDIA_60FE_PRODUCT_ID) },
130 { USB_DEVICE(USB_MICRODIA_VENDOR_ID, USB_UDIA_6242_PRODUCT_ID) },
131 { USB_DEVICE(USB_MICRODIA_VENDOR_ID, USB_UDIA_6253_PRODUCT_ID) },
132 { USB_DEVICE(USB_MICRODIA_VENDOR_ID, USB_UDIA_6260_PRODUCT_ID) },
133 { USB_DEVICE(USB_MICRODIA_VENDOR_ID, USB_UDIA_6270_PRODUCT_ID) },
134 { USB_DEVICE(USB_MICRODIA_VENDOR_ID, USB_UDIA_62C0_PRODUCT_ID) },
135 { USB_DEVICE(USB_MICRODIA_VENDOR_ID, USB_UDIA_613B_PRODUCT_ID) },
136 { USB_DEVICE(USB_MICRODIA_VENDOR_ID, USB_UDIA_613C_PRODUCT_ID) },
137 { USB_DEVICE(USB_MICRODIA_VENDOR_ID, USB_UDIA_624F_PRODUCT_ID) },
138 { USB_DEVICE(USB_MICRODIA_VENDOR_ID, USB_UDIA_627B_PRODUCT_ID) },
139 { USB_DEVICE(USB_MICRODIA_VENDOR_ID, USB_UDIA_62C0_PRODUCT_ID) },
140 { USB_DEVICE(USB_MICRODIA_VENDOR_ID, USB_UDIA_8105_PRODUCT_ID) },
141 { USB_DEVICE(USB_MICRODIA_VENDOR_ID, USB_UDIA_624E_PRODUCT_ID) },
142 { USB_DEVICE(USB_MICRODIA_VENDOR_ID, USB_UDIA_6128_PRODUCT_ID) },
147 MODULE_DEVICE_TABLE(usb, microdia_table); /**< Define the supported devices */
151 /**
152 * @param dev Device structure
154 * @returns 0 if all is OK
156 * @brief Initilize an isochronous pipe.
158 * This function permits to initialize an URB transfert (or isochronous pipe).
160 int usb_microdia_isoc_init(struct usb_microdia *dev)
162 int i, j;
163 int ret = 0;
164 __u16 iso_max_frame_size;
165 struct urb *urb;
166 struct usb_device *udev;
168 if (dev == NULL)
169 return -EFAULT;
171 if (dev->isoc_init_ok)
172 return 0;
174 udev = dev->udev;
176 UDIA_DEBUG("usb_microdia_isoc_init()\n");
178 // Allocate URB structure
179 for (i=0; i<MAX_ISO_BUFS; i++) {
180 urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
182 if (urb == NULL) {
183 UDIA_ERROR("Failed to allocate URB %d\n", i);
184 ret = -ENOMEM;
185 break;
188 dev->isobuf[i].urb = urb;
191 if (ret) {
192 while (i >= 0) {
193 if (dev->isobuf[i].urb != NULL)
194 usb_free_urb(dev->isobuf[i].urb);
196 dev->isobuf[i].urb = NULL;
197 i--;
200 return ret;
204 // iso_max_frame_size should be the endpoints maximum packetsize * the highbandwidth multiplier.
206 iso_max_frame_size = max_packet_sz(le16_to_cpu(dev->isoc_in_size)) * hb_multiplier(le16_to_cpu(dev->isoc_in_size));
208 // Init URB structure
209 for (i=0; i<MAX_ISO_BUFS; i++) {
210 urb = dev->isobuf[i].urb;
212 urb->interval = 1;
213 urb->dev = udev;
214 urb->pipe = usb_rcvisocpipe(udev, dev->isoc_in_endpointAddr);
215 urb->transfer_flags = URB_ISO_ASAP;
216 urb->transfer_buffer = dev->isobuf[i].data;
217 urb->transfer_buffer_length = iso_max_frame_size * ISO_FRAMES_PER_DESC;
218 urb->complete = usb_microdia_isoc_handler;
219 urb->context = dev;
220 urb->start_frame = 0;
221 urb->number_of_packets = ISO_FRAMES_PER_DESC;
223 for (j=0; j<ISO_FRAMES_PER_DESC; j++) {
224 urb->iso_frame_desc[j].offset = j * iso_max_frame_size;
225 urb->iso_frame_desc[j].length = iso_max_frame_size; //dev->isoc_in_size;
229 UDIA_DEBUG("dev->isoc_in_size = %X\n", dev->isoc_in_size);
230 UDIA_DEBUG("dev->isoc_in_endpointAddr = %X\n", dev->isoc_in_endpointAddr);
232 // Link
233 for (i=0; i<MAX_ISO_BUFS; i++) {
234 ret = usb_submit_urb(dev->isobuf[i].urb, GFP_KERNEL);
236 if (ret)
237 UDIA_ERROR("isoc_init() submit_urb %d failed with error %d\n", i, ret);
238 else
239 UDIA_DEBUG("URB 0x%p submitted.\n", dev->isobuf[i].urb);
241 switch (ret) {
242 case -ENOMEM:
243 UDIA_ERROR("ENOMEM\n");
244 break;
245 case -ENODEV:
246 UDIA_ERROR("ENODEV\n");
247 break;
248 case -ENXIO:
249 UDIA_ERROR("ENXIO\n");
250 break;
251 case -EINVAL:
252 UDIA_ERROR("EINVAL\n");
253 break;
254 case -EAGAIN:
255 UDIA_ERROR("EAGAIN\n");
256 break;
257 case -EFBIG:
258 UDIA_ERROR("EFBIG\n");
259 break;
260 case -EPIPE:
261 UDIA_ERROR("EPIPE\n");
262 break;
263 case -EMSGSIZE:
264 UDIA_ERROR("EMSGSIZE\n");
265 break;
269 // All is done
270 dev->isoc_init_ok = 1;
272 return 0;
276 /**
277 * @param urb URB structure
279 * @brief ISOC handler
281 * This function is called as an URB transfert is complete (Isochronous pipe).
282 * So, the traitement is done in interrupt time, so it has be fast, not crash,
283 * ans not stall. Neat.
285 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
286 void usb_microdia_isoc_handler(struct urb *urb, struct pt_regs *regs)
287 #else
288 void usb_microdia_isoc_handler(struct urb *urb)
289 #endif
291 int i;
292 int ret;
293 int skip;
295 int awake = 0;
296 int framestatus;
297 int framelen;
299 unsigned char *fill = NULL;
300 unsigned char *iso_buf = NULL;
302 struct usb_microdia *dev;
303 struct microdia_frame_buf *framebuf;
305 unsigned char frame_header[] = {0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96};
307 UDIA_STREAM("Isoc handler\n");
309 dev = (struct usb_microdia *) urb->context;
311 if (dev == NULL) {
312 UDIA_ERROR("isoc_handler called with NULL device !\n");
313 return;
316 if (urb->status == -ENOENT || urb->status == -ECONNRESET) {
317 UDIA_DEBUG("URB unlinked synchronuously !\n");
318 return;
321 if (urb->status != -EINPROGRESS && urb->status != 0) {
322 const char *errmsg;
324 errmsg = "Unknown";
326 switch(urb->status) {
327 case -ENOSR:
328 errmsg = "Buffer error (overrun)";
329 break;
331 case -EPIPE:
332 errmsg = "Stalled (device not responding)";
333 break;
335 case -EOVERFLOW:
336 errmsg = "Babble (bad cable?)";
337 break;
339 case -EPROTO:
340 errmsg = "Bit-stuff error (bad cable?)";
341 break;
343 case -EILSEQ:
344 errmsg = "CRC/Timeout (could be anything)";
345 break;
347 case -ETIMEDOUT:
348 errmsg = "NAK (device does not respond)";
349 break;
352 UDIA_ERROR("isoc_handler() called with status %d [%s].\n", urb->status, errmsg);
354 dev->visoc_errors++;
356 wake_up_interruptible(&dev->wait_frame);
358 urb->dev = dev->udev;
359 ret = usb_submit_urb(urb, GFP_ATOMIC);
361 if (ret != 0) {
362 UDIA_ERROR("Error (%d) re-submitting urb in microdia_isoc_handler.\n", ret);
365 return;
368 framebuf = dev->fill_frame;
370 if (framebuf == NULL) {
371 UDIA_ERROR("isoc_handler without valid fill frame !\n");
373 wake_up_interruptible(&dev->wait_frame);
375 urb->dev = dev->udev;
376 ret = usb_submit_urb(urb, GFP_ATOMIC);
378 if (ret != 0) {
379 UDIA_ERROR("Error (%d) re-submitting urb in microdia_isoc_handler.\n", ret);
382 return;
384 else {
385 fill = framebuf->data + framebuf->filled;
388 // Reset ISOC error counter
389 dev->visoc_errors = 0;
391 // Compact data
392 for (i=0; i<urb->number_of_packets; i++) {
393 framestatus = urb->iso_frame_desc[i].status;
394 framelen = urb->iso_frame_desc[i].actual_length;
395 iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
397 if (framestatus == 0) {
398 // By default, I keep all packet.
399 // So I don't skip byte !
400 skip = 0;
402 if (framelen > 0) {
403 // For microdia devices, data are isoc packet with site > 0.
404 // ...
405 if (memcmp(iso_buf, frame_header, 6) == 0) {
406 if ((framebuf->filled == dev->frame_size + (dev->frame_size/2)) && framebuf->errors == 0) {
407 if (microdia_next_frame(dev)) {
408 dev->vframes_dumped++;
410 } else {
411 dev->vframes_error++;
413 awake = 1;
414 framebuf = dev->fill_frame;
415 framebuf->filled = 0;
416 framebuf->errors = 0;
417 fill = framebuf->data;
418 skip = 64;
420 // Our buffer is full !!!
421 if (framelen - skip + framebuf->filled > dev->frame_size + (dev->frame_size/2)) {
422 UDIA_ERROR("Frame buffer overflow !\n");
423 framebuf->errors++;
425 // All is OK
426 else {
427 memcpy(fill, iso_buf + skip, framelen - skip);
428 fill += framelen - skip;
431 // New size of our buffer
432 framebuf->filled += framelen - skip;
435 UDIA_STREAM("URB : Length = %d - Skip = %d - Buffer size = %d\n",
436 framelen, skip, framebuf->filled);
438 else {
439 UDIA_ERROR("Iso frame %d of USB has error %d\n", i, framestatus);
443 if (awake == 1)
444 wake_up_interruptible(&dev->wait_frame);
446 urb->dev = dev->udev;
448 ret = usb_submit_urb(urb, GFP_ATOMIC);
450 if (ret != 0) {
451 UDIA_ERROR("Error (%d) re-submitting urb in microdia_isoc_handler.\n", ret);
456 /**
457 * @param dev Device structure
459 * @brief Clean-up all the ISOC buffers
461 * This function permits to clean-up all the ISOC buffers.
463 void usb_microdia_isoc_cleanup(struct usb_microdia *dev)
465 int i;
467 UDIA_DEBUG("Isoc cleanup\n");
469 if (dev == NULL)
470 return;
472 if (dev->isoc_init_ok == 0)
473 return;
475 // Unlinking ISOC buffers
476 for (i=0; i<MAX_ISO_BUFS; i++) {
477 struct urb *urb;
479 urb = dev->isobuf[i].urb;
481 if (urb != 0) {
482 if (dev->isoc_init_ok)
483 usb_kill_urb(urb);
485 usb_free_urb(urb);
486 dev->isobuf[i].urb = NULL;
490 // All is done
491 dev->isoc_init_ok = 0;
496 /**
497 * @param dev Device structure
498 * @param index Choice of the interface
500 * @returns 0 if all is OK
502 * @brief Send the message SET_FEATURE and choose the interface
504 * This function permits to send the message SET_FEATURE on the USB bus.
506 int usb_microdia_set_feature(struct usb_microdia *dev, int index)
508 int result;
509 struct usb_device *udev = dev->udev;
511 result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
512 USB_REQ_SET_FEATURE,
513 USB_TYPE_STANDARD | USB_DIR_OUT | USB_RECIP_DEVICE,
514 USB_DEVICE_REMOTE_WAKEUP,
515 index,
516 NULL,
518 500);
520 if (result < 0)
521 UDIA_ERROR("SET FEATURE fail !\n");
522 else
523 UDIA_DEBUG("SET FEATURE\n");
525 return result;
529 /**
530 * @param dev Device structure
532 * @returns 0 if all is OK
534 * @brief Send the message SET_CONFIGURATION
536 * This function permits to send the message SET_CONFIGURATION on the USB bus.
538 int usb_microdia_set_configuration(struct usb_microdia *dev)
540 int result;
541 struct usb_device *udev = dev->udev;
543 result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
544 USB_REQ_SET_CONFIGURATION,
545 USB_TYPE_STANDARD | USB_DIR_OUT | USB_RECIP_DEVICE,
547 udev->config[0].desc.bConfigurationValue,
548 NULL,
550 500);
552 if (result < 0)
553 UDIA_ERROR("SET CONFIGURATION fail !\n");
554 else
555 UDIA_DEBUG("SET CONFIGURATION %d\n", udev->config[0].desc.bConfigurationValue);
557 return result;
562 * @param dev Device structure
563 * @param value register to write to
564 * @param data
565 * @param length number of bytes
567 * @returns 0 if all is OK
569 * @brief Write a 16-bit value to a 16-bit register
571 * This function permits to write a 16-bit value to a 16-bit register on the USB bus.
573 int usb_microdia_control_write(struct usb_microdia *dev, __u16 value, __u8 *data, __u16 length)
575 int result;
576 struct usb_device *udev = dev->udev;
578 result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
579 0x08,
580 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
581 value,
582 0x00,
583 data,
584 length,
585 500);
587 if (result < 0)
588 UDIA_ERROR("Write register failed index = %02X", value);
590 return result;
594 * @param dev
595 * @param commands
596 * @param data
597 * @param cmdlen
598 * @param datalen
600 * @returns 0 if all is OK
602 * @brief Write a series of 16-bit "commands" using the same buffer
604 int usb_microdia_control_write_multi(struct usb_microdia *dev, __u16 *commands, __u8 *data,
605 __u16 cmdlen, __u16 datalen)
607 int result, i;
609 for(i = 0; i < cmdlen; i++) {
610 result = usb_microdia_control_write(dev, commands[i], data, datalen);
612 if(result < 0)
613 return result;
616 return 0;
620 * @param dev Device structure
621 * @param index register to read from
622 * @param data
623 * @param length number of bytes
625 * @returns 0 if all is OK
627 * @brief Read a 16-bit value from a 16-bit register
629 * This function permits to read a 16-bit value from a 16-bit register on the USB bus.
631 int usb_microdia_control_read(struct usb_microdia *dev, __u16 index, __u8 *data, __u16 length)
633 int result;
635 struct usb_device *udev = dev->udev;
637 *data = 0;
639 result = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
640 0x00,
641 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
642 index,
643 0x00,
644 data,
645 length,
646 500);
648 if (result < 0)
649 UDIA_ERROR("Read register failed %02X", index);
651 return result;
655 /**
656 * @param dev
658 * @returns 0 if all is OK
660 * @brief Set the default value about the video settings.
662 * This function permits to set the video settings for each video camera model.
665 static int usb_microdia_default_settings(struct usb_microdia *dev)
667 switch (dev->webcam_model) {
668 default:
669 dev->vsettings.fps = fps;
670 dev->vsettings.vflip = vflip;
671 dev->vsettings.hflip = hflip;
673 dev->vsettings.brightness = brightness & 0xffff;
674 dev->vsettings.contrast = contrast & 0xffff;
675 dev->vsettings.whiteness = whiteness & 0xffff;
676 dev->vsettings.exposure = exposure & 0xffff;
678 break;
682 return 0;
685 extern int microdia_627b_initialize(struct usb_microdia *dev);
686 extern int microdia_627b_start_stream(struct usb_microdia *dev);
687 extern int microdia_627b_stop_stream(struct usb_microdia *dev);
688 extern int microdia_624f_initialize(struct usb_microdia *dev);
689 extern int microdia_624e_initialize(struct usb_microdia *dev);
690 extern int microdia_6128_initialize(struct usb_microdia *dev);
691 extern int microdia_6270_initialize(struct usb_microdia *dev);
692 extern int microdia_624f_stop_stream(struct usb_microdia *dev);
693 extern int microdia_624f_start_stream(struct usb_microdia *dev);
694 extern int microdia_624f_set_exposure(struct usb_microdia *dev);
695 extern int microdia_6128_start_stream(struct usb_microdia *dev);
696 extern int microdia_6128_stop_stream(struct usb_microdia *dev);
697 extern int microdia_624e_start_stream(struct usb_microdia *dev);
698 extern int microdia_624e_stop_stream(struct usb_microdia *dev);
699 extern int microdia_6242_start_stream(struct usb_microdia *dev);
700 extern int microdia_6242_stop_stream(struct usb_microdia *dev);
701 extern int microdia_6260_initialize(struct usb_microdia *dev);
702 extern int microdia_6260_start_stream(struct usb_microdia *dev);
703 extern int microdia_6260_stop_stream(struct usb_microdia *dev);
704 extern int microdia_6270_start_stream(struct usb_microdia *dev);
705 extern int microdia_6270_stop_stream(struct usb_microdia *dev);
706 extern int microdia_6270_set_exposure(struct usb_microdia *dev);
709 /**
710 * @param interface
711 * @param id
713 * @returns 0 if all is OK
715 * @brief Load the driver
717 * This function detects the device and allocate the buffers for the device
718 * and the video interface.
720 static int usb_microdia_probe(struct usb_interface *interface, const struct usb_device_id *id)
722 int i;
723 int err;
724 size_t buffer_size;
726 int vendor_id;
727 int product_id;
728 int bNumInterfaces;
729 int webcam_model;
730 int webcam_type;
731 int (*initialize)(struct usb_microdia *) = NULL;
732 int (*stop_stream)(struct usb_microdia *) = NULL;
733 int (*start_stream)(struct usb_microdia *) = NULL;
734 int (*set_exposure)(struct usb_microdia *) = NULL;
736 struct usb_microdia *dev = NULL;
737 struct usb_device *udev = interface_to_usbdev(interface);
738 struct usb_host_interface *iface_desc;
739 struct usb_endpoint_descriptor *endpoint;
742 // Get USB VendorID and ProductID
743 vendor_id = le16_to_cpu(udev->descriptor.idVendor);
744 product_id = le16_to_cpu(udev->descriptor.idProduct);
746 // Check if we can handle this device
747 UDIA_DEBUG("Probe function called with VendorID=%04X, ProductID=%04X and InterfaceNumber=%d\n",
748 vendor_id, product_id, interface->cur_altsetting->desc.bInterfaceNumber);
750 // The interface are probed one by one.
751 // We are interested in the video interface (always the interface '0')
752 // The interfaces '1' or '2' (if presents) are the audio control.
753 if (interface->cur_altsetting->desc.bInterfaceNumber > 0)
754 return -ENODEV;
756 // Detect device
757 if (vendor_id == USB_MICRODIA_VENDOR_ID) {
758 switch (product_id) {
759 case USB_UDIA_6027_PRODUCT_ID:
760 UDIA_INFO("Microdia USB2.0 Webcam - Product ID 6027.\n");
761 webcam_model = MICRODIA_6027;
762 webcam_type = MICRODIA_VGA;
763 break;
765 case USB_UDIA_608F_PRODUCT_ID:
766 UDIA_INFO("Microdia USB2.0 Webcam - Product ID 608F.\n");
767 webcam_model = MICRODIA_608F;
768 webcam_type = MICRODIA_VGA;
769 break;
771 case USB_UDIA_60FE_PRODUCT_ID:
772 webcam_model = MICRODIA_60FE;
773 webcam_type = MICRODIA_VGA;
774 break;
776 case USB_UDIA_6242_PRODUCT_ID:
777 UDIA_INFO("Microdia USB2.0 Webcam - Product ID 6242.\n");
778 webcam_model = MICRODIA_6242;
779 webcam_type = MICRODIA_VGA;
780 initialize = microdia_624e_initialize;
781 start_stream = microdia_6242_start_stream;
782 stop_stream = microdia_6242_stop_stream;
783 break;
785 case USB_UDIA_6253_PRODUCT_ID:
786 webcam_model = MICRODIA_6253;
787 webcam_type = MICRODIA_VGA;
788 break;
790 case USB_UDIA_6260_PRODUCT_ID:
791 UDIA_INFO("Microdia USB2.0 Webcam - Product ID 6260.\n");
792 webcam_model = MICRODIA_6260;
793 webcam_type = MICRODIA_VGA;
794 initialize = microdia_6260_initialize;
795 start_stream = microdia_6260_start_stream;
796 stop_stream = microdia_6260_stop_stream;
797 break;
799 case USB_UDIA_6270_PRODUCT_ID:
800 UDIA_INFO("Microdia USB2.0 Webcam - Product ID 6270.\n");
801 webcam_model = MICRODIA_6270;
802 webcam_type = MICRODIA_VGA;
803 initialize = microdia_6270_initialize;
804 start_stream = microdia_6270_start_stream;
805 stop_stream = microdia_6270_stop_stream;
806 set_exposure = microdia_6270_set_exposure;
807 break;
809 case USB_UDIA_60C0_PRODUCT_ID:
810 webcam_model = MICRODIA_60C0;
811 webcam_type = MICRODIA_VGA;
812 break;
814 case USB_UDIA_613B_PRODUCT_ID:
815 webcam_model = MICRODIA_613B;
816 webcam_type = MICRODIA_VGA;
817 break;
819 case USB_UDIA_613C_PRODUCT_ID:
820 webcam_model = MICRODIA_613C;
821 webcam_type = MICRODIA_VGA;
822 break;
824 case USB_UDIA_624F_PRODUCT_ID:
825 UDIA_INFO("Microdia USB2.0 Webcam - Product ID 624F.\n");
826 webcam_model = MICRODIA_624F;
827 webcam_type = MICRODIA_VGA;
828 initialize = microdia_624f_initialize;
829 start_stream = microdia_624f_start_stream;
830 stop_stream = microdia_624f_stop_stream;
831 set_exposure = microdia_624f_set_exposure;
832 break;
834 case USB_UDIA_627B_PRODUCT_ID:
835 UDIA_INFO("Microdia USB2.0 Webcam - Product ID 627B.\n");
836 webcam_model = MICRODIA_627B;
837 webcam_type = MICRODIA_VGA;
838 initialize = microdia_627b_initialize;
839 start_stream = microdia_627b_start_stream;
840 stop_stream = microdia_627b_stop_stream;
841 break;
843 case USB_UDIA_62C0_PRODUCT_ID:
844 webcam_model = MICRODIA_62C0;
845 webcam_type = MICRODIA_VGA;
846 break;
848 case USB_UDIA_8105_PRODUCT_ID:
849 webcam_model = MICRODIA_8105;
850 webcam_type = MICRODIA_VGA;
851 break;
853 case USB_UDIA_624E_PRODUCT_ID:
854 UDIA_INFO("Microdia USB2.0 Webcam - Product ID 624E.\n");
855 webcam_model = MICRODIA_624E;
856 webcam_type = MICRODIA_SXGA;
857 initialize = microdia_624e_initialize;
858 start_stream = microdia_624e_start_stream;
859 stop_stream = microdia_624e_stop_stream;
860 break;
862 case USB_UDIA_6128_PRODUCT_ID:
863 UDIA_INFO("Microdia USB2.0 Webcam - Product ID 6128.\n");
864 webcam_model = MICRODIA_6128;
865 webcam_type = MICRODIA_SXGA;
866 initialize = microdia_6128_initialize;
867 start_stream = microdia_6128_start_stream;
868 stop_stream = microdia_6128_stop_stream;
869 break;
871 default:
872 UDIA_ERROR("usb_microdia_probe failed ! Camera product 0x%04X is not supported.\n",
873 le16_to_cpu(udev->descriptor.idProduct));
874 return -ENODEV;
877 else
878 return -ENODEV;
880 // Allocate structure, initialize pointers, mutexes, etc. and link it to the usb_device
881 dev = kzalloc(sizeof(struct usb_microdia), GFP_KERNEL);
883 if (dev == NULL) {
884 UDIA_ERROR("Out of memory !\n");
885 return -ENOMEM;
888 // Init mutexes, spinlock, etc.
889 init_MUTEX(&dev->mutex);
890 spin_lock_init(&dev->spinlock);
891 init_waitqueue_head(&dev->wait_frame);
893 // Save pointers
894 dev->webcam_model = webcam_model;
895 dev->webcam_type = webcam_type;
896 dev->initialize = initialize;
897 dev->stop_stream = stop_stream;
898 dev->start_stream = start_stream;
899 dev->set_exposure = set_exposure;
900 dev->udev = udev;
901 dev->interface = interface;
903 // Read the product release
904 dev->release = le16_to_cpu(udev->descriptor.bcdDevice);
905 UDIA_INFO("Release: %04x\n", dev->release);
907 // How many interfaces (1 or 3) ?
908 bNumInterfaces = udev->config->desc.bNumInterfaces;
909 UDIA_INFO("Number of interfaces : %d\n", bNumInterfaces);
912 // Constructor
913 dev->nbuffers = 2;
914 dev->len_per_image = PAGE_ALIGN((1280 * 1024 * 4));
917 // Switch on the camera (to detect size of buffers)
918 dev_microdia_camera_on(dev);
921 // Set up the endpoint information
922 // use only the first int-in and isoc-in endpoints
923 // for the current alternate setting
924 iface_desc = interface->cur_altsetting;
926 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
927 endpoint = &iface_desc->endpoint[i].desc;
929 if (!dev->int_in_endpointAddr
930 && ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
931 && ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)) {
932 // we found an interrupt in endpoint
933 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
935 dev->int_in_size = buffer_size;
936 dev->int_in_endpointAddr = (endpoint->bEndpointAddress & 0xf);
939 if (!dev->isoc_in_endpointAddr
940 && ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
941 && ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_ISOC)) {
942 // we found an isoc in endpoint
943 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
945 dev->isoc_in_size = buffer_size;
946 dev->isoc_in_endpointAddr = (endpoint->bEndpointAddress & 0xf);
950 if (!(dev->int_in_endpointAddr && dev->isoc_in_endpointAddr)) {
951 UDIA_ERROR("Could not find both int-in and isoc-in endpoints");
953 kfree(dev);
955 return -ENODEV;
959 // Switch off camera
960 dev_microdia_camera_off(dev);
962 // Initialize the video device
963 dev->vdev = video_device_alloc();
965 if (!dev->vdev) {
966 kfree(dev);
967 return -ENOMEM;
970 // Initialize the camera
971 dev_microdia_initialize_device(dev);
973 // Register the video device
974 err = v4l_microdia_register_video_device(dev);
976 if (err) {
977 video_device_release(dev->vdev); // Oops release video dev
978 kfree(dev);
979 return err;
982 // Create the entries in the sys filesystem
983 microdia_create_sysfs_files(dev->vdev);
985 // Save our data pointer in this interface device
986 usb_set_intfdata(interface, dev);
988 // Default settings video device
989 usb_microdia_default_settings(dev);
992 return 0;
996 /**
997 * @param interface
999 * @brief This function is called when the device is disconnected
1000 * or when the kernel module is unloaded.
1002 static void usb_microdia_disconnect(struct usb_interface *interface)
1004 struct usb_microdia *dev = usb_get_intfdata(interface);
1006 UDIA_INFO("Microdia USB2.0 Camera disconnected\n");
1008 usb_set_intfdata(interface, NULL);
1010 // We got unplugged; this is signalled by an EPIPE error code
1011 if (dev->vopen) {
1012 UDIA_INFO("Disconnected while webcam is in use !\n");
1013 dev->error_status = EPIPE;
1016 // Alert waiting processes
1017 wake_up_interruptible(&dev->wait_frame);
1019 // Wait until device is closed
1020 while (dev->vopen)
1021 schedule();
1023 // Remove the entries in the sys filesystem
1024 microdia_remove_sysfs_files(dev->vdev);
1026 // Unregister the video device
1027 v4l_microdia_unregister_video_device(dev);
1032 * @var usb_microdia_driver
1034 * This variable contains some callback
1036 static struct usb_driver usb_microdia_driver = {
1037 .name = "usb_microdia_driver",
1038 .probe = usb_microdia_probe,
1039 .disconnect = usb_microdia_disconnect,
1040 .id_table = microdia_table,
1043 module_param(fps, int, 0444); /**< @brief Module frame per second parameter */
1044 module_param(hflip, int, 0444); /**< @brief Module horizontal flip process */
1045 module_param(vflip, int, 0444); /**< @brief Module vertical flip process */
1047 module_param(brightness, int, 0444); /**< @brief Module brightness */
1048 module_param(whiteness, int, 0444); /**< @brief Module whiteness */
1049 module_param(contrast, int, 0444); /**< @brief Module contrast */
1050 module_param(exposure, int, 0444); /**< @brief Module exposure */
1053 /**
1054 * @returns 0 if all is OK
1056 * @brief Initialize the driver.
1058 * This function is called at first.
1059 * This function permits to define the default values from the command line.
1061 static int __init usb_microdia_init(void)
1063 int result;
1066 UDIA_INFO("Microdia USB2.0 webcam driver startup\n");
1068 debug_dir = debugfs_create_dir(debug_dir_name, NULL);
1069 if(debug_dir)
1070 debug_file = debugfs_create_u8(debug_file_name, S_IRUGO | S_IWUGO,
1071 debug_dir, &debug_level);
1073 if(fps < 10 || fps > 30) {
1074 UDIA_WARNING("Framerate out of bounds [10-30]! Defaulting to 25\n");
1075 fps = 25;
1078 if(vflip != 0 && vflip != 1) {
1079 UDIA_WARNING("Vertical flip should be 0 or 1! Defaulting to 0\n");
1080 vflip = 0;
1083 if(hflip != 0 && hflip != 1) {
1084 UDIA_WARNING("Horizontal flip should be 0 or 1! Defaulting to 0\n");
1085 hflip = 0;
1088 // Register the driver with the USB subsystem
1089 result = usb_register(&usb_microdia_driver);
1091 if (result)
1092 UDIA_ERROR("usb_register failed ! Error number %d\n", result);
1094 UDIA_INFO(DRIVER_VERSION " : " DRIVER_DESC "\n");
1096 return result;
1100 /**
1101 * @brief Close the driver
1103 * This function is called at last when you unload the driver.
1105 static void __exit usb_microdia_exit(void)
1107 UDIA_INFO("usb_microdia_exit: Microdia USB2.0 webcam driver shutdown\n");
1109 if(debug_file)
1110 debugfs_remove(debug_file);
1111 if(debug_dir)
1112 debugfs_remove(debug_dir);
1114 // Deregister this driver with the USB subsystem
1115 usb_deregister(&usb_microdia_driver);
1119 module_init(usb_microdia_init); /**< @brief Module initialize */
1120 module_exit(usb_microdia_exit); /**< @brief Module exit */
1123 MODULE_PARM_DESC(fps, "Frames per second [10-30]"); /**< @brief Description of 'fps' parameter */
1124 MODULE_PARM_DESC(hflip, "Horizontal image flip"); /**< @brief Description of 'hflip' parameter */
1125 MODULE_PARM_DESC(vflip, "Vertical image flip"); /**< @brief Description of 'vflip' parameter */
1126 MODULE_PARM_DESC(brightness, "Brightness setting"); /**< @brief Description of 'brightness' parameter */
1127 MODULE_PARM_DESC(whiteness, "Whiteness setting"); /**< @brief Description of 'whiteness' parameter */
1128 MODULE_PARM_DESC(exposure, "Exposure setting"); /**< @brief Description of 'exposure' parameter */
1129 MODULE_PARM_DESC(contrast, "Contrast setting"); /**< @brief Description of 'contrast' parameter */
1132 MODULE_LICENSE("GPL"); /**< @brief Driver is under licence GPL */
1133 MODULE_AUTHOR(DRIVER_AUTHOR); /**< @brief Driver is written by Nicolas VIVIEN */
1134 MODULE_DESCRIPTION(DRIVER_DESC); /**< @brief Define the description of the driver */
1135 MODULE_SUPPORTED_DEVICE(DRIVER_SUPPORT); /**< @brief List of supported device */