SyntekUSBVideoCamera

stk11xx-buf.c

Go to the documentation of this file.
00001 
00034 #include <linux/module.h>
00035 #include <linux/init.h>
00036 #include <linux/kernel.h>
00037 #include <linux/version.h>
00038 #include <linux/errno.h>
00039 #include <linux/slab.h>
00040 #include <linux/kref.h>
00041 #include <linux/vmalloc.h>
00042 #include <linux/mm.h>
00043 
00044 #include <linux/usb.h>
00045 #include <media/v4l2-common.h>
00046 #include <media/v4l2-ioctl.h>
00047 
00048 #include "stk11xx.h"
00049 
00050 
00055 static int default_nbrframebuf = 3;
00056 
00057 
00067 void * stk11xx_rvmalloc(unsigned long size)
00068 {
00069     void *mem;
00070     unsigned long addr;
00071 
00072     size = PAGE_ALIGN(size);
00073     mem = vmalloc_32(size);
00074 
00075     if (!mem)
00076         return NULL;
00077 
00078     memset(mem, 0, size);
00079 
00080     addr = (unsigned long) mem;
00081 
00082     while (size > 0) {
00083         SetPageReserved(vmalloc_to_page((void *) addr));
00084         addr += PAGE_SIZE;
00085         size -= PAGE_SIZE;
00086     }
00087 
00088     return mem;
00089 }
00090 
00091 
00100 void stk11xx_rvfree(void *mem, unsigned long size)
00101 {
00102     unsigned long addr;
00103 
00104     if (!mem)
00105         return;
00106 
00107     addr = (unsigned long) mem;
00108 
00109     while ((long) size > 0) {
00110         ClearPageReserved(vmalloc_to_page((void *) addr));
00111         addr += PAGE_SIZE;
00112         size -= PAGE_SIZE;
00113     }
00114 
00115     vfree(mem);
00116 }
00117 
00118 
00128 int stk11xx_allocate_buffers(struct usb_stk11xx *dev)
00129 {
00130     int i;
00131     void *kbuf;
00132 
00133     STK_DEBUG("Allocate video buffers\n");
00134 
00135     if (dev == NULL)
00136         return -ENXIO;
00137 
00138     // Allocate isochronous pipe buffers
00139     for (i=0; i<MAX_ISO_BUFS; i++) {
00140         if (dev->isobuf[i].data == NULL) {
00141             kbuf = kzalloc(ISO_BUFFER_SIZE, GFP_KERNEL);
00142 
00143             if (kbuf == NULL) {
00144                 STK_ERROR("Failed to allocate iso buffer %d\n", i);
00145                 return -ENOMEM;
00146             }
00147 
00148             STK_DEBUG("Allocated iso buffer at %p\n", kbuf);
00149 
00150             dev->isobuf[i].data = kbuf;
00151         }
00152     }
00153 
00154     // Allocate frame buffer structure
00155     if (dev->framebuf == NULL) {
00156         kbuf = kzalloc(default_nbrframebuf * sizeof(struct stk11xx_frame_buf), GFP_KERNEL);
00157 
00158         if (kbuf == NULL) {
00159             STK_ERROR("Failed to allocate frame buffer structure\n");
00160             return -ENOMEM;
00161         }
00162 
00163         STK_DEBUG("Allocated frame buffer structure at %p\n", kbuf);
00164 
00165         dev->framebuf = kbuf;
00166     }
00167 
00168     // Create frame buffers and make circular ring
00169     for (i=0; i<default_nbrframebuf; i++) {
00170         if (dev->framebuf[i].data == NULL) {
00171             kbuf = vmalloc(STK11XX_FRAME_SIZE);
00172 
00173             if (kbuf == NULL) {
00174                 STK_ERROR("Failed to allocate frame buffer %d\n", i);
00175                 return -ENOMEM;
00176             }
00177 
00178             STK_DEBUG("Allocated frame buffer %d at %p.\n", i, kbuf);
00179 
00180             dev->framebuf[i].data = kbuf;
00181             memset(kbuf, 0, STK11XX_FRAME_SIZE);
00182         }
00183     }
00184 
00185     // Allocate image buffer; double buffer for mmap()
00186     kbuf = stk11xx_rvmalloc(dev->nbuffers * dev->len_per_image);
00187 
00188     if (kbuf == NULL) {
00189         STK_ERROR("Failed to allocate image buffer(s). needed (%d)\n",
00190                 dev->nbuffers * dev->len_per_image);
00191         return -ENOMEM;
00192     }
00193 
00194     STK_DEBUG("Allocated image buffer at %p\n", kbuf);
00195 
00196     dev->image_data = kbuf;
00197 
00198     for (i = 0; i < dev->nbuffers; i++) {
00199         dev->images[i].offset = i * dev->len_per_image;
00200         dev->images[i].vma_use_count = 0;
00201     }
00202 
00203     for (; i < STK11XX_MAX_IMAGES; i++)
00204         dev->images[i].offset = 0;
00205 
00206     kbuf = NULL;
00207     
00208     return 0;
00209 }
00210 
00211 
00221 int stk11xx_reset_buffers(struct usb_stk11xx *dev)
00222 {
00223     int i;
00224     unsigned long flags;
00225 
00226     STK_DEBUG("Reset all buffers\n");
00227 
00228     spin_lock_irqsave(&dev->spinlock, flags);
00229 
00230     dev->full_frames = NULL;
00231     dev->full_frames_tail = NULL;
00232 
00233     for (i=0; i<dev->nbuffers; i++) {
00234         dev->framebuf[i].filled = 0;
00235         dev->framebuf[i].errors = 0;
00236 
00237         if (i > 0)
00238             dev->framebuf[i].next = &dev->framebuf[i - 1];
00239         else
00240             dev->framebuf->next = NULL;
00241     }
00242 
00243     dev->empty_frames = &dev->framebuf[dev->nbuffers - 1];
00244     dev->empty_frames_tail = dev->framebuf;
00245     dev->read_frame = NULL;
00246     dev->fill_frame = dev->empty_frames;
00247     dev->empty_frames = dev->empty_frames->next;
00248 
00249     dev->image_read_pos = 0;
00250     dev->fill_image = 0;
00251 
00252     spin_unlock_irqrestore(&dev->spinlock, flags);
00253 
00254     for (i=0; i<dev->nbuffers; i++)
00255         dev->image_used[i] = 0;
00256     
00257     return 0;
00258 }
00259 
00260 
00270 int stk11xx_clear_buffers(struct usb_stk11xx *dev)
00271 {
00272     memset(dev->image_data, 0x00, dev->nbuffers * dev->len_per_image);
00273 
00274     return 0;
00275 }
00276 
00277 
00287 int stk11xx_free_buffers(struct usb_stk11xx *dev)
00288 {
00289     int i;
00290 
00291     STK_DEBUG("Free buffers\n");
00292 
00293     if (dev == NULL)
00294         return -1;
00295 
00296     // Release iso pipe buffers
00297     for (i=0; i<MAX_ISO_BUFS; i++) {
00298         if (dev->isobuf[i].data != NULL) {
00299             kfree(dev->isobuf[i].data);
00300             dev->isobuf[i].data = NULL;
00301         }
00302     }
00303 
00304     // Release frame buffers
00305     if (dev->framebuf != NULL) {
00306         for (i=0; i<default_nbrframebuf; i++) {
00307             if (dev->framebuf[i].data != NULL) {
00308                 vfree(dev->framebuf[i].data);
00309                 dev->framebuf[i].data = NULL;
00310             }
00311         }
00312 
00313         kfree(dev->framebuf);
00314         dev->framebuf = NULL;
00315     }
00316 
00317     // Release image buffers
00318     if (dev->image_data != NULL)
00319         stk11xx_rvfree(dev->image_data, dev->nbuffers * dev->len_per_image);
00320 
00321     dev->image_data = NULL;
00322 
00323     return 0;
00324 }
00325 
00326 
00334 void stk11xx_next_image(struct usb_stk11xx *dev)
00335 {
00336     STK_STREAM("Select next image\n");
00337 
00338     dev->image_used[dev->fill_image] = 0;
00339     dev->fill_image = (dev->fill_image + 1) % dev->nbuffers;
00340 }
00341 
00342 
00352 int stk11xx_next_frame(struct usb_stk11xx *dev)
00353 {
00354     int ret = 0;
00355     unsigned long flags;
00356 
00357     STK_STREAM("Select next frame\n");
00358 
00359     spin_lock_irqsave(&dev->spinlock, flags);
00360 
00361     if (dev->fill_frame != NULL) {
00362         if (dev->full_frames == NULL) {
00363             dev->full_frames = dev->fill_frame;
00364             dev->full_frames_tail = dev->full_frames;
00365         }
00366         else {
00367             dev->full_frames_tail->next = dev->fill_frame;
00368             dev->full_frames_tail = dev->fill_frame;
00369         }
00370     }
00371 
00372     if (dev->empty_frames != NULL) {
00373         dev->fill_frame = dev->empty_frames;
00374         dev->empty_frames = dev->empty_frames->next;
00375     }
00376     else {
00377         if (dev->full_frames == NULL) {
00378             STK_ERROR("Neither empty or full frames available!\n");
00379             spin_unlock_irqrestore(&dev->spinlock, flags);
00380             return -EINVAL;
00381         }
00382 
00383         dev->fill_frame = dev->full_frames;
00384         dev->full_frames = dev->full_frames->next;
00385 
00386         ret = 1;
00387     }
00388 
00389     dev->fill_frame->next = NULL;
00390 
00391     spin_unlock_irqrestore(&dev->spinlock, flags);
00392 
00393     return ret;
00394 }
00395 
00396 
00407 int stk11xx_handle_frame(struct usb_stk11xx *dev)
00408 {
00409     int ret = 0;
00410     unsigned long flags;
00411 
00412     STK_STREAM("Sync Handle Frame\n");
00413 
00414     spin_lock_irqsave(&dev->spinlock, flags);
00415 
00416     if (dev->read_frame != NULL) {
00417         spin_unlock_irqrestore(&dev->spinlock, flags);
00418         return ret;
00419     }
00420 
00421     if (dev->full_frames == NULL) {
00422     }
00423     else {
00424         dev->read_frame = dev->full_frames;
00425         dev->full_frames = dev->full_frames->next;
00426         dev->read_frame->next = NULL;
00427     }
00428 
00429     if (dev->read_frame != NULL) {
00430         spin_unlock_irqrestore(&dev->spinlock, flags);
00431         ret = dev_stk11xx_decompress(dev);
00432         spin_lock_irqsave(&dev->spinlock, flags);
00433 
00434         if (dev->empty_frames == NULL) {
00435             dev->empty_frames = dev->read_frame;
00436             dev->empty_frames_tail = dev->empty_frames;
00437         }
00438         else {
00439             dev->empty_frames_tail->next = dev->read_frame;
00440             dev->empty_frames_tail = dev->read_frame;
00441         }
00442 
00443         dev->read_frame = NULL;
00444     }
00445 
00446     spin_unlock_irqrestore(&dev->spinlock, flags);
00447 
00448     dev_stk11xx_watchdog_camera(dev);
00449 
00450     return ret;
00451 }
00452