1
2
3
4
5
6
7
8
9
10
11
12
13
14
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)
33#endif
34
35static int video_nr = -1;
36module_param(video_nr, int, 0);
37
38
39
40
41static void usbvideo_Disconnect(struct usb_interface *intf);
42static void usbvideo_CameraRelease(struct uvd *uvd);
43
44static int usbvideo_v4l_ioctl(struct inode *inode, 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 inode *inode, 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 inode *inode, 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
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);
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
108
109
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
155 if(len > rql)
156 len = rql;
157
158 toread = len;
159 if(rq->ri > rq->wi) {
160
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
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
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
236
237
238
239
240
241
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
258
259
260
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,
267 0x2492,
268 0xE7CE,
269 0xE79E,
270 0xB792,
271 0xF39E,
272 0xF3DE,
273 0xF492,
274 0xF7DE,
275 0xF79E,
276 0x77DA,
277 0xD75C,
278 0xF24E,
279 0xD6DC,
280 0xF34E,
281 0xF348
282 };
283 unsigned short digit;
284 int ix, iy;
285
286 if ((uvd == NULL) || (frame == NULL))
287 return;
288
289 if (ch >= '0' && ch <= '9')
290 ch -= '0';
291 else if (ch >= 'A' && ch <= 'F')
292 ch = 10 + (ch - 'A');
293 else if (ch >= 'a' && ch <= 'f')
294 ch = 10 + (ch - 'a');
295 else
296 return;
297 digit = digits[ch];
298
299 for (iy=0; iy < 5; iy++) {
300 for (ix=0; ix < 3; ix++) {
301 if (digit & 0x8000) {
302 if (uvd->paletteBits & (1L << VIDEO_PALETTE_RGB24)) {
303 RGB24_PUTPIXEL(frame, x+ix, y+iy, 0xFF, 0xFF, 0xFF);
304 }
305 }
306 digit = digit << 1;
307 }
308 }
309}
310
311
312
313
314
315
316
317static void usbvideo_OverlayString(struct uvd *uvd, struct usbvideo_frame *frame,
318 int x, int y, const char *str)
319{
320 while (*str) {
321 usbvideo_OverlayChar(uvd, frame, x, y, *str);
322 str++;
323 x += 4;
324 }
325}
326
327
328
329
330
331
332
333
334
335static void usbvideo_OverlayStats(struct uvd *uvd, struct usbvideo_frame *frame)
336{
337 const int y_diff = 8;
338 char tmp[16];
339 int x = 10, y=10;
340 long i, j, barLength;
341 const int qi_x1 = 60, qi_y1 = 10;
342 const int qi_x2 = VIDEOSIZE_X(frame->request) - 10, qi_h = 10;
343
344
345 if (VALID_CALLBACK(uvd, overlayHook)) {
346 if (GET_CALLBACK(uvd, overlayHook)(uvd, frame) < 0)
347 return;
348 }
349
350
351
352
353
354
355 barLength = qi_x2 - qi_x1 - 2;
356 if ((barLength > 10) && (uvd->paletteBits & (1L << VIDEO_PALETTE_RGB24))) {
357 long u_lo, u_hi, q_used;
358 long m_ri, m_wi, m_lo, m_hi;
359
360
361
362
363
364
365
366
367 q_used = RingQueue_GetLength(&uvd->dp);
368 if ((uvd->dp.ri + q_used) >= uvd->dp.length) {
369 u_hi = uvd->dp.length;
370 u_lo = (q_used + uvd->dp.ri) & (uvd->dp.length-1);
371 } else {
372 u_hi = (q_used + uvd->dp.ri);
373 u_lo = -1;
374 }
375
376
377 m_ri = qi_x1 + ((barLength * uvd->dp.ri) / uvd->dp.length);
378 m_wi = qi_x1 + ((barLength * uvd->dp.wi) / uvd->dp.length);
379 m_lo = (u_lo > 0) ? (qi_x1 + ((barLength * u_lo) / uvd->dp.length)) : -1;
380 m_hi = qi_x1 + ((barLength * u_hi) / uvd->dp.length);
381
382 for (j=qi_y1; j < (qi_y1 + qi_h); j++) {
383 for (i=qi_x1; i < qi_x2; i++) {
384
385 if ((j == qi_y1) || (j == (qi_y1 + qi_h - 1)) ||
386 (i == qi_x1) || (i == (qi_x2 - 1))) {
387 RGB24_PUTPIXEL(frame, i, j, 0xFF, 0xFF, 0xFF);
388 continue;
389 }
390
391 if ((i >= m_ri) && (i <= (m_ri + 3))) {
392 RGB24_PUTPIXEL(frame, i, j, 0x00, 0xFF, 0x00);
393 } else if ((i >= m_wi) && (i <= (m_wi + 3))) {
394 RGB24_PUTPIXEL(frame, i, j, 0xFF, 0x00, 0x00);
395 } else if ((i < m_lo) || ((i > m_ri) && (i < m_hi)))
396 RGB24_PUTPIXEL(frame, i, j, 0x00, 0x00, 0xFF);
397 }
398 }
399 }
400
401 sprintf(tmp, "%8lx", uvd->stats.frame_num);
402 usbvideo_OverlayString(uvd, frame, x, y, tmp);
403 y += y_diff;
404
405 sprintf(tmp, "%8lx", uvd->stats.urb_count);
406 usbvideo_OverlayString(uvd, frame, x, y, tmp);
407 y += y_diff;
408
409 sprintf(tmp, "%8lx", uvd->stats.urb_length);
410 usbvideo_OverlayString(uvd, frame, x, y, tmp);
411 y += y_diff;
412
413 sprintf(tmp, "%8lx", uvd->stats.data_count);
414 usbvideo_OverlayString(uvd, frame, x, y, tmp);
415 y += y_diff;
416
417 sprintf(tmp, "%8lx", uvd->stats.header_count);
418 usbvideo_OverlayString(uvd, frame, x, y, tmp);
419 y += y_diff;
420
421 sprintf(tmp, "%8lx", uvd->stats.iso_skip_count);
422 usbvideo_OverlayString(uvd, frame, x, y, tmp);
423 y += y_diff;
424
425 sprintf(tmp, "%8lx", uvd->stats.iso_err_count);
426 usbvideo_OverlayString(uvd, frame, x, y, tmp);
427 y += y_diff;
428
429 sprintf(tmp, "%8x", uvd->vpic.colour);
430 usbvideo_OverlayString(uvd, frame, x, y, tmp);
431 y += y_diff;
432
433 sprintf(tmp, "%8x", uvd->vpic.hue);
434 usbvideo_OverlayString(uvd, frame, x, y, tmp);
435 y += y_diff;
436
437 sprintf(tmp, "%8x", uvd->vpic.brightness >> 8);
438 usbvideo_OverlayString(uvd, frame, x, y, tmp);
439 y += y_diff;
440
441 sprintf(tmp, "%8x", uvd->vpic.contrast >> 12);
442 usbvideo_OverlayString(uvd, frame, x, y, tmp);
443 y += y_diff;
444
445 sprintf(tmp, "%8d", uvd->vpic.whiteness >> 8);
446 usbvideo_OverlayString(uvd, frame, x, y, tmp);
447 y += y_diff;
448}
449
450
451
452
453
454
455
456
457
458static void usbvideo_ReportStatistics(const struct uvd *uvd)
459{
460 if ((uvd != NULL) && (uvd->stats.urb_count > 0)) {
461 unsigned long allPackets, badPackets, goodPackets, percent;
462 allPackets = uvd->stats.urb_count * CAMERA_URB_FRAMES;
463 badPackets = uvd->stats.iso_skip_count + uvd->stats.iso_err_count;
464 goodPackets = allPackets - badPackets;
465
466 assert(allPackets != 0);
467 if (goodPackets < (((unsigned long)-1)/100))
468 percent = (100 * goodPackets) / allPackets;
469 else
470 percent = goodPackets / (allPackets / 100);
471 info("Packet Statistics: Total=%lu. Empty=%lu. Usage=%lu%%",
472 allPackets, badPackets, percent);
473 if (uvd->iso_packet_len > 0) {
474 unsigned long allBytes, xferBytes;
475 char multiplier = ' ';
476 allBytes = allPackets * uvd->iso_packet_len;
477 xferBytes = uvd->stats.data_count;
478 assert(allBytes != 0);
479 if (xferBytes < (((unsigned long)-1)/100))
480 percent = (100 * xferBytes) / allBytes;
481 else
482 percent = xferBytes / (allBytes / 100);
483
484 if (xferBytes > 10*1024) {
485 xferBytes /= 1024;
486 multiplier = 'K';
487 if (xferBytes > 10*1024) {
488 xferBytes /= 1024;
489 multiplier = 'M';
490 if (xferBytes > 10*1024) {
491 xferBytes /= 1024;
492 multiplier = 'G';
493 if (xferBytes > 10*1024) {
494 xferBytes /= 1024;
495 multiplier = 'T';
496 }
497 }
498 }
499 }
500 info("Transfer Statistics: Transferred=%lu%cB Usage=%lu%%",
501 xferBytes, multiplier, percent);
502 }
503 }
504}
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520void usbvideo_TestPattern(struct uvd *uvd, int fullframe, int pmode)
521{
522 struct usbvideo_frame *frame;
523 int num_cell = 0;
524 int scan_length = 0;
525 static int num_pass = 0;
526
527 if (uvd == NULL) {
528 err("%s: uvd == NULL", __FUNCTION__);
529 return;
530 }
531 if ((uvd->curframe < 0) || (uvd->curframe >= USBVIDEO_NUMFRAMES)) {
532 err("%s: uvd->curframe=%d.", __FUNCTION__, uvd->curframe);
533 return;
534 }
535
536
537 frame = &uvd->frame[uvd->curframe];
538
539
540 if (fullframe) {
541 frame->curline = 0;
542 frame->seqRead_Length = 0;
543 }
544#if 0
545 {
546 char tmp[20];
547 usbvideo_VideosizeToString(tmp, sizeof(tmp), frame->request);
548 info("testpattern: frame=%s", tmp);
549 }
550#endif
551
552 for (; frame->curline < VIDEOSIZE_Y(frame->request); frame->curline++) {
553 int i;
554 unsigned char *f = frame->data +
555 (VIDEOSIZE_X(frame->request) * V4L_BYTES_PER_PIXEL * frame->curline);
556 for (i=0; i < VIDEOSIZE_X(frame->request); i++) {
557 unsigned char cb=0x80;
558 unsigned char cg = 0;
559 unsigned char cr = 0;
560
561 if (pmode == 1) {
562 if (frame->curline % 32 == 0)
563 cb = 0, cg = cr = 0xFF;
564 else if (i % 32 == 0) {
565 if (frame->curline % 32 == 1)
566 num_cell++;
567 cb = 0, cg = cr = 0xFF;
568 } else {
569 cb = ((num_cell*7) + num_pass) & 0xFF;
570 cg = ((num_cell*5) + num_pass*2) & 0xFF;
571 cr = ((num_cell*3) + num_pass*3) & 0xFF;
572 }
573 } else {
574
575 }
576
577 *f++ = cb;
578 *f++ = cg;
579 *f++ = cr;
580 scan_length += 3;
581 }
582 }
583
584 frame->frameState = FrameState_Done;
585 frame->seqRead_Length += scan_length;
586 ++num_pass;
587
588
589 usbvideo_OverlayStats(uvd, frame);
590}
591
592EXPORT_SYMBOL(usbvideo_TestPattern);
593
594
595#ifdef DEBUG
596
597
598
599
600
601
602
603
604void usbvideo_HexDump(const unsigned char *data, int len)
605{
606 const int bytes_per_line = 32;
607 char tmp[128];
608 int i, k;
609
610 for (i=k=0; len > 0; i++, len--) {
611 if (i > 0 && ((i % bytes_per_line) == 0)) {
612 printk("%s\n", tmp);
613 k=0;
614 }
615 if ((i % bytes_per_line) == 0)
616 k += sprintf(&tmp[k], "%04x: ", i);
617 k += sprintf(&tmp[k], "%02x ", data[i]);
618 }
619 if (k > 0)
620 printk("%s\n", tmp);
621}
622
623EXPORT_SYMBOL(usbvideo_HexDump);
624
625#endif
626
627
628
629
630static int usbvideo_ClientIncModCount(struct uvd *uvd)
631{
632 if (uvd == NULL) {
633 err("%s: uvd == NULL", __FUNCTION__);
634 return -EINVAL;
635 }
636 if (uvd->handle == NULL) {
637 err("%s: uvd->handle == NULL", __FUNCTION__);
638 return -EINVAL;
639 }
640 if (!try_module_get(uvd->handle->md_module)) {
641 err("%s: try_module_get() == 0", __FUNCTION__);
642 return -ENODEV;
643 }
644 return 0;
645}
646
647static void usbvideo_ClientDecModCount(struct uvd *uvd)
648{
649 if (uvd == NULL) {
650 err("%s: uvd == NULL", __FUNCTION__);
651 return;
652 }
653 if (uvd->handle == NULL) {
654 err("%s: uvd->handle == NULL", __FUNCTION__);
655 return;
656 }
657 if (uvd->handle->md_module == NULL) {
658 err("%s: uvd->handle->md_module == NULL", __FUNCTION__);
659 return;
660 }
661 module_put(uvd->handle->md_module);
662}
663
664int usbvideo_register(
665 struct usbvideo **pCams,
666 const int num_cams,
667 const int num_extra,
668 const char *driverName,
669 const struct usbvideo_cb *cbTbl,
670 struct module *md,
671 const struct usb_device_id *id_table)
672{
673 struct usbvideo *cams;
674 int i, base_size, result;
675
676
677 if ((num_cams <= 0) || (pCams == NULL) || (cbTbl == NULL)) {
678 err("%s: Illegal call", __FUNCTION__);
679 return -EINVAL;
680 }
681
682
683 if (cbTbl->probe == NULL) {
684 err("%s: probe() is required!", __FUNCTION__);
685 return -EINVAL;
686 }
687
688 base_size = num_cams * sizeof(struct uvd) + sizeof(struct usbvideo);
689 cams = kzalloc(base_size, GFP_KERNEL);
690 if (cams == NULL) {
691 err("Failed to allocate %d. bytes for usbvideo struct", base_size);
692 return -ENOMEM;
693 }
694 dbg("%s: Allocated $%p (%d. bytes) for %d. cameras",
695 __FUNCTION__, cams, base_size, num_cams);
696
697
698 memmove(&cams->cb, cbTbl, sizeof(cams->cb));
699 if (cams->cb.getFrame == NULL)
700 cams->cb.getFrame = usbvideo_GetFrame;
701 if (cams->cb.disconnect == NULL)
702 cams->cb.disconnect = usbvideo_Disconnect;
703 if (cams->cb.startDataPump == NULL)
704 cams->cb.startDataPump = usbvideo_StartDataPump;
705 if (cams->cb.stopDataPump == NULL)
706 cams->cb.stopDataPump = usbvideo_StopDataPump;
707
708 cams->num_cameras = num_cams;
709 cams->cam = (struct uvd *) &cams[1];
710 cams->md_module = md;
711 mutex_init(&cams->lock);
712
713 for (i = 0; i < num_cams; i++) {
714 struct uvd *up = &cams->cam[i];
715
716 up->handle = cams;
717
718
719 if (num_extra > 0) {
720 up->user_size = num_cams * num_extra;
721 up->user_data = kmalloc(up->user_size, GFP_KERNEL);
722 if (up->user_data == NULL) {
723 err("%s: Failed to allocate user_data (%d. bytes)",
724 __FUNCTION__, up->user_size);
725 while (i) {
726 up = &cams->cam[--i];
727 kfree(up->user_data);
728 }
729 kfree(cams);
730 return -ENOMEM;
731 }
732 dbg("%s: Allocated cams[%d].user_data=$%p (%d. bytes)",
733 __FUNCTION__, i, up->user_data, up->user_size);
734 }
735 }
736
737
738
739
740 strcpy(cams->drvName, (driverName != NULL) ? driverName : "Unknown");
741 cams->usbdrv.name = cams->drvName;
742 cams->usbdrv.probe = cams->cb.probe;
743 cams->usbdrv.disconnect = cams->cb.disconnect;
744 cams->usbdrv.id_table = id_table;
745
746
747
748
749
750
751 *pCams = cams;
752 result = usb_register(&cams->usbdrv);
753 if (result) {
754 for (i = 0; i < num_cams; i++) {
755 struct uvd *up = &cams->cam[i];
756 kfree(up->user_data);
757 }
758 kfree(cams);
759 }
760
761 return result;
762}
763
764EXPORT_SYMBOL(usbvideo_register);
765
766
767
768
769
770
771
772
773void usbvideo_Deregister(struct usbvideo **pCams)
774{
775 struct usbvideo *cams;
776 int i;
777
778 if (pCams == NULL) {
779 err("%s: pCams == NULL", __FUNCTION__);
780 return;
781 }
782 cams = *pCams;
783 if (cams == NULL) {
784 err("%s: cams == NULL", __FUNCTION__);
785 return;
786 }
787
788 dbg("%s: Deregistering %s driver.", __FUNCTION__, cams->drvName);
789 usb_deregister(&cams->usbdrv);
790
791 dbg("%s: Deallocating cams=$%p (%d. cameras)", __FUNCTION__, cams, cams->num_cameras);
792 for (i=0; i < cams->num_cameras; i++) {
793 struct uvd *up = &cams->cam[i];
794 int warning = 0;
795
796 if (up->user_data != NULL) {
797 if (up->user_size <= 0)
798 ++warning;
799 } else {
800 if (up->user_size > 0)
801 ++warning;
802 }
803 if (warning) {
804 err("%s: Warning: user_data=$%p user_size=%d.",
805 __FUNCTION__, up->user_data, up->user_size);
806 } else {
807 dbg("%s: Freeing %d. $%p->user_data=$%p",
808 __FUNCTION__, i, up, up->user_data);
809 kfree(up->user_data);
810 }
811 }
812
813 dbg("%s: Freed %d uvd structures",
814 __FUNCTION__, cams->num_cameras);
815 kfree(cams);
816 *pCams = NULL;
817}
818
819EXPORT_SYMBOL(usbvideo_Deregister);
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843static void usbvideo_Disconnect(struct usb_interface *intf)
844{
845 struct uvd *uvd = usb_get_intfdata (intf);
846 int i;
847
848 if (uvd == NULL) {
849 err("%s($%p): Illegal call.", __FUNCTION__, intf);
850 return;
851 }
852
853 usb_set_intfdata (intf, NULL);
854
855 usbvideo_ClientIncModCount(uvd);
856 if (uvd->debug > 0)
857 info("%s(%p.)", __FUNCTION__, intf);
858
859 mutex_lock(&uvd->lock);
860 uvd->remove_pending = 1;
861
862
863 GET_CALLBACK(uvd, stopDataPump)(uvd);
864
865 for (i=0; i < USBVIDEO_NUMSBUF; i++)
866 usb_free_urb(uvd->sbuf[i].urb);
867
868 usb_put_dev(uvd->dev);
869 uvd->dev = NULL;
870
871 video_unregister_device(&uvd->vdev);
872 if (uvd->debug > 0)
873 info("%s: Video unregistered.", __FUNCTION__);
874
875 if (uvd->user)
876 info("%s: In use, disconnect pending.", __FUNCTION__);
877 else
878 usbvideo_CameraRelease(uvd);
879 mutex_unlock(&uvd->lock);
880 info("USB camera disconnected.");
881
882 usbvideo_ClientDecModCount(uvd);
883}
884
885
886
887
888
889
890
891
892
893
894
895static void usbvideo_CameraRelease(struct uvd *uvd)
896{
897 if (uvd == NULL) {
898 err("%s: Illegal call", __FUNCTION__);
899 return;
900 }
901
902 RingQueue_Free(&uvd->dp);
903 if (VALID_CALLBACK(uvd, userFree))
904 GET_CALLBACK(uvd, userFree)(uvd);
905 uvd->uvd_used = 0;
906}
907
908
909
910
911
912
913
914
915
916
917
918static int usbvideo_find_struct(struct usbvideo *cams)
919{
920 int u, rv = -1;
921
922 if (cams == NULL) {
923 err("No usbvideo handle?");
924 return -1;
925 }
926 mutex_lock(&cams->lock);
927 for (u = 0; u < cams->num_cameras; u++) {
928 struct uvd *uvd = &cams->cam[u];
929 if (!uvd->uvd_used)
930 {
931 uvd->uvd_used = 1;
932 mutex_init(&uvd->lock);
933 uvd->dev = NULL;
934 rv = u;
935 break;
936 }
937 }
938 mutex_unlock(&cams->lock);
939 return rv;
940}
941
942static const struct file_operations usbvideo_fops = {
943 .owner = THIS_MODULE,
944 .open = usbvideo_v4l_open,
945 .release =usbvideo_v4l_close,
946 .read = usbvideo_v4l_read,
947 .mmap = usbvideo_v4l_mmap,
948 .ioctl = usbvideo_v4l_ioctl,
949 .compat_ioctl = v4l_compat_ioctl32,
950 .llseek = no_llseek,
951};
952static const struct video_device usbvideo_template = {
953 .owner = THIS_MODULE,
954 .type = VID_TYPE_CAPTURE,
955 .fops = &usbvideo_fops,
956};
957
958struct uvd *usbvideo_AllocateDevice(struct usbvideo *cams)
959{
960 int i, devnum;
961 struct uvd *uvd = NULL;
962
963 if (cams == NULL) {
964 err("No usbvideo handle?");
965 return NULL;
966 }
967
968 devnum = usbvideo_find_struct(cams);
969 if (devnum == -1) {
970 err("IBM USB camera driver: Too many devices!");
971 return NULL;
972 }
973 uvd = &cams->cam[devnum];
974 dbg("Device entry #%d. at $%p", devnum, uvd);
975
976
977 usbvideo_ClientIncModCount(uvd);
978
979 mutex_lock(&uvd->lock);
980 for (i=0; i < USBVIDEO_NUMSBUF; i++) {
981 uvd->sbuf[i].urb = usb_alloc_urb(FRAMES_PER_DESC, GFP_KERNEL);
982 if (uvd->sbuf[i].urb == NULL) {
983 err("usb_alloc_urb(%d.) failed.", FRAMES_PER_DESC);
984 uvd->uvd_used = 0;
985 uvd = NULL;
986 goto allocate_done;
987 }
988 }
989 uvd->user=0;
990 uvd->remove_pending = 0;
991 uvd->last_error = 0;
992 RingQueue_Initialize(&uvd->dp);
993
994
995 uvd->vdev = usbvideo_template;
996 sprintf(uvd->vdev.name, "%.20s USB Camera", cams->drvName);
997
998
999
1000
1001allocate_done:
1002 mutex_unlock(&uvd->lock);
1003 usbvideo_ClientDecModCount(uvd);
1004 return uvd;
1005}
1006
1007EXPORT_SYMBOL(usbvideo_AllocateDevice);
1008
1009int usbvideo_RegisterVideoDevice(struct uvd *uvd)
1010{
1011 char tmp1[20], tmp2[20];
1012
1013 if (uvd == NULL) {
1014 err("%s: Illegal call.", __FUNCTION__);
1015 return -EINVAL;
1016 }
1017 if (uvd->video_endp == 0) {
1018 info("%s: No video endpoint specified; data pump disabled.", __FUNCTION__);
1019 }
1020 if (uvd->paletteBits == 0) {
1021 err("%s: No palettes specified!", __FUNCTION__);
1022 return -EINVAL;
1023 }
1024 if (uvd->defaultPalette == 0) {
1025 info("%s: No default palette!", __FUNCTION__);
1026 }
1027
1028 uvd->max_frame_size = VIDEOSIZE_X(uvd->canvas) *
1029 VIDEOSIZE_Y(uvd->canvas) * V4L_BYTES_PER_PIXEL;
1030 usbvideo_VideosizeToString(tmp1, sizeof(tmp1), uvd->videosize);
1031 usbvideo_VideosizeToString(tmp2, sizeof(tmp2), uvd->canvas);
1032
1033 if (uvd->debug > 0) {
1034 info("%s: iface=%d. endpoint=$%02x paletteBits=$%08lx",
1035 __FUNCTION__, uvd->iface, uvd->video_endp, uvd->paletteBits);
1036 }
1037 if (video_register_device(&uvd->vdev, VFL_TYPE_GRABBER, video_nr) == -1) {
1038 err("%s: video_register_device failed", __FUNCTION__);
1039 return -EPIPE;
1040 }
1041 if (uvd->debug > 1) {
1042 info("%s: video_register_device() successful", __FUNCTION__);
1043 }
1044 if (uvd->dev == NULL) {
1045 err("%s: uvd->dev == NULL", __FUNCTION__);
1046 return -EINVAL;
1047 }
1048
1049 info("%s on /dev/video%d: canvas=%s videosize=%s",
1050 (uvd->handle != NULL) ? uvd->handle->drvName : "???",
1051 uvd->vdev.minor, tmp2, tmp1);
1052
1053 usb_get_dev(uvd->dev);
1054 return 0;
1055}
1056
1057EXPORT_SYMBOL(usbvideo_RegisterVideoDevice);
1058
1059
1060
1061static int usbvideo_v4l_mmap(struct file *file, struct vm_area_struct *vma)
1062{
1063 struct uvd *uvd = file->private_data;
1064 unsigned long start = vma->vm_start;
1065 unsigned long size = vma->vm_end-vma->vm_start;
1066 unsigned long page, pos;
1067
1068 if (!CAMERA_IS_OPERATIONAL(uvd))
1069 return -EFAULT;
1070
1071 if (size > (((USBVIDEO_NUMFRAMES * uvd->max_frame_size) + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1)))
1072 return -EINVAL;
1073
1074 pos = (unsigned long) uvd->fbuf;
1075 while (size > 0) {
1076 page = vmalloc_to_pfn((void *)pos);
1077 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
1078 return -EAGAIN;
1079
1080 start += PAGE_SIZE;
1081 pos += PAGE_SIZE;
1082 if (size > PAGE_SIZE)
1083 size -= PAGE_SIZE;
1084 else
1085 size = 0;
1086 }
1087
1088 return 0;
1089}
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105static int usbvideo_v4l_open(struct inode *inode, struct file *file)
1106{
1107 struct video_device *dev = video_devdata(file);
1108 struct uvd *uvd = (struct uvd *) dev;
1109 const int sb_size = FRAMES_PER_DESC * uvd->iso_packet_len;
1110 int i, errCode = 0;
1111
1112 if (uvd->debug > 1)
1113 info("%s($%p)", __FUNCTION__, dev);
1114
1115 if (0 < usbvideo_ClientIncModCount(uvd))
1116 return -ENODEV;
1117 mutex_lock(&uvd->lock);
1118
1119 if (uvd->user) {
1120 err("%s: Someone tried to open an already opened device!", __FUNCTION__);
1121 errCode = -EBUSY;
1122 } else {
1123
1124 memset(&uvd->stats, 0, sizeof(uvd->stats));
1125
1126
1127 for (i=0; i < USBVIDEO_NUMSBUF; i++)
1128 uvd->sbuf[i].data = NULL;
1129
1130
1131 uvd->fbuf_size = USBVIDEO_NUMFRAMES * uvd->max_frame_size;
1132 uvd->fbuf = usbvideo_rvmalloc(uvd->fbuf_size);
1133 RingQueue_Allocate(&uvd->dp, RING_QUEUE_SIZE);
1134 if ((uvd->fbuf == NULL) ||
1135 (!RingQueue_IsAllocated(&uvd->dp))) {
1136 err("%s: Failed to allocate fbuf or dp", __FUNCTION__);
1137 errCode = -ENOMEM;
1138 } else {
1139
1140 for (i=0; i < USBVIDEO_NUMFRAMES; i++) {
1141 uvd->frame[i].frameState = FrameState_Unused;
1142 uvd->frame[i].data = uvd->fbuf + i*(uvd->max_frame_size);
1143
1144
1145
1146
1147 uvd->frame[i].canvas = uvd->canvas;
1148 uvd->frame[i].seqRead_Index = 0;
1149 }
1150 for (i=0; i < USBVIDEO_NUMSBUF; i++) {
1151 uvd->sbuf[i].data = kmalloc(sb_size, GFP_KERNEL);
1152 if (uvd->sbuf[i].data == NULL) {
1153 errCode = -ENOMEM;
1154 break;
1155 }
1156 }
1157 }
1158 if (errCode != 0) {
1159
1160 if (uvd->fbuf != NULL) {
1161 usbvideo_rvfree(uvd->fbuf, uvd->fbuf_size);
1162 uvd->fbuf = NULL;
1163 }
1164 RingQueue_Free(&uvd->dp);
1165 for (i=0; i < USBVIDEO_NUMSBUF; i++) {
1166 kfree(uvd->sbuf[i].data);
1167 uvd->sbuf[i].data = NULL;
1168 }
1169 }
1170 }
1171
1172
1173 if (errCode == 0) {
1174
1175 if (uvd->video_endp != 0)
1176 errCode = GET_CALLBACK(uvd, startDataPump)(uvd);
1177 if (errCode == 0) {
1178 if (VALID_CALLBACK(uvd, setupOnOpen)) {
1179 if (uvd->debug > 1)
1180 info("%s: setupOnOpen callback", __FUNCTION__);
1181 errCode = GET_CALLBACK(uvd, setupOnOpen)(uvd);
1182 if (errCode < 0) {
1183 err("%s: setupOnOpen callback failed (%d.).",
1184 __FUNCTION__, errCode);
1185 } else if (uvd->debug > 1) {
1186 info("%s: setupOnOpen callback successful", __FUNCTION__);
1187 }
1188 }
1189 if (errCode == 0) {
1190 uvd->settingsAdjusted = 0;
1191 if (uvd->debug > 1)
1192 info("%s: Open succeeded.", __FUNCTION__);
1193 uvd->user++;
1194 file->private_data = uvd;
1195 }
1196 }
1197 }
1198 mutex_unlock(&uvd->lock);
1199 if (errCode != 0)
1200 usbvideo_ClientDecModCount(uvd);
1201 if (uvd->debug > 0)
1202 info("%s: Returning %d.", __FUNCTION__, errCode);
1203 return errCode;
1204}
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218static int usbvideo_v4l_close(struct inode *inode, struct file *file)
1219{
1220 struct video_device *dev = file->private_data;
1221 struct uvd *uvd = (struct uvd *) dev;
1222 int i;
1223
1224 if (uvd->debug > 1)
1225 info("%s($%p)", __FUNCTION__, dev);
1226
1227 mutex_lock(&uvd->lock);
1228 GET_CALLBACK(uvd, stopDataPump)(uvd);
1229 usbvideo_rvfree(uvd->fbuf, uvd->fbuf_size);
1230 uvd->fbuf = NULL;
1231 RingQueue_Free(&uvd->dp);
1232
1233 for (i=0; i < USBVIDEO_NUMSBUF; i++) {
1234 kfree(uvd->sbuf[i].data);
1235 uvd->sbuf[i].data = NULL;
1236 }
1237
1238#if USBVIDEO_REPORT_STATS
1239 usbvideo_ReportStatistics(uvd);
1240#endif
1241
1242 uvd->user--;
1243 if (uvd->remove_pending) {
1244 if (uvd->debug > 0)
1245 info("usbvideo_v4l_close: Final disconnect.");
1246 usbvideo_CameraRelease(uvd);
1247 }
1248 mutex_unlock(&uvd->lock);
1249 usbvideo_ClientDecModCount(uvd);
1250
1251 if (uvd->debug > 1)
1252 info("%s: Completed.", __FUNCTION__);
1253 file->private_data = NULL;
1254 return 0;
1255}
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265static int usbvideo_v4l_do_ioctl(struct inode *inode, struct file *file,
1266 unsigned int cmd, void *arg)
1267{
1268 struct uvd *uvd = file->private_data;
1269
1270 if (!CAMERA_IS_OPERATIONAL(uvd))
1271 return -EIO;
1272
1273 switch (cmd) {
1274 case VIDIOCGCAP:
1275 {
1276 struct video_capability *b = arg;
1277 *b = uvd->vcap;
1278 return 0;
1279 }
1280 case VIDIOCGCHAN:
1281 {
1282 struct video_channel *v = arg;
1283 *v = uvd->vchan;
1284 return 0;
1285 }
1286 case VIDIOCSCHAN:
1287 {
1288 struct video_channel *v = arg;
1289 if (v->channel != 0)
1290 return -EINVAL;
1291 return 0;
1292 }
1293 case VIDIOCGPICT:
1294 {
1295 struct video_picture *pic = arg;
1296 *pic = uvd->vpic;
1297 return 0;
1298 }
1299 case VIDIOCSPICT:
1300 {
1301 struct video_picture *pic = arg;
1302
1303
1304
1305
1306
1307 uvd->vpic.brightness = pic->brightness;
1308 uvd->vpic.hue = pic->hue;
1309 uvd->vpic.colour = pic->colour;
1310 uvd->vpic.contrast = pic->contrast;
1311 uvd->settingsAdjusted = 0;
1312 return 0;
1313 }
1314 case VIDIOCSWIN:
1315 {
1316 struct video_window *vw = arg;
1317
1318 if(VALID_CALLBACK(uvd, setVideoMode)) {
1319 return GET_CALLBACK(uvd, setVideoMode)(uvd, vw);
1320 }
1321
1322 if (vw->flags)
1323 return -EINVAL;
1324 if (vw->clipcount)
1325 return -EINVAL;
1326 if (vw->width != VIDEOSIZE_X(uvd->canvas))
1327 return -EINVAL;
1328 if (vw->height != VIDEOSIZE_Y(uvd->canvas))
1329 return -EINVAL;
1330
1331 return 0;
1332 }
1333 case VIDIOCGWIN:
1334 {
1335 struct video_window *vw = arg;
1336
1337 vw->x = 0;
1338 vw->y = 0;
1339 vw->width = VIDEOSIZE_X(uvd->videosize);
1340 vw->height = VIDEOSIZE_Y(uvd->videosize);
1341 vw->chromakey = 0;
1342 if (VALID_CALLBACK(uvd, getFPS))
1343 vw->flags = GET_CALLBACK(uvd, getFPS)(uvd);
1344 else
1345 vw->flags = 10;
1346 return 0;
1347 }
1348 case VIDIOCGMBUF:
1349 {
1350 struct video_mbuf *vm = arg;
1351 int i;
1352
1353 memset(vm, 0, sizeof(*vm));
1354 vm->size = uvd->max_frame_size * USBVIDEO_NUMFRAMES;
1355 vm->frames = USBVIDEO_NUMFRAMES;
1356 for(i = 0; i < USBVIDEO_NUMFRAMES; i++)
1357 vm->offsets[i] = i * uvd->max_frame_size;
1358
1359 return 0;
1360 }
1361 case VIDIOCMCAPTURE:
1362 {
1363 struct video_mmap *vm = arg;
1364
1365 if (uvd->debug >= 1) {
1366 info("VIDIOCMCAPTURE: frame=%d. size=%dx%d, format=%d.",
1367 vm->frame, vm->width, vm->height, vm->format);
1368 }
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382 if ((vm->width > VIDEOSIZE_X(uvd->canvas)) ||
1383 (vm->height > VIDEOSIZE_Y(uvd->canvas))) {
1384 if (uvd->debug > 0) {
1385 info("VIDIOCMCAPTURE: Size=%dx%d too large; "
1386 "allowed only up to %ldx%ld", vm->width, vm->height,
1387 VIDEOSIZE_X(uvd->canvas), VIDEOSIZE_Y(uvd->canvas));
1388 }
1389 return -EINVAL;
1390 }
1391
1392 if (((1L << vm->format) & uvd->paletteBits) == 0) {
1393 if (uvd->debug > 0) {
1394 info("VIDIOCMCAPTURE: format=%d. not supported"
1395 " (paletteBits=$%08lx)",
1396 vm->format, uvd->paletteBits);
1397 }
1398 return -EINVAL;
1399 }
1400 if ((vm->frame < 0) || (vm->frame >= USBVIDEO_NUMFRAMES)) {
1401 err("VIDIOCMCAPTURE: vm.frame=%d. !E [0-%d]", vm->frame, USBVIDEO_NUMFRAMES-1);
1402 return -EINVAL;
1403 }
1404 if (uvd->frame[vm->frame].frameState == FrameState_Grabbing) {
1405
1406 }
1407 uvd->frame[vm->frame].request = VIDEOSIZE(vm->width, vm->height);
1408 uvd->frame[vm->frame].palette = vm->format;
1409
1410
1411 uvd->frame[vm->frame].frameState = FrameState_Ready;
1412
1413 return usbvideo_NewFrame(uvd, vm->frame);
1414 }
1415 case VIDIOCSYNC:
1416 {
1417 int *frameNum = arg;
1418 int ret;
1419
1420 if (*frameNum < 0 || *frameNum >= USBVIDEO_NUMFRAMES)
1421 return -EINVAL;
1422
1423 if (uvd->debug >= 1)
1424 info("VIDIOCSYNC: syncing to frame %d.", *frameNum);
1425 if (uvd->flags & FLAGS_NO_DECODING)
1426 ret = usbvideo_GetFrame(uvd, *frameNum);
1427 else if (VALID_CALLBACK(uvd, getFrame)) {
1428 ret = GET_CALLBACK(uvd, getFrame)(uvd, *frameNum);
1429 if ((ret < 0) && (uvd->debug >= 1)) {
1430 err("VIDIOCSYNC: getFrame() returned %d.", ret);
1431 }
1432 } else {
1433 err("VIDIOCSYNC: getFrame is not set");
1434 ret = -EFAULT;
1435 }
1436
1437
1438
1439
1440
1441
1442
1443 uvd->frame[*frameNum].frameState = FrameState_Unused;
1444 return ret;
1445 }
1446 case VIDIOCGFBUF:
1447 {
1448 struct video_buffer *vb = arg;
1449
1450 memset(vb, 0, sizeof(*vb));
1451 return 0;
1452 }
1453 case VIDIOCKEY:
1454 return 0;
1455
1456 case VIDIOCCAPTURE:
1457 return -EINVAL;
1458
1459 case VIDIOCSFBUF:
1460
1461 case VIDIOCGTUNER:
1462 case VIDIOCSTUNER:
1463
1464 case VIDIOCGFREQ:
1465 case VIDIOCSFREQ:
1466
1467 case VIDIOCGAUDIO:
1468 case VIDIOCSAUDIO:
1469 return -EINVAL;
1470
1471 default:
1472 return -ENOIOCTLCMD;
1473 }
1474 return 0;
1475}
1476
1477static int usbvideo_v4l_ioctl(struct inode *inode, struct file *file,
1478 unsigned int cmd, unsigned long arg)
1479{
1480 return video_usercopy(inode, file, cmd, arg, usbvideo_v4l_do_ioctl);
1481}
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494static ssize_t usbvideo_v4l_read(struct file *file, char __user *buf,
1495 size_t count, loff_t *ppos)
1496{
1497 struct uvd *uvd = file->private_data;
1498 int noblock = file->f_flags & O_NONBLOCK;
1499 int frmx = -1, i;
1500 struct usbvideo_frame *frame;
1501
1502 if (!CAMERA_IS_OPERATIONAL(uvd) || (buf == NULL))
1503 return -EFAULT;
1504
1505 if (uvd->debug >= 1)
1506 info("%s: %Zd. bytes, noblock=%d.", __FUNCTION__, count, noblock);
1507
1508 mutex_lock(&uvd->lock);
1509
1510
1511 for(i = 0; i < USBVIDEO_NUMFRAMES; i++) {
1512 if ((uvd->frame[i].frameState == FrameState_Done) ||
1513 (uvd->frame[i].frameState == FrameState_Done_Hold) ||
1514 (uvd->frame[i].frameState == FrameState_Error)) {
1515 frmx = i;
1516 break;
1517 }
1518 }
1519
1520
1521 if (noblock && (frmx == -1)) {
1522 count = -EAGAIN;
1523 goto read_done;
1524 }
1525
1526
1527
1528
1529
1530
1531 if (frmx == -1) {
1532 for(i = 0; i < USBVIDEO_NUMFRAMES; i++) {
1533 if (uvd->frame[i].frameState == FrameState_Grabbing) {
1534 frmx = i;
1535 break;
1536 }
1537 }
1538 }
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551 if (frmx == -1) {
1552 if (uvd->defaultPalette == 0) {
1553 err("%s: No default palette; don't know what to do!", __FUNCTION__);
1554 count = -EFAULT;
1555 goto read_done;
1556 }
1557 frmx = 0;
1558
1559
1560
1561
1562
1563 uvd->frame[frmx].request = uvd->videosize;
1564 uvd->frame[frmx].palette = uvd->defaultPalette;
1565 uvd->frame[frmx].frameState = FrameState_Ready;
1566 usbvideo_NewFrame(uvd, frmx);
1567
1568 }
1569
1570
1571
1572
1573
1574 frame = &uvd->frame[frmx];
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584 if (frame->frameState != FrameState_Done_Hold) {
1585 long rv = -EFAULT;
1586 if (uvd->flags & FLAGS_NO_DECODING)
1587 rv = usbvideo_GetFrame(uvd, frmx);
1588 else if (VALID_CALLBACK(uvd, getFrame))
1589 rv = GET_CALLBACK(uvd, getFrame)(uvd, frmx);
1590 else
1591 err("getFrame is not set");
1592 if ((rv != 0) || (frame->frameState != FrameState_Done_Hold)) {
1593 count = rv;
1594 goto read_done;
1595 }
1596 }
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608 if ((count + frame->seqRead_Index) > frame->seqRead_Length)
1609 count = frame->seqRead_Length - frame->seqRead_Index;
1610
1611
1612
1613
1614
1615
1616 if (copy_to_user(buf, frame->data + frame->seqRead_Index, count)) {
1617 count = -EFAULT;
1618 goto read_done;
1619 }
1620
1621
1622 frame->seqRead_Index += count;
1623 if (uvd->debug >= 1) {
1624 err("%s: {copy} count used=%Zd, new seqRead_Index=%ld",
1625 __FUNCTION__, count, frame->seqRead_Index);
1626 }
1627
1628
1629 if (frame->seqRead_Index >= frame->seqRead_Length) {
1630
1631 frame->seqRead_Index = 0;
1632
1633
1634 uvd->frame[frmx].frameState = FrameState_Unused;
1635 if (usbvideo_NewFrame(uvd, (frmx + 1) % USBVIDEO_NUMFRAMES)) {
1636 err("%s: usbvideo_NewFrame failed.", __FUNCTION__);
1637 }
1638 }
1639read_done:
1640 mutex_unlock(&uvd->lock);
1641 return count;
1642}
1643
1644
1645
1646
1647static int usbvideo_CompressIsochronous(struct uvd *uvd, struct urb *urb)
1648{
1649 char *cdata;
1650 int i, totlen = 0;
1651
1652 for (i = 0; i < urb->number_of_packets; i++) {
1653 int n = urb->iso_frame_desc[i].actual_length;
1654 int st = urb->iso_frame_desc[i].status;
1655
1656 cdata = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
1657
1658
1659 if (st < 0) {
1660 if (uvd->debug >= 1)
1661 err("Data error: packet=%d. len=%d. status=%d.", i, n, st);
1662 uvd->stats.iso_err_count++;
1663 continue;
1664 }
1665
1666
1667 if (n <= 0) {
1668 uvd->stats.iso_skip_count++;
1669 continue;
1670 }
1671 totlen += n;
1672 RingQueue_Enqueue(&uvd->dp, cdata, n);
1673 }
1674 return totlen;
1675}
1676
1677static void usbvideo_IsocIrq(struct urb *urb)
1678{
1679 int i, ret, len;
1680 struct uvd *uvd = urb->context;
1681
1682
1683 if (!CAMERA_IS_OPERATIONAL(uvd))
1684 return;
1685#if 0
1686 if (urb->actual_length > 0) {
1687 info("urb=$%p status=%d. errcount=%d. length=%d.",
1688 urb, urb->status, urb->error_count, urb->actual_length);
1689 } else {
1690 static int c = 0;
1691 if (c++ % 100 == 0)
1692 info("No Isoc data");
1693 }
1694#endif
1695
1696 if (!uvd->streaming) {
1697 if (uvd->debug >= 1)
1698 info("Not streaming, but interrupt!");
1699 return;
1700 }
1701
1702 uvd->stats.urb_count++;
1703 if (urb->actual_length <= 0)
1704 goto urb_done_with;
1705
1706
1707 len = usbvideo_CompressIsochronous(uvd, urb);
1708 uvd->stats.urb_length = len;
1709 if (len <= 0)
1710 goto urb_done_with;
1711
1712
1713 uvd->stats.data_count += len;
1714 RingQueue_WakeUpInterruptible(&uvd->dp);
1715
1716urb_done_with:
1717 for (i = 0; i < FRAMES_PER_DESC; i++) {
1718 urb->iso_frame_desc[i].status = 0;
1719 urb->iso_frame_desc[i].actual_length = 0;
1720 }
1721 urb->status = 0;
1722 urb->dev = uvd->dev;
1723 ret = usb_submit_urb (urb, GFP_KERNEL);
1724 if(ret)
1725 err("usb_submit_urb error (%d)", ret);
1726 return;
1727}
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737static int usbvideo_StartDataPump(struct uvd *uvd)
1738{
1739 struct usb_device *dev = uvd->dev;
1740 int i, errFlag;
1741
1742 if (uvd->debug > 1)
1743 info("%s($%p)", __FUNCTION__, uvd);
1744
1745 if (!CAMERA_IS_OPERATIONAL(uvd)) {
1746 err("%s: Camera is not operational", __FUNCTION__);
1747 return -EFAULT;
1748 }
1749 uvd->curframe = -1;
1750
1751
1752 i = usb_set_interface(dev, uvd->iface, uvd->ifaceAltActive);
1753 if (i < 0) {
1754 err("%s: usb_set_interface error", __FUNCTION__);
1755 uvd->last_error = i;
1756 return -EBUSY;
1757 }
1758 if (VALID_CALLBACK(uvd, videoStart))
1759 GET_CALLBACK(uvd, videoStart)(uvd);
1760 else
1761 err("%s: videoStart not set", __FUNCTION__);
1762
1763
1764 for (i=0; i < USBVIDEO_NUMSBUF; i++) {
1765 int j, k;
1766 struct urb *urb = uvd->sbuf[i].urb;
1767 urb->dev = dev;
1768 urb->context = uvd;
1769 urb->pipe = usb_rcvisocpipe(dev, uvd->video_endp);
1770 urb->interval = 1;
1771 urb->transfer_flags = URB_ISO_ASAP;
1772 urb->transfer_buffer = uvd->sbuf[i].data;
1773 urb->complete = usbvideo_IsocIrq;
1774 urb->number_of_packets = FRAMES_PER_DESC;
1775 urb->transfer_buffer_length = uvd->iso_packet_len * FRAMES_PER_DESC;
1776 for (j=k=0; j < FRAMES_PER_DESC; j++, k += uvd->iso_packet_len) {
1777 urb->iso_frame_desc[j].offset = k;
1778 urb->iso_frame_desc[j].length = uvd->iso_packet_len;
1779 }
1780 }
1781
1782
1783 for (i=0; i < USBVIDEO_NUMSBUF; i++) {
1784 errFlag = usb_submit_urb(uvd->sbuf[i].urb, GFP_KERNEL);
1785 if (errFlag)
1786 err("%s: usb_submit_isoc(%d) ret %d", __FUNCTION__, i, errFlag);
1787 }
1788
1789 uvd->streaming = 1;
1790 if (uvd->debug > 1)
1791 info("%s: streaming=1 video_endp=$%02x", __FUNCTION__, uvd->video_endp);
1792 return 0;
1793}
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805static void usbvideo_StopDataPump(struct uvd *uvd)
1806{
1807 int i, j;
1808
1809 if ((uvd == NULL) || (!uvd->streaming) || (uvd->dev == NULL))
1810 return;
1811
1812 if (uvd->debug > 1)
1813 info("%s($%p)", __FUNCTION__, uvd);
1814
1815
1816 for (i=0; i < USBVIDEO_NUMSBUF; i++) {
1817 usb_kill_urb(uvd->sbuf[i].urb);
1818 }
1819 if (uvd->debug > 1)
1820 info("%s: streaming=0", __FUNCTION__);
1821 uvd->streaming = 0;
1822
1823 if (!uvd->remove_pending) {
1824
1825 if (VALID_CALLBACK(uvd, videoStop))
1826 GET_CALLBACK(uvd, videoStop)(uvd);
1827 else
1828 err("%s: videoStop not set", __FUNCTION__);
1829
1830
1831 j = usb_set_interface(uvd->dev, uvd->iface, uvd->ifaceAltInactive);
1832 if (j < 0) {
1833 err("%s: usb_set_interface() error %d.", __FUNCTION__, j);
1834 uvd->last_error = j;
1835 }
1836 }
1837}
1838
1839
1840
1841
1842
1843
1844
1845
1846static int usbvideo_NewFrame(struct uvd *uvd, int framenum)
1847{
1848 struct usbvideo_frame *frame;
1849 int n;
1850
1851 if (uvd->debug > 1)
1852 info("usbvideo_NewFrame($%p,%d.)", uvd, framenum);
1853
1854
1855
1856 if (uvd->curframe != -1)
1857 return 0;
1858
1859
1860 if (!uvd->settingsAdjusted) {
1861 if (VALID_CALLBACK(uvd, adjustPicture))
1862 GET_CALLBACK(uvd, adjustPicture)(uvd);
1863 uvd->settingsAdjusted = 1;
1864 }
1865
1866 n = (framenum + 1) % USBVIDEO_NUMFRAMES;
1867 if (uvd->frame[n].frameState == FrameState_Ready)
1868 framenum = n;
1869
1870 frame = &uvd->frame[framenum];
1871
1872 frame->frameState = FrameState_Grabbing;
1873 frame->scanstate = ScanState_Scanning;
1874 frame->seqRead_Length = 0;
1875 frame->deinterlace = Deinterlace_None;
1876 frame->flags = 0;
1877 uvd->curframe = framenum;
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896 if (!(uvd->flags & FLAGS_SEPARATE_FRAMES)) {
1897
1898 int prev = (framenum - 1 + USBVIDEO_NUMFRAMES) % USBVIDEO_NUMFRAMES;
1899 memmove(frame->data, uvd->frame[prev].data, uvd->max_frame_size);
1900 } else {
1901 if (uvd->flags & FLAGS_CLEAN_FRAMES) {
1902
1903 memset(frame->data, 0, uvd->max_frame_size);
1904 }
1905 }
1906 return 0;
1907}
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922static void usbvideo_CollectRawData(struct uvd *uvd, struct usbvideo_frame *frame)
1923{
1924 int n;
1925
1926 assert(uvd != NULL);
1927 assert(frame != NULL);
1928
1929
1930 n = RingQueue_GetLength(&uvd->dp);
1931 if (n > 0) {
1932 int m;
1933
1934 m = uvd->max_frame_size - frame->seqRead_Length;
1935 if (n > m)
1936 n = m;
1937
1938 RingQueue_Dequeue(
1939 &uvd->dp,
1940 frame->data + frame->seqRead_Length,
1941 m);
1942 frame->seqRead_Length += m;
1943 }
1944
1945 if (frame->seqRead_Length >= uvd->max_frame_size) {
1946 frame->frameState = FrameState_Done;
1947 uvd->curframe = -1;
1948 uvd->stats.frame_num++;
1949 }
1950}
1951
1952static int usbvideo_GetFrame(struct uvd *uvd, int frameNum)
1953{
1954 struct usbvideo_frame *frame = &uvd->frame[frameNum];
1955
1956 if (uvd->debug >= 2)
1957 info("%s($%p,%d.)", __FUNCTION__, uvd, frameNum);
1958
1959 switch (frame->frameState) {
1960 case FrameState_Unused:
1961 if (uvd->debug >= 2)
1962 info("%s: FrameState_Unused", __FUNCTION__);
1963 return -EINVAL;
1964 case FrameState_Ready:
1965 case FrameState_Grabbing:
1966 case FrameState_Error:
1967 {
1968 int ntries, signalPending;
1969 redo:
1970 if (!CAMERA_IS_OPERATIONAL(uvd)) {
1971 if (uvd->debug >= 2)
1972 info("%s: Camera is not operational (1)", __FUNCTION__);
1973 return -EIO;
1974 }
1975 ntries = 0;
1976 do {
1977 RingQueue_InterruptibleSleepOn(&uvd->dp);
1978 signalPending = signal_pending(current);
1979 if (!CAMERA_IS_OPERATIONAL(uvd)) {
1980 if (uvd->debug >= 2)
1981 info("%s: Camera is not operational (2)", __FUNCTION__);
1982 return -EIO;
1983 }
1984 assert(uvd->fbuf != NULL);
1985 if (signalPending) {
1986 if (uvd->debug >= 2)
1987 info("%s: Signal=$%08x", __FUNCTION__, signalPending);
1988 if (uvd->flags & FLAGS_RETRY_VIDIOCSYNC) {
1989 usbvideo_TestPattern(uvd, 1, 0);
1990 uvd->curframe = -1;
1991 uvd->stats.frame_num++;
1992 if (uvd->debug >= 2)
1993 info("%s: Forced test pattern screen", __FUNCTION__);
1994 return 0;
1995 } else {
1996
1997 if (uvd->debug >= 2)
1998 info("%s: Interrupted!", __FUNCTION__);
1999 return -EINTR;
2000 }
2001 } else {
2002
2003 if (uvd->flags & FLAGS_NO_DECODING)
2004 usbvideo_CollectRawData(uvd, frame);
2005 else if (VALID_CALLBACK(uvd, processData))
2006 GET_CALLBACK(uvd, processData)(uvd, frame);
2007 else
2008 err("%s: processData not set", __FUNCTION__);
2009 }
2010 } while (frame->frameState == FrameState_Grabbing);
2011 if (uvd->debug >= 2) {
2012 info("%s: Grabbing done; state=%d. (%lu. bytes)",
2013 __FUNCTION__, frame->frameState, frame->seqRead_Length);
2014 }
2015 if (frame->frameState == FrameState_Error) {
2016 int ret = usbvideo_NewFrame(uvd, frameNum);
2017 if (ret < 0) {
2018 err("%s: usbvideo_NewFrame() failed (%d.)", __FUNCTION__, ret);
2019 return ret;
2020 }
2021 goto redo;
2022 }
2023
2024 }
2025 case FrameState_Done:
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041 uvd->stats.frame_num++;
2042 if ((uvd->flags & FLAGS_NO_DECODING) == 0) {
2043 if (VALID_CALLBACK(uvd, postProcess))
2044 GET_CALLBACK(uvd, postProcess)(uvd, frame);
2045 if (frame->flags & USBVIDEO_FRAME_FLAG_SOFTWARE_CONTRAST)
2046 usbvideo_SoftwareContrastAdjustment(uvd, frame);
2047 }
2048 frame->frameState = FrameState_Done_Hold;
2049 if (uvd->debug >= 2)
2050 info("%s: Entered FrameState_Done_Hold state.", __FUNCTION__);
2051 return 0;
2052
2053 case FrameState_Done_Hold:
2054
2055
2056
2057
2058
2059
2060 if (uvd->debug >= 2)
2061 info("%s: FrameState_Done_Hold state.", __FUNCTION__);
2062 return 0;
2063 }
2064
2065
2066 err("%s: Invalid state %d.", __FUNCTION__, frame->frameState);
2067 frame->frameState = FrameState_Unused;
2068 return 0;
2069}
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084void usbvideo_DeinterlaceFrame(struct uvd *uvd, struct usbvideo_frame *frame)
2085{
2086 if ((uvd == NULL) || (frame == NULL))
2087 return;
2088
2089 if ((frame->deinterlace == Deinterlace_FillEvenLines) ||
2090 (frame->deinterlace == Deinterlace_FillOddLines))
2091 {
2092 const int v4l_linesize = VIDEOSIZE_X(frame->request) * V4L_BYTES_PER_PIXEL;
2093 int i = (frame->deinterlace == Deinterlace_FillEvenLines) ? 0 : 1;
2094
2095 for (; i < VIDEOSIZE_Y(frame->request); i += 2) {
2096 const unsigned char *fs1, *fs2;
2097 unsigned char *fd;
2098 int ip, in, j;
2099
2100
2101
2102
2103
2104
2105 ip = i - 1;
2106 in = i + 1;
2107
2108
2109 if (ip < 0)
2110 ip = in;
2111 if (in >= VIDEOSIZE_Y(frame->request))
2112 in = ip;
2113
2114
2115 if ((ip < 0) || (in < 0) ||
2116 (ip >= VIDEOSIZE_Y(frame->request)) ||
2117 (in >= VIDEOSIZE_Y(frame->request)))
2118 {
2119 err("Error: ip=%d. in=%d. req.height=%ld.",
2120 ip, in, VIDEOSIZE_Y(frame->request));
2121 break;
2122 }
2123
2124
2125 fs1 = frame->data + (v4l_linesize * ip);
2126 fs2 = frame->data + (v4l_linesize * in);
2127 fd = frame->data + (v4l_linesize * i);
2128
2129
2130 for (j=0; j < v4l_linesize; j++) {
2131 fd[j] = (unsigned char)((((unsigned) fs1[j]) +
2132 ((unsigned)fs2[j])) >> 1);
2133 }
2134 }
2135 }
2136
2137
2138 if (uvd->flags & FLAGS_OVERLAY_STATS)
2139 usbvideo_OverlayStats(uvd, frame);
2140}
2141
2142EXPORT_SYMBOL(usbvideo_DeinterlaceFrame);
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154static void usbvideo_SoftwareContrastAdjustment(struct uvd *uvd,
2155 struct usbvideo_frame *frame)
2156{
2157 int i, j, v4l_linesize;
2158 signed long adj;
2159 const int ccm = 128;
2160
2161 if ((uvd == NULL) || (frame == NULL)) {
2162 err("%s: Illegal call.", __FUNCTION__);
2163 return;
2164 }
2165 adj = (uvd->vpic.contrast - 0x8000) >> 8;
2166 RESTRICT_TO_RANGE(adj, -ccm, ccm+1);
2167 if (adj == 0) {
2168
2169 return;
2170 }
2171 v4l_linesize = VIDEOSIZE_X(frame->request) * V4L_BYTES_PER_PIXEL;
2172 for (i=0; i < VIDEOSIZE_Y(frame->request); i++) {
2173 unsigned char *fd = frame->data + (v4l_linesize * i);
2174 for (j=0; j < v4l_linesize; j++) {
2175 signed long v = (signed long) fd[j];
2176
2177 v = 128 + ((ccm + adj) * (v - 128)) / ccm;
2178 RESTRICT_TO_RANGE(v, 0, 0xFF);
2179 fd[j] = (unsigned char) v;
2180 }
2181 }
2182}
2183
2184MODULE_LICENSE("GPL");
2185