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