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/usb/uvc.h>
15 #include <linux/videodev2.h>
16 #include <linux/vmalloc.h>
17 #include <linux/wait.h>
18
19 #include <media/v4l2-dev.h>
20 #include <media/v4l2-event.h>
21 #include <media/v4l2-ioctl.h>
22
23 #include "f_uvc.h"
24 #include "uvc.h"
25 #include "uvc_queue.h"
26 #include "uvc_video.h"
27 #include "uvc_v4l2.h"
28 #include "uvc_configfs.h"
29
to_uvc_format(struct uvcg_format * uformat)30 static const struct uvc_format_desc *to_uvc_format(struct uvcg_format *uformat)
31 {
32 char guid[16] = UVC_GUID_FORMAT_MJPEG;
33 const struct uvc_format_desc *format;
34 struct uvcg_uncompressed *unc;
35
36 if (uformat->type == UVCG_UNCOMPRESSED) {
37 unc = to_uvcg_uncompressed(&uformat->group.cg_item);
38 if (!unc)
39 return ERR_PTR(-EINVAL);
40
41 memcpy(guid, unc->desc.guidFormat, sizeof(guid));
42 }
43
44 format = uvc_format_by_guid(guid);
45 if (!format)
46 return ERR_PTR(-EINVAL);
47
48 return format;
49 }
50
uvc_v4l2_get_bytesperline(struct uvcg_format * uformat,struct uvcg_frame * uframe)51 static int uvc_v4l2_get_bytesperline(struct uvcg_format *uformat,
52 struct uvcg_frame *uframe)
53 {
54 struct uvcg_uncompressed *u;
55
56 if (uformat->type == UVCG_UNCOMPRESSED) {
57 u = to_uvcg_uncompressed(&uformat->group.cg_item);
58 if (!u)
59 return 0;
60
61 return u->desc.bBitsPerPixel * uframe->frame.w_width / 8;
62 }
63
64 return 0;
65 }
66
uvc_get_frame_size(struct uvcg_format * uformat,struct uvcg_frame * uframe)67 static int uvc_get_frame_size(struct uvcg_format *uformat,
68 struct uvcg_frame *uframe)
69 {
70 unsigned int bpl = uvc_v4l2_get_bytesperline(uformat, uframe);
71
72 return bpl ? bpl * uframe->frame.w_height :
73 uframe->frame.dw_max_video_frame_buffer_size;
74 }
75
find_format_by_index(struct uvc_device * uvc,int index)76 static struct uvcg_format *find_format_by_index(struct uvc_device *uvc, int index)
77 {
78 struct uvcg_format_ptr *format;
79 struct uvcg_format *uformat = NULL;
80 int i = 1;
81
82 list_for_each_entry(format, &uvc->header->formats, entry) {
83 if (index == i) {
84 uformat = format->fmt;
85 break;
86 }
87 i++;
88 }
89
90 return uformat;
91 }
92
find_frame_by_index(struct uvc_device * uvc,struct uvcg_format * uformat,int index)93 static struct uvcg_frame *find_frame_by_index(struct uvc_device *uvc,
94 struct uvcg_format *uformat,
95 int index)
96 {
97 struct uvcg_format_ptr *format;
98 struct uvcg_frame_ptr *frame;
99 struct uvcg_frame *uframe = NULL;
100
101 list_for_each_entry(format, &uvc->header->formats, entry) {
102 if (format->fmt->type != uformat->type)
103 continue;
104 list_for_each_entry(frame, &format->fmt->frames, entry) {
105 if (index == frame->frm->frame.b_frame_index) {
106 uframe = frame->frm;
107 break;
108 }
109 }
110 }
111
112 return uframe;
113 }
114
find_format_by_pix(struct uvc_device * uvc,u32 pixelformat)115 static struct uvcg_format *find_format_by_pix(struct uvc_device *uvc,
116 u32 pixelformat)
117 {
118 struct uvcg_format_ptr *format;
119 struct uvcg_format *uformat = NULL;
120
121 list_for_each_entry(format, &uvc->header->formats, entry) {
122 const struct uvc_format_desc *fmtdesc = to_uvc_format(format->fmt);
123
124 if (IS_ERR(fmtdesc))
125 continue;
126
127 if (fmtdesc->fcc == pixelformat) {
128 uformat = format->fmt;
129 break;
130 }
131 }
132
133 return uformat;
134 }
135
find_closest_frame_by_size(struct uvc_device * uvc,struct uvcg_format * uformat,u16 rw,u16 rh)136 static struct uvcg_frame *find_closest_frame_by_size(struct uvc_device *uvc,
137 struct uvcg_format *uformat,
138 u16 rw, u16 rh)
139 {
140 struct uvc_video *video = &uvc->video;
141 struct uvcg_format_ptr *format;
142 struct uvcg_frame_ptr *frame;
143 struct uvcg_frame *uframe = NULL;
144 unsigned int d, maxd;
145
146 /* Find the closest image size. The distance between image sizes is
147 * the size in pixels of the non-overlapping regions between the
148 * requested size and the frame-specified size.
149 */
150 maxd = (unsigned int)-1;
151
152 list_for_each_entry(format, &uvc->header->formats, entry) {
153 if (format->fmt->type != uformat->type)
154 continue;
155
156 list_for_each_entry(frame, &format->fmt->frames, entry) {
157 u16 w, h;
158
159 w = frame->frm->frame.w_width;
160 h = frame->frm->frame.w_height;
161
162 d = min(w, rw) * min(h, rh);
163 d = w*h + rw*rh - 2*d;
164 if (d < maxd) {
165 maxd = d;
166 uframe = frame->frm;
167 }
168
169 if (maxd == 0)
170 break;
171 }
172 }
173
174 if (!uframe)
175 uvcg_dbg(&video->uvc->func, "Unsupported size %ux%u\n", rw, rh);
176
177 return uframe;
178 }
179
180 /* --------------------------------------------------------------------------
181 * Requests handling
182 */
183
184 static int
uvc_send_response(struct uvc_device * uvc,struct uvc_request_data * data)185 uvc_send_response(struct uvc_device *uvc, struct uvc_request_data *data)
186 {
187 struct usb_composite_dev *cdev = uvc->func.config->cdev;
188 struct usb_request *req = uvc->control_req;
189
190 if (data->length < 0)
191 return usb_ep_set_halt(cdev->gadget->ep0);
192
193 req->length = min_t(unsigned int, uvc->event_length, data->length);
194 req->zero = data->length < uvc->event_length;
195
196 memcpy(req->buf, data->data, req->length);
197
198 return usb_ep_queue(cdev->gadget->ep0, req, GFP_KERNEL);
199 }
200
201 /* --------------------------------------------------------------------------
202 * V4L2 ioctls
203 */
204
205 static int
uvc_v4l2_querycap(struct file * file,void * fh,struct v4l2_capability * cap)206 uvc_v4l2_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
207 {
208 struct video_device *vdev = video_devdata(file);
209 struct uvc_device *uvc = video_get_drvdata(vdev);
210 struct usb_composite_dev *cdev = uvc->func.config->cdev;
211
212 strscpy(cap->driver, "g_uvc", sizeof(cap->driver));
213 strscpy(cap->card, cdev->gadget->name, sizeof(cap->card));
214 strscpy(cap->bus_info, dev_name(&cdev->gadget->dev),
215 sizeof(cap->bus_info));
216 return 0;
217 }
218
219 static int
uvc_v4l2_get_format(struct file * file,void * fh,struct v4l2_format * fmt)220 uvc_v4l2_get_format(struct file *file, void *fh, struct v4l2_format *fmt)
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 fmt->fmt.pix.pixelformat = video->fcc;
227 fmt->fmt.pix.width = video->width;
228 fmt->fmt.pix.height = video->height;
229 fmt->fmt.pix.field = V4L2_FIELD_NONE;
230 fmt->fmt.pix.bytesperline = video->bpp * video->width / 8;
231 fmt->fmt.pix.sizeimage = video->imagesize;
232 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
233 fmt->fmt.pix.priv = 0;
234
235 return 0;
236 }
237
238 static int
uvc_v4l2_try_format(struct file * file,void * fh,struct v4l2_format * fmt)239 uvc_v4l2_try_format(struct file *file, void *fh, struct v4l2_format *fmt)
240 {
241 struct video_device *vdev = video_devdata(file);
242 struct uvc_device *uvc = video_get_drvdata(vdev);
243 struct uvc_video *video = &uvc->video;
244 struct uvcg_format *uformat;
245 struct uvcg_frame *uframe;
246 const struct uvc_format_desc *fmtdesc;
247 u8 *fcc;
248
249 if (fmt->type != video->queue.queue.type)
250 return -EINVAL;
251
252 fcc = (u8 *)&fmt->fmt.pix.pixelformat;
253 uvcg_dbg(&uvc->func, "Trying format 0x%08x (%c%c%c%c): %ux%u\n",
254 fmt->fmt.pix.pixelformat,
255 fcc[0], fcc[1], fcc[2], fcc[3],
256 fmt->fmt.pix.width, fmt->fmt.pix.height);
257
258 uformat = find_format_by_pix(uvc, fmt->fmt.pix.pixelformat);
259 if (!uformat)
260 return -EINVAL;
261
262 uframe = find_closest_frame_by_size(uvc, uformat,
263 fmt->fmt.pix.width, fmt->fmt.pix.height);
264 if (!uframe)
265 return -EINVAL;
266
267 if (uformat->type == UVCG_UNCOMPRESSED) {
268 struct uvcg_uncompressed *u =
269 to_uvcg_uncompressed(&uformat->group.cg_item);
270 if (!u)
271 return 0;
272
273 v4l2_fill_pixfmt(&fmt->fmt.pix, fmt->fmt.pix.pixelformat,
274 uframe->frame.w_width, uframe->frame.w_height);
275
276 if (fmt->fmt.pix.sizeimage != (uvc_v4l2_get_bytesperline(uformat, uframe) *
277 uframe->frame.w_height))
278 return -EINVAL;
279 } else {
280 fmt->fmt.pix.width = uframe->frame.w_width;
281 fmt->fmt.pix.height = uframe->frame.w_height;
282 fmt->fmt.pix.bytesperline = uvc_v4l2_get_bytesperline(uformat, uframe);
283 fmt->fmt.pix.sizeimage = uvc_get_frame_size(uformat, uframe);
284 fmtdesc = to_uvc_format(uformat);
285 if (IS_ERR(fmtdesc))
286 return PTR_ERR(fmtdesc);
287 fmt->fmt.pix.pixelformat = fmtdesc->fcc;
288 }
289 fmt->fmt.pix.field = V4L2_FIELD_NONE;
290 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
291 fmt->fmt.pix.priv = 0;
292
293 return 0;
294 }
295
296 static int
uvc_v4l2_set_format(struct file * file,void * fh,struct v4l2_format * fmt)297 uvc_v4l2_set_format(struct file *file, void *fh, struct v4l2_format *fmt)
298 {
299 struct video_device *vdev = video_devdata(file);
300 struct uvc_device *uvc = video_get_drvdata(vdev);
301 struct uvc_video *video = &uvc->video;
302 int ret;
303
304 ret = uvc_v4l2_try_format(file, fh, fmt);
305 if (ret)
306 return ret;
307
308 video->fcc = fmt->fmt.pix.pixelformat;
309 video->bpp = fmt->fmt.pix.bytesperline * 8 / video->width;
310 video->width = fmt->fmt.pix.width;
311 video->height = fmt->fmt.pix.height;
312 video->imagesize = fmt->fmt.pix.sizeimage;
313
314 return ret;
315 }
316
317 static int
uvc_v4l2_enum_frameintervals(struct file * file,void * fh,struct v4l2_frmivalenum * fival)318 uvc_v4l2_enum_frameintervals(struct file *file, void *fh,
319 struct v4l2_frmivalenum *fival)
320 {
321 struct video_device *vdev = video_devdata(file);
322 struct uvc_device *uvc = video_get_drvdata(vdev);
323 struct uvcg_format *uformat = NULL;
324 struct uvcg_frame *uframe = NULL;
325 struct uvcg_frame_ptr *frame;
326
327 uformat = find_format_by_pix(uvc, fival->pixel_format);
328 if (!uformat)
329 return -EINVAL;
330
331 list_for_each_entry(frame, &uformat->frames, entry) {
332 if (frame->frm->frame.w_width == fival->width &&
333 frame->frm->frame.w_height == fival->height) {
334 uframe = frame->frm;
335 break;
336 }
337 }
338 if (!uframe)
339 return -EINVAL;
340
341 if (fival->index >= uframe->frame.b_frame_interval_type)
342 return -EINVAL;
343
344 fival->discrete.numerator =
345 uframe->dw_frame_interval[fival->index];
346
347 /* TODO: handle V4L2_FRMIVAL_TYPE_STEPWISE */
348 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
349 fival->discrete.denominator = 10000000;
350 v4l2_simplify_fraction(&fival->discrete.numerator,
351 &fival->discrete.denominator, 8, 333);
352
353 return 0;
354 }
355
356 static int
uvc_v4l2_enum_framesizes(struct file * file,void * fh,struct v4l2_frmsizeenum * fsize)357 uvc_v4l2_enum_framesizes(struct file *file, void *fh,
358 struct v4l2_frmsizeenum *fsize)
359 {
360 struct video_device *vdev = video_devdata(file);
361 struct uvc_device *uvc = video_get_drvdata(vdev);
362 struct uvcg_format *uformat = NULL;
363 struct uvcg_frame *uframe = NULL;
364
365 uformat = find_format_by_pix(uvc, fsize->pixel_format);
366 if (!uformat)
367 return -EINVAL;
368
369 if (fsize->index >= uformat->num_frames)
370 return -EINVAL;
371
372 uframe = find_frame_by_index(uvc, uformat, fsize->index + 1);
373 if (!uframe)
374 return -EINVAL;
375
376 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
377 fsize->discrete.width = uframe->frame.w_width;
378 fsize->discrete.height = uframe->frame.w_height;
379
380 return 0;
381 }
382
383 static int
uvc_v4l2_enum_format(struct file * file,void * fh,struct v4l2_fmtdesc * f)384 uvc_v4l2_enum_format(struct file *file, void *fh, struct v4l2_fmtdesc *f)
385 {
386 struct video_device *vdev = video_devdata(file);
387 struct uvc_device *uvc = video_get_drvdata(vdev);
388 const struct uvc_format_desc *fmtdesc;
389 struct uvcg_format *uformat;
390
391 if (f->index >= uvc->header->num_fmt)
392 return -EINVAL;
393
394 uformat = find_format_by_index(uvc, f->index + 1);
395 if (!uformat)
396 return -EINVAL;
397
398 fmtdesc = to_uvc_format(uformat);
399 if (IS_ERR(fmtdesc))
400 return PTR_ERR(fmtdesc);
401
402 f->pixelformat = fmtdesc->fcc;
403
404 return 0;
405 }
406
407 static int
uvc_v4l2_reqbufs(struct file * file,void * fh,struct v4l2_requestbuffers * b)408 uvc_v4l2_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *b)
409 {
410 struct video_device *vdev = video_devdata(file);
411 struct uvc_device *uvc = video_get_drvdata(vdev);
412 struct uvc_video *video = &uvc->video;
413
414 if (b->type != video->queue.queue.type)
415 return -EINVAL;
416
417 return uvcg_alloc_buffers(&video->queue, b);
418 }
419
420 static int
uvc_v4l2_querybuf(struct file * file,void * fh,struct v4l2_buffer * b)421 uvc_v4l2_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
422 {
423 struct video_device *vdev = video_devdata(file);
424 struct uvc_device *uvc = video_get_drvdata(vdev);
425 struct uvc_video *video = &uvc->video;
426
427 return uvcg_query_buffer(&video->queue, b);
428 }
429
430 static int
uvc_v4l2_qbuf(struct file * file,void * fh,struct v4l2_buffer * b)431 uvc_v4l2_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
432 {
433 struct video_device *vdev = video_devdata(file);
434 struct uvc_device *uvc = video_get_drvdata(vdev);
435 struct uvc_video *video = &uvc->video;
436 int ret;
437
438 ret = uvcg_queue_buffer(&video->queue, b);
439 if (ret < 0)
440 return ret;
441
442 if (uvc->state == UVC_STATE_STREAMING)
443 queue_work(video->async_wq, &video->pump);
444
445 return ret;
446 }
447
448 static int
uvc_v4l2_dqbuf(struct file * file,void * fh,struct v4l2_buffer * b)449 uvc_v4l2_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
450 {
451 struct video_device *vdev = video_devdata(file);
452 struct uvc_device *uvc = video_get_drvdata(vdev);
453 struct uvc_video *video = &uvc->video;
454
455 return uvcg_dequeue_buffer(&video->queue, b, file->f_flags & O_NONBLOCK);
456 }
457
458 static int
uvc_v4l2_streamon(struct file * file,void * fh,enum v4l2_buf_type type)459 uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
460 {
461 struct video_device *vdev = video_devdata(file);
462 struct uvc_device *uvc = video_get_drvdata(vdev);
463 struct uvc_video *video = &uvc->video;
464 int ret;
465
466 if (type != video->queue.queue.type)
467 return -EINVAL;
468
469 /* Enable UVC video. */
470 ret = uvcg_video_enable(video, 1);
471 if (ret < 0)
472 return ret;
473
474 /*
475 * Complete the alternate setting selection setup phase now that
476 * userspace is ready to provide video frames.
477 */
478 uvc_function_setup_continue(uvc);
479 uvc->state = UVC_STATE_STREAMING;
480
481 return 0;
482 }
483
484 static int
uvc_v4l2_streamoff(struct file * file,void * fh,enum v4l2_buf_type type)485 uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
486 {
487 struct video_device *vdev = video_devdata(file);
488 struct uvc_device *uvc = video_get_drvdata(vdev);
489 struct uvc_video *video = &uvc->video;
490
491 if (type != video->queue.queue.type)
492 return -EINVAL;
493
494 return uvcg_video_enable(video, 0);
495 }
496
497 static int
uvc_v4l2_subscribe_event(struct v4l2_fh * fh,const struct v4l2_event_subscription * sub)498 uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
499 const struct v4l2_event_subscription *sub)
500 {
501 struct uvc_device *uvc = video_get_drvdata(fh->vdev);
502 struct uvc_file_handle *handle = to_uvc_file_handle(fh);
503 int ret;
504
505 if (sub->type < UVC_EVENT_FIRST || sub->type > UVC_EVENT_LAST)
506 return -EINVAL;
507
508 if (sub->type == UVC_EVENT_SETUP && uvc->func_connected)
509 return -EBUSY;
510
511 ret = v4l2_event_subscribe(fh, sub, 2, NULL);
512 if (ret < 0)
513 return ret;
514
515 if (sub->type == UVC_EVENT_SETUP) {
516 uvc->func_connected = true;
517 handle->is_uvc_app_handle = true;
518 uvc_function_connect(uvc);
519 }
520
521 return 0;
522 }
523
uvc_v4l2_disable(struct uvc_device * uvc)524 static void uvc_v4l2_disable(struct uvc_device *uvc)
525 {
526 uvc_function_disconnect(uvc);
527 uvcg_video_enable(&uvc->video, 0);
528 uvcg_free_buffers(&uvc->video.queue);
529 uvc->func_connected = false;
530 wake_up_interruptible(&uvc->func_connected_queue);
531 }
532
533 static int
uvc_v4l2_unsubscribe_event(struct v4l2_fh * fh,const struct v4l2_event_subscription * sub)534 uvc_v4l2_unsubscribe_event(struct v4l2_fh *fh,
535 const struct v4l2_event_subscription *sub)
536 {
537 struct uvc_device *uvc = video_get_drvdata(fh->vdev);
538 struct uvc_file_handle *handle = to_uvc_file_handle(fh);
539 int ret;
540
541 ret = v4l2_event_unsubscribe(fh, sub);
542 if (ret < 0)
543 return ret;
544
545 if (sub->type == UVC_EVENT_SETUP && handle->is_uvc_app_handle) {
546 uvc_v4l2_disable(uvc);
547 handle->is_uvc_app_handle = false;
548 }
549
550 return 0;
551 }
552
553 static long
uvc_v4l2_ioctl_default(struct file * file,void * fh,bool valid_prio,unsigned int cmd,void * arg)554 uvc_v4l2_ioctl_default(struct file *file, void *fh, bool valid_prio,
555 unsigned int cmd, void *arg)
556 {
557 struct video_device *vdev = video_devdata(file);
558 struct uvc_device *uvc = video_get_drvdata(vdev);
559
560 switch (cmd) {
561 case UVCIOC_SEND_RESPONSE:
562 return uvc_send_response(uvc, arg);
563
564 default:
565 return -ENOIOCTLCMD;
566 }
567 }
568
569 const struct v4l2_ioctl_ops uvc_v4l2_ioctl_ops = {
570 .vidioc_querycap = uvc_v4l2_querycap,
571 .vidioc_try_fmt_vid_out = uvc_v4l2_try_format,
572 .vidioc_g_fmt_vid_out = uvc_v4l2_get_format,
573 .vidioc_s_fmt_vid_out = uvc_v4l2_set_format,
574 .vidioc_enum_frameintervals = uvc_v4l2_enum_frameintervals,
575 .vidioc_enum_framesizes = uvc_v4l2_enum_framesizes,
576 .vidioc_enum_fmt_vid_out = uvc_v4l2_enum_format,
577 .vidioc_reqbufs = uvc_v4l2_reqbufs,
578 .vidioc_querybuf = uvc_v4l2_querybuf,
579 .vidioc_qbuf = uvc_v4l2_qbuf,
580 .vidioc_dqbuf = uvc_v4l2_dqbuf,
581 .vidioc_streamon = uvc_v4l2_streamon,
582 .vidioc_streamoff = uvc_v4l2_streamoff,
583 .vidioc_subscribe_event = uvc_v4l2_subscribe_event,
584 .vidioc_unsubscribe_event = uvc_v4l2_unsubscribe_event,
585 .vidioc_default = uvc_v4l2_ioctl_default,
586 };
587
588 /* --------------------------------------------------------------------------
589 * V4L2
590 */
591
592 static int
uvc_v4l2_open(struct file * file)593 uvc_v4l2_open(struct file *file)
594 {
595 struct video_device *vdev = video_devdata(file);
596 struct uvc_device *uvc = video_get_drvdata(vdev);
597 struct uvc_file_handle *handle;
598
599 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
600 if (handle == NULL)
601 return -ENOMEM;
602
603 v4l2_fh_init(&handle->vfh, vdev);
604 v4l2_fh_add(&handle->vfh);
605
606 handle->device = &uvc->video;
607 file->private_data = &handle->vfh;
608
609 return 0;
610 }
611
612 static int
uvc_v4l2_release(struct file * file)613 uvc_v4l2_release(struct file *file)
614 {
615 struct video_device *vdev = video_devdata(file);
616 struct uvc_device *uvc = video_get_drvdata(vdev);
617 struct uvc_file_handle *handle = to_uvc_file_handle(file->private_data);
618 struct uvc_video *video = handle->device;
619
620 mutex_lock(&video->mutex);
621 if (handle->is_uvc_app_handle)
622 uvc_v4l2_disable(uvc);
623 mutex_unlock(&video->mutex);
624
625 file->private_data = NULL;
626 v4l2_fh_del(&handle->vfh);
627 v4l2_fh_exit(&handle->vfh);
628 kfree(handle);
629
630 return 0;
631 }
632
633 static int
uvc_v4l2_mmap(struct file * file,struct vm_area_struct * vma)634 uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
635 {
636 struct video_device *vdev = video_devdata(file);
637 struct uvc_device *uvc = video_get_drvdata(vdev);
638
639 return uvcg_queue_mmap(&uvc->video.queue, vma);
640 }
641
642 static __poll_t
uvc_v4l2_poll(struct file * file,poll_table * wait)643 uvc_v4l2_poll(struct file *file, poll_table *wait)
644 {
645 struct video_device *vdev = video_devdata(file);
646 struct uvc_device *uvc = video_get_drvdata(vdev);
647
648 return uvcg_queue_poll(&uvc->video.queue, file, wait);
649 }
650
651 #ifndef CONFIG_MMU
uvcg_v4l2_get_unmapped_area(struct file * file,unsigned long addr,unsigned long len,unsigned long pgoff,unsigned long flags)652 static unsigned long uvcg_v4l2_get_unmapped_area(struct file *file,
653 unsigned long addr, unsigned long len, unsigned long pgoff,
654 unsigned long flags)
655 {
656 struct video_device *vdev = video_devdata(file);
657 struct uvc_device *uvc = video_get_drvdata(vdev);
658
659 return uvcg_queue_get_unmapped_area(&uvc->video.queue, pgoff);
660 }
661 #endif
662
663 const struct v4l2_file_operations uvc_v4l2_fops = {
664 .owner = THIS_MODULE,
665 .open = uvc_v4l2_open,
666 .release = uvc_v4l2_release,
667 .unlocked_ioctl = video_ioctl2,
668 .mmap = uvc_v4l2_mmap,
669 .poll = uvc_v4l2_poll,
670 #ifndef CONFIG_MMU
671 .get_unmapped_area = uvcg_v4l2_get_unmapped_area,
672 #endif
673 };
674
675