OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [drivers/] [usb/] [stv680.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.