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