uvc_v4l2.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  1. /*
  2. * uvc_v4l2.c -- USB Video Class driver - V4L2 API
  3. *
  4. * Copyright (C) 2005-2010
  5. * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/version.h>
  15. #include <linux/list.h>
  16. #include <linux/module.h>
  17. #include <linux/slab.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. #include <media/v4l2-common.h>
  25. #include <media/v4l2-ioctl.h>
  26. #include "uvcvideo.h"
  27. /* ------------------------------------------------------------------------
  28. * UVC ioctls
  29. */
  30. static int uvc_ioctl_ctrl_map(struct uvc_video_chain *chain,
  31. struct uvc_xu_control_mapping *xmap, int old)
  32. {
  33. struct uvc_control_mapping *map;
  34. unsigned int size;
  35. int ret;
  36. map = kzalloc(sizeof *map, GFP_KERNEL);
  37. if (map == NULL)
  38. return -ENOMEM;
  39. map->id = xmap->id;
  40. memcpy(map->name, xmap->name, sizeof map->name);
  41. memcpy(map->entity, xmap->entity, sizeof map->entity);
  42. map->selector = xmap->selector;
  43. map->size = xmap->size;
  44. map->offset = xmap->offset;
  45. map->v4l2_type = xmap->v4l2_type;
  46. map->data_type = xmap->data_type;
  47. switch (xmap->v4l2_type) {
  48. case V4L2_CTRL_TYPE_INTEGER:
  49. case V4L2_CTRL_TYPE_BOOLEAN:
  50. case V4L2_CTRL_TYPE_BUTTON:
  51. break;
  52. case V4L2_CTRL_TYPE_MENU:
  53. if (old) {
  54. uvc_trace(UVC_TRACE_CONTROL, "V4L2_CTRL_TYPE_MENU not "
  55. "supported for UVCIOC_CTRL_MAP_OLD.\n");
  56. ret = -EINVAL;
  57. goto done;
  58. }
  59. size = xmap->menu_count * sizeof(*map->menu_info);
  60. map->menu_info = kmalloc(size, GFP_KERNEL);
  61. if (map->menu_info == NULL) {
  62. ret = -ENOMEM;
  63. goto done;
  64. }
  65. if (copy_from_user(map->menu_info, xmap->menu_info, size)) {
  66. ret = -EFAULT;
  67. goto done;
  68. }
  69. map->menu_count = xmap->menu_count;
  70. break;
  71. default:
  72. uvc_trace(UVC_TRACE_CONTROL, "Unsupported V4L2 control type "
  73. "%u.\n", xmap->v4l2_type);
  74. ret = -EINVAL;
  75. goto done;
  76. }
  77. ret = uvc_ctrl_add_mapping(chain, map);
  78. done:
  79. kfree(map->menu_info);
  80. kfree(map);
  81. return ret;
  82. }
  83. /* ------------------------------------------------------------------------
  84. * V4L2 interface
  85. */
  86. /*
  87. * Find the frame interval closest to the requested frame interval for the
  88. * given frame format and size. This should be done by the device as part of
  89. * the Video Probe and Commit negotiation, but some hardware don't implement
  90. * that feature.
  91. */
  92. static __u32 uvc_try_frame_interval(struct uvc_frame *frame, __u32 interval)
  93. {
  94. unsigned int i;
  95. if (frame->bFrameIntervalType) {
  96. __u32 best = -1, dist;
  97. for (i = 0; i < frame->bFrameIntervalType; ++i) {
  98. dist = interval > frame->dwFrameInterval[i]
  99. ? interval - frame->dwFrameInterval[i]
  100. : frame->dwFrameInterval[i] - interval;
  101. if (dist > best)
  102. break;
  103. best = dist;
  104. }
  105. interval = frame->dwFrameInterval[i-1];
  106. } else {
  107. const __u32 min = frame->dwFrameInterval[0];
  108. const __u32 max = frame->dwFrameInterval[1];
  109. const __u32 step = frame->dwFrameInterval[2];
  110. interval = min + (interval - min + step/2) / step * step;
  111. if (interval > max)
  112. interval = max;
  113. }
  114. return interval;
  115. }
  116. static int uvc_v4l2_try_format(struct uvc_streaming *stream,
  117. struct v4l2_format *fmt, struct uvc_streaming_control *probe,
  118. struct uvc_format **uvc_format, struct uvc_frame **uvc_frame)
  119. {
  120. struct uvc_format *format = NULL;
  121. struct uvc_frame *frame = NULL;
  122. __u16 rw, rh;
  123. unsigned int d, maxd;
  124. unsigned int i;
  125. __u32 interval;
  126. int ret = 0;
  127. __u8 *fcc;
  128. if (fmt->type != stream->type)
  129. return -EINVAL;
  130. fcc = (__u8 *)&fmt->fmt.pix.pixelformat;
  131. uvc_trace(UVC_TRACE_FORMAT, "Trying format 0x%08x (%c%c%c%c): %ux%u.\n",
  132. fmt->fmt.pix.pixelformat,
  133. fcc[0], fcc[1], fcc[2], fcc[3],
  134. fmt->fmt.pix.width, fmt->fmt.pix.height);
  135. /* Check if the hardware supports the requested format. */
  136. for (i = 0; i < stream->nformats; ++i) {
  137. format = &stream->format[i];
  138. if (format->fcc == fmt->fmt.pix.pixelformat)
  139. break;
  140. }
  141. if (format == NULL || format->fcc != fmt->fmt.pix.pixelformat) {
  142. uvc_trace(UVC_TRACE_FORMAT, "Unsupported format 0x%08x.\n",
  143. fmt->fmt.pix.pixelformat);
  144. return -EINVAL;
  145. }
  146. /* Find the closest image size. The distance between image sizes is
  147. * the size in pixels of the non-overlapping regions between the
  148. * requested size and the frame-specified size.
  149. */
  150. rw = fmt->fmt.pix.width;
  151. rh = fmt->fmt.pix.height;
  152. maxd = (unsigned int)-1;
  153. for (i = 0; i < format->nframes; ++i) {
  154. __u16 w = format->frame[i].wWidth;
  155. __u16 h = format->frame[i].wHeight;
  156. d = min(w, rw) * min(h, rh);
  157. d = w*h + rw*rh - 2*d;
  158. if (d < maxd) {
  159. maxd = d;
  160. frame = &format->frame[i];
  161. }
  162. if (maxd == 0)
  163. break;
  164. }
  165. if (frame == NULL) {
  166. uvc_trace(UVC_TRACE_FORMAT, "Unsupported size %ux%u.\n",
  167. fmt->fmt.pix.width, fmt->fmt.pix.height);
  168. return -EINVAL;
  169. }
  170. /* Use the default frame interval. */
  171. interval = frame->dwDefaultFrameInterval;
  172. uvc_trace(UVC_TRACE_FORMAT, "Using default frame interval %u.%u us "
  173. "(%u.%u fps).\n", interval/10, interval%10, 10000000/interval,
  174. (100000000/interval)%10);
  175. /* Set the format index, frame index and frame interval. */
  176. memset(probe, 0, sizeof *probe);
  177. probe->bmHint = 1; /* dwFrameInterval */
  178. probe->bFormatIndex = format->index;
  179. probe->bFrameIndex = frame->bFrameIndex;
  180. probe->dwFrameInterval = uvc_try_frame_interval(frame, interval);
  181. /* Some webcams stall the probe control set request when the
  182. * dwMaxVideoFrameSize field is set to zero. The UVC specification
  183. * clearly states that the field is read-only from the host, so this
  184. * is a webcam bug. Set dwMaxVideoFrameSize to the value reported by
  185. * the webcam to work around the problem.
  186. *
  187. * The workaround could probably be enabled for all webcams, so the
  188. * quirk can be removed if needed. It's currently useful to detect
  189. * webcam bugs and fix them before they hit the market (providing
  190. * developers test their webcams with the Linux driver as well as with
  191. * the Windows driver).
  192. */
  193. mutex_lock(&stream->mutex);
  194. if (stream->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS)
  195. probe->dwMaxVideoFrameSize =
  196. stream->ctrl.dwMaxVideoFrameSize;
  197. /* Probe the device. */
  198. ret = uvc_probe_video(stream, probe);
  199. mutex_unlock(&stream->mutex);
  200. if (ret < 0)
  201. goto done;
  202. fmt->fmt.pix.width = frame->wWidth;
  203. fmt->fmt.pix.height = frame->wHeight;
  204. fmt->fmt.pix.field = V4L2_FIELD_NONE;
  205. fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
  206. fmt->fmt.pix.sizeimage = probe->dwMaxVideoFrameSize;
  207. fmt->fmt.pix.colorspace = format->colorspace;
  208. fmt->fmt.pix.priv = 0;
  209. if (uvc_format != NULL)
  210. *uvc_format = format;
  211. if (uvc_frame != NULL)
  212. *uvc_frame = frame;
  213. done:
  214. return ret;
  215. }
  216. static int uvc_v4l2_get_format(struct uvc_streaming *stream,
  217. struct v4l2_format *fmt)
  218. {
  219. struct uvc_format *format;
  220. struct uvc_frame *frame;
  221. int ret = 0;
  222. if (fmt->type != stream->type)
  223. return -EINVAL;
  224. mutex_lock(&stream->mutex);
  225. format = stream->cur_format;
  226. frame = stream->cur_frame;
  227. if (format == NULL || frame == NULL) {
  228. ret = -EINVAL;
  229. goto done;
  230. }
  231. fmt->fmt.pix.pixelformat = format->fcc;
  232. fmt->fmt.pix.width = frame->wWidth;
  233. fmt->fmt.pix.height = frame->wHeight;
  234. fmt->fmt.pix.field = V4L2_FIELD_NONE;
  235. fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
  236. fmt->fmt.pix.sizeimage = stream->ctrl.dwMaxVideoFrameSize;
  237. fmt->fmt.pix.colorspace = format->colorspace;
  238. fmt->fmt.pix.priv = 0;
  239. done:
  240. mutex_unlock(&stream->mutex);
  241. return ret;
  242. }
  243. static int uvc_v4l2_set_format(struct uvc_streaming *stream,
  244. struct v4l2_format *fmt)
  245. {
  246. struct uvc_streaming_control probe;
  247. struct uvc_format *format;
  248. struct uvc_frame *frame;
  249. int ret;
  250. if (fmt->type != stream->type)
  251. return -EINVAL;
  252. ret = uvc_v4l2_try_format(stream, fmt, &probe, &format, &frame);
  253. if (ret < 0)
  254. return ret;
  255. mutex_lock(&stream->mutex);
  256. if (uvc_queue_allocated(&stream->queue)) {
  257. ret = -EBUSY;
  258. goto done;
  259. }
  260. memcpy(&stream->ctrl, &probe, sizeof probe);
  261. stream->cur_format = format;
  262. stream->cur_frame = frame;
  263. done:
  264. mutex_unlock(&stream->mutex);
  265. return ret;
  266. }
  267. static int uvc_v4l2_get_streamparm(struct uvc_streaming *stream,
  268. struct v4l2_streamparm *parm)
  269. {
  270. uint32_t numerator, denominator;
  271. if (parm->type != stream->type)
  272. return -EINVAL;
  273. mutex_lock(&stream->mutex);
  274. numerator = stream->ctrl.dwFrameInterval;
  275. mutex_unlock(&stream->mutex);
  276. denominator = 10000000;
  277. uvc_simplify_fraction(&numerator, &denominator, 8, 333);
  278. memset(parm, 0, sizeof *parm);
  279. parm->type = stream->type;
  280. if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  281. parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
  282. parm->parm.capture.capturemode = 0;
  283. parm->parm.capture.timeperframe.numerator = numerator;
  284. parm->parm.capture.timeperframe.denominator = denominator;
  285. parm->parm.capture.extendedmode = 0;
  286. parm->parm.capture.readbuffers = 0;
  287. } else {
  288. parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
  289. parm->parm.output.outputmode = 0;
  290. parm->parm.output.timeperframe.numerator = numerator;
  291. parm->parm.output.timeperframe.denominator = denominator;
  292. }
  293. return 0;
  294. }
  295. static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream,
  296. struct v4l2_streamparm *parm)
  297. {
  298. struct uvc_streaming_control probe;
  299. struct v4l2_fract timeperframe;
  300. uint32_t interval;
  301. int ret;
  302. if (parm->type != stream->type)
  303. return -EINVAL;
  304. if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  305. timeperframe = parm->parm.capture.timeperframe;
  306. else
  307. timeperframe = parm->parm.output.timeperframe;
  308. interval = uvc_fraction_to_interval(timeperframe.numerator,
  309. timeperframe.denominator);
  310. uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n",
  311. timeperframe.numerator, timeperframe.denominator, interval);
  312. mutex_lock(&stream->mutex);
  313. if (uvc_queue_streaming(&stream->queue)) {
  314. mutex_unlock(&stream->mutex);
  315. return -EBUSY;
  316. }
  317. memcpy(&probe, &stream->ctrl, sizeof probe);
  318. probe.dwFrameInterval =
  319. uvc_try_frame_interval(stream->cur_frame, interval);
  320. /* Probe the device with the new settings. */
  321. ret = uvc_probe_video(stream, &probe);
  322. if (ret < 0) {
  323. mutex_unlock(&stream->mutex);
  324. return ret;
  325. }
  326. memcpy(&stream->ctrl, &probe, sizeof probe);
  327. mutex_unlock(&stream->mutex);
  328. /* Return the actual frame period. */
  329. timeperframe.numerator = probe.dwFrameInterval;
  330. timeperframe.denominator = 10000000;
  331. uvc_simplify_fraction(&timeperframe.numerator,
  332. &timeperframe.denominator, 8, 333);
  333. if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  334. parm->parm.capture.timeperframe = timeperframe;
  335. else
  336. parm->parm.output.timeperframe = timeperframe;
  337. return 0;
  338. }
  339. /* ------------------------------------------------------------------------
  340. * Privilege management
  341. */
  342. /*
  343. * Privilege management is the multiple-open implementation basis. The current
  344. * implementation is completely transparent for the end-user and doesn't
  345. * require explicit use of the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls.
  346. * Those ioctls enable finer control on the device (by making possible for a
  347. * user to request exclusive access to a device), but are not mature yet.
  348. * Switching to the V4L2 priority mechanism might be considered in the future
  349. * if this situation changes.
  350. *
  351. * Each open instance of a UVC device can either be in a privileged or
  352. * unprivileged state. Only a single instance can be in a privileged state at
  353. * a given time. Trying to perform an operation that requires privileges will
  354. * automatically acquire the required privileges if possible, or return -EBUSY
  355. * otherwise. Privileges are dismissed when closing the instance or when
  356. * freeing the video buffers using VIDIOC_REQBUFS.
  357. *
  358. * Operations that require privileges are:
  359. *
  360. * - VIDIOC_S_INPUT
  361. * - VIDIOC_S_PARM
  362. * - VIDIOC_S_FMT
  363. * - VIDIOC_REQBUFS
  364. */
  365. static int uvc_acquire_privileges(struct uvc_fh *handle)
  366. {
  367. /* Always succeed if the handle is already privileged. */
  368. if (handle->state == UVC_HANDLE_ACTIVE)
  369. return 0;
  370. /* Check if the device already has a privileged handle. */
  371. if (atomic_inc_return(&handle->stream->active) != 1) {
  372. atomic_dec(&handle->stream->active);
  373. return -EBUSY;
  374. }
  375. handle->state = UVC_HANDLE_ACTIVE;
  376. return 0;
  377. }
  378. static void uvc_dismiss_privileges(struct uvc_fh *handle)
  379. {
  380. if (handle->state == UVC_HANDLE_ACTIVE)
  381. atomic_dec(&handle->stream->active);
  382. handle->state = UVC_HANDLE_PASSIVE;
  383. }
  384. static int uvc_has_privileges(struct uvc_fh *handle)
  385. {
  386. return handle->state == UVC_HANDLE_ACTIVE;
  387. }
  388. /* ------------------------------------------------------------------------
  389. * V4L2 file operations
  390. */
  391. static int uvc_v4l2_open(struct file *file)
  392. {
  393. struct uvc_streaming *stream;
  394. struct uvc_fh *handle;
  395. int ret = 0;
  396. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n");
  397. stream = video_drvdata(file);
  398. if (stream->dev->state & UVC_DEV_DISCONNECTED)
  399. return -ENODEV;
  400. ret = usb_autopm_get_interface(stream->dev->intf);
  401. if (ret < 0)
  402. return ret;
  403. /* Create the device handle. */
  404. handle = kzalloc(sizeof *handle, GFP_KERNEL);
  405. if (handle == NULL) {
  406. usb_autopm_put_interface(stream->dev->intf);
  407. return -ENOMEM;
  408. }
  409. if (atomic_inc_return(&stream->dev->users) == 1) {
  410. ret = uvc_status_start(stream->dev);
  411. if (ret < 0) {
  412. usb_autopm_put_interface(stream->dev->intf);
  413. atomic_dec(&stream->dev->users);
  414. kfree(handle);
  415. return ret;
  416. }
  417. }
  418. handle->chain = stream->chain;
  419. handle->stream = stream;
  420. handle->state = UVC_HANDLE_PASSIVE;
  421. file->private_data = handle;
  422. return 0;
  423. }
  424. static int uvc_v4l2_release(struct file *file)
  425. {
  426. struct uvc_fh *handle = file->private_data;
  427. struct uvc_streaming *stream = handle->stream;
  428. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n");
  429. /* Only free resources if this is a privileged handle. */
  430. if (uvc_has_privileges(handle)) {
  431. uvc_video_enable(stream, 0);
  432. if (uvc_free_buffers(&stream->queue) < 0)
  433. uvc_printk(KERN_ERR, "uvc_v4l2_release: Unable to "
  434. "free buffers.\n");
  435. }
  436. /* Release the file handle. */
  437. uvc_dismiss_privileges(handle);
  438. kfree(handle);
  439. file->private_data = NULL;
  440. if (atomic_dec_return(&stream->dev->users) == 0)
  441. uvc_status_stop(stream->dev);
  442. usb_autopm_put_interface(stream->dev->intf);
  443. return 0;
  444. }
  445. static long uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
  446. {
  447. struct video_device *vdev = video_devdata(file);
  448. struct uvc_fh *handle = file->private_data;
  449. struct uvc_video_chain *chain = handle->chain;
  450. struct uvc_streaming *stream = handle->stream;
  451. long ret = 0;
  452. switch (cmd) {
  453. /* Query capabilities */
  454. case VIDIOC_QUERYCAP:
  455. {
  456. struct v4l2_capability *cap = arg;
  457. memset(cap, 0, sizeof *cap);
  458. strlcpy(cap->driver, "uvcvideo", sizeof cap->driver);
  459. strlcpy(cap->card, vdev->name, sizeof cap->card);
  460. usb_make_path(stream->dev->udev,
  461. cap->bus_info, sizeof(cap->bus_info));
  462. cap->version = DRIVER_VERSION_NUMBER;
  463. if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  464. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
  465. | V4L2_CAP_STREAMING;
  466. else
  467. cap->capabilities = V4L2_CAP_VIDEO_OUTPUT
  468. | V4L2_CAP_STREAMING;
  469. break;
  470. }
  471. /* Get, Set & Query control */
  472. case VIDIOC_QUERYCTRL:
  473. return uvc_query_v4l2_ctrl(chain, arg);
  474. case VIDIOC_G_CTRL:
  475. {
  476. struct v4l2_control *ctrl = arg;
  477. struct v4l2_ext_control xctrl;
  478. memset(&xctrl, 0, sizeof xctrl);
  479. xctrl.id = ctrl->id;
  480. ret = uvc_ctrl_begin(chain);
  481. if (ret < 0)
  482. return ret;
  483. ret = uvc_ctrl_get(chain, &xctrl);
  484. uvc_ctrl_rollback(chain);
  485. if (ret >= 0)
  486. ctrl->value = xctrl.value;
  487. break;
  488. }
  489. case VIDIOC_S_CTRL:
  490. {
  491. struct v4l2_control *ctrl = arg;
  492. struct v4l2_ext_control xctrl;
  493. memset(&xctrl, 0, sizeof xctrl);
  494. xctrl.id = ctrl->id;
  495. xctrl.value = ctrl->value;
  496. ret = uvc_ctrl_begin(chain);
  497. if (ret < 0)
  498. return ret;
  499. ret = uvc_ctrl_set(chain, &xctrl);
  500. if (ret < 0) {
  501. uvc_ctrl_rollback(chain);
  502. return ret;
  503. }
  504. ret = uvc_ctrl_commit(chain);
  505. if (ret == 0)
  506. ctrl->value = xctrl.value;
  507. break;
  508. }
  509. case VIDIOC_QUERYMENU:
  510. return uvc_query_v4l2_menu(chain, arg);
  511. case VIDIOC_G_EXT_CTRLS:
  512. {
  513. struct v4l2_ext_controls *ctrls = arg;
  514. struct v4l2_ext_control *ctrl = ctrls->controls;
  515. unsigned int i;
  516. ret = uvc_ctrl_begin(chain);
  517. if (ret < 0)
  518. return ret;
  519. for (i = 0; i < ctrls->count; ++ctrl, ++i) {
  520. ret = uvc_ctrl_get(chain, ctrl);
  521. if (ret < 0) {
  522. uvc_ctrl_rollback(chain);
  523. ctrls->error_idx = i;
  524. return ret;
  525. }
  526. }
  527. ctrls->error_idx = 0;
  528. ret = uvc_ctrl_rollback(chain);
  529. break;
  530. }
  531. case VIDIOC_S_EXT_CTRLS:
  532. case VIDIOC_TRY_EXT_CTRLS:
  533. {
  534. struct v4l2_ext_controls *ctrls = arg;
  535. struct v4l2_ext_control *ctrl = ctrls->controls;
  536. unsigned int i;
  537. ret = uvc_ctrl_begin(chain);
  538. if (ret < 0)
  539. return ret;
  540. for (i = 0; i < ctrls->count; ++ctrl, ++i) {
  541. ret = uvc_ctrl_set(chain, ctrl);
  542. if (ret < 0) {
  543. uvc_ctrl_rollback(chain);
  544. ctrls->error_idx = i;
  545. return ret;
  546. }
  547. }
  548. ctrls->error_idx = 0;
  549. if (cmd == VIDIOC_S_EXT_CTRLS)
  550. ret = uvc_ctrl_commit(chain);
  551. else
  552. ret = uvc_ctrl_rollback(chain);
  553. break;
  554. }
  555. /* Get, Set & Enum input */
  556. case VIDIOC_ENUMINPUT:
  557. {
  558. const struct uvc_entity *selector = chain->selector;
  559. struct v4l2_input *input = arg;
  560. struct uvc_entity *iterm = NULL;
  561. u32 index = input->index;
  562. int pin = 0;
  563. if (selector == NULL ||
  564. (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
  565. if (index != 0)
  566. return -EINVAL;
  567. list_for_each_entry(iterm, &chain->entities, chain) {
  568. if (UVC_ENTITY_IS_ITERM(iterm))
  569. break;
  570. }
  571. pin = iterm->id;
  572. } else if (pin < selector->bNrInPins) {
  573. pin = selector->baSourceID[index];
  574. list_for_each_entry(iterm, &chain->entities, chain) {
  575. if (!UVC_ENTITY_IS_ITERM(iterm))
  576. continue;
  577. if (iterm->id == pin)
  578. break;
  579. }
  580. }
  581. if (iterm == NULL || iterm->id != pin)
  582. return -EINVAL;
  583. memset(input, 0, sizeof *input);
  584. input->index = index;
  585. strlcpy(input->name, iterm->name, sizeof input->name);
  586. if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA)
  587. input->type = V4L2_INPUT_TYPE_CAMERA;
  588. break;
  589. }
  590. case VIDIOC_G_INPUT:
  591. {
  592. u8 input;
  593. if (chain->selector == NULL ||
  594. (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
  595. *(int *)arg = 0;
  596. break;
  597. }
  598. ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
  599. chain->selector->id, chain->dev->intfnum,
  600. UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
  601. if (ret < 0)
  602. return ret;
  603. *(int *)arg = input - 1;
  604. break;
  605. }
  606. case VIDIOC_S_INPUT:
  607. {
  608. u32 input = *(u32 *)arg + 1;
  609. if ((ret = uvc_acquire_privileges(handle)) < 0)
  610. return ret;
  611. if (chain->selector == NULL ||
  612. (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
  613. if (input != 1)
  614. return -EINVAL;
  615. break;
  616. }
  617. if (input == 0 || input > chain->selector->bNrInPins)
  618. return -EINVAL;
  619. return uvc_query_ctrl(chain->dev, UVC_SET_CUR,
  620. chain->selector->id, chain->dev->intfnum,
  621. UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
  622. }
  623. /* Try, Get, Set & Enum format */
  624. case VIDIOC_ENUM_FMT:
  625. {
  626. struct v4l2_fmtdesc *fmt = arg;
  627. struct uvc_format *format;
  628. enum v4l2_buf_type type = fmt->type;
  629. __u32 index = fmt->index;
  630. if (fmt->type != stream->type ||
  631. fmt->index >= stream->nformats)
  632. return -EINVAL;
  633. memset(fmt, 0, sizeof(*fmt));
  634. fmt->index = index;
  635. fmt->type = type;
  636. format = &stream->format[fmt->index];
  637. fmt->flags = 0;
  638. if (format->flags & UVC_FMT_FLAG_COMPRESSED)
  639. fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
  640. strlcpy(fmt->description, format->name,
  641. sizeof fmt->description);
  642. fmt->description[sizeof fmt->description - 1] = 0;
  643. fmt->pixelformat = format->fcc;
  644. break;
  645. }
  646. case VIDIOC_TRY_FMT:
  647. {
  648. struct uvc_streaming_control probe;
  649. return uvc_v4l2_try_format(stream, arg, &probe, NULL, NULL);
  650. }
  651. case VIDIOC_S_FMT:
  652. if ((ret = uvc_acquire_privileges(handle)) < 0)
  653. return ret;
  654. return uvc_v4l2_set_format(stream, arg);
  655. case VIDIOC_G_FMT:
  656. return uvc_v4l2_get_format(stream, arg);
  657. /* Frame size enumeration */
  658. case VIDIOC_ENUM_FRAMESIZES:
  659. {
  660. struct v4l2_frmsizeenum *fsize = arg;
  661. struct uvc_format *format = NULL;
  662. struct uvc_frame *frame;
  663. int i;
  664. /* Look for the given pixel format */
  665. for (i = 0; i < stream->nformats; i++) {
  666. if (stream->format[i].fcc ==
  667. fsize->pixel_format) {
  668. format = &stream->format[i];
  669. break;
  670. }
  671. }
  672. if (format == NULL)
  673. return -EINVAL;
  674. if (fsize->index >= format->nframes)
  675. return -EINVAL;
  676. frame = &format->frame[fsize->index];
  677. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  678. fsize->discrete.width = frame->wWidth;
  679. fsize->discrete.height = frame->wHeight;
  680. break;
  681. }
  682. /* Frame interval enumeration */
  683. case VIDIOC_ENUM_FRAMEINTERVALS:
  684. {
  685. struct v4l2_frmivalenum *fival = arg;
  686. struct uvc_format *format = NULL;
  687. struct uvc_frame *frame = NULL;
  688. int i;
  689. /* Look for the given pixel format and frame size */
  690. for (i = 0; i < stream->nformats; i++) {
  691. if (stream->format[i].fcc ==
  692. fival->pixel_format) {
  693. format = &stream->format[i];
  694. break;
  695. }
  696. }
  697. if (format == NULL)
  698. return -EINVAL;
  699. for (i = 0; i < format->nframes; i++) {
  700. if (format->frame[i].wWidth == fival->width &&
  701. format->frame[i].wHeight == fival->height) {
  702. frame = &format->frame[i];
  703. break;
  704. }
  705. }
  706. if (frame == NULL)
  707. return -EINVAL;
  708. if (frame->bFrameIntervalType) {
  709. if (fival->index >= frame->bFrameIntervalType)
  710. return -EINVAL;
  711. fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
  712. fival->discrete.numerator =
  713. frame->dwFrameInterval[fival->index];
  714. fival->discrete.denominator = 10000000;
  715. uvc_simplify_fraction(&fival->discrete.numerator,
  716. &fival->discrete.denominator, 8, 333);
  717. } else {
  718. fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
  719. fival->stepwise.min.numerator =
  720. frame->dwFrameInterval[0];
  721. fival->stepwise.min.denominator = 10000000;
  722. fival->stepwise.max.numerator =
  723. frame->dwFrameInterval[1];
  724. fival->stepwise.max.denominator = 10000000;
  725. fival->stepwise.step.numerator =
  726. frame->dwFrameInterval[2];
  727. fival->stepwise.step.denominator = 10000000;
  728. uvc_simplify_fraction(&fival->stepwise.min.numerator,
  729. &fival->stepwise.min.denominator, 8, 333);
  730. uvc_simplify_fraction(&fival->stepwise.max.numerator,
  731. &fival->stepwise.max.denominator, 8, 333);
  732. uvc_simplify_fraction(&fival->stepwise.step.numerator,
  733. &fival->stepwise.step.denominator, 8, 333);
  734. }
  735. break;
  736. }
  737. /* Get & Set streaming parameters */
  738. case VIDIOC_G_PARM:
  739. return uvc_v4l2_get_streamparm(stream, arg);
  740. case VIDIOC_S_PARM:
  741. if ((ret = uvc_acquire_privileges(handle)) < 0)
  742. return ret;
  743. return uvc_v4l2_set_streamparm(stream, arg);
  744. /* Cropping and scaling */
  745. case VIDIOC_CROPCAP:
  746. {
  747. struct v4l2_cropcap *ccap = arg;
  748. if (ccap->type != stream->type)
  749. return -EINVAL;
  750. ccap->bounds.left = 0;
  751. ccap->bounds.top = 0;
  752. mutex_lock(&stream->mutex);
  753. ccap->bounds.width = stream->cur_frame->wWidth;
  754. ccap->bounds.height = stream->cur_frame->wHeight;
  755. mutex_unlock(&stream->mutex);
  756. ccap->defrect = ccap->bounds;
  757. ccap->pixelaspect.numerator = 1;
  758. ccap->pixelaspect.denominator = 1;
  759. break;
  760. }
  761. case VIDIOC_G_CROP:
  762. case VIDIOC_S_CROP:
  763. return -EINVAL;
  764. /* Buffers & streaming */
  765. case VIDIOC_REQBUFS:
  766. {
  767. struct v4l2_requestbuffers *rb = arg;
  768. if (rb->type != stream->type ||
  769. rb->memory != V4L2_MEMORY_MMAP)
  770. return -EINVAL;
  771. if ((ret = uvc_acquire_privileges(handle)) < 0)
  772. return ret;
  773. mutex_lock(&stream->mutex);
  774. ret = uvc_alloc_buffers(&stream->queue, rb->count,
  775. stream->ctrl.dwMaxVideoFrameSize);
  776. mutex_unlock(&stream->mutex);
  777. if (ret < 0)
  778. return ret;
  779. if (ret == 0)
  780. uvc_dismiss_privileges(handle);
  781. rb->count = ret;
  782. ret = 0;
  783. break;
  784. }
  785. case VIDIOC_QUERYBUF:
  786. {
  787. struct v4l2_buffer *buf = arg;
  788. if (buf->type != stream->type)
  789. return -EINVAL;
  790. if (!uvc_has_privileges(handle))
  791. return -EBUSY;
  792. return uvc_query_buffer(&stream->queue, buf);
  793. }
  794. case VIDIOC_QBUF:
  795. if (!uvc_has_privileges(handle))
  796. return -EBUSY;
  797. return uvc_queue_buffer(&stream->queue, arg);
  798. case VIDIOC_DQBUF:
  799. if (!uvc_has_privileges(handle))
  800. return -EBUSY;
  801. return uvc_dequeue_buffer(&stream->queue, arg,
  802. file->f_flags & O_NONBLOCK);
  803. case VIDIOC_STREAMON:
  804. {
  805. int *type = arg;
  806. if (*type != stream->type)
  807. return -EINVAL;
  808. if (!uvc_has_privileges(handle))
  809. return -EBUSY;
  810. mutex_lock(&stream->mutex);
  811. ret = uvc_video_enable(stream, 1);
  812. mutex_unlock(&stream->mutex);
  813. if (ret < 0)
  814. return ret;
  815. break;
  816. }
  817. case VIDIOC_STREAMOFF:
  818. {
  819. int *type = arg;
  820. if (*type != stream->type)
  821. return -EINVAL;
  822. if (!uvc_has_privileges(handle))
  823. return -EBUSY;
  824. return uvc_video_enable(stream, 0);
  825. }
  826. /* Analog video standards make no sense for digital cameras. */
  827. case VIDIOC_ENUMSTD:
  828. case VIDIOC_QUERYSTD:
  829. case VIDIOC_G_STD:
  830. case VIDIOC_S_STD:
  831. case VIDIOC_OVERLAY:
  832. case VIDIOC_ENUMAUDIO:
  833. case VIDIOC_ENUMAUDOUT:
  834. case VIDIOC_ENUMOUTPUT:
  835. uvc_trace(UVC_TRACE_IOCTL, "Unsupported ioctl 0x%08x\n", cmd);
  836. return -EINVAL;
  837. /* Dynamic controls. */
  838. case UVCIOC_CTRL_ADD:
  839. /* Legacy ioctl, kept for API compatibility reasons */
  840. return -EEXIST;
  841. case UVCIOC_CTRL_MAP_OLD:
  842. case UVCIOC_CTRL_MAP:
  843. return uvc_ioctl_ctrl_map(chain, arg,
  844. cmd == UVCIOC_CTRL_MAP_OLD);
  845. case UVCIOC_CTRL_GET:
  846. return uvc_xu_ctrl_query(chain, arg, 0);
  847. case UVCIOC_CTRL_SET:
  848. return uvc_xu_ctrl_query(chain, arg, 1);
  849. default:
  850. uvc_trace(UVC_TRACE_IOCTL, "Unknown ioctl 0x%08x\n", cmd);
  851. return -EINVAL;
  852. }
  853. return ret;
  854. }
  855. static long uvc_v4l2_ioctl(struct file *file,
  856. unsigned int cmd, unsigned long arg)
  857. {
  858. if (uvc_trace_param & UVC_TRACE_IOCTL) {
  859. uvc_printk(KERN_DEBUG, "uvc_v4l2_ioctl(");
  860. v4l_printk_ioctl(cmd);
  861. printk(")\n");
  862. }
  863. return video_usercopy(file, cmd, arg, uvc_v4l2_do_ioctl);
  864. }
  865. static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
  866. size_t count, loff_t *ppos)
  867. {
  868. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
  869. return -EINVAL;
  870. }
  871. static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
  872. {
  873. struct uvc_fh *handle = file->private_data;
  874. struct uvc_streaming *stream = handle->stream;
  875. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
  876. return uvc_queue_mmap(&stream->queue, vma);
  877. }
  878. static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait)
  879. {
  880. struct uvc_fh *handle = file->private_data;
  881. struct uvc_streaming *stream = handle->stream;
  882. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
  883. return uvc_queue_poll(&stream->queue, file, wait);
  884. }
  885. const struct v4l2_file_operations uvc_fops = {
  886. .owner = THIS_MODULE,
  887. .open = uvc_v4l2_open,
  888. .release = uvc_v4l2_release,
  889. .unlocked_ioctl = uvc_v4l2_ioctl,
  890. .read = uvc_v4l2_read,
  891. .mmap = uvc_v4l2_mmap,
  892. .poll = uvc_v4l2_poll,
  893. };