aboutsummaryrefslogtreecommitdiffstats
path: root/drivers
diff options
context:
space:
mode:
authorMauro Carvalho Chehab <mchehab@infradead.org>2008-04-13 13:37:52 -0400
committerMauro Carvalho Chehab <mchehab@infradead.org>2008-04-24 13:08:00 -0400
commitad0ebb96c220c461386e9a765fca3daf5590d01e (patch)
tree4c13c24682703cbb94f415eb944c573ac0835ccc /drivers
parent78e92006f410a4044f8c1760c25ac9d11d259aa2 (diff)
V4L/DVB (7540): em28xx: convert to use videobuf-vmalloc
The usage of videobuf-vmalloc allows to cleanup em28xx logic. Also, it reduced its size by about 5.42% on i386 arch (and about 7.5% on x86_64): 39113 4876 40 44029 abfd old/em28xx.ko 36731 4868 40 41639 a2a7 /home/v4l/master/v4l/em28xx.ko Also, the preliminary tests, made on a single core 1.5 MHz Centrino showed that CPU usage reduced from 42%-75% to 28%-33% (reports from "top") command. A test with time command presented an even better result: This is the performance tests I did, running code_example to get 1,000 frames @29.995 Hz (about 35 seconds of stream), tested on a i386 machine, running at 1,5GHz: The old driver: $ time -f "%E: %Us User time, %Ss Kernel time, %P CPU used" ./capture_example 0:34.21: 8.22s User time, 25.16s Kernel time, 97% CPU used The videobuf-based driver: $ time -f "%E: %Us User time, %Ss Kernel time, %P CPU used" ./capture_example 0:35.36: 0.01s User time, 0.05s Kernel time, 0% CPU used Conclusion: The time consumption to receive the stream where reduced from about 33.38 seconds to 0.05 seconds. Signed-off-by: Mauro Carvalho Chehab <mchehab@infradead.org>
Diffstat (limited to 'drivers')
-rw-r--r--drivers/media/video/em28xx/em28xx-core.c417
-rw-r--r--drivers/media/video/em28xx/em28xx-video.c1198
-rw-r--r--drivers/media/video/em28xx/em28xx.h94
3 files changed, 811 insertions, 898 deletions
diff --git a/drivers/media/video/em28xx/em28xx-core.c b/drivers/media/video/em28xx/em28xx-core.c
index 9aa96f1713a1..95bc18d0e780 100644
--- a/drivers/media/video/em28xx/em28xx-core.c
+++ b/drivers/media/video/em28xx/em28xx-core.c
@@ -49,87 +49,10 @@ MODULE_PARM_DESC(reg_debug,"enable debug messages [URB reg]");
49 printk(KERN_INFO "%s %s :"fmt, \ 49 printk(KERN_INFO "%s %s :"fmt, \
50 dev->name, __func__ , ##arg); } while (0) 50 dev->name, __func__ , ##arg); } while (0)
51 51
52static unsigned int isoc_debug;
53module_param(isoc_debug,int,0644);
54MODULE_PARM_DESC(isoc_debug,"enable debug messages [isoc transfers]");
55
56#define em28xx_isocdbg(fmt, arg...) do {\
57 if (isoc_debug) \
58 printk(KERN_INFO "%s %s :"fmt, \
59 dev->name, __func__ , ##arg); } while (0)
60
61static int alt = EM28XX_PINOUT; 52static int alt = EM28XX_PINOUT;
62module_param(alt, int, 0644); 53module_param(alt, int, 0644);
63MODULE_PARM_DESC(alt, "alternate setting to use for video endpoint"); 54MODULE_PARM_DESC(alt, "alternate setting to use for video endpoint");
64 55
65
66/*
67 * em28xx_request_buffers()
68 * allocate a number of buffers
69 */
70u32 em28xx_request_buffers(struct em28xx *dev, u32 count)
71{
72 const size_t imagesize = PAGE_ALIGN(dev->frame_size); /*needs to be page aligned cause the buffers can be mapped individually! */
73 void *buff = NULL;
74 u32 i;
75 em28xx_coredbg("requested %i buffers with size %zi\n",
76 count, imagesize);
77 if (count > EM28XX_NUM_FRAMES)
78 count = EM28XX_NUM_FRAMES;
79
80 dev->num_frames = count;
81 while (dev->num_frames > 0) {
82 if ((buff = vmalloc_32(dev->num_frames * imagesize))) {
83 memset(buff, 0, dev->num_frames * imagesize);
84 break;
85 }
86 dev->num_frames--;
87 }
88
89 for (i = 0; i < dev->num_frames; i++) {
90 dev->frame[i].bufmem = buff + i * imagesize;
91 dev->frame[i].buf.index = i;
92 dev->frame[i].buf.m.offset = i * imagesize;
93 dev->frame[i].buf.length = dev->frame_size;
94 dev->frame[i].buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
95 dev->frame[i].buf.sequence = 0;
96 dev->frame[i].buf.field = V4L2_FIELD_NONE;
97 dev->frame[i].buf.memory = V4L2_MEMORY_MMAP;
98 dev->frame[i].buf.flags = 0;
99 }
100 return dev->num_frames;
101}
102
103/*
104 * em28xx_queue_unusedframes()
105 * add all frames that are not currently in use to the inbuffer queue
106 */
107void em28xx_queue_unusedframes(struct em28xx *dev)
108{
109 unsigned long lock_flags;
110 u32 i;
111
112 for (i = 0; i < dev->num_frames; i++)
113 if (dev->frame[i].state == F_UNUSED) {
114 dev->frame[i].state = F_QUEUED;
115 spin_lock_irqsave(&dev->queue_lock, lock_flags);
116 list_add_tail(&dev->frame[i].frame, &dev->inqueue);
117 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
118 }
119}
120
121/*
122 * em28xx_release_buffers()
123 * free frame buffers
124 */
125void em28xx_release_buffers(struct em28xx *dev)
126{
127 if (dev->num_frames) {
128 vfree(dev->frame[0].bufmem);
129 dev->num_frames = 0;
130 }
131}
132
133/* 56/*
134 * em28xx_read_reg_req() 57 * em28xx_read_reg_req()
135 * reads data from the usb device specifying bRequest 58 * reads data from the usb device specifying bRequest
@@ -469,346 +392,6 @@ int em28xx_resolution_set(struct em28xx *dev)
469 return em28xx_scaler_set(dev, dev->hscale, dev->vscale); 392 return em28xx_scaler_set(dev, dev->hscale, dev->vscale);
470} 393}
471 394
472
473/******************* isoc transfer handling ****************************/
474
475#ifdef ENABLE_DEBUG_ISOC_FRAMES
476static void em28xx_isoc_dump(struct urb *urb)
477{
478 int len = 0;
479 int ntrans = 0;
480 int i;
481
482 printk(KERN_DEBUG "isocIrq: sf=%d np=%d ec=%x\n",
483 urb->start_frame, urb->number_of_packets,
484 urb->error_count);
485 for (i = 0; i < urb->number_of_packets; i++) {
486 unsigned char *buf =
487 urb->transfer_buffer +
488 urb->iso_frame_desc[i].offset;
489 int alen = urb->iso_frame_desc[i].actual_length;
490 if (alen > 0) {
491 if (buf[0] == 0x88) {
492 ntrans++;
493 len += alen;
494 } else if (buf[0] == 0x22) {
495 printk(KERN_DEBUG
496 "= l=%d nt=%d bpp=%d\n",
497 len - 4 * ntrans, ntrans,
498 ntrans == 0 ? 0 : len / ntrans);
499 ntrans = 1;
500 len = alen;
501 } else
502 printk(KERN_DEBUG "!\n");
503 }
504 printk(KERN_DEBUG " n=%d s=%d al=%d %x\n", i,
505 urb->iso_frame_desc[i].status,
506 urb->iso_frame_desc[i].actual_length,
507 (unsigned int)
508 *((unsigned char *)(urb->transfer_buffer +
509 urb->iso_frame_desc[i].
510 offset)));
511 }
512}
513#endif
514
515static inline int em28xx_isoc_video(struct em28xx *dev,struct em28xx_frame_t **f,
516 unsigned long *lock_flags, unsigned char buf)
517{
518 if (!(buf & 0x01)) {
519 if ((*f)->state == F_GRABBING) {
520 /*previous frame is incomplete */
521 if ((*f)->fieldbytesused < dev->field_size) {
522 (*f)->state = F_ERROR;
523 em28xx_isocdbg ("dropping incomplete bottom field (%i missing bytes)",
524 dev->field_size-(*f)->fieldbytesused);
525 } else {
526 (*f)->state = F_DONE;
527 (*f)->buf.bytesused = dev->frame_size;
528 }
529 }
530 if ((*f)->state == F_DONE || (*f)->state == F_ERROR) {
531 /* move current frame to outqueue and get next free buffer from inqueue */
532 spin_lock_irqsave(&dev-> queue_lock, *lock_flags);
533 list_move_tail(&(*f)->frame, &dev->outqueue);
534 if (!list_empty(&dev->inqueue))
535 (*f) = list_entry(dev-> inqueue.next,
536 struct em28xx_frame_t,frame);
537 else
538 (*f) = NULL;
539 spin_unlock_irqrestore(&dev->queue_lock,*lock_flags);
540 }
541 if (!(*f)) {
542 em28xx_isocdbg ("new frame but no buffer is free");
543 return -1;
544 }
545 do_gettimeofday(&(*f)->buf.timestamp);
546 (*f)->buf.sequence = ++dev->frame_count;
547 (*f)->buf.field = V4L2_FIELD_INTERLACED;
548 (*f)->state = F_GRABBING;
549 (*f)->buf.bytesused = 0;
550 (*f)->top_field = 1;
551 (*f)->fieldbytesused = 0;
552 } else {
553 /* acquiring bottom field */
554 if ((*f)->state == F_GRABBING) {
555 if (!(*f)->top_field) {
556 (*f)->state = F_ERROR;
557 em28xx_isocdbg ("unexpected begin of bottom field; discarding it");
558 } else if ((*f)-> fieldbytesused < dev->field_size - 172) {
559 (*f)->state = F_ERROR;
560 em28xx_isocdbg ("dropping incomplete top field (%i missing bytes)",
561 dev->field_size-(*f)->fieldbytesused);
562 } else {
563 (*f)->top_field = 0;
564 (*f)->fieldbytesused = 0;
565 }
566 }
567 }
568 return (0);
569}
570
571static inline void em28xx_isoc_video_copy(struct em28xx *dev,
572 struct em28xx_frame_t **f, unsigned char *buf, int len)
573{
574 void *fieldstart, *startwrite, *startread;
575 int linesdone, currlinedone, offset, lencopy,remain;
576
577 if(dev->frame_size != (*f)->buf.length){
578 em28xx_err("frame_size %i and buf.length %i are different!!!\n",dev->frame_size,(*f)->buf.length);
579 return;
580 }
581
582 if ((*f)->fieldbytesused + len > dev->field_size)
583 len =dev->field_size - (*f)->fieldbytesused;
584
585 if (buf[0] != 0x88 && buf[0] != 0x22) {
586 em28xx_isocdbg("frame is not complete\n");
587 startread = buf;
588 len+=4;
589 } else
590 startread = buf + 4;
591
592 remain = len;
593
594 if ((*f)->top_field)
595 fieldstart = (*f)->bufmem;
596 else
597 fieldstart = (*f)->bufmem + dev->bytesperline;
598
599 linesdone = (*f)->fieldbytesused / dev->bytesperline;
600 currlinedone = (*f)->fieldbytesused % dev->bytesperline;
601 offset = linesdone * dev->bytesperline * 2 + currlinedone;
602 startwrite = fieldstart + offset;
603 lencopy = dev->bytesperline - currlinedone;
604 lencopy = lencopy > remain ? remain : lencopy;
605
606 memcpy(startwrite, startread, lencopy);
607 remain -= lencopy;
608
609 while (remain > 0) {
610 startwrite += lencopy + dev->bytesperline;
611 startread += lencopy;
612 if (dev->bytesperline > remain)
613 lencopy = remain;
614 else
615 lencopy = dev->bytesperline;
616
617 memcpy(startwrite, startread, lencopy);
618 remain -= lencopy;
619 }
620
621 (*f)->fieldbytesused += len;
622}
623
624/*
625 * em28xx_isoIrq()
626 * handles the incoming isoc urbs and fills the frames from our inqueue
627 */
628static void em28xx_isocIrq(struct urb *urb)
629{
630 struct em28xx *dev = urb->context;
631 int i, status;
632 struct em28xx_frame_t **f;
633 unsigned long lock_flags;
634
635 if (!dev)
636 return;
637#ifdef ENABLE_DEBUG_ISOC_FRAMES
638 if (isoc_debug>1)
639 em28xx_isoc_dump(urb);
640#endif
641
642 if (urb->status == -ENOENT)
643 return;
644
645 f = &dev->frame_current;
646
647 if (dev->stream == STREAM_INTERRUPT) {
648 dev->stream = STREAM_OFF;
649 if ((*f))
650 (*f)->state = F_QUEUED;
651 em28xx_isocdbg("stream interrupted");
652 wake_up_interruptible(&dev->wait_stream);
653 }
654
655 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
656 return;
657
658 if (dev->stream == STREAM_ON && !list_empty(&dev->inqueue)) {
659 if (!(*f))
660 (*f) = list_entry(dev->inqueue.next,
661 struct em28xx_frame_t, frame);
662
663 for (i = 0; i < urb->number_of_packets; i++) {
664 unsigned char *buf = urb->transfer_buffer +
665 urb->iso_frame_desc[i].offset;
666 int len = urb->iso_frame_desc[i].actual_length - 4;
667
668 if (urb->iso_frame_desc[i].status) {
669 em28xx_isocdbg("data error: [%d] len=%d, status=%d", i,
670 urb->iso_frame_desc[i].actual_length,
671 urb->iso_frame_desc[i].status);
672 if (urb->iso_frame_desc[i].status != -EPROTO)
673 continue;
674 }
675 if (urb->iso_frame_desc[i].actual_length <= 0) {
676 em28xx_isocdbg("packet %d is empty",i);
677 continue;
678 }
679 if (urb->iso_frame_desc[i].actual_length >
680 urb->iso_frame_desc[i].length) {
681 em28xx_isocdbg("packet bigger than packet size");
682 continue;
683 }
684 /*new frame */
685 if (buf[0] == 0x22 && buf[1] == 0x5a) {
686 em28xx_isocdbg("Video frame, length=%i!",len);
687
688 if (em28xx_isoc_video(dev,f,&lock_flags,buf[2]))
689 break;
690 } else if (buf[0]==0x33 && buf[1]==0x95 && buf[2]==0x00) {
691 em28xx_isocdbg("VBI HEADER!!!");
692 }
693
694 /* actual copying */
695 if ((*f)->state == F_GRABBING) {
696 em28xx_isoc_video_copy(dev,f,buf, len);
697 }
698 }
699 }
700
701 for (i = 0; i < urb->number_of_packets; i++) {
702 urb->iso_frame_desc[i].status = 0;
703 urb->iso_frame_desc[i].actual_length = 0;
704 }
705
706 urb->status = 0;
707 if ((status = usb_submit_urb(urb, GFP_ATOMIC))) {
708 em28xx_errdev("resubmit of urb failed (error=%i)\n", status);
709 dev->state |= DEV_MISCONFIGURED;
710 }
711 wake_up_interruptible(&dev->wait_frame);
712 return;
713}
714
715/*
716 * em28xx_uninit_isoc()
717 * deallocates the buffers and urbs allocated during em28xx_init_iosc()
718 */
719void em28xx_uninit_isoc(struct em28xx *dev)
720{
721 int i;
722
723 for (i = 0; i < EM28XX_NUM_BUFS; i++) {
724 if (dev->urb[i]) {
725 usb_kill_urb(dev->urb[i]);
726 if (dev->transfer_buffer[i]) {
727 usb_buffer_free(dev->udev,
728 dev->urb[i]->transfer_buffer_length,
729 dev->transfer_buffer[i],
730 dev->urb[i]->transfer_dma);
731 }
732 usb_free_urb(dev->urb[i]);
733 }
734 dev->urb[i] = NULL;
735 dev->transfer_buffer[i] = NULL;
736 }
737 em28xx_capture_start(dev, 0);
738}
739
740/*
741 * em28xx_init_isoc()
742 * allocates transfer buffers and submits the urbs for isoc transfer
743 */
744int em28xx_init_isoc(struct em28xx *dev)
745{
746 /* change interface to 3 which allows the biggest packet sizes */
747 int i, errCode;
748 int sb_size;
749
750 em28xx_set_alternate(dev);
751 sb_size = EM28XX_NUM_PACKETS * dev->max_pkt_size;
752
753 /* reset streaming vars */
754 dev->frame_current = NULL;
755 dev->frame_count = 0;
756
757 /* allocate urbs */
758 for (i = 0; i < EM28XX_NUM_BUFS; i++) {
759 struct urb *urb;
760 int j;
761 /* allocate transfer buffer */
762 urb = usb_alloc_urb(EM28XX_NUM_PACKETS, GFP_KERNEL);
763 if (!urb){
764 em28xx_errdev("cannot alloc urb %i\n", i);
765 em28xx_uninit_isoc(dev);
766 return -ENOMEM;
767 }
768 dev->transfer_buffer[i] = usb_buffer_alloc(dev->udev, sb_size,
769 GFP_KERNEL,
770 &urb->transfer_dma);
771 if (!dev->transfer_buffer[i]) {
772 em28xx_errdev
773 ("unable to allocate %i bytes for transfer buffer %i\n",
774 sb_size, i);
775 em28xx_uninit_isoc(dev);
776 usb_free_urb(urb);
777 return -ENOMEM;
778 }
779 memset(dev->transfer_buffer[i], 0, sb_size);
780 urb->dev = dev->udev;
781 urb->context = dev;
782 urb->pipe = usb_rcvisocpipe(dev->udev, 0x82);
783 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
784 urb->interval = 1;
785 urb->transfer_buffer = dev->transfer_buffer[i];
786 urb->complete = em28xx_isocIrq;
787 urb->number_of_packets = EM28XX_NUM_PACKETS;
788 urb->transfer_buffer_length = sb_size;
789 for (j = 0; j < EM28XX_NUM_PACKETS; j++) {
790 urb->iso_frame_desc[j].offset = j * dev->max_pkt_size;
791 urb->iso_frame_desc[j].length = dev->max_pkt_size;
792 }
793 dev->urb[i] = urb;
794 }
795
796 /* submit urbs */
797 em28xx_coredbg("Submitting %d urbs of %d packets (%d each)\n",
798 EM28XX_NUM_BUFS, EM28XX_NUM_PACKETS, dev->max_pkt_size);
799 for (i = 0; i < EM28XX_NUM_BUFS; i++) {
800 errCode = usb_submit_urb(dev->urb[i], GFP_KERNEL);
801 if (errCode) {
802 em28xx_errdev("submit of urb %i failed (error=%i)\n", i,
803 errCode);
804 em28xx_uninit_isoc(dev);
805 return errCode;
806 }
807 }
808
809 return 0;
810}
811
812int em28xx_set_alternate(struct em28xx *dev) 395int em28xx_set_alternate(struct em28xx *dev)
813{ 396{
814 int errCode, prev_alt = dev->alt; 397 int errCode, prev_alt = dev->alt;
diff --git a/drivers/media/video/em28xx/em28xx-video.c b/drivers/media/video/em28xx/em28xx-video.c
index c7f074c6f226..182058ac8432 100644
--- a/drivers/media/video/em28xx/em28xx-video.c
+++ b/drivers/media/video/em28xx/em28xx-video.c
@@ -54,6 +54,21 @@
54 printk(KERN_INFO "%s %s :"fmt, \ 54 printk(KERN_INFO "%s %s :"fmt, \
55 dev->name, __func__ , ##arg); } while (0) 55 dev->name, __func__ , ##arg); } while (0)
56 56
57static unsigned int isoc_debug;
58module_param(isoc_debug,int,0644);
59MODULE_PARM_DESC(isoc_debug,"enable debug messages [isoc transfers]");
60
61#define em28xx_isocdbg(fmt, arg...) do {\
62 if (isoc_debug) \
63 printk(KERN_INFO "%s %s :"fmt, \
64 dev->name, __FUNCTION__ , ##arg); } while (0)
65
66#define BUFFER_TIMEOUT msecs_to_jiffies(2000) /* 2 seconds */
67
68/* Limits minimum and default number of buffers */
69#define EM28XX_MIN_BUF 4
70#define EM28XX_DEF_BUF 8
71
57MODULE_AUTHOR(DRIVER_AUTHOR); 72MODULE_AUTHOR(DRIVER_AUTHOR);
58MODULE_DESCRIPTION(DRIVER_DESC); 73MODULE_DESCRIPTION(DRIVER_DESC);
59MODULE_LICENSE("GPL"); 74MODULE_LICENSE("GPL");
@@ -107,6 +122,665 @@ static struct v4l2_queryctrl em28xx_qctrl[] = {
107 122
108static struct usb_driver em28xx_usb_driver; 123static struct usb_driver em28xx_usb_driver;
109 124
125/* ------------------------------------------------------------------
126 DMA and thread functions
127 ------------------------------------------------------------------*/
128
129/*
130 * Announces that a buffer were filled and request the next
131 */
132static void inline buffer_filled (struct em28xx *dev,
133 struct em28xx_dmaqueue *dma_q,
134 struct em28xx_buffer *buf)
135{
136 /* Nobody is waiting something to be done, just return */
137 if (!waitqueue_active(&buf->vb.done)) {
138 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
139
140 printk(KERN_ERR "em28xx: buffer underrun at %ld\n",
141 jiffies);
142
143 return;
144 }
145
146 /* Advice that buffer was filled */
147 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
148 buf->vb.state = VIDEOBUF_DONE;
149 buf->vb.field_count++;
150 do_gettimeofday(&buf->vb.ts);
151
152 list_del(&buf->vb.queue);
153 wake_up(&buf->vb.done);
154}
155
156/*
157 * Identify the buffer header type and properly handles
158 */
159static void em28xx_copy_video(struct em28xx *dev,
160 struct em28xx_dmaqueue *dma_q,
161 struct em28xx_buffer *buf,
162 unsigned char *p,
163 unsigned char *outp, unsigned long len)
164{
165 void *fieldstart, *startwrite, *startread;
166 int linesdone, currlinedone, offset, lencopy,remain;
167
168 if(dev->frame_size != buf->vb.size){
169 em28xx_errdev("size %i and buf.length %lu are different!\n",
170 dev->frame_size, buf->vb.size);
171 return;
172 }
173
174 if (dma_q->pos + len > buf->vb.size)
175 len = buf->vb.size - dma_q->pos;
176
177 if (outp[0] != 0x88 && outp[0] != 0x22) {
178 em28xx_isocdbg("frame is not complete\n");
179 len += 4;
180 } else
181 p +=4;
182
183 startread = p;
184 remain = len;
185
186 /* Interlaces frame */
187 if (buf->top_field)
188 fieldstart = outp;
189 else
190 fieldstart = outp + dev->bytesperline;
191
192 linesdone = dma_q->pos / dev->bytesperline;
193 currlinedone = dma_q->pos % dev->bytesperline;
194 offset = linesdone * dev->bytesperline * 2 + currlinedone;
195 startwrite = fieldstart + offset;
196 lencopy = dev->bytesperline - currlinedone;
197 lencopy = lencopy > remain ? remain : lencopy;
198
199 if (__copy_to_user(startwrite, startread, lencopy) != 0)
200 em28xx_errdev("copy_to_user failed.\n");
201
202 remain -= lencopy;
203
204 while (remain > 0) {
205 startwrite += lencopy + dev->bytesperline;
206 startread += lencopy;
207 if (dev->bytesperline > remain)
208 lencopy = remain;
209 else
210 lencopy = dev->bytesperline;
211
212 if (__copy_to_user(startwrite, startread, lencopy) != 0)
213 em28xx_errdev("copy_to_user failed.\n");
214
215 remain -= lencopy;
216 }
217
218 dma_q->pos += len;
219}
220
221static void inline print_err_status (struct em28xx *dev,
222 int packet, int status)
223{
224 char *errmsg = "Unknown";
225
226 switch(status) {
227 case -ENOENT:
228 errmsg = "unlinked synchronuously";
229 break;
230 case -ECONNRESET:
231 errmsg = "unlinked asynchronuously";
232 break;
233 case -ENOSR:
234 errmsg = "Buffer error (overrun)";
235 break;
236 case -EPIPE:
237 errmsg = "Stalled (device not responding)";
238 break;
239 case -EOVERFLOW:
240 errmsg = "Babble (bad cable?)";
241 break;
242 case -EPROTO:
243 errmsg = "Bit-stuff error (bad cable?)";
244 break;
245 case -EILSEQ:
246 errmsg = "CRC/Timeout (could be anything)";
247 break;
248 case -ETIME:
249 errmsg = "Device does not respond";
250 break;
251 }
252 if (packet<0) {
253 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
254 } else {
255 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
256 packet, status, errmsg);
257 }
258}
259
260/*
261 * video-buf generic routine to get the next available buffer
262 */
263static int inline get_next_buf (struct em28xx_dmaqueue *dma_q,
264 struct em28xx_buffer **buf)
265{
266 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
267
268 if (list_empty(&dma_q->active)) {
269 em28xx_isocdbg("No active queue to serve\n");
270 return 0;
271 }
272
273 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
274
275 return 1;
276}
277
278/*
279 * Controls the isoc copy of each urb packet
280 */
281static inline int em28xx_isoc_copy(struct urb *urb, struct em28xx_buffer **buf)
282{
283 struct em28xx_dmaqueue *dma_q = urb->context;
284 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
285 unsigned char *outp = videobuf_to_vmalloc (&(*buf)->vb);
286 int i, len = 0, rc = 1;
287 char *p;
288
289 if (!dev)
290 return 0;
291
292 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
293 return 0;
294
295 if (urb->status<0) {
296 print_err_status (dev,-1,urb->status);
297 if (urb->status == -ENOENT)
298 return 0;
299 }
300
301 for (i = 0; i < urb->number_of_packets; i++) {
302 int status = urb->iso_frame_desc[i].status;
303
304 if (status<0) {
305 print_err_status (dev,i,status);
306 if (urb->iso_frame_desc[i].status != -EPROTO)
307 continue;
308 }
309
310 len=urb->iso_frame_desc[i].actual_length - 4;
311
312 if (urb->iso_frame_desc[i].actual_length <= 0) {
313 em28xx_isocdbg("packet %d is empty",i);
314 continue;
315 }
316 if (urb->iso_frame_desc[i].actual_length >
317 dev->max_pkt_size) {
318 em28xx_isocdbg("packet bigger than packet size");
319 continue;
320 }
321
322 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
323 if (urb->iso_frame_desc[i].status != -EPROTO)
324 continue;
325
326 /* FIXME: incomplete buffer checks where removed to make
327 logic simpler. Impacts of those changes should be evaluated
328 */
329 if (outp[0] == 0x22 && outp[1] == 0x5a) {
330 em28xx_isocdbg("Video frame, length=%i, %s", len,
331 (outp[2] == 1)? "top" : "botton");
332
333 if (outp[2] == 1) {
334 if ((*buf)->receiving)
335 buffer_filled (dev, dma_q, *buf);
336
337 (*buf)->top_field = 1;
338 } else {
339 (*buf)->top_field = 0;
340 }
341 (*buf)->receiving = 1;
342 dma_q->pos = 0;
343 } else if (outp[0]==0x33 && outp[1]==0x95 && outp[2]==0x00) {
344 em28xx_isocdbg("VBI HEADER!!!");
345 }
346
347 em28xx_copy_video(dev, dma_q, *buf, p, outp, len);
348
349 /* FIXME: Should add vbi copy */
350 }
351 return rc;
352}
353
354/* ------------------------------------------------------------------
355 URB control
356 ------------------------------------------------------------------*/
357
358/*
359 * IRQ callback, called by URB callback
360 */
361static void em28xx_irq_callback(struct urb *urb)
362{
363 struct em28xx_buffer *buf;
364 struct em28xx_dmaqueue *dma_q = urb->context;
365 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
366 int rc,i;
367 unsigned long flags;
368
369 spin_lock_irqsave(&dev->slock,flags);
370
371 buf=dev->isoc_ctl.buf;
372
373 if (!buf) {
374 rc=get_next_buf (dma_q, &buf);
375 if (rc<=0)
376 goto ret;
377 }
378
379 /* Copy data from URB */
380 rc=em28xx_isoc_copy(urb, &buf);
381
382 dev->isoc_ctl.buf=buf;
383ret:
384 /* Reset urb buffers */
385 for (i = 0; i < urb->number_of_packets; i++) {
386 urb->iso_frame_desc[i].status = 0;
387 urb->iso_frame_desc[i].actual_length = 0;
388 }
389 urb->status = 0;
390
391 if ((urb->status = usb_submit_urb(urb, GFP_ATOMIC))) {
392 em28xx_err("urb resubmit failed (error=%i)\n",
393 urb->status);
394 }
395
396 if (rc >= 0)
397 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
398 spin_unlock_irqrestore(&dev->slock,flags);
399}
400
401/*
402 * Stop and Deallocate URBs
403 */
404static void em28xx_uninit_isoc(struct em28xx *dev)
405{
406 struct urb *urb;
407 int i;
408
409 dev->isoc_ctl.nfields=-1;
410 dev->isoc_ctl.buf=NULL;
411 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
412 urb=dev->isoc_ctl.urb[i];
413 if (urb) {
414 usb_kill_urb(urb);
415 usb_unlink_urb(urb);
416 if (dev->isoc_ctl.transfer_buffer[i]) {
417 usb_buffer_free(dev->udev,
418 urb->transfer_buffer_length,
419 dev->isoc_ctl.transfer_buffer[i],
420 urb->transfer_dma);
421 }
422 usb_free_urb(urb);
423 dev->isoc_ctl.urb[i] = NULL;
424 }
425 dev->isoc_ctl.transfer_buffer[i] = NULL;
426 }
427
428 kfree (dev->isoc_ctl.urb);
429 kfree (dev->isoc_ctl.transfer_buffer);
430 dev->isoc_ctl.urb=NULL;
431 dev->isoc_ctl.transfer_buffer=NULL;
432
433 dev->isoc_ctl.num_bufs=0;
434
435 em28xx_capture_start(dev, 0);
436}
437
438/*
439 * Stop video thread - FIXME: Can be easily removed
440 */
441static void em28xx_stop_thread(struct em28xx_dmaqueue *dma_q)
442{
443 struct em28xx *dev= container_of(dma_q, struct em28xx, vidq);
444
445 em28xx_uninit_isoc(dev);
446}
447
448/*
449 * Allocate URBs and start IRQ
450 */
451static int em28xx_prepare_isoc(struct em28xx *dev, int max_packets,
452 int num_bufs)
453{
454 struct em28xx_dmaqueue *dma_q = &dev->vidq;
455 int i;
456 int sb_size, pipe;
457 struct urb *urb;
458 int j, k;
459
460 /* De-allocates all pending stuff */
461 em28xx_uninit_isoc(dev);
462
463 dev->isoc_ctl.num_bufs = num_bufs;
464
465 dev->isoc_ctl.urb = kmalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
466 if (!dev->isoc_ctl.urb) {
467 em28xx_errdev("cannot alloc memory for usb buffers\n");
468 return -ENOMEM;
469 }
470
471 dev->isoc_ctl.transfer_buffer = kmalloc(sizeof(void *)*num_bufs,
472 GFP_KERNEL);
473 if (!dev->isoc_ctl.urb) {
474 em28xx_errdev("cannot allocate memory for usbtransfer\n");
475 kfree(dev->isoc_ctl.urb);
476 return -ENOMEM;
477 }
478
479 dev->isoc_ctl.max_pkt_size = dev->max_pkt_size;
480
481 sb_size = max_packets * dev->isoc_ctl.max_pkt_size;
482
483 /* allocate urbs and transfer buffers */
484 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
485 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
486 if (!urb) {
487 em28xx_err("cannot alloc isoc_ctl.urb %i\n", i);
488 em28xx_uninit_isoc(dev);
489 usb_free_urb(urb);
490 return -ENOMEM;
491 }
492 dev->isoc_ctl.urb[i] = urb;
493
494 dev->isoc_ctl.transfer_buffer[i] = usb_buffer_alloc(dev->udev,
495 sb_size, GFP_KERNEL, &urb->transfer_dma);
496 if (!dev->isoc_ctl.transfer_buffer[i]) {
497 em28xx_err ("unable to allocate %i bytes for transfer"
498 " buffer %i%s\n",
499 sb_size, i,
500 in_interrupt()?" while in int":"");
501 em28xx_uninit_isoc(dev);
502 return -ENOMEM;
503 }
504 memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
505
506 /* FIXME: this is a hack - should be
507 'desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK'
508 should also be using 'desc.bInterval'
509 */
510 pipe=usb_rcvisocpipe(dev->udev, 0x82);
511 usb_fill_int_urb(urb, dev->udev, pipe,
512 dev->isoc_ctl.transfer_buffer[i], sb_size,
513 em28xx_irq_callback, dma_q, 1);
514
515 urb->number_of_packets = max_packets;
516 urb->transfer_flags = URB_ISO_ASAP;
517
518 k = 0;
519 for (j = 0; j < max_packets; j++) {
520 urb->iso_frame_desc[j].offset = k;
521 urb->iso_frame_desc[j].length =
522 dev->isoc_ctl.max_pkt_size;
523 k += dev->isoc_ctl.max_pkt_size;
524 }
525 }
526
527 return 0;
528}
529
530static int em28xx_start_thread( struct em28xx_dmaqueue *dma_q,
531 struct em28xx_buffer *buf)
532{
533 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
534 int i,rc;
535
536 init_waitqueue_head(&dma_q->wq);
537
538 em28xx_capture_start(dev, 1);
539
540 /* submit urbs and enables IRQ */
541 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
542 rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
543 if (rc) {
544 em28xx_err("submit of urb %i failed (error=%i)\n", i,
545 rc);
546 em28xx_uninit_isoc(dev);
547 return rc;
548 }
549 }
550
551 if (rc<0)
552 return rc;
553
554 return 0;
555}
556
557static int restart_video_queue(struct em28xx_dmaqueue *dma_q)
558{
559 struct em28xx *dev= container_of(dma_q,struct em28xx,vidq);
560
561 struct em28xx_buffer *buf, *prev;
562 struct list_head *item;
563
564 em28xx_videodbg("%s dma_q=0x%08lx\n",
565 __FUNCTION__,(unsigned long)dma_q);
566
567 if (!list_empty(&dma_q->active)) {
568 buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
569 em28xx_videodbg("restart_queue [%p/%d]: restart dma\n",
570 buf, buf->vb.i);
571 em28xx_stop_thread(dma_q);
572 em28xx_start_thread(dma_q, buf);
573
574 /* cancel all outstanding capture / vbi requests */
575 list_for_each(item,&dma_q->active) {
576 buf = list_entry(item, struct em28xx_buffer, vb.queue);
577
578 list_del(&buf->vb.queue);
579 buf->vb.state = VIDEOBUF_ERROR;
580 wake_up(&buf->vb.done);
581 }
582 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
583
584 return 0;
585 }
586
587 prev = NULL;
588 for (;;) {
589 if (list_empty(&dma_q->queued))
590 return 0;
591 buf = list_entry(dma_q->queued.next, struct em28xx_buffer, vb.queue);
592 if (NULL == prev) {
593 list_del(&buf->vb.queue);
594 list_add_tail(&buf->vb.queue,&dma_q->active);
595
596 em28xx_videodbg("Restarting video dma\n");
597 em28xx_stop_thread(dma_q);
598 em28xx_start_thread(dma_q, buf);
599
600 buf->vb.state = VIDEOBUF_ACTIVE;
601 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
602 em28xx_videodbg("[%p/%d] restart_queue -"
603 " first active\n", buf, buf->vb.i);
604
605 } else if (prev->vb.width == buf->vb.width &&
606 prev->vb.height == buf->vb.height &&
607 prev->fmt == buf->fmt) {
608 list_del(&buf->vb.queue);
609 list_add_tail(&buf->vb.queue,&dma_q->active);
610 buf->vb.state = VIDEOBUF_ACTIVE;
611 em28xx_videodbg("[%p/%d] restart_queue -"
612 " move to active\n",buf,buf->vb.i);
613 } else {
614 return 0;
615 }
616 prev = buf;
617 }
618}
619
620static void em28xx_vid_timeout(unsigned long data)
621{
622 struct em28xx *dev = (struct em28xx*)data;
623 struct em28xx_dmaqueue *vidq = &dev->vidq;
624 struct em28xx_buffer *buf;
625 unsigned long flags;
626
627 spin_lock_irqsave(&dev->slock,flags);
628 while (!list_empty(&vidq->active)) {
629 buf = list_entry(vidq->active.next, struct em28xx_buffer,
630 vb.queue);
631 list_del(&buf->vb.queue);
632 buf->vb.state = VIDEOBUF_ERROR;
633 wake_up(&buf->vb.done);
634 em28xx_videodbg("em28xx/0: [%p/%d] timeout\n",
635 buf, buf->vb.i);
636 }
637
638 restart_video_queue(vidq);
639 spin_unlock_irqrestore(&dev->slock,flags);
640}
641
642/* ------------------------------------------------------------------
643 Videobuf operations
644 ------------------------------------------------------------------*/
645
646static int
647buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
648{
649 struct em28xx_fh *fh = vq->priv_data;
650
651 *size = 16 * fh->width * fh->height >> 3;
652 if (0 == *count)
653 *count = EM28XX_DEF_BUF;
654
655 if (*count < EM28XX_MIN_BUF) {
656 *count=EM28XX_MIN_BUF;
657 }
658
659 return 0;
660}
661
662static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
663{
664 if (in_interrupt())
665 BUG();
666
667 videobuf_waiton(&buf->vb, 0, 0);
668 videobuf_vmalloc_free(&buf->vb);
669 buf->vb.state = VIDEOBUF_NEEDS_INIT;
670}
671
672static int
673buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
674 enum v4l2_field field)
675{
676 struct em28xx_fh *fh = vq->priv_data;
677 struct em28xx_buffer *buf = container_of(vb,struct em28xx_buffer,vb);
678 struct em28xx *dev = fh->dev;
679 int rc = 0, urb_init = 0;
680 const int urbsize = EM28XX_NUM_PACKETS * dev->max_pkt_size;
681
682 BUG_ON(NULL == fh->fmt);
683
684 /* FIXME: It assumes depth = 16 */
685 /* The only currently supported format is 16 bits/pixel */
686 buf->vb.size = 16 * fh->width * fh->height >> 3;
687
688 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
689 return -EINVAL;
690
691 if (buf->fmt != fh->fmt ||
692 buf->vb.width != fh->width ||
693 buf->vb.height != fh->height ||
694 buf->vb.field != field) {
695 buf->fmt = fh->fmt;
696 buf->vb.width = fh->width;
697 buf->vb.height = fh->height;
698 buf->vb.field = field;
699 buf->vb.state = VIDEOBUF_NEEDS_INIT;
700 }
701
702 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
703 if (0 != (rc = videobuf_iolock(vq,&buf->vb,NULL)))
704 goto fail;
705 urb_init=1;
706 }
707
708
709 if (!dev->isoc_ctl.num_bufs)
710 urb_init=1;
711
712 if (urb_init) {
713 rc = em28xx_prepare_isoc(dev, urbsize, EM28XX_NUM_BUFS);
714 if (rc<0)
715 goto fail;
716 }
717
718 buf->vb.state = VIDEOBUF_PREPARED;
719 return 0;
720
721fail:
722 free_buffer(vq,buf);
723 return rc;
724}
725
726static void
727buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
728{
729 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
730 struct em28xx_fh *fh = vq->priv_data;
731 struct em28xx *dev = fh->dev;
732 struct em28xx_dmaqueue *vidq = &dev->vidq;
733 struct em28xx_buffer *prev;
734
735 if (!list_empty(&vidq->queued)) {
736 list_add_tail(&buf->vb.queue,&vidq->queued);
737 buf->vb.state = VIDEOBUF_QUEUED;
738 em28xx_videodbg("[%p/%d] buffer_queue - append to queued\n",
739 buf, buf->vb.i);
740 } else if (list_empty(&vidq->active)) {
741 list_add_tail(&buf->vb.queue,&vidq->active);
742 buf->vb.state = VIDEOBUF_ACTIVE;
743 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
744 em28xx_videodbg("[%p/%d] buffer_queue - first active\n",
745 buf, buf->vb.i);
746 em28xx_start_thread(vidq, buf);
747 } else {
748 prev = list_entry(vidq->active.prev, struct em28xx_buffer,
749 vb.queue);
750 if (prev->vb.width == buf->vb.width &&
751 prev->vb.height == buf->vb.height &&
752 prev->fmt == buf->fmt) {
753 list_add_tail(&buf->vb.queue,&vidq->active);
754 buf->vb.state = VIDEOBUF_ACTIVE;
755 em28xx_videodbg("[%p/%d] buffer_queue -"
756 " append to active\n", buf, buf->vb.i);
757 } else {
758 list_add_tail(&buf->vb.queue,&vidq->queued);
759 buf->vb.state = VIDEOBUF_QUEUED;
760 em28xx_videodbg("[%p/%d] buffer_queue - first queued\n",
761 buf, buf->vb.i);
762 }
763 }
764}
765
766static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
767{
768 struct em28xx_buffer *buf = container_of(vb,struct em28xx_buffer,vb);
769 struct em28xx_fh *fh = vq->priv_data;
770 struct em28xx *dev = (struct em28xx*)fh->dev;
771 struct em28xx_dmaqueue *vidq = &dev->vidq;
772
773 em28xx_stop_thread(vidq);
774
775 free_buffer(vq,buf);
776}
777
778static struct videobuf_queue_ops em28xx_video_qops = {
779 .buf_setup = buffer_setup,
780 .buf_prepare = buffer_prepare,
781 .buf_queue = buffer_queue,
782 .buf_release = buffer_release,
783};
110 784
111/********************* v4l2 interface ******************************************/ 785/********************* v4l2 interface ******************************************/
112 786
@@ -152,23 +826,6 @@ static void em28xx_config_i2c(struct em28xx *dev)
152 em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL); 826 em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL);
153} 827}
154 828
155/*
156 * em28xx_empty_framequeues()
157 * prepare queues for incoming and outgoing frames
158 */
159static void em28xx_empty_framequeues(struct em28xx *dev)
160{
161 u32 i;
162
163 INIT_LIST_HEAD(&dev->inqueue);
164 INIT_LIST_HEAD(&dev->outqueue);
165
166 for (i = 0; i < EM28XX_NUM_FRAMES; i++) {
167 dev->frame[i].state = F_UNUSED;
168 dev->frame[i].buf.bytesused = 0;
169 }
170}
171
172static void video_mux(struct em28xx *dev, int index) 829static void video_mux(struct em28xx *dev, int index)
173{ 830{
174 struct v4l2_routing route; 831 struct v4l2_routing route;
@@ -231,33 +888,6 @@ static void res_free(struct em28xx_fh *fh)
231} 888}
232 889
233/* 890/*
234 * em28xx_vm_open()
235 */
236static void em28xx_vm_open(struct vm_area_struct *vma)
237{
238 struct em28xx_frame_t *f = vma->vm_private_data;
239 f->vma_use_count++;
240}
241
242/*
243 * em28xx_vm_close()
244 */
245static void em28xx_vm_close(struct vm_area_struct *vma)
246{
247 /* NOTE: buffers are not freed here */
248 struct em28xx_frame_t *f = vma->vm_private_data;
249
250 if (f->vma_use_count)
251 f->vma_use_count--;
252}
253
254static struct vm_operations_struct em28xx_vm_ops = {
255 .open = em28xx_vm_open,
256 .close = em28xx_vm_close,
257};
258
259
260/*
261 * em28xx_get_ctrl() 891 * em28xx_get_ctrl()
262 * return the current saturation, brightness or contrast, mute state 892 * return the current saturation, brightness or contrast, mute state
263 */ 893 */
@@ -296,34 +926,6 @@ static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
296 } 926 }
297} 927}
298 928
299/*
300 * em28xx_stream_interrupt()
301 * stops streaming
302 */
303static int em28xx_stream_interrupt(struct em28xx *dev)
304{
305 int rc = 0;
306
307 /* stop reading from the device */
308
309 dev->stream = STREAM_INTERRUPT;
310 rc = wait_event_timeout(dev->wait_stream,
311 (dev->stream == STREAM_OFF) ||
312 (dev->state & DEV_DISCONNECTED),
313 EM28XX_URB_TIMEOUT);
314
315 if (rc) {
316 dev->state |= DEV_MISCONFIGURED;
317 em28xx_videodbg("device is misconfigured; close and "
318 "open /dev/video%d again\n",
319 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
320 return rc;
321 }
322
323 return 0;
324}
325
326
327static int check_dev(struct em28xx *dev) 929static int check_dev(struct em28xx *dev)
328{ 930{
329 if (dev->state & DEV_DISCONNECTED) { 931 if (dev->state & DEV_DISCONNECTED) {
@@ -447,7 +1049,7 @@ static int vidioc_s_fmt_cap(struct file *file, void *priv,
447{ 1049{
448 struct em28xx_fh *fh = priv; 1050 struct em28xx_fh *fh = priv;
449 struct em28xx *dev = fh->dev; 1051 struct em28xx *dev = fh->dev;
450 int rc, i; 1052 int rc;
451 1053
452 rc = check_dev(dev); 1054 rc = check_dev(dev);
453 if (rc < 0) 1055 if (rc < 0)
@@ -457,25 +1059,6 @@ static int vidioc_s_fmt_cap(struct file *file, void *priv,
457 1059
458 mutex_lock(&dev->lock); 1060 mutex_lock(&dev->lock);
459 1061
460 for (i = 0; i < dev->num_frames; i++)
461 if (dev->frame[i].vma_use_count) {
462 em28xx_videodbg("VIDIOC_S_FMT failed. "
463 "Unmap the buffers first.\n");
464 rc = -EINVAL;
465 goto err;
466 }
467
468 /* stop io in case it is already in progress */
469 if (dev->stream == STREAM_ON) {
470 em28xx_videodbg("VIDIOC_SET_FMT: interrupting stream\n");
471 rc = em28xx_stream_interrupt(dev);
472 if (rc < 0)
473 goto err;
474 }
475
476 em28xx_release_buffers(dev);
477 dev->io = IO_NONE;
478
479 /* set new image size */ 1062 /* set new image size */
480 dev->width = f->fmt.pix.width; 1063 dev->width = f->fmt.pix.width;
481 dev->height = f->fmt.pix.height; 1064 dev->height = f->fmt.pix.height;
@@ -484,19 +1067,11 @@ static int vidioc_s_fmt_cap(struct file *file, void *priv,
484 dev->bytesperline = dev->width * 2; 1067 dev->bytesperline = dev->width * 2;
485 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale); 1068 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
486 1069
487 /* FIXME: This is really weird! Why capture is starting with
488 this ioctl ???
489 */
490 em28xx_uninit_isoc(dev);
491 em28xx_set_alternate(dev); 1070 em28xx_set_alternate(dev);
492 em28xx_capture_start(dev, 1);
493 em28xx_resolution_set(dev); 1071 em28xx_resolution_set(dev);
494 em28xx_init_isoc(dev);
495 rc = 0;
496 1072
497err:
498 mutex_unlock(&dev->lock); 1073 mutex_unlock(&dev->lock);
499 return rc; 1074 return 0;
500} 1075}
501 1076
502static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm) 1077static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
@@ -918,23 +1493,11 @@ static int vidioc_streamon(struct file *file, void *priv,
918 if (rc < 0) 1493 if (rc < 0)
919 return rc; 1494 return rc;
920 1495
921 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || dev->io != IO_MMAP)
922 return -EINVAL;
923
924 if (list_empty(&dev->inqueue))
925 return -EINVAL;
926
927 mutex_lock(&dev->lock);
928 1496
929 if (unlikely(res_get(fh) < 0)) { 1497 if (unlikely(res_get(fh) < 0))
930 mutex_unlock(&dev->lock);
931 return -EBUSY; 1498 return -EBUSY;
932 }
933
934 dev->stream = STREAM_ON; /* FIXME: Start video capture here? */
935 1499
936 mutex_unlock(&dev->lock); 1500 return (videobuf_streamon(&fh->vb_vidq));
937 return 0;
938} 1501}
939 1502
940static int vidioc_streamoff(struct file *file, void *priv, 1503static int vidioc_streamoff(struct file *file, void *priv,
@@ -948,23 +1511,14 @@ static int vidioc_streamoff(struct file *file, void *priv,
948 if (rc < 0) 1511 if (rc < 0)
949 return rc; 1512 return rc;
950 1513
951 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || dev->io != IO_MMAP) 1514 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1515 return -EINVAL;
1516 if (type != fh->type)
952 return -EINVAL; 1517 return -EINVAL;
953 1518
954 mutex_lock(&dev->lock); 1519 videobuf_streamoff(&fh->vb_vidq);
955 1520 res_free(fh);
956 if (dev->stream == STREAM_ON) {
957 em28xx_videodbg("VIDIOC_STREAMOFF: interrupting stream\n");
958 rc = em28xx_stream_interrupt(dev);
959 if (rc < 0) {
960 mutex_unlock(&dev->lock);
961 return rc;
962 }
963 }
964
965 em28xx_empty_framequeues(dev);
966 1521
967 mutex_unlock(&dev->lock);
968 return 0; 1522 return 0;
969} 1523}
970 1524
@@ -1058,53 +1612,13 @@ static int vidioc_reqbufs(struct file *file, void *priv,
1058{ 1612{
1059 struct em28xx_fh *fh = priv; 1613 struct em28xx_fh *fh = priv;
1060 struct em28xx *dev = fh->dev; 1614 struct em28xx *dev = fh->dev;
1061 u32 i;
1062 int rc; 1615 int rc;
1063 1616
1064 rc = check_dev(dev); 1617 rc = check_dev(dev);
1065 if (rc < 0) 1618 if (rc < 0)
1066 return rc; 1619 return rc;
1067 1620
1068 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || 1621 return (videobuf_reqbufs(&fh->vb_vidq, rb));
1069 rb->memory != V4L2_MEMORY_MMAP)
1070 return -EINVAL;
1071
1072 if (dev->io == IO_READ) {
1073 em28xx_videodbg("method is set to read;"
1074 " close and open the device again to"
1075 " choose the mmap I/O method\n");
1076 return -EINVAL;
1077 }
1078
1079 for (i = 0; i < dev->num_frames; i++)
1080 if (dev->frame[i].vma_use_count) {
1081 em28xx_videodbg("VIDIOC_REQBUFS failed; "
1082 "previous buffers are still mapped\n");
1083 return -EINVAL;
1084 }
1085
1086 mutex_lock(&dev->lock);
1087
1088 if (dev->stream == STREAM_ON) {
1089 em28xx_videodbg("VIDIOC_REQBUFS: interrupting stream\n");
1090 rc = em28xx_stream_interrupt(dev);
1091 if (rc < 0) {
1092 mutex_unlock(&dev->lock);
1093 return rc;
1094 }
1095 }
1096
1097 em28xx_empty_framequeues(dev);
1098
1099 em28xx_release_buffers(dev);
1100 if (rb->count)
1101 rb->count = em28xx_request_buffers(dev, rb->count);
1102
1103 dev->frame_current = NULL;
1104 dev->io = rb->count ? IO_MMAP : IO_NONE;
1105
1106 mutex_unlock(&dev->lock);
1107 return 0;
1108} 1622}
1109 1623
1110static int vidioc_querybuf(struct file *file, void *priv, 1624static int vidioc_querybuf(struct file *file, void *priv,
@@ -1118,52 +1632,20 @@ static int vidioc_querybuf(struct file *file, void *priv,
1118 if (rc < 0) 1632 if (rc < 0)
1119 return rc; 1633 return rc;
1120 1634
1121 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || 1635 return (videobuf_querybuf(&fh->vb_vidq, b));
1122 b->index >= dev->num_frames || dev->io != IO_MMAP)
1123 return -EINVAL;
1124
1125 mutex_lock(&dev->lock);
1126
1127 memcpy(b, &dev->frame[b->index].buf, sizeof(*b));
1128
1129 if (dev->frame[b->index].vma_use_count)
1130 b->flags |= V4L2_BUF_FLAG_MAPPED;
1131
1132 if (dev->frame[b->index].state == F_DONE)
1133 b->flags |= V4L2_BUF_FLAG_DONE;
1134 else if (dev->frame[b->index].state != F_UNUSED)
1135 b->flags |= V4L2_BUF_FLAG_QUEUED;
1136
1137 mutex_unlock(&dev->lock);
1138 return 0;
1139} 1636}
1140 1637
1141static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b) 1638static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1142{ 1639{
1143 struct em28xx_fh *fh = priv; 1640 struct em28xx_fh *fh = priv;
1144 struct em28xx *dev = fh->dev; 1641 struct em28xx *dev = fh->dev;
1145 unsigned long lock_flags;
1146 int rc; 1642 int rc;
1147 1643
1148 rc = check_dev(dev); 1644 rc = check_dev(dev);
1149 if (rc < 0) 1645 if (rc < 0)
1150 return rc; 1646 return rc;
1151 1647
1152 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || dev->io != IO_MMAP || 1648 return (videobuf_qbuf(&fh->vb_vidq, b));
1153 b->index >= dev->num_frames)
1154 return -EINVAL;
1155
1156 if (dev->frame[b->index].state != F_UNUSED)
1157 return -EAGAIN;
1158
1159 dev->frame[b->index].state = F_QUEUED;
1160
1161 /* add frame to fifo */
1162 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1163 list_add_tail(&dev->frame[b->index].frame, &dev->inqueue);
1164 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1165
1166 return 0;
1167} 1649}
1168 1650
1169static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b) 1651static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
@@ -1171,46 +1653,24 @@ static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1171 struct em28xx_fh *fh = priv; 1653 struct em28xx_fh *fh = priv;
1172 struct em28xx *dev = fh->dev; 1654 struct em28xx *dev = fh->dev;
1173 int rc; 1655 int rc;
1174 struct em28xx_frame_t *f;
1175 unsigned long lock_flags;
1176 1656
1177 rc = check_dev(dev); 1657 rc = check_dev(dev);
1178 if (rc < 0) 1658 if (rc < 0)
1179 return rc; 1659 return rc;
1180 1660
1181 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || dev->io != IO_MMAP) 1661 return (videobuf_dqbuf(&fh->vb_vidq, b,
1182 return -EINVAL; 1662 file->f_flags & O_NONBLOCK));
1183 1663}
1184 if (list_empty(&dev->outqueue)) {
1185 if (dev->stream == STREAM_OFF)
1186 return -EINVAL;
1187
1188 if (file->f_flags & O_NONBLOCK)
1189 return -EAGAIN;
1190
1191 rc = wait_event_interruptible(dev->wait_frame,
1192 (!list_empty(&dev->outqueue)) ||
1193 (dev->state & DEV_DISCONNECTED));
1194 if (rc)
1195 return rc;
1196
1197 if (dev->state & DEV_DISCONNECTED)
1198 return -ENODEV;
1199 }
1200
1201 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1202 f = list_entry(dev->outqueue.next, struct em28xx_frame_t, frame);
1203 list_del(dev->outqueue.next);
1204 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1205
1206 f->state = F_UNUSED;
1207 memcpy(b, &f->buf, sizeof(*b));
1208 1664
1209 if (f->vma_use_count) 1665#ifdef CONFIG_VIDEO_V4L1_COMPAT
1210 b->flags |= V4L2_BUF_FLAG_MAPPED; 1666static int vidiocgmbuf (struct file *file, void *priv, struct video_mbuf *mbuf)
1667{
1668 struct em28xx_fh *fh=priv;
1211 1669
1212 return 0; 1670 return videobuf_cgmbuf (&fh->vb_vidq, mbuf, 8);
1213} 1671}
1672#endif
1673
1214 1674
1215/* ----------------------------------------------------------- */ 1675/* ----------------------------------------------------------- */
1216/* RADIO ESPECIFIC IOCTLS */ 1676/* RADIO ESPECIFIC IOCTLS */
@@ -1361,16 +1821,8 @@ static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
1361 dev->vscale = 0; 1821 dev->vscale = 0;
1362 1822
1363 em28xx_set_alternate(dev); 1823 em28xx_set_alternate(dev);
1364 em28xx_capture_start(dev, 1);
1365 em28xx_resolution_set(dev); 1824 em28xx_resolution_set(dev);
1366 1825
1367
1368 /* start the transfer */
1369 errCode = em28xx_init_isoc(dev);
1370 if (errCode)
1371 goto err;
1372
1373 em28xx_empty_framequeues(dev);
1374 } 1826 }
1375 if (fh->radio) { 1827 if (fh->radio) {
1376 em28xx_videodbg("video_open: setting radio device\n"); 1828 em28xx_videodbg("video_open: setting radio device\n");
@@ -1379,7 +1831,10 @@ static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
1379 1831
1380 dev->users++; 1832 dev->users++;
1381 1833
1382err: 1834 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
1835 NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
1836 sizeof(struct em28xx_buffer), fh);
1837
1383 mutex_unlock(&dev->lock); 1838 mutex_unlock(&dev->lock);
1384 return errCode; 1839 return errCode;
1385} 1840}
@@ -1445,9 +1900,8 @@ static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
1445 mutex_lock(&dev->lock); 1900 mutex_lock(&dev->lock);
1446 1901
1447 if (dev->users == 1) { 1902 if (dev->users == 1) {
1448 em28xx_uninit_isoc(dev); 1903 videobuf_stop(&fh->vb_vidq);
1449 em28xx_release_buffers(dev); 1904 videobuf_mmap_free(&fh->vb_vidq);
1450 dev->io = IO_NONE;
1451 1905
1452 /* the device is already disconnect, 1906 /* the device is already disconnect,
1453 free the remaining resources */ 1907 free the remaining resources */
@@ -1480,134 +1934,28 @@ static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
1480 */ 1934 */
1481static ssize_t 1935static ssize_t
1482em28xx_v4l2_read(struct file *filp, char __user * buf, size_t count, 1936em28xx_v4l2_read(struct file *filp, char __user * buf, size_t count,
1483 loff_t * f_pos) 1937 loff_t * pos)
1484{ 1938{
1485 struct em28xx_frame_t *f, *i;
1486 unsigned long lock_flags;
1487 int ret = 0;
1488 struct em28xx_fh *fh = filp->private_data; 1939 struct em28xx_fh *fh = filp->private_data;
1489 struct em28xx *dev = fh->dev; 1940 struct em28xx *dev = fh->dev;
1941 int rc;
1942
1943 rc = check_dev(dev);
1944 if (rc < 0)
1945 return rc;
1490 1946
1491 /* FIXME: read() is not prepared to allow changing the video 1947 /* FIXME: read() is not prepared to allow changing the video
1492 resolution while streaming. Seems a bug at em28xx_set_fmt 1948 resolution while streaming. Seems a bug at em28xx_set_fmt
1493 */ 1949 */
1494 1950
1495 if (unlikely(res_get(fh) < 0)) 1951 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1496 return -EBUSY; 1952 if (unlikely(res_get(fh)))
1497 1953 return -EBUSY;
1498 mutex_lock(&dev->lock);
1499
1500 if (dev->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1501 em28xx_videodbg("V4l2_Buf_type_videocapture is set\n");
1502
1503 if (dev->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1504 em28xx_videodbg("V4L2_BUF_TYPE_VBI_CAPTURE is set\n");
1505 em28xx_videodbg("not supported yet! ...\n");
1506 if (copy_to_user(buf, "", 1)) {
1507 mutex_unlock(&dev->lock);
1508 return -EFAULT;
1509 }
1510 mutex_unlock(&dev->lock);
1511 return (1);
1512 }
1513 if (dev->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
1514 em28xx_videodbg("V4L2_BUF_TYPE_SLICED_VBI_CAPTURE is set\n");
1515 em28xx_videodbg("not supported yet! ...\n");
1516 if (copy_to_user(buf, "", 1)) {
1517 mutex_unlock(&dev->lock);
1518 return -EFAULT;
1519 }
1520 mutex_unlock(&dev->lock);
1521 return (1);
1522 }
1523
1524 if (dev->state & DEV_DISCONNECTED) {
1525 em28xx_videodbg("device not present\n");
1526 mutex_unlock(&dev->lock);
1527 return -ENODEV;
1528 }
1529
1530 if (dev->state & DEV_MISCONFIGURED) {
1531 em28xx_videodbg("device misconfigured; close and open it again\n");
1532 mutex_unlock(&dev->lock);
1533 return -EIO;
1534 }
1535
1536 if (dev->io == IO_MMAP) {
1537 em28xx_videodbg ("IO method is set to mmap; close and open"
1538 " the device again to choose the read method\n");
1539 mutex_unlock(&dev->lock);
1540 return -EINVAL;
1541 }
1542
1543 if (dev->io == IO_NONE) {
1544 if (!em28xx_request_buffers(dev, EM28XX_NUM_READ_FRAMES)) {
1545 em28xx_errdev("read failed, not enough memory\n");
1546 mutex_unlock(&dev->lock);
1547 return -ENOMEM;
1548 }
1549 dev->io = IO_READ;
1550 dev->stream = STREAM_ON;
1551 em28xx_queue_unusedframes(dev);
1552 }
1553
1554 if (!count) {
1555 mutex_unlock(&dev->lock);
1556 return 0;
1557 }
1558
1559 if (list_empty(&dev->outqueue)) {
1560 if (filp->f_flags & O_NONBLOCK) {
1561 mutex_unlock(&dev->lock);
1562 return -EAGAIN;
1563 }
1564 ret = wait_event_interruptible
1565 (dev->wait_frame,
1566 (!list_empty(&dev->outqueue)) ||
1567 (dev->state & DEV_DISCONNECTED));
1568 if (ret) {
1569 mutex_unlock(&dev->lock);
1570 return ret;
1571 }
1572 if (dev->state & DEV_DISCONNECTED) {
1573 mutex_unlock(&dev->lock);
1574 return -ENODEV;
1575 }
1576 dev->video_bytesread = 0;
1577 }
1578
1579 f = list_entry(dev->outqueue.prev, struct em28xx_frame_t, frame);
1580
1581 em28xx_queue_unusedframes(dev);
1582
1583 if (count > f->buf.length)
1584 count = f->buf.length;
1585
1586 if ((dev->video_bytesread + count) > dev->frame_size)
1587 count = dev->frame_size - dev->video_bytesread;
1588
1589 if (copy_to_user(buf, f->bufmem+dev->video_bytesread, count)) {
1590 em28xx_err("Error while copying to user\n");
1591 return -EFAULT;
1592 }
1593 dev->video_bytesread += count;
1594
1595 if (dev->video_bytesread == dev->frame_size) {
1596 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1597 list_for_each_entry(i, &dev->outqueue, frame)
1598 i->state = F_UNUSED;
1599 INIT_LIST_HEAD(&dev->outqueue);
1600 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1601 1954
1602 em28xx_queue_unusedframes(dev); 1955 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1603 dev->video_bytesread = 0; 1956 filp->f_flags & O_NONBLOCK);
1604 } 1957 }
1605 1958 return 0;
1606 *f_pos += count;
1607
1608 mutex_unlock(&dev->lock);
1609
1610 return count;
1611} 1959}
1612 1960
1613/* 1961/*
@@ -1616,46 +1964,21 @@ em28xx_v4l2_read(struct file *filp, char __user * buf, size_t count,
1616 */ 1964 */
1617static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait) 1965static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
1618{ 1966{
1619 unsigned int mask = 0;
1620 struct em28xx_fh *fh = filp->private_data; 1967 struct em28xx_fh *fh = filp->private_data;
1621 struct em28xx *dev = fh->dev; 1968 struct em28xx *dev = fh->dev;
1969 int rc;
1970
1971 rc = check_dev(dev);
1972 if (rc < 0)
1973 return rc;
1622 1974
1623 if (unlikely(res_get(fh) < 0)) 1975 if (unlikely(res_get(fh) < 0))
1624 return POLLERR; 1976 return POLLERR;
1625 1977
1626 mutex_lock(&dev->lock); 1978 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1627 1979 return POLLERR;
1628 if (dev->state & DEV_DISCONNECTED) {
1629 em28xx_videodbg("device not present\n");
1630 } else if (dev->state & DEV_MISCONFIGURED) {
1631 em28xx_videodbg("device is misconfigured; close and open it again\n");
1632 } else {
1633 if (dev->io == IO_NONE) {
1634 if (!em28xx_request_buffers
1635 (dev, EM28XX_NUM_READ_FRAMES)) {
1636 em28xx_warn
1637 ("poll() failed, not enough memory\n");
1638 } else {
1639 dev->io = IO_READ;
1640 dev->stream = STREAM_ON;
1641 }
1642 }
1643
1644 if (dev->io == IO_READ) {
1645 em28xx_queue_unusedframes(dev);
1646 poll_wait(filp, &dev->wait_frame, wait);
1647
1648 if (!list_empty(&dev->outqueue))
1649 mask |= POLLIN | POLLRDNORM;
1650
1651 mutex_unlock(&dev->lock);
1652
1653 return mask;
1654 }
1655 }
1656 1980
1657 mutex_unlock(&dev->lock); 1981 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
1658 return POLLERR;
1659} 1982}
1660 1983
1661/* 1984/*
@@ -1665,69 +1988,23 @@ static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
1665{ 1988{
1666 struct em28xx_fh *fh = filp->private_data; 1989 struct em28xx_fh *fh = filp->private_data;
1667 struct em28xx *dev = fh->dev; 1990 struct em28xx *dev = fh->dev;
1668 unsigned long size = vma->vm_end - vma->vm_start; 1991 int rc;
1669 unsigned long start = vma->vm_start;
1670 void *pos;
1671 u32 i;
1672 1992
1673 if (unlikely(res_get(fh) < 0)) 1993 if (unlikely(res_get(fh) < 0))
1674 return -EBUSY; 1994 return -EBUSY;
1675 1995
1676 mutex_lock(&dev->lock); 1996 rc = check_dev(dev);
1677 1997 if (rc < 0)
1678 if (dev->state & DEV_DISCONNECTED) { 1998 return rc;
1679 em28xx_videodbg("mmap: device not present\n");
1680 mutex_unlock(&dev->lock);
1681 return -ENODEV;
1682 }
1683
1684 if (dev->state & DEV_MISCONFIGURED) {
1685 em28xx_videodbg ("mmap: Device is misconfigured; close and "
1686 "open it again\n");
1687 mutex_unlock(&dev->lock);
1688 return -EIO;
1689 }
1690
1691 if (dev->io != IO_MMAP || !(vma->vm_flags & VM_WRITE)) {
1692 mutex_unlock(&dev->lock);
1693 return -EINVAL;
1694 }
1695
1696 if (size > PAGE_ALIGN(dev->frame[0].buf.length))
1697 size = PAGE_ALIGN(dev->frame[0].buf.length);
1698
1699 for (i = 0; i < dev->num_frames; i++) {
1700 if ((dev->frame[i].buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
1701 break;
1702 }
1703 if (i == dev->num_frames) {
1704 em28xx_videodbg("mmap: user supplied mapping address is out of range\n");
1705 mutex_unlock(&dev->lock);
1706 return -EINVAL;
1707 }
1708
1709 /* VM_IO is eventually going to replace PageReserved altogether */
1710 vma->vm_flags |= VM_IO;
1711 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
1712 1999
1713 pos = dev->frame[i].bufmem; 2000 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1714 while (size > 0) { /* size is page-aligned */
1715 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
1716 em28xx_videodbg("mmap: vm_insert_page failed\n");
1717 mutex_unlock(&dev->lock);
1718 return -EAGAIN;
1719 }
1720 start += PAGE_SIZE;
1721 pos += PAGE_SIZE;
1722 size -= PAGE_SIZE;
1723 }
1724 2001
1725 vma->vm_ops = &em28xx_vm_ops; 2002 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1726 vma->vm_private_data = &dev->frame[i]; 2003 (unsigned long)vma->vm_start,
2004 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
2005 rc);
1727 2006
1728 em28xx_vm_open(vma); 2007 return rc;
1729 mutex_unlock(&dev->lock);
1730 return 0;
1731} 2008}
1732 2009
1733static const struct file_operations em28xx_v4l_fops = { 2010static const struct file_operations em28xx_v4l_fops = {
@@ -1790,6 +2067,9 @@ static const struct video_device em28xx_video_template = {
1790 .vidioc_g_register = vidioc_g_register, 2067 .vidioc_g_register = vidioc_g_register,
1791 .vidioc_s_register = vidioc_s_register, 2068 .vidioc_s_register = vidioc_s_register,
1792#endif 2069#endif
2070#ifdef CONFIG_VIDEO_V4L1_COMPAT
2071 .vidiocgmbuf = vidiocgmbuf,
2072#endif
1793 2073
1794 .tvnorms = V4L2_STD_ALL, 2074 .tvnorms = V4L2_STD_ALL,
1795 .current_norm = V4L2_STD_PAL, 2075 .current_norm = V4L2_STD_PAL,
@@ -2005,6 +2285,14 @@ static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
2005 dev->radio_dev->minor & 0x1f); 2285 dev->radio_dev->minor & 0x1f);
2006 } 2286 }
2007 2287
2288 /* init video dma queues */
2289 INIT_LIST_HEAD(&dev->vidq.active);
2290 INIT_LIST_HEAD(&dev->vidq.queued);
2291
2292 dev->vidq.timeout.function = em28xx_vid_timeout;
2293 dev->vidq.timeout.data = (unsigned long)dev;
2294 init_timer(&dev->vidq.timeout);
2295
2008 2296
2009 if (dev->has_msp34xx) { 2297 if (dev->has_msp34xx) {
2010 /* Send a reset to other chips via gpio */ 2298 /* Send a reset to other chips via gpio */
diff --git a/drivers/media/video/em28xx/em28xx.h b/drivers/media/video/em28xx/em28xx.h
index 04e0e48ecabe..dd18ceb30a43 100644
--- a/drivers/media/video/em28xx/em28xx.h
+++ b/drivers/media/video/em28xx/em28xx.h
@@ -26,12 +26,12 @@
26#define _EM28XX_H 26#define _EM28XX_H
27 27
28#include <linux/videodev2.h> 28#include <linux/videodev2.h>
29#include <media/videobuf-vmalloc.h>
30
29#include <linux/i2c.h> 31#include <linux/i2c.h>
30#include <linux/mutex.h> 32#include <linux/mutex.h>
31#include <media/ir-kbd-i2c.h> 33#include <media/ir-kbd-i2c.h>
32 34
33#define UNSET -1
34
35/* maximum number of em28xx boards */ 35/* maximum number of em28xx boards */
36#define EM28XX_MAXBOARDS 4 /*FIXME: should be bigger */ 36#define EM28XX_MAXBOARDS 4 /*FIXME: should be bigger */
37 37
@@ -81,31 +81,69 @@
81/* time in msecs to wait for i2c writes to finish */ 81/* time in msecs to wait for i2c writes to finish */
82#define EM2800_I2C_WRITE_TIMEOUT 20 82#define EM2800_I2C_WRITE_TIMEOUT 20
83 83
84/* the various frame states */
85enum em28xx_frame_state {
86 F_UNUSED = 0,
87 F_QUEUED,
88 F_GRABBING,
89 F_DONE,
90 F_ERROR,
91};
92
93/* stream states */
94enum em28xx_stream_state { 84enum em28xx_stream_state {
95 STREAM_OFF, 85 STREAM_OFF,
96 STREAM_INTERRUPT, 86 STREAM_INTERRUPT,
97 STREAM_ON, 87 STREAM_ON,
98}; 88};
99 89
100/* frames */ 90struct em28xx_usb_isoc_ctl {
101struct em28xx_frame_t { 91 /* max packet size of isoc transaction */
102 void *bufmem; 92 int max_pkt_size;
103 struct v4l2_buffer buf; 93
104 enum em28xx_frame_state state; 94 /* number of allocated urbs */
95 int num_bufs;
96
97 /* urb for isoc transfers */
98 struct urb **urb;
99
100 /* transfer buffers for isoc transfer */
101 char **transfer_buffer;
102
103 /* Last buffer command and region */
104 u8 cmd;
105 int pos, size, pktsize;
106
107 /* Last field: ODD or EVEN? */
108 int field;
109
110 /* Stores incomplete commands */
111 u32 tmp_buf;
112 int tmp_buf_len;
113
114 /* Stores already requested buffers */
115 struct em28xx_buffer *buf;
116
117 /* Stores the number of received fields */
118 int nfields;
119};
120
121struct em28xx_fmt {
122 char *name;
123 u32 fourcc; /* v4l2 format id */
124};
125
126/* buffer for one video frame */
127struct em28xx_buffer {
128 /* common v4l buffer stuff -- must be first */
129 struct videobuf_buffer vb;
130
131 struct em28xx_fmt *fmt;
132
105 struct list_head frame; 133 struct list_head frame;
106 unsigned long vma_use_count;
107 int top_field; 134 int top_field;
108 int fieldbytesused; 135 int receiving;
136};
137
138struct em28xx_dmaqueue {
139 struct list_head active;
140 struct list_head queued;
141 struct timer_list timeout;
142
143 wait_queue_head_t wq;
144
145 /* Counters to control buffer fill */
146 int pos;
109}; 147};
110 148
111/* io methods */ 149/* io methods */
@@ -255,10 +293,6 @@ struct em28xx {
255 int mute; 293 int mute;
256 int volume; 294 int volume;
257 /* frame properties */ 295 /* frame properties */
258 struct em28xx_frame_t frame[EM28XX_NUM_FRAMES]; /* list of frames */
259 int num_frames; /* number of frames currently in use */
260 unsigned int frame_count; /* total number of transfered frames */
261 struct em28xx_frame_t *frame_current; /* the frame that is being filled */
262 int width; /* current frame width */ 296 int width; /* current frame width */
263 int height; /* current frame height */ 297 int height; /* current frame height */
264 int frame_size; /* current frame size */ 298 int frame_size; /* current frame size */
@@ -277,7 +311,6 @@ struct em28xx {
277 311
278 /* states */ 312 /* states */
279 enum em28xx_dev_state state; 313 enum em28xx_dev_state state;
280 enum em28xx_stream_state stream;
281 enum em28xx_io_method io; 314 enum em28xx_io_method io;
282 315
283 struct work_struct request_module_wk; 316 struct work_struct request_module_wk;
@@ -292,6 +325,11 @@ struct em28xx {
292 325
293 unsigned char eedata[256]; 326 unsigned char eedata[256];
294 327
328 /* Isoc control struct */
329 struct em28xx_dmaqueue vidq;
330 struct em28xx_usb_isoc_ctl isoc_ctl;
331 spinlock_t slock;
332
295 /* usb transfer */ 333 /* usb transfer */
296 struct usb_device *udev; /* the usb device */ 334 struct usb_device *udev; /* the usb device */
297 int alt; /* alternate */ 335 int alt; /* alternate */
@@ -315,6 +353,12 @@ struct em28xx_fh {
315 struct em28xx *dev; 353 struct em28xx *dev;
316 unsigned int stream_on:1; /* Locks streams */ 354 unsigned int stream_on:1; /* Locks streams */
317 int radio; 355 int radio;
356
357 unsigned int width, height;
358 struct videobuf_queue vb_vidq;
359 struct em28xx_fmt *fmt;
360
361 enum v4l2_buf_type type;
318}; 362};
319 363
320struct em28xx_ops { 364struct em28xx_ops {
@@ -351,8 +395,6 @@ int em28xx_colorlevels_set_default(struct em28xx *dev);
351int em28xx_capture_start(struct em28xx *dev, int start); 395int em28xx_capture_start(struct em28xx *dev, int start);
352int em28xx_outfmt_set_yuv422(struct em28xx *dev); 396int em28xx_outfmt_set_yuv422(struct em28xx *dev);
353int em28xx_resolution_set(struct em28xx *dev); 397int em28xx_resolution_set(struct em28xx *dev);
354int em28xx_init_isoc(struct em28xx *dev);
355void em28xx_uninit_isoc(struct em28xx *dev);
356int em28xx_set_alternate(struct em28xx *dev); 398int em28xx_set_alternate(struct em28xx *dev);
357 399
358/* Provided by em28xx-video.c */ 400/* Provided by em28xx-video.c */