📄 uvcvideo.c.svn-base
字号:
ret = nbuffers;done: up(&queue->lock); return ret;}/* * VMA operations. */static void uvc_vm_open(struct vm_area_struct *vma){ struct uvc_buffer *buffer = vma->vm_private_data; buffer->vma_use_count++;}static void uvc_vm_close(struct vm_area_struct *vma){ struct uvc_buffer *buffer = vma->vm_private_data; buffer->vma_use_count--;}static struct vm_operations_struct uvc_vm_ops = { .open = uvc_vm_open, .close = uvc_vm_close,};static int uvc_queue_waiton(struct uvc_buffer *buf, int nonblocking){ if (nonblocking) { return (buf->state != UVC_BUF_STATE_QUEUED && buf->state != UVC_BUF_STATE_ACTIVE) ? 0 : -EAGAIN; } return wait_event_interruptible(buf->wait, buf->state != UVC_BUF_STATE_QUEUED && buf->state != UVC_BUF_STATE_ACTIVE);}static void uvc_query_buffer(struct uvc_buffer *buf, struct v4l2_buffer *v4l2_buf){ memcpy(v4l2_buf, &buf->buf, sizeof *v4l2_buf); if(buf->vma_use_count) v4l2_buf->flags |= V4L2_BUF_FLAG_MAPPED; switch (buf->state) { case UVC_BUF_STATE_ERROR: case UVC_BUF_STATE_DONE: v4l2_buf->flags |= V4L2_BUF_FLAG_DONE; break; case UVC_BUF_STATE_QUEUED: case UVC_BUF_STATE_ACTIVE: v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED; break; case UVC_BUF_STATE_IDLE: default: break; }}/* * Dequeue a video buffer. If nonblocking is false, block until a buffer is * available. */static int uvc_dequeue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *v4l2_buf, int nonblocking){ struct uvc_buffer *buf; int ret = 0; if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || v4l2_buf->memory != V4L2_MEMORY_MMAP) { uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer type (%u) " "and/or memory (%u).\n", v4l2_buf->type, v4l2_buf->memory); return -EINVAL; } down(&queue->lock); if (list_empty(&queue->mainqueue)) { uvc_trace(UVC_TRACE_CAPTURE, "[E] Empty buffer queue.\n"); ret = -EINVAL; goto done; } buf = list_entry(queue->mainqueue.next, struct uvc_buffer, stream); uvc_trace(UVC_TRACE_CAPTURE, "Dequeuing buffer %u.\n", buf->buf.index); if ((ret = uvc_queue_waiton(buf, nonblocking)) < 0) goto done; switch (buf->state) { case UVC_BUF_STATE_ERROR: uvc_trace(UVC_TRACE_CAPTURE, "[W] Corrupted data " "(transmission error).\n"); ret = -EIO; case UVC_BUF_STATE_DONE: buf->state = UVC_BUF_STATE_IDLE; break; case UVC_BUF_STATE_IDLE: case UVC_BUF_STATE_QUEUED: case UVC_BUF_STATE_ACTIVE: default: uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer state %u " "(driver bug?).\n", buf->state); ret = -EINVAL; goto done; } list_del(&buf->stream); uvc_query_buffer(buf, v4l2_buf);done: up(&queue->lock); return ret;}/* * Queue a video buffer. Attempting to queue a buffer that has already been * queued will return -EINVAL. */static int uvc_queue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *v4l2_buf){ struct uvc_buffer *buf; unsigned long flags; int ret = 0; uvc_trace(UVC_TRACE_CAPTURE, "Queuing buffer %u.\n", v4l2_buf->index); if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || v4l2_buf->memory != V4L2_MEMORY_MMAP) { uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer type (%u) " "and/or memory (%u).\n", v4l2_buf->type, v4l2_buf->memory); return -EINVAL; } down(&queue->lock); if (v4l2_buf->index >= queue->count) { uvc_trace(UVC_TRACE_CAPTURE, "[E] Out of range index.\n"); ret = -EINVAL; goto done; } buf = &queue->buffer[v4l2_buf->index]; if (buf->state != UVC_BUF_STATE_IDLE) { uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer state " "(%u).\n", buf->state); ret = -EINVAL; goto done; } buf->state = UVC_BUF_STATE_QUEUED; buf->buf.bytesused = 0; list_add_tail(&buf->stream, &queue->mainqueue); spin_lock_irqsave(&queue->irqlock, flags); list_add_tail(&buf->queue, &queue->irqqueue); spin_unlock_irqrestore(&queue->irqlock, flags);done: up(&queue->lock); return ret;}static void uvc_queue_init(struct uvc_video_queue *queue){ init_MUTEX(&queue->lock); spin_lock_init(&queue->irqlock); INIT_LIST_HEAD(&queue->mainqueue); INIT_LIST_HEAD(&queue->irqqueue);}/* * Cancel the video buffers queue. * * Cancelling the queue marks all buffers on the irq queue as erroneous, * wakes them up and remove them from the queue. * * This function acquires the irq spinlock and can be called from interrupt * context. */static void uvc_queue_cancel(struct uvc_video_queue *queue){ struct uvc_buffer *buf; unsigned long flags; spin_lock_irqsave(&queue->irqlock, flags); while (!list_empty(&queue->irqqueue)) { buf = list_entry(queue->irqqueue.next, struct uvc_buffer, queue); list_del(&buf->queue); buf->state = UVC_BUF_STATE_ERROR; wake_up(&buf->wait); } spin_unlock_irqrestore(&queue->irqlock, flags);}/* * Enable or disable the video buffers queue. * * The queue must be enabled before starting video acquisition and must be * disabled after stopping it. This ensures that the video buffers queue * state can be properly initialized before buffers are accessed from the * interrupt handler. * * Enabling the video queue initializes parameters (such as sequence number, * sync pattern, ...). If the queue is already enabled, return -EBUSY. * * Disabling the video queue cancels the queue and removes all buffers from * the main queue. * * This function can't be called from interrupt context. Use * uvc_queue_cancel() instead. */static int uvc_queue_enable(struct uvc_video_queue *queue, int enable){ unsigned int i; int ret = 0; down(&queue->lock); if (enable) { if (queue->streaming) { ret = -EBUSY; goto done; } queue->last_fid = -1; queue->sequence = 0; queue->streaming = 1; } else { uvc_queue_cancel(queue); INIT_LIST_HEAD(&queue->mainqueue); for (i = 0; i < queue->count; ++i) queue->buffer[i].state = UVC_BUF_STATE_IDLE; queue->streaming = 0; }done: up(&queue->lock); return ret;}static struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue, struct uvc_buffer *buf){ struct uvc_buffer *nextbuf; unsigned long flags; spin_lock_irqsave(&queue->irqlock, flags); list_del(&buf->queue); if (!list_empty(&queue->irqqueue)) nextbuf = list_entry(queue->irqqueue.next, struct uvc_buffer, queue); else nextbuf = NULL; spin_unlock_irqrestore(&queue->irqlock, flags); buf->buf.sequence = queue->sequence++; do_gettimeofday(&buf->buf.timestamp); wake_up(&buf->wait); return nextbuf;}/* ------------------------------------------------------------------------ * Video codecs *//* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */#define UVC_STREAM_EOH (1 << 7)#define UVC_STREAM_ERR (1 << 6)#define UVC_STREAM_STI (1 << 5)#define UVC_STREAM_RES (1 << 4)#define UVC_STREAM_SCR (1 << 3)#define UVC_STREAM_PTS (1 << 2)#define UVC_STREAM_EOF (1 << 1)#define UVC_STREAM_FID (1 << 0)/* * */static int uvc_video_decode(struct uvc_video_queue *queue, struct uvc_buffer *buf, const __u8 *data, unsigned int len){ unsigned int maxlen, nbytes; void *mem; __u8 fid; /* Sanity checks: * - packet must be at least 2 bytes long * - bHeaderLength value must be at least 2 bytes (see above) * - bHeaderLength value can't be larger than the packet size. */ if (len < 2 || data[0] < 2 || data[0] > len) return -EINVAL; /* Skip payloads marked with the error bit ("error frames"). */ if (data[1] & UVC_STREAM_ERR) { uvc_trace(UVC_TRACE_FRAME, "Dropping packet (error bit set).\n"); return 0; } fid = data[1] & UVC_STREAM_FID; /* Store the packet FID bit and return immediately when the buffer is * NULL. */ if (buf == NULL) { queue->last_fid = fid; return 0; } /* Synchronize to the input stream by waiting for the FID bit to be * toggled when the the buffer state is not UVC_BUF_STATE_ACTIVE. * queue->last_fid is initialized to -1, so the first isochronous * frame will always be in sync. */ if (buf->state != UVC_BUF_STATE_ACTIVE) { if (fid == queue->last_fid) { uvc_trace(UVC_TRACE_FRAME, "Dropping packet (out of " "sync).\n"); return 0; } /* TODO: Handle PTS and SCR. */ buf->state = UVC_BUF_STATE_ACTIVE; } /* Mark the buffer as done if we're at the beginning of a new frame. * End of frame detection is better implemented by checking the EOF * bit (FID bit toggling is delayed by one frame compared to the EOF * bit), but some devices don't set the bit at end of frame (and the * last packet can be lost anyway). We thus must check if the FID has * been toggled. * * queue->last_fid is initialized to -1, so the first isochronous * frame will never trigger an end of frame detection. * * Empty buffers (bytesused == 0) don't trigger end of frame detection * as it doesn't make sense to return an empty buffer. This also * avoids detecting and of frame conditions at FID toggling if the * previous packet had the EOF bit set. */ if (fid != queue->last_fid && buf->buf.bytesused != 0) { uvc_trace(UVC_TRACE_FRAME, "Frame complete (FID bit " "toggled).\n"); buf->state = UVC_BUF_STATE_DONE; return -EAGAIN; } queue->last_fid = fid; /* Copy the video data to the buffer. */ len -= data[0]; maxlen = buf->buf.length - buf->buf.bytesused; mem = queue->mem + buf->buf.m.offset + buf->buf.bytesused; nbytes = min(len, maxlen); memcpy(mem, data + data[0], nbytes); buf->buf.bytesused += nbytes; /* Drop the current frame if the buffer size was exceeded. */ if (len > maxlen) { uvc_trace(UVC_TRACE_FRAME, "Frame complete (overflow).\n"); buf->state = UVC_BUF_STATE_DONE; } /* Mark the buffer as done if the EOF marker is set. */ if (data[1] & UVC_STREAM_EOF && buf->buf.bytesused != 0) { uvc_trace(UVC_TRACE_FRAME, "Frame complete (EOF found).\n"); if (data[0] == len) printk("EOF in empty packet.\n"); buf->state = UVC_BUF_STATE_DONE; } return 0;}/* ------------------------------------------------------------------------ * URB handling *//* * Completion handler for status URB. */static void uvc_status_complete(struct urb *urb, struct pt_regs *regs){ struct uvc_device *dev = urb->context; int len, ret; switch (urb->status) { case 0: break; case -ENOENT: /* usb_kill_urb() called. */ case -ECONNRESET: /* usb_unlink_urb() called. */ case -ESHUTDOWN: /* The endpoint is being disabled. */ case -EPROTO: /* Device is disconnected (reported by some * host controller). */ return; default: uvc_printk(KERN_WARNING, "Non-zero status (%d) in status " "completion handler.\n", urb->status); return; } len = urb->actual_length; if (len >= 4 && (dev->status[0] & 0x0f) == 2) { if (dev->status[2] == 0) uvc_printk(KERN_DEBUG, "Button event (%d).\n", dev->status[3]); } /* Resubmit the URB. */ urb->interval = dev->int_ep->desc.bInterval; if ((ret = usb_submit_urb(urb, GFP_ATOMIC)) < 0) { uvc_printk(KERN_ERR, "Failed to resubmit status URB (%d).\n", ret); }}/* * Completion handler for isochronous URBs. */static void uvc_video_complete(struct urb *urb, struct pt_regs *regs){ struct uvc_video_device *video = urb->context; struct uvc_video_queue *queue = &video->queue; struct uvc_buffer *buf = NULL; unsigned long flags; int ret, i; switch (urb->status) { case 0: break; default: uvc_printk(KERN_WARNING, "Non-zero status (%d) in isoc " "completion handler.\n", urb->status); case -ENOENT: /* usb_kill_urb() called. */ case -ECONNRESET: /* usb_unlink_urb() called. */ case -ESHUTDOWN: /* The endpoint is being disabled. */ uvc_queue_cancel(queue); return; } spin_lock_irqsave(&queue->irqlock, flags); if (!list_empty(&queue->irqqueue)) buf = list_entry(queue->irqqueue.next, struct uvc_buffer, queue); spin_unlock_irqrestore(&queue->irqlock, flags); for (i = 0; i < urb->number_of_packets; ++i) { if (urb->iso_frame_desc[i].status < 0) { uvc_trace(UVC_TRACE_FRAME, "USB isochronous frame " "lost (%d).\n", urb->iso_frame_desc[i].status); continue; } /* Decode the payload packet. * uvc_video_decode is entered twice when a frame transition * has been detected because the end of frame can only be * reliably detected when the first packet of the new frame * is processed. The first pass detects the transition and * closes the previous frame's buffer, the second pass * processes the data of the first payload of the new frame. */ do { ret = uvc_video_decode(queue, buf, urb->transfer_buffer + urb->iso_frame_desc[i].offset, urb->iso_frame_desc[i].actual_length); if (buf == NULL) break; if (buf->state == UVC_BUF_STATE_DONE || buf->state == UVC_BUF_STATE_ERROR) buf = uvc_queue_next_buffer(queue, buf); } while (ret == -EAGAIN); } if ((ret = usb_submit_urb(urb, GFP_ATOMIC)) < 0) { uvc_printk(KERN_ERR, "Failed to resubmit isoc URB (%d).\n", ret); }}/* * Uninitialize isochronous URBs and free transfer buffers. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -