blob: 55828705d8591988f2ff1c6a8c81ca67846afde0 [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.
11 *
12 */
13
14#include <linux/kernel.h>
15#include <linux/device.h>
16#include <linux/errno.h>
17#include <linux/list.h>
18#include <linux/mutex.h>
19#include <linux/version.h>
20#include <linux/videodev2.h>
21#include <linux/vmalloc.h>
22#include <linux/wait.h>
23
24#include <media/v4l2-dev.h>
25#include <media/v4l2-event.h>
26#include <media/v4l2-ioctl.h>
27
28#include "uvc.h"
29#include "uvc_queue.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(uvc->event_length, data->length);
45 req->zero = data->length < uvc->event_length;
46 req->dma = DMA_ADDR_INVALID;
47
48 memcpy(req->buf, data->data, data->length);
49
50 return usb_ep_queue(cdev->gadget->ep0, req, GFP_KERNEL);
51}
52
53/* --------------------------------------------------------------------------
54 * V4L2
55 */
56
57struct uvc_format
58{
59 u8 bpp;
60 u32 fcc;
61};
62
63static struct uvc_format uvc_formats[] = {
64 { 16, V4L2_PIX_FMT_YUYV },
65 { 0, V4L2_PIX_FMT_MJPEG },
66};
67
68static int
69uvc_v4l2_get_format(struct uvc_video *video, struct v4l2_format *fmt)
70{
71 fmt->fmt.pix.pixelformat = video->fcc;
72 fmt->fmt.pix.width = video->width;
73 fmt->fmt.pix.height = video->height;
74 fmt->fmt.pix.field = V4L2_FIELD_NONE;
75 fmt->fmt.pix.bytesperline = video->bpp * video->width / 8;
76 fmt->fmt.pix.sizeimage = video->imagesize;
77 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
78 fmt->fmt.pix.priv = 0;
79
80 return 0;
81}
82
83static int
84uvc_v4l2_set_format(struct uvc_video *video, struct v4l2_format *fmt)
85{
86 struct uvc_format *format;
87 unsigned int imagesize;
88 unsigned int bpl;
89 unsigned int i;
90
91 for (i = 0; i < ARRAY_SIZE(uvc_formats); ++i) {
92 format = &uvc_formats[i];
93 if (format->fcc == fmt->fmt.pix.pixelformat)
94 break;
95 }
96
Dan Carpenter9a887162010-08-12 09:59:58 +020097 if (i == ARRAY_SIZE(uvc_formats)) {
Laurent Pinchartcdda4792010-05-02 20:57:41 +020098 printk(KERN_INFO "Unsupported format 0x%08x.\n",
99 fmt->fmt.pix.pixelformat);
100 return -EINVAL;
101 }
102
103 bpl = format->bpp * fmt->fmt.pix.width / 8;
104 imagesize = bpl ? bpl * fmt->fmt.pix.height : fmt->fmt.pix.sizeimage;
105
106 video->fcc = format->fcc;
107 video->bpp = format->bpp;
108 video->width = fmt->fmt.pix.width;
109 video->height = fmt->fmt.pix.height;
110 video->imagesize = imagesize;
111
112 fmt->fmt.pix.field = V4L2_FIELD_NONE;
113 fmt->fmt.pix.bytesperline = bpl;
114 fmt->fmt.pix.sizeimage = imagesize;
115 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
116 fmt->fmt.pix.priv = 0;
117
118 return 0;
119}
120
121static int
122uvc_v4l2_open(struct file *file)
123{
124 struct video_device *vdev = video_devdata(file);
125 struct uvc_device *uvc = video_get_drvdata(vdev);
126 struct uvc_file_handle *handle;
127 int ret;
128
129 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
130 if (handle == NULL)
131 return -ENOMEM;
132
Hans Verkuil523f46d2011-06-13 17:44:42 -0300133 v4l2_fh_init(&handle->vfh, vdev);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200134
135 ret = v4l2_event_alloc(&handle->vfh, 8);
136 if (ret < 0)
137 goto error;
138
139 v4l2_fh_add(&handle->vfh);
140
141 handle->device = &uvc->video;
142 file->private_data = &handle->vfh;
143
144 uvc_function_connect(uvc);
145 return 0;
146
147error:
148 v4l2_fh_exit(&handle->vfh);
149 return ret;
150}
151
152static int
153uvc_v4l2_release(struct file *file)
154{
155 struct video_device *vdev = video_devdata(file);
156 struct uvc_device *uvc = video_get_drvdata(vdev);
157 struct uvc_file_handle *handle = to_uvc_file_handle(file->private_data);
158 struct uvc_video *video = handle->device;
159
160 uvc_function_disconnect(uvc);
161
162 uvc_video_enable(video, 0);
163 mutex_lock(&video->queue.mutex);
164 if (uvc_free_buffers(&video->queue) < 0)
165 printk(KERN_ERR "uvc_v4l2_release: Unable to free "
166 "buffers.\n");
167 mutex_unlock(&video->queue.mutex);
168
169 file->private_data = NULL;
170 v4l2_fh_del(&handle->vfh);
171 v4l2_fh_exit(&handle->vfh);
172 kfree(handle);
173 return 0;
174}
175
176static long
177uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
178{
179 struct video_device *vdev = video_devdata(file);
180 struct uvc_device *uvc = video_get_drvdata(vdev);
181 struct uvc_file_handle *handle = to_uvc_file_handle(file->private_data);
182 struct usb_composite_dev *cdev = uvc->func.config->cdev;
183 struct uvc_video *video = &uvc->video;
184 int ret = 0;
185
186 switch (cmd) {
187 /* Query capabilities */
188 case VIDIOC_QUERYCAP:
189 {
190 struct v4l2_capability *cap = arg;
191
192 memset(cap, 0, sizeof *cap);
193 strncpy(cap->driver, "g_uvc", sizeof(cap->driver));
194 strncpy(cap->card, cdev->gadget->name, sizeof(cap->card));
195 strncpy(cap->bus_info, dev_name(&cdev->gadget->dev),
196 sizeof cap->bus_info);
197 cap->version = DRIVER_VERSION_NUMBER;
198 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
199 break;
200 }
201
202 /* Get & Set format */
203 case VIDIOC_G_FMT:
204 {
205 struct v4l2_format *fmt = arg;
206
207 if (fmt->type != video->queue.type)
208 return -EINVAL;
209
210 return uvc_v4l2_get_format(video, fmt);
211 }
212
213 case VIDIOC_S_FMT:
214 {
215 struct v4l2_format *fmt = arg;
216
217 if (fmt->type != video->queue.type)
218 return -EINVAL;
219
220 return uvc_v4l2_set_format(video, fmt);
221 }
222
223 /* Buffers & streaming */
224 case VIDIOC_REQBUFS:
225 {
226 struct v4l2_requestbuffers *rb = arg;
227
228 if (rb->type != video->queue.type ||
229 rb->memory != V4L2_MEMORY_MMAP)
230 return -EINVAL;
231
232 ret = uvc_alloc_buffers(&video->queue, rb->count,
233 video->imagesize);
234 if (ret < 0)
235 return ret;
236
237 rb->count = ret;
238 ret = 0;
239 break;
240 }
241
242 case VIDIOC_QUERYBUF:
243 {
244 struct v4l2_buffer *buf = arg;
245
246 if (buf->type != video->queue.type)
247 return -EINVAL;
248
249 return uvc_query_buffer(&video->queue, buf);
250 }
251
252 case VIDIOC_QBUF:
253 if ((ret = uvc_queue_buffer(&video->queue, arg)) < 0)
254 return ret;
255
256 return uvc_video_pump(video);
257
258 case VIDIOC_DQBUF:
259 return uvc_dequeue_buffer(&video->queue, arg,
260 file->f_flags & O_NONBLOCK);
261
262 case VIDIOC_STREAMON:
263 {
264 int *type = arg;
265
266 if (*type != video->queue.type)
267 return -EINVAL;
268
269 return uvc_video_enable(video, 1);
270 }
271
272 case VIDIOC_STREAMOFF:
273 {
274 int *type = arg;
275
276 if (*type != video->queue.type)
277 return -EINVAL;
278
279 return uvc_video_enable(video, 0);
280 }
281
282 /* Events */
283 case VIDIOC_DQEVENT:
284 {
285 struct v4l2_event *event = arg;
286
287 ret = v4l2_event_dequeue(&handle->vfh, event,
288 file->f_flags & O_NONBLOCK);
289 if (ret == 0 && event->type == UVC_EVENT_SETUP) {
290 struct uvc_event *uvc_event = (void *)&event->u.data;
291
292 /* Tell the complete callback to generate an event for
293 * the next request that will be enqueued by
294 * uvc_event_write.
295 */
296 uvc->event_setup_out =
297 !(uvc_event->req.bRequestType & USB_DIR_IN);
298 uvc->event_length = uvc_event->req.wLength;
299 }
300
301 return ret;
302 }
303
304 case VIDIOC_SUBSCRIBE_EVENT:
305 {
306 struct v4l2_event_subscription *sub = arg;
307
308 if (sub->type < UVC_EVENT_FIRST || sub->type > UVC_EVENT_LAST)
309 return -EINVAL;
310
311 return v4l2_event_subscribe(&handle->vfh, arg);
312 }
313
314 case VIDIOC_UNSUBSCRIBE_EVENT:
315 return v4l2_event_unsubscribe(&handle->vfh, arg);
316
317 case UVCIOC_SEND_RESPONSE:
318 ret = uvc_send_response(uvc, arg);
319 break;
320
321 default:
322 return -ENOIOCTLCMD;
323 }
324
325 return ret;
326}
327
328static long
329uvc_v4l2_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
330{
331 return video_usercopy(file, cmd, arg, uvc_v4l2_do_ioctl);
332}
333
334static int
335uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
336{
337 struct video_device *vdev = video_devdata(file);
338 struct uvc_device *uvc = video_get_drvdata(vdev);
339
340 return uvc_queue_mmap(&uvc->video.queue, vma);
341}
342
343static unsigned int
344uvc_v4l2_poll(struct file *file, poll_table *wait)
345{
346 struct video_device *vdev = video_devdata(file);
347 struct uvc_device *uvc = video_get_drvdata(vdev);
348 struct uvc_file_handle *handle = to_uvc_file_handle(file->private_data);
349 unsigned int mask = 0;
350
Hans Verkuil523f46d2011-06-13 17:44:42 -0300351 poll_wait(file, &handle->vfh.wait, wait);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200352 if (v4l2_event_pending(&handle->vfh))
353 mask |= POLLPRI;
354
355 mask |= uvc_queue_poll(&uvc->video.queue, file, wait);
356
357 return mask;
358}
359
Laurent Pinchart5d9955f2010-07-10 16:13:05 -0300360static struct v4l2_file_operations uvc_v4l2_fops = {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200361 .owner = THIS_MODULE,
362 .open = uvc_v4l2_open,
363 .release = uvc_v4l2_release,
364 .ioctl = uvc_v4l2_ioctl,
365 .mmap = uvc_v4l2_mmap,
366 .poll = uvc_v4l2_poll,
367};
368