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