Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 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. |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 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> |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 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 "uvc.h" |
| 27 | #include "uvc_queue.h" |
| 28 | |
| 29 | /* -------------------------------------------------------------------------- |
| 30 | * Requests handling |
| 31 | */ |
| 32 | |
| 33 | static int |
| 34 | uvc_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 Pinchart | 6f6543f | 2012-04-24 11:29:42 +0200 | [diff] [blame] | 42 | req->length = min_t(unsigned int, uvc->event_length, data->length); |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 43 | req->zero = data->length < uvc->event_length; |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 44 | |
Dan Carpenter | a5eaaa1 | 2013-03-14 11:01:05 +0300 | [diff] [blame] | 45 | memcpy(req->buf, data->data, req->length); |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 46 | |
| 47 | return usb_ep_queue(cdev->gadget->ep0, req, GFP_KERNEL); |
| 48 | } |
| 49 | |
| 50 | /* -------------------------------------------------------------------------- |
Laurent Pinchart | a1d27a4 | 2014-09-08 11:18:15 +0300 | [diff] [blame] | 51 | * V4L2 ioctls |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 52 | */ |
| 53 | |
| 54 | struct uvc_format |
| 55 | { |
| 56 | u8 bpp; |
| 57 | u32 fcc; |
| 58 | }; |
| 59 | |
| 60 | static struct uvc_format uvc_formats[] = { |
| 61 | { 16, V4L2_PIX_FMT_YUYV }, |
| 62 | { 0, V4L2_PIX_FMT_MJPEG }, |
| 63 | }; |
| 64 | |
| 65 | static int |
Laurent Pinchart | a1d27a4 | 2014-09-08 11:18:15 +0300 | [diff] [blame] | 66 | uvc_v4l2_querycap(struct file *file, void *fh, struct v4l2_capability *cap) |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 67 | { |
Laurent Pinchart | a1d27a4 | 2014-09-08 11:18:15 +0300 | [diff] [blame] | 68 | 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 | |
| 82 | static int |
| 83 | uvc_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 Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 89 | 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 | |
| 101 | static int |
Laurent Pinchart | a1d27a4 | 2014-09-08 11:18:15 +0300 | [diff] [blame] | 102 | uvc_v4l2_set_format(struct file *file, void *fh, struct v4l2_format *fmt) |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 103 | { |
Laurent Pinchart | a1d27a4 | 2014-09-08 11:18:15 +0300 | [diff] [blame] | 104 | struct video_device *vdev = video_devdata(file); |
| 105 | struct uvc_device *uvc = video_get_drvdata(vdev); |
| 106 | struct uvc_video *video = &uvc->video; |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 107 | 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 Carpenter | 9a88716 | 2010-08-12 09:59:58 +0200 | [diff] [blame] | 118 | if (i == ARRAY_SIZE(uvc_formats)) { |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 119 | 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 | |
| 142 | static int |
Laurent Pinchart | a1d27a4 | 2014-09-08 11:18:15 +0300 | [diff] [blame] | 143 | uvc_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 Pietrasiewicz | 7ea95b1 | 2014-09-09 02:02:08 +0300 | [diff] [blame^] | 152 | return uvcg_alloc_buffers(&video->queue, b); |
Laurent Pinchart | a1d27a4 | 2014-09-08 11:18:15 +0300 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | static int |
| 156 | uvc_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 Pietrasiewicz | 7ea95b1 | 2014-09-09 02:02:08 +0300 | [diff] [blame^] | 162 | return uvcg_query_buffer(&video->queue, b); |
Laurent Pinchart | a1d27a4 | 2014-09-08 11:18:15 +0300 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | static int |
| 166 | uvc_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 Pietrasiewicz | 7ea95b1 | 2014-09-09 02:02:08 +0300 | [diff] [blame^] | 173 | ret = uvcg_queue_buffer(&video->queue, b); |
Laurent Pinchart | a1d27a4 | 2014-09-08 11:18:15 +0300 | [diff] [blame] | 174 | if (ret < 0) |
| 175 | return ret; |
| 176 | |
Andrzej Pietrasiewicz | 7ea95b1 | 2014-09-09 02:02:08 +0300 | [diff] [blame^] | 177 | return uvcg_video_pump(video); |
Laurent Pinchart | a1d27a4 | 2014-09-08 11:18:15 +0300 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | static int |
| 181 | uvc_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 Pietrasiewicz | 7ea95b1 | 2014-09-09 02:02:08 +0300 | [diff] [blame^] | 187 | return uvcg_dequeue_buffer(&video->queue, b, file->f_flags & O_NONBLOCK); |
Laurent Pinchart | a1d27a4 | 2014-09-08 11:18:15 +0300 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | static int |
| 191 | uvc_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 Pietrasiewicz | 7ea95b1 | 2014-09-09 02:02:08 +0300 | [diff] [blame^] | 202 | ret = uvcg_video_enable(video, 1); |
Laurent Pinchart | a1d27a4 | 2014-09-08 11:18:15 +0300 | [diff] [blame] | 203 | 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 | |
| 216 | static int |
| 217 | uvc_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 Pietrasiewicz | 7ea95b1 | 2014-09-09 02:02:08 +0300 | [diff] [blame^] | 226 | return uvcg_video_enable(video, 0); |
Laurent Pinchart | a1d27a4 | 2014-09-08 11:18:15 +0300 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | static int |
| 230 | uvc_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 | |
| 239 | static int |
| 240 | uvc_v4l2_unsubscribe_event(struct v4l2_fh *fh, |
| 241 | const struct v4l2_event_subscription *sub) |
| 242 | { |
| 243 | return v4l2_event_unsubscribe(fh, sub); |
| 244 | } |
| 245 | |
| 246 | static long |
| 247 | uvc_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 | |
| 262 | static 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 | |
| 281 | static int |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 282 | uvc_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 Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 287 | |
| 288 | handle = kzalloc(sizeof(*handle), GFP_KERNEL); |
| 289 | if (handle == NULL) |
| 290 | return -ENOMEM; |
| 291 | |
Hans Verkuil | 523f46d | 2011-06-13 17:44:42 -0300 | [diff] [blame] | 292 | v4l2_fh_init(&handle->vfh, vdev); |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 293 | 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 Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | static int |
| 303 | uvc_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 Pietrasiewicz | 7ea95b1 | 2014-09-09 02:02:08 +0300 | [diff] [blame^] | 312 | uvcg_video_enable(video, 0); |
| 313 | uvcg_free_buffers(&video->queue); |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 314 | |
| 315 | file->private_data = NULL; |
| 316 | v4l2_fh_del(&handle->vfh); |
| 317 | v4l2_fh_exit(&handle->vfh); |
| 318 | kfree(handle); |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 319 | |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 320 | return 0; |
| 321 | } |
| 322 | |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 323 | static int |
| 324 | uvc_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 Pietrasiewicz | 7ea95b1 | 2014-09-09 02:02:08 +0300 | [diff] [blame^] | 329 | return uvcg_queue_mmap(&uvc->video.queue, vma); |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | static unsigned int |
| 333 | uvc_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 Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 337 | |
Andrzej Pietrasiewicz | 7ea95b1 | 2014-09-09 02:02:08 +0300 | [diff] [blame^] | 338 | return uvcg_queue_poll(&uvc->video.queue, file, wait); |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 339 | } |
| 340 | |
Bhupesh Sharma | 2f1d570 | 2013-03-28 15:11:53 +0530 | [diff] [blame] | 341 | #ifndef CONFIG_MMU |
| 342 | static 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 Pietrasiewicz | 7ea95b1 | 2014-09-09 02:02:08 +0300 | [diff] [blame^] | 349 | return uvcg_queue_get_unmapped_area(&uvc->video.queue, pgoff); |
Bhupesh Sharma | 2f1d570 | 2013-03-28 15:11:53 +0530 | [diff] [blame] | 350 | } |
| 351 | #endif |
| 352 | |
Laurent Pinchart | 5d9955f | 2010-07-10 16:13:05 -0300 | [diff] [blame] | 353 | static struct v4l2_file_operations uvc_v4l2_fops = { |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 354 | .owner = THIS_MODULE, |
| 355 | .open = uvc_v4l2_open, |
| 356 | .release = uvc_v4l2_release, |
Laurent Pinchart | a1d27a4 | 2014-09-08 11:18:15 +0300 | [diff] [blame] | 357 | .ioctl = video_ioctl2, |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 358 | .mmap = uvc_v4l2_mmap, |
| 359 | .poll = uvc_v4l2_poll, |
Bhupesh Sharma | 2f1d570 | 2013-03-28 15:11:53 +0530 | [diff] [blame] | 360 | #ifndef CONFIG_MMU |
Andrzej Pietrasiewicz | 7ea95b1 | 2014-09-09 02:02:08 +0300 | [diff] [blame^] | 361 | .get_unmapped_area = uvcg_v4l2_get_unmapped_area, |
Bhupesh Sharma | 2f1d570 | 2013-03-28 15:11:53 +0530 | [diff] [blame] | 362 | #endif |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 363 | }; |
| 364 | |