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