2 * uvc_v4l2.c -- USB Video Class Gadget driver
4 * Copyright (C) 2009-2010
5 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
14 #include <linux/kernel.h>
15 #include <linux/device.h>
16 #include <linux/errno.h>
17 #include <linux/list.h>
18 #include <linux/mutex.h>
19 #include <linux/version.h>
20 #include <linux/videodev2.h>
21 #include <linux/vmalloc.h>
22 #include <linux/wait.h>
24 #include <media/v4l2-dev.h>
25 #include <media/v4l2-event.h>
26 #include <media/v4l2-ioctl.h>
29 #include "uvc_queue.h"
31 /* --------------------------------------------------------------------------
36 uvc_send_response(struct uvc_device
*uvc
, struct uvc_request_data
*data
)
38 struct usb_composite_dev
*cdev
= uvc
->func
.config
->cdev
;
39 struct usb_request
*req
= uvc
->control_req
;
42 return usb_ep_set_halt(cdev
->gadget
->ep0
);
44 req
->length
= min(uvc
->event_length
, data
->length
);
45 req
->zero
= data
->length
< uvc
->event_length
;
46 req
->dma
= DMA_ADDR_INVALID
;
48 memcpy(req
->buf
, data
->data
, data
->length
);
50 return usb_ep_queue(cdev
->gadget
->ep0
, req
, GFP_KERNEL
);
53 /* --------------------------------------------------------------------------
63 static struct uvc_format uvc_formats
[] = {
64 { 16, V4L2_PIX_FMT_YUYV
},
65 { 0, V4L2_PIX_FMT_MJPEG
},
69 uvc_v4l2_get_format(struct uvc_video
*video
, struct v4l2_format
*fmt
)
71 fmt
->fmt
.pix
.pixelformat
= video
->fcc
;
72 fmt
->fmt
.pix
.width
= video
->width
;
73 fmt
->fmt
.pix
.height
= video
->height
;
74 fmt
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
75 fmt
->fmt
.pix
.bytesperline
= video
->bpp
* video
->width
/ 8;
76 fmt
->fmt
.pix
.sizeimage
= video
->imagesize
;
77 fmt
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SRGB
;
78 fmt
->fmt
.pix
.priv
= 0;
84 uvc_v4l2_set_format(struct uvc_video
*video
, struct v4l2_format
*fmt
)
86 struct uvc_format
*format
;
87 unsigned int imagesize
;
91 for (i
= 0; i
< ARRAY_SIZE(uvc_formats
); ++i
) {
92 format
= &uvc_formats
[i
];
93 if (format
->fcc
== fmt
->fmt
.pix
.pixelformat
)
97 if (i
== ARRAY_SIZE(uvc_formats
)) {
98 printk(KERN_INFO
"Unsupported format 0x%08x.\n",
99 fmt
->fmt
.pix
.pixelformat
);
103 bpl
= format
->bpp
* fmt
->fmt
.pix
.width
/ 8;
104 imagesize
= bpl
? bpl
* fmt
->fmt
.pix
.height
: fmt
->fmt
.pix
.sizeimage
;
106 video
->fcc
= format
->fcc
;
107 video
->bpp
= format
->bpp
;
108 video
->width
= fmt
->fmt
.pix
.width
;
109 video
->height
= fmt
->fmt
.pix
.height
;
110 video
->imagesize
= imagesize
;
112 fmt
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
113 fmt
->fmt
.pix
.bytesperline
= bpl
;
114 fmt
->fmt
.pix
.sizeimage
= imagesize
;
115 fmt
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SRGB
;
116 fmt
->fmt
.pix
.priv
= 0;
122 uvc_v4l2_open(struct file
*file
)
124 struct video_device
*vdev
= video_devdata(file
);
125 struct uvc_device
*uvc
= video_get_drvdata(vdev
);
126 struct uvc_file_handle
*handle
;
128 handle
= kzalloc(sizeof(*handle
), GFP_KERNEL
);
132 v4l2_fh_init(&handle
->vfh
, vdev
);
133 v4l2_fh_add(&handle
->vfh
);
135 handle
->device
= &uvc
->video
;
136 file
->private_data
= &handle
->vfh
;
138 uvc_function_connect(uvc
);
143 uvc_v4l2_release(struct file
*file
)
145 struct video_device
*vdev
= video_devdata(file
);
146 struct uvc_device
*uvc
= video_get_drvdata(vdev
);
147 struct uvc_file_handle
*handle
= to_uvc_file_handle(file
->private_data
);
148 struct uvc_video
*video
= handle
->device
;
150 uvc_function_disconnect(uvc
);
152 uvc_video_enable(video
, 0);
153 mutex_lock(&video
->queue
.mutex
);
154 if (uvc_free_buffers(&video
->queue
) < 0)
155 printk(KERN_ERR
"uvc_v4l2_release: Unable to free "
157 mutex_unlock(&video
->queue
.mutex
);
159 file
->private_data
= NULL
;
160 v4l2_fh_del(&handle
->vfh
);
161 v4l2_fh_exit(&handle
->vfh
);
167 uvc_v4l2_do_ioctl(struct file
*file
, unsigned int cmd
, void *arg
)
169 struct video_device
*vdev
= video_devdata(file
);
170 struct uvc_device
*uvc
= video_get_drvdata(vdev
);
171 struct uvc_file_handle
*handle
= to_uvc_file_handle(file
->private_data
);
172 struct usb_composite_dev
*cdev
= uvc
->func
.config
->cdev
;
173 struct uvc_video
*video
= &uvc
->video
;
177 /* Query capabilities */
178 case VIDIOC_QUERYCAP
:
180 struct v4l2_capability
*cap
= arg
;
182 memset(cap
, 0, sizeof *cap
);
183 strncpy(cap
->driver
, "g_uvc", sizeof(cap
->driver
));
184 strncpy(cap
->card
, cdev
->gadget
->name
, sizeof(cap
->card
));
185 strncpy(cap
->bus_info
, dev_name(&cdev
->gadget
->dev
),
186 sizeof cap
->bus_info
);
187 cap
->version
= DRIVER_VERSION_NUMBER
;
188 cap
->capabilities
= V4L2_CAP_VIDEO_OUTPUT
| V4L2_CAP_STREAMING
;
192 /* Get & Set format */
195 struct v4l2_format
*fmt
= arg
;
197 if (fmt
->type
!= video
->queue
.type
)
200 return uvc_v4l2_get_format(video
, fmt
);
205 struct v4l2_format
*fmt
= arg
;
207 if (fmt
->type
!= video
->queue
.type
)
210 return uvc_v4l2_set_format(video
, fmt
);
213 /* Buffers & streaming */
216 struct v4l2_requestbuffers
*rb
= arg
;
218 if (rb
->type
!= video
->queue
.type
||
219 rb
->memory
!= V4L2_MEMORY_MMAP
)
222 ret
= uvc_alloc_buffers(&video
->queue
, rb
->count
,
232 case VIDIOC_QUERYBUF
:
234 struct v4l2_buffer
*buf
= arg
;
236 if (buf
->type
!= video
->queue
.type
)
239 return uvc_query_buffer(&video
->queue
, buf
);
243 if ((ret
= uvc_queue_buffer(&video
->queue
, arg
)) < 0)
246 return uvc_video_pump(video
);
249 return uvc_dequeue_buffer(&video
->queue
, arg
,
250 file
->f_flags
& O_NONBLOCK
);
252 case VIDIOC_STREAMON
:
256 if (*type
!= video
->queue
.type
)
259 return uvc_video_enable(video
, 1);
262 case VIDIOC_STREAMOFF
:
266 if (*type
!= video
->queue
.type
)
269 return uvc_video_enable(video
, 0);
275 struct v4l2_event
*event
= arg
;
277 ret
= v4l2_event_dequeue(&handle
->vfh
, event
,
278 file
->f_flags
& O_NONBLOCK
);
279 if (ret
== 0 && event
->type
== UVC_EVENT_SETUP
) {
280 struct uvc_event
*uvc_event
= (void *)&event
->u
.data
;
282 /* Tell the complete callback to generate an event for
283 * the next request that will be enqueued by
286 uvc
->event_setup_out
=
287 !(uvc_event
->req
.bRequestType
& USB_DIR_IN
);
288 uvc
->event_length
= uvc_event
->req
.wLength
;
294 case VIDIOC_SUBSCRIBE_EVENT
:
296 struct v4l2_event_subscription
*sub
= arg
;
298 if (sub
->type
< UVC_EVENT_FIRST
|| sub
->type
> UVC_EVENT_LAST
)
301 return v4l2_event_subscribe(&handle
->vfh
, arg
, 2);
304 case VIDIOC_UNSUBSCRIBE_EVENT
:
305 return v4l2_event_unsubscribe(&handle
->vfh
, arg
);
307 case UVCIOC_SEND_RESPONSE
:
308 ret
= uvc_send_response(uvc
, arg
);
319 uvc_v4l2_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
321 return video_usercopy(file
, cmd
, arg
, uvc_v4l2_do_ioctl
);
325 uvc_v4l2_mmap(struct file
*file
, struct vm_area_struct
*vma
)
327 struct video_device
*vdev
= video_devdata(file
);
328 struct uvc_device
*uvc
= video_get_drvdata(vdev
);
330 return uvc_queue_mmap(&uvc
->video
.queue
, vma
);
334 uvc_v4l2_poll(struct file
*file
, poll_table
*wait
)
336 struct video_device
*vdev
= video_devdata(file
);
337 struct uvc_device
*uvc
= video_get_drvdata(vdev
);
338 struct uvc_file_handle
*handle
= to_uvc_file_handle(file
->private_data
);
339 unsigned int mask
= 0;
341 poll_wait(file
, &handle
->vfh
.wait
, wait
);
342 if (v4l2_event_pending(&handle
->vfh
))
345 mask
|= uvc_queue_poll(&uvc
->video
.queue
, file
, wait
);
350 static struct v4l2_file_operations uvc_v4l2_fops
= {
351 .owner
= THIS_MODULE
,
352 .open
= uvc_v4l2_open
,
353 .release
= uvc_v4l2_release
,
354 .ioctl
= uvc_v4l2_ioctl
,
355 .mmap
= uvc_v4l2_mmap
,
356 .poll
= uvc_v4l2_poll
,