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