uvc_v4l2.c 28 KB

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