aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media
diff options
context:
space:
mode:
authorHans Verkuil <hverkuil@xs4all.nl>2010-12-25 05:27:47 -0500
committerMauro Carvalho Chehab <mchehab@redhat.com>2010-12-29 05:17:08 -0500
commit745da4280f272840976d47afba22ed853f07e1b2 (patch)
tree16808326f76543f443855b2d3b4990f3389bd49a /drivers/media
parent5772dcaa790851ec068afcd0d1f160d801b1126e (diff)
[media] usbvideo: deprecate the vicam driver
Move usbvideo to staging and mark it deprecated. Signed-off-by: Hans Verkuil <hverkuil@xs4all.nl> Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
Diffstat (limited to 'drivers/media')
-rw-r--r--drivers/media/video/Kconfig2
-rw-r--r--drivers/media/video/Makefile4
-rw-r--r--drivers/media/video/usbvideo/Kconfig12
-rw-r--r--drivers/media/video/usbvideo/Makefile2
-rw-r--r--drivers/media/video/usbvideo/usbvideo.c2230
-rw-r--r--drivers/media/video/usbvideo/usbvideo.h395
-rw-r--r--drivers/media/video/usbvideo/vicam.c952
7 files changed, 0 insertions, 3597 deletions
diff --git a/drivers/media/video/Kconfig b/drivers/media/video/Kconfig
index 4b751ef47a83..3655b30254d9 100644
--- a/drivers/media/video/Kconfig
+++ b/drivers/media/video/Kconfig
@@ -915,8 +915,6 @@ source "drivers/media/video/cx231xx/Kconfig"
915 915
916source "drivers/media/video/usbvision/Kconfig" 916source "drivers/media/video/usbvision/Kconfig"
917 917
918source "drivers/media/video/usbvideo/Kconfig"
919
920source "drivers/media/video/et61x251/Kconfig" 918source "drivers/media/video/et61x251/Kconfig"
921 919
922config USB_SE401 920config USB_SE401
diff --git a/drivers/media/video/Makefile b/drivers/media/video/Makefile
index ace7b6eaf5b8..2f2e3ac877b4 100644
--- a/drivers/media/video/Makefile
+++ b/drivers/media/video/Makefile
@@ -133,10 +133,6 @@ obj-$(CONFIG_USB_GSPCA) += gspca/
133 133
134obj-$(CONFIG_VIDEO_HDPVR) += hdpvr/ 134obj-$(CONFIG_VIDEO_HDPVR) += hdpvr/
135 135
136obj-$(CONFIG_USB_IBMCAM) += usbvideo/
137obj-$(CONFIG_USB_KONICAWC) += usbvideo/
138obj-$(CONFIG_USB_VICAM) += usbvideo/
139obj-$(CONFIG_USB_QUICKCAM_MESSENGER) += usbvideo/
140obj-$(CONFIG_USB_S2255) += s2255drv.o 136obj-$(CONFIG_USB_S2255) += s2255drv.o
141 137
142obj-$(CONFIG_VIDEO_IVTV) += ivtv/ 138obj-$(CONFIG_VIDEO_IVTV) += ivtv/
diff --git a/drivers/media/video/usbvideo/Kconfig b/drivers/media/video/usbvideo/Kconfig
deleted file mode 100644
index 357860d42a8b..000000000000
--- a/drivers/media/video/usbvideo/Kconfig
+++ /dev/null
@@ -1,12 +0,0 @@
1config VIDEO_USBVIDEO
2 tristate
3
4config USB_VICAM
5 tristate "USB 3com HomeConnect (aka vicam) support (EXPERIMENTAL)"
6 depends on VIDEO_V4L1 && EXPERIMENTAL
7 select VIDEO_USBVIDEO
8 ---help---
9 Say Y here if you have 3com homeconnect camera (vicam).
10
11 To compile this driver as a module, choose M here: the
12 module will be called vicam.
diff --git a/drivers/media/video/usbvideo/Makefile b/drivers/media/video/usbvideo/Makefile
deleted file mode 100644
index 3c99a9a2d8d3..000000000000
--- a/drivers/media/video/usbvideo/Makefile
+++ /dev/null
@@ -1,2 +0,0 @@
1obj-$(CONFIG_VIDEO_USBVIDEO) += usbvideo.o
2obj-$(CONFIG_USB_VICAM) += vicam.o
diff --git a/drivers/media/video/usbvideo/usbvideo.c b/drivers/media/video/usbvideo/usbvideo.c
deleted file mode 100644
index f1fcf9744961..000000000000
--- a/drivers/media/video/usbvideo/usbvideo.c
+++ /dev/null
@@ -1,2230 +0,0 @@
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2, or (at your option)
5 * any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15 */
16
17#include <linux/kernel.h>
18#include <linux/sched.h>
19#include <linux/list.h>
20#include <linux/slab.h>
21#include <linux/module.h>
22#include <linux/mm.h>
23#include <linux/vmalloc.h>
24#include <linux/init.h>
25#include <linux/spinlock.h>
26
27#include <asm/io.h>
28
29#include "usbvideo.h"
30
31#if defined(MAP_NR)
32#define virt_to_page(v) MAP_NR(v) /* Kernels 2.2.x */
33#endif
34
35static int video_nr = -1;
36module_param(video_nr, int, 0);
37
38/*
39 * Local prototypes.
40 */
41static void usbvideo_Disconnect(struct usb_interface *intf);
42static void usbvideo_CameraRelease(struct uvd *uvd);
43
44static long usbvideo_v4l_ioctl(struct file *file,
45 unsigned int cmd, unsigned long arg);
46static int usbvideo_v4l_mmap(struct file *file, struct vm_area_struct *vma);
47static int usbvideo_v4l_open(struct file *file);
48static ssize_t usbvideo_v4l_read(struct file *file, char __user *buf,
49 size_t count, loff_t *ppos);
50static int usbvideo_v4l_close(struct file *file);
51
52static int usbvideo_StartDataPump(struct uvd *uvd);
53static void usbvideo_StopDataPump(struct uvd *uvd);
54static int usbvideo_GetFrame(struct uvd *uvd, int frameNum);
55static int usbvideo_NewFrame(struct uvd *uvd, int framenum);
56static void usbvideo_SoftwareContrastAdjustment(struct uvd *uvd,
57 struct usbvideo_frame *frame);
58
59/*******************************/
60/* Memory management functions */
61/*******************************/
62static void *usbvideo_rvmalloc(unsigned long size)
63{
64 void *mem;
65 unsigned long adr;
66
67 size = PAGE_ALIGN(size);
68 mem = vmalloc_32(size);
69 if (!mem)
70 return NULL;
71
72 memset(mem, 0, size); /* Clear the ram out, no junk to the user */
73 adr = (unsigned long) mem;
74 while (size > 0) {
75 SetPageReserved(vmalloc_to_page((void *)adr));
76 adr += PAGE_SIZE;
77 size -= PAGE_SIZE;
78 }
79
80 return mem;
81}
82
83static void usbvideo_rvfree(void *mem, unsigned long size)
84{
85 unsigned long adr;
86
87 if (!mem)
88 return;
89
90 adr = (unsigned long) mem;
91 while ((long) size > 0) {
92 ClearPageReserved(vmalloc_to_page((void *)adr));
93 adr += PAGE_SIZE;
94 size -= PAGE_SIZE;
95 }
96 vfree(mem);
97}
98
99static void RingQueue_Initialize(struct RingQueue *rq)
100{
101 assert(rq != NULL);
102 init_waitqueue_head(&rq->wqh);
103}
104
105static void RingQueue_Allocate(struct RingQueue *rq, int rqLen)
106{
107 /* Make sure the requested size is a power of 2 and
108 round up if necessary. This allows index wrapping
109 using masks rather than modulo */
110
111 int i = 1;
112 assert(rq != NULL);
113 assert(rqLen > 0);
114
115 while(rqLen >> i)
116 i++;
117 if(rqLen != 1 << (i-1))
118 rqLen = 1 << i;
119
120 rq->length = rqLen;
121 rq->ri = rq->wi = 0;
122 rq->queue = usbvideo_rvmalloc(rq->length);
123 assert(rq->queue != NULL);
124}
125
126static int RingQueue_IsAllocated(const struct RingQueue *rq)
127{
128 if (rq == NULL)
129 return 0;
130 return (rq->queue != NULL) && (rq->length > 0);
131}
132
133static void RingQueue_Free(struct RingQueue *rq)
134{
135 assert(rq != NULL);
136 if (RingQueue_IsAllocated(rq)) {
137 usbvideo_rvfree(rq->queue, rq->length);
138 rq->queue = NULL;
139 rq->length = 0;
140 }
141}
142
143int RingQueue_Dequeue(struct RingQueue *rq, unsigned char *dst, int len)
144{
145 int rql, toread;
146
147 assert(rq != NULL);
148 assert(dst != NULL);
149
150 rql = RingQueue_GetLength(rq);
151 if(!rql)
152 return 0;
153
154 /* Clip requested length to available data */
155 if(len > rql)
156 len = rql;
157
158 toread = len;
159 if(rq->ri > rq->wi) {
160 /* Read data from tail */
161 int read = (toread < (rq->length - rq->ri)) ? toread : rq->length - rq->ri;
162 memcpy(dst, rq->queue + rq->ri, read);
163 toread -= read;
164 dst += read;
165 rq->ri = (rq->ri + read) & (rq->length-1);
166 }
167 if(toread) {
168 /* Read data from head */
169 memcpy(dst, rq->queue + rq->ri, toread);
170 rq->ri = (rq->ri + toread) & (rq->length-1);
171 }
172 return len;
173}
174
175EXPORT_SYMBOL(RingQueue_Dequeue);
176
177int RingQueue_Enqueue(struct RingQueue *rq, const unsigned char *cdata, int n)
178{
179 int enqueued = 0;
180
181 assert(rq != NULL);
182 assert(cdata != NULL);
183 assert(rq->length > 0);
184 while (n > 0) {
185 int m, q_avail;
186
187 /* Calculate the largest chunk that fits the tail of the ring */
188 q_avail = rq->length - rq->wi;
189 if (q_avail <= 0) {
190 rq->wi = 0;
191 q_avail = rq->length;
192 }
193 m = n;
194 assert(q_avail > 0);
195 if (m > q_avail)
196 m = q_avail;
197
198 memcpy(rq->queue + rq->wi, cdata, m);
199 RING_QUEUE_ADVANCE_INDEX(rq, wi, m);
200 cdata += m;
201 enqueued += m;
202 n -= m;
203 }
204 return enqueued;
205}
206
207EXPORT_SYMBOL(RingQueue_Enqueue);
208
209static void RingQueue_InterruptibleSleepOn(struct RingQueue *rq)
210{
211 assert(rq != NULL);
212 interruptible_sleep_on(&rq->wqh);
213}
214
215void RingQueue_WakeUpInterruptible(struct RingQueue *rq)
216{
217 assert(rq != NULL);
218 if (waitqueue_active(&rq->wqh))
219 wake_up_interruptible(&rq->wqh);
220}
221
222EXPORT_SYMBOL(RingQueue_WakeUpInterruptible);
223
224void RingQueue_Flush(struct RingQueue *rq)
225{
226 assert(rq != NULL);
227 rq->ri = 0;
228 rq->wi = 0;
229}
230
231EXPORT_SYMBOL(RingQueue_Flush);
232
233
234/*
235 * usbvideo_VideosizeToString()
236 *
237 * This procedure converts given videosize value to readable string.
238 *
239 * History:
240 * 07-Aug-2000 Created.
241 * 19-Oct-2000 Reworked for usbvideo module.
242 */
243static void usbvideo_VideosizeToString(char *buf, int bufLen, videosize_t vs)
244{
245 char tmp[40];
246 int n;
247
248 n = 1 + sprintf(tmp, "%ldx%ld", VIDEOSIZE_X(vs), VIDEOSIZE_Y(vs));
249 assert(n < sizeof(tmp));
250 if ((buf == NULL) || (bufLen < n))
251 err("usbvideo_VideosizeToString: buffer is too small.");
252 else
253 memmove(buf, tmp, n);
254}
255
256/*
257 * usbvideo_OverlayChar()
258 *
259 * History:
260 * 01-Feb-2000 Created.
261 */
262static void usbvideo_OverlayChar(struct uvd *uvd, struct usbvideo_frame *frame,
263 int x, int y, int ch)
264{
265 static const unsigned short digits[16] = {
266 0xF6DE, /* 0 */
267 0x2492, /* 1 */
268 0xE7CE, /* 2 */
269 0xE79E, /* 3 */
270 0xB792, /* 4 */
271 0xF39E, /* 5 */
272 0xF3DE, /* 6 */
273 0xF492, /* 7 */
274 0xF7DE, /* 8 */
275 0xF79E, /* 9 */
276 0x77DA, /* a */
277 0xD75C, /* b */
278 0xF24E, /* c */
279 0xD6DC, /* d */
280 0xF34E, /* e */
281 0xF348 /* f */
282 };
283 unsigned short digit;
284 int ix, iy;
285 int value;
286
287 if ((uvd == NULL) || (frame == NULL))
288 return;
289
290 value = hex_to_bin(ch);
291 if (value < 0)
292 return;
293 digit = digits[value];
294
295 for (iy=0; iy < 5; iy++) {
296 for (ix=0; ix < 3; ix++) {
297 if (digit & 0x8000) {
298 if (uvd->paletteBits & (1L << VIDEO_PALETTE_RGB24)) {
299/* TODO */ RGB24_PUTPIXEL(frame, x+ix, y+iy, 0xFF, 0xFF, 0xFF);
300 }
301 }
302 digit = digit << 1;
303 }
304 }
305}
306
307/*
308 * usbvideo_OverlayString()
309 *
310 * History:
311 * 01-Feb-2000 Created.
312 */
313static void usbvideo_OverlayString(struct uvd *uvd, struct usbvideo_frame *frame,
314 int x, int y, const char *str)
315{
316 while (*str) {
317 usbvideo_OverlayChar(uvd, frame, x, y, *str);
318 str++;
319 x += 4; /* 3 pixels character + 1 space */
320 }
321}
322
323/*
324 * usbvideo_OverlayStats()
325 *
326 * Overlays important debugging information.
327 *
328 * History:
329 * 01-Feb-2000 Created.
330 */
331static void usbvideo_OverlayStats(struct uvd *uvd, struct usbvideo_frame *frame)
332{
333 const int y_diff = 8;
334 char tmp[16];
335 int x = 10, y=10;
336 long i, j, barLength;
337 const int qi_x1 = 60, qi_y1 = 10;
338 const int qi_x2 = VIDEOSIZE_X(frame->request) - 10, qi_h = 10;
339
340 /* Call the user callback, see if we may proceed after that */
341 if (VALID_CALLBACK(uvd, overlayHook)) {
342 if (GET_CALLBACK(uvd, overlayHook)(uvd, frame) < 0)
343 return;
344 }
345
346 /*
347 * We draw a (mostly) hollow rectangle with qi_xxx coordinates.
348 * Left edge symbolizes the queue index 0; right edge symbolizes
349 * the full capacity of the queue.
350 */
351 barLength = qi_x2 - qi_x1 - 2;
352 if ((barLength > 10) && (uvd->paletteBits & (1L << VIDEO_PALETTE_RGB24))) {
353/* TODO */ long u_lo, u_hi, q_used;
354 long m_ri, m_wi, m_lo, m_hi;
355
356 /*
357 * Determine fill zones (used areas of the queue):
358 * 0 xxxxxxx u_lo ...... uvd->dp.ri xxxxxxxx u_hi ..... uvd->dp.length
359 *
360 * if u_lo < 0 then there is no first filler.
361 */
362
363 q_used = RingQueue_GetLength(&uvd->dp);
364 if ((uvd->dp.ri + q_used) >= uvd->dp.length) {
365 u_hi = uvd->dp.length;
366 u_lo = (q_used + uvd->dp.ri) & (uvd->dp.length-1);
367 } else {
368 u_hi = (q_used + uvd->dp.ri);
369 u_lo = -1;
370 }
371
372 /* Convert byte indices into screen units */
373 m_ri = qi_x1 + ((barLength * uvd->dp.ri) / uvd->dp.length);
374 m_wi = qi_x1 + ((barLength * uvd->dp.wi) / uvd->dp.length);
375 m_lo = (u_lo > 0) ? (qi_x1 + ((barLength * u_lo) / uvd->dp.length)) : -1;
376 m_hi = qi_x1 + ((barLength * u_hi) / uvd->dp.length);
377
378 for (j=qi_y1; j < (qi_y1 + qi_h); j++) {
379 for (i=qi_x1; i < qi_x2; i++) {
380 /* Draw border lines */
381 if ((j == qi_y1) || (j == (qi_y1 + qi_h - 1)) ||
382 (i == qi_x1) || (i == (qi_x2 - 1))) {
383 RGB24_PUTPIXEL(frame, i, j, 0xFF, 0xFF, 0xFF);
384 continue;
385 }
386 /* For all other points the Y coordinate does not matter */
387 if ((i >= m_ri) && (i <= (m_ri + 3))) {
388 RGB24_PUTPIXEL(frame, i, j, 0x00, 0xFF, 0x00);
389 } else if ((i >= m_wi) && (i <= (m_wi + 3))) {
390 RGB24_PUTPIXEL(frame, i, j, 0xFF, 0x00, 0x00);
391 } else if ((i < m_lo) || ((i > m_ri) && (i < m_hi)))
392 RGB24_PUTPIXEL(frame, i, j, 0x00, 0x00, 0xFF);
393 }
394 }
395 }
396
397 sprintf(tmp, "%8lx", uvd->stats.frame_num);
398 usbvideo_OverlayString(uvd, frame, x, y, tmp);
399 y += y_diff;
400
401 sprintf(tmp, "%8lx", uvd->stats.urb_count);
402 usbvideo_OverlayString(uvd, frame, x, y, tmp);
403 y += y_diff;
404
405 sprintf(tmp, "%8lx", uvd->stats.urb_length);
406 usbvideo_OverlayString(uvd, frame, x, y, tmp);
407 y += y_diff;
408
409 sprintf(tmp, "%8lx", uvd->stats.data_count);
410 usbvideo_OverlayString(uvd, frame, x, y, tmp);
411 y += y_diff;
412
413 sprintf(tmp, "%8lx", uvd->stats.header_count);
414 usbvideo_OverlayString(uvd, frame, x, y, tmp);
415 y += y_diff;
416
417 sprintf(tmp, "%8lx", uvd->stats.iso_skip_count);
418 usbvideo_OverlayString(uvd, frame, x, y, tmp);
419 y += y_diff;
420
421 sprintf(tmp, "%8lx", uvd->stats.iso_err_count);
422 usbvideo_OverlayString(uvd, frame, x, y, tmp);
423 y += y_diff;
424
425 sprintf(tmp, "%8x", uvd->vpic.colour);
426 usbvideo_OverlayString(uvd, frame, x, y, tmp);
427 y += y_diff;
428
429 sprintf(tmp, "%8x", uvd->vpic.hue);
430 usbvideo_OverlayString(uvd, frame, x, y, tmp);
431 y += y_diff;
432
433 sprintf(tmp, "%8x", uvd->vpic.brightness >> 8);
434 usbvideo_OverlayString(uvd, frame, x, y, tmp);
435 y += y_diff;
436
437 sprintf(tmp, "%8x", uvd->vpic.contrast >> 12);
438 usbvideo_OverlayString(uvd, frame, x, y, tmp);
439 y += y_diff;
440
441 sprintf(tmp, "%8d", uvd->vpic.whiteness >> 8);
442 usbvideo_OverlayString(uvd, frame, x, y, tmp);
443 y += y_diff;
444}
445
446/*
447 * usbvideo_ReportStatistics()
448 *
449 * This procedure prints packet and transfer statistics.
450 *
451 * History:
452 * 14-Jan-2000 Corrected default multiplier.
453 */
454static void usbvideo_ReportStatistics(const struct uvd *uvd)
455{
456 if ((uvd != NULL) && (uvd->stats.urb_count > 0)) {
457 unsigned long allPackets, badPackets, goodPackets, percent;
458 allPackets = uvd->stats.urb_count * CAMERA_URB_FRAMES;
459 badPackets = uvd->stats.iso_skip_count + uvd->stats.iso_err_count;
460 goodPackets = allPackets - badPackets;
461 /* Calculate percentage wisely, remember integer limits */
462 assert(allPackets != 0);
463 if (goodPackets < (((unsigned long)-1)/100))
464 percent = (100 * goodPackets) / allPackets;
465 else
466 percent = goodPackets / (allPackets / 100);
467 dev_info(&uvd->dev->dev,
468 "Packet Statistics: Total=%lu. Empty=%lu. Usage=%lu%%\n",
469 allPackets, badPackets, percent);
470 if (uvd->iso_packet_len > 0) {
471 unsigned long allBytes, xferBytes;
472 char multiplier = ' ';
473 allBytes = allPackets * uvd->iso_packet_len;
474 xferBytes = uvd->stats.data_count;
475 assert(allBytes != 0);
476 if (xferBytes < (((unsigned long)-1)/100))
477 percent = (100 * xferBytes) / allBytes;
478 else
479 percent = xferBytes / (allBytes / 100);
480 /* Scale xferBytes for easy reading */
481 if (xferBytes > 10*1024) {
482 xferBytes /= 1024;
483 multiplier = 'K';
484 if (xferBytes > 10*1024) {
485 xferBytes /= 1024;
486 multiplier = 'M';
487 if (xferBytes > 10*1024) {
488 xferBytes /= 1024;
489 multiplier = 'G';
490 if (xferBytes > 10*1024) {
491 xferBytes /= 1024;
492 multiplier = 'T';
493 }
494 }
495 }
496 }
497 dev_info(&uvd->dev->dev,
498 "Transfer Statistics: Transferred=%lu%cB Usage=%lu%%\n",
499 xferBytes, multiplier, percent);
500 }
501 }
502}
503
504/*
505 * usbvideo_TestPattern()
506 *
507 * Procedure forms a test pattern (yellow grid on blue background).
508 *
509 * Parameters:
510 * fullframe: if TRUE then entire frame is filled, otherwise the procedure
511 * continues from the current scanline.
512 * pmode 0: fill the frame with solid blue color (like on VCR or TV)
513 * 1: Draw a colored grid
514 *
515 * History:
516 * 01-Feb-2000 Created.
517 */
518void usbvideo_TestPattern(struct uvd *uvd, int fullframe, int pmode)
519{
520 struct usbvideo_frame *frame;
521 int num_cell = 0;
522 int scan_length = 0;
523 static int num_pass;
524
525 if (uvd == NULL) {
526 err("%s: uvd == NULL", __func__);
527 return;
528 }
529 if ((uvd->curframe < 0) || (uvd->curframe >= USBVIDEO_NUMFRAMES)) {
530 err("%s: uvd->curframe=%d.", __func__, uvd->curframe);
531 return;
532 }
533
534 /* Grab the current frame */
535 frame = &uvd->frame[uvd->curframe];
536
537 /* Optionally start at the beginning */
538 if (fullframe) {
539 frame->curline = 0;
540 frame->seqRead_Length = 0;
541 }
542#if 0
543 { /* For debugging purposes only */
544 char tmp[20];
545 usbvideo_VideosizeToString(tmp, sizeof(tmp), frame->request);
546 dev_info(&uvd->dev->dev, "testpattern: frame=%s\n", tmp);
547 }
548#endif
549 /* Form every scan line */
550 for (; frame->curline < VIDEOSIZE_Y(frame->request); frame->curline++) {
551 int i;
552 unsigned char *f = frame->data +
553 (VIDEOSIZE_X(frame->request) * V4L_BYTES_PER_PIXEL * frame->curline);
554 for (i=0; i < VIDEOSIZE_X(frame->request); i++) {
555 unsigned char cb=0x80;
556 unsigned char cg = 0;
557 unsigned char cr = 0;
558
559 if (pmode == 1) {
560 if (frame->curline % 32 == 0)
561 cb = 0, cg = cr = 0xFF;
562 else if (i % 32 == 0) {
563 if (frame->curline % 32 == 1)
564 num_cell++;
565 cb = 0, cg = cr = 0xFF;
566 } else {
567 cb = ((num_cell*7) + num_pass) & 0xFF;
568 cg = ((num_cell*5) + num_pass*2) & 0xFF;
569 cr = ((num_cell*3) + num_pass*3) & 0xFF;
570 }
571 } else {
572 /* Just the blue screen */
573 }
574
575 *f++ = cb;
576 *f++ = cg;
577 *f++ = cr;
578 scan_length += 3;
579 }
580 }
581
582 frame->frameState = FrameState_Done;
583 frame->seqRead_Length += scan_length;
584 ++num_pass;
585
586 /* We do this unconditionally, regardless of FLAGS_OVERLAY_STATS */
587 usbvideo_OverlayStats(uvd, frame);
588}
589
590EXPORT_SYMBOL(usbvideo_TestPattern);
591
592
593#ifdef DEBUG
594/*
595 * usbvideo_HexDump()
596 *
597 * A debugging tool. Prints hex dumps.
598 *
599 * History:
600 * 29-Jul-2000 Added printing of offsets.
601 */
602void usbvideo_HexDump(const unsigned char *data, int len)
603{
604 const int bytes_per_line = 32;
605 char tmp[128]; /* 32*3 + 5 */
606 int i, k;
607
608 for (i=k=0; len > 0; i++, len--) {
609 if (i > 0 && ((i % bytes_per_line) == 0)) {
610 printk("%s\n", tmp);
611 k=0;
612 }
613 if ((i % bytes_per_line) == 0)
614 k += sprintf(&tmp[k], "%04x: ", i);
615 k += sprintf(&tmp[k], "%02x ", data[i]);
616 }
617 if (k > 0)
618 printk("%s\n", tmp);
619}
620
621EXPORT_SYMBOL(usbvideo_HexDump);
622
623#endif
624
625/* ******************************************************************** */
626
627/* XXX: this piece of crap really wants some error handling.. */
628static int usbvideo_ClientIncModCount(struct uvd *uvd)
629{
630 if (uvd == NULL) {
631 err("%s: uvd == NULL", __func__);
632 return -EINVAL;
633 }
634 if (uvd->handle == NULL) {
635 err("%s: uvd->handle == NULL", __func__);
636 return -EINVAL;
637 }
638 if (!try_module_get(uvd->handle->md_module)) {
639 err("%s: try_module_get() == 0", __func__);
640 return -ENODEV;
641 }
642 return 0;
643}
644
645static void usbvideo_ClientDecModCount(struct uvd *uvd)
646{
647 if (uvd == NULL) {
648 err("%s: uvd == NULL", __func__);
649 return;
650 }
651 if (uvd->handle == NULL) {
652 err("%s: uvd->handle == NULL", __func__);
653 return;
654 }
655 if (uvd->handle->md_module == NULL) {
656 err("%s: uvd->handle->md_module == NULL", __func__);
657 return;
658 }
659 module_put(uvd->handle->md_module);
660}
661
662int usbvideo_register(
663 struct usbvideo **pCams,
664 const int num_cams,
665 const int num_extra,
666 const char *driverName,
667 const struct usbvideo_cb *cbTbl,
668 struct module *md,
669 const struct usb_device_id *id_table)
670{
671 struct usbvideo *cams;
672 int i, base_size, result;
673
674 /* Check parameters for sanity */
675 if ((num_cams <= 0) || (pCams == NULL) || (cbTbl == NULL)) {
676 err("%s: Illegal call", __func__);
677 return -EINVAL;
678 }
679
680 /* Check registration callback - must be set! */
681 if (cbTbl->probe == NULL) {
682 err("%s: probe() is required!", __func__);
683 return -EINVAL;
684 }
685
686 base_size = num_cams * sizeof(struct uvd) + sizeof(struct usbvideo);
687 cams = kzalloc(base_size, GFP_KERNEL);
688 if (cams == NULL) {
689 err("Failed to allocate %d. bytes for usbvideo struct", base_size);
690 return -ENOMEM;
691 }
692 dbg("%s: Allocated $%p (%d. bytes) for %d. cameras",
693 __func__, cams, base_size, num_cams);
694
695 /* Copy callbacks, apply defaults for those that are not set */
696 memmove(&cams->cb, cbTbl, sizeof(cams->cb));
697 if (cams->cb.getFrame == NULL)
698 cams->cb.getFrame = usbvideo_GetFrame;
699 if (cams->cb.disconnect == NULL)
700 cams->cb.disconnect = usbvideo_Disconnect;
701 if (cams->cb.startDataPump == NULL)
702 cams->cb.startDataPump = usbvideo_StartDataPump;
703 if (cams->cb.stopDataPump == NULL)
704 cams->cb.stopDataPump = usbvideo_StopDataPump;
705
706 cams->num_cameras = num_cams;
707 cams->cam = (struct uvd *) &cams[1];
708 cams->md_module = md;
709 mutex_init(&cams->lock); /* to 1 == available */
710
711 for (i = 0; i < num_cams; i++) {
712 struct uvd *up = &cams->cam[i];
713
714 up->handle = cams;
715
716 /* Allocate user_data separately because of kmalloc's limits */
717 if (num_extra > 0) {
718 up->user_size = num_cams * num_extra;
719 up->user_data = kmalloc(up->user_size, GFP_KERNEL);
720 if (up->user_data == NULL) {
721 err("%s: Failed to allocate user_data (%d. bytes)",
722 __func__, up->user_size);
723 while (i) {
724 up = &cams->cam[--i];
725 kfree(up->user_data);
726 }
727 kfree(cams);
728 return -ENOMEM;
729 }
730 dbg("%s: Allocated cams[%d].user_data=$%p (%d. bytes)",
731 __func__, i, up->user_data, up->user_size);
732 }
733 }
734
735 /*
736 * Register ourselves with USB stack.
737 */
738 strcpy(cams->drvName, (driverName != NULL) ? driverName : "Unknown");
739 cams->usbdrv.name = cams->drvName;
740 cams->usbdrv.probe = cams->cb.probe;
741 cams->usbdrv.disconnect = cams->cb.disconnect;
742 cams->usbdrv.id_table = id_table;
743
744 /*
745 * Update global handle to usbvideo. This is very important
746 * because probe() can be called before usb_register() returns.
747 * If the handle is not yet updated then the probe() will fail.
748 */
749 *pCams = cams;
750 result = usb_register(&cams->usbdrv);
751 if (result) {
752 for (i = 0; i < num_cams; i++) {
753 struct uvd *up = &cams->cam[i];
754 kfree(up->user_data);
755 }
756 kfree(cams);
757 }
758
759 return result;
760}
761
762EXPORT_SYMBOL(usbvideo_register);
763
764/*
765 * usbvideo_Deregister()
766 *
767 * Procedure frees all usbvideo and user data structures. Be warned that
768 * if you had some dynamically allocated components in ->user field then
769 * you should free them before calling here.
770 */
771void usbvideo_Deregister(struct usbvideo **pCams)
772{
773 struct usbvideo *cams;
774 int i;
775
776 if (pCams == NULL) {
777 err("%s: pCams == NULL", __func__);
778 return;
779 }
780 cams = *pCams;
781 if (cams == NULL) {
782 err("%s: cams == NULL", __func__);
783 return;
784 }
785
786 dbg("%s: Deregistering %s driver.", __func__, cams->drvName);
787 usb_deregister(&cams->usbdrv);
788
789 dbg("%s: Deallocating cams=$%p (%d. cameras)", __func__, cams, cams->num_cameras);
790 for (i=0; i < cams->num_cameras; i++) {
791 struct uvd *up = &cams->cam[i];
792 int warning = 0;
793
794 if (up->user_data != NULL) {
795 if (up->user_size <= 0)
796 ++warning;
797 } else {
798 if (up->user_size > 0)
799 ++warning;
800 }
801 if (warning) {
802 err("%s: Warning: user_data=$%p user_size=%d.",
803 __func__, up->user_data, up->user_size);
804 } else {
805 dbg("%s: Freeing %d. $%p->user_data=$%p",
806 __func__, i, up, up->user_data);
807 kfree(up->user_data);
808 }
809 }
810 /* Whole array was allocated in one chunk */
811 dbg("%s: Freed %d uvd structures",
812 __func__, cams->num_cameras);
813 kfree(cams);
814 *pCams = NULL;
815}
816
817EXPORT_SYMBOL(usbvideo_Deregister);
818
819/*
820 * usbvideo_Disconnect()
821 *
822 * This procedure stops all driver activity. Deallocation of
823 * the interface-private structure (pointed by 'ptr') is done now
824 * (if we don't have any open files) or later, when those files
825 * are closed. After that driver should be removable.
826 *
827 * This code handles surprise removal. The uvd->user is a counter which
828 * increments on open() and decrements on close(). If we see here that
829 * this counter is not 0 then we have a client who still has us opened.
830 * We set uvd->remove_pending flag as early as possible, and after that
831 * all access to the camera will gracefully fail. These failures should
832 * prompt client to (eventually) close the video device, and then - in
833 * usbvideo_v4l_close() - we decrement uvd->uvd_used and usage counter.
834 *
835 * History:
836 * 22-Jan-2000 Added polling of MOD_IN_USE to delay removal until all users gone.
837 * 27-Jan-2000 Reworked to allow pending disconnects; see xxx_close()
838 * 24-May-2000 Corrected to prevent race condition (MOD_xxx_USE_COUNT).
839 * 19-Oct-2000 Moved to usbvideo module.
840 */
841static void usbvideo_Disconnect(struct usb_interface *intf)
842{
843 struct uvd *uvd = usb_get_intfdata (intf);
844 int i;
845
846 if (uvd == NULL) {
847 err("%s($%p): Illegal call.", __func__, intf);
848 return;
849 }
850
851 usb_set_intfdata (intf, NULL);
852
853 usbvideo_ClientIncModCount(uvd);
854 if (uvd->debug > 0)
855 dev_info(&intf->dev, "%s(%p.)\n", __func__, intf);
856
857 mutex_lock(&uvd->lock);
858 uvd->remove_pending = 1; /* Now all ISO data will be ignored */
859
860 /* At this time we ask to cancel outstanding URBs */
861 GET_CALLBACK(uvd, stopDataPump)(uvd);
862
863 for (i=0; i < USBVIDEO_NUMSBUF; i++)
864 usb_free_urb(uvd->sbuf[i].urb);
865
866 usb_put_dev(uvd->dev);
867 uvd->dev = NULL; /* USB device is no more */
868
869 video_unregister_device(&uvd->vdev);
870 if (uvd->debug > 0)
871 dev_info(&intf->dev, "%s: Video unregistered.\n", __func__);
872
873 if (uvd->user)
874 dev_info(&intf->dev, "%s: In use, disconnect pending.\n",
875 __func__);
876 else
877 usbvideo_CameraRelease(uvd);
878 mutex_unlock(&uvd->lock);
879 dev_info(&intf->dev, "USB camera disconnected.\n");
880
881 usbvideo_ClientDecModCount(uvd);
882}
883
884/*
885 * usbvideo_CameraRelease()
886 *
887 * This code does final release of uvd. This happens
888 * after the device is disconnected -and- all clients
889 * closed their files.
890 *
891 * History:
892 * 27-Jan-2000 Created.
893 */
894static void usbvideo_CameraRelease(struct uvd *uvd)
895{
896 if (uvd == NULL) {
897 err("%s: Illegal call", __func__);
898 return;
899 }
900
901 RingQueue_Free(&uvd->dp);
902 if (VALID_CALLBACK(uvd, userFree))
903 GET_CALLBACK(uvd, userFree)(uvd);
904 uvd->uvd_used = 0; /* This is atomic, no need to take mutex */
905}
906
907/*
908 * usbvideo_find_struct()
909 *
910 * This code searches the array of preallocated (static) structures
911 * and returns index of the first one that isn't in use. Returns -1
912 * if there are no free structures.
913 *
914 * History:
915 * 27-Jan-2000 Created.
916 */
917static int usbvideo_find_struct(struct usbvideo *cams)
918{
919 int u, rv = -1;
920
921 if (cams == NULL) {
922 err("No usbvideo handle?");
923 return -1;
924 }
925 mutex_lock(&cams->lock);
926 for (u = 0; u < cams->num_cameras; u++) {
927 struct uvd *uvd = &cams->cam[u];
928 if (!uvd->uvd_used) /* This one is free */
929 {
930 uvd->uvd_used = 1; /* In use now */
931 mutex_init(&uvd->lock); /* to 1 == available */
932 uvd->dev = NULL;
933 rv = u;
934 break;
935 }
936 }
937 mutex_unlock(&cams->lock);
938 return rv;
939}
940
941static const struct v4l2_file_operations usbvideo_fops = {
942 .owner = THIS_MODULE,
943 .open = usbvideo_v4l_open,
944 .release =usbvideo_v4l_close,
945 .read = usbvideo_v4l_read,
946 .mmap = usbvideo_v4l_mmap,
947 .ioctl = usbvideo_v4l_ioctl,
948};
949static const struct video_device usbvideo_template = {
950 .fops = &usbvideo_fops,
951};
952
953struct uvd *usbvideo_AllocateDevice(struct usbvideo *cams)
954{
955 int i, devnum;
956 struct uvd *uvd = NULL;
957
958 if (cams == NULL) {
959 err("No usbvideo handle?");
960 return NULL;
961 }
962
963 devnum = usbvideo_find_struct(cams);
964 if (devnum == -1) {
965 err("IBM USB camera driver: Too many devices!");
966 return NULL;
967 }
968 uvd = &cams->cam[devnum];
969 dbg("Device entry #%d. at $%p", devnum, uvd);
970
971 /* Not relying upon caller we increase module counter ourselves */
972 usbvideo_ClientIncModCount(uvd);
973
974 mutex_lock(&uvd->lock);
975 for (i=0; i < USBVIDEO_NUMSBUF; i++) {
976 uvd->sbuf[i].urb = usb_alloc_urb(FRAMES_PER_DESC, GFP_KERNEL);
977 if (uvd->sbuf[i].urb == NULL) {
978 err("usb_alloc_urb(%d.) failed.", FRAMES_PER_DESC);
979 uvd->uvd_used = 0;
980 uvd = NULL;
981 goto allocate_done;
982 }
983 }
984 uvd->user=0;
985 uvd->remove_pending = 0;
986 uvd->last_error = 0;
987 RingQueue_Initialize(&uvd->dp);
988
989 /* Initialize video device structure */
990 uvd->vdev = usbvideo_template;
991 sprintf(uvd->vdev.name, "%.20s USB Camera", cams->drvName);
992 /*
993 * The client is free to overwrite those because we
994 * return control to the client's probe function right now.
995 */
996allocate_done:
997 mutex_unlock(&uvd->lock);
998 usbvideo_ClientDecModCount(uvd);
999 return uvd;
1000}
1001
1002EXPORT_SYMBOL(usbvideo_AllocateDevice);
1003
1004int usbvideo_RegisterVideoDevice(struct uvd *uvd)
1005{
1006 char tmp1[20], tmp2[20]; /* Buffers for printing */
1007
1008 if (uvd == NULL) {
1009 err("%s: Illegal call.", __func__);
1010 return -EINVAL;
1011 }
1012 if (uvd->video_endp == 0) {
1013 dev_info(&uvd->dev->dev,
1014 "%s: No video endpoint specified; data pump disabled.\n",
1015 __func__);
1016 }
1017 if (uvd->paletteBits == 0) {
1018 err("%s: No palettes specified!", __func__);
1019 return -EINVAL;
1020 }
1021 if (uvd->defaultPalette == 0) {
1022 dev_info(&uvd->dev->dev, "%s: No default palette!\n",
1023 __func__);
1024 }
1025
1026 uvd->max_frame_size = VIDEOSIZE_X(uvd->canvas) *
1027 VIDEOSIZE_Y(uvd->canvas) * V4L_BYTES_PER_PIXEL;
1028 usbvideo_VideosizeToString(tmp1, sizeof(tmp1), uvd->videosize);
1029 usbvideo_VideosizeToString(tmp2, sizeof(tmp2), uvd->canvas);
1030
1031 if (uvd->debug > 0) {
1032 dev_info(&uvd->dev->dev,
1033 "%s: iface=%d. endpoint=$%02x paletteBits=$%08lx\n",
1034 __func__, uvd->iface, uvd->video_endp,
1035 uvd->paletteBits);
1036 }
1037 if (uvd->dev == NULL) {
1038 err("%s: uvd->dev == NULL", __func__);
1039 return -EINVAL;
1040 }
1041 uvd->vdev.parent = &uvd->dev->dev;
1042 uvd->vdev.release = video_device_release_empty;
1043 if (video_register_device(&uvd->vdev, VFL_TYPE_GRABBER, video_nr) < 0) {
1044 err("%s: video_register_device failed", __func__);
1045 return -EPIPE;
1046 }
1047 if (uvd->debug > 1) {
1048 dev_info(&uvd->dev->dev,
1049 "%s: video_register_device() successful\n", __func__);
1050 }
1051
1052 dev_info(&uvd->dev->dev, "%s on %s: canvas=%s videosize=%s\n",
1053 (uvd->handle != NULL) ? uvd->handle->drvName : "???",
1054 video_device_node_name(&uvd->vdev), tmp2, tmp1);
1055
1056 usb_get_dev(uvd->dev);
1057 return 0;
1058}
1059
1060EXPORT_SYMBOL(usbvideo_RegisterVideoDevice);
1061
1062/* ******************************************************************** */
1063
1064static int usbvideo_v4l_mmap(struct file *file, struct vm_area_struct *vma)
1065{
1066 struct uvd *uvd = file->private_data;
1067 unsigned long start = vma->vm_start;
1068 unsigned long size = vma->vm_end-vma->vm_start;
1069 unsigned long page, pos;
1070
1071 if (!CAMERA_IS_OPERATIONAL(uvd))
1072 return -EFAULT;
1073
1074 if (size > (((USBVIDEO_NUMFRAMES * uvd->max_frame_size) + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1)))
1075 return -EINVAL;
1076
1077 pos = (unsigned long) uvd->fbuf;
1078 while (size > 0) {
1079 page = vmalloc_to_pfn((void *)pos);
1080 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
1081 return -EAGAIN;
1082
1083 start += PAGE_SIZE;
1084 pos += PAGE_SIZE;
1085 if (size > PAGE_SIZE)
1086 size -= PAGE_SIZE;
1087 else
1088 size = 0;
1089 }
1090
1091 return 0;
1092}
1093
1094/*
1095 * usbvideo_v4l_open()
1096 *
1097 * This is part of Video 4 Linux API. The driver can be opened by one
1098 * client only (checks internal counter 'uvdser'). The procedure
1099 * then allocates buffers needed for video processing.
1100 *
1101 * History:
1102 * 22-Jan-2000 Rewrote, moved scratch buffer allocation here. Now the
1103 * camera is also initialized here (once per connect), at
1104 * expense of V4L client (it waits on open() call).
1105 * 27-Jan-2000 Used USBVIDEO_NUMSBUF as number of URB buffers.
1106 * 24-May-2000 Corrected to prevent race condition (MOD_xxx_USE_COUNT).
1107 */
1108static int usbvideo_v4l_open(struct file *file)
1109{
1110 struct video_device *dev = video_devdata(file);
1111 struct uvd *uvd = (struct uvd *) dev;
1112 const int sb_size = FRAMES_PER_DESC * uvd->iso_packet_len;
1113 int i, errCode = 0;
1114
1115 if (uvd->debug > 1)
1116 dev_info(&uvd->dev->dev, "%s($%p)\n", __func__, dev);
1117
1118 if (usbvideo_ClientIncModCount(uvd) < 0)
1119 return -ENODEV;
1120 mutex_lock(&uvd->lock);
1121
1122 if (uvd->user) {
1123 err("%s: Someone tried to open an already opened device!", __func__);
1124 errCode = -EBUSY;
1125 } else {
1126 /* Clear statistics */
1127 memset(&uvd->stats, 0, sizeof(uvd->stats));
1128
1129 /* Clean pointers so we know if we allocated something */
1130 for (i=0; i < USBVIDEO_NUMSBUF; i++)
1131 uvd->sbuf[i].data = NULL;
1132
1133 /* Allocate memory for the frame buffers */
1134 uvd->fbuf_size = USBVIDEO_NUMFRAMES * uvd->max_frame_size;
1135 uvd->fbuf = usbvideo_rvmalloc(uvd->fbuf_size);
1136 RingQueue_Allocate(&uvd->dp, RING_QUEUE_SIZE);
1137 if ((uvd->fbuf == NULL) ||
1138 (!RingQueue_IsAllocated(&uvd->dp))) {
1139 err("%s: Failed to allocate fbuf or dp", __func__);
1140 errCode = -ENOMEM;
1141 } else {
1142 /* Allocate all buffers */
1143 for (i=0; i < USBVIDEO_NUMFRAMES; i++) {
1144 uvd->frame[i].frameState = FrameState_Unused;
1145 uvd->frame[i].data = uvd->fbuf + i*(uvd->max_frame_size);
1146 /*
1147 * Set default sizes in case IOCTL (VIDIOCMCAPTURE)
1148 * is not used (using read() instead).
1149 */
1150 uvd->frame[i].canvas = uvd->canvas;
1151 uvd->frame[i].seqRead_Index = 0;
1152 }
1153 for (i=0; i < USBVIDEO_NUMSBUF; i++) {
1154 uvd->sbuf[i].data = kmalloc(sb_size, GFP_KERNEL);
1155 if (uvd->sbuf[i].data == NULL) {
1156 errCode = -ENOMEM;
1157 break;
1158 }
1159 }
1160 }
1161 if (errCode != 0) {
1162 /* Have to free all that memory */
1163 if (uvd->fbuf != NULL) {
1164 usbvideo_rvfree(uvd->fbuf, uvd->fbuf_size);
1165 uvd->fbuf = NULL;
1166 }
1167 RingQueue_Free(&uvd->dp);
1168 for (i=0; i < USBVIDEO_NUMSBUF; i++) {
1169 kfree(uvd->sbuf[i].data);
1170 uvd->sbuf[i].data = NULL;
1171 }
1172 }
1173 }
1174
1175 /* If so far no errors then we shall start the camera */
1176 if (errCode == 0) {
1177 /* Start data pump if we have valid endpoint */
1178 if (uvd->video_endp != 0)
1179 errCode = GET_CALLBACK(uvd, startDataPump)(uvd);
1180 if (errCode == 0) {
1181 if (VALID_CALLBACK(uvd, setupOnOpen)) {
1182 if (uvd->debug > 1)
1183 dev_info(&uvd->dev->dev,
1184 "%s: setupOnOpen callback\n",
1185 __func__);
1186 errCode = GET_CALLBACK(uvd, setupOnOpen)(uvd);
1187 if (errCode < 0) {
1188 err("%s: setupOnOpen callback failed (%d.).",
1189 __func__, errCode);
1190 } else if (uvd->debug > 1) {
1191 dev_info(&uvd->dev->dev,
1192 "%s: setupOnOpen callback successful\n",
1193 __func__);
1194 }
1195 }
1196 if (errCode == 0) {
1197 uvd->settingsAdjusted = 0;
1198 if (uvd->debug > 1)
1199 dev_info(&uvd->dev->dev,
1200 "%s: Open succeeded.\n",
1201 __func__);
1202 uvd->user++;
1203 file->private_data = uvd;
1204 }
1205 }
1206 }
1207 mutex_unlock(&uvd->lock);
1208 if (errCode != 0)
1209 usbvideo_ClientDecModCount(uvd);
1210 if (uvd->debug > 0)
1211 dev_info(&uvd->dev->dev, "%s: Returning %d.\n", __func__,
1212 errCode);
1213 return errCode;
1214}
1215
1216/*
1217 * usbvideo_v4l_close()
1218 *
1219 * This is part of Video 4 Linux API. The procedure
1220 * stops streaming and deallocates all buffers that were earlier
1221 * allocated in usbvideo_v4l_open().
1222 *
1223 * History:
1224 * 22-Jan-2000 Moved scratch buffer deallocation here.
1225 * 27-Jan-2000 Used USBVIDEO_NUMSBUF as number of URB buffers.
1226 * 24-May-2000 Moved MOD_DEC_USE_COUNT outside of code that can sleep.
1227 */
1228static int usbvideo_v4l_close(struct file *file)
1229{
1230 struct video_device *dev = file->private_data;
1231 struct uvd *uvd = (struct uvd *) dev;
1232 int i;
1233
1234 if (uvd->debug > 1)
1235 dev_info(&uvd->dev->dev, "%s($%p)\n", __func__, dev);
1236
1237 mutex_lock(&uvd->lock);
1238 GET_CALLBACK(uvd, stopDataPump)(uvd);
1239 usbvideo_rvfree(uvd->fbuf, uvd->fbuf_size);
1240 uvd->fbuf = NULL;
1241 RingQueue_Free(&uvd->dp);
1242
1243 for (i=0; i < USBVIDEO_NUMSBUF; i++) {
1244 kfree(uvd->sbuf[i].data);
1245 uvd->sbuf[i].data = NULL;
1246 }
1247
1248#if USBVIDEO_REPORT_STATS
1249 usbvideo_ReportStatistics(uvd);
1250#endif
1251
1252 uvd->user--;
1253 if (uvd->remove_pending) {
1254 if (uvd->debug > 0)
1255 dev_info(&uvd->dev->dev, "%s: Final disconnect.\n",
1256 __func__);
1257 usbvideo_CameraRelease(uvd);
1258 }
1259 mutex_unlock(&uvd->lock);
1260 usbvideo_ClientDecModCount(uvd);
1261
1262 if (uvd->debug > 1)
1263 dev_info(&uvd->dev->dev, "%s: Completed.\n", __func__);
1264 file->private_data = NULL;
1265 return 0;
1266}
1267
1268/*
1269 * usbvideo_v4l_ioctl()
1270 *
1271 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
1272 *
1273 * History:
1274 * 22-Jan-2000 Corrected VIDIOCSPICT to reject unsupported settings.
1275 */
1276static long usbvideo_v4l_do_ioctl(struct file *file, unsigned int cmd, void *arg)
1277{
1278 struct uvd *uvd = file->private_data;
1279
1280 if (!CAMERA_IS_OPERATIONAL(uvd))
1281 return -EIO;
1282
1283 switch (cmd) {
1284 case VIDIOCGCAP:
1285 {
1286 struct video_capability *b = arg;
1287 *b = uvd->vcap;
1288 return 0;
1289 }
1290 case VIDIOCGCHAN:
1291 {
1292 struct video_channel *v = arg;
1293 *v = uvd->vchan;
1294 return 0;
1295 }
1296 case VIDIOCSCHAN:
1297 {
1298 struct video_channel *v = arg;
1299 if (v->channel != 0)
1300 return -EINVAL;
1301 return 0;
1302 }
1303 case VIDIOCGPICT:
1304 {
1305 struct video_picture *pic = arg;
1306 *pic = uvd->vpic;
1307 return 0;
1308 }
1309 case VIDIOCSPICT:
1310 {
1311 struct video_picture *pic = arg;
1312 /*
1313 * Use temporary 'video_picture' structure to preserve our
1314 * own settings (such as color depth, palette) that we
1315 * aren't allowing everyone (V4L client) to change.
1316 */
1317 uvd->vpic.brightness = pic->brightness;
1318 uvd->vpic.hue = pic->hue;
1319 uvd->vpic.colour = pic->colour;
1320 uvd->vpic.contrast = pic->contrast;
1321 uvd->settingsAdjusted = 0; /* Will force new settings */
1322 return 0;
1323 }
1324 case VIDIOCSWIN:
1325 {
1326 struct video_window *vw = arg;
1327
1328 if(VALID_CALLBACK(uvd, setVideoMode)) {
1329 return GET_CALLBACK(uvd, setVideoMode)(uvd, vw);
1330 }
1331
1332 if (vw->flags)
1333 return -EINVAL;
1334 if (vw->clipcount)
1335 return -EINVAL;
1336 if (vw->width != VIDEOSIZE_X(uvd->canvas))
1337 return -EINVAL;
1338 if (vw->height != VIDEOSIZE_Y(uvd->canvas))
1339 return -EINVAL;
1340
1341 return 0;
1342 }
1343 case VIDIOCGWIN:
1344 {
1345 struct video_window *vw = arg;
1346
1347 vw->x = 0;
1348 vw->y = 0;
1349 vw->width = VIDEOSIZE_X(uvd->videosize);
1350 vw->height = VIDEOSIZE_Y(uvd->videosize);
1351 vw->chromakey = 0;
1352 if (VALID_CALLBACK(uvd, getFPS))
1353 vw->flags = GET_CALLBACK(uvd, getFPS)(uvd);
1354 else
1355 vw->flags = 10; /* FIXME: do better! */
1356 return 0;
1357 }
1358 case VIDIOCGMBUF:
1359 {
1360 struct video_mbuf *vm = arg;
1361 int i;
1362
1363 memset(vm, 0, sizeof(*vm));
1364 vm->size = uvd->max_frame_size * USBVIDEO_NUMFRAMES;
1365 vm->frames = USBVIDEO_NUMFRAMES;
1366 for(i = 0; i < USBVIDEO_NUMFRAMES; i++)
1367 vm->offsets[i] = i * uvd->max_frame_size;
1368
1369 return 0;
1370 }
1371 case VIDIOCMCAPTURE:
1372 {
1373 struct video_mmap *vm = arg;
1374
1375 if (uvd->debug >= 1) {
1376 dev_info(&uvd->dev->dev,
1377 "VIDIOCMCAPTURE: frame=%d. size=%dx%d, format=%d.\n",
1378 vm->frame, vm->width, vm->height, vm->format);
1379 }
1380 /*
1381 * Check if the requested size is supported. If the requestor
1382 * requests too big a frame then we may be tricked into accessing
1383 * outside of own preallocated frame buffer (in uvd->frame).
1384 * This will cause oops or a security hole. Theoretically, we
1385 * could only clamp the size down to acceptable bounds, but then
1386 * we'd need to figure out how to insert our smaller buffer into
1387 * larger caller's buffer... this is not an easy question. So we
1388 * here just flatly reject too large requests, assuming that the
1389 * caller will resubmit with smaller size. Callers should know
1390 * what size we support (returned by VIDIOCGCAP). However vidcat,
1391 * for one, does not care and allows to ask for any size.
1392 */
1393 if ((vm->width > VIDEOSIZE_X(uvd->canvas)) ||
1394 (vm->height > VIDEOSIZE_Y(uvd->canvas))) {
1395 if (uvd->debug > 0) {
1396 dev_info(&uvd->dev->dev,
1397 "VIDIOCMCAPTURE: Size=%dx%d "
1398 "too large; allowed only up "
1399 "to %ldx%ld\n", vm->width,
1400 vm->height,
1401 VIDEOSIZE_X(uvd->canvas),
1402 VIDEOSIZE_Y(uvd->canvas));
1403 }
1404 return -EINVAL;
1405 }
1406 /* Check if the palette is supported */
1407 if (((1L << vm->format) & uvd->paletteBits) == 0) {
1408 if (uvd->debug > 0) {
1409 dev_info(&uvd->dev->dev,
1410 "VIDIOCMCAPTURE: format=%d. "
1411 "not supported "
1412 "(paletteBits=$%08lx)\n",
1413 vm->format, uvd->paletteBits);
1414 }
1415 return -EINVAL;
1416 }
1417 if ((vm->frame < 0) || (vm->frame >= USBVIDEO_NUMFRAMES)) {
1418 err("VIDIOCMCAPTURE: vm.frame=%d. !E [0-%d]", vm->frame, USBVIDEO_NUMFRAMES-1);
1419 return -EINVAL;
1420 }
1421 if (uvd->frame[vm->frame].frameState == FrameState_Grabbing) {
1422 /* Not an error - can happen */
1423 }
1424 uvd->frame[vm->frame].request = VIDEOSIZE(vm->width, vm->height);
1425 uvd->frame[vm->frame].palette = vm->format;
1426
1427 /* Mark it as ready */
1428 uvd->frame[vm->frame].frameState = FrameState_Ready;
1429
1430 return usbvideo_NewFrame(uvd, vm->frame);
1431 }
1432 case VIDIOCSYNC:
1433 {
1434 int *frameNum = arg;
1435 int ret;
1436
1437 if (*frameNum < 0 || *frameNum >= USBVIDEO_NUMFRAMES)
1438 return -EINVAL;
1439
1440 if (uvd->debug >= 1)
1441 dev_info(&uvd->dev->dev,
1442 "VIDIOCSYNC: syncing to frame %d.\n",
1443 *frameNum);
1444 if (uvd->flags & FLAGS_NO_DECODING)
1445 ret = usbvideo_GetFrame(uvd, *frameNum);
1446 else if (VALID_CALLBACK(uvd, getFrame)) {
1447 ret = GET_CALLBACK(uvd, getFrame)(uvd, *frameNum);
1448 if ((ret < 0) && (uvd->debug >= 1)) {
1449 err("VIDIOCSYNC: getFrame() returned %d.", ret);
1450 }
1451 } else {
1452 err("VIDIOCSYNC: getFrame is not set");
1453 ret = -EFAULT;
1454 }
1455
1456 /*
1457 * The frame is in FrameState_Done_Hold state. Release it
1458 * right now because its data is already mapped into
1459 * the user space and it's up to the application to
1460 * make use of it until it asks for another frame.
1461 */
1462 uvd->frame[*frameNum].frameState = FrameState_Unused;
1463 return ret;
1464 }
1465 case VIDIOCGFBUF:
1466 {
1467 struct video_buffer *vb = arg;
1468
1469 memset(vb, 0, sizeof(*vb));
1470 return 0;
1471 }
1472 case VIDIOCKEY:
1473 return 0;
1474
1475 case VIDIOCCAPTURE:
1476 return -EINVAL;
1477
1478 case VIDIOCSFBUF:
1479
1480 case VIDIOCGTUNER:
1481 case VIDIOCSTUNER:
1482
1483 case VIDIOCGFREQ:
1484 case VIDIOCSFREQ:
1485
1486 case VIDIOCGAUDIO:
1487 case VIDIOCSAUDIO:
1488 return -EINVAL;
1489
1490 default:
1491 return -ENOIOCTLCMD;
1492 }
1493 return 0;
1494}
1495
1496static long usbvideo_v4l_ioctl(struct file *file,
1497 unsigned int cmd, unsigned long arg)
1498{
1499 return video_usercopy(file, cmd, arg, usbvideo_v4l_do_ioctl);
1500}
1501
1502/*
1503 * usbvideo_v4l_read()
1504 *
1505 * This is mostly boring stuff. We simply ask for a frame and when it
1506 * arrives copy all the video data from it into user space. There is
1507 * no obvious need to override this method.
1508 *
1509 * History:
1510 * 20-Oct-2000 Created.
1511 * 01-Nov-2000 Added mutex (uvd->lock).
1512 */
1513static ssize_t usbvideo_v4l_read(struct file *file, char __user *buf,
1514 size_t count, loff_t *ppos)
1515{
1516 struct uvd *uvd = file->private_data;
1517 int noblock = file->f_flags & O_NONBLOCK;
1518 int frmx = -1, i;
1519 struct usbvideo_frame *frame;
1520
1521 if (!CAMERA_IS_OPERATIONAL(uvd) || (buf == NULL))
1522 return -EFAULT;
1523
1524 if (uvd->debug >= 1)
1525 dev_info(&uvd->dev->dev,
1526 "%s: %Zd. bytes, noblock=%d.\n",
1527 __func__, count, noblock);
1528
1529 mutex_lock(&uvd->lock);
1530
1531 /* See if a frame is completed, then use it. */
1532 for(i = 0; i < USBVIDEO_NUMFRAMES; i++) {
1533 if ((uvd->frame[i].frameState == FrameState_Done) ||
1534 (uvd->frame[i].frameState == FrameState_Done_Hold) ||
1535 (uvd->frame[i].frameState == FrameState_Error)) {
1536 frmx = i;
1537 break;
1538 }
1539 }
1540
1541 /* FIXME: If we don't start a frame here then who ever does? */
1542 if (noblock && (frmx == -1)) {
1543 count = -EAGAIN;
1544 goto read_done;
1545 }
1546
1547 /*
1548 * If no FrameState_Done, look for a FrameState_Grabbing state.
1549 * See if a frame is in process (grabbing), then use it.
1550 * We will need to wait until it becomes cooked, of course.
1551 */
1552 if (frmx == -1) {
1553 for(i = 0; i < USBVIDEO_NUMFRAMES; i++) {
1554 if (uvd->frame[i].frameState == FrameState_Grabbing) {
1555 frmx = i;
1556 break;
1557 }
1558 }
1559 }
1560
1561 /*
1562 * If no frame is active, start one. We don't care which one
1563 * it will be, so #0 is as good as any.
1564 * In read access mode we don't have convenience of VIDIOCMCAPTURE
1565 * to specify the requested palette (video format) on per-frame
1566 * basis. This means that we have to return data in -some- format
1567 * and just hope that the client knows what to do with it.
1568 * The default format is configured in uvd->defaultPalette field
1569 * as one of VIDEO_PALETTE_xxx values. We stuff it into the new
1570 * frame and initiate the frame filling process.
1571 */
1572 if (frmx == -1) {
1573 if (uvd->defaultPalette == 0) {
1574 err("%s: No default palette; don't know what to do!", __func__);
1575 count = -EFAULT;
1576 goto read_done;
1577 }
1578 frmx = 0;
1579 /*
1580 * We have no per-frame control over video size.
1581 * Therefore we only can use whatever size was
1582 * specified as default.
1583 */
1584 uvd->frame[frmx].request = uvd->videosize;
1585 uvd->frame[frmx].palette = uvd->defaultPalette;
1586 uvd->frame[frmx].frameState = FrameState_Ready;
1587 usbvideo_NewFrame(uvd, frmx);
1588 /* Now frame 0 is supposed to start filling... */
1589 }
1590
1591 /*
1592 * Get a pointer to the active frame. It is either previously
1593 * completed frame or frame in progress but not completed yet.
1594 */
1595 frame = &uvd->frame[frmx];
1596
1597 /*
1598 * Sit back & wait until the frame gets filled and postprocessed.
1599 * If we fail to get the picture [in time] then return the error.
1600 * In this call we specify that we want the frame to be waited for,
1601 * postprocessed and switched into FrameState_Done_Hold state. This
1602 * state is used to hold the frame as "fully completed" between
1603 * subsequent partial reads of the same frame.
1604 */
1605 if (frame->frameState != FrameState_Done_Hold) {
1606 long rv = -EFAULT;
1607 if (uvd->flags & FLAGS_NO_DECODING)
1608 rv = usbvideo_GetFrame(uvd, frmx);
1609 else if (VALID_CALLBACK(uvd, getFrame))
1610 rv = GET_CALLBACK(uvd, getFrame)(uvd, frmx);
1611 else
1612 err("getFrame is not set");
1613 if ((rv != 0) || (frame->frameState != FrameState_Done_Hold)) {
1614 count = rv;
1615 goto read_done;
1616 }
1617 }
1618
1619 /*
1620 * Copy bytes to user space. We allow for partial reads, which
1621 * means that the user application can request read less than
1622 * the full frame size. It is up to the application to issue
1623 * subsequent calls until entire frame is read.
1624 *
1625 * First things first, make sure we don't copy more than we
1626 * have - even if the application wants more. That would be
1627 * a big security embarassment!
1628 */
1629 if ((count + frame->seqRead_Index) > frame->seqRead_Length)
1630 count = frame->seqRead_Length - frame->seqRead_Index;
1631
1632 /*
1633 * Copy requested amount of data to user space. We start
1634 * copying from the position where we last left it, which
1635 * will be zero for a new frame (not read before).
1636 */
1637 if (copy_to_user(buf, frame->data + frame->seqRead_Index, count)) {
1638 count = -EFAULT;
1639 goto read_done;
1640 }
1641
1642 /* Update last read position */
1643 frame->seqRead_Index += count;
1644 if (uvd->debug >= 1) {
1645 err("%s: {copy} count used=%Zd, new seqRead_Index=%ld",
1646 __func__, count, frame->seqRead_Index);
1647 }
1648
1649 /* Finally check if the frame is done with and "release" it */
1650 if (frame->seqRead_Index >= frame->seqRead_Length) {
1651 /* All data has been read */
1652 frame->seqRead_Index = 0;
1653
1654 /* Mark it as available to be used again. */
1655 uvd->frame[frmx].frameState = FrameState_Unused;
1656 if (usbvideo_NewFrame(uvd, (frmx + 1) % USBVIDEO_NUMFRAMES)) {
1657 err("%s: usbvideo_NewFrame failed.", __func__);
1658 }
1659 }
1660read_done:
1661 mutex_unlock(&uvd->lock);
1662 return count;
1663}
1664
1665/*
1666 * Make all of the blocks of data contiguous
1667 */
1668static int usbvideo_CompressIsochronous(struct uvd *uvd, struct urb *urb)
1669{
1670 char *cdata;
1671 int i, totlen = 0;
1672
1673 for (i = 0; i < urb->number_of_packets; i++) {
1674 int n = urb->iso_frame_desc[i].actual_length;
1675 int st = urb->iso_frame_desc[i].status;
1676
1677 cdata = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
1678
1679 /* Detect and ignore errored packets */
1680 if (st < 0) {
1681 if (uvd->debug >= 1)
1682 err("Data error: packet=%d. len=%d. status=%d.", i, n, st);
1683 uvd->stats.iso_err_count++;
1684 continue;
1685 }
1686
1687 /* Detect and ignore empty packets */
1688 if (n <= 0) {
1689 uvd->stats.iso_skip_count++;
1690 continue;
1691 }
1692 totlen += n; /* Little local accounting */
1693 RingQueue_Enqueue(&uvd->dp, cdata, n);
1694 }
1695 return totlen;
1696}
1697
1698static void usbvideo_IsocIrq(struct urb *urb)
1699{
1700 int i, ret, len;
1701 struct uvd *uvd = urb->context;
1702
1703 /* We don't want to do anything if we are about to be removed! */
1704 if (!CAMERA_IS_OPERATIONAL(uvd))
1705 return;
1706#if 0
1707 if (urb->actual_length > 0) {
1708 dev_info(&uvd->dev->dev,
1709 "urb=$%p status=%d. errcount=%d. length=%d.\n",
1710 urb, urb->status, urb->error_count,
1711 urb->actual_length);
1712 } else {
1713 static int c = 0;
1714 if (c++ % 100 == 0)
1715 dev_info(&uvd->dev->dev, "No Isoc data\n");
1716 }
1717#endif
1718
1719 if (!uvd->streaming) {
1720 if (uvd->debug >= 1)
1721 dev_info(&uvd->dev->dev,
1722 "Not streaming, but interrupt!\n");
1723 return;
1724 }
1725
1726 uvd->stats.urb_count++;
1727 if (urb->actual_length <= 0)
1728 goto urb_done_with;
1729
1730 /* Copy the data received into ring queue */
1731 len = usbvideo_CompressIsochronous(uvd, urb);
1732 uvd->stats.urb_length = len;
1733 if (len <= 0)
1734 goto urb_done_with;
1735
1736 /* Here we got some data */
1737 uvd->stats.data_count += len;
1738 RingQueue_WakeUpInterruptible(&uvd->dp);
1739
1740urb_done_with:
1741 for (i = 0; i < FRAMES_PER_DESC; i++) {
1742 urb->iso_frame_desc[i].status = 0;
1743 urb->iso_frame_desc[i].actual_length = 0;
1744 }
1745 urb->status = 0;
1746 urb->dev = uvd->dev;
1747 ret = usb_submit_urb (urb, GFP_KERNEL);
1748 if(ret)
1749 err("usb_submit_urb error (%d)", ret);
1750 return;
1751}
1752
1753/*
1754 * usbvideo_StartDataPump()
1755 *
1756 * History:
1757 * 27-Jan-2000 Used ibmcam->iface, ibmcam->ifaceAltActive instead
1758 * of hardcoded values. Simplified by using for loop,
1759 * allowed any number of URBs.
1760 */
1761static int usbvideo_StartDataPump(struct uvd *uvd)
1762{
1763 struct usb_device *dev = uvd->dev;
1764 int i, errFlag;
1765
1766 if (uvd->debug > 1)
1767 dev_info(&uvd->dev->dev, "%s($%p)\n", __func__, uvd);
1768
1769 if (!CAMERA_IS_OPERATIONAL(uvd)) {
1770 err("%s: Camera is not operational", __func__);
1771 return -EFAULT;
1772 }
1773 uvd->curframe = -1;
1774
1775 /* Alternate interface 1 is is the biggest frame size */
1776 i = usb_set_interface(dev, uvd->iface, uvd->ifaceAltActive);
1777 if (i < 0) {
1778 err("%s: usb_set_interface error", __func__);
1779 uvd->last_error = i;
1780 return -EBUSY;
1781 }
1782 if (VALID_CALLBACK(uvd, videoStart))
1783 GET_CALLBACK(uvd, videoStart)(uvd);
1784 else
1785 err("%s: videoStart not set", __func__);
1786
1787 /* We double buffer the Iso lists */
1788 for (i=0; i < USBVIDEO_NUMSBUF; i++) {
1789 int j, k;
1790 struct urb *urb = uvd->sbuf[i].urb;
1791 urb->dev = dev;
1792 urb->context = uvd;
1793 urb->pipe = usb_rcvisocpipe(dev, uvd->video_endp);
1794 urb->interval = 1;
1795 urb->transfer_flags = URB_ISO_ASAP;
1796 urb->transfer_buffer = uvd->sbuf[i].data;
1797 urb->complete = usbvideo_IsocIrq;
1798 urb->number_of_packets = FRAMES_PER_DESC;
1799 urb->transfer_buffer_length = uvd->iso_packet_len * FRAMES_PER_DESC;
1800 for (j=k=0; j < FRAMES_PER_DESC; j++, k += uvd->iso_packet_len) {
1801 urb->iso_frame_desc[j].offset = k;
1802 urb->iso_frame_desc[j].length = uvd->iso_packet_len;
1803 }
1804 }
1805
1806 /* Submit all URBs */
1807 for (i=0; i < USBVIDEO_NUMSBUF; i++) {
1808 errFlag = usb_submit_urb(uvd->sbuf[i].urb, GFP_KERNEL);
1809 if (errFlag)
1810 err("%s: usb_submit_isoc(%d) ret %d", __func__, i, errFlag);
1811 }
1812
1813 uvd->streaming = 1;
1814 if (uvd->debug > 1)
1815 dev_info(&uvd->dev->dev,
1816 "%s: streaming=1 video_endp=$%02x\n", __func__,
1817 uvd->video_endp);
1818 return 0;
1819}
1820
1821/*
1822 * usbvideo_StopDataPump()
1823 *
1824 * This procedure stops streaming and deallocates URBs. Then it
1825 * activates zero-bandwidth alt. setting of the video interface.
1826 *
1827 * History:
1828 * 22-Jan-2000 Corrected order of actions to work after surprise removal.
1829 * 27-Jan-2000 Used uvd->iface, uvd->ifaceAltInactive instead of hardcoded values.
1830 */
1831static void usbvideo_StopDataPump(struct uvd *uvd)
1832{
1833 int i, j;
1834
1835 if ((uvd == NULL) || (!uvd->streaming) || (uvd->dev == NULL))
1836 return;
1837
1838 if (uvd->debug > 1)
1839 dev_info(&uvd->dev->dev, "%s($%p)\n", __func__, uvd);
1840
1841 /* Unschedule all of the iso td's */
1842 for (i=0; i < USBVIDEO_NUMSBUF; i++) {
1843 usb_kill_urb(uvd->sbuf[i].urb);
1844 }
1845 if (uvd->debug > 1)
1846 dev_info(&uvd->dev->dev, "%s: streaming=0\n", __func__);
1847 uvd->streaming = 0;
1848
1849 if (!uvd->remove_pending) {
1850 /* Invoke minidriver's magic to stop the camera */
1851 if (VALID_CALLBACK(uvd, videoStop))
1852 GET_CALLBACK(uvd, videoStop)(uvd);
1853 else
1854 err("%s: videoStop not set", __func__);
1855
1856 /* Set packet size to 0 */
1857 j = usb_set_interface(uvd->dev, uvd->iface, uvd->ifaceAltInactive);
1858 if (j < 0) {
1859 err("%s: usb_set_interface() error %d.", __func__, j);
1860 uvd->last_error = j;
1861 }
1862 }
1863}
1864
1865/*
1866 * usbvideo_NewFrame()
1867 *
1868 * History:
1869 * 29-Mar-00 Added copying of previous frame into the current one.
1870 * 6-Aug-00 Added model 3 video sizes, removed redundant width, height.
1871 */
1872static int usbvideo_NewFrame(struct uvd *uvd, int framenum)
1873{
1874 struct usbvideo_frame *frame;
1875 int n;
1876
1877 if (uvd->debug > 1)
1878 dev_info(&uvd->dev->dev, "usbvideo_NewFrame($%p,%d.)\n", uvd,
1879 framenum);
1880
1881 /* If we're not grabbing a frame right now and the other frame is */
1882 /* ready to be grabbed into, then use it instead */
1883 if (uvd->curframe != -1)
1884 return 0;
1885
1886 /* If necessary we adjust picture settings between frames */
1887 if (!uvd->settingsAdjusted) {
1888 if (VALID_CALLBACK(uvd, adjustPicture))
1889 GET_CALLBACK(uvd, adjustPicture)(uvd);
1890 uvd->settingsAdjusted = 1;
1891 }
1892
1893 n = (framenum + 1) % USBVIDEO_NUMFRAMES;
1894 if (uvd->frame[n].frameState == FrameState_Ready)
1895 framenum = n;
1896
1897 frame = &uvd->frame[framenum];
1898
1899 frame->frameState = FrameState_Grabbing;
1900 frame->scanstate = ScanState_Scanning;
1901 frame->seqRead_Length = 0; /* Accumulated in xxx_parse_data() */
1902 frame->deinterlace = Deinterlace_None;
1903 frame->flags = 0; /* No flags yet, up to minidriver (or us) to set them */
1904 uvd->curframe = framenum;
1905
1906 /*
1907 * Normally we would want to copy previous frame into the current one
1908 * before we even start filling it with data; this allows us to stop
1909 * filling at any moment; top portion of the frame will be new and
1910 * bottom portion will stay as it was in previous frame. If we don't
1911 * do that then missing chunks of video stream will result in flickering
1912 * portions of old data whatever it was before.
1913 *
1914 * If we choose not to copy previous frame (to, for example, save few
1915 * bus cycles - the frame can be pretty large!) then we have an option
1916 * to clear the frame before using. If we experience losses in this
1917 * mode then missing picture will be black (no flickering).
1918 *
1919 * Finally, if user chooses not to clean the current frame before
1920 * filling it with data then the old data will be visible if we fail
1921 * to refill entire frame with new data.
1922 */
1923 if (!(uvd->flags & FLAGS_SEPARATE_FRAMES)) {
1924 /* This copies previous frame into this one to mask losses */
1925 int prev = (framenum - 1 + USBVIDEO_NUMFRAMES) % USBVIDEO_NUMFRAMES;
1926 memmove(frame->data, uvd->frame[prev].data, uvd->max_frame_size);
1927 } else {
1928 if (uvd->flags & FLAGS_CLEAN_FRAMES) {
1929 /* This provides a "clean" frame but slows things down */
1930 memset(frame->data, 0, uvd->max_frame_size);
1931 }
1932 }
1933 return 0;
1934}
1935
1936/*
1937 * usbvideo_CollectRawData()
1938 *
1939 * This procedure can be used instead of 'processData' callback if you
1940 * only want to dump the raw data from the camera into the output
1941 * device (frame buffer). You can look at it with V4L client, but the
1942 * image will be unwatchable. The main purpose of this code and of the
1943 * mode FLAGS_NO_DECODING is debugging and capturing of datastreams from
1944 * new, unknown cameras. This procedure will be automatically invoked
1945 * instead of the specified callback handler when uvd->flags has bit
1946 * FLAGS_NO_DECODING set. Therefore, any regular build of any driver
1947 * based on usbvideo can use this feature at any time.
1948 */
1949static void usbvideo_CollectRawData(struct uvd *uvd, struct usbvideo_frame *frame)
1950{
1951 int n;
1952
1953 assert(uvd != NULL);
1954 assert(frame != NULL);
1955
1956 /* Try to move data from queue into frame buffer */
1957 n = RingQueue_GetLength(&uvd->dp);
1958 if (n > 0) {
1959 int m;
1960 /* See how much space we have left */
1961 m = uvd->max_frame_size - frame->seqRead_Length;
1962 if (n > m)
1963 n = m;
1964 /* Now move that much data into frame buffer */
1965 RingQueue_Dequeue(
1966 &uvd->dp,
1967 frame->data + frame->seqRead_Length,
1968 m);
1969 frame->seqRead_Length += m;
1970 }
1971 /* See if we filled the frame */
1972 if (frame->seqRead_Length >= uvd->max_frame_size) {
1973 frame->frameState = FrameState_Done;
1974 uvd->curframe = -1;
1975 uvd->stats.frame_num++;
1976 }
1977}
1978
1979static int usbvideo_GetFrame(struct uvd *uvd, int frameNum)
1980{
1981 struct usbvideo_frame *frame = &uvd->frame[frameNum];
1982
1983 if (uvd->debug >= 2)
1984 dev_info(&uvd->dev->dev, "%s($%p,%d.)\n", __func__, uvd,
1985 frameNum);
1986
1987 switch (frame->frameState) {
1988 case FrameState_Unused:
1989 if (uvd->debug >= 2)
1990 dev_info(&uvd->dev->dev, "%s: FrameState_Unused\n",
1991 __func__);
1992 return -EINVAL;
1993 case FrameState_Ready:
1994 case FrameState_Grabbing:
1995 case FrameState_Error:
1996 {
1997 int ntries, signalPending;
1998 redo:
1999 if (!CAMERA_IS_OPERATIONAL(uvd)) {
2000 if (uvd->debug >= 2)
2001 dev_info(&uvd->dev->dev,
2002 "%s: Camera is not operational (1)\n",
2003 __func__);
2004 return -EIO;
2005 }
2006 ntries = 0;
2007 do {
2008 RingQueue_InterruptibleSleepOn(&uvd->dp);
2009 signalPending = signal_pending(current);
2010 if (!CAMERA_IS_OPERATIONAL(uvd)) {
2011 if (uvd->debug >= 2)
2012 dev_info(&uvd->dev->dev,
2013 "%s: Camera is not "
2014 "operational (2)\n", __func__);
2015 return -EIO;
2016 }
2017 assert(uvd->fbuf != NULL);
2018 if (signalPending) {
2019 if (uvd->debug >= 2)
2020 dev_info(&uvd->dev->dev,
2021 "%s: Signal=$%08x\n", __func__,
2022 signalPending);
2023 if (uvd->flags & FLAGS_RETRY_VIDIOCSYNC) {
2024 usbvideo_TestPattern(uvd, 1, 0);
2025 uvd->curframe = -1;
2026 uvd->stats.frame_num++;
2027 if (uvd->debug >= 2)
2028 dev_info(&uvd->dev->dev,
2029 "%s: Forced test "
2030 "pattern screen\n",
2031 __func__);
2032 return 0;
2033 } else {
2034 /* Standard answer: Interrupted! */
2035 if (uvd->debug >= 2)
2036 dev_info(&uvd->dev->dev,
2037 "%s: Interrupted!\n",
2038 __func__);
2039 return -EINTR;
2040 }
2041 } else {
2042 /* No signals - we just got new data in dp queue */
2043 if (uvd->flags & FLAGS_NO_DECODING)
2044 usbvideo_CollectRawData(uvd, frame);
2045 else if (VALID_CALLBACK(uvd, processData))
2046 GET_CALLBACK(uvd, processData)(uvd, frame);
2047 else
2048 err("%s: processData not set", __func__);
2049 }
2050 } while (frame->frameState == FrameState_Grabbing);
2051 if (uvd->debug >= 2) {
2052 dev_info(&uvd->dev->dev,
2053 "%s: Grabbing done; state=%d. (%lu. bytes)\n",
2054 __func__, frame->frameState,
2055 frame->seqRead_Length);
2056 }
2057 if (frame->frameState == FrameState_Error) {
2058 int ret = usbvideo_NewFrame(uvd, frameNum);
2059 if (ret < 0) {
2060 err("%s: usbvideo_NewFrame() failed (%d.)", __func__, ret);
2061 return ret;
2062 }
2063 goto redo;
2064 }
2065 /* Note that we fall through to meet our destiny below */
2066 }
2067 case FrameState_Done:
2068 /*
2069 * Do all necessary postprocessing of data prepared in
2070 * "interrupt" code and the collecting code above. The
2071 * frame gets marked as FrameState_Done by queue parsing code.
2072 * This status means that we collected enough data and
2073 * most likely processed it as we went through. However
2074 * the data may need postprocessing, such as deinterlacing
2075 * or picture adjustments implemented in software (horror!)
2076 *
2077 * As soon as the frame becomes "final" it gets promoted to
2078 * FrameState_Done_Hold status where it will remain until the
2079 * caller consumed all the video data from the frame. Then
2080 * the empty shell of ex-frame is thrown out for dogs to eat.
2081 * But we, worried about pets, will recycle the frame!
2082 */
2083 uvd->stats.frame_num++;
2084 if ((uvd->flags & FLAGS_NO_DECODING) == 0) {
2085 if (VALID_CALLBACK(uvd, postProcess))
2086 GET_CALLBACK(uvd, postProcess)(uvd, frame);
2087 if (frame->flags & USBVIDEO_FRAME_FLAG_SOFTWARE_CONTRAST)
2088 usbvideo_SoftwareContrastAdjustment(uvd, frame);
2089 }
2090 frame->frameState = FrameState_Done_Hold;
2091 if (uvd->debug >= 2)
2092 dev_info(&uvd->dev->dev,
2093 "%s: Entered FrameState_Done_Hold state.\n",
2094 __func__);
2095 return 0;
2096
2097 case FrameState_Done_Hold:
2098 /*
2099 * We stay in this state indefinitely until someone external,
2100 * like ioctl() or read() call finishes digesting the frame
2101 * data. Then it will mark the frame as FrameState_Unused and
2102 * it will be released back into the wild to roam freely.
2103 */
2104 if (uvd->debug >= 2)
2105 dev_info(&uvd->dev->dev,
2106 "%s: FrameState_Done_Hold state.\n",
2107 __func__);
2108 return 0;
2109 }
2110
2111 /* Catch-all for other cases. We shall not be here. */
2112 err("%s: Invalid state %d.", __func__, frame->frameState);
2113 frame->frameState = FrameState_Unused;
2114 return 0;
2115}
2116
2117/*
2118 * usbvideo_DeinterlaceFrame()
2119 *
2120 * This procedure deinterlaces the given frame. Some cameras produce
2121 * only half of scanlines - sometimes only even lines, sometimes only
2122 * odd lines. The deinterlacing method is stored in frame->deinterlace
2123 * variable.
2124 *
2125 * Here we scan the frame vertically and replace missing scanlines with
2126 * average between surrounding ones - before and after. If we have no
2127 * line above then we just copy next line. Similarly, if we need to
2128 * create a last line then preceding line is used.
2129 */
2130void usbvideo_DeinterlaceFrame(struct uvd *uvd, struct usbvideo_frame *frame)
2131{
2132 if ((uvd == NULL) || (frame == NULL))
2133 return;
2134
2135 if ((frame->deinterlace == Deinterlace_FillEvenLines) ||
2136 (frame->deinterlace == Deinterlace_FillOddLines))
2137 {
2138 const int v4l_linesize = VIDEOSIZE_X(frame->request) * V4L_BYTES_PER_PIXEL;
2139 int i = (frame->deinterlace == Deinterlace_FillEvenLines) ? 0 : 1;
2140
2141 for (; i < VIDEOSIZE_Y(frame->request); i += 2) {
2142 const unsigned char *fs1, *fs2;
2143 unsigned char *fd;
2144 int ip, in, j; /* Previous and next lines */
2145
2146 /*
2147 * Need to average lines before and after 'i'.
2148 * If we go out of bounds seeking those lines then
2149 * we point back to existing line.
2150 */
2151 ip = i - 1; /* First, get rough numbers */
2152 in = i + 1;
2153
2154 /* Now validate */
2155 if (ip < 0)
2156 ip = in;
2157 if (in >= VIDEOSIZE_Y(frame->request))
2158 in = ip;
2159
2160 /* Sanity check */
2161 if ((ip < 0) || (in < 0) ||
2162 (ip >= VIDEOSIZE_Y(frame->request)) ||
2163 (in >= VIDEOSIZE_Y(frame->request)))
2164 {
2165 err("Error: ip=%d. in=%d. req.height=%ld.",
2166 ip, in, VIDEOSIZE_Y(frame->request));
2167 break;
2168 }
2169
2170 /* Now we need to average lines 'ip' and 'in' to produce line 'i' */
2171 fs1 = frame->data + (v4l_linesize * ip);
2172 fs2 = frame->data + (v4l_linesize * in);
2173 fd = frame->data + (v4l_linesize * i);
2174
2175 /* Average lines around destination */
2176 for (j=0; j < v4l_linesize; j++) {
2177 fd[j] = (unsigned char)((((unsigned) fs1[j]) +
2178 ((unsigned)fs2[j])) >> 1);
2179 }
2180 }
2181 }
2182
2183 /* Optionally display statistics on the screen */
2184 if (uvd->flags & FLAGS_OVERLAY_STATS)
2185 usbvideo_OverlayStats(uvd, frame);
2186}
2187
2188EXPORT_SYMBOL(usbvideo_DeinterlaceFrame);
2189
2190/*
2191 * usbvideo_SoftwareContrastAdjustment()
2192 *
2193 * This code adjusts the contrast of the frame, assuming RGB24 format.
2194 * As most software image processing, this job is CPU-intensive.
2195 * Get a camera that supports hardware adjustment!
2196 *
2197 * History:
2198 * 09-Feb-2001 Created.
2199 */
2200static void usbvideo_SoftwareContrastAdjustment(struct uvd *uvd,
2201 struct usbvideo_frame *frame)
2202{
2203 int i, j, v4l_linesize;
2204 signed long adj;
2205 const int ccm = 128; /* Color correction median - see below */
2206
2207 if ((uvd == NULL) || (frame == NULL)) {
2208 err("%s: Illegal call.", __func__);
2209 return;
2210 }
2211 adj = (uvd->vpic.contrast - 0x8000) >> 8; /* -128..+127 = -ccm..+(ccm-1)*/
2212 RESTRICT_TO_RANGE(adj, -ccm, ccm+1);
2213 if (adj == 0) {
2214 /* In rare case of no adjustment */
2215 return;
2216 }
2217 v4l_linesize = VIDEOSIZE_X(frame->request) * V4L_BYTES_PER_PIXEL;
2218 for (i=0; i < VIDEOSIZE_Y(frame->request); i++) {
2219 unsigned char *fd = frame->data + (v4l_linesize * i);
2220 for (j=0; j < v4l_linesize; j++) {
2221 signed long v = (signed long) fd[j];
2222 /* Magnify up to 2 times, reduce down to zero */
2223 v = 128 + ((ccm + adj) * (v - 128)) / ccm;
2224 RESTRICT_TO_RANGE(v, 0, 0xFF); /* Must flatten tails */
2225 fd[j] = (unsigned char) v;
2226 }
2227 }
2228}
2229
2230MODULE_LICENSE("GPL");
diff --git a/drivers/media/video/usbvideo/usbvideo.h b/drivers/media/video/usbvideo/usbvideo.h
deleted file mode 100644
index c66985beb8c9..000000000000
--- a/drivers/media/video/usbvideo/usbvideo.h
+++ /dev/null
@@ -1,395 +0,0 @@
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2, or (at your option)
5 * any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15 */
16#ifndef usbvideo_h
17#define usbvideo_h
18
19#include <linux/videodev.h>
20#include <media/v4l2-common.h>
21#include <media/v4l2-ioctl.h>
22#include <linux/usb.h>
23#include <linux/mutex.h>
24
25/* Most helpful debugging aid */
26#define assert(expr) ((void) ((expr) ? 0 : (err("assert failed at line %d",__LINE__))))
27
28#define USBVIDEO_REPORT_STATS 1 /* Set to 0 to block statistics on close */
29
30/* Bit flags (options) */
31#define FLAGS_RETRY_VIDIOCSYNC (1 << 0)
32#define FLAGS_MONOCHROME (1 << 1)
33#define FLAGS_DISPLAY_HINTS (1 << 2)
34#define FLAGS_OVERLAY_STATS (1 << 3)
35#define FLAGS_FORCE_TESTPATTERN (1 << 4)
36#define FLAGS_SEPARATE_FRAMES (1 << 5)
37#define FLAGS_CLEAN_FRAMES (1 << 6)
38#define FLAGS_NO_DECODING (1 << 7)
39
40/* Bit flags for frames (apply to the frame where they are specified) */
41#define USBVIDEO_FRAME_FLAG_SOFTWARE_CONTRAST (1 << 0)
42
43/* Camera capabilities (maximum) */
44#define CAMERA_URB_FRAMES 32
45#define CAMERA_MAX_ISO_PACKET 1023 /* 1022 actually sent by camera */
46#define FRAMES_PER_DESC (CAMERA_URB_FRAMES)
47#define FRAME_SIZE_PER_DESC (CAMERA_MAX_ISO_PACKET)
48
49/* This macro restricts an int variable to an inclusive range */
50#define RESTRICT_TO_RANGE(v,mi,ma) { if ((v) < (mi)) (v) = (mi); else if ((v) > (ma)) (v) = (ma); }
51
52#define V4L_BYTES_PER_PIXEL 3 /* Because we produce RGB24 */
53
54/*
55 * Use this macro to construct constants for different video sizes.
56 * We have to deal with different video sizes that have to be
57 * configured in the device or compared against when we receive
58 * a data. Normally one would define a bunch of VIDEOSIZE_x_by_y
59 * #defines and that's the end of story. However this solution
60 * does not allow to convert between real pixel sizes and the
61 * constant (integer) value that may be used to tag a frame or
62 * whatever. The set of macros below constructs videosize constants
63 * from the pixel size and allows to reconstruct the pixel size
64 * from the combined value later.
65 */
66#define VIDEOSIZE(x,y) (((x) & 0xFFFFL) | (((y) & 0xFFFFL) << 16))
67#define VIDEOSIZE_X(vs) ((vs) & 0xFFFFL)
68#define VIDEOSIZE_Y(vs) (((vs) >> 16) & 0xFFFFL)
69typedef unsigned long videosize_t;
70
71/*
72 * This macro checks if the camera is still operational. The 'uvd'
73 * pointer must be valid, uvd->dev must be valid, we are not
74 * removing the device and the device has not erred on us.
75 */
76#define CAMERA_IS_OPERATIONAL(uvd) (\
77 (uvd != NULL) && \
78 ((uvd)->dev != NULL) && \
79 ((uvd)->last_error == 0) && \
80 (!(uvd)->remove_pending))
81
82/*
83 * We use macros to do YUV -> RGB conversion because this is
84 * very important for speed and totally unimportant for size.
85 *
86 * YUV -> RGB Conversion
87 * ---------------------
88 *
89 * B = 1.164*(Y-16) + 2.018*(V-128)
90 * G = 1.164*(Y-16) - 0.813*(U-128) - 0.391*(V-128)
91 * R = 1.164*(Y-16) + 1.596*(U-128)
92 *
93 * If you fancy integer arithmetics (as you should), hear this:
94 *
95 * 65536*B = 76284*(Y-16) + 132252*(V-128)
96 * 65536*G = 76284*(Y-16) - 53281*(U-128) - 25625*(V-128)
97 * 65536*R = 76284*(Y-16) + 104595*(U-128)
98 *
99 * Make sure the output values are within [0..255] range.
100 */
101#define LIMIT_RGB(x) (((x) < 0) ? 0 : (((x) > 255) ? 255 : (x)))
102#define YUV_TO_RGB_BY_THE_BOOK(my,mu,mv,mr,mg,mb) { \
103 int mm_y, mm_yc, mm_u, mm_v, mm_r, mm_g, mm_b; \
104 mm_y = (my) - 16; \
105 mm_u = (mu) - 128; \
106 mm_v = (mv) - 128; \
107 mm_yc= mm_y * 76284; \
108 mm_b = (mm_yc + 132252*mm_v ) >> 16; \
109 mm_g = (mm_yc - 53281*mm_u - 25625*mm_v ) >> 16; \
110 mm_r = (mm_yc + 104595*mm_u ) >> 16; \
111 mb = LIMIT_RGB(mm_b); \
112 mg = LIMIT_RGB(mm_g); \
113 mr = LIMIT_RGB(mm_r); \
114}
115
116#define RING_QUEUE_SIZE (128*1024) /* Must be a power of 2 */
117#define RING_QUEUE_ADVANCE_INDEX(rq,ind,n) (rq)->ind = ((rq)->ind + (n)) & ((rq)->length-1)
118#define RING_QUEUE_DEQUEUE_BYTES(rq,n) RING_QUEUE_ADVANCE_INDEX(rq,ri,n)
119#define RING_QUEUE_PEEK(rq,ofs) ((rq)->queue[((ofs) + (rq)->ri) & ((rq)->length-1)])
120
121struct RingQueue {
122 unsigned char *queue; /* Data from the Isoc data pump */
123 int length; /* How many bytes allocated for the queue */
124 int wi; /* That's where we write */
125 int ri; /* Read from here until you hit write index */
126 wait_queue_head_t wqh; /* Processes waiting */
127};
128
129enum ScanState {
130 ScanState_Scanning, /* Scanning for header */
131 ScanState_Lines /* Parsing lines */
132};
133
134/* Completion states of the data parser */
135enum ParseState {
136 scan_Continue, /* Just parse next item */
137 scan_NextFrame, /* Frame done, send it to V4L */
138 scan_Out, /* Not enough data for frame */
139 scan_EndParse /* End parsing */
140};
141
142enum FrameState {
143 FrameState_Unused, /* Unused (no MCAPTURE) */
144 FrameState_Ready, /* Ready to start grabbing */
145 FrameState_Grabbing, /* In the process of being grabbed into */
146 FrameState_Done, /* Finished grabbing, but not been synced yet */
147 FrameState_Done_Hold, /* Are syncing or reading */
148 FrameState_Error, /* Something bad happened while processing */
149};
150
151/*
152 * Some frames may contain only even or odd lines. This type
153 * specifies what type of deinterlacing is required.
154 */
155enum Deinterlace {
156 Deinterlace_None=0,
157 Deinterlace_FillOddLines,
158 Deinterlace_FillEvenLines
159};
160
161#define USBVIDEO_NUMFRAMES 2 /* How many frames we work with */
162#define USBVIDEO_NUMSBUF 2 /* How many URBs linked in a ring */
163
164/* This structure represents one Isoc request - URB and buffer */
165struct usbvideo_sbuf {
166 char *data;
167 struct urb *urb;
168};
169
170struct usbvideo_frame {
171 char *data; /* Frame buffer */
172 unsigned long header; /* Significant bits from the header */
173
174 videosize_t canvas; /* The canvas (max. image) allocated */
175 videosize_t request; /* That's what the application asked for */
176 unsigned short palette; /* The desired format */
177
178 enum FrameState frameState;/* State of grabbing */
179 enum ScanState scanstate; /* State of scanning */
180 enum Deinterlace deinterlace;
181 int flags; /* USBVIDEO_FRAME_FLAG_xxx bit flags */
182
183 int curline; /* Line of frame we're working on */
184
185 long seqRead_Length; /* Raw data length of frame */
186 long seqRead_Index; /* Amount of data that has been already read */
187
188 void *user; /* Additional data that user may need */
189};
190
191/* Statistics that can be overlaid on screen */
192struct usbvideo_statistics {
193 unsigned long frame_num; /* Sequential number of the frame */
194 unsigned long urb_count; /* How many URBs we received so far */
195 unsigned long urb_length; /* Length of last URB */
196 unsigned long data_count; /* How many bytes we received */
197 unsigned long header_count; /* How many frame headers we found */
198 unsigned long iso_skip_count; /* How many empty ISO packets received */
199 unsigned long iso_err_count; /* How many bad ISO packets received */
200};
201
202struct usbvideo;
203
204struct uvd {
205 struct video_device vdev; /* Must be the first field! */
206 struct usb_device *dev;
207 struct usbvideo *handle; /* Points back to the struct usbvideo */
208 void *user_data; /* Camera-dependent data */
209 int user_size; /* Size of that camera-dependent data */
210 int debug; /* Debug level for usbvideo */
211 unsigned char iface; /* Video interface number */
212 unsigned char video_endp;
213 unsigned char ifaceAltActive;
214 unsigned char ifaceAltInactive; /* Alt settings */
215 unsigned long flags; /* FLAGS_USBVIDEO_xxx */
216 unsigned long paletteBits; /* Which palettes we accept? */
217 unsigned short defaultPalette; /* What palette to use for read() */
218 struct mutex lock;
219 int user; /* user count for exclusive use */
220
221 videosize_t videosize; /* Current setting */
222 videosize_t canvas; /* This is the width,height of the V4L canvas */
223 int max_frame_size; /* Bytes in one video frame */
224
225 int uvd_used; /* Is this structure in use? */
226 int streaming; /* Are we streaming Isochronous? */
227 int grabbing; /* Are we grabbing? */
228 int settingsAdjusted; /* Have we adjusted contrast etc.? */
229 int last_error; /* What calamity struck us? */
230
231 char *fbuf; /* Videodev buffer area */
232 int fbuf_size; /* Videodev buffer size */
233
234 int curframe;
235 int iso_packet_len; /* Videomode-dependent, saves bus bandwidth */
236
237 struct RingQueue dp; /* Isoc data pump */
238 struct usbvideo_frame frame[USBVIDEO_NUMFRAMES];
239 struct usbvideo_sbuf sbuf[USBVIDEO_NUMSBUF];
240
241 volatile int remove_pending; /* If set then about to exit */
242
243 struct video_picture vpic, vpic_old; /* Picture settings */
244 struct video_capability vcap; /* Video capabilities */
245 struct video_channel vchan; /* May be used for tuner support */
246 struct usbvideo_statistics stats;
247 char videoName[32]; /* Holds name like "video7" */
248};
249
250/*
251 * usbvideo callbacks (virtual methods). They are set when usbvideo
252 * services are registered. All of these default to NULL, except those
253 * that default to usbvideo-provided methods.
254 */
255struct usbvideo_cb {
256 int (*probe)(struct usb_interface *, const struct usb_device_id *);
257 void (*userFree)(struct uvd *);
258 void (*disconnect)(struct usb_interface *);
259 int (*setupOnOpen)(struct uvd *);
260 void (*videoStart)(struct uvd *);
261 void (*videoStop)(struct uvd *);
262 void (*processData)(struct uvd *, struct usbvideo_frame *);
263 void (*postProcess)(struct uvd *, struct usbvideo_frame *);
264 void (*adjustPicture)(struct uvd *);
265 int (*getFPS)(struct uvd *);
266 int (*overlayHook)(struct uvd *, struct usbvideo_frame *);
267 int (*getFrame)(struct uvd *, int);
268 int (*startDataPump)(struct uvd *uvd);
269 void (*stopDataPump)(struct uvd *uvd);
270 int (*setVideoMode)(struct uvd *uvd, struct video_window *vw);
271};
272
273struct usbvideo {
274 int num_cameras; /* As allocated */
275 struct usb_driver usbdrv; /* Interface to the USB stack */
276 char drvName[80]; /* Driver name */
277 struct mutex lock; /* Mutex protecting camera structures */
278 struct usbvideo_cb cb; /* Table of callbacks (virtual methods) */
279 struct video_device vdt; /* Video device template */
280 struct uvd *cam; /* Array of camera structures */
281 struct module *md_module; /* Minidriver module */
282};
283
284
285/*
286 * This macro retrieves callback address from the struct uvd object.
287 * No validity checks are done here, so be sure to check the
288 * callback beforehand with VALID_CALLBACK.
289 */
290#define GET_CALLBACK(uvd,cbName) ((uvd)->handle->cb.cbName)
291
292/*
293 * This macro returns either callback pointer or NULL. This is safe
294 * macro, meaning that most of components of data structures involved
295 * may be NULL - this only results in NULL being returned. You may
296 * wish to use this macro to make sure that the callback is callable.
297 * However keep in mind that those checks take time.
298 */
299#define VALID_CALLBACK(uvd,cbName) ((((uvd) != NULL) && \
300 ((uvd)->handle != NULL)) ? GET_CALLBACK(uvd,cbName) : NULL)
301
302int RingQueue_Dequeue(struct RingQueue *rq, unsigned char *dst, int len);
303int RingQueue_Enqueue(struct RingQueue *rq, const unsigned char *cdata, int n);
304void RingQueue_WakeUpInterruptible(struct RingQueue *rq);
305void RingQueue_Flush(struct RingQueue *rq);
306
307static inline int RingQueue_GetLength(const struct RingQueue *rq)
308{
309 return (rq->wi - rq->ri + rq->length) & (rq->length-1);
310}
311
312static inline int RingQueue_GetFreeSpace(const struct RingQueue *rq)
313{
314 return rq->length - RingQueue_GetLength(rq);
315}
316
317void usbvideo_DrawLine(
318 struct usbvideo_frame *frame,
319 int x1, int y1,
320 int x2, int y2,
321 unsigned char cr, unsigned char cg, unsigned char cb);
322void usbvideo_HexDump(const unsigned char *data, int len);
323void usbvideo_SayAndWait(const char *what);
324void usbvideo_TestPattern(struct uvd *uvd, int fullframe, int pmode);
325
326/* Memory allocation routines */
327unsigned long usbvideo_kvirt_to_pa(unsigned long adr);
328
329int usbvideo_register(
330 struct usbvideo **pCams,
331 const int num_cams,
332 const int num_extra,
333 const char *driverName,
334 const struct usbvideo_cb *cbTable,
335 struct module *md,
336 const struct usb_device_id *id_table);
337struct uvd *usbvideo_AllocateDevice(struct usbvideo *cams);
338int usbvideo_RegisterVideoDevice(struct uvd *uvd);
339void usbvideo_Deregister(struct usbvideo **uvt);
340
341int usbvideo_v4l_initialize(struct video_device *dev);
342
343void usbvideo_DeinterlaceFrame(struct uvd *uvd, struct usbvideo_frame *frame);
344
345/*
346 * This code performs bounds checking - use it when working with
347 * new formats, or else you may get oopses all over the place.
348 * If pixel falls out of bounds then it gets shoved back (as close
349 * to place of offence as possible) and is painted bright red.
350 *
351 * There are two important concepts: frame width, height and
352 * V4L canvas width, height. The former is the area requested by
353 * the application -for this very frame-. The latter is the largest
354 * possible frame that we can serve (we advertise that via V4L ioctl).
355 * The frame data is expected to be formatted as lines of length
356 * VIDEOSIZE_X(fr->request), total VIDEOSIZE_Y(frame->request) lines.
357 */
358static inline void RGB24_PUTPIXEL(
359 struct usbvideo_frame *fr,
360 int ix, int iy,
361 unsigned char vr,
362 unsigned char vg,
363 unsigned char vb)
364{
365 register unsigned char *pf;
366 int limiter = 0, mx, my;
367 mx = ix;
368 my = iy;
369 if (mx < 0) {
370 mx=0;
371 limiter++;
372 } else if (mx >= VIDEOSIZE_X((fr)->request)) {
373 mx= VIDEOSIZE_X((fr)->request) - 1;
374 limiter++;
375 }
376 if (my < 0) {
377 my = 0;
378 limiter++;
379 } else if (my >= VIDEOSIZE_Y((fr)->request)) {
380 my = VIDEOSIZE_Y((fr)->request) - 1;
381 limiter++;
382 }
383 pf = (fr)->data + V4L_BYTES_PER_PIXEL*((iy)*VIDEOSIZE_X((fr)->request) + (ix));
384 if (limiter) {
385 *pf++ = 0;
386 *pf++ = 0;
387 *pf++ = 0xFF;
388 } else {
389 *pf++ = (vb);
390 *pf++ = (vg);
391 *pf++ = (vr);
392 }
393}
394
395#endif /* usbvideo_h */
diff --git a/drivers/media/video/usbvideo/vicam.c b/drivers/media/video/usbvideo/vicam.c
deleted file mode 100644
index dc17cce2fbb6..000000000000
--- a/drivers/media/video/usbvideo/vicam.c
+++ /dev/null
@@ -1,952 +0,0 @@
1/*
2 * USB ViCam WebCam driver
3 * Copyright (c) 2002 Joe Burks (jburks@wavicle.org),
4 * Christopher L Cheney (ccheney@cheney.cx),
5 * Pavel Machek (pavel@ucw.cz),
6 * John Tyner (jtyner@cs.ucr.edu),
7 * Monroe Williams (monroe@pobox.com)
8 *
9 * Supports 3COM HomeConnect PC Digital WebCam
10 * Supports Compro PS39U WebCam
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *
26 * This source code is based heavily on the CPiA webcam driver which was
27 * written by Peter Pregler, Scott J. Bertin and Johannes Erdfelt
28 *
29 * Portions of this code were also copied from usbvideo.c
30 *
31 * Special thanks to the whole team at Sourceforge for help making
32 * this driver become a reality. Notably:
33 * Andy Armstrong who reverse engineered the color encoding and
34 * Pavel Machek and Chris Cheney who worked on reverse engineering the
35 * camera controls and wrote the first generation driver.
36 */
37
38#include <linux/kernel.h>
39#include <linux/module.h>
40#include <linux/init.h>
41#include <linux/videodev.h>
42#include <linux/usb.h>
43#include <linux/vmalloc.h>
44#include <linux/mm.h>
45#include <linux/slab.h>
46#include <linux/mutex.h>
47#include <linux/firmware.h>
48#include <linux/ihex.h>
49#include "usbvideo.h"
50
51// #define VICAM_DEBUG
52
53#ifdef VICAM_DEBUG
54#define ADBG(lineno,fmt,args...) printk(fmt, jiffies, __func__, lineno, ##args)
55#define DBG(fmt,args...) ADBG((__LINE__),KERN_DEBUG __FILE__"(%ld):%s (%d):"fmt,##args)
56#else
57#define DBG(fmn,args...) do {} while(0)
58#endif
59
60#define DRIVER_AUTHOR "Joe Burks, jburks@wavicle.org"
61#define DRIVER_DESC "ViCam WebCam Driver"
62
63/* Define these values to match your device */
64#define USB_VICAM_VENDOR_ID 0x04c1
65#define USB_VICAM_PRODUCT_ID 0x009d
66#define USB_COMPRO_VENDOR_ID 0x0602
67#define USB_COMPRO_PRODUCT_ID 0x1001
68
69#define VICAM_BYTES_PER_PIXEL 3
70#define VICAM_MAX_READ_SIZE (512*242+128)
71#define VICAM_MAX_FRAME_SIZE (VICAM_BYTES_PER_PIXEL*320*240)
72#define VICAM_FRAMES 2
73
74#define VICAM_HEADER_SIZE 64
75
76/* rvmalloc / rvfree copied from usbvideo.c
77 *
78 * Not sure why these are not yet non-statics which I can reference through
79 * usbvideo.h the same as it is in 2.4.20. I bet this will get fixed sometime
80 * in the future.
81 *
82*/
83static void *rvmalloc(unsigned long size)
84{
85 void *mem;
86 unsigned long adr;
87
88 size = PAGE_ALIGN(size);
89 mem = vmalloc_32(size);
90 if (!mem)
91 return NULL;
92
93 memset(mem, 0, size); /* Clear the ram out, no junk to the user */
94 adr = (unsigned long) mem;
95 while (size > 0) {
96 SetPageReserved(vmalloc_to_page((void *)adr));
97 adr += PAGE_SIZE;
98 size -= PAGE_SIZE;
99 }
100
101 return mem;
102}
103
104static void rvfree(void *mem, unsigned long size)
105{
106 unsigned long adr;
107
108 if (!mem)
109 return;
110
111 adr = (unsigned long) mem;
112 while ((long) size > 0) {
113 ClearPageReserved(vmalloc_to_page((void *)adr));
114 adr += PAGE_SIZE;
115 size -= PAGE_SIZE;
116 }
117 vfree(mem);
118}
119
120struct vicam_camera {
121 u16 shutter_speed; // capture shutter speed
122 u16 gain; // capture gain
123
124 u8 *raw_image; // raw data captured from the camera
125 u8 *framebuf; // processed data in RGB24 format
126 u8 *cntrlbuf; // area used to send control msgs
127
128 struct video_device vdev; // v4l video device
129 struct usb_device *udev; // usb device
130
131 /* guard against simultaneous accesses to the camera */
132 struct mutex cam_lock;
133
134 int is_initialized;
135 u8 open_count;
136 u8 bulkEndpoint;
137 int needsDummyRead;
138};
139
140static int vicam_probe( struct usb_interface *intf, const struct usb_device_id *id);
141static void vicam_disconnect(struct usb_interface *intf);
142static void read_frame(struct vicam_camera *cam, int framenum);
143static void vicam_decode_color(const u8 *, u8 *);
144
145static int __send_control_msg(struct vicam_camera *cam,
146 u8 request,
147 u16 value,
148 u16 index,
149 unsigned char *cp,
150 u16 size)
151{
152 int status;
153
154 /* cp must be memory that has been allocated by kmalloc */
155
156 status = usb_control_msg(cam->udev,
157 usb_sndctrlpipe(cam->udev, 0),
158 request,
159 USB_DIR_OUT | USB_TYPE_VENDOR |
160 USB_RECIP_DEVICE, value, index,
161 cp, size, 1000);
162
163 status = min(status, 0);
164
165 if (status < 0) {
166 printk(KERN_INFO "Failed sending control message, error %d.\n",
167 status);
168 }
169
170 return status;
171}
172
173static int send_control_msg(struct vicam_camera *cam,
174 u8 request,
175 u16 value,
176 u16 index,
177 unsigned char *cp,
178 u16 size)
179{
180 int status = -ENODEV;
181 mutex_lock(&cam->cam_lock);
182 if (cam->udev) {
183 status = __send_control_msg(cam, request, value,
184 index, cp, size);
185 }
186 mutex_unlock(&cam->cam_lock);
187 return status;
188}
189static int
190initialize_camera(struct vicam_camera *cam)
191{
192 int err;
193 const struct ihex_binrec *rec;
194 const struct firmware *uninitialized_var(fw);
195
196 err = request_ihex_firmware(&fw, "vicam/firmware.fw", &cam->udev->dev);
197 if (err) {
198 printk(KERN_ERR "Failed to load \"vicam/firmware.fw\": %d\n",
199 err);
200 return err;
201 }
202
203 for (rec = (void *)fw->data; rec; rec = ihex_next_binrec(rec)) {
204 memcpy(cam->cntrlbuf, rec->data, be16_to_cpu(rec->len));
205
206 err = send_control_msg(cam, 0xff, 0, 0,
207 cam->cntrlbuf, be16_to_cpu(rec->len));
208 if (err)
209 break;
210 }
211
212 release_firmware(fw);
213
214 return err;
215}
216
217static int
218set_camera_power(struct vicam_camera *cam, int state)
219{
220 int status;
221
222 if ((status = send_control_msg(cam, 0x50, state, 0, NULL, 0)) < 0)
223 return status;
224
225 if (state) {
226 send_control_msg(cam, 0x55, 1, 0, NULL, 0);
227 }
228
229 return 0;
230}
231
232static long
233vicam_ioctl(struct file *file, unsigned int ioctlnr, unsigned long arg)
234{
235 void __user *user_arg = (void __user *)arg;
236 struct vicam_camera *cam = file->private_data;
237 long retval = 0;
238
239 if (!cam)
240 return -ENODEV;
241
242 switch (ioctlnr) {
243 /* query capabilities */
244 case VIDIOCGCAP:
245 {
246 struct video_capability b;
247
248 DBG("VIDIOCGCAP\n");
249 memset(&b, 0, sizeof(b));
250 strcpy(b.name, "ViCam-based Camera");
251 b.type = VID_TYPE_CAPTURE;
252 b.channels = 1;
253 b.audios = 0;
254 b.maxwidth = 320; /* VIDEOSIZE_CIF */
255 b.maxheight = 240;
256 b.minwidth = 320; /* VIDEOSIZE_48_48 */
257 b.minheight = 240;
258
259 if (copy_to_user(user_arg, &b, sizeof(b)))
260 retval = -EFAULT;
261
262 break;
263 }
264 /* get/set video source - we are a camera and nothing else */
265 case VIDIOCGCHAN:
266 {
267 struct video_channel v;
268
269 DBG("VIDIOCGCHAN\n");
270 if (copy_from_user(&v, user_arg, sizeof(v))) {
271 retval = -EFAULT;
272 break;
273 }
274 if (v.channel != 0) {
275 retval = -EINVAL;
276 break;
277 }
278
279 v.channel = 0;
280 strcpy(v.name, "Camera");
281 v.tuners = 0;
282 v.flags = 0;
283 v.type = VIDEO_TYPE_CAMERA;
284 v.norm = 0;
285
286 if (copy_to_user(user_arg, &v, sizeof(v)))
287 retval = -EFAULT;
288 break;
289 }
290
291 case VIDIOCSCHAN:
292 {
293 int v;
294
295 if (copy_from_user(&v, user_arg, sizeof(v)))
296 retval = -EFAULT;
297 DBG("VIDIOCSCHAN %d\n", v);
298
299 if (retval == 0 && v != 0)
300 retval = -EINVAL;
301
302 break;
303 }
304
305 /* image properties */
306 case VIDIOCGPICT:
307 {
308 struct video_picture vp;
309 DBG("VIDIOCGPICT\n");
310 memset(&vp, 0, sizeof (struct video_picture));
311 vp.brightness = cam->gain << 8;
312 vp.depth = 24;
313 vp.palette = VIDEO_PALETTE_RGB24;
314 if (copy_to_user(user_arg, &vp, sizeof (struct video_picture)))
315 retval = -EFAULT;
316 break;
317 }
318
319 case VIDIOCSPICT:
320 {
321 struct video_picture vp;
322
323 if (copy_from_user(&vp, user_arg, sizeof(vp))) {
324 retval = -EFAULT;
325 break;
326 }
327
328 DBG("VIDIOCSPICT depth = %d, pal = %d\n", vp.depth,
329 vp.palette);
330
331 cam->gain = vp.brightness >> 8;
332
333 if (vp.depth != 24
334 || vp.palette != VIDEO_PALETTE_RGB24)
335 retval = -EINVAL;
336
337 break;
338 }
339
340 /* get/set capture window */
341 case VIDIOCGWIN:
342 {
343 struct video_window vw;
344 vw.x = 0;
345 vw.y = 0;
346 vw.width = 320;
347 vw.height = 240;
348 vw.chromakey = 0;
349 vw.flags = 0;
350 vw.clips = NULL;
351 vw.clipcount = 0;
352
353 DBG("VIDIOCGWIN\n");
354
355 if (copy_to_user(user_arg, (void *)&vw, sizeof(vw)))
356 retval = -EFAULT;
357
358 // I'm not sure what the deal with a capture window is, it is very poorly described
359 // in the doc. So I won't support it now.
360 break;
361 }
362
363 case VIDIOCSWIN:
364 {
365
366 struct video_window vw;
367
368 if (copy_from_user(&vw, user_arg, sizeof(vw))) {
369 retval = -EFAULT;
370 break;
371 }
372
373 DBG("VIDIOCSWIN %d x %d\n", vw.width, vw.height);
374
375 if ( vw.width != 320 || vw.height != 240 )
376 retval = -EFAULT;
377
378 break;
379 }
380
381 /* mmap interface */
382 case VIDIOCGMBUF:
383 {
384 struct video_mbuf vm;
385 int i;
386
387 DBG("VIDIOCGMBUF\n");
388 memset(&vm, 0, sizeof (vm));
389 vm.size =
390 VICAM_MAX_FRAME_SIZE * VICAM_FRAMES;
391 vm.frames = VICAM_FRAMES;
392 for (i = 0; i < VICAM_FRAMES; i++)
393 vm.offsets[i] = VICAM_MAX_FRAME_SIZE * i;
394
395 if (copy_to_user(user_arg, (void *)&vm, sizeof(vm)))
396 retval = -EFAULT;
397
398 break;
399 }
400
401 case VIDIOCMCAPTURE:
402 {
403 struct video_mmap vm;
404 // int video_size;
405
406 if (copy_from_user((void *)&vm, user_arg, sizeof(vm))) {
407 retval = -EFAULT;
408 break;
409 }
410
411 DBG("VIDIOCMCAPTURE frame=%d, height=%d, width=%d, format=%d.\n",vm.frame,vm.width,vm.height,vm.format);
412
413 if ( vm.frame >= VICAM_FRAMES || vm.format != VIDEO_PALETTE_RGB24 )
414 retval = -EINVAL;
415
416 // in theory right here we'd start the image capturing
417 // (fill in a bulk urb and submit it asynchronously)
418 //
419 // Instead we're going to do a total hack job for now and
420 // retrieve the frame in VIDIOCSYNC
421
422 break;
423 }
424
425 case VIDIOCSYNC:
426 {
427 int frame;
428
429 if (copy_from_user((void *)&frame, user_arg, sizeof(int))) {
430 retval = -EFAULT;
431 break;
432 }
433 DBG("VIDIOCSYNC: %d\n", frame);
434
435 read_frame(cam, frame);
436 vicam_decode_color(cam->raw_image,
437 cam->framebuf +
438 frame * VICAM_MAX_FRAME_SIZE );
439
440 break;
441 }
442
443 /* pointless to implement overlay with this camera */
444 case VIDIOCCAPTURE:
445 case VIDIOCGFBUF:
446 case VIDIOCSFBUF:
447 case VIDIOCKEY:
448 retval = -EINVAL;
449 break;
450
451 /* tuner interface - we have none */
452 case VIDIOCGTUNER:
453 case VIDIOCSTUNER:
454 case VIDIOCGFREQ:
455 case VIDIOCSFREQ:
456 retval = -EINVAL;
457 break;
458
459 /* audio interface - we have none */
460 case VIDIOCGAUDIO:
461 case VIDIOCSAUDIO:
462 retval = -EINVAL;
463 break;
464 default:
465 retval = -ENOIOCTLCMD;
466 break;
467 }
468
469 return retval;
470}
471
472static int
473vicam_open(struct file *file)
474{
475 struct vicam_camera *cam = video_drvdata(file);
476
477 DBG("open\n");
478
479 if (!cam) {
480 printk(KERN_ERR
481 "vicam video_device improperly initialized");
482 return -EINVAL;
483 }
484
485 /* cam_lock/open_count protects us from simultaneous opens
486 * ... for now. we probably shouldn't rely on this fact forever.
487 */
488
489 mutex_lock(&cam->cam_lock);
490 if (cam->open_count > 0) {
491 printk(KERN_INFO
492 "vicam_open called on already opened camera");
493 mutex_unlock(&cam->cam_lock);
494 return -EBUSY;
495 }
496
497 cam->raw_image = kmalloc(VICAM_MAX_READ_SIZE, GFP_KERNEL);
498 if (!cam->raw_image) {
499 mutex_unlock(&cam->cam_lock);
500 return -ENOMEM;
501 }
502
503 cam->framebuf = rvmalloc(VICAM_MAX_FRAME_SIZE * VICAM_FRAMES);
504 if (!cam->framebuf) {
505 kfree(cam->raw_image);
506 mutex_unlock(&cam->cam_lock);
507 return -ENOMEM;
508 }
509
510 cam->cntrlbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
511 if (!cam->cntrlbuf) {
512 kfree(cam->raw_image);
513 rvfree(cam->framebuf, VICAM_MAX_FRAME_SIZE * VICAM_FRAMES);
514 mutex_unlock(&cam->cam_lock);
515 return -ENOMEM;
516 }
517
518 cam->needsDummyRead = 1;
519 cam->open_count++;
520
521 file->private_data = cam;
522 mutex_unlock(&cam->cam_lock);
523
524
525 // First upload firmware, then turn the camera on
526
527 if (!cam->is_initialized) {
528 initialize_camera(cam);
529
530 cam->is_initialized = 1;
531 }
532
533 set_camera_power(cam, 1);
534
535 return 0;
536}
537
538static int
539vicam_close(struct file *file)
540{
541 struct vicam_camera *cam = file->private_data;
542 int open_count;
543 struct usb_device *udev;
544
545 DBG("close\n");
546
547 /* it's not the end of the world if
548 * we fail to turn the camera off.
549 */
550
551 set_camera_power(cam, 0);
552
553 kfree(cam->raw_image);
554 rvfree(cam->framebuf, VICAM_MAX_FRAME_SIZE * VICAM_FRAMES);
555 kfree(cam->cntrlbuf);
556
557 mutex_lock(&cam->cam_lock);
558
559 cam->open_count--;
560 open_count = cam->open_count;
561 udev = cam->udev;
562
563 mutex_unlock(&cam->cam_lock);
564
565 if (!open_count && !udev) {
566 kfree(cam);
567 }
568
569 return 0;
570}
571
572static void vicam_decode_color(const u8 *data, u8 *rgb)
573{
574 /* vicam_decode_color - Convert from Vicam Y-Cr-Cb to RGB
575 * Copyright (C) 2002 Monroe Williams (monroe@pobox.com)
576 */
577
578 int i, prevY, nextY;
579
580 prevY = 512;
581 nextY = 512;
582
583 data += VICAM_HEADER_SIZE;
584
585 for( i = 0; i < 240; i++, data += 512 ) {
586 const int y = ( i * 242 ) / 240;
587
588 int j, prevX, nextX;
589 int Y, Cr, Cb;
590
591 if ( y == 242 - 1 ) {
592 nextY = -512;
593 }
594
595 prevX = 1;
596 nextX = 1;
597
598 for ( j = 0; j < 320; j++, rgb += 3 ) {
599 const int x = ( j * 512 ) / 320;
600 const u8 * const src = &data[x];
601
602 if ( x == 512 - 1 ) {
603 nextX = -1;
604 }
605
606 Cr = ( src[prevX] - src[0] ) +
607 ( src[nextX] - src[0] );
608 Cr /= 2;
609
610 Cb = ( src[prevY] - src[prevX + prevY] ) +
611 ( src[prevY] - src[nextX + prevY] ) +
612 ( src[nextY] - src[prevX + nextY] ) +
613 ( src[nextY] - src[nextX + nextY] );
614 Cb /= 4;
615
616 Y = 1160 * ( src[0] + ( Cr / 2 ) - 16 );
617
618 if ( i & 1 ) {
619 int Ct = Cr;
620 Cr = Cb;
621 Cb = Ct;
622 }
623
624 if ( ( x ^ i ) & 1 ) {
625 Cr = -Cr;
626 Cb = -Cb;
627 }
628
629 rgb[0] = clamp( ( ( Y + ( 2017 * Cb ) ) +
630 500 ) / 900, 0, 255 );
631 rgb[1] = clamp( ( ( Y - ( 392 * Cb ) -
632 ( 813 * Cr ) ) +
633 500 ) / 1000, 0, 255 );
634 rgb[2] = clamp( ( ( Y + ( 1594 * Cr ) ) +
635 500 ) / 1300, 0, 255 );
636
637 prevX = -1;
638 }
639
640 prevY = -512;
641 }
642}
643
644static void
645read_frame(struct vicam_camera *cam, int framenum)
646{
647 unsigned char *request = cam->cntrlbuf;
648 int realShutter;
649 int n;
650 int actual_length;
651
652 if (cam->needsDummyRead) {
653 cam->needsDummyRead = 0;
654 read_frame(cam, framenum);
655 }
656
657 memset(request, 0, 16);
658 request[0] = cam->gain; // 0 = 0% gain, FF = 100% gain
659
660 request[1] = 0; // 512x242 capture
661
662 request[2] = 0x90; // the function of these two bytes
663 request[3] = 0x07; // is not yet understood
664
665 if (cam->shutter_speed > 60) {
666 // Short exposure
667 realShutter =
668 ((-15631900 / cam->shutter_speed) + 260533) / 1000;
669 request[4] = realShutter & 0xFF;
670 request[5] = (realShutter >> 8) & 0xFF;
671 request[6] = 0x03;
672 request[7] = 0x01;
673 } else {
674 // Long exposure
675 realShutter = 15600 / cam->shutter_speed - 1;
676 request[4] = 0;
677 request[5] = 0;
678 request[6] = realShutter & 0xFF;
679 request[7] = realShutter >> 8;
680 }
681
682 // Per John Markus Bjørndalen, byte at index 8 causes problems if it isn't 0
683 request[8] = 0;
684 // bytes 9-15 do not seem to affect exposure or image quality
685
686 mutex_lock(&cam->cam_lock);
687
688 if (!cam->udev) {
689 goto done;
690 }
691
692 n = __send_control_msg(cam, 0x51, 0x80, 0, request, 16);
693
694 if (n < 0) {
695 printk(KERN_ERR
696 " Problem sending frame capture control message");
697 goto done;
698 }
699
700 n = usb_bulk_msg(cam->udev,
701 usb_rcvbulkpipe(cam->udev, cam->bulkEndpoint),
702 cam->raw_image,
703 512 * 242 + 128, &actual_length, 10000);
704
705 if (n < 0) {
706 printk(KERN_ERR "Problem during bulk read of frame data: %d\n",
707 n);
708 }
709
710 done:
711 mutex_unlock(&cam->cam_lock);
712}
713
714static ssize_t
715vicam_read( struct file *file, char __user *buf, size_t count, loff_t *ppos )
716{
717 struct vicam_camera *cam = file->private_data;
718
719 DBG("read %d bytes.\n", (int) count);
720
721 if (*ppos >= VICAM_MAX_FRAME_SIZE) {
722 *ppos = 0;
723 return 0;
724 }
725
726 if (*ppos == 0) {
727 read_frame(cam, 0);
728 vicam_decode_color(cam->raw_image,
729 cam->framebuf +
730 0 * VICAM_MAX_FRAME_SIZE);
731 }
732
733 count = min_t(size_t, count, VICAM_MAX_FRAME_SIZE - *ppos);
734
735 if (copy_to_user(buf, &cam->framebuf[*ppos], count)) {
736 count = -EFAULT;
737 } else {
738 *ppos += count;
739 }
740
741 if (count == VICAM_MAX_FRAME_SIZE) {
742 *ppos = 0;
743 }
744
745 return count;
746}
747
748
749static int
750vicam_mmap(struct file *file, struct vm_area_struct *vma)
751{
752 // TODO: allocate the raw frame buffer if necessary
753 unsigned long page, pos;
754 unsigned long start = vma->vm_start;
755 unsigned long size = vma->vm_end-vma->vm_start;
756 struct vicam_camera *cam = file->private_data;
757
758 if (!cam)
759 return -ENODEV;
760
761 DBG("vicam_mmap: %ld\n", size);
762
763 /* We let mmap allocate as much as it wants because Linux was adding 2048 bytes
764 * to the size the application requested for mmap and it was screwing apps up.
765 if (size > VICAM_FRAMES*VICAM_MAX_FRAME_SIZE)
766 return -EINVAL;
767 */
768
769 pos = (unsigned long)cam->framebuf;
770 while (size > 0) {
771 page = vmalloc_to_pfn((void *)pos);
772 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
773 return -EAGAIN;
774
775 start += PAGE_SIZE;
776 pos += PAGE_SIZE;
777 if (size > PAGE_SIZE)
778 size -= PAGE_SIZE;
779 else
780 size = 0;
781 }
782
783 return 0;
784}
785
786static const struct v4l2_file_operations vicam_fops = {
787 .owner = THIS_MODULE,
788 .open = vicam_open,
789 .release = vicam_close,
790 .read = vicam_read,
791 .mmap = vicam_mmap,
792 .ioctl = vicam_ioctl,
793};
794
795static struct video_device vicam_template = {
796 .name = "ViCam-based USB Camera",
797 .fops = &vicam_fops,
798 .release = video_device_release_empty,
799};
800
801/* table of devices that work with this driver */
802static struct usb_device_id vicam_table[] = {
803 {USB_DEVICE(USB_VICAM_VENDOR_ID, USB_VICAM_PRODUCT_ID)},
804 {USB_DEVICE(USB_COMPRO_VENDOR_ID, USB_COMPRO_PRODUCT_ID)},
805 {} /* Terminating entry */
806};
807
808MODULE_DEVICE_TABLE(usb, vicam_table);
809
810static struct usb_driver vicam_driver = {
811 .name = "vicam",
812 .probe = vicam_probe,
813 .disconnect = vicam_disconnect,
814 .id_table = vicam_table
815};
816
817/**
818 * vicam_probe
819 * @intf: the interface
820 * @id: the device id
821 *
822 * Called by the usb core when a new device is connected that it thinks
823 * this driver might be interested in.
824 */
825static int
826vicam_probe( struct usb_interface *intf, const struct usb_device_id *id)
827{
828 struct usb_device *dev = interface_to_usbdev(intf);
829 int bulkEndpoint = 0;
830 const struct usb_host_interface *interface;
831 const struct usb_endpoint_descriptor *endpoint;
832 struct vicam_camera *cam;
833
834 printk(KERN_INFO "ViCam based webcam connected\n");
835
836 interface = intf->cur_altsetting;
837
838 DBG(KERN_DEBUG "Interface %d. has %u. endpoints!\n",
839 interface->desc.bInterfaceNumber, (unsigned) (interface->desc.bNumEndpoints));
840 endpoint = &interface->endpoint[0].desc;
841
842 if (usb_endpoint_is_bulk_in(endpoint)) {
843 /* we found a bulk in endpoint */
844 bulkEndpoint = endpoint->bEndpointAddress;
845 } else {
846 printk(KERN_ERR
847 "No bulk in endpoint was found ?! (this is bad)\n");
848 }
849
850 if ((cam =
851 kzalloc(sizeof (struct vicam_camera), GFP_KERNEL)) == NULL) {
852 printk(KERN_WARNING
853 "could not allocate kernel memory for vicam_camera struct\n");
854 return -ENOMEM;
855 }
856
857
858 cam->shutter_speed = 15;
859
860 mutex_init(&cam->cam_lock);
861
862 memcpy(&cam->vdev, &vicam_template, sizeof(vicam_template));
863 video_set_drvdata(&cam->vdev, cam);
864
865 cam->udev = dev;
866 cam->bulkEndpoint = bulkEndpoint;
867
868 if (video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1) < 0) {
869 kfree(cam);
870 printk(KERN_WARNING "video_register_device failed\n");
871 return -EIO;
872 }
873
874 printk(KERN_INFO "ViCam webcam driver now controlling device %s\n",
875 video_device_node_name(&cam->vdev));
876
877 usb_set_intfdata (intf, cam);
878
879 return 0;
880}
881
882static void
883vicam_disconnect(struct usb_interface *intf)
884{
885 int open_count;
886 struct vicam_camera *cam = usb_get_intfdata (intf);
887 usb_set_intfdata (intf, NULL);
888
889 /* we must unregister the device before taking its
890 * cam_lock. This is because the video open call
891 * holds the same lock as video unregister. if we
892 * unregister inside of the cam_lock and open also
893 * uses the cam_lock, we get deadlock.
894 */
895
896 video_unregister_device(&cam->vdev);
897
898 /* stop the camera from being used */
899
900 mutex_lock(&cam->cam_lock);
901
902 /* mark the camera as gone */
903
904 cam->udev = NULL;
905
906 /* the only thing left to do is synchronize with
907 * our close/release function on who should release
908 * the camera memory. if there are any users using the
909 * camera, it's their job. if there are no users,
910 * it's ours.
911 */
912
913 open_count = cam->open_count;
914
915 mutex_unlock(&cam->cam_lock);
916
917 if (!open_count) {
918 kfree(cam);
919 }
920
921 printk(KERN_DEBUG "ViCam-based WebCam disconnected\n");
922}
923
924/*
925 */
926static int __init
927usb_vicam_init(void)
928{
929 int retval;
930 DBG(KERN_INFO "ViCam-based WebCam driver startup\n");
931 retval = usb_register(&vicam_driver);
932 if (retval)
933 printk(KERN_WARNING "usb_register failed!\n");
934 return retval;
935}
936
937static void __exit
938usb_vicam_exit(void)
939{
940 DBG(KERN_INFO
941 "ViCam-based WebCam driver shutdown\n");
942
943 usb_deregister(&vicam_driver);
944}
945
946module_init(usb_vicam_init);
947module_exit(usb_vicam_exit);
948
949MODULE_AUTHOR(DRIVER_AUTHOR);
950MODULE_DESCRIPTION(DRIVER_DESC);
951MODULE_LICENSE("GPL");
952MODULE_FIRMWARE("vicam/firmware.fw");