2 * STV0680 USB Camera Driver, by Kevin Sisson (kjsisson@bellsouth.net)
4 * Thanks to STMicroelectronics for information on the usb commands, and
5 * to Steve Miller at STM for his help and encouragement while I was
8 * This driver is based heavily on the
9 * Endpoints (formerly known as AOX) se401 USB Camera Driver
10 * Copyright (c) 2000 Jeroen B. Vreeken (pe1rxq@amsat.org)
12 * Still somewhat based on the Linux ov511 driver.
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software Foundation,
26 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 * ver 0.1 October, 2001. Initial attempt.
31 * ver 0.2 November, 2001. Fixed asbility to resize, added brightness
32 * function, made more stable (?)
34 * ver 0.21 Nov, 2001. Added gamma correction and white balance,
35 * due to Alexander Schwartz. Still trying to
36 * improve stablility. Moved stuff into stv680.h
38 * ver 0.22 Nov, 2001. Added sharpen function (by Michael Sweet,
39 * mike@easysw.com) from GIMP, also used in pencam.
40 * Simple, fast, good integer math routine.
42 * ver 0.23 Dec, 2001 (gkh)
43 * Took out sharpen function, ran code through
44 * Lindent, and did other minor tweaks to get
45 * things to work properly with 2.5.1
47 * ver 0.24 Jan, 2002 (kjs)
48 * Fixed the problem with webcam crashing after
49 * two pictures. Changed the way pic is halved to
50 * improve quality. Got rid of green line around
51 * frame. Fix brightness reset when changing size
52 * bug. Adjusted gamma filters slightly.
54 * ver 0.25 Jan, 2002 (kjs)
55 * Fixed a bug in which the driver sometimes attempted
56 * to set to a non-supported size. This allowed
57 * gnomemeeting to work.
58 * Fixed proc entry removal bug.
61 #include <linux/config.h>
62 #include <linux/module.h>
63 #include <linux/init.h>
64 #include <linux/vmalloc.h>
65 #include <linux/slab.h>
66 #include <linux/pagemap.h>
67 #include <linux/errno.h>
68 #include <linux/videodev.h>
69 #include <linux/usb.h>
73 static int video_nr = -1;
74 static int swapRGB = 0; /* default for auto sleect */
75 static int swapRGB_on = 0; /* default to allow auto select; -1=swap never, +1= swap always */
77 static unsigned int debug = 0;
79 #define PDEBUG(level, fmt, args...) \
82 info("[%s:%d] " fmt, __FUNCTION__, __LINE__ , ## args); \
89 #define DRIVER_VERSION "v0.25"
90 #define DRIVER_AUTHOR "Kevin Sisson <kjsisson@bellsouth.net>"
91 #define DRIVER_DESC "STV0680 USB Camera Driver"
93 MODULE_AUTHOR (DRIVER_AUTHOR);
94 MODULE_DESCRIPTION (DRIVER_DESC);
95 MODULE_LICENSE ("GPL");
96 MODULE_PARM (debug, "i");
97 MODULE_PARM_DESC (debug, "Debug enabled or not");
98 MODULE_PARM (swapRGB_on, "i");
99 MODULE_PARM_DESC (swapRGB_on, "Red/blue swap: 1=always, 0=auto, -1=never");
100 MODULE_PARM (video_nr, "i");
102 /********************************************************************
106 * This is a shameless copy from the USB-cpia driver (linux kernel
107 * version 2.3.29 or so, I have no idea what this code actually does ;).
108 * Actually it seems to be a copy of a shameless copy of the bttv-driver.
109 * Or that is a copy of a shameless copy of ... (To the powers: is there
110 * no generic kernel-function to do this sort of stuff?)
112 * Yes, it was a shameless copy from the bttv-driver. IIRC, Alan says
113 * there will be one, but apparentely not yet -jerdfelt
115 * So I copied it again for the ov511 driver -claudio
117 * Same for the se401 driver -Jeroen
119 * And the STV0680 driver - Kevin
120 ********************************************************************/
122 /* Here we want the physical address of the memory.
123 * This is used when initializing the contents of the area.
125 static inline unsigned long kvirt_to_pa (unsigned long adr)
127 unsigned long kva, ret;
129 kva = (unsigned long) page_address(vmalloc_to_page((void *)adr));
130 kva |= adr & (PAGE_SIZE-1); /* restore the offset */
135 static void *rvmalloc (unsigned long size)
140 size = PAGE_ALIGN(size);
141 mem = vmalloc_32 (size);
145 memset (mem, 0, size); /* Clear the ram out, no junk to the user */
146 adr = (unsigned long) mem;
148 SetPageReserved(vmalloc_to_page((void *)adr));
155 static void rvfree (void *mem, unsigned long size)
162 adr = (unsigned long) mem;
163 while ((long) size > 0) {
164 ClearPageReserved(vmalloc_to_page((void *)adr));
172 /*********************************************************************
173 * pencam read/write functions
174 ********************************************************************/
176 static int stv_sndctrl (int set, struct usb_stv *stv680, unsigned short req, unsigned short value, unsigned char *buffer, int size)
182 ret = usb_control_msg (stv680->udev,
183 usb_rcvctrlpipe (stv680->udev, 0),
185 (USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT),
186 value, 0, buffer, size, PENCAM_TIMEOUT);
190 ret = usb_control_msg (stv680->udev,
191 usb_sndctrlpipe (stv680->udev, 0),
193 (USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT),
194 value, 0, buffer, size, PENCAM_TIMEOUT);
198 ret = usb_control_msg (stv680->udev,
199 usb_rcvctrlpipe (stv680->udev, 0),
201 (USB_DIR_IN | USB_RECIP_DEVICE),
202 value, 0, buffer, size, PENCAM_TIMEOUT);
206 ret = usb_control_msg (stv680->udev,
207 usb_sndctrlpipe (stv680->udev, 0),
209 (USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE),
210 value, 0, buffer, size, PENCAM_TIMEOUT);
214 if ((ret < 0) && (req != 0x0a)) {
215 PDEBUG (1, "STV(e): usb_control_msg error %i, request = 0x%x, error = %i", set, req, ret);
220 static int stv_set_config (struct usb_stv *dev, int configuration, int interface, int alternate)
223 if (configuration != dev->udev->actconfig->desc.bConfigurationValue
224 || usb_reset_configuration (dev->udev) < 0) {
225 PDEBUG (1, "STV(e): FAILED to reset configuration %i", configuration);
228 if (usb_set_interface (dev->udev, interface, alternate) < 0) {
229 PDEBUG (1, "STV(e): FAILED to set alternate interface %i", alternate);
235 static int stv_stop_video (struct usb_stv *dev)
240 buf = kmalloc (40, GFP_KERNEL);
242 PDEBUG (0, "STV(e): Out of (small buf) memory");
246 /* this is a high priority command; it stops all lower order commands */
247 if ((i = stv_sndctrl (1, dev, 0x04, 0x0000, buf, 0x0)) < 0) {
248 i = stv_sndctrl (0, dev, 0x80, 0, buf, 0x02); /* Get Last Error; 2 = busy */
249 PDEBUG (1, "STV(i): last error: %i, command = 0x%x", buf[0], buf[1]);
251 PDEBUG (1, "STV(i): Camera reset to idle mode.");
254 if ((i = stv_set_config (dev, 1, 0, 0)) < 0)
255 PDEBUG (1, "STV(e): Reset config during exit failed");
257 /* get current mode */
259 if ((i = stv_sndctrl (0, dev, 0x87, 0, buf, 0x08)) != 0x08) /* get mode */
260 PDEBUG (0, "STV(e): Stop_video: problem setting original mode");
261 if (dev->origMode != buf[0]) {
263 buf[0] = (unsigned char) dev->origMode;
264 if ((i = stv_sndctrl (3, dev, 0x07, 0x0100, buf, 0x08)) != 0x08) {
265 PDEBUG (0, "STV(e): Stop_video: Set_Camera_Mode failed");
269 i = stv_sndctrl (0, dev, 0x87, 0, buf, 0x08);
270 if ((i != 0x08) || (buf[0] != dev->origMode)) {
271 PDEBUG (0, "STV(e): camera NOT set to original resolution.");
274 PDEBUG (0, "STV(i): Camera set to original resolution");
281 static int stv_set_video_mode (struct usb_stv *dev)
283 int i, stop_video = 1;
286 buf = kmalloc (40, GFP_KERNEL);
288 PDEBUG (0, "STV(e): Out of (small buf) memory");
292 if ((i = stv_set_config (dev, 1, 0, 0)) < 0) {
297 i = stv_sndctrl (2, dev, 0x06, 0x0100, buf, 0x12);
298 if (!(i > 0) && (buf[8] == 0x53) && (buf[9] == 0x05)) {
299 PDEBUG (1, "STV(e): Could not get descriptor 0100.");
303 /* set alternate interface 1 */
304 if ((i = stv_set_config (dev, 1, 0, 1)) < 0)
307 if ((i = stv_sndctrl (0, dev, 0x85, 0, buf, 0x10)) != 0x10)
309 PDEBUG (1, "STV(i): Setting video mode.");
310 /* Switch to Video mode: 0x0100 = VGA (640x480), 0x0000 = CIF (352x288) 0x0300 = QVGA (320x240) */
311 if ((i = stv_sndctrl (1, dev, 0x09, dev->VideoMode, buf, 0x0)) < 0) {
320 stv_stop_video (dev);
328 static int stv_init (struct usb_stv *stv680)
331 unsigned char *buffer;
332 unsigned long int bufsize;
334 buffer = kmalloc (40, GFP_KERNEL);
335 if (buffer == NULL) {
336 PDEBUG (0, "STV(e): Out of (small buf) memory");
339 memset (buffer, 0, 40);
342 /* set config 1, interface 0, alternate 0 */
343 if ((i = stv_set_config (stv680, 1, 0, 0)) < 0) {
345 PDEBUG (0, "STV(e): set config 1,0,0 failed");
348 /* ping camera to be sure STV0680 is present */
349 if ((i = stv_sndctrl (0, stv680, 0x88, 0x5678, buffer, 0x02)) != 0x02)
351 if ((buffer[0] != 0x56) || (buffer[1] != 0x78)) {
352 PDEBUG (1, "STV(e): camera ping failed!!");
356 /* get camera descriptor */
357 if ((i = stv_sndctrl (2, stv680, 0x06, 0x0200, buffer, 0x09)) != 0x09)
359 i = stv_sndctrl (2, stv680, 0x06, 0x0200, buffer, 0x22);
360 if (!(i >= 0) && (buffer[7] == 0xa0) && (buffer[8] == 0x23)) {
361 PDEBUG (1, "STV(e): Could not get descriptor 0200.");
364 if ((i = stv_sndctrl (0, stv680, 0x8a, 0, buffer, 0x02)) != 0x02)
366 if ((i = stv_sndctrl (0, stv680, 0x8b, 0, buffer, 0x24)) != 0x24)
368 if ((i = stv_sndctrl (0, stv680, 0x85, 0, buffer, 0x10)) != 0x10)
371 stv680->SupportedModes = buffer[7];
372 i = stv680->SupportedModes;
382 if (stv680->SupportedModes == 0) {
383 PDEBUG (0, "STV(e): There are NO supported STV680 modes!!");
388 PDEBUG (0, "STV(i): CIF is supported");
390 PDEBUG (0, "STV(i): QVGA is supported");
392 /* FW rev, ASIC rev, sensor ID */
393 PDEBUG (1, "STV(i): Firmware rev is %i.%i", buffer[0], buffer[1]);
394 PDEBUG (1, "STV(i): ASIC rev is %i.%i", buffer[2], buffer[3]);
395 PDEBUG (1, "STV(i): Sensor ID is %i", (buffer[4]*16) + (buffer[5]>>4));
397 /* set alternate interface 1 */
398 if ((i = stv_set_config (stv680, 1, 0, 1)) < 0)
401 if ((i = stv_sndctrl (0, stv680, 0x85, 0, buffer, 0x10)) != 0x10)
403 if ((i = stv_sndctrl (0, stv680, 0x8d, 0, buffer, 0x08)) != 0x08)
406 PDEBUG (0, "STV(i): Camera has %i pictures.", i);
408 /* get current mode */
409 if ((i = stv_sndctrl (0, stv680, 0x87, 0, buffer, 0x08)) != 0x08)
411 stv680->origMode = buffer[0]; /* 01 = VGA, 03 = QVGA, 00 = CIF */
413 /* This will attemp CIF mode, if supported. If not, set to QVGA */
414 memset (buffer, 0, 8);
417 else if (stv680->QVGA)
419 if ((i = stv_sndctrl (3, stv680, 0x07, 0x0100, buffer, 0x08)) != 0x08) {
420 PDEBUG (0, "STV(i): Set_Camera_Mode failed");
425 stv_sndctrl (0, stv680, 0x87, 0, buffer, 0x08);
426 if (((stv680->CIF == 1) && (buffer[0] != 0x00)) || ((stv680->QVGA == 1) && (buffer[0] != 0x03))) {
427 PDEBUG (0, "STV(e): Error setting camera video mode!");
431 if (buffer[0] == 0) {
432 stv680->VideoMode = 0x0000;
433 PDEBUG (0, "STV(i): Video Mode set to CIF");
435 if (buffer[0] == 0x03) {
436 stv680->VideoMode = 0x0300;
437 PDEBUG (0, "STV(i): Video Mode set to QVGA");
440 if ((i = stv_sndctrl (0, stv680, 0x8f, 0, buffer, 0x10)) != 0x10)
442 bufsize = (buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | (buffer[3]);
443 stv680->cwidth = (buffer[4] << 8) | (buffer[5]); /* ->camera = 322, 356, 644 */
444 stv680->cheight = (buffer[6] << 8) | (buffer[7]); /* ->camera = 242, 292, 484 */
445 stv680->origGain = buffer[12];
450 i = stv_sndctrl (0, stv680, 0x80, 0, buffer, 0x02); /* Get Last Error */
451 PDEBUG (1, "STV(i): last error: %i, command = 0x%x", buffer[0], buffer[1]);
458 /* video = 320x240, 352x288 */
459 if (stv680->CIF == 1) {
460 stv680->maxwidth = 352;
461 stv680->maxheight = 288;
462 stv680->vwidth = 352;
463 stv680->vheight = 288;
465 if (stv680->QVGA == 1) {
466 stv680->maxwidth = 320;
467 stv680->maxheight = 240;
468 stv680->vwidth = 320;
469 stv680->vheight = 240;
472 stv680->rawbufsize = bufsize; /* must be ./. by 8 */
473 stv680->maxframesize = bufsize * 3; /* RGB size */
474 PDEBUG (2, "STV(i): cwidth = %i, cheight = %i", stv680->cwidth, stv680->cheight);
475 PDEBUG (1, "STV(i): width = %i, height = %i, rawbufsize = %li", stv680->vwidth, stv680->vheight, stv680->rawbufsize);
477 /* some default values */
478 stv680->bulk_in_endpointAddr = 0x82;
481 stv680->framecount = 0;
482 stv680->readcount = 0;
483 stv680->streaming = 0;
484 /* bright, white, colour, hue, contrast are set by software, not in stv0680 */
485 stv680->brightness = 32767;
486 stv680->chgbright = 0;
487 stv680->whiteness = 0; /* only for greyscale */
488 stv680->colour = 32767;
489 stv680->contrast = 32767;
491 stv680->palette = STV_VIDEO_PALETTE;
492 stv680->depth = 24; /* rgb24 bits */
494 if ((swapRGB_on == 0) && (swapRGB == 0))
495 PDEBUG (1, "STV(i): swapRGB is (auto) OFF");
496 else if ((swapRGB_on == 1) && (swapRGB == 1))
497 PDEBUG (1, "STV(i): swapRGB is (auto) ON");
498 else if (swapRGB_on == 1)
499 PDEBUG (1, "STV(i): swapRGB is (forced) ON");
500 else if (swapRGB_on == -1)
501 PDEBUG (1, "STV(i): swapRGB is (forced) OFF");
503 if (stv_set_video_mode (stv680) < 0) {
504 PDEBUG (0, "STV(e): Could not set video mode in stv_init");
511 /***************** last of pencam routines *******************/
513 /****************************************************************************
515 ***************************************************************************/
516 static inline struct usb_stv *cd_to_stv(struct class_device *cd)
518 struct video_device *vdev = to_video_device(cd);
519 return video_get_drvdata(vdev);
522 #define stv680_file(name, variable, field) \
523 static ssize_t show_##name(struct class_device *class_dev, char *buf) \
525 struct video_device *vdev = to_video_device(class_dev); \
526 struct usb_stv *stv = video_get_drvdata(vdev); \
527 return sprintf(buf, field, stv->variable); \
529 static CLASS_DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
531 stv680_file(model, camera_name, "%s\n");
532 stv680_file(in_use, user, "%d\n");
533 stv680_file(streaming, streaming, "%d\n");
534 stv680_file(palette, palette, "%i\n");
535 stv680_file(frames_total, readcount, "%d\n");
536 stv680_file(frames_read, framecount, "%d\n");
537 stv680_file(packets_dropped, dropped, "%d\n");
538 stv680_file(decoding_errors, error, "%d\n");
540 static void stv680_create_sysfs_files(struct video_device *vdev)
542 video_device_create_file(vdev, &class_device_attr_model);
543 video_device_create_file(vdev, &class_device_attr_in_use);
544 video_device_create_file(vdev, &class_device_attr_streaming);
545 video_device_create_file(vdev, &class_device_attr_palette);
546 video_device_create_file(vdev, &class_device_attr_frames_total);
547 video_device_create_file(vdev, &class_device_attr_frames_read);
548 video_device_create_file(vdev, &class_device_attr_packets_dropped);
549 video_device_create_file(vdev, &class_device_attr_decoding_errors);
552 static void stv680_remove_sysfs_files(struct video_device *vdev)
554 video_device_remove_file(vdev, &class_device_attr_model);
555 video_device_remove_file(vdev, &class_device_attr_in_use);
556 video_device_remove_file(vdev, &class_device_attr_streaming);
557 video_device_remove_file(vdev, &class_device_attr_palette);
558 video_device_remove_file(vdev, &class_device_attr_frames_total);
559 video_device_remove_file(vdev, &class_device_attr_frames_read);
560 video_device_remove_file(vdev, &class_device_attr_packets_dropped);
561 video_device_remove_file(vdev, &class_device_attr_decoding_errors);
564 /********************************************************************
566 *******************************************************************/
568 static int stv680_get_pict (struct usb_stv *stv680, struct video_picture *p)
570 /* This sets values for v4l interface. max/min = 65535/0 */
572 p->brightness = stv680->brightness;
573 p->whiteness = stv680->whiteness; /* greyscale */
574 p->colour = stv680->colour;
575 p->contrast = stv680->contrast;
576 p->hue = stv680->hue;
577 p->palette = stv680->palette;
578 p->depth = stv680->depth;
582 static int stv680_set_pict (struct usb_stv *stv680, struct video_picture *p)
584 /* See above stv680_get_pict */
586 if (p->palette != STV_VIDEO_PALETTE) {
587 PDEBUG (2, "STV(e): Palette set error in _set_pic");
591 if (stv680->brightness != p->brightness) {
592 stv680->chgbright = 1;
593 stv680->brightness = p->brightness;
596 stv680->whiteness = p->whiteness; /* greyscale */
597 stv680->colour = p->colour;
598 stv680->contrast = p->contrast;
599 stv680->hue = p->hue;
600 stv680->palette = p->palette;
601 stv680->depth = p->depth;
606 static void stv680_video_irq (struct urb *urb, struct pt_regs *regs)
608 struct usb_stv *stv680 = urb->context;
609 int length = urb->actual_length;
611 if (length < stv680->rawbufsize)
612 PDEBUG (2, "STV(i): Lost data in transfer: exp %li, got %i", stv680->rawbufsize, length);
615 if (!stv680->streaming)
619 PDEBUG (0, "STV(e): device vapourished in video_irq");
623 /* 0 sized packets happen if we are to fast, but sometimes the camera
624 keeps sending them forever...
626 if (length && !urb->status) {
627 stv680->nullpackets = 0;
628 switch (stv680->scratch[stv680->scratch_next].state) {
635 memcpy (stv680->scratch[stv680->scratch_next].data,
636 (unsigned char *) urb->transfer_buffer, length);
637 stv680->scratch[stv680->scratch_next].state = BUFFER_READY;
638 stv680->scratch[stv680->scratch_next].length = length;
639 if (waitqueue_active (&stv680->wq)) {
640 wake_up_interruptible (&stv680->wq);
642 stv680->scratch_overflow = 0;
643 stv680->scratch_next++;
644 if (stv680->scratch_next >= STV680_NUMSCRATCH)
645 stv680->scratch_next = 0;;
649 stv680->nullpackets++;
650 if (stv680->nullpackets > STV680_MAX_NULLPACKETS) {
651 if (waitqueue_active (&stv680->wq)) {
652 wake_up_interruptible (&stv680->wq);
657 /* Resubmit urb for new data */
659 urb->dev = stv680->udev;
660 if (usb_submit_urb (urb, GFP_KERNEL))
661 PDEBUG (0, "STV(e): urb burned down in video irq");
665 static int stv680_start_stream (struct usb_stv *stv680)
670 stv680->streaming = 1;
672 /* Do some memory allocation */
673 for (i = 0; i < STV680_NUMFRAMES; i++) {
674 stv680->frame[i].data = stv680->fbuf + i * stv680->maxframesize;
675 stv680->frame[i].curpix = 0;
677 /* packet size = 4096 */
678 for (i = 0; i < STV680_NUMSBUF; i++) {
679 stv680->sbuf[i].data = kmalloc (stv680->rawbufsize, GFP_KERNEL);
680 if (stv680->sbuf[i].data == NULL) {
681 PDEBUG (0, "STV(e): Could not kmalloc raw data buffer %i", i);
686 stv680->scratch_next = 0;
687 stv680->scratch_use = 0;
688 stv680->scratch_overflow = 0;
689 for (i = 0; i < STV680_NUMSCRATCH; i++) {
690 stv680->scratch[i].data = kmalloc (stv680->rawbufsize, GFP_KERNEL);
691 if (stv680->scratch[i].data == NULL) {
692 PDEBUG (0, "STV(e): Could not kmalloc raw scratch buffer %i", i);
695 stv680->scratch[i].state = BUFFER_UNUSED;
698 for (i = 0; i < STV680_NUMSBUF; i++) {
699 urb = usb_alloc_urb (0, GFP_KERNEL);
703 /* sbuf is urb->transfer_buffer, later gets memcpyed to scratch */
704 usb_fill_bulk_urb (urb, stv680->udev,
705 usb_rcvbulkpipe (stv680->udev, stv680->bulk_in_endpointAddr),
706 stv680->sbuf[i].data, stv680->rawbufsize,
707 stv680_video_irq, stv680);
708 urb->timeout = PENCAM_TIMEOUT * 2;
709 stv680->urb[i] = urb;
710 err = usb_submit_urb (stv680->urb[i], GFP_KERNEL);
712 PDEBUG (0, "STV(e): urb burned down in start stream");
713 } /* i STV680_NUMSBUF */
715 stv680->framecount = 0;
719 static int stv680_stop_stream (struct usb_stv *stv680)
723 if (!stv680->streaming || !stv680->udev)
726 stv680->streaming = 0;
728 for (i = 0; i < STV680_NUMSBUF; i++)
729 if (stv680->urb[i]) {
730 usb_unlink_urb (stv680->urb[i]);
731 usb_free_urb (stv680->urb[i]);
732 stv680->urb[i] = NULL;
733 kfree (stv680->sbuf[i].data);
735 for (i = 0; i < STV680_NUMSCRATCH; i++) {
736 kfree (stv680->scratch[i].data);
737 stv680->scratch[i].data = NULL;
743 static int stv680_set_size (struct usb_stv *stv680, int width, int height)
745 int wasstreaming = stv680->streaming;
747 /* Check to see if we need to change */
748 if ((stv680->vwidth == width) && (stv680->vheight == height))
751 PDEBUG (1, "STV(i): size request for %i x %i", width, height);
752 /* Check for a valid mode */
753 if ((!width || !height) || ((width & 1) || (height & 1))) {
754 PDEBUG (1, "STV(e): set_size error: request: v.width = %i, v.height = %i actual: stv.width = %i, stv.height = %i", width, height, stv680->vwidth, stv680->vheight);
758 if ((width < (stv680->maxwidth / 2)) || (height < (stv680->maxheight / 2))) {
759 width = stv680->maxwidth / 2;
760 height = stv680->maxheight / 2;
761 } else if ((width >= 158) && (width <= 166) && (stv680->QVGA == 1)) {
764 } else if ((width >= 172) && (width <= 180) && (stv680->CIF == 1)) {
767 } else if ((width >= 318) && (width <= 350) && (stv680->QVGA == 1)) {
770 } else if ((width >= 350) && (width <= 358) && (stv680->CIF == 1)) {
774 PDEBUG (1, "STV(e): request for non-supported size: request: v.width = %i, v.height = %i actual: stv.width = %i, stv.height = %i", width, height, stv680->vwidth, stv680->vheight);
778 /* Stop a current stream and start it again at the new size */
780 stv680_stop_stream (stv680);
781 stv680->vwidth = width;
782 stv680->vheight = height;
783 PDEBUG (1, "STV(i): size set to %i x %i", stv680->vwidth, stv680->vheight);
785 stv680_start_stream (stv680);
790 /**********************************************************************
792 **********************************************************************/
794 /******* routines from the pencam program; hey, they work! ********/
797 * STV0680 Vision Camera Chipset Driver
798 * Copyright (C) 2000 Adam Harrison <adam@antispin.org>
804 #define AD(x, y, w) (((y)*(w)+(x))*3)
806 static void bayer_unshuffle (struct usb_stv *stv680, struct stv680_scratch *buffer)
809 int w = stv680->cwidth;
810 int vw = stv680->cwidth, vh = stv680->cheight;
812 int colour = 0, bayer = 0;
813 unsigned char *raw = buffer->data;
814 struct stv680_frame *frame = &stv680->frame[stv680->curframe];
815 unsigned char *output = frame->data;
816 unsigned char *temp = frame->data;
817 int offset = buffer->offset;
819 if (frame->curpix == 0) {
820 if (frame->grabstate == FRAME_READY) {
821 frame->grabstate = FRAME_GRABBING;
824 if (offset != frame->curpix) { /* Regard frame as lost :( */
830 if ((stv680->vwidth == 320) || (stv680->vwidth == 160)) {
834 if ((stv680->vwidth == 352) || (stv680->vwidth == 176)) {
839 memset (output, 0, 3 * vw * vh); /* clear output matrix. */
841 for (y = 0; y < vh; y++) {
842 for (x = 0; x < vw; x++) {
844 p = *(raw + y * w + (x >> 1));
846 p = *(raw + y * w + (x >> 1) + (w >> 1));
867 i = (y * vw + x) * 3;
868 *(output + i + colour) = (unsigned char) p;
873 /****** gamma correction plus hardcoded white balance */
874 /* Thanks to Alexander Schwartx <alexander.schwartx@gmx.net> for this code.
875 Correction values red[], green[], blue[], are generated by
876 (pow(i/256.0, GAMMA)*255.0)*white balanceRGB where GAMMA=0.55, 1<i<255.
877 White balance (RGB)= 1.0, 1.17, 1.48. Values are calculated as double float and
878 converted to unsigned char. Values are in stv680.h */
880 for (y = 0; y < vh; y++) {
881 for (x = 0; x < vw; x++) {
882 i = (y * vw + x) * 3;
883 *(output + i) = red[*(output + i)];
884 *(output + i + 1) = green[*(output + i + 1)];
885 *(output + i + 2) = blue[*(output + i + 2)];
889 /****** bayer demosaic ******/
890 for (y = 1; y < (vh - 1); y++) {
891 for (x = 1; x < (vw - 1); x++) { /* work out pixel type */
900 case 0: /* green. blue lr, red tb */
901 *(output + AD (x, y, vw) + BLUE) = ((int) *(output + AD (x - 1, y, vw) + BLUE) + (int) *(output + AD (x + 1, y, vw) + BLUE)) >> 1;
902 *(output + AD (x, y, vw) + RED) = ((int) *(output + AD (x, y - 1, vw) + RED) + (int) *(output + AD (x, y + 1, vw) + RED)) >> 1;
905 case 1: /* blue. green lrtb, red diagonals */
906 *(output + AD (x, y, vw) + GREEN) = ((int) *(output + AD (x - 1, y, vw) + GREEN) + (int) *(output + AD (x + 1, y, vw) + GREEN) + (int) *(output + AD (x, y - 1, vw) + GREEN) + (int) *(output + AD (x, y + 1, vw) + GREEN)) >> 2;
907 *(output + AD (x, y, vw) + RED) = ((int) *(output + AD (x - 1, y - 1, vw) + RED) + (int) *(output + AD (x - 1, y + 1, vw) + RED) + (int) *(output + AD (x + 1, y - 1, vw) + RED) + (int) *(output + AD (x + 1, y + 1, vw) + RED)) >> 2;
910 case 2: /* red. green lrtb, blue diagonals */
911 *(output + AD (x, y, vw) + GREEN) = ((int) *(output + AD (x - 1, y, vw) + GREEN) + (int) *(output + AD (x + 1, y, vw) + GREEN) + (int) *(output + AD (x, y - 1, vw) + GREEN) + (int) *(output + AD (x, y + 1, vw) + GREEN)) >> 2;
912 *(output + AD (x, y, vw) + BLUE) = ((int) *(output + AD (x - 1, y - 1, vw) + BLUE) + (int) *(output + AD (x + 1, y - 1, vw) + BLUE) + (int) *(output + AD (x - 1, y + 1, vw) + BLUE) + (int) *(output + AD (x + 1, y + 1, vw) + BLUE)) >> 2;
915 case 3: /* green. red lr, blue tb */
916 *(output + AD (x, y, vw) + RED) = ((int) *(output + AD (x - 1, y, vw) + RED) + (int) *(output + AD (x + 1, y, vw) + RED)) >> 1;
917 *(output + AD (x, y, vw) + BLUE) = ((int) *(output + AD (x, y - 1, vw) + BLUE) + (int) *(output + AD (x, y + 1, vw) + BLUE)) >> 1;
921 } /* for y - end demosaic */
923 /* fix top and bottom row, left and right side */
925 memcpy (output, (output + i), i);
926 memcpy ((output + (vh * i)), (output + ((vh - 1) * i)), i);
927 for (y = 0; y < vh; y++) {
929 memcpy ((output + i), (output + i + 3), 3);
930 memcpy ((output + i + (vw * 3)), (output + i + (vw - 1) * 3), 3);
933 /* process all raw data, then trim to size if necessary */
934 if ((stv680->vwidth == 160) || (stv680->vwidth == 176)) {
936 for (y = 0; y < vh; y++) {
938 for (x = 0; x < vw; x++) {
939 p = (y * vw + x) * 3;
941 *(output + i) = *(output + p);
942 *(output + i + 1) = *(output + p + 1);
943 *(output + i + 2) = *(output + p + 2);
950 /* reset to proper width */
951 if ((stv680->vwidth == 160)) {
955 if ((stv680->vwidth == 176)) {
960 /* output is RGB; some programs want BGR */
961 /* swapRGB_on=0 -> program decides; swapRGB_on=1, always swap */
962 /* swapRGB_on=-1, never swap */
963 if (((swapRGB == 1) && (swapRGB_on != -1)) || (swapRGB_on == 1)) {
964 for (y = 0; y < vh; y++) {
965 for (x = 0; x < vw; x++) {
966 i = (y * vw + x) * 3;
967 *(temp) = *(output + i);
968 *(output + i) = *(output + i + 2);
969 *(output + i + 2) = *(temp);
974 if (stv680->chgbright == 1) {
975 if (stv680->brightness >= 32767) {
976 p = (stv680->brightness - 32767) / 256;
977 for (x = 0; x < (vw * vh * 3); x++) {
978 if ((*(output + x) + (unsigned char) p) > 255)
981 *(output + x) += (unsigned char) p;
984 p = (32767 - stv680->brightness) / 256;
985 for (x = 0; x < (vw * vh * 3); x++) {
986 if ((unsigned char) p > *(output + x))
989 *(output + x) -= (unsigned char) p;
995 frame->curlinepix = 0;
996 frame->grabstate = FRAME_DONE;
997 stv680->framecount++;
999 if (stv680->frame[(stv680->curframe + 1) & (STV680_NUMFRAMES - 1)].grabstate == FRAME_READY) {
1000 stv680->curframe = (stv680->curframe + 1) & (STV680_NUMFRAMES - 1);
1003 } /* bayer_unshuffle */
1005 /******* end routines from the pencam program *********/
1007 static int stv680_newframe (struct usb_stv *stv680, int framenr)
1011 while (stv680->streaming && (stv680->frame[framenr].grabstate == FRAME_READY || stv680->frame[framenr].grabstate == FRAME_GRABBING)) {
1012 if (!stv680->frame[framenr].curpix) {
1015 wait_event_interruptible (stv680->wq, (stv680->scratch[stv680->scratch_use].state == BUFFER_READY));
1017 if (stv680->nullpackets > STV680_MAX_NULLPACKETS) {
1018 stv680->nullpackets = 0;
1019 PDEBUG (2, "STV(i): too many null length packets, restarting capture");
1020 stv680_stop_stream (stv680);
1021 stv680_start_stream (stv680);
1023 if (stv680->scratch[stv680->scratch_use].state != BUFFER_READY) {
1024 stv680->frame[framenr].grabstate = FRAME_ERROR;
1025 PDEBUG (2, "STV(e): FRAME_ERROR in _newframe");
1028 stv680->scratch[stv680->scratch_use].state = BUFFER_BUSY;
1030 bayer_unshuffle (stv680, &stv680->scratch[stv680->scratch_use]);
1032 stv680->scratch[stv680->scratch_use].state = BUFFER_UNUSED;
1033 stv680->scratch_use++;
1034 if (stv680->scratch_use >= STV680_NUMSCRATCH)
1035 stv680->scratch_use = 0;
1036 if (errors > STV680_MAX_ERRORS) {
1038 PDEBUG (2, "STV(i): too many errors, restarting capture");
1039 stv680_stop_stream (stv680);
1040 stv680_start_stream (stv680);
1047 /*********************************************************************
1049 *********************************************************************/
1051 static int stv_open (struct inode *inode, struct file *file)
1053 struct video_device *dev = video_devdata(file);
1054 struct usb_stv *stv680 = video_get_drvdata(dev);
1057 /* we are called with the BKL held */
1059 err = stv_init (stv680); /* main initialization routine for camera */
1062 stv680->fbuf = rvmalloc (stv680->maxframesize * STV680_NUMFRAMES);
1063 if (!stv680->fbuf) {
1064 PDEBUG (0, "STV(e): Could not rvmalloc frame bufer");
1067 file->private_data = dev;
1075 static int stv_close (struct inode *inode, struct file *file)
1077 struct video_device *dev = file->private_data;
1078 struct usb_stv *stv680 = video_get_drvdata(dev);
1081 for (i = 0; i < STV680_NUMFRAMES; i++)
1082 stv680->frame[i].grabstate = FRAME_UNUSED;
1083 if (stv680->streaming)
1084 stv680_stop_stream (stv680);
1086 if ((i = stv_stop_video (stv680)) < 0)
1087 PDEBUG (1, "STV(e): stop_video failed in stv_close");
1089 rvfree (stv680->fbuf, stv680->maxframesize * STV680_NUMFRAMES);
1092 if (stv680->removed) {
1095 PDEBUG (0, "STV(i): device unregistered");
1097 file->private_data = NULL;
1101 static int stv680_do_ioctl (struct inode *inode, struct file *file,
1102 unsigned int cmd, void *arg)
1104 struct video_device *vdev = file->private_data;
1105 struct usb_stv *stv680 = video_get_drvdata(vdev);
1112 struct video_capability *b = arg;
1114 strcpy (b->name, stv680->camera_name);
1115 b->type = VID_TYPE_CAPTURE;
1118 b->maxwidth = stv680->maxwidth;
1119 b->maxheight = stv680->maxheight;
1120 b->minwidth = stv680->maxwidth / 2;
1121 b->minheight = stv680->maxheight / 2;
1125 struct video_channel *v = arg;
1127 if (v->channel != 0)
1131 v->type = VIDEO_TYPE_CAMERA;
1132 strcpy (v->name, "STV Camera");
1136 struct video_channel *v = arg;
1137 if (v->channel != 0)
1142 struct video_picture *p = arg;
1144 stv680_get_pict (stv680, p);
1148 struct video_picture *p = arg;
1150 if (stv680_set_pict (stv680, p))
1155 struct video_window *vw = arg;
1161 if (vw->width != stv680->vwidth) {
1162 if (stv680_set_size (stv680, vw->width, vw->height)) {
1163 PDEBUG (2, "STV(e): failed (from user) set size in VIDIOCSWIN");
1170 struct video_window *vw = arg;
1172 vw->x = 0; /* FIXME */
1177 vw->width = stv680->vwidth;
1178 vw->height = stv680->vheight;
1182 struct video_mbuf *vm = arg;
1185 memset (vm, 0, sizeof (*vm));
1186 vm->size = STV680_NUMFRAMES * stv680->maxframesize;
1187 vm->frames = STV680_NUMFRAMES;
1188 for (i = 0; i < STV680_NUMFRAMES; i++)
1189 vm->offsets[i] = stv680->maxframesize * i;
1192 case VIDIOCMCAPTURE:{
1193 struct video_mmap *vm = arg;
1195 if (vm->format != STV_VIDEO_PALETTE) {
1196 PDEBUG (2, "STV(i): VIDIOCMCAPTURE vm.format (%i) != VIDEO_PALETTE (%i)",
1197 vm->format, STV_VIDEO_PALETTE);
1198 if ((vm->format == 3) && (swapRGB_on == 0)) {
1199 PDEBUG (2, "STV(i): VIDIOCMCAPTURE swapRGB is (auto) ON");
1200 /* this may fix those apps (e.g., xawtv) that want BGR */
1205 if (vm->frame >= STV680_NUMFRAMES) {
1206 PDEBUG (2, "STV(e): VIDIOCMCAPTURE vm.frame > NUMFRAMES");
1209 if ((stv680->frame[vm->frame].grabstate == FRAME_ERROR)
1210 || (stv680->frame[vm->frame].grabstate == FRAME_GRABBING)) {
1211 PDEBUG (2, "STV(e): VIDIOCMCAPTURE grabstate (%i) error",
1212 stv680->frame[vm->frame].grabstate);
1215 /* Is this according to the v4l spec??? */
1216 if (stv680->vwidth != vm->width) {
1217 if (stv680_set_size (stv680, vm->width, vm->height)) {
1218 PDEBUG (2, "STV(e): VIDIOCMCAPTURE set_size failed");
1222 stv680->frame[vm->frame].grabstate = FRAME_READY;
1224 if (!stv680->streaming)
1225 stv680_start_stream (stv680);
1233 if (*frame < 0 || *frame >= STV680_NUMFRAMES) {
1234 PDEBUG (2, "STV(e): Bad frame # in VIDIOCSYNC");
1237 ret = stv680_newframe (stv680, *frame);
1238 stv680->frame[*frame].grabstate = FRAME_UNUSED;
1242 struct video_buffer *vb = arg;
1244 memset (vb, 0, sizeof (*vb));
1251 PDEBUG (2, "STV(e): VIDIOCCAPTURE failed");
1266 return -ENOIOCTLCMD;
1272 static int stv680_ioctl(struct inode *inode, struct file *file,
1273 unsigned int cmd, unsigned long arg)
1275 return video_usercopy(inode, file, cmd, arg, stv680_do_ioctl);
1278 static int stv680_mmap (struct file *file, struct vm_area_struct *vma)
1280 struct video_device *dev = file->private_data;
1281 struct usb_stv *stv680 = video_get_drvdata(dev);
1282 unsigned long start = vma->vm_start;
1283 unsigned long size = vma->vm_end-vma->vm_start;
1284 unsigned long page, pos;
1286 down (&stv680->lock);
1288 if (stv680->udev == NULL) {
1292 if (size > (((STV680_NUMFRAMES * stv680->maxframesize) + PAGE_SIZE - 1)
1293 & ~(PAGE_SIZE - 1))) {
1297 pos = (unsigned long) stv680->fbuf;
1299 page = kvirt_to_pa (pos);
1300 if (remap_page_range (vma, start, page, PAGE_SIZE, PAGE_SHARED)) {
1306 if (size > PAGE_SIZE)
1316 static ssize_t stv680_read (struct file *file, char *buf,
1317 size_t count, loff_t *ppos)
1319 struct video_device *dev = file->private_data;
1320 unsigned long int realcount = count;
1322 struct usb_stv *stv680 = video_get_drvdata(dev);
1323 unsigned long int i;
1325 if (STV680_NUMFRAMES != 2) {
1326 PDEBUG (0, "STV(e): STV680_NUMFRAMES needs to be 2!");
1329 if (stv680->udev == NULL)
1331 if (realcount > (stv680->vwidth * stv680->vheight * 3))
1332 realcount = stv680->vwidth * stv680->vheight * 3;
1334 /* Shouldn't happen: */
1335 if (stv680->frame[0].grabstate == FRAME_GRABBING) {
1336 PDEBUG (2, "STV(e): FRAME_GRABBING in stv680_read");
1339 stv680->frame[0].grabstate = FRAME_READY;
1340 stv680->frame[1].grabstate = FRAME_UNUSED;
1341 stv680->curframe = 0;
1343 if (!stv680->streaming)
1344 stv680_start_stream (stv680);
1346 if (!stv680->streaming) {
1347 ret = stv680_newframe (stv680, 0); /* ret should = 0 */
1350 ret = stv680_newframe (stv680, 0);
1353 if ((i = copy_to_user (buf, stv680->frame[0].data, realcount)) != 0) {
1354 PDEBUG (2, "STV(e): copy_to_user frame 0 failed, ret count = %li", i);
1360 stv680->frame[0].grabstate = FRAME_UNUSED;
1364 static struct file_operations stv680_fops = {
1365 .owner = THIS_MODULE,
1367 .release = stv_close,
1368 .read = stv680_read,
1369 .mmap = stv680_mmap,
1370 .ioctl = stv680_ioctl,
1371 .llseek = no_llseek,
1373 static struct video_device stv680_template = {
1374 .owner = THIS_MODULE,
1375 .name = "STV0680 USB camera",
1376 .type = VID_TYPE_CAPTURE,
1377 .hardware = VID_HARDWARE_SE401,
1378 .fops = &stv680_fops,
1379 .release = video_device_release,
1383 static int stv680_probe (struct usb_interface *intf, const struct usb_device_id *id)
1385 struct usb_device *dev = interface_to_usbdev(intf);
1386 struct usb_host_interface *interface;
1387 struct usb_stv *stv680 = NULL;
1388 char *camera_name = NULL;
1391 /* We don't handle multi-config cameras */
1392 if (dev->descriptor.bNumConfigurations != 1) {
1393 PDEBUG (0, "STV(e): Number of Configurations != 1");
1397 interface = &intf->altsetting[0];
1398 /* Is it a STV680? */
1399 if ((dev->descriptor.idVendor == USB_PENCAM_VENDOR_ID) && (dev->descriptor.idProduct == USB_PENCAM_PRODUCT_ID)) {
1400 camera_name = "STV0680";
1401 PDEBUG (0, "STV(i): STV0680 camera found.");
1403 PDEBUG (0, "STV(e): Vendor/Product ID do not match STV0680 values.");
1404 PDEBUG (0, "STV(e): Check that the STV0680 camera is connected to the computer.");
1409 if ((stv680 = kmalloc (sizeof (*stv680), GFP_KERNEL)) == NULL) {
1410 PDEBUG (0, "STV(e): couldn't kmalloc stv680 struct.");
1415 memset (stv680, 0, sizeof (*stv680));
1418 stv680->camera_name = camera_name;
1420 stv680->vdev = video_device_alloc();
1421 if (!stv680->vdev) {
1425 memcpy(stv680->vdev, &stv680_template, sizeof(stv680_template));
1426 stv680->vdev->dev = &intf->dev;
1427 video_set_drvdata(stv680->vdev, stv680);
1429 memcpy (stv680->vdev->name, stv680->camera_name, strlen (stv680->camera_name));
1430 init_waitqueue_head (&stv680->wq);
1431 init_MUTEX (&stv680->lock);
1434 if (video_register_device (stv680->vdev, VFL_TYPE_GRABBER, video_nr) == -1) {
1435 PDEBUG (0, "STV(e): video_register_device failed");
1439 PDEBUG (0, "STV(i): registered new video device: video%d", stv680->vdev->minor);
1441 usb_set_intfdata (intf, stv680);
1442 stv680_create_sysfs_files(stv680->vdev);
1450 static inline void usb_stv680_remove_disconnected (struct usb_stv *stv680)
1454 stv680->udev = NULL;
1455 stv680->frame[0].grabstate = FRAME_ERROR;
1456 stv680->frame[1].grabstate = FRAME_ERROR;
1457 stv680->streaming = 0;
1459 wake_up_interruptible (&stv680->wq);
1461 for (i = 0; i < STV680_NUMSBUF; i++)
1462 if (stv680->urb[i]) {
1463 usb_unlink_urb (stv680->urb[i]);
1464 usb_free_urb (stv680->urb[i]);
1465 stv680->urb[i] = NULL;
1466 kfree (stv680->sbuf[i].data);
1468 for (i = 0; i < STV680_NUMSCRATCH; i++)
1469 if (stv680->scratch[i].data) {
1470 kfree (stv680->scratch[i].data);
1472 PDEBUG (0, "STV(i): %s disconnected", stv680->camera_name);
1474 /* Free the memory */
1478 static void stv680_disconnect (struct usb_interface *intf)
1480 struct usb_stv *stv680 = usb_get_intfdata (intf);
1482 usb_set_intfdata (intf, NULL);
1485 /* We don't want people trying to open up the device */
1487 stv680_remove_sysfs_files(stv680->vdev);
1488 video_unregister_device(stv680->vdev);
1489 stv680->vdev = NULL;
1491 if (!stv680->user) {
1492 usb_stv680_remove_disconnected (stv680);
1494 stv680->removed = 1;
1499 static struct usb_driver stv680_driver = {
1500 .owner = THIS_MODULE,
1502 .probe = stv680_probe,
1503 .disconnect = stv680_disconnect,
1504 .id_table = device_table
1507 /********************************************************************
1509 ********************************************************************/
1511 static int __init usb_stv680_init (void)
1513 if (usb_register (&stv680_driver) < 0) {
1514 PDEBUG (0, "STV(e): Could not setup STV0680 driver");
1517 PDEBUG (0, "STV(i): usb camera driver version %s registering", DRIVER_VERSION);
1519 info(DRIVER_DESC " " DRIVER_VERSION);
1523 static void __exit usb_stv680_exit (void)
1525 usb_deregister (&stv680_driver);
1526 PDEBUG (0, "STV(i): driver deregistered");
1529 module_init (usb_stv680_init);
1530 module_exit (usb_stv680_exit);