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