From 66eb7bd94e4b134177297e62fd8f66c435e7c273 Mon Sep 17 00:00:00 2001 From: Mauro Carvalho Chehab Date: Sun, 13 Apr 2008 11:37:52 -0300 Subject: em28xx: convert to use videobuf-vmalloc From: Mauro Carvalho Chehab The usage of videobuf-vmalloc allows to cleanup em28xx logic. Also, it reduced its size by about 5.42% on i386 arch (and about 7.5% on x86_64): 39113 4876 40 44029 abfd old/em28xx.ko 36731 4868 40 41639 a2a7 /home/v4l/master/v4l/em28xx.ko Also, the preliminary tests, made on a single core 1.5 MHz Centrino showed that CPU usage reduced from 42%-75% to 28%-33% (reports from "top") command. A test with time command presented an even better result: This is the performance tests I did, running code_example to get 1,000 frames @29.995 Hz (about 35 seconds of stream), tested on a i386 machine, running at 1,5GHz: The old driver: $ time -f "%E: %Us User time, %Ss Kernel time, %P CPU used" ./capture_example 0:34.21: 8.22s User time, 25.16s Kernel time, 97% CPU used The videobuf-based driver: $ time -f "%E: %Us User time, %Ss Kernel time, %P CPU used" ./capture_example 0:35.36: 0.01s User time, 0.05s Kernel time, 0% CPU used Conclusion: The time consumption to receive the stream where reduced from about 33.38 seconds to 0.05 seconds. Signed-off-by: Mauro Carvalho Chehab --- linux/drivers/media/video/em28xx/em28xx-core.c | 480 --------- linux/drivers/media/video/em28xx/em28xx-video.c | 1205 ++++++++++++++--------- linux/drivers/media/video/em28xx/em28xx.h | 94 +- 3 files changed, 818 insertions(+), 961 deletions(-) (limited to 'linux/drivers/media') diff --git a/linux/drivers/media/video/em28xx/em28xx-core.c b/linux/drivers/media/video/em28xx/em28xx-core.c index effa1c7c5..95bc18d0e 100644 --- a/linux/drivers/media/video/em28xx/em28xx-core.c +++ b/linux/drivers/media/video/em28xx/em28xx-core.c @@ -49,138 +49,10 @@ MODULE_PARM_DESC(reg_debug,"enable debug messages [URB reg]"); printk(KERN_INFO "%s %s :"fmt, \ dev->name, __func__ , ##arg); } while (0) -static unsigned int isoc_debug; -module_param(isoc_debug,int,0644); -MODULE_PARM_DESC(isoc_debug,"enable debug messages [isoc transfers]"); - -#define em28xx_isocdbg(fmt, arg...) do {\ - if (isoc_debug) \ - printk(KERN_INFO "%s %s :"fmt, \ - dev->name, __func__ , ##arg); } while (0) - static int alt = EM28XX_PINOUT; module_param(alt, int, 0644); MODULE_PARM_DESC(alt, "alternate setting to use for video endpoint"); -#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,15) -#include -static void *rvmalloc(size_t size) -{ - void *mem; - unsigned long adr; - - size = PAGE_ALIGN(size); - - mem = vmalloc_32((unsigned long)size); - if (!mem) - return NULL; - - adr = (unsigned long)mem; - while (size > 0) { - SetPageReserved(vmalloc_to_page((void *)adr)); - adr += PAGE_SIZE; - size -= PAGE_SIZE; - } - - return mem; -} - -static void rvfree(void *mem, size_t size) -{ - unsigned long adr; - - if (!mem) - return; - - size = PAGE_ALIGN(size); - - adr = (unsigned long)mem; - while (size > 0) { - ClearPageReserved(vmalloc_to_page((void *)adr)); - adr += PAGE_SIZE; - size -= PAGE_SIZE; - } - - vfree(mem); -} -#endif - -/* - * em28xx_request_buffers() - * allocate a number of buffers - */ -u32 em28xx_request_buffers(struct em28xx *dev, u32 count) -{ - const size_t imagesize = PAGE_ALIGN(dev->frame_size); /*needs to be page aligned cause the buffers can be mapped individually! */ - void *buff = NULL; - u32 i; - em28xx_coredbg("requested %i buffers with size %zi\n", - count, imagesize); - if (count > EM28XX_NUM_FRAMES) - count = EM28XX_NUM_FRAMES; - - dev->num_frames = count; - while (dev->num_frames > 0) { -#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,15) - if ((buff = rvmalloc(dev->num_frames * imagesize))) { -#else - if ((buff = vmalloc_32(dev->num_frames * imagesize))) { -#endif - memset(buff, 0, dev->num_frames * imagesize); - break; - } - dev->num_frames--; - } - - for (i = 0; i < dev->num_frames; i++) { - dev->frame[i].bufmem = buff + i * imagesize; - dev->frame[i].buf.index = i; - dev->frame[i].buf.m.offset = i * imagesize; - dev->frame[i].buf.length = dev->frame_size; - dev->frame[i].buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; - dev->frame[i].buf.sequence = 0; - dev->frame[i].buf.field = V4L2_FIELD_NONE; - dev->frame[i].buf.memory = V4L2_MEMORY_MMAP; - dev->frame[i].buf.flags = 0; - } - return dev->num_frames; -} - -/* - * em28xx_queue_unusedframes() - * add all frames that are not currently in use to the inbuffer queue - */ -void em28xx_queue_unusedframes(struct em28xx *dev) -{ - unsigned long lock_flags; - u32 i; - - for (i = 0; i < dev->num_frames; i++) - if (dev->frame[i].state == F_UNUSED) { - dev->frame[i].state = F_QUEUED; - spin_lock_irqsave(&dev->queue_lock, lock_flags); - list_add_tail(&dev->frame[i].frame, &dev->inqueue); - spin_unlock_irqrestore(&dev->queue_lock, lock_flags); - } -} - -/* - * em28xx_release_buffers() - * free frame buffers - */ -void em28xx_release_buffers(struct em28xx *dev) -{ - if (dev->num_frames) { -#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,15) - rvfree(dev->frame[0].bufmem, - dev->num_frames * PAGE_ALIGN(dev->frame[0].buf.length)); -#else - vfree(dev->frame[0].bufmem); -#endif - dev->num_frames = 0; - } -} - /* * em28xx_read_reg_req() * reads data from the usb device specifying bRequest @@ -520,358 +392,6 @@ int em28xx_resolution_set(struct em28xx *dev) return em28xx_scaler_set(dev, dev->hscale, dev->vscale); } - -/******************* isoc transfer handling ****************************/ - -#ifdef ENABLE_DEBUG_ISOC_FRAMES -#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19) -static void em28xx_isoc_dump(struct urb *urb, struct pt_regs *regs) -#else -static void em28xx_isoc_dump(struct urb *urb) -#endif -{ - int len = 0; - int ntrans = 0; - int i; - - printk(KERN_DEBUG "isocIrq: sf=%d np=%d ec=%x\n", - urb->start_frame, urb->number_of_packets, - urb->error_count); - for (i = 0; i < urb->number_of_packets; i++) { - unsigned char *buf = - urb->transfer_buffer + - urb->iso_frame_desc[i].offset; - int alen = urb->iso_frame_desc[i].actual_length; - if (alen > 0) { - if (buf[0] == 0x88) { - ntrans++; - len += alen; - } else if (buf[0] == 0x22) { - printk(KERN_DEBUG - "= l=%d nt=%d bpp=%d\n", - len - 4 * ntrans, ntrans, - ntrans == 0 ? 0 : len / ntrans); - ntrans = 1; - len = alen; - } else - printk(KERN_DEBUG "!\n"); - } - printk(KERN_DEBUG " n=%d s=%d al=%d %x\n", i, - urb->iso_frame_desc[i].status, - urb->iso_frame_desc[i].actual_length, - (unsigned int) - *((unsigned char *)(urb->transfer_buffer + - urb->iso_frame_desc[i]. - offset))); - } -} -#endif - -static inline int em28xx_isoc_video(struct em28xx *dev,struct em28xx_frame_t **f, - unsigned long *lock_flags, unsigned char buf) -{ - if (!(buf & 0x01)) { - if ((*f)->state == F_GRABBING) { - /*previous frame is incomplete */ - if ((*f)->fieldbytesused < dev->field_size) { - (*f)->state = F_ERROR; - em28xx_isocdbg ("dropping incomplete bottom field (%i missing bytes)", - dev->field_size-(*f)->fieldbytesused); - } else { - (*f)->state = F_DONE; - (*f)->buf.bytesused = dev->frame_size; - } - } - if ((*f)->state == F_DONE || (*f)->state == F_ERROR) { - /* move current frame to outqueue and get next free buffer from inqueue */ - spin_lock_irqsave(&dev-> queue_lock, *lock_flags); - list_move_tail(&(*f)->frame, &dev->outqueue); - if (!list_empty(&dev->inqueue)) - (*f) = list_entry(dev-> inqueue.next, - struct em28xx_frame_t,frame); - else - (*f) = NULL; - spin_unlock_irqrestore(&dev->queue_lock,*lock_flags); - } - if (!(*f)) { - em28xx_isocdbg ("new frame but no buffer is free"); - return -1; - } - do_gettimeofday(&(*f)->buf.timestamp); - (*f)->buf.sequence = ++dev->frame_count; - (*f)->buf.field = V4L2_FIELD_INTERLACED; - (*f)->state = F_GRABBING; - (*f)->buf.bytesused = 0; - (*f)->top_field = 1; - (*f)->fieldbytesused = 0; - } else { - /* acquiring bottom field */ - if ((*f)->state == F_GRABBING) { - if (!(*f)->top_field) { - (*f)->state = F_ERROR; - em28xx_isocdbg ("unexpected begin of bottom field; discarding it"); - } else if ((*f)-> fieldbytesused < dev->field_size - 172) { - (*f)->state = F_ERROR; - em28xx_isocdbg ("dropping incomplete top field (%i missing bytes)", - dev->field_size-(*f)->fieldbytesused); - } else { - (*f)->top_field = 0; - (*f)->fieldbytesused = 0; - } - } - } - return (0); -} - -static inline void em28xx_isoc_video_copy(struct em28xx *dev, - struct em28xx_frame_t **f, unsigned char *buf, int len) -{ - void *fieldstart, *startwrite, *startread; - int linesdone, currlinedone, offset, lencopy,remain; - - if(dev->frame_size != (*f)->buf.length){ - em28xx_err("frame_size %i and buf.length %i are different!!!\n",dev->frame_size,(*f)->buf.length); - return; - } - - if ((*f)->fieldbytesused + len > dev->field_size) - len =dev->field_size - (*f)->fieldbytesused; - - if (buf[0] != 0x88 && buf[0] != 0x22) { - em28xx_isocdbg("frame is not complete\n"); - startread = buf; - len+=4; - } else - startread = buf + 4; - - remain = len; - - if ((*f)->top_field) - fieldstart = (*f)->bufmem; - else - fieldstart = (*f)->bufmem + dev->bytesperline; - - linesdone = (*f)->fieldbytesused / dev->bytesperline; - currlinedone = (*f)->fieldbytesused % dev->bytesperline; - offset = linesdone * dev->bytesperline * 2 + currlinedone; - startwrite = fieldstart + offset; - lencopy = dev->bytesperline - currlinedone; - lencopy = lencopy > remain ? remain : lencopy; - - memcpy(startwrite, startread, lencopy); - remain -= lencopy; - - while (remain > 0) { - startwrite += lencopy + dev->bytesperline; - startread += lencopy; - if (dev->bytesperline > remain) - lencopy = remain; - else - lencopy = dev->bytesperline; - - memcpy(startwrite, startread, lencopy); - remain -= lencopy; - } - - (*f)->fieldbytesused += len; -} - -/* - * em28xx_isoIrq() - * handles the incoming isoc urbs and fills the frames from our inqueue - */ -#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19) -static void em28xx_isocIrq(struct urb *urb, struct pt_regs *regs) -#else -static void em28xx_isocIrq(struct urb *urb) -#endif -{ - struct em28xx *dev = urb->context; - int i, status; - struct em28xx_frame_t **f; - unsigned long lock_flags; - - if (!dev) - return; -#ifdef ENABLE_DEBUG_ISOC_FRAMES - if (isoc_debug>1) -#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19) - em28xx_isoc_dump(urb, regs); -#else - em28xx_isoc_dump(urb); -#endif -#endif - - if (urb->status == -ENOENT) - return; - - f = &dev->frame_current; - - if (dev->stream == STREAM_INTERRUPT) { - dev->stream = STREAM_OFF; - if ((*f)) - (*f)->state = F_QUEUED; - em28xx_isocdbg("stream interrupted"); - wake_up_interruptible(&dev->wait_stream); - } - - if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED)) - return; - - if (dev->stream == STREAM_ON && !list_empty(&dev->inqueue)) { - if (!(*f)) - (*f) = list_entry(dev->inqueue.next, - struct em28xx_frame_t, frame); - - for (i = 0; i < urb->number_of_packets; i++) { - unsigned char *buf = urb->transfer_buffer + - urb->iso_frame_desc[i].offset; - int len = urb->iso_frame_desc[i].actual_length - 4; - - if (urb->iso_frame_desc[i].status) { - em28xx_isocdbg("data error: [%d] len=%d, status=%d", i, - urb->iso_frame_desc[i].actual_length, - urb->iso_frame_desc[i].status); - if (urb->iso_frame_desc[i].status != -EPROTO) - continue; - } - if (urb->iso_frame_desc[i].actual_length <= 0) { - em28xx_isocdbg("packet %d is empty",i); - continue; - } - if (urb->iso_frame_desc[i].actual_length > - urb->iso_frame_desc[i].length) { - em28xx_isocdbg("packet bigger than packet size"); - continue; - } - /*new frame */ - if (buf[0] == 0x22 && buf[1] == 0x5a) { - em28xx_isocdbg("Video frame, length=%i!",len); - - if (em28xx_isoc_video(dev,f,&lock_flags,buf[2])) - break; - } else if (buf[0]==0x33 && buf[1]==0x95 && buf[2]==0x00) { - em28xx_isocdbg("VBI HEADER!!!"); - } - - /* actual copying */ - if ((*f)->state == F_GRABBING) { - em28xx_isoc_video_copy(dev,f,buf, len); - } - } - } - - for (i = 0; i < urb->number_of_packets; i++) { - urb->iso_frame_desc[i].status = 0; - urb->iso_frame_desc[i].actual_length = 0; - } - - urb->status = 0; - if ((status = usb_submit_urb(urb, GFP_ATOMIC))) { - em28xx_errdev("resubmit of urb failed (error=%i)\n", status); - dev->state |= DEV_MISCONFIGURED; - } - wake_up_interruptible(&dev->wait_frame); - return; -} - -/* - * em28xx_uninit_isoc() - * deallocates the buffers and urbs allocated during em28xx_init_iosc() - */ -void em28xx_uninit_isoc(struct em28xx *dev) -{ - int i; - - for (i = 0; i < EM28XX_NUM_BUFS; i++) { - if (dev->urb[i]) { - usb_kill_urb(dev->urb[i]); - if (dev->transfer_buffer[i]) { - usb_buffer_free(dev->udev, - dev->urb[i]->transfer_buffer_length, - dev->transfer_buffer[i], - dev->urb[i]->transfer_dma); - } - usb_free_urb(dev->urb[i]); - } - dev->urb[i] = NULL; - dev->transfer_buffer[i] = NULL; - } - em28xx_capture_start(dev, 0); -} - -/* - * em28xx_init_isoc() - * allocates transfer buffers and submits the urbs for isoc transfer - */ -int em28xx_init_isoc(struct em28xx *dev) -{ - /* change interface to 3 which allows the biggest packet sizes */ - int i, errCode; - int sb_size; - - em28xx_set_alternate(dev); - sb_size = EM28XX_NUM_PACKETS * dev->max_pkt_size; - - /* reset streaming vars */ - dev->frame_current = NULL; - dev->frame_count = 0; - - /* allocate urbs */ - for (i = 0; i < EM28XX_NUM_BUFS; i++) { - struct urb *urb; - int j; - /* allocate transfer buffer */ - urb = usb_alloc_urb(EM28XX_NUM_PACKETS, GFP_KERNEL); - if (!urb){ - em28xx_errdev("cannot alloc urb %i\n", i); - em28xx_uninit_isoc(dev); - return -ENOMEM; - } - dev->transfer_buffer[i] = usb_buffer_alloc(dev->udev, sb_size, - GFP_KERNEL, - &urb->transfer_dma); - if (!dev->transfer_buffer[i]) { - em28xx_errdev - ("unable to allocate %i bytes for transfer buffer %i\n", - sb_size, i); - em28xx_uninit_isoc(dev); - usb_free_urb(urb); - return -ENOMEM; - } - memset(dev->transfer_buffer[i], 0, sb_size); - urb->dev = dev->udev; - urb->context = dev; - urb->pipe = usb_rcvisocpipe(dev->udev, 0x82); - urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP; - urb->interval = 1; - urb->transfer_buffer = dev->transfer_buffer[i]; - urb->complete = em28xx_isocIrq; - urb->number_of_packets = EM28XX_NUM_PACKETS; - urb->transfer_buffer_length = sb_size; - for (j = 0; j < EM28XX_NUM_PACKETS; j++) { - urb->iso_frame_desc[j].offset = j * dev->max_pkt_size; - urb->iso_frame_desc[j].length = dev->max_pkt_size; - } - dev->urb[i] = urb; - } - - /* submit urbs */ - em28xx_coredbg("Submitting %d urbs of %d packets (%d each)\n", - EM28XX_NUM_BUFS, EM28XX_NUM_PACKETS, dev->max_pkt_size); - for (i = 0; i < EM28XX_NUM_BUFS; i++) { - errCode = usb_submit_urb(dev->urb[i], GFP_KERNEL); - if (errCode) { - em28xx_errdev("submit of urb %i failed (error=%i)\n", i, - errCode); - em28xx_uninit_isoc(dev); - return errCode; - } - } - - return 0; -} - int em28xx_set_alternate(struct em28xx *dev) { int errCode, prev_alt = dev->alt; diff --git a/linux/drivers/media/video/em28xx/em28xx-video.c b/linux/drivers/media/video/em28xx/em28xx-video.c index 45cdb4820..690f7363c 100644 --- a/linux/drivers/media/video/em28xx/em28xx-video.c +++ b/linux/drivers/media/video/em28xx/em28xx-video.c @@ -59,6 +59,21 @@ printk(KERN_INFO "%s %s :"fmt, \ dev->name, __func__ , ##arg); } while (0) +static unsigned int isoc_debug; +module_param(isoc_debug,int,0644); +MODULE_PARM_DESC(isoc_debug,"enable debug messages [isoc transfers]"); + +#define em28xx_isocdbg(fmt, arg...) do {\ + if (isoc_debug) \ + printk(KERN_INFO "%s %s :"fmt, \ + dev->name, __FUNCTION__ , ##arg); } while (0) + +#define BUFFER_TIMEOUT msecs_to_jiffies(2000) /* 2 seconds */ + +/* Limits minimum and default number of buffers */ +#define EM28XX_MIN_BUF 4 +#define EM28XX_DEF_BUF 8 + MODULE_AUTHOR(DRIVER_AUTHOR); MODULE_DESCRIPTION(DRIVER_DESC); MODULE_LICENSE("GPL"); @@ -127,6 +142,672 @@ static struct v4l2_queryctrl em28xx_qctrl[] = { static struct usb_driver em28xx_usb_driver; +/* ------------------------------------------------------------------ + DMA and thread functions + ------------------------------------------------------------------*/ + +/* + * Announces that a buffer were filled and request the next + */ +static void inline buffer_filled (struct em28xx *dev, + struct em28xx_dmaqueue *dma_q, + struct em28xx_buffer *buf) +{ + /* Nobody is waiting something to be done, just return */ + if (!waitqueue_active(&buf->vb.done)) { + mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT); + + printk(KERN_ERR "em28xx: buffer underrun at %ld\n", + jiffies); + + return; + } + + /* Advice that buffer was filled */ + em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i); + buf->vb.state = VIDEOBUF_DONE; + buf->vb.field_count++; + do_gettimeofday(&buf->vb.ts); + + list_del(&buf->vb.queue); + wake_up(&buf->vb.done); +} + +/* + * Identify the buffer header type and properly handles + */ +static void em28xx_copy_video(struct em28xx *dev, + struct em28xx_dmaqueue *dma_q, + struct em28xx_buffer *buf, + unsigned char *p, + unsigned char *outp, unsigned long len) +{ + void *fieldstart, *startwrite, *startread; + int linesdone, currlinedone, offset, lencopy,remain; + + if(dev->frame_size != buf->vb.size){ + em28xx_errdev("size %i and buf.length %lu are different!\n", + dev->frame_size, buf->vb.size); + return; + } + + if (dma_q->pos + len > buf->vb.size) + len = buf->vb.size - dma_q->pos; + + if (outp[0] != 0x88 && outp[0] != 0x22) { + em28xx_isocdbg("frame is not complete\n"); + len += 4; + } else + p +=4; + + startread = p; + remain = len; + + /* Interlaces frame */ + if (buf->top_field) + fieldstart = outp; + else + fieldstart = outp + dev->bytesperline; + + linesdone = dma_q->pos / dev->bytesperline; + currlinedone = dma_q->pos % dev->bytesperline; + offset = linesdone * dev->bytesperline * 2 + currlinedone; + startwrite = fieldstart + offset; + lencopy = dev->bytesperline - currlinedone; + lencopy = lencopy > remain ? remain : lencopy; + + if (__copy_to_user(startwrite, startread, lencopy) != 0) + em28xx_errdev("copy_to_user failed.\n"); + + remain -= lencopy; + + while (remain > 0) { + startwrite += lencopy + dev->bytesperline; + startread += lencopy; + if (dev->bytesperline > remain) + lencopy = remain; + else + lencopy = dev->bytesperline; + + if (__copy_to_user(startwrite, startread, lencopy) != 0) + em28xx_errdev("copy_to_user failed.\n"); + + remain -= lencopy; + } + + dma_q->pos += len; +} + +static void inline print_err_status (struct em28xx *dev, + int packet, int status) +{ + char *errmsg = "Unknown"; + + switch(status) { + case -ENOENT: + errmsg = "unlinked synchronuously"; + break; + case -ECONNRESET: + errmsg = "unlinked asynchronuously"; + break; + case -ENOSR: + errmsg = "Buffer error (overrun)"; + break; + case -EPIPE: + errmsg = "Stalled (device not responding)"; + break; + case -EOVERFLOW: + errmsg = "Babble (bad cable?)"; + break; + case -EPROTO: + errmsg = "Bit-stuff error (bad cable?)"; + break; + case -EILSEQ: + errmsg = "CRC/Timeout (could be anything)"; + break; + case -ETIME: + errmsg = "Device does not respond"; + break; + } + if (packet<0) { + em28xx_isocdbg("URB status %d [%s].\n", status, errmsg); + } else { + em28xx_isocdbg("URB packet %d, status %d [%s].\n", + packet, status, errmsg); + } +} + +/* + * video-buf generic routine to get the next available buffer + */ +static int inline get_next_buf (struct em28xx_dmaqueue *dma_q, + struct em28xx_buffer **buf) +{ + struct em28xx *dev = container_of(dma_q, struct em28xx, vidq); + + if (list_empty(&dma_q->active)) { + em28xx_isocdbg("No active queue to serve\n"); + return 0; + } + + *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue); + + return 1; +} + +/* + * Controls the isoc copy of each urb packet + */ +static inline int em28xx_isoc_copy(struct urb *urb, struct em28xx_buffer **buf) +{ + struct em28xx_dmaqueue *dma_q = urb->context; + struct em28xx *dev = container_of(dma_q, struct em28xx, vidq); + unsigned char *outp = videobuf_to_vmalloc (&(*buf)->vb); + int i, len = 0, rc = 1; + char *p; + + if (!dev) + return 0; + + if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED)) + return 0; + + if (urb->status<0) { + print_err_status (dev,-1,urb->status); + if (urb->status == -ENOENT) + return 0; + } + + for (i = 0; i < urb->number_of_packets; i++) { + int status = urb->iso_frame_desc[i].status; + + if (status<0) { + print_err_status (dev,i,status); + if (urb->iso_frame_desc[i].status != -EPROTO) + continue; + } + + len=urb->iso_frame_desc[i].actual_length - 4; + + if (urb->iso_frame_desc[i].actual_length <= 0) { + em28xx_isocdbg("packet %d is empty",i); + continue; + } + if (urb->iso_frame_desc[i].actual_length > + dev->max_pkt_size) { + em28xx_isocdbg("packet bigger than packet size"); + continue; + } + + p = urb->transfer_buffer + urb->iso_frame_desc[i].offset; + if (urb->iso_frame_desc[i].status != -EPROTO) + continue; + + /* FIXME: incomplete buffer checks where removed to make + logic simpler. Impacts of those changes should be evaluated + */ + if (outp[0] == 0x22 && outp[1] == 0x5a) { + em28xx_isocdbg("Video frame, length=%i, %s", len, + (outp[2] == 1)? "top" : "botton"); + + if (outp[2] == 1) { + if ((*buf)->receiving) + buffer_filled (dev, dma_q, *buf); + + (*buf)->top_field = 1; + } else { + (*buf)->top_field = 0; + } + (*buf)->receiving = 1; + dma_q->pos = 0; + } else if (outp[0]==0x33 && outp[1]==0x95 && outp[2]==0x00) { + em28xx_isocdbg("VBI HEADER!!!"); + } + + em28xx_copy_video(dev, dma_q, *buf, p, outp, len); + + /* FIXME: Should add vbi copy */ + } + return rc; +} + +/* ------------------------------------------------------------------ + URB control + ------------------------------------------------------------------*/ + +/* + * IRQ callback, called by URB callback + */ +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19) +static void em28xx_irq_callback(struct urb *urb, struct pt_regs *regs) +#else +static void em28xx_irq_callback(struct urb *urb) +#endif +{ + struct em28xx_buffer *buf; + struct em28xx_dmaqueue *dma_q = urb->context; + struct em28xx *dev = container_of(dma_q, struct em28xx, vidq); + int rc,i; + unsigned long flags; + + spin_lock_irqsave(&dev->slock,flags); + + buf=dev->isoc_ctl.buf; + + if (!buf) { + rc=get_next_buf (dma_q, &buf); + if (rc<=0) + goto ret; + } + + /* Copy data from URB */ + rc=em28xx_isoc_copy(urb, &buf); + + dev->isoc_ctl.buf=buf; +ret: + /* Reset urb buffers */ + for (i = 0; i < urb->number_of_packets; i++) { + urb->iso_frame_desc[i].status = 0; + urb->iso_frame_desc[i].actual_length = 0; + } + urb->status = 0; + + if ((urb->status = usb_submit_urb(urb, GFP_ATOMIC))) { + em28xx_err("urb resubmit failed (error=%i)\n", + urb->status); + } + + if (rc >= 0) + mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT); + spin_unlock_irqrestore(&dev->slock,flags); +} + +/* + * Stop and Deallocate URBs + */ +static void em28xx_uninit_isoc(struct em28xx *dev) +{ + struct urb *urb; + int i; + + dev->isoc_ctl.nfields=-1; + dev->isoc_ctl.buf=NULL; + for (i = 0; i < dev->isoc_ctl.num_bufs; i++) { + urb=dev->isoc_ctl.urb[i]; + if (urb) { + usb_kill_urb(urb); + usb_unlink_urb(urb); + if (dev->isoc_ctl.transfer_buffer[i]) { + usb_buffer_free(dev->udev, + urb->transfer_buffer_length, + dev->isoc_ctl.transfer_buffer[i], + urb->transfer_dma); + } + usb_free_urb(urb); + dev->isoc_ctl.urb[i] = NULL; + } + dev->isoc_ctl.transfer_buffer[i] = NULL; + } + + kfree (dev->isoc_ctl.urb); + kfree (dev->isoc_ctl.transfer_buffer); + dev->isoc_ctl.urb=NULL; + dev->isoc_ctl.transfer_buffer=NULL; + + dev->isoc_ctl.num_bufs=0; + + em28xx_capture_start(dev, 0); +} + +/* + * Stop video thread - FIXME: Can be easily removed + */ +static void em28xx_stop_thread(struct em28xx_dmaqueue *dma_q) +{ + struct em28xx *dev= container_of(dma_q, struct em28xx, vidq); + + em28xx_uninit_isoc(dev); +} + +/* + * Allocate URBs and start IRQ + */ +static int em28xx_prepare_isoc(struct em28xx *dev, int max_packets, + int num_bufs) +{ + struct em28xx_dmaqueue *dma_q = &dev->vidq; + int i; + int sb_size, pipe; + struct urb *urb; + int j, k; + + /* De-allocates all pending stuff */ + em28xx_uninit_isoc(dev); + + dev->isoc_ctl.num_bufs = num_bufs; + + dev->isoc_ctl.urb = kmalloc(sizeof(void *)*num_bufs, GFP_KERNEL); + if (!dev->isoc_ctl.urb) { + em28xx_errdev("cannot alloc memory for usb buffers\n"); + return -ENOMEM; + } + + dev->isoc_ctl.transfer_buffer = kmalloc(sizeof(void *)*num_bufs, + GFP_KERNEL); + if (!dev->isoc_ctl.urb) { + em28xx_errdev("cannot allocate memory for usbtransfer\n"); + kfree(dev->isoc_ctl.urb); + return -ENOMEM; + } + + dev->isoc_ctl.max_pkt_size = dev->max_pkt_size; + + sb_size = max_packets * dev->isoc_ctl.max_pkt_size; + + /* allocate urbs and transfer buffers */ + for (i = 0; i < dev->isoc_ctl.num_bufs; i++) { + urb = usb_alloc_urb(max_packets, GFP_KERNEL); + if (!urb) { + em28xx_err("cannot alloc isoc_ctl.urb %i\n", i); + em28xx_uninit_isoc(dev); + usb_free_urb(urb); + return -ENOMEM; + } + dev->isoc_ctl.urb[i] = urb; + + dev->isoc_ctl.transfer_buffer[i] = usb_buffer_alloc(dev->udev, + sb_size, GFP_KERNEL, &urb->transfer_dma); + if (!dev->isoc_ctl.transfer_buffer[i]) { + em28xx_err ("unable to allocate %i bytes for transfer" + " buffer %i%s\n", + sb_size, i, + in_interrupt()?" while in int":""); + em28xx_uninit_isoc(dev); + return -ENOMEM; + } + memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size); + + /* FIXME: this is a hack - should be + 'desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK' + should also be using 'desc.bInterval' + */ + pipe=usb_rcvisocpipe(dev->udev, 0x82); + usb_fill_int_urb(urb, dev->udev, pipe, + dev->isoc_ctl.transfer_buffer[i], sb_size, + em28xx_irq_callback, dma_q, 1); + + urb->number_of_packets = max_packets; + urb->transfer_flags = URB_ISO_ASAP; + + k = 0; + for (j = 0; j < max_packets; j++) { + urb->iso_frame_desc[j].offset = k; + urb->iso_frame_desc[j].length = + dev->isoc_ctl.max_pkt_size; + k += dev->isoc_ctl.max_pkt_size; + } + } + + return 0; +} + +static int em28xx_start_thread( struct em28xx_dmaqueue *dma_q, + struct em28xx_buffer *buf) +{ + struct em28xx *dev = container_of(dma_q, struct em28xx, vidq); + int i,rc; + + init_waitqueue_head(&dma_q->wq); + + em28xx_capture_start(dev, 1); + + /* submit urbs and enables IRQ */ + for (i = 0; i < dev->isoc_ctl.num_bufs; i++) { + rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC); + if (rc) { + em28xx_err("submit of urb %i failed (error=%i)\n", i, + rc); + em28xx_uninit_isoc(dev); + return rc; + } + } + + if (rc<0) + return rc; + + return 0; +} + +static int restart_video_queue(struct em28xx_dmaqueue *dma_q) +{ + struct em28xx *dev= container_of(dma_q,struct em28xx,vidq); + + struct em28xx_buffer *buf, *prev; + struct list_head *item; + + em28xx_videodbg("%s dma_q=0x%08lx\n", + __FUNCTION__,(unsigned long)dma_q); + + if (!list_empty(&dma_q->active)) { + buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue); + em28xx_videodbg("restart_queue [%p/%d]: restart dma\n", + buf, buf->vb.i); + em28xx_stop_thread(dma_q); + em28xx_start_thread(dma_q, buf); + + /* cancel all outstanding capture / vbi requests */ + list_for_each(item,&dma_q->active) { + buf = list_entry(item, struct em28xx_buffer, vb.queue); + + list_del(&buf->vb.queue); + buf->vb.state = VIDEOBUF_ERROR; + wake_up(&buf->vb.done); + } + mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT); + + return 0; + } + + prev = NULL; + for (;;) { + if (list_empty(&dma_q->queued)) + return 0; + buf = list_entry(dma_q->queued.next, struct em28xx_buffer, vb.queue); + if (NULL == prev) { + list_del(&buf->vb.queue); + list_add_tail(&buf->vb.queue,&dma_q->active); + + em28xx_videodbg("Restarting video dma\n"); + em28xx_stop_thread(dma_q); + em28xx_start_thread(dma_q, buf); + + buf->vb.state = VIDEOBUF_ACTIVE; + mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT); + em28xx_videodbg("[%p/%d] restart_queue -" + " first active\n", buf, buf->vb.i); + + } else if (prev->vb.width == buf->vb.width && + prev->vb.height == buf->vb.height && + prev->fmt == buf->fmt) { + list_del(&buf->vb.queue); + list_add_tail(&buf->vb.queue,&dma_q->active); + buf->vb.state = VIDEOBUF_ACTIVE; + em28xx_videodbg("[%p/%d] restart_queue -" + " move to active\n",buf,buf->vb.i); + } else { + return 0; + } + prev = buf; + } +} + +static void em28xx_vid_timeout(unsigned long data) +{ + struct em28xx *dev = (struct em28xx*)data; + struct em28xx_dmaqueue *vidq = &dev->vidq; + struct em28xx_buffer *buf; + unsigned long flags; + + spin_lock_irqsave(&dev->slock,flags); + while (!list_empty(&vidq->active)) { + buf = list_entry(vidq->active.next, struct em28xx_buffer, + vb.queue); + list_del(&buf->vb.queue); + buf->vb.state = VIDEOBUF_ERROR; + wake_up(&buf->vb.done); + em28xx_videodbg("em28xx/0: [%p/%d] timeout\n", + buf, buf->vb.i); + } + + restart_video_queue(vidq); + spin_unlock_irqrestore(&dev->slock,flags); +} + +/* ------------------------------------------------------------------ + Videobuf operations + ------------------------------------------------------------------*/ + +static int +buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size) +{ + struct em28xx_fh *fh = vq->priv_data; + + *size = 16 * fh->width * fh->height >> 3; + if (0 == *count) + *count = EM28XX_DEF_BUF; + + if (*count < EM28XX_MIN_BUF) { + *count=EM28XX_MIN_BUF; + } + + return 0; +} + +static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf) +{ + if (in_interrupt()) + BUG(); + + videobuf_waiton(&buf->vb, 0, 0); + videobuf_vmalloc_free(&buf->vb); + buf->vb.state = VIDEOBUF_NEEDS_INIT; +} + +static int +buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb, + enum v4l2_field field) +{ + struct em28xx_fh *fh = vq->priv_data; + struct em28xx_buffer *buf = container_of(vb,struct em28xx_buffer,vb); + struct em28xx *dev = fh->dev; + int rc = 0, urb_init = 0; + const int urbsize = EM28XX_NUM_PACKETS * dev->max_pkt_size; + + BUG_ON(NULL == fh->fmt); + + /* FIXME: It assumes depth = 16 */ + /* The only currently supported format is 16 bits/pixel */ + buf->vb.size = 16 * fh->width * fh->height >> 3; + + if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size) + return -EINVAL; + + if (buf->fmt != fh->fmt || + buf->vb.width != fh->width || + buf->vb.height != fh->height || + buf->vb.field != field) { + buf->fmt = fh->fmt; + buf->vb.width = fh->width; + buf->vb.height = fh->height; + buf->vb.field = field; + buf->vb.state = VIDEOBUF_NEEDS_INIT; + } + + if (VIDEOBUF_NEEDS_INIT == buf->vb.state) { + if (0 != (rc = videobuf_iolock(vq,&buf->vb,NULL))) + goto fail; + urb_init=1; + } + + + if (!dev->isoc_ctl.num_bufs) + urb_init=1; + + if (urb_init) { + rc = em28xx_prepare_isoc(dev, urbsize, EM28XX_NUM_BUFS); + if (rc<0) + goto fail; + } + + buf->vb.state = VIDEOBUF_PREPARED; + return 0; + +fail: + free_buffer(vq,buf); + return rc; +} + +static void +buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb) +{ + struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb); + struct em28xx_fh *fh = vq->priv_data; + struct em28xx *dev = fh->dev; + struct em28xx_dmaqueue *vidq = &dev->vidq; + struct em28xx_buffer *prev; + + if (!list_empty(&vidq->queued)) { + list_add_tail(&buf->vb.queue,&vidq->queued); + buf->vb.state = VIDEOBUF_QUEUED; + em28xx_videodbg("[%p/%d] buffer_queue - append to queued\n", + buf, buf->vb.i); + } else if (list_empty(&vidq->active)) { + list_add_tail(&buf->vb.queue,&vidq->active); + buf->vb.state = VIDEOBUF_ACTIVE; + mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT); + em28xx_videodbg("[%p/%d] buffer_queue - first active\n", + buf, buf->vb.i); + em28xx_start_thread(vidq, buf); + } else { + prev = list_entry(vidq->active.prev, struct em28xx_buffer, + vb.queue); + if (prev->vb.width == buf->vb.width && + prev->vb.height == buf->vb.height && + prev->fmt == buf->fmt) { + list_add_tail(&buf->vb.queue,&vidq->active); + buf->vb.state = VIDEOBUF_ACTIVE; + em28xx_videodbg("[%p/%d] buffer_queue -" + " append to active\n", buf, buf->vb.i); + } else { + list_add_tail(&buf->vb.queue,&vidq->queued); + buf->vb.state = VIDEOBUF_QUEUED; + em28xx_videodbg("[%p/%d] buffer_queue - first queued\n", + buf, buf->vb.i); + } + } +} + +static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb) +{ + struct em28xx_buffer *buf = container_of(vb,struct em28xx_buffer,vb); + struct em28xx_fh *fh = vq->priv_data; + struct em28xx *dev = (struct em28xx*)fh->dev; + struct em28xx_dmaqueue *vidq = &dev->vidq; + +#if 0 + videobuf_stop(&fh->vb_vidq); +#endif + em28xx_stop_thread(vidq); + + free_buffer(vq,buf); +} + +static struct videobuf_queue_ops em28xx_video_qops = { + .buf_setup = buffer_setup, + .buf_prepare = buffer_prepare, + .buf_queue = buffer_queue, + .buf_release = buffer_release, +}; /********************* v4l2 interface ******************************************/ @@ -174,23 +855,6 @@ static void em28xx_config_i2c(struct em28xx *dev) em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL); } -/* - * em28xx_empty_framequeues() - * prepare queues for incoming and outgoing frames - */ -static void em28xx_empty_framequeues(struct em28xx *dev) -{ - u32 i; - - INIT_LIST_HEAD(&dev->inqueue); - INIT_LIST_HEAD(&dev->outqueue); - - for (i = 0; i < EM28XX_NUM_FRAMES; i++) { - dev->frame[i].state = F_UNUSED; - dev->frame[i].buf.bytesused = 0; - } -} - static void video_mux(struct em28xx *dev, int index) { struct v4l2_routing route; @@ -252,33 +916,6 @@ static void res_free(struct em28xx_fh *fh) mutex_unlock(&dev->lock); } -/* - * em28xx_vm_open() - */ -static void em28xx_vm_open(struct vm_area_struct *vma) -{ - struct em28xx_frame_t *f = vma->vm_private_data; - f->vma_use_count++; -} - -/* - * em28xx_vm_close() - */ -static void em28xx_vm_close(struct vm_area_struct *vma) -{ - /* NOTE: buffers are not freed here */ - struct em28xx_frame_t *f = vma->vm_private_data; - - if (f->vma_use_count) - f->vma_use_count--; -} - -static struct vm_operations_struct em28xx_vm_ops = { - .open = em28xx_vm_open, - .close = em28xx_vm_close, -}; - - /* * em28xx_get_ctrl() * return the current saturation, brightness or contrast, mute state @@ -318,34 +955,6 @@ static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl) } } -/* - * em28xx_stream_interrupt() - * stops streaming - */ -static int em28xx_stream_interrupt(struct em28xx *dev) -{ - int rc = 0; - - /* stop reading from the device */ - - dev->stream = STREAM_INTERRUPT; - rc = wait_event_timeout(dev->wait_stream, - (dev->stream == STREAM_OFF) || - (dev->state & DEV_DISCONNECTED), - EM28XX_URB_TIMEOUT); - - if (rc) { - dev->state |= DEV_MISCONFIGURED; - em28xx_videodbg("device is misconfigured; close and " - "open /dev/video%d again\n", - dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN); - return rc; - } - - return 0; -} - - static int check_dev(struct em28xx *dev) { if (dev->state & DEV_DISCONNECTED) { @@ -469,7 +1078,7 @@ static int vidioc_s_fmt_cap(struct file *file, void *priv, { struct em28xx_fh *fh = priv; struct em28xx *dev = fh->dev; - int rc, i; + int rc; rc = check_dev(dev); if (rc < 0) @@ -479,25 +1088,6 @@ static int vidioc_s_fmt_cap(struct file *file, void *priv, mutex_lock(&dev->lock); - for (i = 0; i < dev->num_frames; i++) - if (dev->frame[i].vma_use_count) { - em28xx_videodbg("VIDIOC_S_FMT failed. " - "Unmap the buffers first.\n"); - rc = -EINVAL; - goto err; - } - - /* stop io in case it is already in progress */ - if (dev->stream == STREAM_ON) { - em28xx_videodbg("VIDIOC_SET_FMT: interrupting stream\n"); - rc = em28xx_stream_interrupt(dev); - if (rc < 0) - goto err; - } - - em28xx_release_buffers(dev); - dev->io = IO_NONE; - /* set new image size */ dev->width = f->fmt.pix.width; dev->height = f->fmt.pix.height; @@ -506,19 +1096,11 @@ static int vidioc_s_fmt_cap(struct file *file, void *priv, dev->bytesperline = dev->width * 2; get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale); - /* FIXME: This is really weird! Why capture is starting with - this ioctl ??? - */ - em28xx_uninit_isoc(dev); em28xx_set_alternate(dev); - em28xx_capture_start(dev, 1); em28xx_resolution_set(dev); - em28xx_init_isoc(dev); - rc = 0; -err: mutex_unlock(&dev->lock); - return rc; + return 0; } static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm) @@ -940,23 +1522,11 @@ static int vidioc_streamon(struct file *file, void *priv, if (rc < 0) return rc; - if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || dev->io != IO_MMAP) - return -EINVAL; - - if (list_empty(&dev->inqueue)) - return -EINVAL; - - mutex_lock(&dev->lock); - if (unlikely(res_get(fh) < 0)) { - mutex_unlock(&dev->lock); + if (unlikely(res_get(fh) < 0)) return -EBUSY; - } - - dev->stream = STREAM_ON; /* FIXME: Start video capture here? */ - mutex_unlock(&dev->lock); - return 0; + return (videobuf_streamon(&fh->vb_vidq)); } static int vidioc_streamoff(struct file *file, void *priv, @@ -970,23 +1540,14 @@ static int vidioc_streamoff(struct file *file, void *priv, if (rc < 0) return rc; - if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || dev->io != IO_MMAP) + if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) + return -EINVAL; + if (type != fh->type) return -EINVAL; - mutex_lock(&dev->lock); - - if (dev->stream == STREAM_ON) { - em28xx_videodbg("VIDIOC_STREAMOFF: interrupting stream\n"); - rc = em28xx_stream_interrupt(dev); - if (rc < 0) { - mutex_unlock(&dev->lock); - return rc; - } - } - - em28xx_empty_framequeues(dev); + videobuf_streamoff(&fh->vb_vidq); + res_free(fh); - mutex_unlock(&dev->lock); return 0; } @@ -1117,53 +1678,13 @@ static int vidioc_reqbufs(struct file *file, void *priv, { struct em28xx_fh *fh = priv; struct em28xx *dev = fh->dev; - u32 i; int rc; rc = check_dev(dev); if (rc < 0) return rc; - if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || - rb->memory != V4L2_MEMORY_MMAP) - return -EINVAL; - - if (dev->io == IO_READ) { - em28xx_videodbg("method is set to read;" - " close and open the device again to" - " choose the mmap I/O method\n"); - return -EINVAL; - } - - for (i = 0; i < dev->num_frames; i++) - if (dev->frame[i].vma_use_count) { - em28xx_videodbg("VIDIOC_REQBUFS failed; " - "previous buffers are still mapped\n"); - return -EINVAL; - } - - mutex_lock(&dev->lock); - - if (dev->stream == STREAM_ON) { - em28xx_videodbg("VIDIOC_REQBUFS: interrupting stream\n"); - rc = em28xx_stream_interrupt(dev); - if (rc < 0) { - mutex_unlock(&dev->lock); - return rc; - } - } - - em28xx_empty_framequeues(dev); - - em28xx_release_buffers(dev); - if (rb->count) - rb->count = em28xx_request_buffers(dev, rb->count); - - dev->frame_current = NULL; - dev->io = rb->count ? IO_MMAP : IO_NONE; - - mutex_unlock(&dev->lock); - return 0; + return (videobuf_reqbufs(&fh->vb_vidq, rb)); } static int vidioc_querybuf(struct file *file, void *priv, @@ -1177,52 +1698,20 @@ static int vidioc_querybuf(struct file *file, void *priv, if (rc < 0) return rc; - if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || - b->index >= dev->num_frames || dev->io != IO_MMAP) - return -EINVAL; - - mutex_lock(&dev->lock); - - memcpy(b, &dev->frame[b->index].buf, sizeof(*b)); - - if (dev->frame[b->index].vma_use_count) - b->flags |= V4L2_BUF_FLAG_MAPPED; - - if (dev->frame[b->index].state == F_DONE) - b->flags |= V4L2_BUF_FLAG_DONE; - else if (dev->frame[b->index].state != F_UNUSED) - b->flags |= V4L2_BUF_FLAG_QUEUED; - - mutex_unlock(&dev->lock); - return 0; + return (videobuf_querybuf(&fh->vb_vidq, b)); } static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b) { struct em28xx_fh *fh = priv; struct em28xx *dev = fh->dev; - unsigned long lock_flags; int rc; rc = check_dev(dev); if (rc < 0) return rc; - if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || dev->io != IO_MMAP || - b->index >= dev->num_frames) - return -EINVAL; - - if (dev->frame[b->index].state != F_UNUSED) - return -EAGAIN; - - dev->frame[b->index].state = F_QUEUED; - - /* add frame to fifo */ - spin_lock_irqsave(&dev->queue_lock, lock_flags); - list_add_tail(&dev->frame[b->index].frame, &dev->inqueue); - spin_unlock_irqrestore(&dev->queue_lock, lock_flags); - - return 0; + return (videobuf_qbuf(&fh->vb_vidq, b)); } static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b) @@ -1230,46 +1719,24 @@ static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b) struct em28xx_fh *fh = priv; struct em28xx *dev = fh->dev; int rc; - struct em28xx_frame_t *f; - unsigned long lock_flags; rc = check_dev(dev); if (rc < 0) return rc; - if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || dev->io != IO_MMAP) - return -EINVAL; - - if (list_empty(&dev->outqueue)) { - if (dev->stream == STREAM_OFF) - return -EINVAL; - - if (file->f_flags & O_NONBLOCK) - return -EAGAIN; - - rc = wait_event_interruptible(dev->wait_frame, - (!list_empty(&dev->outqueue)) || - (dev->state & DEV_DISCONNECTED)); - if (rc) - return rc; - - if (dev->state & DEV_DISCONNECTED) - return -ENODEV; - } - - spin_lock_irqsave(&dev->queue_lock, lock_flags); - f = list_entry(dev->outqueue.next, struct em28xx_frame_t, frame); - list_del(dev->outqueue.next); - spin_unlock_irqrestore(&dev->queue_lock, lock_flags); - - f->state = F_UNUSED; - memcpy(b, &f->buf, sizeof(*b)); + return (videobuf_dqbuf(&fh->vb_vidq, b, + file->f_flags & O_NONBLOCK)); +} - if (f->vma_use_count) - b->flags |= V4L2_BUF_FLAG_MAPPED; +#ifdef CONFIG_VIDEO_V4L1_COMPAT +static int vidiocgmbuf (struct file *file, void *priv, struct video_mbuf *mbuf) +{ + struct em28xx_fh *fh=priv; - return 0; + return videobuf_cgmbuf (&fh->vb_vidq, mbuf, 8); } +#endif + /* ----------------------------------------------------------- */ /* RADIO ESPECIFIC IOCTLS */ @@ -1420,20 +1887,12 @@ static int em28xx_v4l2_open(struct inode *inode, struct file *filp) dev->vscale = 0; em28xx_set_alternate(dev); - em28xx_capture_start(dev, 1); em28xx_resolution_set(dev); #if 0 /* device needs to be initialized before isoc transfer */ video_mux(dev, 0); #endif - - /* start the transfer */ - errCode = em28xx_init_isoc(dev); - if (errCode) - goto err; - - em28xx_empty_framequeues(dev); } if (fh->radio) { em28xx_videodbg("video_open: setting radio device\n"); @@ -1445,7 +1904,10 @@ static int em28xx_v4l2_open(struct inode *inode, struct file *filp) dev->users++; -err: + videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops, + NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED, + sizeof(struct em28xx_buffer), fh); + mutex_unlock(&dev->lock); return errCode; } @@ -1511,9 +1973,8 @@ static int em28xx_v4l2_close(struct inode *inode, struct file *filp) mutex_lock(&dev->lock); if (dev->users == 1) { - em28xx_uninit_isoc(dev); - em28xx_release_buffers(dev); - dev->io = IO_NONE; + videobuf_stop(&fh->vb_vidq); + videobuf_mmap_free(&fh->vb_vidq); /* the device is already disconnect, free the remaining resources */ @@ -1546,134 +2007,28 @@ static int em28xx_v4l2_close(struct inode *inode, struct file *filp) */ static ssize_t em28xx_v4l2_read(struct file *filp, char __user * buf, size_t count, - loff_t * f_pos) + loff_t * pos) { - struct em28xx_frame_t *f, *i; - unsigned long lock_flags; - int ret = 0; struct em28xx_fh *fh = filp->private_data; struct em28xx *dev = fh->dev; + int rc; + + rc = check_dev(dev); + if (rc < 0) + return rc; /* FIXME: read() is not prepared to allow changing the video resolution while streaming. Seems a bug at em28xx_set_fmt */ - if (unlikely(res_get(fh) < 0)) - return -EBUSY; - - mutex_lock(&dev->lock); - - if (dev->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) - em28xx_videodbg("V4l2_Buf_type_videocapture is set\n"); - - if (dev->type == V4L2_BUF_TYPE_VBI_CAPTURE) { - em28xx_videodbg("V4L2_BUF_TYPE_VBI_CAPTURE is set\n"); - em28xx_videodbg("not supported yet! ...\n"); - if (copy_to_user(buf, "", 1)) { - mutex_unlock(&dev->lock); - return -EFAULT; - } - mutex_unlock(&dev->lock); - return (1); - } - if (dev->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) { - em28xx_videodbg("V4L2_BUF_TYPE_SLICED_VBI_CAPTURE is set\n"); - em28xx_videodbg("not supported yet! ...\n"); - if (copy_to_user(buf, "", 1)) { - mutex_unlock(&dev->lock); - return -EFAULT; - } - mutex_unlock(&dev->lock); - return (1); - } - - if (dev->state & DEV_DISCONNECTED) { - em28xx_videodbg("device not present\n"); - mutex_unlock(&dev->lock); - return -ENODEV; - } - - if (dev->state & DEV_MISCONFIGURED) { - em28xx_videodbg("device misconfigured; close and open it again\n"); - mutex_unlock(&dev->lock); - return -EIO; - } - - if (dev->io == IO_MMAP) { - em28xx_videodbg ("IO method is set to mmap; close and open" - " the device again to choose the read method\n"); - mutex_unlock(&dev->lock); - return -EINVAL; - } - - if (dev->io == IO_NONE) { - if (!em28xx_request_buffers(dev, EM28XX_NUM_READ_FRAMES)) { - em28xx_errdev("read failed, not enough memory\n"); - mutex_unlock(&dev->lock); - return -ENOMEM; - } - dev->io = IO_READ; - dev->stream = STREAM_ON; - em28xx_queue_unusedframes(dev); - } - - if (!count) { - mutex_unlock(&dev->lock); - return 0; - } - - if (list_empty(&dev->outqueue)) { - if (filp->f_flags & O_NONBLOCK) { - mutex_unlock(&dev->lock); - return -EAGAIN; - } - ret = wait_event_interruptible - (dev->wait_frame, - (!list_empty(&dev->outqueue)) || - (dev->state & DEV_DISCONNECTED)); - if (ret) { - mutex_unlock(&dev->lock); - return ret; - } - if (dev->state & DEV_DISCONNECTED) { - mutex_unlock(&dev->lock); - return -ENODEV; - } - dev->video_bytesread = 0; - } - - f = list_entry(dev->outqueue.prev, struct em28xx_frame_t, frame); - - em28xx_queue_unusedframes(dev); - - if (count > f->buf.length) - count = f->buf.length; - - if ((dev->video_bytesread + count) > dev->frame_size) - count = dev->frame_size - dev->video_bytesread; - - if (copy_to_user(buf, f->bufmem+dev->video_bytesread, count)) { - em28xx_err("Error while copying to user\n"); - return -EFAULT; - } - dev->video_bytesread += count; - - if (dev->video_bytesread == dev->frame_size) { - spin_lock_irqsave(&dev->queue_lock, lock_flags); - list_for_each_entry(i, &dev->outqueue, frame) - i->state = F_UNUSED; - INIT_LIST_HEAD(&dev->outqueue); - spin_unlock_irqrestore(&dev->queue_lock, lock_flags); + if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { + if (unlikely(res_get(fh))) + return -EBUSY; - em28xx_queue_unusedframes(dev); - dev->video_bytesread = 0; + return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0, + filp->f_flags & O_NONBLOCK); } - - *f_pos += count; - - mutex_unlock(&dev->lock); - - return count; + return 0; } /* @@ -1682,46 +2037,21 @@ em28xx_v4l2_read(struct file *filp, char __user * buf, size_t count, */ static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait) { - unsigned int mask = 0; struct em28xx_fh *fh = filp->private_data; struct em28xx *dev = fh->dev; + int rc; + + rc = check_dev(dev); + if (rc < 0) + return rc; if (unlikely(res_get(fh) < 0)) return POLLERR; - mutex_lock(&dev->lock); - - if (dev->state & DEV_DISCONNECTED) { - em28xx_videodbg("device not present\n"); - } else if (dev->state & DEV_MISCONFIGURED) { - em28xx_videodbg("device is misconfigured; close and open it again\n"); - } else { - if (dev->io == IO_NONE) { - if (!em28xx_request_buffers - (dev, EM28XX_NUM_READ_FRAMES)) { - em28xx_warn - ("poll() failed, not enough memory\n"); - } else { - dev->io = IO_READ; - dev->stream = STREAM_ON; - } - } - - if (dev->io == IO_READ) { - em28xx_queue_unusedframes(dev); - poll_wait(filp, &dev->wait_frame, wait); - - if (!list_empty(&dev->outqueue)) - mask |= POLLIN | POLLRDNORM; - - mutex_unlock(&dev->lock); - - return mask; - } - } + if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type) + return POLLERR; - mutex_unlock(&dev->lock); - return POLLERR; + return videobuf_poll_stream(filp, &fh->vb_vidq, wait); } /* @@ -1731,69 +2061,23 @@ static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma) { struct em28xx_fh *fh = filp->private_data; struct em28xx *dev = fh->dev; - unsigned long size = vma->vm_end - vma->vm_start; - unsigned long start = vma->vm_start; - void *pos; - u32 i; + int rc; if (unlikely(res_get(fh) < 0)) return -EBUSY; - mutex_lock(&dev->lock); - - if (dev->state & DEV_DISCONNECTED) { - em28xx_videodbg("mmap: device not present\n"); - mutex_unlock(&dev->lock); - return -ENODEV; - } - - if (dev->state & DEV_MISCONFIGURED) { - em28xx_videodbg ("mmap: Device is misconfigured; close and " - "open it again\n"); - mutex_unlock(&dev->lock); - return -EIO; - } - - if (dev->io != IO_MMAP || !(vma->vm_flags & VM_WRITE)) { - mutex_unlock(&dev->lock); - return -EINVAL; - } - - if (size > PAGE_ALIGN(dev->frame[0].buf.length)) - size = PAGE_ALIGN(dev->frame[0].buf.length); - - for (i = 0; i < dev->num_frames; i++) { - if ((dev->frame[i].buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff) - break; - } - if (i == dev->num_frames) { - em28xx_videodbg("mmap: user supplied mapping address is out of range\n"); - mutex_unlock(&dev->lock); - return -EINVAL; - } - - /* VM_IO is eventually going to replace PageReserved altogether */ - vma->vm_flags |= VM_IO; - vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */ + rc = check_dev(dev); + if (rc < 0) + return rc; - pos = dev->frame[i].bufmem; - while (size > 0) { /* size is page-aligned */ - if (vm_insert_page(vma, start, vmalloc_to_page(pos))) { - em28xx_videodbg("mmap: vm_insert_page failed\n"); - mutex_unlock(&dev->lock); - return -EAGAIN; - } - start += PAGE_SIZE; - pos += PAGE_SIZE; - size -= PAGE_SIZE; - } + rc = videobuf_mmap_mapper(&fh->vb_vidq, vma); - vma->vm_ops = &em28xx_vm_ops; - vma->vm_private_data = &dev->frame[i]; + em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n", + (unsigned long)vma->vm_start, + (unsigned long)vma->vm_end-(unsigned long)vma->vm_start, + rc); - em28xx_vm_open(vma); - mutex_unlock(&dev->lock); - return 0; + return rc; } static const struct file_operations em28xx_v4l_fops = { @@ -1865,6 +2149,9 @@ static const struct video_device em28xx_video_template = { .vidioc_g_register = vidioc_g_register, .vidioc_s_register = vidioc_s_register, #endif +#ifdef CONFIG_VIDEO_V4L1_COMPAT + .vidiocgmbuf = vidiocgmbuf, +#endif .tvnorms = V4L2_STD_ALL, .current_norm = V4L2_STD_PAL, @@ -2082,6 +2369,14 @@ static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev, dev->radio_dev->minor & 0x1f); } + /* init video dma queues */ + INIT_LIST_HEAD(&dev->vidq.active); + INIT_LIST_HEAD(&dev->vidq.queued); + + dev->vidq.timeout.function = em28xx_vid_timeout; + dev->vidq.timeout.data = (unsigned long)dev; + init_timer(&dev->vidq.timeout); + #if 0 video_set_drvdata(dev->vbi_dev, dev); #endif diff --git a/linux/drivers/media/video/em28xx/em28xx.h b/linux/drivers/media/video/em28xx/em28xx.h index 980989f11..49af08643 100644 --- a/linux/drivers/media/video/em28xx/em28xx.h +++ b/linux/drivers/media/video/em28xx/em28xx.h @@ -27,14 +27,14 @@ #include "compat.h" #include +#include + #include #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,15) #include #endif #include -#define UNSET -1 - /* maximum number of em28xx boards */ #define EM28XX_MAXBOARDS 4 /*FIXME: should be bigger */ @@ -84,31 +84,69 @@ /* time in msecs to wait for i2c writes to finish */ #define EM2800_I2C_WRITE_TIMEOUT 20 -/* the various frame states */ -enum em28xx_frame_state { - F_UNUSED = 0, - F_QUEUED, - F_GRABBING, - F_DONE, - F_ERROR, -}; - -/* stream states */ enum em28xx_stream_state { STREAM_OFF, STREAM_INTERRUPT, STREAM_ON, }; -/* frames */ -struct em28xx_frame_t { - void *bufmem; - struct v4l2_buffer buf; - enum em28xx_frame_state state; +struct em28xx_usb_isoc_ctl { + /* max packet size of isoc transaction */ + int max_pkt_size; + + /* number of allocated urbs */ + int num_bufs; + + /* urb for isoc transfers */ + struct urb **urb; + + /* transfer buffers for isoc transfer */ + char **transfer_buffer; + + /* Last buffer command and region */ + u8 cmd; + int pos, size, pktsize; + + /* Last field: ODD or EVEN? */ + int field; + + /* Stores incomplete commands */ + u32 tmp_buf; + int tmp_buf_len; + + /* Stores already requested buffers */ + struct em28xx_buffer *buf; + + /* Stores the number of received fields */ + int nfields; +}; + +struct em28xx_fmt { + char *name; + u32 fourcc; /* v4l2 format id */ +}; + +/* buffer for one video frame */ +struct em28xx_buffer { + /* common v4l buffer stuff -- must be first */ + struct videobuf_buffer vb; + + struct em28xx_fmt *fmt; + struct list_head frame; - unsigned long vma_use_count; int top_field; - int fieldbytesused; + int receiving; +}; + +struct em28xx_dmaqueue { + struct list_head active; + struct list_head queued; + struct timer_list timeout; + + wait_queue_head_t wq; + + /* Counters to control buffer fill */ + int pos; }; /* io methods */ @@ -266,10 +304,6 @@ struct em28xx { int mute; int volume; /* frame properties */ - struct em28xx_frame_t frame[EM28XX_NUM_FRAMES]; /* list of frames */ - int num_frames; /* number of frames currently in use */ - unsigned int frame_count; /* total number of transfered frames */ - struct em28xx_frame_t *frame_current; /* the frame that is being filled */ int width; /* current frame width */ int height; /* current frame height */ int frame_size; /* current frame size */ @@ -288,7 +322,6 @@ struct em28xx { /* states */ enum em28xx_dev_state state; - enum em28xx_stream_state stream; enum em28xx_io_method io; struct work_struct request_module_wk; @@ -307,6 +340,11 @@ struct em28xx { unsigned char eedata[256]; + /* Isoc control struct */ + struct em28xx_dmaqueue vidq; + struct em28xx_usb_isoc_ctl isoc_ctl; + spinlock_t slock; + /* usb transfer */ struct usb_device *udev; /* the usb device */ int alt; /* alternate */ @@ -330,6 +368,12 @@ struct em28xx_fh { struct em28xx *dev; unsigned int stream_on:1; /* Locks streams */ int radio; + + unsigned int width, height; + struct videobuf_queue vb_vidq; + struct em28xx_fmt *fmt; + + enum v4l2_buf_type type; }; struct em28xx_ops { @@ -366,8 +410,6 @@ int em28xx_colorlevels_set_default(struct em28xx *dev); int em28xx_capture_start(struct em28xx *dev, int start); int em28xx_outfmt_set_yuv422(struct em28xx *dev); int em28xx_resolution_set(struct em28xx *dev); -int em28xx_init_isoc(struct em28xx *dev); -void em28xx_uninit_isoc(struct em28xx *dev); int em28xx_set_alternate(struct em28xx *dev); /* Provided by em28xx-video.c */ -- cgit v1.2.3 From e27b3b0f9894b8c45bf717f9945f9a8d59244b9e Mon Sep 17 00:00:00 2001 From: Mauro Carvalho Chehab Date: Sun, 13 Apr 2008 11:38:47 -0300 Subject: em28xx: Some fixes to videobuf From: Aidan Thornton It fixes a couple of minor bugs, comments out a bogus BUG_ON, sets fh->type correctly, uses dev->width and dev->height for now, and adds a missing spinlock init (nasty - caused a system lockup). It also adds some debug code which probably isn't all that useful. I haven't tested this version of the patch yet, though, so I'm not sure what you can expect if you try it. Signed-off-by: Aidan Thornton Signed-off-by: Mauro Carvalho Chehab --- linux/drivers/media/video/em28xx/em28xx-video.c | 221 +++++++++++++----------- linux/drivers/media/video/em28xx/em28xx.h | 3 +- 2 files changed, 122 insertions(+), 102 deletions(-) (limited to 'linux/drivers/media') diff --git a/linux/drivers/media/video/em28xx/em28xx-video.c b/linux/drivers/media/video/em28xx/em28xx-video.c index 690f7363c..27e95479d 100644 --- a/linux/drivers/media/video/em28xx/em28xx-video.c +++ b/linux/drivers/media/video/em28xx/em28xx-video.c @@ -153,10 +153,10 @@ static void inline buffer_filled (struct em28xx *dev, struct em28xx_dmaqueue *dma_q, struct em28xx_buffer *buf) { + mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT); + /* Nobody is waiting something to be done, just return */ if (!waitqueue_active(&buf->vb.done)) { - mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT); - printk(KERN_ERR "em28xx: buffer underrun at %ld\n", jiffies); @@ -194,7 +194,7 @@ static void em28xx_copy_video(struct em28xx *dev, if (dma_q->pos + len > buf->vb.size) len = buf->vb.size - dma_q->pos; - if (outp[0] != 0x88 && outp[0] != 0x22) { + if (p[0] != 0x88 && p[0] != 0x22) { em28xx_isocdbg("frame is not complete\n"); len += 4; } else @@ -216,8 +216,13 @@ static void em28xx_copy_video(struct em28xx *dev, lencopy = dev->bytesperline - currlinedone; lencopy = lencopy > remain ? remain : lencopy; - if (__copy_to_user(startwrite, startread, lencopy) != 0) - em28xx_errdev("copy_to_user failed.\n"); + if((char*)startwrite + lencopy > (char*)outp + buf->vb.size) { + em28xx_isocdbg("Overflow of %i bytes past buffer end (1)\n", + ((char*)startwrite + lencopy) - ((char*)outp + buf->vb.size)); + lencopy = remain = (char*)outp + buf->vb.size - (char*)startwrite; + } + BUG_ON(lencopy <= 0); + memcpy(startwrite, startread, lencopy); remain -= lencopy; @@ -229,8 +234,16 @@ static void em28xx_copy_video(struct em28xx *dev, else lencopy = dev->bytesperline; - if (__copy_to_user(startwrite, startread, lencopy) != 0) - em28xx_errdev("copy_to_user failed.\n"); + BUG_ON(lencopy <= 0); + + if((char*)startwrite + lencopy > (char*)outp + buf->vb.size) { + em28xx_isocdbg("Overflow of %i bytes past buffer end (2)\n", + ((char*)startwrite + lencopy) - ((char*)outp + buf->vb.size)); + lencopy = remain = (char*)outp + buf->vb.size - (char*)startwrite; + } + if(lencopy <= 0) break; + + memcpy(startwrite, startread, lencopy); remain -= lencopy; } @@ -298,13 +311,14 @@ static int inline get_next_buf (struct em28xx_dmaqueue *dma_q, /* * Controls the isoc copy of each urb packet */ -static inline int em28xx_isoc_copy(struct urb *urb, struct em28xx_buffer **buf) +static inline int em28xx_isoc_copy(struct urb *urb) { + struct em28xx_buffer *buf; struct em28xx_dmaqueue *dma_q = urb->context; struct em28xx *dev = container_of(dma_q, struct em28xx, vidq); - unsigned char *outp = videobuf_to_vmalloc (&(*buf)->vb); + unsigned char *outp; int i, len = 0, rc = 1; - char *p; + unsigned char *p; if (!dev) return 0; @@ -318,6 +332,20 @@ static inline int em28xx_isoc_copy(struct urb *urb, struct em28xx_buffer **buf) return 0; } + buf=dev->isoc_ctl.buf; + + if (!buf) { + rc=get_next_buf (dma_q, &buf); + if (rc<=0) + return rc; + } + + outp = videobuf_to_vmalloc (&buf->vb); + +#if 0 /* FIXME: Need to update this when buffer completed. Until then, don't use it */ + dev->isoc_ctl.buf=buf; +#endif + for (i = 0; i < urb->number_of_packets; i++) { int status = urb->iso_frame_desc[i].status; @@ -330,7 +358,7 @@ static inline int em28xx_isoc_copy(struct urb *urb, struct em28xx_buffer **buf) len=urb->iso_frame_desc[i].actual_length - 4; if (urb->iso_frame_desc[i].actual_length <= 0) { - em28xx_isocdbg("packet %d is empty",i); + /* em28xx_isocdbg("packet %d is empty",i); - spammy */ continue; } if (urb->iso_frame_desc[i].actual_length > @@ -340,31 +368,37 @@ static inline int em28xx_isoc_copy(struct urb *urb, struct em28xx_buffer **buf) } p = urb->transfer_buffer + urb->iso_frame_desc[i].offset; - if (urb->iso_frame_desc[i].status != -EPROTO) - continue; /* FIXME: incomplete buffer checks where removed to make logic simpler. Impacts of those changes should be evaluated */ - if (outp[0] == 0x22 && outp[1] == 0x5a) { - em28xx_isocdbg("Video frame, length=%i, %s", len, - (outp[2] == 1)? "top" : "botton"); - - if (outp[2] == 1) { - if ((*buf)->receiving) - buffer_filled (dev, dma_q, *buf); + if (p[0] == 0x22 && p[1] == 0x5a) { + /* FIXME - are the fields the right way around? */ + em28xx_isocdbg("Video frame, length=%i, %s\n", len, + (p[2] & 1)? "top" : "bottom"); + em28xx_isocdbg("Current buffer is: outp = 0x%p, len = %i\n", outp, (int)buf->vb.size); + + if (p[2] & 1) { + if (buf->receiving) { + buffer_filled (dev, dma_q, buf); + rc=get_next_buf (dma_q, &buf); + if (rc<=0) + return rc; + + outp = videobuf_to_vmalloc (&buf->vb); + } - (*buf)->top_field = 1; + buf->top_field = 1; } else { - (*buf)->top_field = 0; + buf->top_field = 0; } - (*buf)->receiving = 1; + buf->receiving = 1; dma_q->pos = 0; - } else if (outp[0]==0x33 && outp[1]==0x95 && outp[2]==0x00) { - em28xx_isocdbg("VBI HEADER!!!"); + } else if (p[0]==0x33 && p[1]==0x95 && p[2]==0x00) { + em28xx_isocdbg("VBI HEADER!!!\n"); } - em28xx_copy_video(dev, dma_q, *buf, p, outp, len); + em28xx_copy_video(dev, dma_q, buf, p, outp, len); /* FIXME: Should add vbi copy */ } @@ -384,7 +418,6 @@ static void em28xx_irq_callback(struct urb *urb, struct pt_regs *regs) static void em28xx_irq_callback(struct urb *urb) #endif { - struct em28xx_buffer *buf; struct em28xx_dmaqueue *dma_q = urb->context; struct em28xx *dev = container_of(dma_q, struct em28xx, vidq); int rc,i; @@ -392,19 +425,9 @@ static void em28xx_irq_callback(struct urb *urb) spin_lock_irqsave(&dev->slock,flags); - buf=dev->isoc_ctl.buf; - - if (!buf) { - rc=get_next_buf (dma_q, &buf); - if (rc<=0) - goto ret; - } - /* Copy data from URB */ - rc=em28xx_isoc_copy(urb, &buf); + rc=em28xx_isoc_copy(urb); - dev->isoc_ctl.buf=buf; -ret: /* Reset urb buffers */ for (i = 0; i < urb->number_of_packets; i++) { urb->iso_frame_desc[i].status = 0; @@ -417,8 +440,12 @@ ret: urb->status); } +#if 0 /* Bad idea. There are problems that cause a load of valid, but + empty, data packets. Don't reset the timeout unless we've actually + got a frame. I've had xawtv hang in free_buffer due to this! */ if (rc >= 0) mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT); +#endif spin_unlock_irqrestore(&dev->slock,flags); } @@ -430,6 +457,8 @@ static void em28xx_uninit_isoc(struct em28xx *dev) struct urb *urb; int i; + em28xx_isocdbg("em28xx: called em28xx_uninit_isoc\n"); + dev->isoc_ctl.nfields=-1; dev->isoc_ctl.buf=NULL; for (i = 0; i < dev->isoc_ctl.num_bufs; i++) { @@ -456,9 +485,10 @@ static void em28xx_uninit_isoc(struct em28xx *dev) dev->isoc_ctl.num_bufs=0; - em28xx_capture_start(dev, 0); + // em28xx_capture_start(dev, 0); - FIXME - how could I restart it? } +#if 0 /* * Stop video thread - FIXME: Can be easily removed */ @@ -466,8 +496,10 @@ static void em28xx_stop_thread(struct em28xx_dmaqueue *dma_q) { struct em28xx *dev= container_of(dma_q, struct em28xx, vidq); + em28xx_isocdbg("em28xx: called em28xx_stop_thread\n"); em28xx_uninit_isoc(dev); } +#endif /* * Allocate URBs and start IRQ @@ -481,18 +513,20 @@ static int em28xx_prepare_isoc(struct em28xx *dev, int max_packets, struct urb *urb; int j, k; + em28xx_isocdbg("em28xx: called em28xx_prepare_isoc\n"); + /* De-allocates all pending stuff */ em28xx_uninit_isoc(dev); dev->isoc_ctl.num_bufs = num_bufs; - dev->isoc_ctl.urb = kmalloc(sizeof(void *)*num_bufs, GFP_KERNEL); + dev->isoc_ctl.urb = kzalloc(sizeof(void *)*num_bufs, GFP_KERNEL); if (!dev->isoc_ctl.urb) { em28xx_errdev("cannot alloc memory for usb buffers\n"); return -ENOMEM; } - dev->isoc_ctl.transfer_buffer = kmalloc(sizeof(void *)*num_bufs, + dev->isoc_ctl.transfer_buffer = kzalloc(sizeof(void *)*num_bufs, GFP_KERNEL); if (!dev->isoc_ctl.urb) { em28xx_errdev("cannot allocate memory for usbtransfer\n"); @@ -510,7 +544,6 @@ static int em28xx_prepare_isoc(struct em28xx *dev, int max_packets, if (!urb) { em28xx_err("cannot alloc isoc_ctl.urb %i\n", i); em28xx_uninit_isoc(dev); - usb_free_urb(urb); return -ENOMEM; } dev->isoc_ctl.urb[i] = urb; @@ -551,15 +584,14 @@ static int em28xx_prepare_isoc(struct em28xx *dev, int max_packets, return 0; } -static int em28xx_start_thread( struct em28xx_dmaqueue *dma_q, - struct em28xx_buffer *buf) +static int em28xx_start_thread( struct em28xx_dmaqueue *dma_q) { struct em28xx *dev = container_of(dma_q, struct em28xx, vidq); - int i,rc; + int i,rc = 0; - init_waitqueue_head(&dma_q->wq); + em28xx_videodbg("Called em28xx_start_thread\n"); - em28xx_capture_start(dev, 1); + init_waitqueue_head(&dma_q->wq); /* submit urbs and enables IRQ */ for (i = 0; i < dev->isoc_ctl.num_bufs; i++) { @@ -578,6 +610,7 @@ static int em28xx_start_thread( struct em28xx_dmaqueue *dma_q, return 0; } +#if 0 static int restart_video_queue(struct em28xx_dmaqueue *dma_q) { struct em28xx *dev= container_of(dma_q,struct em28xx,vidq); @@ -640,6 +673,7 @@ static int restart_video_queue(struct em28xx_dmaqueue *dma_q) prev = buf; } } +#endif static void em28xx_vid_timeout(unsigned long data) { @@ -659,7 +693,7 @@ static void em28xx_vid_timeout(unsigned long data) buf, buf->vb.i); } - restart_video_queue(vidq); + /* restart_video_queue(vidq); */ spin_unlock_irqrestore(&dev->slock,flags); } @@ -672,7 +706,7 @@ buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size) { struct em28xx_fh *fh = vq->priv_data; - *size = 16 * fh->width * fh->height >> 3; + *size = 16 * fh->dev->width * fh->dev->height >> 3; if (0 == *count) *count = EM28XX_DEF_BUF; @@ -700,41 +734,45 @@ buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb, struct em28xx_fh *fh = vq->priv_data; struct em28xx_buffer *buf = container_of(vb,struct em28xx_buffer,vb); struct em28xx *dev = fh->dev; + struct em28xx_dmaqueue *vidq = &dev->vidq; int rc = 0, urb_init = 0; - const int urbsize = EM28XX_NUM_PACKETS * dev->max_pkt_size; - BUG_ON(NULL == fh->fmt); + /* BUG_ON(NULL == fh->fmt); */ /* FIXME: It assumes depth = 16 */ /* The only currently supported format is 16 bits/pixel */ - buf->vb.size = 16 * fh->width * fh->height >> 3; + buf->vb.size = 16 * dev->width * dev->height >> 3; if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size) return -EINVAL; if (buf->fmt != fh->fmt || - buf->vb.width != fh->width || - buf->vb.height != fh->height || + buf->vb.width != dev->width || + buf->vb.height != dev->height || buf->vb.field != field) { buf->fmt = fh->fmt; - buf->vb.width = fh->width; - buf->vb.height = fh->height; + buf->vb.width = dev->width; + buf->vb.height = dev->height; buf->vb.field = field; buf->vb.state = VIDEOBUF_NEEDS_INIT; } if (VIDEOBUF_NEEDS_INIT == buf->vb.state) { - if (0 != (rc = videobuf_iolock(vq,&buf->vb,NULL))) + rc = videobuf_iolock(vq, &buf->vb, NULL); + if (rc < 0) goto fail; - urb_init=1; } - if (!dev->isoc_ctl.num_bufs) urb_init=1; if (urb_init) { - rc = em28xx_prepare_isoc(dev, urbsize, EM28XX_NUM_BUFS); + rc = em28xx_prepare_isoc(dev, EM28XX_NUM_PACKETS, EM28XX_NUM_BUFS); + if (rc<0) + goto fail; + + /* FIXME - should probably be done in response to STREAMON */ + rc = em28xx_start_thread(vidq); if (rc<0) goto fail; } @@ -754,37 +792,10 @@ buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb) struct em28xx_fh *fh = vq->priv_data; struct em28xx *dev = fh->dev; struct em28xx_dmaqueue *vidq = &dev->vidq; - struct em28xx_buffer *prev; - if (!list_empty(&vidq->queued)) { - list_add_tail(&buf->vb.queue,&vidq->queued); - buf->vb.state = VIDEOBUF_QUEUED; - em28xx_videodbg("[%p/%d] buffer_queue - append to queued\n", - buf, buf->vb.i); - } else if (list_empty(&vidq->active)) { - list_add_tail(&buf->vb.queue,&vidq->active); - buf->vb.state = VIDEOBUF_ACTIVE; - mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT); - em28xx_videodbg("[%p/%d] buffer_queue - first active\n", - buf, buf->vb.i); - em28xx_start_thread(vidq, buf); - } else { - prev = list_entry(vidq->active.prev, struct em28xx_buffer, - vb.queue); - if (prev->vb.width == buf->vb.width && - prev->vb.height == buf->vb.height && - prev->fmt == buf->fmt) { - list_add_tail(&buf->vb.queue,&vidq->active); - buf->vb.state = VIDEOBUF_ACTIVE; - em28xx_videodbg("[%p/%d] buffer_queue -" - " append to active\n", buf, buf->vb.i); - } else { - list_add_tail(&buf->vb.queue,&vidq->queued); - buf->vb.state = VIDEOBUF_QUEUED; - em28xx_videodbg("[%p/%d] buffer_queue - first queued\n", - buf, buf->vb.i); - } - } + buf->vb.state = VIDEOBUF_QUEUED; + list_add_tail(&buf->vb.queue, &vidq->active); + } static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb) @@ -792,12 +803,8 @@ static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb struct em28xx_buffer *buf = container_of(vb,struct em28xx_buffer,vb); struct em28xx_fh *fh = vq->priv_data; struct em28xx *dev = (struct em28xx*)fh->dev; - struct em28xx_dmaqueue *vidq = &dev->vidq; -#if 0 - videobuf_stop(&fh->vb_vidq); -#endif - em28xx_stop_thread(vidq); + em28xx_isocdbg("em28xx: called buffer_release\n"); free_buffer(vq,buf); } @@ -1097,6 +1104,7 @@ static int vidioc_s_fmt_cap(struct file *file, void *priv, get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale); em28xx_set_alternate(dev); + em28xx_capture_start(dev, 1); /* ??? */ em28xx_resolution_set(dev); mutex_unlock(&dev->lock); @@ -1526,6 +1534,14 @@ static int vidioc_streamon(struct file *file, void *priv, if (unlikely(res_get(fh) < 0)) return -EBUSY; + /* We can't do this from buffer_queue or anything called from + there, since it's called with IRQs disabled and the spinlock held, + and this uses USB functions that may sleep + + FIXME FIXME FIXME - putting this here means it may not always + be called when it needs to be */ + em28xx_capture_start(dev, 1); + return (videobuf_streamon(&fh->vb_vidq)); } @@ -1844,15 +1860,16 @@ static int em28xx_v4l2_open(struct inode *inode, struct file *filp) int errCode = 0, radio = 0; struct em28xx *h,*dev = NULL; struct em28xx_fh *fh; + enum v4l2_buf_type fh_type = 0; list_for_each_entry(h, &em28xx_devlist, devlist) { if (h->vdev->minor == minor) { dev = h; - dev->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE; } if (h->vbi_dev->minor == minor) { dev = h; - dev->type = V4L2_BUF_TYPE_VBI_CAPTURE; + fh_type = V4L2_BUF_TYPE_VBI_CAPTURE; } if (h->radio_dev && h->radio_dev->minor == minor) { @@ -1864,7 +1881,7 @@ static int em28xx_v4l2_open(struct inode *inode, struct file *filp) return -ENODEV; em28xx_videodbg("open minor=%d type=%s users=%d\n", - minor,v4l2_type_names[dev->type],dev->users); + minor, v4l2_type_names[fh_type], dev->users); fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL); @@ -1875,9 +1892,10 @@ static int em28xx_v4l2_open(struct inode *inode, struct file *filp) mutex_lock(&dev->lock); fh->dev = dev; fh->radio = radio; + fh->type = fh_type; filp->private_data = fh; - if (dev->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) { + if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) { dev->width = norm_maxw(dev); dev->height = norm_maxh(dev); dev->frame_size = dev->width * dev->height * 2; @@ -1985,6 +2003,9 @@ static int em28xx_v4l2_close(struct inode *inode, struct file *filp) return 0; } + /* do this before setting alternate! */ + em28xx_uninit_isoc(dev); + /* set alternate 0 */ dev->alt = 0; em28xx_videodbg("setting alternate 0\n"); @@ -2262,7 +2283,7 @@ static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev, dev->udev = udev; mutex_init(&dev->lock); - spin_lock_init(&dev->queue_lock); + spin_lock_init(&dev->slock); init_waitqueue_head(&dev->open); init_waitqueue_head(&dev->wait_frame); init_waitqueue_head(&dev->wait_stream); diff --git a/linux/drivers/media/video/em28xx/em28xx.h b/linux/drivers/media/video/em28xx/em28xx.h index 49af08643..da6726719 100644 --- a/linux/drivers/media/video/em28xx/em28xx.h +++ b/linux/drivers/media/video/em28xx/em28xx.h @@ -312,7 +312,6 @@ struct em28xx { int hscale; /* horizontal scale factor (see datasheet) */ int vscale; /* vertical scale factor (see datasheet) */ int interlaced; /* 1=interlace fileds, 0=just top fileds */ - int type; unsigned int video_bytesread; /* Number of bytes read */ unsigned long hash; /* eeprom hash - for boards with generic ID */ @@ -332,7 +331,7 @@ struct em28xx { #else struct semaphore lock, fileop_lock; #endif - spinlock_t queue_lock; + /* spinlock_t queue_lock; */ struct list_head inqueue, outqueue; wait_queue_head_t open, wait_frame, wait_stream; struct video_device *vbi_dev; -- cgit v1.2.3 From 99a713aed39a24dc2f0ba5dd70f202757554c382 Mon Sep 17 00:00:00 2001 From: Mauro Carvalho Chehab Date: Sun, 13 Apr 2008 11:39:29 -0300 Subject: em28xx: Fix some warnings From: Mauro Carvalho Chehab Signed-off-by: Mauro Carvalho Chehab --- linux/drivers/media/video/em28xx/em28xx-video.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'linux/drivers/media') diff --git a/linux/drivers/media/video/em28xx/em28xx-video.c b/linux/drivers/media/video/em28xx/em28xx-video.c index 27e95479d..06ecc3e3f 100644 --- a/linux/drivers/media/video/em28xx/em28xx-video.c +++ b/linux/drivers/media/video/em28xx/em28xx-video.c @@ -217,7 +217,7 @@ static void em28xx_copy_video(struct em28xx *dev, lencopy = lencopy > remain ? remain : lencopy; if((char*)startwrite + lencopy > (char*)outp + buf->vb.size) { - em28xx_isocdbg("Overflow of %i bytes past buffer end (1)\n", + em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n", ((char*)startwrite + lencopy) - ((char*)outp + buf->vb.size)); lencopy = remain = (char*)outp + buf->vb.size - (char*)startwrite; } @@ -237,7 +237,7 @@ static void em28xx_copy_video(struct em28xx *dev, BUG_ON(lencopy <= 0); if((char*)startwrite + lencopy > (char*)outp + buf->vb.size) { - em28xx_isocdbg("Overflow of %i bytes past buffer end (2)\n", + em28xx_isocdbg("Overflow of %zi bytes past buffer end (2)\n", ((char*)startwrite + lencopy) - ((char*)outp + buf->vb.size)); lencopy = remain = (char*)outp + buf->vb.size - (char*)startwrite; } -- cgit v1.2.3 From f608f62c7cd71c356a71c48404a1d6a898f2b01d Mon Sep 17 00:00:00 2001 From: Mauro Carvalho Chehab Date: Sun, 13 Apr 2008 11:40:10 -0300 Subject: Fix capture start/stop and timeout From: Mauro Carvalho Chehab Also removes the dead restart_video_queue() function Signed-off-by: Mauro Carvalho Chehab --- linux/drivers/media/video/em28xx/em28xx-video.c | 88 ++----------------------- 1 file changed, 5 insertions(+), 83 deletions(-) (limited to 'linux/drivers/media') diff --git a/linux/drivers/media/video/em28xx/em28xx-video.c b/linux/drivers/media/video/em28xx/em28xx-video.c index 06ecc3e3f..81e21f538 100644 --- a/linux/drivers/media/video/em28xx/em28xx-video.c +++ b/linux/drivers/media/video/em28xx/em28xx-video.c @@ -440,12 +440,6 @@ static void em28xx_irq_callback(struct urb *urb) urb->status); } -#if 0 /* Bad idea. There are problems that cause a load of valid, but - empty, data packets. Don't reset the timeout unless we've actually - got a frame. I've had xawtv hang in free_buffer due to this! */ - if (rc >= 0) - mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT); -#endif spin_unlock_irqrestore(&dev->slock,flags); } @@ -485,7 +479,7 @@ static void em28xx_uninit_isoc(struct em28xx *dev) dev->isoc_ctl.num_bufs=0; - // em28xx_capture_start(dev, 0); - FIXME - how could I restart it? + em28xx_capture_start(dev, 0); } #if 0 @@ -593,6 +587,8 @@ static int em28xx_start_thread( struct em28xx_dmaqueue *dma_q) init_waitqueue_head(&dma_q->wq); + em28xx_capture_start(dev, 1); + /* submit urbs and enables IRQ */ for (i = 0; i < dev->isoc_ctl.num_bufs; i++) { rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC); @@ -610,71 +606,6 @@ static int em28xx_start_thread( struct em28xx_dmaqueue *dma_q) return 0; } -#if 0 -static int restart_video_queue(struct em28xx_dmaqueue *dma_q) -{ - struct em28xx *dev= container_of(dma_q,struct em28xx,vidq); - - struct em28xx_buffer *buf, *prev; - struct list_head *item; - - em28xx_videodbg("%s dma_q=0x%08lx\n", - __FUNCTION__,(unsigned long)dma_q); - - if (!list_empty(&dma_q->active)) { - buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue); - em28xx_videodbg("restart_queue [%p/%d]: restart dma\n", - buf, buf->vb.i); - em28xx_stop_thread(dma_q); - em28xx_start_thread(dma_q, buf); - - /* cancel all outstanding capture / vbi requests */ - list_for_each(item,&dma_q->active) { - buf = list_entry(item, struct em28xx_buffer, vb.queue); - - list_del(&buf->vb.queue); - buf->vb.state = VIDEOBUF_ERROR; - wake_up(&buf->vb.done); - } - mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT); - - return 0; - } - - prev = NULL; - for (;;) { - if (list_empty(&dma_q->queued)) - return 0; - buf = list_entry(dma_q->queued.next, struct em28xx_buffer, vb.queue); - if (NULL == prev) { - list_del(&buf->vb.queue); - list_add_tail(&buf->vb.queue,&dma_q->active); - - em28xx_videodbg("Restarting video dma\n"); - em28xx_stop_thread(dma_q); - em28xx_start_thread(dma_q, buf); - - buf->vb.state = VIDEOBUF_ACTIVE; - mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT); - em28xx_videodbg("[%p/%d] restart_queue -" - " first active\n", buf, buf->vb.i); - - } else if (prev->vb.width == buf->vb.width && - prev->vb.height == buf->vb.height && - prev->fmt == buf->fmt) { - list_del(&buf->vb.queue); - list_add_tail(&buf->vb.queue,&dma_q->active); - buf->vb.state = VIDEOBUF_ACTIVE; - em28xx_videodbg("[%p/%d] restart_queue -" - " move to active\n",buf,buf->vb.i); - } else { - return 0; - } - prev = buf; - } -} -#endif - static void em28xx_vid_timeout(unsigned long data) { struct em28xx *dev = (struct em28xx*)data; @@ -692,8 +623,9 @@ static void em28xx_vid_timeout(unsigned long data) em28xx_videodbg("em28xx/0: [%p/%d] timeout\n", buf, buf->vb.i); } + /* Instead of trying to restart, just sets timeout again */ + mod_timer(&vidq->timeout, jiffies + BUFFER_TIMEOUT); - /* restart_video_queue(vidq); */ spin_unlock_irqrestore(&dev->slock,flags); } @@ -771,7 +703,6 @@ buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb, if (rc<0) goto fail; - /* FIXME - should probably be done in response to STREAMON */ rc = em28xx_start_thread(vidq); if (rc<0) goto fail; @@ -1104,7 +1035,6 @@ static int vidioc_s_fmt_cap(struct file *file, void *priv, get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale); em28xx_set_alternate(dev); - em28xx_capture_start(dev, 1); /* ??? */ em28xx_resolution_set(dev); mutex_unlock(&dev->lock); @@ -1534,14 +1464,6 @@ static int vidioc_streamon(struct file *file, void *priv, if (unlikely(res_get(fh) < 0)) return -EBUSY; - /* We can't do this from buffer_queue or anything called from - there, since it's called with IRQs disabled and the spinlock held, - and this uses USB functions that may sleep - - FIXME FIXME FIXME - putting this here means it may not always - be called when it needs to be */ - em28xx_capture_start(dev, 1); - return (videobuf_streamon(&fh->vb_vidq)); } -- cgit v1.2.3 From 607386c3d653dee94808640dbc0ebf58c9308d41 Mon Sep 17 00:00:00 2001 From: Mauro Carvalho Chehab Date: Sun, 13 Apr 2008 11:40:36 -0300 Subject: em28xx: Fix timeout code From: Mauro Carvalho Chehab Signed-off-by: Mauro Carvalho Chehab --- linux/drivers/media/video/em28xx/em28xx-video.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'linux/drivers/media') diff --git a/linux/drivers/media/video/em28xx/em28xx-video.c b/linux/drivers/media/video/em28xx/em28xx-video.c index 81e21f538..d21c7ad64 100644 --- a/linux/drivers/media/video/em28xx/em28xx-video.c +++ b/linux/drivers/media/video/em28xx/em28xx-video.c @@ -479,6 +479,7 @@ static void em28xx_uninit_isoc(struct em28xx *dev) dev->isoc_ctl.num_bufs=0; + del_timer(&dev->vidq.timeout); em28xx_capture_start(dev, 0); } @@ -614,9 +615,8 @@ static void em28xx_vid_timeout(unsigned long data) unsigned long flags; spin_lock_irqsave(&dev->slock,flags); - while (!list_empty(&vidq->active)) { - buf = list_entry(vidq->active.next, struct em28xx_buffer, - vb.queue); + + list_for_each_entry(buf, vidq->active.next, vb.queue) { list_del(&buf->vb.queue); buf->vb.state = VIDEOBUF_ERROR; wake_up(&buf->vb.done); -- cgit v1.2.3 From a294f171dfaa690aaecc84009fb6e35d4df5d7bf Mon Sep 17 00:00:00 2001 From: Mauro Carvalho Chehab Date: Sun, 13 Apr 2008 11:41:23 -0300 Subject: em28xx: Fix CodingStyle errors and most warnings introduced by videobuf From: Mauro Carvalho Chehab The last videobuf changes introduced several CodingStyle errors. Fixes all those errors, as reported by checkpatch.pl Signed-off-by: Mauro Carvalho Chehab --- linux/drivers/media/video/em28xx/em28xx-video.c | 157 ++++++++++++------------ 1 file changed, 81 insertions(+), 76 deletions(-) (limited to 'linux/drivers/media') diff --git a/linux/drivers/media/video/em28xx/em28xx-video.c b/linux/drivers/media/video/em28xx/em28xx-video.c index d21c7ad64..9ebb43742 100644 --- a/linux/drivers/media/video/em28xx/em28xx-video.c +++ b/linux/drivers/media/video/em28xx/em28xx-video.c @@ -60,13 +60,13 @@ dev->name, __func__ , ##arg); } while (0) static unsigned int isoc_debug; -module_param(isoc_debug,int,0644); -MODULE_PARM_DESC(isoc_debug,"enable debug messages [isoc transfers]"); +module_param(isoc_debug, int, 0644); +MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]"); #define em28xx_isocdbg(fmt, arg...) do {\ if (isoc_debug) \ printk(KERN_INFO "%s %s :"fmt, \ - dev->name, __FUNCTION__ , ##arg); } while (0) + dev->name, __func__ , ##arg); } while (0) #define BUFFER_TIMEOUT msecs_to_jiffies(2000) /* 2 seconds */ @@ -149,7 +149,7 @@ static struct usb_driver em28xx_usb_driver; /* * Announces that a buffer were filled and request the next */ -static void inline buffer_filled (struct em28xx *dev, +static inline void buffer_filled(struct em28xx *dev, struct em28xx_dmaqueue *dma_q, struct em28xx_buffer *buf) { @@ -183,9 +183,9 @@ static void em28xx_copy_video(struct em28xx *dev, unsigned char *outp, unsigned long len) { void *fieldstart, *startwrite, *startread; - int linesdone, currlinedone, offset, lencopy,remain; + int linesdone, currlinedone, offset, lencopy, remain; - if(dev->frame_size != buf->vb.size){ + if (dev->frame_size != buf->vb.size) { em28xx_errdev("size %i and buf.length %lu are different!\n", dev->frame_size, buf->vb.size); return; @@ -198,7 +198,7 @@ static void em28xx_copy_video(struct em28xx *dev, em28xx_isocdbg("frame is not complete\n"); len += 4; } else - p +=4; + p += 4; startread = p; remain = len; @@ -216,10 +216,11 @@ static void em28xx_copy_video(struct em28xx *dev, lencopy = dev->bytesperline - currlinedone; lencopy = lencopy > remain ? remain : lencopy; - if((char*)startwrite + lencopy > (char*)outp + buf->vb.size) { + if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) { em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n", - ((char*)startwrite + lencopy) - ((char*)outp + buf->vb.size)); - lencopy = remain = (char*)outp + buf->vb.size - (char*)startwrite; + ((char *)startwrite + lencopy) - + ((char *)outp + buf->vb.size)); + lencopy = remain = (char *)outp + buf->vb.size - (char *)startwrite; } BUG_ON(lencopy <= 0); memcpy(startwrite, startread, lencopy); @@ -236,12 +237,15 @@ static void em28xx_copy_video(struct em28xx *dev, BUG_ON(lencopy <= 0); - if((char*)startwrite + lencopy > (char*)outp + buf->vb.size) { + if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) { em28xx_isocdbg("Overflow of %zi bytes past buffer end (2)\n", - ((char*)startwrite + lencopy) - ((char*)outp + buf->vb.size)); - lencopy = remain = (char*)outp + buf->vb.size - (char*)startwrite; + ((char *)startwrite + lencopy) - + ((char *)outp + buf->vb.size)); + lencopy = remain = (char *)outp + buf->vb.size - + (char *)startwrite; } - if(lencopy <= 0) break; + if (lencopy <= 0) + break; memcpy(startwrite, startread, lencopy); @@ -251,12 +255,12 @@ static void em28xx_copy_video(struct em28xx *dev, dma_q->pos += len; } -static void inline print_err_status (struct em28xx *dev, +static inline void print_err_status(struct em28xx *dev, int packet, int status) { char *errmsg = "Unknown"; - switch(status) { + switch (status) { case -ENOENT: errmsg = "unlinked synchronuously"; break; @@ -282,7 +286,7 @@ static void inline print_err_status (struct em28xx *dev, errmsg = "Device does not respond"; break; } - if (packet<0) { + if (packet < 0) { em28xx_isocdbg("URB status %d [%s].\n", status, errmsg); } else { em28xx_isocdbg("URB packet %d, status %d [%s].\n", @@ -293,7 +297,7 @@ static void inline print_err_status (struct em28xx *dev, /* * video-buf generic routine to get the next available buffer */ -static int inline get_next_buf (struct em28xx_dmaqueue *dma_q, +static inline int get_next_buf(struct em28xx_dmaqueue *dma_q, struct em28xx_buffer **buf) { struct em28xx *dev = container_of(dma_q, struct em28xx, vidq); @@ -326,36 +330,36 @@ static inline int em28xx_isoc_copy(struct urb *urb) if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED)) return 0; - if (urb->status<0) { - print_err_status (dev,-1,urb->status); + if (urb->status < 0) { + print_err_status(dev, -1, urb->status); if (urb->status == -ENOENT) return 0; } - buf=dev->isoc_ctl.buf; + buf = dev->isoc_ctl.buf; if (!buf) { - rc=get_next_buf (dma_q, &buf); - if (rc<=0) + rc = get_next_buf(dma_q, &buf); + if (rc <= 0) return rc; } - outp = videobuf_to_vmalloc (&buf->vb); + outp = videobuf_to_vmalloc(&buf->vb); #if 0 /* FIXME: Need to update this when buffer completed. Until then, don't use it */ - dev->isoc_ctl.buf=buf; + dev->isoc_ctl.buf = buf; #endif for (i = 0; i < urb->number_of_packets; i++) { int status = urb->iso_frame_desc[i].status; - if (status<0) { - print_err_status (dev,i,status); + if (status < 0) { + print_err_status(dev, i, status); if (urb->iso_frame_desc[i].status != -EPROTO) continue; } - len=urb->iso_frame_desc[i].actual_length - 4; + len = urb->iso_frame_desc[i].actual_length - 4; if (urb->iso_frame_desc[i].actual_length <= 0) { /* em28xx_isocdbg("packet %d is empty",i); - spammy */ @@ -376,25 +380,25 @@ static inline int em28xx_isoc_copy(struct urb *urb) /* FIXME - are the fields the right way around? */ em28xx_isocdbg("Video frame, length=%i, %s\n", len, (p[2] & 1)? "top" : "bottom"); - em28xx_isocdbg("Current buffer is: outp = 0x%p, len = %i\n", outp, (int)buf->vb.size); + em28xx_isocdbg("Current buffer is: outp = 0x%p," + " len = %i\n", outp, (int)buf->vb.size); if (p[2] & 1) { if (buf->receiving) { - buffer_filled (dev, dma_q, buf); - rc=get_next_buf (dma_q, &buf); - if (rc<=0) + buffer_filled(dev, dma_q, buf); + rc = get_next_buf(dma_q, &buf); + if (rc <= 0) return rc; - outp = videobuf_to_vmalloc (&buf->vb); + outp = videobuf_to_vmalloc(&buf->vb); } buf->top_field = 1; - } else { + } else buf->top_field = 0; - } buf->receiving = 1; dma_q->pos = 0; - } else if (p[0]==0x33 && p[1]==0x95 && p[2]==0x00) { + } else if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) { em28xx_isocdbg("VBI HEADER!!!\n"); } @@ -412,7 +416,7 @@ static inline int em28xx_isoc_copy(struct urb *urb) /* * IRQ callback, called by URB callback */ -#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19) +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19) static void em28xx_irq_callback(struct urb *urb, struct pt_regs *regs) #else static void em28xx_irq_callback(struct urb *urb) @@ -420,13 +424,13 @@ static void em28xx_irq_callback(struct urb *urb) { struct em28xx_dmaqueue *dma_q = urb->context; struct em28xx *dev = container_of(dma_q, struct em28xx, vidq); - int rc,i; + int rc, i; unsigned long flags; - spin_lock_irqsave(&dev->slock,flags); + spin_lock_irqsave(&dev->slock, flags); /* Copy data from URB */ - rc=em28xx_isoc_copy(urb); + rc = em28xx_isoc_copy(urb); /* Reset urb buffers */ for (i = 0; i < urb->number_of_packets; i++) { @@ -435,12 +439,13 @@ static void em28xx_irq_callback(struct urb *urb) } urb->status = 0; - if ((urb->status = usb_submit_urb(urb, GFP_ATOMIC))) { + urb->status = usb_submit_urb(urb, GFP_ATOMIC); + if (urb->status) { em28xx_err("urb resubmit failed (error=%i)\n", urb->status); } - spin_unlock_irqrestore(&dev->slock,flags); + spin_unlock_irqrestore(&dev->slock, flags); } /* @@ -453,10 +458,10 @@ static void em28xx_uninit_isoc(struct em28xx *dev) em28xx_isocdbg("em28xx: called em28xx_uninit_isoc\n"); - dev->isoc_ctl.nfields=-1; - dev->isoc_ctl.buf=NULL; + dev->isoc_ctl.nfields = -1; + dev->isoc_ctl.buf = NULL; for (i = 0; i < dev->isoc_ctl.num_bufs; i++) { - urb=dev->isoc_ctl.urb[i]; + urb = dev->isoc_ctl.urb[i]; if (urb) { usb_kill_urb(urb); usb_unlink_urb(urb); @@ -472,12 +477,12 @@ static void em28xx_uninit_isoc(struct em28xx *dev) dev->isoc_ctl.transfer_buffer[i] = NULL; } - kfree (dev->isoc_ctl.urb); - kfree (dev->isoc_ctl.transfer_buffer); - dev->isoc_ctl.urb=NULL; - dev->isoc_ctl.transfer_buffer=NULL; + kfree(dev->isoc_ctl.urb); + kfree(dev->isoc_ctl.transfer_buffer); + dev->isoc_ctl.urb = NULL; + dev->isoc_ctl.transfer_buffer = NULL; - dev->isoc_ctl.num_bufs=0; + dev->isoc_ctl.num_bufs = 0; del_timer(&dev->vidq.timeout); em28xx_capture_start(dev, 0); @@ -489,7 +494,7 @@ static void em28xx_uninit_isoc(struct em28xx *dev) */ static void em28xx_stop_thread(struct em28xx_dmaqueue *dma_q) { - struct em28xx *dev= container_of(dma_q, struct em28xx, vidq); + struct em28xx *dev = container_of(dma_q, struct em28xx, vidq); em28xx_isocdbg("em28xx: called em28xx_stop_thread\n"); em28xx_uninit_isoc(dev); @@ -546,7 +551,7 @@ static int em28xx_prepare_isoc(struct em28xx *dev, int max_packets, dev->isoc_ctl.transfer_buffer[i] = usb_buffer_alloc(dev->udev, sb_size, GFP_KERNEL, &urb->transfer_dma); if (!dev->isoc_ctl.transfer_buffer[i]) { - em28xx_err ("unable to allocate %i bytes for transfer" + em28xx_err("unable to allocate %i bytes for transfer" " buffer %i%s\n", sb_size, i, in_interrupt()?" while in int":""); @@ -559,7 +564,7 @@ static int em28xx_prepare_isoc(struct em28xx *dev, int max_packets, 'desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK' should also be using 'desc.bInterval' */ - pipe=usb_rcvisocpipe(dev->udev, 0x82); + pipe = usb_rcvisocpipe(dev->udev, 0x82); usb_fill_int_urb(urb, dev->udev, pipe, dev->isoc_ctl.transfer_buffer[i], sb_size, em28xx_irq_callback, dma_q, 1); @@ -579,10 +584,10 @@ static int em28xx_prepare_isoc(struct em28xx *dev, int max_packets, return 0; } -static int em28xx_start_thread( struct em28xx_dmaqueue *dma_q) +static int em28xx_start_thread(struct em28xx_dmaqueue *dma_q) { struct em28xx *dev = container_of(dma_q, struct em28xx, vidq); - int i,rc = 0; + int i, rc = 0; em28xx_videodbg("Called em28xx_start_thread\n"); @@ -601,7 +606,7 @@ static int em28xx_start_thread( struct em28xx_dmaqueue *dma_q) } } - if (rc<0) + if (rc < 0) return rc; return 0; @@ -609,12 +614,12 @@ static int em28xx_start_thread( struct em28xx_dmaqueue *dma_q) static void em28xx_vid_timeout(unsigned long data) { - struct em28xx *dev = (struct em28xx*)data; + struct em28xx *dev = (struct em28xx *)data; struct em28xx_dmaqueue *vidq = &dev->vidq; struct em28xx_buffer *buf; unsigned long flags; - spin_lock_irqsave(&dev->slock,flags); + spin_lock_irqsave(&dev->slock, flags); list_for_each_entry(buf, vidq->active.next, vb.queue) { list_del(&buf->vb.queue); @@ -626,7 +631,7 @@ static void em28xx_vid_timeout(unsigned long data) /* Instead of trying to restart, just sets timeout again */ mod_timer(&vidq->timeout, jiffies + BUFFER_TIMEOUT); - spin_unlock_irqrestore(&dev->slock,flags); + spin_unlock_irqrestore(&dev->slock, flags); } /* ------------------------------------------------------------------ @@ -642,9 +647,8 @@ buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size) if (0 == *count) *count = EM28XX_DEF_BUF; - if (*count < EM28XX_MIN_BUF) { - *count=EM28XX_MIN_BUF; - } + if (*count < EM28XX_MIN_BUF) + *count = EM28XX_MIN_BUF; return 0; } @@ -664,7 +668,7 @@ buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb, enum v4l2_field field) { struct em28xx_fh *fh = vq->priv_data; - struct em28xx_buffer *buf = container_of(vb,struct em28xx_buffer,vb); + struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb); struct em28xx *dev = fh->dev; struct em28xx_dmaqueue *vidq = &dev->vidq; int rc = 0, urb_init = 0; @@ -696,15 +700,16 @@ buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb, } if (!dev->isoc_ctl.num_bufs) - urb_init=1; + urb_init = 1; if (urb_init) { - rc = em28xx_prepare_isoc(dev, EM28XX_NUM_PACKETS, EM28XX_NUM_BUFS); - if (rc<0) + rc = em28xx_prepare_isoc(dev, EM28XX_NUM_PACKETS, + EM28XX_NUM_BUFS); + if (rc < 0) goto fail; rc = em28xx_start_thread(vidq); - if (rc<0) + if (rc < 0) goto fail; } @@ -712,7 +717,7 @@ buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb, return 0; fail: - free_buffer(vq,buf); + free_buffer(vq, buf); return rc; } @@ -721,7 +726,7 @@ buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb) { struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb); struct em28xx_fh *fh = vq->priv_data; - struct em28xx *dev = fh->dev; + struct em28xx *dev = fh->dev; struct em28xx_dmaqueue *vidq = &dev->vidq; buf->vb.state = VIDEOBUF_QUEUED; @@ -731,13 +736,13 @@ buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb) static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb) { - struct em28xx_buffer *buf = container_of(vb,struct em28xx_buffer,vb); + struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb); struct em28xx_fh *fh = vq->priv_data; - struct em28xx *dev = (struct em28xx*)fh->dev; + struct em28xx *dev = (struct em28xx *)fh->dev; em28xx_isocdbg("em28xx: called buffer_release\n"); - free_buffer(vq,buf); + free_buffer(vq, buf); } static struct videobuf_queue_ops em28xx_video_qops = { @@ -1667,11 +1672,11 @@ static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b) } #ifdef CONFIG_VIDEO_V4L1_COMPAT -static int vidiocgmbuf (struct file *file, void *priv, struct video_mbuf *mbuf) +static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf) { - struct em28xx_fh *fh=priv; + struct em28xx_fh *fh = priv; - return videobuf_cgmbuf (&fh->vb_vidq, mbuf, 8); + return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8); } #endif @@ -1950,7 +1955,7 @@ static int em28xx_v4l2_close(struct inode *inode, struct file *filp) */ static ssize_t em28xx_v4l2_read(struct file *filp, char __user * buf, size_t count, - loff_t * pos) + loff_t *pos) { struct em28xx_fh *fh = filp->private_data; struct em28xx *dev = fh->dev; -- cgit v1.2.3 From e0c1e01aabf66d47f4903f4b6d439258faa880af Mon Sep 17 00:00:00 2001 From: Mauro Carvalho Chehab Date: Sun, 13 Apr 2008 11:54:05 -0300 Subject: videobuf: revert changeset #7490:5b77f099a151 From: Mauro Carvalho Chehab This changeset were supposed to optimize mmap() support on vmalloc. However, it just broke mmap() on real devices. Revert the changeset to make mmap() to work again. Signed-off-by: Mauro Carvalho Chehab --- linux/drivers/media/video/videobuf-core.c | 29 +++++++++++-------- linux/drivers/media/video/videobuf-vmalloc.c | 43 ++++++++++++++-------------- 2 files changed, 39 insertions(+), 33 deletions(-) (limited to 'linux/drivers/media') diff --git a/linux/drivers/media/video/videobuf-core.c b/linux/drivers/media/video/videobuf-core.c index 7138410fa..52d9a766c 100644 --- a/linux/drivers/media/video/videobuf-core.c +++ b/linux/drivers/media/video/videobuf-core.c @@ -92,17 +92,25 @@ int videobuf_iolock(struct videobuf_queue *q, struct videobuf_buffer *vb, MAGIC_CHECK(vb->magic, MAGIC_BUFFER); MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS); + /* This is required to avoid OOPS on some cases, + since mmap_mapper() method should be called before _iolock. + On some cases, the mmap_mapper() is called only after scheduling. + */ + if (vb->memory == V4L2_MEMORY_MMAP) { + wait_event_timeout(vb->done, q->is_mmapped, + msecs_to_jiffies(100)); + if (!q->is_mmapped) { + printk(KERN_ERR + "Error: mmap_mapper() never called!\n"); + return -EINVAL; + } + } + return CALL(q, iolock, q, vb, fbuf); } /* --------------------------------------------------------------------- */ -static int videobuf_mmap_setup_default(struct videobuf_queue *q, - struct videobuf_buffer *vb) -{ - return 0; -} - void videobuf_queue_core_init(struct videobuf_queue *q, struct videobuf_queue_ops *ops, @@ -124,9 +132,6 @@ void videobuf_queue_core_init(struct videobuf_queue *q, q->priv_data = priv; q->int_ops = int_ops; - if (!q->int_ops->mmap_setup) - q->int_ops->mmap_setup = videobuf_mmap_setup_default; - /* All buffer operations are mandatory */ BUG_ON(!q->ops->buf_setup); BUG_ON(!q->ops->buf_prepare); @@ -304,6 +309,8 @@ static int __videobuf_mmap_free(struct videobuf_queue *q) rc = CALL(q, mmap_free, q); + q->is_mmapped = 0; + if (rc < 0) return rc; @@ -355,9 +362,6 @@ static int __videobuf_mmap_setup(struct videobuf_queue *q, switch (memory) { case V4L2_MEMORY_MMAP: q->bufs[i]->boff = bsize * i; - err = q->int_ops->mmap_setup(q, q->bufs[i]); - if (err) - break; break; case V4L2_MEMORY_USERPTR: case V4L2_MEMORY_OVERLAY: @@ -1080,6 +1084,7 @@ int videobuf_mmap_mapper(struct videobuf_queue *q, mutex_lock(&q->vb_lock); retval = CALL(q, mmap_mapper, q, vma); + q->is_mmapped = 1; mutex_unlock(&q->vb_lock); return retval; diff --git a/linux/drivers/media/video/videobuf-vmalloc.c b/linux/drivers/media/video/videobuf-vmalloc.c index 9e6ff640e..6cdd27d55 100644 --- a/linux/drivers/media/video/videobuf-vmalloc.c +++ b/linux/drivers/media/video/videobuf-vmalloc.c @@ -152,26 +152,21 @@ static int __videobuf_iolock (struct videobuf_queue* q, (unsigned long)mem->vmalloc, pages << PAGE_SHIFT); - return 0; -} - -static int __videobuf_mmap_setup(struct videobuf_queue *q, - struct videobuf_buffer *vb) -{ - int retval = 0; - BUG_ON(vb->memory != V4L2_MEMORY_MMAP); - if (vb->state == VIDEOBUF_NEEDS_INIT) { - /* bsize == size since the buffer needs to be large enough to - * hold an entire frame, not the case in the read case for - * example*/ - vb->size = vb->bsize; - retval = __videobuf_iolock(q, vb, NULL); - if (!retval) { - /* Don't IOLOCK later */ - vb->state = VIDEOBUF_IDLE; + /* It seems that some kernel versions need to do remap *after* + the mmap() call + */ + if (mem->vma) { + int retval=remap_vmalloc_range(mem->vma, mem->vmalloc,0); + kfree(mem->vma); + mem->vma=NULL; + if (retval<0) { + dprintk(1,"mmap app bug: remap_vmalloc_range area %p error %d\n", + mem->vmalloc,retval); + return retval; } } - return retval; + + return 0; } static int __videobuf_sync(struct videobuf_queue *q, @@ -244,8 +239,15 @@ static int __videobuf_mmap_mapper(struct videobuf_queue *q, /* Try to remap memory */ retval=remap_vmalloc_range(vma, mem->vmalloc,0); if (retval<0) { - dprintk(1, "mmap: failed to remap_vmalloc_range\n"); - return -EINVAL; + dprintk(1,"mmap: postponing remap_vmalloc_range\n"); + + mem->vma=kmalloc(sizeof(*vma),GFP_KERNEL); + if (!mem->vma) { + kfree(map); + q->bufs[first]->map=NULL; + return -ENOMEM; + } + memcpy(mem->vma,vma,sizeof(*vma)); } dprintk(1,"mmap %p: q=%p %08lx-%08lx (%lx) pgoff %08lx buf %d\n", @@ -313,7 +315,6 @@ static struct videobuf_qtype_ops qops = { .alloc = __videobuf_alloc, .iolock = __videobuf_iolock, .sync = __videobuf_sync, - .mmap_setup = __videobuf_mmap_setup, .mmap_free = __videobuf_mmap_free, .mmap_mapper = __videobuf_mmap_mapper, .video_copy_to_user = __videobuf_copy_to_user, -- cgit v1.2.3 From b86b4b8178e1138396086c4a21abe9a2e0695860 Mon Sep 17 00:00:00 2001 From: Mauro Carvalho Chehab Date: Sun, 13 Apr 2008 11:55:38 -0300 Subject: em28xx: Fix a broken lock From: Mauro Carvalho Chehab Signed-off-by: Mauro Carvalho Chehab --- linux/drivers/media/video/em28xx/em28xx-video.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) (limited to 'linux/drivers/media') diff --git a/linux/drivers/media/video/em28xx/em28xx-video.c b/linux/drivers/media/video/em28xx/em28xx-video.c index 9ebb43742..d40f72096 100644 --- a/linux/drivers/media/video/em28xx/em28xx-video.c +++ b/linux/drivers/media/video/em28xx/em28xx-video.c @@ -831,15 +831,12 @@ static int res_get(struct em28xx_fh *fh) if (fh->stream_on) return rc; - mutex_lock(&dev->lock); - if (dev->stream_on) - rc = -EINVAL; - else { - dev->stream_on = 1; - fh->stream_on = 1; - } + return -EINVAL; + mutex_lock(&dev->lock); + dev->stream_on = 1; + fh->stream_on = 1; mutex_unlock(&dev->lock); return rc; } -- cgit v1.2.3 From efa9721ae9eaafcd1e0c57c5025bdda6c2bbecba Mon Sep 17 00:00:00 2001 From: Mauro Carvalho Chehab Date: Sun, 13 Apr 2008 11:56:02 -0300 Subject: Various fixes for the em28xx videobuf code From: Aidan Thornton - Aborting buffer_filled if no-one's waiting on the waitqueue probably isn't what we want, since just because no-one's waiting for it now doesn't mean they wouldn't dequeue it in time. (vivi gets away with this, possibly because it can fill each buffer much faster.) - The first BUG_ON(lencopy <= 0); really isn't worth causing a kernel panic over, especially since there are some reasons why it could trigger in normal use. - The top and botom frames are actually the wrong way around. Signed-off-by: Aidan Thornton Signed-off-by: Mauro Carvalho Chehab --- linux/drivers/media/video/em28xx/em28xx-video.c | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) (limited to 'linux/drivers/media') diff --git a/linux/drivers/media/video/em28xx/em28xx-video.c b/linux/drivers/media/video/em28xx/em28xx-video.c index d40f72096..517299c52 100644 --- a/linux/drivers/media/video/em28xx/em28xx-video.c +++ b/linux/drivers/media/video/em28xx/em28xx-video.c @@ -155,14 +155,6 @@ static inline void buffer_filled(struct em28xx *dev, { mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT); - /* Nobody is waiting something to be done, just return */ - if (!waitqueue_active(&buf->vb.done)) { - printk(KERN_ERR "em28xx: buffer underrun at %ld\n", - jiffies); - - return; - } - /* Advice that buffer was filled */ em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i); buf->vb.state = VIDEOBUF_DONE; @@ -222,7 +214,8 @@ static void em28xx_copy_video(struct em28xx *dev, ((char *)outp + buf->vb.size)); lencopy = remain = (char *)outp + buf->vb.size - (char *)startwrite; } - BUG_ON(lencopy <= 0); + if (lencopy <= 0) + return; memcpy(startwrite, startread, lencopy); remain -= lencopy; @@ -379,11 +372,13 @@ static inline int em28xx_isoc_copy(struct urb *urb) if (p[0] == 0x22 && p[1] == 0x5a) { /* FIXME - are the fields the right way around? */ em28xx_isocdbg("Video frame, length=%i, %s\n", len, - (p[2] & 1)? "top" : "bottom"); + (p[2] & 1)? "odd" : "even"); em28xx_isocdbg("Current buffer is: outp = 0x%p," " len = %i\n", outp, (int)buf->vb.size); - if (p[2] & 1) { + if (p[2] & 1) + buf->top_field = 0; + else { if (buf->receiving) { buffer_filled(dev, dma_q, buf); rc = get_next_buf(dma_q, &buf); @@ -394,8 +389,7 @@ static inline int em28xx_isoc_copy(struct urb *urb) } buf->top_field = 1; - } else - buf->top_field = 0; + } buf->receiving = 1; dma_q->pos = 0; } else if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) { -- cgit v1.2.3 From f1f8bbbb2fcd9dc56fc873f194b06b8f82c0a1c0 Mon Sep 17 00:00:00 2001 From: Mauro Carvalho Chehab Date: Sun, 13 Apr 2008 11:56:25 -0300 Subject: em28xx: some small cleanups From: Mauro Carvalho Chehab - Remove dead code; - Fix a few CodingStyle issues; - Prints frame number, if debug is enabled. Signed-off-by: Mauro Carvalho Chehab --- linux/drivers/media/video/em28xx/em28xx-video.c | 30 ++++--------------------- 1 file changed, 4 insertions(+), 26 deletions(-) (limited to 'linux/drivers/media') diff --git a/linux/drivers/media/video/em28xx/em28xx-video.c b/linux/drivers/media/video/em28xx/em28xx-video.c index 517299c52..7eb08bca1 100644 --- a/linux/drivers/media/video/em28xx/em28xx-video.c +++ b/linux/drivers/media/video/em28xx/em28xx-video.c @@ -153,7 +153,7 @@ static inline void buffer_filled(struct em28xx *dev, struct em28xx_dmaqueue *dma_q, struct em28xx_buffer *buf) { - mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT); + mod_timer(&dma_q->timeout, jiffies + BUFFER_TIMEOUT); /* Advice that buffer was filled */ em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i); @@ -179,7 +179,7 @@ static void em28xx_copy_video(struct em28xx *dev, if (dev->frame_size != buf->vb.size) { em28xx_errdev("size %i and buf.length %lu are different!\n", - dev->frame_size, buf->vb.size); + dev->frame_size, buf->vb.size); return; } @@ -228,8 +228,6 @@ static void em28xx_copy_video(struct em28xx *dev, else lencopy = dev->bytesperline; - BUG_ON(lencopy <= 0); - if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) { em28xx_isocdbg("Overflow of %zi bytes past buffer end (2)\n", ((char *)startwrite + lencopy) - @@ -339,10 +337,6 @@ static inline int em28xx_isoc_copy(struct urb *urb) outp = videobuf_to_vmalloc(&buf->vb); -#if 0 /* FIXME: Need to update this when buffer completed. Until then, don't use it */ - dev->isoc_ctl.buf = buf; -#endif - for (i = 0; i < urb->number_of_packets; i++) { int status = urb->iso_frame_desc[i].status; @@ -370,11 +364,8 @@ static inline int em28xx_isoc_copy(struct urb *urb) logic simpler. Impacts of those changes should be evaluated */ if (p[0] == 0x22 && p[1] == 0x5a) { - /* FIXME - are the fields the right way around? */ - em28xx_isocdbg("Video frame, length=%i, %s\n", len, - (p[2] & 1)? "odd" : "even"); - em28xx_isocdbg("Current buffer is: outp = 0x%p," - " len = %i\n", outp, (int)buf->vb.size); + em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2], + len, (p[2] & 1)? "odd" : "even"); if (p[2] & 1) buf->top_field = 0; @@ -482,19 +473,6 @@ static void em28xx_uninit_isoc(struct em28xx *dev) em28xx_capture_start(dev, 0); } -#if 0 -/* - * Stop video thread - FIXME: Can be easily removed - */ -static void em28xx_stop_thread(struct em28xx_dmaqueue *dma_q) -{ - struct em28xx *dev = container_of(dma_q, struct em28xx, vidq); - - em28xx_isocdbg("em28xx: called em28xx_stop_thread\n"); - em28xx_uninit_isoc(dev); -} -#endif - /* * Allocate URBs and start IRQ */ -- cgit v1.2.3 From bb5c2d14253038b38529cf0faf6f99744e119ba8 Mon Sep 17 00:00:00 2001 From: Mauro Carvalho Chehab Date: Sun, 13 Apr 2008 11:57:01 -0300 Subject: em28xx: Fix a possible memory leak From: Brandon Philips I did notice a possible memory leak since iolock is could possibly be called before a buffer has been freed. This ensure s_fmt isn't called while the queue is busy thereby avoiding iolock on already allocated buffers. Signed-off-by: Brandon Philips Signed-off-by: Mauro Carvalho Chehab --- linux/drivers/media/video/em28xx/em28xx-video.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) (limited to 'linux/drivers/media') diff --git a/linux/drivers/media/video/em28xx/em28xx-video.c b/linux/drivers/media/video/em28xx/em28xx-video.c index 7eb08bca1..347fc3837 100644 --- a/linux/drivers/media/video/em28xx/em28xx-video.c +++ b/linux/drivers/media/video/em28xx/em28xx-video.c @@ -654,16 +654,10 @@ buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb, if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size) return -EINVAL; - if (buf->fmt != fh->fmt || - buf->vb.width != dev->width || - buf->vb.height != dev->height || - buf->vb.field != field) { - buf->fmt = fh->fmt; - buf->vb.width = dev->width; - buf->vb.height = dev->height; - buf->vb.field = field; - buf->vb.state = VIDEOBUF_NEEDS_INIT; - } + buf->fmt = fh->fmt; + buf->vb.width = dev->width; + buf->vb.height = dev->height; + buf->vb.field = field; if (VIDEOBUF_NEEDS_INIT == buf->vb.state) { rc = videobuf_iolock(vq, &buf->vb, NULL); @@ -1000,6 +994,12 @@ static int vidioc_s_fmt_cap(struct file *file, void *priv, mutex_lock(&dev->lock); + if (videobuf_queue_is_busy(&fh->vb_vidq)) { + em28xx_errdev("%s queue busy\n", __func__); + rc = -EBUSY; + goto out; + } + /* set new image size */ dev->width = f->fmt.pix.width; dev->height = f->fmt.pix.height; @@ -1011,8 +1011,11 @@ static int vidioc_s_fmt_cap(struct file *file, void *priv, em28xx_set_alternate(dev); em28xx_resolution_set(dev); + rc = 0; + +out: mutex_unlock(&dev->lock); - return 0; + return rc; } static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm) -- cgit v1.2.3 From df4f33ecc5bdff1b94488720bc111a1893b87248 Mon Sep 17 00:00:00 2001 From: Mauro Carvalho Chehab Date: Sun, 13 Apr 2008 11:57:44 -0300 Subject: vivi: Add a missing \n From: Mauro Carvalho Chehab Signed-off-by: Mauro Carvalho Chehab --- linux/drivers/media/video/vivi.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'linux/drivers/media') diff --git a/linux/drivers/media/video/vivi.c b/linux/drivers/media/video/vivi.c index 3ad428808..900a17404 100644 --- a/linux/drivers/media/video/vivi.c +++ b/linux/drivers/media/video/vivi.c @@ -603,7 +603,7 @@ static void free_buffer(struct videobuf_queue *vq, struct vivi_buffer *buf) BUG(); videobuf_vmalloc_free(&buf->vb); - dprintk(dev, 1, "free_buffer: freed"); + dprintk(dev, 1, "free_buffer: freed\n"); buf->vb.state = VIDEOBUF_NEEDS_INIT; } @@ -1123,7 +1123,7 @@ static const struct file_operations vivi_fops = { .poll = vivi_poll, .ioctl = video_ioctl2, /* V4L2 ioctl handler */ #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,11) - .compat_ioctl = v4l_compat_ioctl32, + .compat_ioctl = v4l_compat_ioctl32, #endif .mmap = vivi_mmap, .llseek = no_llseek, -- cgit v1.2.3 From cd67e78204d5af9dd57eec434cd35517d3c1e2b3 Mon Sep 17 00:00:00 2001 From: Mauro Carvalho Chehab Date: Sun, 13 Apr 2008 11:58:21 -0300 Subject: videbuf-vmalloc: Corrects mmap code From: Mauro Carvalho Chehab There were some bugs on videobuf-vmalloc. Basically, remap were called with a wrong parameter. Due to that, a later remap were needed, generating the need of some hacks on videobuf-vmalloc and videobuf-core. This patch fixes the remap and removes the hacks. TODO: - V4L2_MEMORY_USERPTR is not implemented yet. This method should be properly implemented, in order to work with a few userspace applications. - The driver also doesn't implement V4L2_MEMORY_OVERLAY. This method is used only by a few applications, and are becaming obsolete, due to the increment of cpu performance. So, most apps prefer to retrieve data to an internal buffer, doing some processing like de-interlacing. Signed-off-by: Mauro Carvalho Chehab --- linux/drivers/media/video/videobuf-core.c | 14 --- linux/drivers/media/video/videobuf-vmalloc.c | 168 ++++++++++++++++++--------- 2 files changed, 110 insertions(+), 72 deletions(-) (limited to 'linux/drivers/media') diff --git a/linux/drivers/media/video/videobuf-core.c b/linux/drivers/media/video/videobuf-core.c index 52d9a766c..660d28972 100644 --- a/linux/drivers/media/video/videobuf-core.c +++ b/linux/drivers/media/video/videobuf-core.c @@ -92,20 +92,6 @@ int videobuf_iolock(struct videobuf_queue *q, struct videobuf_buffer *vb, MAGIC_CHECK(vb->magic, MAGIC_BUFFER); MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS); - /* This is required to avoid OOPS on some cases, - since mmap_mapper() method should be called before _iolock. - On some cases, the mmap_mapper() is called only after scheduling. - */ - if (vb->memory == V4L2_MEMORY_MMAP) { - wait_event_timeout(vb->done, q->is_mmapped, - msecs_to_jiffies(100)); - if (!q->is_mmapped) { - printk(KERN_ERR - "Error: mmap_mapper() never called!\n"); - return -EINVAL; - } - } - return CALL(q, iolock, q, vb, fbuf); } diff --git a/linux/drivers/media/video/videobuf-vmalloc.c b/linux/drivers/media/video/videobuf-vmalloc.c index 6cdd27d55..340337eda 100644 --- a/linux/drivers/media/video/videobuf-vmalloc.c +++ b/linux/drivers/media/video/videobuf-vmalloc.c @@ -125,45 +125,75 @@ static int __videobuf_iolock (struct videobuf_queue* q, struct videobuf_buffer *vb, struct v4l2_framebuffer *fbuf) { - int pages; - struct videobuf_vmalloc_memory *mem=vb->priv; + struct videobuf_vmalloc_memory *mem = vb->priv; BUG_ON(!mem); - MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM); + MAGIC_CHECK(mem->magic, MAGIC_VMAL_MEM); - pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT; + switch (vb->memory) { + case V4L2_MEMORY_MMAP: + dprintk(1, "%s memory method MMAP\n", __func__); - /* Currently, doesn't support V4L2_MEMORY_OVERLAY */ - if ((vb->memory != V4L2_MEMORY_MMAP) && - (vb->memory != V4L2_MEMORY_USERPTR) ) { - printk(KERN_ERR "Method currently unsupported.\n"); - return -EINVAL; - } + /* All handling should be done by __videobuf_mmap_mapper() */ + if (!mem->vmalloc) { + printk(KERN_ERR "memory is not alloced/mmapped.\n"); + return -EINVAL; + } + break; + case V4L2_MEMORY_USERPTR: + { + int pages = PAGE_ALIGN(vb->size); - /* FIXME: should be tested with kernel mmap mem */ - mem->vmalloc=vmalloc_user (PAGE_ALIGN(vb->size)); - if (NULL == mem->vmalloc) { - printk(KERN_ERR "vmalloc (%d pages) failed\n",pages); - return -ENOMEM; - } + dprintk(1, "%s memory method USERPTR\n", __func__); + +#if 1 /* keep */ + if (vb->baddr) { + printk(KERN_ERR "USERPTR is currently not supported\n"); + return -EINVAL; + } +#endif - dprintk(1,"vmalloc is at addr 0x%08lx, size=%d\n", - (unsigned long)mem->vmalloc, - pages << PAGE_SHIFT); - - /* It seems that some kernel versions need to do remap *after* - the mmap() call - */ - if (mem->vma) { - int retval=remap_vmalloc_range(mem->vma, mem->vmalloc,0); - kfree(mem->vma); - mem->vma=NULL; - if (retval<0) { - dprintk(1,"mmap app bug: remap_vmalloc_range area %p error %d\n", - mem->vmalloc,retval); - return retval; + /* The only USERPTR currently supported is the one needed for + read() method. + */ + + mem->vmalloc = vmalloc_user(pages); + if (!mem->vmalloc) { + printk(KERN_ERR "vmalloc (%d pages) failed\n", pages); + return -ENOMEM; } + dprintk(1, "vmalloc is at addr %p (%d pages)\n", + mem->vmalloc, pages); + +#if 0 /* keep */ + int rc; + /* Kernel userptr is used also by read() method. In this case, + there's no need to remap, since data will be copied to user + */ + if (!vb->baddr) + return 0; + + /* FIXME: to properly support USERPTR, remap should occur. + The code bellow won't work, since mem->vma = NULL + */ + /* Try to remap memory */ + rc = remap_vmalloc_range(mem->vma, (void *)vb->baddr, 0); + if (rc < 0) { + printk(KERN_ERR "mmap: remap failed with error %d. ", rc); + return -ENOMEM; + } +#endif + + break; + } + case V4L2_MEMORY_OVERLAY: + default: + dprintk(1, "%s memory method OVERLAY/unknown\n", __func__); + + /* Currently, doesn't support V4L2_MEMORY_OVERLAY */ + printk(KERN_ERR "Memory method currently unsupported.\n"); + return -EINVAL; } return 0; @@ -179,6 +209,7 @@ static int __videobuf_mmap_free(struct videobuf_queue *q) { unsigned int i; + dprintk(1, "%s\n", __func__); for (i = 0; i < VIDEO_MAX_FRAME; i++) { if (q->bufs[i]) { if (q->bufs[i]->map) @@ -195,10 +226,11 @@ static int __videobuf_mmap_mapper(struct videobuf_queue *q, struct videobuf_vmalloc_memory *mem; struct videobuf_mapping *map; unsigned int first; - int retval; + int retval, pages; unsigned long offset = vma->vm_pgoff << PAGE_SHIFT; - if (! (vma->vm_flags & VM_WRITE) || ! (vma->vm_flags & VM_SHARED)) + dprintk(1, "%s\n", __func__); + if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED)) return -EINVAL; /* look for first buffer to map */ @@ -218,46 +250,55 @@ static int __videobuf_mmap_mapper(struct videobuf_queue *q, } /* create mapping + update buffer list */ - map = q->bufs[first]->map = kzalloc(sizeof(struct videobuf_mapping),GFP_KERNEL); + map = kzalloc(sizeof(struct videobuf_mapping), GFP_KERNEL); if (NULL == map) return -ENOMEM; + q->bufs[first]->map = map; map->start = vma->vm_start; map->end = vma->vm_end; map->q = q; q->bufs[first]->baddr = vma->vm_start; - vma->vm_ops = &videobuf_vm_ops; - vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED; - vma->vm_private_data = map; + mem = q->bufs[first]->priv; + BUG_ON(!mem); + MAGIC_CHECK(mem->magic, MAGIC_VMAL_MEM); - mem=q->bufs[first]->priv; - BUG_ON (!mem); - MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM); + pages = PAGE_ALIGN(vma->vm_end - vma->vm_start); + mem->vmalloc = vmalloc_user(pages); + if (!mem->vmalloc) { + printk(KERN_ERR "vmalloc (%d pages) failed\n", pages); + goto error; + } + dprintk(1, "vmalloc is at addr %p (%d pages)\n", + mem->vmalloc, pages); /* Try to remap memory */ - retval=remap_vmalloc_range(vma, mem->vmalloc,0); - if (retval<0) { - dprintk(1,"mmap: postponing remap_vmalloc_range\n"); - - mem->vma=kmalloc(sizeof(*vma),GFP_KERNEL); - if (!mem->vma) { - kfree(map); - q->bufs[first]->map=NULL; - return -ENOMEM; - } - memcpy(mem->vma,vma,sizeof(*vma)); + retval = remap_vmalloc_range(vma, mem->vmalloc, 0); + if (retval < 0) { + printk(KERN_ERR "mmap: remap failed with error %d. ", retval); + vfree(mem->vmalloc); + goto error; } + vma->vm_ops = &videobuf_vm_ops; + vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED; + vma->vm_private_data = map; + dprintk(1,"mmap %p: q=%p %08lx-%08lx (%lx) pgoff %08lx buf %d\n", - map,q,vma->vm_start,vma->vm_end, + map, q, vma->vm_start, vma->vm_end, (long int) q->bufs[first]->bsize, - vma->vm_pgoff,first); + vma->vm_pgoff, first); videobuf_vm_open(vma); - return (0); + return 0; + +error: + mem = NULL; + kfree(map); + return -ENOMEM; } static int __videobuf_copy_to_user ( struct videobuf_queue *q, @@ -348,13 +389,24 @@ EXPORT_SYMBOL_GPL(videobuf_to_vmalloc); void videobuf_vmalloc_free (struct videobuf_buffer *buf) { - struct videobuf_vmalloc_memory *mem=buf->priv; - BUG_ON (!mem); + struct videobuf_vmalloc_memory *mem = buf->priv; - MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM); + if (!mem) + return; + + MAGIC_CHECK(mem->magic, MAGIC_VMAL_MEM); vfree(mem->vmalloc); - mem->vmalloc=NULL; + mem->vmalloc = NULL; + + +#if 0 + /* FIXME: This would be the proper place for freeing buf->map? */ + kfree(buf->map); + buf->map = NULL; +#endif + + /* FIXME: need to do buf->priv = NULL? */ return; } -- cgit v1.2.3 From 1578e14b943d0db4c03ad27e229a4df96207226e Mon Sep 17 00:00:00 2001 From: Mauro Carvalho Chehab Date: Sun, 13 Apr 2008 11:58:43 -0300 Subject: videobuf-vmalloc: fix STREAMOFF/STREAMON From: Mauro Carvalho Chehab There were a small bug on videobuf-vmalloc that were preventing STREAMOFF to work. The issue is that vmalloc'ed mmaped memory should only be freed after being sure that there aren't any mmap usage. Otherwise, the memory remap will stop working, and the userspace won't receive any frames. This bug were affecting some userspace applications, like tvtime. After this patch, tvtime started to work again with the drivers that use videobuf-vmalloc. Signed-off-by: Mauro Carvalho Chehab --- linux/drivers/media/video/videobuf-vmalloc.c | 41 ++++++++++++++++++---------- 1 file changed, 27 insertions(+), 14 deletions(-) (limited to 'linux/drivers/media') diff --git a/linux/drivers/media/video/videobuf-vmalloc.c b/linux/drivers/media/video/videobuf-vmalloc.c index 340337eda..b031c822f 100644 --- a/linux/drivers/media/video/videobuf-vmalloc.c +++ b/linux/drivers/media/video/videobuf-vmalloc.c @@ -58,19 +58,20 @@ videobuf_vm_open(struct vm_area_struct *vma) map->count++; } -static void -videobuf_vm_close(struct vm_area_struct *vma) +static void videobuf_vm_close(struct vm_area_struct *vma) { struct videobuf_mapping *map = vma->vm_private_data; struct videobuf_queue *q = map->q; int i; - dprintk(2,"vm_close %p [count=%u,vma=%08lx-%08lx]\n",map, - map->count,vma->vm_start,vma->vm_end); + dprintk(2,"vm_close %p [count=%u,vma=%08lx-%08lx]\n", map, + map->count, vma->vm_start, vma->vm_end); map->count--; if (0 == map->count) { - dprintk(1,"munmap %p q=%p\n",map,q); + struct videobuf_vmalloc_memory *mem; + + dprintk(1, "munmap %p q=%p\n", map, q); mutex_lock(&q->vb_lock); for (i = 0; i < VIDEO_MAX_FRAME; i++) { if (NULL == q->bufs[i]) @@ -79,6 +80,18 @@ videobuf_vm_close(struct vm_area_struct *vma) if (q->bufs[i]->map != map) continue; + mem = q->bufs[i]->priv; + if (mem) { + /* This callback is called only if kernel has + allocated memory and this memory is mmapped. + In this case, memory should be freed, + in order to do memory unmap. + */ + MAGIC_CHECK(mem->magic, MAGIC_VMAL_MEM); + vfree(mem->vmalloc); + mem->vmalloc = NULL; + } + q->bufs[i]->map = NULL; q->bufs[i]->baddr = 0; } @@ -391,6 +404,15 @@ void videobuf_vmalloc_free (struct videobuf_buffer *buf) { struct videobuf_vmalloc_memory *mem = buf->priv; + /* mmapped memory can't be freed here, otherwise mmapped region + would be released, while still needed. In this case, the memory + release should happen inside videobuf_vm_close(). + So, it should free memory only if the memory were allocated for + read() operation. + */ + if ((buf->memory != V4L2_MEMORY_USERPTR) || (buf->baddr == 0)) + return; + if (!mem) return; @@ -399,15 +421,6 @@ void videobuf_vmalloc_free (struct videobuf_buffer *buf) vfree(mem->vmalloc); mem->vmalloc = NULL; - -#if 0 - /* FIXME: This would be the proper place for freeing buf->map? */ - kfree(buf->map); - buf->map = NULL; -#endif - - /* FIXME: need to do buf->priv = NULL? */ - return; } EXPORT_SYMBOL_GPL(videobuf_vmalloc_free); -- cgit v1.2.3 From e63393d099ee7dc257d46a85611ac301c7cb2f1c Mon Sep 17 00:00:00 2001 From: Mauro Carvalho Chehab Date: Sun, 13 Apr 2008 11:59:29 -0300 Subject: videobuf-dma-sg: Remove unused flag From: Mauro Carvalho Chehab Signed-off-by: Mauro Carvalho Chehab --- linux/drivers/media/video/videobuf-dma-sg.c | 4 ---- 1 file changed, 4 deletions(-) (limited to 'linux/drivers/media') diff --git a/linux/drivers/media/video/videobuf-dma-sg.c b/linux/drivers/media/video/videobuf-dma-sg.c index 47280bf9f..ca1c60451 100644 --- a/linux/drivers/media/video/videobuf-dma-sg.c +++ b/linux/drivers/media/video/videobuf-dma-sg.c @@ -163,9 +163,6 @@ static int videobuf_dma_init_user_locked(struct videobuf_dmabuf *dma, dprintk(1,"init user [0x%lx+0x%lx => %d pages]\n", data,size,dma->nr_pages); - dma->varea = (void *) data; - - err = get_user_pages(current,current->mm, data & PAGE_MASK, dma->nr_pages, rw == READ, 1, /* force */ @@ -305,7 +302,6 @@ int videobuf_dma_free(struct videobuf_dmabuf *dma) vfree(dma->vmalloc); dma->vmalloc = NULL; - dma->varea = NULL; if (dma->bus_addr) { dma->bus_addr = 0; -- cgit v1.2.3 From 8010a5bd46af7405df1730fd12dff88753d717ba Mon Sep 17 00:00:00 2001 From: Mauro Carvalho Chehab Date: Sun, 13 Apr 2008 12:01:12 -0300 Subject: em28xx: remove timeout From: Mauro Carvalho Chehab It seems that we don't need a timeout for em28xx. Signed-off-by: Mauro Carvalho Chehab --- linux/drivers/media/video/em28xx/em28xx-video.c | 31 ------------------------- linux/drivers/media/video/em28xx/em28xx.h | 1 - 2 files changed, 32 deletions(-) (limited to 'linux/drivers/media') diff --git a/linux/drivers/media/video/em28xx/em28xx-video.c b/linux/drivers/media/video/em28xx/em28xx-video.c index 347fc3837..b96fc598c 100644 --- a/linux/drivers/media/video/em28xx/em28xx-video.c +++ b/linux/drivers/media/video/em28xx/em28xx-video.c @@ -68,8 +68,6 @@ MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]"); printk(KERN_INFO "%s %s :"fmt, \ dev->name, __func__ , ##arg); } while (0) -#define BUFFER_TIMEOUT msecs_to_jiffies(2000) /* 2 seconds */ - /* Limits minimum and default number of buffers */ #define EM28XX_MIN_BUF 4 #define EM28XX_DEF_BUF 8 @@ -153,8 +151,6 @@ static inline void buffer_filled(struct em28xx *dev, struct em28xx_dmaqueue *dma_q, struct em28xx_buffer *buf) { - mod_timer(&dma_q->timeout, jiffies + BUFFER_TIMEOUT); - /* Advice that buffer was filled */ em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i); buf->vb.state = VIDEOBUF_DONE; @@ -469,7 +465,6 @@ static void em28xx_uninit_isoc(struct em28xx *dev) dev->isoc_ctl.num_bufs = 0; - del_timer(&dev->vidq.timeout); em28xx_capture_start(dev, 0); } @@ -584,28 +579,6 @@ static int em28xx_start_thread(struct em28xx_dmaqueue *dma_q) return 0; } -static void em28xx_vid_timeout(unsigned long data) -{ - struct em28xx *dev = (struct em28xx *)data; - struct em28xx_dmaqueue *vidq = &dev->vidq; - struct em28xx_buffer *buf; - unsigned long flags; - - spin_lock_irqsave(&dev->slock, flags); - - list_for_each_entry(buf, vidq->active.next, vb.queue) { - list_del(&buf->vb.queue); - buf->vb.state = VIDEOBUF_ERROR; - wake_up(&buf->vb.done); - em28xx_videodbg("em28xx/0: [%p/%d] timeout\n", - buf, buf->vb.i); - } - /* Instead of trying to restart, just sets timeout again */ - mod_timer(&vidq->timeout, jiffies + BUFFER_TIMEOUT); - - spin_unlock_irqrestore(&dev->slock, flags); -} - /* ------------------------------------------------------------------ Videobuf operations ------------------------------------------------------------------*/ @@ -2293,10 +2266,6 @@ static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev, INIT_LIST_HEAD(&dev->vidq.active); INIT_LIST_HEAD(&dev->vidq.queued); - dev->vidq.timeout.function = em28xx_vid_timeout; - dev->vidq.timeout.data = (unsigned long)dev; - init_timer(&dev->vidq.timeout); - #if 0 video_set_drvdata(dev->vbi_dev, dev); #endif diff --git a/linux/drivers/media/video/em28xx/em28xx.h b/linux/drivers/media/video/em28xx/em28xx.h index da6726719..0280a6847 100644 --- a/linux/drivers/media/video/em28xx/em28xx.h +++ b/linux/drivers/media/video/em28xx/em28xx.h @@ -141,7 +141,6 @@ struct em28xx_buffer { struct em28xx_dmaqueue { struct list_head active; struct list_head queued; - struct timer_list timeout; wait_queue_head_t wq; -- cgit v1.2.3 From fb0ecb723f596bc9c34f10219f35e615736400c7 Mon Sep 17 00:00:00 2001 From: Mauro Carvalho Chehab Date: Sun, 13 Apr 2008 12:02:24 -0300 Subject: em28xx: fix locking on vidioc_s_fmt_cap Currently, vidioc_s_fmt_cap is allowed even if streaming is running on some other fh. This is likely to cause issues. Block use of vidioc_s_fmt_cap if someone else has claimed access to the device. Signed-Off-By: Aidan Thornton Signed-off-by: Mauro Carvalho Chehab --- linux/drivers/media/video/em28xx/em28xx-video.c | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'linux/drivers/media') diff --git a/linux/drivers/media/video/em28xx/em28xx-video.c b/linux/drivers/media/video/em28xx/em28xx-video.c index b96fc598c..91bbd3432 100644 --- a/linux/drivers/media/video/em28xx/em28xx-video.c +++ b/linux/drivers/media/video/em28xx/em28xx-video.c @@ -973,6 +973,12 @@ static int vidioc_s_fmt_cap(struct file *file, void *priv, goto out; } + if (dev->stream_on && !fh->stream_on) { + em28xx_errdev("%s device in use by another fh\n", __func__); + rc = -EBUSY; + goto out; + } + /* set new image size */ dev->width = f->fmt.pix.width; dev->height = f->fmt.pix.height; -- cgit v1.2.3 From 2edff93c89e1c94ffa7195a40f1ee0985abbf378 Mon Sep 17 00:00:00 2001 From: Mauro Carvalho Chehab Date: Sun, 13 Apr 2008 12:05:47 -0300 Subject: em28xx: honour video_debug modprobe parameter From: Mauro Carvalho Chehab Signed-off-by: Mauro Carvalho Chehab --- linux/drivers/media/video/em28xx/em28xx-video.c | 1 + 1 file changed, 1 insertion(+) (limited to 'linux/drivers/media') diff --git a/linux/drivers/media/video/em28xx/em28xx-video.c b/linux/drivers/media/video/em28xx/em28xx-video.c index 91bbd3432..0eff246dc 100644 --- a/linux/drivers/media/video/em28xx/em28xx-video.c +++ b/linux/drivers/media/video/em28xx/em28xx-video.c @@ -2138,6 +2138,7 @@ static struct video_device *em28xx_vdev_init(struct em28xx *dev, vfd->release = video_device_release; #endif vfd->type = type; + vfd->debug = video_debug; snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name); -- cgit v1.2.3 From 0f126d6b111a068bc911b3d01ff561818c073345 Mon Sep 17 00:00:00 2001 From: Mauro Carvalho Chehab Date: Sun, 13 Apr 2008 12:06:24 -0300 Subject: videobuf: Improve command output for debug purposes From: Mauro Carvalho Chehab Signed-off-by: Mauro Carvalho Chehab --- linux/drivers/media/video/ivtv/ivtv-ioctl.c | 3 ++ linux/drivers/media/video/pwc/pwc-v4l.c | 4 +- linux/drivers/media/video/videodev.c | 57 ++++++++++++++++------------- 3 files changed, 38 insertions(+), 26 deletions(-) (limited to 'linux/drivers/media') diff --git a/linux/drivers/media/video/ivtv/ivtv-ioctl.c b/linux/drivers/media/video/ivtv/ivtv-ioctl.c index 873296394..a5da513d6 100644 --- a/linux/drivers/media/video/ivtv/ivtv-ioctl.c +++ b/linux/drivers/media/video/ivtv/ivtv-ioctl.c @@ -1644,6 +1644,7 @@ static int ivtv_v4l2_do_ioctl(struct inode *inode, struct file *filp, if (ivtv_debug & IVTV_DBGFLG_IOCTL) { printk(KERN_INFO "ivtv%d ioctl: ", itv->num); v4l_printk_ioctl(cmd); + printk("\n"); } return ivtv_debug_ioctls(filp, cmd, arg); @@ -1687,6 +1688,7 @@ static int ivtv_v4l2_do_ioctl(struct inode *inode, struct file *filp, if (ivtv_debug & IVTV_DBGFLG_IOCTL) { printk(KERN_INFO "ivtv%d ioctl: ", itv->num); v4l_printk_ioctl(cmd); + printk("\n"); } return ivtv_v4l2_ioctls(itv, filp, cmd, arg); @@ -1700,6 +1702,7 @@ static int ivtv_v4l2_do_ioctl(struct inode *inode, struct file *filp, if (ivtv_debug & IVTV_DBGFLG_IOCTL) { printk(KERN_INFO "ivtv%d ioctl: ", itv->num); v4l_printk_ioctl(cmd); + printk("\n"); } return ivtv_control_ioctls(itv, cmd, arg); diff --git a/linux/drivers/media/video/pwc/pwc-v4l.c b/linux/drivers/media/video/pwc/pwc-v4l.c index 32fbe1ae6..174288987 100644 --- a/linux/drivers/media/video/pwc/pwc-v4l.c +++ b/linux/drivers/media/video/pwc/pwc-v4l.c @@ -351,8 +351,10 @@ int pwc_video_do_ioctl(struct inode *inode, struct file *file, return -EFAULT; #ifdef CONFIG_USB_PWC_DEBUG - if (PWC_DEBUG_LEVEL_IOCTL & pwc_trace) + if (PWC_DEBUG_LEVEL_IOCTL & pwc_trace) { v4l_printk_ioctl(cmd); + printk("\n"); + } #endif diff --git a/linux/drivers/media/video/videodev.c b/linux/drivers/media/video/videodev.c index f73f876e0..9c5f66274 100644 --- a/linux/drivers/media/video/videodev.c +++ b/linux/drivers/media/video/videodev.c @@ -18,9 +18,9 @@ #define dbgarg(cmd, fmt, arg...) \ if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) { \ - printk (KERN_DEBUG "%s: ", vfd->name); \ + printk(KERN_DEBUG "%s: ", vfd->name); \ v4l_printk_ioctl(cmd); \ - printk (KERN_DEBUG "%s: " fmt, vfd->name, ## arg); \ + printk(" " fmt, ## arg); \ } #define dbgarg2(fmt, arg...) \ @@ -382,38 +382,45 @@ static const char *v4l2_int_ioctls[] = { external ioctl messages as well as internal V4L ioctl */ void v4l_printk_ioctl(unsigned int cmd) { - char *dir; + char *dir, *type; - switch (_IOC_DIR(cmd)) { - case _IOC_NONE: dir = "--"; break; - case _IOC_READ: dir = "r-"; break; - case _IOC_WRITE: dir = "-w"; break; - case _IOC_READ | _IOC_WRITE: dir = "rw"; break; - default: dir = "*ERR*"; break; - } switch (_IOC_TYPE(cmd)) { case 'd': - printk("v4l2_int ioctl %s, dir=%s (0x%08x)\n", - (_IOC_NR(cmd) < V4L2_INT_IOCTLS) ? - v4l2_int_ioctls[_IOC_NR(cmd)] : "UNKNOWN", dir, cmd); - break; + if (_IOC_NR(cmd) >= V4L2_INT_IOCTLS) { + type = "v4l2_int"; + break; + } + printk("%s", v4l2_int_ioctls[_IOC_NR(cmd)]); + return; #ifdef CONFIG_VIDEO_V4L1_COMPAT case 'v': - printk("v4l1 ioctl %s, dir=%s (0x%08x)\n", - (_IOC_NR(cmd) < V4L1_IOCTLS) ? - v4l1_ioctls[_IOC_NR(cmd)] : "UNKNOWN", dir, cmd); - break; + if (_IOC_NR(cmd) >= V4L1_IOCTLS) { + type = "v4l1"; + break; + } + printk("%s", v4l1_ioctls[_IOC_NR(cmd)]); + return; #endif case 'V': - printk("v4l2 ioctl %s, dir=%s (0x%08x)\n", - (_IOC_NR(cmd) < V4L2_IOCTLS) ? - v4l2_ioctls[_IOC_NR(cmd)] : "UNKNOWN", dir, cmd); - break; - + if (_IOC_NR(cmd) >= V4L2_IOCTLS) { + type = "v4l2"; + break; + } + printk("%s", v4l2_ioctls[_IOC_NR(cmd)]); + return; default: - printk("unknown ioctl '%c', dir=%s, #%d (0x%08x)\n", - _IOC_TYPE(cmd), dir, _IOC_NR(cmd), cmd); + type = "unknown"; + } + + switch (_IOC_DIR(cmd)) { + case _IOC_NONE: dir = "--"; break; + case _IOC_READ: dir = "r-"; break; + case _IOC_WRITE: dir = "-w"; break; + case _IOC_READ | _IOC_WRITE: dir = "rw"; break; + default: dir = "*ERR*"; break; } + printk("%s ioctl '%c', dir=%s, #%d (0x%08x)", + type, _IOC_TYPE(cmd), dir, _IOC_NR(cmd), cmd); } EXPORT_SYMBOL(v4l_printk_ioctl); -- cgit v1.2.3 From 22add3caf635d41daa1988b08a8f343ed9823318 Mon Sep 17 00:00:00 2001 From: Mauro Carvalho Chehab Date: Sun, 13 Apr 2008 12:06:52 -0300 Subject: em28xx: Fills the entire buffer, before getting another one From: Mauro Carvalho Chehab Signed-off-by: Mauro Carvalho Chehab --- linux/drivers/media/video/em28xx/em28xx-video.c | 31 ++++++++++++++++++------- 1 file changed, 22 insertions(+), 9 deletions(-) (limited to 'linux/drivers/media') diff --git a/linux/drivers/media/video/em28xx/em28xx-video.c b/linux/drivers/media/video/em28xx/em28xx-video.c index 0eff246dc..53e460cf9 100644 --- a/linux/drivers/media/video/em28xx/em28xx-video.c +++ b/linux/drivers/media/video/em28xx/em28xx-video.c @@ -157,6 +157,8 @@ static inline void buffer_filled(struct em28xx *dev, buf->vb.field_count++; do_gettimeofday(&buf->vb.ts); + dev->isoc_ctl.buf = NULL; + list_del(&buf->vb.queue); wake_up(&buf->vb.done); } @@ -288,6 +290,14 @@ static inline int get_next_buf(struct em28xx_dmaqueue *dma_q, struct em28xx_buffer **buf) { struct em28xx *dev = container_of(dma_q, struct em28xx, vidq); +#if 0 + char *outp; +#endif + + /* If the previous buffer were not filled yet, continue */ + *buf = dev->isoc_ctl.buf; + if (*buf) + return 1; if (list_empty(&dma_q->active)) { em28xx_isocdbg("No active queue to serve\n"); @@ -296,6 +306,14 @@ static inline int get_next_buf(struct em28xx_dmaqueue *dma_q, *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue); +#if 0 + /* Cleans up buffer - Usefull for testing for frame continuity */ + outp = videobuf_to_vmalloc(&(*buf)->vb); + memset(outp, 0, (*buf)->vb.size); +#endif + + dev->isoc_ctl.buf = *buf; + return 1; } @@ -323,13 +341,9 @@ static inline int em28xx_isoc_copy(struct urb *urb) return 0; } - buf = dev->isoc_ctl.buf; - - if (!buf) { - rc = get_next_buf(dma_q, &buf); - if (rc <= 0) - return rc; - } + rc = get_next_buf(dma_q, &buf); + if (rc <= 0) + return rc; outp = videobuf_to_vmalloc(&buf->vb); @@ -371,7 +385,6 @@ static inline int em28xx_isoc_copy(struct urb *urb) rc = get_next_buf(dma_q, &buf); if (rc <= 0) return rc; - outp = videobuf_to_vmalloc(&buf->vb); } @@ -440,7 +453,6 @@ static void em28xx_uninit_isoc(struct em28xx *dev) em28xx_isocdbg("em28xx: called em28xx_uninit_isoc\n"); dev->isoc_ctl.nfields = -1; - dev->isoc_ctl.buf = NULL; for (i = 0; i < dev->isoc_ctl.num_bufs; i++) { urb = dev->isoc_ctl.urb[i]; if (urb) { @@ -502,6 +514,7 @@ static int em28xx_prepare_isoc(struct em28xx *dev, int max_packets, } dev->isoc_ctl.max_pkt_size = dev->max_pkt_size; + dev->isoc_ctl.buf = NULL; sb_size = max_packets * dev->isoc_ctl.max_pkt_size; -- cgit v1.2.3 From 97d73f901ce2d7930f127373845d07107e747525 Mon Sep 17 00:00:00 2001 From: Mauro Carvalho Chehab Date: Sun, 13 Apr 2008 12:07:16 -0300 Subject: videodev: Some printk fixes From: Mauro Carvalho Chehab Signed-off-by: Mauro Carvalho Chehab --- linux/drivers/media/video/videodev.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'linux/drivers/media') diff --git a/linux/drivers/media/video/videodev.c b/linux/drivers/media/video/videodev.c index 9c5f66274..31e28a14f 100644 --- a/linux/drivers/media/video/videodev.c +++ b/linux/drivers/media/video/videodev.c @@ -25,7 +25,7 @@ #define dbgarg2(fmt, arg...) \ if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) \ - printk (KERN_DEBUG "%s: " fmt, vfd->name, ## arg); + printk(KERN_DEBUG "%s: " fmt, vfd->name, ## arg); #include #include @@ -818,6 +818,7 @@ static int __video_do_ioctl(struct inode *inode, struct file *file, if ( (vfd->debug & V4L2_DEBUG_IOCTL) && !(vfd->debug & V4L2_DEBUG_IOCTL_ARG)) { v4l_print_ioctl(vfd->name, cmd); + printk("\n"); } #ifdef CONFIG_VIDEO_V4L1_COMPAT @@ -1901,8 +1902,9 @@ static int __video_do_ioctl(struct inode *inode, struct file *file, if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) { if (ret<0) { - printk ("%s: err:\n", vfd->name); + printk("%s: err: on ", vfd->name); v4l_print_ioctl(vfd->name, cmd); + printk("\n"); } } -- cgit v1.2.3 From 94cf06dade425c25f3d5b798177ff81dc9ca1713 Mon Sep 17 00:00:00 2001 From: Mauro Carvalho Chehab Date: Sun, 13 Apr 2008 12:07:56 -0300 Subject: videobuf-vmalloc: stop streaming before unmap From: Mauro Carvalho Chehab Before the patch, there were a risk of freeing and unmapping userspace memory, while there were pending requests. Signed-off-by: Mauro Carvalho Chehab --- linux/drivers/media/video/videobuf-core.c | 1 - linux/drivers/media/video/videobuf-vmalloc.c | 23 +++++++++++++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) (limited to 'linux/drivers/media') diff --git a/linux/drivers/media/video/videobuf-core.c b/linux/drivers/media/video/videobuf-core.c index 660d28972..7d420a119 100644 --- a/linux/drivers/media/video/videobuf-core.c +++ b/linux/drivers/media/video/videobuf-core.c @@ -902,7 +902,6 @@ static void __videobuf_read_stop(struct videobuf_queue *q) { int i; - videobuf_queue_cancel(q); __videobuf_mmap_free(q); INIT_LIST_HEAD(&q->stream); diff --git a/linux/drivers/media/video/videobuf-vmalloc.c b/linux/drivers/media/video/videobuf-vmalloc.c index b031c822f..c5f04b4c1 100644 --- a/linux/drivers/media/video/videobuf-vmalloc.c +++ b/linux/drivers/media/video/videobuf-vmalloc.c @@ -73,6 +73,11 @@ static void videobuf_vm_close(struct vm_area_struct *vma) dprintk(1, "munmap %p q=%p\n", map, q); mutex_lock(&q->vb_lock); + + /* We need first to cancel streams, before unmapping */ + if (q->streaming) + videobuf_queue_cancel(q); + for (i = 0; i < VIDEO_MAX_FRAME; i++) { if (NULL == q->bufs[i]) continue; @@ -87,7 +92,15 @@ static void videobuf_vm_close(struct vm_area_struct *vma) In this case, memory should be freed, in order to do memory unmap. */ + MAGIC_CHECK(mem->magic, MAGIC_VMAL_MEM); + + /* vfree is not atomic - can't be + called with IRQ's disabled + */ + dprintk(1, "%s: buf[%d] freeing (%p)\n", + __func__, i, mem->vmalloc); + vfree(mem->vmalloc); mem->vmalloc = NULL; } @@ -95,9 +108,12 @@ static void videobuf_vm_close(struct vm_area_struct *vma) q->bufs[i]->map = NULL; q->bufs[i]->baddr = 0; } - mutex_unlock(&q->vb_lock); + kfree(map); + + mutex_unlock(&q->vb_lock); } + return; } @@ -139,6 +155,7 @@ static int __videobuf_iolock (struct videobuf_queue* q, struct v4l2_framebuffer *fbuf) { struct videobuf_vmalloc_memory *mem = vb->priv; + int pages; BUG_ON(!mem); @@ -155,8 +172,7 @@ static int __videobuf_iolock (struct videobuf_queue* q, } break; case V4L2_MEMORY_USERPTR: - { - int pages = PAGE_ALIGN(vb->size); + pages = PAGE_ALIGN(vb->size); dprintk(1, "%s memory method USERPTR\n", __func__); @@ -199,7 +215,6 @@ static int __videobuf_iolock (struct videobuf_queue* q, #endif break; - } case V4L2_MEMORY_OVERLAY: default: dprintk(1, "%s memory method OVERLAY/unknown\n", __func__); -- cgit v1.2.3 From 554dd58d9f1afff01a8d3e24d09f48e3e18566a1 Mon Sep 17 00:00:00 2001 From: Mauro Carvalho Chehab Date: Sun, 13 Apr 2008 12:08:26 -0300 Subject: videobuf: All videobuf clients need a spinlock From: Mauro Carvalho Chehab Signed-off-by: Mauro Carvalho Chehab --- linux/drivers/media/video/videobuf-core.c | 52 ++++++++++++++----------------- 1 file changed, 23 insertions(+), 29 deletions(-) (limited to 'linux/drivers/media') diff --git a/linux/drivers/media/video/videobuf-core.c b/linux/drivers/media/video/videobuf-core.c index 7d420a119..ded07991b 100644 --- a/linux/drivers/media/video/videobuf-core.c +++ b/linux/drivers/media/video/videobuf-core.c @@ -127,6 +127,9 @@ void videobuf_queue_core_init(struct videobuf_queue *q, /* Having implementations for abstract methods are mandatory */ BUG_ON(!q->int_ops); + /* Having irqlock is mandatory */ + BUG_ON(!q->irqlock); + mutex_init(&q->vb_lock); init_waitqueue_head(&q->wait); INIT_LIST_HEAD(&q->stream); @@ -181,8 +184,7 @@ void videobuf_queue_cancel(struct videobuf_queue *q) wake_up_interruptible_sync(&q->wait); /* remove queued buffers from list */ - if (q->irqlock) - spin_lock_irqsave(q->irqlock, flags); + spin_lock_irqsave(q->irqlock, flags); for (i = 0; i < VIDEO_MAX_FRAME; i++) { if (NULL == q->bufs[i]) continue; @@ -192,8 +194,7 @@ void videobuf_queue_cancel(struct videobuf_queue *q) wake_up_all(&q->bufs[i]->done); } } - if (q->irqlock) - spin_unlock_irqrestore(q->irqlock, flags); + spin_unlock_irqrestore(q->irqlock, flags); /* free all buffers + clear queue */ for (i = 0; i < VIDEO_MAX_FRAME; i++) { @@ -549,11 +550,9 @@ int videobuf_qbuf(struct videobuf_queue *q, list_add_tail(&buf->stream, &q->stream); if (q->streaming) { - if (q->irqlock) - spin_lock_irqsave(q->irqlock, flags); + spin_lock_irqsave(q->irqlock, flags); q->ops->buf_queue(q, buf); - if (q->irqlock) - spin_unlock_irqrestore(q->irqlock, flags); + spin_unlock_irqrestore(q->irqlock, flags); } dprintk(1, "qbuf: succeded\n"); retval = 0; @@ -690,13 +689,11 @@ int videobuf_streamon(struct videobuf_queue *q) if (q->streaming) goto done; q->streaming = 1; - if (q->irqlock) - spin_lock_irqsave(q->irqlock, flags); + spin_lock_irqsave(q->irqlock, flags); list_for_each_entry(buf, &q->stream, stream) if (buf->state == VIDEOBUF_PREPARED) q->ops->buf_queue(q, buf); - if (q->irqlock) - spin_unlock_irqrestore(q->irqlock, flags); + spin_unlock_irqrestore(q->irqlock, flags); wake_up_interruptible_sync(&q->wait); done: @@ -752,11 +749,10 @@ static ssize_t videobuf_read_zerocopy(struct videobuf_queue *q, goto done; /* start capture & wait */ - if (q->irqlock) - spin_lock_irqsave(q->irqlock, flags); + spin_lock_irqsave(q->irqlock, flags); q->ops->buf_queue(q, q->read_buf); - if (q->irqlock) - spin_unlock_irqrestore(q->irqlock, flags); + spin_unlock_irqrestore(q->irqlock, flags); + retval = videobuf_waiton(q->read_buf, 0, 0); if (0 == retval) { CALL(q, sync, q, q->read_buf); @@ -817,12 +813,10 @@ ssize_t videobuf_read_one(struct videobuf_queue *q, q->read_buf = NULL; goto done; } - if (q->irqlock) - spin_lock_irqsave(q->irqlock, flags); - + spin_lock_irqsave(q->irqlock, flags); q->ops->buf_queue(q, q->read_buf); - if (q->irqlock) - spin_unlock_irqrestore(q->irqlock, flags); + spin_unlock_irqrestore(q->irqlock, flags); + q->read_off = 0; } @@ -888,12 +882,12 @@ static int __videobuf_read_start(struct videobuf_queue *q) return err; list_add_tail(&q->bufs[i]->stream, &q->stream); } - if (q->irqlock) - spin_lock_irqsave(q->irqlock, flags); + + spin_lock_irqsave(q->irqlock, flags); for (i = 0; i < count; i++) q->ops->buf_queue(q, q->bufs[i]); - if (q->irqlock) - spin_unlock_irqrestore(q->irqlock, flags); + spin_unlock_irqrestore(q->irqlock, flags); + q->reading = 1; return 0; } @@ -1005,11 +999,11 @@ ssize_t videobuf_read_stream(struct videobuf_queue *q, if (q->read_off == q->read_buf->size) { list_add_tail(&q->read_buf->stream, &q->stream); - if (q->irqlock) - spin_lock_irqsave(q->irqlock, flags); + + spin_lock_irqsave(q->irqlock, flags); q->ops->buf_queue(q, q->read_buf); - if (q->irqlock) - spin_unlock_irqrestore(q->irqlock, flags); + spin_unlock_irqrestore(q->irqlock, flags); + q->read_buf = NULL; } if (retval < 0) -- cgit v1.2.3 From c46748191d2f1175c2211b5ed20332935d06c9a8 Mon Sep 17 00:00:00 2001 From: Mauro Carvalho Chehab Date: Sun, 13 Apr 2008 12:08:55 -0300 Subject: em28xx: Add missing checks From: Mauro Carvalho Chehab There are some cases where nobody is waiting for a buffer. Due to the lack of check, if you try to abort the userspace app, machine were hanging, since IRQ were trying to use a buffer that were disallocated. Signed-off-by: Mauro Carvalho Chehab --- linux/drivers/media/video/em28xx/em28xx-video.c | 37 +++++++++++++++++-------- 1 file changed, 25 insertions(+), 12 deletions(-) (limited to 'linux/drivers/media') diff --git a/linux/drivers/media/video/em28xx/em28xx-video.c b/linux/drivers/media/video/em28xx/em28xx-video.c index 53e460cf9..0d8b359d1 100644 --- a/linux/drivers/media/video/em28xx/em28xx-video.c +++ b/linux/drivers/media/video/em28xx/em28xx-video.c @@ -290,24 +290,40 @@ static inline int get_next_buf(struct em28xx_dmaqueue *dma_q, struct em28xx_buffer **buf) { struct em28xx *dev = container_of(dma_q, struct em28xx, vidq); -#if 0 +#if 1 char *outp; #endif - /* If the previous buffer were not filled yet, continue */ + if (list_empty(&dma_q->active)) { + em28xx_isocdbg("No active queue to serve\n"); + dev->isoc_ctl.buf = NULL; + return 0; + } + + /* Check if the last buffer were fully filled */ *buf = dev->isoc_ctl.buf; + + /* Nobody is waiting on this buffer - discards */ + if (*buf && !waitqueue_active(&(*buf)->vb.done)) { + dev->isoc_ctl.buf = NULL; + *buf = NULL; + } + + /* Returns the last buffer, to be filled with remaining data */ if (*buf) return 1; - if (list_empty(&dma_q->active)) { + /* Get the next buffer */ + *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue); + + /* Nobody is waiting on the next buffer. returns */ + if (!*buf || !waitqueue_active(&(*buf)->vb.done)) { em28xx_isocdbg("No active queue to serve\n"); return 0; } - *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue); - -#if 0 - /* Cleans up buffer - Usefull for testing for frame continuity */ +#if 1 + /* Cleans up buffer - Usefull for testing for frame/URB loss */ outp = videobuf_to_vmalloc(&(*buf)->vb); memset(outp, 0, (*buf)->vb.size); #endif @@ -419,12 +435,11 @@ static void em28xx_irq_callback(struct urb *urb) struct em28xx_dmaqueue *dma_q = urb->context; struct em28xx *dev = container_of(dma_q, struct em28xx, vidq); int rc, i; - unsigned long flags; - - spin_lock_irqsave(&dev->slock, flags); /* Copy data from URB */ + spin_lock(&dev->slock); rc = em28xx_isoc_copy(urb); + spin_unlock(&dev->slock); /* Reset urb buffers */ for (i = 0; i < urb->number_of_packets; i++) { @@ -438,8 +453,6 @@ static void em28xx_irq_callback(struct urb *urb) em28xx_err("urb resubmit failed (error=%i)\n", urb->status); } - - spin_unlock_irqrestore(&dev->slock, flags); } /* -- cgit v1.2.3 From 081541c29a9db0da6d859a340de9dc5b7e7c77fc Mon Sep 17 00:00:00 2001 From: Mauro Carvalho Chehab Date: Sun, 13 Apr 2008 12:09:14 -0300 Subject: em28xx: Some fixes to display logic From: Mauro Carvalho Chehab Signed-off-by: Mauro Carvalho Chehab --- linux/drivers/media/video/em28xx/em28xx-video.c | 31 +++++++++++++------------ linux/drivers/media/video/em28xx/em28xx.h | 3 +++ 2 files changed, 19 insertions(+), 15 deletions(-) (limited to 'linux/drivers/media') diff --git a/linux/drivers/media/video/em28xx/em28xx-video.c b/linux/drivers/media/video/em28xx/em28xx-video.c index 0d8b359d1..2b1649be3 100644 --- a/linux/drivers/media/video/em28xx/em28xx-video.c +++ b/linux/drivers/media/video/em28xx/em28xx-video.c @@ -389,32 +389,33 @@ static inline int em28xx_isoc_copy(struct urb *urb) /* FIXME: incomplete buffer checks where removed to make logic simpler. Impacts of those changes should be evaluated */ + if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) { + em28xx_isocdbg("VBI HEADER!!!\n"); + /* FIXME: Should add vbi copy */ + continue; + } if (p[0] == 0x22 && p[1] == 0x5a) { em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2], len, (p[2] & 1)? "odd" : "even"); if (p[2] & 1) buf->top_field = 0; - else { - if (buf->receiving) { - buffer_filled(dev, dma_q, buf); - rc = get_next_buf(dma_q, &buf); - if (rc <= 0) - return rc; - outp = videobuf_to_vmalloc(&buf->vb); - } - + else buf->top_field = 1; + +// if (dev->isoc_ctl.last_field && !buf->top_field) { + if (dev->isoc_ctl.last_field != buf->top_field) { + buffer_filled(dev, dma_q, buf); + rc = get_next_buf(dma_q, &buf); + if (rc <= 0) + return rc; + outp = videobuf_to_vmalloc(&buf->vb); } - buf->receiving = 1; + dev->isoc_ctl.last_field = buf->top_field; + dma_q->pos = 0; - } else if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) { - em28xx_isocdbg("VBI HEADER!!!\n"); } - em28xx_copy_video(dev, dma_q, buf, p, outp, len); - - /* FIXME: Should add vbi copy */ } return rc; } diff --git a/linux/drivers/media/video/em28xx/em28xx.h b/linux/drivers/media/video/em28xx/em28xx.h index 0280a6847..0597fd74b 100644 --- a/linux/drivers/media/video/em28xx/em28xx.h +++ b/linux/drivers/media/video/em28xx/em28xx.h @@ -117,6 +117,9 @@ struct em28xx_usb_isoc_ctl { /* Stores already requested buffers */ struct em28xx_buffer *buf; + /* Store last filled frame */ + int last_field; + /* Stores the number of received fields */ int nfields; }; -- cgit v1.2.3 From 85d8e4c1c7772689a1b1fc27cf448ac3a261e3ec Mon Sep 17 00:00:00 2001 From: Mauro Carvalho Chehab Date: Sun, 13 Apr 2008 12:09:36 -0300 Subject: em28xx: fix buffer underrun handling This patch fixes three related issues and a fourth trivial one: - Use buffers even if no-one's currently waiting for them (fixes underrun issues); - Don't return incomplete/mangled frames at the start of streaming and in the case of buffer underruns; - Fix an issue which could cause the driver to write to a buffer that's been freed after videobuf_queue_cancel is called (exposed by the previous two fixes - for some reason, ignoring buffers that weren't being waited on worked around the issue); - Fix a bug which could cause only one field to be filled in the first buffer (or first few buffers) after streaming is started. Signed-Off-By: Aidan Thornton Signed-off-by: Mauro Carvalho Chehab --- linux/drivers/media/video/em28xx/em28xx-video.c | 84 ++++++++++++------------- linux/drivers/media/video/em28xx/em28xx.h | 3 - 2 files changed, 42 insertions(+), 45 deletions(-) (limited to 'linux/drivers/media') diff --git a/linux/drivers/media/video/em28xx/em28xx-video.c b/linux/drivers/media/video/em28xx/em28xx-video.c index 2b1649be3..188bd512c 100644 --- a/linux/drivers/media/video/em28xx/em28xx-video.c +++ b/linux/drivers/media/video/em28xx/em28xx-video.c @@ -286,7 +286,7 @@ static inline void print_err_status(struct em28xx *dev, /* * video-buf generic routine to get the next available buffer */ -static inline int get_next_buf(struct em28xx_dmaqueue *dma_q, +static inline void get_next_buf(struct em28xx_dmaqueue *dma_q, struct em28xx_buffer **buf) { struct em28xx *dev = container_of(dma_q, struct em28xx, vidq); @@ -296,32 +296,14 @@ static inline int get_next_buf(struct em28xx_dmaqueue *dma_q, if (list_empty(&dma_q->active)) { em28xx_isocdbg("No active queue to serve\n"); - dev->isoc_ctl.buf = NULL; - return 0; - } - - /* Check if the last buffer were fully filled */ - *buf = dev->isoc_ctl.buf; - - /* Nobody is waiting on this buffer - discards */ - if (*buf && !waitqueue_active(&(*buf)->vb.done)) { dev->isoc_ctl.buf = NULL; *buf = NULL; + return; } - /* Returns the last buffer, to be filled with remaining data */ - if (*buf) - return 1; - /* Get the next buffer */ *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue); - /* Nobody is waiting on the next buffer. returns */ - if (!*buf || !waitqueue_active(&(*buf)->vb.done)) { - em28xx_isocdbg("No active queue to serve\n"); - return 0; - } - #if 1 /* Cleans up buffer - Usefull for testing for frame/URB loss */ outp = videobuf_to_vmalloc(&(*buf)->vb); @@ -330,7 +312,7 @@ static inline int get_next_buf(struct em28xx_dmaqueue *dma_q, dev->isoc_ctl.buf = *buf; - return 1; + return; } /* @@ -341,7 +323,7 @@ static inline int em28xx_isoc_copy(struct urb *urb) struct em28xx_buffer *buf; struct em28xx_dmaqueue *dma_q = urb->context; struct em28xx *dev = container_of(dma_q, struct em28xx, vidq); - unsigned char *outp; + unsigned char *outp = NULL; int i, len = 0, rc = 1; unsigned char *p; @@ -357,11 +339,9 @@ static inline int em28xx_isoc_copy(struct urb *urb) return 0; } - rc = get_next_buf(dma_q, &buf); - if (rc <= 0) - return rc; - - outp = videobuf_to_vmalloc(&buf->vb); + buf = dev->isoc_ctl.buf; + if (buf != NULL) + outp = videobuf_to_vmalloc(&buf->vb); for (i = 0; i < urb->number_of_packets; i++) { int status = urb->iso_frame_desc[i].status; @@ -398,24 +378,27 @@ static inline int em28xx_isoc_copy(struct urb *urb) em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2], len, (p[2] & 1)? "odd" : "even"); - if (p[2] & 1) - buf->top_field = 0; - else - buf->top_field = 1; - -// if (dev->isoc_ctl.last_field && !buf->top_field) { - if (dev->isoc_ctl.last_field != buf->top_field) { - buffer_filled(dev, dma_q, buf); - rc = get_next_buf(dma_q, &buf); - if (rc <= 0) - return rc; - outp = videobuf_to_vmalloc(&buf->vb); + if (!(p[2] & 1)) { + if (buf != NULL) + buffer_filled(dev, dma_q, buf); + get_next_buf(dma_q, &buf); + if (buf == NULL) + outp = NULL; + else + outp = videobuf_to_vmalloc(&buf->vb); + } + + if (buf != NULL) { + if (p[2] & 1) + buf->top_field = 0; + else + buf->top_field = 1; } - dev->isoc_ctl.last_field = buf->top_field; dma_q->pos = 0; } - em28xx_copy_video(dev, dma_q, buf, p, outp, len); + if (buf != NULL) + em28xx_copy_video(dev, dma_q, buf, p, outp, len); } return rc; } @@ -625,12 +608,29 @@ buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size) return 0; } +/* This is called *without* dev->slock held; please keep it that way */ static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf) { + struct em28xx_fh *fh = vq->priv_data; + struct em28xx *dev = fh->dev; + unsigned long flags = 0; if (in_interrupt()) BUG(); - videobuf_waiton(&buf->vb, 0, 0); + /* We used to wait for the buffer to finish here, but this didn't work + because, as we were keeping the state as VIDEOBUF_QUEUED, + videobuf_queue_cancel marked it as finished for us. + (Also, it could wedge forever if the hardware was misconfigured.) + + This should be safe; by the time we get here, the buffer isn't + queued anymore. If we ever start marking the buffers as + VIDEOBUF_ACTIVE, it won't be, though. + */ + spin_lock_irqsave(&dev->slock, flags); + if (dev->isoc_ctl.buf == buf) + dev->isoc_ctl.buf = NULL; + spin_unlock_irqrestore(&dev->slock, flags); + videobuf_vmalloc_free(&buf->vb); buf->vb.state = VIDEOBUF_NEEDS_INIT; } diff --git a/linux/drivers/media/video/em28xx/em28xx.h b/linux/drivers/media/video/em28xx/em28xx.h index 0597fd74b..0280a6847 100644 --- a/linux/drivers/media/video/em28xx/em28xx.h +++ b/linux/drivers/media/video/em28xx/em28xx.h @@ -117,9 +117,6 @@ struct em28xx_usb_isoc_ctl { /* Stores already requested buffers */ struct em28xx_buffer *buf; - /* Store last filled frame */ - int last_field; - /* Stores the number of received fields */ int nfields; }; -- cgit v1.2.3 From 1def8c1f6f0fb108cf841f29e899253c8b8e3523 Mon Sep 17 00:00:00 2001 From: Mauro Carvalho Chehab Date: Sun, 13 Apr 2008 12:10:00 -0300 Subject: videobuf-dvb: allow its usage with videobuf-vmalloc From: Mauro Carvalho Chehab videobuf-dvb were still using a function that were videobuf-dma-sg dependent. This patch creates a generic handler for this function. This way, videobuf-dvb can now work with all videobuf implementations. Signed-off-by: Mauro Carvalho Chehab --- linux/drivers/media/video/videobuf-core.c | 7 +++++++ linux/drivers/media/video/videobuf-dma-sg.c | 11 +++++++++++ linux/drivers/media/video/videobuf-dvb.c | 10 ++++++---- linux/drivers/media/video/videobuf-vmalloc.c | 1 + 4 files changed, 25 insertions(+), 4 deletions(-) (limited to 'linux/drivers/media') diff --git a/linux/drivers/media/video/videobuf-core.c b/linux/drivers/media/video/videobuf-core.c index ded07991b..367f150a9 100644 --- a/linux/drivers/media/video/videobuf-core.c +++ b/linux/drivers/media/video/videobuf-core.c @@ -95,6 +95,13 @@ int videobuf_iolock(struct videobuf_queue *q, struct videobuf_buffer *vb, return CALL(q, iolock, q, vb, fbuf); } +void *videobuf_queue_to_vmalloc (struct videobuf_queue *q, + struct videobuf_buffer *buf) +{ + return CALL(q, vmalloc, buf); +} +EXPORT_SYMBOL_GPL(videobuf_queue_to_vmalloc); + /* --------------------------------------------------------------------- */ diff --git a/linux/drivers/media/video/videobuf-dma-sg.c b/linux/drivers/media/video/videobuf-dma-sg.c index ca1c60451..7d459ed6b 100644 --- a/linux/drivers/media/video/videobuf-dma-sg.c +++ b/linux/drivers/media/video/videobuf-dma-sg.c @@ -478,6 +478,16 @@ static void *__videobuf_alloc(size_t size) return vb; } +static void *__videobuf_to_vmalloc (struct videobuf_buffer *buf) +{ + struct videobuf_dma_sg_memory *mem = buf->priv; + BUG_ON(!mem); + + MAGIC_CHECK(mem->magic, MAGIC_SG_MEM); + + return mem->dma.vmalloc; +} + static int __videobuf_iolock (struct videobuf_queue* q, struct videobuf_buffer *vb, struct v4l2_framebuffer *fbuf) @@ -723,6 +733,7 @@ static struct videobuf_qtype_ops sg_ops = { .mmap_mapper = __videobuf_mmap_mapper, .video_copy_to_user = __videobuf_copy_to_user, .copy_stream = __videobuf_copy_stream, + .vmalloc = __videobuf_to_vmalloc, }; void *videobuf_sg_alloc(size_t size) diff --git a/linux/drivers/media/video/videobuf-dvb.c b/linux/drivers/media/video/videobuf-dvb.c index 301d5af12..9a821371a 100644 --- a/linux/drivers/media/video/videobuf-dvb.c +++ b/linux/drivers/media/video/videobuf-dvb.c @@ -21,13 +21,14 @@ #include #include #include + #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) #include #else #include #endif -#include +#include #include #include "compat.h" @@ -51,7 +52,7 @@ static int videobuf_dvb_thread(void *data) struct videobuf_buffer *buf; unsigned long flags; int err; - struct videobuf_dmabuf *dma; + void *outp; dprintk("dvb thread started\n"); set_freezable(); @@ -72,9 +73,10 @@ static int videobuf_dvb_thread(void *data) try_to_freeze(); /* feed buffer data to demux */ - dma=videobuf_to_dma(buf); + outp = videobuf_queue_to_vmalloc (&dvb->dvbq, buf); + if (buf->state == VIDEOBUF_DONE) - dvb_dmx_swfilter(&dvb->demux, dma->vmalloc, + dvb_dmx_swfilter(&dvb->demux, outp, buf->size); /* requeue buffer */ diff --git a/linux/drivers/media/video/videobuf-vmalloc.c b/linux/drivers/media/video/videobuf-vmalloc.c index c5f04b4c1..6f487b348 100644 --- a/linux/drivers/media/video/videobuf-vmalloc.c +++ b/linux/drivers/media/video/videobuf-vmalloc.c @@ -388,6 +388,7 @@ static struct videobuf_qtype_ops qops = { .mmap_mapper = __videobuf_mmap_mapper, .video_copy_to_user = __videobuf_copy_to_user, .copy_stream = __videobuf_copy_stream, + .vmalloc = videobuf_to_vmalloc, }; void videobuf_queue_vmalloc_init(struct videobuf_queue* q, -- cgit v1.2.3 From cfff1cb68e96dcce0c0880af6db1b4461e19ba9b Mon Sep 17 00:00:00 2001 From: Mauro Carvalho Chehab Date: Sun, 13 Apr 2008 12:11:08 -0300 Subject: em28xx: Some cleanups From: Mauro Carvalho Chehab Removes some fields from data structs. There are some fields that are just caching some calculus for buffer size. The calculus were moved to the places it were needed and the now unused fields were removed. Signed-off-by: Mauro Carvalho Chehab --- linux/drivers/media/video/em28xx/em28xx-core.c | 4 +-- linux/drivers/media/video/em28xx/em28xx-video.c | 43 +++++++------------------ linux/drivers/media/video/em28xx/em28xx.h | 7 ---- 3 files changed, 13 insertions(+), 41 deletions(-) (limited to 'linux/drivers/media') diff --git a/linux/drivers/media/video/em28xx/em28xx-core.c b/linux/drivers/media/video/em28xx/em28xx-core.c index 95bc18d0e..97635abb9 100644 --- a/linux/drivers/media/video/em28xx/em28xx-core.c +++ b/linux/drivers/media/video/em28xx/em28xx-core.c @@ -396,13 +396,13 @@ int em28xx_set_alternate(struct em28xx *dev) { int errCode, prev_alt = dev->alt; int i; - unsigned int min_pkt_size = dev->bytesperline + 4; + unsigned int min_pkt_size = dev->width * 2 + 4; /* When image size is bigger than a certain value, the frame size should be increased, otherwise, only green screen will be received. */ - if (dev->frame_size > 720*240*2) + if (dev->width * 2 * dev->height > 720 * 240 * 2) min_pkt_size *= 2; for (i = 0; i < dev->num_alt; i++) { diff --git a/linux/drivers/media/video/em28xx/em28xx-video.c b/linux/drivers/media/video/em28xx/em28xx-video.c index 188bd512c..924f29214 100644 --- a/linux/drivers/media/video/em28xx/em28xx-video.c +++ b/linux/drivers/media/video/em28xx/em28xx-video.c @@ -174,12 +174,7 @@ static void em28xx_copy_video(struct em28xx *dev, { void *fieldstart, *startwrite, *startread; int linesdone, currlinedone, offset, lencopy, remain; - - if (dev->frame_size != buf->vb.size) { - em28xx_errdev("size %i and buf.length %lu are different!\n", - dev->frame_size, buf->vb.size); - return; - } + int bytesperline = dev->width << 1; if (dma_q->pos + len > buf->vb.size) len = buf->vb.size - dma_q->pos; @@ -197,13 +192,13 @@ static void em28xx_copy_video(struct em28xx *dev, if (buf->top_field) fieldstart = outp; else - fieldstart = outp + dev->bytesperline; + fieldstart = outp + bytesperline; - linesdone = dma_q->pos / dev->bytesperline; - currlinedone = dma_q->pos % dev->bytesperline; - offset = linesdone * dev->bytesperline * 2 + currlinedone; + linesdone = dma_q->pos / bytesperline; + currlinedone = dma_q->pos % bytesperline; + offset = linesdone * bytesperline * 2 + currlinedone; startwrite = fieldstart + offset; - lencopy = dev->bytesperline - currlinedone; + lencopy = bytesperline - currlinedone; lencopy = lencopy > remain ? remain : lencopy; if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) { @@ -219,12 +214,12 @@ static void em28xx_copy_video(struct em28xx *dev, remain -= lencopy; while (remain > 0) { - startwrite += lencopy + dev->bytesperline; + startwrite += lencopy + bytesperline; startread += lencopy; - if (dev->bytesperline > remain) + if (bytesperline > remain) lencopy = remain; else - lencopy = dev->bytesperline; + lencopy = bytesperline; if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) { em28xx_isocdbg("Overflow of %zi bytes past buffer end (2)\n", @@ -645,8 +640,6 @@ buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb, struct em28xx_dmaqueue *vidq = &dev->vidq; int rc = 0, urb_init = 0; - /* BUG_ON(NULL == fh->fmt); */ - /* FIXME: It assumes depth = 16 */ /* The only currently supported format is 16 bits/pixel */ buf->vb.size = 16 * dev->width * dev->height >> 3; @@ -654,7 +647,6 @@ buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb, if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size) return -EINVAL; - buf->fmt = fh->fmt; buf->vb.width = dev->width; buf->vb.height = dev->height; buf->vb.field = field; @@ -907,8 +899,8 @@ static int vidioc_g_fmt_cap(struct file *file, void *priv, f->fmt.pix.width = dev->width; f->fmt.pix.height = dev->height; f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV; - f->fmt.pix.bytesperline = dev->bytesperline; - f->fmt.pix.sizeimage = dev->frame_size; + f->fmt.pix.bytesperline = dev->width * 2; + f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height; f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */ @@ -1009,9 +1001,6 @@ static int vidioc_s_fmt_cap(struct file *file, void *priv, /* set new image size */ dev->width = f->fmt.pix.width; dev->height = f->fmt.pix.height; - dev->frame_size = dev->width * dev->height * 2; - dev->field_size = dev->frame_size >> 1; - dev->bytesperline = dev->width * 2; get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale); em28xx_set_alternate(dev); @@ -1049,9 +1038,6 @@ static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm) /* set new image size */ dev->width = f.fmt.pix.width; dev->height = f.fmt.pix.height; - dev->frame_size = dev->width * dev->height * 2; - dev->field_size = dev->frame_size >> 1; - dev->bytesperline = dev->width * 2; get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale); em28xx_resolution_set(dev); @@ -1803,9 +1789,6 @@ static int em28xx_v4l2_open(struct inode *inode, struct file *filp) if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) { dev->width = norm_maxw(dev); dev->height = norm_maxh(dev); - dev->frame_size = dev->width * dev->height * 2; - dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */ - dev->bytesperline = dev->width * 2; dev->hscale = 0; dev->vscale = 0; @@ -2237,10 +2220,6 @@ static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev, dev->width = maxw; dev->height = maxh; dev->interlaced = EM28XX_INTERLACED_DEFAULT; - dev->field_size = dev->width * dev->height; - dev->frame_size = - dev->interlaced ? dev->field_size << 1 : dev->field_size; - dev->bytesperline = dev->width * 2; dev->hscale = 0; dev->vscale = 0; dev->ctl_input = 2; diff --git a/linux/drivers/media/video/em28xx/em28xx.h b/linux/drivers/media/video/em28xx/em28xx.h index 0280a6847..cf6efe070 100644 --- a/linux/drivers/media/video/em28xx/em28xx.h +++ b/linux/drivers/media/video/em28xx/em28xx.h @@ -131,8 +131,6 @@ struct em28xx_buffer { /* common v4l buffer stuff -- must be first */ struct videobuf_buffer vb; - struct em28xx_fmt *fmt; - struct list_head frame; int top_field; int receiving; @@ -305,9 +303,6 @@ struct em28xx { /* frame properties */ int width; /* current frame width */ int height; /* current frame height */ - int frame_size; /* current frame size */ - int field_size; /* current field size */ - int bytesperline; int hscale; /* horizontal scale factor (see datasheet) */ int vscale; /* vertical scale factor (see datasheet) */ int interlaced; /* 1=interlace fileds, 0=just top fileds */ @@ -367,9 +362,7 @@ struct em28xx_fh { unsigned int stream_on:1; /* Locks streams */ int radio; - unsigned int width, height; struct videobuf_queue vb_vidq; - struct em28xx_fmt *fmt; enum v4l2_buf_type type; }; -- cgit v1.2.3