Import 2.3.18pre1
[davej-history.git] / drivers / usb / cpia.c
blob6a466c5a7f373242ee761932e02bc65d6143c3f0
1 /*
2 * USB CPiA Video Camera driver
4 * Supports CPiA based Video Cameras. Many manufacturers use this chipset.
5 * There's a good chance, if you have a USB video camera, it's a CPiA based
6 * one.
8 * (C) Copyright 1999 Johannes Erdfelt
9 * (C) Copyright 1999 Randy Dunlap
12 #include <linux/kernel.h>
13 #include <linux/sched.h>
14 #include <linux/list.h>
15 #include <linux/malloc.h>
16 #include <linux/mm.h>
17 #include <linux/smp_lock.h>
18 #include <linux/videodev.h>
19 #include <linux/vmalloc.h>
20 #include <linux/wrapper.h>
21 #include <linux/module.h>
22 #include <linux/spinlock.h>
24 #include <asm/io.h>
26 #include "usb.h"
27 #include "cpia.h"
29 #define CPIA_DEBUG /* Gobs of debugging info */
31 #define MAX_FRAME_SIZE (384 * 288 * 3)
33 /*******************************/
34 /* Memory management functions */
35 /*******************************/
37 /* convert virtual user memory address to physical address */
38 /* (virt_to_phys only works for kmalloced kernel memory) */
40 static inline unsigned long uvirt_to_phys(unsigned long adr)
42 pgd_t *pgd;
43 pmd_t *pmd;
44 pte_t *ptep, pte;
46 pgd = pgd_offset(current->mm, adr);
47 if (pgd_none(*pgd))
48 return 0;
49 pmd = pmd_offset(pgd, adr);
50 if (pmd_none(*pmd))
51 return 0;
52 ptep = pte_offset(pmd, adr/*&(~PGDIR_MASK)*/);
53 pte = *ptep;
54 if(pte_present(pte))
55 return
56 virt_to_phys((void *)(pte_page(pte)|(adr&(PAGE_SIZE-1))));
57 return 0;
60 static inline unsigned long uvirt_to_bus(unsigned long adr)
62 return virt_to_bus(phys_to_virt(uvirt_to_phys(adr)));
65 /* convert virtual kernel memory address to physical address */
66 /* (virt_to_phys only works for kmalloced kernel memory) */
68 static inline unsigned long kvirt_to_phys(unsigned long adr)
70 return uvirt_to_phys(VMALLOC_VMADDR(adr));
73 static inline unsigned long kvirt_to_bus(unsigned long adr)
75 return uvirt_to_bus(VMALLOC_VMADDR(adr));
79 static void * rvmalloc(unsigned long size)
81 void * mem;
82 unsigned long adr, page;
84 size += (PAGE_SIZE - 1);
85 size &= ~(PAGE_SIZE - 1);
87 mem=vmalloc(size);
88 if (mem)
90 memset(mem, 0, size); /* Clear the ram out, no junk to the user */
91 adr=(unsigned long) mem;
92 while (size > 0)
94 page = kvirt_to_phys(adr);
95 mem_map_reserve(MAP_NR(phys_to_virt(page)));
96 adr+=PAGE_SIZE;
97 if (size > PAGE_SIZE)
98 size-=PAGE_SIZE;
99 else
100 size=0;
103 return mem;
106 static void rvfree(void * mem, unsigned long size)
108 unsigned long adr, page;
110 size += (PAGE_SIZE - 1);
111 size &= ~(PAGE_SIZE - 1);
113 if (mem)
115 adr=(unsigned long) mem;
116 while (size > 0)
118 page = kvirt_to_phys(adr);
119 mem_map_unreserve(MAP_NR(phys_to_virt(page)));
120 adr+=PAGE_SIZE;
121 if (size > PAGE_SIZE)
122 size-=PAGE_SIZE;
123 else
124 size=0;
126 vfree(mem);
130 int usb_cpia_get_version(struct usb_device *dev, void *buf)
132 devrequest dr;
134 dr.requesttype = (USB_TYPE_VENDOR | USB_RECIP_DEVICE) | 0x80;
135 dr.request = USB_REQ_CPIA_GET_VERSION;
136 dr.value = 0;
137 dr.index = 0;
138 dr.length = 4;
140 return dev->bus->op->control_msg(dev, usb_rcvctrlpipe(dev,0), &dr, buf, 4);
143 int usb_cpia_get_pnp_id(struct usb_device *dev, void *buf)
145 devrequest dr;
147 dr.requesttype = (USB_TYPE_VENDOR | USB_RECIP_DEVICE) | 0x80;
148 dr.request = USB_REQ_CPIA_GET_PNP_ID;
149 dr.value = 0;
150 dr.index = 0;
151 dr.length = 6;
153 return dev->bus->op->control_msg(dev, usb_rcvctrlpipe(dev,0), &dr, buf, 6);
156 int usb_cpia_get_camera_status(struct usb_device *dev, void *buf)
158 devrequest dr;
160 dr.requesttype = (USB_TYPE_VENDOR | USB_RECIP_DEVICE) | 0x80;
161 dr.request = USB_REQ_CPIA_GET_CAMERA_STATUS;
162 dr.value = 0;
163 dr.index = 0;
164 dr.length = 8;
166 return dev->bus->op->control_msg(dev, usb_rcvctrlpipe(dev,0), &dr, buf, 8);
169 int usb_cpia_goto_hi_power(struct usb_device *dev)
171 devrequest dr;
173 dr.requesttype = (USB_TYPE_VENDOR | USB_RECIP_DEVICE);
174 dr.request = USB_REQ_CPIA_GOTO_HI_POWER;
175 dr.value = 0;
176 dr.index = 0;
177 dr.length = 0;
179 return dev->bus->op->control_msg(dev, usb_sndctrlpipe(dev,0), &dr, NULL, 0);
182 int usb_cpia_get_vp_version(struct usb_device *dev, void *buf)
184 devrequest dr;
186 dr.requesttype = (USB_TYPE_VENDOR | USB_RECIP_DEVICE);
187 dr.request = USB_REQ_CPIA_GET_VP_VERSION;
188 dr.value = 0;
189 dr.index = 0;
190 dr.length = 4;
192 return dev->bus->op->control_msg(dev, usb_sndctrlpipe(dev,0), &dr, buf, 4);
195 int usb_cpia_set_sensor_fps(struct usb_device *dev, int sensorbaserate, int sensorclkdivisor)
197 devrequest dr;
199 dr.requesttype = (USB_TYPE_VENDOR | USB_RECIP_DEVICE);
200 dr.request = USB_REQ_CPIA_SET_SENSOR_FPS;
201 dr.value = (sensorclkdivisor << 8) + sensorbaserate;
202 dr.index = 0;
203 dr.length = 0;
205 return dev->bus->op->control_msg(dev, usb_sndctrlpipe(dev,0), &dr, NULL, 0);
208 int usb_cpia_grab_frame(struct usb_device *dev, int streamstartline)
210 devrequest dr;
212 dr.requesttype = (USB_TYPE_VENDOR | USB_RECIP_DEVICE);
213 dr.request = USB_REQ_CPIA_GRAB_FRAME;
214 dr.value = streamstartline << 8;
215 dr.index = 0;
216 dr.length = 0;
218 return dev->bus->op->control_msg(dev, usb_sndctrlpipe(dev,0), &dr, NULL, 0);
221 int usb_cpia_upload_frame(struct usb_device *dev, int forceupload)
223 devrequest dr;
225 dr.requesttype = (USB_TYPE_VENDOR | USB_RECIP_DEVICE);
226 dr.request = USB_REQ_CPIA_UPLOAD_FRAME;
227 dr.value = forceupload;
228 dr.index = 0;
229 dr.length = 0;
231 return dev->bus->op->control_msg(dev, usb_sndctrlpipe(dev,0), &dr, NULL, 0);
234 int usb_cpia_set_grab_mode(struct usb_device *dev, int continuousgrab)
236 devrequest dr;
238 dr.requesttype = (USB_TYPE_VENDOR | USB_RECIP_DEVICE);
239 dr.request = USB_REQ_CPIA_SET_GRAB_MODE;
240 dr.value = continuousgrab;
241 dr.index = 0;
242 dr.length = 0;
244 return dev->bus->op->control_msg(dev, usb_sndctrlpipe(dev,0), &dr, NULL, 0);
247 int usb_cpia_set_format(struct usb_device *dev, int size, int subsample, int order)
249 devrequest dr;
251 dr.requesttype = (USB_TYPE_VENDOR | USB_RECIP_DEVICE);
252 dr.request = USB_REQ_CPIA_SET_FORMAT;
253 dr.value = (subsample << 8) + size;
254 dr.index = order;
255 dr.length = 0;
257 return dev->bus->op->control_msg(dev, usb_sndctrlpipe(dev,0), &dr, NULL, 0);
260 int usb_cpia_set_compression(struct usb_device *dev, int compmode, int decimation)
262 devrequest dr;
264 dr.requesttype = (USB_TYPE_VENDOR | USB_RECIP_DEVICE);
265 dr.request = USB_REQ_CPIA_SET_COMPRESSION;
266 dr.value = (decimation << 8) + compmode;
267 dr.index = 0;
268 dr.length = 0;
270 return dev->bus->op->control_msg(dev, usb_sndctrlpipe(dev,0), &dr, NULL, 0);
273 int usb_cpia_initstreamcap(struct usb_device *dev, int skipframes, int streamstartline)
275 devrequest dr;
277 dr.requesttype = (USB_TYPE_VENDOR | USB_RECIP_DEVICE);
278 dr.request = USB_REQ_CPIA_INIT_STREAM_CAP;
279 dr.value = (streamstartline << 8) + skipframes;
280 dr.index = 0;
281 dr.length = 0;
283 return dev->bus->op->control_msg(dev, usb_sndctrlpipe(dev,0), &dr, NULL, 0);
286 int usb_cpia_finistreamcap(struct usb_device *dev)
288 devrequest dr;
290 dr.requesttype = (USB_TYPE_VENDOR | USB_RECIP_DEVICE);
291 dr.request = USB_REQ_CPIA_FINI_STREAM_CAP;
292 dr.value = 0;
293 dr.index = 0;
294 dr.length = 0;
296 return dev->bus->op->control_msg(dev, usb_sndctrlpipe(dev,0), &dr, NULL, 0);
299 int usb_cpia_startstreamcap(struct usb_device *dev)
301 devrequest dr;
303 dr.requesttype = (USB_TYPE_VENDOR | USB_RECIP_DEVICE);
304 dr.request = USB_REQ_CPIA_START_STREAM_CAP;
305 dr.value = 0;
306 dr.index = 0;
307 dr.length = 0;
309 return dev->bus->op->control_msg(dev, usb_sndctrlpipe(dev,0), &dr, NULL, 0);
312 int usb_cpia_endstreamcap(struct usb_device *dev)
314 devrequest dr;
316 dr.requesttype = (USB_TYPE_VENDOR | USB_RECIP_DEVICE);
317 dr.request = USB_REQ_CPIA_END_STREAM_CAP;
318 dr.value = 0;
319 dr.index = 0;
320 dr.length = 0;
322 return dev->bus->op->control_msg(dev, usb_sndctrlpipe(dev,0), &dr, NULL, 0);
325 /* How much data is left in the scratch buf? */
326 #define scratch_left(x) (cpia->scratchlen - (int)((char *)x - (char *)cpia->scratch))
328 static void cpia_parse_data(struct usb_cpia *cpia)
330 unsigned char *data = cpia->scratch;
331 unsigned long l;
332 int done;
334 done = 0;
335 while (!done && scratch_left(data)) {
336 switch (cpia->state) {
337 case STATE_SCANNING:
339 unsigned char *begin = data;
341 /* We need atleast 2 bytes for the magic value */
342 if (scratch_left(data) < 2) {
343 done = 1;
344 break;
347 /* 0x1968 is magic */
348 printk("header: %X\n", (*data << 8) + *(data + 1));
349 if ((*data == 0x19) && (*(data + 1) == 0x68)) {
350 cpia->state = STATE_HEADER;
351 printk("moving to header\n");
352 break;
355 /* Woops, lost the header, find the end of the frame */
356 if (scratch_left(data) < 4) {
357 done = 1;
358 break;
361 printk("Scanning for end of frame\n");
362 while (scratch_left(data) >= 4) {
363 if ((*data == 0xFF) &&
364 (*(data + 1) == 0xFF) &&
365 (*(data + 2) == 0xFF) &&
366 (*(data + 3) == 0xFF)) {
367 data += 4;
368 break;
370 data++;
372 #ifdef CPIA_DEBUG
373 printk("scan: scanned %d bytes\n", data-begin);
374 #endif
375 break;
377 case STATE_HEADER:
378 /* We need atleast 64 bytes for the header */
379 if (scratch_left(data) < 64) {
380 done = 1;
381 break;
384 #ifdef CPIA_DEBUG
385 printk("header: framerate %d\n", data[41]);
386 #endif
388 data += 64;
390 cpia->state = STATE_LINES;
392 break;
393 case STATE_LINES:
395 unsigned char *begin = data;
396 int found = 0;
398 while (scratch_left(data)) {
399 if (*data == 0xFD) {
400 data++;
401 found = 1;
402 break;
403 } else if ((*data == 0xFF) &&
404 (scratch_left(data) >= 3) &&
405 (*(data + 1) == 0xFF) &&
406 (*(data + 2) == 0xFF) &&
407 (*(data + 3) == 0xFF)) {
408 data += 4;
409 cpia->curline = 144;
410 found = 1;
411 break;
414 data++;
417 if (data-begin == 355 && cpia->frame[cpia->curframe].width != 64) {
418 int i;
419 char *f = cpia->frame[cpia->curframe].data, *b = begin;
421 b += 2;
422 f += (cpia->frame[cpia->curframe].width * 3) * cpia->curline;
424 for (i = 0; i < 176; i++)
425 f[(i * 3) + 0] =
426 f[(i * 3) + 1] =
427 f[(i * 3) + 2] =
428 b[(i * 2)];
431 if (found) {
432 cpia->curline++;
433 if (cpia->curline >= 144) {
434 wake_up(&cpia->wq);
435 cpia->state = STATE_SCANNING;
436 cpia->curline = 0;
437 cpia->curframe = -1;
438 done = 1;
440 } else {
441 data = begin;
442 done = 1;
445 break;
450 /* Grab the remaining */
451 l = scratch_left(data);
452 memmove(cpia->scratch, data, l);
454 cpia->scratchlen = l;
458 * For the moment there is no actual data compression (making blocks
459 * of data contiguous). This just checks the "frames" array for errors.
461 static int cpia_compress_isochronous(struct usb_isoc_desc *isodesc)
463 char *data = isodesc->data;
464 int i, totlen = 0;
466 for (i = 0; i < isodesc->frame_count; i++) {
467 int n = isodesc->frames [i].frame_length;
468 int st = isodesc->frames [i].frame_status;
470 #ifdef CPIA_DEBUG
471 /* Debugging */
472 if (st)
473 printk(KERN_DEBUG "cpia data error: [%d] len=%d, status=%X\n",
474 i, n, st);
475 #endif
479 return totlen;
482 static int cpia_isoc_irq(int status, void *__buffer, int len, void *dev_id)
484 struct usb_cpia *cpia = dev_id;
485 struct usb_device *dev = cpia->dev;
486 struct cpia_sbuf *sbuf;
487 int i;
488 char *p;
490 if (!cpia->streaming) {
491 printk("oops, not streaming, but interrupt\n");
492 return 0;
495 if (cpia->curframe < 0) {
496 if (cpia->frame[0].state == FRAME_READY) {
497 cpia->curframe = 0;
498 cpia->frame[0].state = FRAME_GRABBING;
499 #ifdef CPIA_DEBUG
500 printk("capturing to frame 0\n");
501 #endif
502 } else if (cpia->frame[1].state == FRAME_READY) {
503 cpia->curframe = 1;
504 cpia->frame[1].state = FRAME_GRABBING;
505 #ifdef CPIA_DEBUG
506 printk("capturing to frame 1\n");
507 #endif
508 #ifdef CPIA_DEBUG
509 } else
510 printk("no frame available\n");
511 #else
513 #endif
516 sbuf = &cpia->sbuf[cpia->receivesbuf];
518 usb_kill_isoc(sbuf->isodesc);
520 /* Do something to it now */
521 sbuf->len = cpia_compress_isochronous(sbuf->isodesc);
523 #ifdef CPIA_DEBUG
524 if (sbuf->len)
525 printk("%d bytes received\n", sbuf->len);
526 #endif
528 if (sbuf->len && cpia->curframe >= 0) {
529 if (sbuf->len > (SCRATCH_BUF_SIZE - cpia->scratchlen)) {
530 printk("overflow!\n");
531 return 0;
533 memcpy(cpia->scratch + cpia->scratchlen, sbuf->data, sbuf->len);
534 cpia->scratchlen += sbuf->len;
536 cpia_parse_data(cpia);
539 /* Reschedule this block of Isochronous desc */
541 usb_run_isoc(sbuf->isodesc, NULL);
544 usb_schedule_isochronous(dev, sbuf->isodesc, cpia->sbuf[(cpia->receivesbuf + 2) % 3].isodesc);
547 /* Move to the next one */
548 cpia->receivesbuf = (cpia->receivesbuf + 1) % 3;
550 return 1;
553 int cpia_init_isoc(struct usb_cpia *cpia)
555 struct usb_device *dev = cpia->dev;
556 struct usb_isoc_desc *id;
557 int fx, err;
559 cpia->receivesbuf = 0;
561 cpia->scratchlen = 0;
562 cpia->curline = 0;
563 cpia->state = STATE_SCANNING;
565 /* ALT_ISOC is only doing double-buffering, not triple. */
566 err = usb_init_isoc (dev, usb_rcvisocpipe (dev, 1), FRAMES_PER_DESC, cpia,
567 &cpia->sbuf[0].isodesc);
568 if (err)
569 printk ("cpia_init_isoc: usb_init_isoc() ret. %d\n", err);
570 err = usb_init_isoc (dev, usb_rcvisocpipe (dev, 1), FRAMES_PER_DESC, cpia,
571 &cpia->sbuf[1].isodesc);
572 if (err)
573 printk ("cpia_init_isoc: usb_init_isoc() ret. %d\n", err);
575 if (!cpia->sbuf[0].isodesc || !cpia->sbuf[1].isodesc) {
576 if (cpia->sbuf[0].isodesc)
577 usb_free_isoc (cpia->sbuf[0].isodesc);
578 if (cpia->sbuf[1].isodesc)
579 usb_free_isoc (cpia->sbuf[1].isodesc);
580 return -ENOMEM;
582 #if 0
583 cpia->sbuf[0].isodesc = usb_allocate_isochronous(dev, usb_rcvisocpipe(dev,1), cpia->sbuf[0].data, STREAM_BUF_SIZE, 960, cpia_isoc_irq, cpia);
584 cpia->sbuf[1].isodesc = usb_allocate_isochronous(dev, usb_rcvisocpipe(dev,1), cpia->sbuf[1].data, STREAM_BUF_SIZE, 960, cpia_isoc_irq, cpia);
585 cpia->sbuf[2].isodesc = usb_allocate_isochronous(dev, usb_rcvisocpipe(dev,1), cpia->sbuf[2].data, STREAM_BUF_SIZE, 960, cpia_isoc_irq, cpia);
586 #endif
588 #ifdef CPIA_DEBUG
589 printk("isodesc[0] @ %p\n", cpia->sbuf[0].isodesc);
590 printk("isodesc[1] @ %p\n", cpia->sbuf[1].isodesc);
591 #if 0
592 printk("isodesc[2] @ %p\n", cpia->sbuf[2].isodesc);
593 #endif
594 #endif
596 /* Set the Isoc. desc. parameters. */
597 /* First for desc. [0] */
598 id = cpia->sbuf [0].isodesc;
599 id->start_type = START_ASAP;
600 id->callback_frames = 10; /* on every 10th frame */
601 id->callback_fn = cpia_isoc_irq;
602 id->data = cpia->sbuf[0].data;
603 id->buf_size = FRAME_SIZE_PER_DESC * FRAMES_PER_DESC;
604 for (fx = 0; fx < FRAMES_PER_DESC; fx++)
605 id->frames [fx].frame_length = FRAME_SIZE_PER_DESC;
607 /* and the desc. [1] */
608 id = cpia->sbuf [1].isodesc;
609 id->start_type = 0; /* will follow the first desc. */
610 id->callback_frames = 10; /* on every 10th frame */
611 id->callback_fn = cpia_isoc_irq;
612 id->data = cpia->sbuf[1].data;
613 id->buf_size = FRAME_SIZE_PER_DESC * FRAMES_PER_DESC;
614 for (fx = 0; fx < FRAMES_PER_DESC; fx++)
615 id->frames [fx].frame_length = FRAME_SIZE_PER_DESC;
617 usb_run_isoc (cpia->sbuf[0].isodesc, NULL);
618 usb_run_isoc (cpia->sbuf[1].isodesc, cpia->sbuf[0].isodesc);
620 #if 0
621 usb_schedule_isochronous(dev, cpia->sbuf[0].isodesc, NULL);
622 usb_schedule_isochronous(dev, cpia->sbuf[1].isodesc, cpia->sbuf[0].isodesc);
623 usb_schedule_isochronous(dev, cpia->sbuf[2].isodesc, cpia->sbuf[1].isodesc);
624 #endif
626 #ifdef CPIA_DEBUG
627 printk("done scheduling\n");
628 #endif
629 if (usb_set_interface(cpia->dev, 1, 3)) {
630 printk("cpia_set_interface error\n");
631 return -EINVAL;
634 usb_cpia_startstreamcap(cpia->dev);
636 cpia->streaming = 1;
637 #ifdef CPIA_DEBUG
638 printk("now streaming\n");
639 #endif
641 return 0;
644 void cpia_stop_isoc(struct usb_cpia *cpia)
646 struct usb_device *dev = cpia->dev;
648 if (!cpia->streaming)
649 return;
651 cpia->streaming = 0;
653 /* Stop the streaming */
654 usb_cpia_endstreamcap(cpia->dev);
656 /* Set packet size to 0 */
657 if (usb_set_interface(cpia->dev, 1, 0)) {
658 printk("cpia_set_interface error\n");
659 return /* -EINVAL */;
662 /* Unschedule all of the iso td's */
663 usb_kill_isoc (cpia->sbuf[1].isodesc);
664 usb_kill_isoc (cpia->sbuf[0].isodesc);
665 #if 0
666 usb_unschedule_isochronous(dev, cpia->sbuf[2].isodesc);
667 usb_unschedule_isochronous(dev, cpia->sbuf[1].isodesc);
668 usb_unschedule_isochronous(dev, cpia->sbuf[0].isodesc);
669 #endif
671 /* Delete them all */
672 usb_free_isoc (cpia->sbuf[1].isodesc);
673 usb_free_isoc (cpia->sbuf[0].isodesc);
674 #if 0
675 usb_delete_isochronous(dev, cpia->sbuf[2].isodesc);
676 usb_delete_isochronous(dev, cpia->sbuf[1].isodesc);
677 usb_delete_isochronous(dev, cpia->sbuf[0].isodesc);
678 #endif
681 /* Video 4 Linux API */
682 static int cpia_open(struct video_device *dev, int flags)
684 int err = -ENOMEM;
685 struct usb_cpia *cpia = (struct usb_cpia *)dev;
687 #ifdef CPIA_DEBUG
688 printk("cpia_open\n");
689 #endif
691 cpia->fbuf = rvmalloc(2 * MAX_FRAME_SIZE);
692 if (!cpia->fbuf)
693 goto open_err_ret;
695 cpia->frame[0].state = FRAME_DONE;
696 cpia->frame[1].state = FRAME_DONE;
698 cpia->frame[0].data = cpia->fbuf;
699 cpia->frame[1].data = cpia->fbuf + MAX_FRAME_SIZE;
700 #ifdef CPIA_DEBUG
701 printk("frame [0] @ %p\n", cpia->frame[0].data);
702 printk("frame [1] @ %p\n", cpia->frame[1].data);
703 #endif
705 cpia->sbuf[0].data = kmalloc (FRAMES_PER_DESC * FRAME_SIZE_PER_DESC, GFP_KERNEL);
706 if (!cpia->sbuf[0].data)
707 goto open_err_on0;
709 cpia->sbuf[1].data = kmalloc (FRAMES_PER_DESC * FRAME_SIZE_PER_DESC, GFP_KERNEL);
710 if (!cpia->sbuf[1].data)
711 goto open_err_on1;
713 #if 0
714 cpia->sbuf[0].data = kmalloc(STREAM_BUF_SIZE, GFP_KERNEL);
715 if (!cpia->sbuf[0].data)
716 goto open_err_on0;
718 cpia->sbuf[1].data = kmalloc(STREAM_BUF_SIZE, GFP_KERNEL);
719 if (!cpia->sbuf[1].data)
720 goto open_err_on1;
722 cpia->sbuf[2].data = kmalloc(STREAM_BUF_SIZE, GFP_KERNEL);
723 if (!cpia->sbuf[2].data)
724 goto open_err_on2;
725 #endif
727 #ifdef CPIA_DEBUG
728 printk("sbuf[0] @ %p\n", cpia->sbuf[0].data);
729 printk("sbuf[1] @ %p\n", cpia->sbuf[1].data);
730 #if 0
731 printk("sbuf[2] @ %p\n", cpia->sbuf[2].data);
732 #endif
733 #endif
735 cpia->curframe = -1;
736 cpia->receivesbuf = 0;
738 usb_cpia_initstreamcap(cpia->dev, 0, 60);
740 err = cpia_init_isoc(cpia);
741 if (err)
742 goto open_err_on2;
744 return 0;
746 open_err_on2:
747 kfree (cpia->sbuf[1].data);
748 open_err_on1:
749 kfree (cpia->sbuf[0].data);
750 open_err_on0:
751 rvfree(cpia->fbuf, 2 * MAX_FRAME_SIZE);
752 open_err_ret:
753 return err;
756 static void cpia_close(struct video_device *dev)
758 struct usb_cpia *cpia = (struct usb_cpia *)dev;
760 #ifdef CPIA_DEBUG
761 printk("cpia_close\n");
762 #endif
764 cpia_stop_isoc(cpia);
766 usb_cpia_finistreamcap(cpia->dev);
768 rvfree(cpia->fbuf, 2 * MAX_FRAME_SIZE);
770 kfree(cpia->sbuf[2].data);
771 kfree(cpia->sbuf[1].data);
772 kfree(cpia->sbuf[0].data);
775 static int cpia_init_done(struct video_device *dev)
777 return 0;
780 static long cpia_write(struct video_device *dev, const char *buf, unsigned long count, int noblock)
782 return -EINVAL;
785 static int cpia_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
787 struct usb_cpia *cpia = (struct usb_cpia *)dev;
789 switch (cmd) {
790 case VIDIOCGCAP:
792 struct video_capability b;
794 strcpy(b.name, "CPiA USB Camera");
795 b.type = VID_TYPE_CAPTURE /* | VID_TYPE_SUBCAPTURE */;
796 b.channels = 1;
797 b.audios = 0;
798 b.maxwidth = 176 /* 352 */;
799 b.maxheight = 144 /* 240 */;
800 b.minwidth = 176 /* (Something small?) */;
801 b.minheight = 144 /* " " */;
803 if (copy_to_user(arg, &b, sizeof(b)))
804 return -EFAULT;
805 return 0;
807 case VIDIOCGCHAN:
809 struct video_channel v;
811 if (copy_from_user(&v, arg, sizeof(v)))
812 return -EFAULT;
813 if (v.channel != 0)
814 return -EINVAL;
816 v.flags = 0;
817 v.tuners = 0;
818 v.type = VIDEO_TYPE_CAMERA;
819 strcpy(v.name, "Camera");
821 if (copy_to_user(arg, &v, sizeof(v)))
822 return -EFAULT;
823 return 0;
825 case VIDIOCSCHAN:
827 int v;
829 if (copy_from_user(&v, arg, sizeof(v)))
830 return -EFAULT;
832 if (v != 0)
833 return -EINVAL;
835 return 0;
837 case VIDIOCGTUNER:
839 struct video_tuner v;
841 if (copy_from_user(&v, arg, sizeof(v)))
842 return -EFAULT;
844 if (v.tuner)
845 return -EINVAL;
847 strcpy(v.name, "Format");
849 v.rangelow = 0;
850 v.rangehigh = 0;
851 v.flags = 0;
852 v.mode = VIDEO_MODE_AUTO;
854 if (copy_to_user(arg, &v, sizeof(v)))
855 return -EFAULT;
857 return 0;
859 case VIDIOCSTUNER:
861 struct video_tuner v;
863 if (copy_from_user(&v, arg, sizeof(v)))
864 return -EFAULT;
866 if (v.tuner)
867 return -EINVAL;
869 if (v.mode != VIDEO_MODE_AUTO)
870 return -EINVAL;
872 return 0;
874 case VIDIOCGPICT:
876 struct video_picture p;
878 p.colour = 0x8000; /* Damn British people :) */
879 p.hue = 0x8000;
880 p.brightness = 180 << 8; /* XXX */
881 p.contrast = 192 << 8; /* XXX */
882 p.whiteness = 105 << 8; /* XXX */
883 #if 0
884 p.depth = 24;
885 #endif
886 p.depth = 16;
887 p.palette = VIDEO_PALETTE_YUYV;
889 if (copy_to_user(arg, &p, sizeof(p)))
890 return -EFAULT;
892 return 0;
894 case VIDIOCSPICT:
896 struct video_picture p;
898 if (copy_from_user(&p, arg, sizeof(p)))
899 return -EFAULT;
901 #ifdef CPIA_DEBUG
902 printk("Attempting to set palette %d, depth %d\n",
903 p.palette, p.depth);
904 #endif
906 return 0;
908 case VIDIOCSWIN:
910 struct video_window vw;
912 #ifdef CPIA_DEBUG
913 printk("VIDIOCSWIN\n");
914 #endif
916 if (copy_from_user(&vw, arg, sizeof(vw)))
917 return -EFAULT;
918 if (vw.flags)
919 return -EINVAL;
920 if (vw.clipcount)
921 return -EINVAL;
922 if (vw.height != 176)
923 return -EINVAL;
924 if (vw.width != 144)
925 return -EINVAL;
927 return 0;
929 case VIDIOCGWIN:
931 struct video_window vw;
933 #ifdef CPIA_DEBUG
934 printk("VIDIOCGWIN\n");
935 #endif
937 vw.x = 0;
938 vw.y = 0;
939 vw.width = 176;
940 vw.height = 144;
941 vw.chromakey = 0;
942 vw.flags = 0;
944 if (copy_to_user(arg, &vw, sizeof(vw)))
945 return -EFAULT;
947 return 0;
949 case VIDIOCGMBUF:
951 struct video_mbuf vm;
953 memset(&vm, 0, sizeof(vm));
954 vm.size = MAX_FRAME_SIZE * 2;
955 vm.frames = 2;
956 vm.offsets[0] = 0;
957 vm.offsets[1] = MAX_FRAME_SIZE;
959 if (copy_to_user((void *)arg, (void *)&vm, sizeof(vm)))
960 return -EFAULT;
962 return 0;
964 case VIDIOCMCAPTURE:
966 struct video_mmap vm;
968 if (copy_from_user((void *)&vm, (void *)arg, sizeof(vm)))
969 return -EFAULT;
971 #ifdef CPIA_DEBUG
972 printk("MCAPTURE\n");
973 printk("frame: %d, size: %dx%d, format: %d\n",
974 vm.frame, vm.width, vm.height, vm.format);
975 #endif
977 if (vm.format != VIDEO_PALETTE_RGB24)
978 return -EINVAL;
980 if ((vm.frame != 0) && (vm.frame != 1))
981 return -EINVAL;
983 cpia->frame[vm.frame].width = vm.width;
984 cpia->frame[vm.frame].height = vm.height;
986 /* Mark it as free */
987 cpia->frame[vm.frame].state = FRAME_READY;
989 return 0;
991 case VIDIOCSYNC:
993 int frame;
995 if (copy_from_user((void *)&frame, arg, sizeof(int)))
996 return -EFAULT;
998 #ifdef CPIA_DEBUG
999 printk("syncing to frame %d\n", frame);
1000 #endif
1001 switch (cpia->frame[frame].state) {
1002 case FRAME_UNUSED:
1003 return -EINVAL;
1004 case FRAME_READY:
1005 case FRAME_GRABBING:
1006 interruptible_sleep_on(&cpia->wq);
1007 case FRAME_DONE:
1008 cpia->frame[frame].state = FRAME_UNUSED;
1009 break;
1011 #ifdef CPIA_DEBUG
1012 printk("synced to frame %d\n", frame);
1013 #endif
1014 return 0;
1016 case VIDIOCCAPTURE:
1017 return -EINVAL;
1018 case VIDIOCGFBUF:
1019 return -EINVAL;
1020 case VIDIOCSFBUF:
1021 return -EINVAL;
1022 case VIDIOCKEY:
1023 return 0;
1024 case VIDIOCGFREQ:
1025 return -EINVAL;
1026 case VIDIOCSFREQ:
1027 return -EINVAL;
1028 case VIDIOCGAUDIO:
1029 return -EINVAL;
1030 case VIDIOCSAUDIO:
1031 return -EINVAL;
1032 default:
1033 return -ENOIOCTLCMD;
1035 return 0;
1038 static long cpia_read(struct video_device *dev, char *buf, unsigned long count, int noblock)
1040 struct usb_cpia *cpia = (struct usb_cpia *)dev;
1041 int len;
1043 #ifdef CPIA_DEBUG
1044 printk("cpia_read: %ld bytes\n", count);
1045 #endif
1046 #if 0
1047 len = cpia_capture(cpia, buf, count);
1049 return len;
1050 #endif
1051 return 0;
1054 static int cpia_mmap(struct video_device *dev, const char *adr, unsigned long size)
1056 struct usb_cpia *cpia = (struct usb_cpia *)dev;
1057 unsigned long start = (unsigned long)adr;
1058 unsigned long page, pos;
1060 #ifdef CPIA_DEBUG
1061 printk("mmap: %ld (%lX) bytes\n", size, size);
1062 #endif
1063 if (size > (((2 * MAX_FRAME_SIZE) + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1)))
1064 return -EINVAL;
1066 pos = (unsigned long)cpia->fbuf;
1067 while (size > 0)
1069 page = kvirt_to_phys(pos);
1070 if (remap_page_range(start, page, PAGE_SIZE, PAGE_SHARED))
1071 return -EAGAIN;
1072 start+=PAGE_SIZE;
1073 pos+=PAGE_SIZE;
1074 if (size > PAGE_SIZE)
1075 size-=PAGE_SIZE;
1076 else
1077 size=0;
1080 return 0;
1083 static struct video_device cpia_template = {
1084 "CPiA USB Camera",
1085 VID_TYPE_CAPTURE,
1086 VID_HARDWARE_CPIA,
1087 cpia_open,
1088 cpia_close,
1089 cpia_read,
1090 cpia_write,
1091 NULL,
1092 cpia_ioctl,
1093 cpia_mmap,
1094 cpia_init_done,
1095 NULL,
1100 static void usb_cpia_configure(struct usb_cpia *cpia)
1102 struct usb_device *dev = cpia->dev;
1103 unsigned char version[4];
1104 unsigned char pnpid[6];
1105 unsigned char camerastat[8];
1106 unsigned char *buf;
1108 if (usb_set_configuration(dev, dev->config[0].bConfigurationValue)) {
1109 printk (KERN_INFO " Failed usb_set_configuration: CPIA\n");
1110 return;
1113 if (usb_cpia_get_version(dev, version)) {
1114 printk("cpia_get_version error\n");
1115 return;
1118 printk("cpia: Firmware v%d.%d, VC Hardware v%d.%d\n",
1119 version[0], version[1], version[2], version[3]);
1121 if (usb_cpia_get_pnp_id(dev, pnpid)) {
1122 printk("cpia_get_pnp_id error\n");
1123 return;
1126 printk("cpia: PnP Id: Vendor: %X, Product: %X, Revision: %X\n",
1127 (pnpid[1] << 8) + pnpid[0], (pnpid[3] << 8) + pnpid[2],
1128 (pnpid[5] << 8) + pnpid[4]);
1130 memcpy(&cpia->vdev, &cpia_template, sizeof(cpia_template));
1132 init_waitqueue_head(&cpia->wq);
1134 if (video_register_device(&cpia->vdev, VFL_TYPE_GRABBER) == -1) {
1135 printk("video_register_device failed\n");
1136 return;
1139 if (usb_cpia_goto_hi_power(dev)) {
1140 printk("cpia_goto_hi_power error\n");
1141 return;
1144 if (usb_cpia_get_vp_version(dev, version)) {
1145 printk("cpia_get_vp_version error\n");
1146 return;
1149 printk("cpia: VP v%d rev %d\n", version[0], version[1]);
1150 printk("cpia: Camera Head ID %04X\n", (version[3] << 8) + version[2]);
1152 /* Turn off continuous grab */
1153 if (usb_cpia_set_grab_mode(dev, 1)) {
1154 printk("cpia_set_grab_mode error\n");
1155 return;
1158 /* Set up the sensor to be 30fps */
1159 if (usb_cpia_set_sensor_fps(dev, 1, 0)) {
1160 printk("cpia_set_sensor_fps error\n");
1161 return;
1164 /* Set video into QCIF mode, and order into YUYV mode */
1165 if (usb_cpia_set_format(dev, CPIA_QCIF, 1, CPIA_YUYV)) {
1166 printk("cpia_set_format error\n");
1167 return;
1170 /* Turn off compression */
1171 if (usb_cpia_set_compression(dev, 0, 0)) {
1172 printk("cpia_set_compression error\n");
1173 return;
1177 static int cpia_probe(struct usb_device *dev)
1179 struct usb_interface_descriptor *interface;
1180 struct usb_endpoint_descriptor *endpoint;
1181 struct usb_cpia *cpia;
1183 /* We don't handle multi-config cameras */
1184 if (dev->descriptor.bNumConfigurations != 1)
1185 return -1;
1187 interface = &dev->config[0].interface[0].altsetting[0];
1189 /* Is it a CPiA? */
1190 if (dev->descriptor.idVendor != 0x0553)
1191 return -1;
1192 if (dev->descriptor.idProduct != 0x0002)
1193 return -1;
1194 if (interface->bInterfaceClass != 0xFF)
1195 return -1;
1196 if (interface->bInterfaceSubClass != 0x00)
1197 return -1;
1199 /* We found a CPiA */
1200 printk("USB CPiA camera found\n");
1202 if ((cpia = kmalloc(sizeof(*cpia), GFP_KERNEL)) == NULL) {
1203 printk("couldn't kmalloc cpia struct\n");
1204 return -1;
1207 memset(cpia, 0, sizeof(*cpia));
1209 dev->private = cpia;
1210 cpia->dev = dev;
1212 usb_cpia_configure(cpia);
1214 return 0;
1217 static void cpia_disconnect(struct usb_device *dev)
1219 struct usb_cpia *cpia = dev->private;
1221 video_unregister_device(&cpia->vdev);
1223 /* Free the memory */
1224 kfree(cpia);
1227 static struct usb_driver cpia_driver = {
1228 "cpia",
1229 cpia_probe,
1230 cpia_disconnect,
1231 { NULL, NULL }
1235 * This should be a separate module.
1237 int usb_cpia_init(void)
1239 usb_register(&cpia_driver);
1241 return 0;
1244 #ifdef MODULE
1245 int init_module(void)
1247 return usb_cpia_init();
1250 void cleanup_module(void)
1253 #endif