Starting the process of seperating bridge-sensor for 6240
[microdia.git] / microdia-usb.c
blobf8cf7b9c084446f9fe39466f6990667e7730fc07
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
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/kernel.h>
31 #include <linux/version.h>
32 #include <linux/errno.h>
33 #include <linux/slab.h>
34 #include <linux/kref.h>
35 #include <linux/stat.h>
37 #include <linux/usb.h>
38 #include <media/v4l2-common.h>
40 #include "microdia.h"
41 #include "sn9c20x.h"
42 #include "mt9vx11.h"
43 #include "ov965x.h"
44 #include "ov7660.h"
46 /**
47 * @var fps
48 * Module parameter to set frame per second
50 static int fps = 25;
52 /**
53 * @var hflip
54 * Module parameter to enable/disable the horizontal flip process
56 static int hflip;
58 /**
59 * @var flip_detect
60 * Module parameter to enable/disable vflip detection
62 static int flip_detect;
64 /**
65 * @var vflip
66 * Module parameter to enable/disable the vertical flip process
68 static int vflip;
70 /**
71 * @var brightness
72 * Module parameter to set the brightness
74 static int brightness = MICRODIA_PERCENT(50, 0xFFFF);
76 /**
77 * @var whiteness
78 * Module parameter to set the whiteness
80 static int whiteness = MICRODIA_PERCENT(20, 0xFFFF);
82 /**
83 * @var contrast
84 * Module parameter to set the contrast
86 static int contrast = MICRODIA_PERCENT(50, 0xFFFF);
88 /**
89 * @var exposure
90 * Module parameter to set the exposure
92 /* static int exposure = MICRODIA_PERCENT(20, 0xFFFF); */
93 static int exposure = MICRODIA_PERCENT(2, 0xFFFF);
95 /**
96 * @var sharpness
97 * Module parameter to set the sharpness
99 static int sharpness = MICRODIA_PERCENT(50, 0x3F);
102 * @var rgb_gain
103 * Module parameter to set the red/green/blue gain
105 static int rgb_gain = MICRODIA_PERCENT(25, 0x7F) |
106 (MICRODIA_PERCENT(25, 0x7F) << 8) |
107 (MICRODIA_PERCENT(25, 0x7F) << 16);
110 * @var min_buffers
111 * Module parameter to set the minimum number of image buffers
113 static int min_buffers = 2;
116 * @var max_buffers
117 * Module parameter to set the maximum number of image buffers
119 static int max_buffers = 5;
122 * @var auto_exposure
123 * Module parameter to set the exposure
125 static int auto_exposure = 1;
128 * @var auto_whitebalance
129 * Module parameter to set the exposure
131 static int auto_whitebalance = 1;
134 * @var log_level
135 * Module parameter to set the log level
137 __u8 log_level = 1;
141 * @var microdia_table
142 * Define all the hotplug supported devices by this driver
144 static struct usb_device_id microdia_table[] = {
145 /* SN9C201 + MI1300 */
146 { USB_DEVICE(USB_0C45_VID, USB_6240_PID) },
147 /* SN9C201 + MI1310 */
148 { USB_DEVICE(USB_0C45_VID, USB_6242_PID) },
149 /* SN9C201 + S5K4AAFX */
150 { USB_DEVICE(USB_0C45_VID, USB_6243_PID) },
151 /* SN9C201 + OV9655 */
152 { USB_DEVICE(USB_0C45_VID, USB_6248_PID) },
153 /* SN9C201 + CX1332 */
154 { USB_DEVICE(USB_0C45_VID, USB_624B_PID) },
155 /* SN9C201 + MI1320 */
156 { USB_DEVICE(USB_0C45_VID, USB_624C_PID) },
157 /* SN9C201 + SOI968 */
158 { USB_DEVICE(USB_0C45_VID, USB_624E_PID) },
159 /* SN9C201 + OV9650 */
160 { USB_DEVICE(USB_0C45_VID, USB_624F_PID) },
161 /* SN9C201 + OV9650 */
162 { USB_DEVICE(USB_0C45_VID, USB_6253_PID) },
163 /* SN9C201 + OV7670ISP */
164 { USB_DEVICE(USB_0C45_VID, USB_6260_PID) },
165 /* SN9C201 + OM6802 */
166 { USB_DEVICE(USB_0C45_VID, USB_6262_PID) },
167 /* SN9C201 + MI0360/MT9V111 */
168 { USB_DEVICE(USB_0C45_VID, USB_6270_PID) },
169 /* SN9C201 + S5K53BEB */
170 { USB_DEVICE(USB_0C45_VID, USB_627A_PID) },
171 /* SN9C201 + OV7660 */
172 { USB_DEVICE(USB_0C45_VID, USB_627B_PID) },
173 /* SN9C201 + HV7131R */
174 { USB_DEVICE(USB_0C45_VID, USB_627C_PID) },
175 /* EEPROM */
176 { USB_DEVICE(USB_0C45_VID, USB_627F_PID) },
177 /* SN9C202 + MI1300 */
178 { USB_DEVICE(USB_0C45_VID, USB_6280_PID) },
179 /* SN9C202 + MI1310 */
180 { USB_DEVICE(USB_0C45_VID, USB_6282_PID) },
181 /* SN9C202 + S5K4AAFX */
182 { USB_DEVICE(USB_0C45_VID, USB_6283_PID) },
183 /* SN9C202 + OV9655 */
184 { USB_DEVICE(USB_0C45_VID, USB_6288_PID) },
185 /* SN9C202 + ICM107 */
186 { USB_DEVICE(USB_0C45_VID, USB_628A_PID) },
187 /* SN9C202 + CX1332 */
188 { USB_DEVICE(USB_0C45_VID, USB_628B_PID) },
189 /* SN9C202 + MI1320 */
190 { USB_DEVICE(USB_0C45_VID, USB_628C_PID) },
191 /* SN9C202 + SOI968 */
192 { USB_DEVICE(USB_0C45_VID, USB_628E_PID) },
193 /* SN9C202 + OV9650 */
194 { USB_DEVICE(USB_0C45_VID, USB_628F_PID) },
195 /* SN9C202 + OV7670ISP */
196 { USB_DEVICE(USB_0C45_VID, USB_62A0_PID) },
197 /* SN9C202 + OM6802 */
198 { USB_DEVICE(USB_0C45_VID, USB_62A2_PID) },
199 /* SN9C202 + MI0360/MT9V111 */
200 { USB_DEVICE(USB_0C45_VID, USB_62B0_PID) },
201 /* SN9C202 + OV9655 */
202 { USB_DEVICE(USB_0C45_VID, USB_62B3_PID) },
203 /* SN9C202 + S5K53BEB */
204 { USB_DEVICE(USB_0C45_VID, USB_62BA_PID) },
205 /* SN9C202 + OV7660 */
206 { USB_DEVICE(USB_0C45_VID, USB_62BB_PID) },
207 /* SN9C202 + HV7131R */
208 { USB_DEVICE(USB_0C45_VID, USB_62BC_PID) },
209 /* SN9C202 + OV7663 */
210 { USB_DEVICE(USB_0C45_VID, USB_62BE_PID) },
211 /* => 628f (SN9C202 + OV9650) */
212 { USB_DEVICE(USB_045E_VID, USB_00F4_PID) },
213 /* => 627b (SN9C201 + OV7660) */
214 { USB_DEVICE(USB_145F_VID, USB_013D_PID) },
215 /* => 62be (SN9C202 + OV7663 + EEPROM) */
216 { USB_DEVICE(USB_04F2_VID, USB_A128_PID) },
221 MODULE_DEVICE_TABLE(usb, microdia_table); /**< Define the supported devices */
223 DEFINE_MUTEX(open_lock); /**< Define global mutex */
225 int microdia_6240_initialize(struct usb_microdia *dev);
226 int microdia_6240_start_stream(struct usb_microdia *dev);
227 int microdia_6240_stop_stream(struct usb_microdia *dev);
228 int microdia_6242_start_stream(struct usb_microdia *dev);
229 int microdia_6242_stop_stream(struct usb_microdia *dev);
230 int microdia_624e_initialize(struct usb_microdia *dev);
231 int microdia_624e_start_stream(struct usb_microdia *dev);
232 int microdia_624e_stop_stream(struct usb_microdia *dev);
233 int microdia_624f_initialize(struct usb_microdia *dev);
234 int microdia_624f_stop_stream(struct usb_microdia *dev);
235 int microdia_624f_start_stream(struct usb_microdia *dev);
236 int microdia_624f_set_exposure(struct usb_microdia *dev);
237 int microdia_624f_flip_detect(struct usb_microdia *dev);
238 int microdia_6260_initialize(struct usb_microdia *dev);
239 int microdia_6260_start_stream(struct usb_microdia *dev);
240 int microdia_6260_stop_stream(struct usb_microdia *dev);
241 int microdia_6260_flip_detect(struct usb_microdia *dev);
242 int microdia_6270_initialize(struct usb_microdia *dev);
243 int microdia_6270_start_stream(struct usb_microdia *dev);
244 int microdia_6270_stop_stream(struct usb_microdia *dev);
245 int microdia_627b_initialize(struct usb_microdia *dev);
246 int microdia_627b_start_stream(struct usb_microdia *dev);
247 int microdia_627b_stop_stream(struct usb_microdia *dev);
248 int microdia_627f_initialize(struct usb_microdia *dev);
249 int microdia_6288_initialize(struct usb_microdia *dev);
250 int microdia_6288_start_stream(struct usb_microdia *dev);
251 int microdia_6288_stop_stream(struct usb_microdia *dev);
253 struct microdia_camera cameras[] = {
255 .model = CAMERA_MODEL(USB_0C45_VID, USB_6240_PID),
256 .type = MICRODIA_VGA,
257 .sensor_slave_address = 0x5d,
258 .sensor_flags = SN9C20X_I2C_2WIRE,
259 .supported_fmts = 0x03,
260 .initialize = microdia_6240_initialize,
261 .start_stream = microdia_6240_start_stream,
262 .stop_stream = microdia_6240_stop_stream,
265 .model = CAMERA_MODEL(USB_0C45_VID, USB_6242_PID),
266 .type = MICRODIA_VGA,
267 .sensor_slave_address = 0x5d,
268 .sensor_flags = SN9C20X_I2C_2WIRE,
269 .supported_fmts = 0x03,
270 .initialize = microdia_624e_initialize,
271 .start_stream = microdia_6242_start_stream,
272 .stop_stream = microdia_6242_stop_stream,
275 .model = CAMERA_MODEL(USB_0C45_VID, USB_624E_PID),
276 .type = MICRODIA_VGA,
277 .sensor_slave_address = 0x30,
278 .sensor_flags = SN9C20X_I2C_2WIRE,
279 .supported_fmts = 0x03,
280 .initialize = microdia_624e_initialize,
281 .sensor_init = soi968_initialize,
282 .start_stream = microdia_624e_start_stream,
283 .stop_stream = microdia_624e_stop_stream,
284 .set_auto_exposure = ov965x_set_autoexposure,
285 /* .set_exposure = soi968_set_exposure, */
288 .model = CAMERA_MODEL(USB_0C45_VID, USB_624F_PID),
289 .type = MICRODIA_VGA,
290 .sensor_slave_address = 0x30,
291 .sensor_flags = SN9C20X_I2C_2WIRE,
292 .supported_fmts = 0x03,
293 .initialize = microdia_624f_initialize,
294 .sensor_init = ov965x_initialize,
295 .start_stream = microdia_624f_start_stream,
296 .stop_stream = microdia_624f_stop_stream,
297 .flip_detect = microdia_624f_flip_detect,
298 .set_auto_exposure = ov965x_set_autoexposure,
299 .set_exposure = ov965x_set_exposure,
300 .set_hvflip = ov965x_set_hvflip,
303 .model = CAMERA_MODEL(USB_0C45_VID, USB_6253_PID),
304 .type = MICRODIA_VGA,
305 .sensor_slave_address = 0x30,
306 .sensor_flags = SN9C20X_I2C_2WIRE,
307 .supported_fmts = 0x03,
308 .initialize = microdia_624f_initialize,
309 .sensor_init = ov965x_initialize,
310 .start_stream = microdia_624f_start_stream,
311 .stop_stream = microdia_624f_stop_stream,
312 .flip_detect = microdia_624f_flip_detect,
313 .set_auto_exposure = ov965x_set_autoexposure,
314 .set_exposure = ov965x_set_exposure,
315 .set_hvflip = ov965x_set_hvflip,
318 .model = CAMERA_MODEL(USB_0C45_VID, USB_6260_PID),
319 .type = MICRODIA_VGA,
320 .sensor_slave_address = 0x21,
321 .sensor_flags = SN9C20X_I2C_2WIRE,
322 .supported_fmts = 0x08,
323 .initialize = microdia_6260_initialize,
324 .start_stream = microdia_6260_start_stream,
325 .stop_stream = microdia_6260_stop_stream,
326 .flip_detect = microdia_6260_flip_detect,
329 .model = CAMERA_MODEL(USB_0C45_VID, USB_6270_PID),
330 .type = MICRODIA_VGA,
331 .sensor_slave_address = 0,
332 .sensor_flags = SN9C20X_I2C_2WIRE,
333 .supported_fmts = 0x07,
334 .initialize = microdia_6270_initialize,
335 .start_stream = microdia_6270_start_stream,
336 .stop_stream = microdia_6270_stop_stream,
337 .set_exposure = mt9vx11_set_exposure,
338 .set_hvflip = mt9vx11_set_hvflip,
339 .set_auto_exposure = mt9v111_set_autoexposure,
340 .set_auto_whitebalance = mt9v111_set_autowhitebalance,
343 .model = CAMERA_MODEL(USB_0C45_VID, USB_627B_PID),
344 .type = MICRODIA_VGA,
345 .sensor_slave_address = 0x21,
346 .sensor_flags = SN9C20X_I2C_2WIRE,
347 .supported_fmts = 0x07,
348 .initialize = microdia_627b_initialize,
349 .sensor_init = ov7660_initialize,
350 .start_stream = microdia_627b_start_stream,
351 .stop_stream = microdia_627b_stop_stream,
352 .set_auto_exposure = ov7660_set_autoexposure,
353 .set_exposure = ov7660_set_exposure,
356 .model = CAMERA_MODEL(USB_0C45_VID, USB_627F_PID),
357 .type = MICRODIA_VGA,
358 .sensor_slave_address = 0x30,
359 .sensor_flags = SN9C20X_I2C_2WIRE,
360 .supported_fmts = 0x03,
361 .initialize = microdia_624f_initialize,
362 .sensor_init = ov965x_initialize,
363 .start_stream = microdia_624f_start_stream,
364 .stop_stream = microdia_624f_stop_stream,
365 .flip_detect = microdia_624f_flip_detect,
366 .set_exposure = ov965x_set_exposure,
367 .set_hvflip = ov965x_set_hvflip,
370 .model = CAMERA_MODEL(USB_0C45_VID, USB_6288_PID),
371 .type = MICRODIA_VGA,
372 .sensor_slave_address = 0x30,
373 .sensor_flags = SN9C20X_I2C_2WIRE,
374 .supported_fmts = 0x03,
375 .initialize = microdia_6288_initialize,
376 .start_stream = microdia_6288_start_stream,
377 .stop_stream = microdia_6288_stop_stream,
378 .set_auto_exposure = ov965x_set_autoexposure,
381 .model = CAMERA_MODEL(USB_0C45_VID, USB_62B3_PID),
382 .type = MICRODIA_VGA,
383 .sensor_slave_address = 0x30,
384 .sensor_flags = SN9C20X_I2C_2WIRE,
385 .supported_fmts = 0x03,
386 .initialize = microdia_6288_initialize,
387 .start_stream = microdia_6288_start_stream,
388 .stop_stream = microdia_6288_stop_stream,
391 .model = CAMERA_MODEL(USB_0C45_VID, USB_62BB_PID),
392 .type = MICRODIA_VGA,
393 .sensor_slave_address = 0x21,
394 .sensor_flags = SN9C20X_I2C_2WIRE,
395 .supported_fmts = 0x07,
396 .initialize = microdia_627b_initialize,
397 .start_stream = microdia_627b_start_stream,
398 .stop_stream = microdia_627b_stop_stream,
401 .model = CAMERA_MODEL(USB_145F_VID, USB_013D_PID),
402 .type = MICRODIA_VGA,
403 .sensor_slave_address = 0x21,
404 .sensor_flags = SN9C20X_I2C_2WIRE,
405 .supported_fmts = 0x07,
406 .initialize = microdia_627b_initialize,
407 .start_stream = microdia_627b_start_stream,
408 .stop_stream = microdia_627b_stop_stream,
413 static inline struct microdia_camera *find_camera(__u32 model)
415 int i;
416 for (i = 0; i < ARRAY_SIZE(cameras); i++) {
417 if (cameras[i].model == model)
418 return &cameras[i];
420 return NULL;
424 * @param dev Device structure
426 * @returns 0 if all is OK
428 * @brief Initilize an isochronous pipe.
430 * This function permits to initialize an URB transfert (or isochronous pipe).
432 int usb_microdia_isoc_init(struct usb_microdia *dev)
434 int i, j;
435 int ret = 0;
436 __u16 iso_max_frame_size;
437 struct urb *urb;
438 struct usb_device *udev;
440 if (dev == NULL)
441 return -EFAULT;
443 udev = dev->udev;
445 UDIA_DEBUG("usb_microdia_isoc_init()\n");
447 iso_max_frame_size = max_packet_sz(le16_to_cpu(dev->isoc_in_size)) *
448 hb_multiplier(le16_to_cpu(dev->isoc_in_size));
450 for (i = 0; i < MAX_ISO_BUFS; i++) {
451 urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
453 if (urb == NULL) {
454 UDIA_ERROR("Failed to allocate URB %d\n", i);
455 usb_microdia_isoc_cleanup(dev);
456 return -ENOMEM;
459 urb->interval = 1;
460 urb->dev = udev;
461 urb->pipe = usb_rcvisocpipe(udev, dev->isoc_in_endpointAddr);
462 urb->transfer_flags = URB_ISO_ASAP;
463 urb->transfer_buffer_length = iso_max_frame_size * ISO_FRAMES_PER_DESC;
464 urb->complete = usb_microdia_isoc_handler;
465 urb->context = dev;
466 urb->start_frame = 0;
467 urb->number_of_packets = ISO_FRAMES_PER_DESC;
469 for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
470 urb->iso_frame_desc[j].offset = j * iso_max_frame_size;
471 urb->iso_frame_desc[j].length = iso_max_frame_size;
474 dev->isobuf[i].data = kzalloc(urb->transfer_buffer_length,
475 GFP_KERNEL);
476 if (dev->isobuf[i].data == NULL) {
477 usb_microdia_isoc_cleanup(dev);
478 return -ENOMEM;
481 urb->transfer_buffer = dev->isobuf[i].data;
482 dev->isobuf[i].urb = urb;
485 UDIA_DEBUG("dev->isoc_in_size = %X\n", (unsigned int)dev->isoc_in_size);
486 UDIA_DEBUG("dev->isoc_in_endpointAddr = %X\n", dev->isoc_in_endpointAddr);
488 for (i = 0; i < MAX_ISO_BUFS; i++) {
489 ret = usb_submit_urb(dev->isobuf[i].urb, GFP_KERNEL);
491 if (ret)
492 UDIA_ERROR("isoc_init() submit_urb %d failed with error %d\n", i, ret);
495 return 0;
500 * @param urb URB structure
502 * @brief ISOC handler
504 * This function is called as an URB transfert is complete (Isochronous pipe).
505 * So, the traitement is done in interrupt time, so it has be fast, not crash,
506 * and not stall. Neat.
508 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19)
509 void usb_microdia_isoc_handler(struct urb *urb, struct pt_regs *regs)
510 #else
511 void usb_microdia_isoc_handler(struct urb *urb)
512 #endif
514 int i;
515 int ret;
517 int framestatus;
518 unsigned int framelen;
519 unsigned long flags;
521 unsigned char *iso_buf = NULL;
523 void *mem = NULL;
524 struct microdia_buffer *buf = NULL;
525 struct usb_microdia *dev = urb->context;
526 struct microdia_video_queue *queue = &dev->queue;
528 unsigned char frame_header[] = {0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96};
530 UDIA_STREAM("Isoc handler\n");
532 switch (urb->status) {
533 case 0:
534 break;
536 default:
537 UDIA_WARNING("Non-zero status (%d) in video "
538 "completion handler.\n", urb->status);
540 case -ENOENT: /* usb_kill_urb() called. */
541 if (queue->frozen)
542 return;
544 case -ECONNRESET: /* usb_unlink_urb() called. */
545 case -ESHUTDOWN: /* The endpoint is being disabled. */
546 microdia_queue_cancel(queue);
547 return;
550 spin_lock_irqsave(&queue->irqlock, flags);
551 if (!list_empty(&queue->irqqueue))
552 buf = list_first_entry(&queue->irqqueue, struct microdia_buffer,
553 queue);
554 spin_unlock_irqrestore(&queue->irqlock, flags);
556 for (i = 0; i < urb->number_of_packets; i++) {
557 framestatus = urb->iso_frame_desc[i].status;
558 if (framestatus != 0) {
559 UDIA_ERROR("Iso frame %d of USB has error %d\n",
560 i, framestatus);
561 continue;
563 framelen = urb->iso_frame_desc[i].actual_length;
564 iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
566 if (buf == NULL)
567 continue;
569 if (memcmp(iso_buf, frame_header, 6) == 0 && framelen == 64) {
570 UDIA_DEBUG("Frame Resolution: %dx%d\n",
571 iso_buf[0x3a] << 4, iso_buf[0x3b] << 3);
572 if (buf->buf.bytesused != 0)
573 buf->state = MICRODIA_BUF_STATE_DONE;
574 } else {
575 if (buf->state != MICRODIA_BUF_STATE_ACTIVE)
576 buf->state = MICRODIA_BUF_STATE_ACTIVE;
578 if (framelen + buf->buf.bytesused > queue->frame_size) {
579 UDIA_WARNING("Frame Buffer overflow!\n");
580 dev->vframes_overflow++;
581 buf->state = MICRODIA_BUF_STATE_DONE;
583 framelen = min(queue->frame_size - buf->buf.bytesused,
584 framelen);
585 mem = queue->mem + buf->buf.m.offset +
586 buf->buf.bytesused;
587 memcpy(mem, iso_buf, framelen);
588 buf->buf.bytesused += framelen;
590 if (buf->state == MICRODIA_BUF_STATE_DONE ||
591 buf->state == MICRODIA_BUF_STATE_ERROR) {
592 buf = microdia_queue_next_buffer(queue, buf);
593 if (buf == NULL)
594 dev->vframes_dropped++;
598 urb->dev = dev->udev;
600 ret = usb_submit_urb(urb, GFP_ATOMIC);
602 if (ret != 0) {
603 UDIA_ERROR("Error (%d) re-submitting urb in "
604 "microdia_isoc_handler.\n", ret);
610 * @param dev Device structure
612 * @brief Clean-up all the ISOC buffers
614 * This function permits to clean-up all the ISOC buffers.
616 void usb_microdia_isoc_cleanup(struct usb_microdia *dev)
618 int i;
619 struct urb *urb;
621 UDIA_DEBUG("Isoc cleanup\n");
623 if (dev == NULL)
624 return;
626 for (i = 0; i < MAX_ISO_BUFS; i++) {
627 urb = dev->isobuf[i].urb;
628 if (urb == NULL)
629 continue;
631 usb_kill_urb(urb);
633 kfree(dev->isobuf[i].data);
635 usb_free_urb(urb);
637 dev->isobuf[i].urb = NULL;
644 * @param dev Device structure
645 * @param index Choice of the interface
647 * @returns 0 if all is OK
649 * @brief Send the message SET_FEATURE and choose the interface
651 * This function permits to send the message SET_FEATURE on the USB bus.
653 int usb_microdia_set_feature(struct usb_microdia *dev, int index)
655 int result;
656 struct usb_device *udev = dev->udev;
658 result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
659 USB_REQ_SET_FEATURE,
660 USB_TYPE_STANDARD | USB_DIR_OUT | USB_RECIP_DEVICE,
661 USB_DEVICE_REMOTE_WAKEUP,
662 index,
663 NULL,
665 500);
667 if (result < 0)
668 UDIA_ERROR("SET FEATURE fail !\n");
669 else
670 UDIA_DEBUG("SET FEATURE\n");
672 return result;
677 * @param dev Device structure
679 * @returns 0 if all is OK
681 * @brief Send the message SET_CONFIGURATION
683 * This function permits to send the message SET_CONFIGURATION on the USB bus.
685 int usb_microdia_set_configuration(struct usb_microdia *dev)
687 int result;
688 struct usb_device *udev = dev->udev;
690 result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
691 USB_REQ_SET_CONFIGURATION,
692 USB_TYPE_STANDARD | USB_DIR_OUT | USB_RECIP_DEVICE,
694 udev->config[0].desc.bConfigurationValue,
695 NULL,
697 500);
699 if (result < 0)
700 UDIA_ERROR("SET CONFIGURATION fail !\n");
701 else
702 UDIA_DEBUG("SET CONFIGURATION %d\n", udev->config[0].desc.bConfigurationValue);
704 return result;
709 * @param dev Device structure
710 * @param value register to write to
711 * @param data
712 * @param length number of bytes
714 * @returns 0 if all is OK
716 * @brief Write a 16-bit value to a 16-bit register
718 * This function permits to write a 16-bit value to a 16-bit register on the USB bus.
720 int usb_microdia_control_write(struct usb_microdia *dev, __u16 value, __u8 *data, __u16 length)
722 int result;
723 struct usb_device *udev = dev->udev;
725 result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
726 0x08,
727 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
728 value,
729 0x00,
730 data,
731 length,
732 500);
734 if (result < 0)
735 UDIA_ERROR("Write register failed index = 0x%02X\n", value);
737 return result;
741 * @param dev Device structure
742 * @param index register to read from
743 * @param data
744 * @param length number of bytes
746 * @returns 0 if all is OK
748 * @brief Read a 16-bit value from a 16-bit register
750 * This function permits to read a 16-bit value from a 16-bit register on the USB bus.
752 int usb_microdia_control_read(struct usb_microdia *dev, __u16 index, __u8 *data, __u16 length)
754 int result;
756 struct usb_device *udev = dev->udev;
758 *data = 0;
760 result = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
761 0x00,
762 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
763 index,
764 0x00,
765 data,
766 length,
767 500);
769 if (result < 0)
770 UDIA_ERROR("Read register failed 0x%02X\n", index);
772 return result;
777 * @param dev
779 * @returns 0 if all is OK
781 * @brief Set the default value about the video settings.
783 * This function permits to set the video settings for each video camera model.
786 static int usb_microdia_default_settings(struct usb_microdia *dev)
788 struct v4l2_pix_format *def_fmt;
790 dev->vframes_overflow = 0;
791 dev->vframes_incomplete = 0;
792 dev->vframes_dropped = 0;
794 dev->queue.min_buffers = min_buffers;
795 dev->queue.max_buffers = max_buffers;
797 def_fmt = v4l2_enum_supported_formats(dev, 0);
799 switch (dev->webcam_model) {
800 default:
801 dev->vsettings.fps = fps;
802 dev->vsettings.vflip = vflip;
803 dev->vsettings.hflip = hflip;
804 dev->vsettings.brightness = brightness & 0xffff;
805 dev->vsettings.contrast = contrast & 0xffff;
806 dev->vsettings.whiteness = whiteness & 0xffff;
807 dev->vsettings.exposure = exposure & 0xffff;
808 dev->vsettings.sharpness = sharpness & 0x3f;
809 dev->vsettings.rgb_gain[0] = (rgb_gain >> 16) & 0x7f;
810 dev->vsettings.rgb_gain[1] = (rgb_gain >> 8) & 0x7f;
811 dev->vsettings.rgb_gain[2] = (rgb_gain >> 8) & 0x7f;
812 dev->vsettings.rgb_gain[3] = rgb_gain & 0x7f;
813 dev->vsettings.auto_exposure = auto_exposure & 1;
814 dev->vsettings.auto_whitebalance = auto_whitebalance & 1;
815 dev->vsettings.hue = 0xffff;
817 if (def_fmt) {
818 memcpy(&(dev->vsettings.format),
819 def_fmt,
820 sizeof(struct v4l2_pix_format));
823 sn9c20x_set_resolution(dev, 640, 480);
824 break;
826 return 0;
830 * @param interface
831 * @param id
833 * @returns 0 if all is OK
835 * @brief Load the driver
837 * This function detects the device and allocate the buffers for the device
838 * and the video interface.
840 static int usb_microdia_probe(struct usb_interface *interface, const struct usb_device_id *id)
842 int i;
843 int ret;
844 size_t buffer_size;
846 int vendor_id;
847 int product_id;
848 int bNumInterfaces;
850 struct usb_microdia *dev = NULL;
851 struct usb_device *udev = interface_to_usbdev(interface);
852 struct usb_host_interface *iface_desc;
853 struct usb_endpoint_descriptor *endpoint;
854 struct microdia_camera *camera = NULL;
856 /* Get USB VendorID and ProductID */
857 vendor_id = le16_to_cpu(udev->descriptor.idVendor);
858 product_id = le16_to_cpu(udev->descriptor.idProduct);
860 /* Check if we can handle this device */
861 UDIA_DEBUG("Probe function called with VendorID=%04X, ProductID=%04X and InterfaceNumber=%d\n",
862 vendor_id, product_id, interface->cur_altsetting->desc.bInterfaceNumber);
865 //The interface are probed one by one.
866 // We are interested in the video interface (always the interface '0')
867 // The interfaces '1' or '2' (if presents) are the audio control.
869 if (interface->cur_altsetting->desc.bInterfaceNumber > 0) {
870 ret = -ENODEV;
871 goto error;
874 /* Detect device */
875 camera = find_camera(CAMERA_MODEL(vendor_id, product_id));
876 if (camera == NULL) {
877 UDIA_INFO("Camera %04X:%04X not supported.\n",
878 vendor_id, product_id);
879 ret = -ENODEV;
880 goto error;
883 UDIA_INFO("Microdia USB 2.0 Webcam - %04X:%04X plugged-in.\n",
884 vendor_id, product_id);
888 // Allocate structure, initialize pointers, mutexes, etc.
889 // and link it to the usb_device
891 dev = kzalloc(sizeof(struct usb_microdia), GFP_KERNEL);
893 if (dev == NULL) {
894 UDIA_ERROR("Out of memory !\n");
895 ret = -ENOMEM;
896 goto error;
899 /* Init mutexes, spinlock, etc. */
900 mutex_init(&dev->mutex);
901 kref_init(&dev->vopen);
903 /* Save pointers */
904 dev->webcam_model = camera->model;
905 dev->webcam_type = camera->type;
906 if (dev->webcam_model == CAMERA_MODEL(USB_0C45_VID, USB_6260_PID))
907 dev->frame_size_divisor = MICRODIA_FRAME_SIZE_DIVISOR_6260;
908 else
909 dev->frame_size_divisor = MICRODIA_FRAME_SIZE_DIVISOR_DEFAULT;
910 dev->initialize = camera->initialize;
911 dev->sensor_init = camera->sensor_init;
912 dev->stop_stream = camera->stop_stream;
913 dev->start_stream = camera->start_stream;
914 dev->set_contrast = camera->set_contrast == NULL ?
915 sn9c20x_set_contrast : camera->set_contrast;
916 dev->set_brightness = camera->set_brightness == NULL ?
917 sn9c20x_set_brightness : camera->set_brightness;
918 dev->set_gamma = camera->set_gamma == NULL ?
919 sn9c20x_set_gamma : camera->set_gamma;
920 dev->set_sharpness = camera->set_sharpness == NULL ?
921 sn9c20x_set_sharpness : camera->set_sharpness;
922 dev->set_rgb_gain = camera->set_rgb_gain == NULL ?
923 sn9c20x_set_rgb_gain : camera->set_rgb_gain;
924 dev->set_exposure = camera->set_exposure == NULL ?
925 sn9c20x_set_exposure : camera->set_exposure;
926 if (flip_detect) {
927 UDIA_INFO("Rotate detection enabled\n");
928 dev->flip_detect = camera->flip_detect;
930 dev->set_hvflip = camera->set_hvflip;
931 dev->set_auto_exposure = camera->set_auto_exposure;
932 dev->set_auto_whitebalance = camera->set_auto_whitebalance;
933 dev->udev = udev;
934 dev->interface = interface;
936 dev->sensor_slave_address = camera->sensor_slave_address;
937 dev->sensor_flags = camera->sensor_flags;
938 dev->supported_fmts = camera->supported_fmts;
940 /* Read the product release */
941 dev->release = le16_to_cpu(udev->descriptor.bcdDevice);
942 UDIA_DEBUG("Release: %04x\n", dev->release);
944 /* How many interfaces (1 or 3) ? */
945 bNumInterfaces = udev->config->desc.bNumInterfaces;
946 UDIA_DEBUG("Number of interfaces : %d\n", bNumInterfaces);
949 /* Switch on the camera (to detect size of buffers) */
950 dev_microdia_camera_on(dev);
953 // Set up the endpoint information
954 // use only the first int-in and isoc-in endpoints
955 // for the current alternate setting
957 iface_desc = interface->cur_altsetting;
959 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
960 endpoint = &iface_desc->endpoint[i].desc;
962 if (!dev->int_in_endpointAddr
963 && ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
964 && ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)) {
965 /* we found an interrupt in endpoint */
966 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
968 dev->int_in_size = buffer_size;
969 dev->int_in_endpointAddr = (endpoint->bEndpointAddress & 0xf);
972 if (!dev->isoc_in_endpointAddr
973 && ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
974 && ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_ISOC)) {
975 /* we found an isoc in endpoint */
976 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
978 dev->isoc_in_size = buffer_size;
979 dev->isoc_in_endpointAddr = (endpoint->bEndpointAddress & 0xf);
983 if (!(dev->int_in_endpointAddr && dev->isoc_in_endpointAddr)) {
984 UDIA_ERROR("Could not find both int-in and isoc-in endpoints\n");
985 ret = -ENODEV;
986 goto free_dev;
989 usb_microdia_default_settings(dev);
991 dev_microdia_camera_off(dev);
993 /* Initialize the video device */
994 dev->vdev = video_device_alloc();
996 if (!dev->vdev) {
997 ret = -ENOMEM;
998 goto free_dev;
1001 /* Initialize the camera */
1002 dev_microdia_initialize_device(dev);
1004 /* Register the video device */
1005 ret = v4l_microdia_register_video_device(dev);
1007 if (ret)
1008 goto free_dev;
1010 /* Create the entries in the sys filesystem */
1011 microdia_create_sysfs_files(dev->vdev);
1013 #ifdef CONFIG_MICRODIA_DEBUGFS
1014 microdia_create_debugfs_files(dev);
1015 #endif
1017 /* Save our data pointer in this interface device */
1018 usb_set_intfdata(interface, dev);
1020 return 0;
1022 free_dev:
1023 kref_put(&dev->vopen, usb_microdia_delete);
1024 error:
1025 return ret;
1028 void usb_microdia_delete(struct kref *kref)
1030 struct usb_microdia *dev;
1031 dev = container_of(kref, struct usb_microdia, vopen);
1033 if (dev->vdev != NULL) {
1034 microdia_remove_sysfs_files(dev->vdev);
1035 #ifdef CONFIG_MICRODIA_DEBUGFS
1036 microdia_remove_debugfs_files(dev);
1037 #endif
1038 v4l_microdia_unregister_video_device(dev);
1040 kfree(dev);
1044 * @param interface
1046 * @brief This function is called when the device is disconnected
1047 * or when the kernel module is unloaded.
1049 static void usb_microdia_disconnect(struct usb_interface *interface)
1051 struct usb_microdia *dev = usb_get_intfdata(interface);
1053 UDIA_INFO("Microdia USB 2.0 Webcam unplugged\n");
1055 usb_set_intfdata(interface, NULL);
1057 mutex_lock(&open_lock);
1058 kref_put(&dev->vopen, usb_microdia_delete);
1059 mutex_unlock(&open_lock);
1064 * @var usb_microdia_driver
1066 * This variable contains some callback
1068 static struct usb_driver usb_microdia_driver = {
1069 .name = "usb_microdia_driver",
1070 .probe = usb_microdia_probe,
1071 .disconnect = usb_microdia_disconnect,
1072 .id_table = microdia_table,
1075 module_param(fps, int, 0444); /**< @brief Module parameter frames per second */
1076 module_param(hflip, int, 0444); /**< @brief Module parameter horizontal flip process */
1077 module_param(vflip, int, 0444); /**< @brief Module parameter vertical flip process */
1078 module_param(flip_detect, int, 0444); /**< @brief Module parameter flip detect */
1079 module_param(auto_exposure, int, 0444); /**< @brief Module parameter automatic exposure control */
1080 module_param(auto_whitebalance, int, 0444); /**< @brief Module parameter automatic whitebalance control */
1081 module_param(brightness, int, 0444); /**< @brief Module parameter brightness */
1082 module_param(whiteness, int, 0444); /**< @brief Module parameter whiteness */
1083 module_param(contrast, int, 0444); /**< @brief Module parameter contrast */
1084 module_param(exposure, int, 0444); /**< @brief Module parameter exposure */
1085 module_param(sharpness, int, 0444); /**< @brief Module parameter sharpness */
1086 module_param(rgb_gain, int, 0444); /**< @brief Module parameter red/green/blue gain */
1088 module_param(min_buffers, int, 0444);
1089 module_param(max_buffers, int, 0444);
1091 module_param(log_level, byte, 0444);
1094 * @returns 0 if all is OK
1096 * @brief Initialize the driver.
1098 * This function is called at first.
1099 * This function permits to define the default values from the command line.
1101 static int __init usb_microdia_init(void)
1103 int result;
1105 UDIA_INFO("Microdia USB 2.0 webcam driver loaded\n");
1107 #ifdef CONFIG_MICRODIA_DEBUGFS
1108 microdia_init_debugfs();
1109 #endif
1111 if (fps < 10 || fps > 30) {
1112 UDIA_WARNING("Framerate out of bounds [10-30]! Defaulting to 25\n");
1113 fps = 25;
1116 if (vflip != 0 && vflip != 1) {
1117 UDIA_WARNING("Vertical flip should be 0 or 1! Defaulting to 0\n");
1118 vflip = 0;
1121 if (hflip != 0 && hflip != 1) {
1122 UDIA_WARNING("Horizontal flip should be 0 or 1! Defaulting to 0\n");
1123 hflip = 0;
1126 if (sharpness < 0 || sharpness > 0x3f) {
1127 UDIA_WARNING("Sharpness should be 0 to 63 ! Defaulting to 31\n");
1128 sharpness = 0x1f;
1131 if ((rgb_gain >> 16) < 0 || (rgb_gain >> 16) > 0x7f) {
1132 UDIA_WARNING("Red Gain should be 0 to 127 ! Defaulting to 32\n");
1133 rgb_gain = (rgb_gain & 0x0000ffff) | 0x200000;
1136 if (((rgb_gain >> 8) & 0xFF) < 0 || ((rgb_gain >> 8) & 0xFF) > 0x7f) {
1137 UDIA_WARNING("Green Gain should be 0 to 127 ! Defaulting to 32\n");
1138 rgb_gain = (rgb_gain & 0x00ff00ff) | 0x002000;
1141 if ((rgb_gain & 0xFF) < 0 || (rgb_gain & 0xFF) > 0x7f) {
1142 UDIA_WARNING("Blue Gain should be 0 to 127 ! Defaulting to 32\n");
1143 rgb_gain = (rgb_gain & 0x00ffff00) | 0x20;
1146 if (auto_exposure != 0 && auto_exposure != 1) {
1147 UDIA_WARNING("Automatic exposure should be 0 or 1! "
1148 "Defaulting to 1\n");
1149 auto_exposure = 1;
1152 if (auto_whitebalance != 0 && auto_whitebalance != 1) {
1153 UDIA_WARNING("Automatic whitebalance should be 0 or 1! "
1154 "Defaulting to 1\n");
1155 auto_whitebalance = 1;
1158 if (min_buffers < 2) {
1159 UDIA_WARNING("Minimum buffers can't be less then 2! "
1160 "Defaulting to 2\n");
1161 min_buffers = 2;
1165 if (min_buffers > max_buffers) {
1166 UDIA_WARNING("Minimum buffers must be less then or equal to "
1167 "max buffers! Defaulting to 2, 10\n");
1168 min_buffers = 2;
1169 max_buffers = 5;
1172 /* Register the driver with the USB subsystem */
1173 result = usb_register(&usb_microdia_driver);
1175 if (result)
1176 UDIA_ERROR("usb_register failed ! Error number %d\n", result);
1178 UDIA_INFO(DRIVER_VERSION " : " DRIVER_DESC "\n");
1180 return result;
1185 * @brief Close the driver
1187 * This function is called at last when you unload the driver.
1189 static void __exit usb_microdia_exit(void)
1191 UDIA_INFO("usb_microdia_exit: Microdia USB 2.0 webcam driver unloaded\n");
1193 #ifdef CONFIG_MICRODIA_DEBUGFS
1194 microdia_uninit_debugfs();
1195 #endif
1197 /* Deregister this driver with the USB subsystem */
1198 usb_deregister(&usb_microdia_driver);
1202 module_init(usb_microdia_init); /**< @brief Module initialize */
1203 module_exit(usb_microdia_exit); /**< @brief Module exit */
1206 MODULE_PARM_DESC(fps, "Frames per second [10-30]"); /**< @brief Description of 'fps' parameter */
1207 MODULE_PARM_DESC(hflip, "Horizontal image flip"); /**< @brief Description of 'hflip' parameter */
1208 MODULE_PARM_DESC(vflip, "Vertical image flip"); /**< @brief Description of 'vflip' parameter */
1209 MODULE_PARM_DESC(flip_detect, "Image flip detection"); /**< @brief Description of 'vflip_detect' parameter */
1210 MODULE_PARM_DESC(auto_exposure, "Automatic exposure control"); /**< @brief Description of 'auto_exposure' parameter */
1211 MODULE_PARM_DESC(auto_whitebalance, "Automatic whitebalance"); /**< @brief Description of 'auto_whitebalance' parameter */
1212 MODULE_PARM_DESC(brightness, "Brightness setting"); /**< @brief Description of 'brightness' parameter */
1213 MODULE_PARM_DESC(whiteness, "Whiteness setting"); /**< @brief Description of 'whiteness' parameter */
1214 MODULE_PARM_DESC(exposure, "Exposure setting"); /**< @brief Description of 'exposure' parameter */
1215 MODULE_PARM_DESC(contrast, "Contrast setting"); /**< @brief Description of 'contrast' parameter */
1216 MODULE_PARM_DESC(sharpness, "Sharpness setting"); /**< @brief Description of 'sharpness' parameter */
1217 MODULE_PARM_DESC(rgb_gain, "Red/Green/Blue Gain setting"); /**< @brief Description of 'RGB Gain' parameter */
1219 MODULE_PARM_DESC(min_buffers, "Minimum number of image buffers");
1220 MODULE_PARM_DESC(max_buffers, "Maximum number of image buffers");
1221 MODULE_PARM_DESC(log_level, " <n>\n"
1222 "Driver log level\n"
1223 "1 = info (default)\n"
1224 "2 = warning\n"
1225 "4 = error\n"
1226 "8 = debug\n"
1227 "16 = stream\n");
1229 MODULE_LICENSE("GPL"); /**< @brief Driver is under licence GPL */
1230 MODULE_AUTHOR(DRIVER_AUTHOR); /**< @brief Driver is written by Nicolas VIVIEN */
1231 MODULE_DESCRIPTION(DRIVER_DESC); /**< @brief Define the description of the driver */
1232 MODULE_SUPPORTED_DEVICE(DRIVER_SUPPORT); /**< @brief List of supported device */