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

Generated on Tue Oct 30 13:32:13 2007 for SyntekUSBVideoCamera by  doxygen 1.5.3