Import 2.3.15pre1
[davej-history.git] / drivers / usb / cpia.c
blob9e7f0acd8c8d1b79917d01a5576b9e683df425a9
1 /*
2 * USB CPiA Video Camera driver
4 * Supports CPiA based Video Camera's. 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 */
11 #include <linux/kernel.h>
12 #include <linux/sched.h>
13 #include <linux/list.h>
14 #include <linux/malloc.h>
15 #include <linux/mm.h>
16 #include <linux/smp_lock.h>
17 #include <linux/videodev.h>
18 #include <linux/vmalloc.h>
19 #include <linux/wrapper.h>
20 #include <linux/module.h>
22 #include <asm/spinlock.h>
23 #include <asm/io.h>
25 #include "usb.h"
26 #include "cpia.h"
28 #define CPIA_DEBUG /* Gobs of debugging info */
30 #define MAX_FRAME_SIZE (384 * 288 * 3)
32 /*******************************/
33 /* Memory management functions */
34 /*******************************/
36 /* convert virtual user memory address to physical address */
37 /* (virt_to_phys only works for kmalloced kernel memory) */
39 static inline unsigned long uvirt_to_phys(unsigned long adr)
41 pgd_t *pgd;
42 pmd_t *pmd;
43 pte_t *ptep, pte;
45 pgd = pgd_offset(current->mm, adr);
46 if (pgd_none(*pgd))
47 return 0;
48 pmd = pmd_offset(pgd, adr);
49 if (pmd_none(*pmd))
50 return 0;
51 ptep = pte_offset(pmd, adr/*&(~PGDIR_MASK)*/);
52 pte = *ptep;
53 if(pte_present(pte))
54 return
55 virt_to_phys((void *)(pte_page(pte)|(adr&(PAGE_SIZE-1))));
56 return 0;
59 static inline unsigned long uvirt_to_bus(unsigned long adr)
61 return virt_to_bus(phys_to_virt(uvirt_to_phys(adr)));
64 /* convert virtual kernel memory address to physical address */
65 /* (virt_to_phys only works for kmalloced kernel memory) */
67 static inline unsigned long kvirt_to_phys(unsigned long adr)
69 return uvirt_to_phys(VMALLOC_VMADDR(adr));
72 static inline unsigned long kvirt_to_bus(unsigned long adr)
74 return uvirt_to_bus(VMALLOC_VMADDR(adr));
78 static void * rvmalloc(unsigned long size)
80 void * mem;
81 unsigned long adr, page;
83 size += (PAGE_SIZE - 1);
84 size &= ~(PAGE_SIZE - 1);
86 mem=vmalloc(size);
87 if (mem)
89 memset(mem, 0, size); /* Clear the ram out, no junk to the user */
90 adr=(unsigned long) mem;
91 while (size > 0)
93 page = kvirt_to_phys(adr);
94 mem_map_reserve(MAP_NR(phys_to_virt(page)));
95 adr+=PAGE_SIZE;
96 if (size > PAGE_SIZE)
97 size-=PAGE_SIZE;
98 else
99 size=0;
102 return mem;
105 static void rvfree(void * mem, unsigned long size)
107 unsigned long adr, page;
109 size += (PAGE_SIZE - 1);
110 size &= ~(PAGE_SIZE - 1);
112 if (mem)
114 adr=(unsigned long) mem;
115 while (size > 0)
117 page = kvirt_to_phys(adr);
118 mem_map_unreserve(MAP_NR(phys_to_virt(page)));
119 adr+=PAGE_SIZE;
120 if (size > PAGE_SIZE)
121 size-=PAGE_SIZE;
122 else
123 size=0;
125 vfree(mem);
129 int usb_cpia_get_version(struct usb_device *dev, void *buf)
131 devrequest dr;
133 dr.requesttype = (USB_TYPE_VENDOR | USB_RECIP_DEVICE) | 0x80;
134 dr.request = USB_REQ_CPIA_GET_VERSION;
135 dr.value = 0;
136 dr.index = 0;
137 dr.length = 4;
139 return dev->bus->op->control_msg(dev, usb_rcvctrlpipe(dev,0), &dr, buf, 4);
142 int usb_cpia_get_pnp_id(struct usb_device *dev, void *buf)
144 devrequest dr;
146 dr.requesttype = (USB_TYPE_VENDOR | USB_RECIP_DEVICE) | 0x80;
147 dr.request = USB_REQ_CPIA_GET_PNP_ID;
148 dr.value = 0;
149 dr.index = 0;
150 dr.length = 6;
152 return dev->bus->op->control_msg(dev, usb_rcvctrlpipe(dev,0), &dr, buf, 6);
155 int usb_cpia_get_camera_status(struct usb_device *dev, void *buf)
157 devrequest dr;
159 dr.requesttype = (USB_TYPE_VENDOR | USB_RECIP_DEVICE) | 0x80;
160 dr.request = USB_REQ_CPIA_GET_CAMERA_STATUS;
161 dr.value = 0;
162 dr.index = 0;
163 dr.length = 8;
165 return dev->bus->op->control_msg(dev, usb_rcvctrlpipe(dev,0), &dr, buf, 8);
168 int usb_cpia_goto_hi_power(struct usb_device *dev)
170 devrequest dr;
172 dr.requesttype = (USB_TYPE_VENDOR | USB_RECIP_DEVICE);
173 dr.request = USB_REQ_CPIA_GOTO_HI_POWER;
174 dr.value = 0;
175 dr.index = 0;
176 dr.length = 0;
178 return dev->bus->op->control_msg(dev, usb_sndctrlpipe(dev,0), &dr, NULL, 0);
181 int usb_cpia_get_vp_version(struct usb_device *dev, void *buf)
183 devrequest dr;
185 dr.requesttype = (USB_TYPE_VENDOR | USB_RECIP_DEVICE);
186 dr.request = USB_REQ_CPIA_GET_VP_VERSION;
187 dr.value = 0;
188 dr.index = 0;
189 dr.length = 4;
191 return dev->bus->op->control_msg(dev, usb_sndctrlpipe(dev,0), &dr, buf, 4);
194 int usb_cpia_set_sensor_fps(struct usb_device *dev, int sensorbaserate, int sensorclkdivisor)
196 devrequest dr;
198 dr.requesttype = (USB_TYPE_VENDOR | USB_RECIP_DEVICE);
199 dr.request = USB_REQ_CPIA_SET_SENSOR_FPS;
200 dr.value = (sensorclkdivisor << 8) + sensorbaserate;
201 dr.index = 0;
202 dr.length = 0;
204 return dev->bus->op->control_msg(dev, usb_sndctrlpipe(dev,0), &dr, NULL, 0);
207 int usb_cpia_grab_frame(struct usb_device *dev, int streamstartline)
209 devrequest dr;
211 dr.requesttype = (USB_TYPE_VENDOR | USB_RECIP_DEVICE);
212 dr.request = USB_REQ_CPIA_GRAB_FRAME;
213 dr.value = streamstartline << 8;
214 dr.index = 0;
215 dr.length = 0;
217 return dev->bus->op->control_msg(dev, usb_sndctrlpipe(dev,0), &dr, NULL, 0);
220 int usb_cpia_upload_frame(struct usb_device *dev, int forceupload)
222 devrequest dr;
224 dr.requesttype = (USB_TYPE_VENDOR | USB_RECIP_DEVICE);
225 dr.request = USB_REQ_CPIA_UPLOAD_FRAME;
226 dr.value = forceupload;
227 dr.index = 0;
228 dr.length = 0;
230 return dev->bus->op->control_msg(dev, usb_sndctrlpipe(dev,0), &dr, NULL, 0);
233 int usb_cpia_set_grab_mode(struct usb_device *dev, int continuousgrab)
235 devrequest dr;
237 dr.requesttype = (USB_TYPE_VENDOR | USB_RECIP_DEVICE);
238 dr.request = USB_REQ_CPIA_SET_GRAB_MODE;
239 dr.value = continuousgrab;
240 dr.index = 0;
241 dr.length = 0;
243 return dev->bus->op->control_msg(dev, usb_sndctrlpipe(dev,0), &dr, NULL, 0);
246 int usb_cpia_set_format(struct usb_device *dev, int size, int subsample, int order)
248 devrequest dr;
250 dr.requesttype = (USB_TYPE_VENDOR | USB_RECIP_DEVICE);
251 dr.request = USB_REQ_CPIA_SET_FORMAT;
252 dr.value = (subsample << 8) + size;
253 dr.index = order;
254 dr.length = 0;
256 return dev->bus->op->control_msg(dev, usb_sndctrlpipe(dev,0), &dr, NULL, 0);
259 int usb_cpia_set_compression(struct usb_device *dev, int compmode, int decimation)
261 devrequest dr;
263 dr.requesttype = (USB_TYPE_VENDOR | USB_RECIP_DEVICE);
264 dr.request = USB_REQ_CPIA_SET_COMPRESSION;
265 dr.value = (decimation << 8) + compmode;
266 dr.index = 0;
267 dr.length = 0;
269 return dev->bus->op->control_msg(dev, usb_sndctrlpipe(dev,0), &dr, NULL, 0);
272 int usb_cpia_initstreamcap(struct usb_device *dev, int skipframes, int streamstartline)
274 devrequest dr;
276 dr.requesttype = (USB_TYPE_VENDOR | USB_RECIP_DEVICE);
277 dr.request = USB_REQ_CPIA_INIT_STREAM_CAP;
278 dr.value = (streamstartline << 8) + skipframes;
279 dr.index = 0;
280 dr.length = 0;
282 return dev->bus->op->control_msg(dev, usb_sndctrlpipe(dev,0), &dr, NULL, 0);
285 int usb_cpia_finistreamcap(struct usb_device *dev)
287 devrequest dr;
289 dr.requesttype = (USB_TYPE_VENDOR | USB_RECIP_DEVICE);
290 dr.request = USB_REQ_CPIA_FINI_STREAM_CAP;
291 dr.value = 0;
292 dr.index = 0;
293 dr.length = 0;
295 return dev->bus->op->control_msg(dev, usb_sndctrlpipe(dev,0), &dr, NULL, 0);
298 int usb_cpia_startstreamcap(struct usb_device *dev)
300 devrequest dr;
302 dr.requesttype = (USB_TYPE_VENDOR | USB_RECIP_DEVICE);
303 dr.request = USB_REQ_CPIA_START_STREAM_CAP;
304 dr.value = 0;
305 dr.index = 0;
306 dr.length = 0;
308 return dev->bus->op->control_msg(dev, usb_sndctrlpipe(dev,0), &dr, NULL, 0);
311 int usb_cpia_endstreamcap(struct usb_device *dev)
313 devrequest dr;
315 dr.requesttype = (USB_TYPE_VENDOR | USB_RECIP_DEVICE);
316 dr.request = USB_REQ_CPIA_END_STREAM_CAP;
317 dr.value = 0;
318 dr.index = 0;
319 dr.length = 0;
321 return dev->bus->op->control_msg(dev, usb_sndctrlpipe(dev,0), &dr, NULL, 0);
324 /* How much data is left in the scratch buf? */
325 #define scratch_left(x) (cpia->scratchlen - (int)((char *)x - (char *)cpia->scratch))
327 static void cpia_parse_data(struct usb_cpia *cpia)
329 unsigned char *data = cpia->scratch;
330 unsigned long l;
331 int done;
333 done = 0;
334 while (!done && scratch_left(data)) {
335 switch (cpia->state) {
336 case STATE_SCANNING:
338 unsigned char *begin = data;
340 /* We need atleast 2 bytes for the magic value */
341 if (scratch_left(data) < 2) {
342 done = 1;
343 break;
346 /* 0x1968 is magic */
347 printk("header: %X\n", (*data << 8) + *(data + 1));
348 if ((*data == 0x19) && (*(data + 1) == 0x68)) {
349 cpia->state = STATE_HEADER;
350 printk("moving to header\n");
351 break;
354 /* Woops, lost the header, find the end of the frame */
355 if (scratch_left(data) < 4) {
356 done = 1;
357 break;
360 printk("Scanning for end of frame\n");
361 while (scratch_left(data) >= 4) {
362 if ((*data == 0xFF) &&
363 (*(data + 1) == 0xFF) &&
364 (*(data + 2) == 0xFF) &&
365 (*(data + 3) == 0xFF)) {
366 data += 4;
367 break;
369 data++;
371 #ifdef CPIA_DEBUG
372 printk("scan: scanned %d bytes\n", data-begin);
373 #endif
374 break;
376 case STATE_HEADER:
377 /* We need atleast 64 bytes for the header */
378 if (scratch_left(data) < 64) {
379 done = 1;
380 break;
383 #ifdef CPIA_DEBUG
384 printk("header: framerate %d\n", data[41]);
385 #endif
387 data += 64;
389 cpia->state = STATE_LINES;
391 break;
392 case STATE_LINES:
394 unsigned char *begin = data;
395 int found = 0;
397 while (scratch_left(data)) {
398 if (*data == 0xFD) {
399 data++;
400 found = 1;
401 break;
402 } else if ((*data == 0xFF) &&
403 (scratch_left(data) >= 3) &&
404 (*(data + 1) == 0xFF) &&
405 (*(data + 2) == 0xFF) &&
406 (*(data + 3) == 0xFF)) {
407 data += 4;
408 cpia->curline = 144;
409 found = 1;
410 break;
413 data++;
416 if (data-begin == 355 && cpia->frame[cpia->curframe].width != 64) {
417 int i;
418 char *f = cpia->frame[cpia->curframe].data, *b = begin;
420 b += 2;
421 f += (cpia->frame[cpia->curframe].width * 3) * cpia->curline;
423 for (i = 0; i < 176; i++)
424 f[(i * 3) + 0] =
425 f[(i * 3) + 1] =
426 f[(i * 3) + 2] =
427 b[(i * 2)];
430 if (found) {
431 cpia->curline++;
432 if (cpia->curline >= 144) {
433 wake_up(&cpia->wq);
434 cpia->state = STATE_SCANNING;
435 cpia->curline = 0;
436 cpia->curframe = -1;
437 done = 1;
439 } else {
440 data = begin;
441 done = 1;
444 break;
449 /* Grab the remaining */
450 l = scratch_left(data);
451 memmove(cpia->scratch, data, l);
453 cpia->scratchlen = l;
456 static int cpia_isoc_irq(int status, void *__buffer, int len, void *dev_id)
458 struct usb_cpia *cpia = dev_id;
459 struct usb_device *dev = cpia->dev;
460 struct cpia_sbuf *sbuf;
461 int i;
462 char *p;
464 if (!cpia->streaming) {
465 printk("oops, not streaming, but interrupt\n");
466 return 0;
469 if (cpia->curframe < 0) {
470 if (cpia->frame[0].state == FRAME_READY) {
471 cpia->curframe = 0;
472 cpia->frame[0].state = FRAME_GRABBING;
473 #ifdef CPIA_DEBUG
474 printk("capturing to frame 0\n");
475 #endif
476 } else if (cpia->frame[1].state == FRAME_READY) {
477 cpia->curframe = 1;
478 cpia->frame[1].state = FRAME_GRABBING;
479 #ifdef CPIA_DEBUG
480 printk("capturing to frame 1\n");
481 #endif
482 #ifdef CPIA_DEBUG
483 } else
484 printk("no frame available\n");
485 #else
487 #endif
490 sbuf = &cpia->sbuf[cpia->receivesbuf];
492 usb_unschedule_isochronous(dev, sbuf->isodesc);
494 /* Do something to it now */
495 sbuf->len = usb_compress_isochronous(dev, sbuf->isodesc);
497 #ifdef CPIA_DEBUG
498 if (sbuf->len)
499 printk("%d bytes received\n", sbuf->len);
500 #endif
502 if (sbuf->len && cpia->curframe >= 0) {
503 if (sbuf->len > (SCRATCH_BUF_SIZE - cpia->scratchlen)) {
504 printk("overflow!\n");
505 return 0;
507 memcpy(cpia->scratch + cpia->scratchlen, sbuf->data, sbuf->len);
508 cpia->scratchlen += sbuf->len;
510 cpia_parse_data(cpia);
513 /* Reschedule this block of Isochronous desc */
514 usb_schedule_isochronous(dev, sbuf->isodesc, cpia->sbuf[(cpia->receivesbuf + 2) % 3].isodesc);
516 /* Move to the next one */
517 cpia->receivesbuf = (cpia->receivesbuf + 1) % 3;
519 return 1;
522 int cpia_init_isoc(struct usb_cpia *cpia)
524 struct usb_device *dev = cpia->dev;
526 cpia->receivesbuf = 0;
528 cpia->scratchlen = 0;
529 cpia->curline = 0;
530 cpia->state = STATE_SCANNING;
532 /* Allocate all of the memory necessary */
533 cpia->sbuf[0].isodesc = usb_allocate_isochronous(dev, usb_rcvisocpipe(dev,1), cpia->sbuf[0].data, STREAM_BUF_SIZE, 960, cpia_isoc_irq, cpia);
534 cpia->sbuf[1].isodesc = usb_allocate_isochronous(dev, usb_rcvisocpipe(dev,1), cpia->sbuf[1].data, STREAM_BUF_SIZE, 960, cpia_isoc_irq, cpia);
535 cpia->sbuf[2].isodesc = usb_allocate_isochronous(dev, usb_rcvisocpipe(dev,1), cpia->sbuf[2].data, STREAM_BUF_SIZE, 960, cpia_isoc_irq, cpia);
537 #ifdef CPIA_DEBUG
538 printk("isodesc[0] @ %p\n", cpia->sbuf[0].isodesc);
539 printk("isodesc[1] @ %p\n", cpia->sbuf[1].isodesc);
540 printk("isodesc[2] @ %p\n", cpia->sbuf[2].isodesc);
541 #endif
543 /* Schedule the queues */
544 usb_schedule_isochronous(dev, cpia->sbuf[0].isodesc, NULL);
545 usb_schedule_isochronous(dev, cpia->sbuf[1].isodesc, cpia->sbuf[0].isodesc);
546 usb_schedule_isochronous(dev, cpia->sbuf[2].isodesc, cpia->sbuf[1].isodesc);
548 #ifdef CPIA_DEBUG
549 printk("done scheduling\n");
550 #endif
551 if (usb_set_interface(cpia->dev, 1, 3)) {
552 printk("cpia_set_interface error\n");
553 return -EINVAL;
556 usb_cpia_startstreamcap(cpia->dev);
558 cpia->streaming = 1;
559 #ifdef CPIA_DEBUG
560 printk("now streaming\n");
561 #endif
563 return 0;
566 void cpia_stop_isoc(struct usb_cpia *cpia)
568 struct usb_device *dev = cpia->dev;
570 if (!cpia->streaming)
571 return;
573 cpia->streaming = 0;
575 /* Stop the streaming */
576 usb_cpia_endstreamcap(cpia->dev);
578 /* Set packet size to 0 */
579 if (usb_set_interface(cpia->dev, 1, 0)) {
580 printk("cpia_set_interface error\n");
581 return /* -EINVAL */;
584 /* Unschedule all of the iso td's */
585 usb_unschedule_isochronous(dev, cpia->sbuf[2].isodesc);
586 usb_unschedule_isochronous(dev, cpia->sbuf[1].isodesc);
587 usb_unschedule_isochronous(dev, cpia->sbuf[0].isodesc);
589 /* Delete them all */
590 usb_delete_isochronous(dev, cpia->sbuf[2].isodesc);
591 usb_delete_isochronous(dev, cpia->sbuf[1].isodesc);
592 usb_delete_isochronous(dev, cpia->sbuf[0].isodesc);
595 /* Video 4 Linux API */
596 static int cpia_open(struct video_device *dev, int flags)
598 struct usb_cpia *cpia = (struct usb_cpia *)dev;
600 #ifdef CPIA_DEBUG
601 printk("cpia_open\n");
602 #endif
604 cpia->fbuf = rvmalloc(2 * MAX_FRAME_SIZE);
605 if (!cpia->fbuf)
606 goto open_err_ret;
608 cpia->frame[0].state = FRAME_DONE;
609 cpia->frame[1].state = FRAME_DONE;
611 cpia->frame[0].data = cpia->fbuf;
612 cpia->frame[1].data = cpia->fbuf + MAX_FRAME_SIZE;
613 #ifdef CPIA_DEBUG
614 printk("frame [0] @ %p\n", cpia->frame[0].data);
615 printk("frame [1] @ %p\n", cpia->frame[1].data);
616 #endif
618 cpia->sbuf[0].data = kmalloc(STREAM_BUF_SIZE, GFP_KERNEL);
619 if (!cpia->sbuf[0].data)
620 goto open_err_on0;
622 cpia->sbuf[1].data = kmalloc(STREAM_BUF_SIZE, GFP_KERNEL);
623 if (!cpia->sbuf[1].data)
624 goto open_err_on1;
626 cpia->sbuf[2].data = kmalloc(STREAM_BUF_SIZE, GFP_KERNEL);
627 if (!cpia->sbuf[2].data)
628 goto open_err_on2;
630 #ifdef CPIA_DEBUG
631 printk("sbuf[0] @ %p\n", cpia->sbuf[0].data);
632 printk("sbuf[1] @ %p\n", cpia->sbuf[1].data);
633 printk("sbuf[2] @ %p\n", cpia->sbuf[2].data);
634 #endif
636 cpia->curframe = -1;
637 cpia->receivesbuf = 0;
639 usb_cpia_initstreamcap(cpia->dev, 0, 60);
641 cpia_init_isoc(cpia);
643 return 0;
645 open_err_on2:
646 kfree (cpia->sbuf[1].data);
647 open_err_on1:
648 kfree (cpia->sbuf[0].data);
649 open_err_on0:
650 rvfree(cpia->fbuf, 2 * MAX_FRAME_SIZE);
651 open_err_ret:
652 return -ENOMEM;
655 static void cpia_close(struct video_device *dev)
657 struct usb_cpia *cpia = (struct usb_cpia *)dev;
659 #ifdef CPIA_DEBUG
660 printk("cpia_close\n");
661 #endif
663 cpia_stop_isoc(cpia);
665 usb_cpia_finistreamcap(cpia->dev);
667 rvfree(cpia->fbuf, 2 * MAX_FRAME_SIZE);
669 kfree(cpia->sbuf[2].data);
670 kfree(cpia->sbuf[1].data);
671 kfree(cpia->sbuf[0].data);
674 static int cpia_init_done(struct video_device *dev)
676 return 0;
679 static long cpia_write(struct video_device *dev, const char *buf, unsigned long count, int noblock)
681 return -EINVAL;
684 static int cpia_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
686 struct usb_cpia *cpia = (struct usb_cpia *)dev;
688 switch (cmd) {
689 case VIDIOCGCAP:
691 struct video_capability b;
693 strcpy(b.name, "CPiA USB Camera");
694 b.type = VID_TYPE_CAPTURE /* | VID_TYPE_SUBCAPTURE */;
695 b.channels = 1;
696 b.audios = 0;
697 b.maxwidth = 176 /* 352 */;
698 b.maxheight = 144 /* 240 */;
699 b.minwidth = 176 /* (Something small?) */;
700 b.minheight = 144 /* " " */;
702 if (copy_to_user(arg, &b, sizeof(b)))
703 return -EFAULT;
704 return 0;
706 case VIDIOCGCHAN:
708 struct video_channel v;
710 if (copy_from_user(&v, arg, sizeof(v)))
711 return -EFAULT;
712 if (v.channel != 0)
713 return -EINVAL;
715 v.flags = 0;
716 v.tuners = 0;
717 v.type = VIDEO_TYPE_CAMERA;
718 strcpy(v.name, "Camera");
720 if (copy_to_user(arg, &v, sizeof(v)))
721 return -EFAULT;
722 return 0;
724 case VIDIOCSCHAN:
726 int v;
728 if (copy_from_user(&v, arg, sizeof(v)))
729 return -EFAULT;
731 if (v != 0)
732 return -EINVAL;
734 return 0;
736 case VIDIOCGTUNER:
738 struct video_tuner v;
740 if (copy_from_user(&v, arg, sizeof(v)))
741 return -EFAULT;
743 if (v.tuner)
744 return -EINVAL;
746 strcpy(v.name, "Format");
748 v.rangelow = 0;
749 v.rangehigh = 0;
750 v.flags = 0;
751 v.mode = VIDEO_MODE_AUTO;
753 if (copy_to_user(arg, &v, sizeof(v)))
754 return -EFAULT;
756 return 0;
758 case VIDIOCSTUNER:
760 struct video_tuner v;
762 if (copy_from_user(&v, arg, sizeof(v)))
763 return -EFAULT;
765 if (v.tuner)
766 return -EINVAL;
768 if (v.mode != VIDEO_MODE_AUTO)
769 return -EINVAL;
771 return 0;
773 case VIDIOCGPICT:
775 struct video_picture p;
777 p.colour = 0x8000; /* Damn British people :) */
778 p.hue = 0x8000;
779 p.brightness = 180 << 8; /* XXX */
780 p.contrast = 192 << 8; /* XXX */
781 p.whiteness = 105 << 8; /* XXX */
782 #if 0
783 p.depth = 24;
784 #endif
785 p.depth = 16;
786 p.palette = VIDEO_PALETTE_YUYV;
788 if (copy_to_user(arg, &p, sizeof(p)))
789 return -EFAULT;
791 return 0;
793 case VIDIOCSPICT:
795 struct video_picture p;
797 if (copy_from_user(&p, arg, sizeof(p)))
798 return -EFAULT;
800 #ifdef CPIA_DEBUG
801 printk("Attempting to set palette %d, depth %d\n",
802 p.palette, p.depth);
803 #endif
805 return 0;
807 case VIDIOCSWIN:
809 struct video_window vw;
811 #ifdef CPIA_DEBUG
812 printk("VIDIOCSWIN\n");
813 #endif
815 if (copy_from_user(&vw, arg, sizeof(vw)))
816 return -EFAULT;
817 if (vw.flags)
818 return -EINVAL;
819 if (vw.clipcount)
820 return -EINVAL;
821 if (vw.height != 176)
822 return -EINVAL;
823 if (vw.width != 144)
824 return -EINVAL;
826 return 0;
828 case VIDIOCGWIN:
830 struct video_window vw;
832 #ifdef CPIA_DEBUG
833 printk("VIDIOCGWIN\n");
834 #endif
836 vw.x = 0;
837 vw.y = 0;
838 vw.width = 176;
839 vw.height = 144;
840 vw.chromakey = 0;
841 vw.flags = 0;
843 if (copy_to_user(arg, &vw, sizeof(vw)))
844 return -EFAULT;
846 return 0;
848 case VIDIOCGMBUF:
850 struct video_mbuf vm;
852 memset(&vm, 0, sizeof(vm));
853 vm.size = MAX_FRAME_SIZE * 2;
854 vm.frames = 2;
855 vm.offsets[0] = 0;
856 vm.offsets[1] = MAX_FRAME_SIZE;
858 if (copy_to_user((void *)arg, (void *)&vm, sizeof(vm)))
859 return -EFAULT;
861 return 0;
863 case VIDIOCMCAPTURE:
865 struct video_mmap vm;
867 if (copy_from_user((void *)&vm, (void *)arg, sizeof(vm)))
868 return -EFAULT;
870 #ifdef CPIA_DEBUG
871 printk("MCAPTURE\n");
872 printk("frame: %d, size: %dx%d, format: %d\n",
873 vm.frame, vm.width, vm.height, vm.format);
874 #endif
876 if (vm.format != VIDEO_PALETTE_RGB24)
877 return -EINVAL;
879 if ((vm.frame != 0) && (vm.frame != 1))
880 return -EINVAL;
882 cpia->frame[vm.frame].width = vm.width;
883 cpia->frame[vm.frame].height = vm.height;
885 /* Mark it as free */
886 cpia->frame[vm.frame].state = FRAME_READY;
888 return 0;
890 case VIDIOCSYNC:
892 int frame;
894 if (copy_from_user((void *)&frame, arg, sizeof(int)))
895 return -EFAULT;
897 #ifdef CPIA_DEBUG
898 printk("syncing to frame %d\n", frame);
899 #endif
900 switch (cpia->frame[frame].state) {
901 case FRAME_UNUSED:
902 return -EINVAL;
903 case FRAME_READY:
904 case FRAME_GRABBING:
905 interruptible_sleep_on(&cpia->wq);
906 case FRAME_DONE:
907 cpia->frame[frame].state = FRAME_UNUSED;
908 break;
910 #ifdef CPIA_DEBUG
911 printk("synced to frame %d\n", frame);
912 #endif
913 return 0;
915 case VIDIOCCAPTURE:
916 return -EINVAL;
917 case VIDIOCGFBUF:
918 return -EINVAL;
919 case VIDIOCSFBUF:
920 return -EINVAL;
921 case VIDIOCKEY:
922 return 0;
923 case VIDIOCGFREQ:
924 return -EINVAL;
925 case VIDIOCSFREQ:
926 return -EINVAL;
927 case VIDIOCGAUDIO:
928 return -EINVAL;
929 case VIDIOCSAUDIO:
930 return -EINVAL;
931 default:
932 return -ENOIOCTLCMD;
934 return 0;
937 static long cpia_read(struct video_device *dev, char *buf, unsigned long count, int noblock)
939 struct usb_cpia *cpia = (struct usb_cpia *)dev;
940 int len;
942 #ifdef CPIA_DEBUG
943 printk("cpia_read: %ld bytes\n", count);
944 #endif
945 #if 0
946 len = cpia_capture(cpia, buf, count);
948 return len;
949 #endif
950 return 0;
953 static int cpia_mmap(struct video_device *dev, const char *adr, unsigned long size)
955 struct usb_cpia *cpia = (struct usb_cpia *)dev;
956 unsigned long start = (unsigned long)adr;
957 unsigned long page, pos;
959 #ifdef CPIA_DEBUG
960 printk("mmap: %ld (%lX) bytes\n", size, size);
961 #endif
962 if (size > (((2 * MAX_FRAME_SIZE) + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1)))
963 return -EINVAL;
965 pos = (unsigned long)cpia->fbuf;
966 while (size > 0)
968 page = kvirt_to_phys(pos);
969 if (remap_page_range(start, page, PAGE_SIZE, PAGE_SHARED))
970 return -EAGAIN;
971 start+=PAGE_SIZE;
972 pos+=PAGE_SIZE;
973 if (size > PAGE_SIZE)
974 size-=PAGE_SIZE;
975 else
976 size=0;
979 return 0;
982 static struct video_device cpia_template = {
983 "CPiA USB Camera",
984 VID_TYPE_CAPTURE,
985 VID_HARDWARE_CPIA,
986 cpia_open,
987 cpia_close,
988 cpia_read,
989 cpia_write,
990 NULL,
991 cpia_ioctl,
992 cpia_mmap,
993 cpia_init_done,
994 NULL,
999 static void usb_cpia_configure(struct usb_cpia *cpia)
1001 struct usb_device *dev = cpia->dev;
1002 unsigned char version[4];
1003 unsigned char pnpid[6];
1004 unsigned char camerastat[8];
1005 unsigned char *buf;
1007 if (usb_set_configuration(dev, dev->config[0].bConfigurationValue)) {
1008 printk (KERN_INFO " Failed usb_set_configuration: CPIA\n");
1009 return;
1012 if (usb_cpia_get_version(dev, version)) {
1013 printk("cpia_get_version error\n");
1014 return;
1017 printk("cpia: Firmware v%d.%d, VC Hardware v%d.%d\n",
1018 version[0], version[1], version[2], version[3]);
1020 if (usb_cpia_get_pnp_id(dev, pnpid)) {
1021 printk("cpia_get_pnp_id error\n");
1022 return;
1025 printk("cpia: PnP Id: Vendor: %X, Product: %X, Revision: %X\n",
1026 (pnpid[1] << 8) + pnpid[0], (pnpid[3] << 8) + pnpid[2],
1027 (pnpid[5] << 8) + pnpid[4]);
1029 memcpy(&cpia->vdev, &cpia_template, sizeof(cpia_template));
1031 init_waitqueue_head(&cpia->wq);
1033 if (video_register_device(&cpia->vdev, VFL_TYPE_GRABBER) == -1) {
1034 printk("video_register_device failed\n");
1035 return;
1038 if (usb_cpia_goto_hi_power(dev)) {
1039 printk("cpia_goto_hi_power error\n");
1040 return;
1043 if (usb_cpia_get_vp_version(dev, version)) {
1044 printk("cpia_get_vp_version error\n");
1045 return;
1048 printk("cpia: VP v%d rev %d\n", version[0], version[1]);
1049 printk("cpia: Camera Head ID %04X\n", (version[3] << 8) + version[2]);
1051 /* Turn off continuous grab */
1052 if (usb_cpia_set_grab_mode(dev, 1)) {
1053 printk("cpia_set_grab_mode error\n");
1054 return;
1057 /* Set up the sensor to be 30fps */
1058 if (usb_cpia_set_sensor_fps(dev, 1, 0)) {
1059 printk("cpia_set_sensor_fps error\n");
1060 return;
1063 /* Set video into QCIF mode, and order into YUYV mode */
1064 if (usb_cpia_set_format(dev, CPIA_QCIF, 1, CPIA_YUYV)) {
1065 printk("cpia_set_format error\n");
1066 return;
1069 /* Turn off compression */
1070 if (usb_cpia_set_compression(dev, 0, 0)) {
1071 printk("cpia_set_compression error\n");
1072 return;
1076 static int cpia_probe(struct usb_device *dev)
1078 struct usb_interface_descriptor *interface;
1079 struct usb_endpoint_descriptor *endpoint;
1080 struct usb_cpia *cpia;
1082 /* We don't handle multi-config cameras */
1083 if (dev->descriptor.bNumConfigurations != 1)
1084 return -1;
1086 interface = &dev->config[0].interface[0].altsetting[0];
1088 /* Is it a CPiA? */
1089 if (dev->descriptor.idVendor != 0x0553)
1090 return -1;
1091 if (dev->descriptor.idProduct != 0x0002)
1092 return -1;
1093 if (interface->bInterfaceClass != 0xFF)
1094 return -1;
1095 if (interface->bInterfaceSubClass != 0x00)
1096 return -1;
1098 /* We found a CPiA */
1099 printk("USB CPiA camera found\n");
1101 if ((cpia = kmalloc(sizeof(*cpia), GFP_KERNEL)) == NULL) {
1102 printk("couldn't kmalloc cpia struct\n");
1103 return -1;
1106 memset(cpia, 0, sizeof(*cpia));
1108 dev->private = cpia;
1109 cpia->dev = dev;
1111 usb_cpia_configure(cpia);
1113 return 0;
1116 static void cpia_disconnect(struct usb_device *dev)
1118 struct usb_cpia *cpia = dev->private;
1120 video_unregister_device(&cpia->vdev);
1122 /* Free the memory */
1123 kfree(cpia);
1126 static struct usb_driver cpia_driver = {
1127 "cpia",
1128 cpia_probe,
1129 cpia_disconnect,
1130 { NULL, NULL }
1134 * This should be a separate module.
1136 int usb_cpia_init(void)
1138 usb_register(&cpia_driver);
1140 return 0;
1143 #ifdef MODULE
1144 int init_module(void)
1146 return usb_cpia_init();
1149 void cleanup_module(void)
1152 #endif