Merge branch 'origin'
[linux-2.6] / drivers / media / video / video-buf.c
1 /*
2  *
3  * generic helper functions for video4linux capture buffers, to handle
4  * memory management and PCI DMA.  Right now bttv + saa7134 use it.
5  *
6  * The functions expect the hardware being able to scatter gatter
7  * (i.e. the buffers are not linear in physical memory, but fragmented
8  * into PAGE_SIZE chunks).  They also assume the driver does not need
9  * to touch the video data (thus it is probably not useful for USB 1.1
10  * as data often must be uncompressed by the drivers).
11  *
12  * (c) 2001-2004 Gerd Knorr <kraxel@bytesex.org> [SUSE Labs]
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  */
19
20 #include <linux/init.h>
21 #include <linux/module.h>
22 #include <linux/moduleparam.h>
23 #include <linux/vmalloc.h>
24 #include <linux/pagemap.h>
25 #include <linux/slab.h>
26 #include <linux/pci.h>
27 #include <linux/interrupt.h>
28 #include <asm/page.h>
29 #include <asm/pgtable.h>
30
31 #include <media/video-buf.h>
32
33 #define MAGIC_DMABUF 0x19721112
34 #define MAGIC_BUFFER 0x20040302
35 #define MAGIC_CHECK(is,should)  if (unlikely((is) != (should))) \
36         { printk(KERN_ERR "magic mismatch: %x (expected %x)\n",is,should); BUG(); }
37
38 static int debug = 0;
39 module_param(debug, int, 0644);
40
41 MODULE_DESCRIPTION("helper module to manage video4linux pci dma buffers");
42 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
43 MODULE_LICENSE("GPL");
44
45 #define dprintk(level, fmt, arg...)     if (debug >= level) \
46         printk(KERN_DEBUG "vbuf: " fmt , ## arg)
47
48 struct scatterlist*
49 videobuf_vmalloc_to_sg(unsigned char *virt, int nr_pages)
50 {
51         struct scatterlist *sglist;
52         struct page *pg;
53         int i;
54
55         sglist = kcalloc(nr_pages, sizeof(struct scatterlist), GFP_KERNEL);
56         if (NULL == sglist)
57                 return NULL;
58         for (i = 0; i < nr_pages; i++, virt += PAGE_SIZE) {
59                 pg = vmalloc_to_page(virt);
60                 if (NULL == pg)
61                         goto err;
62                 if (PageHighMem(pg))
63                         BUG();
64                 sglist[i].page   = pg;
65                 sglist[i].length = PAGE_SIZE;
66         }
67         return sglist;
68
69  err:
70         kfree(sglist);
71         return NULL;
72 }
73
74 struct scatterlist*
75 videobuf_pages_to_sg(struct page **pages, int nr_pages, int offset)
76 {
77         struct scatterlist *sglist;
78         int i = 0;
79
80         if (NULL == pages[0])
81                 return NULL;
82         sglist = kcalloc(nr_pages, sizeof(*sglist), GFP_KERNEL);
83         if (NULL == sglist)
84                 return NULL;
85
86         if (NULL == pages[0])
87                 goto nopage;
88         if (PageHighMem(pages[0]))
89                 /* DMA to highmem pages might not work */
90                 goto highmem;
91         sglist[0].page   = pages[0];
92         sglist[0].offset = offset;
93         sglist[0].length = PAGE_SIZE - offset;
94         for (i = 1; i < nr_pages; i++) {
95                 if (NULL == pages[i])
96                         goto nopage;
97                 if (PageHighMem(pages[i]))
98                         goto highmem;
99                 sglist[i].page   = pages[i];
100                 sglist[i].length = PAGE_SIZE;
101         }
102         return sglist;
103
104  nopage:
105         dprintk(2,"sgl: oops - no page\n");
106         kfree(sglist);
107         return NULL;
108
109  highmem:
110         dprintk(2,"sgl: oops - highmem page\n");
111         kfree(sglist);
112         return NULL;
113 }
114
115 /* --------------------------------------------------------------------- */
116
117 void videobuf_dma_init(struct videobuf_dmabuf *dma)
118 {
119         memset(dma,0,sizeof(*dma));
120         dma->magic = MAGIC_DMABUF;
121 }
122
123 int videobuf_dma_init_user(struct videobuf_dmabuf *dma, int direction,
124                            unsigned long data, unsigned long size)
125 {
126         unsigned long first,last;
127         int err, rw = 0;
128
129         dma->direction = direction;
130         switch (dma->direction) {
131         case PCI_DMA_FROMDEVICE: rw = READ;  break;
132         case PCI_DMA_TODEVICE:   rw = WRITE; break;
133         default:                 BUG();
134         }
135
136         first = (data          & PAGE_MASK) >> PAGE_SHIFT;
137         last  = ((data+size-1) & PAGE_MASK) >> PAGE_SHIFT;
138         dma->offset   = data & ~PAGE_MASK;
139         dma->nr_pages = last-first+1;
140         dma->pages = kmalloc(dma->nr_pages * sizeof(struct page*),
141                              GFP_KERNEL);
142         if (NULL == dma->pages)
143                 return -ENOMEM;
144         dprintk(1,"init user [0x%lx+0x%lx => %d pages]\n",
145                 data,size,dma->nr_pages);
146
147         down_read(&current->mm->mmap_sem);
148         err = get_user_pages(current,current->mm,
149                              data & PAGE_MASK, dma->nr_pages,
150                              rw == READ, 1, /* force */
151                              dma->pages, NULL);
152         up_read(&current->mm->mmap_sem);
153         if (err != dma->nr_pages) {
154                 dma->nr_pages = (err >= 0) ? err : 0;
155                 dprintk(1,"get_user_pages: err=%d [%d]\n",err,dma->nr_pages);
156                 return err < 0 ? err : -EINVAL;
157         }
158         return 0;
159 }
160
161 int videobuf_dma_init_kernel(struct videobuf_dmabuf *dma, int direction,
162                              int nr_pages)
163 {
164         dprintk(1,"init kernel [%d pages]\n",nr_pages);
165         dma->direction = direction;
166         dma->vmalloc = vmalloc_32(nr_pages << PAGE_SHIFT);
167         if (NULL == dma->vmalloc) {
168                 dprintk(1,"vmalloc_32(%d pages) failed\n",nr_pages);
169                 return -ENOMEM;
170         }
171         memset(dma->vmalloc,0,nr_pages << PAGE_SHIFT);
172         dma->nr_pages = nr_pages;
173         return 0;
174 }
175
176 int videobuf_dma_init_overlay(struct videobuf_dmabuf *dma, int direction,
177                               dma_addr_t addr, int nr_pages)
178 {
179         dprintk(1,"init overlay [%d pages @ bus 0x%lx]\n",
180                 nr_pages,(unsigned long)addr);
181         dma->direction = direction;
182         if (0 == addr)
183                 return -EINVAL;
184
185         dma->bus_addr = addr;
186         dma->nr_pages = nr_pages;
187         return 0;
188 }
189
190 int videobuf_dma_pci_map(struct pci_dev *dev, struct videobuf_dmabuf *dma)
191 {
192         MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
193         BUG_ON(0 == dma->nr_pages);
194
195         if (dma->pages) {
196                 dma->sglist = videobuf_pages_to_sg(dma->pages, dma->nr_pages,
197                                                    dma->offset);
198         }
199         if (dma->vmalloc) {
200                 dma->sglist = videobuf_vmalloc_to_sg
201                         (dma->vmalloc,dma->nr_pages);
202         }
203         if (dma->bus_addr) {
204                 dma->sglist = kmalloc(sizeof(struct scatterlist), GFP_KERNEL);
205                 if (NULL != dma->sglist) {
206                         dma->sglen  = 1;
207                         sg_dma_address(&dma->sglist[0]) = dma->bus_addr & PAGE_MASK;
208                         dma->sglist[0].offset           = dma->bus_addr & ~PAGE_MASK;
209                         sg_dma_len(&dma->sglist[0])     = dma->nr_pages * PAGE_SIZE;
210                 }
211         }
212         if (NULL == dma->sglist) {
213                 dprintk(1,"scatterlist is NULL\n");
214                 return -ENOMEM;
215         }
216
217         if (!dma->bus_addr) {
218                 dma->sglen = pci_map_sg(dev,dma->sglist,dma->nr_pages,
219                                         dma->direction);
220                 if (0 == dma->sglen) {
221                         printk(KERN_WARNING
222                                "%s: pci_map_sg failed\n",__FUNCTION__);
223                         kfree(dma->sglist);
224                         dma->sglist = NULL;
225                         dma->sglen = 0;
226                         return -EIO;
227                 }
228         }
229         return 0;
230 }
231
232 int videobuf_dma_pci_sync(struct pci_dev *dev, struct videobuf_dmabuf *dma)
233 {
234         MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
235         BUG_ON(!dma->sglen);
236
237         if (!dma->bus_addr)
238                 pci_dma_sync_sg_for_cpu(dev,dma->sglist,dma->nr_pages,dma->direction);
239         return 0;
240 }
241
242 int videobuf_dma_pci_unmap(struct pci_dev *dev, struct videobuf_dmabuf *dma)
243 {
244         MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
245         if (!dma->sglen)
246                 return 0;
247
248         if (!dma->bus_addr)
249                 pci_unmap_sg(dev,dma->sglist,dma->nr_pages,dma->direction);
250         kfree(dma->sglist);
251         dma->sglist = NULL;
252         dma->sglen = 0;
253         return 0;
254 }
255
256 int videobuf_dma_free(struct videobuf_dmabuf *dma)
257 {
258         MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
259         BUG_ON(dma->sglen);
260
261         if (dma->pages) {
262                 int i;
263                 for (i=0; i < dma->nr_pages; i++)
264                         page_cache_release(dma->pages[i]);
265                 kfree(dma->pages);
266                 dma->pages = NULL;
267         }
268
269         vfree(dma->vmalloc);
270         dma->vmalloc = NULL;
271
272         if (dma->bus_addr) {
273                 dma->bus_addr = 0;
274         }
275         dma->direction = PCI_DMA_NONE;
276         return 0;
277 }
278
279 /* --------------------------------------------------------------------- */
280
281 void* videobuf_alloc(unsigned int size)
282 {
283         struct videobuf_buffer *vb;
284
285         vb = kzalloc(size,GFP_KERNEL);
286         if (NULL != vb) {
287                 videobuf_dma_init(&vb->dma);
288                 init_waitqueue_head(&vb->done);
289                 vb->magic     = MAGIC_BUFFER;
290         }
291         return vb;
292 }
293
294 int videobuf_waiton(struct videobuf_buffer *vb, int non_blocking, int intr)
295 {
296         int retval = 0;
297         DECLARE_WAITQUEUE(wait, current);
298
299         MAGIC_CHECK(vb->magic,MAGIC_BUFFER);
300         add_wait_queue(&vb->done, &wait);
301         while (vb->state == STATE_ACTIVE || vb->state == STATE_QUEUED) {
302                 if (non_blocking) {
303                         retval = -EAGAIN;
304                         break;
305                 }
306                 set_current_state(intr  ? TASK_INTERRUPTIBLE
307                                         : TASK_UNINTERRUPTIBLE);
308                 if (vb->state == STATE_ACTIVE || vb->state == STATE_QUEUED)
309                         schedule();
310                 set_current_state(TASK_RUNNING);
311                 if (intr && signal_pending(current)) {
312                         dprintk(1,"buffer waiton: -EINTR\n");
313                         retval = -EINTR;
314                         break;
315                 }
316         }
317         remove_wait_queue(&vb->done, &wait);
318         return retval;
319 }
320
321 int
322 videobuf_iolock(struct pci_dev *pci, struct videobuf_buffer *vb,
323                 struct v4l2_framebuffer *fbuf)
324 {
325         int err,pages;
326         dma_addr_t bus;
327
328         MAGIC_CHECK(vb->magic,MAGIC_BUFFER);
329         switch (vb->memory) {
330         case V4L2_MEMORY_MMAP:
331         case V4L2_MEMORY_USERPTR:
332                 if (0 == vb->baddr) {
333                         /* no userspace addr -- kernel bounce buffer */
334                         pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
335                         err = videobuf_dma_init_kernel(&vb->dma,PCI_DMA_FROMDEVICE,
336                                                        pages);
337                         if (0 != err)
338                                 return err;
339                 } else {
340                         /* dma directly to userspace */
341                         err = videobuf_dma_init_user(&vb->dma,PCI_DMA_FROMDEVICE,
342                                                      vb->baddr,vb->bsize);
343                         if (0 != err)
344                                 return err;
345                 }
346                 break;
347         case V4L2_MEMORY_OVERLAY:
348                 if (NULL == fbuf)
349                         return -EINVAL;
350                 /* FIXME: need sanity checks for vb->boff */
351                 bus   = (dma_addr_t)fbuf->base + vb->boff;
352                 pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
353                 err = videobuf_dma_init_overlay(&vb->dma,PCI_DMA_FROMDEVICE,
354                                                 bus, pages);
355                 if (0 != err)
356                         return err;
357                 break;
358         default:
359                 BUG();
360         }
361         err = videobuf_dma_pci_map(pci,&vb->dma);
362         if (0 != err)
363                 return err;
364
365         return 0;
366 }
367
368 /* --------------------------------------------------------------------- */
369
370 void videobuf_queue_init(struct videobuf_queue* q,
371                          struct videobuf_queue_ops *ops,
372                          struct pci_dev *pci,
373                          spinlock_t *irqlock,
374                          enum v4l2_buf_type type,
375                          enum v4l2_field field,
376                          unsigned int msize,
377                          void *priv)
378 {
379         memset(q,0,sizeof(*q));
380         q->irqlock = irqlock;
381         q->pci     = pci;
382         q->type    = type;
383         q->field   = field;
384         q->msize   = msize;
385         q->ops     = ops;
386         q->priv_data = priv;
387
388         mutex_init(&q->lock);
389         INIT_LIST_HEAD(&q->stream);
390 }
391
392 int
393 videobuf_queue_is_busy(struct videobuf_queue *q)
394 {
395         int i;
396
397         if (q->streaming) {
398                 dprintk(1,"busy: streaming active\n");
399                 return 1;
400         }
401         if (q->reading) {
402                 dprintk(1,"busy: pending read #1\n");
403                 return 1;
404         }
405         if (q->read_buf) {
406                 dprintk(1,"busy: pending read #2\n");
407                 return 1;
408         }
409         for (i = 0; i < VIDEO_MAX_FRAME; i++) {
410                 if (NULL == q->bufs[i])
411                         continue;
412                 if (q->bufs[i]->map) {
413                         dprintk(1,"busy: buffer #%d mapped\n",i);
414                         return 1;
415                 }
416                 if (q->bufs[i]->state == STATE_QUEUED) {
417                         dprintk(1,"busy: buffer #%d queued\n",i);
418                         return 1;
419                 }
420                 if (q->bufs[i]->state == STATE_ACTIVE) {
421                         dprintk(1,"busy: buffer #%d avtive\n",i);
422                         return 1;
423                 }
424         }
425         return 0;
426 }
427
428 void
429 videobuf_queue_cancel(struct videobuf_queue *q)
430 {
431         unsigned long flags;
432         int i;
433
434         /* remove queued buffers from list */
435         spin_lock_irqsave(q->irqlock,flags);
436         for (i = 0; i < VIDEO_MAX_FRAME; i++) {
437                 if (NULL == q->bufs[i])
438                         continue;
439                 if (q->bufs[i]->state == STATE_QUEUED) {
440                         list_del(&q->bufs[i]->queue);
441                         q->bufs[i]->state = STATE_ERROR;
442                 }
443         }
444         spin_unlock_irqrestore(q->irqlock,flags);
445
446         /* free all buffers + clear queue */
447         for (i = 0; i < VIDEO_MAX_FRAME; i++) {
448                 if (NULL == q->bufs[i])
449                         continue;
450                 q->ops->buf_release(q,q->bufs[i]);
451         }
452         INIT_LIST_HEAD(&q->stream);
453 }
454
455 /* --------------------------------------------------------------------- */
456
457 enum v4l2_field
458 videobuf_next_field(struct videobuf_queue *q)
459 {
460         enum v4l2_field field = q->field;
461
462         BUG_ON(V4L2_FIELD_ANY == field);
463
464         if (V4L2_FIELD_ALTERNATE == field) {
465                 if (V4L2_FIELD_TOP == q->last) {
466                         field   = V4L2_FIELD_BOTTOM;
467                         q->last = V4L2_FIELD_BOTTOM;
468                 } else {
469                         field   = V4L2_FIELD_TOP;
470                         q->last = V4L2_FIELD_TOP;
471                 }
472         }
473         return field;
474 }
475
476 void
477 videobuf_status(struct v4l2_buffer *b, struct videobuf_buffer *vb,
478                 enum v4l2_buf_type type)
479 {
480         MAGIC_CHECK(vb->magic,MAGIC_BUFFER);
481
482         b->index    = vb->i;
483         b->type     = type;
484
485         b->memory   = vb->memory;
486         switch (b->memory) {
487         case V4L2_MEMORY_MMAP:
488                 b->m.offset  = vb->boff;
489                 b->length    = vb->bsize;
490                 break;
491         case V4L2_MEMORY_USERPTR:
492                 b->m.userptr = vb->baddr;
493                 b->length    = vb->bsize;
494                 break;
495         case V4L2_MEMORY_OVERLAY:
496                 b->m.offset  = vb->boff;
497                 break;
498         }
499
500         b->flags    = 0;
501         if (vb->map)
502                 b->flags |= V4L2_BUF_FLAG_MAPPED;
503
504         switch (vb->state) {
505         case STATE_PREPARED:
506         case STATE_QUEUED:
507         case STATE_ACTIVE:
508                 b->flags |= V4L2_BUF_FLAG_QUEUED;
509                 break;
510         case STATE_DONE:
511         case STATE_ERROR:
512                 b->flags |= V4L2_BUF_FLAG_DONE;
513                 break;
514         case STATE_NEEDS_INIT:
515         case STATE_IDLE:
516                 /* nothing */
517                 break;
518         }
519
520         if (vb->input != UNSET) {
521                 b->flags |= V4L2_BUF_FLAG_INPUT;
522                 b->input  = vb->input;
523         }
524
525         b->field     = vb->field;
526         b->timestamp = vb->ts;
527         b->bytesused = vb->size;
528         b->sequence  = vb->field_count >> 1;
529 }
530
531 int
532 videobuf_reqbufs(struct videobuf_queue *q,
533                  struct v4l2_requestbuffers *req)
534 {
535         unsigned int size,count;
536         int retval;
537
538         if (req->type != q->type)
539                 return -EINVAL;
540         if (req->count < 1)
541                 return -EINVAL;
542         if (req->memory != V4L2_MEMORY_MMAP     &&
543             req->memory != V4L2_MEMORY_USERPTR  &&
544             req->memory != V4L2_MEMORY_OVERLAY)
545                 return -EINVAL;
546
547         if (q->streaming)
548                 return -EBUSY;
549         if (!list_empty(&q->stream))
550                 return -EBUSY;
551
552         mutex_lock(&q->lock);
553         count = req->count;
554         if (count > VIDEO_MAX_FRAME)
555                 count = VIDEO_MAX_FRAME;
556         size = 0;
557         q->ops->buf_setup(q,&count,&size);
558         size = PAGE_ALIGN(size);
559         dprintk(1,"reqbufs: bufs=%d, size=0x%x [%d pages total]\n",
560                 count, size, (count*size)>>PAGE_SHIFT);
561
562         retval = videobuf_mmap_setup(q,count,size,req->memory);
563         if (retval < 0)
564                 goto done;
565
566         req->count = count;
567
568  done:
569         mutex_unlock(&q->lock);
570         return retval;
571 }
572
573 int
574 videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b)
575 {
576         if (unlikely(b->type != q->type))
577                 return -EINVAL;
578         if (unlikely(b->index < 0 || b->index >= VIDEO_MAX_FRAME))
579                 return -EINVAL;
580         if (unlikely(NULL == q->bufs[b->index]))
581                 return -EINVAL;
582         videobuf_status(b,q->bufs[b->index],q->type);
583         return 0;
584 }
585
586 int
587 videobuf_qbuf(struct videobuf_queue *q,
588               struct v4l2_buffer *b)
589 {
590         struct videobuf_buffer *buf;
591         enum v4l2_field field;
592         unsigned long flags;
593         int retval;
594
595         mutex_lock(&q->lock);
596         retval = -EBUSY;
597         if (q->reading)
598                 goto done;
599         retval = -EINVAL;
600         if (b->type != q->type)
601                 goto done;
602         if (b->index < 0 || b->index >= VIDEO_MAX_FRAME)
603                 goto done;
604         buf = q->bufs[b->index];
605         if (NULL == buf)
606                 goto done;
607         MAGIC_CHECK(buf->magic,MAGIC_BUFFER);
608         if (buf->memory != b->memory)
609                 goto done;
610         if (buf->state == STATE_QUEUED ||
611             buf->state == STATE_ACTIVE)
612                 goto done;
613
614         if (b->flags & V4L2_BUF_FLAG_INPUT) {
615                 if (b->input >= q->inputs)
616                         goto done;
617                 buf->input = b->input;
618         } else {
619                 buf->input = UNSET;
620         }
621
622         switch (b->memory) {
623         case V4L2_MEMORY_MMAP:
624                 if (0 == buf->baddr)
625                         goto done;
626                 break;
627         case V4L2_MEMORY_USERPTR:
628                 if (b->length < buf->bsize)
629                         goto done;
630                 if (STATE_NEEDS_INIT != buf->state && buf->baddr != b->m.userptr)
631                         q->ops->buf_release(q,buf);
632                 buf->baddr = b->m.userptr;
633                 break;
634         case V4L2_MEMORY_OVERLAY:
635                 buf->boff = b->m.offset;
636                 break;
637         default:
638                 goto done;
639         }
640
641         field = videobuf_next_field(q);
642         retval = q->ops->buf_prepare(q,buf,field);
643         if (0 != retval)
644                 goto done;
645
646         list_add_tail(&buf->stream,&q->stream);
647         if (q->streaming) {
648                 spin_lock_irqsave(q->irqlock,flags);
649                 q->ops->buf_queue(q,buf);
650                 spin_unlock_irqrestore(q->irqlock,flags);
651         }
652         retval = 0;
653
654  done:
655         mutex_unlock(&q->lock);
656         return retval;
657 }
658
659 int
660 videobuf_dqbuf(struct videobuf_queue *q,
661                struct v4l2_buffer *b, int nonblocking)
662 {
663         struct videobuf_buffer *buf;
664         int retval;
665
666         mutex_lock(&q->lock);
667         retval = -EBUSY;
668         if (q->reading)
669                 goto done;
670         retval = -EINVAL;
671         if (b->type != q->type)
672                 goto done;
673         if (list_empty(&q->stream))
674                 goto done;
675         buf = list_entry(q->stream.next, struct videobuf_buffer, stream);
676         retval = videobuf_waiton(buf, nonblocking, 1);
677         if (retval < 0)
678                 goto done;
679         switch (buf->state) {
680         case STATE_ERROR:
681                 retval = -EIO;
682                 /* fall through */
683         case STATE_DONE:
684                 videobuf_dma_pci_sync(q->pci,&buf->dma);
685                 buf->state = STATE_IDLE;
686                 break;
687         default:
688                 retval = -EINVAL;
689                 goto done;
690         }
691         list_del(&buf->stream);
692         memset(b,0,sizeof(*b));
693         videobuf_status(b,buf,q->type);
694
695  done:
696         mutex_unlock(&q->lock);
697         return retval;
698 }
699
700 int videobuf_streamon(struct videobuf_queue *q)
701 {
702         struct videobuf_buffer *buf;
703         struct list_head *list;
704         unsigned long flags;
705         int retval;
706
707         mutex_lock(&q->lock);
708         retval = -EBUSY;
709         if (q->reading)
710                 goto done;
711         retval = 0;
712         if (q->streaming)
713                 goto done;
714         q->streaming = 1;
715         spin_lock_irqsave(q->irqlock,flags);
716         list_for_each(list,&q->stream) {
717                 buf = list_entry(list, struct videobuf_buffer, stream);
718                 if (buf->state == STATE_PREPARED)
719                         q->ops->buf_queue(q,buf);
720         }
721         spin_unlock_irqrestore(q->irqlock,flags);
722
723  done:
724         mutex_unlock(&q->lock);
725         return retval;
726 }
727
728 int videobuf_streamoff(struct videobuf_queue *q)
729 {
730         int retval = -EINVAL;
731
732         mutex_lock(&q->lock);
733         if (!q->streaming)
734                 goto done;
735         videobuf_queue_cancel(q);
736         q->streaming = 0;
737         retval = 0;
738
739  done:
740         mutex_unlock(&q->lock);
741         return retval;
742 }
743
744 static ssize_t
745 videobuf_read_zerocopy(struct videobuf_queue *q, char __user *data,
746                        size_t count, loff_t *ppos)
747 {
748         enum v4l2_field field;
749         unsigned long flags;
750         int retval;
751
752         /* setup stuff */
753         q->read_buf = videobuf_alloc(q->msize);
754         if (NULL == q->read_buf)
755                 return -ENOMEM;
756
757         q->read_buf->memory = V4L2_MEMORY_USERPTR;
758         q->read_buf->baddr  = (unsigned long)data;
759         q->read_buf->bsize  = count;
760         field = videobuf_next_field(q);
761         retval = q->ops->buf_prepare(q,q->read_buf,field);
762         if (0 != retval)
763                 goto done;
764
765         /* start capture & wait */
766         spin_lock_irqsave(q->irqlock,flags);
767         q->ops->buf_queue(q,q->read_buf);
768         spin_unlock_irqrestore(q->irqlock,flags);
769         retval = videobuf_waiton(q->read_buf,0,0);
770         if (0 == retval) {
771                 videobuf_dma_pci_sync(q->pci,&q->read_buf->dma);
772                 if (STATE_ERROR == q->read_buf->state)
773                         retval = -EIO;
774                 else
775                         retval = q->read_buf->size;
776         }
777
778  done:
779         /* cleanup */
780         q->ops->buf_release(q,q->read_buf);
781         kfree(q->read_buf);
782         q->read_buf = NULL;
783         return retval;
784 }
785
786 ssize_t videobuf_read_one(struct videobuf_queue *q,
787                           char __user *data, size_t count, loff_t *ppos,
788                           int nonblocking)
789 {
790         enum v4l2_field field;
791         unsigned long flags;
792         unsigned size, nbufs, bytes;
793         int retval;
794
795         mutex_lock(&q->lock);
796
797         nbufs = 1; size = 0;
798         q->ops->buf_setup(q,&nbufs,&size);
799         if (NULL == q->read_buf  &&
800             count >= size        &&
801             !nonblocking) {
802                 retval = videobuf_read_zerocopy(q,data,count,ppos);
803                 if (retval >= 0  ||  retval == -EIO)
804                         /* ok, all done */
805                         goto done;
806                 /* fallback to kernel bounce buffer on failures */
807         }
808
809         if (NULL == q->read_buf) {
810                 /* need to capture a new frame */
811                 retval = -ENOMEM;
812                 q->read_buf = videobuf_alloc(q->msize);
813                 if (NULL == q->read_buf)
814                         goto done;
815                 q->read_buf->memory = V4L2_MEMORY_USERPTR;
816                 q->read_buf->bsize = count; /* preferred size */
817                 field = videobuf_next_field(q);
818                 retval = q->ops->buf_prepare(q,q->read_buf,field);
819                 if (0 != retval) {
820                         kfree (q->read_buf);
821                         q->read_buf = NULL;
822                         goto done;
823                 }
824                 spin_lock_irqsave(q->irqlock,flags);
825                 q->ops->buf_queue(q,q->read_buf);
826                 spin_unlock_irqrestore(q->irqlock,flags);
827                 q->read_off = 0;
828         }
829
830         /* wait until capture is done */
831         retval = videobuf_waiton(q->read_buf, nonblocking, 1);
832         if (0 != retval)
833                 goto done;
834         videobuf_dma_pci_sync(q->pci,&q->read_buf->dma);
835
836         if (STATE_ERROR == q->read_buf->state) {
837                 /* catch I/O errors */
838                 q->ops->buf_release(q,q->read_buf);
839                 kfree(q->read_buf);
840                 q->read_buf = NULL;
841                 retval = -EIO;
842                 goto done;
843         }
844
845         /* copy to userspace */
846         bytes = count;
847         if (bytes > q->read_buf->size - q->read_off)
848                 bytes = q->read_buf->size - q->read_off;
849         retval = -EFAULT;
850         if (copy_to_user(data, q->read_buf->dma.vmalloc+q->read_off, bytes))
851                 goto done;
852
853         retval = bytes;
854         q->read_off += bytes;
855         if (q->read_off == q->read_buf->size) {
856                 /* all data copied, cleanup */
857                 q->ops->buf_release(q,q->read_buf);
858                 kfree(q->read_buf);
859                 q->read_buf = NULL;
860         }
861
862  done:
863         mutex_unlock(&q->lock);
864         return retval;
865 }
866
867 int videobuf_read_start(struct videobuf_queue *q)
868 {
869         enum v4l2_field field;
870         unsigned long flags;
871         int count = 0, size = 0;
872         int err, i;
873
874         q->ops->buf_setup(q,&count,&size);
875         if (count < 2)
876                 count = 2;
877         if (count > VIDEO_MAX_FRAME)
878                 count = VIDEO_MAX_FRAME;
879         size = PAGE_ALIGN(size);
880
881         err = videobuf_mmap_setup(q, count, size, V4L2_MEMORY_USERPTR);
882         if (err)
883                 return err;
884         for (i = 0; i < count; i++) {
885                 field = videobuf_next_field(q);
886                 err = q->ops->buf_prepare(q,q->bufs[i],field);
887                 if (err)
888                         return err;
889                 list_add_tail(&q->bufs[i]->stream, &q->stream);
890         }
891         spin_lock_irqsave(q->irqlock,flags);
892         for (i = 0; i < count; i++)
893                 q->ops->buf_queue(q,q->bufs[i]);
894         spin_unlock_irqrestore(q->irqlock,flags);
895         q->reading = 1;
896         return 0;
897 }
898
899 void videobuf_read_stop(struct videobuf_queue *q)
900 {
901         int i;
902
903         videobuf_queue_cancel(q);
904         videobuf_mmap_free(q);
905         INIT_LIST_HEAD(&q->stream);
906         for (i = 0; i < VIDEO_MAX_FRAME; i++) {
907                 if (NULL == q->bufs[i])
908                         continue;
909                 kfree(q->bufs[i]);
910                 q->bufs[i] = NULL;
911         }
912         q->read_buf = NULL;
913         q->reading  = 0;
914 }
915
916 ssize_t videobuf_read_stream(struct videobuf_queue *q,
917                              char __user *data, size_t count, loff_t *ppos,
918                              int vbihack, int nonblocking)
919 {
920         unsigned int *fc, bytes;
921         int err, retval;
922         unsigned long flags;
923
924         dprintk(2,"%s\n",__FUNCTION__);
925         mutex_lock(&q->lock);
926         retval = -EBUSY;
927         if (q->streaming)
928                 goto done;
929         if (!q->reading) {
930                 retval = videobuf_read_start(q);
931                 if (retval < 0)
932                         goto done;
933         }
934
935         retval = 0;
936         while (count > 0) {
937                 /* get / wait for data */
938                 if (NULL == q->read_buf) {
939                         q->read_buf = list_entry(q->stream.next,
940                                                  struct videobuf_buffer,
941                                                  stream);
942                         list_del(&q->read_buf->stream);
943                         q->read_off = 0;
944                 }
945                 err = videobuf_waiton(q->read_buf, nonblocking, 1);
946                 if (err < 0) {
947                         if (0 == retval)
948                                 retval = err;
949                         break;
950                 }
951
952                 if (q->read_buf->state == STATE_DONE) {
953                         if (vbihack) {
954                                 /* dirty, undocumented hack -- pass the frame counter
955                                  * within the last four bytes of each vbi data block.
956                                  * We need that one to maintain backward compatibility
957                                  * to all vbi decoding software out there ... */
958                                 fc  = (unsigned int*)q->read_buf->dma.vmalloc;
959                                 fc += (q->read_buf->size>>2) -1;
960                                 *fc = q->read_buf->field_count >> 1;
961                                 dprintk(1,"vbihack: %d\n",*fc);
962                         }
963
964                         /* copy stuff */
965                         bytes = count;
966                         if (bytes > q->read_buf->size - q->read_off)
967                                 bytes = q->read_buf->size - q->read_off;
968                         if (copy_to_user(data + retval,
969                                          q->read_buf->dma.vmalloc + q->read_off,
970                                          bytes)) {
971                                 if (0 == retval)
972                                         retval = -EFAULT;
973                                 break;
974                         }
975                         count       -= bytes;
976                         retval      += bytes;
977                         q->read_off += bytes;
978                 } else {
979                         /* some error */
980                         q->read_off = q->read_buf->size;
981                         if (0 == retval)
982                                 retval = -EIO;
983                 }
984
985                 /* requeue buffer when done with copying */
986                 if (q->read_off == q->read_buf->size) {
987                         list_add_tail(&q->read_buf->stream,
988                                       &q->stream);
989                         spin_lock_irqsave(q->irqlock,flags);
990                         q->ops->buf_queue(q,q->read_buf);
991                         spin_unlock_irqrestore(q->irqlock,flags);
992                         q->read_buf = NULL;
993                 }
994                 if (retval < 0)
995                         break;
996         }
997
998  done:
999         mutex_unlock(&q->lock);
1000         return retval;
1001 }
1002
1003 unsigned int videobuf_poll_stream(struct file *file,
1004                                   struct videobuf_queue *q,
1005                                   poll_table *wait)
1006 {
1007         struct videobuf_buffer *buf = NULL;
1008         unsigned int rc = 0;
1009
1010         mutex_lock(&q->lock);
1011         if (q->streaming) {
1012                 if (!list_empty(&q->stream))
1013                         buf = list_entry(q->stream.next,
1014                                          struct videobuf_buffer, stream);
1015         } else {
1016                 if (!q->reading)
1017                         videobuf_read_start(q);
1018                 if (!q->reading) {
1019                         rc = POLLERR;
1020                 } else if (NULL == q->read_buf) {
1021                         q->read_buf = list_entry(q->stream.next,
1022                                                  struct videobuf_buffer,
1023                                                  stream);
1024                         list_del(&q->read_buf->stream);
1025                         q->read_off = 0;
1026                 }
1027                 buf = q->read_buf;
1028         }
1029         if (!buf)
1030                 rc = POLLERR;
1031
1032         if (0 == rc) {
1033                 poll_wait(file, &buf->done, wait);
1034                 if (buf->state == STATE_DONE ||
1035                     buf->state == STATE_ERROR)
1036                         rc = POLLIN|POLLRDNORM;
1037         }
1038         mutex_unlock(&q->lock);
1039         return rc;
1040 }
1041
1042 /* --------------------------------------------------------------------- */
1043
1044 static void
1045 videobuf_vm_open(struct vm_area_struct *vma)
1046 {
1047         struct videobuf_mapping *map = vma->vm_private_data;
1048
1049         dprintk(2,"vm_open %p [count=%d,vma=%08lx-%08lx]\n",map,
1050                 map->count,vma->vm_start,vma->vm_end);
1051         map->count++;
1052 }
1053
1054 static void
1055 videobuf_vm_close(struct vm_area_struct *vma)
1056 {
1057         struct videobuf_mapping *map = vma->vm_private_data;
1058         struct videobuf_queue *q = map->q;
1059         int i;
1060
1061         dprintk(2,"vm_close %p [count=%d,vma=%08lx-%08lx]\n",map,
1062                 map->count,vma->vm_start,vma->vm_end);
1063
1064         map->count--;
1065         if (0 == map->count) {
1066                 dprintk(1,"munmap %p q=%p\n",map,q);
1067                 mutex_lock(&q->lock);
1068                 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1069                         if (NULL == q->bufs[i])
1070                                 continue;
1071                         if (q->bufs[i])
1072                                 ;
1073                         if (q->bufs[i]->map != map)
1074                                 continue;
1075                         q->bufs[i]->map   = NULL;
1076                         q->bufs[i]->baddr = 0;
1077                         q->ops->buf_release(q,q->bufs[i]);
1078                 }
1079                 mutex_unlock(&q->lock);
1080                 kfree(map);
1081         }
1082         return;
1083 }
1084
1085 /*
1086  * Get a anonymous page for the mapping.  Make sure we can DMA to that
1087  * memory location with 32bit PCI devices (i.e. don't use highmem for
1088  * now ...).  Bounce buffers don't work very well for the data rates
1089  * video capture has.
1090  */
1091 static struct page*
1092 videobuf_vm_nopage(struct vm_area_struct *vma, unsigned long vaddr,
1093                    int *type)
1094 {
1095         struct page *page;
1096
1097         dprintk(3,"nopage: fault @ %08lx [vma %08lx-%08lx]\n",
1098                 vaddr,vma->vm_start,vma->vm_end);
1099         if (vaddr > vma->vm_end)
1100                 return NOPAGE_SIGBUS;
1101         page = alloc_page(GFP_USER);
1102         if (!page)
1103                 return NOPAGE_OOM;
1104         clear_user_page(page_address(page), vaddr, page);
1105         if (type)
1106                 *type = VM_FAULT_MINOR;
1107         return page;
1108 }
1109
1110 static struct vm_operations_struct videobuf_vm_ops =
1111 {
1112         .open     = videobuf_vm_open,
1113         .close    = videobuf_vm_close,
1114         .nopage   = videobuf_vm_nopage,
1115 };
1116
1117 int videobuf_mmap_setup(struct videobuf_queue *q,
1118                         unsigned int bcount, unsigned int bsize,
1119                         enum v4l2_memory memory)
1120 {
1121         unsigned int i;
1122         int err;
1123
1124         err = videobuf_mmap_free(q);
1125         if (0 != err)
1126                 return err;
1127
1128         for (i = 0; i < bcount; i++) {
1129                 q->bufs[i] = videobuf_alloc(q->msize);
1130                 q->bufs[i]->i      = i;
1131                 q->bufs[i]->input  = UNSET;
1132                 q->bufs[i]->memory = memory;
1133                 q->bufs[i]->bsize  = bsize;
1134                 switch (memory) {
1135                 case V4L2_MEMORY_MMAP:
1136                         q->bufs[i]->boff  = bsize * i;
1137                         break;
1138                 case V4L2_MEMORY_USERPTR:
1139                 case V4L2_MEMORY_OVERLAY:
1140                         /* nothing */
1141                         break;
1142                 }
1143         }
1144         dprintk(1,"mmap setup: %d buffers, %d bytes each\n",
1145                 bcount,bsize);
1146         return 0;
1147 }
1148
1149 int videobuf_mmap_free(struct videobuf_queue *q)
1150 {
1151         int i;
1152
1153         for (i = 0; i < VIDEO_MAX_FRAME; i++)
1154                 if (q->bufs[i] && q->bufs[i]->map)
1155                         return -EBUSY;
1156         for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1157                 if (NULL == q->bufs[i])
1158                         continue;
1159                 q->ops->buf_release(q,q->bufs[i]);
1160                 kfree(q->bufs[i]);
1161                 q->bufs[i] = NULL;
1162         }
1163         return 0;
1164 }
1165
1166 int videobuf_mmap_mapper(struct videobuf_queue *q,
1167                          struct vm_area_struct *vma)
1168 {
1169         struct videobuf_mapping *map;
1170         unsigned int first,last,size,i;
1171         int retval;
1172
1173         mutex_lock(&q->lock);
1174         retval = -EINVAL;
1175         if (!(vma->vm_flags & VM_WRITE)) {
1176                 dprintk(1,"mmap app bug: PROT_WRITE please\n");
1177                 goto done;
1178         }
1179         if (!(vma->vm_flags & VM_SHARED)) {
1180                 dprintk(1,"mmap app bug: MAP_SHARED please\n");
1181                 goto done;
1182         }
1183
1184         /* look for first buffer to map */
1185         for (first = 0; first < VIDEO_MAX_FRAME; first++) {
1186                 if (NULL == q->bufs[first])
1187                         continue;
1188                 if (V4L2_MEMORY_MMAP != q->bufs[first]->memory)
1189                         continue;
1190                 if (q->bufs[first]->boff == (vma->vm_pgoff << PAGE_SHIFT))
1191                         break;
1192         }
1193         if (VIDEO_MAX_FRAME == first) {
1194                 dprintk(1,"mmap app bug: offset invalid [offset=0x%lx]\n",
1195                         (vma->vm_pgoff << PAGE_SHIFT));
1196                 goto done;
1197         }
1198
1199         /* look for last buffer to map */
1200         for (size = 0, last = first; last < VIDEO_MAX_FRAME; last++) {
1201                 if (NULL == q->bufs[last])
1202                         continue;
1203                 if (V4L2_MEMORY_MMAP != q->bufs[last]->memory)
1204                         continue;
1205                 if (q->bufs[last]->map) {
1206                         retval = -EBUSY;
1207                         goto done;
1208                 }
1209                 size += q->bufs[last]->bsize;
1210                 if (size == (vma->vm_end - vma->vm_start))
1211                         break;
1212         }
1213         if (VIDEO_MAX_FRAME == last) {
1214                 dprintk(1,"mmap app bug: size invalid [size=0x%lx]\n",
1215                         (vma->vm_end - vma->vm_start));
1216                 goto done;
1217         }
1218
1219         /* create mapping + update buffer list */
1220         retval = -ENOMEM;
1221         map = kmalloc(sizeof(struct videobuf_mapping),GFP_KERNEL);
1222         if (NULL == map)
1223                 goto done;
1224         for (size = 0, i = first; i <= last; size += q->bufs[i++]->bsize) {
1225                 q->bufs[i]->map   = map;
1226                 q->bufs[i]->baddr = vma->vm_start + size;
1227         }
1228         map->count    = 1;
1229         map->start    = vma->vm_start;
1230         map->end      = vma->vm_end;
1231         map->q        = q;
1232         vma->vm_ops   = &videobuf_vm_ops;
1233         vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED;
1234         vma->vm_flags &= ~VM_IO; /* using shared anonymous pages */
1235         vma->vm_private_data = map;
1236         dprintk(1,"mmap %p: q=%p %08lx-%08lx pgoff %08lx bufs %d-%d\n",
1237                 map,q,vma->vm_start,vma->vm_end,vma->vm_pgoff,first,last);
1238         retval = 0;
1239
1240  done:
1241         mutex_unlock(&q->lock);
1242         return retval;
1243 }
1244
1245 /* --------------------------------------------------------------------- */
1246
1247 EXPORT_SYMBOL_GPL(videobuf_vmalloc_to_sg);
1248
1249 EXPORT_SYMBOL_GPL(videobuf_dma_init);
1250 EXPORT_SYMBOL_GPL(videobuf_dma_init_user);
1251 EXPORT_SYMBOL_GPL(videobuf_dma_init_kernel);
1252 EXPORT_SYMBOL_GPL(videobuf_dma_init_overlay);
1253 EXPORT_SYMBOL_GPL(videobuf_dma_pci_map);
1254 EXPORT_SYMBOL_GPL(videobuf_dma_pci_sync);
1255 EXPORT_SYMBOL_GPL(videobuf_dma_pci_unmap);
1256 EXPORT_SYMBOL_GPL(videobuf_dma_free);
1257
1258 EXPORT_SYMBOL_GPL(videobuf_alloc);
1259 EXPORT_SYMBOL_GPL(videobuf_waiton);
1260 EXPORT_SYMBOL_GPL(videobuf_iolock);
1261
1262 EXPORT_SYMBOL_GPL(videobuf_queue_init);
1263 EXPORT_SYMBOL_GPL(videobuf_queue_cancel);
1264 EXPORT_SYMBOL_GPL(videobuf_queue_is_busy);
1265
1266 EXPORT_SYMBOL_GPL(videobuf_next_field);
1267 EXPORT_SYMBOL_GPL(videobuf_status);
1268 EXPORT_SYMBOL_GPL(videobuf_reqbufs);
1269 EXPORT_SYMBOL_GPL(videobuf_querybuf);
1270 EXPORT_SYMBOL_GPL(videobuf_qbuf);
1271 EXPORT_SYMBOL_GPL(videobuf_dqbuf);
1272 EXPORT_SYMBOL_GPL(videobuf_streamon);
1273 EXPORT_SYMBOL_GPL(videobuf_streamoff);
1274
1275 EXPORT_SYMBOL_GPL(videobuf_read_start);
1276 EXPORT_SYMBOL_GPL(videobuf_read_stop);
1277 EXPORT_SYMBOL_GPL(videobuf_read_stream);
1278 EXPORT_SYMBOL_GPL(videobuf_read_one);
1279 EXPORT_SYMBOL_GPL(videobuf_poll_stream);
1280
1281 EXPORT_SYMBOL_GPL(videobuf_mmap_setup);
1282 EXPORT_SYMBOL_GPL(videobuf_mmap_free);
1283 EXPORT_SYMBOL_GPL(videobuf_mmap_mapper);
1284
1285 /*
1286  * Local variables:
1287  * c-basic-offset: 8
1288  * End:
1289  */