Revert saturation control patches
[microdia.git] / sn9c20x-usb.c
blob17d8c2efac7e14ddd13fd2be64051bd3f85a541e
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 contrast
100 * Module parameter to set the contrast
102 static __u16 contrast = SN9C20X_PERCENT(50, 0xFF);
105 * @var exposure
106 * Module parameter to set the exposure
108 static __u16 exposure = SN9C20X_PERCENT(20, 0xFF);
111 * @var gain
112 * Module parameter to set the gain
114 static __u16 gain = SN9C20X_PERCENT(20, 0xFF);
117 * @var sharpness
118 * Module parameter to set the sharpness
120 static __u16 sharpness = SN9C20X_PERCENT(50, 0x3F);
123 * @var red_gain
124 * Module parameter to set the red gain
126 static __u16 red_gain = SN9C20X_PERCENT(25, 0x7F);
129 * @var blue_gain
130 * Module parameter to set the blue gain
132 static __u16 blue_gain = SN9C20X_PERCENT(25, 0x7F);
135 * @var min_buffers
136 * Module parameter to set the minimum number of image buffers
138 static __u8 min_buffers = 2;
141 * @var max_buffers
142 * Module parameter to set the maximum number of image buffers
144 static __u8 max_buffers = 5;
147 * @var auto_exposure
148 * Module parameter to set the exposure
150 static __u8 auto_exposure = 1;
153 * @var auto_gain
154 * Module parameter to set the gain
156 static __u8 auto_gain;
160 * @var auto_whitebalance
161 * Module parameter to set the auto-whitebalance
163 static __u8 auto_whitebalance = 1;
166 * @var log_level
167 * Module parameter to set the log level
169 __u8 log_level = 5;
173 * @var sn9c20x_table
174 * Define all the hotplug supported devices by this driver
176 static struct usb_device_id sn9c20x_table[] = {
177 /* SN9C201 + MI1300 */
178 {SN9C20X_USB_DEVICE(0x0C45, 0x6240, MT9M001_SENSOR, 0x5d)},
179 /* SN9C201 + MI1310 */
180 {SN9C20X_USB_DEVICE(0x0C45, 0x6242, MT9M111_SENSOR, 0x5d)},
181 /* SN9C201 + S5K4AAFX */
182 {SN9C20X_USB_DEVICE(0x0C45, 0x6243, 0, 0)},
183 /* SN9C201 + OV9655 */
184 {SN9C20X_USB_DEVICE(0x0C45, 0x6248, OV9655_SENSOR, 0x30)},
185 /* SN9C201 + CX1332 */
186 {SN9C20X_USB_DEVICE(0x0C45, 0x624B, 0, 0)},
187 /* SN9C201 + MI1320 */
188 {SN9C20X_USB_DEVICE(0x0C45, 0x624C, 0, 0)},
189 /* SN9C201 + SOI968 */
190 {SN9C20X_USB_DEVICE(0x0C45, 0x624E, SOI968_SENSOR, 0x30)},
191 /* SN9C201 + OV9650 */
192 {SN9C20X_USB_DEVICE(0x0C45, 0x624F, OV9650_SENSOR, 0x30)},
193 /* SN9C201 + OV9650 */
194 {SN9C20X_USB_DEVICE(0x0C45, 0x6251, OV9650_SENSOR, 0x30)},
195 /* SN9C201 + OV9650 */
196 {SN9C20X_USB_DEVICE(0x0C45, 0x6253, OV9650_SENSOR, 0x30)},
197 /* SN9C201 + OV7670ISP */
198 {SN9C20X_USB_DEVICE(0x0C45, 0x6260, OV7670_SENSOR, 0x21)},
199 /* SN9C201 + OM6802 */
200 {SN9C20X_USB_DEVICE(0x0C45, 0x6262, 0, 0)},
201 /* SN9C201 + MI0360/MT9V111 */
202 {SN9C20X_USB_DEVICE(0x0C45, 0x6270, 0, 0)},
203 /* SN9C201 + S5K53BEB */
204 {SN9C20X_USB_DEVICE(0x0C45, 0x627A, 0, 0)},
205 /* SN9C201 + OV7660 */
206 {SN9C20X_USB_DEVICE(0x0C45, 0x627B, OV7660_SENSOR, 0x21)},
207 /* SN9C201 + HV7131R */
208 {SN9C20X_USB_DEVICE(0x0C45, 0x627C, HV7131R_SENSOR, 0x11)},
209 /* EEPROM */
210 {SN9C20X_USB_DEVICE(0x0C45, 0x627F, OV9650_SENSOR, 0x30)},
211 /* SN9C202 + MI1300 */
212 {SN9C20X_USB_DEVICE(0x0C45, 0x6280, MT9M001_SENSOR, 0x5d)},
213 /* SN9C202 + MI1310 */
214 {SN9C20X_USB_DEVICE(0x0C45, 0x6282, MT9M111_SENSOR, 0x5d)},
215 /* SN9C202 + S5K4AAFX */
216 {SN9C20X_USB_DEVICE(0x0C45, 0x6283, 0, 0)},
217 /* SN9C202 + OV9655 */
218 {SN9C20X_USB_DEVICE(0x0C45, 0x6288, OV9655_SENSOR, 0x30)},
219 /* SN9C202 + ICM107 */
220 {SN9C20X_USB_DEVICE(0x0C45, 0x628A, 0, 0)},
221 /* SN9C202 + CX1332 */
222 {SN9C20X_USB_DEVICE(0x0C45, 0x628B, 0, 0)},
223 /* SN9C202 + MI1320 */
224 {SN9C20X_USB_DEVICE(0x0C45, 0x628C, 0, 0)},
225 /* SN9C202 + SOI968 */
226 {SN9C20X_USB_DEVICE(0x0C45, 0x628E, SOI968_SENSOR, 0x30)},
227 /* SN9C202 + OV9650 */
228 {SN9C20X_USB_DEVICE(0x0C45, 0x628F, OV9650_SENSOR, 0x30)},
229 /* SN9C202 + OV7670ISP */
230 {SN9C20X_USB_DEVICE(0x0C45, 0x62A0, OV7670_SENSOR, 0x21)},
231 /* SN9C202 + OM6802 */
232 {SN9C20X_USB_DEVICE(0x0C45, 0x62A2, 0, 0)},
233 /* SN9C202 + MI0360/MT9V111 */
234 {SN9C20X_USB_DEVICE(0x0C45, 0x62B0, 0, 0)},
235 /* SN9C202 + OV9655 */
236 {SN9C20X_USB_DEVICE(0x0C45, 0x62B3, OV9655_SENSOR, 0x30)},
237 /* SN9C202 + S5K53BEB */
238 {SN9C20X_USB_DEVICE(0x0C45, 0x62BA, 0, 0)},
239 /* SN9C202 + OV7660 */
240 {SN9C20X_USB_DEVICE(0x0C45, 0x62BB, OV7660_SENSOR, 0x21)},
241 /* SN9C202 + HV7131R */
242 {SN9C20X_USB_DEVICE(0x0C45, 0x62BC, HV7131R_SENSOR, 0x11)},
243 /* SN9C202 + OV7663 */
244 {SN9C20X_USB_DEVICE(0x0C45, 0x62BE, 0, 0)},
245 /* => 628f (SN9C202 + OV9650) */
246 {SN9C20X_USB_DEVICE(0x045E, 0x00F4, OV9650_SENSOR, 0x30)},
247 /* => 627b (SN9C201 + OV7660) */
248 {SN9C20X_USB_DEVICE(0x145F, 0x013D, OV7660_SENSOR, 0x21)},
249 /* => 62be (SN9C202 + OV7663 + EEPROM) */
250 {SN9C20X_USB_DEVICE(0x04F2, 0xA128, 0, 0)},
251 /* => 627c (SN9C201 + HV7131R) */
252 {SN9C20X_USB_DEVICE(0x0458, 0x7029, HV7131R_SENSOR, 0x11)},
257 MODULE_DEVICE_TABLE(usb, sn9c20x_table); /**< Define the supported devices */
259 DEFINE_MUTEX(open_lock); /**< Define global mutex */
261 struct usb_endpoint_descriptor *find_endpoint(struct usb_host_interface *alts,
262 __u8 epaddr)
264 unsigned long i;
265 struct usb_endpoint_descriptor *ep;
267 for (i = 0; i < alts->desc.bNumEndpoints; ++i) {
268 ep = &alts->endpoint[i].desc;
269 if ((ep->bEndpointAddress & 0xf) == epaddr) {
270 UDIA_DEBUG("Found Endpoint 0x%X\n", epaddr);
271 return ep;
275 return NULL;
278 * @param dev Device structure
279 * @param ep Usb endpoint structure
281 * @returns 0 if all is OK
283 * @brief Initilize an isochronous pipe.
285 * This function permits to initialize an URB transfert (or isochronous pipe).
287 int usb_sn9c20x_isoc_init(struct usb_sn9c20x *dev,
288 struct usb_endpoint_descriptor *ep)
290 int i, j;
291 __u16 iso_max_frame_size;
292 struct urb *urb;
293 struct usb_device *udev;
295 udev = dev->udev;
297 UDIA_DEBUG("usb_sn9c20x_isoc_init()\n");
299 iso_max_frame_size =
300 max_packet_sz(le16_to_cpu(ep->wMaxPacketSize)) *
301 hb_multiplier(le16_to_cpu(ep->wMaxPacketSize));
302 for (i = 0; i < MAX_URBS; i++) {
303 urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
305 if (urb == NULL) {
306 UDIA_ERROR("Failed to allocate URB %d\n", i);
307 usb_sn9c20x_uninit_urbs(dev);
308 return -ENOMEM;
311 urb->interval = 1;
312 urb->dev = udev;
313 urb->pipe = usb_rcvisocpipe(udev, ep->bEndpointAddress);
314 urb->transfer_flags = URB_ISO_ASAP;
315 urb->transfer_buffer_length = iso_max_frame_size * ISO_FRAMES_PER_DESC;
316 urb->complete = usb_sn9c20x_completion_handler;
317 urb->context = dev;
318 urb->start_frame = 0;
319 urb->number_of_packets = ISO_FRAMES_PER_DESC;
321 for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
322 urb->iso_frame_desc[j].offset = j * iso_max_frame_size;
323 urb->iso_frame_desc[j].length = iso_max_frame_size;
326 dev->urbs[i].data = kzalloc(urb->transfer_buffer_length,
327 GFP_KERNEL);
328 if (dev->urbs[i].data == NULL) {
329 usb_sn9c20x_uninit_urbs(dev);
330 return -ENOMEM;
333 urb->transfer_buffer = dev->urbs[i].data;
334 dev->urbs[i].urb = urb;
337 return 0;
340 int usb_sn9c20x_bulk_init(struct usb_sn9c20x *dev,
341 struct usb_endpoint_descriptor *ep)
343 struct urb *urb;
344 unsigned int pipe, i;
345 __u16 psize;
346 __u32 size;
347 psize = max_packet_sz(le16_to_cpu(ep->wMaxPacketSize));
348 size = psize * ISO_FRAMES_PER_DESC;
349 pipe = usb_rcvbulkpipe(dev->udev, ep->bEndpointAddress);
351 for (i = 0; i < MAX_URBS; ++i) {
352 urb = usb_alloc_urb(0, GFP_KERNEL);
353 if (urb == NULL) {
354 usb_sn9c20x_uninit_urbs(dev);
355 return -ENOMEM;
358 dev->urbs[i].data = kzalloc(size, GFP_KERNEL);
360 usb_fill_bulk_urb(urb, dev->udev, pipe,
361 dev->urbs[i].data, size,
362 usb_sn9c20x_completion_handler,
363 dev);
365 dev->urbs[i].urb = urb;
368 return 0;
371 int usb_sn9c20x_init_urbs(struct usb_sn9c20x *dev)
373 int ret, i;
374 __u8 value;
375 struct usb_endpoint_descriptor *ep;
376 struct usb_interface *intf = dev->interface;
378 ret = usb_sn9c20x_control_read(dev, 0x1061, &value, 1);
379 if (ret < 0)
380 return ret;
382 if (!bulk) {
383 if (bandwidth > 8)
384 return -EINVAL;
386 ep = find_endpoint(usb_altnum_to_altsetting(intf, bandwidth), SN9C20X_VID_ISOC);
387 if (ep == NULL)
388 return -EIO;
390 ret = usb_set_interface(dev->udev, 0, bandwidth);
391 if (ret < 0)
392 return ret;
394 value |= 0x01;
395 ret = usb_sn9c20x_control_write(dev, 0x1061, &value, 1);
396 if (ret < 0)
397 return ret;
399 ret = usb_sn9c20x_isoc_init(dev, ep);
400 } else {
401 ep = find_endpoint(usb_altnum_to_altsetting(intf, 0), SN9C20X_BULK);
402 if (ep == NULL)
403 return -EIO;
405 ret = usb_set_interface(dev->udev, 0, 0);
406 if (ret < 0)
407 return ret;
409 value &= ~0x01;
410 ret = usb_sn9c20x_control_write(dev, 0x1061, &value, 1);
411 if (ret < 0)
412 return ret;
414 ret = usb_sn9c20x_bulk_init(dev, ep);
417 if (ret < 0)
418 return ret;
420 for (i = 0; i < MAX_URBS; i++) {
421 ret = usb_submit_urb(dev->urbs[i].urb, GFP_KERNEL);
422 if (ret)
423 UDIA_ERROR("isoc_init() submit_urb %d failed with error %d\n", i, ret);
426 return 0;
430 * @param dev Device structure
432 * @brief Clean-up all the ISOC buffers
434 * This function permits to clean-up all the ISOC buffers.
436 void usb_sn9c20x_uninit_urbs(struct usb_sn9c20x *dev)
438 int i;
439 struct urb *urb;
441 UDIA_DEBUG("Isoc cleanup\n");
443 if (dev == NULL)
444 return;
446 for (i = 0; i < MAX_URBS; i++) {
447 urb = dev->urbs[i].urb;
448 if (urb == NULL)
449 continue;
450 usb_kill_urb(urb);
451 kfree(dev->urbs[i].data);
452 usb_free_urb(urb);
453 dev->urbs[i].urb = NULL;
457 int usb_sn9c20x_detect_frame(unsigned char *buf, unsigned int buf_length)
459 static unsigned char frame_header[] = {0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96};
460 int index;
461 if (buf_length < 64)
462 return -1;
463 if (buf_length == 64) {
464 if (memcmp(buf, frame_header, 6) == 0)
465 return 0;
466 else
467 return -1;
469 for (index = 0; index < buf_length - 63; index++) {
470 if (memcmp(buf + index, frame_header, 6) == 0) {
471 UDIA_DEBUG("Found Header at %d\n", index);
472 return index;
475 return -1;
478 void usb_sn9c20x_assemble_video(struct usb_sn9c20x *dev,
479 unsigned char *transfer, unsigned int transfer_length,
480 struct sn9c20x_buffer **buffer)
482 void *mem = NULL;
483 unsigned char *header;
484 int header_index;
485 int yavg;
486 struct sn9c20x_buffer *buf = *buffer;
487 struct sn9c20x_video_queue *queue = &dev->queue;
489 if (buf->state != SN9C20X_BUF_STATE_ACTIVE)
490 buf->state = SN9C20X_BUF_STATE_ACTIVE;
492 header_index = usb_sn9c20x_detect_frame(transfer, transfer_length);
493 if (header_index >= 0) {
494 if (header_index + buf->buf.bytesused > buf->buf.length) {
495 UDIA_WARNING("Frame Buffer overflow!\n");
496 dev->vframes_overflow++;
497 buf->state = SN9C20X_BUF_STATE_DONE;
499 header_index = min(buf->buf.length - buf->buf.bytesused,
500 (unsigned int)header_index);
501 mem = queue->mem + buf->buf.m.offset + buf->buf.bytesused;
502 memcpy(mem, transfer, header_index);
503 buf->buf.bytesused += header_index;
504 header = transfer+header_index;
505 /* UDIA_INFO("color window: %dx%d\n",
506 header[0x3a] << 4,
507 header[0x3b] << 3);
508 UDIA_INFO("Frame Size: %d\n", buf->buf.bytesused);*/
509 yavg = ((header[35] >> 2) & 3)|(header[20] << 2)|(header[19] << 10);
510 yavg += ((header[35] >> 4) & 3)|(header[22] << 2)|(header[21] << 10);
511 yavg += ((header[35] >> 6) & 3)|(header[24] << 2)|(header[23] << 10);
512 yavg += (header[36] & 3)|(header[26] << 2)|(header[25] << 10);
513 yavg += ((header[36] >> 2) & 3)|(header[28] << 2)|(header[27] << 10);
514 yavg += ((header[36] >> 4) & 3)|(header[30] << 2)|(header[29] << 10);
515 yavg += ((header[36] >> 6) & 3)|(header[32] << 2)|(header[31] << 10);
516 yavg += ((header[44] >> 4) & 3)|(header[34] << 2)|(header[33] << 10);
517 UDIA_DEBUG("AVGY Total: %d (%d)\n", yavg, yavg >> 9);
518 yavg >>= 9;
519 atomic_set(&dev->camera.yavg, yavg);
521 if (buf->buf.bytesused != 0)
522 buf->state = SN9C20X_BUF_STATE_DONE;
523 } else {
524 if (transfer_length + buf->buf.bytesused > buf->buf.length) {
525 UDIA_WARNING("Frame Buffer overflow!\n");
526 dev->vframes_overflow++;
527 buf->state = SN9C20X_BUF_STATE_DONE;
529 transfer_length = min(buf->buf.length - buf->buf.bytesused,
530 transfer_length);
531 mem = queue->mem + buf->buf.m.offset + buf->buf.bytesused;
532 memcpy(mem, transfer, transfer_length);
533 buf->buf.bytesused += transfer_length;
535 if (buf->state == SN9C20X_BUF_STATE_DONE ||
536 buf->state == SN9C20X_BUF_STATE_ERROR) {
537 buf = sn9c20x_queue_next_buffer(queue, buf);
538 *buffer = buf;
539 if (buf == NULL) {
540 dev->vframes_dropped++;
541 } else {
542 if (header_index + 64 < transfer_length) {
543 memcpy(queue->mem + buf->buf.m.offset,
544 transfer + header_index + 64,
545 transfer_length - (header_index + 64));
546 buf->buf.bytesused +=
547 transfer_length - (header_index + 64);
553 * @param urb URB structure
555 * @brief ISOC handler
557 * This function is called as an URB transfert is complete (Isochronous pipe).
558 * So, the traitement is done in interrupt time, so it has be fast, not crash,
559 * and not stall. Neat.
561 void usb_sn9c20x_completion_handler(struct urb *urb)
563 int i;
564 int ret;
565 unsigned long flags;
567 unsigned char *transfer = NULL;
568 unsigned int transfer_length;
570 struct sn9c20x_buffer *buf = NULL;
571 struct usb_sn9c20x *dev = urb->context;
572 struct sn9c20x_video_queue *queue = &dev->queue;
574 UDIA_STREAM("Isoc handler\n");
576 switch (urb->status) {
577 case 0:
578 break;
580 default:
581 UDIA_WARNING("Non-zero status (%d) in video "
582 "completion handler.\n", urb->status);
584 case -ENOENT: /* usb_kill_urb() called. */
585 if (dev->frozen)
586 return;
588 case -ECONNRESET: /* usb_unlink_urb() called. */
589 case -ESHUTDOWN: /* The endpoint is being disabled. */
590 sn9c20x_queue_cancel(queue, urb->status == -ESHUTDOWN);
591 return;
594 spin_lock_irqsave(&queue->irqlock, flags);
595 if (!list_empty(&queue->irqqueue))
596 buf = list_first_entry(&queue->irqqueue, struct sn9c20x_buffer,
597 queue);
598 spin_unlock_irqrestore(&queue->irqlock, flags);
599 if (!bulk) {
600 for (i = 0; i < urb->number_of_packets; i++) {
601 if (urb->iso_frame_desc[i].status != 0) {
602 /*UDIA_ERROR("Iso frame %d of USB has error %d\n",
603 i, urb->iso_frame_desc[i].status);*/
604 mdelay(1);
605 continue;
607 transfer_length = urb->iso_frame_desc[i].actual_length;
608 transfer = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
609 if (buf == NULL)
610 continue;
612 usb_sn9c20x_assemble_video(dev, transfer, transfer_length, &buf);
614 } else {
615 if (buf != NULL) {
616 usb_sn9c20x_assemble_video(dev, urb->transfer_buffer,
617 urb->actual_length, &buf);
620 ret = usb_submit_urb(urb, GFP_ATOMIC);
622 if (ret != 0) {
623 UDIA_ERROR("Error (%d) re-submitting urb in "
624 "sn9c20x_isoc_handler.\n", ret);
629 * @param dev Device structure
630 * @param value register to write to
631 * @param data
632 * @param length number of bytes
634 * @returns 0 if all is OK
636 * @brief Write a 16-bit value to a 16-bit register
638 * This function permits to write a 16-bit value to a 16-bit register on the USB bus.
640 int usb_sn9c20x_control_write(struct usb_sn9c20x *dev, __u16 value, __u8 *data, __u16 length)
642 int result;
643 struct usb_device *udev = dev->udev;
645 result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
646 0x08,
647 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
648 value,
649 0x00,
650 data,
651 length,
652 500);
654 if (unlikely(result < 0))
655 UDIA_ERROR("Write register failed index = 0x%02X\n", value);
657 return result;
661 * @param dev Device structure
662 * @param index register to read from
663 * @param data
664 * @param length number of bytes
666 * @returns 0 if all is OK
668 * @brief Read a 16-bit value from a 16-bit register
670 * This function permits to read a 16-bit value from a 16-bit register on the USB bus.
672 int usb_sn9c20x_control_read(struct usb_sn9c20x *dev, __u16 index, __u8 *data, __u16 length)
674 int result;
676 struct usb_device *udev = dev->udev;
678 *data = 0;
680 result = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
681 0x00,
682 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
683 index,
684 0x00,
685 data,
686 length,
687 500);
689 if (unlikely(result < 0))
690 UDIA_ERROR("Read register failed 0x%02X\n", index);
692 return result;
697 * @param dev
699 * @returns 0 if all is OK
701 * @brief Set the default value about the video settings.
703 * This function permits to set the video settings for each video camera model.
706 static int usb_sn9c20x_default_settings(struct usb_sn9c20x *dev)
708 dev->vframes_overflow = 0;
709 dev->vframes_incomplete = 0;
710 dev->vframes_dropped = 0;
712 dev->queue.min_buffers = min_buffers;
713 dev->queue.max_buffers = max_buffers;
715 dev->vsettings.fps = fps;
717 v4l2_set_control_default(dev, V4L2_CID_HFLIP, hflip);
718 v4l2_set_control_default(dev, V4L2_CID_VFLIP, vflip);
719 v4l2_set_control_default(dev, V4L2_CID_GAIN, gain);
720 v4l2_set_control_default(dev, V4L2_CID_BRIGHTNESS, brightness);
721 v4l2_set_control_default(dev, V4L2_CID_CONTRAST, contrast);
722 v4l2_set_control_default(dev, V4L2_CID_GAMMA, gamma);
723 v4l2_set_control_default(dev, V4L2_CID_SHARPNESS, sharpness);
724 v4l2_set_control_default(dev, V4L2_CID_RED_BALANCE, red_gain);
725 v4l2_set_control_default(dev, V4L2_CID_BLUE_BALANCE, blue_gain);
726 v4l2_set_control_default(dev, V4L2_CID_EXPOSURE_AUTO, auto_exposure);
727 v4l2_set_control_default(dev, V4L2_CID_AUTOGAIN, auto_gain);
728 v4l2_set_control_default(dev, V4L2_CID_AUTO_WHITE_BALANCE, auto_whitebalance);
729 v4l2_set_control_default(dev, V4L2_CID_EXPOSURE, exposure);
731 if (jpeg == 2) {
732 if (dev->udev->speed == USB_SPEED_HIGH && bandwidth == 8)
733 dev->jpeg = 0;
734 else
735 dev->jpeg = 1;
736 } else
737 dev->jpeg = jpeg;
739 sn9c20x_set_resolution(dev, 640, 480);
740 sn9c20x_set_format(dev, sn9c20x_fmts[0].pix_fmt);
742 return 0;
746 * @brief Load the driver
748 * @param interface
749 * @param id
751 * @returns 0 if all is OK
753 * This function detects the device and allocate the buffers for the device
754 * and the video interface.
756 static int usb_sn9c20x_probe(struct usb_interface *interface, const struct usb_device_id *id)
758 int ret;
760 int vendor_id;
761 int product_id;
762 int bNumInterfaces;
764 struct usb_sn9c20x *dev = NULL;
765 struct usb_device *udev = interface_to_usbdev(interface);
767 /* Get USB VendorID and ProductID */
768 vendor_id = le16_to_cpu(udev->descriptor.idVendor);
769 product_id = le16_to_cpu(udev->descriptor.idProduct);
771 /* Check if we can handle this device */
772 UDIA_DEBUG("Probe function called with VendorID=%04X, ProductID=%04X and InterfaceNumber=%d\n",
773 vendor_id, product_id, interface->cur_altsetting->desc.bInterfaceNumber);
775 UDIA_INFO("SN9C20X USB 2.0 Webcam - %04X:%04X plugged-in.\n",
776 vendor_id, product_id);
780 // Allocate structure, initialize pointers, mutexes, etc.
781 // and link it to the usb_device
783 dev = kzalloc(sizeof(struct usb_sn9c20x), GFP_KERNEL);
785 if (dev == NULL) {
786 UDIA_ERROR("Out of memory !\n");
787 ret = -ENOMEM;
788 goto error;
791 kref_init(&dev->vopen);
793 dev->udev = udev;
794 dev->interface = interface;
796 /* Read the product release */
797 dev->release = le16_to_cpu(udev->descriptor.bcdDevice);
798 UDIA_DEBUG("Release: %04x\n", dev->release);
800 /* How many interfaces (1 or 3) ? */
801 bNumInterfaces = udev->config->desc.bNumInterfaces;
802 UDIA_DEBUG("Number of interfaces : %d\n", bNumInterfaces);
804 dev->camera.sensor = id->driver_info & 0xFF;
805 dev->camera.address = (id->driver_info >> 8) & 0xFF;
807 /* Initialize the camera */
808 ret = sn9c20x_initialize(dev);
809 if (ret < 0)
810 goto error;
812 /* Initialize the video device */
813 dev->vdev = video_device_alloc();
815 if (!dev->vdev) {
816 ret = -ENOMEM;
817 goto free_dev;
820 /* Register the video device */
821 ret = v4l_sn9c20x_register_video_device(dev);
823 if (ret)
824 goto free_dev;
826 /* Create the entries in the sys filesystem */
827 sn9c20x_create_sysfs_files(dev->vdev);
829 sn9c20x_create_debugfs_files(dev);
831 /* Save our data pointer in this interface device */
832 usb_set_intfdata(interface, dev);
834 usb_sn9c20x_default_settings(dev);
836 return 0;
838 free_dev:
839 kref_put(&dev->vopen, usb_sn9c20x_delete);
840 error:
841 return ret;
844 void usb_sn9c20x_delete(struct kref *kref)
846 struct usb_sn9c20x *dev;
847 dev = container_of(kref, struct usb_sn9c20x, vopen);
849 if (dev->vdev != NULL) {
850 sn9c20x_remove_sysfs_files(dev->vdev);
851 sn9c20x_remove_debugfs_files(dev);
852 v4l_sn9c20x_unregister_video_device(dev);
854 kfree(dev);
858 * @param interface
860 * @brief This function is called when the device is disconnected
861 * or when the kernel module is unloaded.
863 static void usb_sn9c20x_disconnect(struct usb_interface *interface)
865 struct usb_sn9c20x *dev = usb_get_intfdata(interface);
867 UDIA_INFO("SN9C20X USB 2.0 Webcam unplugged\n");
869 usb_set_intfdata(interface, NULL);
871 mutex_lock(&open_lock);
872 kref_put(&dev->vopen, usb_sn9c20x_delete);
873 mutex_unlock(&open_lock);
878 * @var usb_sn9c20x_driver
880 * This variable contains some callback
882 static struct usb_driver usb_sn9c20x_driver = {
883 .name = "sn9c20x",
884 .probe = usb_sn9c20x_probe,
885 .disconnect = usb_sn9c20x_disconnect,
886 .id_table = sn9c20x_table,
889 module_param(fps, byte, 0444); /**< @brief Module parameter frames per second */
890 module_param(bulk, byte, 0444);
891 module_param(jpeg, byte, 0444);
892 module_param(bandwidth, byte, 0444);
893 module_param(hflip, byte, 0444); /**< @brief Module parameter horizontal flip process */
894 module_param(vflip, byte, 0444); /**< @brief Module parameter vertical flip process */
895 module_param(flip_detect, byte, 0444); /**< @brief Module parameter flip detect */
896 module_param(auto_exposure, byte, 0444); /**< @brief Module parameter automatic exposure control */
897 module_param(auto_gain, byte, 0444); /**< @brief Module parameter automatic gain control */
898 module_param(auto_whitebalance, byte, 0444); /**< @brief Module parameter automatic whitebalance control */
899 module_param(brightness, ushort, 0444); /**< @brief Module parameter brightness */
900 module_param(gamma, ushort, 0444); /**< @brief Module parameter gamma */
901 module_param(contrast, ushort, 0444); /**< @brief Module parameter contrast */
902 module_param(exposure, ushort, 0444); /**< @brief Module parameter exposure */
903 module_param(sharpness, ushort, 0444); /**< @brief Module parameter sharpness */
904 module_param(red_gain, ushort, 0444); /**< @brief Module parameter red gain */
905 module_param(blue_gain, ushort, 0444); /**< @brief Module parameter blue gain */
907 module_param(min_buffers, byte, 0444);
908 module_param(max_buffers, byte, 0444);
910 module_param(log_level, byte, 0444);
913 * @returns 0 if all is OK
915 * @brief Initialize the driver.
917 * This function is called at first.
918 * This function permits to define the default values from the command line.
920 static int __init usb_sn9c20x_init(void)
922 int result;
924 UDIA_DEBUG(DRIVER_DESC " initializing\n");
926 sn9c20x_init_debugfs();
928 if (fps < 10 || fps > 30) {
929 UDIA_WARNING("Framerate out of bounds [10-30]! Defaulting to 25\n");
930 fps = 25;
933 if (bandwidth < 1 || bandwidth > 8) {
934 UDIA_WARNING("Bandwidth out of bounds [1-8]! Defaulting to 8\n");
935 bandwidth = 8;
938 if (bulk != 0 && bulk != 1) {
939 UDIA_WARNING("Bulk transfer should be 0 or 1! Defaulting to 0\n");
940 bulk = 0;
943 if (jpeg > 2) {
944 UDIA_WARNING("JPEG should be 0 or 1 or 2! Defaulting to 2\n");
945 jpeg = 2;
948 if (vflip != 0 && vflip != 1) {
949 UDIA_WARNING("Vertical flip should be 0 or 1! Defaulting to 0\n");
950 vflip = 0;
953 if (hflip != 0 && hflip != 1) {
954 UDIA_WARNING("Horizontal flip should be 0 or 1! Defaulting to 0\n");
955 hflip = 0;
958 if (sharpness > 0x3f) {
959 UDIA_WARNING("Sharpness should be 0 to 63 ! Defaulting to 31\n");
960 sharpness = 0x1f;
963 if (red_gain > 0x7f) {
964 UDIA_WARNING("Red Gain should be 0 to 127 ! Defaulting to 31\n");
965 red_gain = 31;
968 if (blue_gain > 0x7f) {
969 UDIA_WARNING("Blue Gain should be 0 to 127 ! Defaulting to 31\n");
970 blue_gain = 31;
973 if (auto_exposure != 0 && auto_exposure != 1) {
974 UDIA_WARNING("Automatic exposure should be 0 or 1! "
975 "Defaulting to 0\n");
976 auto_exposure = 1;
979 if (auto_gain != 0 && auto_gain != 1) {
980 UDIA_WARNING("Automatic gain should be 0 or 1! "
981 "Defaulting to 0\n");
982 auto_gain = 0;
985 if (auto_whitebalance != 0 && auto_whitebalance != 1) {
986 UDIA_WARNING("Automatic whitebalance should be 0 or 1! "
987 "Defaulting to 1\n");
988 auto_whitebalance = 1;
991 if (min_buffers < 2) {
992 UDIA_WARNING("Minimum buffers can't be less then 2! "
993 "Defaulting to 2\n");
994 min_buffers = 2;
998 if (min_buffers > max_buffers) {
999 UDIA_WARNING("Minimum buffers must be less then or equal to "
1000 "max buffers! Defaulting to 2, 10\n");
1001 min_buffers = 2;
1002 max_buffers = 5;
1005 /* Register the driver with the USB subsystem */
1006 result = usb_register(&usb_sn9c20x_driver);
1008 if (result)
1009 UDIA_ERROR("usb_register failed ! Error number %d\n", result);
1011 UDIA_INFO(DRIVER_DESC " " DRIVER_VERSION " loaded\n");
1013 return result;
1018 * @brief Close the driver
1020 * This function is called at last when you unload the driver.
1022 static void __exit usb_sn9c20x_exit(void)
1024 UDIA_INFO("usb_sn9c20x_exit: SN9C20X USB 2.0 webcam driver unloaded\n");
1026 sn9c20x_uninit_debugfs();
1028 /* Deregister this driver with the USB subsystem */
1029 usb_deregister(&usb_sn9c20x_driver);
1033 module_init(usb_sn9c20x_init); /**< @brief Module initialize */
1034 module_exit(usb_sn9c20x_exit); /**< @brief Module exit */
1037 MODULE_PARM_DESC(fps, "Frames per second [10-30]"); /**< @brief Description of 'fps' parameter */
1038 MODULE_PARM_DESC(jpeg, "Enable JPEG support (default is auto-detect)");
1039 MODULE_PARM_DESC(bulk, "Enable Bulk transfer (default is to use ISOC)");
1040 MODULE_PARM_DESC(bandwidth, "Bandwidth Setting (only for ISOC)");
1041 MODULE_PARM_DESC(hflip, "Horizontal image flip"); /**< @brief Description of 'hflip' parameter */
1042 MODULE_PARM_DESC(vflip, "Vertical image flip"); /**< @brief Description of 'vflip' parameter */
1043 MODULE_PARM_DESC(flip_detect, "Image flip detection"); /**< @brief Description of 'vflip_detect' parameter */
1044 MODULE_PARM_DESC(auto_exposure, "Automatic exposure control"); /**< @brief Description of 'auto_exposure' parameter */
1045 MODULE_PARM_DESC(auto_gain, "Automatic gain control"); /**< @brief Description of 'auto_gain' parameter */
1046 MODULE_PARM_DESC(auto_whitebalance, "Automatic whitebalance"); /**< @brief Description of 'auto_whitebalance' parameter */
1047 MODULE_PARM_DESC(brightness, "Brightness setting"); /**< @brief Description of 'brightness' parameter */
1048 MODULE_PARM_DESC(gamma, "Gamma setting"); /**< @brief Description of 'gamma' parameter */
1049 MODULE_PARM_DESC(exposure, "Exposure setting"); /**< @brief Description of 'exposure' parameter */
1050 MODULE_PARM_DESC(gain, "Gain setting"); /**< @brief Description of 'gain' parameter */
1051 MODULE_PARM_DESC(contrast, "Contrast setting"); /**< @brief Description of 'contrast' parameter */
1052 MODULE_PARM_DESC(sharpness, "Sharpness setting"); /**< @brief Description of 'sharpness' parameter */
1053 MODULE_PARM_DESC(red_gain, "Red Gain setting"); /**< @brief Description of 'Red Gain' parameter */
1054 MODULE_PARM_DESC(blue_gain, "Blue Gain setting"); /**< @brief Description of 'Blue Gain' parameter */
1056 MODULE_PARM_DESC(min_buffers, "Minimum number of image buffers");
1057 MODULE_PARM_DESC(max_buffers, "Maximum number of image buffers");
1058 MODULE_PARM_DESC(log_level, " <n>\n"
1059 "Driver log level\n"
1060 "1 = info (default)\n"
1061 "2 = warning\n"
1062 "4 = error\n"
1063 "8 = debug\n"
1064 "16 = stream\n");
1066 MODULE_LICENSE("GPL"); /**< @brief Driver is under licence GPL */
1067 MODULE_AUTHOR(DRIVER_AUTHOR); /**< @brief Driver is written by Nicolas VIVIEN */
1068 MODULE_DESCRIPTION(DRIVER_DESC); /**< @brief Define the description of the driver */
1069 MODULE_SUPPORTED_DEVICE(DRIVER_SUPPORT); /**< @brief List of supported device */