Commit c9e80989 authored by Gerd Knorr's avatar Gerd Knorr Committed by Linus Torvalds

[PATCH] v4l: videobuf update

This updates the videobuf module used by the bttv, saa7134 and saa7146
drivers.

Changes:
  * adds support for overlays (i.e. PCI-PCI DMA transfers).
  * adds support for userspace DMA.
  * fixes bug in the read() helper function (catch I/O errors).
parent 367e82de
...@@ -197,6 +197,20 @@ int videobuf_dma_init_kernel(struct videobuf_dmabuf *dma, int direction, ...@@ -197,6 +197,20 @@ int videobuf_dma_init_kernel(struct videobuf_dmabuf *dma, int direction,
return 0; return 0;
} }
int videobuf_dma_init_overlay(struct videobuf_dmabuf *dma, int direction,
dma_addr_t addr, int nr_pages)
{
dprintk(1,"init overlay [%d pages @ bus 0x%lx]\n",
nr_pages,(unsigned long)addr);
dma->direction = direction;
if (0 == addr)
return -EINVAL;
dma->bus_addr = addr;
dma->nr_pages = nr_pages;
return 0;
}
int videobuf_dma_pci_map(struct pci_dev *dev, struct videobuf_dmabuf *dma) int videobuf_dma_pci_map(struct pci_dev *dev, struct videobuf_dmabuf *dma)
{ {
int err; int err;
...@@ -218,10 +232,21 @@ int videobuf_dma_pci_map(struct pci_dev *dev, struct videobuf_dmabuf *dma) ...@@ -218,10 +232,21 @@ int videobuf_dma_pci_map(struct pci_dev *dev, struct videobuf_dmabuf *dma)
dma->sglist = videobuf_vmalloc_to_sg dma->sglist = videobuf_vmalloc_to_sg
(dma->vmalloc,dma->nr_pages); (dma->vmalloc,dma->nr_pages);
} }
if (dma->bus_addr) {
dma->sglist = kmalloc(sizeof(struct scatterlist), GFP_KERNEL);
if (NULL != dma->sglist) {
dma->sglen = 1;
sg_dma_address(&dma->sglist[0]) = dma->bus_addr & ~PAGE_MASK;
dma->sglist[0].offset = dma->bus_addr & PAGE_MASK;
sg_dma_len(&dma->sglist[0]) = dma->nr_pages * PAGE_SIZE;
}
}
if (NULL == dma->sglist) { if (NULL == dma->sglist) {
dprintk(1,"scatterlist is NULL\n"); dprintk(1,"scatterlist is NULL\n");
return -ENOMEM; return -ENOMEM;
} }
if (!dma->bus_addr)
dma->sglen = pci_map_sg(dev,dma->sglist,dma->nr_pages, dma->sglen = pci_map_sg(dev,dma->sglist,dma->nr_pages,
dma->direction); dma->direction);
return 0; return 0;
...@@ -232,6 +257,7 @@ int videobuf_dma_pci_sync(struct pci_dev *dev, struct videobuf_dmabuf *dma) ...@@ -232,6 +257,7 @@ int videobuf_dma_pci_sync(struct pci_dev *dev, struct videobuf_dmabuf *dma)
if (!dma->sglen) if (!dma->sglen)
BUG(); BUG();
if (!dma->bus_addr)
pci_dma_sync_sg(dev,dma->sglist,dma->nr_pages,dma->direction); pci_dma_sync_sg(dev,dma->sglist,dma->nr_pages,dma->direction);
return 0; return 0;
} }
...@@ -241,6 +267,7 @@ int videobuf_dma_pci_unmap(struct pci_dev *dev, struct videobuf_dmabuf *dma) ...@@ -241,6 +267,7 @@ int videobuf_dma_pci_unmap(struct pci_dev *dev, struct videobuf_dmabuf *dma)
if (!dma->sglen) if (!dma->sglen)
return 0; return 0;
if (!dma->bus_addr)
pci_unmap_sg(dev,dma->sglist,dma->nr_pages,dma->direction); pci_unmap_sg(dev,dma->sglist,dma->nr_pages,dma->direction);
kfree(dma->sglist); kfree(dma->sglist);
dma->sglist = NULL; dma->sglist = NULL;
...@@ -266,6 +293,9 @@ int videobuf_dma_free(struct videobuf_dmabuf *dma) ...@@ -266,6 +293,9 @@ int videobuf_dma_free(struct videobuf_dmabuf *dma)
vfree(dma->vmalloc); vfree(dma->vmalloc);
dma->vmalloc = NULL; dma->vmalloc = NULL;
} }
if (dma->bus_addr) {
dma->bus_addr = 0;
}
dma->direction = PCI_DMA_NONE; dma->direction = PCI_DMA_NONE;
return 0; return 0;
} }
...@@ -309,10 +339,15 @@ int videobuf_waiton(struct videobuf_buffer *vb, int non_blocking, int intr) ...@@ -309,10 +339,15 @@ int videobuf_waiton(struct videobuf_buffer *vb, int non_blocking, int intr)
} }
int int
videobuf_iolock(struct pci_dev *pci, struct videobuf_buffer *vb) videobuf_iolock(struct pci_dev *pci, struct videobuf_buffer *vb,
struct v4l2_framebuffer *fbuf)
{ {
int err,pages; int err,pages;
dma_addr_t bus;
switch (vb->memory) {
case V4L2_MEMORY_MMAP:
case V4L2_MEMORY_USERPTR:
if (0 == vb->baddr) { if (0 == vb->baddr) {
/* no userspace addr -- kernel bounce buffer */ /* no userspace addr -- kernel bounce buffer */
pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT; pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
...@@ -327,9 +362,25 @@ videobuf_iolock(struct pci_dev *pci, struct videobuf_buffer *vb) ...@@ -327,9 +362,25 @@ videobuf_iolock(struct pci_dev *pci, struct videobuf_buffer *vb)
if (0 != err) if (0 != err)
return err; return err;
} }
break;
case V4L2_MEMORY_OVERLAY:
if (NULL == fbuf)
return -EINVAL;
/* FIXME: need sanity checks for vb->boff */
bus = (dma_addr_t)fbuf->base + vb->boff;
pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
err = videobuf_dma_init_overlay(&vb->dma,PCI_DMA_FROMDEVICE,
bus, pages);
if (0 != err)
return err;
break;
default:
BUG();
}
err = videobuf_dma_pci_map(pci,&vb->dma); err = videobuf_dma_pci_map(pci,&vb->dma);
if (0 != err) if (0 != err)
return err; return err;
return 0; return 0;
} }
...@@ -447,11 +498,26 @@ videobuf_status(struct v4l2_buffer *b, struct videobuf_buffer *vb, ...@@ -447,11 +498,26 @@ videobuf_status(struct v4l2_buffer *b, struct videobuf_buffer *vb,
{ {
b->index = vb->i; b->index = vb->i;
b->type = type; b->type = type;
b->memory = vb->memory;
switch (b->memory) {
case V4L2_MEMORY_MMAP:
b->m.offset = vb->boff; b->m.offset = vb->boff;
b->length = vb->bsize; b->length = vb->bsize;
break;
case V4L2_MEMORY_USERPTR:
b->m.userptr = vb->baddr;
b->length = vb->bsize;
break;
case V4L2_MEMORY_OVERLAY:
b->m.offset = vb->boff;
break;
}
b->flags = 0; b->flags = 0;
if (vb->map) if (vb->map)
b->flags |= V4L2_BUF_FLAG_MAPPED; b->flags |= V4L2_BUF_FLAG_MAPPED;
switch (vb->state) { switch (vb->state) {
case STATE_PREPARED: case STATE_PREPARED:
case STATE_QUEUED: case STATE_QUEUED:
...@@ -467,6 +533,7 @@ videobuf_status(struct v4l2_buffer *b, struct videobuf_buffer *vb, ...@@ -467,6 +533,7 @@ videobuf_status(struct v4l2_buffer *b, struct videobuf_buffer *vb,
/* nothing */ /* nothing */
break; break;
} }
b->field = vb->field; b->field = vb->field;
b->timestamp = vb->ts; b->timestamp = vb->ts;
b->bytesused = vb->size; b->bytesused = vb->size;
...@@ -484,6 +551,10 @@ videobuf_reqbufs(struct file *file, struct videobuf_queue *q, ...@@ -484,6 +551,10 @@ videobuf_reqbufs(struct file *file, struct videobuf_queue *q,
return -EINVAL; return -EINVAL;
if (req->count < 1) if (req->count < 1)
return -EINVAL; return -EINVAL;
if (req->memory != V4L2_MEMORY_MMAP &&
req->memory != V4L2_MEMORY_USERPTR &&
req->memory != V4L2_MEMORY_OVERLAY)
return -EINVAL;
down(&q->lock); down(&q->lock);
count = req->count; count = req->count;
...@@ -495,10 +566,10 @@ videobuf_reqbufs(struct file *file, struct videobuf_queue *q, ...@@ -495,10 +566,10 @@ videobuf_reqbufs(struct file *file, struct videobuf_queue *q,
dprintk(1,"reqbufs: bufs=%d, size=0x%x [%d pages total]\n", dprintk(1,"reqbufs: bufs=%d, size=0x%x [%d pages total]\n",
count, size, (count*size)>>PAGE_SHIFT); count, size, (count*size)>>PAGE_SHIFT);
retval = videobuf_mmap_setup(file,q,count,size); retval = videobuf_mmap_setup(file,q,count,size,req->memory);
if (retval < 0) if (retval < 0)
goto done; goto done;
req->type = q->type;
req->count = count; req->count = count;
done: done:
...@@ -540,12 +611,29 @@ videobuf_qbuf(struct file *file, struct videobuf_queue *q, ...@@ -540,12 +611,29 @@ videobuf_qbuf(struct file *file, struct videobuf_queue *q,
buf = q->bufs[b->index]; buf = q->bufs[b->index];
if (NULL == buf) if (NULL == buf)
goto done; goto done;
if (0 == buf->baddr) if (buf->memory != b->memory)
goto done; goto done;
if (buf->state == STATE_QUEUED || if (buf->state == STATE_QUEUED ||
buf->state == STATE_ACTIVE) buf->state == STATE_ACTIVE)
goto done; goto done;
switch (b->memory) {
case V4L2_MEMORY_MMAP:
if (0 == buf->baddr)
goto done;
break;
case V4L2_MEMORY_USERPTR:
if (b->length < buf->bsize)
goto done;
buf->baddr = b->m.userptr;
break;
case V4L2_MEMORY_OVERLAY:
buf->boff = b->m.offset;
break;
default:
goto done;
}
field = videobuf_next_field(q); field = videobuf_next_field(q);
retval = q->ops->buf_prepare(file,buf,field); retval = q->ops->buf_prepare(file,buf,field);
if (0 != retval) if (0 != retval)
...@@ -663,6 +751,7 @@ videobuf_read_zerocopy(struct file *file, struct videobuf_queue *q, ...@@ -663,6 +751,7 @@ videobuf_read_zerocopy(struct file *file, struct videobuf_queue *q,
if (NULL == q->read_buf) if (NULL == q->read_buf)
goto done; goto done;
q->read_buf->memory = V4L2_MEMORY_USERPTR;
q->read_buf->baddr = (unsigned long)data; q->read_buf->baddr = (unsigned long)data;
q->read_buf->bsize = count; q->read_buf->bsize = count;
field = videobuf_next_field(q); field = videobuf_next_field(q);
...@@ -719,6 +808,7 @@ ssize_t videobuf_read_one(struct file *file, struct videobuf_queue *q, ...@@ -719,6 +808,7 @@ ssize_t videobuf_read_one(struct file *file, struct videobuf_queue *q,
q->read_buf = videobuf_alloc(q->msize); q->read_buf = videobuf_alloc(q->msize);
if (NULL == q->read_buf) if (NULL == q->read_buf)
goto done; goto done;
q->read_buf->memory = V4L2_MEMORY_USERPTR;
field = videobuf_next_field(q); field = videobuf_next_field(q);
retval = q->ops->buf_prepare(file,q->read_buf,field); retval = q->ops->buf_prepare(file,q->read_buf,field);
if (0 != retval) if (0 != retval)
...@@ -780,7 +870,7 @@ int videobuf_read_start(struct file *file, struct videobuf_queue *q) ...@@ -780,7 +870,7 @@ int videobuf_read_start(struct file *file, struct videobuf_queue *q)
count = VIDEO_MAX_FRAME; count = VIDEO_MAX_FRAME;
size = PAGE_ALIGN(size); size = PAGE_ALIGN(size);
err = videobuf_mmap_setup(file, q, count, size); err = videobuf_mmap_setup(file, q, count, size, V4L2_MEMORY_USERPTR);
if (err) if (err)
return err; return err;
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
...@@ -850,6 +940,7 @@ ssize_t videobuf_read_stream(struct file *file, struct videobuf_queue *q, ...@@ -850,6 +940,7 @@ ssize_t videobuf_read_stream(struct file *file, struct videobuf_queue *q,
break; break;
} }
if (q->read_buf->state == STATE_DONE) {
if (vbihack) { if (vbihack) {
/* dirty, undocumented hack -- pass the frame counter /* dirty, undocumented hack -- pass the frame counter
* within the last four bytes of each vbi data block. * within the last four bytes of each vbi data block.
...@@ -875,6 +966,10 @@ ssize_t videobuf_read_stream(struct file *file, struct videobuf_queue *q, ...@@ -875,6 +966,10 @@ ssize_t videobuf_read_stream(struct file *file, struct videobuf_queue *q,
count -= bytes; count -= bytes;
retval += bytes; retval += bytes;
q->read_off += bytes; q->read_off += bytes;
} else {
/* some error -- skip buffer */
q->read_off = q->read_buf->size;
}
/* requeue buffer when done with copying */ /* requeue buffer when done with copying */
if (q->read_off == q->read_buf->size) { if (q->read_off == q->read_buf->size) {
...@@ -1004,7 +1099,8 @@ static struct vm_operations_struct videobuf_vm_ops = ...@@ -1004,7 +1099,8 @@ static struct vm_operations_struct videobuf_vm_ops =
}; };
int videobuf_mmap_setup(struct file *file, struct videobuf_queue *q, int videobuf_mmap_setup(struct file *file, struct videobuf_queue *q,
unsigned int bcount, unsigned int bsize) unsigned int bcount, unsigned int bsize,
enum v4l2_memory memory)
{ {
unsigned int i; unsigned int i;
int err; int err;
...@@ -1016,8 +1112,17 @@ int videobuf_mmap_setup(struct file *file, struct videobuf_queue *q, ...@@ -1016,8 +1112,17 @@ int videobuf_mmap_setup(struct file *file, struct videobuf_queue *q,
for (i = 0; i < bcount; i++) { for (i = 0; i < bcount; i++) {
q->bufs[i] = videobuf_alloc(q->msize); q->bufs[i] = videobuf_alloc(q->msize);
q->bufs[i]->i = i; q->bufs[i]->i = i;
q->bufs[i]->boff = bsize * i; q->bufs[i]->memory = memory;
q->bufs[i]->bsize = bsize; q->bufs[i]->bsize = bsize;
switch (memory) {
case V4L2_MEMORY_MMAP:
q->bufs[i]->boff = bsize * i;
break;
case V4L2_MEMORY_USERPTR:
case V4L2_MEMORY_OVERLAY:
/* nothing */
break;
};
} }
dprintk(1,"mmap setup: %d buffers, %d bytes each\n", dprintk(1,"mmap setup: %d buffers, %d bytes each\n",
bcount,bsize); bcount,bsize);
...@@ -1063,6 +1168,8 @@ int videobuf_mmap_mapper(struct vm_area_struct *vma, ...@@ -1063,6 +1168,8 @@ int videobuf_mmap_mapper(struct vm_area_struct *vma,
for (first = 0; first < VIDEO_MAX_FRAME; first++) { for (first = 0; first < VIDEO_MAX_FRAME; first++) {
if (NULL == q->bufs[first]) if (NULL == q->bufs[first])
continue; continue;
if (V4L2_MEMORY_MMAP != q->bufs[first]->memory)
continue;
if (q->bufs[first]->boff == (vma->vm_pgoff << PAGE_SHIFT)) if (q->bufs[first]->boff == (vma->vm_pgoff << PAGE_SHIFT))
break; break;
} }
...@@ -1076,6 +1183,8 @@ int videobuf_mmap_mapper(struct vm_area_struct *vma, ...@@ -1076,6 +1183,8 @@ int videobuf_mmap_mapper(struct vm_area_struct *vma,
for (size = 0, last = first; last < VIDEO_MAX_FRAME; last++) { for (size = 0, last = first; last < VIDEO_MAX_FRAME; last++) {
if (NULL == q->bufs[last]) if (NULL == q->bufs[last])
continue; continue;
if (V4L2_MEMORY_MMAP != q->bufs[last]->memory)
continue;
if (q->bufs[last]->map) { if (q->bufs[last]->map) {
retval = -EBUSY; retval = -EBUSY;
goto done; goto done;
...@@ -1124,6 +1233,7 @@ EXPORT_SYMBOL_GPL(videobuf_unlock); ...@@ -1124,6 +1233,7 @@ EXPORT_SYMBOL_GPL(videobuf_unlock);
EXPORT_SYMBOL_GPL(videobuf_dma_init_user); EXPORT_SYMBOL_GPL(videobuf_dma_init_user);
EXPORT_SYMBOL_GPL(videobuf_dma_init_kernel); EXPORT_SYMBOL_GPL(videobuf_dma_init_kernel);
EXPORT_SYMBOL_GPL(videobuf_dma_init_overlay);
EXPORT_SYMBOL_GPL(videobuf_dma_pci_map); EXPORT_SYMBOL_GPL(videobuf_dma_pci_map);
EXPORT_SYMBOL_GPL(videobuf_dma_pci_sync); EXPORT_SYMBOL_GPL(videobuf_dma_pci_sync);
EXPORT_SYMBOL_GPL(videobuf_dma_pci_unmap); EXPORT_SYMBOL_GPL(videobuf_dma_pci_unmap);
......
...@@ -43,17 +43,17 @@ int videobuf_unlock(struct page **pages, int nr_pages); ...@@ -43,17 +43,17 @@ int videobuf_unlock(struct page **pages, int nr_pages);
* A small set of helper functions to manage buffers (both userland * A small set of helper functions to manage buffers (both userland
* and kernel) for DMA. * and kernel) for DMA.
* *
* videobuf_init_*_dmabuf() * videobuf_dma_init_*()
* creates a buffer. The userland version takes a userspace * creates a buffer. The userland version takes a userspace
* pointer + length. The kernel version just wants the size and * pointer + length. The kernel version just wants the size and
* does memory allocation too using vmalloc_32(). * does memory allocation too using vmalloc_32().
* *
* videobuf_pci_*_dmabuf() * videobuf_dma_pci_*()
* see Documentation/DMA-mapping.txt, these functions to * see Documentation/DMA-mapping.txt, these functions to
* basically the same. The map function does also build a * basically the same. The map function does also build a
* scatterlist for the buffer (and unmap frees it ...) * scatterlist for the buffer (and unmap frees it ...)
* *
* videobuf_free_dmabuf() * videobuf_dma_free()
* no comment ... * no comment ...
* *
*/ */
...@@ -66,6 +66,9 @@ struct videobuf_dmabuf { ...@@ -66,6 +66,9 @@ struct videobuf_dmabuf {
/* for kernel buffers */ /* for kernel buffers */
void *vmalloc; void *vmalloc;
/* for overlay buffers (pci-pci dma) */
dma_addr_t bus_addr;
/* common */ /* common */
struct scatterlist *sglist; struct scatterlist *sglist;
int sglen; int sglen;
...@@ -77,6 +80,8 @@ int videobuf_dma_init_user(struct videobuf_dmabuf *dma, int direction, ...@@ -77,6 +80,8 @@ int videobuf_dma_init_user(struct videobuf_dmabuf *dma, int direction,
unsigned long data, unsigned long size); unsigned long data, unsigned long size);
int videobuf_dma_init_kernel(struct videobuf_dmabuf *dma, int direction, int videobuf_dma_init_kernel(struct videobuf_dmabuf *dma, int direction,
int nr_pages); int nr_pages);
int videobuf_dma_init_overlay(struct videobuf_dmabuf *dma, int direction,
dma_addr_t addr, int nr_pages);
int videobuf_dma_pci_map(struct pci_dev *dev, struct videobuf_dmabuf *dma); int videobuf_dma_pci_map(struct pci_dev *dev, struct videobuf_dmabuf *dma);
int videobuf_dma_pci_sync(struct pci_dev *dev, int videobuf_dma_pci_sync(struct pci_dev *dev,
struct videobuf_dmabuf *dma); struct videobuf_dmabuf *dma);
...@@ -133,6 +138,7 @@ struct videobuf_buffer { ...@@ -133,6 +138,7 @@ struct videobuf_buffer {
/* info about the buffer */ /* info about the buffer */
unsigned int width; unsigned int width;
unsigned int height; unsigned int height;
unsigned int bytesperline; /* use only if != 0 */
unsigned long size; unsigned long size;
enum v4l2_field field; enum v4l2_field field;
enum videobuf_state state; enum videobuf_state state;
...@@ -140,7 +146,8 @@ struct videobuf_buffer { ...@@ -140,7 +146,8 @@ struct videobuf_buffer {
struct list_head stream; /* QBUF/DQBUF list */ struct list_head stream; /* QBUF/DQBUF list */
/* for mmap'ed buffers */ /* for mmap'ed buffers */
size_t boff; /* buffer offset (mmap) */ enum v4l2_memory memory;
size_t boff; /* buffer offset (mmap + overlay) */
size_t bsize; /* buffer size */ size_t bsize; /* buffer size */
unsigned long baddr; /* buffer addr (userland ptr!) */ unsigned long baddr; /* buffer addr (userland ptr!) */
struct videobuf_mapping *map; struct videobuf_mapping *map;
...@@ -185,7 +192,8 @@ struct videobuf_queue { ...@@ -185,7 +192,8 @@ struct videobuf_queue {
void* videobuf_alloc(unsigned int size); void* videobuf_alloc(unsigned int size);
int videobuf_waiton(struct videobuf_buffer *vb, int non_blocking, int intr); int videobuf_waiton(struct videobuf_buffer *vb, int non_blocking, int intr);
int videobuf_iolock(struct pci_dev *pci, struct videobuf_buffer *vb); int videobuf_iolock(struct pci_dev *pci, struct videobuf_buffer *vb,
struct v4l2_framebuffer *fbuf);
void videobuf_queue_init(struct videobuf_queue *q, void videobuf_queue_init(struct videobuf_queue *q,
struct videobuf_queue_ops *ops, struct videobuf_queue_ops *ops,
...@@ -221,7 +229,8 @@ unsigned int videobuf_poll_stream(struct file *file, ...@@ -221,7 +229,8 @@ unsigned int videobuf_poll_stream(struct file *file,
poll_table *wait); poll_table *wait);
int videobuf_mmap_setup(struct file *file, struct videobuf_queue *q, int videobuf_mmap_setup(struct file *file, struct videobuf_queue *q,
unsigned int bcount, unsigned int bsize); unsigned int bcount, unsigned int bsize,
enum v4l2_memory memory);
int videobuf_mmap_free(struct file *file, struct videobuf_queue *q); int videobuf_mmap_free(struct file *file, struct videobuf_queue *q);
int videobuf_mmap_mapper(struct vm_area_struct *vma, int videobuf_mmap_mapper(struct vm_area_struct *vma,
struct videobuf_queue *q); struct videobuf_queue *q);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment