2008-06-30 15:04:50 -03:00
/*
* uvc_queue . c - - USB Video Class driver - Buffers management
*
2010-09-20 06:10:10 -03:00
* Copyright ( C ) 2005 - 2010
* Laurent Pinchart ( laurent . pinchart @ ideasonboard . com )
2008-06-30 15:04:50 -03:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 2 of the License , or
* ( at your option ) any later version .
*
*/
2011-10-24 11:53:59 -03:00
# include <linux/atomic.h>
2008-06-30 15:04:50 -03:00
# include <linux/kernel.h>
2008-07-23 21:28:13 -07:00
# include <linux/mm.h>
2008-06-30 15:04:50 -03:00
# include <linux/list.h>
# include <linux/module.h>
# include <linux/usb.h>
# include <linux/videodev2.h>
# include <linux/vmalloc.h>
# include <linux/wait.h>
2011-10-24 11:53:59 -03:00
# include <media/videobuf2-vmalloc.h>
2008-06-30 15:04:50 -03:00
# include "uvcvideo.h"
/* ------------------------------------------------------------------------
* Video buffers queue management .
*
* Video queues is initialized by uvc_queue_init ( ) . The function performs
* basic initialization of the uvc_video_queue struct and never fails .
*
2011-10-24 11:53:59 -03:00
* Video buffers are managed by videobuf2 . The driver uses a mutex to protect
* the videobuf2 queue operations by serializing calls to videobuf2 and a
* spinlock to protect the IRQ queue that holds the buffers to be processed by
* the driver .
2008-06-30 15:04:50 -03:00
*/
2011-10-24 11:53:59 -03:00
/* -----------------------------------------------------------------------------
* videobuf2 queue operations
2010-11-21 14:46:44 -03:00
*/
2011-10-24 11:53:59 -03:00
static int uvc_queue_setup ( struct vb2_queue * vq , const struct v4l2_format * fmt ,
unsigned int * nbuffers , unsigned int * nplanes ,
unsigned int sizes [ ] , void * alloc_ctxs [ ] )
2010-11-21 14:46:44 -03:00
{
2011-10-24 11:53:59 -03:00
struct uvc_video_queue * queue = vb2_get_drv_priv ( vq ) ;
struct uvc_streaming * stream =
container_of ( queue , struct uvc_streaming , queue ) ;
2010-11-21 14:46:44 -03:00
2014-02-18 10:40:47 -03:00
/* Make sure the image size is large enough. */
if ( fmt & & fmt - > fmt . pix . sizeimage < stream - > ctrl . dwMaxVideoFrameSize )
return - EINVAL ;
2011-10-24 11:53:59 -03:00
* nplanes = 1 ;
2014-02-18 10:40:47 -03:00
sizes [ 0 ] = fmt ? fmt - > fmt . pix . sizeimage
: stream - > ctrl . dwMaxVideoFrameSize ;
2010-11-21 14:46:44 -03:00
return 0 ;
}
2011-10-24 11:53:59 -03:00
static int uvc_buffer_prepare ( struct vb2_buffer * vb )
2010-11-21 14:46:44 -03:00
{
2011-10-24 11:53:59 -03:00
struct uvc_video_queue * queue = vb2_get_drv_priv ( vb - > vb2_queue ) ;
struct uvc_buffer * buf = container_of ( vb , struct uvc_buffer , buf ) ;
2010-11-21 14:46:44 -03:00
2011-10-24 11:53:59 -03:00
if ( vb - > v4l2_buf . type = = V4L2_BUF_TYPE_VIDEO_OUTPUT & &
vb2_get_plane_payload ( vb , 0 ) > vb2_plane_size ( vb , 0 ) ) {
uvc_trace ( UVC_TRACE_CAPTURE , " [E] Bytes used out of bounds. \n " ) ;
return - EINVAL ;
}
2008-06-30 15:04:50 -03:00
2011-10-24 11:53:59 -03:00
if ( unlikely ( queue - > flags & UVC_QUEUE_DISCONNECTED ) )
return - ENODEV ;
2008-06-30 15:04:50 -03:00
2011-10-24 11:53:59 -03:00
buf - > state = UVC_BUF_STATE_QUEUED ;
buf - > error = 0 ;
buf - > mem = vb2_plane_vaddr ( vb , 0 ) ;
buf - > length = vb2_plane_size ( vb , 0 ) ;
if ( vb - > v4l2_buf . type = = V4L2_BUF_TYPE_VIDEO_CAPTURE )
buf - > bytesused = 0 ;
else
buf - > bytesused = vb2_get_plane_payload ( vb , 0 ) ;
2008-06-30 15:04:50 -03:00
2011-10-24 11:53:59 -03:00
return 0 ;
}
2008-06-30 15:04:50 -03:00
2011-10-24 11:53:59 -03:00
static void uvc_buffer_queue ( struct vb2_buffer * vb )
{
struct uvc_video_queue * queue = vb2_get_drv_priv ( vb - > vb2_queue ) ;
struct uvc_buffer * buf = container_of ( vb , struct uvc_buffer , buf ) ;
unsigned long flags ;
2008-06-30 15:04:50 -03:00
2011-10-24 11:53:59 -03:00
spin_lock_irqsave ( & queue - > irqlock , flags ) ;
if ( likely ( ! ( queue - > flags & UVC_QUEUE_DISCONNECTED ) ) ) {
list_add_tail ( & buf - > queue , & queue - > irqqueue ) ;
} else {
/* If the device is disconnected return the buffer to userspace
* directly . The next QBUF call will fail with - ENODEV .
*/
buf - > state = UVC_BUF_STATE_ERROR ;
vb2_buffer_done ( & buf - > buf , VB2_BUF_STATE_ERROR ) ;
2008-06-30 15:04:50 -03:00
}
2011-10-24 11:53:59 -03:00
spin_unlock_irqrestore ( & queue - > irqlock , flags ) ;
}
2008-06-30 15:04:50 -03:00
2014-03-04 07:27:13 -03:00
static void uvc_buffer_finish ( struct vb2_buffer * vb )
2011-09-24 10:46:55 -03:00
{
struct uvc_video_queue * queue = vb2_get_drv_priv ( vb - > vb2_queue ) ;
struct uvc_streaming * stream =
container_of ( queue , struct uvc_streaming , queue ) ;
struct uvc_buffer * buf = container_of ( vb , struct uvc_buffer , buf ) ;
2014-03-04 07:34:49 -03:00
if ( vb - > state = = VB2_BUF_STATE_DONE )
uvc_video_clock_update ( stream , & vb - > v4l2_buf , buf ) ;
2011-09-24 10:46:55 -03:00
}
2013-01-21 06:51:41 -03:00
static void uvc_wait_prepare ( struct vb2_queue * vq )
{
struct uvc_video_queue * queue = vb2_get_drv_priv ( vq ) ;
mutex_unlock ( & queue - > mutex ) ;
}
static void uvc_wait_finish ( struct vb2_queue * vq )
{
struct uvc_video_queue * queue = vb2_get_drv_priv ( vq ) ;
mutex_lock ( & queue - > mutex ) ;
}
2011-10-24 11:53:59 -03:00
static struct vb2_ops uvc_queue_qops = {
. queue_setup = uvc_queue_setup ,
. buf_prepare = uvc_buffer_prepare ,
. buf_queue = uvc_buffer_queue ,
2011-09-24 10:46:55 -03:00
. buf_finish = uvc_buffer_finish ,
2013-01-21 06:51:41 -03:00
. wait_prepare = uvc_wait_prepare ,
. wait_finish = uvc_wait_finish ,
2011-10-24 11:53:59 -03:00
} ;
2008-06-30 15:04:50 -03:00
2012-09-26 07:30:34 -03:00
int uvc_queue_init ( struct uvc_video_queue * queue , enum v4l2_buf_type type ,
2011-10-24 11:53:59 -03:00
int drop_corrupted )
{
2012-09-26 07:30:34 -03:00
int ret ;
2011-10-24 11:53:59 -03:00
queue - > queue . type = type ;
2012-10-19 08:54:43 -03:00
queue - > queue . io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF ;
2011-10-24 11:53:59 -03:00
queue - > queue . drv_priv = queue ;
queue - > queue . buf_struct_size = sizeof ( struct uvc_buffer ) ;
queue - > queue . ops = & uvc_queue_qops ;
queue - > queue . mem_ops = & vb2_vmalloc_memops ;
2014-02-10 19:26:44 -03:00
queue - > queue . timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
| V4L2_BUF_FLAG_TSTAMP_SRC_SOE ;
2012-09-26 07:30:34 -03:00
ret = vb2_queue_init ( & queue - > queue ) ;
if ( ret )
return ret ;
2008-06-30 15:04:50 -03:00
2011-10-24 11:53:59 -03:00
mutex_init ( & queue - > mutex ) ;
spin_lock_init ( & queue - > irqlock ) ;
INIT_LIST_HEAD ( & queue - > irqqueue ) ;
queue - > flags = drop_corrupted ? UVC_QUEUE_DROP_CORRUPTED : 0 ;
2012-09-26 07:30:34 -03:00
return 0 ;
2008-06-30 15:04:50 -03:00
}
2011-10-24 11:53:59 -03:00
/* -----------------------------------------------------------------------------
* V4L2 queue operations
2009-06-04 09:26:39 -03:00
*/
2011-10-24 11:53:59 -03:00
int uvc_alloc_buffers ( struct uvc_video_queue * queue ,
struct v4l2_requestbuffers * rb )
2009-06-04 09:26:39 -03:00
{
2011-10-24 11:53:59 -03:00
int ret ;
2009-06-04 09:26:39 -03:00
mutex_lock ( & queue - > mutex ) ;
2011-10-24 11:53:59 -03:00
ret = vb2_reqbufs ( & queue - > queue , rb ) ;
2009-06-04 09:26:39 -03:00
mutex_unlock ( & queue - > mutex ) ;
2011-10-24 11:53:59 -03:00
return ret ? ret : rb - > count ;
2009-06-04 09:26:39 -03:00
}
2011-10-24 11:53:59 -03:00
void uvc_free_buffers ( struct uvc_video_queue * queue )
2008-06-30 15:04:50 -03:00
{
2011-10-24 11:53:59 -03:00
mutex_lock ( & queue - > mutex ) ;
vb2_queue_release ( & queue - > queue ) ;
mutex_unlock ( & queue - > mutex ) ;
2008-06-30 15:04:50 -03:00
}
2011-10-24 11:53:59 -03:00
int uvc_query_buffer ( struct uvc_video_queue * queue , struct v4l2_buffer * buf )
2008-06-30 15:04:50 -03:00
{
2011-10-24 11:53:59 -03:00
int ret ;
2008-06-30 15:04:50 -03:00
mutex_lock ( & queue - > mutex ) ;
2011-10-24 11:53:59 -03:00
ret = vb2_querybuf ( & queue - > queue , buf ) ;
2009-01-03 19:12:40 -03:00
mutex_unlock ( & queue - > mutex ) ;
2011-10-24 11:53:59 -03:00
2009-01-03 19:12:40 -03:00
return ret ;
2008-06-30 15:04:50 -03:00
}
2014-01-29 13:13:52 -03:00
int uvc_create_buffers ( struct uvc_video_queue * queue ,
struct v4l2_create_buffers * cb )
{
int ret ;
mutex_lock ( & queue - > mutex ) ;
ret = vb2_create_bufs ( & queue - > queue , cb ) ;
mutex_unlock ( & queue - > mutex ) ;
return ret ;
}
2011-10-24 11:53:59 -03:00
int uvc_queue_buffer ( struct uvc_video_queue * queue , struct v4l2_buffer * buf )
2008-06-30 15:04:50 -03:00
{
2011-10-24 11:53:59 -03:00
int ret ;
2008-06-30 15:04:50 -03:00
mutex_lock ( & queue - > mutex ) ;
2011-10-24 11:53:59 -03:00
ret = vb2_qbuf ( & queue - > queue , buf ) ;
2008-06-30 15:04:50 -03:00
mutex_unlock ( & queue - > mutex ) ;
2011-10-24 11:53:59 -03:00
return ret ;
2008-06-30 15:04:50 -03:00
}
2011-10-24 11:53:59 -03:00
int uvc_dequeue_buffer ( struct uvc_video_queue * queue , struct v4l2_buffer * buf ,
int nonblocking )
2008-06-30 15:04:50 -03:00
{
2011-10-24 11:53:59 -03:00
int ret ;
2008-06-30 15:04:50 -03:00
mutex_lock ( & queue - > mutex ) ;
2011-10-24 11:53:59 -03:00
ret = vb2_dqbuf ( & queue - > queue , buf , nonblocking ) ;
2008-06-30 15:04:50 -03:00
mutex_unlock ( & queue - > mutex ) ;
2011-10-24 11:53:59 -03:00
return ret ;
2010-11-21 15:18:08 -03:00
}
int uvc_queue_mmap ( struct uvc_video_queue * queue , struct vm_area_struct * vma )
{
2011-10-24 11:53:59 -03:00
int ret ;
2010-11-21 15:18:08 -03:00
mutex_lock ( & queue - > mutex ) ;
2011-10-24 11:53:59 -03:00
ret = vb2_mmap ( & queue - > queue , vma ) ;
mutex_unlock ( & queue - > mutex ) ;
2010-11-21 15:18:08 -03:00
2011-10-24 11:53:59 -03:00
return ret ;
}
2010-11-21 15:18:08 -03:00
2012-04-30 08:19:10 -03:00
# ifndef CONFIG_MMU
unsigned long uvc_queue_get_unmapped_area ( struct uvc_video_queue * queue ,
unsigned long pgoff )
{
unsigned long ret ;
mutex_lock ( & queue - > mutex ) ;
ret = vb2_get_unmapped_area ( & queue - > queue , 0 , 0 , pgoff , 0 ) ;
mutex_unlock ( & queue - > mutex ) ;
return ret ;
}
# endif
2011-10-24 11:53:59 -03:00
unsigned int uvc_queue_poll ( struct uvc_video_queue * queue , struct file * file ,
poll_table * wait )
{
unsigned int ret ;
2010-11-21 15:18:08 -03:00
2011-10-24 11:53:59 -03:00
mutex_lock ( & queue - > mutex ) ;
ret = vb2_poll ( & queue - > queue , file , wait ) ;
2010-11-21 15:18:08 -03:00
mutex_unlock ( & queue - > mutex ) ;
2011-10-24 11:53:59 -03:00
2010-11-21 15:18:08 -03:00
return ret ;
}
2011-10-24 11:53:59 -03:00
/* -----------------------------------------------------------------------------
2008-06-30 15:04:50 -03:00
*
*/
2011-10-24 11:53:59 -03:00
/*
* Check if buffers have been allocated .
*/
int uvc_queue_allocated ( struct uvc_video_queue * queue )
2008-06-30 15:04:50 -03:00
{
2011-10-24 11:53:59 -03:00
int allocated ;
2008-06-30 15:04:50 -03:00
mutex_lock ( & queue - > mutex ) ;
2011-10-24 11:53:59 -03:00
allocated = vb2_is_busy ( & queue - > queue ) ;
2008-06-30 15:04:50 -03:00
mutex_unlock ( & queue - > mutex ) ;
2011-10-24 11:53:59 -03:00
return allocated ;
2008-06-30 15:04:50 -03:00
}
/*
* 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 .
*
2010-10-02 11:06:05 -03:00
* Enabling the video queue returns - EBUSY if the queue is already enabled .
2008-06-30 15:04:50 -03:00
*
* 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 .
*/
int uvc_queue_enable ( struct uvc_video_queue * queue , int enable )
{
2011-10-24 11:53:59 -03:00
unsigned long flags ;
int ret ;
2008-06-30 15:04:50 -03:00
mutex_lock ( & queue - > mutex ) ;
if ( enable ) {
2011-10-24 11:53:59 -03:00
ret = vb2_streamon ( & queue - > queue , queue - > queue . type ) ;
if ( ret < 0 )
2008-06-30 15:04:50 -03:00
goto done ;
2011-10-24 11:53:59 -03:00
2008-12-28 22:32:29 -03:00
queue - > buf_used = 0 ;
2008-06-30 15:04:50 -03:00
} else {
2011-10-24 11:53:59 -03:00
ret = vb2_streamoff ( & queue - > queue , queue - > queue . type ) ;
if ( ret < 0 )
goto done ;
2008-06-30 15:04:50 -03:00
2011-10-24 11:53:59 -03:00
spin_lock_irqsave ( & queue - > irqlock , flags ) ;
INIT_LIST_HEAD ( & queue - > irqqueue ) ;
spin_unlock_irqrestore ( & queue - > irqlock , flags ) ;
2008-06-30 15:04:50 -03:00
}
done :
mutex_unlock ( & queue - > mutex ) ;
return ret ;
}
/*
* Cancel the video buffers queue .
*
* Cancelling the queue marks all buffers on the irq queue as erroneous ,
2009-01-03 19:12:40 -03:00
* wakes them up and removes them from the queue .
2008-06-30 15:04:50 -03:00
*
* If the disconnect parameter is set , further calls to uvc_queue_buffer will
* fail with - ENODEV .
*
* This function acquires the irq spinlock and can be called from interrupt
* context .
*/
void uvc_queue_cancel ( struct uvc_video_queue * queue , int disconnect )
{
struct uvc_buffer * buf ;
unsigned long flags ;
spin_lock_irqsave ( & queue - > irqlock , flags ) ;
while ( ! list_empty ( & queue - > irqqueue ) ) {
buf = list_first_entry ( & queue - > irqqueue , struct uvc_buffer ,
queue ) ;
list_del ( & buf - > queue ) ;
buf - > state = UVC_BUF_STATE_ERROR ;
2011-10-24 11:53:59 -03:00
vb2_buffer_done ( & buf - > buf , VB2_BUF_STATE_ERROR ) ;
2008-06-30 15:04:50 -03:00
}
/* This must be protected by the irqlock spinlock to avoid race
2011-10-24 11:53:59 -03:00
* conditions between uvc_buffer_queue and the disconnection event that
2008-06-30 15:04:50 -03:00
* could result in an interruptible wait in uvc_dequeue_buffer . Do not
2011-10-24 11:53:59 -03:00
* blindly replace this logic by checking for the UVC_QUEUE_DISCONNECTED
2008-06-30 15:04:50 -03:00
* state outside the queue code .
*/
if ( disconnect )
queue - > flags | = UVC_QUEUE_DISCONNECTED ;
spin_unlock_irqrestore ( & queue - > irqlock , flags ) ;
}
struct uvc_buffer * uvc_queue_next_buffer ( struct uvc_video_queue * queue ,
struct uvc_buffer * buf )
{
struct uvc_buffer * nextbuf ;
unsigned long flags ;
2010-06-17 06:52:37 -03:00
if ( ( queue - > flags & UVC_QUEUE_DROP_CORRUPTED ) & & buf - > error ) {
buf - > error = 0 ;
2008-06-30 15:04:50 -03:00
buf - > state = UVC_BUF_STATE_QUEUED ;
2012-07-15 10:54:03 -03:00
buf - > bytesused = 0 ;
2011-10-24 11:53:59 -03:00
vb2_set_plane_payload ( & buf - > buf , 0 , 0 ) ;
2008-06-30 15:04:50 -03:00
return buf ;
}
spin_lock_irqsave ( & queue - > irqlock , flags ) ;
list_del ( & buf - > queue ) ;
if ( ! list_empty ( & queue - > irqqueue ) )
nextbuf = list_first_entry ( & queue - > irqqueue , struct uvc_buffer ,
queue ) ;
else
nextbuf = NULL ;
spin_unlock_irqrestore ( & queue - > irqlock , flags ) ;
2011-10-24 11:53:59 -03:00
buf - > state = buf - > error ? VB2_BUF_STATE_ERROR : UVC_BUF_STATE_DONE ;
vb2_set_plane_payload ( & buf - > buf , 0 , buf - > bytesused ) ;
vb2_buffer_done ( & buf - > buf , VB2_BUF_STATE_DONE ) ;
2008-06-30 15:04:50 -03:00
return nextbuf ;
}