uvc_v4l2.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  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. if (stream->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS)
  194. probe->dwMaxVideoFrameSize =
  195. stream->ctrl.dwMaxVideoFrameSize;
  196. /* Probe the device. */
  197. ret = uvc_probe_video(stream, probe);
  198. if (ret < 0)
  199. goto done;
  200. fmt->fmt.pix.width = frame->wWidth;
  201. fmt->fmt.pix.height = frame->wHeight;
  202. fmt->fmt.pix.field = V4L2_FIELD_NONE;
  203. fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
  204. fmt->fmt.pix.sizeimage = probe->dwMaxVideoFrameSize;
  205. fmt->fmt.pix.colorspace = format->colorspace;
  206. fmt->fmt.pix.priv = 0;
  207. if (uvc_format != NULL)
  208. *uvc_format = format;
  209. if (uvc_frame != NULL)
  210. *uvc_frame = frame;
  211. done:
  212. return ret;
  213. }
  214. static int uvc_v4l2_get_format(struct uvc_streaming *stream,
  215. struct v4l2_format *fmt)
  216. {
  217. struct uvc_format *format = stream->cur_format;
  218. struct uvc_frame *frame = stream->cur_frame;
  219. if (fmt->type != stream->type)
  220. return -EINVAL;
  221. if (format == NULL || frame == NULL)
  222. return -EINVAL;
  223. fmt->fmt.pix.pixelformat = format->fcc;
  224. fmt->fmt.pix.width = frame->wWidth;
  225. fmt->fmt.pix.height = frame->wHeight;
  226. fmt->fmt.pix.field = V4L2_FIELD_NONE;
  227. fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
  228. fmt->fmt.pix.sizeimage = stream->ctrl.dwMaxVideoFrameSize;
  229. fmt->fmt.pix.colorspace = format->colorspace;
  230. fmt->fmt.pix.priv = 0;
  231. return 0;
  232. }
  233. static int uvc_v4l2_set_format(struct uvc_streaming *stream,
  234. struct v4l2_format *fmt)
  235. {
  236. struct uvc_streaming_control probe;
  237. struct uvc_format *format;
  238. struct uvc_frame *frame;
  239. int ret;
  240. if (fmt->type != stream->type)
  241. return -EINVAL;
  242. if (uvc_queue_allocated(&stream->queue))
  243. return -EBUSY;
  244. ret = uvc_v4l2_try_format(stream, fmt, &probe, &format, &frame);
  245. if (ret < 0)
  246. return ret;
  247. memcpy(&stream->ctrl, &probe, sizeof probe);
  248. stream->cur_format = format;
  249. stream->cur_frame = frame;
  250. return 0;
  251. }
  252. static int uvc_v4l2_get_streamparm(struct uvc_streaming *stream,
  253. struct v4l2_streamparm *parm)
  254. {
  255. uint32_t numerator, denominator;
  256. if (parm->type != stream->type)
  257. return -EINVAL;
  258. numerator = stream->ctrl.dwFrameInterval;
  259. denominator = 10000000;
  260. uvc_simplify_fraction(&numerator, &denominator, 8, 333);
  261. memset(parm, 0, sizeof *parm);
  262. parm->type = stream->type;
  263. if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  264. parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
  265. parm->parm.capture.capturemode = 0;
  266. parm->parm.capture.timeperframe.numerator = numerator;
  267. parm->parm.capture.timeperframe.denominator = denominator;
  268. parm->parm.capture.extendedmode = 0;
  269. parm->parm.capture.readbuffers = 0;
  270. } else {
  271. parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
  272. parm->parm.output.outputmode = 0;
  273. parm->parm.output.timeperframe.numerator = numerator;
  274. parm->parm.output.timeperframe.denominator = denominator;
  275. }
  276. return 0;
  277. }
  278. static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream,
  279. struct v4l2_streamparm *parm)
  280. {
  281. struct uvc_frame *frame = stream->cur_frame;
  282. struct uvc_streaming_control probe;
  283. struct v4l2_fract timeperframe;
  284. uint32_t interval;
  285. int ret;
  286. if (parm->type != stream->type)
  287. return -EINVAL;
  288. if (uvc_queue_streaming(&stream->queue))
  289. return -EBUSY;
  290. if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  291. timeperframe = parm->parm.capture.timeperframe;
  292. else
  293. timeperframe = parm->parm.output.timeperframe;
  294. memcpy(&probe, &stream->ctrl, sizeof probe);
  295. interval = uvc_fraction_to_interval(timeperframe.numerator,
  296. timeperframe.denominator);
  297. uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n",
  298. timeperframe.numerator, timeperframe.denominator, interval);
  299. probe.dwFrameInterval = uvc_try_frame_interval(frame, interval);
  300. /* Probe the device with the new settings. */
  301. ret = uvc_probe_video(stream, &probe);
  302. if (ret < 0)
  303. return ret;
  304. memcpy(&stream->ctrl, &probe, sizeof probe);
  305. /* Return the actual frame period. */
  306. timeperframe.numerator = probe.dwFrameInterval;
  307. timeperframe.denominator = 10000000;
  308. uvc_simplify_fraction(&timeperframe.numerator,
  309. &timeperframe.denominator, 8, 333);
  310. if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  311. parm->parm.capture.timeperframe = timeperframe;
  312. else
  313. parm->parm.output.timeperframe = timeperframe;
  314. return 0;
  315. }
  316. /* ------------------------------------------------------------------------
  317. * Privilege management
  318. */
  319. /*
  320. * Privilege management is the multiple-open implementation basis. The current
  321. * implementation is completely transparent for the end-user and doesn't
  322. * require explicit use of the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls.
  323. * Those ioctls enable finer control on the device (by making possible for a
  324. * user to request exclusive access to a device), but are not mature yet.
  325. * Switching to the V4L2 priority mechanism might be considered in the future
  326. * if this situation changes.
  327. *
  328. * Each open instance of a UVC device can either be in a privileged or
  329. * unprivileged state. Only a single instance can be in a privileged state at
  330. * a given time. Trying to perform an operation that requires privileges will
  331. * automatically acquire the required privileges if possible, or return -EBUSY
  332. * otherwise. Privileges are dismissed when closing the instance or when
  333. * freeing the video buffers using VIDIOC_REQBUFS.
  334. *
  335. * Operations that require privileges are:
  336. *
  337. * - VIDIOC_S_INPUT
  338. * - VIDIOC_S_PARM
  339. * - VIDIOC_S_FMT
  340. * - VIDIOC_REQBUFS
  341. */
  342. static int uvc_acquire_privileges(struct uvc_fh *handle)
  343. {
  344. /* Always succeed if the handle is already privileged. */
  345. if (handle->state == UVC_HANDLE_ACTIVE)
  346. return 0;
  347. /* Check if the device already has a privileged handle. */
  348. if (atomic_inc_return(&handle->stream->active) != 1) {
  349. atomic_dec(&handle->stream->active);
  350. return -EBUSY;
  351. }
  352. handle->state = UVC_HANDLE_ACTIVE;
  353. return 0;
  354. }
  355. static void uvc_dismiss_privileges(struct uvc_fh *handle)
  356. {
  357. if (handle->state == UVC_HANDLE_ACTIVE)
  358. atomic_dec(&handle->stream->active);
  359. handle->state = UVC_HANDLE_PASSIVE;
  360. }
  361. static int uvc_has_privileges(struct uvc_fh *handle)
  362. {
  363. return handle->state == UVC_HANDLE_ACTIVE;
  364. }
  365. /* ------------------------------------------------------------------------
  366. * V4L2 file operations
  367. */
  368. static int uvc_v4l2_open(struct file *file)
  369. {
  370. struct uvc_streaming *stream;
  371. struct uvc_fh *handle;
  372. int ret = 0;
  373. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n");
  374. stream = video_drvdata(file);
  375. if (stream->dev->state & UVC_DEV_DISCONNECTED)
  376. return -ENODEV;
  377. ret = usb_autopm_get_interface(stream->dev->intf);
  378. if (ret < 0)
  379. return ret;
  380. /* Create the device handle. */
  381. handle = kzalloc(sizeof *handle, GFP_KERNEL);
  382. if (handle == NULL) {
  383. usb_autopm_put_interface(stream->dev->intf);
  384. return -ENOMEM;
  385. }
  386. if (atomic_inc_return(&stream->dev->users) == 1) {
  387. ret = uvc_status_start(stream->dev);
  388. if (ret < 0) {
  389. usb_autopm_put_interface(stream->dev->intf);
  390. atomic_dec(&stream->dev->users);
  391. kfree(handle);
  392. return ret;
  393. }
  394. }
  395. handle->chain = stream->chain;
  396. handle->stream = stream;
  397. handle->state = UVC_HANDLE_PASSIVE;
  398. file->private_data = handle;
  399. return 0;
  400. }
  401. static int uvc_v4l2_release(struct file *file)
  402. {
  403. struct uvc_fh *handle = file->private_data;
  404. struct uvc_streaming *stream = handle->stream;
  405. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n");
  406. /* Only free resources if this is a privileged handle. */
  407. if (uvc_has_privileges(handle)) {
  408. uvc_video_enable(stream, 0);
  409. if (uvc_free_buffers(&stream->queue) < 0)
  410. uvc_printk(KERN_ERR, "uvc_v4l2_release: Unable to "
  411. "free buffers.\n");
  412. }
  413. /* Release the file handle. */
  414. uvc_dismiss_privileges(handle);
  415. kfree(handle);
  416. file->private_data = NULL;
  417. if (atomic_dec_return(&stream->dev->users) == 0)
  418. uvc_status_stop(stream->dev);
  419. usb_autopm_put_interface(stream->dev->intf);
  420. return 0;
  421. }
  422. static long uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
  423. {
  424. struct video_device *vdev = video_devdata(file);
  425. struct uvc_fh *handle = file->private_data;
  426. struct uvc_video_chain *chain = handle->chain;
  427. struct uvc_streaming *stream = handle->stream;
  428. long ret = 0;
  429. switch (cmd) {
  430. /* Query capabilities */
  431. case VIDIOC_QUERYCAP:
  432. {
  433. struct v4l2_capability *cap = arg;
  434. memset(cap, 0, sizeof *cap);
  435. strlcpy(cap->driver, "uvcvideo", sizeof cap->driver);
  436. strlcpy(cap->card, vdev->name, sizeof cap->card);
  437. usb_make_path(stream->dev->udev,
  438. cap->bus_info, sizeof(cap->bus_info));
  439. cap->version = DRIVER_VERSION_NUMBER;
  440. if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  441. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
  442. | V4L2_CAP_STREAMING;
  443. else
  444. cap->capabilities = V4L2_CAP_VIDEO_OUTPUT
  445. | V4L2_CAP_STREAMING;
  446. break;
  447. }
  448. /* Get, Set & Query control */
  449. case VIDIOC_QUERYCTRL:
  450. return uvc_query_v4l2_ctrl(chain, arg);
  451. case VIDIOC_G_CTRL:
  452. {
  453. struct v4l2_control *ctrl = arg;
  454. struct v4l2_ext_control xctrl;
  455. memset(&xctrl, 0, sizeof xctrl);
  456. xctrl.id = ctrl->id;
  457. ret = uvc_ctrl_begin(chain);
  458. if (ret < 0)
  459. return ret;
  460. ret = uvc_ctrl_get(chain, &xctrl);
  461. uvc_ctrl_rollback(chain);
  462. if (ret >= 0)
  463. ctrl->value = xctrl.value;
  464. break;
  465. }
  466. case VIDIOC_S_CTRL:
  467. {
  468. struct v4l2_control *ctrl = arg;
  469. struct v4l2_ext_control xctrl;
  470. memset(&xctrl, 0, sizeof xctrl);
  471. xctrl.id = ctrl->id;
  472. xctrl.value = ctrl->value;
  473. ret = uvc_ctrl_begin(chain);
  474. if (ret < 0)
  475. return ret;
  476. ret = uvc_ctrl_set(chain, &xctrl);
  477. if (ret < 0) {
  478. uvc_ctrl_rollback(chain);
  479. return ret;
  480. }
  481. ret = uvc_ctrl_commit(chain);
  482. if (ret == 0)
  483. ctrl->value = xctrl.value;
  484. break;
  485. }
  486. case VIDIOC_QUERYMENU:
  487. return uvc_query_v4l2_menu(chain, arg);
  488. case VIDIOC_G_EXT_CTRLS:
  489. {
  490. struct v4l2_ext_controls *ctrls = arg;
  491. struct v4l2_ext_control *ctrl = ctrls->controls;
  492. unsigned int i;
  493. ret = uvc_ctrl_begin(chain);
  494. if (ret < 0)
  495. return ret;
  496. for (i = 0; i < ctrls->count; ++ctrl, ++i) {
  497. ret = uvc_ctrl_get(chain, ctrl);
  498. if (ret < 0) {
  499. uvc_ctrl_rollback(chain);
  500. ctrls->error_idx = i;
  501. return ret;
  502. }
  503. }
  504. ctrls->error_idx = 0;
  505. ret = uvc_ctrl_rollback(chain);
  506. break;
  507. }
  508. case VIDIOC_S_EXT_CTRLS:
  509. case VIDIOC_TRY_EXT_CTRLS:
  510. {
  511. struct v4l2_ext_controls *ctrls = arg;
  512. struct v4l2_ext_control *ctrl = ctrls->controls;
  513. unsigned int i;
  514. ret = uvc_ctrl_begin(chain);
  515. if (ret < 0)
  516. return ret;
  517. for (i = 0; i < ctrls->count; ++ctrl, ++i) {
  518. ret = uvc_ctrl_set(chain, ctrl);
  519. if (ret < 0) {
  520. uvc_ctrl_rollback(chain);
  521. ctrls->error_idx = i;
  522. return ret;
  523. }
  524. }
  525. ctrls->error_idx = 0;
  526. if (cmd == VIDIOC_S_EXT_CTRLS)
  527. ret = uvc_ctrl_commit(chain);
  528. else
  529. ret = uvc_ctrl_rollback(chain);
  530. break;
  531. }
  532. /* Get, Set & Enum input */
  533. case VIDIOC_ENUMINPUT:
  534. {
  535. const struct uvc_entity *selector = chain->selector;
  536. struct v4l2_input *input = arg;
  537. struct uvc_entity *iterm = NULL;
  538. u32 index = input->index;
  539. int pin = 0;
  540. if (selector == NULL ||
  541. (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
  542. if (index != 0)
  543. return -EINVAL;
  544. list_for_each_entry(iterm, &chain->entities, chain) {
  545. if (UVC_ENTITY_IS_ITERM(iterm))
  546. break;
  547. }
  548. pin = iterm->id;
  549. } else if (pin < selector->bNrInPins) {
  550. pin = selector->baSourceID[index];
  551. list_for_each_entry(iterm, &chain->entities, chain) {
  552. if (!UVC_ENTITY_IS_ITERM(iterm))
  553. continue;
  554. if (iterm->id == pin)
  555. break;
  556. }
  557. }
  558. if (iterm == NULL || iterm->id != pin)
  559. return -EINVAL;
  560. memset(input, 0, sizeof *input);
  561. input->index = index;
  562. strlcpy(input->name, iterm->name, sizeof input->name);
  563. if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA)
  564. input->type = V4L2_INPUT_TYPE_CAMERA;
  565. break;
  566. }
  567. case VIDIOC_G_INPUT:
  568. {
  569. u8 input;
  570. if (chain->selector == NULL ||
  571. (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
  572. *(int *)arg = 0;
  573. break;
  574. }
  575. ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
  576. chain->selector->id, chain->dev->intfnum,
  577. UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
  578. if (ret < 0)
  579. return ret;
  580. *(int *)arg = input - 1;
  581. break;
  582. }
  583. case VIDIOC_S_INPUT:
  584. {
  585. u32 input = *(u32 *)arg + 1;
  586. if ((ret = uvc_acquire_privileges(handle)) < 0)
  587. return ret;
  588. if (chain->selector == NULL ||
  589. (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
  590. if (input != 1)
  591. return -EINVAL;
  592. break;
  593. }
  594. if (input == 0 || input > chain->selector->bNrInPins)
  595. return -EINVAL;
  596. return uvc_query_ctrl(chain->dev, UVC_SET_CUR,
  597. chain->selector->id, chain->dev->intfnum,
  598. UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
  599. }
  600. /* Try, Get, Set & Enum format */
  601. case VIDIOC_ENUM_FMT:
  602. {
  603. struct v4l2_fmtdesc *fmt = arg;
  604. struct uvc_format *format;
  605. enum v4l2_buf_type type = fmt->type;
  606. __u32 index = fmt->index;
  607. if (fmt->type != stream->type ||
  608. fmt->index >= stream->nformats)
  609. return -EINVAL;
  610. memset(fmt, 0, sizeof(*fmt));
  611. fmt->index = index;
  612. fmt->type = type;
  613. format = &stream->format[fmt->index];
  614. fmt->flags = 0;
  615. if (format->flags & UVC_FMT_FLAG_COMPRESSED)
  616. fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
  617. strlcpy(fmt->description, format->name,
  618. sizeof fmt->description);
  619. fmt->description[sizeof fmt->description - 1] = 0;
  620. fmt->pixelformat = format->fcc;
  621. break;
  622. }
  623. case VIDIOC_TRY_FMT:
  624. {
  625. struct uvc_streaming_control probe;
  626. return uvc_v4l2_try_format(stream, arg, &probe, NULL, NULL);
  627. }
  628. case VIDIOC_S_FMT:
  629. if ((ret = uvc_acquire_privileges(handle)) < 0)
  630. return ret;
  631. return uvc_v4l2_set_format(stream, arg);
  632. case VIDIOC_G_FMT:
  633. return uvc_v4l2_get_format(stream, arg);
  634. /* Frame size enumeration */
  635. case VIDIOC_ENUM_FRAMESIZES:
  636. {
  637. struct v4l2_frmsizeenum *fsize = arg;
  638. struct uvc_format *format = NULL;
  639. struct uvc_frame *frame;
  640. int i;
  641. /* Look for the given pixel format */
  642. for (i = 0; i < stream->nformats; i++) {
  643. if (stream->format[i].fcc ==
  644. fsize->pixel_format) {
  645. format = &stream->format[i];
  646. break;
  647. }
  648. }
  649. if (format == NULL)
  650. return -EINVAL;
  651. if (fsize->index >= format->nframes)
  652. return -EINVAL;
  653. frame = &format->frame[fsize->index];
  654. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  655. fsize->discrete.width = frame->wWidth;
  656. fsize->discrete.height = frame->wHeight;
  657. break;
  658. }
  659. /* Frame interval enumeration */
  660. case VIDIOC_ENUM_FRAMEINTERVALS:
  661. {
  662. struct v4l2_frmivalenum *fival = arg;
  663. struct uvc_format *format = NULL;
  664. struct uvc_frame *frame = NULL;
  665. int i;
  666. /* Look for the given pixel format and frame size */
  667. for (i = 0; i < stream->nformats; i++) {
  668. if (stream->format[i].fcc ==
  669. fival->pixel_format) {
  670. format = &stream->format[i];
  671. break;
  672. }
  673. }
  674. if (format == NULL)
  675. return -EINVAL;
  676. for (i = 0; i < format->nframes; i++) {
  677. if (format->frame[i].wWidth == fival->width &&
  678. format->frame[i].wHeight == fival->height) {
  679. frame = &format->frame[i];
  680. break;
  681. }
  682. }
  683. if (frame == NULL)
  684. return -EINVAL;
  685. if (frame->bFrameIntervalType) {
  686. if (fival->index >= frame->bFrameIntervalType)
  687. return -EINVAL;
  688. fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
  689. fival->discrete.numerator =
  690. frame->dwFrameInterval[fival->index];
  691. fival->discrete.denominator = 10000000;
  692. uvc_simplify_fraction(&fival->discrete.numerator,
  693. &fival->discrete.denominator, 8, 333);
  694. } else {
  695. fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
  696. fival->stepwise.min.numerator =
  697. frame->dwFrameInterval[0];
  698. fival->stepwise.min.denominator = 10000000;
  699. fival->stepwise.max.numerator =
  700. frame->dwFrameInterval[1];
  701. fival->stepwise.max.denominator = 10000000;
  702. fival->stepwise.step.numerator =
  703. frame->dwFrameInterval[2];
  704. fival->stepwise.step.denominator = 10000000;
  705. uvc_simplify_fraction(&fival->stepwise.min.numerator,
  706. &fival->stepwise.min.denominator, 8, 333);
  707. uvc_simplify_fraction(&fival->stepwise.max.numerator,
  708. &fival->stepwise.max.denominator, 8, 333);
  709. uvc_simplify_fraction(&fival->stepwise.step.numerator,
  710. &fival->stepwise.step.denominator, 8, 333);
  711. }
  712. break;
  713. }
  714. /* Get & Set streaming parameters */
  715. case VIDIOC_G_PARM:
  716. return uvc_v4l2_get_streamparm(stream, arg);
  717. case VIDIOC_S_PARM:
  718. if ((ret = uvc_acquire_privileges(handle)) < 0)
  719. return ret;
  720. return uvc_v4l2_set_streamparm(stream, arg);
  721. /* Cropping and scaling */
  722. case VIDIOC_CROPCAP:
  723. {
  724. struct v4l2_cropcap *ccap = arg;
  725. struct uvc_frame *frame = stream->cur_frame;
  726. if (ccap->type != stream->type)
  727. return -EINVAL;
  728. ccap->bounds.left = 0;
  729. ccap->bounds.top = 0;
  730. ccap->bounds.width = frame->wWidth;
  731. ccap->bounds.height = frame->wHeight;
  732. ccap->defrect = ccap->bounds;
  733. ccap->pixelaspect.numerator = 1;
  734. ccap->pixelaspect.denominator = 1;
  735. break;
  736. }
  737. case VIDIOC_G_CROP:
  738. case VIDIOC_S_CROP:
  739. return -EINVAL;
  740. /* Buffers & streaming */
  741. case VIDIOC_REQBUFS:
  742. {
  743. struct v4l2_requestbuffers *rb = arg;
  744. unsigned int bufsize =
  745. stream->ctrl.dwMaxVideoFrameSize;
  746. if (rb->type != stream->type ||
  747. rb->memory != V4L2_MEMORY_MMAP)
  748. return -EINVAL;
  749. if ((ret = uvc_acquire_privileges(handle)) < 0)
  750. return ret;
  751. ret = uvc_alloc_buffers(&stream->queue, rb->count, bufsize);
  752. if (ret < 0)
  753. return ret;
  754. if (ret == 0)
  755. uvc_dismiss_privileges(handle);
  756. rb->count = ret;
  757. ret = 0;
  758. break;
  759. }
  760. case VIDIOC_QUERYBUF:
  761. {
  762. struct v4l2_buffer *buf = arg;
  763. if (buf->type != stream->type)
  764. return -EINVAL;
  765. if (!uvc_has_privileges(handle))
  766. return -EBUSY;
  767. return uvc_query_buffer(&stream->queue, buf);
  768. }
  769. case VIDIOC_QBUF:
  770. if (!uvc_has_privileges(handle))
  771. return -EBUSY;
  772. return uvc_queue_buffer(&stream->queue, arg);
  773. case VIDIOC_DQBUF:
  774. if (!uvc_has_privileges(handle))
  775. return -EBUSY;
  776. return uvc_dequeue_buffer(&stream->queue, arg,
  777. file->f_flags & O_NONBLOCK);
  778. case VIDIOC_STREAMON:
  779. {
  780. int *type = arg;
  781. if (*type != stream->type)
  782. return -EINVAL;
  783. if (!uvc_has_privileges(handle))
  784. return -EBUSY;
  785. ret = uvc_video_enable(stream, 1);
  786. if (ret < 0)
  787. return ret;
  788. break;
  789. }
  790. case VIDIOC_STREAMOFF:
  791. {
  792. int *type = arg;
  793. if (*type != stream->type)
  794. return -EINVAL;
  795. if (!uvc_has_privileges(handle))
  796. return -EBUSY;
  797. return uvc_video_enable(stream, 0);
  798. }
  799. /* Analog video standards make no sense for digital cameras. */
  800. case VIDIOC_ENUMSTD:
  801. case VIDIOC_QUERYSTD:
  802. case VIDIOC_G_STD:
  803. case VIDIOC_S_STD:
  804. case VIDIOC_OVERLAY:
  805. case VIDIOC_ENUMAUDIO:
  806. case VIDIOC_ENUMAUDOUT:
  807. case VIDIOC_ENUMOUTPUT:
  808. uvc_trace(UVC_TRACE_IOCTL, "Unsupported ioctl 0x%08x\n", cmd);
  809. return -EINVAL;
  810. /* Dynamic controls. */
  811. case UVCIOC_CTRL_ADD:
  812. /* Legacy ioctl, kept for API compatibility reasons */
  813. return -EEXIST;
  814. case UVCIOC_CTRL_MAP_OLD:
  815. case UVCIOC_CTRL_MAP:
  816. return uvc_ioctl_ctrl_map(chain, arg,
  817. cmd == UVCIOC_CTRL_MAP_OLD);
  818. case UVCIOC_CTRL_GET:
  819. return uvc_xu_ctrl_query(chain, arg, 0);
  820. case UVCIOC_CTRL_SET:
  821. return uvc_xu_ctrl_query(chain, arg, 1);
  822. default:
  823. if ((ret = v4l_compat_translate_ioctl(file, cmd, arg,
  824. uvc_v4l2_do_ioctl)) == -ENOIOCTLCMD)
  825. uvc_trace(UVC_TRACE_IOCTL, "Unknown ioctl 0x%08x\n",
  826. cmd);
  827. return ret;
  828. }
  829. return ret;
  830. }
  831. static long uvc_v4l2_ioctl(struct file *file,
  832. unsigned int cmd, unsigned long arg)
  833. {
  834. if (uvc_trace_param & UVC_TRACE_IOCTL) {
  835. uvc_printk(KERN_DEBUG, "uvc_v4l2_ioctl(");
  836. v4l_printk_ioctl(cmd);
  837. printk(")\n");
  838. }
  839. return video_usercopy(file, cmd, arg, uvc_v4l2_do_ioctl);
  840. }
  841. static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
  842. size_t count, loff_t *ppos)
  843. {
  844. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
  845. return -EINVAL;
  846. }
  847. /*
  848. * VMA operations.
  849. */
  850. static void uvc_vm_open(struct vm_area_struct *vma)
  851. {
  852. struct uvc_buffer *buffer = vma->vm_private_data;
  853. buffer->vma_use_count++;
  854. }
  855. static void uvc_vm_close(struct vm_area_struct *vma)
  856. {
  857. struct uvc_buffer *buffer = vma->vm_private_data;
  858. buffer->vma_use_count--;
  859. }
  860. static const struct vm_operations_struct uvc_vm_ops = {
  861. .open = uvc_vm_open,
  862. .close = uvc_vm_close,
  863. };
  864. static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
  865. {
  866. struct uvc_fh *handle = file->private_data;
  867. struct uvc_streaming *stream = handle->stream;
  868. struct uvc_video_queue *queue = &stream->queue;
  869. struct uvc_buffer *uninitialized_var(buffer);
  870. struct page *page;
  871. unsigned long addr, start, size;
  872. unsigned int i;
  873. int ret = 0;
  874. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
  875. start = vma->vm_start;
  876. size = vma->vm_end - vma->vm_start;
  877. mutex_lock(&queue->mutex);
  878. for (i = 0; i < queue->count; ++i) {
  879. buffer = &queue->buffer[i];
  880. if ((buffer->buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
  881. break;
  882. }
  883. if (i == queue->count || size != queue->buf_size) {
  884. ret = -EINVAL;
  885. goto done;
  886. }
  887. /*
  888. * VM_IO marks the area as being an mmaped region for I/O to a
  889. * device. It also prevents the region from being core dumped.
  890. */
  891. vma->vm_flags |= VM_IO;
  892. addr = (unsigned long)queue->mem + buffer->buf.m.offset;
  893. while (size > 0) {
  894. page = vmalloc_to_page((void *)addr);
  895. if ((ret = vm_insert_page(vma, start, page)) < 0)
  896. goto done;
  897. start += PAGE_SIZE;
  898. addr += PAGE_SIZE;
  899. size -= PAGE_SIZE;
  900. }
  901. vma->vm_ops = &uvc_vm_ops;
  902. vma->vm_private_data = buffer;
  903. uvc_vm_open(vma);
  904. done:
  905. mutex_unlock(&queue->mutex);
  906. return ret;
  907. }
  908. static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait)
  909. {
  910. struct uvc_fh *handle = file->private_data;
  911. struct uvc_streaming *stream = handle->stream;
  912. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
  913. return uvc_queue_poll(&stream->queue, file, wait);
  914. }
  915. const struct v4l2_file_operations uvc_fops = {
  916. .owner = THIS_MODULE,
  917. .open = uvc_v4l2_open,
  918. .release = uvc_v4l2_release,
  919. .ioctl = uvc_v4l2_ioctl,
  920. .read = uvc_v4l2_read,
  921. .mmap = uvc_v4l2_mmap,
  922. .poll = uvc_v4l2_poll,
  923. };