Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1 | /* |
| 2 | * uvc_v4l2.c -- USB Video Class driver - V4L2 API |
| 3 | * |
Laurent Pinchart | 2c2d264 | 2009-01-03 19:12:40 -0300 | [diff] [blame] | 4 | * Copyright (C) 2005-2009 |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 5 | * Laurent Pinchart (laurent.pinchart@skynet.be) |
| 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/version.h> |
| 16 | #include <linux/list.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/usb.h> |
| 19 | #include <linux/videodev2.h> |
| 20 | #include <linux/vmalloc.h> |
| 21 | #include <linux/mm.h> |
| 22 | #include <linux/wait.h> |
| 23 | #include <asm/atomic.h> |
| 24 | |
| 25 | #include <media/v4l2-common.h> |
Hans Verkuil | 35ea11f | 2008-07-20 08:12:02 -0300 | [diff] [blame] | 26 | #include <media/v4l2-ioctl.h> |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 27 | |
| 28 | #include "uvcvideo.h" |
| 29 | |
| 30 | /* ------------------------------------------------------------------------ |
| 31 | * V4L2 interface |
| 32 | */ |
| 33 | |
| 34 | /* |
| 35 | * Mapping V4L2 controls to UVC controls can be straighforward if done well. |
| 36 | * Most of the UVC controls exist in V4L2, and can be mapped directly. Some |
| 37 | * must be grouped (for instance the Red Balance, Blue Balance and Do White |
| 38 | * Balance V4L2 controls use the White Balance Component UVC control) or |
| 39 | * otherwise translated. The approach we take here is to use a translation |
Laurent Pinchart | 2c2d264 | 2009-01-03 19:12:40 -0300 | [diff] [blame] | 40 | * table for the controls that can be mapped directly, and handle the others |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 41 | * manually. |
| 42 | */ |
| 43 | static int uvc_v4l2_query_menu(struct uvc_video_device *video, |
| 44 | struct v4l2_querymenu *query_menu) |
| 45 | { |
| 46 | struct uvc_menu_info *menu_info; |
| 47 | struct uvc_control_mapping *mapping; |
| 48 | struct uvc_control *ctrl; |
| 49 | |
| 50 | ctrl = uvc_find_control(video, query_menu->id, &mapping); |
| 51 | if (ctrl == NULL || mapping->v4l2_type != V4L2_CTRL_TYPE_MENU) |
| 52 | return -EINVAL; |
| 53 | |
| 54 | if (query_menu->index >= mapping->menu_count) |
| 55 | return -EINVAL; |
| 56 | |
| 57 | menu_info = &mapping->menu_info[query_menu->index]; |
Laurent Pinchart | d0ebf30 | 2009-01-08 14:05:11 -0300 | [diff] [blame] | 58 | strlcpy(query_menu->name, menu_info->name, sizeof query_menu->name); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | /* |
| 63 | * Find the frame interval closest to the requested frame interval for the |
| 64 | * given frame format and size. This should be done by the device as part of |
| 65 | * the Video Probe and Commit negotiation, but some hardware don't implement |
| 66 | * that feature. |
| 67 | */ |
| 68 | static __u32 uvc_try_frame_interval(struct uvc_frame *frame, __u32 interval) |
| 69 | { |
| 70 | unsigned int i; |
| 71 | |
| 72 | if (frame->bFrameIntervalType) { |
| 73 | __u32 best = -1, dist; |
| 74 | |
| 75 | for (i = 0; i < frame->bFrameIntervalType; ++i) { |
| 76 | dist = interval > frame->dwFrameInterval[i] |
| 77 | ? interval - frame->dwFrameInterval[i] |
| 78 | : frame->dwFrameInterval[i] - interval; |
| 79 | |
| 80 | if (dist > best) |
| 81 | break; |
| 82 | |
| 83 | best = dist; |
| 84 | } |
| 85 | |
| 86 | interval = frame->dwFrameInterval[i-1]; |
| 87 | } else { |
| 88 | const __u32 min = frame->dwFrameInterval[0]; |
| 89 | const __u32 max = frame->dwFrameInterval[1]; |
| 90 | const __u32 step = frame->dwFrameInterval[2]; |
| 91 | |
| 92 | interval = min + (interval - min + step/2) / step * step; |
| 93 | if (interval > max) |
| 94 | interval = max; |
| 95 | } |
| 96 | |
| 97 | return interval; |
| 98 | } |
| 99 | |
| 100 | static int uvc_v4l2_try_format(struct uvc_video_device *video, |
| 101 | struct v4l2_format *fmt, struct uvc_streaming_control *probe, |
| 102 | struct uvc_format **uvc_format, struct uvc_frame **uvc_frame) |
| 103 | { |
| 104 | struct uvc_format *format = NULL; |
| 105 | struct uvc_frame *frame = NULL; |
| 106 | __u16 rw, rh; |
| 107 | unsigned int d, maxd; |
| 108 | unsigned int i; |
| 109 | __u32 interval; |
| 110 | int ret = 0; |
| 111 | __u8 *fcc; |
| 112 | |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 113 | if (fmt->type != video->streaming->type) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 114 | return -EINVAL; |
| 115 | |
| 116 | fcc = (__u8 *)&fmt->fmt.pix.pixelformat; |
| 117 | uvc_trace(UVC_TRACE_FORMAT, "Trying format 0x%08x (%c%c%c%c): %ux%u.\n", |
| 118 | fmt->fmt.pix.pixelformat, |
| 119 | fcc[0], fcc[1], fcc[2], fcc[3], |
| 120 | fmt->fmt.pix.width, fmt->fmt.pix.height); |
| 121 | |
| 122 | /* Check if the hardware supports the requested format. */ |
| 123 | for (i = 0; i < video->streaming->nformats; ++i) { |
| 124 | format = &video->streaming->format[i]; |
| 125 | if (format->fcc == fmt->fmt.pix.pixelformat) |
| 126 | break; |
| 127 | } |
| 128 | |
| 129 | if (format == NULL || format->fcc != fmt->fmt.pix.pixelformat) { |
| 130 | uvc_trace(UVC_TRACE_FORMAT, "Unsupported format 0x%08x.\n", |
| 131 | fmt->fmt.pix.pixelformat); |
| 132 | return -EINVAL; |
| 133 | } |
| 134 | |
| 135 | /* Find the closest image size. The distance between image sizes is |
| 136 | * the size in pixels of the non-overlapping regions between the |
| 137 | * requested size and the frame-specified size. |
| 138 | */ |
| 139 | rw = fmt->fmt.pix.width; |
| 140 | rh = fmt->fmt.pix.height; |
| 141 | maxd = (unsigned int)-1; |
| 142 | |
| 143 | for (i = 0; i < format->nframes; ++i) { |
| 144 | __u16 w = format->frame[i].wWidth; |
| 145 | __u16 h = format->frame[i].wHeight; |
| 146 | |
| 147 | d = min(w, rw) * min(h, rh); |
| 148 | d = w*h + rw*rh - 2*d; |
| 149 | if (d < maxd) { |
| 150 | maxd = d; |
| 151 | frame = &format->frame[i]; |
| 152 | } |
| 153 | |
| 154 | if (maxd == 0) |
| 155 | break; |
| 156 | } |
| 157 | |
| 158 | if (frame == NULL) { |
| 159 | uvc_trace(UVC_TRACE_FORMAT, "Unsupported size %ux%u.\n", |
| 160 | fmt->fmt.pix.width, fmt->fmt.pix.height); |
| 161 | return -EINVAL; |
| 162 | } |
| 163 | |
| 164 | /* Use the default frame interval. */ |
| 165 | interval = frame->dwDefaultFrameInterval; |
| 166 | uvc_trace(UVC_TRACE_FORMAT, "Using default frame interval %u.%u us " |
| 167 | "(%u.%u fps).\n", interval/10, interval%10, 10000000/interval, |
| 168 | (100000000/interval)%10); |
| 169 | |
| 170 | /* Set the format index, frame index and frame interval. */ |
| 171 | memset(probe, 0, sizeof *probe); |
| 172 | probe->bmHint = 1; /* dwFrameInterval */ |
| 173 | probe->bFormatIndex = format->index; |
| 174 | probe->bFrameIndex = frame->bFrameIndex; |
| 175 | probe->dwFrameInterval = uvc_try_frame_interval(frame, interval); |
| 176 | /* Some webcams stall the probe control set request when the |
| 177 | * dwMaxVideoFrameSize field is set to zero. The UVC specification |
| 178 | * clearly states that the field is read-only from the host, so this |
| 179 | * is a webcam bug. Set dwMaxVideoFrameSize to the value reported by |
| 180 | * the webcam to work around the problem. |
| 181 | * |
| 182 | * The workaround could probably be enabled for all webcams, so the |
| 183 | * quirk can be removed if needed. It's currently useful to detect |
| 184 | * webcam bugs and fix them before they hit the market (providing |
| 185 | * developers test their webcams with the Linux driver as well as with |
| 186 | * the Windows driver). |
| 187 | */ |
| 188 | if (video->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS) |
| 189 | probe->dwMaxVideoFrameSize = |
| 190 | video->streaming->ctrl.dwMaxVideoFrameSize; |
| 191 | |
Laurent Pinchart | 2c2d264 | 2009-01-03 19:12:40 -0300 | [diff] [blame] | 192 | /* Probe the device. */ |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 193 | if ((ret = uvc_probe_video(video, probe)) < 0) |
| 194 | goto done; |
| 195 | |
| 196 | fmt->fmt.pix.width = frame->wWidth; |
| 197 | fmt->fmt.pix.height = frame->wHeight; |
| 198 | fmt->fmt.pix.field = V4L2_FIELD_NONE; |
| 199 | fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8; |
| 200 | fmt->fmt.pix.sizeimage = probe->dwMaxVideoFrameSize; |
| 201 | fmt->fmt.pix.colorspace = format->colorspace; |
| 202 | fmt->fmt.pix.priv = 0; |
| 203 | |
| 204 | if (uvc_format != NULL) |
| 205 | *uvc_format = format; |
| 206 | if (uvc_frame != NULL) |
| 207 | *uvc_frame = frame; |
| 208 | |
| 209 | done: |
| 210 | return ret; |
| 211 | } |
| 212 | |
| 213 | static int uvc_v4l2_get_format(struct uvc_video_device *video, |
| 214 | struct v4l2_format *fmt) |
| 215 | { |
| 216 | struct uvc_format *format = video->streaming->cur_format; |
| 217 | struct uvc_frame *frame = video->streaming->cur_frame; |
| 218 | |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 219 | if (fmt->type != video->streaming->type) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 220 | return -EINVAL; |
| 221 | |
| 222 | if (format == NULL || frame == NULL) |
| 223 | return -EINVAL; |
| 224 | |
| 225 | fmt->fmt.pix.pixelformat = format->fcc; |
| 226 | fmt->fmt.pix.width = frame->wWidth; |
| 227 | fmt->fmt.pix.height = frame->wHeight; |
| 228 | fmt->fmt.pix.field = V4L2_FIELD_NONE; |
| 229 | fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8; |
| 230 | fmt->fmt.pix.sizeimage = video->streaming->ctrl.dwMaxVideoFrameSize; |
| 231 | fmt->fmt.pix.colorspace = format->colorspace; |
| 232 | fmt->fmt.pix.priv = 0; |
| 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | static int uvc_v4l2_set_format(struct uvc_video_device *video, |
| 238 | struct v4l2_format *fmt) |
| 239 | { |
| 240 | struct uvc_streaming_control probe; |
| 241 | struct uvc_format *format; |
| 242 | struct uvc_frame *frame; |
| 243 | int ret; |
| 244 | |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 245 | if (fmt->type != video->streaming->type) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 246 | return -EINVAL; |
| 247 | |
| 248 | if (uvc_queue_streaming(&video->queue)) |
| 249 | return -EBUSY; |
| 250 | |
| 251 | ret = uvc_v4l2_try_format(video, fmt, &probe, &format, &frame); |
| 252 | if (ret < 0) |
| 253 | return ret; |
| 254 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 255 | memcpy(&video->streaming->ctrl, &probe, sizeof probe); |
| 256 | video->streaming->cur_format = format; |
| 257 | video->streaming->cur_frame = frame; |
| 258 | |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | static int uvc_v4l2_get_streamparm(struct uvc_video_device *video, |
| 263 | struct v4l2_streamparm *parm) |
| 264 | { |
| 265 | uint32_t numerator, denominator; |
| 266 | |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 267 | if (parm->type != video->streaming->type) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 268 | return -EINVAL; |
| 269 | |
| 270 | numerator = video->streaming->ctrl.dwFrameInterval; |
| 271 | denominator = 10000000; |
| 272 | uvc_simplify_fraction(&numerator, &denominator, 8, 333); |
| 273 | |
| 274 | memset(parm, 0, sizeof *parm); |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 275 | parm->type = video->streaming->type; |
| 276 | |
| 277 | if (video->streaming->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { |
| 278 | parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME; |
| 279 | parm->parm.capture.capturemode = 0; |
| 280 | parm->parm.capture.timeperframe.numerator = numerator; |
| 281 | parm->parm.capture.timeperframe.denominator = denominator; |
| 282 | parm->parm.capture.extendedmode = 0; |
| 283 | parm->parm.capture.readbuffers = 0; |
| 284 | } else { |
| 285 | parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME; |
| 286 | parm->parm.output.outputmode = 0; |
| 287 | parm->parm.output.timeperframe.numerator = numerator; |
| 288 | parm->parm.output.timeperframe.denominator = denominator; |
| 289 | } |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | static int uvc_v4l2_set_streamparm(struct uvc_video_device *video, |
| 295 | struct v4l2_streamparm *parm) |
| 296 | { |
| 297 | struct uvc_frame *frame = video->streaming->cur_frame; |
| 298 | struct uvc_streaming_control probe; |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 299 | struct v4l2_fract timeperframe; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 300 | uint32_t interval; |
| 301 | int ret; |
| 302 | |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 303 | if (parm->type != video->streaming->type) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 304 | return -EINVAL; |
| 305 | |
| 306 | if (uvc_queue_streaming(&video->queue)) |
| 307 | return -EBUSY; |
| 308 | |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 309 | if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 310 | timeperframe = parm->parm.capture.timeperframe; |
| 311 | else |
| 312 | timeperframe = parm->parm.output.timeperframe; |
| 313 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 314 | memcpy(&probe, &video->streaming->ctrl, sizeof probe); |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 315 | interval = uvc_fraction_to_interval(timeperframe.numerator, |
| 316 | timeperframe.denominator); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 317 | |
| 318 | uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n", |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 319 | timeperframe.numerator, timeperframe.denominator, interval); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 320 | probe.dwFrameInterval = uvc_try_frame_interval(frame, interval); |
| 321 | |
| 322 | /* Probe the device with the new settings. */ |
| 323 | if ((ret = uvc_probe_video(video, &probe)) < 0) |
| 324 | return ret; |
| 325 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 326 | memcpy(&video->streaming->ctrl, &probe, sizeof probe); |
| 327 | |
| 328 | /* Return the actual frame period. */ |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 329 | timeperframe.numerator = probe.dwFrameInterval; |
| 330 | timeperframe.denominator = 10000000; |
| 331 | uvc_simplify_fraction(&timeperframe.numerator, |
| 332 | &timeperframe.denominator, 8, 333); |
| 333 | |
| 334 | if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 335 | parm->parm.capture.timeperframe = timeperframe; |
| 336 | else |
| 337 | parm->parm.output.timeperframe = timeperframe; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 338 | |
| 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | /* ------------------------------------------------------------------------ |
| 343 | * Privilege management |
| 344 | */ |
| 345 | |
| 346 | /* |
| 347 | * Privilege management is the multiple-open implementation basis. The current |
| 348 | * implementation is completely transparent for the end-user and doesn't |
| 349 | * require explicit use of the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls. |
| 350 | * Those ioctls enable finer control on the device (by making possible for a |
| 351 | * user to request exclusive access to a device), but are not mature yet. |
| 352 | * Switching to the V4L2 priority mechanism might be considered in the future |
| 353 | * if this situation changes. |
| 354 | * |
| 355 | * Each open instance of a UVC device can either be in a privileged or |
| 356 | * unprivileged state. Only a single instance can be in a privileged state at |
Laurent Pinchart | 2c2d264 | 2009-01-03 19:12:40 -0300 | [diff] [blame] | 357 | * a given time. Trying to perform an operation that requires privileges will |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 358 | * automatically acquire the required privileges if possible, or return -EBUSY |
| 359 | * otherwise. Privileges are dismissed when closing the instance. |
| 360 | * |
Laurent Pinchart | 2c2d264 | 2009-01-03 19:12:40 -0300 | [diff] [blame] | 361 | * Operations that require privileges are: |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 362 | * |
| 363 | * - VIDIOC_S_INPUT |
| 364 | * - VIDIOC_S_PARM |
| 365 | * - VIDIOC_S_FMT |
| 366 | * - VIDIOC_TRY_FMT |
| 367 | * - VIDIOC_REQBUFS |
| 368 | */ |
| 369 | static int uvc_acquire_privileges(struct uvc_fh *handle) |
| 370 | { |
| 371 | int ret = 0; |
| 372 | |
| 373 | /* Always succeed if the handle is already privileged. */ |
| 374 | if (handle->state == UVC_HANDLE_ACTIVE) |
| 375 | return 0; |
| 376 | |
| 377 | /* Check if the device already has a privileged handle. */ |
| 378 | mutex_lock(&uvc_driver.open_mutex); |
| 379 | if (atomic_inc_return(&handle->device->active) != 1) { |
| 380 | atomic_dec(&handle->device->active); |
| 381 | ret = -EBUSY; |
| 382 | goto done; |
| 383 | } |
| 384 | |
| 385 | handle->state = UVC_HANDLE_ACTIVE; |
| 386 | |
| 387 | done: |
| 388 | mutex_unlock(&uvc_driver.open_mutex); |
| 389 | return ret; |
| 390 | } |
| 391 | |
| 392 | static void uvc_dismiss_privileges(struct uvc_fh *handle) |
| 393 | { |
| 394 | if (handle->state == UVC_HANDLE_ACTIVE) |
| 395 | atomic_dec(&handle->device->active); |
| 396 | |
| 397 | handle->state = UVC_HANDLE_PASSIVE; |
| 398 | } |
| 399 | |
| 400 | static int uvc_has_privileges(struct uvc_fh *handle) |
| 401 | { |
| 402 | return handle->state == UVC_HANDLE_ACTIVE; |
| 403 | } |
| 404 | |
| 405 | /* ------------------------------------------------------------------------ |
| 406 | * V4L2 file operations |
| 407 | */ |
| 408 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 409 | static int uvc_v4l2_open(struct file *file) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 410 | { |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 411 | struct uvc_video_device *video; |
| 412 | struct uvc_fh *handle; |
| 413 | int ret = 0; |
| 414 | |
| 415 | uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n"); |
| 416 | mutex_lock(&uvc_driver.open_mutex); |
Hans Verkuil | c170ecf | 2008-08-23 08:32:09 -0300 | [diff] [blame] | 417 | video = video_drvdata(file); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 418 | |
| 419 | if (video->dev->state & UVC_DEV_DISCONNECTED) { |
| 420 | ret = -ENODEV; |
| 421 | goto done; |
| 422 | } |
| 423 | |
| 424 | ret = usb_autopm_get_interface(video->dev->intf); |
| 425 | if (ret < 0) |
| 426 | goto done; |
| 427 | |
| 428 | /* Create the device handle. */ |
| 429 | handle = kzalloc(sizeof *handle, GFP_KERNEL); |
| 430 | if (handle == NULL) { |
| 431 | usb_autopm_put_interface(video->dev->intf); |
| 432 | ret = -ENOMEM; |
| 433 | goto done; |
| 434 | } |
| 435 | |
| 436 | handle->device = video; |
| 437 | handle->state = UVC_HANDLE_PASSIVE; |
| 438 | file->private_data = handle; |
| 439 | |
| 440 | kref_get(&video->dev->kref); |
| 441 | |
| 442 | done: |
| 443 | mutex_unlock(&uvc_driver.open_mutex); |
| 444 | return ret; |
| 445 | } |
| 446 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 447 | static int uvc_v4l2_release(struct file *file) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 448 | { |
Hans Verkuil | c170ecf | 2008-08-23 08:32:09 -0300 | [diff] [blame] | 449 | struct uvc_video_device *video = video_drvdata(file); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 450 | struct uvc_fh *handle = (struct uvc_fh *)file->private_data; |
| 451 | |
| 452 | uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n"); |
| 453 | |
| 454 | /* Only free resources if this is a privileged handle. */ |
| 455 | if (uvc_has_privileges(handle)) { |
| 456 | uvc_video_enable(video, 0); |
| 457 | |
| 458 | mutex_lock(&video->queue.mutex); |
| 459 | if (uvc_free_buffers(&video->queue) < 0) |
| 460 | uvc_printk(KERN_ERR, "uvc_v4l2_release: Unable to " |
| 461 | "free buffers.\n"); |
| 462 | mutex_unlock(&video->queue.mutex); |
| 463 | } |
| 464 | |
| 465 | /* Release the file handle. */ |
| 466 | uvc_dismiss_privileges(handle); |
| 467 | kfree(handle); |
| 468 | file->private_data = NULL; |
| 469 | |
| 470 | usb_autopm_put_interface(video->dev->intf); |
| 471 | kref_put(&video->dev->kref, uvc_delete); |
| 472 | return 0; |
| 473 | } |
| 474 | |
Hans Verkuil | 069b747 | 2008-12-30 07:04:34 -0300 | [diff] [blame] | 475 | static long uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 476 | { |
| 477 | struct video_device *vdev = video_devdata(file); |
| 478 | struct uvc_video_device *video = video_get_drvdata(vdev); |
| 479 | struct uvc_fh *handle = (struct uvc_fh *)file->private_data; |
Hans Verkuil | 069b747 | 2008-12-30 07:04:34 -0300 | [diff] [blame] | 480 | long ret = 0; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 481 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 482 | switch (cmd) { |
| 483 | /* Query capabilities */ |
| 484 | case VIDIOC_QUERYCAP: |
| 485 | { |
| 486 | struct v4l2_capability *cap = arg; |
| 487 | |
| 488 | memset(cap, 0, sizeof *cap); |
Laurent Pinchart | d0ebf30 | 2009-01-08 14:05:11 -0300 | [diff] [blame] | 489 | strlcpy(cap->driver, "uvcvideo", sizeof cap->driver); |
| 490 | strlcpy(cap->card, vdev->name, sizeof cap->card); |
Thierry MERLE | b12049a | 2009-01-27 16:53:23 -0300 | [diff] [blame^] | 491 | usb_make_path(video->dev->udev, |
| 492 | cap->bus_info, sizeof(cap->bus_info)); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 493 | cap->version = DRIVER_VERSION_NUMBER; |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 494 | if (video->streaming->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 495 | cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
| 496 | | V4L2_CAP_STREAMING; |
| 497 | else |
| 498 | cap->capabilities = V4L2_CAP_VIDEO_OUTPUT |
| 499 | | V4L2_CAP_STREAMING; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 500 | break; |
| 501 | } |
| 502 | |
| 503 | /* Get, Set & Query control */ |
| 504 | case VIDIOC_QUERYCTRL: |
| 505 | return uvc_query_v4l2_ctrl(video, arg); |
| 506 | |
| 507 | case VIDIOC_G_CTRL: |
| 508 | { |
| 509 | struct v4l2_control *ctrl = arg; |
| 510 | struct v4l2_ext_control xctrl; |
| 511 | |
| 512 | memset(&xctrl, 0, sizeof xctrl); |
| 513 | xctrl.id = ctrl->id; |
| 514 | |
| 515 | uvc_ctrl_begin(video); |
| 516 | ret = uvc_ctrl_get(video, &xctrl); |
| 517 | uvc_ctrl_rollback(video); |
| 518 | if (ret >= 0) |
| 519 | ctrl->value = xctrl.value; |
| 520 | break; |
| 521 | } |
| 522 | |
| 523 | case VIDIOC_S_CTRL: |
| 524 | { |
| 525 | struct v4l2_control *ctrl = arg; |
| 526 | struct v4l2_ext_control xctrl; |
| 527 | |
| 528 | memset(&xctrl, 0, sizeof xctrl); |
| 529 | xctrl.id = ctrl->id; |
| 530 | xctrl.value = ctrl->value; |
| 531 | |
| 532 | uvc_ctrl_begin(video); |
| 533 | ret = uvc_ctrl_set(video, &xctrl); |
| 534 | if (ret < 0) { |
| 535 | uvc_ctrl_rollback(video); |
| 536 | return ret; |
| 537 | } |
| 538 | ret = uvc_ctrl_commit(video); |
| 539 | break; |
| 540 | } |
| 541 | |
| 542 | case VIDIOC_QUERYMENU: |
| 543 | return uvc_v4l2_query_menu(video, arg); |
| 544 | |
| 545 | case VIDIOC_G_EXT_CTRLS: |
| 546 | { |
| 547 | struct v4l2_ext_controls *ctrls = arg; |
| 548 | struct v4l2_ext_control *ctrl = ctrls->controls; |
| 549 | unsigned int i; |
| 550 | |
| 551 | uvc_ctrl_begin(video); |
| 552 | for (i = 0; i < ctrls->count; ++ctrl, ++i) { |
| 553 | ret = uvc_ctrl_get(video, ctrl); |
| 554 | if (ret < 0) { |
| 555 | uvc_ctrl_rollback(video); |
| 556 | ctrls->error_idx = i; |
| 557 | return ret; |
| 558 | } |
| 559 | } |
| 560 | ctrls->error_idx = 0; |
| 561 | ret = uvc_ctrl_rollback(video); |
| 562 | break; |
| 563 | } |
| 564 | |
| 565 | case VIDIOC_S_EXT_CTRLS: |
| 566 | case VIDIOC_TRY_EXT_CTRLS: |
| 567 | { |
| 568 | struct v4l2_ext_controls *ctrls = arg; |
| 569 | struct v4l2_ext_control *ctrl = ctrls->controls; |
| 570 | unsigned int i; |
| 571 | |
| 572 | ret = uvc_ctrl_begin(video); |
| 573 | if (ret < 0) |
| 574 | return ret; |
| 575 | |
| 576 | for (i = 0; i < ctrls->count; ++ctrl, ++i) { |
| 577 | ret = uvc_ctrl_set(video, ctrl); |
| 578 | if (ret < 0) { |
| 579 | uvc_ctrl_rollback(video); |
| 580 | ctrls->error_idx = i; |
| 581 | return ret; |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | ctrls->error_idx = 0; |
| 586 | |
| 587 | if (cmd == VIDIOC_S_EXT_CTRLS) |
| 588 | ret = uvc_ctrl_commit(video); |
| 589 | else |
| 590 | ret = uvc_ctrl_rollback(video); |
| 591 | break; |
| 592 | } |
| 593 | |
| 594 | /* Get, Set & Enum input */ |
| 595 | case VIDIOC_ENUMINPUT: |
| 596 | { |
| 597 | const struct uvc_entity *selector = video->selector; |
| 598 | struct v4l2_input *input = arg; |
| 599 | struct uvc_entity *iterm = NULL; |
| 600 | u32 index = input->index; |
| 601 | int pin = 0; |
| 602 | |
| 603 | if (selector == NULL || |
| 604 | (video->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) { |
| 605 | if (index != 0) |
| 606 | return -EINVAL; |
| 607 | iterm = list_first_entry(&video->iterms, |
| 608 | struct uvc_entity, chain); |
| 609 | pin = iterm->id; |
| 610 | } else if (pin < selector->selector.bNrInPins) { |
| 611 | pin = selector->selector.baSourceID[index]; |
| 612 | list_for_each_entry(iterm, video->iterms.next, chain) { |
| 613 | if (iterm->id == pin) |
| 614 | break; |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | if (iterm == NULL || iterm->id != pin) |
| 619 | return -EINVAL; |
| 620 | |
| 621 | memset(input, 0, sizeof *input); |
| 622 | input->index = index; |
Laurent Pinchart | d0ebf30 | 2009-01-08 14:05:11 -0300 | [diff] [blame] | 623 | strlcpy(input->name, iterm->name, sizeof input->name); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 624 | if (UVC_ENTITY_TYPE(iterm) == ITT_CAMERA) |
| 625 | input->type = V4L2_INPUT_TYPE_CAMERA; |
| 626 | break; |
| 627 | } |
| 628 | |
| 629 | case VIDIOC_G_INPUT: |
| 630 | { |
| 631 | u8 input; |
| 632 | |
| 633 | if (video->selector == NULL || |
| 634 | (video->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) { |
| 635 | *(int *)arg = 0; |
| 636 | break; |
| 637 | } |
| 638 | |
| 639 | ret = uvc_query_ctrl(video->dev, GET_CUR, video->selector->id, |
| 640 | video->dev->intfnum, SU_INPUT_SELECT_CONTROL, |
| 641 | &input, 1); |
| 642 | if (ret < 0) |
| 643 | return ret; |
| 644 | |
| 645 | *(int *)arg = input - 1; |
| 646 | break; |
| 647 | } |
| 648 | |
| 649 | case VIDIOC_S_INPUT: |
| 650 | { |
| 651 | u8 input = *(u32 *)arg + 1; |
| 652 | |
| 653 | if ((ret = uvc_acquire_privileges(handle)) < 0) |
| 654 | return ret; |
| 655 | |
| 656 | if (video->selector == NULL || |
| 657 | (video->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) { |
| 658 | if (input != 1) |
| 659 | return -EINVAL; |
| 660 | break; |
| 661 | } |
| 662 | |
| 663 | if (input > video->selector->selector.bNrInPins) |
| 664 | return -EINVAL; |
| 665 | |
| 666 | return uvc_query_ctrl(video->dev, SET_CUR, video->selector->id, |
| 667 | video->dev->intfnum, SU_INPUT_SELECT_CONTROL, |
| 668 | &input, 1); |
| 669 | } |
| 670 | |
| 671 | /* Try, Get, Set & Enum format */ |
| 672 | case VIDIOC_ENUM_FMT: |
| 673 | { |
| 674 | struct v4l2_fmtdesc *fmt = arg; |
| 675 | struct uvc_format *format; |
| 676 | |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 677 | if (fmt->type != video->streaming->type || |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 678 | fmt->index >= video->streaming->nformats) |
| 679 | return -EINVAL; |
| 680 | |
| 681 | format = &video->streaming->format[fmt->index]; |
| 682 | fmt->flags = 0; |
| 683 | if (format->flags & UVC_FMT_FLAG_COMPRESSED) |
| 684 | fmt->flags |= V4L2_FMT_FLAG_COMPRESSED; |
Laurent Pinchart | d0ebf30 | 2009-01-08 14:05:11 -0300 | [diff] [blame] | 685 | strlcpy(fmt->description, format->name, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 686 | sizeof fmt->description); |
| 687 | fmt->description[sizeof fmt->description - 1] = 0; |
| 688 | fmt->pixelformat = format->fcc; |
| 689 | break; |
| 690 | } |
| 691 | |
| 692 | case VIDIOC_TRY_FMT: |
| 693 | { |
| 694 | struct uvc_streaming_control probe; |
| 695 | |
| 696 | if ((ret = uvc_acquire_privileges(handle)) < 0) |
| 697 | return ret; |
| 698 | |
| 699 | return uvc_v4l2_try_format(video, arg, &probe, NULL, NULL); |
| 700 | } |
| 701 | |
| 702 | case VIDIOC_S_FMT: |
| 703 | if ((ret = uvc_acquire_privileges(handle)) < 0) |
| 704 | return ret; |
| 705 | |
| 706 | return uvc_v4l2_set_format(video, arg); |
| 707 | |
| 708 | case VIDIOC_G_FMT: |
| 709 | return uvc_v4l2_get_format(video, arg); |
| 710 | |
| 711 | /* Frame size enumeration */ |
| 712 | case VIDIOC_ENUM_FRAMESIZES: |
| 713 | { |
| 714 | struct v4l2_frmsizeenum *fsize = arg; |
| 715 | struct uvc_format *format = NULL; |
| 716 | struct uvc_frame *frame; |
| 717 | int i; |
| 718 | |
| 719 | /* Look for the given pixel format */ |
| 720 | for (i = 0; i < video->streaming->nformats; i++) { |
| 721 | if (video->streaming->format[i].fcc == |
| 722 | fsize->pixel_format) { |
| 723 | format = &video->streaming->format[i]; |
| 724 | break; |
| 725 | } |
| 726 | } |
| 727 | if (format == NULL) |
| 728 | return -EINVAL; |
| 729 | |
| 730 | if (fsize->index >= format->nframes) |
| 731 | return -EINVAL; |
| 732 | |
| 733 | frame = &format->frame[fsize->index]; |
| 734 | fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE; |
| 735 | fsize->discrete.width = frame->wWidth; |
| 736 | fsize->discrete.height = frame->wHeight; |
| 737 | break; |
| 738 | } |
| 739 | |
| 740 | /* Frame interval enumeration */ |
| 741 | case VIDIOC_ENUM_FRAMEINTERVALS: |
| 742 | { |
| 743 | struct v4l2_frmivalenum *fival = arg; |
| 744 | struct uvc_format *format = NULL; |
| 745 | struct uvc_frame *frame = NULL; |
| 746 | int i; |
| 747 | |
| 748 | /* Look for the given pixel format and frame size */ |
| 749 | for (i = 0; i < video->streaming->nformats; i++) { |
| 750 | if (video->streaming->format[i].fcc == |
| 751 | fival->pixel_format) { |
| 752 | format = &video->streaming->format[i]; |
| 753 | break; |
| 754 | } |
| 755 | } |
| 756 | if (format == NULL) |
| 757 | return -EINVAL; |
| 758 | |
| 759 | for (i = 0; i < format->nframes; i++) { |
| 760 | if (format->frame[i].wWidth == fival->width && |
| 761 | format->frame[i].wHeight == fival->height) { |
| 762 | frame = &format->frame[i]; |
| 763 | break; |
| 764 | } |
| 765 | } |
| 766 | if (frame == NULL) |
| 767 | return -EINVAL; |
| 768 | |
| 769 | if (frame->bFrameIntervalType) { |
| 770 | if (fival->index >= frame->bFrameIntervalType) |
| 771 | return -EINVAL; |
| 772 | |
| 773 | fival->type = V4L2_FRMIVAL_TYPE_DISCRETE; |
| 774 | fival->discrete.numerator = |
| 775 | frame->dwFrameInterval[fival->index]; |
| 776 | fival->discrete.denominator = 10000000; |
| 777 | uvc_simplify_fraction(&fival->discrete.numerator, |
| 778 | &fival->discrete.denominator, 8, 333); |
| 779 | } else { |
| 780 | fival->type = V4L2_FRMIVAL_TYPE_STEPWISE; |
| 781 | fival->stepwise.min.numerator = |
| 782 | frame->dwFrameInterval[0]; |
| 783 | fival->stepwise.min.denominator = 10000000; |
| 784 | fival->stepwise.max.numerator = |
| 785 | frame->dwFrameInterval[1]; |
| 786 | fival->stepwise.max.denominator = 10000000; |
| 787 | fival->stepwise.step.numerator = |
| 788 | frame->dwFrameInterval[2]; |
| 789 | fival->stepwise.step.denominator = 10000000; |
| 790 | uvc_simplify_fraction(&fival->stepwise.min.numerator, |
| 791 | &fival->stepwise.min.denominator, 8, 333); |
| 792 | uvc_simplify_fraction(&fival->stepwise.max.numerator, |
| 793 | &fival->stepwise.max.denominator, 8, 333); |
| 794 | uvc_simplify_fraction(&fival->stepwise.step.numerator, |
| 795 | &fival->stepwise.step.denominator, 8, 333); |
| 796 | } |
| 797 | break; |
| 798 | } |
| 799 | |
| 800 | /* Get & Set streaming parameters */ |
| 801 | case VIDIOC_G_PARM: |
| 802 | return uvc_v4l2_get_streamparm(video, arg); |
| 803 | |
| 804 | case VIDIOC_S_PARM: |
| 805 | if ((ret = uvc_acquire_privileges(handle)) < 0) |
| 806 | return ret; |
| 807 | |
| 808 | return uvc_v4l2_set_streamparm(video, arg); |
| 809 | |
| 810 | /* Cropping and scaling */ |
| 811 | case VIDIOC_CROPCAP: |
| 812 | { |
| 813 | struct v4l2_cropcap *ccap = arg; |
| 814 | struct uvc_frame *frame = video->streaming->cur_frame; |
| 815 | |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 816 | if (ccap->type != video->streaming->type) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 817 | return -EINVAL; |
| 818 | |
| 819 | ccap->bounds.left = 0; |
| 820 | ccap->bounds.top = 0; |
| 821 | ccap->bounds.width = frame->wWidth; |
| 822 | ccap->bounds.height = frame->wHeight; |
| 823 | |
| 824 | ccap->defrect = ccap->bounds; |
| 825 | |
| 826 | ccap->pixelaspect.numerator = 1; |
| 827 | ccap->pixelaspect.denominator = 1; |
| 828 | break; |
| 829 | } |
| 830 | |
| 831 | case VIDIOC_G_CROP: |
| 832 | case VIDIOC_S_CROP: |
| 833 | return -EINVAL; |
| 834 | |
| 835 | /* Buffers & streaming */ |
| 836 | case VIDIOC_REQBUFS: |
| 837 | { |
| 838 | struct v4l2_requestbuffers *rb = arg; |
| 839 | unsigned int bufsize = |
| 840 | video->streaming->ctrl.dwMaxVideoFrameSize; |
| 841 | |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 842 | if (rb->type != video->streaming->type || |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 843 | rb->memory != V4L2_MEMORY_MMAP) |
| 844 | return -EINVAL; |
| 845 | |
| 846 | if ((ret = uvc_acquire_privileges(handle)) < 0) |
| 847 | return ret; |
| 848 | |
| 849 | ret = uvc_alloc_buffers(&video->queue, rb->count, bufsize); |
| 850 | if (ret < 0) |
| 851 | return ret; |
| 852 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 853 | rb->count = ret; |
| 854 | ret = 0; |
| 855 | break; |
| 856 | } |
| 857 | |
| 858 | case VIDIOC_QUERYBUF: |
| 859 | { |
| 860 | struct v4l2_buffer *buf = arg; |
| 861 | |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 862 | if (buf->type != video->streaming->type) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 863 | return -EINVAL; |
| 864 | |
| 865 | if (!uvc_has_privileges(handle)) |
| 866 | return -EBUSY; |
| 867 | |
| 868 | return uvc_query_buffer(&video->queue, buf); |
| 869 | } |
| 870 | |
| 871 | case VIDIOC_QBUF: |
| 872 | if (!uvc_has_privileges(handle)) |
| 873 | return -EBUSY; |
| 874 | |
| 875 | return uvc_queue_buffer(&video->queue, arg); |
| 876 | |
| 877 | case VIDIOC_DQBUF: |
| 878 | if (!uvc_has_privileges(handle)) |
| 879 | return -EBUSY; |
| 880 | |
| 881 | return uvc_dequeue_buffer(&video->queue, arg, |
| 882 | file->f_flags & O_NONBLOCK); |
| 883 | |
| 884 | case VIDIOC_STREAMON: |
| 885 | { |
| 886 | int *type = arg; |
| 887 | |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 888 | if (*type != video->streaming->type) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 889 | return -EINVAL; |
| 890 | |
| 891 | if (!uvc_has_privileges(handle)) |
| 892 | return -EBUSY; |
| 893 | |
| 894 | if ((ret = uvc_video_enable(video, 1)) < 0) |
| 895 | return ret; |
| 896 | break; |
| 897 | } |
| 898 | |
| 899 | case VIDIOC_STREAMOFF: |
| 900 | { |
| 901 | int *type = arg; |
| 902 | |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 903 | if (*type != video->streaming->type) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 904 | return -EINVAL; |
| 905 | |
| 906 | if (!uvc_has_privileges(handle)) |
| 907 | return -EBUSY; |
| 908 | |
| 909 | return uvc_video_enable(video, 0); |
| 910 | } |
| 911 | |
| 912 | /* Analog video standards make no sense for digital cameras. */ |
| 913 | case VIDIOC_ENUMSTD: |
| 914 | case VIDIOC_QUERYSTD: |
| 915 | case VIDIOC_G_STD: |
| 916 | case VIDIOC_S_STD: |
| 917 | |
| 918 | case VIDIOC_OVERLAY: |
| 919 | |
| 920 | case VIDIOC_ENUMAUDIO: |
| 921 | case VIDIOC_ENUMAUDOUT: |
| 922 | |
| 923 | case VIDIOC_ENUMOUTPUT: |
| 924 | uvc_trace(UVC_TRACE_IOCTL, "Unsupported ioctl 0x%08x\n", cmd); |
| 925 | return -EINVAL; |
| 926 | |
| 927 | /* Dynamic controls. */ |
| 928 | case UVCIOC_CTRL_ADD: |
| 929 | { |
| 930 | struct uvc_xu_control_info *xinfo = arg; |
| 931 | struct uvc_control_info *info; |
| 932 | |
| 933 | if (!capable(CAP_SYS_ADMIN)) |
| 934 | return -EPERM; |
| 935 | |
Laurent Pinchart | 9768352 | 2008-12-16 06:46:32 -0300 | [diff] [blame] | 936 | info = kzalloc(sizeof *info, GFP_KERNEL); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 937 | if (info == NULL) |
| 938 | return -ENOMEM; |
| 939 | |
| 940 | memcpy(info->entity, xinfo->entity, sizeof info->entity); |
| 941 | info->index = xinfo->index; |
| 942 | info->selector = xinfo->selector; |
| 943 | info->size = xinfo->size; |
| 944 | info->flags = xinfo->flags; |
| 945 | |
| 946 | info->flags |= UVC_CONTROL_GET_MIN | UVC_CONTROL_GET_MAX | |
| 947 | UVC_CONTROL_GET_RES | UVC_CONTROL_GET_DEF; |
| 948 | |
| 949 | ret = uvc_ctrl_add_info(info); |
| 950 | if (ret < 0) |
| 951 | kfree(info); |
| 952 | break; |
| 953 | } |
| 954 | |
| 955 | case UVCIOC_CTRL_MAP: |
| 956 | { |
| 957 | struct uvc_xu_control_mapping *xmap = arg; |
| 958 | struct uvc_control_mapping *map; |
| 959 | |
| 960 | if (!capable(CAP_SYS_ADMIN)) |
| 961 | return -EPERM; |
| 962 | |
Laurent Pinchart | 9768352 | 2008-12-16 06:46:32 -0300 | [diff] [blame] | 963 | map = kzalloc(sizeof *map, GFP_KERNEL); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 964 | if (map == NULL) |
| 965 | return -ENOMEM; |
| 966 | |
| 967 | map->id = xmap->id; |
| 968 | memcpy(map->name, xmap->name, sizeof map->name); |
| 969 | memcpy(map->entity, xmap->entity, sizeof map->entity); |
| 970 | map->selector = xmap->selector; |
| 971 | map->size = xmap->size; |
| 972 | map->offset = xmap->offset; |
| 973 | map->v4l2_type = xmap->v4l2_type; |
| 974 | map->data_type = xmap->data_type; |
| 975 | |
| 976 | ret = uvc_ctrl_add_mapping(map); |
| 977 | if (ret < 0) |
| 978 | kfree(map); |
| 979 | break; |
| 980 | } |
| 981 | |
| 982 | case UVCIOC_CTRL_GET: |
| 983 | return uvc_xu_ctrl_query(video, arg, 0); |
| 984 | |
| 985 | case UVCIOC_CTRL_SET: |
| 986 | return uvc_xu_ctrl_query(video, arg, 1); |
| 987 | |
| 988 | default: |
Mauro Carvalho Chehab | b1f8840 | 2008-10-21 11:27:20 -0300 | [diff] [blame] | 989 | if ((ret = v4l_compat_translate_ioctl(file, cmd, arg, |
Hans Verkuil | f473bf7 | 2008-11-01 08:25:11 -0300 | [diff] [blame] | 990 | uvc_v4l2_do_ioctl)) == -ENOIOCTLCMD) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 991 | uvc_trace(UVC_TRACE_IOCTL, "Unknown ioctl 0x%08x\n", |
| 992 | cmd); |
| 993 | return ret; |
| 994 | } |
| 995 | |
| 996 | return ret; |
| 997 | } |
| 998 | |
Hans Verkuil | 069b747 | 2008-12-30 07:04:34 -0300 | [diff] [blame] | 999 | static long uvc_v4l2_ioctl(struct file *file, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1000 | unsigned int cmd, unsigned long arg) |
| 1001 | { |
Laurent Pinchart | 308bb52 | 2008-11-17 18:32:11 -0300 | [diff] [blame] | 1002 | if (uvc_trace_param & UVC_TRACE_IOCTL) { |
| 1003 | uvc_printk(KERN_DEBUG, "uvc_v4l2_ioctl("); |
| 1004 | v4l_printk_ioctl(cmd); |
| 1005 | printk(")\n"); |
| 1006 | } |
| 1007 | |
Hans Verkuil | f473bf7 | 2008-11-01 08:25:11 -0300 | [diff] [blame] | 1008 | return video_usercopy(file, cmd, arg, uvc_v4l2_do_ioctl); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | static ssize_t uvc_v4l2_read(struct file *file, char __user *data, |
| 1012 | size_t count, loff_t *ppos) |
| 1013 | { |
| 1014 | uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n"); |
| 1015 | return -ENODEV; |
| 1016 | } |
| 1017 | |
| 1018 | /* |
| 1019 | * VMA operations. |
| 1020 | */ |
| 1021 | static void uvc_vm_open(struct vm_area_struct *vma) |
| 1022 | { |
| 1023 | struct uvc_buffer *buffer = vma->vm_private_data; |
| 1024 | buffer->vma_use_count++; |
| 1025 | } |
| 1026 | |
| 1027 | static void uvc_vm_close(struct vm_area_struct *vma) |
| 1028 | { |
| 1029 | struct uvc_buffer *buffer = vma->vm_private_data; |
| 1030 | buffer->vma_use_count--; |
| 1031 | } |
| 1032 | |
| 1033 | static struct vm_operations_struct uvc_vm_ops = { |
| 1034 | .open = uvc_vm_open, |
| 1035 | .close = uvc_vm_close, |
| 1036 | }; |
| 1037 | |
| 1038 | static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma) |
| 1039 | { |
Hans Verkuil | c170ecf | 2008-08-23 08:32:09 -0300 | [diff] [blame] | 1040 | struct uvc_video_device *video = video_drvdata(file); |
Andrew Morton | 6f80e1b | 2008-07-04 06:33:23 -0300 | [diff] [blame] | 1041 | struct uvc_buffer *uninitialized_var(buffer); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1042 | struct page *page; |
| 1043 | unsigned long addr, start, size; |
| 1044 | unsigned int i; |
| 1045 | int ret = 0; |
| 1046 | |
| 1047 | uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n"); |
| 1048 | |
| 1049 | start = vma->vm_start; |
| 1050 | size = vma->vm_end - vma->vm_start; |
| 1051 | |
| 1052 | mutex_lock(&video->queue.mutex); |
| 1053 | |
| 1054 | for (i = 0; i < video->queue.count; ++i) { |
| 1055 | buffer = &video->queue.buffer[i]; |
| 1056 | if ((buffer->buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff) |
| 1057 | break; |
| 1058 | } |
| 1059 | |
| 1060 | if (i == video->queue.count || size != video->queue.buf_size) { |
| 1061 | ret = -EINVAL; |
| 1062 | goto done; |
| 1063 | } |
| 1064 | |
| 1065 | /* |
| 1066 | * VM_IO marks the area as being an mmaped region for I/O to a |
| 1067 | * device. It also prevents the region from being core dumped. |
| 1068 | */ |
| 1069 | vma->vm_flags |= VM_IO; |
| 1070 | |
| 1071 | addr = (unsigned long)video->queue.mem + buffer->buf.m.offset; |
| 1072 | while (size > 0) { |
| 1073 | page = vmalloc_to_page((void *)addr); |
| 1074 | if ((ret = vm_insert_page(vma, start, page)) < 0) |
| 1075 | goto done; |
| 1076 | |
| 1077 | start += PAGE_SIZE; |
| 1078 | addr += PAGE_SIZE; |
| 1079 | size -= PAGE_SIZE; |
| 1080 | } |
| 1081 | |
| 1082 | vma->vm_ops = &uvc_vm_ops; |
| 1083 | vma->vm_private_data = buffer; |
| 1084 | uvc_vm_open(vma); |
| 1085 | |
| 1086 | done: |
| 1087 | mutex_unlock(&video->queue.mutex); |
| 1088 | return ret; |
| 1089 | } |
| 1090 | |
| 1091 | static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait) |
| 1092 | { |
Hans Verkuil | c170ecf | 2008-08-23 08:32:09 -0300 | [diff] [blame] | 1093 | struct uvc_video_device *video = video_drvdata(file); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1094 | |
| 1095 | uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n"); |
| 1096 | |
| 1097 | return uvc_queue_poll(&video->queue, file, wait); |
| 1098 | } |
| 1099 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 1100 | const struct v4l2_file_operations uvc_fops = { |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1101 | .owner = THIS_MODULE, |
| 1102 | .open = uvc_v4l2_open, |
| 1103 | .release = uvc_v4l2_release, |
| 1104 | .ioctl = uvc_v4l2_ioctl, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1105 | .read = uvc_v4l2_read, |
| 1106 | .mmap = uvc_v4l2_mmap, |
| 1107 | .poll = uvc_v4l2_poll, |
| 1108 | }; |
Hans Verkuil | f87086e | 2008-07-18 00:50:58 -0300 | [diff] [blame] | 1109 | |