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