Commit | Line | Data |
---|---|---|
87b9ad07 MCC |
1 | /* |
2 | * helper functions for vmalloc video4linux capture buffers | |
3 | * | |
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 | * This program is free software; you can redistribute it and/or modify | |
12 | * it under the terms of the GNU General Public License as published by | |
13 | * the Free Software Foundation; either version 2 | |
14 | */ | |
15 | ||
16 | #include <linux/init.h> | |
17 | #include <linux/module.h> | |
18 | #include <linux/moduleparam.h> | |
19 | #include <linux/slab.h> | |
20 | #include <linux/interrupt.h> | |
21 | ||
22 | #include <linux/pci.h> | |
23 | #include <linux/vmalloc.h> | |
24 | #include <linux/pagemap.h> | |
25 | #include <asm/page.h> | |
26 | #include <asm/pgtable.h> | |
27 | ||
28 | #include <media/videobuf-vmalloc.h> | |
29 | ||
30 | #define MAGIC_DMABUF 0x17760309 | |
31 | #define MAGIC_VMAL_MEM 0x18221223 | |
32 | ||
33 | #define MAGIC_CHECK(is,should) if (unlikely((is) != (should))) \ | |
34 | { printk(KERN_ERR "magic mismatch: %x (expected %x)\n",is,should); BUG(); } | |
35 | ||
ff699e6b | 36 | static int debug; |
87b9ad07 MCC |
37 | module_param(debug, int, 0644); |
38 | ||
39 | MODULE_DESCRIPTION("helper module to manage video4linux vmalloc buffers"); | |
40 | MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>"); | |
41 | MODULE_LICENSE("GPL"); | |
42 | ||
43 | #define dprintk(level, fmt, arg...) if (debug >= level) \ | |
493977f0 | 44 | printk(KERN_DEBUG "vbuf-vmalloc: " fmt , ## arg) |
87b9ad07 MCC |
45 | |
46 | ||
47 | /***************************************************************************/ | |
48 | ||
49 | static void | |
50 | videobuf_vm_open(struct vm_area_struct *vma) | |
51 | { | |
52 | struct videobuf_mapping *map = vma->vm_private_data; | |
53 | ||
0b29669c | 54 | dprintk(2,"vm_open %p [count=%u,vma=%08lx-%08lx]\n",map, |
87b9ad07 MCC |
55 | map->count,vma->vm_start,vma->vm_end); |
56 | ||
57 | map->count++; | |
58 | } | |
59 | ||
60 | static void | |
61 | videobuf_vm_close(struct vm_area_struct *vma) | |
62 | { | |
63 | struct videobuf_mapping *map = vma->vm_private_data; | |
64 | struct videobuf_queue *q = map->q; | |
87b9ad07 MCC |
65 | int i; |
66 | ||
0b29669c | 67 | dprintk(2,"vm_close %p [count=%u,vma=%08lx-%08lx]\n",map, |
87b9ad07 MCC |
68 | map->count,vma->vm_start,vma->vm_end); |
69 | ||
70 | map->count--; | |
71 | if (0 == map->count) { | |
72 | dprintk(1,"munmap %p q=%p\n",map,q); | |
64f9477f | 73 | mutex_lock(&q->vb_lock); |
87b9ad07 MCC |
74 | for (i = 0; i < VIDEO_MAX_FRAME; i++) { |
75 | if (NULL == q->bufs[i]) | |
76 | continue; | |
87b9ad07 | 77 | |
851c0c96 | 78 | if (q->bufs[i]->map != map) |
87b9ad07 | 79 | continue; |
123f8ef6 | 80 | |
851c0c96 | 81 | q->bufs[i]->map = NULL; |
87b9ad07 | 82 | q->bufs[i]->baddr = 0; |
87b9ad07 | 83 | } |
64f9477f | 84 | mutex_unlock(&q->vb_lock); |
87b9ad07 MCC |
85 | kfree(map); |
86 | } | |
87 | return; | |
88 | } | |
89 | ||
90 | static struct vm_operations_struct videobuf_vm_ops = | |
91 | { | |
92 | .open = videobuf_vm_open, | |
93 | .close = videobuf_vm_close, | |
94 | }; | |
95 | ||
96 | /* --------------------------------------------------------------------- | |
97 | * vmalloc handlers for the generic methods | |
98 | */ | |
99 | ||
100 | /* Allocated area consists on 3 parts: | |
101 | struct video_buffer | |
102 | struct <driver>_buffer (cx88_buffer, saa7134_buf, ...) | |
0705135e | 103 | struct videobuf_dma_sg_memory |
87b9ad07 MCC |
104 | */ |
105 | ||
106 | static void *__videobuf_alloc(size_t size) | |
107 | { | |
384b835a | 108 | struct videobuf_vmalloc_memory *mem; |
87b9ad07 MCC |
109 | struct videobuf_buffer *vb; |
110 | ||
111 | vb = kzalloc(size+sizeof(*mem),GFP_KERNEL); | |
112 | ||
113 | mem = vb->priv = ((char *)vb)+size; | |
114 | mem->magic=MAGIC_VMAL_MEM; | |
115 | ||
116 | dprintk(1,"%s: allocated at %p(%ld+%ld) & %p(%ld)\n", | |
117 | __FUNCTION__,vb,(long)sizeof(*vb),(long)size-sizeof(*vb), | |
118 | mem,(long)sizeof(*mem)); | |
119 | ||
120 | return vb; | |
121 | } | |
122 | ||
123 | static int __videobuf_iolock (struct videobuf_queue* q, | |
124 | struct videobuf_buffer *vb, | |
125 | struct v4l2_framebuffer *fbuf) | |
126 | { | |
127 | int pages; | |
384b835a | 128 | struct videobuf_vmalloc_memory *mem=vb->priv; |
87b9ad07 MCC |
129 | |
130 | BUG_ON(!mem); | |
131 | ||
132 | MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM); | |
133 | ||
87b9ad07 MCC |
134 | pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT; |
135 | ||
136 | /* Currently, doesn't support V4L2_MEMORY_OVERLAY */ | |
137 | if ((vb->memory != V4L2_MEMORY_MMAP) && | |
138 | (vb->memory != V4L2_MEMORY_USERPTR) ) { | |
139 | printk(KERN_ERR "Method currently unsupported.\n"); | |
140 | return -EINVAL; | |
141 | } | |
142 | ||
143 | /* FIXME: should be tested with kernel mmap mem */ | |
144 | mem->vmalloc=vmalloc_user (PAGE_ALIGN(vb->size)); | |
145 | if (NULL == mem->vmalloc) { | |
e78dcf55 | 146 | printk(KERN_ERR "vmalloc (%d pages) failed\n",pages); |
87b9ad07 MCC |
147 | return -ENOMEM; |
148 | } | |
149 | ||
150 | dprintk(1,"vmalloc is at addr 0x%08lx, size=%d\n", | |
151 | (unsigned long)mem->vmalloc, | |
152 | pages << PAGE_SHIFT); | |
153 | ||
154 | /* It seems that some kernel versions need to do remap *after* | |
155 | the mmap() call | |
156 | */ | |
157 | if (mem->vma) { | |
158 | int retval=remap_vmalloc_range(mem->vma, mem->vmalloc,0); | |
159 | kfree(mem->vma); | |
160 | mem->vma=NULL; | |
161 | if (retval<0) { | |
162 | dprintk(1,"mmap app bug: remap_vmalloc_range area %p error %d\n", | |
163 | mem->vmalloc,retval); | |
164 | return retval; | |
165 | } | |
166 | } | |
167 | ||
168 | return 0; | |
169 | } | |
170 | ||
171 | static int __videobuf_sync(struct videobuf_queue *q, | |
172 | struct videobuf_buffer *buf) | |
173 | { | |
174 | return 0; | |
175 | } | |
176 | ||
177 | static int __videobuf_mmap_free(struct videobuf_queue *q) | |
178 | { | |
179 | unsigned int i; | |
180 | ||
181 | for (i = 0; i < VIDEO_MAX_FRAME; i++) { | |
182 | if (q->bufs[i]) { | |
851c0c96 | 183 | if (q->bufs[i]->map) |
87b9ad07 MCC |
184 | return -EBUSY; |
185 | } | |
186 | } | |
187 | ||
188 | return 0; | |
189 | } | |
190 | ||
191 | static int __videobuf_mmap_mapper(struct videobuf_queue *q, | |
192 | struct vm_area_struct *vma) | |
193 | { | |
384b835a | 194 | struct videobuf_vmalloc_memory *mem; |
87b9ad07 MCC |
195 | struct videobuf_mapping *map; |
196 | unsigned int first; | |
197 | int retval; | |
198 | unsigned long offset = vma->vm_pgoff << PAGE_SHIFT; | |
199 | ||
200 | if (! (vma->vm_flags & VM_WRITE) || ! (vma->vm_flags & VM_SHARED)) | |
201 | return -EINVAL; | |
202 | ||
203 | /* look for first buffer to map */ | |
204 | for (first = 0; first < VIDEO_MAX_FRAME; first++) { | |
205 | if (NULL == q->bufs[first]) | |
206 | continue; | |
207 | ||
208 | if (V4L2_MEMORY_MMAP != q->bufs[first]->memory) | |
209 | continue; | |
210 | if (q->bufs[first]->boff == offset) | |
211 | break; | |
212 | } | |
213 | if (VIDEO_MAX_FRAME == first) { | |
214 | dprintk(1,"mmap app bug: offset invalid [offset=0x%lx]\n", | |
215 | (vma->vm_pgoff << PAGE_SHIFT)); | |
216 | return -EINVAL; | |
217 | } | |
87b9ad07 MCC |
218 | |
219 | /* create mapping + update buffer list */ | |
0b29669c | 220 | map = q->bufs[first]->map = kzalloc(sizeof(struct videobuf_mapping),GFP_KERNEL); |
87b9ad07 MCC |
221 | if (NULL == map) |
222 | return -ENOMEM; | |
223 | ||
224 | map->start = vma->vm_start; | |
225 | map->end = vma->vm_end; | |
226 | map->q = q; | |
227 | ||
228 | q->bufs[first]->baddr = vma->vm_start; | |
229 | ||
230 | vma->vm_ops = &videobuf_vm_ops; | |
231 | vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED; | |
232 | vma->vm_private_data = map; | |
233 | ||
851c0c96 MCC |
234 | mem=q->bufs[first]->priv; |
235 | BUG_ON (!mem); | |
236 | MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM); | |
237 | ||
87b9ad07 MCC |
238 | /* Try to remap memory */ |
239 | retval=remap_vmalloc_range(vma, mem->vmalloc,0); | |
240 | if (retval<0) { | |
241 | dprintk(1,"mmap: postponing remap_vmalloc_range\n"); | |
851c0c96 | 242 | |
87b9ad07 MCC |
243 | mem->vma=kmalloc(sizeof(*vma),GFP_KERNEL); |
244 | if (!mem->vma) { | |
245 | kfree(map); | |
851c0c96 | 246 | q->bufs[first]->map=NULL; |
87b9ad07 MCC |
247 | return -ENOMEM; |
248 | } | |
249 | memcpy(mem->vma,vma,sizeof(*vma)); | |
250 | } | |
251 | ||
252 | dprintk(1,"mmap %p: q=%p %08lx-%08lx (%lx) pgoff %08lx buf %d\n", | |
253 | map,q,vma->vm_start,vma->vm_end, | |
254 | (long int) q->bufs[first]->bsize, | |
255 | vma->vm_pgoff,first); | |
256 | ||
257 | videobuf_vm_open(vma); | |
258 | ||
259 | return (0); | |
260 | } | |
261 | ||
87b9ad07 MCC |
262 | static int __videobuf_copy_to_user ( struct videobuf_queue *q, |
263 | char __user *data, size_t count, | |
264 | int nonblocking ) | |
265 | { | |
384b835a | 266 | struct videobuf_vmalloc_memory *mem=q->read_buf->priv; |
87b9ad07 MCC |
267 | BUG_ON (!mem); |
268 | MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM); | |
269 | ||
270 | BUG_ON (!mem->vmalloc); | |
271 | ||
272 | /* copy to userspace */ | |
273 | if (count > q->read_buf->size - q->read_off) | |
274 | count = q->read_buf->size - q->read_off; | |
275 | ||
276 | if (copy_to_user(data, mem->vmalloc+q->read_off, count)) | |
277 | return -EFAULT; | |
278 | ||
279 | return count; | |
280 | } | |
281 | ||
282 | static int __videobuf_copy_stream ( struct videobuf_queue *q, | |
283 | char __user *data, size_t count, size_t pos, | |
284 | int vbihack, int nonblocking ) | |
285 | { | |
286 | unsigned int *fc; | |
384b835a | 287 | struct videobuf_vmalloc_memory *mem=q->read_buf->priv; |
87b9ad07 MCC |
288 | BUG_ON (!mem); |
289 | MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM); | |
290 | ||
291 | if (vbihack) { | |
292 | /* dirty, undocumented hack -- pass the frame counter | |
293 | * within the last four bytes of each vbi data block. | |
294 | * We need that one to maintain backward compatibility | |
295 | * to all vbi decoding software out there ... */ | |
296 | fc = (unsigned int*)mem->vmalloc; | |
297 | fc += (q->read_buf->size>>2) -1; | |
298 | *fc = q->read_buf->field_count >> 1; | |
299 | dprintk(1,"vbihack: %d\n",*fc); | |
300 | } | |
301 | ||
302 | /* copy stuff using the common method */ | |
303 | count = __videobuf_copy_to_user (q,data,count,nonblocking); | |
304 | ||
305 | if ( (count==-EFAULT) && (0 == pos) ) | |
306 | return -EFAULT; | |
307 | ||
308 | return count; | |
309 | } | |
310 | ||
311 | static struct videobuf_qtype_ops qops = { | |
312 | .magic = MAGIC_QTYPE_OPS, | |
313 | ||
314 | .alloc = __videobuf_alloc, | |
315 | .iolock = __videobuf_iolock, | |
316 | .sync = __videobuf_sync, | |
317 | .mmap_free = __videobuf_mmap_free, | |
318 | .mmap_mapper = __videobuf_mmap_mapper, | |
13bcd5d0 | 319 | .video_copy_to_user = __videobuf_copy_to_user, |
87b9ad07 MCC |
320 | .copy_stream = __videobuf_copy_stream, |
321 | }; | |
322 | ||
323 | void videobuf_queue_vmalloc_init(struct videobuf_queue* q, | |
324 | struct videobuf_queue_ops *ops, | |
325 | void *dev, | |
326 | spinlock_t *irqlock, | |
327 | enum v4l2_buf_type type, | |
328 | enum v4l2_field field, | |
329 | unsigned int msize, | |
330 | void *priv) | |
331 | { | |
d4cae5a5 MCC |
332 | videobuf_queue_core_init(q, ops, dev, irqlock, type, field, msize, |
333 | priv, &qops); | |
87b9ad07 MCC |
334 | } |
335 | ||
336 | EXPORT_SYMBOL_GPL(videobuf_queue_vmalloc_init); | |
337 | ||
338 | void *videobuf_to_vmalloc (struct videobuf_buffer *buf) | |
339 | { | |
384b835a | 340 | struct videobuf_vmalloc_memory *mem=buf->priv; |
87b9ad07 MCC |
341 | BUG_ON (!mem); |
342 | MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM); | |
343 | ||
344 | return mem->vmalloc; | |
345 | } | |
346 | EXPORT_SYMBOL_GPL(videobuf_to_vmalloc); | |
347 | ||
348 | void videobuf_vmalloc_free (struct videobuf_buffer *buf) | |
349 | { | |
384b835a | 350 | struct videobuf_vmalloc_memory *mem=buf->priv; |
87b9ad07 MCC |
351 | BUG_ON (!mem); |
352 | ||
353 | MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM); | |
354 | ||
355 | vfree(mem->vmalloc); | |
c520a497 | 356 | mem->vmalloc=NULL; |
87b9ad07 MCC |
357 | |
358 | return; | |
359 | } | |
360 | EXPORT_SYMBOL_GPL(videobuf_vmalloc_free); | |
361 | ||
362 | /* | |
363 | * Local variables: | |
364 | * c-basic-offset: 8 | |
365 | * End: | |
366 | */ |