Release 1.2.1
[syntekdriver.git] / stk11xx-buf.c
blob097b64bc9c13c45311843d96732499933c472689
1 /**
2 * @file stk11xx-buf.c
3 * @author Nicolas VIVIEN
4 * @date 2006-10-23
5 * @version v1.2.0
7 * @brief Driver for Syntek 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
27 * @par SubVersion
28 * $Date$
29 * $Revision$
30 * $Author$
31 * $HeadURL$
34 #include <linux/module.h>
35 #include <linux/init.h>
36 #include <linux/kernel.h>
37 #include <linux/version.h>
38 #include <linux/errno.h>
39 #include <linux/slab.h>
40 #include <linux/kref.h>
41 #include <linux/vmalloc.h>
43 #include <linux/usb.h>
44 #include <media/v4l2-common.h>
46 #include "stk11xx.h"
49 /**
50 * @var default_nbrframebuf
51 * Number of frame buffer by default
53 static int default_nbrframebuf = 3;
56 /**
57 * @param size Size of memory
59 * @returns Address on the allocated memory
61 * @brief Allocate a buffer.
63 * This function permits to allocate a buffer in memory.
65 void * stk11xx_rvmalloc(unsigned long size)
67 void *mem;
68 unsigned long addr;
70 size = PAGE_ALIGN(size);
71 mem = vmalloc_32(size);
73 if (!mem)
74 return NULL;
76 memset(mem, 0, size);
78 addr = (unsigned long) mem;
80 while (size > 0) {
81 SetPageReserved(vmalloc_to_page((void *) addr));
82 addr += PAGE_SIZE;
83 size -= PAGE_SIZE;
86 return mem;
90 /**
91 * @param mem Memory address
92 * @param size Size of allocated memory
94 * @brief Free a buffer
96 * This function permits to free a buffer.
98 void stk11xx_rvfree(void *mem, unsigned long size)
100 unsigned long addr;
102 if (!mem)
103 return;
105 addr = (unsigned long) mem;
107 while ((long) size > 0) {
108 ClearPageReserved(vmalloc_to_page((void *) addr));
109 addr += PAGE_SIZE;
110 size -= PAGE_SIZE;
113 vfree(mem);
117 /**
118 * @param dev Device structure
120 * @returns 0 if all is OK
122 * @brief Allocate all ISOC buffers.
124 * This function permits to reserved the memory for each ISOC buffer.
126 int stk11xx_allocate_buffers(struct usb_stk11xx *dev)
128 int i;
129 void *kbuf;
131 STK_DEBUG("Allocate video buffers\n");
133 if (dev == NULL)
134 return -ENXIO;
136 // Allocate isochronous pipe buffers
137 for (i=0; i<MAX_ISO_BUFS; i++) {
138 if (dev->isobuf[i].data == NULL) {
139 kbuf = kzalloc(ISO_BUFFER_SIZE, GFP_KERNEL);
141 if (kbuf == NULL) {
142 STK_ERROR("Failed to allocate iso buffer %d\n", i);
143 return -ENOMEM;
146 STK_DEBUG("Allocated iso buffer at %p\n", kbuf);
148 dev->isobuf[i].data = kbuf;
152 // Allocate frame buffer structure
153 if (dev->framebuf == NULL) {
154 kbuf = kzalloc(default_nbrframebuf * sizeof(struct stk11xx_frame_buf), GFP_KERNEL);
156 if (kbuf == NULL) {
157 STK_ERROR("Failed to allocate frame buffer structure\n");
158 return -ENOMEM;
161 STK_DEBUG("Allocated frame buffer structure at %p\n", kbuf);
163 dev->framebuf = kbuf;
166 // Create frame buffers and make circular ring
167 for (i=0; i<default_nbrframebuf; i++) {
168 if (dev->framebuf[i].data == NULL) {
169 kbuf = vmalloc(STK11XX_FRAME_SIZE);
171 if (kbuf == NULL) {
172 STK_ERROR("Failed to allocate frame buffer %d\n", i);
173 return -ENOMEM;
176 STK_DEBUG("Allocated frame buffer %d at %p.\n", i, kbuf);
178 dev->framebuf[i].data = kbuf;
179 memset(kbuf, 0, STK11XX_FRAME_SIZE);
183 // Allocate image buffer; double buffer for mmap()
184 kbuf = stk11xx_rvmalloc(dev->nbuffers * dev->len_per_image);
186 if (kbuf == NULL) {
187 STK_ERROR("Failed to allocate image buffer(s). needed (%d)\n",
188 dev->nbuffers * dev->len_per_image);
189 return -ENOMEM;
192 STK_DEBUG("Allocated image buffer at %p\n", kbuf);
194 dev->image_data = kbuf;
196 for (i = 0; i < dev->nbuffers; i++) {
197 dev->images[i].offset = i * dev->len_per_image;
198 dev->images[i].vma_use_count = 0;
201 for (; i < STK11XX_MAX_IMAGES; i++)
202 dev->images[i].offset = 0;
204 kbuf = NULL;
206 return 0;
210 /**
211 * @param dev Device structure
213 * @returns 0 if all is OK
215 * @brief Reset all ISOC buffers.
217 * This function permits to reset all ISOC buffers.
219 int stk11xx_reset_buffers(struct usb_stk11xx *dev)
221 int i;
222 unsigned long flags;
224 STK_DEBUG("Reset all buffers\n");
226 spin_lock_irqsave(&dev->spinlock, flags);
228 dev->full_frames = NULL;
229 dev->full_frames_tail = NULL;
231 for (i=0; i<dev->nbuffers; i++) {
232 dev->framebuf[i].filled = 0;
233 dev->framebuf[i].errors = 0;
235 if (i > 0)
236 dev->framebuf[i].next = &dev->framebuf[i - 1];
237 else
238 dev->framebuf->next = NULL;
241 dev->empty_frames = &dev->framebuf[dev->nbuffers - 1];
242 dev->empty_frames_tail = dev->framebuf;
243 dev->read_frame = NULL;
244 dev->fill_frame = dev->empty_frames;
245 dev->empty_frames = dev->empty_frames->next;
247 dev->image_read_pos = 0;
248 dev->fill_image = 0;
250 spin_unlock_irqrestore(&dev->spinlock, flags);
252 for (i=0; i<dev->nbuffers; i++)
253 dev->image_used[i] = 0;
255 return 0;
259 /**
260 * @param dev Device structure
262 * @returns 0 if all is OK
264 * @brief Clear current buffers.
266 * This function permits to clear the memory.
268 int stk11xx_clear_buffers(struct usb_stk11xx *dev)
270 memset(dev->image_data, 0x00, dev->nbuffers * dev->len_per_image);
272 return 0;
276 /**
277 * @param dev Device structure
279 * @returns 0 if all is OK
281 * @brief Release all buffers.
283 * This function permits to release and free the memory for each ISOC buffer.
285 int stk11xx_free_buffers(struct usb_stk11xx *dev)
287 int i;
289 STK_DEBUG("Free buffers\n");
291 if (dev == NULL)
292 return -1;
294 // Release iso pipe buffers
295 for (i=0; i<MAX_ISO_BUFS; i++) {
296 if (dev->isobuf[i].data != NULL) {
297 kfree(dev->isobuf[i].data);
298 dev->isobuf[i].data = NULL;
302 // Release frame buffers
303 if (dev->framebuf != NULL) {
304 for (i=0; i<default_nbrframebuf; i++) {
305 if (dev->framebuf[i].data != NULL) {
306 vfree(dev->framebuf[i].data);
307 dev->framebuf[i].data = NULL;
311 kfree(dev->framebuf);
312 dev->framebuf = NULL;
315 // Release image buffers
316 if (dev->image_data != NULL)
317 stk11xx_rvfree(dev->image_data, dev->nbuffers * dev->len_per_image);
319 dev->image_data = NULL;
321 return 0;
325 /**
326 * @param dev Device structure
328 * @brief Prepare the next image.
330 * This function is called when an image is ready, so as to prepare the next image.
332 void stk11xx_next_image(struct usb_stk11xx *dev)
334 STK_STREAM("Select next image\n");
336 dev->image_used[dev->fill_image] = 0;
337 dev->fill_image = (dev->fill_image + 1) % dev->nbuffers;
341 /**
342 * @param dev Device structure
344 * @returns 0 if all is OK
346 * @brief Prepare the next frame.
348 * This function is called when a frame is ready, so as to prepare the next frame.
350 int stk11xx_next_frame(struct usb_stk11xx *dev)
352 int ret = 0;
353 unsigned long flags;
355 STK_STREAM("Select next frame\n");
357 spin_lock_irqsave(&dev->spinlock, flags);
359 if (dev->fill_frame != NULL) {
360 if (dev->full_frames == NULL) {
361 dev->full_frames = dev->fill_frame;
362 dev->full_frames_tail = dev->full_frames;
364 else {
365 dev->full_frames_tail->next = dev->fill_frame;
366 dev->full_frames_tail = dev->fill_frame;
370 if (dev->empty_frames != NULL) {
371 dev->fill_frame = dev->empty_frames;
372 dev->empty_frames = dev->empty_frames->next;
374 else {
375 if (dev->full_frames == NULL) {
376 STK_ERROR("Neither empty or full frames available!\n");
377 spin_unlock_irqrestore(&dev->spinlock, flags);
378 return -EINVAL;
381 dev->fill_frame = dev->full_frames;
382 dev->full_frames = dev->full_frames->next;
384 ret = 1;
387 dev->fill_frame->next = NULL;
389 spin_unlock_irqrestore(&dev->spinlock, flags);
391 return ret;
395 /**
396 * @param dev Device structure
398 * @returns 0 if all is OK
400 * @brief Handler frame
402 * This function gets called for the isochronous pipe. This function is only called
403 * when a frame is ready. So we have to be fast to decompress the data.
405 int stk11xx_handle_frame(struct usb_stk11xx *dev)
407 int ret = 0;
408 unsigned long flags;
410 STK_STREAM("Sync Handle Frame\n");
412 spin_lock_irqsave(&dev->spinlock, flags);
414 if (dev->read_frame != NULL) {
415 spin_unlock_irqrestore(&dev->spinlock, flags);
416 return ret;
419 if (dev->full_frames == NULL) {
421 else {
422 dev->read_frame = dev->full_frames;
423 dev->full_frames = dev->full_frames->next;
424 dev->read_frame->next = NULL;
427 if (dev->read_frame != NULL) {
428 spin_unlock_irqrestore(&dev->spinlock, flags);
429 ret = stk11xx_decompress(dev);
430 spin_lock_irqsave(&dev->spinlock, flags);
432 if (dev->empty_frames == NULL) {
433 dev->empty_frames = dev->read_frame;
434 dev->empty_frames_tail = dev->empty_frames;
436 else {
437 dev->empty_frames_tail->next = dev->read_frame;
438 dev->empty_frames_tail = dev->read_frame;
441 dev->read_frame = NULL;
444 spin_unlock_irqrestore(&dev->spinlock, flags);
446 dev_stk11xx_watchdog_camera(dev);
448 return ret;