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
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>
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>
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
)
46 pgd
= pgd_offset(current
->mm
, adr
);
49 pmd
= pmd_offset(pgd
, adr
);
52 ptep
= pte_offset(pmd
, adr
/*&(~PGDIR_MASK)*/);
56 virt_to_phys((void *)(pte_page(pte
)|(adr
&(PAGE_SIZE
-1))));
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
)
82 unsigned long adr
, page
;
84 size
+= (PAGE_SIZE
- 1);
85 size
&= ~(PAGE_SIZE
- 1);
90 memset(mem
, 0, size
); /* Clear the ram out, no junk to the user */
91 adr
=(unsigned long) mem
;
94 page
= kvirt_to_phys(adr
);
95 mem_map_reserve(MAP_NR(phys_to_virt(page
)));
106 static void rvfree(void * mem
, unsigned long size
)
108 unsigned long adr
, page
;
110 size
+= (PAGE_SIZE
- 1);
111 size
&= ~(PAGE_SIZE
- 1);
115 adr
=(unsigned long) mem
;
118 page
= kvirt_to_phys(adr
);
119 mem_map_unreserve(MAP_NR(phys_to_virt(page
)));
121 if (size
> PAGE_SIZE
)
130 int usb_cpia_get_version(struct usb_device
*dev
, void *buf
)
134 dr
.requesttype
= (USB_TYPE_VENDOR
| USB_RECIP_DEVICE
) | 0x80;
135 dr
.request
= USB_REQ_CPIA_GET_VERSION
;
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
)
147 dr
.requesttype
= (USB_TYPE_VENDOR
| USB_RECIP_DEVICE
) | 0x80;
148 dr
.request
= USB_REQ_CPIA_GET_PNP_ID
;
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
)
160 dr
.requesttype
= (USB_TYPE_VENDOR
| USB_RECIP_DEVICE
) | 0x80;
161 dr
.request
= USB_REQ_CPIA_GET_CAMERA_STATUS
;
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
)
173 dr
.requesttype
= (USB_TYPE_VENDOR
| USB_RECIP_DEVICE
);
174 dr
.request
= USB_REQ_CPIA_GOTO_HI_POWER
;
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
)
186 dr
.requesttype
= (USB_TYPE_VENDOR
| USB_RECIP_DEVICE
);
187 dr
.request
= USB_REQ_CPIA_GET_VP_VERSION
;
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
)
199 dr
.requesttype
= (USB_TYPE_VENDOR
| USB_RECIP_DEVICE
);
200 dr
.request
= USB_REQ_CPIA_SET_SENSOR_FPS
;
201 dr
.value
= (sensorclkdivisor
<< 8) + sensorbaserate
;
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
)
212 dr
.requesttype
= (USB_TYPE_VENDOR
| USB_RECIP_DEVICE
);
213 dr
.request
= USB_REQ_CPIA_GRAB_FRAME
;
214 dr
.value
= streamstartline
<< 8;
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
)
225 dr
.requesttype
= (USB_TYPE_VENDOR
| USB_RECIP_DEVICE
);
226 dr
.request
= USB_REQ_CPIA_UPLOAD_FRAME
;
227 dr
.value
= forceupload
;
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
)
238 dr
.requesttype
= (USB_TYPE_VENDOR
| USB_RECIP_DEVICE
);
239 dr
.request
= USB_REQ_CPIA_SET_GRAB_MODE
;
240 dr
.value
= continuousgrab
;
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
)
251 dr
.requesttype
= (USB_TYPE_VENDOR
| USB_RECIP_DEVICE
);
252 dr
.request
= USB_REQ_CPIA_SET_FORMAT
;
253 dr
.value
= (subsample
<< 8) + size
;
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
)
264 dr
.requesttype
= (USB_TYPE_VENDOR
| USB_RECIP_DEVICE
);
265 dr
.request
= USB_REQ_CPIA_SET_COMPRESSION
;
266 dr
.value
= (decimation
<< 8) + compmode
;
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
)
277 dr
.requesttype
= (USB_TYPE_VENDOR
| USB_RECIP_DEVICE
);
278 dr
.request
= USB_REQ_CPIA_INIT_STREAM_CAP
;
279 dr
.value
= (streamstartline
<< 8) + skipframes
;
283 return dev
->bus
->op
->control_msg(dev
, usb_sndctrlpipe(dev
,0), &dr
, NULL
, 0);
286 int usb_cpia_finistreamcap(struct usb_device
*dev
)
290 dr
.requesttype
= (USB_TYPE_VENDOR
| USB_RECIP_DEVICE
);
291 dr
.request
= USB_REQ_CPIA_FINI_STREAM_CAP
;
296 return dev
->bus
->op
->control_msg(dev
, usb_sndctrlpipe(dev
,0), &dr
, NULL
, 0);
299 int usb_cpia_startstreamcap(struct usb_device
*dev
)
303 dr
.requesttype
= (USB_TYPE_VENDOR
| USB_RECIP_DEVICE
);
304 dr
.request
= USB_REQ_CPIA_START_STREAM_CAP
;
309 return dev
->bus
->op
->control_msg(dev
, usb_sndctrlpipe(dev
,0), &dr
, NULL
, 0);
312 int usb_cpia_endstreamcap(struct usb_device
*dev
)
316 dr
.requesttype
= (USB_TYPE_VENDOR
| USB_RECIP_DEVICE
);
317 dr
.request
= USB_REQ_CPIA_END_STREAM_CAP
;
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
;
335 while (!done
&& scratch_left(data
)) {
336 switch (cpia
->state
) {
339 unsigned char *begin
= data
;
341 /* We need atleast 2 bytes for the magic value */
342 if (scratch_left(data
) < 2) {
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");
355 /* Woops, lost the header, find the end of the frame */
356 if (scratch_left(data
) < 4) {
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)) {
373 printk("scan: scanned %d bytes\n", data
-begin
);
378 /* We need atleast 64 bytes for the header */
379 if (scratch_left(data
) < 64) {
385 printk("header: framerate %d\n", data
[41]);
390 cpia
->state
= STATE_LINES
;
395 unsigned char *begin
= data
;
398 while (scratch_left(data
)) {
403 } else if ((*data
== 0xFF) &&
404 (scratch_left(data
) >= 3) &&
405 (*(data
+ 1) == 0xFF) &&
406 (*(data
+ 2) == 0xFF) &&
407 (*(data
+ 3) == 0xFF)) {
417 if (data
-begin
== 355 && cpia
->frame
[cpia
->curframe
].width
!= 64) {
419 char *f
= cpia
->frame
[cpia
->curframe
].data
, *b
= begin
;
422 f
+= (cpia
->frame
[cpia
->curframe
].width
* 3) * cpia
->curline
;
424 for (i
= 0; i
< 176; i
++)
433 if (cpia
->curline
>= 144) {
435 cpia
->state
= STATE_SCANNING
;
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
;
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
;
473 printk(KERN_DEBUG
"cpia data error: [%d] len=%d, status=%X\n",
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
;
490 if (!cpia
->streaming
) {
491 printk("oops, not streaming, but interrupt\n");
495 if (cpia
->curframe
< 0) {
496 if (cpia
->frame
[0].state
== FRAME_READY
) {
498 cpia
->frame
[0].state
= FRAME_GRABBING
;
500 printk("capturing to frame 0\n");
502 } else if (cpia
->frame
[1].state
== FRAME_READY
) {
504 cpia
->frame
[1].state
= FRAME_GRABBING
;
506 printk("capturing to frame 1\n");
510 printk("no frame available\n");
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
);
525 printk("%d bytes received\n", sbuf
->len
);
528 if (sbuf
->len
&& cpia
->curframe
>= 0) {
529 if (sbuf
->len
> (SCRATCH_BUF_SIZE
- cpia
->scratchlen
)) {
530 printk("overflow!\n");
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;
553 int cpia_init_isoc(struct usb_cpia
*cpia
)
555 struct usb_device
*dev
= cpia
->dev
;
556 struct usb_isoc_desc
*id
;
559 cpia
->receivesbuf
= 0;
561 cpia
->scratchlen
= 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
);
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
);
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
);
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
);
589 printk("isodesc[0] @ %p\n", cpia
->sbuf
[0].isodesc
);
590 printk("isodesc[1] @ %p\n", cpia
->sbuf
[1].isodesc
);
592 printk("isodesc[2] @ %p\n", cpia
->sbuf
[2].isodesc
);
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
);
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
);
627 printk("done scheduling\n");
629 if (usb_set_interface(cpia
->dev
, 1, 3)) {
630 printk("cpia_set_interface error\n");
634 usb_cpia_startstreamcap(cpia
->dev
);
638 printk("now streaming\n");
644 void cpia_stop_isoc(struct usb_cpia
*cpia
)
646 struct usb_device
*dev
= cpia
->dev
;
648 if (!cpia
->streaming
)
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
);
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
);
671 /* Delete them all */
672 usb_free_isoc (cpia
->sbuf
[1].isodesc
);
673 usb_free_isoc (cpia
->sbuf
[0].isodesc
);
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
);
681 /* Video 4 Linux API */
682 static int cpia_open(struct video_device
*dev
, int flags
)
685 struct usb_cpia
*cpia
= (struct usb_cpia
*)dev
;
688 printk("cpia_open\n");
691 cpia
->fbuf
= rvmalloc(2 * MAX_FRAME_SIZE
);
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
;
701 printk("frame [0] @ %p\n", cpia
->frame
[0].data
);
702 printk("frame [1] @ %p\n", cpia
->frame
[1].data
);
705 cpia
->sbuf
[0].data
= kmalloc (FRAMES_PER_DESC
* FRAME_SIZE_PER_DESC
, GFP_KERNEL
);
706 if (!cpia
->sbuf
[0].data
)
709 cpia
->sbuf
[1].data
= kmalloc (FRAMES_PER_DESC
* FRAME_SIZE_PER_DESC
, GFP_KERNEL
);
710 if (!cpia
->sbuf
[1].data
)
714 cpia
->sbuf
[0].data
= kmalloc(STREAM_BUF_SIZE
, GFP_KERNEL
);
715 if (!cpia
->sbuf
[0].data
)
718 cpia
->sbuf
[1].data
= kmalloc(STREAM_BUF_SIZE
, GFP_KERNEL
);
719 if (!cpia
->sbuf
[1].data
)
722 cpia
->sbuf
[2].data
= kmalloc(STREAM_BUF_SIZE
, GFP_KERNEL
);
723 if (!cpia
->sbuf
[2].data
)
728 printk("sbuf[0] @ %p\n", cpia
->sbuf
[0].data
);
729 printk("sbuf[1] @ %p\n", cpia
->sbuf
[1].data
);
731 printk("sbuf[2] @ %p\n", cpia
->sbuf
[2].data
);
736 cpia
->receivesbuf
= 0;
738 usb_cpia_initstreamcap(cpia
->dev
, 0, 60);
740 err
= cpia_init_isoc(cpia
);
747 kfree (cpia
->sbuf
[1].data
);
749 kfree (cpia
->sbuf
[0].data
);
751 rvfree(cpia
->fbuf
, 2 * MAX_FRAME_SIZE
);
756 static void cpia_close(struct video_device
*dev
)
758 struct usb_cpia
*cpia
= (struct usb_cpia
*)dev
;
761 printk("cpia_close\n");
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
)
780 static long cpia_write(struct video_device
*dev
, const char *buf
, unsigned long count
, int noblock
)
785 static int cpia_ioctl(struct video_device
*dev
, unsigned int cmd
, void *arg
)
787 struct usb_cpia
*cpia
= (struct usb_cpia
*)dev
;
792 struct video_capability b
;
794 strcpy(b
.name
, "CPiA USB Camera");
795 b
.type
= VID_TYPE_CAPTURE
/* | VID_TYPE_SUBCAPTURE */;
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
)))
809 struct video_channel v
;
811 if (copy_from_user(&v
, arg
, sizeof(v
)))
818 v
.type
= VIDEO_TYPE_CAMERA
;
819 strcpy(v
.name
, "Camera");
821 if (copy_to_user(arg
, &v
, sizeof(v
)))
829 if (copy_from_user(&v
, arg
, sizeof(v
)))
839 struct video_tuner v
;
841 if (copy_from_user(&v
, arg
, sizeof(v
)))
847 strcpy(v
.name
, "Format");
852 v
.mode
= VIDEO_MODE_AUTO
;
854 if (copy_to_user(arg
, &v
, sizeof(v
)))
861 struct video_tuner v
;
863 if (copy_from_user(&v
, arg
, sizeof(v
)))
869 if (v
.mode
!= VIDEO_MODE_AUTO
)
876 struct video_picture p
;
878 p
.colour
= 0x8000; /* Damn British people :) */
880 p
.brightness
= 180 << 8; /* XXX */
881 p
.contrast
= 192 << 8; /* XXX */
882 p
.whiteness
= 105 << 8; /* XXX */
887 p
.palette
= VIDEO_PALETTE_YUYV
;
889 if (copy_to_user(arg
, &p
, sizeof(p
)))
896 struct video_picture p
;
898 if (copy_from_user(&p
, arg
, sizeof(p
)))
902 printk("Attempting to set palette %d, depth %d\n",
910 struct video_window vw
;
913 printk("VIDIOCSWIN\n");
916 if (copy_from_user(&vw
, arg
, sizeof(vw
)))
922 if (vw
.height
!= 176)
931 struct video_window vw
;
934 printk("VIDIOCGWIN\n");
944 if (copy_to_user(arg
, &vw
, sizeof(vw
)))
951 struct video_mbuf vm
;
953 memset(&vm
, 0, sizeof(vm
));
954 vm
.size
= MAX_FRAME_SIZE
* 2;
957 vm
.offsets
[1] = MAX_FRAME_SIZE
;
959 if (copy_to_user((void *)arg
, (void *)&vm
, sizeof(vm
)))
966 struct video_mmap vm
;
968 if (copy_from_user((void *)&vm
, (void *)arg
, sizeof(vm
)))
972 printk("MCAPTURE\n");
973 printk("frame: %d, size: %dx%d, format: %d\n",
974 vm
.frame
, vm
.width
, vm
.height
, vm
.format
);
977 if (vm
.format
!= VIDEO_PALETTE_RGB24
)
980 if ((vm
.frame
!= 0) && (vm
.frame
!= 1))
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
;
995 if (copy_from_user((void *)&frame
, arg
, sizeof(int)))
999 printk("syncing to frame %d\n", frame
);
1001 switch (cpia
->frame
[frame
].state
) {
1005 case FRAME_GRABBING
:
1006 interruptible_sleep_on(&cpia
->wq
);
1008 cpia
->frame
[frame
].state
= FRAME_UNUSED
;
1012 printk("synced to frame %d\n", frame
);
1033 return -ENOIOCTLCMD
;
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
;
1044 printk("cpia_read: %ld bytes\n", count
);
1047 len
= cpia_capture(cpia
, buf
, count
);
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
;
1061 printk("mmap: %ld (%lX) bytes\n", size
, size
);
1063 if (size
> (((2 * MAX_FRAME_SIZE
) + PAGE_SIZE
- 1) & ~(PAGE_SIZE
- 1)))
1066 pos
= (unsigned long)cpia
->fbuf
;
1069 page
= kvirt_to_phys(pos
);
1070 if (remap_page_range(start
, page
, PAGE_SIZE
, PAGE_SHARED
))
1074 if (size
> PAGE_SIZE
)
1083 static struct video_device cpia_template
= {
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];
1108 if (usb_set_configuration(dev
, dev
->config
[0].bConfigurationValue
)) {
1109 printk (KERN_INFO
" Failed usb_set_configuration: CPIA\n");
1113 if (usb_cpia_get_version(dev
, version
)) {
1114 printk("cpia_get_version error\n");
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");
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");
1139 if (usb_cpia_goto_hi_power(dev
)) {
1140 printk("cpia_goto_hi_power error\n");
1144 if (usb_cpia_get_vp_version(dev
, version
)) {
1145 printk("cpia_get_vp_version error\n");
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");
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");
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");
1170 /* Turn off compression */
1171 if (usb_cpia_set_compression(dev
, 0, 0)) {
1172 printk("cpia_set_compression error\n");
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)
1187 interface
= &dev
->config
[0].interface
[0].altsetting
[0];
1190 if (dev
->descriptor
.idVendor
!= 0x0553)
1192 if (dev
->descriptor
.idProduct
!= 0x0002)
1194 if (interface
->bInterfaceClass
!= 0xFF)
1196 if (interface
->bInterfaceSubClass
!= 0x00)
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");
1207 memset(cpia
, 0, sizeof(*cpia
));
1209 dev
->private = cpia
;
1212 usb_cpia_configure(cpia
);
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 */
1227 static struct usb_driver cpia_driver
= {
1235 * This should be a separate module.
1237 int usb_cpia_init(void)
1239 usb_register(&cpia_driver
);
1245 int init_module(void)
1247 return usb_cpia_init();
1250 void cleanup_module(void)