Make auto exposure a boolean variable.
[microdia.git] / sn9c20x-usb.c
blobf7a2710fd3decf10b7c54ffc3cfda9a3df88b35a
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 int 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(le16_to_cpu(ep->wMaxPacketSize))) *
301 hb_multiplier(le16_to_cpu(le16_to_cpu(ep->wMaxPacketSize)));
303 for (i = 0; i < MAX_URBS; i++) {
304 urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
306 if (urb == NULL) {
307 UDIA_ERROR("Failed to allocate URB %d\n", i);
308 usb_sn9c20x_uninit_urbs(dev);
309 return -ENOMEM;
312 urb->interval = 1;
313 urb->dev = udev;
314 urb->pipe = usb_rcvisocpipe(udev, ep->bEndpointAddress);
315 urb->transfer_flags = URB_ISO_ASAP;
316 urb->transfer_buffer_length = iso_max_frame_size * ISO_FRAMES_PER_DESC;
317 urb->complete = usb_sn9c20x_completion_handler;
318 urb->context = dev;
319 urb->start_frame = 0;
320 urb->number_of_packets = ISO_FRAMES_PER_DESC;
322 for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
323 urb->iso_frame_desc[j].offset = j * iso_max_frame_size;
324 urb->iso_frame_desc[j].length = iso_max_frame_size;
327 dev->urbs[i].data = kzalloc(urb->transfer_buffer_length,
328 GFP_KERNEL);
329 if (dev->urbs[i].data == NULL) {
330 usb_sn9c20x_uninit_urbs(dev);
331 return -ENOMEM;
334 urb->transfer_buffer = dev->urbs[i].data;
335 dev->urbs[i].urb = urb;
338 return 0;
341 int usb_sn9c20x_bulk_init(struct usb_sn9c20x *dev,
342 struct usb_endpoint_descriptor *ep)
344 struct urb *urb;
345 unsigned int pipe, i;
346 __u16 psize;
347 __u32 size;
348 psize = max_packet_sz(le16_to_cpu(le16_to_cpu(ep->wMaxPacketSize)));
349 size = psize * ISO_FRAMES_PER_DESC;
350 pipe = usb_rcvbulkpipe(dev->udev, ep->bEndpointAddress);
352 for (i = 0; i < MAX_URBS; ++i) {
353 urb = usb_alloc_urb(0, GFP_KERNEL);
354 if (urb == NULL) {
355 usb_sn9c20x_uninit_urbs(dev);
356 return -ENOMEM;
359 dev->urbs[i].data = kzalloc(size, GFP_KERNEL);
361 usb_fill_bulk_urb(urb, dev->udev, pipe,
362 dev->urbs[i].data, size,
363 usb_sn9c20x_completion_handler,
364 dev);
366 dev->urbs[i].urb = urb;
369 return 0;
372 int usb_sn9c20x_init_urbs(struct usb_sn9c20x *dev)
374 int ret, i;
375 __u8 value;
376 struct usb_endpoint_descriptor *ep;
377 struct usb_interface *intf = dev->interface;
379 ret = usb_sn9c20x_control_read(dev, 0x1061, &value, 1);
380 if (ret < 0)
381 return ret;
383 if (!bulk) {
384 if (bandwidth > 8)
385 return -EINVAL;
387 ep = find_endpoint(usb_altnum_to_altsetting(intf, bandwidth), SN9C20X_VID_ISOC);
388 if (ep == NULL)
389 return -EIO;
391 ret = usb_set_interface(dev->udev, 0, bandwidth);
392 if (ret < 0)
393 return ret;
395 value |= 0x01;
396 ret = usb_sn9c20x_control_write(dev, 0x1061, &value, 1);
397 if (ret < 0)
398 return ret;
400 ret = usb_sn9c20x_isoc_init(dev, ep);
401 } else {
402 ep = find_endpoint(usb_altnum_to_altsetting(intf, 0), SN9C20X_BULK);
403 if (ep == NULL)
404 return -EIO;
406 ret = usb_set_interface(dev->udev, 0, 0);
407 if (ret < 0)
408 return ret;
410 value &= ~0x01;
411 ret = usb_sn9c20x_control_write(dev, 0x1061, &value, 1);
412 if (ret < 0)
413 return ret;
415 ret = usb_sn9c20x_bulk_init(dev, ep);
418 if (ret < 0)
419 return ret;
421 for (i = 0; i < MAX_URBS; i++) {
422 ret = usb_submit_urb(dev->urbs[i].urb, GFP_KERNEL);
423 if (ret)
424 UDIA_ERROR("isoc_init() submit_urb %d failed with error %d\n", i, ret);
427 return 0;
431 * @param dev Device structure
433 * @brief Clean-up all the ISOC buffers
435 * This function permits to clean-up all the ISOC buffers.
437 void usb_sn9c20x_uninit_urbs(struct usb_sn9c20x *dev)
439 int i;
440 struct urb *urb;
442 UDIA_DEBUG("Isoc cleanup\n");
444 if (dev == NULL)
445 return;
447 for (i = 0; i < MAX_URBS; i++) {
448 urb = dev->urbs[i].urb;
449 if (urb == NULL)
450 continue;
451 usb_kill_urb(urb);
452 kfree(dev->urbs[i].data);
453 usb_free_urb(urb);
454 dev->urbs[i].urb = NULL;
458 int usb_sn9c20x_detect_frame(unsigned char *buf, unsigned int buf_length)
460 unsigned char frame_header[] = {0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96};
461 int index;
462 if (buf_length < 64)
463 return -1;
464 if (buf_length == 64) {
465 if (memcmp(buf, frame_header, 6) == 0)
466 return 0;
467 else
468 return -1;
470 for (index = 0; index < buf_length - 63; index++) {
471 if (memcmp(buf + index, frame_header, 6) == 0) {
472 UDIA_DEBUG("Found Header at %d\n", index);
473 return index;
476 return -1;
479 void usb_sn9c20x_assemble_video(struct usb_sn9c20x *dev,
480 unsigned char *transfer, unsigned int transfer_length,
481 struct sn9c20x_buffer **buffer)
483 void *mem = NULL;
484 unsigned char *header;
485 int header_index;
486 int yavg;
487 struct sn9c20x_buffer *buf = *buffer;
488 struct sn9c20x_video_queue *queue = &dev->queue;
490 if (buf->state != SN9C20X_BUF_STATE_ACTIVE)
491 buf->state = SN9C20X_BUF_STATE_ACTIVE;
493 header_index = usb_sn9c20x_detect_frame(transfer, transfer_length);
494 if (header_index >= 0) {
495 if (header_index + buf->buf.bytesused > buf->buf.length) {
496 UDIA_WARNING("Frame Buffer overflow!\n");
497 dev->vframes_overflow++;
498 buf->state = SN9C20X_BUF_STATE_DONE;
500 header_index = min(buf->buf.length - buf->buf.bytesused,
501 (unsigned int)header_index);
502 mem = queue->mem + buf->buf.m.offset + buf->buf.bytesused;
503 memcpy(mem, transfer, header_index);
504 buf->buf.bytesused += header_index;
505 header = transfer+header_index;
506 /* UDIA_INFO("color window: %dx%d\n",
507 header[0x3a] << 4,
508 header[0x3b] << 3);
509 UDIA_INFO("Frame Size: %d\n", buf->buf.bytesused);*/
510 yavg = ((header[35] >> 2) & 3)|(header[20] << 2)|(header[19] << 10);
511 yavg += ((header[35] >> 4) & 3)|(header[22] << 2)|(header[21] << 10);
512 yavg += ((header[35] >> 6) & 3)|(header[24] << 2)|(header[23] << 10);
513 yavg += (header[36] & 3)|(header[26] << 2)|(header[25] << 10);
514 yavg += ((header[36] >> 2) & 3)|(header[28] << 2)|(header[27] << 10);
515 yavg += ((header[36] >> 4) & 3)|(header[30] << 2)|(header[29] << 10);
516 yavg += ((header[36] >> 6) & 3)|(header[32] << 2)|(header[31] << 10);
517 yavg += ((header[44] >> 4) & 3)|(header[34] << 2)|(header[33] << 10);
518 UDIA_DEBUG("AVGY Total: %d (%d)\n", yavg, yavg >> 9);
519 yavg >>= 9;
520 atomic_set(&dev->camera.yavg, yavg);
522 if (buf->buf.bytesused != 0)
523 buf->state = SN9C20X_BUF_STATE_DONE;
524 } else {
525 if (transfer_length + buf->buf.bytesused > buf->buf.length) {
526 UDIA_WARNING("Frame Buffer overflow!\n");
527 dev->vframes_overflow++;
528 buf->state = SN9C20X_BUF_STATE_DONE;
530 transfer_length = min(buf->buf.length - buf->buf.bytesused,
531 transfer_length);
532 mem = queue->mem + buf->buf.m.offset + buf->buf.bytesused;
533 memcpy(mem, transfer, transfer_length);
534 buf->buf.bytesused += transfer_length;
536 if (buf->state == SN9C20X_BUF_STATE_DONE ||
537 buf->state == SN9C20X_BUF_STATE_ERROR) {
538 buf = sn9c20x_queue_next_buffer(queue, buf);
539 *buffer = buf;
540 if (buf == NULL) {
541 dev->vframes_dropped++;
542 } else {
543 if (header_index + 64 < transfer_length) {
544 memcpy(queue->mem + buf->buf.m.offset,
545 transfer + header_index + 64,
546 transfer_length - (header_index + 64));
547 buf->buf.bytesused +=
548 transfer_length - (header_index + 64);
554 * @param urb URB structure
556 * @brief ISOC handler
558 * This function is called as an URB transfert is complete (Isochronous pipe).
559 * So, the traitement is done in interrupt time, so it has be fast, not crash,
560 * and not stall. Neat.
562 void usb_sn9c20x_completion_handler(struct urb *urb)
564 int i;
565 int ret;
566 unsigned long flags;
568 unsigned char *transfer = NULL;
569 unsigned int transfer_length;
571 struct sn9c20x_buffer *buf = NULL;
572 struct usb_sn9c20x *dev = urb->context;
573 struct sn9c20x_video_queue *queue = &dev->queue;
575 UDIA_STREAM("Isoc handler\n");
577 switch (urb->status) {
578 case 0:
579 break;
581 default:
582 UDIA_WARNING("Non-zero status (%d) in video "
583 "completion handler.\n", urb->status);
585 case -ENOENT: /* usb_kill_urb() called. */
586 if (dev->frozen)
587 return;
589 case -ECONNRESET: /* usb_unlink_urb() called. */
590 case -ESHUTDOWN: /* The endpoint is being disabled. */
591 sn9c20x_queue_cancel(queue, urb->status == -ESHUTDOWN);
592 return;
595 spin_lock_irqsave(&queue->irqlock, flags);
596 if (!list_empty(&queue->irqqueue))
597 buf = list_first_entry(&queue->irqqueue, struct sn9c20x_buffer,
598 queue);
599 spin_unlock_irqrestore(&queue->irqlock, flags);
600 if (!bulk) {
601 for (i = 0; i < urb->number_of_packets; i++) {
602 if (urb->iso_frame_desc[i].status != 0) {
603 UDIA_ERROR("Iso frame %d of USB has error %d\n",
604 i, urb->iso_frame_desc[i].status);
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 (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 (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 /* Init mutexes, spinlock, etc. */
792 mutex_init(&dev->mutex);
793 kref_init(&dev->vopen);
795 dev->udev = udev;
796 dev->interface = interface;
798 /* Read the product release */
799 dev->release = le16_to_cpu(udev->descriptor.bcdDevice);
800 UDIA_DEBUG("Release: %04x\n", dev->release);
802 /* How many interfaces (1 or 3) ? */
803 bNumInterfaces = udev->config->desc.bNumInterfaces;
804 UDIA_DEBUG("Number of interfaces : %d\n", bNumInterfaces);
806 dev->camera.sensor = id->driver_info & 0xFF;
807 dev->camera.address = (id->driver_info >> 8) & 0xFF;
809 /* Initialize the camera */
810 ret = sn9c20x_initialize(dev);
811 if (ret < 0)
812 goto error;
814 /* Initialize the video device */
815 dev->vdev = video_device_alloc();
817 if (!dev->vdev) {
818 ret = -ENOMEM;
819 goto free_dev;
822 /* Register the video device */
823 ret = v4l_sn9c20x_register_video_device(dev);
825 if (ret)
826 goto free_dev;
828 /* Create the entries in the sys filesystem */
829 sn9c20x_create_sysfs_files(dev->vdev);
831 sn9c20x_create_debugfs_files(dev);
833 /* Save our data pointer in this interface device */
834 usb_set_intfdata(interface, dev);
836 usb_sn9c20x_default_settings(dev);
838 return 0;
840 free_dev:
841 kref_put(&dev->vopen, usb_sn9c20x_delete);
842 error:
843 return ret;
846 void usb_sn9c20x_delete(struct kref *kref)
848 struct usb_sn9c20x *dev;
849 dev = container_of(kref, struct usb_sn9c20x, vopen);
851 if (dev->vdev != NULL) {
852 sn9c20x_remove_sysfs_files(dev->vdev);
853 sn9c20x_remove_debugfs_files(dev);
854 v4l_sn9c20x_unregister_video_device(dev);
856 kfree(dev);
860 * @param interface
862 * @brief This function is called when the device is disconnected
863 * or when the kernel module is unloaded.
865 static void usb_sn9c20x_disconnect(struct usb_interface *interface)
867 struct usb_sn9c20x *dev = usb_get_intfdata(interface);
869 UDIA_INFO("SN9C20X USB 2.0 Webcam unplugged\n");
871 usb_set_intfdata(interface, NULL);
873 mutex_lock(&open_lock);
874 kref_put(&dev->vopen, usb_sn9c20x_delete);
875 mutex_unlock(&open_lock);
880 * @var usb_sn9c20x_driver
882 * This variable contains some callback
884 static struct usb_driver usb_sn9c20x_driver = {
885 .name = "sn9c20x",
886 .probe = usb_sn9c20x_probe,
887 .disconnect = usb_sn9c20x_disconnect,
888 .id_table = sn9c20x_table,
891 module_param(fps, byte, 0444); /**< @brief Module parameter frames per second */
892 module_param(bulk, byte, 0444);
893 module_param(jpeg, byte, 0444);
894 module_param(bandwidth, byte, 0444);
895 module_param(hflip, byte, 0444); /**< @brief Module parameter horizontal flip process */
896 module_param(vflip, byte, 0444); /**< @brief Module parameter vertical flip process */
897 module_param(flip_detect, byte, 0444); /**< @brief Module parameter flip detect */
898 module_param(auto_exposure, byte, 0444); /**< @brief Module parameter automatic exposure control */
899 module_param(auto_gain, byte, 0444); /**< @brief Module parameter automatic gain control */
900 module_param(auto_whitebalance, byte, 0444); /**< @brief Module parameter automatic whitebalance control */
901 module_param(brightness, ushort, 0444); /**< @brief Module parameter brightness */
902 module_param(gamma, ushort, 0444); /**< @brief Module parameter gamma */
903 module_param(contrast, ushort, 0444); /**< @brief Module parameter contrast */
904 module_param(exposure, ushort, 0444); /**< @brief Module parameter exposure */
905 module_param(sharpness, ushort, 0444); /**< @brief Module parameter sharpness */
906 module_param(red_gain, ushort, 0444); /**< @brief Module parameter red gain */
907 module_param(blue_gain, ushort, 0444); /**< @brief Module parameter blue gain */
909 module_param(min_buffers, byte, 0444);
910 module_param(max_buffers, byte, 0444);
912 module_param(log_level, byte, 0444);
915 * @returns 0 if all is OK
917 * @brief Initialize the driver.
919 * This function is called at first.
920 * This function permits to define the default values from the command line.
922 static int __init usb_sn9c20x_init(void)
924 int result;
926 UDIA_DEBUG(DRIVER_DESC " initializing\n");
928 sn9c20x_init_debugfs();
930 if (fps < 10 || fps > 30) {
931 UDIA_WARNING("Framerate out of bounds [10-30]! Defaulting to 25\n");
932 fps = 25;
935 if (bandwidth < 1 || bandwidth > 8) {
936 UDIA_WARNING("Bandwidth out of bounds [1-8]! Defaulting to 8\n");
937 bandwidth = 8;
940 if (bulk != 0 && bulk != 1) {
941 UDIA_WARNING("Bulk transfer should be 0 or 1! Defaulting to 0\n");
942 bulk = 0;
945 if (jpeg > 2) {
946 UDIA_WARNING("JPEG should be 0 or 1 or 2! Defaulting to 2\n");
947 jpeg = 2;
950 if (vflip != 0 && vflip != 1) {
951 UDIA_WARNING("Vertical flip should be 0 or 1! Defaulting to 0\n");
952 vflip = 0;
955 if (hflip != 0 && hflip != 1) {
956 UDIA_WARNING("Horizontal flip should be 0 or 1! Defaulting to 0\n");
957 hflip = 0;
960 if (sharpness > 0x3f) {
961 UDIA_WARNING("Sharpness should be 0 to 63 ! Defaulting to 31\n");
962 sharpness = 0x1f;
965 if (red_gain > 0x7f) {
966 UDIA_WARNING("Red Gain should be 0 to 127 ! Defaulting to 31\n");
967 red_gain = 31;
970 if (blue_gain > 0x7f) {
971 UDIA_WARNING("Blue Gain should be 0 to 127 ! Defaulting to 31\n");
972 blue_gain = 31;
975 if (auto_exposure != 0 && auto_exposure != 1) {
976 UDIA_WARNING("Automatic exposure should be 0 or 1! "
977 "Defaulting to 0\n");
978 auto_exposure = 1;
981 if (auto_gain != 0 && auto_gain != 1) {
982 UDIA_WARNING("Automatic gain should be 0 or 1! "
983 "Defaulting to 0\n");
984 auto_gain = 0;
987 if (auto_whitebalance != 0 && auto_whitebalance != 1) {
988 UDIA_WARNING("Automatic whitebalance should be 0 or 1! "
989 "Defaulting to 1\n");
990 auto_whitebalance = 1;
993 if (min_buffers < 2) {
994 UDIA_WARNING("Minimum buffers can't be less then 2! "
995 "Defaulting to 2\n");
996 min_buffers = 2;
1000 if (min_buffers > max_buffers) {
1001 UDIA_WARNING("Minimum buffers must be less then or equal to "
1002 "max buffers! Defaulting to 2, 10\n");
1003 min_buffers = 2;
1004 max_buffers = 5;
1007 /* Register the driver with the USB subsystem */
1008 result = usb_register(&usb_sn9c20x_driver);
1010 if (result)
1011 UDIA_ERROR("usb_register failed ! Error number %d\n", result);
1013 UDIA_INFO(DRIVER_DESC " " DRIVER_VERSION " loaded\n");
1015 return result;
1020 * @brief Close the driver
1022 * This function is called at last when you unload the driver.
1024 static void __exit usb_sn9c20x_exit(void)
1026 UDIA_INFO("usb_sn9c20x_exit: SN9C20X USB 2.0 webcam driver unloaded\n");
1028 sn9c20x_uninit_debugfs();
1030 /* Deregister this driver with the USB subsystem */
1031 usb_deregister(&usb_sn9c20x_driver);
1035 module_init(usb_sn9c20x_init); /**< @brief Module initialize */
1036 module_exit(usb_sn9c20x_exit); /**< @brief Module exit */
1039 MODULE_PARM_DESC(fps, "Frames per second [10-30]"); /**< @brief Description of 'fps' parameter */
1040 MODULE_PARM_DESC(jpeg, "Enable JPEG support (default is auto-detect)");
1041 MODULE_PARM_DESC(bulk, "Enable Bulk transfer (default is to use ISOC)");
1042 MODULE_PARM_DESC(bandwidth, "Bandwidth Setting (only for ISOC)");
1043 MODULE_PARM_DESC(hflip, "Horizontal image flip"); /**< @brief Description of 'hflip' parameter */
1044 MODULE_PARM_DESC(vflip, "Vertical image flip"); /**< @brief Description of 'vflip' parameter */
1045 MODULE_PARM_DESC(flip_detect, "Image flip detection"); /**< @brief Description of 'vflip_detect' parameter */
1046 MODULE_PARM_DESC(auto_exposure, "Automatic exposure control"); /**< @brief Description of 'auto_exposure' parameter */
1047 MODULE_PARM_DESC(auto_gain, "Automatic gain control"); /**< @brief Description of 'auto_gain' parameter */
1048 MODULE_PARM_DESC(auto_whitebalance, "Automatic whitebalance"); /**< @brief Description of 'auto_whitebalance' parameter */
1049 MODULE_PARM_DESC(brightness, "Brightness setting"); /**< @brief Description of 'brightness' parameter */
1050 MODULE_PARM_DESC(gamma, "Gamma setting"); /**< @brief Description of 'gamma' parameter */
1051 MODULE_PARM_DESC(exposure, "Exposure setting"); /**< @brief Description of 'exposure' parameter */
1052 MODULE_PARM_DESC(gain, "Gain setting"); /**< @brief Description of 'gain' parameter */
1053 MODULE_PARM_DESC(contrast, "Contrast setting"); /**< @brief Description of 'contrast' parameter */
1054 MODULE_PARM_DESC(sharpness, "Sharpness setting"); /**< @brief Description of 'sharpness' parameter */
1055 MODULE_PARM_DESC(red_gain, "Red Gain setting"); /**< @brief Description of 'Red Gain' parameter */
1056 MODULE_PARM_DESC(blue_gain, "Blue Gain setting"); /**< @brief Description of 'Blue Gain' parameter */
1058 MODULE_PARM_DESC(min_buffers, "Minimum number of image buffers");
1059 MODULE_PARM_DESC(max_buffers, "Maximum number of image buffers");
1060 MODULE_PARM_DESC(log_level, " <n>\n"
1061 "Driver log level\n"
1062 "1 = info (default)\n"
1063 "2 = warning\n"
1064 "4 = error\n"
1065 "8 = debug\n"
1066 "16 = stream\n");
1068 MODULE_LICENSE("GPL"); /**< @brief Driver is under licence GPL */
1069 MODULE_AUTHOR(DRIVER_AUTHOR); /**< @brief Driver is written by Nicolas VIVIEN */
1070 MODULE_DESCRIPTION(DRIVER_DESC); /**< @brief Define the description of the driver */
1071 MODULE_SUPPORTED_DEVICE(DRIVER_SUPPORT); /**< @brief List of supported device */