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 #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>
44 * @var default_nbrframebuf
45 * Number of frame buffer by default
47 static int default_nbrframebuf
= 3;
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
)
64 size
= PAGE_ALIGN(size
);
65 mem
= vmalloc_32(size
);
72 addr
= (unsigned long) mem
;
75 SetPageReserved(vmalloc_to_page((void *) addr
));
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
)
99 addr
= (unsigned long) mem
;
101 while ((long) size
> 0) {
102 ClearPageReserved(vmalloc_to_page((void *) addr
));
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
)
124 __u16 iso_max_frame_size
;
126 UDIA_DEBUG("Allocate video buffers\n");
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
);
141 UDIA_ERROR("Failed to allocate iso buffer %d\n", i
);
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
);
156 UDIA_ERROR("Failed to allocate frame buffer structure\n");
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
);
171 UDIA_ERROR("Failed to allocate frame buffer %d\n", i
);
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
);
186 UDIA_ERROR("Failed to allocate image buffer(s). needed (%d)\n",
187 dev
->nbuffers
* dev
->len_per_image
);
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;
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
)
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;
235 dev
->framebuf
[i
].next
= &dev
->framebuf
[i
- 1];
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;
249 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
251 for (i
=0; i
<dev
->nbuffers
; i
++)
252 dev
->image_used
[i
] = 0;
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
);
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
)
288 UDIA_DEBUG("Free buffers\n");
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
;
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
;
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
)
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
;
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
;
374 if (dev
->full_frames
== NULL
) {
375 UDIA_ERROR("Neither empty or full frames available!\n");
376 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
380 dev
->fill_frame
= dev
->full_frames
;
381 dev
->full_frames
= dev
->full_frames
->next
;
386 dev
->fill_frame
->next
= NULL
;
388 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
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
)
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
);
418 if (dev
->full_frames
== NULL
) {
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
;
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
);