blob: c1fb5249f4a49d1d49e0bd905b2e59eaaf005363 [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0+
Laurent Pinchartcdda4792010-05-02 20:57:41 +02002/*
3 * uvc_v4l2.c -- USB Video Class Gadget driver
4 *
5 * Copyright (C) 2009-2010
6 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
Laurent Pinchartcdda4792010-05-02 20:57:41 +020012 */
13
14#include <linux/kernel.h>
15#include <linux/device.h>
16#include <linux/errno.h>
17#include <linux/list.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
Andrzej Pietrasiewicz3a83c162014-09-09 02:02:09 +030026#include "f_uvc.h"
Laurent Pinchartcdda4792010-05-02 20:57:41 +020027#include "uvc.h"
28#include "uvc_queue.h"
Andrzej Pietrasiewicz3a83c162014-09-09 02:02:09 +030029#include "uvc_video.h"
Lad, Prabhakar1f754ef2015-02-05 13:16:26 +000030#include "uvc_v4l2.h"
Laurent Pinchartcdda4792010-05-02 20:57:41 +020031
32/* --------------------------------------------------------------------------
33 * Requests handling
34 */
35
36static int
37uvc_send_response(struct uvc_device *uvc, struct uvc_request_data *data)
38{
39 struct usb_composite_dev *cdev = uvc->func.config->cdev;
40 struct usb_request *req = uvc->control_req;
41
42 if (data->length < 0)
43 return usb_ep_set_halt(cdev->gadget->ep0);
44
Laurent Pinchart6f6543f2012-04-24 11:29:42 +020045 req->length = min_t(unsigned int, uvc->event_length, data->length);
Laurent Pinchartcdda4792010-05-02 20:57:41 +020046 req->zero = data->length < uvc->event_length;
Laurent Pinchartcdda4792010-05-02 20:57:41 +020047
Dan Carpentera5eaaa12013-03-14 11:01:05 +030048 memcpy(req->buf, data->data, req->length);
Laurent Pinchartcdda4792010-05-02 20:57:41 +020049
50 return usb_ep_queue(cdev->gadget->ep0, req, GFP_KERNEL);
51}
52
53/* --------------------------------------------------------------------------
Laurent Pincharta1d27a42014-09-08 11:18:15 +030054 * V4L2 ioctls
Laurent Pinchartcdda4792010-05-02 20:57:41 +020055 */
56
Anson Jacob97f01172016-11-11 19:07:01 -050057struct uvc_format {
Laurent Pinchartcdda4792010-05-02 20:57:41 +020058 u8 bpp;
59 u32 fcc;
60};
61
62static struct uvc_format uvc_formats[] = {
63 { 16, V4L2_PIX_FMT_YUYV },
64 { 0, V4L2_PIX_FMT_MJPEG },
65};
66
67static int
Laurent Pincharta1d27a42014-09-08 11:18:15 +030068uvc_v4l2_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
Laurent Pinchartcdda4792010-05-02 20:57:41 +020069{
Laurent Pincharta1d27a42014-09-08 11:18:15 +030070 struct video_device *vdev = video_devdata(file);
71 struct uvc_device *uvc = video_get_drvdata(vdev);
72 struct usb_composite_dev *cdev = uvc->func.config->cdev;
73
74 strlcpy(cap->driver, "g_uvc", sizeof(cap->driver));
75 strlcpy(cap->card, cdev->gadget->name, sizeof(cap->card));
76 strlcpy(cap->bus_info, dev_name(&cdev->gadget->dev),
77 sizeof(cap->bus_info));
78
Hans Verkuil06e5cc32015-02-17 05:44:08 -030079 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
80 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
Laurent Pincharta1d27a42014-09-08 11:18:15 +030081
82 return 0;
83}
84
85static int
86uvc_v4l2_get_format(struct file *file, void *fh, struct v4l2_format *fmt)
87{
88 struct video_device *vdev = video_devdata(file);
89 struct uvc_device *uvc = video_get_drvdata(vdev);
90 struct uvc_video *video = &uvc->video;
91
Laurent Pinchartcdda4792010-05-02 20:57:41 +020092 fmt->fmt.pix.pixelformat = video->fcc;
93 fmt->fmt.pix.width = video->width;
94 fmt->fmt.pix.height = video->height;
95 fmt->fmt.pix.field = V4L2_FIELD_NONE;
96 fmt->fmt.pix.bytesperline = video->bpp * video->width / 8;
97 fmt->fmt.pix.sizeimage = video->imagesize;
98 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
99 fmt->fmt.pix.priv = 0;
100
101 return 0;
102}
103
104static int
Laurent Pincharta1d27a42014-09-08 11:18:15 +0300105uvc_v4l2_set_format(struct file *file, void *fh, struct v4l2_format *fmt)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200106{
Laurent Pincharta1d27a42014-09-08 11:18:15 +0300107 struct video_device *vdev = video_devdata(file);
108 struct uvc_device *uvc = video_get_drvdata(vdev);
109 struct uvc_video *video = &uvc->video;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200110 struct uvc_format *format;
111 unsigned int imagesize;
112 unsigned int bpl;
113 unsigned int i;
114
115 for (i = 0; i < ARRAY_SIZE(uvc_formats); ++i) {
116 format = &uvc_formats[i];
117 if (format->fcc == fmt->fmt.pix.pixelformat)
118 break;
119 }
120
Dan Carpenter9a887162010-08-12 09:59:58 +0200121 if (i == ARRAY_SIZE(uvc_formats)) {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200122 printk(KERN_INFO "Unsupported format 0x%08x.\n",
123 fmt->fmt.pix.pixelformat);
124 return -EINVAL;
125 }
126
127 bpl = format->bpp * fmt->fmt.pix.width / 8;
128 imagesize = bpl ? bpl * fmt->fmt.pix.height : fmt->fmt.pix.sizeimage;
129
130 video->fcc = format->fcc;
131 video->bpp = format->bpp;
132 video->width = fmt->fmt.pix.width;
133 video->height = fmt->fmt.pix.height;
134 video->imagesize = imagesize;
135
136 fmt->fmt.pix.field = V4L2_FIELD_NONE;
137 fmt->fmt.pix.bytesperline = bpl;
138 fmt->fmt.pix.sizeimage = imagesize;
139 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
140 fmt->fmt.pix.priv = 0;
141
142 return 0;
143}
144
145static int
Laurent Pincharta1d27a42014-09-08 11:18:15 +0300146uvc_v4l2_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *b)
147{
148 struct video_device *vdev = video_devdata(file);
149 struct uvc_device *uvc = video_get_drvdata(vdev);
150 struct uvc_video *video = &uvc->video;
151
152 if (b->type != video->queue.queue.type)
153 return -EINVAL;
154
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300155 return uvcg_alloc_buffers(&video->queue, b);
Laurent Pincharta1d27a42014-09-08 11:18:15 +0300156}
157
158static int
159uvc_v4l2_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
160{
161 struct video_device *vdev = video_devdata(file);
162 struct uvc_device *uvc = video_get_drvdata(vdev);
163 struct uvc_video *video = &uvc->video;
164
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300165 return uvcg_query_buffer(&video->queue, b);
Laurent Pincharta1d27a42014-09-08 11:18:15 +0300166}
167
168static int
169uvc_v4l2_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
170{
171 struct video_device *vdev = video_devdata(file);
172 struct uvc_device *uvc = video_get_drvdata(vdev);
173 struct uvc_video *video = &uvc->video;
174 int ret;
175
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300176 ret = uvcg_queue_buffer(&video->queue, b);
Laurent Pincharta1d27a42014-09-08 11:18:15 +0300177 if (ret < 0)
178 return ret;
179
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300180 return uvcg_video_pump(video);
Laurent Pincharta1d27a42014-09-08 11:18:15 +0300181}
182
183static int
184uvc_v4l2_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
185{
186 struct video_device *vdev = video_devdata(file);
187 struct uvc_device *uvc = video_get_drvdata(vdev);
188 struct uvc_video *video = &uvc->video;
189
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300190 return uvcg_dequeue_buffer(&video->queue, b, file->f_flags & O_NONBLOCK);
Laurent Pincharta1d27a42014-09-08 11:18:15 +0300191}
192
193static int
194uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
195{
196 struct video_device *vdev = video_devdata(file);
197 struct uvc_device *uvc = video_get_drvdata(vdev);
198 struct uvc_video *video = &uvc->video;
199 int ret;
200
201 if (type != video->queue.queue.type)
202 return -EINVAL;
203
204 /* Enable UVC video. */
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300205 ret = uvcg_video_enable(video, 1);
Laurent Pincharta1d27a42014-09-08 11:18:15 +0300206 if (ret < 0)
207 return ret;
208
209 /*
210 * Complete the alternate setting selection setup phase now that
211 * userspace is ready to provide video frames.
212 */
213 uvc_function_setup_continue(uvc);
214 uvc->state = UVC_STATE_STREAMING;
215
216 return 0;
217}
218
219static int
220uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
221{
222 struct video_device *vdev = video_devdata(file);
223 struct uvc_device *uvc = video_get_drvdata(vdev);
224 struct uvc_video *video = &uvc->video;
225
226 if (type != video->queue.queue.type)
227 return -EINVAL;
228
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300229 return uvcg_video_enable(video, 0);
Laurent Pincharta1d27a42014-09-08 11:18:15 +0300230}
231
232static int
233uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
234 const struct v4l2_event_subscription *sub)
235{
236 if (sub->type < UVC_EVENT_FIRST || sub->type > UVC_EVENT_LAST)
237 return -EINVAL;
238
239 return v4l2_event_subscribe(fh, sub, 2, NULL);
240}
241
242static int
243uvc_v4l2_unsubscribe_event(struct v4l2_fh *fh,
244 const struct v4l2_event_subscription *sub)
245{
246 return v4l2_event_unsubscribe(fh, sub);
247}
248
249static long
250uvc_v4l2_ioctl_default(struct file *file, void *fh, bool valid_prio,
251 unsigned int cmd, void *arg)
252{
253 struct video_device *vdev = video_devdata(file);
254 struct uvc_device *uvc = video_get_drvdata(vdev);
255
256 switch (cmd) {
257 case UVCIOC_SEND_RESPONSE:
258 return uvc_send_response(uvc, arg);
259
260 default:
261 return -ENOIOCTLCMD;
262 }
263}
264
Andrzej Pietrasiewicz3a83c162014-09-09 02:02:09 +0300265const struct v4l2_ioctl_ops uvc_v4l2_ioctl_ops = {
Laurent Pincharta1d27a42014-09-08 11:18:15 +0300266 .vidioc_querycap = uvc_v4l2_querycap,
267 .vidioc_g_fmt_vid_out = uvc_v4l2_get_format,
268 .vidioc_s_fmt_vid_out = uvc_v4l2_set_format,
269 .vidioc_reqbufs = uvc_v4l2_reqbufs,
270 .vidioc_querybuf = uvc_v4l2_querybuf,
271 .vidioc_qbuf = uvc_v4l2_qbuf,
272 .vidioc_dqbuf = uvc_v4l2_dqbuf,
273 .vidioc_streamon = uvc_v4l2_streamon,
274 .vidioc_streamoff = uvc_v4l2_streamoff,
275 .vidioc_subscribe_event = uvc_v4l2_subscribe_event,
276 .vidioc_unsubscribe_event = uvc_v4l2_unsubscribe_event,
277 .vidioc_default = uvc_v4l2_ioctl_default,
278};
279
280/* --------------------------------------------------------------------------
281 * V4L2
282 */
283
284static int
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200285uvc_v4l2_open(struct file *file)
286{
287 struct video_device *vdev = video_devdata(file);
288 struct uvc_device *uvc = video_get_drvdata(vdev);
289 struct uvc_file_handle *handle;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200290
291 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
292 if (handle == NULL)
293 return -ENOMEM;
294
Hans Verkuil523f46d2011-06-13 17:44:42 -0300295 v4l2_fh_init(&handle->vfh, vdev);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200296 v4l2_fh_add(&handle->vfh);
297
298 handle->device = &uvc->video;
299 file->private_data = &handle->vfh;
300
301 uvc_function_connect(uvc);
302 return 0;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200303}
304
305static int
306uvc_v4l2_release(struct file *file)
307{
308 struct video_device *vdev = video_devdata(file);
309 struct uvc_device *uvc = video_get_drvdata(vdev);
310 struct uvc_file_handle *handle = to_uvc_file_handle(file->private_data);
311 struct uvc_video *video = handle->device;
312
313 uvc_function_disconnect(uvc);
314
Hans Verkuild8e96c42015-02-17 05:44:06 -0300315 mutex_lock(&video->mutex);
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300316 uvcg_video_enable(video, 0);
317 uvcg_free_buffers(&video->queue);
Hans Verkuild8e96c42015-02-17 05:44:06 -0300318 mutex_unlock(&video->mutex);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200319
320 file->private_data = NULL;
321 v4l2_fh_del(&handle->vfh);
322 v4l2_fh_exit(&handle->vfh);
323 kfree(handle);
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530324
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200325 return 0;
326}
327
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200328static int
329uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
330{
331 struct video_device *vdev = video_devdata(file);
332 struct uvc_device *uvc = video_get_drvdata(vdev);
333
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300334 return uvcg_queue_mmap(&uvc->video.queue, vma);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200335}
336
337static unsigned int
338uvc_v4l2_poll(struct file *file, poll_table *wait)
339{
340 struct video_device *vdev = video_devdata(file);
341 struct uvc_device *uvc = video_get_drvdata(vdev);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200342
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300343 return uvcg_queue_poll(&uvc->video.queue, file, wait);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200344}
345
Bhupesh Sharma2f1d5702013-03-28 15:11:53 +0530346#ifndef CONFIG_MMU
Arnd Bergmanna173dc42014-09-29 14:30:20 +0200347static unsigned long uvcg_v4l2_get_unmapped_area(struct file *file,
Bhupesh Sharma2f1d5702013-03-28 15:11:53 +0530348 unsigned long addr, unsigned long len, unsigned long pgoff,
349 unsigned long flags)
350{
351 struct video_device *vdev = video_devdata(file);
352 struct uvc_device *uvc = video_get_drvdata(vdev);
353
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300354 return uvcg_queue_get_unmapped_area(&uvc->video.queue, pgoff);
Bhupesh Sharma2f1d5702013-03-28 15:11:53 +0530355}
356#endif
357
Bhumika Goyal87e981d2017-09-27 22:57:07 +0530358const struct v4l2_file_operations uvc_v4l2_fops = {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200359 .owner = THIS_MODULE,
360 .open = uvc_v4l2_open,
361 .release = uvc_v4l2_release,
Hans Verkuil9945eb92015-02-17 05:44:07 -0300362 .unlocked_ioctl = video_ioctl2,
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200363 .mmap = uvc_v4l2_mmap,
364 .poll = uvc_v4l2_poll,
Bhupesh Sharma2f1d5702013-03-28 15:11:53 +0530365#ifndef CONFIG_MMU
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300366 .get_unmapped_area = uvcg_v4l2_get_unmapped_area,
Bhupesh Sharma2f1d5702013-03-28 15:11:53 +0530367#endif
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200368};
369