blob: c85730eb36ddfcbac5e823845402e46d31dffaae [file] [log] [blame]
Laurent Pinchartcdda4792010-05-02 20:57:41 +02001/*
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.
Laurent Pinchartcdda4792010-05-02 20:57:41 +020011 */
12
13#include <linux/kernel.h>
14#include <linux/device.h>
15#include <linux/errno.h>
16#include <linux/list.h>
17#include <linux/mutex.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
26#include "uvc.h"
27#include "uvc_queue.h"
28
29/* --------------------------------------------------------------------------
30 * Requests handling
31 */
32
33static int
34uvc_send_response(struct uvc_device *uvc, struct uvc_request_data *data)
35{
36 struct usb_composite_dev *cdev = uvc->func.config->cdev;
37 struct usb_request *req = uvc->control_req;
38
39 if (data->length < 0)
40 return usb_ep_set_halt(cdev->gadget->ep0);
41
Laurent Pinchart6f6543f2012-04-24 11:29:42 +020042 req->length = min_t(unsigned int, uvc->event_length, data->length);
Laurent Pinchartcdda4792010-05-02 20:57:41 +020043 req->zero = data->length < uvc->event_length;
Laurent Pinchartcdda4792010-05-02 20:57:41 +020044
Dan Carpentera5eaaa12013-03-14 11:01:05 +030045 memcpy(req->buf, data->data, req->length);
Laurent Pinchartcdda4792010-05-02 20:57:41 +020046
47 return usb_ep_queue(cdev->gadget->ep0, req, GFP_KERNEL);
48}
49
50/* --------------------------------------------------------------------------
Laurent Pincharta1d27a42014-09-08 11:18:15 +030051 * V4L2 ioctls
Laurent Pinchartcdda4792010-05-02 20:57:41 +020052 */
53
54struct uvc_format
55{
56 u8 bpp;
57 u32 fcc;
58};
59
60static struct uvc_format uvc_formats[] = {
61 { 16, V4L2_PIX_FMT_YUYV },
62 { 0, V4L2_PIX_FMT_MJPEG },
63};
64
65static int
Laurent Pincharta1d27a42014-09-08 11:18:15 +030066uvc_v4l2_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
Laurent Pinchartcdda4792010-05-02 20:57:41 +020067{
Laurent Pincharta1d27a42014-09-08 11:18:15 +030068 struct video_device *vdev = video_devdata(file);
69 struct uvc_device *uvc = video_get_drvdata(vdev);
70 struct usb_composite_dev *cdev = uvc->func.config->cdev;
71
72 strlcpy(cap->driver, "g_uvc", sizeof(cap->driver));
73 strlcpy(cap->card, cdev->gadget->name, sizeof(cap->card));
74 strlcpy(cap->bus_info, dev_name(&cdev->gadget->dev),
75 sizeof(cap->bus_info));
76
77 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
78
79 return 0;
80}
81
82static int
83uvc_v4l2_get_format(struct file *file, void *fh, struct v4l2_format *fmt)
84{
85 struct video_device *vdev = video_devdata(file);
86 struct uvc_device *uvc = video_get_drvdata(vdev);
87 struct uvc_video *video = &uvc->video;
88
Laurent Pinchartcdda4792010-05-02 20:57:41 +020089 fmt->fmt.pix.pixelformat = video->fcc;
90 fmt->fmt.pix.width = video->width;
91 fmt->fmt.pix.height = video->height;
92 fmt->fmt.pix.field = V4L2_FIELD_NONE;
93 fmt->fmt.pix.bytesperline = video->bpp * video->width / 8;
94 fmt->fmt.pix.sizeimage = video->imagesize;
95 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
96 fmt->fmt.pix.priv = 0;
97
98 return 0;
99}
100
101static int
Laurent Pincharta1d27a42014-09-08 11:18:15 +0300102uvc_v4l2_set_format(struct file *file, void *fh, struct v4l2_format *fmt)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200103{
Laurent Pincharta1d27a42014-09-08 11:18:15 +0300104 struct video_device *vdev = video_devdata(file);
105 struct uvc_device *uvc = video_get_drvdata(vdev);
106 struct uvc_video *video = &uvc->video;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200107 struct uvc_format *format;
108 unsigned int imagesize;
109 unsigned int bpl;
110 unsigned int i;
111
112 for (i = 0; i < ARRAY_SIZE(uvc_formats); ++i) {
113 format = &uvc_formats[i];
114 if (format->fcc == fmt->fmt.pix.pixelformat)
115 break;
116 }
117
Dan Carpenter9a887162010-08-12 09:59:58 +0200118 if (i == ARRAY_SIZE(uvc_formats)) {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200119 printk(KERN_INFO "Unsupported format 0x%08x.\n",
120 fmt->fmt.pix.pixelformat);
121 return -EINVAL;
122 }
123
124 bpl = format->bpp * fmt->fmt.pix.width / 8;
125 imagesize = bpl ? bpl * fmt->fmt.pix.height : fmt->fmt.pix.sizeimage;
126
127 video->fcc = format->fcc;
128 video->bpp = format->bpp;
129 video->width = fmt->fmt.pix.width;
130 video->height = fmt->fmt.pix.height;
131 video->imagesize = imagesize;
132
133 fmt->fmt.pix.field = V4L2_FIELD_NONE;
134 fmt->fmt.pix.bytesperline = bpl;
135 fmt->fmt.pix.sizeimage = imagesize;
136 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
137 fmt->fmt.pix.priv = 0;
138
139 return 0;
140}
141
142static int
Laurent Pincharta1d27a42014-09-08 11:18:15 +0300143uvc_v4l2_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *b)
144{
145 struct video_device *vdev = video_devdata(file);
146 struct uvc_device *uvc = video_get_drvdata(vdev);
147 struct uvc_video *video = &uvc->video;
148
149 if (b->type != video->queue.queue.type)
150 return -EINVAL;
151
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300152 return uvcg_alloc_buffers(&video->queue, b);
Laurent Pincharta1d27a42014-09-08 11:18:15 +0300153}
154
155static int
156uvc_v4l2_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
157{
158 struct video_device *vdev = video_devdata(file);
159 struct uvc_device *uvc = video_get_drvdata(vdev);
160 struct uvc_video *video = &uvc->video;
161
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300162 return uvcg_query_buffer(&video->queue, b);
Laurent Pincharta1d27a42014-09-08 11:18:15 +0300163}
164
165static int
166uvc_v4l2_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
167{
168 struct video_device *vdev = video_devdata(file);
169 struct uvc_device *uvc = video_get_drvdata(vdev);
170 struct uvc_video *video = &uvc->video;
171 int ret;
172
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300173 ret = uvcg_queue_buffer(&video->queue, b);
Laurent Pincharta1d27a42014-09-08 11:18:15 +0300174 if (ret < 0)
175 return ret;
176
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300177 return uvcg_video_pump(video);
Laurent Pincharta1d27a42014-09-08 11:18:15 +0300178}
179
180static int
181uvc_v4l2_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
182{
183 struct video_device *vdev = video_devdata(file);
184 struct uvc_device *uvc = video_get_drvdata(vdev);
185 struct uvc_video *video = &uvc->video;
186
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300187 return uvcg_dequeue_buffer(&video->queue, b, file->f_flags & O_NONBLOCK);
Laurent Pincharta1d27a42014-09-08 11:18:15 +0300188}
189
190static int
191uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
192{
193 struct video_device *vdev = video_devdata(file);
194 struct uvc_device *uvc = video_get_drvdata(vdev);
195 struct uvc_video *video = &uvc->video;
196 int ret;
197
198 if (type != video->queue.queue.type)
199 return -EINVAL;
200
201 /* Enable UVC video. */
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300202 ret = uvcg_video_enable(video, 1);
Laurent Pincharta1d27a42014-09-08 11:18:15 +0300203 if (ret < 0)
204 return ret;
205
206 /*
207 * Complete the alternate setting selection setup phase now that
208 * userspace is ready to provide video frames.
209 */
210 uvc_function_setup_continue(uvc);
211 uvc->state = UVC_STATE_STREAMING;
212
213 return 0;
214}
215
216static int
217uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
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 if (type != video->queue.queue.type)
224 return -EINVAL;
225
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300226 return uvcg_video_enable(video, 0);
Laurent Pincharta1d27a42014-09-08 11:18:15 +0300227}
228
229static int
230uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
231 const struct v4l2_event_subscription *sub)
232{
233 if (sub->type < UVC_EVENT_FIRST || sub->type > UVC_EVENT_LAST)
234 return -EINVAL;
235
236 return v4l2_event_subscribe(fh, sub, 2, NULL);
237}
238
239static int
240uvc_v4l2_unsubscribe_event(struct v4l2_fh *fh,
241 const struct v4l2_event_subscription *sub)
242{
243 return v4l2_event_unsubscribe(fh, sub);
244}
245
246static long
247uvc_v4l2_ioctl_default(struct file *file, void *fh, bool valid_prio,
248 unsigned int cmd, void *arg)
249{
250 struct video_device *vdev = video_devdata(file);
251 struct uvc_device *uvc = video_get_drvdata(vdev);
252
253 switch (cmd) {
254 case UVCIOC_SEND_RESPONSE:
255 return uvc_send_response(uvc, arg);
256
257 default:
258 return -ENOIOCTLCMD;
259 }
260}
261
262static const struct v4l2_ioctl_ops uvc_v4l2_ioctl_ops = {
263 .vidioc_querycap = uvc_v4l2_querycap,
264 .vidioc_g_fmt_vid_out = uvc_v4l2_get_format,
265 .vidioc_s_fmt_vid_out = uvc_v4l2_set_format,
266 .vidioc_reqbufs = uvc_v4l2_reqbufs,
267 .vidioc_querybuf = uvc_v4l2_querybuf,
268 .vidioc_qbuf = uvc_v4l2_qbuf,
269 .vidioc_dqbuf = uvc_v4l2_dqbuf,
270 .vidioc_streamon = uvc_v4l2_streamon,
271 .vidioc_streamoff = uvc_v4l2_streamoff,
272 .vidioc_subscribe_event = uvc_v4l2_subscribe_event,
273 .vidioc_unsubscribe_event = uvc_v4l2_unsubscribe_event,
274 .vidioc_default = uvc_v4l2_ioctl_default,
275};
276
277/* --------------------------------------------------------------------------
278 * V4L2
279 */
280
281static int
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200282uvc_v4l2_open(struct file *file)
283{
284 struct video_device *vdev = video_devdata(file);
285 struct uvc_device *uvc = video_get_drvdata(vdev);
286 struct uvc_file_handle *handle;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200287
288 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
289 if (handle == NULL)
290 return -ENOMEM;
291
Hans Verkuil523f46d2011-06-13 17:44:42 -0300292 v4l2_fh_init(&handle->vfh, vdev);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200293 v4l2_fh_add(&handle->vfh);
294
295 handle->device = &uvc->video;
296 file->private_data = &handle->vfh;
297
298 uvc_function_connect(uvc);
299 return 0;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200300}
301
302static int
303uvc_v4l2_release(struct file *file)
304{
305 struct video_device *vdev = video_devdata(file);
306 struct uvc_device *uvc = video_get_drvdata(vdev);
307 struct uvc_file_handle *handle = to_uvc_file_handle(file->private_data);
308 struct uvc_video *video = handle->device;
309
310 uvc_function_disconnect(uvc);
311
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300312 uvcg_video_enable(video, 0);
313 uvcg_free_buffers(&video->queue);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200314
315 file->private_data = NULL;
316 v4l2_fh_del(&handle->vfh);
317 v4l2_fh_exit(&handle->vfh);
318 kfree(handle);
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530319
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200320 return 0;
321}
322
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200323static int
324uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
325{
326 struct video_device *vdev = video_devdata(file);
327 struct uvc_device *uvc = video_get_drvdata(vdev);
328
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300329 return uvcg_queue_mmap(&uvc->video.queue, vma);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200330}
331
332static unsigned int
333uvc_v4l2_poll(struct file *file, poll_table *wait)
334{
335 struct video_device *vdev = video_devdata(file);
336 struct uvc_device *uvc = video_get_drvdata(vdev);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200337
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300338 return uvcg_queue_poll(&uvc->video.queue, file, wait);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200339}
340
Bhupesh Sharma2f1d5702013-03-28 15:11:53 +0530341#ifndef CONFIG_MMU
342static unsigned long uvc_v4l2_get_unmapped_area(struct file *file,
343 unsigned long addr, unsigned long len, unsigned long pgoff,
344 unsigned long flags)
345{
346 struct video_device *vdev = video_devdata(file);
347 struct uvc_device *uvc = video_get_drvdata(vdev);
348
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300349 return uvcg_queue_get_unmapped_area(&uvc->video.queue, pgoff);
Bhupesh Sharma2f1d5702013-03-28 15:11:53 +0530350}
351#endif
352
Laurent Pinchart5d9955f2010-07-10 16:13:05 -0300353static struct v4l2_file_operations uvc_v4l2_fops = {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200354 .owner = THIS_MODULE,
355 .open = uvc_v4l2_open,
356 .release = uvc_v4l2_release,
Laurent Pincharta1d27a42014-09-08 11:18:15 +0300357 .ioctl = video_ioctl2,
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200358 .mmap = uvc_v4l2_mmap,
359 .poll = uvc_v4l2_poll,
Bhupesh Sharma2f1d5702013-03-28 15:11:53 +0530360#ifndef CONFIG_MMU
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300361 .get_unmapped_area = uvcg_v4l2_get_unmapped_area,
Bhupesh Sharma2f1d5702013-03-28 15:11:53 +0530362#endif
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200363};
364