xref: /OK3568_Linux_fs/kernel/drivers/usb/gadget/function/uvc_v4l2.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *	uvc_v4l2.c  --  USB Video Class Gadget driver
4  *
5  *	Copyright (C) 2009-2010
6  *	    Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7  */
8 
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>
17 
18 #include <media/v4l2-dev.h>
19 #include <media/v4l2-event.h>
20 #include <media/v4l2-ioctl.h>
21 
22 #include "f_uvc.h"
23 #include "uvc.h"
24 #include "uvc_queue.h"
25 #include "uvc_video.h"
26 #include "uvc_v4l2.h"
27 
28 /* --------------------------------------------------------------------------
29  * Requests handling
30  */
31 
32 static int
uvc_send_response(struct uvc_device * uvc,struct uvc_request_data * data)33 uvc_send_response(struct uvc_device *uvc, struct uvc_request_data *data)
34 {
35 	struct usb_composite_dev *cdev = uvc->func.config->cdev;
36 	struct usb_request *req = uvc->control_req;
37 
38 	if (data->length < 0)
39 		return usb_ep_set_halt(cdev->gadget->ep0);
40 
41 	req->length = min_t(unsigned int, uvc->event_length, data->length);
42 	req->zero = data->length < uvc->event_length;
43 
44 	uvc_trace(UVC_TRACE_CONTROL, "%s: req len %d\n", __func__, req->length);
45 	memcpy(req->buf, data->data, req->length);
46 
47 	return usb_ep_queue(cdev->gadget->ep0, req, GFP_KERNEL);
48 }
49 
50 /* --------------------------------------------------------------------------
51  * V4L2 ioctls
52  */
53 
54 struct uvc_format {
55 	u8 bpp;
56 	u32 fcc;
57 };
58 
59 static struct uvc_format uvc_formats[] = {
60 	{ 16, V4L2_PIX_FMT_YUYV  },
61 	{ 12, V4L2_PIX_FMT_NV12  },
62 	{ 0,  V4L2_PIX_FMT_MJPEG },
63 	{ 0,  V4L2_PIX_FMT_H264  },
64 	{ 0,  V4L2_PIX_FMT_H265  },
65 };
66 
67 static int
uvc_v4l2_querycap(struct file * file,void * fh,struct v4l2_capability * cap)68 uvc_v4l2_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
69 {
70 	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 	return 0;
79 }
80 
81 static int
uvc_v4l2_get_format(struct file * file,void * fh,struct v4l2_format * fmt)82 uvc_v4l2_get_format(struct file *file, void *fh, struct v4l2_format *fmt)
83 {
84 	struct video_device *vdev = video_devdata(file);
85 	struct uvc_device *uvc = video_get_drvdata(vdev);
86 	struct uvc_video *video = &uvc->video;
87 
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;
96 
97 	return 0;
98 }
99 
100 static int
uvc_v4l2_set_format(struct file * file,void * fh,struct v4l2_format * fmt)101 uvc_v4l2_set_format(struct file *file, void *fh, struct v4l2_format *fmt)
102 {
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;
110 
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;
115 	}
116 
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;
121 	}
122 
123 	bpl = format->bpp * fmt->fmt.pix.width / 8;
124 	imagesize = bpl ? bpl * fmt->fmt.pix.height : fmt->fmt.pix.sizeimage;
125 
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;
131 
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;
137 
138 	return 0;
139 }
140 
141 static int
uvc_v4l2_reqbufs(struct file * file,void * fh,struct v4l2_requestbuffers * b)142 uvc_v4l2_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *b)
143 {
144 	struct video_device *vdev = video_devdata(file);
145 	struct uvc_device *uvc = video_get_drvdata(vdev);
146 	struct uvc_video *video = &uvc->video;
147 
148 	if (b->type != video->queue.queue.type)
149 		return -EINVAL;
150 
151 	return uvcg_alloc_buffers(&video->queue, b);
152 }
153 
154 static int
uvc_v4l2_querybuf(struct file * file,void * fh,struct v4l2_buffer * b)155 uvc_v4l2_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
156 {
157 	struct video_device *vdev = video_devdata(file);
158 	struct uvc_device *uvc = video_get_drvdata(vdev);
159 	struct uvc_video *video = &uvc->video;
160 
161 	return uvcg_query_buffer(&video->queue, b);
162 }
163 
164 static int
uvc_v4l2_qbuf(struct file * file,void * fh,struct v4l2_buffer * b)165 uvc_v4l2_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
166 {
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;
171 
172 	ret = uvcg_queue_buffer(&video->queue, b);
173 	if (ret < 0)
174 		return ret;
175 
176 	if (uvc->state == UVC_STATE_STREAMING)
177 		schedule_work(&video->pump);
178 
179 	return ret;
180 }
181 
182 static int
uvc_v4l2_dqbuf(struct file * file,void * fh,struct v4l2_buffer * b)183 uvc_v4l2_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
184 {
185 	struct video_device *vdev = video_devdata(file);
186 	struct uvc_device *uvc = video_get_drvdata(vdev);
187 	struct uvc_video *video = &uvc->video;
188 
189 	return uvcg_dequeue_buffer(&video->queue, b, file->f_flags & O_NONBLOCK);
190 }
191 
192 static int
uvc_v4l2_streamon(struct file * file,void * fh,enum v4l2_buf_type type)193 uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
194 {
195 	struct video_device *vdev = video_devdata(file);
196 	struct uvc_device *uvc = video_get_drvdata(vdev);
197 	struct uvc_video *video = &uvc->video;
198 	int ret;
199 
200 	if (type != video->queue.queue.type)
201 		return -EINVAL;
202 
203 	if (uvc->state == UVC_STATE_DISCONNECTED)
204 		return -ENODEV;
205 
206 	/* Enable UVC video. */
207 	ret = uvcg_video_enable(video, 1);
208 	if (ret < 0)
209 		return ret;
210 
211 	/*
212 	 * Alt settings in an interface are supported only
213 	 * for ISOC endpoints as there are different alt-
214 	 * settings for zero-bandwidth and full-bandwidth
215 	 * cases, but the same is not true for BULK endpoints,
216 	 * as they have a single alt-setting.
217 	 *
218 	 * For ISOC endpoints, Complete the alternate setting
219 	 * selection setup phase now that userspace is ready
220 	 * to provide video frames.
221 	 */
222 	if (!usb_endpoint_xfer_bulk(video->ep->desc))
223 		uvc_function_setup_continue(uvc);
224 
225 	uvc->state = UVC_STATE_STREAMING;
226 
227 	return 0;
228 }
229 
230 static int
uvc_v4l2_streamoff(struct file * file,void * fh,enum v4l2_buf_type type)231 uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
232 {
233 	struct video_device *vdev = video_devdata(file);
234 	struct uvc_device *uvc = video_get_drvdata(vdev);
235 	struct uvc_video *video = &uvc->video;
236 
237 	if (type != video->queue.queue.type)
238 		return -EINVAL;
239 
240 	return uvcg_video_enable(video, 0);
241 }
242 
243 static int
uvc_v4l2_subscribe_event(struct v4l2_fh * fh,const struct v4l2_event_subscription * sub)244 uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
245 			 const struct v4l2_event_subscription *sub)
246 {
247 	struct uvc_device *uvc = video_get_drvdata(fh->vdev);
248 	struct uvc_file_handle *handle = to_uvc_file_handle(fh);
249 	int ret;
250 
251 	if (sub->type < UVC_EVENT_FIRST || sub->type > UVC_EVENT_LAST)
252 		return -EINVAL;
253 
254 	if (sub->type == UVC_EVENT_SETUP && uvc->func_connected)
255 		return -EBUSY;
256 
257 	ret = v4l2_event_subscribe(fh, sub, 2, NULL);
258 	if (ret < 0)
259 		return ret;
260 
261 	if (sub->type == UVC_EVENT_SETUP) {
262 		uvc->func_connected = true;
263 		handle->is_uvc_app_handle = true;
264 		uvc_function_connect(uvc);
265 	}
266 
267 	return 0;
268 }
269 
uvc_v4l2_disable(struct uvc_device * uvc)270 static void uvc_v4l2_disable(struct uvc_device *uvc)
271 {
272 	uvc_function_disconnect(uvc);
273 	uvcg_video_enable(&uvc->video, 0);
274 	uvcg_free_buffers(&uvc->video.queue);
275 	uvc->func_connected = false;
276 	wake_up_interruptible(&uvc->func_connected_queue);
277 }
278 
279 static int
uvc_v4l2_unsubscribe_event(struct v4l2_fh * fh,const struct v4l2_event_subscription * sub)280 uvc_v4l2_unsubscribe_event(struct v4l2_fh *fh,
281 			   const struct v4l2_event_subscription *sub)
282 {
283 	struct uvc_device *uvc = video_get_drvdata(fh->vdev);
284 	struct uvc_file_handle *handle = to_uvc_file_handle(fh);
285 	int ret;
286 
287 	ret = v4l2_event_unsubscribe(fh, sub);
288 	if (ret < 0)
289 		return ret;
290 
291 	if (sub->type == UVC_EVENT_SETUP && handle->is_uvc_app_handle) {
292 		uvc_v4l2_disable(uvc);
293 		handle->is_uvc_app_handle = false;
294 	}
295 
296 	return 0;
297 }
298 
299 static long
uvc_v4l2_ioctl_default(struct file * file,void * fh,bool valid_prio,unsigned int cmd,void * arg)300 uvc_v4l2_ioctl_default(struct file *file, void *fh, bool valid_prio,
301 		       unsigned int cmd, void *arg)
302 {
303 	struct video_device *vdev = video_devdata(file);
304 	struct uvc_device *uvc = video_get_drvdata(vdev);
305 
306 	switch (cmd) {
307 	case UVCIOC_SEND_RESPONSE:
308 		return uvc_send_response(uvc, arg);
309 
310 	default:
311 		return -ENOIOCTLCMD;
312 	}
313 }
314 
315 const struct v4l2_ioctl_ops uvc_v4l2_ioctl_ops = {
316 	.vidioc_querycap = uvc_v4l2_querycap,
317 	.vidioc_g_fmt_vid_out = uvc_v4l2_get_format,
318 	.vidioc_s_fmt_vid_out = uvc_v4l2_set_format,
319 	.vidioc_reqbufs = uvc_v4l2_reqbufs,
320 	.vidioc_querybuf = uvc_v4l2_querybuf,
321 	.vidioc_qbuf = uvc_v4l2_qbuf,
322 	.vidioc_dqbuf = uvc_v4l2_dqbuf,
323 	.vidioc_streamon = uvc_v4l2_streamon,
324 	.vidioc_streamoff = uvc_v4l2_streamoff,
325 	.vidioc_subscribe_event = uvc_v4l2_subscribe_event,
326 	.vidioc_unsubscribe_event = uvc_v4l2_unsubscribe_event,
327 	.vidioc_default = uvc_v4l2_ioctl_default,
328 };
329 
330 /* --------------------------------------------------------------------------
331  * V4L2
332  */
333 
334 static int
uvc_v4l2_open(struct file * file)335 uvc_v4l2_open(struct file *file)
336 {
337 	struct video_device *vdev = video_devdata(file);
338 	struct uvc_device *uvc = video_get_drvdata(vdev);
339 	struct uvc_file_handle *handle;
340 
341 	handle = kzalloc(sizeof(*handle), GFP_KERNEL);
342 	if (handle == NULL)
343 		return -ENOMEM;
344 
345 	v4l2_fh_init(&handle->vfh, vdev);
346 	v4l2_fh_add(&handle->vfh);
347 
348 	handle->device = &uvc->video;
349 	file->private_data = &handle->vfh;
350 
351 	return 0;
352 }
353 
354 static int
uvc_v4l2_release(struct file * file)355 uvc_v4l2_release(struct file *file)
356 {
357 	struct video_device *vdev = video_devdata(file);
358 	struct uvc_device *uvc = video_get_drvdata(vdev);
359 	struct uvc_file_handle *handle = to_uvc_file_handle(file->private_data);
360 	struct uvc_video *video = handle->device;
361 
362 	mutex_lock(&video->mutex);
363 	if (handle->is_uvc_app_handle)
364 		uvc_v4l2_disable(uvc);
365 	mutex_unlock(&video->mutex);
366 
367 	file->private_data = NULL;
368 	v4l2_fh_del(&handle->vfh);
369 	v4l2_fh_exit(&handle->vfh);
370 	kfree(handle);
371 
372 	return 0;
373 }
374 
375 static int
uvc_v4l2_mmap(struct file * file,struct vm_area_struct * vma)376 uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
377 {
378 	struct video_device *vdev = video_devdata(file);
379 	struct uvc_device *uvc = video_get_drvdata(vdev);
380 
381 	return uvcg_queue_mmap(&uvc->video.queue, vma);
382 }
383 
384 static __poll_t
uvc_v4l2_poll(struct file * file,poll_table * wait)385 uvc_v4l2_poll(struct file *file, poll_table *wait)
386 {
387 	struct video_device *vdev = video_devdata(file);
388 	struct uvc_device *uvc = video_get_drvdata(vdev);
389 
390 	return uvcg_queue_poll(&uvc->video.queue, file, wait);
391 }
392 
393 #ifndef CONFIG_MMU
uvcg_v4l2_get_unmapped_area(struct file * file,unsigned long addr,unsigned long len,unsigned long pgoff,unsigned long flags)394 static unsigned long uvcg_v4l2_get_unmapped_area(struct file *file,
395 		unsigned long addr, unsigned long len, unsigned long pgoff,
396 		unsigned long flags)
397 {
398 	struct video_device *vdev = video_devdata(file);
399 	struct uvc_device *uvc = video_get_drvdata(vdev);
400 
401 	return uvcg_queue_get_unmapped_area(&uvc->video.queue, pgoff);
402 }
403 #endif
404 
405 const struct v4l2_file_operations uvc_v4l2_fops = {
406 	.owner		= THIS_MODULE,
407 	.open		= uvc_v4l2_open,
408 	.release	= uvc_v4l2_release,
409 	.unlocked_ioctl	= video_ioctl2,
410 #ifdef CONFIG_COMPAT
411 	.compat_ioctl32	= video_ioctl2,
412 #endif
413 	.mmap		= uvc_v4l2_mmap,
414 	.poll		= uvc_v4l2_poll,
415 #ifndef CONFIG_MMU
416 	.get_unmapped_area = uvcg_v4l2_get_unmapped_area,
417 #endif
418 };
419 
420