blob: 06f9a9c2a39add9256a58850d0cf5c4b0c52bf5b [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 *
Magnus Damm5d6aaf52008-07-16 21:27:49 -03004 * The functions expect the hardware being able to scatter gather
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03005 * (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>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040024#include <linux/sched.h>
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030025#include <linux/slab.h>
26#include <linux/interrupt.h>
27
Guennadi Liakhovetski07051352008-04-22 14:42:13 -030028#include <linux/dma-mapping.h>
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030029#include <linux/vmalloc.h>
30#include <linux/pagemap.h>
Ralf Baechle11763602007-10-23 20:42:11 +020031#include <linux/scatterlist.h>
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030032#include <asm/page.h>
33#include <asm/pgtable.h>
34
35#include <media/videobuf-dma-sg.h>
36
37#define MAGIC_DMABUF 0x19721112
38#define MAGIC_SG_MEM 0x17890714
39
Pawel Osciak7a022642010-03-17 04:01:04 -030040#define MAGIC_CHECK(is, should) \
41 if (unlikely((is) != (should))) { \
42 printk(KERN_ERR "magic mismatch: %x (expected %x)\n", \
43 is, should); \
44 BUG(); \
45 }
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030046
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030047static int debug;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030048module_param(debug, int, 0644);
49
Guennadi Liakhovetski07051352008-04-22 14:42:13 -030050MODULE_DESCRIPTION("helper module to manage video4linux dma sg buffers");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030051MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
52MODULE_LICENSE("GPL");
53
Pawel Osciak7a022642010-03-17 04:01:04 -030054#define dprintk(level, fmt, arg...) \
55 if (debug >= level) \
56 printk(KERN_DEBUG "vbuf-sg: " fmt , ## arg)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030057
58/* --------------------------------------------------------------------- */
59
Laurent Pinchart71817722010-05-11 10:36:32 -030060/*
61 * Return a scatterlist for some page-aligned vmalloc()'ed memory
62 * block (NULL on errors). Memory for the scatterlist is allocated
63 * using kmalloc. The caller must free the memory.
64 */
65static struct scatterlist *videobuf_vmalloc_to_sg(unsigned char *virt,
66 int nr_pages)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030067{
68 struct scatterlist *sglist;
69 struct page *pg;
70 int i;
71
Cohen David.A1010ed12009-05-11 11:00:20 -030072 sglist = vmalloc(nr_pages * sizeof(*sglist));
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030073 if (NULL == sglist)
74 return NULL;
Cohen David.A1010ed12009-05-11 11:00:20 -030075 memset(sglist, 0, nr_pages * sizeof(*sglist));
Jens Axboe45711f12007-10-22 21:19:53 +020076 sg_init_table(sglist, nr_pages);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030077 for (i = 0; i < nr_pages; i++, virt += PAGE_SIZE) {
78 pg = vmalloc_to_page(virt);
79 if (NULL == pg)
80 goto err;
81 BUG_ON(PageHighMem(pg));
Jens Axboe642f1492007-10-24 11:20:47 +020082 sg_set_page(&sglist[i], pg, PAGE_SIZE, 0);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030083 }
84 return sglist;
85
Pawel Osciak7a022642010-03-17 04:01:04 -030086err:
Cohen David.A1010ed12009-05-11 11:00:20 -030087 vfree(sglist);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030088 return NULL;
89}
90
Laurent Pinchart71817722010-05-11 10:36:32 -030091/*
92 * Return a scatterlist for a an array of userpages (NULL on errors).
93 * Memory for the scatterlist is allocated using kmalloc. The caller
94 * must free the memory.
95 */
96static struct scatterlist *videobuf_pages_to_sg(struct page **pages,
97 int nr_pages, int offset)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030098{
99 struct scatterlist *sglist;
Christophe Jailleta47cacb2008-07-04 06:33:22 -0300100 int i;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300101
102 if (NULL == pages[0])
103 return NULL;
Cohen David.A1010ed12009-05-11 11:00:20 -0300104 sglist = vmalloc(nr_pages * sizeof(*sglist));
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300105 if (NULL == sglist)
106 return NULL;
Jens Axboe45711f12007-10-22 21:19:53 +0200107 sg_init_table(sglist, nr_pages);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300108
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300109 if (PageHighMem(pages[0]))
110 /* DMA to highmem pages might not work */
111 goto highmem;
Jens Axboe642f1492007-10-24 11:20:47 +0200112 sg_set_page(&sglist[0], pages[0], PAGE_SIZE - offset, offset);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300113 for (i = 1; i < nr_pages; i++) {
114 if (NULL == pages[i])
115 goto nopage;
116 if (PageHighMem(pages[i]))
117 goto highmem;
Jens Axboe642f1492007-10-24 11:20:47 +0200118 sg_set_page(&sglist[i], pages[i], PAGE_SIZE, 0);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300119 }
120 return sglist;
121
Pawel Osciak7a022642010-03-17 04:01:04 -0300122nopage:
123 dprintk(2, "sgl: oops - no page\n");
Cohen David.A1010ed12009-05-11 11:00:20 -0300124 vfree(sglist);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300125 return NULL;
126
Pawel Osciak7a022642010-03-17 04:01:04 -0300127highmem:
128 dprintk(2, "sgl: oops - highmem page\n");
Cohen David.A1010ed12009-05-11 11:00:20 -0300129 vfree(sglist);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300130 return NULL;
131}
132
133/* --------------------------------------------------------------------- */
134
Pawel Osciak7a022642010-03-17 04:01:04 -0300135struct videobuf_dmabuf *videobuf_to_dma(struct videobuf_buffer *buf)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300136{
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300137 struct videobuf_dma_sg_memory *mem = buf->priv;
138 BUG_ON(!mem);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300139
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300140 MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300141
142 return &mem->dma;
143}
Pawel Osciak7a022642010-03-17 04:01:04 -0300144EXPORT_SYMBOL_GPL(videobuf_to_dma);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300145
146void videobuf_dma_init(struct videobuf_dmabuf *dma)
147{
Pawel Osciak7a022642010-03-17 04:01:04 -0300148 memset(dma, 0, sizeof(*dma));
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300149 dma->magic = MAGIC_DMABUF;
150}
Pawel Osciak7a022642010-03-17 04:01:04 -0300151EXPORT_SYMBOL_GPL(videobuf_dma_init);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300152
Maxim Levitsky99001322007-09-27 20:34:09 -0300153static int videobuf_dma_init_user_locked(struct videobuf_dmabuf *dma,
154 int direction, unsigned long data, unsigned long size)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300155{
Pawel Osciak7a022642010-03-17 04:01:04 -0300156 unsigned long first, last;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300157 int err, rw = 0;
158
159 dma->direction = direction;
160 switch (dma->direction) {
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300161 case DMA_FROM_DEVICE:
162 rw = READ;
163 break;
164 case DMA_TO_DEVICE:
165 rw = WRITE;
166 break;
167 default:
168 BUG();
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300169 }
170
171 first = (data & PAGE_MASK) >> PAGE_SHIFT;
172 last = ((data+size-1) & PAGE_MASK) >> PAGE_SHIFT;
173 dma->offset = data & ~PAGE_MASK;
174 dma->nr_pages = last-first+1;
Pawel Osciak7a022642010-03-17 04:01:04 -0300175 dma->pages = kmalloc(dma->nr_pages * sizeof(struct page *), GFP_KERNEL);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300176 if (NULL == dma->pages)
177 return -ENOMEM;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300178
Pawel Osciak7a022642010-03-17 04:01:04 -0300179 dprintk(1, "init user [0x%lx+0x%lx => %d pages]\n",
180 data, size, dma->nr_pages);
181
182 err = get_user_pages(current, current->mm,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300183 data & PAGE_MASK, dma->nr_pages,
184 rw == READ, 1, /* force */
185 dma->pages, NULL);
Maxim Levitsky99001322007-09-27 20:34:09 -0300186
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300187 if (err != dma->nr_pages) {
188 dma->nr_pages = (err >= 0) ? err : 0;
Pawel Osciak7a022642010-03-17 04:01:04 -0300189 dprintk(1, "get_user_pages: err=%d [%d]\n", err, dma->nr_pages);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300190 return err < 0 ? err : -EINVAL;
191 }
192 return 0;
193}
194
Maxim Levitsky99001322007-09-27 20:34:09 -0300195int videobuf_dma_init_user(struct videobuf_dmabuf *dma, int direction,
196 unsigned long data, unsigned long size)
197{
198 int ret;
Pawel Osciak7a022642010-03-17 04:01:04 -0300199
Maxim Levitsky99001322007-09-27 20:34:09 -0300200 down_read(&current->mm->mmap_sem);
201 ret = videobuf_dma_init_user_locked(dma, direction, data, size);
202 up_read(&current->mm->mmap_sem);
203
204 return ret;
205}
Pawel Osciak7a022642010-03-17 04:01:04 -0300206EXPORT_SYMBOL_GPL(videobuf_dma_init_user);
Maxim Levitsky99001322007-09-27 20:34:09 -0300207
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300208int videobuf_dma_init_kernel(struct videobuf_dmabuf *dma, int direction,
209 int nr_pages)
210{
Pawel Osciak7a022642010-03-17 04:01:04 -0300211 dprintk(1, "init kernel [%d pages]\n", nr_pages);
212
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300213 dma->direction = direction;
Laurent Pinchartbb6dbe72010-05-11 10:36:34 -0300214 dma->vaddr = vmalloc_32(nr_pages << PAGE_SHIFT);
215 if (NULL == dma->vaddr) {
Pawel Osciak7a022642010-03-17 04:01:04 -0300216 dprintk(1, "vmalloc_32(%d pages) failed\n", nr_pages);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300217 return -ENOMEM;
218 }
Pawel Osciak7a022642010-03-17 04:01:04 -0300219
220 dprintk(1, "vmalloc is at addr 0x%08lx, size=%d\n",
Laurent Pinchartbb6dbe72010-05-11 10:36:34 -0300221 (unsigned long)dma->vaddr,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300222 nr_pages << PAGE_SHIFT);
Pawel Osciak7a022642010-03-17 04:01:04 -0300223
Laurent Pinchartbb6dbe72010-05-11 10:36:34 -0300224 memset(dma->vaddr, 0, nr_pages << PAGE_SHIFT);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300225 dma->nr_pages = nr_pages;
Pawel Osciak7a022642010-03-17 04:01:04 -0300226
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300227 return 0;
228}
Pawel Osciak7a022642010-03-17 04:01:04 -0300229EXPORT_SYMBOL_GPL(videobuf_dma_init_kernel);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300230
231int videobuf_dma_init_overlay(struct videobuf_dmabuf *dma, int direction,
232 dma_addr_t addr, int nr_pages)
233{
Pawel Osciak7a022642010-03-17 04:01:04 -0300234 dprintk(1, "init overlay [%d pages @ bus 0x%lx]\n",
235 nr_pages, (unsigned long)addr);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300236 dma->direction = direction;
Pawel Osciak7a022642010-03-17 04:01:04 -0300237
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300238 if (0 == addr)
239 return -EINVAL;
240
241 dma->bus_addr = addr;
242 dma->nr_pages = nr_pages;
Pawel Osciak7a022642010-03-17 04:01:04 -0300243
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300244 return 0;
245}
Pawel Osciak7a022642010-03-17 04:01:04 -0300246EXPORT_SYMBOL_GPL(videobuf_dma_init_overlay);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300247
Laurent Pinchart95268402010-05-11 10:36:30 -0300248int videobuf_dma_map(struct device *dev, struct videobuf_dmabuf *dma)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300249{
Pawel Osciak7a022642010-03-17 04:01:04 -0300250 MAGIC_CHECK(dma->magic, MAGIC_DMABUF);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300251 BUG_ON(0 == dma->nr_pages);
252
253 if (dma->pages) {
254 dma->sglist = videobuf_pages_to_sg(dma->pages, dma->nr_pages,
255 dma->offset);
256 }
Laurent Pinchartbb6dbe72010-05-11 10:36:34 -0300257 if (dma->vaddr) {
258 dma->sglist = videobuf_vmalloc_to_sg(dma->vaddr,
Pawel Osciak7a022642010-03-17 04:01:04 -0300259 dma->nr_pages);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300260 }
261 if (dma->bus_addr) {
Cohen David.A1010ed12009-05-11 11:00:20 -0300262 dma->sglist = vmalloc(sizeof(*dma->sglist));
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300263 if (NULL != dma->sglist) {
Pawel Osciak7a022642010-03-17 04:01:04 -0300264 dma->sglen = 1;
265 sg_dma_address(&dma->sglist[0]) = dma->bus_addr
266 & PAGE_MASK;
267 dma->sglist[0].offset = dma->bus_addr & ~PAGE_MASK;
268 sg_dma_len(&dma->sglist[0]) = dma->nr_pages * PAGE_SIZE;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300269 }
270 }
271 if (NULL == dma->sglist) {
Pawel Osciak7a022642010-03-17 04:01:04 -0300272 dprintk(1, "scatterlist is NULL\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300273 return -ENOMEM;
274 }
275 if (!dma->bus_addr) {
Laurent Pinchart95268402010-05-11 10:36:30 -0300276 dma->sglen = dma_map_sg(dev, dma->sglist,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300277 dma->nr_pages, dma->direction);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300278 if (0 == dma->sglen) {
279 printk(KERN_WARNING
Pawel Osciak7a022642010-03-17 04:01:04 -0300280 "%s: videobuf_map_sg failed\n", __func__);
Cohen David.A1010ed12009-05-11 11:00:20 -0300281 vfree(dma->sglist);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300282 dma->sglist = NULL;
283 dma->sglen = 0;
Figo.zhang92051b22009-06-10 23:17:27 -0300284 return -ENOMEM;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300285 }
286 }
Pawel Osciak7a022642010-03-17 04:01:04 -0300287
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300288 return 0;
289}
Pawel Osciak7a022642010-03-17 04:01:04 -0300290EXPORT_SYMBOL_GPL(videobuf_dma_map);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300291
Laurent Pinchart95268402010-05-11 10:36:30 -0300292int videobuf_dma_unmap(struct device *dev, struct videobuf_dmabuf *dma)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300293{
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300294 MAGIC_CHECK(dma->magic, MAGIC_DMABUF);
Pawel Osciak7a022642010-03-17 04:01:04 -0300295
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300296 if (!dma->sglen)
297 return 0;
298
Laurent Pinchart95268402010-05-11 10:36:30 -0300299 dma_unmap_sg(dev, dma->sglist, dma->sglen, dma->direction);
Mauro Carvalho Chehab5ddff432007-10-08 11:43:49 -0300300
Cohen David.A1010ed12009-05-11 11:00:20 -0300301 vfree(dma->sglist);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300302 dma->sglist = NULL;
303 dma->sglen = 0;
Pawel Osciak7a022642010-03-17 04:01:04 -0300304
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300305 return 0;
306}
Pawel Osciak7a022642010-03-17 04:01:04 -0300307EXPORT_SYMBOL_GPL(videobuf_dma_unmap);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300308
309int videobuf_dma_free(struct videobuf_dmabuf *dma)
310{
Pawel Osciak7a022642010-03-17 04:01:04 -0300311 int i;
312 MAGIC_CHECK(dma->magic, MAGIC_DMABUF);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300313 BUG_ON(dma->sglen);
314
315 if (dma->pages) {
Pawel Osciak7a022642010-03-17 04:01:04 -0300316 for (i = 0; i < dma->nr_pages; i++)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300317 page_cache_release(dma->pages[i]);
318 kfree(dma->pages);
319 dma->pages = NULL;
320 }
321
Laurent Pinchartbb6dbe72010-05-11 10:36:34 -0300322 vfree(dma->vaddr);
323 dma->vaddr = NULL;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300324
Pawel Osciak7a022642010-03-17 04:01:04 -0300325 if (dma->bus_addr)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300326 dma->bus_addr = 0;
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300327 dma->direction = DMA_NONE;
Pawel Osciak7a022642010-03-17 04:01:04 -0300328
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300329 return 0;
330}
Pawel Osciak7a022642010-03-17 04:01:04 -0300331EXPORT_SYMBOL_GPL(videobuf_dma_free);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300332
333/* --------------------------------------------------------------------- */
334
Pawel Osciak7a022642010-03-17 04:01:04 -0300335static void videobuf_vm_open(struct vm_area_struct *vma)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300336{
337 struct videobuf_mapping *map = vma->vm_private_data;
338
Pawel Osciak7a022642010-03-17 04:01:04 -0300339 dprintk(2, "vm_open %p [count=%d,vma=%08lx-%08lx]\n", map,
340 map->count, vma->vm_start, vma->vm_end);
341
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300342 map->count++;
343}
344
Pawel Osciak7a022642010-03-17 04:01:04 -0300345static void videobuf_vm_close(struct vm_area_struct *vma)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300346{
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
Pawel Osciak7a022642010-03-17 04:01:04 -0300352 dprintk(2, "vm_close %p [count=%d,vma=%08lx-%08lx]\n", map,
353 map->count, vma->vm_start, vma->vm_end);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300354
355 map->count--;
356 if (0 == map->count) {
Pawel Osciak7a022642010-03-17 04:01:04 -0300357 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;
Pawel Osciak7a022642010-03-17 04:01:04 -0300362 mem = q->bufs[i]->priv;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300363 if (!mem)
364 continue;
365
Pawel Osciak7a022642010-03-17 04:01:04 -0300366 MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300367
Mauro Carvalho Chehab851c0c92007-09-27 18:25:44 -0300368 if (q->bufs[i]->map != map)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300369 continue;
Mauro Carvalho Chehab851c0c92007-09-27 18:25:44 -0300370 q->bufs[i]->map = NULL;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300371 q->bufs[i]->baddr = 0;
Pawel Osciak7a022642010-03-17 04:01:04 -0300372 q->ops->buf_release(q, q->bufs[i]);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300373 }
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300374 mutex_unlock(&q->vb_lock);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300375 kfree(map);
376 }
377 return;
378}
379
380/*
381 * Get a anonymous page for the mapping. Make sure we can DMA to that
382 * memory location with 32bit PCI devices (i.e. don't use highmem for
383 * now ...). Bounce buffers don't work very well for the data rates
384 * video capture has.
385 */
Pawel Osciak7a022642010-03-17 04:01:04 -0300386static int videobuf_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300387{
388 struct page *page;
389
Pawel Osciak7a022642010-03-17 04:01:04 -0300390 dprintk(3, "fault: fault @ %08lx [vma %08lx-%08lx]\n",
391 (unsigned long)vmf->virtual_address,
392 vma->vm_start, vma->vm_end);
393
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;
Guennadi Liakhovetskic0cd5012009-01-03 18:20:04 -0300397 clear_user_highpage(page, (unsigned long)vmf->virtual_address);
Nick Piggin105354a2007-12-07 17:57:38 -0300398 vmf->page = page;
Pawel Osciak7a022642010-03-17 04:01:04 -0300399
Nick Piggin105354a2007-12-07 17:57:38 -0300400 return 0;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300401}
402
Pawel Osciak7a022642010-03-17 04:01:04 -0300403static const struct vm_operations_struct videobuf_vm_ops = {
404 .open = videobuf_vm_open,
405 .close = videobuf_vm_close,
406 .fault = videobuf_vm_fault,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300407};
408
409/* ---------------------------------------------------------------------
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300410 * SG handlers for the generic methods
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300411 */
412
413/* Allocated area consists on 3 parts:
414 struct video_buffer
415 struct <driver>_buffer (cx88_buffer, saa7134_buf, ...)
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300416 struct videobuf_dma_sg_memory
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300417 */
418
Pawel Osciak33c38282010-05-11 10:36:28 -0300419static struct videobuf_buffer *__videobuf_alloc_vb(size_t size)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300420{
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300421 struct videobuf_dma_sg_memory *mem;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300422 struct videobuf_buffer *vb;
423
Pawel Osciak7a022642010-03-17 04:01:04 -0300424 vb = kzalloc(size + sizeof(*mem), GFP_KERNEL);
Pawel Osciakbee527f2010-02-22 13:10:06 -0300425 if (!vb)
426 return vb;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300427
Pawel Osciak7a022642010-03-17 04:01:04 -0300428 mem = vb->priv = ((char *)vb) + size;
429 mem->magic = MAGIC_SG_MEM;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300430
431 videobuf_dma_init(&mem->dma);
432
Pawel Osciak7a022642010-03-17 04:01:04 -0300433 dprintk(1, "%s: allocated at %p(%ld+%ld) & %p(%ld)\n",
434 __func__, vb, (long)sizeof(*vb), (long)size - sizeof(*vb),
435 mem, (long)sizeof(*mem));
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300436
437 return vb;
438}
439
Hans Verkuil037c75e2010-03-28 08:18:37 -0300440static void *__videobuf_to_vaddr(struct videobuf_buffer *buf)
Mauro Carvalho Chehab59d34482008-04-13 15:10:00 -0300441{
442 struct videobuf_dma_sg_memory *mem = buf->priv;
443 BUG_ON(!mem);
444
445 MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
446
Laurent Pinchartbb6dbe72010-05-11 10:36:34 -0300447 return mem->dma.vaddr;
Mauro Carvalho Chehab59d34482008-04-13 15:10:00 -0300448}
449
Pawel Osciak7a022642010-03-17 04:01:04 -0300450static int __videobuf_iolock(struct videobuf_queue *q,
451 struct videobuf_buffer *vb,
452 struct v4l2_framebuffer *fbuf)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300453{
Pawel Osciak7a022642010-03-17 04:01:04 -0300454 int err, pages;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300455 dma_addr_t bus;
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300456 struct videobuf_dma_sg_memory *mem = vb->priv;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300457 BUG_ON(!mem);
458
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300459 MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300460
461 switch (vb->memory) {
462 case V4L2_MEMORY_MMAP:
463 case V4L2_MEMORY_USERPTR:
464 if (0 == vb->baddr) {
465 /* no userspace addr -- kernel bounce buffer */
466 pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
Pawel Osciak7a022642010-03-17 04:01:04 -0300467 err = videobuf_dma_init_kernel(&mem->dma,
468 DMA_FROM_DEVICE,
469 pages);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300470 if (0 != err)
471 return err;
Maxim Levitsky99001322007-09-27 20:34:09 -0300472 } else if (vb->memory == V4L2_MEMORY_USERPTR) {
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300473 /* dma directly to userspace */
Pawel Osciak7a022642010-03-17 04:01:04 -0300474 err = videobuf_dma_init_user(&mem->dma,
475 DMA_FROM_DEVICE,
476 vb->baddr, vb->bsize);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300477 if (0 != err)
478 return err;
Maxim Levitsky99001322007-09-27 20:34:09 -0300479 } else {
480 /* NOTE: HACK: videobuf_iolock on V4L2_MEMORY_MMAP
481 buffers can only be called from videobuf_qbuf
482 we take current->mm->mmap_sem there, to prevent
483 locking inversion, so don't take it here */
484
485 err = videobuf_dma_init_user_locked(&mem->dma,
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300486 DMA_FROM_DEVICE,
Maxim Levitsky99001322007-09-27 20:34:09 -0300487 vb->baddr, vb->bsize);
488 if (0 != err)
489 return err;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300490 }
491 break;
492 case V4L2_MEMORY_OVERLAY:
493 if (NULL == fbuf)
494 return -EINVAL;
495 /* FIXME: need sanity checks for vb->boff */
496 /*
497 * Using a double cast to avoid compiler warnings when
498 * building for PAE. Compiler doesn't like direct casting
499 * of a 32 bit ptr to 64 bit integer.
500 */
501 bus = (dma_addr_t)(unsigned long)fbuf->base + vb->boff;
502 pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300503 err = videobuf_dma_init_overlay(&mem->dma, DMA_FROM_DEVICE,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300504 bus, pages);
505 if (0 != err)
506 return err;
507 break;
508 default:
509 BUG();
510 }
Laurent Pinchart95268402010-05-11 10:36:30 -0300511 err = videobuf_dma_map(q->dev, &mem->dma);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300512 if (0 != err)
513 return err;
514
515 return 0;
516}
517
518static int __videobuf_sync(struct videobuf_queue *q,
519 struct videobuf_buffer *buf)
520{
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300521 struct videobuf_dma_sg_memory *mem = buf->priv;
Mauro Carvalho Chehab97f81052010-05-05 16:23:09 -0300522 BUG_ON(!mem || !mem->dma.sglen);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300523
Mauro Carvalho Chehab97f81052010-05-05 16:23:09 -0300524 MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
525 MAGIC_CHECK(mem->dma.magic, MAGIC_DMABUF);
526
527 dma_sync_sg_for_cpu(q->dev, mem->dma.sglist,
Arnout Vandecappellefc7f8fd2010-03-17 19:53:04 -0300528 mem->dma.sglen, mem->dma.direction);
Mauro Carvalho Chehab97f81052010-05-05 16:23:09 -0300529
530 return 0;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300531}
532
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300533static int __videobuf_mmap_mapper(struct videobuf_queue *q,
Hans Verkuil0b62b732010-03-28 09:09:05 -0300534 struct videobuf_buffer *buf,
535 struct vm_area_struct *vma)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300536{
Hans Verkuil0b62b732010-03-28 09:09:05 -0300537 struct videobuf_dma_sg_memory *mem = buf->priv;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300538 struct videobuf_mapping *map;
Mauro Carvalho Chehab474675a2010-04-25 11:23:52 -0300539 unsigned int first, last, size = 0, i;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300540 int retval;
541
542 retval = -EINVAL;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300543
Brandon Philipsce540932008-04-02 18:10:59 -0300544 /* This function maintains backwards compatibility with V4L1 and will
545 * map more than one buffer if the vma length is equal to the combined
546 * size of multiple buffers than it will map them together. See
547 * VIDIOCGMBUF in the v4l spec
548 *
549 * TODO: Allow drivers to specify if they support this mode
550 */
551
Hans Verkuil0b62b732010-03-28 09:09:05 -0300552 BUG_ON(!mem);
553 MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
554
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300555 /* look for first buffer to map */
556 for (first = 0; first < VIDEO_MAX_FRAME; first++) {
Hans Verkuil0b62b732010-03-28 09:09:05 -0300557 if (buf == q->bufs[first]) {
558 size = PAGE_ALIGN(q->bufs[first]->bsize);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300559 break;
Hans Verkuil0b62b732010-03-28 09:09:05 -0300560 }
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300561 }
Hans Verkuil0b62b732010-03-28 09:09:05 -0300562
563 /* paranoia, should never happen since buf is always valid. */
Mauro Carvalho Chehab474675a2010-04-25 11:23:52 -0300564 if (!size) {
Pawel Osciak7a022642010-03-17 04:01:04 -0300565 dprintk(1, "mmap app bug: offset invalid [offset=0x%lx]\n",
Hans Verkuil0b62b732010-03-28 09:09:05 -0300566 (vma->vm_pgoff << PAGE_SHIFT));
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300567 goto done;
568 }
569
Hans Verkuil0b62b732010-03-28 09:09:05 -0300570 last = first;
571#ifdef CONFIG_VIDEO_V4L1_COMPAT
572 if (size != (vma->vm_end - vma->vm_start)) {
573 /* look for last buffer to map */
574 for (last = first + 1; last < VIDEO_MAX_FRAME; last++) {
575 if (NULL == q->bufs[last])
576 continue;
577 if (V4L2_MEMORY_MMAP != q->bufs[last]->memory)
578 continue;
579 if (q->bufs[last]->map) {
580 retval = -EBUSY;
581 goto done;
582 }
583 size += PAGE_ALIGN(q->bufs[last]->bsize);
584 if (size == (vma->vm_end - vma->vm_start))
585 break;
586 }
587 if (VIDEO_MAX_FRAME == last) {
588 dprintk(1, "mmap app bug: size invalid [size=0x%lx]\n",
589 (vma->vm_end - vma->vm_start));
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300590 goto done;
591 }
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300592 }
Hans Verkuil0b62b732010-03-28 09:09:05 -0300593#endif
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300594
595 /* create mapping + update buffer list */
596 retval = -ENOMEM;
Pawel Osciak7a022642010-03-17 04:01:04 -0300597 map = kmalloc(sizeof(struct videobuf_mapping), GFP_KERNEL);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300598 if (NULL == map)
599 goto done;
Brandon Philipsce540932008-04-02 18:10:59 -0300600
601 size = 0;
602 for (i = first; i <= last; i++) {
603 if (NULL == q->bufs[i])
604 continue;
Mauro Carvalho Chehab851c0c92007-09-27 18:25:44 -0300605 q->bufs[i]->map = map;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300606 q->bufs[i]->baddr = vma->vm_start + size;
Tuukka Toivonen7cbefad2009-07-23 10:56:25 -0300607 size += PAGE_ALIGN(q->bufs[i]->bsize);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300608 }
Brandon Philipsce540932008-04-02 18:10:59 -0300609
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300610 map->count = 1;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300611 map->q = q;
612 vma->vm_ops = &videobuf_vm_ops;
613 vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED;
614 vma->vm_flags &= ~VM_IO; /* using shared anonymous pages */
615 vma->vm_private_data = map;
Pawel Osciak7a022642010-03-17 04:01:04 -0300616 dprintk(1, "mmap %p: q=%p %08lx-%08lx pgoff %08lx bufs %d-%d\n",
617 map, q, vma->vm_start, vma->vm_end, vma->vm_pgoff, first, last);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300618 retval = 0;
619
Pawel Osciak7a022642010-03-17 04:01:04 -0300620done:
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300621 return retval;
622}
623
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300624static struct videobuf_qtype_ops sg_ops = {
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300625 .magic = MAGIC_QTYPE_OPS,
626
Pawel Osciak33c38282010-05-11 10:36:28 -0300627 .alloc_vb = __videobuf_alloc_vb,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300628 .iolock = __videobuf_iolock,
629 .sync = __videobuf_sync,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300630 .mmap_mapper = __videobuf_mmap_mapper,
Hans Verkuil037c75e2010-03-28 08:18:37 -0300631 .vaddr = __videobuf_to_vaddr,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300632};
633
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300634void *videobuf_sg_alloc(size_t size)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300635{
636 struct videobuf_queue q;
637
638 /* Required to make generic handler to call __videobuf_alloc */
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300639 q.int_ops = &sg_ops;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300640
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300641 q.msize = size;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300642
Pawel Osciak33c38282010-05-11 10:36:28 -0300643 return videobuf_alloc_vb(&q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300644}
Pawel Osciak7a022642010-03-17 04:01:04 -0300645EXPORT_SYMBOL_GPL(videobuf_sg_alloc);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300646
Pawel Osciak7a022642010-03-17 04:01:04 -0300647void videobuf_queue_sg_init(struct videobuf_queue *q,
Jonathan Corbet38a54f32009-11-17 19:43:41 -0300648 const struct videobuf_queue_ops *ops,
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300649 struct device *dev,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300650 spinlock_t *irqlock,
651 enum v4l2_buf_type type,
652 enum v4l2_field field,
653 unsigned int msize,
654 void *priv)
655{
Mauro Carvalho Chehabd4cae5a2007-10-08 12:20:02 -0300656 videobuf_queue_core_init(q, ops, dev, irqlock, type, field, msize,
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300657 priv, &sg_ops);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300658}
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300659EXPORT_SYMBOL_GPL(videobuf_queue_sg_init);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300660