- pre3:
[davej-history.git] / drivers / usb / ibmcam.c
blob0364b33c6093f2c08d69837f127f4cfffa4b47fe
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/init.h>
39 #include <linux/spinlock.h>
40 #include <linux/usb.h>
42 #include <asm/io.h>
44 #include "ibmcam.h"
46 #define ENABLE_HEXDUMP 0 /* Enable if you need it */
47 static int debug = 0;
49 /* Completion states of the data parser */
50 typedef enum {
51 scan_Continue, /* Just parse next item */
52 scan_NextFrame, /* Frame done, send it to V4L */
53 scan_Out, /* Not enough data for frame */
54 scan_EndParse /* End parsing */
55 } scan_state_t;
57 /* Bit flags (options) */
58 #define FLAGS_RETRY_VIDIOCSYNC (1 << 0)
59 #define FLAGS_MONOCHROME (1 << 1)
60 #define FLAGS_DISPLAY_HINTS (1 << 2)
61 #define FLAGS_OVERLAY_STATS (1 << 3)
62 #define FLAGS_FORCE_TESTPATTERN (1 << 4)
63 #define FLAGS_SEPARATE_FRAMES (1 << 5)
64 #define FLAGS_CLEAN_FRAMES (1 << 6)
66 static int flags = 0; /* FLAGS_DISPLAY_HINTS | FLAGS_OVERLAY_STATS; */
68 /* This is the size of V4L frame that we provide */
69 static const int imgwidth = V4L_FRAME_WIDTH_USED;
70 static const int imgheight = V4L_FRAME_HEIGHT;
71 static const int min_imgwidth = 8;
72 static const int min_imgheight = 4;
74 static int lighting = 1; /* Medium */
76 #define SHARPNESS_MIN 0
77 #define SHARPNESS_MAX 6
78 static int sharpness = 4; /* Low noise, good details */
80 #define FRAMERATE_MIN 0
81 #define FRAMERATE_MAX 6
82 static int framerate = 2; /* Lower, reliable frame rate (8-12 fps) */
84 enum {
85 VIDEOSIZE_128x96 = 0,
86 VIDEOSIZE_176x144,
87 VIDEOSIZE_352x288,
88 VIDEOSIZE_320x240,
89 VIDEOSIZE_352x240,
92 static int videosize = VIDEOSIZE_352x288;
95 * The value of 'scratchbufsize' affects quality of the picture
96 * in many ways. Shorter buffers may cause loss of data when client
97 * is too slow. Larger buffers are memory-consuming and take longer
98 * to work with. This setting can be adjusted, but the default value
99 * should be OK for most desktop users.
101 #define DEFAULT_SCRATCH_BUF_SIZE (0x10000) /* 64 KB */
102 static const int scratchbufsize = DEFAULT_SCRATCH_BUF_SIZE;
105 * Here we define several initialization variables. They may
106 * be used to automatically set color, hue, brightness and
107 * contrast to desired values. This is particularly useful in
108 * case of webcams (which have no controls and no on-screen
109 * output) and also when a client V4L software is used that
110 * does not have some of those controls. In any case it's
111 * good to have startup values as options.
113 * These values are all in [0..255] range. This simplifies
114 * operation. Note that actual values of V4L variables may
115 * be scaled up (as much as << 8). User can see that only
116 * on overlay output, however, or through a V4L client.
118 static int init_brightness = 128;
119 static int init_contrast = 192;
120 static int init_color = 128;
121 static int init_hue = 128;
122 static int hue_correction = 128;
124 /* Settings for camera model 2 */
125 static int init_model2_rg = -1;
126 static int init_model2_rg2 = -1;
127 static int init_model2_sat = -1;
128 static int init_model2_yb = -1;
130 MODULE_PARM(debug, "i");
131 MODULE_PARM_DESC(debug, "Debug level: 0-9 (default=0)");
132 MODULE_PARM(flags, "i");
133 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");
134 MODULE_PARM(framerate, "i");
135 MODULE_PARM_DESC(framerate, "Framerate setting: 0=slowest, 6=fastest (default=2)");
136 MODULE_PARM(lighting, "i");
137 MODULE_PARM_DESC(lighting, "Photosensitivity: 0=bright, 1=medium (default), 2=low light");
138 MODULE_PARM(sharpness, "i");
139 MODULE_PARM_DESC(sharpness, "Model1 noise reduction: 0=smooth, 6=sharp (default=4)");
140 MODULE_PARM(videosize, "i");
141 MODULE_PARM_DESC(videosize, "Image size: 0=128x96, 1=176x144, 2=352x288, 3=320x240, 4=352x240 (default=1)");
142 MODULE_PARM(init_brightness, "i");
143 MODULE_PARM_DESC(init_brightness, "Brightness preconfiguration: 0-255 (default=128)");
144 MODULE_PARM(init_contrast, "i");
145 MODULE_PARM_DESC(init_contrast, "Contrast preconfiguration: 0-255 (default=192)");
146 MODULE_PARM(init_color, "i");
147 MODULE_PARM_DESC(init_color, "Dolor preconfiguration: 0-255 (default=128)");
148 MODULE_PARM(init_hue, "i");
149 MODULE_PARM_DESC(init_hue, "Hue preconfiguration: 0-255 (default=128)");
150 MODULE_PARM(hue_correction, "i");
151 MODULE_PARM_DESC(hue_correction, "YUV colorspace regulation: 0-255 (default=128)");
153 MODULE_PARM(init_model2_rg, "i");
154 MODULE_PARM_DESC(init_model2_rg, "Model2 preconfiguration: 0-255 (default=112)");
155 MODULE_PARM(init_model2_rg2, "i");
156 MODULE_PARM_DESC(init_model2_rg2, "Model2 preconfiguration: 0-255 (default=47)");
157 MODULE_PARM(init_model2_sat, "i");
158 MODULE_PARM_DESC(init_model2_sat, "Model2 preconfiguration: 0-255 (default=52)");
159 MODULE_PARM(init_model2_yb, "i");
160 MODULE_PARM_DESC(init_model2_yb, "Model2 preconfiguration: 0-255 (default=160)");
162 MODULE_AUTHOR ("module author");
163 MODULE_DESCRIPTION ("IBM/Xirlink C-it USB Camera Driver for Linux (c) 2000");
165 /* Still mysterious i2c commands */
166 static const unsigned short unknown_88 = 0x0088;
167 static const unsigned short unknown_89 = 0x0089;
168 static const unsigned short bright_3x[3] = { 0x0031, 0x0032, 0x0033 };
169 static const unsigned short contrast_14 = 0x0014;
170 static const unsigned short light_27 = 0x0027;
171 static const unsigned short sharp_13 = 0x0013;
173 /* i2c commands for Model 2 cameras */
174 static const unsigned short mod2_brightness = 0x001a; /* $5b .. $ee; default=$5a */
175 static const unsigned short mod2_set_framerate = 0x001c; /* 0 (fast).. $1F (slow) */
176 static const unsigned short mod2_color_balance_rg2 = 0x001e; /* 0 (red) .. $7F (green) */
177 static const unsigned short mod2_saturation = 0x0020; /* 0 (b/w) - $7F (full color) */
178 static const unsigned short mod2_color_balance_yb = 0x0022; /* 0..$7F, $50 is about right */
179 static const unsigned short mod2_color_balance_rg = 0x0024; /* 0..$7F, $70 is about right */
180 static const unsigned short mod2_sensitivity = 0x0028; /* 0 (min) .. $1F (max) */
182 #define MAX_IBMCAM 4
184 struct usb_ibmcam cams[MAX_IBMCAM];
186 /*******************************/
187 /* Memory management functions */
188 /*******************************/
190 #define MDEBUG(x) do { } while(0) /* Debug memory management */
192 static struct usb_driver ibmcam_driver;
193 static void usb_ibmcam_release(struct usb_ibmcam *ibmcam);
195 /* Given PGD from the address space's page table, return the kernel
196 * virtual mapping of the physical memory mapped at ADR.
198 static inline unsigned long uvirt_to_kva(pgd_t *pgd, unsigned long adr)
200 unsigned long ret = 0UL;
201 pmd_t *pmd;
202 pte_t *ptep, pte;
204 if (!pgd_none(*pgd)) {
205 pmd = pmd_offset(pgd, adr);
206 if (!pmd_none(*pmd)) {
207 ptep = pte_offset(pmd, adr);
208 pte = *ptep;
209 if (pte_present(pte)) {
210 ret = (unsigned long) page_address(pte_page(pte));
211 ret |= (adr & (PAGE_SIZE - 1));
215 MDEBUG(printk("uv2kva(%lx-->%lx)", adr, ret));
216 return ret;
219 static inline unsigned long uvirt_to_bus(unsigned long adr)
221 unsigned long kva, ret;
223 kva = uvirt_to_kva(pgd_offset(current->mm, adr), adr);
224 ret = virt_to_bus((void *)kva);
225 MDEBUG(printk("uv2b(%lx-->%lx)", adr, ret));
226 return ret;
229 static inline unsigned long kvirt_to_bus(unsigned long adr)
231 unsigned long va, kva, ret;
233 va = VMALLOC_VMADDR(adr);
234 kva = uvirt_to_kva(pgd_offset_k(va), va);
235 ret = virt_to_bus((void *)kva);
236 MDEBUG(printk("kv2b(%lx-->%lx)", adr, ret));
237 return ret;
240 /* Here we want the physical address of the memory.
241 * This is used when initializing the contents of the
242 * area and marking the pages as reserved.
244 static inline unsigned long kvirt_to_pa(unsigned long adr)
246 unsigned long va, kva, ret;
248 va = VMALLOC_VMADDR(adr);
249 kva = uvirt_to_kva(pgd_offset_k(va), va);
250 ret = __pa(kva);
251 MDEBUG(printk("kv2pa(%lx-->%lx)", adr, ret));
252 return ret;
255 static void *rvmalloc(unsigned long size)
257 void *mem;
258 unsigned long adr, page;
260 /* Round it off to PAGE_SIZE */
261 size += (PAGE_SIZE - 1);
262 size &= ~(PAGE_SIZE - 1);
264 mem = vmalloc_32(size);
265 if (!mem)
266 return NULL;
268 memset(mem, 0, size); /* Clear the ram out, no junk to the user */
269 adr = (unsigned long) mem;
270 while (size > 0) {
271 page = kvirt_to_pa(adr);
272 mem_map_reserve(virt_to_page(__va(page)));
273 adr += PAGE_SIZE;
274 if (size > PAGE_SIZE)
275 size -= PAGE_SIZE;
276 else
277 size = 0;
280 return mem;
283 static void rvfree(void *mem, unsigned long size)
285 unsigned long adr, page;
287 if (!mem)
288 return;
290 size += (PAGE_SIZE - 1);
291 size &= ~(PAGE_SIZE - 1);
293 adr=(unsigned long) mem;
294 while (size > 0) {
295 page = kvirt_to_pa(adr);
296 mem_map_unreserve(virt_to_page(__va(page)));
297 adr += PAGE_SIZE;
298 if (size > PAGE_SIZE)
299 size -= PAGE_SIZE;
300 else
301 size = 0;
303 vfree(mem);
306 #if ENABLE_HEXDUMP
307 static void ibmcam_hexdump(const unsigned char *data, int len)
309 char tmp[80];
310 int i, k;
312 for (i=k=0; len > 0; i++, len--) {
313 if (i > 0 && (i%16 == 0)) {
314 printk("%s\n", tmp);
315 k=0;
317 k += sprintf(&tmp[k], "%02x ", data[i]);
319 if (k > 0)
320 printk("%s\n", tmp);
322 #endif
325 * usb_ibmcam_overlaychar()
327 * History:
328 * 1/2/00 Created.
330 void usb_ibmcam_overlaychar(
331 struct usb_ibmcam *ibmcam,
332 struct ibmcam_frame *frame,
333 int x, int y, int ch)
335 static const unsigned short digits[16] = {
336 0xF6DE, /* 0 */
337 0x2492, /* 1 */
338 0xE7CE, /* 2 */
339 0xE79E, /* 3 */
340 0xB792, /* 4 */
341 0xF39E, /* 5 */
342 0xF3DE, /* 6 */
343 0xF492, /* 7 */
344 0xF7DE, /* 8 */
345 0xF79E, /* 9 */
346 0x77DA, /* a */
347 0xD75C, /* b */
348 0xF24E, /* c */
349 0xD6DC, /* d */
350 0xF34E, /* e */
351 0xF348 /* f */
353 unsigned short digit;
354 int ix, iy;
356 if ((ibmcam == NULL) || (frame == NULL))
357 return;
359 if (ch >= '0' && ch <= '9')
360 ch -= '0';
361 else if (ch >= 'A' && ch <= 'F')
362 ch = 10 + (ch - 'A');
363 else if (ch >= 'a' && ch <= 'f')
364 ch = 10 + (ch - 'a');
365 else
366 return;
367 digit = digits[ch];
369 for (iy=0; iy < 5; iy++) {
370 for (ix=0; ix < 3; ix++) {
371 if (digit & 0x8000) {
372 IBMCAM_PUTPIXEL(frame, x+ix, y+iy, 0xFF, 0xFF, 0xFF);
374 digit = digit << 1;
380 * usb_ibmcam_overlaystring()
382 * History:
383 * 1/2/00 Created.
385 void usb_ibmcam_overlaystring(
386 struct usb_ibmcam *ibmcam,
387 struct ibmcam_frame *frame,
388 int x, int y, const char *str)
390 while (*str) {
391 usb_ibmcam_overlaychar(ibmcam, frame, x, y, *str);
392 str++;
393 x += 4; /* 3 pixels character + 1 space */
398 * usb_ibmcam_overlaystats()
400 * Overlays important debugging information.
402 * History:
403 * 1/2/00 Created.
405 void usb_ibmcam_overlaystats(struct usb_ibmcam *ibmcam, struct ibmcam_frame *frame)
407 const int y_diff = 8;
408 char tmp[16];
409 int x = 10;
410 int y = 10;
412 sprintf(tmp, "%8x", ibmcam->frame_num);
413 usb_ibmcam_overlaystring(ibmcam, frame, x, y, tmp);
414 y += y_diff;
416 sprintf(tmp, "%8lx", ibmcam->urb_count);
417 usb_ibmcam_overlaystring(ibmcam, frame, x, y, tmp);
418 y += y_diff;
420 sprintf(tmp, "%8lx", ibmcam->urb_length);
421 usb_ibmcam_overlaystring(ibmcam, frame, x, y, tmp);
422 y += y_diff;
424 sprintf(tmp, "%8lx", ibmcam->data_count);
425 usb_ibmcam_overlaystring(ibmcam, frame, x, y, tmp);
426 y += y_diff;
428 sprintf(tmp, "%8lx", ibmcam->header_count);
429 usb_ibmcam_overlaystring(ibmcam, frame, x, y, tmp);
430 y += y_diff;
432 sprintf(tmp, "%8lx", ibmcam->scratch_ovf_count);
433 usb_ibmcam_overlaystring(ibmcam, frame, x, y, tmp);
434 y += y_diff;
436 sprintf(tmp, "%8lx", ibmcam->iso_skip_count);
437 usb_ibmcam_overlaystring(ibmcam, frame, x, y, tmp);
438 y += y_diff;
440 sprintf(tmp, "%8lx", ibmcam->iso_err_count);
441 usb_ibmcam_overlaystring(ibmcam, frame, x, y, tmp);
442 y += y_diff;
444 sprintf(tmp, "%8x", ibmcam->vpic.colour);
445 usb_ibmcam_overlaystring(ibmcam, frame, x, y, tmp);
446 y += y_diff;
448 sprintf(tmp, "%8x", ibmcam->vpic.hue);
449 usb_ibmcam_overlaystring(ibmcam, frame, x, y, tmp);
450 y += y_diff;
452 sprintf(tmp, "%8x", ibmcam->vpic.brightness >> 8);
453 usb_ibmcam_overlaystring(ibmcam, frame, x, y, tmp);
454 y += y_diff;
456 sprintf(tmp, "%8x", ibmcam->vpic.contrast >> 12);
457 usb_ibmcam_overlaystring(ibmcam, frame, x, y, tmp);
458 y += y_diff;
460 sprintf(tmp, "%8d", ibmcam->vpic.whiteness >> 8);
461 usb_ibmcam_overlaystring(ibmcam, frame, x, y, tmp);
462 y += y_diff;
466 * usb_ibmcam_testpattern()
468 * Procedure forms a test pattern (yellow grid on blue background).
470 * Parameters:
471 * fullframe: if TRUE then entire frame is filled, otherwise the procedure
472 * continues from the current scanline.
473 * pmode 0: fill the frame with solid blue color (like on VCR or TV)
474 * 1: Draw a colored grid
476 * History:
477 * 1/2/00 Created.
479 void usb_ibmcam_testpattern(struct usb_ibmcam *ibmcam, int fullframe, int pmode)
481 static const char proc[] = "usb_ibmcam_testpattern";
482 struct ibmcam_frame *frame;
483 unsigned char *f;
484 int num_cell = 0;
485 int scan_length = 0;
486 static int num_pass = 0;
488 if (ibmcam == NULL) {
489 printk(KERN_ERR "%s: ibmcam == NULL\n", proc);
490 return;
492 if ((ibmcam->curframe < 0) || (ibmcam->curframe >= IBMCAM_NUMFRAMES)) {
493 printk(KERN_ERR "%s: ibmcam->curframe=%d.\n", proc, ibmcam->curframe);
494 return;
497 /* Grab the current frame */
498 frame = &ibmcam->frame[ibmcam->curframe];
500 /* Optionally start at the beginning */
501 if (fullframe) {
502 frame->curline = 0;
503 frame->scanlength = 0;
506 /* Form every scan line */
507 for (; frame->curline < imgheight; frame->curline++) {
508 int i;
510 f = frame->data + (imgwidth * 3 * frame->curline);
511 for (i=0; i < imgwidth; i++) {
512 unsigned char cb=0x80;
513 unsigned char cg = 0;
514 unsigned char cr = 0;
516 if (pmode == 1) {
517 if (frame->curline % 32 == 0)
518 cb = 0, cg = cr = 0xFF;
519 else if (i % 32 == 0) {
520 if (frame->curline % 32 == 1)
521 num_cell++;
522 cb = 0, cg = cr = 0xFF;
523 } else {
524 cb = ((num_cell*7) + num_pass) & 0xFF;
525 cg = ((num_cell*5) + num_pass*2) & 0xFF;
526 cr = ((num_cell*3) + num_pass*3) & 0xFF;
528 } else {
529 /* Just the blue screen */
532 *f++ = cb;
533 *f++ = cg;
534 *f++ = cr;
535 scan_length += 3;
539 frame->grabstate = FRAME_DONE;
540 frame->scanlength += scan_length;
541 ++num_pass;
543 /* We do this unconditionally, regardless of FLAGS_OVERLAY_STATS */
544 usb_ibmcam_overlaystats(ibmcam, frame);
547 static unsigned char *ibmcam_model1_find_header(unsigned char hdr_sig, unsigned char *data, int len)
549 while (len >= 4)
551 if ((data[0] == 0x00) && (data[1] == 0xFF) && (data[2] == 0x00))
553 #if 0
554 /* This code helps to detect new frame markers */
555 printk(KERN_DEBUG "Header sig: 00 FF 00 %02X\n", data[3]);
556 #endif
557 if (data[3] == hdr_sig) {
558 if (debug > 2)
559 printk(KERN_DEBUG "Header found.\n");
560 return data+4;
563 ++data;
564 --len;
566 return NULL;
569 static unsigned char *ibmcam_model2_find_header(unsigned char hdr_sig, unsigned char *data, int len)
571 int marker_len = 0;
573 switch (videosize) {
574 case VIDEOSIZE_176x144:
575 marker_len = 10;
576 break;
577 default:
578 marker_len = 2;
579 break;
581 while (len >= marker_len)
583 if ((data[0] == 0x00) && (data[1] == 0xFF))
585 #if 0
586 /* This code helps to detect new frame markers */
587 static int pass = 0;
588 if (pass++ == 0)
589 ibmcam_hexdump(data, (len > 16) ? 16 : len);
590 #endif
591 if (debug > 2)
592 printk(KERN_DEBUG "Header found.\n");
593 return data+marker_len;
595 ++data;
596 --len;
598 return NULL;
601 /* How much data is left in the scratch buf? */
602 #define scratch_left(x) (ibmcam->scratchlen - (int)((char *)x - (char *)ibmcam->scratch))
604 /* Grab the remaining */
605 static void usb_ibmcam_align_scratch(struct usb_ibmcam *ibmcam, unsigned char *data)
607 unsigned long left;
609 left = scratch_left(data);
610 memmove(ibmcam->scratch, data, left);
611 ibmcam->scratchlen = left;
615 * usb_ibmcam_find_header()
617 * Locate one of supported header markers in the scratch buffer.
618 * Once found, remove all preceding bytes AND the marker (4 bytes)
619 * from the scratch buffer. Whatever follows must be video lines.
621 * History:
622 * 1/21/00 Created.
624 static scan_state_t usb_ibmcam_find_header(struct usb_ibmcam *ibmcam)
626 struct ibmcam_frame *frame;
627 unsigned char *data, *tmp;
629 data = ibmcam->scratch;
630 frame = &ibmcam->frame[ibmcam->curframe];
632 if (ibmcam->camera_model == IBMCAM_MODEL_1)
633 tmp = ibmcam_model1_find_header(frame->hdr_sig, data, scratch_left(data));
634 else if (ibmcam->camera_model == IBMCAM_MODEL_2)
635 tmp = ibmcam_model2_find_header(frame->hdr_sig, data, scratch_left(data));
636 else
637 tmp = NULL;
639 if (tmp == NULL) {
640 /* No header - entire scratch buffer is useless! */
641 if (debug > 2)
642 printk(KERN_DEBUG "Skipping frame, no header\n");
643 ibmcam->scratchlen = 0;
644 return scan_EndParse;
646 /* Header found */
647 data = tmp;
649 ibmcam->has_hdr = 1;
650 ibmcam->header_count++;
651 frame->scanstate = STATE_LINES;
652 frame->curline = 0;
654 if (flags & FLAGS_FORCE_TESTPATTERN) {
655 usb_ibmcam_testpattern(ibmcam, 1, 1);
656 return scan_NextFrame;
658 usb_ibmcam_align_scratch(ibmcam, data);
659 return scan_Continue;
663 * usb_ibmcam_parse_lines()
665 * Parse one line (TODO: more than one!) from the scratch buffer, put
666 * decoded RGB value into the current frame buffer and add the written
667 * number of bytes (RGB) to the *pcopylen.
669 * History:
670 * 1/21/00 Created.
672 static scan_state_t usb_ibmcam_parse_lines(struct usb_ibmcam *ibmcam, long *pcopylen)
674 struct ibmcam_frame *frame;
675 unsigned char *data, *f, *chromaLine;
676 unsigned int len;
677 const int v4l_linesize = imgwidth * V4L_BYTES_PER_PIXEL; /* V4L line offset */
678 const int hue_corr = (ibmcam->vpic.hue - 0x8000) >> 10; /* -32..+31 */
679 const int hue2_corr = (hue_correction - 128) / 4; /* -32..+31 */
680 const int ccm = 128; /* Color correction median - see below */
681 int y, u, v, i, frame_done=0, mono_plane, color_corr;
683 color_corr = (ibmcam->vpic.colour - 0x8000) >> 8; /* -128..+127 = -ccm..+(ccm-1)*/
684 RESTRICT_TO_RANGE(color_corr, -ccm, ccm+1);
685 data = ibmcam->scratch;
686 frame = &ibmcam->frame[ibmcam->curframe];
688 len = frame->frmwidth * 3; /* 1 line of mono + 1 line of color */
689 /*printk(KERN_DEBUG "len=%d. left=%d.\n",len,scratch_left(data));*/
691 mono_plane = ((frame->curline & 1) == 0);
694 * Lines are organized this way (or are they?)
696 * I420:
697 * ~~~~
698 * ___________________________________
699 * |-----Y-----|---UVUVUV...UVUV-----| \
700 * |-----------+---------------------| \
701 * |<-- 176 -->|<------ 176*2 ------>| Total 72. pairs of lines
702 * |... ... ...| /
703 * |___________|_____________________| /
704 * - odd line- ------- even line ---
706 * another format:
707 * ~~~~~~~~~~~~~~
708 * ___________________________________
709 * |-----Y-----|---UVUVUV...UVUV-----| \
710 * |-----------+---------------------| \
711 * |<-- 352 -->|<------ 352*2 ------>| Total 144. pairs of lines
712 * |... ... ...| /
713 * |___________|_____________________| /
714 * - odd line- ------- even line ---
717 /* Make sure there's enough data for the entire line */
718 if (scratch_left(data) < (len+1024)) {
719 /*printk(KERN_DEBUG "out of data, need %u.\n", len);*/
720 return scan_Out;
723 #if 0
724 { /* This code prints beginning of the source frame */
725 static int pass = 0;
726 if ((pass++ % 3000) == 0)
727 ibmcam_hexdump(data, 16);
729 #endif
731 #if 0
732 if (frame->curline == 10 || frame->curline == 11) {
733 /* This code prints beginning of 10th (mono), 11th (chroma) line */
734 static int pass = 0;
735 if ((pass % 100) == 0)
736 ibmcam_hexdump(data, 16);
737 if (frame->curline == 11)
738 pass++;
740 #endif
742 * Make sure that our writing into output buffer
743 * will not exceed the buffer. Mind that we may write
744 * not into current output scanline but in several after
745 * it as well (if we enlarge image vertically.)
747 if ((frame->curline + 1) >= V4L_FRAME_HEIGHT)
748 return scan_NextFrame;
751 * Now we are sure that entire line (representing all 'frame->frmwidth'
752 * pixels from the camera) is available in the scratch buffer. We
753 * start copying the line left-aligned to the V4L buffer (which
754 * might be larger - not smaller, hopefully). If the camera
755 * line is shorter then we should pad the V4L buffer with something
756 * (black in this case) to complete the line.
758 f = frame->data + (v4l_linesize * frame->curline);
761 * chromaLine points to 1st pixel of the line with chrominance.
762 * If current line is monochrome then chromaLine points to next
763 * line after monochrome one. If current line has chrominance
764 * then chromaLine points to this very line. Such method allows
765 * to access chrominance data uniformly.
767 * To obtain chrominance data from the 'chromaLine' use this:
768 * v = chromaLine[0]; // 0-1:[0], 2-3:[4], 4-5:[8]...
769 * u = chromaLine[2]; // 0-1:[2], 2-3:[6], 4-5:[10]...
771 * Indices must be calculated this way:
772 * v_index = (i >> 1) << 2;
773 * u_index = (i >> 1) << 2 + 2;
775 * where 'i' is the column number [0..frame->frmwidth-1]
777 chromaLine = data;
778 if (mono_plane)
779 chromaLine += frame->frmwidth;
781 for (i = 0; i < frame->frmwidth; i++, data += (mono_plane ? 1 : 2))
783 unsigned char rv, gv, bv; /* RGB components */
786 * Search for potential Start-Of-Frame marker. It should
787 * not be here, of course, but if your formats don't match
788 * you might exceed the frame. We must match the marker to
789 * each byte of multi-byte data element if it is multi-byte.
791 #if 1
792 if ((ibmcam->camera_model == IBMCAM_MODEL_1) && (scratch_left(data) >= (4+2))) {
793 unsigned char *dp;
794 int j;
796 for (j=0, dp=data; j < 2; j++, dp++) {
797 if ((dp[0] == 0x00) && (dp[1] == 0xFF) &&
798 (dp[2] == 0x00) && (dp[3] == frame->hdr_sig)) {
799 ibmcam->has_hdr = 2;
800 frame_done++;
801 break;
805 #endif
807 /* Check for various visual debugging hints (colorized pixels) */
808 if ((flags & FLAGS_DISPLAY_HINTS) && (ibmcam->has_hdr)) {
810 * This is bad and should not happen. This means that
811 * we somehow overshoot the line and encountered new
812 * frame! Obviously our camera/V4L frame size is out
813 * of whack. This cyan dot will help you to figure
814 * out where exactly the new frame arrived.
816 if (ibmcam->has_hdr == 1) {
817 bv = 0; /* Yellow marker */
818 gv = 0xFF;
819 rv = 0xFF;
820 } else {
821 bv = 0xFF; /* Cyan marker */
822 gv = 0xFF;
823 rv = 0;
825 ibmcam->has_hdr = 0;
826 goto make_pixel;
829 if (mono_plane || frame->order_yc)
830 y = data[0];
831 else
832 y = data[1];
834 if (flags & FLAGS_MONOCHROME) /* Use monochrome for debugging */
835 rv = gv = bv = y;
836 else {
837 int off_0, off_2;
839 off_0 = (i >> 1) << 2;
840 off_2 = off_0 + 2;
842 if (frame->order_yc) {
843 off_0++;
844 off_2++;
846 if (!frame->order_uv) {
847 off_0 += 2;
848 off_2 -= 2;
850 u = chromaLine[off_0] + hue_corr;
851 v = chromaLine[off_2] + hue2_corr;
853 /* Apply color correction */
854 if (color_corr != 0) {
855 /* Magnify up to 2 times, reduce down to zero saturation */
856 u = 128 + ((ccm + color_corr) * (u - 128)) / ccm;
857 v = 128 + ((ccm + color_corr) * (v - 128)) / ccm;
859 YUV_TO_RGB_BY_THE_BOOK(y, u, v, rv, gv, bv);
862 make_pixel:
864 * The purpose of creating the pixel here, in one,
865 * dedicated place is that we may need to make the
866 * pixel wider and taller than it actually is. This
867 * may be used if camera generates small frames for
868 * sake of frame rate (or any other reason.)
870 * The output data consists of B, G, R bytes
871 * (in this order).
873 #if USES_IBMCAM_PUTPIXEL
874 IBMCAM_PUTPIXEL(frame, i, frame->curline, rv, gv, bv);
875 #else
876 *f++ = bv;
877 *f++ = gv;
878 *f++ = rv;
879 #endif
881 * Typically we do not decide within a legitimate frame
882 * that we want to end the frame. However debugging code
883 * may detect marker of new frame within the data. Then
884 * this condition activates. The 'data' pointer is already
885 * pointing at the new marker, so we'd better leave it as is.
887 if (frame_done)
888 break; /* End scanning of lines */
891 * Account for number of bytes that we wrote into output V4L frame.
892 * We do it here, after we are done with the scanline, because we
893 * may fill more than one output scanline if we do vertical
894 * enlargement.
896 frame->curline++;
897 *pcopylen += v4l_linesize;
898 usb_ibmcam_align_scratch(ibmcam, data);
900 if (frame_done || (frame->curline >= frame->frmheight))
901 return scan_NextFrame;
902 else
903 return scan_Continue;
907 * usb_ibmcam_model2_parse_lines()
909 * This procedure deals with a weird RGB format that is produced by IBM
910 * camera model 2 in modes 320x240 and above; 'x' below is 159 or 175,
911 * depending on horizontal size of the picture:
913 * <--- 160 or 176 pairs of RA,RB bytes ----->
914 * *-----------------------------------------* \
915 * | RA0 | RB0 | RA1 | RB1 | ... | RAx | RBx | \
916 * |-----+-----+-----+-----+ ... +-----+-----| *- This is pair of horizontal lines,
917 * | B0 | G0 | B1 | G1 | ... | Bx | Gx | / total 240 or 288 lines (120 or 144
918 * |=====+=====+=====+=====+ ... +=====+=====| / such pairs).
920 * Each group of FOUR bytes (RAi, RBi, Bi, Gi) where i=0..frame_width/2-1
921 * defines ONE pixel. Therefore this format yields 176x144 "decoded"
922 * resolution at best. I do not know why camera sends such format - the
923 * previous model just used I420 and everyone was happy.
925 * I do not know what is the difference between RAi and RBi bytes. Both
926 * seemingly represent R component, but slightly vary in value (so that
927 * the picture looks a bit colored if one or another is used). I use
928 * them both as R component in attempt to at least partially recover the
929 * lost resolution.
931 static scan_state_t usb_ibmcam_model2_parse_lines(struct usb_ibmcam *ibmcam, long *pcopylen)
933 struct ibmcam_frame *frame;
934 unsigned char *data, *f, *la, *lb;
935 unsigned int len;
936 const int v4l_linesize = imgwidth * V4L_BYTES_PER_PIXEL; /* V4L line offset */
937 int i, j, frame_done=0, color_corr;
939 color_corr = (ibmcam->vpic.colour) >> 8; /* 0..+255 */
941 data = ibmcam->scratch;
942 frame = &ibmcam->frame[ibmcam->curframe];
944 /* Here we deal with pairs of horizontal lines */
946 len = frame->frmwidth * 2; /* 2 lines */
947 /*printk(KERN_DEBUG "len=%d. left=%d.\n",len,scratch_left(data));*/
949 /* Make sure there's enough data for the entire line */
950 if (scratch_left(data) < (len+32)) {
951 /*printk(KERN_DEBUG "out of data, need %u.\n", len);*/
952 return scan_Out;
956 * Make sure that our writing into output buffer
957 * will not exceed the buffer. Mind that we may write
958 * not into current output scanline but in several after
959 * it as well (if we enlarge image vertically.)
961 if ((frame->curline + 1) >= V4L_FRAME_HEIGHT)
962 return scan_NextFrame;
964 if ((frame->curline & 1) == 0) {
965 la = data;
966 lb = data + frame->frmwidth;
967 } else {
968 la = data + frame->frmwidth;
969 lb = data;
973 * Now we are sure that entire line (representing all 'frame->frmwidth'
974 * pixels from the camera) is available in the scratch buffer. We
975 * start copying the line left-aligned to the V4L buffer (which
976 * might be larger - not smaller, hopefully). If the camera
977 * line is shorter then we should pad the V4L buffer with something
978 * (black in this case) to complete the line.
980 f = frame->data + (v4l_linesize * frame->curline);
982 /* Fill the 2-line strip */
983 for (i = 0; i < frame->frmwidth; i++) {
984 int y, rv, gv, bv; /* RGB components */
986 j = i & (~1);
988 /* Check for various visual debugging hints (colorized pixels) */
989 if ((flags & FLAGS_DISPLAY_HINTS) && (ibmcam->has_hdr)) {
990 if (ibmcam->has_hdr == 1) {
991 bv = 0; /* Yellow marker */
992 gv = 0xFF;
993 rv = 0xFF;
994 } else {
995 bv = 0xFF; /* Cyan marker */
996 gv = 0xFF;
997 rv = 0;
999 ibmcam->has_hdr = 0;
1000 goto make_pixel;
1004 * Here I use RA and RB components, one per physical pixel.
1005 * This causes fine vertical grid on the picture but may improve
1006 * horizontal resolution. If you prefer replicating, use this:
1007 * rv = la[j + 0]; ... or ... rv = la[j + 1];
1008 * then the pixel will be replicated.
1010 rv = la[i];
1011 gv = lb[j + 1];
1012 bv = lb[j + 0];
1014 y = (rv + gv + bv) / 3; /* Brightness (badly calculated) */
1016 if (flags & FLAGS_MONOCHROME) /* Use monochrome for debugging */
1017 rv = gv = bv = y;
1018 else if (color_corr != 128) {
1020 /* Calculate difference between color and brightness */
1021 rv -= y;
1022 gv -= y;
1023 bv -= y;
1025 /* Scale differences */
1026 rv = (rv * color_corr) / 128;
1027 gv = (gv * color_corr) / 128;
1028 bv = (bv * color_corr) / 128;
1030 /* Reapply brightness */
1031 rv += y;
1032 gv += y;
1033 bv += y;
1035 /* Watch for overflows */
1036 RESTRICT_TO_RANGE(rv, 0, 255);
1037 RESTRICT_TO_RANGE(gv, 0, 255);
1038 RESTRICT_TO_RANGE(bv, 0, 255);
1041 make_pixel:
1042 IBMCAM_PUTPIXEL(frame, i, frame->curline, rv, gv, bv);
1043 IBMCAM_PUTPIXEL(frame, i, frame->curline+1, rv, gv, bv);
1046 * Account for number of bytes that we wrote into output V4L frame.
1047 * We do it here, after we are done with the scanline, because we
1048 * may fill more than one output scanline if we do vertical
1049 * enlargement.
1051 frame->curline += 2;
1052 *pcopylen += v4l_linesize * 2;
1053 data += frame->frmwidth * 2;
1054 usb_ibmcam_align_scratch(ibmcam, data);
1056 if (frame_done || (frame->curline >= frame->frmheight))
1057 return scan_NextFrame;
1058 else
1059 return scan_Continue;
1063 * ibmcam_parse_data()
1065 * Generic routine to parse the scratch buffer. It employs either
1066 * usb_ibmcam_find_header() or usb_ibmcam_parse_lines() to do most
1067 * of work.
1069 * History:
1070 * 1/21/00 Created.
1072 static void ibmcam_parse_data(struct usb_ibmcam *ibmcam)
1074 struct ibmcam_frame *frame;
1075 unsigned char *data = ibmcam->scratch;
1076 scan_state_t newstate;
1077 long copylen = 0;
1079 /* Grab the current frame and the previous frame */
1080 frame = &ibmcam->frame[ibmcam->curframe];
1082 /* printk(KERN_DEBUG "parsing %u.\n", ibmcam->scratchlen); */
1084 while (1) {
1086 newstate = scan_Out;
1087 if (scratch_left(data)) {
1088 if (frame->scanstate == STATE_SCANNING)
1089 newstate = usb_ibmcam_find_header(ibmcam);
1090 else if (frame->scanstate == STATE_LINES) {
1091 if ((ibmcam->camera_model == IBMCAM_MODEL_2) &&
1092 (videosize >= VIDEOSIZE_352x288)) {
1093 newstate = usb_ibmcam_model2_parse_lines(ibmcam, &copylen);
1095 else {
1096 newstate = usb_ibmcam_parse_lines(ibmcam, &copylen);
1100 if (newstate == scan_Continue)
1101 continue;
1102 else if ((newstate == scan_NextFrame) || (newstate == scan_Out))
1103 break;
1104 else
1105 return; /* scan_EndParse */
1108 if (newstate == scan_NextFrame) {
1109 frame->grabstate = FRAME_DONE;
1110 ibmcam->curframe = -1;
1111 ibmcam->frame_num++;
1113 /* Optionally display statistics on the screen */
1114 if (flags & FLAGS_OVERLAY_STATS)
1115 usb_ibmcam_overlaystats(ibmcam, frame);
1117 /* This will cause the process to request another frame. */
1118 if (waitqueue_active(&frame->wq))
1119 wake_up_interruptible(&frame->wq);
1122 /* Update the frame's uncompressed length. */
1123 frame->scanlength += copylen;
1127 * Make all of the blocks of data contiguous
1129 static int ibmcam_compress_isochronous(struct usb_ibmcam *ibmcam, urb_t *urb)
1131 unsigned char *cdata, *data, *data0;
1132 int i, totlen = 0;
1134 data = data0 = ibmcam->scratch + ibmcam->scratchlen;
1135 for (i = 0; i < urb->number_of_packets; i++) {
1136 int n = urb->iso_frame_desc[i].actual_length;
1137 int st = urb->iso_frame_desc[i].status;
1139 cdata = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
1141 /* Detect and ignore errored packets */
1142 if (st < 0) {
1143 if (debug >= 1) {
1144 printk(KERN_ERR "ibmcam data error: [%d] len=%d, status=%X\n",
1145 i, n, st);
1147 ibmcam->iso_err_count++;
1148 continue;
1151 /* Detect and ignore empty packets */
1152 if (n <= 0) {
1153 ibmcam->iso_skip_count++;
1154 continue;
1158 * If camera continues to feed us with data but there is no
1159 * consumption (if, for example, V4L client fell asleep) we
1160 * may overflow the buffer. We have to move old data over to
1161 * free room for new data. This is bad for old data. If we
1162 * just drop new data then it's bad for new data... choose
1163 * your favorite evil here.
1165 if ((ibmcam->scratchlen + n) > scratchbufsize) {
1166 #if 0
1167 ibmcam->scratch_ovf_count++;
1168 if (debug >= 3)
1169 printk(KERN_ERR "ibmcam: scratch buf overflow! "
1170 "scr_len: %d, n: %d\n", ibmcam->scratchlen, n );
1171 return totlen;
1172 #else
1173 int mv;
1175 ibmcam->scratch_ovf_count++;
1176 if (debug >= 3) {
1177 printk(KERN_ERR "ibmcam: scratch buf overflow! "
1178 "scr_len: %d, n: %d\n", ibmcam->scratchlen, n );
1180 mv = (ibmcam->scratchlen + n) - scratchbufsize;
1181 if (ibmcam->scratchlen >= mv) {
1182 int newslen = ibmcam->scratchlen - mv;
1183 memmove(ibmcam->scratch, ibmcam->scratch + mv, newslen);
1184 ibmcam->scratchlen = newslen;
1185 data = data0 = ibmcam->scratch + ibmcam->scratchlen;
1186 } else {
1187 printk(KERN_ERR "ibmcam: scratch buf too small\n");
1188 return totlen;
1190 #endif
1193 /* Now we know that there is enough room in scratch buffer */
1194 memmove(data, cdata, n);
1195 data += n;
1196 totlen += n;
1197 ibmcam->scratchlen += n;
1199 #if 0
1200 if (totlen > 0) {
1201 static int foo=0;
1202 if (foo < 1) {
1203 printk(KERN_DEBUG "+%d.\n", totlen);
1204 ibmcam_hexdump(data0, (totlen > 64) ? 64:totlen);
1205 ++foo;
1208 #endif
1209 return totlen;
1212 static void ibmcam_isoc_irq(struct urb *urb)
1214 int len;
1215 struct usb_ibmcam *ibmcam = urb->context;
1216 struct ibmcam_sbuf *sbuf;
1217 int i;
1219 /* We don't want to do anything if we are about to be removed! */
1220 if (!IBMCAM_IS_OPERATIONAL(ibmcam))
1221 return;
1223 #if 0
1224 if (urb->actual_length > 0) {
1225 printk(KERN_DEBUG "ibmcam_isoc_irq: %p status %d, "
1226 " errcount = %d, length = %d\n", urb, urb->status,
1227 urb->error_count, urb->actual_length);
1228 } else {
1229 static int c = 0;
1230 if (c++ % 100 == 0)
1231 printk(KERN_DEBUG "ibmcam_isoc_irq: no data\n");
1233 #endif
1235 if (!ibmcam->streaming) {
1236 if (debug >= 1)
1237 printk(KERN_DEBUG "ibmcam: oops, not streaming, but interrupt\n");
1238 return;
1241 sbuf = &ibmcam->sbuf[ibmcam->cursbuf];
1243 /* Copy the data received into our scratch buffer */
1244 len = ibmcam_compress_isochronous(ibmcam, urb);
1246 ibmcam->urb_count++;
1247 ibmcam->urb_length = len;
1248 ibmcam->data_count += len;
1250 #if 0 /* This code prints few initial bytes of ISO data: used to decode markers */
1251 if (ibmcam->urb_count % 64 == 1) {
1252 if (ibmcam->urb_count == 1) {
1253 ibmcam_hexdump(ibmcam->scratch,
1254 (ibmcam->scratchlen > 32) ? 32 : ibmcam->scratchlen);
1257 #endif
1259 /* If we collected enough data let's parse! */
1260 if (ibmcam->scratchlen) {
1261 /* If we don't have a frame we're current working on, complain */
1262 if (ibmcam->curframe >= 0)
1263 ibmcam_parse_data(ibmcam);
1264 else {
1265 if (debug >= 1)
1266 printk(KERN_DEBUG "ibmcam: received data, but no frame available\n");
1270 for (i = 0; i < FRAMES_PER_DESC; i++) {
1271 sbuf->urb->iso_frame_desc[i].status = 0;
1272 sbuf->urb->iso_frame_desc[i].actual_length = 0;
1275 /* Move to the next sbuf */
1276 ibmcam->cursbuf = (ibmcam->cursbuf + 1) % IBMCAM_NUMSBUF;
1278 return;
1282 * usb_ibmcam_veio()
1284 * History:
1285 * 1/27/00 Added check for dev == NULL; this happens if camera is unplugged.
1287 static int usb_ibmcam_veio(
1288 struct usb_ibmcam *ibmcam,
1289 unsigned char req,
1290 unsigned short value,
1291 unsigned short index)
1293 static const char proc[] = "usb_ibmcam_veio";
1294 unsigned char cp[8] /* = { 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef } */;
1295 int i;
1297 if (!IBMCAM_IS_OPERATIONAL(ibmcam))
1298 return 0;
1300 if (req == 1) {
1301 i = usb_control_msg(
1302 ibmcam->dev,
1303 usb_rcvctrlpipe(ibmcam->dev, 0),
1304 req,
1305 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT,
1306 value,
1307 index,
1309 sizeof(cp),
1310 HZ);
1311 #if 0
1312 printk(KERN_DEBUG "USB => %02x%02x%02x%02x%02x%02x%02x%02x "
1313 "(req=$%02x val=$%04x ind=$%04x)\n",
1314 cp[0],cp[1],cp[2],cp[3],cp[4],cp[5],cp[6],cp[7],
1315 req, value, index);
1316 #endif
1317 } else {
1318 i = usb_control_msg(
1319 ibmcam->dev,
1320 usb_sndctrlpipe(ibmcam->dev, 0),
1321 req,
1322 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT,
1323 value,
1324 index,
1325 NULL,
1327 HZ);
1329 if (i < 0) {
1330 printk(KERN_ERR "%s: ERROR=%d. Camera stopped - "
1331 "reconnect or reload driver.\n", proc, i);
1332 ibmcam->last_error = i;
1334 return i;
1338 * usb_ibmcam_calculate_fps()
1340 * This procedure roughly calculates the real frame rate based
1341 * on FPS code (framerate=NNN option). Actual FPS differs
1342 * slightly depending on lighting conditions, so that actual frame
1343 * rate is determined by the camera. Since I don't know how to ask
1344 * the camera what FPS is now I have to use the FPS code instead.
1346 * The FPS code is in range [0..6], 0 is slowest, 6 is fastest.
1347 * Corresponding real FPS should be in range [3..30] frames per second.
1348 * The conversion formula is obvious:
1350 * real_fps = 3 + (fps_code * 4.5)
1352 * History:
1353 * 1/18/00 Created.
1355 static int usb_ibmcam_calculate_fps(void)
1357 return 3 + framerate*4 + framerate/2;
1361 * usb_ibmcam_send_FF_04_02()
1363 * This procedure sends magic 3-command prefix to the camera.
1364 * The purpose of this prefix is not known.
1366 * History:
1367 * 1/2/00 Created.
1369 static void usb_ibmcam_send_FF_04_02(struct usb_ibmcam *ibmcam)
1371 usb_ibmcam_veio(ibmcam, 0, 0x00FF, 0x0127);
1372 usb_ibmcam_veio(ibmcam, 0, 0x0004, 0x0124);
1373 usb_ibmcam_veio(ibmcam, 0, 0x0002, 0x0124);
1376 static void usb_ibmcam_send_00_04_06(struct usb_ibmcam *ibmcam)
1378 usb_ibmcam_veio(ibmcam, 0, 0x0000, 0x0127);
1379 usb_ibmcam_veio(ibmcam, 0, 0x0004, 0x0124);
1380 usb_ibmcam_veio(ibmcam, 0, 0x0006, 0x0124);
1383 static void usb_ibmcam_send_x_00(struct usb_ibmcam *ibmcam, unsigned short x)
1385 usb_ibmcam_veio(ibmcam, 0, x, 0x0127);
1386 usb_ibmcam_veio(ibmcam, 0, 0x0000, 0x0124);
1389 static void usb_ibmcam_send_x_00_05(struct usb_ibmcam *ibmcam, unsigned short x)
1391 usb_ibmcam_send_x_00(ibmcam, x);
1392 usb_ibmcam_veio(ibmcam, 0, 0x0005, 0x0124);
1395 static void usb_ibmcam_send_x_00_05_02(struct usb_ibmcam *ibmcam, unsigned short x)
1397 usb_ibmcam_veio(ibmcam, 0, x, 0x0127);
1398 usb_ibmcam_veio(ibmcam, 0, 0x0000, 0x0124);
1399 usb_ibmcam_veio(ibmcam, 0, 0x0005, 0x0124);
1400 usb_ibmcam_veio(ibmcam, 0, 0x0002, 0x0124);
1403 static void usb_ibmcam_send_x_01_00_05(struct usb_ibmcam *ibmcam, unsigned short x)
1405 usb_ibmcam_veio(ibmcam, 0, x, 0x0127);
1406 usb_ibmcam_veio(ibmcam, 0, 0x0001, 0x0124);
1407 usb_ibmcam_veio(ibmcam, 0, 0x0000, 0x0124);
1408 usb_ibmcam_veio(ibmcam, 0, 0x0005, 0x0124);
1411 static void usb_ibmcam_send_x_00_05_02_01(struct usb_ibmcam *ibmcam, unsigned short x)
1413 usb_ibmcam_veio(ibmcam, 0, x, 0x0127);
1414 usb_ibmcam_veio(ibmcam, 0, 0x0000, 0x0124);
1415 usb_ibmcam_veio(ibmcam, 0, 0x0005, 0x0124);
1416 usb_ibmcam_veio(ibmcam, 0, 0x0002, 0x0124);
1417 usb_ibmcam_veio(ibmcam, 0, 0x0001, 0x0124);
1420 static void usb_ibmcam_send_x_00_05_02_08_01(struct usb_ibmcam *ibmcam, unsigned short x)
1422 usb_ibmcam_veio(ibmcam, 0, x, 0x0127);
1423 usb_ibmcam_veio(ibmcam, 0, 0x0000, 0x0124);
1424 usb_ibmcam_veio(ibmcam, 0, 0x0005, 0x0124);
1425 usb_ibmcam_veio(ibmcam, 0, 0x0002, 0x0124);
1426 usb_ibmcam_veio(ibmcam, 0, 0x0008, 0x0124);
1427 usb_ibmcam_veio(ibmcam, 0, 0x0001, 0x0124);
1430 static void usb_ibmcam_Packet_Format1(struct usb_ibmcam *ibmcam, unsigned char fkey, unsigned char val)
1432 usb_ibmcam_send_x_01_00_05 (ibmcam, unknown_88);
1433 usb_ibmcam_send_x_00_05 (ibmcam, fkey);
1434 usb_ibmcam_send_x_00_05_02_08_01(ibmcam, val);
1435 usb_ibmcam_send_x_00_05 (ibmcam, unknown_88);
1436 usb_ibmcam_send_x_00_05_02_01 (ibmcam, fkey);
1437 usb_ibmcam_send_x_00_05 (ibmcam, unknown_89);
1438 usb_ibmcam_send_x_00 (ibmcam, fkey);
1439 usb_ibmcam_send_00_04_06 (ibmcam);
1440 usb_ibmcam_veio (ibmcam, 1, 0x0000, 0x0126);
1441 usb_ibmcam_send_FF_04_02 (ibmcam);
1444 static void usb_ibmcam_PacketFormat2(struct usb_ibmcam *ibmcam, unsigned char fkey, unsigned char val)
1446 usb_ibmcam_send_x_01_00_05 (ibmcam, unknown_88);
1447 usb_ibmcam_send_x_00_05 (ibmcam, fkey);
1448 usb_ibmcam_send_x_00_05_02 (ibmcam, val);
1451 static void usb_ibmcam_model2_Packet2(struct usb_ibmcam *ibmcam)
1453 usb_ibmcam_veio(ibmcam, 0, 0x00ff, 0x012d);
1454 usb_ibmcam_veio(ibmcam, 0, 0xfea3, 0x0124);
1457 static void usb_ibmcam_model2_Packet1(struct usb_ibmcam *ibmcam, unsigned short v1, unsigned short v2)
1459 usb_ibmcam_veio(ibmcam, 0, 0x00aa, 0x012d);
1460 usb_ibmcam_veio(ibmcam, 0, 0x00ff, 0x012e);
1461 usb_ibmcam_veio(ibmcam, 0, v1, 0x012f);
1462 usb_ibmcam_veio(ibmcam, 0, 0x00ff, 0x0130);
1463 usb_ibmcam_veio(ibmcam, 0, 0xc719, 0x0124);
1464 usb_ibmcam_veio(ibmcam, 0, v2, 0x0127);
1466 usb_ibmcam_model2_Packet2(ibmcam);
1470 * usb_ibmcam_adjust_contrast()
1472 * The contrast value changes from 0 (high contrast) to 15 (low contrast).
1473 * This is in reverse to usual order of things (such as TV controls), so
1474 * we reverse it again here.
1476 * TODO: we probably don't need to send the setup 5 times...
1478 * History:
1479 * 1/2/00 Created.
1481 static void usb_ibmcam_adjust_contrast(struct usb_ibmcam *ibmcam)
1483 unsigned char new_contrast = ibmcam->vpic.contrast >> 12;
1484 const int ntries = 5;
1486 if (new_contrast >= 16)
1487 new_contrast = 15;
1488 new_contrast = 15 - new_contrast;
1489 if (new_contrast != ibmcam->vpic_old.contrast) {
1490 ibmcam->vpic_old.contrast = new_contrast;
1491 if (ibmcam->camera_model == IBMCAM_MODEL_1) {
1492 int i;
1493 for (i=0; i < ntries; i++) {
1494 usb_ibmcam_Packet_Format1(ibmcam, contrast_14, new_contrast);
1495 usb_ibmcam_send_FF_04_02(ibmcam);
1497 } else {
1498 /* Camera model 2 does not have this control; implemented in software. */
1504 * usb_ibmcam_change_lighting_conditions()
1506 * Camera model 1:
1507 * We have 3 levels of lighting conditions: 0=Bright, 1=Medium, 2=Low.
1509 * Camera model 2:
1510 * We have 16 levels of lighting, 0 for bright light and up to 15 for
1511 * low light. But values above 5 or so are useless because camera is
1512 * not really capable to produce anything worth viewing at such light.
1513 * This setting may be altered only in certain camera state.
1515 * Low lighting forces slower FPS. Lighting is set as a module parameter.
1517 * History:
1518 * 1/5/00 Created.
1519 * 2/20/00 Added support for Model 2 cameras.
1521 static void usb_ibmcam_change_lighting_conditions(struct usb_ibmcam *ibmcam)
1523 static const char proc[] = "usb_ibmcam_change_lighting_conditions";
1525 if (debug > 0)
1526 printk(KERN_INFO "%s: Set lighting to %hu.\n", proc, lighting);
1528 if (ibmcam->camera_model == IBMCAM_MODEL_1) {
1529 const int ntries = 5;
1530 int i;
1531 for (i=0; i < ntries; i++)
1532 usb_ibmcam_Packet_Format1(ibmcam, light_27, (unsigned short) lighting);
1533 } else {
1535 * This command apparently requires camera to be stopped. My
1536 * experiments showed that it -is- possible to alter the lighting
1537 * conditions setting "on the fly", but why bother? This setting does
1538 * not work reliably in all cases, so I decided simply to leave the
1539 * setting where Xirlink put it - in the camera setup phase. This code
1540 * is commented out because it does not work at -any- moment, so its
1541 * presence makes no sense. You may use it for experiments.
1543 #if 0
1544 usb_ibmcam_veio(ibmcam, 0, 0x0000, 0x010c); /* Stop camera */
1545 usb_ibmcam_model2_Packet1(ibmcam, mod2_sensitivity, lighting);
1546 usb_ibmcam_veio(ibmcam, 0, 0x00c0, 0x010c); /* Start camera */
1547 #endif
1552 * usb_ibmcam_set_sharpness()
1554 * Cameras model 1 have internal smoothing feature. It is controlled by value in
1555 * range [0..6], where 0 is most smooth and 6 is most sharp (raw image, I guess).
1556 * Recommended value is 4. Cameras model 2 do not have this feature at all.
1558 static void usb_ibmcam_set_sharpness(struct usb_ibmcam *ibmcam)
1560 static const char proc[] = "usb_ibmcam_set_sharpness";
1562 if (ibmcam->camera_model == IBMCAM_MODEL_1) {
1563 static const unsigned short sa[] = { 0x11, 0x13, 0x16, 0x18, 0x1a, 0x8, 0x0a };
1564 unsigned short i, sv;
1566 RESTRICT_TO_RANGE(sharpness, SHARPNESS_MIN, SHARPNESS_MAX);
1567 if (debug > 0)
1568 printk(KERN_INFO "%s: Set sharpness to %hu.\n", proc, sharpness);
1570 sv = sa[sharpness - SHARPNESS_MIN];
1571 for (i=0; i < 2; i++) {
1572 usb_ibmcam_send_x_01_00_05 (ibmcam, unknown_88);
1573 usb_ibmcam_send_x_00_05 (ibmcam, sharp_13);
1574 usb_ibmcam_send_x_00_05_02 (ibmcam, sv);
1576 } else {
1577 /* Camera model 2 does not have this control */
1582 * usb_ibmcam_set_brightness()
1584 * This procedure changes brightness of the picture.
1586 static void usb_ibmcam_set_brightness(struct usb_ibmcam *ibmcam)
1588 static const char proc[] = "usb_ibmcam_set_brightness";
1589 static const unsigned short n = 1;
1590 unsigned short i, j, bv[3];
1592 bv[0] = bv[1] = bv[2] = ibmcam->vpic.brightness >> 10;
1593 if (bv[0] == (ibmcam->vpic_old.brightness >> 10))
1594 return;
1595 ibmcam->vpic_old.brightness = ibmcam->vpic.brightness;
1597 if (debug > 0)
1598 printk(KERN_INFO "%s: Set brightness to (%hu,%hu,%hu)\n",
1599 proc, bv[0], bv[1], bv[2]);
1601 if (ibmcam->camera_model == IBMCAM_MODEL_1) {
1602 for (j=0; j < 3; j++)
1603 for (i=0; i < n; i++)
1604 usb_ibmcam_Packet_Format1(ibmcam, bright_3x[j], bv[j]);
1605 } else {
1606 i = ibmcam->vpic.brightness >> 12; /* 0 .. 15 */
1607 j = 0x60 + i * ((0xee - 0x60) / 16); /* 0x60 .. 0xee or so */
1608 usb_ibmcam_model2_Packet1(ibmcam, mod2_brightness, j);
1612 static void usb_ibmcam_model2_set_hue(struct usb_ibmcam *ibmcam)
1614 unsigned short hue = ibmcam->vpic.hue >> 9; /* 0 .. 7F */
1616 usb_ibmcam_model2_Packet1(ibmcam, mod2_color_balance_rg, hue);
1617 /* usb_ibmcam_model2_Packet1(ibmcam, mod2_saturation, sat); */
1621 * usb_ibmcam_adjust_picture()
1623 * This procedure gets called from V4L interface to update picture settings.
1624 * Here we change brightness and contrast.
1626 static void usb_ibmcam_adjust_picture(struct usb_ibmcam *ibmcam)
1628 usb_ibmcam_adjust_contrast(ibmcam);
1629 usb_ibmcam_set_brightness(ibmcam);
1630 if (ibmcam->camera_model == IBMCAM_MODEL_2) {
1631 usb_ibmcam_model2_set_hue(ibmcam);
1635 static int usb_ibmcam_model1_setup(struct usb_ibmcam *ibmcam)
1637 const int ntries = 5;
1638 int i;
1640 usb_ibmcam_veio(ibmcam, 1, 0x00, 0x0128);
1641 usb_ibmcam_veio(ibmcam, 1, 0x00, 0x0100);
1642 usb_ibmcam_veio(ibmcam, 0, 0x01, 0x0100); /* LED On */
1643 usb_ibmcam_veio(ibmcam, 1, 0x00, 0x0100);
1644 usb_ibmcam_veio(ibmcam, 0, 0x81, 0x0100); /* LED Off */
1645 usb_ibmcam_veio(ibmcam, 1, 0x00, 0x0100);
1646 usb_ibmcam_veio(ibmcam, 0, 0x01, 0x0100); /* LED On */
1647 usb_ibmcam_veio(ibmcam, 0, 0x01, 0x0108);
1649 usb_ibmcam_veio(ibmcam, 0, 0x03, 0x0112);
1650 usb_ibmcam_veio(ibmcam, 1, 0x00, 0x0115);
1651 usb_ibmcam_veio(ibmcam, 0, 0x06, 0x0115);
1652 usb_ibmcam_veio(ibmcam, 1, 0x00, 0x0116);
1653 usb_ibmcam_veio(ibmcam, 0, 0x44, 0x0116);
1654 usb_ibmcam_veio(ibmcam, 1, 0x00, 0x0116);
1655 usb_ibmcam_veio(ibmcam, 0, 0x40, 0x0116);
1656 usb_ibmcam_veio(ibmcam, 1, 0x00, 0x0115);
1657 usb_ibmcam_veio(ibmcam, 0, 0x0e, 0x0115);
1658 usb_ibmcam_veio(ibmcam, 0, 0x19, 0x012c);
1660 usb_ibmcam_Packet_Format1(ibmcam, 0x00, 0x1e);
1661 usb_ibmcam_Packet_Format1(ibmcam, 0x39, 0x0d);
1662 usb_ibmcam_Packet_Format1(ibmcam, 0x39, 0x09);
1663 usb_ibmcam_Packet_Format1(ibmcam, 0x3b, 0x00);
1664 usb_ibmcam_Packet_Format1(ibmcam, 0x28, 0x22);
1665 usb_ibmcam_Packet_Format1(ibmcam, light_27, 0);
1666 usb_ibmcam_Packet_Format1(ibmcam, 0x2b, 0x1f);
1667 usb_ibmcam_Packet_Format1(ibmcam, 0x39, 0x08);
1669 for (i=0; i < ntries; i++)
1670 usb_ibmcam_Packet_Format1(ibmcam, 0x2c, 0x00);
1672 for (i=0; i < ntries; i++)
1673 usb_ibmcam_Packet_Format1(ibmcam, 0x30, 0x14);
1675 usb_ibmcam_PacketFormat2(ibmcam, 0x39, 0x02);
1676 usb_ibmcam_PacketFormat2(ibmcam, 0x01, 0xe1);
1677 usb_ibmcam_PacketFormat2(ibmcam, 0x02, 0xcd);
1678 usb_ibmcam_PacketFormat2(ibmcam, 0x03, 0xcd);
1679 usb_ibmcam_PacketFormat2(ibmcam, 0x04, 0xfa);
1680 usb_ibmcam_PacketFormat2(ibmcam, 0x3f, 0xff);
1681 usb_ibmcam_PacketFormat2(ibmcam, 0x39, 0x00);
1683 usb_ibmcam_PacketFormat2(ibmcam, 0x39, 0x02);
1684 usb_ibmcam_PacketFormat2(ibmcam, 0x0a, 0x37);
1685 usb_ibmcam_PacketFormat2(ibmcam, 0x0b, 0xb8);
1686 usb_ibmcam_PacketFormat2(ibmcam, 0x0c, 0xf3);
1687 usb_ibmcam_PacketFormat2(ibmcam, 0x0d, 0xe3);
1688 usb_ibmcam_PacketFormat2(ibmcam, 0x0e, 0x0d);
1689 usb_ibmcam_PacketFormat2(ibmcam, 0x0f, 0xf2);
1690 usb_ibmcam_PacketFormat2(ibmcam, 0x10, 0xd5);
1691 usb_ibmcam_PacketFormat2(ibmcam, 0x11, 0xba);
1692 usb_ibmcam_PacketFormat2(ibmcam, 0x12, 0x53);
1693 usb_ibmcam_PacketFormat2(ibmcam, 0x3f, 0xff);
1694 usb_ibmcam_PacketFormat2(ibmcam, 0x39, 0x00);
1696 usb_ibmcam_PacketFormat2(ibmcam, 0x39, 0x02);
1697 usb_ibmcam_PacketFormat2(ibmcam, 0x16, 0x00);
1698 usb_ibmcam_PacketFormat2(ibmcam, 0x17, 0x28);
1699 usb_ibmcam_PacketFormat2(ibmcam, 0x18, 0x7d);
1700 usb_ibmcam_PacketFormat2(ibmcam, 0x19, 0xbe);
1701 usb_ibmcam_PacketFormat2(ibmcam, 0x3f, 0xff);
1702 usb_ibmcam_PacketFormat2(ibmcam, 0x39, 0x00);
1704 for (i=0; i < ntries; i++)
1705 usb_ibmcam_Packet_Format1(ibmcam, 0x00, 0x18);
1706 for (i=0; i < ntries; i++)
1707 usb_ibmcam_Packet_Format1(ibmcam, 0x13, 0x18);
1708 for (i=0; i < ntries; i++)
1709 usb_ibmcam_Packet_Format1(ibmcam, 0x14, 0x06);
1711 /* This is default brightness */
1712 for (i=0; i < ntries; i++)
1713 usb_ibmcam_Packet_Format1(ibmcam, 0x31, 0x37);
1714 for (i=0; i < ntries; i++)
1715 usb_ibmcam_Packet_Format1(ibmcam, 0x32, 0x46);
1716 for (i=0; i < ntries; i++)
1717 usb_ibmcam_Packet_Format1(ibmcam, 0x33, 0x55);
1719 usb_ibmcam_Packet_Format1(ibmcam, 0x2e, 0x04);
1720 for (i=0; i < ntries; i++)
1721 usb_ibmcam_Packet_Format1(ibmcam, 0x2d, 0x04);
1722 for (i=0; i < ntries; i++)
1723 usb_ibmcam_Packet_Format1(ibmcam, 0x29, 0x80);
1724 usb_ibmcam_Packet_Format1(ibmcam, 0x2c, 0x01);
1725 usb_ibmcam_Packet_Format1(ibmcam, 0x30, 0x17);
1726 usb_ibmcam_Packet_Format1(ibmcam, 0x39, 0x08);
1727 for (i=0; i < ntries; i++)
1728 usb_ibmcam_Packet_Format1(ibmcam, 0x34, 0x00);
1730 usb_ibmcam_veio(ibmcam, 0, 0x00, 0x0101);
1731 usb_ibmcam_veio(ibmcam, 0, 0x00, 0x010a);
1733 switch (videosize) {
1734 case VIDEOSIZE_128x96:
1735 usb_ibmcam_veio(ibmcam, 0, 0x80, 0x0103);
1736 usb_ibmcam_veio(ibmcam, 0, 0x60, 0x0105);
1737 usb_ibmcam_veio(ibmcam, 0, 0x0c, 0x010b);
1738 usb_ibmcam_veio(ibmcam, 0, 0x04, 0x011b); /* Same everywhere */
1739 usb_ibmcam_veio(ibmcam, 0, 0x0b, 0x011d);
1740 usb_ibmcam_veio(ibmcam, 0, 0x00, 0x011e); /* Same everywhere */
1741 usb_ibmcam_veio(ibmcam, 0, 0x00, 0x0129);
1742 break;
1743 case VIDEOSIZE_176x144:
1744 usb_ibmcam_veio(ibmcam, 0, 0xb0, 0x0103);
1745 usb_ibmcam_veio(ibmcam, 0, 0x8f, 0x0105);
1746 usb_ibmcam_veio(ibmcam, 0, 0x06, 0x010b);
1747 usb_ibmcam_veio(ibmcam, 0, 0x04, 0x011b); /* Same everywhere */
1748 usb_ibmcam_veio(ibmcam, 0, 0x0d, 0x011d);
1749 usb_ibmcam_veio(ibmcam, 0, 0x00, 0x011e); /* Same everywhere */
1750 usb_ibmcam_veio(ibmcam, 0, 0x03, 0x0129);
1751 break;
1752 case VIDEOSIZE_352x288:
1753 usb_ibmcam_veio(ibmcam, 0, 0xb0, 0x0103);
1754 usb_ibmcam_veio(ibmcam, 0, 0x90, 0x0105);
1755 usb_ibmcam_veio(ibmcam, 0, 0x02, 0x010b);
1756 usb_ibmcam_veio(ibmcam, 0, 0x04, 0x011b); /* Same everywhere */
1757 usb_ibmcam_veio(ibmcam, 0, 0x05, 0x011d);
1758 usb_ibmcam_veio(ibmcam, 0, 0x00, 0x011e); /* Same everywhere */
1759 usb_ibmcam_veio(ibmcam, 0, 0x00, 0x0129);
1760 break;
1763 usb_ibmcam_veio(ibmcam, 0, 0xff, 0x012b);
1765 /* This is another brightness - don't know why */
1766 for (i=0; i < ntries; i++)
1767 usb_ibmcam_Packet_Format1(ibmcam, 0x31, 0xc3);
1768 for (i=0; i < ntries; i++)
1769 usb_ibmcam_Packet_Format1(ibmcam, 0x32, 0xd2);
1770 for (i=0; i < ntries; i++)
1771 usb_ibmcam_Packet_Format1(ibmcam, 0x33, 0xe1);
1773 /* Default contrast */
1774 for (i=0; i < ntries; i++)
1775 usb_ibmcam_Packet_Format1(ibmcam, contrast_14, 0x0a);
1777 /* Default sharpness */
1778 for (i=0; i < 2; i++)
1779 usb_ibmcam_PacketFormat2(ibmcam, sharp_13, 0x1a); /* Level 4 FIXME */
1781 /* Default lighting conditions */
1782 usb_ibmcam_Packet_Format1(ibmcam, light_27, lighting); /* 0=Bright 2=Low */
1784 /* Assorted init */
1786 switch (videosize) {
1787 case VIDEOSIZE_128x96:
1788 usb_ibmcam_Packet_Format1(ibmcam, 0x2b, 0x1e);
1789 usb_ibmcam_veio(ibmcam, 0, 0xc9, 0x0119); /* Same everywhere */
1790 usb_ibmcam_veio(ibmcam, 0, 0x80, 0x0109); /* Same everywhere */
1791 usb_ibmcam_veio(ibmcam, 0, 0x36, 0x0102);
1792 usb_ibmcam_veio(ibmcam, 0, 0x1a, 0x0104);
1793 usb_ibmcam_veio(ibmcam, 0, 0x04, 0x011a); /* Same everywhere */
1794 usb_ibmcam_veio(ibmcam, 0, 0x2b, 0x011c);
1795 usb_ibmcam_veio(ibmcam, 0, 0x23, 0x012a); /* Same everywhere */
1796 #if 0
1797 usb_ibmcam_veio(ibmcam, 0, 0x00, 0x0106);
1798 usb_ibmcam_veio(ibmcam, 0, 0x38, 0x0107);
1799 #else
1800 usb_ibmcam_veio(ibmcam, 0, 0x02, 0x0106);
1801 usb_ibmcam_veio(ibmcam, 0, 0x2a, 0x0107);
1802 #endif
1803 break;
1804 case VIDEOSIZE_176x144:
1805 usb_ibmcam_Packet_Format1(ibmcam, 0x2b, 0x1e);
1806 usb_ibmcam_veio(ibmcam, 0, 0xc9, 0x0119); /* Same everywhere */
1807 usb_ibmcam_veio(ibmcam, 0, 0x80, 0x0109); /* Same everywhere */
1808 usb_ibmcam_veio(ibmcam, 0, 0x04, 0x0102);
1809 usb_ibmcam_veio(ibmcam, 0, 0x02, 0x0104);
1810 usb_ibmcam_veio(ibmcam, 0, 0x04, 0x011a); /* Same everywhere */
1811 usb_ibmcam_veio(ibmcam, 0, 0x2b, 0x011c);
1812 usb_ibmcam_veio(ibmcam, 0, 0x23, 0x012a); /* Same everywhere */
1813 usb_ibmcam_veio(ibmcam, 0, 0x01, 0x0106);
1814 usb_ibmcam_veio(ibmcam, 0, 0xca, 0x0107);
1815 break;
1816 case VIDEOSIZE_352x288:
1817 usb_ibmcam_Packet_Format1(ibmcam, 0x2b, 0x1f);
1818 usb_ibmcam_veio(ibmcam, 0, 0xc9, 0x0119); /* Same everywhere */
1819 usb_ibmcam_veio(ibmcam, 0, 0x80, 0x0109); /* Same everywhere */
1820 usb_ibmcam_veio(ibmcam, 0, 0x08, 0x0102);
1821 usb_ibmcam_veio(ibmcam, 0, 0x01, 0x0104);
1822 usb_ibmcam_veio(ibmcam, 0, 0x04, 0x011a); /* Same everywhere */
1823 usb_ibmcam_veio(ibmcam, 0, 0x2f, 0x011c);
1824 usb_ibmcam_veio(ibmcam, 0, 0x23, 0x012a); /* Same everywhere */
1825 usb_ibmcam_veio(ibmcam, 0, 0x03, 0x0106);
1826 usb_ibmcam_veio(ibmcam, 0, 0xf6, 0x0107);
1827 break;
1829 return IBMCAM_IS_OPERATIONAL(ibmcam);
1832 static int usb_ibmcam_model2_setup(struct usb_ibmcam *ibmcam)
1834 usb_ibmcam_veio(ibmcam, 0, 0x0000, 0x0100); /* LED on */
1835 usb_ibmcam_veio(ibmcam, 1, 0x0000, 0x0116);
1836 usb_ibmcam_veio(ibmcam, 0, 0x0060, 0x0116);
1837 usb_ibmcam_veio(ibmcam, 0, 0x0002, 0x0112);
1838 usb_ibmcam_veio(ibmcam, 0, 0x00bc, 0x012c);
1839 usb_ibmcam_veio(ibmcam, 0, 0x0008, 0x012b);
1840 usb_ibmcam_veio(ibmcam, 0, 0x0000, 0x0108);
1841 usb_ibmcam_veio(ibmcam, 0, 0x0001, 0x0133);
1842 usb_ibmcam_veio(ibmcam, 0, 0x0001, 0x0102);
1843 switch (videosize) {
1844 case VIDEOSIZE_176x144:
1845 usb_ibmcam_veio(ibmcam, 0, 0x002c, 0x0103); /* All except 320x240 */
1846 usb_ibmcam_veio(ibmcam, 0, 0x0000, 0x0104); /* Same */
1847 usb_ibmcam_veio(ibmcam, 0, 0x0024, 0x0105); /* 176x144, 352x288 */
1848 usb_ibmcam_veio(ibmcam, 0, 0x00b9, 0x010a); /* Unique to this mode */
1849 usb_ibmcam_veio(ibmcam, 0, 0x0038, 0x0119); /* Unique to this mode */
1850 usb_ibmcam_veio(ibmcam, 0, 0x0003, 0x0106); /* Same */
1851 usb_ibmcam_veio(ibmcam, 0, 0x0090, 0x0107); /* Unique to every mode*/
1852 break;
1853 case VIDEOSIZE_320x240:
1854 usb_ibmcam_veio(ibmcam, 0, 0x0028, 0x0103); /* Unique to this mode */
1855 usb_ibmcam_veio(ibmcam, 0, 0x0000, 0x0104); /* Same */
1856 usb_ibmcam_veio(ibmcam, 0, 0x001e, 0x0105); /* 320x240, 352x240 */
1857 usb_ibmcam_veio(ibmcam, 0, 0x0039, 0x010a); /* All except 176x144 */
1858 usb_ibmcam_veio(ibmcam, 0, 0x0070, 0x0119); /* All except 176x144 */
1859 usb_ibmcam_veio(ibmcam, 0, 0x0003, 0x0106); /* Same */
1860 usb_ibmcam_veio(ibmcam, 0, 0x0098, 0x0107); /* Unique to every mode*/
1861 break;
1862 case VIDEOSIZE_352x240:
1863 usb_ibmcam_veio(ibmcam, 0, 0x002c, 0x0103); /* All except 320x240 */
1864 usb_ibmcam_veio(ibmcam, 0, 0x0000, 0x0104); /* Same */
1865 usb_ibmcam_veio(ibmcam, 0, 0x001e, 0x0105); /* 320x240, 352x240 */
1866 usb_ibmcam_veio(ibmcam, 0, 0x0039, 0x010a); /* All except 176x144 */
1867 usb_ibmcam_veio(ibmcam, 0, 0x0070, 0x0119); /* All except 176x144 */
1868 usb_ibmcam_veio(ibmcam, 0, 0x0003, 0x0106); /* Same */
1869 usb_ibmcam_veio(ibmcam, 0, 0x00da, 0x0107); /* Unique to every mode*/
1870 break;
1871 case VIDEOSIZE_352x288:
1872 usb_ibmcam_veio(ibmcam, 0, 0x002c, 0x0103); /* All except 320x240 */
1873 usb_ibmcam_veio(ibmcam, 0, 0x0000, 0x0104); /* Same */
1874 usb_ibmcam_veio(ibmcam, 0, 0x0024, 0x0105); /* 176x144, 352x288 */
1875 usb_ibmcam_veio(ibmcam, 0, 0x0039, 0x010a); /* All except 176x144 */
1876 usb_ibmcam_veio(ibmcam, 0, 0x0070, 0x0119); /* All except 176x144 */
1877 usb_ibmcam_veio(ibmcam, 0, 0x0003, 0x0106); /* Same */
1878 usb_ibmcam_veio(ibmcam, 0, 0x00fe, 0x0107); /* Unique to every mode*/
1879 break;
1881 return IBMCAM_IS_OPERATIONAL(ibmcam);
1885 * usb_ibmcam_model1_setup_after_video_if()
1887 * This code adds finishing touches to the video data interface.
1888 * Here we configure the frame rate and turn on the LED.
1890 static void usb_ibmcam_model1_setup_after_video_if(struct usb_ibmcam *ibmcam)
1892 unsigned short internal_frame_rate;
1894 RESTRICT_TO_RANGE(framerate, FRAMERATE_MIN, FRAMERATE_MAX);
1895 internal_frame_rate = FRAMERATE_MAX - framerate; /* 0=Fast 6=Slow */
1896 usb_ibmcam_veio(ibmcam, 0, 0x01, 0x0100); /* LED On */
1897 usb_ibmcam_veio(ibmcam, 0, internal_frame_rate, 0x0111);
1898 usb_ibmcam_veio(ibmcam, 0, 0x01, 0x0114);
1899 usb_ibmcam_veio(ibmcam, 0, 0xc0, 0x010c);
1902 static void usb_ibmcam_model2_setup_after_video_if(struct usb_ibmcam *ibmcam)
1904 unsigned short setup_model2_rg, setup_model2_rg2, setup_model2_sat, setup_model2_yb;
1906 usb_ibmcam_veio(ibmcam, 0, 0x0000, 0x0100); /* LED on */
1908 switch (videosize) {
1909 case VIDEOSIZE_176x144:
1910 usb_ibmcam_veio(ibmcam, 0, 0x0050, 0x0111);
1911 usb_ibmcam_veio(ibmcam, 0, 0x00d0, 0x0111);
1912 break;
1913 case VIDEOSIZE_320x240:
1914 case VIDEOSIZE_352x240:
1915 case VIDEOSIZE_352x288:
1916 usb_ibmcam_veio(ibmcam, 0, 0x0040, 0x0111);
1917 usb_ibmcam_veio(ibmcam, 0, 0x00c0, 0x0111);
1918 break;
1920 usb_ibmcam_veio(ibmcam, 0, 0x009b, 0x010f);
1921 usb_ibmcam_veio(ibmcam, 0, 0x00bb, 0x010f);
1924 * Hardware settings, may affect CMOS sensor; not user controls!
1925 * -------------------------------------------------------------
1926 * 0x0004: no effect
1927 * 0x0006: hardware effect
1928 * 0x0008: no effect
1929 * 0x000a: stops video stream, probably important h/w setting
1930 * 0x000c: changes color in hardware manner (not user setting)
1931 * 0x0012: changes number of colors (does not affect speed)
1932 * 0x002a: no effect
1933 * 0x002c: hardware setting (related to scan lines)
1934 * 0x002e: stops video stream, probably important h/w setting
1936 usb_ibmcam_model2_Packet1(ibmcam, 0x000a, 0x005c);
1937 usb_ibmcam_model2_Packet1(ibmcam, 0x0004, 0x0000);
1938 usb_ibmcam_model2_Packet1(ibmcam, 0x0006, 0x00fb);
1939 usb_ibmcam_model2_Packet1(ibmcam, 0x0008, 0x0000);
1940 usb_ibmcam_model2_Packet1(ibmcam, 0x000c, 0x0009);
1941 usb_ibmcam_model2_Packet1(ibmcam, 0x0012, 0x000a);
1942 usb_ibmcam_model2_Packet1(ibmcam, 0x002a, 0x0000);
1943 usb_ibmcam_model2_Packet1(ibmcam, 0x002c, 0x0000);
1944 usb_ibmcam_model2_Packet1(ibmcam, 0x002e, 0x0008);
1947 * Function 0x0030 pops up all over the place. Apparently
1948 * it is a hardware control register, with every bit assigned to
1949 * do something.
1951 usb_ibmcam_model2_Packet1(ibmcam, 0x0030, 0x0000);
1954 * Magic control of CMOS sensor. Only lower values like
1955 * 0-3 work, and picture shifts left or right. Don't change.
1957 switch (videosize) {
1958 case VIDEOSIZE_176x144:
1959 usb_ibmcam_model2_Packet1(ibmcam, 0x0014, 0x0002);
1960 usb_ibmcam_model2_Packet1(ibmcam, 0x0016, 0x0002); /* Horizontal shift */
1961 usb_ibmcam_model2_Packet1(ibmcam, 0x0018, 0x004a); /* Another hardware setting */
1962 break;
1963 case VIDEOSIZE_320x240:
1964 usb_ibmcam_model2_Packet1(ibmcam, 0x0014, 0x0009);
1965 usb_ibmcam_model2_Packet1(ibmcam, 0x0016, 0x0005); /* Horizontal shift */
1966 usb_ibmcam_model2_Packet1(ibmcam, 0x0018, 0x0044); /* Another hardware setting */
1967 break;
1968 case VIDEOSIZE_352x240:
1969 /* This mode doesn't work as Windows programs it; changed to work */
1970 usb_ibmcam_model2_Packet1(ibmcam, 0x0014, 0x0009); /* Windows sets this to 8 */
1971 usb_ibmcam_model2_Packet1(ibmcam, 0x0016, 0x0003); /* Horizontal shift */
1972 usb_ibmcam_model2_Packet1(ibmcam, 0x0018, 0x0044); /* Windows sets this to 0x0045 */
1973 break;
1974 case VIDEOSIZE_352x288:
1975 usb_ibmcam_model2_Packet1(ibmcam, 0x0014, 0x0003);
1976 usb_ibmcam_model2_Packet1(ibmcam, 0x0016, 0x0002); /* Horizontal shift */
1977 usb_ibmcam_model2_Packet1(ibmcam, 0x0018, 0x004a); /* Another hardware setting */
1978 break;
1981 usb_ibmcam_model2_Packet1(ibmcam, mod2_brightness, 0x005a);
1984 * We have our own frame rate setting varying from 0 (slowest) to 6 (fastest).
1985 * The camera model 2 allows frame rate in range [0..0x1F] where 0 is also the
1986 * slowest setting. However for all practical reasons high settings make no
1987 * sense because USB is not fast enough to support high FPS. Be aware that
1988 * the picture datastream will be severely disrupted if you ask for
1989 * frame rate faster than allowed for the video size - see below:
1991 * Allowable ranges (obtained experimentally on OHCI, K6-3, 450 MHz):
1992 * -----------------------------------------------------------------
1993 * 176x144: [6..31]
1994 * 320x240: [8..31]
1995 * 352x240: [10..31]
1996 * 352x288: [16..31] I have to raise lower threshold for stability...
1998 * As usual, slower FPS provides better sensitivity.
2001 short hw_fps=31, i_framerate;
2003 RESTRICT_TO_RANGE(framerate, FRAMERATE_MIN, FRAMERATE_MAX);
2004 i_framerate = FRAMERATE_MAX - framerate + FRAMERATE_MIN;
2005 switch (videosize) {
2006 case VIDEOSIZE_176x144:
2007 hw_fps = 6 + i_framerate*4;
2008 break;
2009 case VIDEOSIZE_320x240:
2010 hw_fps = 8 + i_framerate*3;
2011 break;
2012 case VIDEOSIZE_352x240:
2013 hw_fps = 10 + i_framerate*2;
2014 break;
2015 case VIDEOSIZE_352x288:
2016 hw_fps = 28 + i_framerate/2;
2017 break;
2019 if (debug > 0)
2020 printk(KERN_DEBUG "Framerate (hardware): %hd.\n", hw_fps);
2021 RESTRICT_TO_RANGE(hw_fps, 0, 31);
2022 usb_ibmcam_model2_Packet1(ibmcam, mod2_set_framerate, hw_fps);
2026 * This setting does not visibly affect pictures; left it here
2027 * because it was present in Windows USB data stream. This function
2028 * does not allow arbitrary values and apparently is a bit mask, to
2029 * be activated only at appropriate time. Don't change it randomly!
2031 switch (videosize) {
2032 case VIDEOSIZE_176x144:
2033 usb_ibmcam_model2_Packet1(ibmcam, 0x0026, 0x00c2);
2034 break;
2035 case VIDEOSIZE_320x240:
2036 usb_ibmcam_model2_Packet1(ibmcam, 0x0026, 0x0044);
2037 break;
2038 case VIDEOSIZE_352x240:
2039 usb_ibmcam_model2_Packet1(ibmcam, 0x0026, 0x0046);
2040 break;
2041 case VIDEOSIZE_352x288:
2042 usb_ibmcam_model2_Packet1(ibmcam, 0x0026, 0x0048);
2043 break;
2046 usb_ibmcam_model2_Packet1(ibmcam, mod2_sensitivity, lighting);
2048 if (init_model2_rg >= 0) {
2049 RESTRICT_TO_RANGE(init_model2_rg, 0, 255);
2050 setup_model2_rg = init_model2_rg;
2051 } else
2052 setup_model2_rg = 0x0070;
2054 if (init_model2_rg2 >= 0) {
2055 RESTRICT_TO_RANGE(init_model2_rg2, 0, 255);
2056 setup_model2_rg2 = init_model2_rg2;
2057 } else
2058 setup_model2_rg2 = 0x002f;
2060 if (init_model2_sat >= 0) {
2061 RESTRICT_TO_RANGE(init_model2_sat, 0, 255);
2062 setup_model2_sat = init_model2_sat;
2063 } else
2064 setup_model2_sat = 0x0034;
2066 if (init_model2_yb >= 0) {
2067 RESTRICT_TO_RANGE(init_model2_yb, 0, 255);
2068 setup_model2_yb = init_model2_yb;
2069 } else
2070 setup_model2_yb = 0x00a0;
2072 usb_ibmcam_model2_Packet1(ibmcam, mod2_color_balance_rg2, setup_model2_rg2);
2073 usb_ibmcam_model2_Packet1(ibmcam, mod2_saturation, setup_model2_sat);
2074 usb_ibmcam_model2_Packet1(ibmcam, mod2_color_balance_yb, setup_model2_yb);
2075 usb_ibmcam_model2_Packet1(ibmcam, mod2_color_balance_rg, setup_model2_rg);
2077 /* Hardware control command */
2078 usb_ibmcam_model2_Packet1(ibmcam, 0x0030, 0x0004);
2080 usb_ibmcam_veio(ibmcam, 0, 0x00c0, 0x010c); /* Go camera, go! */
2081 usb_clear_halt(ibmcam->dev, ibmcam->video_endp);
2085 * usb_ibmcam_setup_video_stop()
2087 * This code tells camera to stop streaming. The interface remains
2088 * configured and bandwidth - claimed.
2090 static void usb_ibmcam_setup_video_stop(struct usb_ibmcam *ibmcam)
2092 if (ibmcam->camera_model == IBMCAM_MODEL_1) {
2093 usb_ibmcam_veio(ibmcam, 0, 0x00, 0x010c);
2094 usb_ibmcam_veio(ibmcam, 0, 0x00, 0x010c);
2095 usb_ibmcam_veio(ibmcam, 0, 0x01, 0x0114);
2096 usb_ibmcam_veio(ibmcam, 0, 0xc0, 0x010c);
2097 usb_ibmcam_veio(ibmcam, 0, 0x00, 0x010c);
2098 usb_ibmcam_send_FF_04_02(ibmcam);
2099 usb_ibmcam_veio(ibmcam, 1, 0x00, 0x0100);
2100 usb_ibmcam_veio(ibmcam, 0, 0x81, 0x0100); /* LED Off */
2101 } else if (ibmcam->camera_model == IBMCAM_MODEL_2) {
2102 usb_ibmcam_veio(ibmcam, 0, 0x0000, 0x010c); /* Stop the camera */
2104 usb_ibmcam_model2_Packet1(ibmcam, 0x0030, 0x0004);
2106 usb_ibmcam_veio(ibmcam, 0, 0x0080, 0x0100); /* LED Off */
2107 usb_ibmcam_veio(ibmcam, 0, 0x0020, 0x0111);
2108 usb_ibmcam_veio(ibmcam, 0, 0x00a0, 0x0111);
2110 usb_ibmcam_model2_Packet1(ibmcam, 0x0030, 0x0002);
2112 usb_ibmcam_veio(ibmcam, 0, 0x0020, 0x0111);
2113 usb_ibmcam_veio(ibmcam, 0, 0x0000, 0x0112);
2118 * usb_ibmcam_reinit_iso()
2120 * This procedure sends couple of commands to the camera and then
2121 * resets the video pipe. This sequence was observed to reinit the
2122 * camera or, at least, to initiate ISO data stream.
2124 * History:
2125 * 1/2/00 Created.
2127 static void usb_ibmcam_reinit_iso(struct usb_ibmcam *ibmcam, int do_stop)
2129 if (ibmcam->camera_model == IBMCAM_MODEL_1) {
2130 if (do_stop)
2131 usb_ibmcam_setup_video_stop(ibmcam);
2132 usb_ibmcam_veio(ibmcam, 0, 0x0001, 0x0114);
2133 usb_ibmcam_veio(ibmcam, 0, 0x00c0, 0x010c);
2134 usb_clear_halt(ibmcam->dev, ibmcam->video_endp);
2135 usb_ibmcam_model1_setup_after_video_if(ibmcam);
2136 } else if (ibmcam->camera_model == IBMCAM_MODEL_2) {
2137 usb_ibmcam_model2_setup_after_video_if(ibmcam);
2142 * ibmcam_init_isoc()
2144 * History:
2145 * 1/27/00 Used ibmcam->iface, ibmcam->ifaceAltActive instead of hardcoded values.
2146 * Simplified by using for loop, allowed any number of URBs.
2148 static int ibmcam_init_isoc(struct usb_ibmcam *ibmcam)
2150 struct usb_device *dev = ibmcam->dev;
2151 int i, err;
2153 if (!IBMCAM_IS_OPERATIONAL(ibmcam))
2154 return -EFAULT;
2156 ibmcam->compress = 0;
2157 ibmcam->curframe = -1;
2158 ibmcam->cursbuf = 0;
2159 ibmcam->scratchlen = 0;
2161 /* Alternate interface 1 is is the biggest frame size */
2162 i = usb_set_interface(dev, ibmcam->iface, ibmcam->ifaceAltActive);
2163 if (i < 0) {
2164 printk(KERN_ERR "usb_set_interface error\n");
2165 ibmcam->last_error = i;
2166 return -EBUSY;
2168 usb_ibmcam_change_lighting_conditions(ibmcam);
2169 usb_ibmcam_set_sharpness(ibmcam);
2170 usb_ibmcam_reinit_iso(ibmcam, 0);
2172 /* We double buffer the Iso lists */
2174 for (i=0; i < IBMCAM_NUMSBUF; i++) {
2175 int j, k;
2176 urb_t *urb;
2178 urb = usb_alloc_urb(FRAMES_PER_DESC);
2179 if (urb == NULL) {
2180 printk(KERN_ERR "ibmcam_init_isoc: usb_init_isoc() failed.\n");
2181 return -ENOMEM;
2183 ibmcam->sbuf[i].urb = urb;
2184 urb->dev = dev;
2185 urb->context = ibmcam;
2186 urb->pipe = usb_rcvisocpipe(dev, ibmcam->video_endp);
2187 urb->transfer_flags = USB_ISO_ASAP;
2188 urb->transfer_buffer = ibmcam->sbuf[i].data;
2189 urb->complete = ibmcam_isoc_irq;
2190 urb->number_of_packets = FRAMES_PER_DESC;
2191 urb->transfer_buffer_length = ibmcam->iso_packet_len * FRAMES_PER_DESC;
2192 for (j=k=0; j < FRAMES_PER_DESC; j++, k += ibmcam->iso_packet_len) {
2193 urb->iso_frame_desc[j].offset = k;
2194 urb->iso_frame_desc[j].length = ibmcam->iso_packet_len;
2198 /* Link URBs into a ring so that they invoke each other infinitely */
2199 for (i=0; i < IBMCAM_NUMSBUF; i++) {
2200 if ((i+1) < IBMCAM_NUMSBUF)
2201 ibmcam->sbuf[i].urb->next = ibmcam->sbuf[i+1].urb;
2202 else
2203 ibmcam->sbuf[i].urb->next = ibmcam->sbuf[0].urb;
2206 /* Submit all URBs */
2207 for (i=0; i < IBMCAM_NUMSBUF; i++) {
2208 err = usb_submit_urb(ibmcam->sbuf[i].urb);
2209 if (err)
2210 printk(KERN_ERR "ibmcam_init_isoc: usb_run_isoc(%d) ret %d\n",
2211 i, err);
2214 ibmcam->streaming = 1;
2215 /* printk(KERN_DEBUG "streaming=1 ibmcam->video_endp=$%02x\n", ibmcam->video_endp); */
2216 return 0;
2220 * ibmcam_stop_isoc()
2222 * This procedure stops streaming and deallocates URBs. Then it
2223 * activates zero-bandwidth alt. setting of the video interface.
2225 * History:
2226 * 1/22/00 Corrected order of actions to work after surprise removal.
2227 * 1/27/00 Used ibmcam->iface, ibmcam->ifaceAltInactive instead of hardcoded values.
2229 static void ibmcam_stop_isoc(struct usb_ibmcam *ibmcam)
2231 static const char proc[] = "ibmcam_stop_isoc";
2232 int i, j;
2234 if (!ibmcam->streaming || (ibmcam->dev == NULL))
2235 return;
2237 /* Unschedule all of the iso td's */
2238 for (i=0; i < IBMCAM_NUMSBUF; i++) {
2239 j = usb_unlink_urb(ibmcam->sbuf[i].urb);
2240 if (j < 0)
2241 printk(KERN_ERR "%s: usb_unlink_urb() error %d.\n", proc, j);
2243 /* printk(KERN_DEBUG "streaming=0\n"); */
2244 ibmcam->streaming = 0;
2246 /* Delete them all */
2247 for (i=0; i < IBMCAM_NUMSBUF; i++)
2248 usb_free_urb(ibmcam->sbuf[i].urb);
2250 if (!ibmcam->remove_pending) {
2251 usb_ibmcam_setup_video_stop(ibmcam);
2253 /* Set packet size to 0 */
2254 j = usb_set_interface(ibmcam->dev, ibmcam->iface, ibmcam->ifaceAltInactive);
2255 if (j < 0) {
2256 printk(KERN_ERR "%s: usb_set_interface() error %d.\n", proc, j);
2257 ibmcam->last_error = j;
2263 * ibmcam_new_frame()
2265 * History:
2266 * 29-Mar-00 Added copying of previous frame into the current one.
2268 static int ibmcam_new_frame(struct usb_ibmcam *ibmcam, int framenum)
2270 struct ibmcam_frame *frame;
2271 int n, width, height;
2273 /* If we're not grabbing a frame right now and the other frame is */
2274 /* ready to be grabbed into, then use it instead */
2275 if (ibmcam->curframe != -1)
2276 return 0;
2278 n = (framenum - 1 + IBMCAM_NUMFRAMES) % IBMCAM_NUMFRAMES;
2279 if (ibmcam->frame[n].grabstate == FRAME_READY)
2280 framenum = n;
2282 frame = &ibmcam->frame[framenum];
2284 frame->grabstate = FRAME_GRABBING;
2285 frame->scanstate = STATE_SCANNING;
2286 frame->scanlength = 0; /* Accumulated in ibmcam_parse_data() */
2287 ibmcam->curframe = framenum;
2290 * Normally we would want to copy previous frame into the current one
2291 * before we even start filling it with data; this allows us to stop
2292 * filling at any moment; top portion of the frame will be new and
2293 * bottom portion will stay as it was in previous frame. If we don't
2294 * do that then missing chunks of video stream will result in flickering
2295 * portions of old data whatever it was before.
2297 * If we choose not to copy previous frame (to, for example, save few
2298 * bus cycles - the frame can be pretty large!) then we have an option
2299 * to clear the frame before using. If we experience losses in this
2300 * mode then missing picture will be black (no flickering).
2302 * Finally, if user chooses not to clean the current frame before
2303 * filling it with data then the old data will be visible if we fail
2304 * to refill entire frame with new data.
2306 if (!(flags & FLAGS_SEPARATE_FRAMES)) {
2307 /* This copies previous frame into this one to mask losses */
2308 memmove(frame->data, ibmcam->frame[1-framenum].data, MAX_FRAME_SIZE);
2309 } else {
2310 if (flags & FLAGS_CLEAN_FRAMES) {
2311 /* This provides a "clean" frame but slows things down */
2312 memset(frame->data, 0, MAX_FRAME_SIZE);
2315 switch (videosize) {
2316 case VIDEOSIZE_128x96:
2317 frame->frmwidth = 128;
2318 frame->frmheight = 96;
2319 frame->order_uv = 1; /* U Y V Y ... */
2320 frame->hdr_sig = 0x06; /* 00 FF 00 06 */
2321 break;
2322 case VIDEOSIZE_176x144:
2323 frame->frmwidth = 176;
2324 frame->frmheight = 144;
2325 frame->order_uv = 1; /* U Y V Y ... */
2326 frame->hdr_sig = 0x0E; /* 00 FF 00 0E */
2327 break;
2328 case VIDEOSIZE_320x240: /* For model 2 only */
2329 frame->frmwidth = 320;
2330 frame->frmheight = 240;
2331 break;
2332 case VIDEOSIZE_352x240: /* For model 2 only */
2333 frame->frmwidth = 352;
2334 frame->frmheight = 240;
2335 break;
2336 case VIDEOSIZE_352x288:
2337 frame->frmwidth = 352;
2338 frame->frmheight = 288;
2339 frame->order_uv = 0; /* V Y U Y ... */
2340 frame->hdr_sig = 0x00; /* 00 FF 00 00 */
2341 break;
2343 frame->order_yc = (ibmcam->camera_model == IBMCAM_MODEL_2);
2345 width = frame->width;
2346 RESTRICT_TO_RANGE(width, min_imgwidth, imgwidth);
2347 width &= ~7; /* Multiple of 8 */
2349 height = frame->height;
2350 RESTRICT_TO_RANGE(height, min_imgheight, imgheight);
2351 height &= ~3; /* Multiple of 4 */
2353 return 0;
2357 * ibmcam_open()
2359 * This is part of Video 4 Linux API. The driver can be opened by one
2360 * client only (checks internal counter 'ibmcam->user'). The procedure
2361 * then allocates buffers needed for video processing.
2363 * History:
2364 * 1/22/00 Rewrote, moved scratch buffer allocation here. Now the
2365 * camera is also initialized here (once per connect), at
2366 * expense of V4L client (it waits on open() call).
2367 * 1/27/00 Used IBMCAM_NUMSBUF as number of URB buffers.
2368 * 5/24/00 Corrected to prevent race condition (MOD_xxx_USE_COUNT).
2370 static int ibmcam_open(struct video_device *dev, int flags)
2372 struct usb_ibmcam *ibmcam = (struct usb_ibmcam *)dev;
2373 const int sb_size = FRAMES_PER_DESC * ibmcam->iso_packet_len;
2374 int i, err = 0;
2376 MOD_INC_USE_COUNT;
2377 down(&ibmcam->lock);
2379 if (ibmcam->user)
2380 err = -EBUSY;
2381 else {
2382 /* Clean pointers so we know if we allocated something */
2383 for (i=0; i < IBMCAM_NUMSBUF; i++)
2384 ibmcam->sbuf[i].data = NULL;
2386 /* Allocate memory for the frame buffers */
2387 ibmcam->fbuf_size = IBMCAM_NUMFRAMES * MAX_FRAME_SIZE;
2388 ibmcam->fbuf = rvmalloc(ibmcam->fbuf_size);
2389 ibmcam->scratch = kmalloc(scratchbufsize, GFP_KERNEL);
2390 ibmcam->scratchlen = 0;
2391 if ((ibmcam->fbuf == NULL) || (ibmcam->scratch == NULL))
2392 err = -ENOMEM;
2393 else {
2394 /* Allocate all buffers */
2395 for (i=0; i < IBMCAM_NUMFRAMES; i++) {
2396 ibmcam->frame[i].grabstate = FRAME_UNUSED;
2397 ibmcam->frame[i].data = ibmcam->fbuf + i*MAX_FRAME_SIZE;
2399 * Set default sizes in case IOCTL (VIDIOCMCAPTURE)
2400 * is not used (using read() instead).
2402 ibmcam->frame[i].width = imgwidth;
2403 ibmcam->frame[i].height = imgheight;
2404 ibmcam->frame[i].bytes_read = 0;
2406 for (i=0; i < IBMCAM_NUMSBUF; i++) {
2407 ibmcam->sbuf[i].data = kmalloc(sb_size, GFP_KERNEL);
2408 if (ibmcam->sbuf[i].data == NULL) {
2409 err = -ENOMEM;
2410 break;
2414 if (err) {
2415 /* Have to free all that memory */
2416 if (ibmcam->fbuf != NULL) {
2417 rvfree(ibmcam->fbuf, ibmcam->fbuf_size);
2418 ibmcam->fbuf = NULL;
2420 if (ibmcam->scratch != NULL) {
2421 kfree(ibmcam->scratch);
2422 ibmcam->scratch = NULL;
2424 for (i=0; i < IBMCAM_NUMSBUF; i++) {
2425 if (ibmcam->sbuf[i].data != NULL) {
2426 kfree (ibmcam->sbuf[i].data);
2427 ibmcam->sbuf[i].data = NULL;
2433 /* If so far no errors then we shall start the camera */
2434 if (!err) {
2435 err = ibmcam_init_isoc(ibmcam);
2436 if (!err) {
2437 /* Send init sequence only once, it's large! */
2438 if (!ibmcam->initialized) {
2439 int setup_ok = 0;
2440 if (ibmcam->camera_model == IBMCAM_MODEL_1)
2441 setup_ok = usb_ibmcam_model1_setup(ibmcam);
2442 else if (ibmcam->camera_model == IBMCAM_MODEL_2)
2443 setup_ok = usb_ibmcam_model2_setup(ibmcam);
2444 if (setup_ok)
2445 ibmcam->initialized = 1;
2446 else
2447 err = -EBUSY;
2449 if (!err)
2450 ibmcam->user++;
2453 up(&ibmcam->lock);
2454 if (err)
2455 MOD_DEC_USE_COUNT;
2456 return err;
2460 * ibmcam_close()
2462 * This is part of Video 4 Linux API. The procedure
2463 * stops streaming and deallocates all buffers that were earlier
2464 * allocated in ibmcam_open().
2466 * History:
2467 * 1/22/00 Moved scratch buffer deallocation here.
2468 * 1/27/00 Used IBMCAM_NUMSBUF as number of URB buffers.
2469 * 5/24/00 Moved MOD_DEC_USE_COUNT outside of code that can sleep.
2471 static void ibmcam_close(struct video_device *dev)
2473 struct usb_ibmcam *ibmcam = (struct usb_ibmcam *)dev;
2474 int i;
2476 down(&ibmcam->lock);
2478 ibmcam_stop_isoc(ibmcam);
2480 rvfree(ibmcam->fbuf, ibmcam->fbuf_size);
2481 kfree(ibmcam->scratch);
2482 for (i=0; i < IBMCAM_NUMSBUF; i++)
2483 kfree(ibmcam->sbuf[i].data);
2485 ibmcam->user--;
2487 if (ibmcam->remove_pending) {
2488 printk(KERN_INFO "ibmcam_close: Final disconnect.\n");
2489 usb_ibmcam_release(ibmcam);
2491 up(&ibmcam->lock);
2492 MOD_DEC_USE_COUNT;
2495 static int ibmcam_init_done(struct video_device *dev)
2497 return 0;
2500 static long ibmcam_write(struct video_device *dev, const char *buf, unsigned long count, int noblock)
2502 return -EINVAL;
2506 * ibmcam_ioctl()
2508 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
2510 * History:
2511 * 1/22/00 Corrected VIDIOCSPICT to reject unsupported settings.
2513 static int ibmcam_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
2515 struct usb_ibmcam *ibmcam = (struct usb_ibmcam *)dev;
2517 if (!IBMCAM_IS_OPERATIONAL(ibmcam))
2518 return -EFAULT;
2520 switch (cmd) {
2521 case VIDIOCGCAP:
2523 if (copy_to_user(arg, &ibmcam->vcap, sizeof(ibmcam->vcap)))
2524 return -EFAULT;
2525 return 0;
2527 case VIDIOCGCHAN:
2529 if (copy_to_user(arg, &ibmcam->vchan, sizeof(ibmcam->vchan)))
2530 return -EFAULT;
2531 return 0;
2533 case VIDIOCSCHAN:
2535 int v;
2537 if (copy_from_user(&v, arg, sizeof(v)))
2538 return -EFAULT;
2539 if ((v < 0) || (v >= 3)) /* 3 grades of lighting conditions */
2540 return -EINVAL;
2541 if (v != ibmcam->vchan.channel) {
2542 ibmcam->vchan.channel = v;
2543 usb_ibmcam_change_lighting_conditions(ibmcam);
2545 return 0;
2547 case VIDIOCGPICT:
2549 if (copy_to_user(arg, &ibmcam->vpic, sizeof(ibmcam->vpic)))
2550 return -EFAULT;
2551 return 0;
2553 case VIDIOCSPICT:
2555 struct video_picture tmp;
2557 * Use temporary 'video_picture' structure to preserve our
2558 * own settings (such as color depth, palette) that we
2559 * aren't allowing everyone (V4L client) to change.
2561 if (copy_from_user(&tmp, arg, sizeof(tmp)))
2562 return -EFAULT;
2563 ibmcam->vpic.brightness = tmp.brightness;
2564 ibmcam->vpic.hue = tmp.hue;
2565 ibmcam->vpic.colour = tmp.colour;
2566 ibmcam->vpic.contrast = tmp.contrast;
2567 usb_ibmcam_adjust_picture(ibmcam);
2568 return 0;
2570 case VIDIOCSWIN:
2572 struct video_window vw;
2574 if (copy_from_user(&vw, arg, sizeof(vw)))
2575 return -EFAULT;
2576 if (vw.flags)
2577 return -EINVAL;
2578 if (vw.clipcount)
2579 return -EINVAL;
2580 if (vw.height != imgheight)
2581 return -EINVAL;
2582 if (vw.width != imgwidth)
2583 return -EINVAL;
2585 ibmcam->compress = 0;
2587 return 0;
2589 case VIDIOCGWIN:
2591 struct video_window vw;
2593 vw.x = 0;
2594 vw.y = 0;
2595 vw.width = imgwidth;
2596 vw.height = imgheight;
2597 vw.chromakey = 0;
2598 vw.flags = usb_ibmcam_calculate_fps();
2600 if (copy_to_user(arg, &vw, sizeof(vw)))
2601 return -EFAULT;
2603 return 0;
2605 case VIDIOCGMBUF:
2607 struct video_mbuf vm;
2609 memset(&vm, 0, sizeof(vm));
2610 vm.size = MAX_FRAME_SIZE * 2;
2611 vm.frames = 2;
2612 vm.offsets[0] = 0;
2613 vm.offsets[1] = MAX_FRAME_SIZE;
2615 if (copy_to_user((void *)arg, (void *)&vm, sizeof(vm)))
2616 return -EFAULT;
2618 return 0;
2620 case VIDIOCMCAPTURE:
2622 struct video_mmap vm;
2624 if (copy_from_user((void *)&vm, (void *)arg, sizeof(vm)))
2625 return -EFAULT;
2627 if (debug >= 1)
2628 printk(KERN_DEBUG "frame: %d, size: %dx%d, format: %d\n",
2629 vm.frame, vm.width, vm.height, vm.format);
2631 if (vm.format != VIDEO_PALETTE_RGB24)
2632 return -EINVAL;
2634 if ((vm.frame != 0) && (vm.frame != 1))
2635 return -EINVAL;
2637 if (ibmcam->frame[vm.frame].grabstate == FRAME_GRABBING)
2638 return -EBUSY;
2640 /* Don't compress if the size changed */
2641 if ((ibmcam->frame[vm.frame].width != vm.width) ||
2642 (ibmcam->frame[vm.frame].height != vm.height))
2643 ibmcam->compress = 0;
2645 ibmcam->frame[vm.frame].width = vm.width;
2646 ibmcam->frame[vm.frame].height = vm.height;
2648 /* Mark it as ready */
2649 ibmcam->frame[vm.frame].grabstate = FRAME_READY;
2651 return ibmcam_new_frame(ibmcam, vm.frame);
2653 case VIDIOCSYNC:
2655 int frame;
2657 if (copy_from_user((void *)&frame, arg, sizeof(int)))
2658 return -EFAULT;
2660 if (debug >= 1)
2661 printk(KERN_DEBUG "ibmcam: syncing to frame %d\n", frame);
2663 switch (ibmcam->frame[frame].grabstate) {
2664 case FRAME_UNUSED:
2665 return -EINVAL;
2666 case FRAME_READY:
2667 case FRAME_GRABBING:
2668 case FRAME_ERROR:
2670 int ntries;
2671 redo:
2672 if (!IBMCAM_IS_OPERATIONAL(ibmcam))
2673 return -EIO;
2674 ntries = 0;
2675 do {
2676 interruptible_sleep_on(&ibmcam->frame[frame].wq);
2677 if (signal_pending(current)) {
2678 if (flags & FLAGS_RETRY_VIDIOCSYNC) {
2679 /* Polling apps will destroy frames with that! */
2680 ibmcam_new_frame(ibmcam, frame);
2681 usb_ibmcam_testpattern(ibmcam, 1, 0);
2682 ibmcam->curframe = -1;
2683 ibmcam->frame_num++;
2685 /* This will request another frame. */
2686 if (waitqueue_active(&ibmcam->frame[frame].wq))
2687 wake_up_interruptible(&ibmcam->frame[frame].wq);
2688 return 0;
2689 } else {
2690 /* Standard answer: not ready yet! */
2691 return -EINTR;
2694 } while (ibmcam->frame[frame].grabstate == FRAME_GRABBING);
2696 if (ibmcam->frame[frame].grabstate == FRAME_ERROR) {
2697 int ret = ibmcam_new_frame(ibmcam, frame);
2698 if (ret < 0)
2699 return ret;
2700 goto redo;
2703 case FRAME_DONE:
2704 ibmcam->frame[frame].grabstate = FRAME_UNUSED;
2705 break;
2708 ibmcam->frame[frame].grabstate = FRAME_UNUSED;
2710 return 0;
2712 case VIDIOCGFBUF:
2714 struct video_buffer vb;
2716 memset(&vb, 0, sizeof(vb));
2717 vb.base = NULL; /* frame buffer not supported, not used */
2719 if (copy_to_user((void *)arg, (void *)&vb, sizeof(vb)))
2720 return -EFAULT;
2722 return 0;
2724 case VIDIOCKEY:
2725 return 0;
2727 case VIDIOCCAPTURE:
2728 return -EINVAL;
2730 case VIDIOCSFBUF:
2732 case VIDIOCGTUNER:
2733 case VIDIOCSTUNER:
2735 case VIDIOCGFREQ:
2736 case VIDIOCSFREQ:
2738 case VIDIOCGAUDIO:
2739 case VIDIOCSAUDIO:
2740 return -EINVAL;
2742 default:
2743 return -ENOIOCTLCMD;
2745 return 0;
2748 static long ibmcam_read(struct video_device *dev, char *buf, unsigned long count, int noblock)
2750 struct usb_ibmcam *ibmcam = (struct usb_ibmcam *)dev;
2751 int frmx = -1;
2752 volatile struct ibmcam_frame *frame;
2754 if (debug >= 1)
2755 printk(KERN_DEBUG "ibmcam_read: %ld bytes, noblock=%d\n", count, noblock);
2757 if (!IBMCAM_IS_OPERATIONAL(ibmcam) || (buf == NULL))
2758 return -EFAULT;
2760 /* See if a frame is completed, then use it. */
2761 if (ibmcam->frame[0].grabstate >= FRAME_DONE) /* _DONE or _ERROR */
2762 frmx = 0;
2763 else if (ibmcam->frame[1].grabstate >= FRAME_DONE)/* _DONE or _ERROR */
2764 frmx = 1;
2766 if (noblock && (frmx == -1))
2767 return -EAGAIN;
2769 /* If no FRAME_DONE, look for a FRAME_GRABBING state. */
2770 /* See if a frame is in process (grabbing), then use it. */
2771 if (frmx == -1) {
2772 if (ibmcam->frame[0].grabstate == FRAME_GRABBING)
2773 frmx = 0;
2774 else if (ibmcam->frame[1].grabstate == FRAME_GRABBING)
2775 frmx = 1;
2778 /* If no frame is active, start one. */
2779 if (frmx == -1)
2780 ibmcam_new_frame(ibmcam, frmx = 0);
2782 frame = &ibmcam->frame[frmx];
2784 restart:
2785 if (!IBMCAM_IS_OPERATIONAL(ibmcam))
2786 return -EIO;
2787 while (frame->grabstate == FRAME_GRABBING) {
2788 interruptible_sleep_on((void *)&frame->wq);
2789 if (signal_pending(current))
2790 return -EINTR;
2793 if (frame->grabstate == FRAME_ERROR) {
2794 frame->bytes_read = 0;
2795 if (ibmcam_new_frame(ibmcam, frmx))
2796 printk(KERN_ERR "ibmcam_read: ibmcam_new_frame error\n");
2797 goto restart;
2800 if (debug >= 1)
2801 printk(KERN_DEBUG "ibmcam_read: frmx=%d, bytes_read=%ld, scanlength=%ld\n",
2802 frmx, frame->bytes_read, frame->scanlength);
2804 /* copy bytes to user space; we allow for partials reads */
2805 if ((count + frame->bytes_read) > frame->scanlength)
2806 count = frame->scanlength - frame->bytes_read;
2808 if (copy_to_user(buf, frame->data + frame->bytes_read, count))
2809 return -EFAULT;
2811 frame->bytes_read += count;
2812 if (debug >= 1)
2813 printk(KERN_DEBUG "ibmcam_read: {copy} count used=%ld, new bytes_read=%ld\n",
2814 count, frame->bytes_read);
2816 if (frame->bytes_read >= frame->scanlength) { /* All data has been read */
2817 frame->bytes_read = 0;
2819 /* Mark it as available to be used again. */
2820 ibmcam->frame[frmx].grabstate = FRAME_UNUSED;
2821 if (ibmcam_new_frame(ibmcam, frmx ? 0 : 1))
2822 printk(KERN_ERR "ibmcam_read: ibmcam_new_frame returned error\n");
2825 return count;
2828 static int ibmcam_mmap(struct video_device *dev, const char *adr, unsigned long size)
2830 struct usb_ibmcam *ibmcam = (struct usb_ibmcam *)dev;
2831 unsigned long start = (unsigned long)adr;
2832 unsigned long page, pos;
2834 if (!IBMCAM_IS_OPERATIONAL(ibmcam))
2835 return -EFAULT;
2837 if (size > (((2 * MAX_FRAME_SIZE) + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1)))
2838 return -EINVAL;
2840 pos = (unsigned long)ibmcam->fbuf;
2841 while (size > 0) {
2842 page = kvirt_to_pa(pos);
2843 if (remap_page_range(start, page, PAGE_SIZE, PAGE_SHARED))
2844 return -EAGAIN;
2846 start += PAGE_SIZE;
2847 pos += PAGE_SIZE;
2848 if (size > PAGE_SIZE)
2849 size -= PAGE_SIZE;
2850 else
2851 size = 0;
2854 return 0;
2857 static struct video_device ibmcam_template = {
2858 "CPiA USB Camera",
2859 VID_TYPE_CAPTURE,
2860 VID_HARDWARE_CPIA,
2861 ibmcam_open,
2862 ibmcam_close,
2863 ibmcam_read,
2864 ibmcam_write,
2865 NULL,
2866 ibmcam_ioctl,
2867 ibmcam_mmap,
2868 ibmcam_init_done,
2869 NULL,
2874 static void usb_ibmcam_configure_video(struct usb_ibmcam *ibmcam)
2876 if (ibmcam == NULL)
2877 return;
2879 RESTRICT_TO_RANGE(init_brightness, 0, 255);
2880 RESTRICT_TO_RANGE(init_contrast, 0, 255);
2881 RESTRICT_TO_RANGE(init_color, 0, 255);
2882 RESTRICT_TO_RANGE(init_hue, 0, 255);
2883 RESTRICT_TO_RANGE(hue_correction, 0, 255);
2885 memset(&ibmcam->vpic, 0, sizeof(ibmcam->vpic));
2886 memset(&ibmcam->vpic_old, 0x55, sizeof(ibmcam->vpic_old));
2888 ibmcam->vpic.colour = init_color << 8;
2889 ibmcam->vpic.hue = init_hue << 8;
2890 ibmcam->vpic.brightness = init_brightness << 8;
2891 ibmcam->vpic.contrast = init_contrast << 8;
2892 ibmcam->vpic.whiteness = 105 << 8; /* This one isn't used */
2893 ibmcam->vpic.depth = 24;
2894 ibmcam->vpic.palette = VIDEO_PALETTE_RGB24;
2896 memset(&ibmcam->vcap, 0, sizeof(ibmcam->vcap));
2897 strcpy(ibmcam->vcap.name, "IBM USB Camera");
2898 ibmcam->vcap.type = VID_TYPE_CAPTURE;
2899 ibmcam->vcap.channels = 1;
2900 ibmcam->vcap.audios = 0;
2901 ibmcam->vcap.maxwidth = imgwidth;
2902 ibmcam->vcap.maxheight = imgheight;
2903 ibmcam->vcap.minwidth = min_imgwidth;
2904 ibmcam->vcap.minheight = min_imgheight;
2906 memset(&ibmcam->vchan, 0, sizeof(ibmcam->vchan));
2907 ibmcam->vchan.flags = 0;
2908 ibmcam->vchan.tuners = 0;
2909 ibmcam->vchan.channel = 0;
2910 ibmcam->vchan.type = VIDEO_TYPE_CAMERA;
2911 strcpy(ibmcam->vchan.name, "Camera");
2915 * ibmcam_find_struct()
2917 * This code searches the array of preallocated (static) structures
2918 * and returns index of the first one that isn't in use. Returns -1
2919 * if there are no free structures.
2921 * History:
2922 * 1/27/00 Created.
2924 static int ibmcam_find_struct(void)
2926 int i, u;
2928 for (u = 0; u < MAX_IBMCAM; u++) {
2929 struct usb_ibmcam *ibmcam = &cams[u];
2930 if (!ibmcam->ibmcam_used) /* This one is free */
2932 ibmcam->ibmcam_used = 1; /* In use now */
2933 for (i=0; i < IBMCAM_NUMFRAMES; i++)
2934 init_waitqueue_head(&ibmcam->frame[i].wq);
2935 init_MUTEX(&ibmcam->lock); /* to 1 == available */
2936 ibmcam->dev = NULL;
2937 memcpy(&ibmcam->vdev, &ibmcam_template, sizeof(ibmcam_template));
2938 return u;
2941 return -1;
2945 * usb_ibmcam_probe()
2947 * This procedure queries device descriptor and accepts the interface
2948 * if it looks like IBM C-it camera.
2950 * History:
2951 * 1/22/00 Moved camera init code to ibmcam_open()
2952 * 1/27/00 Changed to use static structures, added locking.
2953 * 5/24/00 Corrected to prevent race condition (MOD_xxx_USE_COUNT).
2954 * 7/3/00 Fixed endianness bug.
2956 static void *usb_ibmcam_probe(struct usb_device *dev, unsigned int ifnum,
2957 const struct usb_device_id *id)
2959 struct usb_ibmcam *ibmcam = NULL;
2960 const struct usb_interface_descriptor *interface;
2961 const struct usb_endpoint_descriptor *endpoint;
2962 int devnum, model=0;
2964 if (debug >= 1)
2965 printk(KERN_DEBUG "ibmcam_probe(%p,%u.)\n", dev, ifnum);
2967 /* We don't handle multi-config cameras */
2968 if (dev->descriptor.bNumConfigurations != 1)
2969 return NULL;
2971 /* Check the version/revision */
2972 switch (dev->descriptor.bcdDevice) {
2973 case 0x0002:
2974 if (ifnum != 2)
2975 return NULL;
2976 printk(KERN_INFO "IBM USB camera found (model 1, rev. 0x%04x).\n",
2977 dev->descriptor.bcdDevice);
2978 model = IBMCAM_MODEL_1;
2979 break;
2980 case 0x030A:
2981 if (ifnum != 0)
2982 return NULL;
2983 printk(KERN_INFO "IBM USB camera found (model 2, rev. 0x%04x).\n",
2984 dev->descriptor.bcdDevice);
2985 model = IBMCAM_MODEL_2;
2986 break;
2988 /* ibmcam_table contents prevents any other values from ever
2989 being passed to us, so no need for "default" case. */
2992 /* Validate found interface: must have one ISO endpoint */
2993 interface = &dev->actconfig->interface[ifnum].altsetting[0];
2994 if (interface->bNumEndpoints != 1) {
2995 printk(KERN_ERR "IBM camera: interface %d. has %u. endpoints!\n",
2996 ifnum, (unsigned)(interface->bNumEndpoints));
2997 return NULL;
2999 endpoint = &interface->endpoint[0];
3000 if ((endpoint->bmAttributes & 0x03) != 0x01) {
3001 printk(KERN_ERR "IBM camera: interface %d. has non-ISO endpoint!\n", ifnum);
3002 return NULL;
3004 if ((endpoint->bEndpointAddress & 0x80) == 0) {
3005 printk(KERN_ERR "IBM camera: interface %d. has ISO OUT endpoint!\n", ifnum);
3006 return NULL;
3009 /* Validate options */
3010 if (model == IBMCAM_MODEL_1) {
3011 RESTRICT_TO_RANGE(lighting, 0, 2);
3012 RESTRICT_TO_RANGE(videosize, VIDEOSIZE_128x96, VIDEOSIZE_352x288);
3013 } else {
3014 RESTRICT_TO_RANGE(lighting, 0, 15);
3015 RESTRICT_TO_RANGE(videosize, VIDEOSIZE_176x144, VIDEOSIZE_352x240);
3018 /* Code below may sleep, need to lock module while we are here */
3019 MOD_INC_USE_COUNT;
3021 devnum = ibmcam_find_struct();
3022 if (devnum == -1) {
3023 printk(KERN_INFO "IBM USB camera driver: Too many devices!\n");
3024 ibmcam = NULL; /* Do not free, it's preallocated */
3025 goto probe_done;
3027 ibmcam = &cams[devnum];
3029 down(&ibmcam->lock);
3030 ibmcam->camera_model = model;
3031 ibmcam->remove_pending = 0;
3032 ibmcam->last_error = 0;
3033 ibmcam->dev = dev;
3034 ibmcam->iface = ifnum;
3035 ibmcam->ifaceAltInactive = 0;
3036 ibmcam->ifaceAltActive = 1;
3037 ibmcam->video_endp = endpoint->bEndpointAddress;
3038 ibmcam->iso_packet_len = 1014;
3039 ibmcam->compress = 0;
3040 ibmcam->user=0;
3042 usb_ibmcam_configure_video(ibmcam);
3043 up (&ibmcam->lock);
3045 if (video_register_device(&ibmcam->vdev, VFL_TYPE_GRABBER) == -1) {
3046 printk(KERN_ERR "video_register_device failed\n");
3047 ibmcam = NULL; /* Do not free, it's preallocated */
3049 if (debug > 1)
3050 printk(KERN_DEBUG "video_register_device() successful\n");
3051 probe_done:
3052 MOD_DEC_USE_COUNT;
3053 return ibmcam;
3057 * usb_ibmcam_release()
3059 * This code does final release of struct usb_ibmcam. This happens
3060 * after the device is disconnected -and- all clients closed their files.
3062 * History:
3063 * 1/27/00 Created.
3065 static void usb_ibmcam_release(struct usb_ibmcam *ibmcam)
3067 video_unregister_device(&ibmcam->vdev);
3068 if (debug > 0)
3069 printk(KERN_DEBUG "usb_ibmcam_release: Video unregistered.\n");
3070 ibmcam->ibmcam_used = 0;
3071 ibmcam->initialized = 0;
3075 * usb_ibmcam_disconnect()
3077 * This procedure stops all driver activity, deallocates interface-private
3078 * structure (pointed by 'ptr') and after that driver should be removable
3079 * with no ill consequences.
3081 * This code handles surprise removal. The ibmcam->user is a counter which
3082 * increments on open() and decrements on close(). If we see here that
3083 * this counter is not 0 then we have a client who still has us opened.
3084 * We set ibmcam->remove_pending flag as early as possible, and after that
3085 * all access to the camera will gracefully fail. These failures should
3086 * prompt client to (eventually) close the video device, and then - in
3087 * ibmcam_close() - we decrement ibmcam->ibmcam_used and usage counter.
3089 * History:
3090 * 1/22/00 Added polling of MOD_IN_USE to delay removal until all users gone.
3091 * 1/27/00 Reworked to allow pending disconnects; see ibmcam_close()
3092 * 5/24/00 Corrected to prevent race condition (MOD_xxx_USE_COUNT).
3094 static void usb_ibmcam_disconnect(struct usb_device *dev, void *ptr)
3096 static const char proc[] = "usb_ibmcam_disconnect";
3097 struct usb_ibmcam *ibmcam = (struct usb_ibmcam *) ptr;
3099 MOD_INC_USE_COUNT;
3101 if (debug > 0)
3102 printk(KERN_DEBUG "%s(%p,%p.)\n", proc, dev, ptr);
3104 down(&ibmcam->lock);
3105 ibmcam->remove_pending = 1; /* Now all ISO data will be ignored */
3107 /* At this time we ask to cancel outstanding URBs */
3108 ibmcam_stop_isoc(ibmcam);
3110 ibmcam->dev = NULL; /* USB device is no more */
3112 if (ibmcam->user)
3113 printk(KERN_INFO "%s: In use, disconnect pending.\n", proc);
3114 else
3115 usb_ibmcam_release(ibmcam);
3116 up(&ibmcam->lock);
3117 printk(KERN_INFO "IBM USB camera disconnected.\n");
3119 MOD_DEC_USE_COUNT;
3122 static struct usb_device_id ibmcam_table [] = {
3124 idVendor: 0x0545,
3125 idProduct: 0x8080,
3126 bcdDevice_lo: 0x0002,
3127 bcdDevice_hi: 0x0002
3130 idVendor: 0x0545,
3131 idProduct: 0x8080,
3132 bcdDevice_lo: 0X030a,
3133 bcdDevice_hi: 0x030a
3135 { } /* Terminating entry */
3138 MODULE_DEVICE_TABLE (usb, ibmcam_table);
3140 static struct usb_driver ibmcam_driver = {
3141 name: "ibmcam",
3142 probe: usb_ibmcam_probe,
3143 disconnect: usb_ibmcam_disconnect,
3144 id_table: ibmcam_table,
3148 * usb_ibmcam_init()
3150 * This code is run to initialize the driver.
3152 * History:
3153 * 1/27/00 Reworked to use statically allocated usb_ibmcam structures.
3155 static int __init usb_ibmcam_init(void)
3157 unsigned u;
3159 /* Initialize struct */
3160 for (u = 0; u < MAX_IBMCAM; u++) {
3161 struct usb_ibmcam *ibmcam = &cams[u];
3162 memset (ibmcam, 0, sizeof(struct usb_ibmcam));
3164 return usb_register(&ibmcam_driver);
3167 static void __exit usb_ibmcam_cleanup(void)
3169 usb_deregister(&ibmcam_driver);
3172 module_init(usb_ibmcam_init);
3173 module_exit(usb_ibmcam_cleanup);