Increase performance with compiler instructions
[microdia.git] / sn9c20x-usb.c
blob74c157932aa6fd45fe5dff1fec0847ff1e9e6068
1 /**
2 * @file sn9c20x-usb.c
3 * @author Nicolas VIVIEN
4 * @date 2008-02-01
6 * @brief Driver for SN9C20X USB video camera
8 * @note Copyright (C) Nicolas VIVIEN
10 * @par Licences
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/kernel.h>
30 #include <linux/version.h>
31 #include <linux/errno.h>
32 #include <linux/slab.h>
33 #include <linux/kref.h>
34 #include <linux/stat.h>
36 #include <linux/usb.h>
37 #include <media/v4l2-common.h>
39 #include "sn9c20x.h"
40 #include "sn9c20x-bridge.h"
41 #include "micron.h"
42 #include "omnivision.h"
44 /**
45 * @var fps
46 * Module parameter to set frame per second
48 static __u8 fps = 25;
50 /**
51 * @var bulk
52 * Module parameter to enable/disable bulk transfers
54 static __u8 bulk;
56 /**
57 * @var jpeg
58 * Module parameter to enable/disable JPEG format
60 __u8 jpeg = 2;
62 /**
63 * @var bandwidth
64 * Module parameter to set the available bandwidth via the alternate setting
66 static __u8 bandwidth = 8;
68 /**
69 * @var hflip
70 * Module parameter to enable/disable the horizontal flip process
72 static __u8 hflip;
74 /**
75 * @var flip_detect
76 * Module parameter to enable/disable vflip detection
78 static __u8 flip_detect;
80 /**
81 * @var vflip
82 * Module parameter to enable/disable the vertical flip process
84 static __u8 vflip;
86 /**
87 * @var brightness
88 * Module parameter to set the brightness
90 static __u16 brightness = SN9C20X_PERCENT(50, 0xFF);
92 /**
93 * @var gamma
94 * Module parameter to set the gamma
96 static __u16 gamma = SN9C20X_PERCENT(20, 0xFF);
98 /**
99 * @var gamma
100 * Module parameter to set the gamma
102 static __u16 saturation = SN9C20X_PERCENT(50, 0xFF);
105 * @var contrast
106 * Module parameter to set the contrast
108 static __u16 contrast = SN9C20X_PERCENT(50, 0xFF);
111 * @var exposure
112 * Module parameter to set the exposure
114 static __u16 exposure = SN9C20X_PERCENT(20, 0xFF);
117 * @var gain
118 * Module parameter to set the gain
120 static int gain = SN9C20X_PERCENT(20, 0xFF);
123 * @var sharpness
124 * Module parameter to set the sharpness
126 static __u16 sharpness = SN9C20X_PERCENT(50, 0x3F);
129 * @var red_gain
130 * Module parameter to set the red gain
132 static __u16 red_gain = SN9C20X_PERCENT(25, 0x7F);
135 * @var blue_gain
136 * Module parameter to set the blue gain
138 static __u16 blue_gain = SN9C20X_PERCENT(25, 0x7F);
141 * @var min_buffers
142 * Module parameter to set the minimum number of image buffers
144 static __u8 min_buffers = 2;
147 * @var max_buffers
148 * Module parameter to set the maximum number of image buffers
150 static __u8 max_buffers = 5;
153 * @var auto_exposure
154 * Module parameter to set the exposure
156 static __u8 auto_exposure = 1;
159 * @var auto_gain
160 * Module parameter to set the gain
162 static __u8 auto_gain;
166 * @var auto_whitebalance
167 * Module parameter to set the auto-whitebalance
169 static __u8 auto_whitebalance = 1;
172 * @var log_level
173 * Module parameter to set the log level
175 __u8 log_level = 5;
179 * @var sn9c20x_table
180 * Define all the hotplug supported devices by this driver
182 static struct usb_device_id sn9c20x_table[] = {
183 /* SN9C201 + MI1300 */
184 {SN9C20X_USB_DEVICE(0x0C45, 0x6240, MT9M001_SENSOR, 0x5d)},
185 /* SN9C201 + MI1310 */
186 {SN9C20X_USB_DEVICE(0x0C45, 0x6242, MT9M111_SENSOR, 0x5d)},
187 /* SN9C201 + S5K4AAFX */
188 {SN9C20X_USB_DEVICE(0x0C45, 0x6243, 0, 0)},
189 /* SN9C201 + OV9655 */
190 {SN9C20X_USB_DEVICE(0x0C45, 0x6248, OV9655_SENSOR, 0x30)},
191 /* SN9C201 + CX1332 */
192 {SN9C20X_USB_DEVICE(0x0C45, 0x624B, 0, 0)},
193 /* SN9C201 + MI1320 */
194 {SN9C20X_USB_DEVICE(0x0C45, 0x624C, 0, 0)},
195 /* SN9C201 + SOI968 */
196 {SN9C20X_USB_DEVICE(0x0C45, 0x624E, SOI968_SENSOR, 0x30)},
197 /* SN9C201 + OV9650 */
198 {SN9C20X_USB_DEVICE(0x0C45, 0x624F, OV9650_SENSOR, 0x30)},
199 /* SN9C201 + OV9650 */
200 {SN9C20X_USB_DEVICE(0x0C45, 0x6251, OV9650_SENSOR, 0x30)},
201 /* SN9C201 + OV9650 */
202 {SN9C20X_USB_DEVICE(0x0C45, 0x6253, OV9650_SENSOR, 0x30)},
203 /* SN9C201 + OV7670ISP */
204 {SN9C20X_USB_DEVICE(0x0C45, 0x6260, OV7670_SENSOR, 0x21)},
205 /* SN9C201 + OM6802 */
206 {SN9C20X_USB_DEVICE(0x0C45, 0x6262, 0, 0)},
207 /* SN9C201 + MI0360/MT9V111 */
208 {SN9C20X_USB_DEVICE(0x0C45, 0x6270, 0, 0)},
209 /* SN9C201 + S5K53BEB */
210 {SN9C20X_USB_DEVICE(0x0C45, 0x627A, 0, 0)},
211 /* SN9C201 + OV7660 */
212 {SN9C20X_USB_DEVICE(0x0C45, 0x627B, OV7660_SENSOR, 0x21)},
213 /* SN9C201 + HV7131R */
214 {SN9C20X_USB_DEVICE(0x0C45, 0x627C, HV7131R_SENSOR, 0x11)},
215 /* EEPROM */
216 {SN9C20X_USB_DEVICE(0x0C45, 0x627F, OV9650_SENSOR, 0x30)},
217 /* SN9C202 + MI1300 */
218 {SN9C20X_USB_DEVICE(0x0C45, 0x6280, MT9M001_SENSOR, 0x5d)},
219 /* SN9C202 + MI1310 */
220 {SN9C20X_USB_DEVICE(0x0C45, 0x6282, MT9M111_SENSOR, 0x5d)},
221 /* SN9C202 + S5K4AAFX */
222 {SN9C20X_USB_DEVICE(0x0C45, 0x6283, 0, 0)},
223 /* SN9C202 + OV9655 */
224 {SN9C20X_USB_DEVICE(0x0C45, 0x6288, OV9655_SENSOR, 0x30)},
225 /* SN9C202 + ICM107 */
226 {SN9C20X_USB_DEVICE(0x0C45, 0x628A, 0, 0)},
227 /* SN9C202 + CX1332 */
228 {SN9C20X_USB_DEVICE(0x0C45, 0x628B, 0, 0)},
229 /* SN9C202 + MI1320 */
230 {SN9C20X_USB_DEVICE(0x0C45, 0x628C, 0, 0)},
231 /* SN9C202 + SOI968 */
232 {SN9C20X_USB_DEVICE(0x0C45, 0x628E, SOI968_SENSOR, 0x30)},
233 /* SN9C202 + OV9650 */
234 {SN9C20X_USB_DEVICE(0x0C45, 0x628F, OV9650_SENSOR, 0x30)},
235 /* SN9C202 + OV7670ISP */
236 {SN9C20X_USB_DEVICE(0x0C45, 0x62A0, OV7670_SENSOR, 0x21)},
237 /* SN9C202 + OM6802 */
238 {SN9C20X_USB_DEVICE(0x0C45, 0x62A2, 0, 0)},
239 /* SN9C202 + MI0360/MT9V111 */
240 {SN9C20X_USB_DEVICE(0x0C45, 0x62B0, 0, 0)},
241 /* SN9C202 + OV9655 */
242 {SN9C20X_USB_DEVICE(0x0C45, 0x62B3, OV9655_SENSOR, 0x30)},
243 /* SN9C202 + S5K53BEB */
244 {SN9C20X_USB_DEVICE(0x0C45, 0x62BA, 0, 0)},
245 /* SN9C202 + OV7660 */
246 {SN9C20X_USB_DEVICE(0x0C45, 0x62BB, OV7660_SENSOR, 0x21)},
247 /* SN9C202 + HV7131R */
248 {SN9C20X_USB_DEVICE(0x0C45, 0x62BC, HV7131R_SENSOR, 0x11)},
249 /* SN9C202 + OV7663 */
250 {SN9C20X_USB_DEVICE(0x0C45, 0x62BE, 0, 0)},
251 /* => 628f (SN9C202 + OV9650) */
252 {SN9C20X_USB_DEVICE(0x045E, 0x00F4, OV9650_SENSOR, 0x30)},
253 /* => 627b (SN9C201 + OV7660) */
254 {SN9C20X_USB_DEVICE(0x145F, 0x013D, OV7660_SENSOR, 0x21)},
255 /* => 62be (SN9C202 + OV7663 + EEPROM) */
256 {SN9C20X_USB_DEVICE(0x04F2, 0xA128, 0, 0)},
257 /* => 627c (SN9C201 + HV7131R) */
258 {SN9C20X_USB_DEVICE(0x0458, 0x7029, HV7131R_SENSOR, 0x11)},
263 MODULE_DEVICE_TABLE(usb, sn9c20x_table); /**< Define the supported devices */
265 DEFINE_MUTEX(open_lock); /**< Define global mutex */
267 struct usb_endpoint_descriptor *find_endpoint(struct usb_host_interface *alts,
268 __u8 epaddr)
270 unsigned long i;
271 struct usb_endpoint_descriptor *ep;
273 for (i = 0; i < alts->desc.bNumEndpoints; ++i) {
274 ep = &alts->endpoint[i].desc;
275 if ((ep->bEndpointAddress & 0xf) == epaddr) {
276 UDIA_DEBUG("Found Endpoint 0x%X\n", epaddr);
277 return ep;
281 return NULL;
284 * @param dev Device structure
285 * @param ep Usb endpoint structure
287 * @returns 0 if all is OK
289 * @brief Initilize an isochronous pipe.
291 * This function permits to initialize an URB transfert (or isochronous pipe).
293 int usb_sn9c20x_isoc_init(struct usb_sn9c20x *dev,
294 struct usb_endpoint_descriptor *ep)
296 int i, j;
297 __u16 iso_max_frame_size;
298 struct urb *urb;
299 struct usb_device *udev;
301 udev = dev->udev;
303 UDIA_DEBUG("usb_sn9c20x_isoc_init()\n");
305 iso_max_frame_size =
306 max_packet_sz(le16_to_cpu(ep->wMaxPacketSize)) *
307 hb_multiplier(le16_to_cpu(ep->wMaxPacketSize));
308 for (i = 0; i < MAX_URBS; i++) {
309 urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
311 if (urb == NULL) {
312 UDIA_ERROR("Failed to allocate URB %d\n", i);
313 usb_sn9c20x_uninit_urbs(dev);
314 return -ENOMEM;
317 urb->interval = 1;
318 urb->dev = udev;
319 urb->pipe = usb_rcvisocpipe(udev, ep->bEndpointAddress);
320 urb->transfer_flags = URB_ISO_ASAP;
321 urb->transfer_buffer_length = iso_max_frame_size * ISO_FRAMES_PER_DESC;
322 urb->complete = usb_sn9c20x_completion_handler;
323 urb->context = dev;
324 urb->start_frame = 0;
325 urb->number_of_packets = ISO_FRAMES_PER_DESC;
327 for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
328 urb->iso_frame_desc[j].offset = j * iso_max_frame_size;
329 urb->iso_frame_desc[j].length = iso_max_frame_size;
332 dev->urbs[i].data = kzalloc(urb->transfer_buffer_length,
333 GFP_KERNEL);
334 if (dev->urbs[i].data == NULL) {
335 usb_sn9c20x_uninit_urbs(dev);
336 return -ENOMEM;
339 urb->transfer_buffer = dev->urbs[i].data;
340 dev->urbs[i].urb = urb;
343 return 0;
346 int usb_sn9c20x_bulk_init(struct usb_sn9c20x *dev,
347 struct usb_endpoint_descriptor *ep)
349 struct urb *urb;
350 unsigned int pipe, i;
351 __u16 psize;
352 __u32 size;
353 psize = max_packet_sz(le16_to_cpu(ep->wMaxPacketSize));
354 size = psize * ISO_FRAMES_PER_DESC;
355 pipe = usb_rcvbulkpipe(dev->udev, ep->bEndpointAddress);
357 for (i = 0; i < MAX_URBS; ++i) {
358 urb = usb_alloc_urb(0, GFP_KERNEL);
359 if (urb == NULL) {
360 usb_sn9c20x_uninit_urbs(dev);
361 return -ENOMEM;
364 dev->urbs[i].data = kzalloc(size, GFP_KERNEL);
366 usb_fill_bulk_urb(urb, dev->udev, pipe,
367 dev->urbs[i].data, size,
368 usb_sn9c20x_completion_handler,
369 dev);
371 dev->urbs[i].urb = urb;
374 return 0;
377 int usb_sn9c20x_init_urbs(struct usb_sn9c20x *dev)
379 int ret, i;
380 __u8 value;
381 struct usb_endpoint_descriptor *ep;
382 struct usb_interface *intf = dev->interface;
384 ret = usb_sn9c20x_control_read(dev, 0x1061, &value, 1);
385 if (ret < 0)
386 return ret;
388 if (!bulk) {
389 if (bandwidth > 8)
390 return -EINVAL;
392 ep = find_endpoint(usb_altnum_to_altsetting(intf, bandwidth), SN9C20X_VID_ISOC);
393 if (ep == NULL)
394 return -EIO;
396 ret = usb_set_interface(dev->udev, 0, bandwidth);
397 if (ret < 0)
398 return ret;
400 value |= 0x01;
401 ret = usb_sn9c20x_control_write(dev, 0x1061, &value, 1);
402 if (ret < 0)
403 return ret;
405 ret = usb_sn9c20x_isoc_init(dev, ep);
406 } else {
407 ep = find_endpoint(usb_altnum_to_altsetting(intf, 0), SN9C20X_BULK);
408 if (ep == NULL)
409 return -EIO;
411 ret = usb_set_interface(dev->udev, 0, 0);
412 if (ret < 0)
413 return ret;
415 value &= ~0x01;
416 ret = usb_sn9c20x_control_write(dev, 0x1061, &value, 1);
417 if (ret < 0)
418 return ret;
420 ret = usb_sn9c20x_bulk_init(dev, ep);
423 if (ret < 0)
424 return ret;
426 for (i = 0; i < MAX_URBS; i++) {
427 ret = usb_submit_urb(dev->urbs[i].urb, GFP_KERNEL);
428 if (ret)
429 UDIA_ERROR("isoc_init() submit_urb %d failed with error %d\n", i, ret);
432 return 0;
436 * @param dev Device structure
438 * @brief Clean-up all the ISOC buffers
440 * This function permits to clean-up all the ISOC buffers.
442 void usb_sn9c20x_uninit_urbs(struct usb_sn9c20x *dev)
444 int i;
445 struct urb *urb;
447 UDIA_DEBUG("Isoc cleanup\n");
449 if (dev == NULL)
450 return;
452 for (i = 0; i < MAX_URBS; i++) {
453 urb = dev->urbs[i].urb;
454 if (urb == NULL)
455 continue;
456 usb_kill_urb(urb);
457 kfree(dev->urbs[i].data);
458 usb_free_urb(urb);
459 dev->urbs[i].urb = NULL;
463 int usb_sn9c20x_detect_frame(unsigned char *buf, unsigned int buf_length)
465 static unsigned char frame_header[] = {0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96};
466 int index;
467 if (buf_length < 64)
468 return -1;
469 if (buf_length == 64) {
470 if (memcmp(buf, frame_header, 6) == 0)
471 return 0;
472 else
473 return -1;
475 for (index = 0; index < buf_length - 63; index++) {
476 if (memcmp(buf + index, frame_header, 6) == 0) {
477 UDIA_DEBUG("Found Header at %d\n", index);
478 return index;
481 return -1;
484 void usb_sn9c20x_assemble_video(struct usb_sn9c20x *dev,
485 unsigned char *transfer, unsigned int transfer_length,
486 struct sn9c20x_buffer **buffer)
488 void *mem = NULL;
489 unsigned char *header;
490 int header_index;
491 int yavg;
492 struct sn9c20x_buffer *buf = *buffer;
493 struct sn9c20x_video_queue *queue = &dev->queue;
495 if (buf->state != SN9C20X_BUF_STATE_ACTIVE)
496 buf->state = SN9C20X_BUF_STATE_ACTIVE;
498 header_index = usb_sn9c20x_detect_frame(transfer, transfer_length);
499 if (header_index >= 0) {
500 if (header_index + buf->buf.bytesused > buf->buf.length) {
501 UDIA_WARNING("Frame Buffer overflow!\n");
502 dev->vframes_overflow++;
503 buf->state = SN9C20X_BUF_STATE_DONE;
505 header_index = min(buf->buf.length - buf->buf.bytesused,
506 (unsigned int)header_index);
507 mem = queue->mem + buf->buf.m.offset + buf->buf.bytesused;
508 memcpy(mem, transfer, header_index);
509 buf->buf.bytesused += header_index;
510 header = transfer+header_index;
511 /* UDIA_INFO("color window: %dx%d\n",
512 header[0x3a] << 4,
513 header[0x3b] << 3);
514 UDIA_INFO("Frame Size: %d\n", buf->buf.bytesused);*/
515 yavg = ((header[35] >> 2) & 3)|(header[20] << 2)|(header[19] << 10);
516 yavg += ((header[35] >> 4) & 3)|(header[22] << 2)|(header[21] << 10);
517 yavg += ((header[35] >> 6) & 3)|(header[24] << 2)|(header[23] << 10);
518 yavg += (header[36] & 3)|(header[26] << 2)|(header[25] << 10);
519 yavg += ((header[36] >> 2) & 3)|(header[28] << 2)|(header[27] << 10);
520 yavg += ((header[36] >> 4) & 3)|(header[30] << 2)|(header[29] << 10);
521 yavg += ((header[36] >> 6) & 3)|(header[32] << 2)|(header[31] << 10);
522 yavg += ((header[44] >> 4) & 3)|(header[34] << 2)|(header[33] << 10);
523 UDIA_DEBUG("AVGY Total: %d (%d)\n", yavg, yavg >> 9);
524 yavg >>= 9;
525 atomic_set(&dev->camera.yavg, yavg);
527 if (buf->buf.bytesused != 0)
528 buf->state = SN9C20X_BUF_STATE_DONE;
529 } else {
530 if (transfer_length + buf->buf.bytesused > buf->buf.length) {
531 UDIA_WARNING("Frame Buffer overflow!\n");
532 dev->vframes_overflow++;
533 buf->state = SN9C20X_BUF_STATE_DONE;
535 transfer_length = min(buf->buf.length - buf->buf.bytesused,
536 transfer_length);
537 mem = queue->mem + buf->buf.m.offset + buf->buf.bytesused;
538 memcpy(mem, transfer, transfer_length);
539 buf->buf.bytesused += transfer_length;
541 if (buf->state == SN9C20X_BUF_STATE_DONE ||
542 buf->state == SN9C20X_BUF_STATE_ERROR) {
543 buf = sn9c20x_queue_next_buffer(queue, buf);
544 *buffer = buf;
545 if (buf == NULL) {
546 dev->vframes_dropped++;
547 } else {
548 if (header_index + 64 < transfer_length) {
549 memcpy(queue->mem + buf->buf.m.offset,
550 transfer + header_index + 64,
551 transfer_length - (header_index + 64));
552 buf->buf.bytesused +=
553 transfer_length - (header_index + 64);
559 * @param urb URB structure
561 * @brief ISOC handler
563 * This function is called as an URB transfert is complete (Isochronous pipe).
564 * So, the traitement is done in interrupt time, so it has be fast, not crash,
565 * and not stall. Neat.
567 void usb_sn9c20x_completion_handler(struct urb *urb)
569 int i;
570 int ret;
571 unsigned long flags;
573 unsigned char *transfer = NULL;
574 unsigned int transfer_length;
576 struct sn9c20x_buffer *buf = NULL;
577 struct usb_sn9c20x *dev = urb->context;
578 struct sn9c20x_video_queue *queue = &dev->queue;
580 UDIA_STREAM("Isoc handler\n");
582 switch (urb->status) {
583 case 0:
584 break;
586 default:
587 UDIA_WARNING("Non-zero status (%d) in video "
588 "completion handler.\n", urb->status);
590 case -ENOENT: /* usb_kill_urb() called. */
591 if (dev->frozen)
592 return;
594 case -ECONNRESET: /* usb_unlink_urb() called. */
595 case -ESHUTDOWN: /* The endpoint is being disabled. */
596 sn9c20x_queue_cancel(queue, urb->status == -ESHUTDOWN);
597 return;
600 spin_lock_irqsave(&queue->irqlock, flags);
601 if (!list_empty(&queue->irqqueue))
602 buf = list_first_entry(&queue->irqqueue, struct sn9c20x_buffer,
603 queue);
604 spin_unlock_irqrestore(&queue->irqlock, flags);
605 if (!bulk) {
606 for (i = 0; i < urb->number_of_packets; i++) {
607 if (urb->iso_frame_desc[i].status != 0) {
608 /*UDIA_ERROR("Iso frame %d of USB has error %d\n",
609 i, urb->iso_frame_desc[i].status);*/
610 mdelay(1);
611 continue;
613 transfer_length = urb->iso_frame_desc[i].actual_length;
614 transfer = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
615 if (buf == NULL)
616 continue;
618 usb_sn9c20x_assemble_video(dev, transfer, transfer_length, &buf);
620 } else {
621 if (buf != NULL) {
622 usb_sn9c20x_assemble_video(dev, urb->transfer_buffer,
623 urb->actual_length, &buf);
626 ret = usb_submit_urb(urb, GFP_ATOMIC);
628 if (ret != 0) {
629 UDIA_ERROR("Error (%d) re-submitting urb in "
630 "sn9c20x_isoc_handler.\n", ret);
635 * @param dev Device structure
636 * @param value register to write to
637 * @param data
638 * @param length number of bytes
640 * @returns 0 if all is OK
642 * @brief Write a 16-bit value to a 16-bit register
644 * This function permits to write a 16-bit value to a 16-bit register on the USB bus.
646 int usb_sn9c20x_control_write(struct usb_sn9c20x *dev, __u16 value, __u8 *data, __u16 length)
648 int result;
649 struct usb_device *udev = dev->udev;
651 result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
652 0x08,
653 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
654 value,
655 0x00,
656 data,
657 length,
658 500);
660 if (unlikely(result < 0))
661 UDIA_ERROR("Write register failed index = 0x%02X\n", value);
663 return result;
667 * @param dev Device structure
668 * @param index register to read from
669 * @param data
670 * @param length number of bytes
672 * @returns 0 if all is OK
674 * @brief Read a 16-bit value from a 16-bit register
676 * This function permits to read a 16-bit value from a 16-bit register on the USB bus.
678 int usb_sn9c20x_control_read(struct usb_sn9c20x *dev, __u16 index, __u8 *data, __u16 length)
680 int result;
682 struct usb_device *udev = dev->udev;
684 *data = 0;
686 result = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
687 0x00,
688 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
689 index,
690 0x00,
691 data,
692 length,
693 500);
695 if (unlikely(result < 0))
696 UDIA_ERROR("Read register failed 0x%02X\n", index);
698 return result;
703 * @param dev
705 * @returns 0 if all is OK
707 * @brief Set the default value about the video settings.
709 * This function permits to set the video settings for each video camera model.
712 static int usb_sn9c20x_default_settings(struct usb_sn9c20x *dev)
714 dev->vframes_overflow = 0;
715 dev->vframes_incomplete = 0;
716 dev->vframes_dropped = 0;
718 dev->queue.min_buffers = min_buffers;
719 dev->queue.max_buffers = max_buffers;
721 dev->vsettings.fps = fps;
723 v4l2_set_control_default(dev, V4L2_CID_HFLIP, hflip);
724 v4l2_set_control_default(dev, V4L2_CID_VFLIP, vflip);
725 v4l2_set_control_default(dev, V4L2_CID_GAIN, gain);
726 v4l2_set_control_default(dev, V4L2_CID_BRIGHTNESS, brightness);
727 v4l2_set_control_default(dev, V4L2_CID_CONTRAST, contrast);
728 v4l2_set_control_default(dev, V4L2_CID_GAMMA, gamma);
729 v4l2_set_control_default(dev, V4L2_CID_SATURATION, saturation);
730 v4l2_set_control_default(dev, V4L2_CID_SHARPNESS, sharpness);
731 v4l2_set_control_default(dev, V4L2_CID_RED_BALANCE, red_gain);
732 v4l2_set_control_default(dev, V4L2_CID_BLUE_BALANCE, blue_gain);
733 v4l2_set_control_default(dev, V4L2_CID_EXPOSURE_AUTO, auto_exposure);
734 v4l2_set_control_default(dev, V4L2_CID_AUTOGAIN, auto_gain);
735 v4l2_set_control_default(dev, V4L2_CID_AUTO_WHITE_BALANCE, auto_whitebalance);
736 v4l2_set_control_default(dev, V4L2_CID_EXPOSURE, exposure);
738 if (jpeg == 2) {
739 if (dev->udev->speed == USB_SPEED_HIGH && bandwidth == 8)
740 dev->jpeg = 0;
741 else
742 dev->jpeg = 1;
743 } else
744 dev->jpeg = jpeg;
746 sn9c20x_set_resolution(dev, 640, 480);
747 sn9c20x_set_format(dev, sn9c20x_fmts[0].pix_fmt);
749 return 0;
753 * @brief Load the driver
755 * @param interface
756 * @param id
758 * @returns 0 if all is OK
760 * This function detects the device and allocate the buffers for the device
761 * and the video interface.
763 static int usb_sn9c20x_probe(struct usb_interface *interface, const struct usb_device_id *id)
765 int ret;
767 int vendor_id;
768 int product_id;
769 int bNumInterfaces;
771 struct usb_sn9c20x *dev = NULL;
772 struct usb_device *udev = interface_to_usbdev(interface);
774 /* Get USB VendorID and ProductID */
775 vendor_id = le16_to_cpu(udev->descriptor.idVendor);
776 product_id = le16_to_cpu(udev->descriptor.idProduct);
778 /* Check if we can handle this device */
779 UDIA_DEBUG("Probe function called with VendorID=%04X, ProductID=%04X and InterfaceNumber=%d\n",
780 vendor_id, product_id, interface->cur_altsetting->desc.bInterfaceNumber);
782 UDIA_INFO("SN9C20X USB 2.0 Webcam - %04X:%04X plugged-in.\n",
783 vendor_id, product_id);
787 // Allocate structure, initialize pointers, mutexes, etc.
788 // and link it to the usb_device
790 dev = kzalloc(sizeof(struct usb_sn9c20x), GFP_KERNEL);
792 if (dev == NULL) {
793 UDIA_ERROR("Out of memory !\n");
794 ret = -ENOMEM;
795 goto error;
798 /* Init mutexes, spinlock, etc. */
799 mutex_init(&dev->mutex);
800 kref_init(&dev->vopen);
802 dev->udev = udev;
803 dev->interface = interface;
805 /* Read the product release */
806 dev->release = le16_to_cpu(udev->descriptor.bcdDevice);
807 UDIA_DEBUG("Release: %04x\n", dev->release);
809 /* How many interfaces (1 or 3) ? */
810 bNumInterfaces = udev->config->desc.bNumInterfaces;
811 UDIA_DEBUG("Number of interfaces : %d\n", bNumInterfaces);
813 dev->camera.sensor = id->driver_info & 0xFF;
814 dev->camera.address = (id->driver_info >> 8) & 0xFF;
816 /* Initialize the camera */
817 ret = sn9c20x_initialize(dev);
818 if (ret < 0)
819 goto error;
821 /* Initialize the video device */
822 dev->vdev = video_device_alloc();
824 if (!dev->vdev) {
825 ret = -ENOMEM;
826 goto free_dev;
829 /* Register the video device */
830 ret = v4l_sn9c20x_register_video_device(dev);
832 if (ret)
833 goto free_dev;
835 /* Create the entries in the sys filesystem */
836 sn9c20x_create_sysfs_files(dev->vdev);
838 sn9c20x_create_debugfs_files(dev);
840 /* Save our data pointer in this interface device */
841 usb_set_intfdata(interface, dev);
843 usb_sn9c20x_default_settings(dev);
845 return 0;
847 free_dev:
848 kref_put(&dev->vopen, usb_sn9c20x_delete);
849 error:
850 return ret;
853 void usb_sn9c20x_delete(struct kref *kref)
855 struct usb_sn9c20x *dev;
856 dev = container_of(kref, struct usb_sn9c20x, vopen);
858 if (dev->vdev != NULL) {
859 sn9c20x_remove_sysfs_files(dev->vdev);
860 sn9c20x_remove_debugfs_files(dev);
861 v4l_sn9c20x_unregister_video_device(dev);
863 kfree(dev);
867 * @param interface
869 * @brief This function is called when the device is disconnected
870 * or when the kernel module is unloaded.
872 static void usb_sn9c20x_disconnect(struct usb_interface *interface)
874 struct usb_sn9c20x *dev = usb_get_intfdata(interface);
876 UDIA_INFO("SN9C20X USB 2.0 Webcam unplugged\n");
878 usb_set_intfdata(interface, NULL);
880 mutex_lock(&open_lock);
881 kref_put(&dev->vopen, usb_sn9c20x_delete);
882 mutex_unlock(&open_lock);
887 * @var usb_sn9c20x_driver
889 * This variable contains some callback
891 static struct usb_driver usb_sn9c20x_driver = {
892 .name = "sn9c20x",
893 .probe = usb_sn9c20x_probe,
894 .disconnect = usb_sn9c20x_disconnect,
895 .id_table = sn9c20x_table,
898 module_param(fps, byte, 0444); /**< @brief Module parameter frames per second */
899 module_param(bulk, byte, 0444);
900 module_param(jpeg, byte, 0444);
901 module_param(bandwidth, byte, 0444);
902 module_param(hflip, byte, 0444); /**< @brief Module parameter horizontal flip process */
903 module_param(vflip, byte, 0444); /**< @brief Module parameter vertical flip process */
904 module_param(flip_detect, byte, 0444); /**< @brief Module parameter flip detect */
905 module_param(auto_exposure, byte, 0444); /**< @brief Module parameter automatic exposure control */
906 module_param(auto_gain, byte, 0444); /**< @brief Module parameter automatic gain control */
907 module_param(auto_whitebalance, byte, 0444); /**< @brief Module parameter automatic whitebalance control */
908 module_param(brightness, ushort, 0444); /**< @brief Module parameter brightness */
909 module_param(gamma, ushort, 0444); /**< @brief Module parameter gamma */
910 module_param(saturation, ushort, 0444); /**< @brief Module parameter saturation */
911 module_param(contrast, ushort, 0444); /**< @brief Module parameter contrast */
912 module_param(exposure, ushort, 0444); /**< @brief Module parameter exposure */
913 module_param(sharpness, ushort, 0444); /**< @brief Module parameter sharpness */
914 module_param(red_gain, ushort, 0444); /**< @brief Module parameter red gain */
915 module_param(blue_gain, ushort, 0444); /**< @brief Module parameter blue gain */
917 module_param(min_buffers, byte, 0444);
918 module_param(max_buffers, byte, 0444);
920 module_param(log_level, byte, 0444);
923 * @returns 0 if all is OK
925 * @brief Initialize the driver.
927 * This function is called at first.
928 * This function permits to define the default values from the command line.
930 static int __init usb_sn9c20x_init(void)
932 int result;
934 UDIA_DEBUG(DRIVER_DESC " initializing\n");
936 sn9c20x_init_debugfs();
938 if (fps < 10 || fps > 30) {
939 UDIA_WARNING("Framerate out of bounds [10-30]! Defaulting to 25\n");
940 fps = 25;
943 if (bandwidth < 1 || bandwidth > 8) {
944 UDIA_WARNING("Bandwidth out of bounds [1-8]! Defaulting to 8\n");
945 bandwidth = 8;
948 if (bulk != 0 && bulk != 1) {
949 UDIA_WARNING("Bulk transfer should be 0 or 1! Defaulting to 0\n");
950 bulk = 0;
953 if (jpeg > 2) {
954 UDIA_WARNING("JPEG should be 0 or 1 or 2! Defaulting to 2\n");
955 jpeg = 2;
958 if (vflip != 0 && vflip != 1) {
959 UDIA_WARNING("Vertical flip should be 0 or 1! Defaulting to 0\n");
960 vflip = 0;
963 if (hflip != 0 && hflip != 1) {
964 UDIA_WARNING("Horizontal flip should be 0 or 1! Defaulting to 0\n");
965 hflip = 0;
968 if (sharpness > 0x3f) {
969 UDIA_WARNING("Sharpness should be 0 to 63 ! Defaulting to 31\n");
970 sharpness = 0x1f;
973 if (red_gain > 0x7f) {
974 UDIA_WARNING("Red Gain should be 0 to 127 ! Defaulting to 31\n");
975 red_gain = 31;
978 if (blue_gain > 0x7f) {
979 UDIA_WARNING("Blue Gain should be 0 to 127 ! Defaulting to 31\n");
980 blue_gain = 31;
983 if (auto_exposure != 0 && auto_exposure != 1) {
984 UDIA_WARNING("Automatic exposure should be 0 or 1! "
985 "Defaulting to 0\n");
986 auto_exposure = 1;
989 if (auto_gain != 0 && auto_gain != 1) {
990 UDIA_WARNING("Automatic gain should be 0 or 1! "
991 "Defaulting to 0\n");
992 auto_gain = 0;
995 if (auto_whitebalance != 0 && auto_whitebalance != 1) {
996 UDIA_WARNING("Automatic whitebalance should be 0 or 1! "
997 "Defaulting to 1\n");
998 auto_whitebalance = 1;
1001 if (min_buffers < 2) {
1002 UDIA_WARNING("Minimum buffers can't be less then 2! "
1003 "Defaulting to 2\n");
1004 min_buffers = 2;
1008 if (min_buffers > max_buffers) {
1009 UDIA_WARNING("Minimum buffers must be less then or equal to "
1010 "max buffers! Defaulting to 2, 10\n");
1011 min_buffers = 2;
1012 max_buffers = 5;
1015 /* Register the driver with the USB subsystem */
1016 result = usb_register(&usb_sn9c20x_driver);
1018 if (result)
1019 UDIA_ERROR("usb_register failed ! Error number %d\n", result);
1021 UDIA_INFO(DRIVER_DESC " " DRIVER_VERSION " loaded\n");
1023 return result;
1028 * @brief Close the driver
1030 * This function is called at last when you unload the driver.
1032 static void __exit usb_sn9c20x_exit(void)
1034 UDIA_INFO("usb_sn9c20x_exit: SN9C20X USB 2.0 webcam driver unloaded\n");
1036 sn9c20x_uninit_debugfs();
1038 /* Deregister this driver with the USB subsystem */
1039 usb_deregister(&usb_sn9c20x_driver);
1043 module_init(usb_sn9c20x_init); /**< @brief Module initialize */
1044 module_exit(usb_sn9c20x_exit); /**< @brief Module exit */
1047 MODULE_PARM_DESC(fps, "Frames per second [10-30]"); /**< @brief Description of 'fps' parameter */
1048 MODULE_PARM_DESC(jpeg, "Enable JPEG support (default is auto-detect)");
1049 MODULE_PARM_DESC(bulk, "Enable Bulk transfer (default is to use ISOC)");
1050 MODULE_PARM_DESC(bandwidth, "Bandwidth Setting (only for ISOC)");
1051 MODULE_PARM_DESC(hflip, "Horizontal image flip"); /**< @brief Description of 'hflip' parameter */
1052 MODULE_PARM_DESC(vflip, "Vertical image flip"); /**< @brief Description of 'vflip' parameter */
1053 MODULE_PARM_DESC(flip_detect, "Image flip detection"); /**< @brief Description of 'vflip_detect' parameter */
1054 MODULE_PARM_DESC(auto_exposure, "Automatic exposure control"); /**< @brief Description of 'auto_exposure' parameter */
1055 MODULE_PARM_DESC(auto_gain, "Automatic gain control"); /**< @brief Description of 'auto_gain' parameter */
1056 MODULE_PARM_DESC(auto_whitebalance, "Automatic whitebalance"); /**< @brief Description of 'auto_whitebalance' parameter */
1057 MODULE_PARM_DESC(brightness, "Brightness setting"); /**< @brief Description of 'brightness' parameter */
1058 MODULE_PARM_DESC(gamma, "Gamma setting"); /**< @brief Description of 'gamma' parameter */
1059 MODULE_PARM_DESC(saturation, "Saturation setting"); /**< @brief Description of 'saturation' parameter */
1060 MODULE_PARM_DESC(exposure, "Exposure setting"); /**< @brief Description of 'exposure' parameter */
1061 MODULE_PARM_DESC(gain, "Gain setting"); /**< @brief Description of 'gain' parameter */
1062 MODULE_PARM_DESC(contrast, "Contrast setting"); /**< @brief Description of 'contrast' parameter */
1063 MODULE_PARM_DESC(sharpness, "Sharpness setting"); /**< @brief Description of 'sharpness' parameter */
1064 MODULE_PARM_DESC(red_gain, "Red Gain setting"); /**< @brief Description of 'Red Gain' parameter */
1065 MODULE_PARM_DESC(blue_gain, "Blue Gain setting"); /**< @brief Description of 'Blue Gain' parameter */
1067 MODULE_PARM_DESC(min_buffers, "Minimum number of image buffers");
1068 MODULE_PARM_DESC(max_buffers, "Maximum number of image buffers");
1069 MODULE_PARM_DESC(log_level, " <n>\n"
1070 "Driver log level\n"
1071 "1 = info (default)\n"
1072 "2 = warning\n"
1073 "4 = error\n"
1074 "8 = debug\n"
1075 "16 = stream\n");
1077 MODULE_LICENSE("GPL"); /**< @brief Driver is under licence GPL */
1078 MODULE_AUTHOR(DRIVER_AUTHOR); /**< @brief Driver is written by Nicolas VIVIEN */
1079 MODULE_DESCRIPTION(DRIVER_DESC); /**< @brief Define the description of the driver */
1080 MODULE_SUPPORTED_DEVICE(DRIVER_SUPPORT); /**< @brief List of supported device */