3 * @author Nicolas VIVIEN
7 * @brief Driver for Microdia USB video camera
9 * @note Copyright (C) Nicolas VIVIEN
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
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 * $Date: 2007-12-08 11:34:45 +0100 (sam, 08 déc 2007) $
30 * $Author: nicklas79 $
31 * $HeadURL: https://syntekdriver.svn.sourceforge.net/svnroot/syntekdriver/trunk/driver/microdia-buf.c $
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>
50 * @var default_nbrframebuf
51 * Number of frame buffer by default
53 static int default_nbrframebuf
= 3;
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 * microdia_rvmalloc(unsigned long size
)
70 size
= PAGE_ALIGN(size
);
71 mem
= vmalloc_32(size
);
78 addr
= (unsigned long) mem
;
81 SetPageReserved(vmalloc_to_page((void *) addr
));
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 microdia_rvfree(void *mem
, unsigned long size
)
105 addr
= (unsigned long) mem
;
107 while ((long) size
> 0) {
108 ClearPageReserved(vmalloc_to_page((void *) addr
));
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 microdia_allocate_buffers(struct usb_microdia
*dev
)
130 __u16 iso_max_frame_size
;
132 UDIA_DEBUG("Allocate video buffers\n");
137 // iso_max_frame_size should be the endpoints maximum packetsize * the highbandwidth multiplier.
139 iso_max_frame_size
= max_packet_sz(le16_to_cpu(dev
->isoc_in_size
)) * hb_multiplier(le16_to_cpu(dev
->isoc_in_size
));
141 // Allocate isochronous pipe buffers
142 for (i
=0; i
<MAX_ISO_BUFS
; i
++) {
143 if (dev
->isobuf
[i
].data
== NULL
) {
144 kbuf
= kzalloc(iso_max_frame_size
* ISO_FRAMES_PER_DESC
, GFP_KERNEL
);
147 UDIA_ERROR("Failed to allocate iso buffer %d\n", i
);
151 UDIA_DEBUG("Allocated iso buffer at %p\n", kbuf
);
153 dev
->isobuf
[i
].data
= kbuf
;
157 // Allocate frame buffer structure
158 if (dev
->framebuf
== NULL
) {
159 kbuf
= kzalloc(default_nbrframebuf
* sizeof(struct microdia_frame_buf
), GFP_KERNEL
);
162 UDIA_ERROR("Failed to allocate frame buffer structure\n");
166 UDIA_DEBUG("Allocated frame buffer structure at %p\n", kbuf
);
168 dev
->framebuf
= kbuf
;
171 // Create frame buffers and make circular ring
172 for (i
=0; i
<default_nbrframebuf
; i
++) {
173 if (dev
->framebuf
[i
].data
== NULL
) {
174 kbuf
= vmalloc(MICRODIA_FRAME_SIZE
);
177 UDIA_ERROR("Failed to allocate frame buffer %d\n", i
);
181 UDIA_DEBUG("Allocated frame buffer %d at %p.\n", i
, kbuf
);
183 dev
->framebuf
[i
].data
= kbuf
;
184 memset(kbuf
, 0, MICRODIA_FRAME_SIZE
);
188 // Allocate image buffer; double buffer for mmap()
189 kbuf
= microdia_rvmalloc(dev
->nbuffers
* dev
->len_per_image
);
192 UDIA_ERROR("Failed to allocate image buffer(s). needed (%d)\n",
193 dev
->nbuffers
* dev
->len_per_image
);
197 UDIA_DEBUG("Allocated image buffer at %p\n", kbuf
);
199 dev
->image_data
= kbuf
;
201 for (i
= 0; i
< dev
->nbuffers
; i
++) {
202 dev
->images
[i
].offset
= i
* dev
->len_per_image
;
203 dev
->images
[i
].vma_use_count
= 0;
206 for (; i
< MICRODIA_MAX_IMAGES
; i
++)
207 dev
->images
[i
].offset
= 0;
216 * @param dev Device structure
218 * @returns 0 if all is OK
220 * @brief Reset all ISOC buffers.
222 * This function permits to reset all ISOC buffers.
224 int microdia_reset_buffers(struct usb_microdia
*dev
)
229 UDIA_DEBUG("Reset all buffers\n");
231 spin_lock_irqsave(&dev
->spinlock
, flags
);
233 dev
->full_frames
= NULL
;
234 dev
->full_frames_tail
= NULL
;
236 for (i
=0; i
<dev
->nbuffers
; i
++) {
237 dev
->framebuf
[i
].filled
= 0;
238 dev
->framebuf
[i
].errors
= 0;
241 dev
->framebuf
[i
].next
= &dev
->framebuf
[i
- 1];
243 dev
->framebuf
->next
= NULL
;
246 dev
->empty_frames
= &dev
->framebuf
[dev
->nbuffers
- 1];
247 dev
->empty_frames_tail
= dev
->framebuf
;
248 dev
->read_frame
= NULL
;
249 dev
->fill_frame
= dev
->empty_frames
;
250 dev
->empty_frames
= dev
->empty_frames
->next
;
252 dev
->image_read_pos
= 0;
255 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
257 for (i
=0; i
<dev
->nbuffers
; i
++)
258 dev
->image_used
[i
] = 0;
265 * @param dev Device structure
267 * @returns 0 if all is OK
269 * @brief Clear current buffers.
271 * This function permits to clear the memory.
273 int microdia_clear_buffers(struct usb_microdia
*dev
)
275 memset(dev
->image_data
, 0x00, dev
->nbuffers
* dev
->len_per_image
);
282 * @param dev Device structure
284 * @returns 0 if all is OK
286 * @brief Release all buffers.
288 * This function permits to release and free the memory for each ISOC buffer.
290 int microdia_free_buffers(struct usb_microdia
*dev
)
294 UDIA_DEBUG("Free buffers\n");
299 // Release iso pipe buffers
300 for (i
=0; i
<MAX_ISO_BUFS
; i
++) {
301 if (dev
->isobuf
[i
].data
!= NULL
) {
302 kfree(dev
->isobuf
[i
].data
);
303 dev
->isobuf
[i
].data
= NULL
;
307 // Release frame buffers
308 if (dev
->framebuf
!= NULL
) {
309 for (i
=0; i
<default_nbrframebuf
; i
++) {
310 if (dev
->framebuf
[i
].data
!= NULL
) {
311 vfree(dev
->framebuf
[i
].data
);
312 dev
->framebuf
[i
].data
= NULL
;
316 kfree(dev
->framebuf
);
317 dev
->framebuf
= NULL
;
320 // Release image buffers
321 if (dev
->image_data
!= NULL
)
322 microdia_rvfree(dev
->image_data
, dev
->nbuffers
* dev
->len_per_image
);
324 dev
->image_data
= NULL
;
331 * @param dev Device structure
333 * @brief Prepare the next image.
335 * This function is called when an image is ready, so as to prepare the next image.
337 void microdia_next_image(struct usb_microdia
*dev
)
339 UDIA_STREAM("Select next image\n");
341 dev
->image_used
[dev
->fill_image
] = 0;
342 dev
->fill_image
= (dev
->fill_image
+ 1) % dev
->nbuffers
;
347 * @param dev Device structure
349 * @returns 0 if all is OK
351 * @brief Prepare the next frame.
353 * This function is called when a frame is ready, so as to prepare the next frame.
355 int microdia_next_frame(struct usb_microdia
*dev
)
360 UDIA_STREAM("Select next frame\n");
362 spin_lock_irqsave(&dev
->spinlock
, flags
);
364 if (dev
->fill_frame
!= NULL
) {
365 if (dev
->full_frames
== NULL
) {
366 dev
->full_frames
= dev
->fill_frame
;
367 dev
->full_frames_tail
= dev
->full_frames
;
370 dev
->full_frames_tail
->next
= dev
->fill_frame
;
371 dev
->full_frames_tail
= dev
->fill_frame
;
375 if (dev
->empty_frames
!= NULL
) {
376 dev
->fill_frame
= dev
->empty_frames
;
377 dev
->empty_frames
= dev
->empty_frames
->next
;
380 if (dev
->full_frames
== NULL
) {
381 UDIA_ERROR("Neither empty or full frames available!\n");
382 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
386 dev
->fill_frame
= dev
->full_frames
;
387 dev
->full_frames
= dev
->full_frames
->next
;
392 dev
->fill_frame
->next
= NULL
;
394 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
401 * @param dev Device structure
403 * @returns 0 if all is OK
405 * @brief Handler frame
407 * This function gets called for the isochronous pipe. This function is only called
408 * when a frame is ready. So we have to be fast to decompress the data.
410 int microdia_handle_frame(struct usb_microdia
*dev
)
415 UDIA_STREAM("Sync Handle Frame\n");
417 spin_lock_irqsave(&dev
->spinlock
, flags
);
419 if (dev
->read_frame
!= NULL
) {
420 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
424 if (dev
->full_frames
== NULL
) {
427 dev
->read_frame
= dev
->full_frames
;
428 dev
->full_frames
= dev
->full_frames
->next
;
429 dev
->read_frame
->next
= NULL
;
432 if (dev
->read_frame
!= NULL
) {
433 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
434 ret
= microdia_decompress(dev
);
435 spin_lock_irqsave(&dev
->spinlock
, flags
);
437 if (dev
->empty_frames
== NULL
) {
438 dev
->empty_frames
= dev
->read_frame
;
439 dev
->empty_frames_tail
= dev
->empty_frames
;
442 dev
->empty_frames_tail
->next
= dev
->read_frame
;
443 dev
->empty_frames_tail
= dev
->read_frame
;
446 dev
->read_frame
= NULL
;
449 spin_unlock_irqrestore(&dev
->spinlock
, flags
);