perf python: Do not force closing original perf descriptor in evlist.get_pollfd()
[linux/fpc-iii.git] / drivers / usb / gadget / function / uvc_v4l2.c
bloba1183eccee227febeafea3471d74dc0221ca82ab
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * uvc_v4l2.c -- USB Video Class Gadget driver
5 * Copyright (C) 2009-2010
6 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7 */
9 #include <linux/device.h>
10 #include <linux/errno.h>
11 #include <linux/kernel.h>
12 #include <linux/list.h>
13 #include <linux/usb/g_uvc.h>
14 #include <linux/videodev2.h>
15 #include <linux/vmalloc.h>
16 #include <linux/wait.h>
18 #include <media/v4l2-dev.h>
19 #include <media/v4l2-event.h>
20 #include <media/v4l2-ioctl.h>
22 #include "f_uvc.h"
23 #include "uvc.h"
24 #include "uvc_queue.h"
25 #include "uvc_video.h"
26 #include "uvc_v4l2.h"
28 /* --------------------------------------------------------------------------
29 * Requests handling
32 static int
33 uvc_send_response(struct uvc_device *uvc, struct uvc_request_data *data)
35 struct usb_composite_dev *cdev = uvc->func.config->cdev;
36 struct usb_request *req = uvc->control_req;
38 if (data->length < 0)
39 return usb_ep_set_halt(cdev->gadget->ep0);
41 req->length = min_t(unsigned int, uvc->event_length, data->length);
42 req->zero = data->length < uvc->event_length;
44 memcpy(req->buf, data->data, req->length);
46 return usb_ep_queue(cdev->gadget->ep0, req, GFP_KERNEL);
49 /* --------------------------------------------------------------------------
50 * V4L2 ioctls
53 struct uvc_format {
54 u8 bpp;
55 u32 fcc;
58 static struct uvc_format uvc_formats[] = {
59 { 16, V4L2_PIX_FMT_YUYV },
60 { 0, V4L2_PIX_FMT_MJPEG },
63 static int
64 uvc_v4l2_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
66 struct video_device *vdev = video_devdata(file);
67 struct uvc_device *uvc = video_get_drvdata(vdev);
68 struct usb_composite_dev *cdev = uvc->func.config->cdev;
70 strlcpy(cap->driver, "g_uvc", sizeof(cap->driver));
71 strlcpy(cap->card, cdev->gadget->name, sizeof(cap->card));
72 strlcpy(cap->bus_info, dev_name(&cdev->gadget->dev),
73 sizeof(cap->bus_info));
75 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
76 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
78 return 0;
81 static int
82 uvc_v4l2_get_format(struct file *file, void *fh, struct v4l2_format *fmt)
84 struct video_device *vdev = video_devdata(file);
85 struct uvc_device *uvc = video_get_drvdata(vdev);
86 struct uvc_video *video = &uvc->video;
88 fmt->fmt.pix.pixelformat = video->fcc;
89 fmt->fmt.pix.width = video->width;
90 fmt->fmt.pix.height = video->height;
91 fmt->fmt.pix.field = V4L2_FIELD_NONE;
92 fmt->fmt.pix.bytesperline = video->bpp * video->width / 8;
93 fmt->fmt.pix.sizeimage = video->imagesize;
94 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
95 fmt->fmt.pix.priv = 0;
97 return 0;
100 static int
101 uvc_v4l2_set_format(struct file *file, void *fh, struct v4l2_format *fmt)
103 struct video_device *vdev = video_devdata(file);
104 struct uvc_device *uvc = video_get_drvdata(vdev);
105 struct uvc_video *video = &uvc->video;
106 struct uvc_format *format;
107 unsigned int imagesize;
108 unsigned int bpl;
109 unsigned int i;
111 for (i = 0; i < ARRAY_SIZE(uvc_formats); ++i) {
112 format = &uvc_formats[i];
113 if (format->fcc == fmt->fmt.pix.pixelformat)
114 break;
117 if (i == ARRAY_SIZE(uvc_formats)) {
118 uvcg_info(&uvc->func, "Unsupported format 0x%08x.\n",
119 fmt->fmt.pix.pixelformat);
120 return -EINVAL;
123 bpl = format->bpp * fmt->fmt.pix.width / 8;
124 imagesize = bpl ? bpl * fmt->fmt.pix.height : fmt->fmt.pix.sizeimage;
126 video->fcc = format->fcc;
127 video->bpp = format->bpp;
128 video->width = fmt->fmt.pix.width;
129 video->height = fmt->fmt.pix.height;
130 video->imagesize = imagesize;
132 fmt->fmt.pix.field = V4L2_FIELD_NONE;
133 fmt->fmt.pix.bytesperline = bpl;
134 fmt->fmt.pix.sizeimage = imagesize;
135 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
136 fmt->fmt.pix.priv = 0;
138 return 0;
141 static int
142 uvc_v4l2_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *b)
144 struct video_device *vdev = video_devdata(file);
145 struct uvc_device *uvc = video_get_drvdata(vdev);
146 struct uvc_video *video = &uvc->video;
148 if (b->type != video->queue.queue.type)
149 return -EINVAL;
151 return uvcg_alloc_buffers(&video->queue, b);
154 static int
155 uvc_v4l2_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
157 struct video_device *vdev = video_devdata(file);
158 struct uvc_device *uvc = video_get_drvdata(vdev);
159 struct uvc_video *video = &uvc->video;
161 return uvcg_query_buffer(&video->queue, b);
164 static int
165 uvc_v4l2_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
167 struct video_device *vdev = video_devdata(file);
168 struct uvc_device *uvc = video_get_drvdata(vdev);
169 struct uvc_video *video = &uvc->video;
170 int ret;
172 ret = uvcg_queue_buffer(&video->queue, b);
173 if (ret < 0)
174 return ret;
176 return uvcg_video_pump(video);
179 static int
180 uvc_v4l2_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
182 struct video_device *vdev = video_devdata(file);
183 struct uvc_device *uvc = video_get_drvdata(vdev);
184 struct uvc_video *video = &uvc->video;
186 return uvcg_dequeue_buffer(&video->queue, b, file->f_flags & O_NONBLOCK);
189 static int
190 uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
192 struct video_device *vdev = video_devdata(file);
193 struct uvc_device *uvc = video_get_drvdata(vdev);
194 struct uvc_video *video = &uvc->video;
195 int ret;
197 if (type != video->queue.queue.type)
198 return -EINVAL;
200 /* Enable UVC video. */
201 ret = uvcg_video_enable(video, 1);
202 if (ret < 0)
203 return ret;
206 * Complete the alternate setting selection setup phase now that
207 * userspace is ready to provide video frames.
209 uvc_function_setup_continue(uvc);
210 uvc->state = UVC_STATE_STREAMING;
212 return 0;
215 static int
216 uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
218 struct video_device *vdev = video_devdata(file);
219 struct uvc_device *uvc = video_get_drvdata(vdev);
220 struct uvc_video *video = &uvc->video;
222 if (type != video->queue.queue.type)
223 return -EINVAL;
225 return uvcg_video_enable(video, 0);
228 static int
229 uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
230 const struct v4l2_event_subscription *sub)
232 if (sub->type < UVC_EVENT_FIRST || sub->type > UVC_EVENT_LAST)
233 return -EINVAL;
235 return v4l2_event_subscribe(fh, sub, 2, NULL);
238 static int
239 uvc_v4l2_unsubscribe_event(struct v4l2_fh *fh,
240 const struct v4l2_event_subscription *sub)
242 return v4l2_event_unsubscribe(fh, sub);
245 static long
246 uvc_v4l2_ioctl_default(struct file *file, void *fh, bool valid_prio,
247 unsigned int cmd, void *arg)
249 struct video_device *vdev = video_devdata(file);
250 struct uvc_device *uvc = video_get_drvdata(vdev);
252 switch (cmd) {
253 case UVCIOC_SEND_RESPONSE:
254 return uvc_send_response(uvc, arg);
256 default:
257 return -ENOIOCTLCMD;
261 const struct v4l2_ioctl_ops uvc_v4l2_ioctl_ops = {
262 .vidioc_querycap = uvc_v4l2_querycap,
263 .vidioc_g_fmt_vid_out = uvc_v4l2_get_format,
264 .vidioc_s_fmt_vid_out = uvc_v4l2_set_format,
265 .vidioc_reqbufs = uvc_v4l2_reqbufs,
266 .vidioc_querybuf = uvc_v4l2_querybuf,
267 .vidioc_qbuf = uvc_v4l2_qbuf,
268 .vidioc_dqbuf = uvc_v4l2_dqbuf,
269 .vidioc_streamon = uvc_v4l2_streamon,
270 .vidioc_streamoff = uvc_v4l2_streamoff,
271 .vidioc_subscribe_event = uvc_v4l2_subscribe_event,
272 .vidioc_unsubscribe_event = uvc_v4l2_unsubscribe_event,
273 .vidioc_default = uvc_v4l2_ioctl_default,
276 /* --------------------------------------------------------------------------
277 * V4L2
280 static int
281 uvc_v4l2_open(struct file *file)
283 struct video_device *vdev = video_devdata(file);
284 struct uvc_device *uvc = video_get_drvdata(vdev);
285 struct uvc_file_handle *handle;
287 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
288 if (handle == NULL)
289 return -ENOMEM;
291 v4l2_fh_init(&handle->vfh, vdev);
292 v4l2_fh_add(&handle->vfh);
294 handle->device = &uvc->video;
295 file->private_data = &handle->vfh;
297 uvc_function_connect(uvc);
298 return 0;
301 static int
302 uvc_v4l2_release(struct file *file)
304 struct video_device *vdev = video_devdata(file);
305 struct uvc_device *uvc = video_get_drvdata(vdev);
306 struct uvc_file_handle *handle = to_uvc_file_handle(file->private_data);
307 struct uvc_video *video = handle->device;
309 uvc_function_disconnect(uvc);
311 mutex_lock(&video->mutex);
312 uvcg_video_enable(video, 0);
313 uvcg_free_buffers(&video->queue);
314 mutex_unlock(&video->mutex);
316 file->private_data = NULL;
317 v4l2_fh_del(&handle->vfh);
318 v4l2_fh_exit(&handle->vfh);
319 kfree(handle);
321 return 0;
324 static int
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 uvcg_queue_mmap(&uvc->video.queue, vma);
333 static __poll_t
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);
339 return uvcg_queue_poll(&uvc->video.queue, file, wait);
342 #ifndef CONFIG_MMU
343 static unsigned long uvcg_v4l2_get_unmapped_area(struct file *file,
344 unsigned long addr, unsigned long len, unsigned long pgoff,
345 unsigned long flags)
347 struct video_device *vdev = video_devdata(file);
348 struct uvc_device *uvc = video_get_drvdata(vdev);
350 return uvcg_queue_get_unmapped_area(&uvc->video.queue, pgoff);
352 #endif
354 const struct v4l2_file_operations uvc_v4l2_fops = {
355 .owner = THIS_MODULE,
356 .open = uvc_v4l2_open,
357 .release = uvc_v4l2_release,
358 .unlocked_ioctl = video_ioctl2,
359 .mmap = uvc_v4l2_mmap,
360 .poll = uvc_v4l2_poll,
361 #ifndef CONFIG_MMU
362 .get_unmapped_area = uvcg_v4l2_get_unmapped_area,
363 #endif