2 * USB ViCam WebCam driver
3 * Copyright (c) 2002 Joe Burks (jburks@wavicle.org),
4 * Christopher L Cheney (ccheney@cheney.cx),
5 * Pavel Machek (pavel@suse.cz),
6 * John Tyner (jtyner@cs.ucr.edu),
7 * Monroe Williams (monroe@pobox.com)
9 * Supports 3COM HomeConnect PC Digital WebCam
10 * Supports Compro PS39U WebCam
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 * This source code is based heavily on the CPiA webcam driver which was
27 * written by Peter Pregler, Scott J. Bertin and Johannes Erdfelt
29 * Portions of this code were also copied from usbvideo.c
31 * Special thanks to the whole team at Sourceforge for help making
32 * this driver become a reality. Notably:
33 * Andy Armstrong who reverse engineered the color encoding and
34 * Pavel Machek and Chris Cheney who worked on reverse engineering the
35 * camera controls and wrote the first generation driver.
38 #include <linux/kernel.h>
39 #include <linux/module.h>
40 #include <linux/init.h>
41 #include <linux/videodev.h>
42 #include <linux/usb.h>
43 #include <linux/vmalloc.h>
44 #include <linux/slab.h>
45 #include <linux/mutex.h>
46 #include <linux/firmware.h>
47 #include <linux/ihex.h>
50 // #define VICAM_DEBUG
53 #define ADBG(lineno,fmt,args...) printk(fmt, jiffies, __func__, lineno, ##args)
54 #define DBG(fmt,args...) ADBG((__LINE__),KERN_DEBUG __FILE__"(%ld):%s (%d):"fmt,##args)
56 #define DBG(fmn,args...) do {} while(0)
59 #define DRIVER_AUTHOR "Joe Burks, jburks@wavicle.org"
60 #define DRIVER_DESC "ViCam WebCam Driver"
62 /* Define these values to match your device */
63 #define USB_VICAM_VENDOR_ID 0x04c1
64 #define USB_VICAM_PRODUCT_ID 0x009d
65 #define USB_COMPRO_VENDOR_ID 0x0602
66 #define USB_COMPRO_PRODUCT_ID 0x1001
68 #define VICAM_BYTES_PER_PIXEL 3
69 #define VICAM_MAX_READ_SIZE (512*242+128)
70 #define VICAM_MAX_FRAME_SIZE (VICAM_BYTES_PER_PIXEL*320*240)
71 #define VICAM_FRAMES 2
73 #define VICAM_HEADER_SIZE 64
75 /* rvmalloc / rvfree copied from usbvideo.c
77 * Not sure why these are not yet non-statics which I can reference through
78 * usbvideo.h the same as it is in 2.4.20. I bet this will get fixed sometime
82 static void *rvmalloc(unsigned long size
)
87 size
= PAGE_ALIGN(size
);
88 mem
= vmalloc_32(size
);
92 memset(mem
, 0, size
); /* Clear the ram out, no junk to the user */
93 adr
= (unsigned long) mem
;
95 SetPageReserved(vmalloc_to_page((void *)adr
));
103 static void rvfree(void *mem
, unsigned long size
)
110 adr
= (unsigned long) mem
;
111 while ((long) size
> 0) {
112 ClearPageReserved(vmalloc_to_page((void *)adr
));
119 struct vicam_camera
{
120 u16 shutter_speed
; // capture shutter speed
121 u16 gain
; // capture gain
123 u8
*raw_image
; // raw data captured from the camera
124 u8
*framebuf
; // processed data in RGB24 format
125 u8
*cntrlbuf
; // area used to send control msgs
127 struct video_device vdev
; // v4l video device
128 struct usb_device
*udev
; // usb device
130 /* guard against simultaneous accesses to the camera */
131 struct mutex cam_lock
;
139 static int vicam_probe( struct usb_interface
*intf
, const struct usb_device_id
*id
);
140 static void vicam_disconnect(struct usb_interface
*intf
);
141 static void read_frame(struct vicam_camera
*cam
, int framenum
);
142 static void vicam_decode_color(const u8
*, u8
*);
144 static int __send_control_msg(struct vicam_camera
*cam
,
153 /* cp must be memory that has been allocated by kmalloc */
155 status
= usb_control_msg(cam
->udev
,
156 usb_sndctrlpipe(cam
->udev
, 0),
158 USB_DIR_OUT
| USB_TYPE_VENDOR
|
159 USB_RECIP_DEVICE
, value
, index
,
162 status
= min(status
, 0);
165 printk(KERN_INFO
"Failed sending control message, error %d.\n",
172 static int send_control_msg(struct vicam_camera
*cam
,
179 int status
= -ENODEV
;
180 mutex_lock(&cam
->cam_lock
);
182 status
= __send_control_msg(cam
, request
, value
,
185 mutex_unlock(&cam
->cam_lock
);
189 initialize_camera(struct vicam_camera
*cam
)
192 const struct ihex_binrec
*rec
;
193 const struct firmware
*fw
;
195 err
= request_ihex_firmware(&fw
, "vicam/firmware.fw", &cam
->udev
->dev
);
197 printk(KERN_ERR
"Failed to load \"vicam/firmware.fw\": %d\n",
202 for (rec
= (void *)fw
->data
; rec
; rec
= ihex_next_binrec(rec
)) {
203 memcpy(cam
->cntrlbuf
, rec
->data
, be16_to_cpu(rec
->len
));
205 err
= send_control_msg(cam
, 0xff, 0, 0,
206 cam
->cntrlbuf
, be16_to_cpu(rec
->len
));
211 release_firmware(fw
);
217 set_camera_power(struct vicam_camera
*cam
, int state
)
221 if ((status
= send_control_msg(cam
, 0x50, state
, 0, NULL
, 0)) < 0)
225 send_control_msg(cam
, 0x55, 1, 0, NULL
, 0);
232 vicam_ioctl(struct inode
*inode
, struct file
*file
, unsigned int ioctlnr
, unsigned long arg
)
234 void __user
*user_arg
= (void __user
*)arg
;
235 struct vicam_camera
*cam
= file
->private_data
;
242 /* query capabilities */
245 struct video_capability b
;
248 memset(&b
, 0, sizeof(b
));
249 strcpy(b
.name
, "ViCam-based Camera");
250 b
.type
= VID_TYPE_CAPTURE
;
253 b
.maxwidth
= 320; /* VIDEOSIZE_CIF */
255 b
.minwidth
= 320; /* VIDEOSIZE_48_48 */
258 if (copy_to_user(user_arg
, &b
, sizeof(b
)))
263 /* get/set video source - we are a camera and nothing else */
266 struct video_channel v
;
268 DBG("VIDIOCGCHAN\n");
269 if (copy_from_user(&v
, user_arg
, sizeof(v
))) {
273 if (v
.channel
!= 0) {
279 strcpy(v
.name
, "Camera");
282 v
.type
= VIDEO_TYPE_CAMERA
;
285 if (copy_to_user(user_arg
, &v
, sizeof(v
)))
294 if (copy_from_user(&v
, user_arg
, sizeof(v
)))
296 DBG("VIDIOCSCHAN %d\n", v
);
298 if (retval
== 0 && v
!= 0)
304 /* image properties */
307 struct video_picture vp
;
308 DBG("VIDIOCGPICT\n");
309 memset(&vp
, 0, sizeof (struct video_picture
));
310 vp
.brightness
= cam
->gain
<< 8;
312 vp
.palette
= VIDEO_PALETTE_RGB24
;
313 if (copy_to_user(user_arg
, &vp
, sizeof (struct video_picture
)))
320 struct video_picture vp
;
322 if (copy_from_user(&vp
, user_arg
, sizeof(vp
))) {
327 DBG("VIDIOCSPICT depth = %d, pal = %d\n", vp
.depth
,
330 cam
->gain
= vp
.brightness
>> 8;
333 || vp
.palette
!= VIDEO_PALETTE_RGB24
)
339 /* get/set capture window */
342 struct video_window vw
;
354 if (copy_to_user(user_arg
, (void *)&vw
, sizeof(vw
)))
357 // I'm not sure what the deal with a capture window is, it is very poorly described
358 // in the doc. So I won't support it now.
365 struct video_window vw
;
367 if (copy_from_user(&vw
, user_arg
, sizeof(vw
))) {
372 DBG("VIDIOCSWIN %d x %d\n", vw
.width
, vw
.height
);
374 if ( vw
.width
!= 320 || vw
.height
!= 240 )
383 struct video_mbuf vm
;
386 DBG("VIDIOCGMBUF\n");
387 memset(&vm
, 0, sizeof (vm
));
389 VICAM_MAX_FRAME_SIZE
* VICAM_FRAMES
;
390 vm
.frames
= VICAM_FRAMES
;
391 for (i
= 0; i
< VICAM_FRAMES
; i
++)
392 vm
.offsets
[i
] = VICAM_MAX_FRAME_SIZE
* i
;
394 if (copy_to_user(user_arg
, (void *)&vm
, sizeof(vm
)))
402 struct video_mmap vm
;
405 if (copy_from_user((void *)&vm
, user_arg
, sizeof(vm
))) {
410 DBG("VIDIOCMCAPTURE frame=%d, height=%d, width=%d, format=%d.\n",vm
.frame
,vm
.width
,vm
.height
,vm
.format
);
412 if ( vm
.frame
>= VICAM_FRAMES
|| vm
.format
!= VIDEO_PALETTE_RGB24
)
415 // in theory right here we'd start the image capturing
416 // (fill in a bulk urb and submit it asynchronously)
418 // Instead we're going to do a total hack job for now and
419 // retrieve the frame in VIDIOCSYNC
428 if (copy_from_user((void *)&frame
, user_arg
, sizeof(int))) {
432 DBG("VIDIOCSYNC: %d\n", frame
);
434 read_frame(cam
, frame
);
435 vicam_decode_color(cam
->raw_image
,
437 frame
* VICAM_MAX_FRAME_SIZE
);
442 /* pointless to implement overlay with this camera */
450 /* tuner interface - we have none */
458 /* audio interface - we have none */
464 retval
= -ENOIOCTLCMD
;
472 vicam_open(struct inode
*inode
, struct file
*file
)
474 struct video_device
*dev
= video_devdata(file
);
475 struct vicam_camera
*cam
=
476 (struct vicam_camera
*) dev
->priv
;
481 "vicam video_device improperly initialized");
485 /* the videodev_lock held above us protects us from
486 * simultaneous opens...for now. we probably shouldn't
487 * rely on this fact forever.
490 if (cam
->open_count
> 0) {
492 "vicam_open called on already opened camera");
496 cam
->raw_image
= kmalloc(VICAM_MAX_READ_SIZE
, GFP_KERNEL
);
497 if (!cam
->raw_image
) {
501 cam
->framebuf
= rvmalloc(VICAM_MAX_FRAME_SIZE
* VICAM_FRAMES
);
502 if (!cam
->framebuf
) {
503 kfree(cam
->raw_image
);
507 cam
->cntrlbuf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
508 if (!cam
->cntrlbuf
) {
509 kfree(cam
->raw_image
);
510 rvfree(cam
->framebuf
, VICAM_MAX_FRAME_SIZE
* VICAM_FRAMES
);
514 // First upload firmware, then turn the camera on
516 if (!cam
->is_initialized
) {
517 initialize_camera(cam
);
519 cam
->is_initialized
= 1;
522 set_camera_power(cam
, 1);
524 cam
->needsDummyRead
= 1;
527 file
->private_data
= cam
;
533 vicam_close(struct inode
*inode
, struct file
*file
)
535 struct vicam_camera
*cam
= file
->private_data
;
537 struct usb_device
*udev
;
541 /* it's not the end of the world if
542 * we fail to turn the camera off.
545 set_camera_power(cam
, 0);
547 kfree(cam
->raw_image
);
548 rvfree(cam
->framebuf
, VICAM_MAX_FRAME_SIZE
* VICAM_FRAMES
);
549 kfree(cam
->cntrlbuf
);
551 mutex_lock(&cam
->cam_lock
);
554 open_count
= cam
->open_count
;
557 mutex_unlock(&cam
->cam_lock
);
559 if (!open_count
&& !udev
) {
566 static void vicam_decode_color(const u8
*data
, u8
*rgb
)
568 /* vicam_decode_color - Convert from Vicam Y-Cr-Cb to RGB
569 * Copyright (C) 2002 Monroe Williams (monroe@pobox.com)
577 data
+= VICAM_HEADER_SIZE
;
579 for( i
= 0; i
< 240; i
++, data
+= 512 ) {
580 const int y
= ( i
* 242 ) / 240;
585 if ( y
== 242 - 1 ) {
592 for ( j
= 0; j
< 320; j
++, rgb
+= 3 ) {
593 const int x
= ( j
* 512 ) / 320;
594 const u8
* const src
= &data
[x
];
596 if ( x
== 512 - 1 ) {
600 Cr
= ( src
[prevX
] - src
[0] ) +
601 ( src
[nextX
] - src
[0] );
604 Cb
= ( src
[prevY
] - src
[prevX
+ prevY
] ) +
605 ( src
[prevY
] - src
[nextX
+ prevY
] ) +
606 ( src
[nextY
] - src
[prevX
+ nextY
] ) +
607 ( src
[nextY
] - src
[nextX
+ nextY
] );
610 Y
= 1160 * ( src
[0] + ( Cr
/ 2 ) - 16 );
618 if ( ( x
^ i
) & 1 ) {
623 rgb
[0] = clamp( ( ( Y
+ ( 2017 * Cb
) ) +
624 500 ) / 900, 0, 255 );
625 rgb
[1] = clamp( ( ( Y
- ( 392 * Cb
) -
627 500 ) / 1000, 0, 255 );
628 rgb
[2] = clamp( ( ( Y
+ ( 1594 * Cr
) ) +
629 500 ) / 1300, 0, 255 );
639 read_frame(struct vicam_camera
*cam
, int framenum
)
641 unsigned char *request
= cam
->cntrlbuf
;
646 if (cam
->needsDummyRead
) {
647 cam
->needsDummyRead
= 0;
648 read_frame(cam
, framenum
);
651 memset(request
, 0, 16);
652 request
[0] = cam
->gain
; // 0 = 0% gain, FF = 100% gain
654 request
[1] = 0; // 512x242 capture
656 request
[2] = 0x90; // the function of these two bytes
657 request
[3] = 0x07; // is not yet understood
659 if (cam
->shutter_speed
> 60) {
662 ((-15631900 / cam
->shutter_speed
) + 260533) / 1000;
663 request
[4] = realShutter
& 0xFF;
664 request
[5] = (realShutter
>> 8) & 0xFF;
669 realShutter
= 15600 / cam
->shutter_speed
- 1;
672 request
[6] = realShutter
& 0xFF;
673 request
[7] = realShutter
>> 8;
676 // Per John Markus Bjørndalen, byte at index 8 causes problems if it isn't 0
678 // bytes 9-15 do not seem to affect exposure or image quality
680 mutex_lock(&cam
->cam_lock
);
686 n
= __send_control_msg(cam
, 0x51, 0x80, 0, request
, 16);
690 " Problem sending frame capture control message");
694 n
= usb_bulk_msg(cam
->udev
,
695 usb_rcvbulkpipe(cam
->udev
, cam
->bulkEndpoint
),
697 512 * 242 + 128, &actual_length
, 10000);
700 printk(KERN_ERR
"Problem during bulk read of frame data: %d\n",
705 mutex_unlock(&cam
->cam_lock
);
709 vicam_read( struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
711 struct vicam_camera
*cam
= file
->private_data
;
713 DBG("read %d bytes.\n", (int) count
);
715 if (*ppos
>= VICAM_MAX_FRAME_SIZE
) {
722 vicam_decode_color(cam
->raw_image
,
724 0 * VICAM_MAX_FRAME_SIZE
);
727 count
= min_t(size_t, count
, VICAM_MAX_FRAME_SIZE
- *ppos
);
729 if (copy_to_user(buf
, &cam
->framebuf
[*ppos
], count
)) {
735 if (count
== VICAM_MAX_FRAME_SIZE
) {
744 vicam_mmap(struct file
*file
, struct vm_area_struct
*vma
)
746 // TODO: allocate the raw frame buffer if necessary
747 unsigned long page
, pos
;
748 unsigned long start
= vma
->vm_start
;
749 unsigned long size
= vma
->vm_end
-vma
->vm_start
;
750 struct vicam_camera
*cam
= file
->private_data
;
755 DBG("vicam_mmap: %ld\n", size
);
757 /* We let mmap allocate as much as it wants because Linux was adding 2048 bytes
758 * to the size the application requested for mmap and it was screwing apps up.
759 if (size > VICAM_FRAMES*VICAM_MAX_FRAME_SIZE)
763 pos
= (unsigned long)cam
->framebuf
;
765 page
= vmalloc_to_pfn((void *)pos
);
766 if (remap_pfn_range(vma
, start
, page
, PAGE_SIZE
, PAGE_SHARED
))
771 if (size
> PAGE_SIZE
)
780 static const struct file_operations vicam_fops
= {
781 .owner
= THIS_MODULE
,
783 .release
= vicam_close
,
786 .ioctl
= vicam_ioctl
,
788 .compat_ioctl
= v4l_compat_ioctl32
,
793 static struct video_device vicam_template
= {
794 .owner
= THIS_MODULE
,
795 .name
= "ViCam-based USB Camera",
796 .type
= VID_TYPE_CAPTURE
,
801 /* table of devices that work with this driver */
802 static struct usb_device_id vicam_table
[] = {
803 {USB_DEVICE(USB_VICAM_VENDOR_ID
, USB_VICAM_PRODUCT_ID
)},
804 {USB_DEVICE(USB_COMPRO_VENDOR_ID
, USB_COMPRO_PRODUCT_ID
)},
805 {} /* Terminating entry */
808 MODULE_DEVICE_TABLE(usb
, vicam_table
);
810 static struct usb_driver vicam_driver
= {
812 .probe
= vicam_probe
,
813 .disconnect
= vicam_disconnect
,
814 .id_table
= vicam_table
819 * @intf: the interface
822 * Called by the usb core when a new device is connected that it thinks
823 * this driver might be interested in.
826 vicam_probe( struct usb_interface
*intf
, const struct usb_device_id
*id
)
828 struct usb_device
*dev
= interface_to_usbdev(intf
);
829 int bulkEndpoint
= 0;
830 const struct usb_host_interface
*interface
;
831 const struct usb_endpoint_descriptor
*endpoint
;
832 struct vicam_camera
*cam
;
834 printk(KERN_INFO
"ViCam based webcam connected\n");
836 interface
= intf
->cur_altsetting
;
838 DBG(KERN_DEBUG
"Interface %d. has %u. endpoints!\n",
839 interface
->desc
.bInterfaceNumber
, (unsigned) (interface
->desc
.bNumEndpoints
));
840 endpoint
= &interface
->endpoint
[0].desc
;
842 if ((endpoint
->bEndpointAddress
& 0x80) &&
843 ((endpoint
->bmAttributes
& 3) == 0x02)) {
844 /* we found a bulk in endpoint */
845 bulkEndpoint
= endpoint
->bEndpointAddress
;
848 "No bulk in endpoint was found ?! (this is bad)\n");
852 kzalloc(sizeof (struct vicam_camera
), GFP_KERNEL
)) == NULL
) {
854 "could not allocate kernel memory for vicam_camera struct\n");
859 cam
->shutter_speed
= 15;
861 mutex_init(&cam
->cam_lock
);
863 memcpy(&cam
->vdev
, &vicam_template
,
864 sizeof (vicam_template
));
865 cam
->vdev
.priv
= cam
; // sort of a reverse mapping for those functions that get vdev only
868 cam
->bulkEndpoint
= bulkEndpoint
;
870 if (video_register_device(&cam
->vdev
, VFL_TYPE_GRABBER
, -1) == -1) {
872 printk(KERN_WARNING
"video_register_device failed\n");
876 printk(KERN_INFO
"ViCam webcam driver now controlling video device %d\n",cam
->vdev
.minor
);
878 usb_set_intfdata (intf
, cam
);
884 vicam_disconnect(struct usb_interface
*intf
)
887 struct vicam_camera
*cam
= usb_get_intfdata (intf
);
888 usb_set_intfdata (intf
, NULL
);
890 /* we must unregister the device before taking its
891 * cam_lock. This is because the video open call
892 * holds the same lock as video unregister. if we
893 * unregister inside of the cam_lock and open also
894 * uses the cam_lock, we get deadlock.
897 video_unregister_device(&cam
->vdev
);
899 /* stop the camera from being used */
901 mutex_lock(&cam
->cam_lock
);
903 /* mark the camera as gone */
907 /* the only thing left to do is synchronize with
908 * our close/release function on who should release
909 * the camera memory. if there are any users using the
910 * camera, it's their job. if there are no users,
914 open_count
= cam
->open_count
;
916 mutex_unlock(&cam
->cam_lock
);
922 printk(KERN_DEBUG
"ViCam-based WebCam disconnected\n");
931 DBG(KERN_INFO
"ViCam-based WebCam driver startup\n");
932 retval
= usb_register(&vicam_driver
);
934 printk(KERN_WARNING
"usb_register failed!\n");
942 "ViCam-based WebCam driver shutdown\n");
944 usb_deregister(&vicam_driver
);
947 module_init(usb_vicam_init
);
948 module_exit(usb_vicam_exit
);
950 MODULE_AUTHOR(DRIVER_AUTHOR
);
951 MODULE_DESCRIPTION(DRIVER_DESC
);
952 MODULE_LICENSE("GPL");
953 MODULE_FIRMWARE("vicam/firmware.fw");