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