Linux Audio

Check our new training course

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