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