blob: 17b1f89e81337779359929d41b4fc53791e078b9 [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
Pawel Osciak7a022642010-03-17 04:01:04 -030060struct scatterlist *videobuf_vmalloc_to_sg(unsigned char *virt, int nr_pages)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030061{
62 struct scatterlist *sglist;
63 struct page *pg;
64 int i;
65
Cohen David.A1010ed12009-05-11 11:00:20 -030066 sglist = vmalloc(nr_pages * sizeof(*sglist));
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030067 if (NULL == sglist)
68 return NULL;
Cohen David.A1010ed12009-05-11 11:00:20 -030069 memset(sglist, 0, nr_pages * sizeof(*sglist));
Jens Axboe45711f12007-10-22 21:19:53 +020070 sg_init_table(sglist, nr_pages);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030071 for (i = 0; i < nr_pages; i++, virt += PAGE_SIZE) {
72 pg = vmalloc_to_page(virt);
73 if (NULL == pg)
74 goto err;
75 BUG_ON(PageHighMem(pg));
Jens Axboe642f149032007-10-24 11:20:47 +020076 sg_set_page(&sglist[i], pg, PAGE_SIZE, 0);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030077 }
78 return sglist;
79
Pawel Osciak7a022642010-03-17 04:01:04 -030080err:
Cohen David.A1010ed12009-05-11 11:00:20 -030081 vfree(sglist);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030082 return NULL;
83}
Pawel Osciak7a022642010-03-17 04:01:04 -030084EXPORT_SYMBOL_GPL(videobuf_vmalloc_to_sg);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030085
Pawel Osciak7a022642010-03-17 04:01:04 -030086struct scatterlist *videobuf_pages_to_sg(struct page **pages, int nr_pages,
87 int offset)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030088{
89 struct scatterlist *sglist;
Christophe Jailleta47cacb2008-07-04 06:33:22 -030090 int i;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030091
92 if (NULL == pages[0])
93 return NULL;
Cohen David.A1010ed12009-05-11 11:00:20 -030094 sglist = vmalloc(nr_pages * sizeof(*sglist));
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030095 if (NULL == sglist)
96 return NULL;
Jens Axboe45711f12007-10-22 21:19:53 +020097 sg_init_table(sglist, nr_pages);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030098
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030099 if (PageHighMem(pages[0]))
100 /* DMA to highmem pages might not work */
101 goto highmem;
Jens Axboe642f149032007-10-24 11:20:47 +0200102 sg_set_page(&sglist[0], pages[0], PAGE_SIZE - offset, offset);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300103 for (i = 1; i < nr_pages; i++) {
104 if (NULL == pages[i])
105 goto nopage;
106 if (PageHighMem(pages[i]))
107 goto highmem;
Jens Axboe642f149032007-10-24 11:20:47 +0200108 sg_set_page(&sglist[i], pages[i], PAGE_SIZE, 0);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300109 }
110 return sglist;
111
Pawel Osciak7a022642010-03-17 04:01:04 -0300112nopage:
113 dprintk(2, "sgl: oops - no page\n");
Cohen David.A1010ed12009-05-11 11:00:20 -0300114 vfree(sglist);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300115 return NULL;
116
Pawel Osciak7a022642010-03-17 04:01:04 -0300117highmem:
118 dprintk(2, "sgl: oops - highmem page\n");
Cohen David.A1010ed12009-05-11 11:00:20 -0300119 vfree(sglist);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300120 return NULL;
121}
122
123/* --------------------------------------------------------------------- */
124
Pawel Osciak7a022642010-03-17 04:01:04 -0300125struct videobuf_dmabuf *videobuf_to_dma(struct videobuf_buffer *buf)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300126{
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300127 struct videobuf_dma_sg_memory *mem = buf->priv;
128 BUG_ON(!mem);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300129
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300130 MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300131
132 return &mem->dma;
133}
Pawel Osciak7a022642010-03-17 04:01:04 -0300134EXPORT_SYMBOL_GPL(videobuf_to_dma);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300135
136void videobuf_dma_init(struct videobuf_dmabuf *dma)
137{
Pawel Osciak7a022642010-03-17 04:01:04 -0300138 memset(dma, 0, sizeof(*dma));
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300139 dma->magic = MAGIC_DMABUF;
140}
Pawel Osciak7a022642010-03-17 04:01:04 -0300141EXPORT_SYMBOL_GPL(videobuf_dma_init);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300142
Maxim Levitsky99001322007-09-27 20:34:09 -0300143static int videobuf_dma_init_user_locked(struct videobuf_dmabuf *dma,
144 int direction, unsigned long data, unsigned long size)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300145{
Pawel Osciak7a022642010-03-17 04:01:04 -0300146 unsigned long first, last;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300147 int err, rw = 0;
148
149 dma->direction = direction;
150 switch (dma->direction) {
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300151 case DMA_FROM_DEVICE:
152 rw = READ;
153 break;
154 case DMA_TO_DEVICE:
155 rw = WRITE;
156 break;
157 default:
158 BUG();
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300159 }
160
161 first = (data & PAGE_MASK) >> PAGE_SHIFT;
162 last = ((data+size-1) & PAGE_MASK) >> PAGE_SHIFT;
163 dma->offset = data & ~PAGE_MASK;
164 dma->nr_pages = last-first+1;
Pawel Osciak7a022642010-03-17 04:01:04 -0300165 dma->pages = kmalloc(dma->nr_pages * sizeof(struct page *), GFP_KERNEL);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300166 if (NULL == dma->pages)
167 return -ENOMEM;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300168
Pawel Osciak7a022642010-03-17 04:01:04 -0300169 dprintk(1, "init user [0x%lx+0x%lx => %d pages]\n",
170 data, size, dma->nr_pages);
171
172 err = get_user_pages(current, current->mm,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300173 data & PAGE_MASK, dma->nr_pages,
174 rw == READ, 1, /* force */
175 dma->pages, NULL);
Maxim Levitsky99001322007-09-27 20:34:09 -0300176
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300177 if (err != dma->nr_pages) {
178 dma->nr_pages = (err >= 0) ? err : 0;
Pawel Osciak7a022642010-03-17 04:01:04 -0300179 dprintk(1, "get_user_pages: err=%d [%d]\n", err, dma->nr_pages);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300180 return err < 0 ? err : -EINVAL;
181 }
182 return 0;
183}
184
Maxim Levitsky99001322007-09-27 20:34:09 -0300185int videobuf_dma_init_user(struct videobuf_dmabuf *dma, int direction,
186 unsigned long data, unsigned long size)
187{
188 int ret;
Pawel Osciak7a022642010-03-17 04:01:04 -0300189
Maxim Levitsky99001322007-09-27 20:34:09 -0300190 down_read(&current->mm->mmap_sem);
191 ret = videobuf_dma_init_user_locked(dma, direction, data, size);
192 up_read(&current->mm->mmap_sem);
193
194 return ret;
195}
Pawel Osciak7a022642010-03-17 04:01:04 -0300196EXPORT_SYMBOL_GPL(videobuf_dma_init_user);
Maxim Levitsky99001322007-09-27 20:34:09 -0300197
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300198int videobuf_dma_init_kernel(struct videobuf_dmabuf *dma, int direction,
199 int nr_pages)
200{
Pawel Osciak7a022642010-03-17 04:01:04 -0300201 dprintk(1, "init kernel [%d pages]\n", nr_pages);
202
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300203 dma->direction = direction;
204 dma->vmalloc = vmalloc_32(nr_pages << PAGE_SHIFT);
205 if (NULL == dma->vmalloc) {
Pawel Osciak7a022642010-03-17 04:01:04 -0300206 dprintk(1, "vmalloc_32(%d pages) failed\n", nr_pages);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300207 return -ENOMEM;
208 }
Pawel Osciak7a022642010-03-17 04:01:04 -0300209
210 dprintk(1, "vmalloc is at addr 0x%08lx, size=%d\n",
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300211 (unsigned long)dma->vmalloc,
212 nr_pages << PAGE_SHIFT);
Pawel Osciak7a022642010-03-17 04:01:04 -0300213
214 memset(dma->vmalloc, 0, nr_pages << PAGE_SHIFT);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300215 dma->nr_pages = nr_pages;
Pawel Osciak7a022642010-03-17 04:01:04 -0300216
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300217 return 0;
218}
Pawel Osciak7a022642010-03-17 04:01:04 -0300219EXPORT_SYMBOL_GPL(videobuf_dma_init_kernel);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300220
221int videobuf_dma_init_overlay(struct videobuf_dmabuf *dma, int direction,
222 dma_addr_t addr, int nr_pages)
223{
Pawel Osciak7a022642010-03-17 04:01:04 -0300224 dprintk(1, "init overlay [%d pages @ bus 0x%lx]\n",
225 nr_pages, (unsigned long)addr);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300226 dma->direction = direction;
Pawel Osciak7a022642010-03-17 04:01:04 -0300227
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300228 if (0 == addr)
229 return -EINVAL;
230
231 dma->bus_addr = addr;
232 dma->nr_pages = nr_pages;
Pawel Osciak7a022642010-03-17 04:01:04 -0300233
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300234 return 0;
235}
Pawel Osciak7a022642010-03-17 04:01:04 -0300236EXPORT_SYMBOL_GPL(videobuf_dma_init_overlay);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300237
Laurent Pinchart95268402010-05-11 10:36:30 -0300238int videobuf_dma_map(struct device *dev, struct videobuf_dmabuf *dma)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300239{
Pawel Osciak7a022642010-03-17 04:01:04 -0300240 MAGIC_CHECK(dma->magic, MAGIC_DMABUF);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300241 BUG_ON(0 == dma->nr_pages);
242
243 if (dma->pages) {
244 dma->sglist = videobuf_pages_to_sg(dma->pages, dma->nr_pages,
245 dma->offset);
246 }
247 if (dma->vmalloc) {
Pawel Osciak7a022642010-03-17 04:01:04 -0300248 dma->sglist = videobuf_vmalloc_to_sg(dma->vmalloc,
249 dma->nr_pages);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300250 }
251 if (dma->bus_addr) {
Cohen David.A1010ed12009-05-11 11:00:20 -0300252 dma->sglist = vmalloc(sizeof(*dma->sglist));
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300253 if (NULL != dma->sglist) {
Pawel Osciak7a022642010-03-17 04:01:04 -0300254 dma->sglen = 1;
255 sg_dma_address(&dma->sglist[0]) = dma->bus_addr
256 & PAGE_MASK;
257 dma->sglist[0].offset = dma->bus_addr & ~PAGE_MASK;
258 sg_dma_len(&dma->sglist[0]) = dma->nr_pages * PAGE_SIZE;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300259 }
260 }
261 if (NULL == dma->sglist) {
Pawel Osciak7a022642010-03-17 04:01:04 -0300262 dprintk(1, "scatterlist is NULL\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300263 return -ENOMEM;
264 }
265 if (!dma->bus_addr) {
Laurent Pinchart95268402010-05-11 10:36:30 -0300266 dma->sglen = dma_map_sg(dev, dma->sglist,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300267 dma->nr_pages, dma->direction);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300268 if (0 == dma->sglen) {
269 printk(KERN_WARNING
Pawel Osciak7a022642010-03-17 04:01:04 -0300270 "%s: videobuf_map_sg failed\n", __func__);
Cohen David.A1010ed12009-05-11 11:00:20 -0300271 vfree(dma->sglist);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300272 dma->sglist = NULL;
273 dma->sglen = 0;
Figo.zhang92051b22009-06-10 23:17:27 -0300274 return -ENOMEM;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300275 }
276 }
Pawel Osciak7a022642010-03-17 04:01:04 -0300277
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300278 return 0;
279}
Pawel Osciak7a022642010-03-17 04:01:04 -0300280EXPORT_SYMBOL_GPL(videobuf_dma_map);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300281
Laurent Pinchart95268402010-05-11 10:36:30 -0300282int videobuf_dma_unmap(struct device *dev, struct videobuf_dmabuf *dma)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300283{
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300284 MAGIC_CHECK(dma->magic, MAGIC_DMABUF);
Pawel Osciak7a022642010-03-17 04:01:04 -0300285
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300286 if (!dma->sglen)
287 return 0;
288
Laurent Pinchart95268402010-05-11 10:36:30 -0300289 dma_unmap_sg(dev, dma->sglist, dma->sglen, dma->direction);
Mauro Carvalho Chehab5ddff432007-10-08 11:43:49 -0300290
Cohen David.A1010ed12009-05-11 11:00:20 -0300291 vfree(dma->sglist);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300292 dma->sglist = NULL;
293 dma->sglen = 0;
Pawel Osciak7a022642010-03-17 04:01:04 -0300294
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300295 return 0;
296}
Pawel Osciak7a022642010-03-17 04:01:04 -0300297EXPORT_SYMBOL_GPL(videobuf_dma_unmap);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300298
299int videobuf_dma_free(struct videobuf_dmabuf *dma)
300{
Pawel Osciak7a022642010-03-17 04:01:04 -0300301 int i;
302 MAGIC_CHECK(dma->magic, MAGIC_DMABUF);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300303 BUG_ON(dma->sglen);
304
305 if (dma->pages) {
Pawel Osciak7a022642010-03-17 04:01:04 -0300306 for (i = 0; i < dma->nr_pages; i++)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300307 page_cache_release(dma->pages[i]);
308 kfree(dma->pages);
309 dma->pages = NULL;
310 }
311
312 vfree(dma->vmalloc);
313 dma->vmalloc = NULL;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300314
Pawel Osciak7a022642010-03-17 04:01:04 -0300315 if (dma->bus_addr)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300316 dma->bus_addr = 0;
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300317 dma->direction = DMA_NONE;
Pawel Osciak7a022642010-03-17 04:01:04 -0300318
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300319 return 0;
320}
Pawel Osciak7a022642010-03-17 04:01:04 -0300321EXPORT_SYMBOL_GPL(videobuf_dma_free);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300322
323/* --------------------------------------------------------------------- */
324
Pawel Osciak7a022642010-03-17 04:01:04 -0300325static void videobuf_vm_open(struct vm_area_struct *vma)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300326{
327 struct videobuf_mapping *map = vma->vm_private_data;
328
Pawel Osciak7a022642010-03-17 04:01:04 -0300329 dprintk(2, "vm_open %p [count=%d,vma=%08lx-%08lx]\n", map,
330 map->count, vma->vm_start, vma->vm_end);
331
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300332 map->count++;
333}
334
Pawel Osciak7a022642010-03-17 04:01:04 -0300335static void videobuf_vm_close(struct vm_area_struct *vma)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300336{
337 struct videobuf_mapping *map = vma->vm_private_data;
338 struct videobuf_queue *q = map->q;
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300339 struct videobuf_dma_sg_memory *mem;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300340 int i;
341
Pawel Osciak7a022642010-03-17 04:01:04 -0300342 dprintk(2, "vm_close %p [count=%d,vma=%08lx-%08lx]\n", map,
343 map->count, vma->vm_start, vma->vm_end);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300344
345 map->count--;
346 if (0 == map->count) {
Pawel Osciak7a022642010-03-17 04:01:04 -0300347 dprintk(1, "munmap %p q=%p\n", map, q);
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300348 mutex_lock(&q->vb_lock);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300349 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
350 if (NULL == q->bufs[i])
351 continue;
Pawel Osciak7a022642010-03-17 04:01:04 -0300352 mem = q->bufs[i]->priv;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300353 if (!mem)
354 continue;
355
Pawel Osciak7a022642010-03-17 04:01:04 -0300356 MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300357
Mauro Carvalho Chehab851c0c92007-09-27 18:25:44 -0300358 if (q->bufs[i]->map != map)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300359 continue;
Mauro Carvalho Chehab851c0c92007-09-27 18:25:44 -0300360 q->bufs[i]->map = NULL;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300361 q->bufs[i]->baddr = 0;
Pawel Osciak7a022642010-03-17 04:01:04 -0300362 q->ops->buf_release(q, q->bufs[i]);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300363 }
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300364 mutex_unlock(&q->vb_lock);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300365 kfree(map);
366 }
367 return;
368}
369
370/*
371 * Get a anonymous page for the mapping. Make sure we can DMA to that
372 * memory location with 32bit PCI devices (i.e. don't use highmem for
373 * now ...). Bounce buffers don't work very well for the data rates
374 * video capture has.
375 */
Pawel Osciak7a022642010-03-17 04:01:04 -0300376static int videobuf_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300377{
378 struct page *page;
379
Pawel Osciak7a022642010-03-17 04:01:04 -0300380 dprintk(3, "fault: fault @ %08lx [vma %08lx-%08lx]\n",
381 (unsigned long)vmf->virtual_address,
382 vma->vm_start, vma->vm_end);
383
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300384 page = alloc_page(GFP_USER | __GFP_DMA32);
385 if (!page)
Nick Piggin105354a2007-12-07 17:57:38 -0300386 return VM_FAULT_OOM;
Guennadi Liakhovetskic0cd5012009-01-03 18:20:04 -0300387 clear_user_highpage(page, (unsigned long)vmf->virtual_address);
Nick Piggin105354a2007-12-07 17:57:38 -0300388 vmf->page = page;
Pawel Osciak7a022642010-03-17 04:01:04 -0300389
Nick Piggin105354a2007-12-07 17:57:38 -0300390 return 0;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300391}
392
Pawel Osciak7a022642010-03-17 04:01:04 -0300393static const struct vm_operations_struct videobuf_vm_ops = {
394 .open = videobuf_vm_open,
395 .close = videobuf_vm_close,
396 .fault = videobuf_vm_fault,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300397};
398
399/* ---------------------------------------------------------------------
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300400 * SG handlers for the generic methods
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300401 */
402
403/* Allocated area consists on 3 parts:
404 struct video_buffer
405 struct <driver>_buffer (cx88_buffer, saa7134_buf, ...)
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300406 struct videobuf_dma_sg_memory
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300407 */
408
Pawel Osciak33c38282010-05-11 10:36:28 -0300409static struct videobuf_buffer *__videobuf_alloc_vb(size_t size)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300410{
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300411 struct videobuf_dma_sg_memory *mem;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300412 struct videobuf_buffer *vb;
413
Pawel Osciak7a022642010-03-17 04:01:04 -0300414 vb = kzalloc(size + sizeof(*mem), GFP_KERNEL);
Pawel Osciakbee527f2010-02-22 13:10:06 -0300415 if (!vb)
416 return vb;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300417
Pawel Osciak7a022642010-03-17 04:01:04 -0300418 mem = vb->priv = ((char *)vb) + size;
419 mem->magic = MAGIC_SG_MEM;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300420
421 videobuf_dma_init(&mem->dma);
422
Pawel Osciak7a022642010-03-17 04:01:04 -0300423 dprintk(1, "%s: allocated at %p(%ld+%ld) & %p(%ld)\n",
424 __func__, vb, (long)sizeof(*vb), (long)size - sizeof(*vb),
425 mem, (long)sizeof(*mem));
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300426
427 return vb;
428}
429
Hans Verkuil037c75e2010-03-28 08:18:37 -0300430static void *__videobuf_to_vaddr(struct videobuf_buffer *buf)
Mauro Carvalho Chehab59d34482008-04-13 15:10:00 -0300431{
432 struct videobuf_dma_sg_memory *mem = buf->priv;
433 BUG_ON(!mem);
434
435 MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
436
437 return mem->dma.vmalloc;
438}
439
Pawel Osciak7a022642010-03-17 04:01:04 -0300440static int __videobuf_iolock(struct videobuf_queue *q,
441 struct videobuf_buffer *vb,
442 struct v4l2_framebuffer *fbuf)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300443{
Pawel Osciak7a022642010-03-17 04:01:04 -0300444 int err, pages;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300445 dma_addr_t bus;
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300446 struct videobuf_dma_sg_memory *mem = vb->priv;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300447 BUG_ON(!mem);
448
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300449 MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300450
451 switch (vb->memory) {
452 case V4L2_MEMORY_MMAP:
453 case V4L2_MEMORY_USERPTR:
454 if (0 == vb->baddr) {
455 /* no userspace addr -- kernel bounce buffer */
456 pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
Pawel Osciak7a022642010-03-17 04:01:04 -0300457 err = videobuf_dma_init_kernel(&mem->dma,
458 DMA_FROM_DEVICE,
459 pages);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300460 if (0 != err)
461 return err;
Maxim Levitsky99001322007-09-27 20:34:09 -0300462 } else if (vb->memory == V4L2_MEMORY_USERPTR) {
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300463 /* dma directly to userspace */
Pawel Osciak7a022642010-03-17 04:01:04 -0300464 err = videobuf_dma_init_user(&mem->dma,
465 DMA_FROM_DEVICE,
466 vb->baddr, vb->bsize);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300467 if (0 != err)
468 return err;
Maxim Levitsky99001322007-09-27 20:34:09 -0300469 } else {
470 /* NOTE: HACK: videobuf_iolock on V4L2_MEMORY_MMAP
471 buffers can only be called from videobuf_qbuf
472 we take current->mm->mmap_sem there, to prevent
473 locking inversion, so don't take it here */
474
475 err = videobuf_dma_init_user_locked(&mem->dma,
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300476 DMA_FROM_DEVICE,
Maxim Levitsky99001322007-09-27 20:34:09 -0300477 vb->baddr, vb->bsize);
478 if (0 != err)
479 return err;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300480 }
481 break;
482 case V4L2_MEMORY_OVERLAY:
483 if (NULL == fbuf)
484 return -EINVAL;
485 /* FIXME: need sanity checks for vb->boff */
486 /*
487 * Using a double cast to avoid compiler warnings when
488 * building for PAE. Compiler doesn't like direct casting
489 * of a 32 bit ptr to 64 bit integer.
490 */
491 bus = (dma_addr_t)(unsigned long)fbuf->base + vb->boff;
492 pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300493 err = videobuf_dma_init_overlay(&mem->dma, DMA_FROM_DEVICE,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300494 bus, pages);
495 if (0 != err)
496 return err;
497 break;
498 default:
499 BUG();
500 }
Laurent Pinchart95268402010-05-11 10:36:30 -0300501 err = videobuf_dma_map(q->dev, &mem->dma);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300502 if (0 != err)
503 return err;
504
505 return 0;
506}
507
508static int __videobuf_sync(struct videobuf_queue *q,
509 struct videobuf_buffer *buf)
510{
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300511 struct videobuf_dma_sg_memory *mem = buf->priv;
Mauro Carvalho Chehab97f81052010-05-05 16:23:09 -0300512 BUG_ON(!mem || !mem->dma.sglen);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300513
Mauro Carvalho Chehab97f81052010-05-05 16:23:09 -0300514 MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
515 MAGIC_CHECK(mem->dma.magic, MAGIC_DMABUF);
516
517 dma_sync_sg_for_cpu(q->dev, mem->dma.sglist,
Arnout Vandecappellefc7f8fd2010-03-17 19:53:04 -0300518 mem->dma.sglen, mem->dma.direction);
Mauro Carvalho Chehab97f81052010-05-05 16:23:09 -0300519
520 return 0;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300521}
522
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300523static int __videobuf_mmap_mapper(struct videobuf_queue *q,
Hans Verkuil0b62b732010-03-28 09:09:05 -0300524 struct videobuf_buffer *buf,
525 struct vm_area_struct *vma)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300526{
Hans Verkuil0b62b732010-03-28 09:09:05 -0300527 struct videobuf_dma_sg_memory *mem = buf->priv;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300528 struct videobuf_mapping *map;
Mauro Carvalho Chehab474675a2010-04-25 11:23:52 -0300529 unsigned int first, last, size = 0, i;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300530 int retval;
531
532 retval = -EINVAL;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300533
Brandon Philipsce540932008-04-02 18:10:59 -0300534 /* This function maintains backwards compatibility with V4L1 and will
535 * map more than one buffer if the vma length is equal to the combined
536 * size of multiple buffers than it will map them together. See
537 * VIDIOCGMBUF in the v4l spec
538 *
539 * TODO: Allow drivers to specify if they support this mode
540 */
541
Hans Verkuil0b62b732010-03-28 09:09:05 -0300542 BUG_ON(!mem);
543 MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
544
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300545 /* look for first buffer to map */
546 for (first = 0; first < VIDEO_MAX_FRAME; first++) {
Hans Verkuil0b62b732010-03-28 09:09:05 -0300547 if (buf == q->bufs[first]) {
548 size = PAGE_ALIGN(q->bufs[first]->bsize);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300549 break;
Hans Verkuil0b62b732010-03-28 09:09:05 -0300550 }
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300551 }
Hans Verkuil0b62b732010-03-28 09:09:05 -0300552
553 /* paranoia, should never happen since buf is always valid. */
Mauro Carvalho Chehab474675a2010-04-25 11:23:52 -0300554 if (!size) {
Pawel Osciak7a022642010-03-17 04:01:04 -0300555 dprintk(1, "mmap app bug: offset invalid [offset=0x%lx]\n",
Hans Verkuil0b62b732010-03-28 09:09:05 -0300556 (vma->vm_pgoff << PAGE_SHIFT));
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300557 goto done;
558 }
559
Hans Verkuil0b62b732010-03-28 09:09:05 -0300560 last = first;
561#ifdef CONFIG_VIDEO_V4L1_COMPAT
562 if (size != (vma->vm_end - vma->vm_start)) {
563 /* look for last buffer to map */
564 for (last = first + 1; last < VIDEO_MAX_FRAME; last++) {
565 if (NULL == q->bufs[last])
566 continue;
567 if (V4L2_MEMORY_MMAP != q->bufs[last]->memory)
568 continue;
569 if (q->bufs[last]->map) {
570 retval = -EBUSY;
571 goto done;
572 }
573 size += PAGE_ALIGN(q->bufs[last]->bsize);
574 if (size == (vma->vm_end - vma->vm_start))
575 break;
576 }
577 if (VIDEO_MAX_FRAME == last) {
578 dprintk(1, "mmap app bug: size invalid [size=0x%lx]\n",
579 (vma->vm_end - vma->vm_start));
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300580 goto done;
581 }
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300582 }
Hans Verkuil0b62b732010-03-28 09:09:05 -0300583#endif
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300584
585 /* create mapping + update buffer list */
586 retval = -ENOMEM;
Pawel Osciak7a022642010-03-17 04:01:04 -0300587 map = kmalloc(sizeof(struct videobuf_mapping), GFP_KERNEL);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300588 if (NULL == map)
589 goto done;
Brandon Philipsce540932008-04-02 18:10:59 -0300590
591 size = 0;
592 for (i = first; i <= last; i++) {
593 if (NULL == q->bufs[i])
594 continue;
Mauro Carvalho Chehab851c0c92007-09-27 18:25:44 -0300595 q->bufs[i]->map = map;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300596 q->bufs[i]->baddr = vma->vm_start + size;
Tuukka Toivonen7cbefad2009-07-23 10:56:25 -0300597 size += PAGE_ALIGN(q->bufs[i]->bsize);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300598 }
Brandon Philipsce540932008-04-02 18:10:59 -0300599
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300600 map->count = 1;
601 map->start = vma->vm_start;
602 map->end = vma->vm_end;
603 map->q = q;
604 vma->vm_ops = &videobuf_vm_ops;
605 vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED;
606 vma->vm_flags &= ~VM_IO; /* using shared anonymous pages */
607 vma->vm_private_data = map;
Pawel Osciak7a022642010-03-17 04:01:04 -0300608 dprintk(1, "mmap %p: q=%p %08lx-%08lx pgoff %08lx bufs %d-%d\n",
609 map, q, vma->vm_start, vma->vm_end, vma->vm_pgoff, first, last);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300610 retval = 0;
611
Pawel Osciak7a022642010-03-17 04:01:04 -0300612done:
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300613 return retval;
614}
615
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300616static struct videobuf_qtype_ops sg_ops = {
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300617 .magic = MAGIC_QTYPE_OPS,
618
Pawel Osciak33c38282010-05-11 10:36:28 -0300619 .alloc_vb = __videobuf_alloc_vb,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300620 .iolock = __videobuf_iolock,
621 .sync = __videobuf_sync,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300622 .mmap_mapper = __videobuf_mmap_mapper,
Hans Verkuil037c75e2010-03-28 08:18:37 -0300623 .vaddr = __videobuf_to_vaddr,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300624};
625
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300626void *videobuf_sg_alloc(size_t size)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300627{
628 struct videobuf_queue q;
629
630 /* Required to make generic handler to call __videobuf_alloc */
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300631 q.int_ops = &sg_ops;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300632
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300633 q.msize = size;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300634
Pawel Osciak33c38282010-05-11 10:36:28 -0300635 return videobuf_alloc_vb(&q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300636}
Pawel Osciak7a022642010-03-17 04:01:04 -0300637EXPORT_SYMBOL_GPL(videobuf_sg_alloc);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300638
Pawel Osciak7a022642010-03-17 04:01:04 -0300639void videobuf_queue_sg_init(struct videobuf_queue *q,
Jonathan Corbet38a54f32009-11-17 19:43:41 -0300640 const struct videobuf_queue_ops *ops,
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300641 struct device *dev,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300642 spinlock_t *irqlock,
643 enum v4l2_buf_type type,
644 enum v4l2_field field,
645 unsigned int msize,
646 void *priv)
647{
Mauro Carvalho Chehabd4cae5a2007-10-08 12:20:02 -0300648 videobuf_queue_core_init(q, ops, dev, irqlock, type, field, msize,
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300649 priv, &sg_ops);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300650}
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300651EXPORT_SYMBOL_GPL(videobuf_queue_sg_init);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300652