blob: 0bd696510cf872cc86d9eafd791658653ced66d3 [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>
Laurent Pinchartcdda4792010-05-02 20:57:41 +020017#include <linux/videodev2.h>
18#include <linux/vmalloc.h>
19#include <linux/wait.h>
20
21#include <media/v4l2-dev.h>
22#include <media/v4l2-event.h>
23#include <media/v4l2-ioctl.h>
24
Andrzej Pietrasiewicz3a83c162014-09-09 02:02:09 +030025#include "f_uvc.h"
Laurent Pinchartcdda4792010-05-02 20:57:41 +020026#include "uvc.h"
27#include "uvc_queue.h"
Andrzej Pietrasiewicz3a83c162014-09-09 02:02:09 +030028#include "uvc_video.h"
Laurent Pinchartcdda4792010-05-02 20:57:41 +020029
30/* --------------------------------------------------------------------------
31 * Requests handling
32 */
33
34static int
35uvc_send_response(struct uvc_device *uvc, struct uvc_request_data *data)
36{
37 struct usb_composite_dev *cdev = uvc->func.config->cdev;
38 struct usb_request *req = uvc->control_req;
39
40 if (data->length < 0)
41 return usb_ep_set_halt(cdev->gadget->ep0);
42
Laurent Pinchart6f6543f2012-04-24 11:29:42 +020043 req->length = min_t(unsigned int, uvc->event_length, data->length);
Laurent Pinchartcdda4792010-05-02 20:57:41 +020044 req->zero = data->length < uvc->event_length;
Laurent Pinchartcdda4792010-05-02 20:57:41 +020045
Dan Carpentera5eaaa12013-03-14 11:01:05 +030046 memcpy(req->buf, data->data, req->length);
Laurent Pinchartcdda4792010-05-02 20:57:41 +020047
48 return usb_ep_queue(cdev->gadget->ep0, req, GFP_KERNEL);
49}
50
51/* --------------------------------------------------------------------------
Laurent Pincharta1d27a42014-09-08 11:18:15 +030052 * V4L2 ioctls
Laurent Pinchartcdda4792010-05-02 20:57:41 +020053 */
54
55struct uvc_format
56{
57 u8 bpp;
58 u32 fcc;
59};
60
61static struct uvc_format uvc_formats[] = {
62 { 16, V4L2_PIX_FMT_YUYV },
63 { 0, V4L2_PIX_FMT_MJPEG },
64};
65
66static int
Laurent Pincharta1d27a42014-09-08 11:18:15 +030067uvc_v4l2_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
Laurent Pinchartcdda4792010-05-02 20:57:41 +020068{
Laurent Pincharta1d27a42014-09-08 11:18:15 +030069 struct video_device *vdev = video_devdata(file);
70 struct uvc_device *uvc = video_get_drvdata(vdev);
71 struct usb_composite_dev *cdev = uvc->func.config->cdev;
72
73 strlcpy(cap->driver, "g_uvc", sizeof(cap->driver));
74 strlcpy(cap->card, cdev->gadget->name, sizeof(cap->card));
75 strlcpy(cap->bus_info, dev_name(&cdev->gadget->dev),
76 sizeof(cap->bus_info));
77
78 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
79
80 return 0;
81}
82
83static int
84uvc_v4l2_get_format(struct file *file, void *fh, struct v4l2_format *fmt)
85{
86 struct video_device *vdev = video_devdata(file);
87 struct uvc_device *uvc = video_get_drvdata(vdev);
88 struct uvc_video *video = &uvc->video;
89
Laurent Pinchartcdda4792010-05-02 20:57:41 +020090 fmt->fmt.pix.pixelformat = video->fcc;
91 fmt->fmt.pix.width = video->width;
92 fmt->fmt.pix.height = video->height;
93 fmt->fmt.pix.field = V4L2_FIELD_NONE;
94 fmt->fmt.pix.bytesperline = video->bpp * video->width / 8;
95 fmt->fmt.pix.sizeimage = video->imagesize;
96 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
97 fmt->fmt.pix.priv = 0;
98
99 return 0;
100}
101
102static int
Laurent Pincharta1d27a42014-09-08 11:18:15 +0300103uvc_v4l2_set_format(struct file *file, void *fh, struct v4l2_format *fmt)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200104{
Laurent Pincharta1d27a42014-09-08 11:18:15 +0300105 struct video_device *vdev = video_devdata(file);
106 struct uvc_device *uvc = video_get_drvdata(vdev);
107 struct uvc_video *video = &uvc->video;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200108 struct uvc_format *format;
109 unsigned int imagesize;
110 unsigned int bpl;
111 unsigned int i;
112
113 for (i = 0; i < ARRAY_SIZE(uvc_formats); ++i) {
114 format = &uvc_formats[i];
115 if (format->fcc == fmt->fmt.pix.pixelformat)
116 break;
117 }
118
Dan Carpenter9a887162010-08-12 09:59:58 +0200119 if (i == ARRAY_SIZE(uvc_formats)) {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200120 printk(KERN_INFO "Unsupported format 0x%08x.\n",
121 fmt->fmt.pix.pixelformat);
122 return -EINVAL;
123 }
124
125 bpl = format->bpp * fmt->fmt.pix.width / 8;
126 imagesize = bpl ? bpl * fmt->fmt.pix.height : fmt->fmt.pix.sizeimage;
127
128 video->fcc = format->fcc;
129 video->bpp = format->bpp;
130 video->width = fmt->fmt.pix.width;
131 video->height = fmt->fmt.pix.height;
132 video->imagesize = imagesize;
133
134 fmt->fmt.pix.field = V4L2_FIELD_NONE;
135 fmt->fmt.pix.bytesperline = bpl;
136 fmt->fmt.pix.sizeimage = imagesize;
137 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
138 fmt->fmt.pix.priv = 0;
139
140 return 0;
141}
142
143static int
Laurent Pincharta1d27a42014-09-08 11:18:15 +0300144uvc_v4l2_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *b)
145{
146 struct video_device *vdev = video_devdata(file);
147 struct uvc_device *uvc = video_get_drvdata(vdev);
148 struct uvc_video *video = &uvc->video;
149
150 if (b->type != video->queue.queue.type)
151 return -EINVAL;
152
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300153 return uvcg_alloc_buffers(&video->queue, b);
Laurent Pincharta1d27a42014-09-08 11:18:15 +0300154}
155
156static int
157uvc_v4l2_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
158{
159 struct video_device *vdev = video_devdata(file);
160 struct uvc_device *uvc = video_get_drvdata(vdev);
161 struct uvc_video *video = &uvc->video;
162
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300163 return uvcg_query_buffer(&video->queue, b);
Laurent Pincharta1d27a42014-09-08 11:18:15 +0300164}
165
166static int
167uvc_v4l2_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
168{
169 struct video_device *vdev = video_devdata(file);
170 struct uvc_device *uvc = video_get_drvdata(vdev);
171 struct uvc_video *video = &uvc->video;
172 int ret;
173
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300174 ret = uvcg_queue_buffer(&video->queue, b);
Laurent Pincharta1d27a42014-09-08 11:18:15 +0300175 if (ret < 0)
176 return ret;
177
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300178 return uvcg_video_pump(video);
Laurent Pincharta1d27a42014-09-08 11:18:15 +0300179}
180
181static int
182uvc_v4l2_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
183{
184 struct video_device *vdev = video_devdata(file);
185 struct uvc_device *uvc = video_get_drvdata(vdev);
186 struct uvc_video *video = &uvc->video;
187
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300188 return uvcg_dequeue_buffer(&video->queue, b, file->f_flags & O_NONBLOCK);
Laurent Pincharta1d27a42014-09-08 11:18:15 +0300189}
190
191static int
192uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
193{
194 struct video_device *vdev = video_devdata(file);
195 struct uvc_device *uvc = video_get_drvdata(vdev);
196 struct uvc_video *video = &uvc->video;
197 int ret;
198
199 if (type != video->queue.queue.type)
200 return -EINVAL;
201
202 /* Enable UVC video. */
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300203 ret = uvcg_video_enable(video, 1);
Laurent Pincharta1d27a42014-09-08 11:18:15 +0300204 if (ret < 0)
205 return ret;
206
207 /*
208 * Complete the alternate setting selection setup phase now that
209 * userspace is ready to provide video frames.
210 */
211 uvc_function_setup_continue(uvc);
212 uvc->state = UVC_STATE_STREAMING;
213
214 return 0;
215}
216
217static int
218uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
219{
220 struct video_device *vdev = video_devdata(file);
221 struct uvc_device *uvc = video_get_drvdata(vdev);
222 struct uvc_video *video = &uvc->video;
223
224 if (type != video->queue.queue.type)
225 return -EINVAL;
226
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300227 return uvcg_video_enable(video, 0);
Laurent Pincharta1d27a42014-09-08 11:18:15 +0300228}
229
230static int
231uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
232 const struct v4l2_event_subscription *sub)
233{
234 if (sub->type < UVC_EVENT_FIRST || sub->type > UVC_EVENT_LAST)
235 return -EINVAL;
236
237 return v4l2_event_subscribe(fh, sub, 2, NULL);
238}
239
240static int
241uvc_v4l2_unsubscribe_event(struct v4l2_fh *fh,
242 const struct v4l2_event_subscription *sub)
243{
244 return v4l2_event_unsubscribe(fh, sub);
245}
246
247static long
248uvc_v4l2_ioctl_default(struct file *file, void *fh, bool valid_prio,
249 unsigned int cmd, void *arg)
250{
251 struct video_device *vdev = video_devdata(file);
252 struct uvc_device *uvc = video_get_drvdata(vdev);
253
254 switch (cmd) {
255 case UVCIOC_SEND_RESPONSE:
256 return uvc_send_response(uvc, arg);
257
258 default:
259 return -ENOIOCTLCMD;
260 }
261}
262
Andrzej Pietrasiewicz3a83c162014-09-09 02:02:09 +0300263const struct v4l2_ioctl_ops uvc_v4l2_ioctl_ops = {
Laurent Pincharta1d27a42014-09-08 11:18:15 +0300264 .vidioc_querycap = uvc_v4l2_querycap,
265 .vidioc_g_fmt_vid_out = uvc_v4l2_get_format,
266 .vidioc_s_fmt_vid_out = uvc_v4l2_set_format,
267 .vidioc_reqbufs = uvc_v4l2_reqbufs,
268 .vidioc_querybuf = uvc_v4l2_querybuf,
269 .vidioc_qbuf = uvc_v4l2_qbuf,
270 .vidioc_dqbuf = uvc_v4l2_dqbuf,
271 .vidioc_streamon = uvc_v4l2_streamon,
272 .vidioc_streamoff = uvc_v4l2_streamoff,
273 .vidioc_subscribe_event = uvc_v4l2_subscribe_event,
274 .vidioc_unsubscribe_event = uvc_v4l2_unsubscribe_event,
275 .vidioc_default = uvc_v4l2_ioctl_default,
276};
277
278/* --------------------------------------------------------------------------
279 * V4L2
280 */
281
282static int
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200283uvc_v4l2_open(struct file *file)
284{
285 struct video_device *vdev = video_devdata(file);
286 struct uvc_device *uvc = video_get_drvdata(vdev);
287 struct uvc_file_handle *handle;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200288
289 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
290 if (handle == NULL)
291 return -ENOMEM;
292
Hans Verkuil523f46d2011-06-13 17:44:42 -0300293 v4l2_fh_init(&handle->vfh, vdev);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200294 v4l2_fh_add(&handle->vfh);
295
296 handle->device = &uvc->video;
297 file->private_data = &handle->vfh;
298
299 uvc_function_connect(uvc);
300 return 0;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200301}
302
303static int
304uvc_v4l2_release(struct file *file)
305{
306 struct video_device *vdev = video_devdata(file);
307 struct uvc_device *uvc = video_get_drvdata(vdev);
308 struct uvc_file_handle *handle = to_uvc_file_handle(file->private_data);
309 struct uvc_video *video = handle->device;
310
311 uvc_function_disconnect(uvc);
312
Hans Verkuild8e96c42015-02-17 05:44:06 -0300313 mutex_lock(&video->mutex);
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300314 uvcg_video_enable(video, 0);
315 uvcg_free_buffers(&video->queue);
Hans Verkuild8e96c42015-02-17 05:44:06 -0300316 mutex_unlock(&video->mutex);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200317
318 file->private_data = NULL;
319 v4l2_fh_del(&handle->vfh);
320 v4l2_fh_exit(&handle->vfh);
321 kfree(handle);
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530322
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200323 return 0;
324}
325
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200326static int
327uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
328{
329 struct video_device *vdev = video_devdata(file);
330 struct uvc_device *uvc = video_get_drvdata(vdev);
331
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300332 return uvcg_queue_mmap(&uvc->video.queue, vma);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200333}
334
335static unsigned int
336uvc_v4l2_poll(struct file *file, poll_table *wait)
337{
338 struct video_device *vdev = video_devdata(file);
339 struct uvc_device *uvc = video_get_drvdata(vdev);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200340
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300341 return uvcg_queue_poll(&uvc->video.queue, file, wait);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200342}
343
Bhupesh Sharma2f1d5702013-03-28 15:11:53 +0530344#ifndef CONFIG_MMU
Arnd Bergmanna173dc42014-09-29 14:30:20 +0200345static unsigned long uvcg_v4l2_get_unmapped_area(struct file *file,
Bhupesh Sharma2f1d5702013-03-28 15:11:53 +0530346 unsigned long addr, unsigned long len, unsigned long pgoff,
347 unsigned long flags)
348{
349 struct video_device *vdev = video_devdata(file);
350 struct uvc_device *uvc = video_get_drvdata(vdev);
351
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300352 return uvcg_queue_get_unmapped_area(&uvc->video.queue, pgoff);
Bhupesh Sharma2f1d5702013-03-28 15:11:53 +0530353}
354#endif
355
Andrzej Pietrasiewicz3a83c162014-09-09 02:02:09 +0300356struct v4l2_file_operations uvc_v4l2_fops = {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200357 .owner = THIS_MODULE,
358 .open = uvc_v4l2_open,
359 .release = uvc_v4l2_release,
Laurent Pincharta1d27a42014-09-08 11:18:15 +0300360 .ioctl = video_ioctl2,
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200361 .mmap = uvc_v4l2_mmap,
362 .poll = uvc_v4l2_poll,
Bhupesh Sharma2f1d5702013-03-28 15:11:53 +0530363#ifndef CONFIG_MMU
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300364 .get_unmapped_area = uvcg_v4l2_get_unmapped_area,
Bhupesh Sharma2f1d5702013-03-28 15:11:53 +0530365#endif
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200366};
367