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