Merge tag 'for-4.1' of git://git.kernel.org/pub/scm/linux/kernel/git/kishon/linux...
[deliverable/linux.git] / drivers / usb / gadget / function / uvc_v4l2.c
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/mutex.h>
18 #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 "f_uvc.h"
27 #include "uvc.h"
28 #include "uvc_queue.h"
29 #include "uvc_video.h"
30 #include "uvc_v4l2.h"
31
32 /* --------------------------------------------------------------------------
33 * Requests handling
34 */
35
36 static int
37 uvc_send_response(struct uvc_device *uvc, struct uvc_request_data *data)
38 {
39 struct usb_composite_dev *cdev = uvc->func.config->cdev;
40 struct usb_request *req = uvc->control_req;
41
42 if (data->length < 0)
43 return usb_ep_set_halt(cdev->gadget->ep0);
44
45 req->length = min_t(unsigned int, uvc->event_length, data->length);
46 req->zero = data->length < uvc->event_length;
47
48 memcpy(req->buf, data->data, req->length);
49
50 return usb_ep_queue(cdev->gadget->ep0, req, GFP_KERNEL);
51 }
52
53 /* --------------------------------------------------------------------------
54 * V4L2 ioctls
55 */
56
57 struct uvc_format
58 {
59 u8 bpp;
60 u32 fcc;
61 };
62
63 static struct uvc_format uvc_formats[] = {
64 { 16, V4L2_PIX_FMT_YUYV },
65 { 0, V4L2_PIX_FMT_MJPEG },
66 };
67
68 static int
69 uvc_v4l2_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
70 {
71 struct video_device *vdev = video_devdata(file);
72 struct uvc_device *uvc = video_get_drvdata(vdev);
73 struct usb_composite_dev *cdev = uvc->func.config->cdev;
74
75 strlcpy(cap->driver, "g_uvc", sizeof(cap->driver));
76 strlcpy(cap->card, cdev->gadget->name, sizeof(cap->card));
77 strlcpy(cap->bus_info, dev_name(&cdev->gadget->dev),
78 sizeof(cap->bus_info));
79
80 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
81
82 return 0;
83 }
84
85 static int
86 uvc_v4l2_get_format(struct file *file, void *fh, struct v4l2_format *fmt)
87 {
88 struct video_device *vdev = video_devdata(file);
89 struct uvc_device *uvc = video_get_drvdata(vdev);
90 struct uvc_video *video = &uvc->video;
91
92 fmt->fmt.pix.pixelformat = video->fcc;
93 fmt->fmt.pix.width = video->width;
94 fmt->fmt.pix.height = video->height;
95 fmt->fmt.pix.field = V4L2_FIELD_NONE;
96 fmt->fmt.pix.bytesperline = video->bpp * video->width / 8;
97 fmt->fmt.pix.sizeimage = video->imagesize;
98 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
99 fmt->fmt.pix.priv = 0;
100
101 return 0;
102 }
103
104 static int
105 uvc_v4l2_set_format(struct file *file, void *fh, struct v4l2_format *fmt)
106 {
107 struct video_device *vdev = video_devdata(file);
108 struct uvc_device *uvc = video_get_drvdata(vdev);
109 struct uvc_video *video = &uvc->video;
110 struct uvc_format *format;
111 unsigned int imagesize;
112 unsigned int bpl;
113 unsigned int i;
114
115 for (i = 0; i < ARRAY_SIZE(uvc_formats); ++i) {
116 format = &uvc_formats[i];
117 if (format->fcc == fmt->fmt.pix.pixelformat)
118 break;
119 }
120
121 if (i == ARRAY_SIZE(uvc_formats)) {
122 printk(KERN_INFO "Unsupported format 0x%08x.\n",
123 fmt->fmt.pix.pixelformat);
124 return -EINVAL;
125 }
126
127 bpl = format->bpp * fmt->fmt.pix.width / 8;
128 imagesize = bpl ? bpl * fmt->fmt.pix.height : fmt->fmt.pix.sizeimage;
129
130 video->fcc = format->fcc;
131 video->bpp = format->bpp;
132 video->width = fmt->fmt.pix.width;
133 video->height = fmt->fmt.pix.height;
134 video->imagesize = imagesize;
135
136 fmt->fmt.pix.field = V4L2_FIELD_NONE;
137 fmt->fmt.pix.bytesperline = bpl;
138 fmt->fmt.pix.sizeimage = imagesize;
139 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
140 fmt->fmt.pix.priv = 0;
141
142 return 0;
143 }
144
145 static int
146 uvc_v4l2_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *b)
147 {
148 struct video_device *vdev = video_devdata(file);
149 struct uvc_device *uvc = video_get_drvdata(vdev);
150 struct uvc_video *video = &uvc->video;
151
152 if (b->type != video->queue.queue.type)
153 return -EINVAL;
154
155 return uvcg_alloc_buffers(&video->queue, b);
156 }
157
158 static int
159 uvc_v4l2_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
160 {
161 struct video_device *vdev = video_devdata(file);
162 struct uvc_device *uvc = video_get_drvdata(vdev);
163 struct uvc_video *video = &uvc->video;
164
165 return uvcg_query_buffer(&video->queue, b);
166 }
167
168 static int
169 uvc_v4l2_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
170 {
171 struct video_device *vdev = video_devdata(file);
172 struct uvc_device *uvc = video_get_drvdata(vdev);
173 struct uvc_video *video = &uvc->video;
174 int ret;
175
176 ret = uvcg_queue_buffer(&video->queue, b);
177 if (ret < 0)
178 return ret;
179
180 return uvcg_video_pump(video);
181 }
182
183 static int
184 uvc_v4l2_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
185 {
186 struct video_device *vdev = video_devdata(file);
187 struct uvc_device *uvc = video_get_drvdata(vdev);
188 struct uvc_video *video = &uvc->video;
189
190 return uvcg_dequeue_buffer(&video->queue, b, file->f_flags & O_NONBLOCK);
191 }
192
193 static int
194 uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
195 {
196 struct video_device *vdev = video_devdata(file);
197 struct uvc_device *uvc = video_get_drvdata(vdev);
198 struct uvc_video *video = &uvc->video;
199 int ret;
200
201 if (type != video->queue.queue.type)
202 return -EINVAL;
203
204 /* Enable UVC video. */
205 ret = uvcg_video_enable(video, 1);
206 if (ret < 0)
207 return ret;
208
209 /*
210 * Complete the alternate setting selection setup phase now that
211 * userspace is ready to provide video frames.
212 */
213 uvc_function_setup_continue(uvc);
214 uvc->state = UVC_STATE_STREAMING;
215
216 return 0;
217 }
218
219 static int
220 uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
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 if (type != video->queue.queue.type)
227 return -EINVAL;
228
229 return uvcg_video_enable(video, 0);
230 }
231
232 static int
233 uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
234 const struct v4l2_event_subscription *sub)
235 {
236 if (sub->type < UVC_EVENT_FIRST || sub->type > UVC_EVENT_LAST)
237 return -EINVAL;
238
239 return v4l2_event_subscribe(fh, sub, 2, NULL);
240 }
241
242 static int
243 uvc_v4l2_unsubscribe_event(struct v4l2_fh *fh,
244 const struct v4l2_event_subscription *sub)
245 {
246 return v4l2_event_unsubscribe(fh, sub);
247 }
248
249 static long
250 uvc_v4l2_ioctl_default(struct file *file, void *fh, bool valid_prio,
251 unsigned int cmd, void *arg)
252 {
253 struct video_device *vdev = video_devdata(file);
254 struct uvc_device *uvc = video_get_drvdata(vdev);
255
256 switch (cmd) {
257 case UVCIOC_SEND_RESPONSE:
258 return uvc_send_response(uvc, arg);
259
260 default:
261 return -ENOIOCTLCMD;
262 }
263 }
264
265 const struct v4l2_ioctl_ops uvc_v4l2_ioctl_ops = {
266 .vidioc_querycap = uvc_v4l2_querycap,
267 .vidioc_g_fmt_vid_out = uvc_v4l2_get_format,
268 .vidioc_s_fmt_vid_out = uvc_v4l2_set_format,
269 .vidioc_reqbufs = uvc_v4l2_reqbufs,
270 .vidioc_querybuf = uvc_v4l2_querybuf,
271 .vidioc_qbuf = uvc_v4l2_qbuf,
272 .vidioc_dqbuf = uvc_v4l2_dqbuf,
273 .vidioc_streamon = uvc_v4l2_streamon,
274 .vidioc_streamoff = uvc_v4l2_streamoff,
275 .vidioc_subscribe_event = uvc_v4l2_subscribe_event,
276 .vidioc_unsubscribe_event = uvc_v4l2_unsubscribe_event,
277 .vidioc_default = uvc_v4l2_ioctl_default,
278 };
279
280 /* --------------------------------------------------------------------------
281 * V4L2
282 */
283
284 static int
285 uvc_v4l2_open(struct file *file)
286 {
287 struct video_device *vdev = video_devdata(file);
288 struct uvc_device *uvc = video_get_drvdata(vdev);
289 struct uvc_file_handle *handle;
290
291 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
292 if (handle == NULL)
293 return -ENOMEM;
294
295 v4l2_fh_init(&handle->vfh, vdev);
296 v4l2_fh_add(&handle->vfh);
297
298 handle->device = &uvc->video;
299 file->private_data = &handle->vfh;
300
301 uvc_function_connect(uvc);
302 return 0;
303 }
304
305 static int
306 uvc_v4l2_release(struct file *file)
307 {
308 struct video_device *vdev = video_devdata(file);
309 struct uvc_device *uvc = video_get_drvdata(vdev);
310 struct uvc_file_handle *handle = to_uvc_file_handle(file->private_data);
311 struct uvc_video *video = handle->device;
312
313 uvc_function_disconnect(uvc);
314
315 uvcg_video_enable(video, 0);
316 uvcg_free_buffers(&video->queue);
317
318 file->private_data = NULL;
319 v4l2_fh_del(&handle->vfh);
320 v4l2_fh_exit(&handle->vfh);
321 kfree(handle);
322
323 return 0;
324 }
325
326 static int
327 uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
328 {
329 struct video_device *vdev = video_devdata(file);
330 struct uvc_device *uvc = video_get_drvdata(vdev);
331
332 return uvcg_queue_mmap(&uvc->video.queue, vma);
333 }
334
335 static unsigned int
336 uvc_v4l2_poll(struct file *file, poll_table *wait)
337 {
338 struct video_device *vdev = video_devdata(file);
339 struct uvc_device *uvc = video_get_drvdata(vdev);
340
341 return uvcg_queue_poll(&uvc->video.queue, file, wait);
342 }
343
344 #ifndef CONFIG_MMU
345 static unsigned long uvcg_v4l2_get_unmapped_area(struct file *file,
346 unsigned long addr, unsigned long len, unsigned long pgoff,
347 unsigned long flags)
348 {
349 struct video_device *vdev = video_devdata(file);
350 struct uvc_device *uvc = video_get_drvdata(vdev);
351
352 return uvcg_queue_get_unmapped_area(&uvc->video.queue, pgoff);
353 }
354 #endif
355
356 struct v4l2_file_operations uvc_v4l2_fops = {
357 .owner = THIS_MODULE,
358 .open = uvc_v4l2_open,
359 .release = uvc_v4l2_release,
360 .ioctl = video_ioctl2,
361 .mmap = uvc_v4l2_mmap,
362 .poll = uvc_v4l2_poll,
363 #ifndef CONFIG_MMU
364 .get_unmapped_area = uvcg_v4l2_get_unmapped_area,
365 #endif
366 };
367
This page took 0.050795 seconds and 5 git commands to generate.