blob: bb140dd931645b8f75b56b3602865a4404e7b2ea [file] [log] [blame]
Laurent Pinchartcdda4792010-05-02 20:57:41 +02001/*
2 * uvc_v4l2.c -- USB Video Class Gadget driver
3 *
4 * Copyright (C) 2009-2010
5 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
6 *
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.
Laurent Pinchartcdda4792010-05-02 20:57:41 +020011 */
12
13#include <linux/kernel.h>
14#include <linux/device.h>
15#include <linux/errno.h>
16#include <linux/list.h>
17#include <linux/mutex.h>
Laurent Pinchartcdda4792010-05-02 20:57:41 +020018#include <linux/videodev2.h>
19#include <linux/vmalloc.h>
20#include <linux/wait.h>
21
22#include <media/v4l2-dev.h>
23#include <media/v4l2-event.h>
24#include <media/v4l2-ioctl.h>
25
26#include "uvc.h"
27#include "uvc_queue.h"
28
29/* --------------------------------------------------------------------------
30 * Requests handling
31 */
32
33static int
34uvc_send_response(struct uvc_device *uvc, struct uvc_request_data *data)
35{
36 struct usb_composite_dev *cdev = uvc->func.config->cdev;
37 struct usb_request *req = uvc->control_req;
38
39 if (data->length < 0)
40 return usb_ep_set_halt(cdev->gadget->ep0);
41
Laurent Pinchart6f6543f2012-04-24 11:29:42 +020042 req->length = min_t(unsigned int, uvc->event_length, data->length);
Laurent Pinchartcdda4792010-05-02 20:57:41 +020043 req->zero = data->length < uvc->event_length;
Laurent Pinchartcdda4792010-05-02 20:57:41 +020044
Dan Carpentera5eaaa12013-03-14 11:01:05 +030045 memcpy(req->buf, data->data, req->length);
Laurent Pinchartcdda4792010-05-02 20:57:41 +020046
47 return usb_ep_queue(cdev->gadget->ep0, req, GFP_KERNEL);
48}
49
50/* --------------------------------------------------------------------------
51 * V4L2
52 */
53
54struct uvc_format
55{
56 u8 bpp;
57 u32 fcc;
58};
59
60static struct uvc_format uvc_formats[] = {
61 { 16, V4L2_PIX_FMT_YUYV },
62 { 0, V4L2_PIX_FMT_MJPEG },
63};
64
65static int
66uvc_v4l2_get_format(struct uvc_video *video, struct v4l2_format *fmt)
67{
68 fmt->fmt.pix.pixelformat = video->fcc;
69 fmt->fmt.pix.width = video->width;
70 fmt->fmt.pix.height = video->height;
71 fmt->fmt.pix.field = V4L2_FIELD_NONE;
72 fmt->fmt.pix.bytesperline = video->bpp * video->width / 8;
73 fmt->fmt.pix.sizeimage = video->imagesize;
74 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
75 fmt->fmt.pix.priv = 0;
76
77 return 0;
78}
79
80static int
81uvc_v4l2_set_format(struct uvc_video *video, struct v4l2_format *fmt)
82{
83 struct uvc_format *format;
84 unsigned int imagesize;
85 unsigned int bpl;
86 unsigned int i;
87
88 for (i = 0; i < ARRAY_SIZE(uvc_formats); ++i) {
89 format = &uvc_formats[i];
90 if (format->fcc == fmt->fmt.pix.pixelformat)
91 break;
92 }
93
Dan Carpenter9a887162010-08-12 09:59:58 +020094 if (i == ARRAY_SIZE(uvc_formats)) {
Laurent Pinchartcdda4792010-05-02 20:57:41 +020095 printk(KERN_INFO "Unsupported format 0x%08x.\n",
96 fmt->fmt.pix.pixelformat);
97 return -EINVAL;
98 }
99
100 bpl = format->bpp * fmt->fmt.pix.width / 8;
101 imagesize = bpl ? bpl * fmt->fmt.pix.height : fmt->fmt.pix.sizeimage;
102
103 video->fcc = format->fcc;
104 video->bpp = format->bpp;
105 video->width = fmt->fmt.pix.width;
106 video->height = fmt->fmt.pix.height;
107 video->imagesize = imagesize;
108
109 fmt->fmt.pix.field = V4L2_FIELD_NONE;
110 fmt->fmt.pix.bytesperline = bpl;
111 fmt->fmt.pix.sizeimage = imagesize;
112 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
113 fmt->fmt.pix.priv = 0;
114
115 return 0;
116}
117
118static int
119uvc_v4l2_open(struct file *file)
120{
121 struct video_device *vdev = video_devdata(file);
122 struct uvc_device *uvc = video_get_drvdata(vdev);
123 struct uvc_file_handle *handle;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200124
125 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
126 if (handle == NULL)
127 return -ENOMEM;
128
Hans Verkuil523f46d2011-06-13 17:44:42 -0300129 v4l2_fh_init(&handle->vfh, vdev);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200130 v4l2_fh_add(&handle->vfh);
131
132 handle->device = &uvc->video;
133 file->private_data = &handle->vfh;
134
135 uvc_function_connect(uvc);
136 return 0;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200137}
138
139static int
140uvc_v4l2_release(struct file *file)
141{
142 struct video_device *vdev = video_devdata(file);
143 struct uvc_device *uvc = video_get_drvdata(vdev);
144 struct uvc_file_handle *handle = to_uvc_file_handle(file->private_data);
145 struct uvc_video *video = handle->device;
146
147 uvc_function_disconnect(uvc);
148
149 uvc_video_enable(video, 0);
150 mutex_lock(&video->queue.mutex);
151 if (uvc_free_buffers(&video->queue) < 0)
152 printk(KERN_ERR "uvc_v4l2_release: Unable to free "
153 "buffers.\n");
154 mutex_unlock(&video->queue.mutex);
155
156 file->private_data = NULL;
157 v4l2_fh_del(&handle->vfh);
158 v4l2_fh_exit(&handle->vfh);
159 kfree(handle);
160 return 0;
161}
162
163static long
164uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
165{
166 struct video_device *vdev = video_devdata(file);
167 struct uvc_device *uvc = video_get_drvdata(vdev);
168 struct uvc_file_handle *handle = to_uvc_file_handle(file->private_data);
169 struct usb_composite_dev *cdev = uvc->func.config->cdev;
170 struct uvc_video *video = &uvc->video;
171 int ret = 0;
172
173 switch (cmd) {
174 /* Query capabilities */
175 case VIDIOC_QUERYCAP:
176 {
177 struct v4l2_capability *cap = arg;
178
179 memset(cap, 0, sizeof *cap);
Chen Gangc3ec8302013-03-01 20:46:33 +0100180 strlcpy(cap->driver, "g_uvc", sizeof(cap->driver));
181 strlcpy(cap->card, cdev->gadget->name, sizeof(cap->card));
182 strlcpy(cap->bus_info, dev_name(&cdev->gadget->dev),
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200183 sizeof cap->bus_info);
184 cap->version = DRIVER_VERSION_NUMBER;
185 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
186 break;
187 }
188
189 /* Get & Set format */
190 case VIDIOC_G_FMT:
191 {
192 struct v4l2_format *fmt = arg;
193
194 if (fmt->type != video->queue.type)
195 return -EINVAL;
196
197 return uvc_v4l2_get_format(video, fmt);
198 }
199
200 case VIDIOC_S_FMT:
201 {
202 struct v4l2_format *fmt = arg;
203
204 if (fmt->type != video->queue.type)
205 return -EINVAL;
206
207 return uvc_v4l2_set_format(video, fmt);
208 }
209
210 /* Buffers & streaming */
211 case VIDIOC_REQBUFS:
212 {
213 struct v4l2_requestbuffers *rb = arg;
214
215 if (rb->type != video->queue.type ||
216 rb->memory != V4L2_MEMORY_MMAP)
217 return -EINVAL;
218
219 ret = uvc_alloc_buffers(&video->queue, rb->count,
220 video->imagesize);
221 if (ret < 0)
222 return ret;
223
224 rb->count = ret;
225 ret = 0;
226 break;
227 }
228
229 case VIDIOC_QUERYBUF:
230 {
231 struct v4l2_buffer *buf = arg;
232
233 if (buf->type != video->queue.type)
234 return -EINVAL;
235
236 return uvc_query_buffer(&video->queue, buf);
237 }
238
239 case VIDIOC_QBUF:
240 if ((ret = uvc_queue_buffer(&video->queue, arg)) < 0)
241 return ret;
242
243 return uvc_video_pump(video);
244
245 case VIDIOC_DQBUF:
246 return uvc_dequeue_buffer(&video->queue, arg,
247 file->f_flags & O_NONBLOCK);
248
249 case VIDIOC_STREAMON:
250 {
251 int *type = arg;
252
253 if (*type != video->queue.type)
254 return -EINVAL;
255
Bhupesh Sharma41837c32013-03-01 20:46:30 +0100256 /* Enable UVC video. */
257 ret = uvc_video_enable(video, 1);
258 if (ret < 0)
259 return ret;
260
261 /*
262 * Complete the alternate setting selection setup phase now that
263 * userspace is ready to provide video frames.
264 */
265 uvc_function_setup_continue(uvc);
266 uvc->state = UVC_STATE_STREAMING;
267
268 return 0;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200269 }
270
271 case VIDIOC_STREAMOFF:
272 {
273 int *type = arg;
274
275 if (*type != video->queue.type)
276 return -EINVAL;
277
278 return uvc_video_enable(video, 0);
279 }
280
281 /* Events */
282 case VIDIOC_DQEVENT:
283 {
284 struct v4l2_event *event = arg;
285
286 ret = v4l2_event_dequeue(&handle->vfh, event,
287 file->f_flags & O_NONBLOCK);
288 if (ret == 0 && event->type == UVC_EVENT_SETUP) {
289 struct uvc_event *uvc_event = (void *)&event->u.data;
290
291 /* Tell the complete callback to generate an event for
292 * the next request that will be enqueued by
293 * uvc_event_write.
294 */
295 uvc->event_setup_out =
296 !(uvc_event->req.bRequestType & USB_DIR_IN);
297 uvc->event_length = uvc_event->req.wLength;
298 }
299
300 return ret;
301 }
302
303 case VIDIOC_SUBSCRIBE_EVENT:
304 {
305 struct v4l2_event_subscription *sub = arg;
306
307 if (sub->type < UVC_EVENT_FIRST || sub->type > UVC_EVENT_LAST)
308 return -EINVAL;
309
Hans de Goedec53c2542012-04-08 12:59:46 -0300310 return v4l2_event_subscribe(&handle->vfh, arg, 2, NULL);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200311 }
312
313 case VIDIOC_UNSUBSCRIBE_EVENT:
314 return v4l2_event_unsubscribe(&handle->vfh, arg);
315
316 case UVCIOC_SEND_RESPONSE:
317 ret = uvc_send_response(uvc, arg);
318 break;
319
320 default:
321 return -ENOIOCTLCMD;
322 }
323
324 return ret;
325}
326
327static long
328uvc_v4l2_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
329{
330 return video_usercopy(file, cmd, arg, uvc_v4l2_do_ioctl);
331}
332
333static int
334uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
335{
336 struct video_device *vdev = video_devdata(file);
337 struct uvc_device *uvc = video_get_drvdata(vdev);
338
339 return uvc_queue_mmap(&uvc->video.queue, vma);
340}
341
342static unsigned int
343uvc_v4l2_poll(struct file *file, poll_table *wait)
344{
345 struct video_device *vdev = video_devdata(file);
346 struct uvc_device *uvc = video_get_drvdata(vdev);
347 struct uvc_file_handle *handle = to_uvc_file_handle(file->private_data);
348 unsigned int mask = 0;
349
Hans Verkuil523f46d2011-06-13 17:44:42 -0300350 poll_wait(file, &handle->vfh.wait, wait);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200351 if (v4l2_event_pending(&handle->vfh))
352 mask |= POLLPRI;
353
354 mask |= uvc_queue_poll(&uvc->video.queue, file, wait);
355
356 return mask;
357}
358
Laurent Pinchart5d9955f2010-07-10 16:13:05 -0300359static struct v4l2_file_operations uvc_v4l2_fops = {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200360 .owner = THIS_MODULE,
361 .open = uvc_v4l2_open,
362 .release = uvc_v4l2_release,
363 .ioctl = uvc_v4l2_ioctl,
364 .mmap = uvc_v4l2_mmap,
365 .poll = uvc_v4l2_poll,
366};
367