iwlwifi: Fix TKIP update key and get_free_ucode_key
[linux/fpc-iii.git] / drivers / media / video / stv680.c
blobafc32aa56fde2eb3952cb69dd214cdd9cd52701b
1 /*
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
6 * writing this driver.
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
22 * for more details.
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.
28 * History:
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/module.h>
62 #include <linux/init.h>
63 #include <linux/vmalloc.h>
64 #include <linux/slab.h>
65 #include <linux/pagemap.h>
66 #include <linux/errno.h>
67 #include <linux/videodev.h>
68 #include <media/v4l2-common.h>
69 #include <linux/usb.h>
70 #include <linux/mutex.h>
72 #include "stv680.h"
74 static int video_nr = -1;
75 static int swapRGB = 0; /* default for auto sleect */
76 static int swapRGB_on = 0; /* default to allow auto select; -1=swap never, +1= swap always */
78 static unsigned int debug = 0;
80 #define PDEBUG(level, fmt, args...) \
81 do { \
82 if (debug >= level) \
83 info("[%s:%d] " fmt, __FUNCTION__, __LINE__ , ## args); \
84 } while (0)
88 * Version Information
90 #define DRIVER_VERSION "v0.25"
91 #define DRIVER_AUTHOR "Kevin Sisson <kjsisson@bellsouth.net>"
92 #define DRIVER_DESC "STV0680 USB Camera Driver"
94 MODULE_AUTHOR (DRIVER_AUTHOR);
95 MODULE_DESCRIPTION (DRIVER_DESC);
96 MODULE_LICENSE ("GPL");
97 module_param(debug, int, S_IRUGO | S_IWUSR);
98 MODULE_PARM_DESC (debug, "Debug enabled or not");
99 module_param(swapRGB_on, int, 0);
100 MODULE_PARM_DESC (swapRGB_on, "Red/blue swap: 1=always, 0=auto, -1=never");
101 module_param(video_nr, int, 0);
103 /********************************************************************
105 * Memory management
107 * This is a shameless copy from the USB-cpia driver (linux kernel
108 * version 2.3.29 or so, I have no idea what this code actually does ;).
109 * Actually it seems to be a copy of a shameless copy of the bttv-driver.
110 * Or that is a copy of a shameless copy of ... (To the powers: is there
111 * no generic kernel-function to do this sort of stuff?)
113 * Yes, it was a shameless copy from the bttv-driver. IIRC, Alan says
114 * there will be one, but apparentely not yet -jerdfelt
116 * So I copied it again for the ov511 driver -claudio
118 * Same for the se401 driver -Jeroen
120 * And the STV0680 driver - Kevin
121 ********************************************************************/
122 static void *rvmalloc (unsigned long size)
124 void *mem;
125 unsigned long adr;
127 size = PAGE_ALIGN(size);
128 mem = vmalloc_32 (size);
129 if (!mem)
130 return NULL;
132 memset (mem, 0, size); /* Clear the ram out, no junk to the user */
133 adr = (unsigned long) mem;
134 while (size > 0) {
135 SetPageReserved(vmalloc_to_page((void *)adr));
136 adr += PAGE_SIZE;
137 size -= PAGE_SIZE;
139 return mem;
142 static void rvfree (void *mem, unsigned long size)
144 unsigned long adr;
146 if (!mem)
147 return;
149 adr = (unsigned long) mem;
150 while ((long) size > 0) {
151 ClearPageReserved(vmalloc_to_page((void *)adr));
152 adr += PAGE_SIZE;
153 size -= PAGE_SIZE;
155 vfree (mem);
159 /*********************************************************************
160 * pencam read/write functions
161 ********************************************************************/
163 static int stv_sndctrl (int set, struct usb_stv *stv680, unsigned short req, unsigned short value, unsigned char *buffer, int size)
165 int ret = -1;
167 switch (set) {
168 case 0: /* 0xc1 */
169 ret = usb_control_msg (stv680->udev,
170 usb_rcvctrlpipe (stv680->udev, 0),
171 req,
172 (USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT),
173 value, 0, buffer, size, PENCAM_TIMEOUT);
174 break;
176 case 1: /* 0x41 */
177 ret = usb_control_msg (stv680->udev,
178 usb_sndctrlpipe (stv680->udev, 0),
179 req,
180 (USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT),
181 value, 0, buffer, size, PENCAM_TIMEOUT);
182 break;
184 case 2: /* 0x80 */
185 ret = usb_control_msg (stv680->udev,
186 usb_rcvctrlpipe (stv680->udev, 0),
187 req,
188 (USB_DIR_IN | USB_RECIP_DEVICE),
189 value, 0, buffer, size, PENCAM_TIMEOUT);
190 break;
192 case 3: /* 0x40 */
193 ret = usb_control_msg (stv680->udev,
194 usb_sndctrlpipe (stv680->udev, 0),
195 req,
196 (USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE),
197 value, 0, buffer, size, PENCAM_TIMEOUT);
198 break;
201 if ((ret < 0) && (req != 0x0a)) {
202 PDEBUG (1, "STV(e): usb_control_msg error %i, request = 0x%x, error = %i", set, req, ret);
204 return ret;
207 static int stv_set_config (struct usb_stv *dev, int configuration, int interface, int alternate)
210 if (configuration != dev->udev->actconfig->desc.bConfigurationValue
211 || usb_reset_configuration (dev->udev) < 0) {
212 PDEBUG (1, "STV(e): FAILED to reset configuration %i", configuration);
213 return -1;
215 if (usb_set_interface (dev->udev, interface, alternate) < 0) {
216 PDEBUG (1, "STV(e): FAILED to set alternate interface %i", alternate);
217 return -1;
219 return 0;
222 static int stv_stop_video (struct usb_stv *dev)
224 int i;
225 unsigned char *buf;
227 buf = kmalloc (40, GFP_KERNEL);
228 if (buf == NULL) {
229 PDEBUG (0, "STV(e): Out of (small buf) memory");
230 return -1;
233 /* this is a high priority command; it stops all lower order commands */
234 if ((i = stv_sndctrl (1, dev, 0x04, 0x0000, buf, 0x0)) < 0) {
235 i = stv_sndctrl (0, dev, 0x80, 0, buf, 0x02); /* Get Last Error; 2 = busy */
236 PDEBUG (1, "STV(i): last error: %i, command = 0x%x", buf[0], buf[1]);
237 } else {
238 PDEBUG (1, "STV(i): Camera reset to idle mode.");
241 if ((i = stv_set_config (dev, 1, 0, 0)) < 0)
242 PDEBUG (1, "STV(e): Reset config during exit failed");
244 /* get current mode */
245 buf[0] = 0xf0;
246 if ((i = stv_sndctrl (0, dev, 0x87, 0, buf, 0x08)) != 0x08) /* get mode */
247 PDEBUG (0, "STV(e): Stop_video: problem setting original mode");
248 if (dev->origMode != buf[0]) {
249 memset (buf, 0, 8);
250 buf[0] = (unsigned char) dev->origMode;
251 if ((i = stv_sndctrl (3, dev, 0x07, 0x0100, buf, 0x08)) != 0x08) {
252 PDEBUG (0, "STV(e): Stop_video: Set_Camera_Mode failed");
253 i = -1;
255 buf[0] = 0xf0;
256 i = stv_sndctrl (0, dev, 0x87, 0, buf, 0x08);
257 if ((i != 0x08) || (buf[0] != dev->origMode)) {
258 PDEBUG (0, "STV(e): camera NOT set to original resolution.");
259 i = -1;
260 } else
261 PDEBUG (0, "STV(i): Camera set to original resolution");
263 /* origMode */
264 kfree(buf);
265 return i;
268 static int stv_set_video_mode (struct usb_stv *dev)
270 int i, stop_video = 1;
271 unsigned char *buf;
273 buf = kmalloc (40, GFP_KERNEL);
274 if (buf == NULL) {
275 PDEBUG (0, "STV(e): Out of (small buf) memory");
276 return -1;
279 if ((i = stv_set_config (dev, 1, 0, 0)) < 0) {
280 kfree(buf);
281 return i;
284 i = stv_sndctrl (2, dev, 0x06, 0x0100, buf, 0x12);
285 if (!(i > 0) && (buf[8] == 0x53) && (buf[9] == 0x05)) {
286 PDEBUG (1, "STV(e): Could not get descriptor 0100.");
287 goto error;
290 /* set alternate interface 1 */
291 if ((i = stv_set_config (dev, 1, 0, 1)) < 0)
292 goto error;
294 if ((i = stv_sndctrl (0, dev, 0x85, 0, buf, 0x10)) != 0x10)
295 goto error;
296 PDEBUG (1, "STV(i): Setting video mode.");
297 /* Switch to Video mode: 0x0100 = VGA (640x480), 0x0000 = CIF (352x288) 0x0300 = QVGA (320x240) */
298 if ((i = stv_sndctrl (1, dev, 0x09, dev->VideoMode, buf, 0x0)) < 0) {
299 stop_video = 0;
300 goto error;
302 goto exit;
304 error:
305 kfree(buf);
306 if (stop_video == 1)
307 stv_stop_video (dev);
308 return -1;
310 exit:
311 kfree(buf);
312 return 0;
315 static int stv_init (struct usb_stv *stv680)
317 int i = 0;
318 unsigned char *buffer;
319 unsigned long int bufsize;
321 buffer = kzalloc (40, GFP_KERNEL);
322 if (buffer == NULL) {
323 PDEBUG (0, "STV(e): Out of (small buf) memory");
324 return -1;
326 udelay (100);
328 /* set config 1, interface 0, alternate 0 */
329 if ((i = stv_set_config (stv680, 1, 0, 0)) < 0) {
330 kfree(buffer);
331 PDEBUG (0, "STV(e): set config 1,0,0 failed");
332 return -1;
334 /* ping camera to be sure STV0680 is present */
335 if ((i = stv_sndctrl (0, stv680, 0x88, 0x5678, buffer, 0x02)) != 0x02)
336 goto error;
337 if ((buffer[0] != 0x56) || (buffer[1] != 0x78)) {
338 PDEBUG (1, "STV(e): camera ping failed!!");
339 goto error;
342 /* get camera descriptor */
343 if ((i = stv_sndctrl (2, stv680, 0x06, 0x0200, buffer, 0x09)) != 0x09)
344 goto error;
345 i = stv_sndctrl (2, stv680, 0x06, 0x0200, buffer, 0x22);
346 if (!(i >= 0) && (buffer[7] == 0xa0) && (buffer[8] == 0x23)) {
347 PDEBUG (1, "STV(e): Could not get descriptor 0200.");
348 goto error;
350 if ((i = stv_sndctrl (0, stv680, 0x8a, 0, buffer, 0x02)) != 0x02)
351 goto error;
352 if ((i = stv_sndctrl (0, stv680, 0x8b, 0, buffer, 0x24)) != 0x24)
353 goto error;
354 if ((i = stv_sndctrl (0, stv680, 0x85, 0, buffer, 0x10)) != 0x10)
355 goto error;
357 stv680->SupportedModes = buffer[7];
358 i = stv680->SupportedModes;
359 stv680->CIF = 0;
360 stv680->VGA = 0;
361 stv680->QVGA = 0;
362 if (i & 1)
363 stv680->CIF = 1;
364 if (i & 2)
365 stv680->VGA = 1;
366 if (i & 8)
367 stv680->QVGA = 1;
368 if (stv680->SupportedModes == 0) {
369 PDEBUG (0, "STV(e): There are NO supported STV680 modes!!");
370 i = -1;
371 goto error;
372 } else {
373 if (stv680->CIF)
374 PDEBUG (0, "STV(i): CIF is supported");
375 if (stv680->QVGA)
376 PDEBUG (0, "STV(i): QVGA is supported");
378 /* FW rev, ASIC rev, sensor ID */
379 PDEBUG (1, "STV(i): Firmware rev is %i.%i", buffer[0], buffer[1]);
380 PDEBUG (1, "STV(i): ASIC rev is %i.%i", buffer[2], buffer[3]);
381 PDEBUG (1, "STV(i): Sensor ID is %i", (buffer[4]*16) + (buffer[5]>>4));
383 /* set alternate interface 1 */
384 if ((i = stv_set_config (stv680, 1, 0, 1)) < 0)
385 goto error;
387 if ((i = stv_sndctrl (0, stv680, 0x85, 0, buffer, 0x10)) != 0x10)
388 goto error;
389 if ((i = stv_sndctrl (0, stv680, 0x8d, 0, buffer, 0x08)) != 0x08)
390 goto error;
391 i = buffer[3];
392 PDEBUG (0, "STV(i): Camera has %i pictures.", i);
394 /* get current mode */
395 if ((i = stv_sndctrl (0, stv680, 0x87, 0, buffer, 0x08)) != 0x08)
396 goto error;
397 stv680->origMode = buffer[0]; /* 01 = VGA, 03 = QVGA, 00 = CIF */
399 /* This will attemp CIF mode, if supported. If not, set to QVGA */
400 memset (buffer, 0, 8);
401 if (stv680->CIF)
402 buffer[0] = 0x00;
403 else if (stv680->QVGA)
404 buffer[0] = 0x03;
405 if ((i = stv_sndctrl (3, stv680, 0x07, 0x0100, buffer, 0x08)) != 0x08) {
406 PDEBUG (0, "STV(i): Set_Camera_Mode failed");
407 i = -1;
408 goto error;
410 buffer[0] = 0xf0;
411 stv_sndctrl (0, stv680, 0x87, 0, buffer, 0x08);
412 if (((stv680->CIF == 1) && (buffer[0] != 0x00)) || ((stv680->QVGA == 1) && (buffer[0] != 0x03))) {
413 PDEBUG (0, "STV(e): Error setting camera video mode!");
414 i = -1;
415 goto error;
416 } else {
417 if (buffer[0] == 0) {
418 stv680->VideoMode = 0x0000;
419 PDEBUG (0, "STV(i): Video Mode set to CIF");
421 if (buffer[0] == 0x03) {
422 stv680->VideoMode = 0x0300;
423 PDEBUG (0, "STV(i): Video Mode set to QVGA");
426 if ((i = stv_sndctrl (0, stv680, 0x8f, 0, buffer, 0x10)) != 0x10)
427 goto error;
428 bufsize = (buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | (buffer[3]);
429 stv680->cwidth = (buffer[4] << 8) | (buffer[5]); /* ->camera = 322, 356, 644 */
430 stv680->cheight = (buffer[6] << 8) | (buffer[7]); /* ->camera = 242, 292, 484 */
431 stv680->origGain = buffer[12];
433 goto exit;
435 error:
436 i = stv_sndctrl (0, stv680, 0x80, 0, buffer, 0x02); /* Get Last Error */
437 PDEBUG (1, "STV(i): last error: %i, command = 0x%x", buffer[0], buffer[1]);
438 kfree(buffer);
439 return -1;
441 exit:
442 kfree(buffer);
444 /* video = 320x240, 352x288 */
445 if (stv680->CIF == 1) {
446 stv680->maxwidth = 352;
447 stv680->maxheight = 288;
448 stv680->vwidth = 352;
449 stv680->vheight = 288;
451 if (stv680->QVGA == 1) {
452 stv680->maxwidth = 320;
453 stv680->maxheight = 240;
454 stv680->vwidth = 320;
455 stv680->vheight = 240;
458 stv680->rawbufsize = bufsize; /* must be ./. by 8 */
459 stv680->maxframesize = bufsize * 3; /* RGB size */
460 PDEBUG (2, "STV(i): cwidth = %i, cheight = %i", stv680->cwidth, stv680->cheight);
461 PDEBUG (1, "STV(i): width = %i, height = %i, rawbufsize = %li", stv680->vwidth, stv680->vheight, stv680->rawbufsize);
463 /* some default values */
464 stv680->bulk_in_endpointAddr = 0x82;
465 stv680->dropped = 0;
466 stv680->error = 0;
467 stv680->framecount = 0;
468 stv680->readcount = 0;
469 stv680->streaming = 0;
470 /* bright, white, colour, hue, contrast are set by software, not in stv0680 */
471 stv680->brightness = 32767;
472 stv680->chgbright = 0;
473 stv680->whiteness = 0; /* only for greyscale */
474 stv680->colour = 32767;
475 stv680->contrast = 32767;
476 stv680->hue = 32767;
477 stv680->palette = STV_VIDEO_PALETTE;
478 stv680->depth = 24; /* rgb24 bits */
479 if ((swapRGB_on == 0) && (swapRGB == 0))
480 PDEBUG (1, "STV(i): swapRGB is (auto) OFF");
481 else if ((swapRGB_on == 0) && (swapRGB == 1))
482 PDEBUG (1, "STV(i): swapRGB is (auto) ON");
483 else if (swapRGB_on == 1)
484 PDEBUG (1, "STV(i): swapRGB is (forced) ON");
485 else if (swapRGB_on == -1)
486 PDEBUG (1, "STV(i): swapRGB is (forced) OFF");
488 if (stv_set_video_mode (stv680) < 0) {
489 PDEBUG (0, "STV(e): Could not set video mode in stv_init");
490 return -1;
493 return 0;
496 /***************** last of pencam routines *******************/
498 /****************************************************************************
499 * sysfs
500 ***************************************************************************/
501 #define stv680_file(name, variable, field) \
502 static ssize_t show_##name(struct device *class_dev, \
503 struct device_attribute *attr, char *buf) \
505 struct video_device *vdev = to_video_device(class_dev); \
506 struct usb_stv *stv = video_get_drvdata(vdev); \
507 return sprintf(buf, field, stv->variable); \
509 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
511 stv680_file(model, camera_name, "%s\n");
512 stv680_file(in_use, user, "%d\n");
513 stv680_file(streaming, streaming, "%d\n");
514 stv680_file(palette, palette, "%i\n");
515 stv680_file(frames_total, readcount, "%d\n");
516 stv680_file(frames_read, framecount, "%d\n");
517 stv680_file(packets_dropped, dropped, "%d\n");
518 stv680_file(decoding_errors, error, "%d\n");
520 static int stv680_create_sysfs_files(struct video_device *vdev)
522 int rc;
524 rc = video_device_create_file(vdev, &dev_attr_model);
525 if (rc) goto err;
526 rc = video_device_create_file(vdev, &dev_attr_in_use);
527 if (rc) goto err_model;
528 rc = video_device_create_file(vdev, &dev_attr_streaming);
529 if (rc) goto err_inuse;
530 rc = video_device_create_file(vdev, &dev_attr_palette);
531 if (rc) goto err_stream;
532 rc = video_device_create_file(vdev, &dev_attr_frames_total);
533 if (rc) goto err_pal;
534 rc = video_device_create_file(vdev, &dev_attr_frames_read);
535 if (rc) goto err_framtot;
536 rc = video_device_create_file(vdev, &dev_attr_packets_dropped);
537 if (rc) goto err_framread;
538 rc = video_device_create_file(vdev, &dev_attr_decoding_errors);
539 if (rc) goto err_dropped;
541 return 0;
543 err_dropped:
544 video_device_remove_file(vdev, &dev_attr_packets_dropped);
545 err_framread:
546 video_device_remove_file(vdev, &dev_attr_frames_read);
547 err_framtot:
548 video_device_remove_file(vdev, &dev_attr_frames_total);
549 err_pal:
550 video_device_remove_file(vdev, &dev_attr_palette);
551 err_stream:
552 video_device_remove_file(vdev, &dev_attr_streaming);
553 err_inuse:
554 video_device_remove_file(vdev, &dev_attr_in_use);
555 err_model:
556 video_device_remove_file(vdev, &dev_attr_model);
557 err:
558 return rc;
561 static void stv680_remove_sysfs_files(struct video_device *vdev)
563 video_device_remove_file(vdev, &dev_attr_model);
564 video_device_remove_file(vdev, &dev_attr_in_use);
565 video_device_remove_file(vdev, &dev_attr_streaming);
566 video_device_remove_file(vdev, &dev_attr_palette);
567 video_device_remove_file(vdev, &dev_attr_frames_total);
568 video_device_remove_file(vdev, &dev_attr_frames_read);
569 video_device_remove_file(vdev, &dev_attr_packets_dropped);
570 video_device_remove_file(vdev, &dev_attr_decoding_errors);
573 /********************************************************************
574 * Camera control
575 *******************************************************************/
577 static int stv680_get_pict (struct usb_stv *stv680, struct video_picture *p)
579 /* This sets values for v4l interface. max/min = 65535/0 */
581 p->brightness = stv680->brightness;
582 p->whiteness = stv680->whiteness; /* greyscale */
583 p->colour = stv680->colour;
584 p->contrast = stv680->contrast;
585 p->hue = stv680->hue;
586 p->palette = stv680->palette;
587 p->depth = stv680->depth;
588 return 0;
591 static int stv680_set_pict (struct usb_stv *stv680, struct video_picture *p)
593 /* See above stv680_get_pict */
595 if (p->palette != STV_VIDEO_PALETTE) {
596 PDEBUG (2, "STV(e): Palette set error in _set_pic");
597 return 1;
600 if (stv680->brightness != p->brightness) {
601 stv680->chgbright = 1;
602 stv680->brightness = p->brightness;
605 stv680->whiteness = p->whiteness; /* greyscale */
606 stv680->colour = p->colour;
607 stv680->contrast = p->contrast;
608 stv680->hue = p->hue;
609 stv680->palette = p->palette;
610 stv680->depth = p->depth;
612 return 0;
615 static void stv680_video_irq (struct urb *urb)
617 struct usb_stv *stv680 = urb->context;
618 int length = urb->actual_length;
620 if (length < stv680->rawbufsize)
621 PDEBUG (2, "STV(i): Lost data in transfer: exp %li, got %i", stv680->rawbufsize, length);
623 /* ohoh... */
624 if (!stv680->streaming)
625 return;
627 if (!stv680->udev) {
628 PDEBUG (0, "STV(e): device vapourished in video_irq");
629 return;
632 /* 0 sized packets happen if we are to fast, but sometimes the camera
633 keeps sending them forever...
635 if (length && !urb->status) {
636 stv680->nullpackets = 0;
637 switch (stv680->scratch[stv680->scratch_next].state) {
638 case BUFFER_READY:
639 case BUFFER_BUSY:
640 stv680->dropped++;
641 break;
643 case BUFFER_UNUSED:
644 memcpy (stv680->scratch[stv680->scratch_next].data,
645 (unsigned char *) urb->transfer_buffer, length);
646 stv680->scratch[stv680->scratch_next].state = BUFFER_READY;
647 stv680->scratch[stv680->scratch_next].length = length;
648 if (waitqueue_active (&stv680->wq)) {
649 wake_up_interruptible (&stv680->wq);
651 stv680->scratch_overflow = 0;
652 stv680->scratch_next++;
653 if (stv680->scratch_next >= STV680_NUMSCRATCH)
654 stv680->scratch_next = 0;
655 break;
656 } /* switch */
657 } else {
658 stv680->nullpackets++;
659 if (stv680->nullpackets > STV680_MAX_NULLPACKETS) {
660 if (waitqueue_active (&stv680->wq)) {
661 wake_up_interruptible (&stv680->wq);
664 } /* if - else */
666 /* Resubmit urb for new data */
667 urb->status = 0;
668 urb->dev = stv680->udev;
669 if (usb_submit_urb (urb, GFP_ATOMIC))
670 PDEBUG (0, "STV(e): urb burned down in video irq");
671 return;
672 } /* _video_irq */
674 static int stv680_start_stream (struct usb_stv *stv680)
676 struct urb *urb;
677 int err = 0, i;
679 stv680->streaming = 1;
681 /* Do some memory allocation */
682 for (i = 0; i < STV680_NUMFRAMES; i++) {
683 stv680->frame[i].data = stv680->fbuf + i * stv680->maxframesize;
684 stv680->frame[i].curpix = 0;
686 /* packet size = 4096 */
687 for (i = 0; i < STV680_NUMSBUF; i++) {
688 stv680->sbuf[i].data = kmalloc (stv680->rawbufsize, GFP_KERNEL);
689 if (stv680->sbuf[i].data == NULL) {
690 PDEBUG (0, "STV(e): Could not kmalloc raw data buffer %i", i);
691 goto nomem_err;
695 stv680->scratch_next = 0;
696 stv680->scratch_use = 0;
697 stv680->scratch_overflow = 0;
698 for (i = 0; i < STV680_NUMSCRATCH; i++) {
699 stv680->scratch[i].data = kmalloc (stv680->rawbufsize, GFP_KERNEL);
700 if (stv680->scratch[i].data == NULL) {
701 PDEBUG (0, "STV(e): Could not kmalloc raw scratch buffer %i", i);
702 goto nomem_err;
704 stv680->scratch[i].state = BUFFER_UNUSED;
707 for (i = 0; i < STV680_NUMSBUF; i++) {
708 urb = usb_alloc_urb (0, GFP_KERNEL);
709 if (!urb)
710 goto nomem_err;
712 /* sbuf is urb->transfer_buffer, later gets memcpyed to scratch */
713 usb_fill_bulk_urb (urb, stv680->udev,
714 usb_rcvbulkpipe (stv680->udev, stv680->bulk_in_endpointAddr),
715 stv680->sbuf[i].data, stv680->rawbufsize,
716 stv680_video_irq, stv680);
717 stv680->urb[i] = urb;
718 err = usb_submit_urb (stv680->urb[i], GFP_KERNEL);
719 if (err) {
720 PDEBUG (0, "STV(e): urb burned down with err "
721 "%d in start stream %d", err, i);
722 goto nomem_err;
724 } /* i STV680_NUMSBUF */
726 stv680->framecount = 0;
727 return 0;
729 nomem_err:
730 for (i = 0; i < STV680_NUMSCRATCH; i++) {
731 kfree(stv680->scratch[i].data);
732 stv680->scratch[i].data = NULL;
734 for (i = 0; i < STV680_NUMSBUF; i++) {
735 usb_kill_urb(stv680->urb[i]);
736 usb_free_urb(stv680->urb[i]);
737 stv680->urb[i] = NULL;
738 kfree(stv680->sbuf[i].data);
739 stv680->sbuf[i].data = NULL;
741 return -ENOMEM;
745 static int stv680_stop_stream (struct usb_stv *stv680)
747 int i;
749 if (!stv680->streaming || !stv680->udev)
750 return 1;
752 stv680->streaming = 0;
754 for (i = 0; i < STV680_NUMSBUF; i++)
755 if (stv680->urb[i]) {
756 usb_kill_urb (stv680->urb[i]);
757 usb_free_urb (stv680->urb[i]);
758 stv680->urb[i] = NULL;
759 kfree(stv680->sbuf[i].data);
761 for (i = 0; i < STV680_NUMSCRATCH; i++) {
762 kfree(stv680->scratch[i].data);
763 stv680->scratch[i].data = NULL;
766 return 0;
769 static int stv680_set_size (struct usb_stv *stv680, int width, int height)
771 int wasstreaming = stv680->streaming;
773 /* Check to see if we need to change */
774 if ((stv680->vwidth == width) && (stv680->vheight == height))
775 return 0;
777 PDEBUG (1, "STV(i): size request for %i x %i", width, height);
778 /* Check for a valid mode */
779 if ((!width || !height) || ((width & 1) || (height & 1))) {
780 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);
781 return 1;
784 if ((width < (stv680->maxwidth / 2)) || (height < (stv680->maxheight / 2))) {
785 width = stv680->maxwidth / 2;
786 height = stv680->maxheight / 2;
787 } else if ((width >= 158) && (width <= 166) && (stv680->QVGA == 1)) {
788 width = 160;
789 height = 120;
790 } else if ((width >= 172) && (width <= 180) && (stv680->CIF == 1)) {
791 width = 176;
792 height = 144;
793 } else if ((width >= 318) && (width <= 350) && (stv680->QVGA == 1)) {
794 width = 320;
795 height = 240;
796 } else if ((width >= 350) && (width <= 358) && (stv680->CIF == 1)) {
797 width = 352;
798 height = 288;
799 } else {
800 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);
801 return 1;
804 /* Stop a current stream and start it again at the new size */
805 if (wasstreaming)
806 stv680_stop_stream (stv680);
807 stv680->vwidth = width;
808 stv680->vheight = height;
809 PDEBUG (1, "STV(i): size set to %i x %i", stv680->vwidth, stv680->vheight);
810 if (wasstreaming)
811 stv680_start_stream (stv680);
813 return 0;
816 /**********************************************************************
817 * Video Decoding
818 **********************************************************************/
820 /******* routines from the pencam program; hey, they work! ********/
823 * STV0680 Vision Camera Chipset Driver
824 * Copyright (C) 2000 Adam Harrison <adam@antispin.org>
827 #define RED 0
828 #define GREEN 1
829 #define BLUE 2
830 #define AD(x, y, w) (((y)*(w)+(x))*3)
832 static void bayer_unshuffle (struct usb_stv *stv680, struct stv680_scratch *buffer)
834 int x, y, i;
835 int w = stv680->cwidth;
836 int vw = stv680->cwidth, vh = stv680->cheight;
837 unsigned int p = 0;
838 int colour = 0, bayer = 0;
839 unsigned char *raw = buffer->data;
840 struct stv680_frame *frame = &stv680->frame[stv680->curframe];
841 unsigned char *output = frame->data;
842 unsigned char *temp = frame->data;
843 int offset = buffer->offset;
845 if (frame->curpix == 0) {
846 if (frame->grabstate == FRAME_READY) {
847 frame->grabstate = FRAME_GRABBING;
850 if (offset != frame->curpix) { /* Regard frame as lost :( */
851 frame->curpix = 0;
852 stv680->error++;
853 return;
856 if ((stv680->vwidth == 320) || (stv680->vwidth == 160)) {
857 vw = 320;
858 vh = 240;
860 if ((stv680->vwidth == 352) || (stv680->vwidth == 176)) {
861 vw = 352;
862 vh = 288;
865 memset (output, 0, 3 * vw * vh); /* clear output matrix. */
867 for (y = 0; y < vh; y++) {
868 for (x = 0; x < vw; x++) {
869 if (x & 1)
870 p = *(raw + y * w + (x >> 1));
871 else
872 p = *(raw + y * w + (x >> 1) + (w >> 1));
874 if (y & 1)
875 bayer = 2;
876 else
877 bayer = 0;
878 if (x & 1)
879 bayer++;
881 switch (bayer) {
882 case 0:
883 case 3:
884 colour = 1;
885 break;
886 case 1:
887 colour = 0;
888 break;
889 case 2:
890 colour = 2;
891 break;
893 i = (y * vw + x) * 3;
894 *(output + i + colour) = (unsigned char) p;
895 } /* for x */
897 } /* for y */
899 /****** gamma correction plus hardcoded white balance */
900 /* Thanks to Alexander Schwartx <alexander.schwartx@gmx.net> for this code.
901 Correction values red[], green[], blue[], are generated by
902 (pow(i/256.0, GAMMA)*255.0)*white balanceRGB where GAMMA=0.55, 1<i<255.
903 White balance (RGB)= 1.0, 1.17, 1.48. Values are calculated as double float and
904 converted to unsigned char. Values are in stv680.h */
906 for (y = 0; y < vh; y++) {
907 for (x = 0; x < vw; x++) {
908 i = (y * vw + x) * 3;
909 *(output + i) = red[*(output + i)];
910 *(output + i + 1) = green[*(output + i + 1)];
911 *(output + i + 2) = blue[*(output + i + 2)];
915 /****** bayer demosaic ******/
916 for (y = 1; y < (vh - 1); y++) {
917 for (x = 1; x < (vw - 1); x++) { /* work out pixel type */
918 if (y & 1)
919 bayer = 0;
920 else
921 bayer = 2;
922 if (!(x & 1))
923 bayer++;
925 switch (bayer) {
926 case 0: /* green. blue lr, red tb */
927 *(output + AD (x, y, vw) + BLUE) = ((int) *(output + AD (x - 1, y, vw) + BLUE) + (int) *(output + AD (x + 1, y, vw) + BLUE)) >> 1;
928 *(output + AD (x, y, vw) + RED) = ((int) *(output + AD (x, y - 1, vw) + RED) + (int) *(output + AD (x, y + 1, vw) + RED)) >> 1;
929 break;
931 case 1: /* blue. green lrtb, red diagonals */
932 *(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;
933 *(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;
934 break;
936 case 2: /* red. green lrtb, blue diagonals */
937 *(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;
938 *(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;
939 break;
941 case 3: /* green. red lr, blue tb */
942 *(output + AD (x, y, vw) + RED) = ((int) *(output + AD (x - 1, y, vw) + RED) + (int) *(output + AD (x + 1, y, vw) + RED)) >> 1;
943 *(output + AD (x, y, vw) + BLUE) = ((int) *(output + AD (x, y - 1, vw) + BLUE) + (int) *(output + AD (x, y + 1, vw) + BLUE)) >> 1;
944 break;
945 } /* switch */
946 } /* for x */
947 } /* for y - end demosaic */
949 /* fix top and bottom row, left and right side */
950 i = vw * 3;
951 memcpy (output, (output + i), i);
952 memcpy ((output + (vh * i)), (output + ((vh - 1) * i)), i);
953 for (y = 0; y < vh; y++) {
954 i = y * vw * 3;
955 memcpy ((output + i), (output + i + 3), 3);
956 memcpy ((output + i + (vw * 3)), (output + i + (vw - 1) * 3), 3);
959 /* process all raw data, then trim to size if necessary */
960 if ((stv680->vwidth == 160) || (stv680->vwidth == 176)) {
961 i = 0;
962 for (y = 0; y < vh; y++) {
963 if (!(y & 1)) {
964 for (x = 0; x < vw; x++) {
965 p = (y * vw + x) * 3;
966 if (!(x & 1)) {
967 *(output + i) = *(output + p);
968 *(output + i + 1) = *(output + p + 1);
969 *(output + i + 2) = *(output + p + 2);
970 i += 3;
972 } /* for x */
974 } /* for y */
976 /* reset to proper width */
977 if ((stv680->vwidth == 160)) {
978 vw = 160;
979 vh = 120;
981 if ((stv680->vwidth == 176)) {
982 vw = 176;
983 vh = 144;
986 /* output is RGB; some programs want BGR */
987 /* swapRGB_on=0 -> program decides; swapRGB_on=1, always swap */
988 /* swapRGB_on=-1, never swap */
989 if (((swapRGB == 1) && (swapRGB_on != -1)) || (swapRGB_on == 1)) {
990 for (y = 0; y < vh; y++) {
991 for (x = 0; x < vw; x++) {
992 i = (y * vw + x) * 3;
993 *(temp) = *(output + i);
994 *(output + i) = *(output + i + 2);
995 *(output + i + 2) = *(temp);
999 /* brightness */
1000 if (stv680->chgbright == 1) {
1001 if (stv680->brightness >= 32767) {
1002 p = (stv680->brightness - 32767) / 256;
1003 for (x = 0; x < (vw * vh * 3); x++) {
1004 if ((*(output + x) + (unsigned char) p) > 255)
1005 *(output + x) = 255;
1006 else
1007 *(output + x) += (unsigned char) p;
1008 } /* for */
1009 } else {
1010 p = (32767 - stv680->brightness) / 256;
1011 for (x = 0; x < (vw * vh * 3); x++) {
1012 if ((unsigned char) p > *(output + x))
1013 *(output + x) = 0;
1014 else
1015 *(output + x) -= (unsigned char) p;
1016 } /* for */
1017 } /* else */
1019 /* if */
1020 frame->curpix = 0;
1021 frame->curlinepix = 0;
1022 frame->grabstate = FRAME_DONE;
1023 stv680->framecount++;
1024 stv680->readcount++;
1025 if (stv680->frame[(stv680->curframe + 1) & (STV680_NUMFRAMES - 1)].grabstate == FRAME_READY) {
1026 stv680->curframe = (stv680->curframe + 1) & (STV680_NUMFRAMES - 1);
1029 } /* bayer_unshuffle */
1031 /******* end routines from the pencam program *********/
1033 static int stv680_newframe (struct usb_stv *stv680, int framenr)
1035 int errors = 0;
1037 while (stv680->streaming && (stv680->frame[framenr].grabstate == FRAME_READY || stv680->frame[framenr].grabstate == FRAME_GRABBING)) {
1038 if (!stv680->frame[framenr].curpix) {
1039 errors++;
1041 wait_event_interruptible (stv680->wq, (stv680->scratch[stv680->scratch_use].state == BUFFER_READY));
1043 if (stv680->nullpackets > STV680_MAX_NULLPACKETS) {
1044 stv680->nullpackets = 0;
1045 PDEBUG (2, "STV(i): too many null length packets, restarting capture");
1046 stv680_stop_stream (stv680);
1047 stv680_start_stream (stv680);
1048 } else {
1049 if (stv680->scratch[stv680->scratch_use].state != BUFFER_READY) {
1050 stv680->frame[framenr].grabstate = FRAME_ERROR;
1051 PDEBUG (2, "STV(e): FRAME_ERROR in _newframe");
1052 return -EIO;
1054 stv680->scratch[stv680->scratch_use].state = BUFFER_BUSY;
1056 bayer_unshuffle (stv680, &stv680->scratch[stv680->scratch_use]);
1058 stv680->scratch[stv680->scratch_use].state = BUFFER_UNUSED;
1059 stv680->scratch_use++;
1060 if (stv680->scratch_use >= STV680_NUMSCRATCH)
1061 stv680->scratch_use = 0;
1062 if (errors > STV680_MAX_ERRORS) {
1063 errors = 0;
1064 PDEBUG (2, "STV(i): too many errors, restarting capture");
1065 stv680_stop_stream (stv680);
1066 stv680_start_stream (stv680);
1068 } /* else */
1069 } /* while */
1070 return 0;
1073 /*********************************************************************
1074 * Video4Linux
1075 *********************************************************************/
1077 static int stv_open (struct inode *inode, struct file *file)
1079 struct video_device *dev = video_devdata(file);
1080 struct usb_stv *stv680 = video_get_drvdata(dev);
1081 int err = 0;
1083 /* we are called with the BKL held */
1084 stv680->user = 1;
1085 err = stv_init (stv680); /* main initialization routine for camera */
1087 if (err >= 0) {
1088 stv680->fbuf = rvmalloc (stv680->maxframesize * STV680_NUMFRAMES);
1089 if (!stv680->fbuf) {
1090 PDEBUG (0, "STV(e): Could not rvmalloc frame bufer");
1091 err = -ENOMEM;
1093 file->private_data = dev;
1095 if (err)
1096 stv680->user = 0;
1098 return err;
1101 static int stv_close (struct inode *inode, struct file *file)
1103 struct video_device *dev = file->private_data;
1104 struct usb_stv *stv680 = video_get_drvdata(dev);
1105 int i;
1107 for (i = 0; i < STV680_NUMFRAMES; i++)
1108 stv680->frame[i].grabstate = FRAME_UNUSED;
1109 if (stv680->streaming)
1110 stv680_stop_stream (stv680);
1112 if ((i = stv_stop_video (stv680)) < 0)
1113 PDEBUG (1, "STV(e): stop_video failed in stv_close");
1115 rvfree (stv680->fbuf, stv680->maxframesize * STV680_NUMFRAMES);
1116 stv680->user = 0;
1118 if (stv680->removed) {
1119 kfree(stv680);
1120 stv680 = NULL;
1121 PDEBUG (0, "STV(i): device unregistered");
1123 file->private_data = NULL;
1124 return 0;
1127 static int stv680_do_ioctl (struct inode *inode, struct file *file,
1128 unsigned int cmd, void *arg)
1130 struct video_device *vdev = file->private_data;
1131 struct usb_stv *stv680 = video_get_drvdata(vdev);
1133 if (!stv680->udev)
1134 return -EIO;
1136 switch (cmd) {
1137 case VIDIOCGCAP:{
1138 struct video_capability *b = arg;
1140 strcpy (b->name, stv680->camera_name);
1141 b->type = VID_TYPE_CAPTURE;
1142 b->channels = 1;
1143 b->audios = 0;
1144 b->maxwidth = stv680->maxwidth;
1145 b->maxheight = stv680->maxheight;
1146 b->minwidth = stv680->maxwidth / 2;
1147 b->minheight = stv680->maxheight / 2;
1148 return 0;
1150 case VIDIOCGCHAN:{
1151 struct video_channel *v = arg;
1153 if (v->channel != 0)
1154 return -EINVAL;
1155 v->flags = 0;
1156 v->tuners = 0;
1157 v->type = VIDEO_TYPE_CAMERA;
1158 strcpy (v->name, "STV Camera");
1159 return 0;
1161 case VIDIOCSCHAN:{
1162 struct video_channel *v = arg;
1163 if (v->channel != 0)
1164 return -EINVAL;
1165 return 0;
1167 case VIDIOCGPICT:{
1168 struct video_picture *p = arg;
1170 stv680_get_pict (stv680, p);
1171 return 0;
1173 case VIDIOCSPICT:{
1174 struct video_picture *p = arg;
1176 if (stv680_set_pict (stv680, p))
1177 return -EINVAL;
1178 return 0;
1180 case VIDIOCSWIN:{
1181 struct video_window *vw = arg;
1183 if (vw->flags)
1184 return -EINVAL;
1185 if (vw->clipcount)
1186 return -EINVAL;
1187 if (vw->width != stv680->vwidth) {
1188 if (stv680_set_size (stv680, vw->width, vw->height)) {
1189 PDEBUG (2, "STV(e): failed (from user) set size in VIDIOCSWIN");
1190 return -EINVAL;
1193 return 0;
1195 case VIDIOCGWIN:{
1196 struct video_window *vw = arg;
1198 vw->x = 0; /* FIXME */
1199 vw->y = 0;
1200 vw->chromakey = 0;
1201 vw->flags = 0;
1202 vw->clipcount = 0;
1203 vw->width = stv680->vwidth;
1204 vw->height = stv680->vheight;
1205 return 0;
1207 case VIDIOCGMBUF:{
1208 struct video_mbuf *vm = arg;
1209 int i;
1211 memset (vm, 0, sizeof (*vm));
1212 vm->size = STV680_NUMFRAMES * stv680->maxframesize;
1213 vm->frames = STV680_NUMFRAMES;
1214 for (i = 0; i < STV680_NUMFRAMES; i++)
1215 vm->offsets[i] = stv680->maxframesize * i;
1216 return 0;
1218 case VIDIOCMCAPTURE:{
1219 struct video_mmap *vm = arg;
1221 if (vm->format != STV_VIDEO_PALETTE) {
1222 PDEBUG (2, "STV(i): VIDIOCMCAPTURE vm.format (%i) != VIDEO_PALETTE (%i)",
1223 vm->format, STV_VIDEO_PALETTE);
1224 if ((vm->format == 3) && (swapRGB_on == 0)) {
1225 PDEBUG (2, "STV(i): VIDIOCMCAPTURE swapRGB is (auto) ON");
1226 /* this may fix those apps (e.g., xawtv) that want BGR */
1227 swapRGB = 1;
1229 return -EINVAL;
1231 if (vm->frame >= STV680_NUMFRAMES) {
1232 PDEBUG (2, "STV(e): VIDIOCMCAPTURE vm.frame > NUMFRAMES");
1233 return -EINVAL;
1235 if ((stv680->frame[vm->frame].grabstate == FRAME_ERROR)
1236 || (stv680->frame[vm->frame].grabstate == FRAME_GRABBING)) {
1237 PDEBUG (2, "STV(e): VIDIOCMCAPTURE grabstate (%i) error",
1238 stv680->frame[vm->frame].grabstate);
1239 return -EBUSY;
1241 /* Is this according to the v4l spec??? */
1242 if (stv680->vwidth != vm->width) {
1243 if (stv680_set_size (stv680, vm->width, vm->height)) {
1244 PDEBUG (2, "STV(e): VIDIOCMCAPTURE set_size failed");
1245 return -EINVAL;
1248 stv680->frame[vm->frame].grabstate = FRAME_READY;
1250 if (!stv680->streaming)
1251 stv680_start_stream (stv680);
1253 return 0;
1255 case VIDIOCSYNC:{
1256 int *frame = arg;
1257 int ret = 0;
1259 if (*frame < 0 || *frame >= STV680_NUMFRAMES) {
1260 PDEBUG (2, "STV(e): Bad frame # in VIDIOCSYNC");
1261 return -EINVAL;
1263 ret = stv680_newframe (stv680, *frame);
1264 stv680->frame[*frame].grabstate = FRAME_UNUSED;
1265 return ret;
1267 case VIDIOCGFBUF:{
1268 struct video_buffer *vb = arg;
1270 memset (vb, 0, sizeof (*vb));
1271 return 0;
1273 case VIDIOCKEY:
1274 return 0;
1275 case VIDIOCCAPTURE:
1277 PDEBUG (2, "STV(e): VIDIOCCAPTURE failed");
1278 return -EINVAL;
1280 case VIDIOCSFBUF:
1281 case VIDIOCGTUNER:
1282 case VIDIOCSTUNER:
1283 case VIDIOCGFREQ:
1284 case VIDIOCSFREQ:
1285 case VIDIOCGAUDIO:
1286 case VIDIOCSAUDIO:
1287 return -EINVAL;
1288 default:
1289 return -ENOIOCTLCMD;
1290 } /* end switch */
1292 return 0;
1295 static int stv680_ioctl(struct inode *inode, struct file *file,
1296 unsigned int cmd, unsigned long arg)
1298 return video_usercopy(inode, file, cmd, arg, stv680_do_ioctl);
1301 static int stv680_mmap (struct file *file, struct vm_area_struct *vma)
1303 struct video_device *dev = file->private_data;
1304 struct usb_stv *stv680 = video_get_drvdata(dev);
1305 unsigned long start = vma->vm_start;
1306 unsigned long size = vma->vm_end-vma->vm_start;
1307 unsigned long page, pos;
1309 mutex_lock(&stv680->lock);
1311 if (stv680->udev == NULL) {
1312 mutex_unlock(&stv680->lock);
1313 return -EIO;
1315 if (size > (((STV680_NUMFRAMES * stv680->maxframesize) + PAGE_SIZE - 1)
1316 & ~(PAGE_SIZE - 1))) {
1317 mutex_unlock(&stv680->lock);
1318 return -EINVAL;
1320 pos = (unsigned long) stv680->fbuf;
1321 while (size > 0) {
1322 page = vmalloc_to_pfn((void *)pos);
1323 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED)) {
1324 mutex_unlock(&stv680->lock);
1325 return -EAGAIN;
1327 start += PAGE_SIZE;
1328 pos += PAGE_SIZE;
1329 if (size > PAGE_SIZE)
1330 size -= PAGE_SIZE;
1331 else
1332 size = 0;
1334 mutex_unlock(&stv680->lock);
1336 return 0;
1339 static ssize_t stv680_read (struct file *file, char __user *buf,
1340 size_t count, loff_t *ppos)
1342 struct video_device *dev = file->private_data;
1343 unsigned long int realcount = count;
1344 int ret = 0;
1345 struct usb_stv *stv680 = video_get_drvdata(dev);
1346 unsigned long int i;
1348 if (STV680_NUMFRAMES != 2) {
1349 PDEBUG (0, "STV(e): STV680_NUMFRAMES needs to be 2!");
1350 return -1;
1352 if (stv680->udev == NULL)
1353 return -EIO;
1354 if (realcount > (stv680->vwidth * stv680->vheight * 3))
1355 realcount = stv680->vwidth * stv680->vheight * 3;
1357 /* Shouldn't happen: */
1358 if (stv680->frame[0].grabstate == FRAME_GRABBING) {
1359 PDEBUG (2, "STV(e): FRAME_GRABBING in stv680_read");
1360 return -EBUSY;
1362 stv680->frame[0].grabstate = FRAME_READY;
1363 stv680->frame[1].grabstate = FRAME_UNUSED;
1364 stv680->curframe = 0;
1366 if (!stv680->streaming)
1367 stv680_start_stream (stv680);
1369 if (!stv680->streaming) {
1370 ret = stv680_newframe (stv680, 0); /* ret should = 0 */
1373 ret = stv680_newframe (stv680, 0);
1375 if (!ret) {
1376 if ((i = copy_to_user (buf, stv680->frame[0].data, realcount)) != 0) {
1377 PDEBUG (2, "STV(e): copy_to_user frame 0 failed, ret count = %li", i);
1378 return -EFAULT;
1380 } else {
1381 realcount = ret;
1383 stv680->frame[0].grabstate = FRAME_UNUSED;
1384 return realcount;
1385 } /* stv680_read */
1387 static const struct file_operations stv680_fops = {
1388 .owner = THIS_MODULE,
1389 .open = stv_open,
1390 .release = stv_close,
1391 .read = stv680_read,
1392 .mmap = stv680_mmap,
1393 .ioctl = stv680_ioctl,
1394 .compat_ioctl = v4l_compat_ioctl32,
1395 .llseek = no_llseek,
1397 static struct video_device stv680_template = {
1398 .owner = THIS_MODULE,
1399 .name = "STV0680 USB camera",
1400 .type = VID_TYPE_CAPTURE,
1401 .fops = &stv680_fops,
1402 .release = video_device_release,
1403 .minor = -1,
1406 static int stv680_probe (struct usb_interface *intf, const struct usb_device_id *id)
1408 struct usb_device *dev = interface_to_usbdev(intf);
1409 struct usb_host_interface *interface;
1410 struct usb_stv *stv680 = NULL;
1411 char *camera_name = NULL;
1412 int retval = 0;
1414 /* We don't handle multi-config cameras */
1415 if (dev->descriptor.bNumConfigurations != 1) {
1416 PDEBUG (0, "STV(e): Number of Configurations != 1");
1417 return -ENODEV;
1420 interface = &intf->altsetting[0];
1421 /* Is it a STV680? */
1422 if ((le16_to_cpu(dev->descriptor.idVendor) == USB_PENCAM_VENDOR_ID) &&
1423 (le16_to_cpu(dev->descriptor.idProduct) == USB_PENCAM_PRODUCT_ID)) {
1424 camera_name = "STV0680";
1425 PDEBUG (0, "STV(i): STV0680 camera found.");
1426 } else if ((le16_to_cpu(dev->descriptor.idVendor) == USB_CREATIVEGOMINI_VENDOR_ID) &&
1427 (le16_to_cpu(dev->descriptor.idProduct) == USB_CREATIVEGOMINI_PRODUCT_ID)) {
1428 camera_name = "Creative WebCam Go Mini";
1429 PDEBUG (0, "STV(i): Creative WebCam Go Mini found.");
1430 } else {
1431 PDEBUG (0, "STV(e): Vendor/Product ID do not match STV0680 or Creative WebCam Go Mini values.");
1432 PDEBUG (0, "STV(e): Check that the STV0680 or Creative WebCam Go Mini camera is connected to the computer.");
1433 retval = -ENODEV;
1434 goto error;
1436 /* We found one */
1437 if ((stv680 = kzalloc (sizeof (*stv680), GFP_KERNEL)) == NULL) {
1438 PDEBUG (0, "STV(e): couldn't kmalloc stv680 struct.");
1439 retval = -ENOMEM;
1440 goto error;
1443 stv680->udev = dev;
1444 stv680->camera_name = camera_name;
1446 stv680->vdev = video_device_alloc();
1447 if (!stv680->vdev) {
1448 retval = -ENOMEM;
1449 goto error;
1451 memcpy(stv680->vdev, &stv680_template, sizeof(stv680_template));
1452 stv680->vdev->dev = &intf->dev;
1453 video_set_drvdata(stv680->vdev, stv680);
1455 memcpy (stv680->vdev->name, stv680->camera_name, strlen (stv680->camera_name));
1456 init_waitqueue_head (&stv680->wq);
1457 mutex_init (&stv680->lock);
1458 wmb ();
1460 if (video_register_device (stv680->vdev, VFL_TYPE_GRABBER, video_nr) == -1) {
1461 PDEBUG (0, "STV(e): video_register_device failed");
1462 retval = -EIO;
1463 goto error_vdev;
1465 PDEBUG (0, "STV(i): registered new video device: video%d", stv680->vdev->minor);
1467 usb_set_intfdata (intf, stv680);
1468 retval = stv680_create_sysfs_files(stv680->vdev);
1469 if (retval)
1470 goto error_unreg;
1471 return 0;
1473 error_unreg:
1474 video_unregister_device(stv680->vdev);
1475 error_vdev:
1476 video_device_release(stv680->vdev);
1477 error:
1478 kfree(stv680);
1479 return retval;
1482 static inline void usb_stv680_remove_disconnected (struct usb_stv *stv680)
1484 int i;
1486 stv680->udev = NULL;
1487 stv680->frame[0].grabstate = FRAME_ERROR;
1488 stv680->frame[1].grabstate = FRAME_ERROR;
1489 stv680->streaming = 0;
1491 wake_up_interruptible (&stv680->wq);
1493 for (i = 0; i < STV680_NUMSBUF; i++)
1494 if (stv680->urb[i]) {
1495 usb_kill_urb (stv680->urb[i]);
1496 usb_free_urb (stv680->urb[i]);
1497 stv680->urb[i] = NULL;
1498 kfree(stv680->sbuf[i].data);
1500 for (i = 0; i < STV680_NUMSCRATCH; i++)
1501 kfree(stv680->scratch[i].data);
1502 PDEBUG (0, "STV(i): %s disconnected", stv680->camera_name);
1504 /* Free the memory */
1505 kfree(stv680);
1508 static void stv680_disconnect (struct usb_interface *intf)
1510 struct usb_stv *stv680 = usb_get_intfdata (intf);
1512 usb_set_intfdata (intf, NULL);
1514 if (stv680) {
1515 /* We don't want people trying to open up the device */
1516 if (stv680->vdev) {
1517 stv680_remove_sysfs_files(stv680->vdev);
1518 video_unregister_device(stv680->vdev);
1519 stv680->vdev = NULL;
1521 if (!stv680->user) {
1522 usb_stv680_remove_disconnected (stv680);
1523 } else {
1524 stv680->removed = 1;
1529 static struct usb_driver stv680_driver = {
1530 .name = "stv680",
1531 .probe = stv680_probe,
1532 .disconnect = stv680_disconnect,
1533 .id_table = device_table
1536 /********************************************************************
1537 * Module routines
1538 ********************************************************************/
1540 static int __init usb_stv680_init (void)
1542 if (usb_register (&stv680_driver) < 0) {
1543 PDEBUG (0, "STV(e): Could not setup STV0680 driver");
1544 return -1;
1546 PDEBUG (0, "STV(i): usb camera driver version %s registering", DRIVER_VERSION);
1548 info(DRIVER_DESC " " DRIVER_VERSION);
1549 return 0;
1552 static void __exit usb_stv680_exit (void)
1554 usb_deregister (&stv680_driver);
1555 PDEBUG (0, "STV(i): driver deregistered");
1558 module_init (usb_stv680_init);
1559 module_exit (usb_stv680_exit);