3 * @author Nicolas VIVIEN
7 * @brief Driver for Syntek 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
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 * stk11xx_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 stk11xx_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 stk11xx_allocate_buffers(struct usb_stk11xx
*dev
)
131 STK_DEBUG("Allocate video buffers\n");
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
);
142 STK_ERROR("Failed to allocate iso buffer %d\n", i
);
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
);
157 STK_ERROR("Failed to allocate frame buffer structure\n");
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
);
172 STK_ERROR("Failed to allocate frame buffer %d\n", i
);
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
);
187 STK_ERROR("Failed to allocate image buffer(s). needed (%d)\n",
188 dev
->nbuffers
* dev
->len_per_image
);
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;
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
)
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;
236 dev
->framebuf
[i
].next
= &dev
->framebuf
[i
- 1];
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;
250 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
252 for (i
=0; i
<dev
->nbuffers
; i
++)
253 dev
->image_used
[i
] = 0;
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
);
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
)
289 STK_DEBUG("Free buffers\n");
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
;
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
;
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
)
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
;
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
;
375 if (dev
->full_frames
== NULL
) {
376 STK_ERROR("Neither empty or full frames available!\n");
377 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
381 dev
->fill_frame
= dev
->full_frames
;
382 dev
->full_frames
= dev
->full_frames
->next
;
387 dev
->fill_frame
->next
= NULL
;
389 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
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
)
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
);
419 if (dev
->full_frames
== NULL
) {
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
;
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
);