blob: 440c1a8e9c1ffc5ffa348b6783cb5b5570de80e0 [file] [log] [blame]
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001/*
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03002 * helper functions for SG DMA video4linux capture buffers
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03003 *
4 * The functions expect the hardware being able to scatter gatter
5 * (i.e. the buffers are not linear in physical memory, but fragmented
6 * into PAGE_SIZE chunks). They also assume the driver does not need
7 * to touch the video data.
8 *
9 * (c) 2007 Mauro Carvalho Chehab, <mchehab@infradead.org>
10 *
11 * Highly based on video-buf written originally by:
12 * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org>
13 * (c) 2006 Mauro Carvalho Chehab, <mchehab@infradead.org>
14 * (c) 2006 Ted Walther and John Sokol
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2
19 */
20
21#include <linux/init.h>
22#include <linux/module.h>
23#include <linux/moduleparam.h>
24#include <linux/slab.h>
25#include <linux/interrupt.h>
26
Guennadi Liakhovetski07051352008-04-22 14:42:13 -030027#include <linux/dma-mapping.h>
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030028#include <linux/vmalloc.h>
29#include <linux/pagemap.h>
Ralf Baechle11763602007-10-23 20:42:11 +020030#include <linux/scatterlist.h>
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030031#include <asm/page.h>
32#include <asm/pgtable.h>
33
34#include <media/videobuf-dma-sg.h>
35
36#define MAGIC_DMABUF 0x19721112
37#define MAGIC_SG_MEM 0x17890714
38
39#define MAGIC_CHECK(is,should) if (unlikely((is) != (should))) \
40 { printk(KERN_ERR "magic mismatch: %x (expected %x)\n",is,should); BUG(); }
41
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030042static int debug;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030043module_param(debug, int, 0644);
44
Guennadi Liakhovetski07051352008-04-22 14:42:13 -030045MODULE_DESCRIPTION("helper module to manage video4linux dma sg buffers");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030046MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
47MODULE_LICENSE("GPL");
48
49#define dprintk(level, fmt, arg...) if (debug >= level) \
50 printk(KERN_DEBUG "vbuf-sg: " fmt , ## arg)
51
52/* --------------------------------------------------------------------- */
53
54struct scatterlist*
55videobuf_vmalloc_to_sg(unsigned char *virt, int nr_pages)
56{
57 struct scatterlist *sglist;
58 struct page *pg;
59 int i;
60
61 sglist = kcalloc(nr_pages, sizeof(struct scatterlist), GFP_KERNEL);
62 if (NULL == sglist)
63 return NULL;
Jens Axboe45711f12007-10-22 21:19:53 +020064 sg_init_table(sglist, nr_pages);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030065 for (i = 0; i < nr_pages; i++, virt += PAGE_SIZE) {
66 pg = vmalloc_to_page(virt);
67 if (NULL == pg)
68 goto err;
69 BUG_ON(PageHighMem(pg));
Jens Axboe642f1492007-10-24 11:20:47 +020070 sg_set_page(&sglist[i], pg, PAGE_SIZE, 0);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030071 }
72 return sglist;
73
74 err:
75 kfree(sglist);
76 return NULL;
77}
78
79struct scatterlist*
80videobuf_pages_to_sg(struct page **pages, int nr_pages, int offset)
81{
82 struct scatterlist *sglist;
83 int i = 0;
84
85 if (NULL == pages[0])
86 return NULL;
87 sglist = kcalloc(nr_pages, sizeof(*sglist), GFP_KERNEL);
88 if (NULL == sglist)
89 return NULL;
Jens Axboe45711f12007-10-22 21:19:53 +020090 sg_init_table(sglist, nr_pages);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030091
92 if (NULL == pages[0])
93 goto nopage;
94 if (PageHighMem(pages[0]))
95 /* DMA to highmem pages might not work */
96 goto highmem;
Jens Axboe642f1492007-10-24 11:20:47 +020097 sg_set_page(&sglist[0], pages[0], PAGE_SIZE - offset, offset);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030098 for (i = 1; i < nr_pages; i++) {
99 if (NULL == pages[i])
100 goto nopage;
101 if (PageHighMem(pages[i]))
102 goto highmem;
Jens Axboe642f1492007-10-24 11:20:47 +0200103 sg_set_page(&sglist[i], pages[i], PAGE_SIZE, 0);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300104 }
105 return sglist;
106
107 nopage:
108 dprintk(2,"sgl: oops - no page\n");
109 kfree(sglist);
110 return NULL;
111
112 highmem:
113 dprintk(2,"sgl: oops - highmem page\n");
114 kfree(sglist);
115 return NULL;
116}
117
118/* --------------------------------------------------------------------- */
119
120struct videobuf_dmabuf *videobuf_to_dma (struct videobuf_buffer *buf)
121{
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300122 struct videobuf_dma_sg_memory *mem = buf->priv;
123 BUG_ON(!mem);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300124
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300125 MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300126
127 return &mem->dma;
128}
129
130void videobuf_dma_init(struct videobuf_dmabuf *dma)
131{
132 memset(dma,0,sizeof(*dma));
133 dma->magic = MAGIC_DMABUF;
134}
135
Maxim Levitsky99001322007-09-27 20:34:09 -0300136static int videobuf_dma_init_user_locked(struct videobuf_dmabuf *dma,
137 int direction, unsigned long data, unsigned long size)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300138{
139 unsigned long first,last;
140 int err, rw = 0;
141
142 dma->direction = direction;
143 switch (dma->direction) {
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300144 case DMA_FROM_DEVICE:
145 rw = READ;
146 break;
147 case DMA_TO_DEVICE:
148 rw = WRITE;
149 break;
150 default:
151 BUG();
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300152 }
153
154 first = (data & PAGE_MASK) >> PAGE_SHIFT;
155 last = ((data+size-1) & PAGE_MASK) >> PAGE_SHIFT;
156 dma->offset = data & ~PAGE_MASK;
157 dma->nr_pages = last-first+1;
158 dma->pages = kmalloc(dma->nr_pages * sizeof(struct page*),
159 GFP_KERNEL);
160 if (NULL == dma->pages)
161 return -ENOMEM;
162 dprintk(1,"init user [0x%lx+0x%lx => %d pages]\n",
163 data,size,dma->nr_pages);
164
165 dma->varea = (void *) data;
166
Maxim Levitsky99001322007-09-27 20:34:09 -0300167
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300168 err = get_user_pages(current,current->mm,
169 data & PAGE_MASK, dma->nr_pages,
170 rw == READ, 1, /* force */
171 dma->pages, NULL);
Maxim Levitsky99001322007-09-27 20:34:09 -0300172
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300173 if (err != dma->nr_pages) {
174 dma->nr_pages = (err >= 0) ? err : 0;
175 dprintk(1,"get_user_pages: err=%d [%d]\n",err,dma->nr_pages);
176 return err < 0 ? err : -EINVAL;
177 }
178 return 0;
179}
180
Maxim Levitsky99001322007-09-27 20:34:09 -0300181int videobuf_dma_init_user(struct videobuf_dmabuf *dma, int direction,
182 unsigned long data, unsigned long size)
183{
184 int ret;
185 down_read(&current->mm->mmap_sem);
186 ret = videobuf_dma_init_user_locked(dma, direction, data, size);
187 up_read(&current->mm->mmap_sem);
188
189 return ret;
190}
191
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300192int videobuf_dma_init_kernel(struct videobuf_dmabuf *dma, int direction,
193 int nr_pages)
194{
195 dprintk(1,"init kernel [%d pages]\n",nr_pages);
196 dma->direction = direction;
197 dma->vmalloc = vmalloc_32(nr_pages << PAGE_SHIFT);
198 if (NULL == dma->vmalloc) {
199 dprintk(1,"vmalloc_32(%d pages) failed\n",nr_pages);
200 return -ENOMEM;
201 }
202 dprintk(1,"vmalloc is at addr 0x%08lx, size=%d\n",
203 (unsigned long)dma->vmalloc,
204 nr_pages << PAGE_SHIFT);
205 memset(dma->vmalloc,0,nr_pages << PAGE_SHIFT);
206 dma->nr_pages = nr_pages;
207 return 0;
208}
209
210int videobuf_dma_init_overlay(struct videobuf_dmabuf *dma, int direction,
211 dma_addr_t addr, int nr_pages)
212{
213 dprintk(1,"init overlay [%d pages @ bus 0x%lx]\n",
214 nr_pages,(unsigned long)addr);
215 dma->direction = direction;
216 if (0 == addr)
217 return -EINVAL;
218
219 dma->bus_addr = addr;
220 dma->nr_pages = nr_pages;
221 return 0;
222}
223
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300224int videobuf_dma_map(struct videobuf_queue* q, struct videobuf_dmabuf *dma)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300225{
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300226 MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
227 BUG_ON(0 == dma->nr_pages);
228
229 if (dma->pages) {
230 dma->sglist = videobuf_pages_to_sg(dma->pages, dma->nr_pages,
231 dma->offset);
232 }
233 if (dma->vmalloc) {
234 dma->sglist = videobuf_vmalloc_to_sg
235 (dma->vmalloc,dma->nr_pages);
236 }
237 if (dma->bus_addr) {
238 dma->sglist = kmalloc(sizeof(struct scatterlist), GFP_KERNEL);
239 if (NULL != dma->sglist) {
240 dma->sglen = 1;
241 sg_dma_address(&dma->sglist[0]) = dma->bus_addr & PAGE_MASK;
242 dma->sglist[0].offset = dma->bus_addr & ~PAGE_MASK;
243 sg_dma_len(&dma->sglist[0]) = dma->nr_pages * PAGE_SIZE;
244 }
245 }
246 if (NULL == dma->sglist) {
247 dprintk(1,"scatterlist is NULL\n");
248 return -ENOMEM;
249 }
250 if (!dma->bus_addr) {
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300251 dma->sglen = dma_map_sg(q->dev, dma->sglist,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300252 dma->nr_pages, dma->direction);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300253 if (0 == dma->sglen) {
254 printk(KERN_WARNING
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300255 "%s: videobuf_map_sg failed\n",__func__);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300256 kfree(dma->sglist);
257 dma->sglist = NULL;
258 dma->sglen = 0;
259 return -EIO;
260 }
261 }
262 return 0;
263}
264
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300265int videobuf_dma_sync(struct videobuf_queue *q, struct videobuf_dmabuf *dma)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300266{
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300267 MAGIC_CHECK(dma->magic, MAGIC_DMABUF);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300268 BUG_ON(!dma->sglen);
269
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300270 dma_sync_sg_for_cpu(q->dev, dma->sglist, dma->nr_pages, dma->direction);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300271 return 0;
272}
273
274int videobuf_dma_unmap(struct videobuf_queue* q,struct videobuf_dmabuf *dma)
275{
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300276 MAGIC_CHECK(dma->magic, MAGIC_DMABUF);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300277 if (!dma->sglen)
278 return 0;
279
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300280 dma_unmap_sg(q->dev, dma->sglist, dma->nr_pages, dma->direction);
Mauro Carvalho Chehab5ddff432007-10-08 11:43:49 -0300281
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300282 kfree(dma->sglist);
283 dma->sglist = NULL;
284 dma->sglen = 0;
285 return 0;
286}
287
288int videobuf_dma_free(struct videobuf_dmabuf *dma)
289{
290 MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
291 BUG_ON(dma->sglen);
292
293 if (dma->pages) {
294 int i;
295 for (i=0; i < dma->nr_pages; i++)
296 page_cache_release(dma->pages[i]);
297 kfree(dma->pages);
298 dma->pages = NULL;
299 }
300
301 vfree(dma->vmalloc);
302 dma->vmalloc = NULL;
303 dma->varea = NULL;
304
305 if (dma->bus_addr) {
306 dma->bus_addr = 0;
307 }
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300308 dma->direction = DMA_NONE;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300309 return 0;
310}
311
312/* --------------------------------------------------------------------- */
313
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300314int videobuf_sg_dma_map(struct device *dev, struct videobuf_dmabuf *dma)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300315{
316 struct videobuf_queue q;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300317
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300318 q.dev = dev;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300319
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300320 return videobuf_dma_map(&q, dma);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300321}
322
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300323int videobuf_sg_dma_unmap(struct device *dev, struct videobuf_dmabuf *dma)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300324{
325 struct videobuf_queue q;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300326
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300327 q.dev = dev;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300328
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300329 return videobuf_dma_unmap(&q, dma);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300330}
331
332/* --------------------------------------------------------------------- */
333
334static void
335videobuf_vm_open(struct vm_area_struct *vma)
336{
337 struct videobuf_mapping *map = vma->vm_private_data;
338
339 dprintk(2,"vm_open %p [count=%d,vma=%08lx-%08lx]\n",map,
340 map->count,vma->vm_start,vma->vm_end);
341 map->count++;
342}
343
344static void
345videobuf_vm_close(struct vm_area_struct *vma)
346{
347 struct videobuf_mapping *map = vma->vm_private_data;
348 struct videobuf_queue *q = map->q;
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300349 struct videobuf_dma_sg_memory *mem;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300350 int i;
351
352 dprintk(2,"vm_close %p [count=%d,vma=%08lx-%08lx]\n",map,
353 map->count,vma->vm_start,vma->vm_end);
354
355 map->count--;
356 if (0 == map->count) {
357 dprintk(1,"munmap %p q=%p\n",map,q);
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300358 mutex_lock(&q->vb_lock);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300359 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
360 if (NULL == q->bufs[i])
361 continue;
362 mem=q->bufs[i]->priv;
363
364 if (!mem)
365 continue;
366
367 MAGIC_CHECK(mem->magic,MAGIC_SG_MEM);
368
Mauro Carvalho Chehab851c0c92007-09-27 18:25:44 -0300369 if (q->bufs[i]->map != map)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300370 continue;
Mauro Carvalho Chehab851c0c92007-09-27 18:25:44 -0300371 q->bufs[i]->map = NULL;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300372 q->bufs[i]->baddr = 0;
373 q->ops->buf_release(q,q->bufs[i]);
374 }
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300375 mutex_unlock(&q->vb_lock);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300376 kfree(map);
377 }
378 return;
379}
380
381/*
382 * Get a anonymous page for the mapping. Make sure we can DMA to that
383 * memory location with 32bit PCI devices (i.e. don't use highmem for
384 * now ...). Bounce buffers don't work very well for the data rates
385 * video capture has.
386 */
Nick Piggin105354a2007-12-07 17:57:38 -0300387static int
388videobuf_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300389{
390 struct page *page;
391
Nick Piggin105354a2007-12-07 17:57:38 -0300392 dprintk(3,"fault: fault @ %08lx [vma %08lx-%08lx]\n",
393 (unsigned long)vmf->virtual_address,vma->vm_start,vma->vm_end);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300394 page = alloc_page(GFP_USER | __GFP_DMA32);
395 if (!page)
Nick Piggin105354a2007-12-07 17:57:38 -0300396 return VM_FAULT_OOM;
Andrew Mortond172b8b2007-12-07 21:14:43 -0300397 clear_user_page(page_address(page), (unsigned long)vmf->virtual_address,
398 page);
Nick Piggin105354a2007-12-07 17:57:38 -0300399 vmf->page = page;
400 return 0;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300401}
402
403static struct vm_operations_struct videobuf_vm_ops =
404{
405 .open = videobuf_vm_open,
406 .close = videobuf_vm_close,
Nick Piggin105354a2007-12-07 17:57:38 -0300407 .fault = videobuf_vm_fault,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300408};
409
410/* ---------------------------------------------------------------------
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300411 * SG handlers for the generic methods
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300412 */
413
414/* Allocated area consists on 3 parts:
415 struct video_buffer
416 struct <driver>_buffer (cx88_buffer, saa7134_buf, ...)
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300417 struct videobuf_dma_sg_memory
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300418 */
419
420static void *__videobuf_alloc(size_t size)
421{
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300422 struct videobuf_dma_sg_memory *mem;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300423 struct videobuf_buffer *vb;
424
425 vb = kzalloc(size+sizeof(*mem),GFP_KERNEL);
426
427 mem = vb->priv = ((char *)vb)+size;
428 mem->magic=MAGIC_SG_MEM;
429
430 videobuf_dma_init(&mem->dma);
431
432 dprintk(1,"%s: allocated at %p(%ld+%ld) & %p(%ld)\n",
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300433 __func__,vb,(long)sizeof(*vb),(long)size-sizeof(*vb),
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300434 mem,(long)sizeof(*mem));
435
436 return vb;
437}
438
439static int __videobuf_iolock (struct videobuf_queue* q,
440 struct videobuf_buffer *vb,
441 struct v4l2_framebuffer *fbuf)
442{
443 int err,pages;
444 dma_addr_t bus;
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300445 struct videobuf_dma_sg_memory *mem = vb->priv;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300446 BUG_ON(!mem);
447
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300448 MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300449
450 switch (vb->memory) {
451 case V4L2_MEMORY_MMAP:
452 case V4L2_MEMORY_USERPTR:
453 if (0 == vb->baddr) {
454 /* no userspace addr -- kernel bounce buffer */
455 pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
456 err = videobuf_dma_init_kernel( &mem->dma,
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300457 DMA_FROM_DEVICE,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300458 pages );
459 if (0 != err)
460 return err;
Maxim Levitsky99001322007-09-27 20:34:09 -0300461 } else if (vb->memory == V4L2_MEMORY_USERPTR) {
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300462 /* dma directly to userspace */
463 err = videobuf_dma_init_user( &mem->dma,
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300464 DMA_FROM_DEVICE,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300465 vb->baddr,vb->bsize );
466 if (0 != err)
467 return err;
Maxim Levitsky99001322007-09-27 20:34:09 -0300468 } else {
469 /* NOTE: HACK: videobuf_iolock on V4L2_MEMORY_MMAP
470 buffers can only be called from videobuf_qbuf
471 we take current->mm->mmap_sem there, to prevent
472 locking inversion, so don't take it here */
473
474 err = videobuf_dma_init_user_locked(&mem->dma,
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300475 DMA_FROM_DEVICE,
Maxim Levitsky99001322007-09-27 20:34:09 -0300476 vb->baddr, vb->bsize);
477 if (0 != err)
478 return err;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300479 }
480 break;
481 case V4L2_MEMORY_OVERLAY:
482 if (NULL == fbuf)
483 return -EINVAL;
484 /* FIXME: need sanity checks for vb->boff */
485 /*
486 * Using a double cast to avoid compiler warnings when
487 * building for PAE. Compiler doesn't like direct casting
488 * of a 32 bit ptr to 64 bit integer.
489 */
490 bus = (dma_addr_t)(unsigned long)fbuf->base + vb->boff;
491 pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300492 err = videobuf_dma_init_overlay(&mem->dma, DMA_FROM_DEVICE,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300493 bus, pages);
494 if (0 != err)
495 return err;
496 break;
497 default:
498 BUG();
499 }
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300500 err = videobuf_dma_map(q, &mem->dma);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300501 if (0 != err)
502 return err;
503
504 return 0;
505}
506
507static int __videobuf_sync(struct videobuf_queue *q,
508 struct videobuf_buffer *buf)
509{
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300510 struct videobuf_dma_sg_memory *mem = buf->priv;
511 BUG_ON(!mem);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300512 MAGIC_CHECK(mem->magic,MAGIC_SG_MEM);
513
514 return videobuf_dma_sync(q,&mem->dma);
515}
516
517static int __videobuf_mmap_free(struct videobuf_queue *q)
518{
519 int i;
520
521 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
522 if (q->bufs[i]) {
Mauro Carvalho Chehab851c0c92007-09-27 18:25:44 -0300523 if (q->bufs[i]->map)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300524 return -EBUSY;
525 }
526 }
527
528 return 0;
529}
530
531static int __videobuf_mmap_mapper(struct videobuf_queue *q,
532 struct vm_area_struct *vma)
533{
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300534 struct videobuf_dma_sg_memory *mem;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300535 struct videobuf_mapping *map;
536 unsigned int first,last,size,i;
537 int retval;
538
539 retval = -EINVAL;
540 if (!(vma->vm_flags & VM_WRITE)) {
541 dprintk(1,"mmap app bug: PROT_WRITE please\n");
542 goto done;
543 }
544 if (!(vma->vm_flags & VM_SHARED)) {
545 dprintk(1,"mmap app bug: MAP_SHARED please\n");
546 goto done;
547 }
548
Brandon Philipsce540932008-04-02 18:10:59 -0300549 /* This function maintains backwards compatibility with V4L1 and will
550 * map more than one buffer if the vma length is equal to the combined
551 * size of multiple buffers than it will map them together. See
552 * VIDIOCGMBUF in the v4l spec
553 *
554 * TODO: Allow drivers to specify if they support this mode
555 */
556
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300557 /* look for first buffer to map */
558 for (first = 0; first < VIDEO_MAX_FRAME; first++) {
559 if (NULL == q->bufs[first])
560 continue;
561 mem=q->bufs[first]->priv;
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300562 BUG_ON(!mem);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300563 MAGIC_CHECK(mem->magic,MAGIC_SG_MEM);
564
565 if (V4L2_MEMORY_MMAP != q->bufs[first]->memory)
566 continue;
567 if (q->bufs[first]->boff == (vma->vm_pgoff << PAGE_SHIFT))
568 break;
569 }
570 if (VIDEO_MAX_FRAME == first) {
571 dprintk(1,"mmap app bug: offset invalid [offset=0x%lx]\n",
572 (vma->vm_pgoff << PAGE_SHIFT));
573 goto done;
574 }
575
576 /* look for last buffer to map */
577 for (size = 0, last = first; last < VIDEO_MAX_FRAME; last++) {
578 if (NULL == q->bufs[last])
579 continue;
580 if (V4L2_MEMORY_MMAP != q->bufs[last]->memory)
581 continue;
Mauro Carvalho Chehab851c0c92007-09-27 18:25:44 -0300582 if (q->bufs[last]->map) {
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300583 retval = -EBUSY;
584 goto done;
585 }
586 size += q->bufs[last]->bsize;
587 if (size == (vma->vm_end - vma->vm_start))
588 break;
589 }
590 if (VIDEO_MAX_FRAME == last) {
591 dprintk(1,"mmap app bug: size invalid [size=0x%lx]\n",
592 (vma->vm_end - vma->vm_start));
593 goto done;
594 }
595
596 /* create mapping + update buffer list */
597 retval = -ENOMEM;
598 map = kmalloc(sizeof(struct videobuf_mapping),GFP_KERNEL);
599 if (NULL == map)
600 goto done;
Brandon Philipsce540932008-04-02 18:10:59 -0300601
602 size = 0;
603 for (i = first; i <= last; i++) {
604 if (NULL == q->bufs[i])
605 continue;
Mauro Carvalho Chehab851c0c92007-09-27 18:25:44 -0300606 q->bufs[i]->map = map;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300607 q->bufs[i]->baddr = vma->vm_start + size;
Brandon Philipsce540932008-04-02 18:10:59 -0300608 size += q->bufs[i]->bsize;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300609 }
Brandon Philipsce540932008-04-02 18:10:59 -0300610
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300611 map->count = 1;
612 map->start = vma->vm_start;
613 map->end = vma->vm_end;
614 map->q = q;
615 vma->vm_ops = &videobuf_vm_ops;
616 vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED;
617 vma->vm_flags &= ~VM_IO; /* using shared anonymous pages */
618 vma->vm_private_data = map;
619 dprintk(1,"mmap %p: q=%p %08lx-%08lx pgoff %08lx bufs %d-%d\n",
620 map,q,vma->vm_start,vma->vm_end,vma->vm_pgoff,first,last);
621 retval = 0;
622
623 done:
624 return retval;
625}
626
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300627static int __videobuf_copy_to_user ( struct videobuf_queue *q,
628 char __user *data, size_t count,
629 int nonblocking )
630{
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300631 struct videobuf_dma_sg_memory *mem = q->read_buf->priv;
632 BUG_ON(!mem);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300633 MAGIC_CHECK(mem->magic,MAGIC_SG_MEM);
634
635 /* copy to userspace */
636 if (count > q->read_buf->size - q->read_off)
637 count = q->read_buf->size - q->read_off;
638
639 if (copy_to_user(data, mem->dma.vmalloc+q->read_off, count))
640 return -EFAULT;
641
642 return count;
643}
644
645static int __videobuf_copy_stream ( struct videobuf_queue *q,
646 char __user *data, size_t count, size_t pos,
647 int vbihack, int nonblocking )
648{
649 unsigned int *fc;
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300650 struct videobuf_dma_sg_memory *mem = q->read_buf->priv;
651 BUG_ON(!mem);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300652 MAGIC_CHECK(mem->magic,MAGIC_SG_MEM);
653
654 if (vbihack) {
655 /* dirty, undocumented hack -- pass the frame counter
656 * within the last four bytes of each vbi data block.
657 * We need that one to maintain backward compatibility
658 * to all vbi decoding software out there ... */
659 fc = (unsigned int*)mem->dma.vmalloc;
660 fc += (q->read_buf->size>>2) -1;
661 *fc = q->read_buf->field_count >> 1;
662 dprintk(1,"vbihack: %d\n",*fc);
663 }
664
665 /* copy stuff using the common method */
666 count = __videobuf_copy_to_user (q,data,count,nonblocking);
667
668 if ( (count==-EFAULT) && (0 == pos) )
669 return -EFAULT;
670
671 return count;
672}
673
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300674static struct videobuf_qtype_ops sg_ops = {
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300675 .magic = MAGIC_QTYPE_OPS,
676
677 .alloc = __videobuf_alloc,
678 .iolock = __videobuf_iolock,
679 .sync = __videobuf_sync,
680 .mmap_free = __videobuf_mmap_free,
681 .mmap_mapper = __videobuf_mmap_mapper,
Al Viro13bcd5d2007-10-13 08:25:24 +0100682 .video_copy_to_user = __videobuf_copy_to_user,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300683 .copy_stream = __videobuf_copy_stream,
684};
685
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300686void *videobuf_sg_alloc(size_t size)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300687{
688 struct videobuf_queue q;
689
690 /* Required to make generic handler to call __videobuf_alloc */
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300691 q.int_ops = &sg_ops;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300692
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300693 q.msize = size;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300694
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300695 return videobuf_alloc(&q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300696}
697
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300698void videobuf_queue_sg_init(struct videobuf_queue* q,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300699 struct videobuf_queue_ops *ops,
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300700 struct device *dev,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300701 spinlock_t *irqlock,
702 enum v4l2_buf_type type,
703 enum v4l2_field field,
704 unsigned int msize,
705 void *priv)
706{
Mauro Carvalho Chehabd4cae5a2007-10-08 12:20:02 -0300707 videobuf_queue_core_init(q, ops, dev, irqlock, type, field, msize,
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300708 priv, &sg_ops);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300709}
710
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300711/* --------------------------------------------------------------------- */
712
713EXPORT_SYMBOL_GPL(videobuf_vmalloc_to_sg);
714
715EXPORT_SYMBOL_GPL(videobuf_to_dma);
716EXPORT_SYMBOL_GPL(videobuf_dma_init);
717EXPORT_SYMBOL_GPL(videobuf_dma_init_user);
718EXPORT_SYMBOL_GPL(videobuf_dma_init_kernel);
719EXPORT_SYMBOL_GPL(videobuf_dma_init_overlay);
720EXPORT_SYMBOL_GPL(videobuf_dma_map);
721EXPORT_SYMBOL_GPL(videobuf_dma_sync);
722EXPORT_SYMBOL_GPL(videobuf_dma_unmap);
723EXPORT_SYMBOL_GPL(videobuf_dma_free);
724
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300725EXPORT_SYMBOL_GPL(videobuf_sg_dma_map);
726EXPORT_SYMBOL_GPL(videobuf_sg_dma_unmap);
727EXPORT_SYMBOL_GPL(videobuf_sg_alloc);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300728
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300729EXPORT_SYMBOL_GPL(videobuf_queue_sg_init);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300730
731/*
732 * Local variables:
733 * c-basic-offset: 8
734 * End:
735 */