Import 2.4.0-test6pre6
[davej-history.git] / drivers / usb / ibmcam.c
blob86360f68796aa179f3f0d98680ae6c2824fcf83f
1 /*
2 * USB IBM C-It Video Camera driver
4 * Supports IBM C-It Video Camera.
6 * This driver is based on earlier work of:
8 * (C) Copyright 1999 Johannes Erdfelt
9 * (C) Copyright 1999 Randy Dunlap
11 * 5/24/00 Removed optional (and unnecessary) locking of the driver while
12 * the device remains plugged in. Corrected race conditions in ibmcam_open
13 * and ibmcam_probe() routines using this as a guideline:
15 * (2) The big kernel lock is automatically released when a process sleeps
16 * in the kernel and is automatically reacquired on reschedule if the
17 * process had the lock originally. Any code that can be compiled as
18 * a module and is entered with the big kernel lock held *MUST*
19 * increment the use count to activate the indirect module protection
20 * before doing anything that might sleep.
22 * In practice, this means that all routines that live in modules and
23 * are invoked under the big kernel lock should do MOD_INC_USE_COUNT
24 * as their very first action. And all failure paths from that
25 * routine must do MOD_DEC_USE_COUNT before returning.
28 #include <linux/kernel.h>
29 #include <linux/sched.h>
30 #include <linux/list.h>
31 #include <linux/malloc.h>
32 #include <linux/mm.h>
33 #include <linux/smp_lock.h>
34 #include <linux/videodev.h>
35 #include <linux/vmalloc.h>
36 #include <linux/wrapper.h>
37 #include <linux/module.h>
38 #include <linux/spinlock.h>
39 #include <linux/usb.h>
41 #include <asm/io.h>
43 #include "ibmcam.h"
45 #define ENABLE_HEXDUMP 0 /* Enable if you need it */
46 static int debug = 0;
48 /* Completion states of the data parser */
49 typedef enum {
50 scan_Continue, /* Just parse next item */
51 scan_NextFrame, /* Frame done, send it to V4L */
52 scan_Out, /* Not enough data for frame */
53 scan_EndParse /* End parsing */
54 } scan_state_t;
56 /* Bit flags (options) */
57 #define FLAGS_RETRY_VIDIOCSYNC (1 << 0)
58 #define FLAGS_MONOCHROME (1 << 1)
59 #define FLAGS_DISPLAY_HINTS (1 << 2)
60 #define FLAGS_OVERLAY_STATS (1 << 3)
61 #define FLAGS_FORCE_TESTPATTERN (1 << 4)
62 #define FLAGS_SEPARATE_FRAMES (1 << 5)
63 #define FLAGS_CLEAN_FRAMES (1 << 6)
65 static int flags = 0; /* FLAGS_DISPLAY_HINTS | FLAGS_OVERLAY_STATS; */
67 /* This is the size of V4L frame that we provide */
68 static const int imgwidth = V4L_FRAME_WIDTH_USED;
69 static const int imgheight = V4L_FRAME_HEIGHT;
70 static const int min_imgwidth = 8;
71 static const int min_imgheight = 4;
73 static int lighting = 1; /* Medium */
75 #define SHARPNESS_MIN 0
76 #define SHARPNESS_MAX 6
77 static int sharpness = 4; /* Low noise, good details */
79 #define FRAMERATE_MIN 0
80 #define FRAMERATE_MAX 6
81 static int framerate = 2; /* Lower, reliable frame rate (8-12 fps) */
83 enum {
84 VIDEOSIZE_128x96 = 0,
85 VIDEOSIZE_176x144,
86 VIDEOSIZE_352x288,
87 VIDEOSIZE_320x240,
88 VIDEOSIZE_352x240,
91 static int videosize = VIDEOSIZE_352x288;
94 * The value of 'scratchbufsize' affects quality of the picture
95 * in many ways. Shorter buffers may cause loss of data when client
96 * is too slow. Larger buffers are memory-consuming and take longer
97 * to work with. This setting can be adjusted, but the default value
98 * should be OK for most desktop users.
100 #define DEFAULT_SCRATCH_BUF_SIZE (0x10000) /* 64 KB */
101 static const int scratchbufsize = DEFAULT_SCRATCH_BUF_SIZE;
104 * Here we define several initialization variables. They may
105 * be used to automatically set color, hue, brightness and
106 * contrast to desired values. This is particularly useful in
107 * case of webcams (which have no controls and no on-screen
108 * output) and also when a client V4L software is used that
109 * does not have some of those controls. In any case it's
110 * good to have startup values as options.
112 * These values are all in [0..255] range. This simplifies
113 * operation. Note that actual values of V4L variables may
114 * be scaled up (as much as << 8). User can see that only
115 * on overlay output, however, or through a V4L client.
117 static int init_brightness = 128;
118 static int init_contrast = 192;
119 static int init_color = 128;
120 static int init_hue = 128;
121 static int hue_correction = 128;
123 /* Settings for camera model 2 */
124 static int init_model2_rg = -1;
125 static int init_model2_rg2 = -1;
126 static int init_model2_sat = -1;
127 static int init_model2_yb = -1;
129 MODULE_PARM(debug, "i");
130 MODULE_PARM_DESC(debug, "Debug level: 0-9 (default=0)");
131 MODULE_PARM(flags, "i");
132 MODULE_PARM_DESC(flags, "Bitfield: 0=VIDIOCSYNC, 1=B/W, 2=show hints, 3=show stats, 4=test pattern, 5=seperate frames, 6=clean frames");
133 MODULE_PARM(framerate, "i");
134 MODULE_PARM_DESC(framerate, "Framerate setting: 0=slowest, 6=fastest (default=2)");
135 MODULE_PARM(lighting, "i");
136 MODULE_PARM_DESC(lighting, "Photosensitivity: 0=bright, 1=medium (default), 2=low light");
137 MODULE_PARM(sharpness, "i");
138 MODULE_PARM_DESC(sharpness, "Model1 noise reduction: 0=smooth, 6=sharp (default=4)");
139 MODULE_PARM(videosize, "i");
140 MODULE_PARM_DESC(videosize, "Image size: 0=128x96, 1=176x144, 2=352x288, 3=320x240, 4=352x240 (default=1)");
141 MODULE_PARM(init_brightness, "i");
142 MODULE_PARM_DESC(init_brightness, "Brightness preconfiguration: 0-255 (default=128)");
143 MODULE_PARM(init_contrast, "i");
144 MODULE_PARM_DESC(init_contrast, "Contrast preconfiguration: 0-255 (default=192)");
145 MODULE_PARM(init_color, "i");
146 MODULE_PARM_DESC(init_color, "Dolor preconfiguration: 0-255 (default=128)");
147 MODULE_PARM(init_hue, "i");
148 MODULE_PARM_DESC(init_hue, "Hue preconfiguration: 0-255 (default=128)");
149 MODULE_PARM(hue_correction, "i");
150 MODULE_PARM_DESC(hue_correction, "YUV colorspace regulation: 0-255 (default=128)");
152 MODULE_PARM(init_model2_rg, "i");
153 MODULE_PARM_DESC(init_model2_rg, "Model2 preconfiguration: 0-255 (default=112)");
154 MODULE_PARM(init_model2_rg2, "i");
155 MODULE_PARM_DESC(init_model2_rg2, "Model2 preconfiguration: 0-255 (default=47)");
156 MODULE_PARM(init_model2_sat, "i");
157 MODULE_PARM_DESC(init_model2_sat, "Model2 preconfiguration: 0-255 (default=52)");
158 MODULE_PARM(init_model2_yb, "i");
159 MODULE_PARM_DESC(init_model2_yb, "Model2 preconfiguration: 0-255 (default=160)");
161 MODULE_AUTHOR ("module author");
162 MODULE_DESCRIPTION ("IBM/Xirlink C-it USB Camera Driver for Linux (c) 2000");
164 /* Still mysterious i2c commands */
165 static const unsigned short unknown_88 = 0x0088;
166 static const unsigned short unknown_89 = 0x0089;
167 static const unsigned short bright_3x[3] = { 0x0031, 0x0032, 0x0033 };
168 static const unsigned short contrast_14 = 0x0014;
169 static const unsigned short light_27 = 0x0027;
170 static const unsigned short sharp_13 = 0x0013;
172 /* i2c commands for Model 2 cameras */
173 static const unsigned short mod2_brightness = 0x001a; /* $5b .. $ee; default=$5a */
174 static const unsigned short mod2_set_framerate = 0x001c; /* 0 (fast).. $1F (slow) */
175 static const unsigned short mod2_color_balance_rg2 = 0x001e; /* 0 (red) .. $7F (green) */
176 static const unsigned short mod2_saturation = 0x0020; /* 0 (b/w) - $7F (full color) */
177 static const unsigned short mod2_color_balance_yb = 0x0022; /* 0..$7F, $50 is about right */
178 static const unsigned short mod2_color_balance_rg = 0x0024; /* 0..$7F, $70 is about right */
179 static const unsigned short mod2_sensitivity = 0x0028; /* 0 (min) .. $1F (max) */
181 #define MAX_IBMCAM 4
183 struct usb_ibmcam cams[MAX_IBMCAM];
185 /*******************************/
186 /* Memory management functions */
187 /*******************************/
189 #define MDEBUG(x) do { } while(0) /* Debug memory management */
191 static struct usb_driver ibmcam_driver;
192 static void usb_ibmcam_release(struct usb_ibmcam *ibmcam);
194 /* Given PGD from the address space's page table, return the kernel
195 * virtual mapping of the physical memory mapped at ADR.
197 static inline unsigned long uvirt_to_kva(pgd_t *pgd, unsigned long adr)
199 unsigned long ret = 0UL;
200 pmd_t *pmd;
201 pte_t *ptep, pte;
203 if (!pgd_none(*pgd)) {
204 pmd = pmd_offset(pgd, adr);
205 if (!pmd_none(*pmd)) {
206 ptep = pte_offset(pmd, adr);
207 pte = *ptep;
208 if (pte_present(pte)) {
209 ret = (unsigned long) page_address(pte_page(pte));
210 ret |= (adr & (PAGE_SIZE - 1));
214 MDEBUG(printk("uv2kva(%lx-->%lx)", adr, ret));
215 return ret;
218 static inline unsigned long uvirt_to_bus(unsigned long adr)
220 unsigned long kva, ret;
222 kva = uvirt_to_kva(pgd_offset(current->mm, adr), adr);
223 ret = virt_to_bus((void *)kva);
224 MDEBUG(printk("uv2b(%lx-->%lx)", adr, ret));
225 return ret;
228 static inline unsigned long kvirt_to_bus(unsigned long adr)
230 unsigned long va, kva, ret;
232 va = VMALLOC_VMADDR(adr);
233 kva = uvirt_to_kva(pgd_offset_k(va), va);
234 ret = virt_to_bus((void *)kva);
235 MDEBUG(printk("kv2b(%lx-->%lx)", adr, ret));
236 return ret;
239 /* Here we want the physical address of the memory.
240 * This is used when initializing the contents of the
241 * area and marking the pages as reserved.
243 static inline unsigned long kvirt_to_pa(unsigned long adr)
245 unsigned long va, kva, ret;
247 va = VMALLOC_VMADDR(adr);
248 kva = uvirt_to_kva(pgd_offset_k(va), va);
249 ret = __pa(kva);
250 MDEBUG(printk("kv2pa(%lx-->%lx)", adr, ret));
251 return ret;
254 static void *rvmalloc(unsigned long size)
256 void *mem;
257 unsigned long adr, page;
259 /* Round it off to PAGE_SIZE */
260 size += (PAGE_SIZE - 1);
261 size &= ~(PAGE_SIZE - 1);
263 mem = vmalloc_32(size);
264 if (!mem)
265 return NULL;
267 memset(mem, 0, size); /* Clear the ram out, no junk to the user */
268 adr = (unsigned long) mem;
269 while (size > 0) {
270 page = kvirt_to_pa(adr);
271 mem_map_reserve(MAP_NR(__va(page)));
272 adr += PAGE_SIZE;
273 if (size > PAGE_SIZE)
274 size -= PAGE_SIZE;
275 else
276 size = 0;
279 return mem;
282 static void rvfree(void *mem, unsigned long size)
284 unsigned long adr, page;
286 if (!mem)
287 return;
289 size += (PAGE_SIZE - 1);
290 size &= ~(PAGE_SIZE - 1);
292 adr=(unsigned long) mem;
293 while (size > 0) {
294 page = kvirt_to_pa(adr);
295 mem_map_unreserve(MAP_NR(__va(page)));
296 adr += PAGE_SIZE;
297 if (size > PAGE_SIZE)
298 size -= PAGE_SIZE;
299 else
300 size = 0;
302 vfree(mem);
305 #if ENABLE_HEXDUMP
306 static void ibmcam_hexdump(const unsigned char *data, int len)
308 char tmp[80];
309 int i, k;
311 for (i=k=0; len > 0; i++, len--) {
312 if (i > 0 && (i%16 == 0)) {
313 printk("%s\n", tmp);
314 k=0;
316 k += sprintf(&tmp[k], "%02x ", data[i]);
318 if (k > 0)
319 printk("%s\n", tmp);
321 #endif
324 * usb_ibmcam_overlaychar()
326 * History:
327 * 1/2/00 Created.
329 void usb_ibmcam_overlaychar(
330 struct usb_ibmcam *ibmcam,
331 struct ibmcam_frame *frame,
332 int x, int y, int ch)
334 static const unsigned short digits[16] = {
335 0xF6DE, /* 0 */
336 0x2492, /* 1 */
337 0xE7CE, /* 2 */
338 0xE79E, /* 3 */
339 0xB792, /* 4 */
340 0xF39E, /* 5 */
341 0xF3DE, /* 6 */
342 0xF492, /* 7 */
343 0xF7DE, /* 8 */
344 0xF79E, /* 9 */
345 0x77DA, /* a */
346 0xD75C, /* b */
347 0xF24E, /* c */
348 0xD6DC, /* d */
349 0xF34E, /* e */
350 0xF348 /* f */
352 unsigned short digit;
353 int ix, iy;
355 if ((ibmcam == NULL) || (frame == NULL))
356 return;
358 if (ch >= '0' && ch <= '9')
359 ch -= '0';
360 else if (ch >= 'A' && ch <= 'F')
361 ch = 10 + (ch - 'A');
362 else if (ch >= 'a' && ch <= 'f')
363 ch = 10 + (ch - 'a');
364 else
365 return;
366 digit = digits[ch];
368 for (iy=0; iy < 5; iy++) {
369 for (ix=0; ix < 3; ix++) {
370 if (digit & 0x8000) {
371 IBMCAM_PUTPIXEL(frame, x+ix, y+iy, 0xFF, 0xFF, 0xFF);
373 digit = digit << 1;
379 * usb_ibmcam_overlaystring()
381 * History:
382 * 1/2/00 Created.
384 void usb_ibmcam_overlaystring(
385 struct usb_ibmcam *ibmcam,
386 struct ibmcam_frame *frame,
387 int x, int y, const char *str)
389 while (*str) {
390 usb_ibmcam_overlaychar(ibmcam, frame, x, y, *str);
391 str++;
392 x += 4; /* 3 pixels character + 1 space */
397 * usb_ibmcam_overlaystats()
399 * Overlays important debugging information.
401 * History:
402 * 1/2/00 Created.
404 void usb_ibmcam_overlaystats(struct usb_ibmcam *ibmcam, struct ibmcam_frame *frame)
406 const int y_diff = 8;
407 char tmp[16];
408 int x = 10;
409 int y = 10;
411 sprintf(tmp, "%8x", ibmcam->frame_num);
412 usb_ibmcam_overlaystring(ibmcam, frame, x, y, tmp);
413 y += y_diff;
415 sprintf(tmp, "%8lx", ibmcam->urb_count);
416 usb_ibmcam_overlaystring(ibmcam, frame, x, y, tmp);
417 y += y_diff;
419 sprintf(tmp, "%8lx", ibmcam->urb_length);
420 usb_ibmcam_overlaystring(ibmcam, frame, x, y, tmp);
421 y += y_diff;
423 sprintf(tmp, "%8lx", ibmcam->data_count);
424 usb_ibmcam_overlaystring(ibmcam, frame, x, y, tmp);
425 y += y_diff;
427 sprintf(tmp, "%8lx", ibmcam->header_count);
428 usb_ibmcam_overlaystring(ibmcam, frame, x, y, tmp);
429 y += y_diff;
431 sprintf(tmp, "%8lx", ibmcam->scratch_ovf_count);
432 usb_ibmcam_overlaystring(ibmcam, frame, x, y, tmp);
433 y += y_diff;
435 sprintf(tmp, "%8lx", ibmcam->iso_skip_count);
436 usb_ibmcam_overlaystring(ibmcam, frame, x, y, tmp);
437 y += y_diff;
439 sprintf(tmp, "%8lx", ibmcam->iso_err_count);
440 usb_ibmcam_overlaystring(ibmcam, frame, x, y, tmp);
441 y += y_diff;
443 sprintf(tmp, "%8x", ibmcam->vpic.colour);
444 usb_ibmcam_overlaystring(ibmcam, frame, x, y, tmp);
445 y += y_diff;
447 sprintf(tmp, "%8x", ibmcam->vpic.hue);
448 usb_ibmcam_overlaystring(ibmcam, frame, x, y, tmp);
449 y += y_diff;
451 sprintf(tmp, "%8x", ibmcam->vpic.brightness >> 8);
452 usb_ibmcam_overlaystring(ibmcam, frame, x, y, tmp);
453 y += y_diff;
455 sprintf(tmp, "%8x", ibmcam->vpic.contrast >> 12);
456 usb_ibmcam_overlaystring(ibmcam, frame, x, y, tmp);
457 y += y_diff;
459 sprintf(tmp, "%8d", ibmcam->vpic.whiteness >> 8);
460 usb_ibmcam_overlaystring(ibmcam, frame, x, y, tmp);
461 y += y_diff;
465 * usb_ibmcam_testpattern()
467 * Procedure forms a test pattern (yellow grid on blue background).
469 * Parameters:
470 * fullframe: if TRUE then entire frame is filled, otherwise the procedure
471 * continues from the current scanline.
472 * pmode 0: fill the frame with solid blue color (like on VCR or TV)
473 * 1: Draw a colored grid
475 * History:
476 * 1/2/00 Created.
478 void usb_ibmcam_testpattern(struct usb_ibmcam *ibmcam, int fullframe, int pmode)
480 static const char proc[] = "usb_ibmcam_testpattern";
481 struct ibmcam_frame *frame;
482 unsigned char *f;
483 int num_cell = 0;
484 int scan_length = 0;
485 static int num_pass = 0;
487 if (ibmcam == NULL) {
488 printk(KERN_ERR "%s: ibmcam == NULL\n", proc);
489 return;
491 if ((ibmcam->curframe < 0) || (ibmcam->curframe >= IBMCAM_NUMFRAMES)) {
492 printk(KERN_ERR "%s: ibmcam->curframe=%d.\n", proc, ibmcam->curframe);
493 return;
496 /* Grab the current frame */
497 frame = &ibmcam->frame[ibmcam->curframe];
499 /* Optionally start at the beginning */
500 if (fullframe) {
501 frame->curline = 0;
502 frame->scanlength = 0;
505 /* Form every scan line */
506 for (; frame->curline < imgheight; frame->curline++) {
507 int i;
509 f = frame->data + (imgwidth * 3 * frame->curline);
510 for (i=0; i < imgwidth; i++) {
511 unsigned char cb=0x80;
512 unsigned char cg = 0;
513 unsigned char cr = 0;
515 if (pmode == 1) {
516 if (frame->curline % 32 == 0)
517 cb = 0, cg = cr = 0xFF;
518 else if (i % 32 == 0) {
519 if (frame->curline % 32 == 1)
520 num_cell++;
521 cb = 0, cg = cr = 0xFF;
522 } else {
523 cb = ((num_cell*7) + num_pass) & 0xFF;
524 cg = ((num_cell*5) + num_pass*2) & 0xFF;
525 cr = ((num_cell*3) + num_pass*3) & 0xFF;
527 } else {
528 /* Just the blue screen */
531 *f++ = cb;
532 *f++ = cg;
533 *f++ = cr;
534 scan_length += 3;
538 frame->grabstate = FRAME_DONE;
539 frame->scanlength += scan_length;
540 ++num_pass;
542 /* We do this unconditionally, regardless of FLAGS_OVERLAY_STATS */
543 usb_ibmcam_overlaystats(ibmcam, frame);
546 static unsigned char *ibmcam_model1_find_header(unsigned char hdr_sig, unsigned char *data, int len)
548 while (len >= 4)
550 if ((data[0] == 0x00) && (data[1] == 0xFF) && (data[2] == 0x00))
552 #if 0
553 /* This code helps to detect new frame markers */
554 printk(KERN_DEBUG "Header sig: 00 FF 00 %02X\n", data[3]);
555 #endif
556 if (data[3] == hdr_sig) {
557 if (debug > 2)
558 printk(KERN_DEBUG "Header found.\n");
559 return data+4;
562 ++data;
563 --len;
565 return NULL;
568 static unsigned char *ibmcam_model2_find_header(unsigned char hdr_sig, unsigned char *data, int len)
570 int marker_len = 0;
572 switch (videosize) {
573 case VIDEOSIZE_176x144:
574 marker_len = 10;
575 break;
576 default:
577 marker_len = 2;
578 break;
580 while (len >= marker_len)
582 if ((data[0] == 0x00) && (data[1] == 0xFF))
584 #if 0
585 /* This code helps to detect new frame markers */
586 static int pass = 0;
587 if (pass++ == 0)
588 ibmcam_hexdump(data, (len > 16) ? 16 : len);
589 #endif
590 if (debug > 2)
591 printk(KERN_DEBUG "Header found.\n");
592 return data+marker_len;
594 ++data;
595 --len;
597 return NULL;
600 /* How much data is left in the scratch buf? */
601 #define scratch_left(x) (ibmcam->scratchlen - (int)((char *)x - (char *)ibmcam->scratch))
603 /* Grab the remaining */
604 static void usb_ibmcam_align_scratch(struct usb_ibmcam *ibmcam, unsigned char *data)
606 unsigned long left;
608 left = scratch_left(data);
609 memmove(ibmcam->scratch, data, left);
610 ibmcam->scratchlen = left;
614 * usb_ibmcam_find_header()
616 * Locate one of supported header markers in the scratch buffer.
617 * Once found, remove all preceding bytes AND the marker (4 bytes)
618 * from the scratch buffer. Whatever follows must be video lines.
620 * History:
621 * 1/21/00 Created.
623 static scan_state_t usb_ibmcam_find_header(struct usb_ibmcam *ibmcam)
625 struct ibmcam_frame *frame;
626 unsigned char *data, *tmp;
628 data = ibmcam->scratch;
629 frame = &ibmcam->frame[ibmcam->curframe];
631 if (ibmcam->camera_model == IBMCAM_MODEL_1)
632 tmp = ibmcam_model1_find_header(frame->hdr_sig, data, scratch_left(data));
633 else if (ibmcam->camera_model == IBMCAM_MODEL_2)
634 tmp = ibmcam_model2_find_header(frame->hdr_sig, data, scratch_left(data));
635 else
636 tmp = NULL;
638 if (tmp == NULL) {
639 /* No header - entire scratch buffer is useless! */
640 if (debug > 2)
641 printk(KERN_DEBUG "Skipping frame, no header\n");
642 ibmcam->scratchlen = 0;
643 return scan_EndParse;
645 /* Header found */
646 data = tmp;
648 ibmcam->has_hdr = 1;
649 ibmcam->header_count++;
650 frame->scanstate = STATE_LINES;
651 frame->curline = 0;
653 if (flags & FLAGS_FORCE_TESTPATTERN) {
654 usb_ibmcam_testpattern(ibmcam, 1, 1);
655 return scan_NextFrame;
657 usb_ibmcam_align_scratch(ibmcam, data);
658 return scan_Continue;
662 * usb_ibmcam_parse_lines()
664 * Parse one line (TODO: more than one!) from the scratch buffer, put
665 * decoded RGB value into the current frame buffer and add the written
666 * number of bytes (RGB) to the *pcopylen.
668 * History:
669 * 1/21/00 Created.
671 static scan_state_t usb_ibmcam_parse_lines(struct usb_ibmcam *ibmcam, long *pcopylen)
673 struct ibmcam_frame *frame;
674 unsigned char *data, *f, *chromaLine;
675 unsigned int len;
676 const int v4l_linesize = imgwidth * V4L_BYTES_PER_PIXEL; /* V4L line offset */
677 const int hue_corr = (ibmcam->vpic.hue - 0x8000) >> 10; /* -32..+31 */
678 const int hue2_corr = (hue_correction - 128) / 4; /* -32..+31 */
679 const int ccm = 128; /* Color correction median - see below */
680 int y, u, v, i, frame_done=0, mono_plane, color_corr;
682 color_corr = (ibmcam->vpic.colour - 0x8000) >> 8; /* -128..+127 = -ccm..+(ccm-1)*/
683 RESTRICT_TO_RANGE(color_corr, -ccm, ccm+1);
684 data = ibmcam->scratch;
685 frame = &ibmcam->frame[ibmcam->curframe];
687 len = frame->frmwidth * 3; /* 1 line of mono + 1 line of color */
688 /*printk(KERN_DEBUG "len=%d. left=%d.\n",len,scratch_left(data));*/
690 mono_plane = ((frame->curline & 1) == 0);
693 * Lines are organized this way (or are they?)
695 * I420:
696 * ~~~~
697 * ___________________________________
698 * |-----Y-----|---UVUVUV...UVUV-----| \
699 * |-----------+---------------------| \
700 * |<-- 176 -->|<------ 176*2 ------>| Total 72. pairs of lines
701 * |... ... ...| /
702 * |___________|_____________________| /
703 * - odd line- ------- even line ---
705 * another format:
706 * ~~~~~~~~~~~~~~
707 * ___________________________________
708 * |-----Y-----|---UVUVUV...UVUV-----| \
709 * |-----------+---------------------| \
710 * |<-- 352 -->|<------ 352*2 ------>| Total 144. pairs of lines
711 * |... ... ...| /
712 * |___________|_____________________| /
713 * - odd line- ------- even line ---
716 /* Make sure there's enough data for the entire line */
717 if (scratch_left(data) < (len+1024)) {
718 /*printk(KERN_DEBUG "out of data, need %u.\n", len);*/
719 return scan_Out;
722 #if 0
723 { /* This code prints beginning of the source frame */
724 static int pass = 0;
725 if ((pass++ % 3000) == 0)
726 ibmcam_hexdump(data, 16);
728 #endif
730 #if 0
731 if (frame->curline == 10 || frame->curline == 11) {
732 /* This code prints beginning of 10th (mono), 11th (chroma) line */
733 static int pass = 0;
734 if ((pass % 100) == 0)
735 ibmcam_hexdump(data, 16);
736 if (frame->curline == 11)
737 pass++;
739 #endif
741 * Make sure that our writing into output buffer
742 * will not exceed the buffer. Mind that we may write
743 * not into current output scanline but in several after
744 * it as well (if we enlarge image vertically.)
746 if ((frame->curline + 1) >= V4L_FRAME_HEIGHT)
747 return scan_NextFrame;
750 * Now we are sure that entire line (representing all 'frame->frmwidth'
751 * pixels from the camera) is available in the scratch buffer. We
752 * start copying the line left-aligned to the V4L buffer (which
753 * might be larger - not smaller, hopefully). If the camera
754 * line is shorter then we should pad the V4L buffer with something
755 * (black in this case) to complete the line.
757 f = frame->data + (v4l_linesize * frame->curline);
760 * chromaLine points to 1st pixel of the line with chrominance.
761 * If current line is monochrome then chromaLine points to next
762 * line after monochrome one. If current line has chrominance
763 * then chromaLine points to this very line. Such method allows
764 * to access chrominance data uniformly.
766 * To obtain chrominance data from the 'chromaLine' use this:
767 * v = chromaLine[0]; // 0-1:[0], 2-3:[4], 4-5:[8]...
768 * u = chromaLine[2]; // 0-1:[2], 2-3:[6], 4-5:[10]...
770 * Indices must be calculated this way:
771 * v_index = (i >> 1) << 2;
772 * u_index = (i >> 1) << 2 + 2;
774 * where 'i' is the column number [0..frame->frmwidth-1]
776 chromaLine = data;
777 if (mono_plane)
778 chromaLine += frame->frmwidth;
780 for (i = 0; i < frame->frmwidth; i++, data += (mono_plane ? 1 : 2))
782 unsigned char rv, gv, bv; /* RGB components */
785 * Search for potential Start-Of-Frame marker. It should
786 * not be here, of course, but if your formats don't match
787 * you might exceed the frame. We must match the marker to
788 * each byte of multi-byte data element if it is multi-byte.
790 #if 1
791 if ((ibmcam->camera_model == IBMCAM_MODEL_1) && (scratch_left(data) >= (4+2))) {
792 unsigned char *dp;
793 int j;
795 for (j=0, dp=data; j < 2; j++, dp++) {
796 if ((dp[0] == 0x00) && (dp[1] == 0xFF) &&
797 (dp[2] == 0x00) && (dp[3] == frame->hdr_sig)) {
798 ibmcam->has_hdr = 2;
799 frame_done++;
800 break;
804 #endif
806 /* Check for various visual debugging hints (colorized pixels) */
807 if ((flags & FLAGS_DISPLAY_HINTS) && (ibmcam->has_hdr)) {
809 * This is bad and should not happen. This means that
810 * we somehow overshoot the line and encountered new
811 * frame! Obviously our camera/V4L frame size is out
812 * of whack. This cyan dot will help you to figure
813 * out where exactly the new frame arrived.
815 if (ibmcam->has_hdr == 1) {
816 bv = 0; /* Yellow marker */
817 gv = 0xFF;
818 rv = 0xFF;
819 } else {
820 bv = 0xFF; /* Cyan marker */
821 gv = 0xFF;
822 rv = 0;
824 ibmcam->has_hdr = 0;
825 goto make_pixel;
828 if (mono_plane || frame->order_yc)
829 y = data[0];
830 else
831 y = data[1];
833 if (flags & FLAGS_MONOCHROME) /* Use monochrome for debugging */
834 rv = gv = bv = y;
835 else {
836 int off_0, off_2;
838 off_0 = (i >> 1) << 2;
839 off_2 = off_0 + 2;
841 if (frame->order_yc) {
842 off_0++;
843 off_2++;
845 if (!frame->order_uv) {
846 off_0 += 2;
847 off_2 -= 2;
849 u = chromaLine[off_0] + hue_corr;
850 v = chromaLine[off_2] + hue2_corr;
852 /* Apply color correction */
853 if (color_corr != 0) {
854 /* Magnify up to 2 times, reduce down to zero saturation */
855 u = 128 + ((ccm + color_corr) * (u - 128)) / ccm;
856 v = 128 + ((ccm + color_corr) * (v - 128)) / ccm;
858 YUV_TO_RGB_BY_THE_BOOK(y, u, v, rv, gv, bv);
861 make_pixel:
863 * The purpose of creating the pixel here, in one,
864 * dedicated place is that we may need to make the
865 * pixel wider and taller than it actually is. This
866 * may be used if camera generates small frames for
867 * sake of frame rate (or any other reason.)
869 * The output data consists of B, G, R bytes
870 * (in this order).
872 #if USES_IBMCAM_PUTPIXEL
873 IBMCAM_PUTPIXEL(frame, i, frame->curline, rv, gv, bv);
874 #else
875 *f++ = bv;
876 *f++ = gv;
877 *f++ = rv;
878 #endif
880 * Typically we do not decide within a legitimate frame
881 * that we want to end the frame. However debugging code
882 * may detect marker of new frame within the data. Then
883 * this condition activates. The 'data' pointer is already
884 * pointing at the new marker, so we'd better leave it as is.
886 if (frame_done)
887 break; /* End scanning of lines */
890 * Account for number of bytes that we wrote into output V4L frame.
891 * We do it here, after we are done with the scanline, because we
892 * may fill more than one output scanline if we do vertical
893 * enlargement.
895 frame->curline++;
896 *pcopylen += v4l_linesize;
897 usb_ibmcam_align_scratch(ibmcam, data);
899 if (frame_done || (frame->curline >= frame->frmheight))
900 return scan_NextFrame;
901 else
902 return scan_Continue;
906 * usb_ibmcam_model2_parse_lines()
908 * This procedure deals with a weird RGB format that is produced by IBM
909 * camera model 2 in modes 320x240 and above; 'x' below is 159 or 175,
910 * depending on horizontal size of the picture:
912 * <--- 160 or 176 pairs of RA,RB bytes ----->
913 * *-----------------------------------------* \
914 * | RA0 | RB0 | RA1 | RB1 | ... | RAx | RBx | \
915 * |-----+-----+-----+-----+ ... +-----+-----| *- This is pair of horizontal lines,
916 * | B0 | G0 | B1 | G1 | ... | Bx | Gx | / total 240 or 288 lines (120 or 144
917 * |=====+=====+=====+=====+ ... +=====+=====| / such pairs).
919 * Each group of FOUR bytes (RAi, RBi, Bi, Gi) where i=0..frame_width/2-1
920 * defines ONE pixel. Therefore this format yields 176x144 "decoded"
921 * resolution at best. I do not know why camera sends such format - the
922 * previous model just used I420 and everyone was happy.
924 * I do not know what is the difference between RAi and RBi bytes. Both
925 * seemingly represent R component, but slightly vary in value (so that
926 * the picture looks a bit colored if one or another is used). I use
927 * them both as R component in attempt to at least partially recover the
928 * lost resolution.
930 static scan_state_t usb_ibmcam_model2_parse_lines(struct usb_ibmcam *ibmcam, long *pcopylen)
932 struct ibmcam_frame *frame;
933 unsigned char *data, *f, *la, *lb;
934 unsigned int len;
935 const int v4l_linesize = imgwidth * V4L_BYTES_PER_PIXEL; /* V4L line offset */
936 int i, j, frame_done=0, color_corr;
938 color_corr = (ibmcam->vpic.colour) >> 8; /* 0..+255 */
940 data = ibmcam->scratch;
941 frame = &ibmcam->frame[ibmcam->curframe];
943 /* Here we deal with pairs of horizontal lines */
945 len = frame->frmwidth * 2; /* 2 lines */
946 /*printk(KERN_DEBUG "len=%d. left=%d.\n",len,scratch_left(data));*/
948 /* Make sure there's enough data for the entire line */
949 if (scratch_left(data) < (len+32)) {
950 /*printk(KERN_DEBUG "out of data, need %u.\n", len);*/
951 return scan_Out;
955 * Make sure that our writing into output buffer
956 * will not exceed the buffer. Mind that we may write
957 * not into current output scanline but in several after
958 * it as well (if we enlarge image vertically.)
960 if ((frame->curline + 1) >= V4L_FRAME_HEIGHT)
961 return scan_NextFrame;
963 if ((frame->curline & 1) == 0) {
964 la = data;
965 lb = data + frame->frmwidth;
966 } else {
967 la = data + frame->frmwidth;
968 lb = data;
972 * Now we are sure that entire line (representing all 'frame->frmwidth'
973 * pixels from the camera) is available in the scratch buffer. We
974 * start copying the line left-aligned to the V4L buffer (which
975 * might be larger - not smaller, hopefully). If the camera
976 * line is shorter then we should pad the V4L buffer with something
977 * (black in this case) to complete the line.
979 f = frame->data + (v4l_linesize * frame->curline);
981 /* Fill the 2-line strip */
982 for (i = 0; i < frame->frmwidth; i++) {
983 int y, rv, gv, bv; /* RGB components */
985 j = i & (~1);
987 /* Check for various visual debugging hints (colorized pixels) */
988 if ((flags & FLAGS_DISPLAY_HINTS) && (ibmcam->has_hdr)) {
989 if (ibmcam->has_hdr == 1) {
990 bv = 0; /* Yellow marker */
991 gv = 0xFF;
992 rv = 0xFF;
993 } else {
994 bv = 0xFF; /* Cyan marker */
995 gv = 0xFF;
996 rv = 0;
998 ibmcam->has_hdr = 0;
999 goto make_pixel;
1003 * Here I use RA and RB components, one per physical pixel.
1004 * This causes fine vertical grid on the picture but may improve
1005 * horizontal resolution. If you prefer replicating, use this:
1006 * rv = la[j + 0]; ... or ... rv = la[j + 1];
1007 * then the pixel will be replicated.
1009 rv = la[i];
1010 gv = lb[j + 1];
1011 bv = lb[j + 0];
1013 y = (rv + gv + bv) / 3; /* Brightness (badly calculated) */
1015 if (flags & FLAGS_MONOCHROME) /* Use monochrome for debugging */
1016 rv = gv = bv = y;
1017 else if (color_corr != 128) {
1019 /* Calculate difference between color and brightness */
1020 rv -= y;
1021 gv -= y;
1022 bv -= y;
1024 /* Scale differences */
1025 rv = (rv * color_corr) / 128;
1026 gv = (gv * color_corr) / 128;
1027 bv = (bv * color_corr) / 128;
1029 /* Reapply brightness */
1030 rv += y;
1031 gv += y;
1032 bv += y;
1034 /* Watch for overflows */
1035 RESTRICT_TO_RANGE(rv, 0, 255);
1036 RESTRICT_TO_RANGE(gv, 0, 255);
1037 RESTRICT_TO_RANGE(bv, 0, 255);
1040 make_pixel:
1041 IBMCAM_PUTPIXEL(frame, i, frame->curline, rv, gv, bv);
1042 IBMCAM_PUTPIXEL(frame, i, frame->curline+1, rv, gv, bv);
1045 * Account for number of bytes that we wrote into output V4L frame.
1046 * We do it here, after we are done with the scanline, because we
1047 * may fill more than one output scanline if we do vertical
1048 * enlargement.
1050 frame->curline += 2;
1051 *pcopylen += v4l_linesize * 2;
1052 data += frame->frmwidth * 2;
1053 usb_ibmcam_align_scratch(ibmcam, data);
1055 if (frame_done || (frame->curline >= frame->frmheight))
1056 return scan_NextFrame;
1057 else
1058 return scan_Continue;
1062 * ibmcam_parse_data()
1064 * Generic routine to parse the scratch buffer. It employs either
1065 * usb_ibmcam_find_header() or usb_ibmcam_parse_lines() to do most
1066 * of work.
1068 * History:
1069 * 1/21/00 Created.
1071 static void ibmcam_parse_data(struct usb_ibmcam *ibmcam)
1073 struct ibmcam_frame *frame;
1074 unsigned char *data = ibmcam->scratch;
1075 scan_state_t newstate;
1076 long copylen = 0;
1078 /* Grab the current frame and the previous frame */
1079 frame = &ibmcam->frame[ibmcam->curframe];
1081 /* printk(KERN_DEBUG "parsing %u.\n", ibmcam->scratchlen); */
1083 while (1) {
1085 newstate = scan_Out;
1086 if (scratch_left(data)) {
1087 if (frame->scanstate == STATE_SCANNING)
1088 newstate = usb_ibmcam_find_header(ibmcam);
1089 else if (frame->scanstate == STATE_LINES) {
1090 if ((ibmcam->camera_model == IBMCAM_MODEL_2) &&
1091 (videosize >= VIDEOSIZE_352x288)) {
1092 newstate = usb_ibmcam_model2_parse_lines(ibmcam, &copylen);
1094 else {
1095 newstate = usb_ibmcam_parse_lines(ibmcam, &copylen);
1099 if (newstate == scan_Continue)
1100 continue;
1101 else if ((newstate == scan_NextFrame) || (newstate == scan_Out))
1102 break;
1103 else
1104 return; /* scan_EndParse */
1107 if (newstate == scan_NextFrame) {
1108 frame->grabstate = FRAME_DONE;
1109 ibmcam->curframe = -1;
1110 ibmcam->frame_num++;
1112 /* Optionally display statistics on the screen */
1113 if (flags & FLAGS_OVERLAY_STATS)
1114 usb_ibmcam_overlaystats(ibmcam, frame);
1116 /* This will cause the process to request another frame. */
1117 if (waitqueue_active(&frame->wq))
1118 wake_up_interruptible(&frame->wq);
1121 /* Update the frame's uncompressed length. */
1122 frame->scanlength += copylen;
1126 * Make all of the blocks of data contiguous
1128 static int ibmcam_compress_isochronous(struct usb_ibmcam *ibmcam, urb_t *urb)
1130 unsigned char *cdata, *data, *data0;
1131 int i, totlen = 0;
1133 data = data0 = ibmcam->scratch + ibmcam->scratchlen;
1134 for (i = 0; i < urb->number_of_packets; i++) {
1135 int n = urb->iso_frame_desc[i].actual_length;
1136 int st = urb->iso_frame_desc[i].status;
1138 cdata = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
1140 /* Detect and ignore errored packets */
1141 if (st < 0) {
1142 if (debug >= 1) {
1143 printk(KERN_ERR "ibmcam data error: [%d] len=%d, status=%X\n",
1144 i, n, st);
1146 ibmcam->iso_err_count++;
1147 continue;
1150 /* Detect and ignore empty packets */
1151 if (n <= 0) {
1152 ibmcam->iso_skip_count++;
1153 continue;
1157 * If camera continues to feed us with data but there is no
1158 * consumption (if, for example, V4L client fell asleep) we
1159 * may overflow the buffer. We have to move old data over to
1160 * free room for new data. This is bad for old data. If we
1161 * just drop new data then it's bad for new data... choose
1162 * your favorite evil here.
1164 if ((ibmcam->scratchlen + n) > scratchbufsize) {
1165 #if 0
1166 ibmcam->scratch_ovf_count++;
1167 if (debug >= 3)
1168 printk(KERN_ERR "ibmcam: scratch buf overflow! "
1169 "scr_len: %d, n: %d\n", ibmcam->scratchlen, n );
1170 return totlen;
1171 #else
1172 int mv;
1174 ibmcam->scratch_ovf_count++;
1175 if (debug >= 3) {
1176 printk(KERN_ERR "ibmcam: scratch buf overflow! "
1177 "scr_len: %d, n: %d\n", ibmcam->scratchlen, n );
1179 mv = (ibmcam->scratchlen + n) - scratchbufsize;
1180 if (ibmcam->scratchlen >= mv) {
1181 int newslen = ibmcam->scratchlen - mv;
1182 memmove(ibmcam->scratch, ibmcam->scratch + mv, newslen);
1183 ibmcam->scratchlen = newslen;
1184 data = data0 = ibmcam->scratch + ibmcam->scratchlen;
1185 } else {
1186 printk(KERN_ERR "ibmcam: scratch buf too small\n");
1187 return totlen;
1189 #endif
1192 /* Now we know that there is enough room in scratch buffer */
1193 memmove(data, cdata, n);
1194 data += n;
1195 totlen += n;
1196 ibmcam->scratchlen += n;
1198 #if 0
1199 if (totlen > 0) {
1200 static int foo=0;
1201 if (foo < 1) {
1202 printk(KERN_DEBUG "+%d.\n", totlen);
1203 ibmcam_hexdump(data0, (totlen > 64) ? 64:totlen);
1204 ++foo;
1207 #endif
1208 return totlen;
1211 static void ibmcam_isoc_irq(struct urb *urb)
1213 int len;
1214 struct usb_ibmcam *ibmcam = urb->context;
1215 struct ibmcam_sbuf *sbuf;
1216 int i;
1218 /* We don't want to do anything if we are about to be removed! */
1219 if (!IBMCAM_IS_OPERATIONAL(ibmcam))
1220 return;
1222 #if 0
1223 if (urb->actual_length > 0) {
1224 printk(KERN_DEBUG "ibmcam_isoc_irq: %p status %d, "
1225 " errcount = %d, length = %d\n", urb, urb->status,
1226 urb->error_count, urb->actual_length);
1227 } else {
1228 static int c = 0;
1229 if (c++ % 100 == 0)
1230 printk(KERN_DEBUG "ibmcam_isoc_irq: no data\n");
1232 #endif
1234 if (!ibmcam->streaming) {
1235 if (debug >= 1)
1236 printk(KERN_DEBUG "ibmcam: oops, not streaming, but interrupt\n");
1237 return;
1240 sbuf = &ibmcam->sbuf[ibmcam->cursbuf];
1242 /* Copy the data received into our scratch buffer */
1243 len = ibmcam_compress_isochronous(ibmcam, urb);
1245 ibmcam->urb_count++;
1246 ibmcam->urb_length = len;
1247 ibmcam->data_count += len;
1249 #if 0 /* This code prints few initial bytes of ISO data: used to decode markers */
1250 if (ibmcam->urb_count % 64 == 1) {
1251 if (ibmcam->urb_count == 1) {
1252 ibmcam_hexdump(ibmcam->scratch,
1253 (ibmcam->scratchlen > 32) ? 32 : ibmcam->scratchlen);
1256 #endif
1258 /* If we collected enough data let's parse! */
1259 if (ibmcam->scratchlen) {
1260 /* If we don't have a frame we're current working on, complain */
1261 if (ibmcam->curframe >= 0)
1262 ibmcam_parse_data(ibmcam);
1263 else {
1264 if (debug >= 1)
1265 printk(KERN_DEBUG "ibmcam: received data, but no frame available\n");
1269 for (i = 0; i < FRAMES_PER_DESC; i++) {
1270 sbuf->urb->iso_frame_desc[i].status = 0;
1271 sbuf->urb->iso_frame_desc[i].actual_length = 0;
1274 /* Move to the next sbuf */
1275 ibmcam->cursbuf = (ibmcam->cursbuf + 1) % IBMCAM_NUMSBUF;
1277 return;
1281 * usb_ibmcam_veio()
1283 * History:
1284 * 1/27/00 Added check for dev == NULL; this happens if camera is unplugged.
1286 static int usb_ibmcam_veio(
1287 struct usb_ibmcam *ibmcam,
1288 unsigned char req,
1289 unsigned short value,
1290 unsigned short index)
1292 static const char proc[] = "usb_ibmcam_veio";
1293 unsigned char cp[8] /* = { 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef } */;
1294 int i;
1296 if (!IBMCAM_IS_OPERATIONAL(ibmcam))
1297 return 0;
1299 if (req == 1) {
1300 i = usb_control_msg(
1301 ibmcam->dev,
1302 usb_rcvctrlpipe(ibmcam->dev, 0),
1303 req,
1304 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT,
1305 value,
1306 index,
1308 sizeof(cp),
1309 HZ);
1310 #if 0
1311 printk(KERN_DEBUG "USB => %02x%02x%02x%02x%02x%02x%02x%02x "
1312 "(req=$%02x val=$%04x ind=$%04x)\n",
1313 cp[0],cp[1],cp[2],cp[3],cp[4],cp[5],cp[6],cp[7],
1314 req, value, index);
1315 #endif
1316 } else {
1317 i = usb_control_msg(
1318 ibmcam->dev,
1319 usb_sndctrlpipe(ibmcam->dev, 0),
1320 req,
1321 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT,
1322 value,
1323 index,
1324 NULL,
1326 HZ);
1328 if (i < 0) {
1329 printk(KERN_ERR "%s: ERROR=%d. Camera stopped - "
1330 "reconnect or reload driver.\n", proc, i);
1331 ibmcam->last_error = i;
1333 return i;
1337 * usb_ibmcam_calculate_fps()
1339 * This procedure roughly calculates the real frame rate based
1340 * on FPS code (framerate=NNN option). Actual FPS differs
1341 * slightly depending on lighting conditions, so that actual frame
1342 * rate is determined by the camera. Since I don't know how to ask
1343 * the camera what FPS is now I have to use the FPS code instead.
1345 * The FPS code is in range [0..6], 0 is slowest, 6 is fastest.
1346 * Corresponding real FPS should be in range [3..30] frames per second.
1347 * The conversion formula is obvious:
1349 * real_fps = 3 + (fps_code * 4.5)
1351 * History:
1352 * 1/18/00 Created.
1354 static int usb_ibmcam_calculate_fps(void)
1356 return 3 + framerate*4 + framerate/2;
1360 * usb_ibmcam_send_FF_04_02()
1362 * This procedure sends magic 3-command prefix to the camera.
1363 * The purpose of this prefix is not known.
1365 * History:
1366 * 1/2/00 Created.
1368 static void usb_ibmcam_send_FF_04_02(struct usb_ibmcam *ibmcam)
1370 usb_ibmcam_veio(ibmcam, 0, 0x00FF, 0x0127);
1371 usb_ibmcam_veio(ibmcam, 0, 0x0004, 0x0124);
1372 usb_ibmcam_veio(ibmcam, 0, 0x0002, 0x0124);
1375 static void usb_ibmcam_send_00_04_06(struct usb_ibmcam *ibmcam)
1377 usb_ibmcam_veio(ibmcam, 0, 0x0000, 0x0127);
1378 usb_ibmcam_veio(ibmcam, 0, 0x0004, 0x0124);
1379 usb_ibmcam_veio(ibmcam, 0, 0x0006, 0x0124);
1382 static void usb_ibmcam_send_x_00(struct usb_ibmcam *ibmcam, unsigned short x)
1384 usb_ibmcam_veio(ibmcam, 0, x, 0x0127);
1385 usb_ibmcam_veio(ibmcam, 0, 0x0000, 0x0124);
1388 static void usb_ibmcam_send_x_00_05(struct usb_ibmcam *ibmcam, unsigned short x)
1390 usb_ibmcam_send_x_00(ibmcam, x);
1391 usb_ibmcam_veio(ibmcam, 0, 0x0005, 0x0124);
1394 static void usb_ibmcam_send_x_00_05_02(struct usb_ibmcam *ibmcam, unsigned short x)
1396 usb_ibmcam_veio(ibmcam, 0, x, 0x0127);
1397 usb_ibmcam_veio(ibmcam, 0, 0x0000, 0x0124);
1398 usb_ibmcam_veio(ibmcam, 0, 0x0005, 0x0124);
1399 usb_ibmcam_veio(ibmcam, 0, 0x0002, 0x0124);
1402 static void usb_ibmcam_send_x_01_00_05(struct usb_ibmcam *ibmcam, unsigned short x)
1404 usb_ibmcam_veio(ibmcam, 0, x, 0x0127);
1405 usb_ibmcam_veio(ibmcam, 0, 0x0001, 0x0124);
1406 usb_ibmcam_veio(ibmcam, 0, 0x0000, 0x0124);
1407 usb_ibmcam_veio(ibmcam, 0, 0x0005, 0x0124);
1410 static void usb_ibmcam_send_x_00_05_02_01(struct usb_ibmcam *ibmcam, unsigned short x)
1412 usb_ibmcam_veio(ibmcam, 0, x, 0x0127);
1413 usb_ibmcam_veio(ibmcam, 0, 0x0000, 0x0124);
1414 usb_ibmcam_veio(ibmcam, 0, 0x0005, 0x0124);
1415 usb_ibmcam_veio(ibmcam, 0, 0x0002, 0x0124);
1416 usb_ibmcam_veio(ibmcam, 0, 0x0001, 0x0124);
1419 static void usb_ibmcam_send_x_00_05_02_08_01(struct usb_ibmcam *ibmcam, unsigned short x)
1421 usb_ibmcam_veio(ibmcam, 0, x, 0x0127);
1422 usb_ibmcam_veio(ibmcam, 0, 0x0000, 0x0124);
1423 usb_ibmcam_veio(ibmcam, 0, 0x0005, 0x0124);
1424 usb_ibmcam_veio(ibmcam, 0, 0x0002, 0x0124);
1425 usb_ibmcam_veio(ibmcam, 0, 0x0008, 0x0124);
1426 usb_ibmcam_veio(ibmcam, 0, 0x0001, 0x0124);
1429 static void usb_ibmcam_Packet_Format1(struct usb_ibmcam *ibmcam, unsigned char fkey, unsigned char val)
1431 usb_ibmcam_send_x_01_00_05 (ibmcam, unknown_88);
1432 usb_ibmcam_send_x_00_05 (ibmcam, fkey);
1433 usb_ibmcam_send_x_00_05_02_08_01(ibmcam, val);
1434 usb_ibmcam_send_x_00_05 (ibmcam, unknown_88);
1435 usb_ibmcam_send_x_00_05_02_01 (ibmcam, fkey);
1436 usb_ibmcam_send_x_00_05 (ibmcam, unknown_89);
1437 usb_ibmcam_send_x_00 (ibmcam, fkey);
1438 usb_ibmcam_send_00_04_06 (ibmcam);
1439 usb_ibmcam_veio (ibmcam, 1, 0x0000, 0x0126);
1440 usb_ibmcam_send_FF_04_02 (ibmcam);
1443 static void usb_ibmcam_PacketFormat2(struct usb_ibmcam *ibmcam, unsigned char fkey, unsigned char val)
1445 usb_ibmcam_send_x_01_00_05 (ibmcam, unknown_88);
1446 usb_ibmcam_send_x_00_05 (ibmcam, fkey);
1447 usb_ibmcam_send_x_00_05_02 (ibmcam, val);
1450 static void usb_ibmcam_model2_Packet2(struct usb_ibmcam *ibmcam)
1452 usb_ibmcam_veio(ibmcam, 0, 0x00ff, 0x012d);
1453 usb_ibmcam_veio(ibmcam, 0, 0xfea3, 0x0124);
1456 static void usb_ibmcam_model2_Packet1(struct usb_ibmcam *ibmcam, unsigned short v1, unsigned short v2)
1458 usb_ibmcam_veio(ibmcam, 0, 0x00aa, 0x012d);
1459 usb_ibmcam_veio(ibmcam, 0, 0x00ff, 0x012e);
1460 usb_ibmcam_veio(ibmcam, 0, v1, 0x012f);
1461 usb_ibmcam_veio(ibmcam, 0, 0x00ff, 0x0130);
1462 usb_ibmcam_veio(ibmcam, 0, 0xc719, 0x0124);
1463 usb_ibmcam_veio(ibmcam, 0, v2, 0x0127);
1465 usb_ibmcam_model2_Packet2(ibmcam);
1469 * usb_ibmcam_adjust_contrast()
1471 * The contrast value changes from 0 (high contrast) to 15 (low contrast).
1472 * This is in reverse to usual order of things (such as TV controls), so
1473 * we reverse it again here.
1475 * TODO: we probably don't need to send the setup 5 times...
1477 * History:
1478 * 1/2/00 Created.
1480 static void usb_ibmcam_adjust_contrast(struct usb_ibmcam *ibmcam)
1482 unsigned char new_contrast = ibmcam->vpic.contrast >> 12;
1483 const int ntries = 5;
1485 if (new_contrast >= 16)
1486 new_contrast = 15;
1487 new_contrast = 15 - new_contrast;
1488 if (new_contrast != ibmcam->vpic_old.contrast) {
1489 ibmcam->vpic_old.contrast = new_contrast;
1490 if (ibmcam->camera_model == IBMCAM_MODEL_1) {
1491 int i;
1492 for (i=0; i < ntries; i++) {
1493 usb_ibmcam_Packet_Format1(ibmcam, contrast_14, new_contrast);
1494 usb_ibmcam_send_FF_04_02(ibmcam);
1496 } else {
1497 /* Camera model 2 does not have this control; implemented in software. */
1503 * usb_ibmcam_change_lighting_conditions()
1505 * Camera model 1:
1506 * We have 3 levels of lighting conditions: 0=Bright, 1=Medium, 2=Low.
1508 * Camera model 2:
1509 * We have 16 levels of lighting, 0 for bright light and up to 15 for
1510 * low light. But values above 5 or so are useless because camera is
1511 * not really capable to produce anything worth viewing at such light.
1512 * This setting may be altered only in certain camera state.
1514 * Low lighting forces slower FPS. Lighting is set as a module parameter.
1516 * History:
1517 * 1/5/00 Created.
1518 * 2/20/00 Added support for Model 2 cameras.
1520 static void usb_ibmcam_change_lighting_conditions(struct usb_ibmcam *ibmcam)
1522 static const char proc[] = "usb_ibmcam_change_lighting_conditions";
1524 if (debug > 0)
1525 printk(KERN_INFO "%s: Set lighting to %hu.\n", proc, lighting);
1527 if (ibmcam->camera_model == IBMCAM_MODEL_1) {
1528 const int ntries = 5;
1529 int i;
1530 for (i=0; i < ntries; i++)
1531 usb_ibmcam_Packet_Format1(ibmcam, light_27, (unsigned short) lighting);
1532 } else {
1534 * This command apparently requires camera to be stopped. My
1535 * experiments showed that it -is- possible to alter the lighting
1536 * conditions setting "on the fly", but why bother? This setting does
1537 * not work reliably in all cases, so I decided simply to leave the
1538 * setting where Xirlink put it - in the camera setup phase. This code
1539 * is commented out because it does not work at -any- moment, so its
1540 * presence makes no sense. You may use it for experiments.
1542 #if 0
1543 usb_ibmcam_veio(ibmcam, 0, 0x0000, 0x010c); /* Stop camera */
1544 usb_ibmcam_model2_Packet1(ibmcam, mod2_sensitivity, lighting);
1545 usb_ibmcam_veio(ibmcam, 0, 0x00c0, 0x010c); /* Start camera */
1546 #endif
1551 * usb_ibmcam_set_sharpness()
1553 * Cameras model 1 have internal smoothing feature. It is controlled by value in
1554 * range [0..6], where 0 is most smooth and 6 is most sharp (raw image, I guess).
1555 * Recommended value is 4. Cameras model 2 do not have this feature at all.
1557 static void usb_ibmcam_set_sharpness(struct usb_ibmcam *ibmcam)
1559 static const char proc[] = "usb_ibmcam_set_sharpness";
1561 if (ibmcam->camera_model == IBMCAM_MODEL_1) {
1562 static const unsigned short sa[] = { 0x11, 0x13, 0x16, 0x18, 0x1a, 0x8, 0x0a };
1563 unsigned short i, sv;
1565 RESTRICT_TO_RANGE(sharpness, SHARPNESS_MIN, SHARPNESS_MAX);
1566 if (debug > 0)
1567 printk(KERN_INFO "%s: Set sharpness to %hu.\n", proc, sharpness);
1569 sv = sa[sharpness - SHARPNESS_MIN];
1570 for (i=0; i < 2; i++) {
1571 usb_ibmcam_send_x_01_00_05 (ibmcam, unknown_88);
1572 usb_ibmcam_send_x_00_05 (ibmcam, sharp_13);
1573 usb_ibmcam_send_x_00_05_02 (ibmcam, sv);
1575 } else {
1576 /* Camera model 2 does not have this control */
1581 * usb_ibmcam_set_brightness()
1583 * This procedure changes brightness of the picture.
1585 static void usb_ibmcam_set_brightness(struct usb_ibmcam *ibmcam)
1587 static const char proc[] = "usb_ibmcam_set_brightness";
1588 static const unsigned short n = 1;
1589 unsigned short i, j, bv[3];
1591 bv[0] = bv[1] = bv[2] = ibmcam->vpic.brightness >> 10;
1592 if (bv[0] == (ibmcam->vpic_old.brightness >> 10))
1593 return;
1594 ibmcam->vpic_old.brightness = ibmcam->vpic.brightness;
1596 if (debug > 0)
1597 printk(KERN_INFO "%s: Set brightness to (%hu,%hu,%hu)\n",
1598 proc, bv[0], bv[1], bv[2]);
1600 if (ibmcam->camera_model == IBMCAM_MODEL_1) {
1601 for (j=0; j < 3; j++)
1602 for (i=0; i < n; i++)
1603 usb_ibmcam_Packet_Format1(ibmcam, bright_3x[j], bv[j]);
1604 } else {
1605 i = ibmcam->vpic.brightness >> 12; /* 0 .. 15 */
1606 j = 0x60 + i * ((0xee - 0x60) / 16); /* 0x60 .. 0xee or so */
1607 usb_ibmcam_model2_Packet1(ibmcam, mod2_brightness, j);
1611 static void usb_ibmcam_model2_set_hue(struct usb_ibmcam *ibmcam)
1613 unsigned short hue = ibmcam->vpic.hue >> 9; /* 0 .. 7F */
1615 usb_ibmcam_model2_Packet1(ibmcam, mod2_color_balance_rg, hue);
1616 /* usb_ibmcam_model2_Packet1(ibmcam, mod2_saturation, sat); */
1620 * usb_ibmcam_adjust_picture()
1622 * This procedure gets called from V4L interface to update picture settings.
1623 * Here we change brightness and contrast.
1625 static void usb_ibmcam_adjust_picture(struct usb_ibmcam *ibmcam)
1627 usb_ibmcam_adjust_contrast(ibmcam);
1628 usb_ibmcam_set_brightness(ibmcam);
1629 if (ibmcam->camera_model == IBMCAM_MODEL_2) {
1630 usb_ibmcam_model2_set_hue(ibmcam);
1634 static int usb_ibmcam_model1_setup(struct usb_ibmcam *ibmcam)
1636 const int ntries = 5;
1637 int i;
1639 usb_ibmcam_veio(ibmcam, 1, 0x00, 0x0128);
1640 usb_ibmcam_veio(ibmcam, 1, 0x00, 0x0100);
1641 usb_ibmcam_veio(ibmcam, 0, 0x01, 0x0100); /* LED On */
1642 usb_ibmcam_veio(ibmcam, 1, 0x00, 0x0100);
1643 usb_ibmcam_veio(ibmcam, 0, 0x81, 0x0100); /* LED Off */
1644 usb_ibmcam_veio(ibmcam, 1, 0x00, 0x0100);
1645 usb_ibmcam_veio(ibmcam, 0, 0x01, 0x0100); /* LED On */
1646 usb_ibmcam_veio(ibmcam, 0, 0x01, 0x0108);
1648 usb_ibmcam_veio(ibmcam, 0, 0x03, 0x0112);
1649 usb_ibmcam_veio(ibmcam, 1, 0x00, 0x0115);
1650 usb_ibmcam_veio(ibmcam, 0, 0x06, 0x0115);
1651 usb_ibmcam_veio(ibmcam, 1, 0x00, 0x0116);
1652 usb_ibmcam_veio(ibmcam, 0, 0x44, 0x0116);
1653 usb_ibmcam_veio(ibmcam, 1, 0x00, 0x0116);
1654 usb_ibmcam_veio(ibmcam, 0, 0x40, 0x0116);
1655 usb_ibmcam_veio(ibmcam, 1, 0x00, 0x0115);
1656 usb_ibmcam_veio(ibmcam, 0, 0x0e, 0x0115);
1657 usb_ibmcam_veio(ibmcam, 0, 0x19, 0x012c);
1659 usb_ibmcam_Packet_Format1(ibmcam, 0x00, 0x1e);
1660 usb_ibmcam_Packet_Format1(ibmcam, 0x39, 0x0d);
1661 usb_ibmcam_Packet_Format1(ibmcam, 0x39, 0x09);
1662 usb_ibmcam_Packet_Format1(ibmcam, 0x3b, 0x00);
1663 usb_ibmcam_Packet_Format1(ibmcam, 0x28, 0x22);
1664 usb_ibmcam_Packet_Format1(ibmcam, light_27, 0);
1665 usb_ibmcam_Packet_Format1(ibmcam, 0x2b, 0x1f);
1666 usb_ibmcam_Packet_Format1(ibmcam, 0x39, 0x08);
1668 for (i=0; i < ntries; i++)
1669 usb_ibmcam_Packet_Format1(ibmcam, 0x2c, 0x00);
1671 for (i=0; i < ntries; i++)
1672 usb_ibmcam_Packet_Format1(ibmcam, 0x30, 0x14);
1674 usb_ibmcam_PacketFormat2(ibmcam, 0x39, 0x02);
1675 usb_ibmcam_PacketFormat2(ibmcam, 0x01, 0xe1);
1676 usb_ibmcam_PacketFormat2(ibmcam, 0x02, 0xcd);
1677 usb_ibmcam_PacketFormat2(ibmcam, 0x03, 0xcd);
1678 usb_ibmcam_PacketFormat2(ibmcam, 0x04, 0xfa);
1679 usb_ibmcam_PacketFormat2(ibmcam, 0x3f, 0xff);
1680 usb_ibmcam_PacketFormat2(ibmcam, 0x39, 0x00);
1682 usb_ibmcam_PacketFormat2(ibmcam, 0x39, 0x02);
1683 usb_ibmcam_PacketFormat2(ibmcam, 0x0a, 0x37);
1684 usb_ibmcam_PacketFormat2(ibmcam, 0x0b, 0xb8);
1685 usb_ibmcam_PacketFormat2(ibmcam, 0x0c, 0xf3);
1686 usb_ibmcam_PacketFormat2(ibmcam, 0x0d, 0xe3);
1687 usb_ibmcam_PacketFormat2(ibmcam, 0x0e, 0x0d);
1688 usb_ibmcam_PacketFormat2(ibmcam, 0x0f, 0xf2);
1689 usb_ibmcam_PacketFormat2(ibmcam, 0x10, 0xd5);
1690 usb_ibmcam_PacketFormat2(ibmcam, 0x11, 0xba);
1691 usb_ibmcam_PacketFormat2(ibmcam, 0x12, 0x53);
1692 usb_ibmcam_PacketFormat2(ibmcam, 0x3f, 0xff);
1693 usb_ibmcam_PacketFormat2(ibmcam, 0x39, 0x00);
1695 usb_ibmcam_PacketFormat2(ibmcam, 0x39, 0x02);
1696 usb_ibmcam_PacketFormat2(ibmcam, 0x16, 0x00);
1697 usb_ibmcam_PacketFormat2(ibmcam, 0x17, 0x28);
1698 usb_ibmcam_PacketFormat2(ibmcam, 0x18, 0x7d);
1699 usb_ibmcam_PacketFormat2(ibmcam, 0x19, 0xbe);
1700 usb_ibmcam_PacketFormat2(ibmcam, 0x3f, 0xff);
1701 usb_ibmcam_PacketFormat2(ibmcam, 0x39, 0x00);
1703 for (i=0; i < ntries; i++)
1704 usb_ibmcam_Packet_Format1(ibmcam, 0x00, 0x18);
1705 for (i=0; i < ntries; i++)
1706 usb_ibmcam_Packet_Format1(ibmcam, 0x13, 0x18);
1707 for (i=0; i < ntries; i++)
1708 usb_ibmcam_Packet_Format1(ibmcam, 0x14, 0x06);
1710 /* This is default brightness */
1711 for (i=0; i < ntries; i++)
1712 usb_ibmcam_Packet_Format1(ibmcam, 0x31, 0x37);
1713 for (i=0; i < ntries; i++)
1714 usb_ibmcam_Packet_Format1(ibmcam, 0x32, 0x46);
1715 for (i=0; i < ntries; i++)
1716 usb_ibmcam_Packet_Format1(ibmcam, 0x33, 0x55);
1718 usb_ibmcam_Packet_Format1(ibmcam, 0x2e, 0x04);
1719 for (i=0; i < ntries; i++)
1720 usb_ibmcam_Packet_Format1(ibmcam, 0x2d, 0x04);
1721 for (i=0; i < ntries; i++)
1722 usb_ibmcam_Packet_Format1(ibmcam, 0x29, 0x80);
1723 usb_ibmcam_Packet_Format1(ibmcam, 0x2c, 0x01);
1724 usb_ibmcam_Packet_Format1(ibmcam, 0x30, 0x17);
1725 usb_ibmcam_Packet_Format1(ibmcam, 0x39, 0x08);
1726 for (i=0; i < ntries; i++)
1727 usb_ibmcam_Packet_Format1(ibmcam, 0x34, 0x00);
1729 usb_ibmcam_veio(ibmcam, 0, 0x00, 0x0101);
1730 usb_ibmcam_veio(ibmcam, 0, 0x00, 0x010a);
1732 switch (videosize) {
1733 case VIDEOSIZE_128x96:
1734 usb_ibmcam_veio(ibmcam, 0, 0x80, 0x0103);
1735 usb_ibmcam_veio(ibmcam, 0, 0x60, 0x0105);
1736 usb_ibmcam_veio(ibmcam, 0, 0x0c, 0x010b);
1737 usb_ibmcam_veio(ibmcam, 0, 0x04, 0x011b); /* Same everywhere */
1738 usb_ibmcam_veio(ibmcam, 0, 0x0b, 0x011d);
1739 usb_ibmcam_veio(ibmcam, 0, 0x00, 0x011e); /* Same everywhere */
1740 usb_ibmcam_veio(ibmcam, 0, 0x00, 0x0129);
1741 break;
1742 case VIDEOSIZE_176x144:
1743 usb_ibmcam_veio(ibmcam, 0, 0xb0, 0x0103);
1744 usb_ibmcam_veio(ibmcam, 0, 0x8f, 0x0105);
1745 usb_ibmcam_veio(ibmcam, 0, 0x06, 0x010b);
1746 usb_ibmcam_veio(ibmcam, 0, 0x04, 0x011b); /* Same everywhere */
1747 usb_ibmcam_veio(ibmcam, 0, 0x0d, 0x011d);
1748 usb_ibmcam_veio(ibmcam, 0, 0x00, 0x011e); /* Same everywhere */
1749 usb_ibmcam_veio(ibmcam, 0, 0x03, 0x0129);
1750 break;
1751 case VIDEOSIZE_352x288:
1752 usb_ibmcam_veio(ibmcam, 0, 0xb0, 0x0103);
1753 usb_ibmcam_veio(ibmcam, 0, 0x90, 0x0105);
1754 usb_ibmcam_veio(ibmcam, 0, 0x02, 0x010b);
1755 usb_ibmcam_veio(ibmcam, 0, 0x04, 0x011b); /* Same everywhere */
1756 usb_ibmcam_veio(ibmcam, 0, 0x05, 0x011d);
1757 usb_ibmcam_veio(ibmcam, 0, 0x00, 0x011e); /* Same everywhere */
1758 usb_ibmcam_veio(ibmcam, 0, 0x00, 0x0129);
1759 break;
1762 usb_ibmcam_veio(ibmcam, 0, 0xff, 0x012b);
1764 /* This is another brightness - don't know why */
1765 for (i=0; i < ntries; i++)
1766 usb_ibmcam_Packet_Format1(ibmcam, 0x31, 0xc3);
1767 for (i=0; i < ntries; i++)
1768 usb_ibmcam_Packet_Format1(ibmcam, 0x32, 0xd2);
1769 for (i=0; i < ntries; i++)
1770 usb_ibmcam_Packet_Format1(ibmcam, 0x33, 0xe1);
1772 /* Default contrast */
1773 for (i=0; i < ntries; i++)
1774 usb_ibmcam_Packet_Format1(ibmcam, contrast_14, 0x0a);
1776 /* Default sharpness */
1777 for (i=0; i < 2; i++)
1778 usb_ibmcam_PacketFormat2(ibmcam, sharp_13, 0x1a); /* Level 4 FIXME */
1780 /* Default lighting conditions */
1781 usb_ibmcam_Packet_Format1(ibmcam, light_27, lighting); /* 0=Bright 2=Low */
1783 /* Assorted init */
1785 switch (videosize) {
1786 case VIDEOSIZE_128x96:
1787 usb_ibmcam_Packet_Format1(ibmcam, 0x2b, 0x1e);
1788 usb_ibmcam_veio(ibmcam, 0, 0xc9, 0x0119); /* Same everywhere */
1789 usb_ibmcam_veio(ibmcam, 0, 0x80, 0x0109); /* Same everywhere */
1790 usb_ibmcam_veio(ibmcam, 0, 0x36, 0x0102);
1791 usb_ibmcam_veio(ibmcam, 0, 0x1a, 0x0104);
1792 usb_ibmcam_veio(ibmcam, 0, 0x04, 0x011a); /* Same everywhere */
1793 usb_ibmcam_veio(ibmcam, 0, 0x2b, 0x011c);
1794 usb_ibmcam_veio(ibmcam, 0, 0x23, 0x012a); /* Same everywhere */
1795 #if 0
1796 usb_ibmcam_veio(ibmcam, 0, 0x00, 0x0106);
1797 usb_ibmcam_veio(ibmcam, 0, 0x38, 0x0107);
1798 #else
1799 usb_ibmcam_veio(ibmcam, 0, 0x02, 0x0106);
1800 usb_ibmcam_veio(ibmcam, 0, 0x2a, 0x0107);
1801 #endif
1802 break;
1803 case VIDEOSIZE_176x144:
1804 usb_ibmcam_Packet_Format1(ibmcam, 0x2b, 0x1e);
1805 usb_ibmcam_veio(ibmcam, 0, 0xc9, 0x0119); /* Same everywhere */
1806 usb_ibmcam_veio(ibmcam, 0, 0x80, 0x0109); /* Same everywhere */
1807 usb_ibmcam_veio(ibmcam, 0, 0x04, 0x0102);
1808 usb_ibmcam_veio(ibmcam, 0, 0x02, 0x0104);
1809 usb_ibmcam_veio(ibmcam, 0, 0x04, 0x011a); /* Same everywhere */
1810 usb_ibmcam_veio(ibmcam, 0, 0x2b, 0x011c);
1811 usb_ibmcam_veio(ibmcam, 0, 0x23, 0x012a); /* Same everywhere */
1812 usb_ibmcam_veio(ibmcam, 0, 0x01, 0x0106);
1813 usb_ibmcam_veio(ibmcam, 0, 0xca, 0x0107);
1814 break;
1815 case VIDEOSIZE_352x288:
1816 usb_ibmcam_Packet_Format1(ibmcam, 0x2b, 0x1f);
1817 usb_ibmcam_veio(ibmcam, 0, 0xc9, 0x0119); /* Same everywhere */
1818 usb_ibmcam_veio(ibmcam, 0, 0x80, 0x0109); /* Same everywhere */
1819 usb_ibmcam_veio(ibmcam, 0, 0x08, 0x0102);
1820 usb_ibmcam_veio(ibmcam, 0, 0x01, 0x0104);
1821 usb_ibmcam_veio(ibmcam, 0, 0x04, 0x011a); /* Same everywhere */
1822 usb_ibmcam_veio(ibmcam, 0, 0x2f, 0x011c);
1823 usb_ibmcam_veio(ibmcam, 0, 0x23, 0x012a); /* Same everywhere */
1824 usb_ibmcam_veio(ibmcam, 0, 0x03, 0x0106);
1825 usb_ibmcam_veio(ibmcam, 0, 0xf6, 0x0107);
1826 break;
1828 return IBMCAM_IS_OPERATIONAL(ibmcam);
1831 static int usb_ibmcam_model2_setup(struct usb_ibmcam *ibmcam)
1833 usb_ibmcam_veio(ibmcam, 0, 0x0000, 0x0100); /* LED on */
1834 usb_ibmcam_veio(ibmcam, 1, 0x0000, 0x0116);
1835 usb_ibmcam_veio(ibmcam, 0, 0x0060, 0x0116);
1836 usb_ibmcam_veio(ibmcam, 0, 0x0002, 0x0112);
1837 usb_ibmcam_veio(ibmcam, 0, 0x00bc, 0x012c);
1838 usb_ibmcam_veio(ibmcam, 0, 0x0008, 0x012b);
1839 usb_ibmcam_veio(ibmcam, 0, 0x0000, 0x0108);
1840 usb_ibmcam_veio(ibmcam, 0, 0x0001, 0x0133);
1841 usb_ibmcam_veio(ibmcam, 0, 0x0001, 0x0102);
1842 switch (videosize) {
1843 case VIDEOSIZE_176x144:
1844 usb_ibmcam_veio(ibmcam, 0, 0x002c, 0x0103); /* All except 320x240 */
1845 usb_ibmcam_veio(ibmcam, 0, 0x0000, 0x0104); /* Same */
1846 usb_ibmcam_veio(ibmcam, 0, 0x0024, 0x0105); /* 176x144, 352x288 */
1847 usb_ibmcam_veio(ibmcam, 0, 0x00b9, 0x010a); /* Unique to this mode */
1848 usb_ibmcam_veio(ibmcam, 0, 0x0038, 0x0119); /* Unique to this mode */
1849 usb_ibmcam_veio(ibmcam, 0, 0x0003, 0x0106); /* Same */
1850 usb_ibmcam_veio(ibmcam, 0, 0x0090, 0x0107); /* Unique to every mode*/
1851 break;
1852 case VIDEOSIZE_320x240:
1853 usb_ibmcam_veio(ibmcam, 0, 0x0028, 0x0103); /* Unique to this mode */
1854 usb_ibmcam_veio(ibmcam, 0, 0x0000, 0x0104); /* Same */
1855 usb_ibmcam_veio(ibmcam, 0, 0x001e, 0x0105); /* 320x240, 352x240 */
1856 usb_ibmcam_veio(ibmcam, 0, 0x0039, 0x010a); /* All except 176x144 */
1857 usb_ibmcam_veio(ibmcam, 0, 0x0070, 0x0119); /* All except 176x144 */
1858 usb_ibmcam_veio(ibmcam, 0, 0x0003, 0x0106); /* Same */
1859 usb_ibmcam_veio(ibmcam, 0, 0x0098, 0x0107); /* Unique to every mode*/
1860 break;
1861 case VIDEOSIZE_352x240:
1862 usb_ibmcam_veio(ibmcam, 0, 0x002c, 0x0103); /* All except 320x240 */
1863 usb_ibmcam_veio(ibmcam, 0, 0x0000, 0x0104); /* Same */
1864 usb_ibmcam_veio(ibmcam, 0, 0x001e, 0x0105); /* 320x240, 352x240 */
1865 usb_ibmcam_veio(ibmcam, 0, 0x0039, 0x010a); /* All except 176x144 */
1866 usb_ibmcam_veio(ibmcam, 0, 0x0070, 0x0119); /* All except 176x144 */
1867 usb_ibmcam_veio(ibmcam, 0, 0x0003, 0x0106); /* Same */
1868 usb_ibmcam_veio(ibmcam, 0, 0x00da, 0x0107); /* Unique to every mode*/
1869 break;
1870 case VIDEOSIZE_352x288:
1871 usb_ibmcam_veio(ibmcam, 0, 0x002c, 0x0103); /* All except 320x240 */
1872 usb_ibmcam_veio(ibmcam, 0, 0x0000, 0x0104); /* Same */
1873 usb_ibmcam_veio(ibmcam, 0, 0x0024, 0x0105); /* 176x144, 352x288 */
1874 usb_ibmcam_veio(ibmcam, 0, 0x0039, 0x010a); /* All except 176x144 */
1875 usb_ibmcam_veio(ibmcam, 0, 0x0070, 0x0119); /* All except 176x144 */
1876 usb_ibmcam_veio(ibmcam, 0, 0x0003, 0x0106); /* Same */
1877 usb_ibmcam_veio(ibmcam, 0, 0x00fe, 0x0107); /* Unique to every mode*/
1878 break;
1880 return IBMCAM_IS_OPERATIONAL(ibmcam);
1884 * usb_ibmcam_model1_setup_after_video_if()
1886 * This code adds finishing touches to the video data interface.
1887 * Here we configure the frame rate and turn on the LED.
1889 static void usb_ibmcam_model1_setup_after_video_if(struct usb_ibmcam *ibmcam)
1891 unsigned short internal_frame_rate;
1893 RESTRICT_TO_RANGE(framerate, FRAMERATE_MIN, FRAMERATE_MAX);
1894 internal_frame_rate = FRAMERATE_MAX - framerate; /* 0=Fast 6=Slow */
1895 usb_ibmcam_veio(ibmcam, 0, 0x01, 0x0100); /* LED On */
1896 usb_ibmcam_veio(ibmcam, 0, internal_frame_rate, 0x0111);
1897 usb_ibmcam_veio(ibmcam, 0, 0x01, 0x0114);
1898 usb_ibmcam_veio(ibmcam, 0, 0xc0, 0x010c);
1901 static void usb_ibmcam_model2_setup_after_video_if(struct usb_ibmcam *ibmcam)
1903 unsigned short setup_model2_rg, setup_model2_rg2, setup_model2_sat, setup_model2_yb;
1905 usb_ibmcam_veio(ibmcam, 0, 0x0000, 0x0100); /* LED on */
1907 switch (videosize) {
1908 case VIDEOSIZE_176x144:
1909 usb_ibmcam_veio(ibmcam, 0, 0x0050, 0x0111);
1910 usb_ibmcam_veio(ibmcam, 0, 0x00d0, 0x0111);
1911 break;
1912 case VIDEOSIZE_320x240:
1913 case VIDEOSIZE_352x240:
1914 case VIDEOSIZE_352x288:
1915 usb_ibmcam_veio(ibmcam, 0, 0x0040, 0x0111);
1916 usb_ibmcam_veio(ibmcam, 0, 0x00c0, 0x0111);
1917 break;
1919 usb_ibmcam_veio(ibmcam, 0, 0x009b, 0x010f);
1920 usb_ibmcam_veio(ibmcam, 0, 0x00bb, 0x010f);
1923 * Hardware settings, may affect CMOS sensor; not user controls!
1924 * -------------------------------------------------------------
1925 * 0x0004: no effect
1926 * 0x0006: hardware effect
1927 * 0x0008: no effect
1928 * 0x000a: stops video stream, probably important h/w setting
1929 * 0x000c: changes color in hardware manner (not user setting)
1930 * 0x0012: changes number of colors (does not affect speed)
1931 * 0x002a: no effect
1932 * 0x002c: hardware setting (related to scan lines)
1933 * 0x002e: stops video stream, probably important h/w setting
1935 usb_ibmcam_model2_Packet1(ibmcam, 0x000a, 0x005c);
1936 usb_ibmcam_model2_Packet1(ibmcam, 0x0004, 0x0000);
1937 usb_ibmcam_model2_Packet1(ibmcam, 0x0006, 0x00fb);
1938 usb_ibmcam_model2_Packet1(ibmcam, 0x0008, 0x0000);
1939 usb_ibmcam_model2_Packet1(ibmcam, 0x000c, 0x0009);
1940 usb_ibmcam_model2_Packet1(ibmcam, 0x0012, 0x000a);
1941 usb_ibmcam_model2_Packet1(ibmcam, 0x002a, 0x0000);
1942 usb_ibmcam_model2_Packet1(ibmcam, 0x002c, 0x0000);
1943 usb_ibmcam_model2_Packet1(ibmcam, 0x002e, 0x0008);
1946 * Function 0x0030 pops up all over the place. Apparently
1947 * it is a hardware control register, with every bit assigned to
1948 * do something.
1950 usb_ibmcam_model2_Packet1(ibmcam, 0x0030, 0x0000);
1953 * Magic control of CMOS sensor. Only lower values like
1954 * 0-3 work, and picture shifts left or right. Don't change.
1956 switch (videosize) {
1957 case VIDEOSIZE_176x144:
1958 usb_ibmcam_model2_Packet1(ibmcam, 0x0014, 0x0002);
1959 usb_ibmcam_model2_Packet1(ibmcam, 0x0016, 0x0002); /* Horizontal shift */
1960 usb_ibmcam_model2_Packet1(ibmcam, 0x0018, 0x004a); /* Another hardware setting */
1961 break;
1962 case VIDEOSIZE_320x240:
1963 usb_ibmcam_model2_Packet1(ibmcam, 0x0014, 0x0009);
1964 usb_ibmcam_model2_Packet1(ibmcam, 0x0016, 0x0005); /* Horizontal shift */
1965 usb_ibmcam_model2_Packet1(ibmcam, 0x0018, 0x0044); /* Another hardware setting */
1966 break;
1967 case VIDEOSIZE_352x240:
1968 /* This mode doesn't work as Windows programs it; changed to work */
1969 usb_ibmcam_model2_Packet1(ibmcam, 0x0014, 0x0009); /* Windows sets this to 8 */
1970 usb_ibmcam_model2_Packet1(ibmcam, 0x0016, 0x0003); /* Horizontal shift */
1971 usb_ibmcam_model2_Packet1(ibmcam, 0x0018, 0x0044); /* Windows sets this to 0x0045 */
1972 break;
1973 case VIDEOSIZE_352x288:
1974 usb_ibmcam_model2_Packet1(ibmcam, 0x0014, 0x0003);
1975 usb_ibmcam_model2_Packet1(ibmcam, 0x0016, 0x0002); /* Horizontal shift */
1976 usb_ibmcam_model2_Packet1(ibmcam, 0x0018, 0x004a); /* Another hardware setting */
1977 break;
1980 usb_ibmcam_model2_Packet1(ibmcam, mod2_brightness, 0x005a);
1983 * We have our own frame rate setting varying from 0 (slowest) to 6 (fastest).
1984 * The camera model 2 allows frame rate in range [0..0x1F] where 0 is also the
1985 * slowest setting. However for all practical reasons high settings make no
1986 * sense because USB is not fast enough to support high FPS. Be aware that
1987 * the picture datastream will be severely disrupted if you ask for
1988 * frame rate faster than allowed for the video size - see below:
1990 * Allowable ranges (obtained experimentally on OHCI, K6-3, 450 MHz):
1991 * -----------------------------------------------------------------
1992 * 176x144: [6..31]
1993 * 320x240: [8..31]
1994 * 352x240: [10..31]
1995 * 352x288: [16..31] I have to raise lower threshold for stability...
1997 * As usual, slower FPS provides better sensitivity.
2000 short hw_fps=31, i_framerate;
2002 RESTRICT_TO_RANGE(framerate, FRAMERATE_MIN, FRAMERATE_MAX);
2003 i_framerate = FRAMERATE_MAX - framerate + FRAMERATE_MIN;
2004 switch (videosize) {
2005 case VIDEOSIZE_176x144:
2006 hw_fps = 6 + i_framerate*4;
2007 break;
2008 case VIDEOSIZE_320x240:
2009 hw_fps = 8 + i_framerate*3;
2010 break;
2011 case VIDEOSIZE_352x240:
2012 hw_fps = 10 + i_framerate*2;
2013 break;
2014 case VIDEOSIZE_352x288:
2015 hw_fps = 28 + i_framerate/2;
2016 break;
2018 if (debug > 0)
2019 printk(KERN_DEBUG "Framerate (hardware): %hd.\n", hw_fps);
2020 RESTRICT_TO_RANGE(hw_fps, 0, 31);
2021 usb_ibmcam_model2_Packet1(ibmcam, mod2_set_framerate, hw_fps);
2025 * This setting does not visibly affect pictures; left it here
2026 * because it was present in Windows USB data stream. This function
2027 * does not allow arbitrary values and apparently is a bit mask, to
2028 * be activated only at appropriate time. Don't change it randomly!
2030 switch (videosize) {
2031 case VIDEOSIZE_176x144:
2032 usb_ibmcam_model2_Packet1(ibmcam, 0x0026, 0x00c2);
2033 break;
2034 case VIDEOSIZE_320x240:
2035 usb_ibmcam_model2_Packet1(ibmcam, 0x0026, 0x0044);
2036 break;
2037 case VIDEOSIZE_352x240:
2038 usb_ibmcam_model2_Packet1(ibmcam, 0x0026, 0x0046);
2039 break;
2040 case VIDEOSIZE_352x288:
2041 usb_ibmcam_model2_Packet1(ibmcam, 0x0026, 0x0048);
2042 break;
2045 usb_ibmcam_model2_Packet1(ibmcam, mod2_sensitivity, lighting);
2047 if (init_model2_rg >= 0) {
2048 RESTRICT_TO_RANGE(init_model2_rg, 0, 255);
2049 setup_model2_rg = init_model2_rg;
2050 } else
2051 setup_model2_rg = 0x0070;
2053 if (init_model2_rg2 >= 0) {
2054 RESTRICT_TO_RANGE(init_model2_rg2, 0, 255);
2055 setup_model2_rg2 = init_model2_rg2;
2056 } else
2057 setup_model2_rg2 = 0x002f;
2059 if (init_model2_sat >= 0) {
2060 RESTRICT_TO_RANGE(init_model2_sat, 0, 255);
2061 setup_model2_sat = init_model2_sat;
2062 } else
2063 setup_model2_sat = 0x0034;
2065 if (init_model2_yb >= 0) {
2066 RESTRICT_TO_RANGE(init_model2_yb, 0, 255);
2067 setup_model2_yb = init_model2_yb;
2068 } else
2069 setup_model2_yb = 0x00a0;
2071 usb_ibmcam_model2_Packet1(ibmcam, mod2_color_balance_rg2, setup_model2_rg2);
2072 usb_ibmcam_model2_Packet1(ibmcam, mod2_saturation, setup_model2_sat);
2073 usb_ibmcam_model2_Packet1(ibmcam, mod2_color_balance_yb, setup_model2_yb);
2074 usb_ibmcam_model2_Packet1(ibmcam, mod2_color_balance_rg, setup_model2_rg);
2076 /* Hardware control command */
2077 usb_ibmcam_model2_Packet1(ibmcam, 0x0030, 0x0004);
2079 usb_ibmcam_veio(ibmcam, 0, 0x00c0, 0x010c); /* Go camera, go! */
2080 usb_clear_halt(ibmcam->dev, ibmcam->video_endp);
2084 * usb_ibmcam_setup_video_stop()
2086 * This code tells camera to stop streaming. The interface remains
2087 * configured and bandwidth - claimed.
2089 static void usb_ibmcam_setup_video_stop(struct usb_ibmcam *ibmcam)
2091 if (ibmcam->camera_model == IBMCAM_MODEL_1) {
2092 usb_ibmcam_veio(ibmcam, 0, 0x00, 0x010c);
2093 usb_ibmcam_veio(ibmcam, 0, 0x00, 0x010c);
2094 usb_ibmcam_veio(ibmcam, 0, 0x01, 0x0114);
2095 usb_ibmcam_veio(ibmcam, 0, 0xc0, 0x010c);
2096 usb_ibmcam_veio(ibmcam, 0, 0x00, 0x010c);
2097 usb_ibmcam_send_FF_04_02(ibmcam);
2098 usb_ibmcam_veio(ibmcam, 1, 0x00, 0x0100);
2099 usb_ibmcam_veio(ibmcam, 0, 0x81, 0x0100); /* LED Off */
2100 } else if (ibmcam->camera_model == IBMCAM_MODEL_2) {
2101 usb_ibmcam_veio(ibmcam, 0, 0x0000, 0x010c); /* Stop the camera */
2103 usb_ibmcam_model2_Packet1(ibmcam, 0x0030, 0x0004);
2105 usb_ibmcam_veio(ibmcam, 0, 0x0080, 0x0100); /* LED Off */
2106 usb_ibmcam_veio(ibmcam, 0, 0x0020, 0x0111);
2107 usb_ibmcam_veio(ibmcam, 0, 0x00a0, 0x0111);
2109 usb_ibmcam_model2_Packet1(ibmcam, 0x0030, 0x0002);
2111 usb_ibmcam_veio(ibmcam, 0, 0x0020, 0x0111);
2112 usb_ibmcam_veio(ibmcam, 0, 0x0000, 0x0112);
2117 * usb_ibmcam_reinit_iso()
2119 * This procedure sends couple of commands to the camera and then
2120 * resets the video pipe. This sequence was observed to reinit the
2121 * camera or, at least, to initiate ISO data stream.
2123 * History:
2124 * 1/2/00 Created.
2126 static void usb_ibmcam_reinit_iso(struct usb_ibmcam *ibmcam, int do_stop)
2128 if (ibmcam->camera_model == IBMCAM_MODEL_1) {
2129 if (do_stop)
2130 usb_ibmcam_setup_video_stop(ibmcam);
2131 usb_ibmcam_veio(ibmcam, 0, 0x0001, 0x0114);
2132 usb_ibmcam_veio(ibmcam, 0, 0x00c0, 0x010c);
2133 usb_clear_halt(ibmcam->dev, ibmcam->video_endp);
2134 usb_ibmcam_model1_setup_after_video_if(ibmcam);
2135 } else if (ibmcam->camera_model == IBMCAM_MODEL_2) {
2136 usb_ibmcam_model2_setup_after_video_if(ibmcam);
2141 * ibmcam_init_isoc()
2143 * History:
2144 * 1/27/00 Used ibmcam->iface, ibmcam->ifaceAltActive instead of hardcoded values.
2145 * Simplified by using for loop, allowed any number of URBs.
2147 static int ibmcam_init_isoc(struct usb_ibmcam *ibmcam)
2149 struct usb_device *dev = ibmcam->dev;
2150 int i, err;
2152 if (!IBMCAM_IS_OPERATIONAL(ibmcam))
2153 return -EFAULT;
2155 ibmcam->compress = 0;
2156 ibmcam->curframe = -1;
2157 ibmcam->cursbuf = 0;
2158 ibmcam->scratchlen = 0;
2160 /* Alternate interface 1 is is the biggest frame size */
2161 i = usb_set_interface(dev, ibmcam->iface, ibmcam->ifaceAltActive);
2162 if (i < 0) {
2163 printk(KERN_ERR "usb_set_interface error\n");
2164 ibmcam->last_error = i;
2165 return -EBUSY;
2167 usb_ibmcam_change_lighting_conditions(ibmcam);
2168 usb_ibmcam_set_sharpness(ibmcam);
2169 usb_ibmcam_reinit_iso(ibmcam, 0);
2171 /* We double buffer the Iso lists */
2173 for (i=0; i < IBMCAM_NUMSBUF; i++) {
2174 int j, k;
2175 urb_t *urb;
2177 urb = usb_alloc_urb(FRAMES_PER_DESC);
2178 if (urb == NULL) {
2179 printk(KERN_ERR "ibmcam_init_isoc: usb_init_isoc() failed.\n");
2180 return -ENOMEM;
2182 ibmcam->sbuf[i].urb = urb;
2183 urb->dev = dev;
2184 urb->context = ibmcam;
2185 urb->pipe = usb_rcvisocpipe(dev, ibmcam->video_endp);
2186 urb->transfer_flags = USB_ISO_ASAP;
2187 urb->transfer_buffer = ibmcam->sbuf[i].data;
2188 urb->complete = ibmcam_isoc_irq;
2189 urb->number_of_packets = FRAMES_PER_DESC;
2190 urb->transfer_buffer_length = ibmcam->iso_packet_len * FRAMES_PER_DESC;
2191 for (j=k=0; j < FRAMES_PER_DESC; j++, k += ibmcam->iso_packet_len) {
2192 urb->iso_frame_desc[j].offset = k;
2193 urb->iso_frame_desc[j].length = ibmcam->iso_packet_len;
2197 /* Link URBs into a ring so that they invoke each other infinitely */
2198 for (i=0; i < IBMCAM_NUMSBUF; i++) {
2199 if ((i+1) < IBMCAM_NUMSBUF)
2200 ibmcam->sbuf[i].urb->next = ibmcam->sbuf[i+1].urb;
2201 else
2202 ibmcam->sbuf[i].urb->next = ibmcam->sbuf[0].urb;
2205 /* Submit all URBs */
2206 for (i=0; i < IBMCAM_NUMSBUF; i++) {
2207 err = usb_submit_urb(ibmcam->sbuf[i].urb);
2208 if (err)
2209 printk(KERN_ERR "ibmcam_init_isoc: usb_run_isoc(%d) ret %d\n",
2210 i, err);
2213 ibmcam->streaming = 1;
2214 /* printk(KERN_DEBUG "streaming=1 ibmcam->video_endp=$%02x\n", ibmcam->video_endp); */
2215 return 0;
2219 * ibmcam_stop_isoc()
2221 * This procedure stops streaming and deallocates URBs. Then it
2222 * activates zero-bandwidth alt. setting of the video interface.
2224 * History:
2225 * 1/22/00 Corrected order of actions to work after surprise removal.
2226 * 1/27/00 Used ibmcam->iface, ibmcam->ifaceAltInactive instead of hardcoded values.
2228 static void ibmcam_stop_isoc(struct usb_ibmcam *ibmcam)
2230 static const char proc[] = "ibmcam_stop_isoc";
2231 int i, j;
2233 if (!ibmcam->streaming || (ibmcam->dev == NULL))
2234 return;
2236 /* Unschedule all of the iso td's */
2237 for (i=0; i < IBMCAM_NUMSBUF; i++) {
2238 j = usb_unlink_urb(ibmcam->sbuf[i].urb);
2239 if (j < 0)
2240 printk(KERN_ERR "%s: usb_unlink_urb() error %d.\n", proc, j);
2242 /* printk(KERN_DEBUG "streaming=0\n"); */
2243 ibmcam->streaming = 0;
2245 /* Delete them all */
2246 for (i=0; i < IBMCAM_NUMSBUF; i++)
2247 usb_free_urb(ibmcam->sbuf[i].urb);
2249 if (!ibmcam->remove_pending) {
2250 usb_ibmcam_setup_video_stop(ibmcam);
2252 /* Set packet size to 0 */
2253 j = usb_set_interface(ibmcam->dev, ibmcam->iface, ibmcam->ifaceAltInactive);
2254 if (j < 0) {
2255 printk(KERN_ERR "%s: usb_set_interface() error %d.\n", proc, j);
2256 ibmcam->last_error = j;
2262 * ibmcam_new_frame()
2264 * History:
2265 * 29-Mar-00 Added copying of previous frame into the current one.
2267 static int ibmcam_new_frame(struct usb_ibmcam *ibmcam, int framenum)
2269 struct ibmcam_frame *frame;
2270 int n, width, height;
2272 /* If we're not grabbing a frame right now and the other frame is */
2273 /* ready to be grabbed into, then use it instead */
2274 if (ibmcam->curframe != -1)
2275 return 0;
2277 n = (framenum - 1 + IBMCAM_NUMFRAMES) % IBMCAM_NUMFRAMES;
2278 if (ibmcam->frame[n].grabstate == FRAME_READY)
2279 framenum = n;
2281 frame = &ibmcam->frame[framenum];
2283 frame->grabstate = FRAME_GRABBING;
2284 frame->scanstate = STATE_SCANNING;
2285 frame->scanlength = 0; /* Accumulated in ibmcam_parse_data() */
2286 ibmcam->curframe = framenum;
2289 * Normally we would want to copy previous frame into the current one
2290 * before we even start filling it with data; this allows us to stop
2291 * filling at any moment; top portion of the frame will be new and
2292 * bottom portion will stay as it was in previous frame. If we don't
2293 * do that then missing chunks of video stream will result in flickering
2294 * portions of old data whatever it was before.
2296 * If we choose not to copy previous frame (to, for example, save few
2297 * bus cycles - the frame can be pretty large!) then we have an option
2298 * to clear the frame before using. If we experience losses in this
2299 * mode then missing picture will be black (no flickering).
2301 * Finally, if user chooses not to clean the current frame before
2302 * filling it with data then the old data will be visible if we fail
2303 * to refill entire frame with new data.
2305 if (!(flags & FLAGS_SEPARATE_FRAMES)) {
2306 /* This copies previous frame into this one to mask losses */
2307 memmove(frame->data, ibmcam->frame[1-framenum].data, MAX_FRAME_SIZE);
2308 } else {
2309 if (flags & FLAGS_CLEAN_FRAMES) {
2310 /* This provides a "clean" frame but slows things down */
2311 memset(frame->data, 0, MAX_FRAME_SIZE);
2314 switch (videosize) {
2315 case VIDEOSIZE_128x96:
2316 frame->frmwidth = 128;
2317 frame->frmheight = 96;
2318 frame->order_uv = 1; /* U Y V Y ... */
2319 frame->hdr_sig = 0x06; /* 00 FF 00 06 */
2320 break;
2321 case VIDEOSIZE_176x144:
2322 frame->frmwidth = 176;
2323 frame->frmheight = 144;
2324 frame->order_uv = 1; /* U Y V Y ... */
2325 frame->hdr_sig = 0x0E; /* 00 FF 00 0E */
2326 break;
2327 case VIDEOSIZE_320x240: /* For model 2 only */
2328 frame->frmwidth = 320;
2329 frame->frmheight = 240;
2330 break;
2331 case VIDEOSIZE_352x240: /* For model 2 only */
2332 frame->frmwidth = 352;
2333 frame->frmheight = 240;
2334 break;
2335 case VIDEOSIZE_352x288:
2336 frame->frmwidth = 352;
2337 frame->frmheight = 288;
2338 frame->order_uv = 0; /* V Y U Y ... */
2339 frame->hdr_sig = 0x00; /* 00 FF 00 00 */
2340 break;
2342 frame->order_yc = (ibmcam->camera_model == IBMCAM_MODEL_2);
2344 width = frame->width;
2345 RESTRICT_TO_RANGE(width, min_imgwidth, imgwidth);
2346 width &= ~7; /* Multiple of 8 */
2348 height = frame->height;
2349 RESTRICT_TO_RANGE(height, min_imgheight, imgheight);
2350 height &= ~3; /* Multiple of 4 */
2352 return 0;
2356 * ibmcam_open()
2358 * This is part of Video 4 Linux API. The driver can be opened by one
2359 * client only (checks internal counter 'ibmcam->user'). The procedure
2360 * then allocates buffers needed for video processing.
2362 * History:
2363 * 1/22/00 Rewrote, moved scratch buffer allocation here. Now the
2364 * camera is also initialized here (once per connect), at
2365 * expense of V4L client (it waits on open() call).
2366 * 1/27/00 Used IBMCAM_NUMSBUF as number of URB buffers.
2367 * 5/24/00 Corrected to prevent race condition (MOD_xxx_USE_COUNT).
2369 static int ibmcam_open(struct video_device *dev, int flags)
2371 struct usb_ibmcam *ibmcam = (struct usb_ibmcam *)dev;
2372 const int sb_size = FRAMES_PER_DESC * ibmcam->iso_packet_len;
2373 int i, err = 0;
2375 MOD_INC_USE_COUNT;
2376 down(&ibmcam->lock);
2378 if (ibmcam->user)
2379 err = -EBUSY;
2380 else {
2381 /* Clean pointers so we know if we allocated something */
2382 for (i=0; i < IBMCAM_NUMSBUF; i++)
2383 ibmcam->sbuf[i].data = NULL;
2385 /* Allocate memory for the frame buffers */
2386 ibmcam->fbuf_size = IBMCAM_NUMFRAMES * MAX_FRAME_SIZE;
2387 ibmcam->fbuf = rvmalloc(ibmcam->fbuf_size);
2388 ibmcam->scratch = kmalloc(scratchbufsize, GFP_KERNEL);
2389 ibmcam->scratchlen = 0;
2390 if ((ibmcam->fbuf == NULL) || (ibmcam->scratch == NULL))
2391 err = -ENOMEM;
2392 else {
2393 /* Allocate all buffers */
2394 for (i=0; i < IBMCAM_NUMFRAMES; i++) {
2395 ibmcam->frame[i].grabstate = FRAME_UNUSED;
2396 ibmcam->frame[i].data = ibmcam->fbuf + i*MAX_FRAME_SIZE;
2398 * Set default sizes in case IOCTL (VIDIOCMCAPTURE)
2399 * is not used (using read() instead).
2401 ibmcam->frame[i].width = imgwidth;
2402 ibmcam->frame[i].height = imgheight;
2403 ibmcam->frame[i].bytes_read = 0;
2405 for (i=0; i < IBMCAM_NUMSBUF; i++) {
2406 ibmcam->sbuf[i].data = kmalloc(sb_size, GFP_KERNEL);
2407 if (ibmcam->sbuf[i].data == NULL) {
2408 err = -ENOMEM;
2409 break;
2413 if (err) {
2414 /* Have to free all that memory */
2415 if (ibmcam->fbuf != NULL) {
2416 rvfree(ibmcam->fbuf, ibmcam->fbuf_size);
2417 ibmcam->fbuf = NULL;
2419 if (ibmcam->scratch != NULL) {
2420 kfree(ibmcam->scratch);
2421 ibmcam->scratch = NULL;
2423 for (i=0; i < IBMCAM_NUMSBUF; i++) {
2424 if (ibmcam->sbuf[i].data != NULL) {
2425 kfree (ibmcam->sbuf[i].data);
2426 ibmcam->sbuf[i].data = NULL;
2432 /* If so far no errors then we shall start the camera */
2433 if (!err) {
2434 err = ibmcam_init_isoc(ibmcam);
2435 if (!err) {
2436 /* Send init sequence only once, it's large! */
2437 if (!ibmcam->initialized) {
2438 int setup_ok = 0;
2439 if (ibmcam->camera_model == IBMCAM_MODEL_1)
2440 setup_ok = usb_ibmcam_model1_setup(ibmcam);
2441 else if (ibmcam->camera_model == IBMCAM_MODEL_2)
2442 setup_ok = usb_ibmcam_model2_setup(ibmcam);
2443 if (setup_ok)
2444 ibmcam->initialized = 1;
2445 else
2446 err = -EBUSY;
2448 if (!err)
2449 ibmcam->user++;
2452 up(&ibmcam->lock);
2453 if (err)
2454 MOD_DEC_USE_COUNT;
2455 return err;
2459 * ibmcam_close()
2461 * This is part of Video 4 Linux API. The procedure
2462 * stops streaming and deallocates all buffers that were earlier
2463 * allocated in ibmcam_open().
2465 * History:
2466 * 1/22/00 Moved scratch buffer deallocation here.
2467 * 1/27/00 Used IBMCAM_NUMSBUF as number of URB buffers.
2468 * 5/24/00 Moved MOD_DEC_USE_COUNT outside of code that can sleep.
2470 static void ibmcam_close(struct video_device *dev)
2472 struct usb_ibmcam *ibmcam = (struct usb_ibmcam *)dev;
2473 int i;
2475 down(&ibmcam->lock);
2477 ibmcam_stop_isoc(ibmcam);
2479 rvfree(ibmcam->fbuf, ibmcam->fbuf_size);
2480 kfree(ibmcam->scratch);
2481 for (i=0; i < IBMCAM_NUMSBUF; i++)
2482 kfree(ibmcam->sbuf[i].data);
2484 ibmcam->user--;
2486 if (ibmcam->remove_pending) {
2487 printk(KERN_INFO "ibmcam_close: Final disconnect.\n");
2488 usb_ibmcam_release(ibmcam);
2490 up(&ibmcam->lock);
2491 MOD_DEC_USE_COUNT;
2494 static int ibmcam_init_done(struct video_device *dev)
2496 return 0;
2499 static long ibmcam_write(struct video_device *dev, const char *buf, unsigned long count, int noblock)
2501 return -EINVAL;
2505 * ibmcam_ioctl()
2507 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
2509 * History:
2510 * 1/22/00 Corrected VIDIOCSPICT to reject unsupported settings.
2512 static int ibmcam_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
2514 struct usb_ibmcam *ibmcam = (struct usb_ibmcam *)dev;
2516 if (!IBMCAM_IS_OPERATIONAL(ibmcam))
2517 return -EFAULT;
2519 switch (cmd) {
2520 case VIDIOCGCAP:
2522 if (copy_to_user(arg, &ibmcam->vcap, sizeof(ibmcam->vcap)))
2523 return -EFAULT;
2524 return 0;
2526 case VIDIOCGCHAN:
2528 if (copy_to_user(arg, &ibmcam->vchan, sizeof(ibmcam->vchan)))
2529 return -EFAULT;
2530 return 0;
2532 case VIDIOCSCHAN:
2534 int v;
2536 if (copy_from_user(&v, arg, sizeof(v)))
2537 return -EFAULT;
2538 if ((v < 0) || (v >= 3)) /* 3 grades of lighting conditions */
2539 return -EINVAL;
2540 if (v != ibmcam->vchan.channel) {
2541 ibmcam->vchan.channel = v;
2542 usb_ibmcam_change_lighting_conditions(ibmcam);
2544 return 0;
2546 case VIDIOCGPICT:
2548 if (copy_to_user(arg, &ibmcam->vpic, sizeof(ibmcam->vpic)))
2549 return -EFAULT;
2550 return 0;
2552 case VIDIOCSPICT:
2554 struct video_picture tmp;
2556 * Use temporary 'video_picture' structure to preserve our
2557 * own settings (such as color depth, palette) that we
2558 * aren't allowing everyone (V4L client) to change.
2560 if (copy_from_user(&tmp, arg, sizeof(tmp)))
2561 return -EFAULT;
2562 ibmcam->vpic.brightness = tmp.brightness;
2563 ibmcam->vpic.hue = tmp.hue;
2564 ibmcam->vpic.colour = tmp.colour;
2565 ibmcam->vpic.contrast = tmp.contrast;
2566 usb_ibmcam_adjust_picture(ibmcam);
2567 return 0;
2569 case VIDIOCSWIN:
2571 struct video_window vw;
2573 if (copy_from_user(&vw, arg, sizeof(vw)))
2574 return -EFAULT;
2575 if (vw.flags)
2576 return -EINVAL;
2577 if (vw.clipcount)
2578 return -EINVAL;
2579 if (vw.height != imgheight)
2580 return -EINVAL;
2581 if (vw.width != imgwidth)
2582 return -EINVAL;
2584 ibmcam->compress = 0;
2586 return 0;
2588 case VIDIOCGWIN:
2590 struct video_window vw;
2592 vw.x = 0;
2593 vw.y = 0;
2594 vw.width = imgwidth;
2595 vw.height = imgheight;
2596 vw.chromakey = 0;
2597 vw.flags = usb_ibmcam_calculate_fps();
2599 if (copy_to_user(arg, &vw, sizeof(vw)))
2600 return -EFAULT;
2602 return 0;
2604 case VIDIOCGMBUF:
2606 struct video_mbuf vm;
2608 memset(&vm, 0, sizeof(vm));
2609 vm.size = MAX_FRAME_SIZE * 2;
2610 vm.frames = 2;
2611 vm.offsets[0] = 0;
2612 vm.offsets[1] = MAX_FRAME_SIZE;
2614 if (copy_to_user((void *)arg, (void *)&vm, sizeof(vm)))
2615 return -EFAULT;
2617 return 0;
2619 case VIDIOCMCAPTURE:
2621 struct video_mmap vm;
2623 if (copy_from_user((void *)&vm, (void *)arg, sizeof(vm)))
2624 return -EFAULT;
2626 if (debug >= 1)
2627 printk(KERN_DEBUG "frame: %d, size: %dx%d, format: %d\n",
2628 vm.frame, vm.width, vm.height, vm.format);
2630 if (vm.format != VIDEO_PALETTE_RGB24)
2631 return -EINVAL;
2633 if ((vm.frame != 0) && (vm.frame != 1))
2634 return -EINVAL;
2636 if (ibmcam->frame[vm.frame].grabstate == FRAME_GRABBING)
2637 return -EBUSY;
2639 /* Don't compress if the size changed */
2640 if ((ibmcam->frame[vm.frame].width != vm.width) ||
2641 (ibmcam->frame[vm.frame].height != vm.height))
2642 ibmcam->compress = 0;
2644 ibmcam->frame[vm.frame].width = vm.width;
2645 ibmcam->frame[vm.frame].height = vm.height;
2647 /* Mark it as ready */
2648 ibmcam->frame[vm.frame].grabstate = FRAME_READY;
2650 return ibmcam_new_frame(ibmcam, vm.frame);
2652 case VIDIOCSYNC:
2654 int frame;
2656 if (copy_from_user((void *)&frame, arg, sizeof(int)))
2657 return -EFAULT;
2659 if (debug >= 1)
2660 printk(KERN_DEBUG "ibmcam: syncing to frame %d\n", frame);
2662 switch (ibmcam->frame[frame].grabstate) {
2663 case FRAME_UNUSED:
2664 return -EINVAL;
2665 case FRAME_READY:
2666 case FRAME_GRABBING:
2667 case FRAME_ERROR:
2669 int ntries;
2670 redo:
2671 if (!IBMCAM_IS_OPERATIONAL(ibmcam))
2672 return -EIO;
2673 ntries = 0;
2674 do {
2675 interruptible_sleep_on(&ibmcam->frame[frame].wq);
2676 if (signal_pending(current)) {
2677 if (flags & FLAGS_RETRY_VIDIOCSYNC) {
2678 /* Polling apps will destroy frames with that! */
2679 ibmcam_new_frame(ibmcam, frame);
2680 usb_ibmcam_testpattern(ibmcam, 1, 0);
2681 ibmcam->curframe = -1;
2682 ibmcam->frame_num++;
2684 /* This will request another frame. */
2685 if (waitqueue_active(&ibmcam->frame[frame].wq))
2686 wake_up_interruptible(&ibmcam->frame[frame].wq);
2687 return 0;
2688 } else {
2689 /* Standard answer: not ready yet! */
2690 return -EINTR;
2693 } while (ibmcam->frame[frame].grabstate == FRAME_GRABBING);
2695 if (ibmcam->frame[frame].grabstate == FRAME_ERROR) {
2696 int ret = ibmcam_new_frame(ibmcam, frame);
2697 if (ret < 0)
2698 return ret;
2699 goto redo;
2702 case FRAME_DONE:
2703 ibmcam->frame[frame].grabstate = FRAME_UNUSED;
2704 break;
2707 ibmcam->frame[frame].grabstate = FRAME_UNUSED;
2709 return 0;
2711 case VIDIOCGFBUF:
2713 struct video_buffer vb;
2715 memset(&vb, 0, sizeof(vb));
2716 vb.base = NULL; /* frame buffer not supported, not used */
2718 if (copy_to_user((void *)arg, (void *)&vb, sizeof(vb)))
2719 return -EFAULT;
2721 return 0;
2723 case VIDIOCKEY:
2724 return 0;
2726 case VIDIOCCAPTURE:
2727 return -EINVAL;
2729 case VIDIOCSFBUF:
2731 case VIDIOCGTUNER:
2732 case VIDIOCSTUNER:
2734 case VIDIOCGFREQ:
2735 case VIDIOCSFREQ:
2737 case VIDIOCGAUDIO:
2738 case VIDIOCSAUDIO:
2739 return -EINVAL;
2741 default:
2742 return -ENOIOCTLCMD;
2744 return 0;
2747 static long ibmcam_read(struct video_device *dev, char *buf, unsigned long count, int noblock)
2749 struct usb_ibmcam *ibmcam = (struct usb_ibmcam *)dev;
2750 int frmx = -1;
2751 volatile struct ibmcam_frame *frame;
2753 if (debug >= 1)
2754 printk(KERN_DEBUG "ibmcam_read: %ld bytes, noblock=%d\n", count, noblock);
2756 if (!IBMCAM_IS_OPERATIONAL(ibmcam) || (buf == NULL))
2757 return -EFAULT;
2759 /* See if a frame is completed, then use it. */
2760 if (ibmcam->frame[0].grabstate >= FRAME_DONE) /* _DONE or _ERROR */
2761 frmx = 0;
2762 else if (ibmcam->frame[1].grabstate >= FRAME_DONE)/* _DONE or _ERROR */
2763 frmx = 1;
2765 if (noblock && (frmx == -1))
2766 return -EAGAIN;
2768 /* If no FRAME_DONE, look for a FRAME_GRABBING state. */
2769 /* See if a frame is in process (grabbing), then use it. */
2770 if (frmx == -1) {
2771 if (ibmcam->frame[0].grabstate == FRAME_GRABBING)
2772 frmx = 0;
2773 else if (ibmcam->frame[1].grabstate == FRAME_GRABBING)
2774 frmx = 1;
2777 /* If no frame is active, start one. */
2778 if (frmx == -1)
2779 ibmcam_new_frame(ibmcam, frmx = 0);
2781 frame = &ibmcam->frame[frmx];
2783 restart:
2784 if (!IBMCAM_IS_OPERATIONAL(ibmcam))
2785 return -EIO;
2786 while (frame->grabstate == FRAME_GRABBING) {
2787 interruptible_sleep_on((void *)&frame->wq);
2788 if (signal_pending(current))
2789 return -EINTR;
2792 if (frame->grabstate == FRAME_ERROR) {
2793 frame->bytes_read = 0;
2794 if (ibmcam_new_frame(ibmcam, frmx))
2795 printk(KERN_ERR "ibmcam_read: ibmcam_new_frame error\n");
2796 goto restart;
2799 if (debug >= 1)
2800 printk(KERN_DEBUG "ibmcam_read: frmx=%d, bytes_read=%ld, scanlength=%ld\n",
2801 frmx, frame->bytes_read, frame->scanlength);
2803 /* copy bytes to user space; we allow for partials reads */
2804 if ((count + frame->bytes_read) > frame->scanlength)
2805 count = frame->scanlength - frame->bytes_read;
2807 if (copy_to_user(buf, frame->data + frame->bytes_read, count))
2808 return -EFAULT;
2810 frame->bytes_read += count;
2811 if (debug >= 1)
2812 printk(KERN_DEBUG "ibmcam_read: {copy} count used=%ld, new bytes_read=%ld\n",
2813 count, frame->bytes_read);
2815 if (frame->bytes_read >= frame->scanlength) { /* All data has been read */
2816 frame->bytes_read = 0;
2818 /* Mark it as available to be used again. */
2819 ibmcam->frame[frmx].grabstate = FRAME_UNUSED;
2820 if (ibmcam_new_frame(ibmcam, frmx ? 0 : 1))
2821 printk(KERN_ERR "ibmcam_read: ibmcam_new_frame returned error\n");
2824 return count;
2827 static int ibmcam_mmap(struct video_device *dev, const char *adr, unsigned long size)
2829 struct usb_ibmcam *ibmcam = (struct usb_ibmcam *)dev;
2830 unsigned long start = (unsigned long)adr;
2831 unsigned long page, pos;
2833 if (!IBMCAM_IS_OPERATIONAL(ibmcam))
2834 return -EFAULT;
2836 if (size > (((2 * MAX_FRAME_SIZE) + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1)))
2837 return -EINVAL;
2839 pos = (unsigned long)ibmcam->fbuf;
2840 while (size > 0) {
2841 page = kvirt_to_pa(pos);
2842 if (remap_page_range(start, page, PAGE_SIZE, PAGE_SHARED))
2843 return -EAGAIN;
2845 start += PAGE_SIZE;
2846 pos += PAGE_SIZE;
2847 if (size > PAGE_SIZE)
2848 size -= PAGE_SIZE;
2849 else
2850 size = 0;
2853 return 0;
2856 static struct video_device ibmcam_template = {
2857 "CPiA USB Camera",
2858 VID_TYPE_CAPTURE,
2859 VID_HARDWARE_CPIA,
2860 ibmcam_open,
2861 ibmcam_close,
2862 ibmcam_read,
2863 ibmcam_write,
2864 NULL,
2865 ibmcam_ioctl,
2866 ibmcam_mmap,
2867 ibmcam_init_done,
2868 NULL,
2873 static void usb_ibmcam_configure_video(struct usb_ibmcam *ibmcam)
2875 if (ibmcam == NULL)
2876 return;
2878 RESTRICT_TO_RANGE(init_brightness, 0, 255);
2879 RESTRICT_TO_RANGE(init_contrast, 0, 255);
2880 RESTRICT_TO_RANGE(init_color, 0, 255);
2881 RESTRICT_TO_RANGE(init_hue, 0, 255);
2882 RESTRICT_TO_RANGE(hue_correction, 0, 255);
2884 memset(&ibmcam->vpic, 0, sizeof(ibmcam->vpic));
2885 memset(&ibmcam->vpic_old, 0x55, sizeof(ibmcam->vpic_old));
2887 ibmcam->vpic.colour = init_color << 8;
2888 ibmcam->vpic.hue = init_hue << 8;
2889 ibmcam->vpic.brightness = init_brightness << 8;
2890 ibmcam->vpic.contrast = init_contrast << 8;
2891 ibmcam->vpic.whiteness = 105 << 8; /* This one isn't used */
2892 ibmcam->vpic.depth = 24;
2893 ibmcam->vpic.palette = VIDEO_PALETTE_RGB24;
2895 memset(&ibmcam->vcap, 0, sizeof(ibmcam->vcap));
2896 strcpy(ibmcam->vcap.name, "IBM USB Camera");
2897 ibmcam->vcap.type = VID_TYPE_CAPTURE;
2898 ibmcam->vcap.channels = 1;
2899 ibmcam->vcap.audios = 0;
2900 ibmcam->vcap.maxwidth = imgwidth;
2901 ibmcam->vcap.maxheight = imgheight;
2902 ibmcam->vcap.minwidth = min_imgwidth;
2903 ibmcam->vcap.minheight = min_imgheight;
2905 memset(&ibmcam->vchan, 0, sizeof(ibmcam->vchan));
2906 ibmcam->vchan.flags = 0;
2907 ibmcam->vchan.tuners = 0;
2908 ibmcam->vchan.channel = 0;
2909 ibmcam->vchan.type = VIDEO_TYPE_CAMERA;
2910 strcpy(ibmcam->vchan.name, "Camera");
2914 * ibmcam_find_struct()
2916 * This code searches the array of preallocated (static) structures
2917 * and returns index of the first one that isn't in use. Returns -1
2918 * if there are no free structures.
2920 * History:
2921 * 1/27/00 Created.
2923 static int ibmcam_find_struct(void)
2925 int i, u;
2927 for (u = 0; u < MAX_IBMCAM; u++) {
2928 struct usb_ibmcam *ibmcam = &cams[u];
2929 if (!ibmcam->ibmcam_used) /* This one is free */
2931 ibmcam->ibmcam_used = 1; /* In use now */
2932 for (i=0; i < IBMCAM_NUMFRAMES; i++)
2933 init_waitqueue_head(&ibmcam->frame[i].wq);
2934 init_MUTEX(&ibmcam->lock); /* to 1 == available */
2935 ibmcam->dev = NULL;
2936 memcpy(&ibmcam->vdev, &ibmcam_template, sizeof(ibmcam_template));
2937 return u;
2940 return -1;
2944 * usb_ibmcam_probe()
2946 * This procedure queries device descriptor and accepts the interface
2947 * if it looks like IBM C-it camera.
2949 * History:
2950 * 1/22/00 Moved camera init code to ibmcam_open()
2951 * 1/27/00 Changed to use static structures, added locking.
2952 * 5/24/00 Corrected to prevent race condition (MOD_xxx_USE_COUNT).
2953 * 7/3/00 Fixed endianness bug.
2955 static void *usb_ibmcam_probe(struct usb_device *dev, unsigned int ifnum)
2957 struct usb_ibmcam *ibmcam = NULL;
2958 const struct usb_interface_descriptor *interface;
2959 const struct usb_endpoint_descriptor *endpoint;
2960 int devnum, model=0;
2962 if (debug >= 1)
2963 printk(KERN_DEBUG "ibmcam_probe(%p,%u.)\n", dev, ifnum);
2965 /* We don't handle multi-config cameras */
2966 if (dev->descriptor.bNumConfigurations != 1)
2967 return NULL;
2969 /* Is it an IBM camera? */
2970 if ((dev->descriptor.idVendor != 0x0545) ||
2971 (dev->descriptor.idProduct != 0x8080))
2972 return NULL;
2974 /* Check the version/revision */
2975 switch (dev->descriptor.bcdDevice) {
2976 case 0x0002:
2977 if (ifnum != 2)
2978 return NULL;
2979 printk(KERN_INFO "IBM USB camera found (model 1, rev. 0x%04x).\n",
2980 dev->descriptor.bcdDevice);
2981 model = IBMCAM_MODEL_1;
2982 break;
2983 case 0x030A:
2984 if (ifnum != 0)
2985 return NULL;
2986 printk(KERN_INFO "IBM USB camera found (model 2, rev. 0x%04x).\n",
2987 dev->descriptor.bcdDevice);
2988 model = IBMCAM_MODEL_2;
2989 break;
2990 default:
2991 printk(KERN_ERR "IBM camera with revision 0x%04x is not supported.\n",
2992 dev->descriptor.bcdDevice);
2993 return NULL;
2996 /* Validate found interface: must have one ISO endpoint */
2997 interface = &dev->actconfig->interface[ifnum].altsetting[0];
2998 if (interface->bNumEndpoints != 1) {
2999 printk(KERN_ERR "IBM camera: interface %d. has %u. endpoints!\n",
3000 ifnum, (unsigned)(interface->bNumEndpoints));
3001 return NULL;
3003 endpoint = &interface->endpoint[0];
3004 if ((endpoint->bmAttributes & 0x03) != 0x01) {
3005 printk(KERN_ERR "IBM camera: interface %d. has non-ISO endpoint!\n", ifnum);
3006 return NULL;
3008 if ((endpoint->bEndpointAddress & 0x80) == 0) {
3009 printk(KERN_ERR "IBM camera: interface %d. has ISO OUT endpoint!\n", ifnum);
3010 return NULL;
3013 /* Validate options */
3014 if (model == IBMCAM_MODEL_1) {
3015 RESTRICT_TO_RANGE(lighting, 0, 2);
3016 RESTRICT_TO_RANGE(videosize, VIDEOSIZE_128x96, VIDEOSIZE_352x288);
3017 } else {
3018 RESTRICT_TO_RANGE(lighting, 0, 15);
3019 RESTRICT_TO_RANGE(videosize, VIDEOSIZE_176x144, VIDEOSIZE_352x240);
3022 /* Code below may sleep, need to lock module while we are here */
3023 MOD_INC_USE_COUNT;
3025 devnum = ibmcam_find_struct();
3026 if (devnum == -1) {
3027 printk(KERN_INFO "IBM USB camera driver: Too many devices!\n");
3028 ibmcam = NULL; /* Do not free, it's preallocated */
3029 goto probe_done;
3031 ibmcam = &cams[devnum];
3033 down(&ibmcam->lock);
3034 ibmcam->camera_model = model;
3035 ibmcam->remove_pending = 0;
3036 ibmcam->last_error = 0;
3037 ibmcam->dev = dev;
3038 ibmcam->iface = ifnum;
3039 ibmcam->ifaceAltInactive = 0;
3040 ibmcam->ifaceAltActive = 1;
3041 ibmcam->video_endp = endpoint->bEndpointAddress;
3042 ibmcam->iso_packet_len = 1014;
3043 ibmcam->compress = 0;
3044 ibmcam->user=0;
3046 usb_ibmcam_configure_video(ibmcam);
3047 up (&ibmcam->lock);
3049 if (video_register_device(&ibmcam->vdev, VFL_TYPE_GRABBER) == -1) {
3050 printk(KERN_ERR "video_register_device failed\n");
3051 ibmcam = NULL; /* Do not free, it's preallocated */
3053 if (debug > 1)
3054 printk(KERN_DEBUG "video_register_device() successful\n");
3055 probe_done:
3056 MOD_DEC_USE_COUNT;
3057 return ibmcam;
3061 * usb_ibmcam_release()
3063 * This code does final release of struct usb_ibmcam. This happens
3064 * after the device is disconnected -and- all clients closed their files.
3066 * History:
3067 * 1/27/00 Created.
3069 static void usb_ibmcam_release(struct usb_ibmcam *ibmcam)
3071 video_unregister_device(&ibmcam->vdev);
3072 if (debug > 0)
3073 printk(KERN_DEBUG "usb_ibmcam_release: Video unregistered.\n");
3074 ibmcam->ibmcam_used = 0;
3075 ibmcam->initialized = 0;
3079 * usb_ibmcam_disconnect()
3081 * This procedure stops all driver activity, deallocates interface-private
3082 * structure (pointed by 'ptr') and after that driver should be removable
3083 * with no ill consequences.
3085 * This code handles surprise removal. The ibmcam->user is a counter which
3086 * increments on open() and decrements on close(). If we see here that
3087 * this counter is not 0 then we have a client who still has us opened.
3088 * We set ibmcam->remove_pending flag as early as possible, and after that
3089 * all access to the camera will gracefully fail. These failures should
3090 * prompt client to (eventually) close the video device, and then - in
3091 * ibmcam_close() - we decrement ibmcam->ibmcam_used and usage counter.
3093 * History:
3094 * 1/22/00 Added polling of MOD_IN_USE to delay removal until all users gone.
3095 * 1/27/00 Reworked to allow pending disconnects; see ibmcam_close()
3096 * 5/24/00 Corrected to prevent race condition (MOD_xxx_USE_COUNT).
3098 static void usb_ibmcam_disconnect(struct usb_device *dev, void *ptr)
3100 static const char proc[] = "usb_ibmcam_disconnect";
3101 struct usb_ibmcam *ibmcam = (struct usb_ibmcam *) ptr;
3103 MOD_INC_USE_COUNT;
3105 if (debug > 0)
3106 printk(KERN_DEBUG "%s(%p,%p.)\n", proc, dev, ptr);
3108 down(&ibmcam->lock);
3109 ibmcam->remove_pending = 1; /* Now all ISO data will be ignored */
3111 /* At this time we ask to cancel outstanding URBs */
3112 ibmcam_stop_isoc(ibmcam);
3114 ibmcam->dev = NULL; /* USB device is no more */
3116 if (ibmcam->user)
3117 printk(KERN_INFO "%s: In use, disconnect pending.\n", proc);
3118 else
3119 usb_ibmcam_release(ibmcam);
3120 up(&ibmcam->lock);
3121 printk(KERN_INFO "IBM USB camera disconnected.\n");
3123 MOD_DEC_USE_COUNT;
3126 static struct usb_driver ibmcam_driver = {
3127 "ibmcam",
3128 usb_ibmcam_probe,
3129 usb_ibmcam_disconnect,
3130 { NULL, NULL }
3134 * usb_ibmcam_init()
3136 * This code is run to initialize the driver.
3138 * History:
3139 * 1/27/00 Reworked to use statically allocated usb_ibmcam structures.
3141 int usb_ibmcam_init(void)
3143 unsigned u;
3145 /* Initialize struct */
3146 for (u = 0; u < MAX_IBMCAM; u++) {
3147 struct usb_ibmcam *ibmcam = &cams[u];
3148 memset (ibmcam, 0, sizeof(struct usb_ibmcam));
3150 return usb_register(&ibmcam_driver);
3153 void usb_ibmcam_cleanup(void)
3155 usb_deregister(&ibmcam_driver);
3158 #ifdef MODULE
3159 int init_module(void)
3161 return usb_ibmcam_init();
3164 void cleanup_module(void)
3166 usb_ibmcam_cleanup();
3168 #endif