Remove subversion comments.
[microdia.git] / microdia-buf.c
blob39f326ba6e79d2eabaca939330ca0d5326364ee5
1 /**
2 * @file microdia-buf.c
3 * @author Nicolas VIVIEN
4 * @date 2008-02-01
5 * @version v0.0.0
7 * @brief Driver for Microdia USB video camera
9 * @note Copyright (C) Nicolas VIVIEN
11 * @par Licences
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/kernel.h>
31 #include <linux/version.h>
32 #include <linux/errno.h>
33 #include <linux/slab.h>
34 #include <linux/kref.h>
35 #include <linux/vmalloc.h>
37 #include <linux/usb.h>
38 #include <media/v4l2-common.h>
40 #include "microdia.h"
43 /**
44 * @var default_nbrframebuf
45 * Number of frame buffer by default
47 static int default_nbrframebuf = 3;
50 /**
51 * @param size Size of memory
53 * @returns Address on the allocated memory
55 * @brief Allocate a buffer.
57 * This function permits to allocate a buffer in memory.
59 void * microdia_rvmalloc(unsigned long size)
61 void *mem;
62 unsigned long addr;
64 size = PAGE_ALIGN(size);
65 mem = vmalloc_32(size);
67 if (!mem)
68 return NULL;
70 memset(mem, 0, size);
72 addr = (unsigned long) mem;
74 while (size > 0) {
75 SetPageReserved(vmalloc_to_page((void *) addr));
76 addr += PAGE_SIZE;
77 size -= PAGE_SIZE;
80 return mem;
84 /**
85 * @param mem Memory address
86 * @param size Size of allocated memory
88 * @brief Free a buffer
90 * This function permits to free a buffer.
92 void microdia_rvfree(void *mem, unsigned long size)
94 unsigned long addr;
96 if (!mem)
97 return;
99 addr = (unsigned long) mem;
101 while ((long) size > 0) {
102 ClearPageReserved(vmalloc_to_page((void *) addr));
103 addr += PAGE_SIZE;
104 size -= PAGE_SIZE;
107 vfree(mem);
111 /**
112 * @param dev Device structure
114 * @returns 0 if all is OK
116 * @brief Allocate all ISOC buffers.
118 * This function permits to reserved the memory for each ISOC buffer.
120 int microdia_allocate_buffers(struct usb_microdia *dev)
122 int i;
123 void *kbuf;
124 __u16 iso_max_frame_size;
126 UDIA_DEBUG("Allocate video buffers\n");
128 if (dev == NULL)
129 return -ENXIO;
131 // iso_max_frame_size should be the endpoints maximum packetsize * the highbandwidth multiplier.
133 iso_max_frame_size = max_packet_sz(le16_to_cpu(dev->isoc_in_size)) * hb_multiplier(le16_to_cpu(dev->isoc_in_size));
135 // Allocate isochronous pipe buffers
136 for (i=0; i<MAX_ISO_BUFS; i++) {
137 if (dev->isobuf[i].data == NULL) {
138 kbuf = kzalloc(iso_max_frame_size * ISO_FRAMES_PER_DESC, GFP_KERNEL);
140 if (kbuf == NULL) {
141 UDIA_ERROR("Failed to allocate iso buffer %d\n", i);
142 return -ENOMEM;
145 UDIA_DEBUG("Allocated iso buffer at %p\n", kbuf);
147 dev->isobuf[i].data = kbuf;
151 // Allocate frame buffer structure
152 if (dev->framebuf == NULL) {
153 kbuf = kzalloc(default_nbrframebuf * sizeof(struct microdia_frame_buf), GFP_KERNEL);
155 if (kbuf == NULL) {
156 UDIA_ERROR("Failed to allocate frame buffer structure\n");
157 return -ENOMEM;
160 UDIA_DEBUG("Allocated frame buffer structure at %p\n", kbuf);
162 dev->framebuf = kbuf;
165 // Create frame buffers and make circular ring
166 for (i=0; i<default_nbrframebuf; i++) {
167 if (dev->framebuf[i].data == NULL) {
168 kbuf = vmalloc(MICRODIA_FRAME_SIZE);
170 if (kbuf == NULL) {
171 UDIA_ERROR("Failed to allocate frame buffer %d\n", i);
172 return -ENOMEM;
175 UDIA_DEBUG("Allocated frame buffer %d at %p.\n", i, kbuf);
177 dev->framebuf[i].data = kbuf;
178 memset(kbuf, 0, MICRODIA_FRAME_SIZE);
182 // Allocate image buffer; double buffer for mmap()
183 kbuf = microdia_rvmalloc(dev->nbuffers * dev->len_per_image);
185 if (kbuf == NULL) {
186 UDIA_ERROR("Failed to allocate image buffer(s). needed (%d)\n",
187 dev->nbuffers * dev->len_per_image);
188 return -ENOMEM;
191 UDIA_DEBUG("Allocated image buffer at %p\n", kbuf);
193 dev->image_data = kbuf;
195 for (i = 0; i < dev->nbuffers; i++) {
196 dev->images[i].offset = i * dev->len_per_image;
197 dev->images[i].vma_use_count = 0;
200 for (; i < MICRODIA_MAX_IMAGES; i++)
201 dev->images[i].offset = 0;
203 kbuf = NULL;
205 return 0;
209 /**
210 * @param dev Device structure
212 * @returns 0 if all is OK
214 * @brief Reset all ISOC buffers.
216 * This function permits to reset all ISOC buffers.
218 int microdia_reset_buffers(struct usb_microdia *dev)
220 int i;
221 unsigned long flags;
223 UDIA_DEBUG("Reset all buffers\n");
225 spin_lock_irqsave(&dev->spinlock, flags);
227 dev->full_frames = NULL;
228 dev->full_frames_tail = NULL;
230 for (i=0; i<dev->nbuffers; i++) {
231 dev->framebuf[i].filled = 0;
232 dev->framebuf[i].errors = 0;
234 if (i > 0)
235 dev->framebuf[i].next = &dev->framebuf[i - 1];
236 else
237 dev->framebuf->next = NULL;
240 dev->empty_frames = &dev->framebuf[dev->nbuffers - 1];
241 dev->empty_frames_tail = dev->framebuf;
242 dev->read_frame = NULL;
243 dev->fill_frame = dev->empty_frames;
244 dev->empty_frames = dev->empty_frames->next;
246 dev->image_read_pos = 0;
247 dev->fill_image = 0;
249 spin_unlock_irqrestore(&dev->spinlock, flags);
251 for (i=0; i<dev->nbuffers; i++)
252 dev->image_used[i] = 0;
254 return 0;
258 /**
259 * @param dev Device structure
261 * @returns 0 if all is OK
263 * @brief Clear current buffers.
265 * This function permits to clear the memory.
267 int microdia_clear_buffers(struct usb_microdia *dev)
269 memset(dev->image_data, 0x00, dev->nbuffers * dev->len_per_image);
271 return 0;
275 /**
276 * @param dev Device structure
278 * @returns 0 if all is OK
280 * @brief Release all buffers.
282 * This function permits to release and free the memory for each ISOC buffer.
284 int microdia_free_buffers(struct usb_microdia *dev)
286 int i;
288 UDIA_DEBUG("Free buffers\n");
290 if (dev == NULL)
291 return -1;
293 // Release iso pipe buffers
294 for (i=0; i<MAX_ISO_BUFS; i++) {
295 if (dev->isobuf[i].data != NULL) {
296 kfree(dev->isobuf[i].data);
297 dev->isobuf[i].data = NULL;
301 // Release frame buffers
302 if (dev->framebuf != NULL) {
303 for (i=0; i<default_nbrframebuf; i++) {
304 if (dev->framebuf[i].data != NULL) {
305 vfree(dev->framebuf[i].data);
306 dev->framebuf[i].data = NULL;
310 kfree(dev->framebuf);
311 dev->framebuf = NULL;
314 // Release image buffers
315 if (dev->image_data != NULL)
316 microdia_rvfree(dev->image_data, dev->nbuffers * dev->len_per_image);
318 dev->image_data = NULL;
320 return 0;
324 /**
325 * @param dev Device structure
327 * @brief Prepare the next image.
329 * This function is called when an image is ready, so as to prepare the next image.
331 void microdia_next_image(struct usb_microdia *dev)
333 UDIA_STREAM("Select next image\n");
335 dev->image_used[dev->fill_image] = 0;
336 dev->fill_image = (dev->fill_image + 1) % dev->nbuffers;
340 /**
341 * @param dev Device structure
343 * @returns 0 if all is OK
345 * @brief Prepare the next frame.
347 * This function is called when a frame is ready, so as to prepare the next frame.
349 int microdia_next_frame(struct usb_microdia *dev)
351 int ret = 0;
352 unsigned long flags;
354 UDIA_STREAM("Select next frame\n");
356 spin_lock_irqsave(&dev->spinlock, flags);
358 if (dev->fill_frame != NULL) {
359 if (dev->full_frames == NULL) {
360 dev->full_frames = dev->fill_frame;
361 dev->full_frames_tail = dev->full_frames;
363 else {
364 dev->full_frames_tail->next = dev->fill_frame;
365 dev->full_frames_tail = dev->fill_frame;
369 if (dev->empty_frames != NULL) {
370 dev->fill_frame = dev->empty_frames;
371 dev->empty_frames = dev->empty_frames->next;
373 else {
374 if (dev->full_frames == NULL) {
375 UDIA_ERROR("Neither empty or full frames available!\n");
376 spin_unlock_irqrestore(&dev->spinlock, flags);
377 return -EINVAL;
380 dev->fill_frame = dev->full_frames;
381 dev->full_frames = dev->full_frames->next;
383 ret = 1;
386 dev->fill_frame->next = NULL;
388 spin_unlock_irqrestore(&dev->spinlock, flags);
390 return ret;
394 /**
395 * @param dev Device structure
397 * @returns 0 if all is OK
399 * @brief Handler frame
401 * This function gets called for the isochronous pipe. This function is only called
402 * when a frame is ready. So we have to be fast to decompress the data.
404 int microdia_handle_frame(struct usb_microdia *dev)
406 int ret = 0;
407 unsigned long flags;
409 UDIA_STREAM("Sync Handle Frame\n");
411 spin_lock_irqsave(&dev->spinlock, flags);
413 if (dev->read_frame != NULL) {
414 spin_unlock_irqrestore(&dev->spinlock, flags);
415 return ret;
418 if (dev->full_frames == NULL) {
420 else {
421 dev->read_frame = dev->full_frames;
422 dev->full_frames = dev->full_frames->next;
423 dev->read_frame->next = NULL;
426 if (dev->read_frame != NULL) {
427 spin_unlock_irqrestore(&dev->spinlock, flags);
428 ret = microdia_decompress(dev);
429 spin_lock_irqsave(&dev->spinlock, flags);
431 if (dev->empty_frames == NULL) {
432 dev->empty_frames = dev->read_frame;
433 dev->empty_frames_tail = dev->empty_frames;
435 else {
436 dev->empty_frames_tail->next = dev->read_frame;
437 dev->empty_frames_tail = dev->read_frame;
440 dev->read_frame = NULL;
443 spin_unlock_irqrestore(&dev->spinlock, flags);
445 return ret;