uvc_v4l2.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  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.
  305. *
  306. * Operations that require privileges are:
  307. *
  308. * - VIDIOC_S_INPUT
  309. * - VIDIOC_S_PARM
  310. * - VIDIOC_S_FMT
  311. * - VIDIOC_REQBUFS
  312. */
  313. static int uvc_acquire_privileges(struct uvc_fh *handle)
  314. {
  315. int ret = 0;
  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. mutex_lock(&uvc_driver.open_mutex);
  321. if (atomic_inc_return(&handle->stream->active) != 1) {
  322. atomic_dec(&handle->stream->active);
  323. ret = -EBUSY;
  324. goto done;
  325. }
  326. handle->state = UVC_HANDLE_ACTIVE;
  327. done:
  328. mutex_unlock(&uvc_driver.open_mutex);
  329. return ret;
  330. }
  331. static void uvc_dismiss_privileges(struct uvc_fh *handle)
  332. {
  333. if (handle->state == UVC_HANDLE_ACTIVE)
  334. atomic_dec(&handle->stream->active);
  335. handle->state = UVC_HANDLE_PASSIVE;
  336. }
  337. static int uvc_has_privileges(struct uvc_fh *handle)
  338. {
  339. return handle->state == UVC_HANDLE_ACTIVE;
  340. }
  341. /* ------------------------------------------------------------------------
  342. * V4L2 file operations
  343. */
  344. static int uvc_v4l2_open(struct file *file)
  345. {
  346. struct uvc_streaming *stream;
  347. struct uvc_fh *handle;
  348. int ret = 0;
  349. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n");
  350. mutex_lock(&uvc_driver.open_mutex);
  351. stream = video_drvdata(file);
  352. if (stream->dev->state & UVC_DEV_DISCONNECTED) {
  353. ret = -ENODEV;
  354. goto done;
  355. }
  356. ret = usb_autopm_get_interface(stream->dev->intf);
  357. if (ret < 0)
  358. goto done;
  359. /* Create the device handle. */
  360. handle = kzalloc(sizeof *handle, GFP_KERNEL);
  361. if (handle == NULL) {
  362. usb_autopm_put_interface(stream->dev->intf);
  363. ret = -ENOMEM;
  364. goto done;
  365. }
  366. if (atomic_inc_return(&stream->dev->users) == 1) {
  367. ret = uvc_status_start(stream->dev);
  368. if (ret < 0) {
  369. usb_autopm_put_interface(stream->dev->intf);
  370. atomic_dec(&stream->dev->users);
  371. kfree(handle);
  372. goto done;
  373. }
  374. }
  375. handle->chain = stream->chain;
  376. handle->stream = stream;
  377. handle->state = UVC_HANDLE_PASSIVE;
  378. file->private_data = handle;
  379. kref_get(&stream->dev->kref);
  380. done:
  381. mutex_unlock(&uvc_driver.open_mutex);
  382. return ret;
  383. }
  384. static int uvc_v4l2_release(struct file *file)
  385. {
  386. struct uvc_fh *handle = (struct uvc_fh *)file->private_data;
  387. struct uvc_streaming *stream = handle->stream;
  388. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n");
  389. /* Only free resources if this is a privileged handle. */
  390. if (uvc_has_privileges(handle)) {
  391. uvc_video_enable(stream, 0);
  392. mutex_lock(&stream->queue.mutex);
  393. if (uvc_free_buffers(&stream->queue) < 0)
  394. uvc_printk(KERN_ERR, "uvc_v4l2_release: Unable to "
  395. "free buffers.\n");
  396. mutex_unlock(&stream->queue.mutex);
  397. }
  398. /* Release the file handle. */
  399. uvc_dismiss_privileges(handle);
  400. kfree(handle);
  401. file->private_data = NULL;
  402. if (atomic_dec_return(&stream->dev->users) == 0)
  403. uvc_status_stop(stream->dev);
  404. usb_autopm_put_interface(stream->dev->intf);
  405. kref_put(&stream->dev->kref, uvc_delete);
  406. return 0;
  407. }
  408. static long uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
  409. {
  410. struct video_device *vdev = video_devdata(file);
  411. struct uvc_fh *handle = (struct uvc_fh *)file->private_data;
  412. struct uvc_video_chain *chain = handle->chain;
  413. struct uvc_streaming *stream = handle->stream;
  414. long ret = 0;
  415. switch (cmd) {
  416. /* Query capabilities */
  417. case VIDIOC_QUERYCAP:
  418. {
  419. struct v4l2_capability *cap = arg;
  420. memset(cap, 0, sizeof *cap);
  421. strlcpy(cap->driver, "uvcvideo", sizeof cap->driver);
  422. strlcpy(cap->card, vdev->name, sizeof cap->card);
  423. usb_make_path(stream->dev->udev,
  424. cap->bus_info, sizeof(cap->bus_info));
  425. cap->version = DRIVER_VERSION_NUMBER;
  426. if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  427. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
  428. | V4L2_CAP_STREAMING;
  429. else
  430. cap->capabilities = V4L2_CAP_VIDEO_OUTPUT
  431. | V4L2_CAP_STREAMING;
  432. break;
  433. }
  434. /* Get, Set & Query control */
  435. case VIDIOC_QUERYCTRL:
  436. return uvc_query_v4l2_ctrl(chain, arg);
  437. case VIDIOC_G_CTRL:
  438. {
  439. struct v4l2_control *ctrl = arg;
  440. struct v4l2_ext_control xctrl;
  441. memset(&xctrl, 0, sizeof xctrl);
  442. xctrl.id = ctrl->id;
  443. ret = uvc_ctrl_begin(chain);
  444. if (ret < 0)
  445. return ret;
  446. ret = uvc_ctrl_get(chain, &xctrl);
  447. uvc_ctrl_rollback(chain);
  448. if (ret >= 0)
  449. ctrl->value = xctrl.value;
  450. break;
  451. }
  452. case VIDIOC_S_CTRL:
  453. {
  454. struct v4l2_control *ctrl = arg;
  455. struct v4l2_ext_control xctrl;
  456. memset(&xctrl, 0, sizeof xctrl);
  457. xctrl.id = ctrl->id;
  458. xctrl.value = ctrl->value;
  459. uvc_ctrl_begin(chain);
  460. if (ret < 0)
  461. return ret;
  462. ret = uvc_ctrl_set(chain, &xctrl);
  463. if (ret < 0) {
  464. uvc_ctrl_rollback(chain);
  465. return ret;
  466. }
  467. ret = uvc_ctrl_commit(chain);
  468. break;
  469. }
  470. case VIDIOC_QUERYMENU:
  471. return uvc_v4l2_query_menu(chain, arg);
  472. case VIDIOC_G_EXT_CTRLS:
  473. {
  474. struct v4l2_ext_controls *ctrls = arg;
  475. struct v4l2_ext_control *ctrl = ctrls->controls;
  476. unsigned int i;
  477. ret = uvc_ctrl_begin(chain);
  478. if (ret < 0)
  479. return ret;
  480. for (i = 0; i < ctrls->count; ++ctrl, ++i) {
  481. ret = uvc_ctrl_get(chain, ctrl);
  482. if (ret < 0) {
  483. uvc_ctrl_rollback(chain);
  484. ctrls->error_idx = i;
  485. return ret;
  486. }
  487. }
  488. ctrls->error_idx = 0;
  489. ret = uvc_ctrl_rollback(chain);
  490. break;
  491. }
  492. case VIDIOC_S_EXT_CTRLS:
  493. case VIDIOC_TRY_EXT_CTRLS:
  494. {
  495. struct v4l2_ext_controls *ctrls = arg;
  496. struct v4l2_ext_control *ctrl = ctrls->controls;
  497. unsigned int i;
  498. ret = uvc_ctrl_begin(chain);
  499. if (ret < 0)
  500. return ret;
  501. for (i = 0; i < ctrls->count; ++ctrl, ++i) {
  502. ret = uvc_ctrl_set(chain, ctrl);
  503. if (ret < 0) {
  504. uvc_ctrl_rollback(chain);
  505. ctrls->error_idx = i;
  506. return ret;
  507. }
  508. }
  509. ctrls->error_idx = 0;
  510. if (cmd == VIDIOC_S_EXT_CTRLS)
  511. ret = uvc_ctrl_commit(chain);
  512. else
  513. ret = uvc_ctrl_rollback(chain);
  514. break;
  515. }
  516. /* Get, Set & Enum input */
  517. case VIDIOC_ENUMINPUT:
  518. {
  519. const struct uvc_entity *selector = chain->selector;
  520. struct v4l2_input *input = arg;
  521. struct uvc_entity *iterm = NULL;
  522. u32 index = input->index;
  523. int pin = 0;
  524. if (selector == NULL ||
  525. (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
  526. if (index != 0)
  527. return -EINVAL;
  528. iterm = list_first_entry(&chain->iterms,
  529. struct uvc_entity, chain);
  530. pin = iterm->id;
  531. } else if (pin < selector->selector.bNrInPins) {
  532. pin = selector->selector.baSourceID[index];
  533. list_for_each_entry(iterm, chain->iterms.next, chain) {
  534. if (iterm->id == pin)
  535. break;
  536. }
  537. }
  538. if (iterm == NULL || iterm->id != pin)
  539. return -EINVAL;
  540. memset(input, 0, sizeof *input);
  541. input->index = index;
  542. strlcpy(input->name, iterm->name, sizeof input->name);
  543. if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA)
  544. input->type = V4L2_INPUT_TYPE_CAMERA;
  545. break;
  546. }
  547. case VIDIOC_G_INPUT:
  548. {
  549. u8 input;
  550. if (chain->selector == NULL ||
  551. (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
  552. *(int *)arg = 0;
  553. break;
  554. }
  555. ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
  556. chain->selector->id, chain->dev->intfnum,
  557. UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
  558. if (ret < 0)
  559. return ret;
  560. *(int *)arg = input - 1;
  561. break;
  562. }
  563. case VIDIOC_S_INPUT:
  564. {
  565. u32 input = *(u32 *)arg + 1;
  566. if ((ret = uvc_acquire_privileges(handle)) < 0)
  567. return ret;
  568. if (chain->selector == NULL ||
  569. (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
  570. if (input != 1)
  571. return -EINVAL;
  572. break;
  573. }
  574. if (input == 0 || input > chain->selector->selector.bNrInPins)
  575. return -EINVAL;
  576. return uvc_query_ctrl(chain->dev, UVC_SET_CUR,
  577. chain->selector->id, chain->dev->intfnum,
  578. UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
  579. }
  580. /* Try, Get, Set & Enum format */
  581. case VIDIOC_ENUM_FMT:
  582. {
  583. struct v4l2_fmtdesc *fmt = arg;
  584. struct uvc_format *format;
  585. enum v4l2_buf_type type = fmt->type;
  586. __u32 index = fmt->index;
  587. if (fmt->type != stream->type ||
  588. fmt->index >= stream->nformats)
  589. return -EINVAL;
  590. memset(fmt, 0, sizeof(*fmt));
  591. fmt->index = index;
  592. fmt->type = type;
  593. format = &stream->format[fmt->index];
  594. fmt->flags = 0;
  595. if (format->flags & UVC_FMT_FLAG_COMPRESSED)
  596. fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
  597. strlcpy(fmt->description, format->name,
  598. sizeof fmt->description);
  599. fmt->description[sizeof fmt->description - 1] = 0;
  600. fmt->pixelformat = format->fcc;
  601. break;
  602. }
  603. case VIDIOC_TRY_FMT:
  604. {
  605. struct uvc_streaming_control probe;
  606. return uvc_v4l2_try_format(stream, arg, &probe, NULL, NULL);
  607. }
  608. case VIDIOC_S_FMT:
  609. if ((ret = uvc_acquire_privileges(handle)) < 0)
  610. return ret;
  611. return uvc_v4l2_set_format(stream, arg);
  612. case VIDIOC_G_FMT:
  613. return uvc_v4l2_get_format(stream, arg);
  614. /* Frame size enumeration */
  615. case VIDIOC_ENUM_FRAMESIZES:
  616. {
  617. struct v4l2_frmsizeenum *fsize = arg;
  618. struct uvc_format *format = NULL;
  619. struct uvc_frame *frame;
  620. int i;
  621. /* Look for the given pixel format */
  622. for (i = 0; i < stream->nformats; i++) {
  623. if (stream->format[i].fcc ==
  624. fsize->pixel_format) {
  625. format = &stream->format[i];
  626. break;
  627. }
  628. }
  629. if (format == NULL)
  630. return -EINVAL;
  631. if (fsize->index >= format->nframes)
  632. return -EINVAL;
  633. frame = &format->frame[fsize->index];
  634. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  635. fsize->discrete.width = frame->wWidth;
  636. fsize->discrete.height = frame->wHeight;
  637. break;
  638. }
  639. /* Frame interval enumeration */
  640. case VIDIOC_ENUM_FRAMEINTERVALS:
  641. {
  642. struct v4l2_frmivalenum *fival = arg;
  643. struct uvc_format *format = NULL;
  644. struct uvc_frame *frame = NULL;
  645. int i;
  646. /* Look for the given pixel format and frame size */
  647. for (i = 0; i < stream->nformats; i++) {
  648. if (stream->format[i].fcc ==
  649. fival->pixel_format) {
  650. format = &stream->format[i];
  651. break;
  652. }
  653. }
  654. if (format == NULL)
  655. return -EINVAL;
  656. for (i = 0; i < format->nframes; i++) {
  657. if (format->frame[i].wWidth == fival->width &&
  658. format->frame[i].wHeight == fival->height) {
  659. frame = &format->frame[i];
  660. break;
  661. }
  662. }
  663. if (frame == NULL)
  664. return -EINVAL;
  665. if (frame->bFrameIntervalType) {
  666. if (fival->index >= frame->bFrameIntervalType)
  667. return -EINVAL;
  668. fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
  669. fival->discrete.numerator =
  670. frame->dwFrameInterval[fival->index];
  671. fival->discrete.denominator = 10000000;
  672. uvc_simplify_fraction(&fival->discrete.numerator,
  673. &fival->discrete.denominator, 8, 333);
  674. } else {
  675. fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
  676. fival->stepwise.min.numerator =
  677. frame->dwFrameInterval[0];
  678. fival->stepwise.min.denominator = 10000000;
  679. fival->stepwise.max.numerator =
  680. frame->dwFrameInterval[1];
  681. fival->stepwise.max.denominator = 10000000;
  682. fival->stepwise.step.numerator =
  683. frame->dwFrameInterval[2];
  684. fival->stepwise.step.denominator = 10000000;
  685. uvc_simplify_fraction(&fival->stepwise.min.numerator,
  686. &fival->stepwise.min.denominator, 8, 333);
  687. uvc_simplify_fraction(&fival->stepwise.max.numerator,
  688. &fival->stepwise.max.denominator, 8, 333);
  689. uvc_simplify_fraction(&fival->stepwise.step.numerator,
  690. &fival->stepwise.step.denominator, 8, 333);
  691. }
  692. break;
  693. }
  694. /* Get & Set streaming parameters */
  695. case VIDIOC_G_PARM:
  696. return uvc_v4l2_get_streamparm(stream, arg);
  697. case VIDIOC_S_PARM:
  698. if ((ret = uvc_acquire_privileges(handle)) < 0)
  699. return ret;
  700. return uvc_v4l2_set_streamparm(stream, arg);
  701. /* Cropping and scaling */
  702. case VIDIOC_CROPCAP:
  703. {
  704. struct v4l2_cropcap *ccap = arg;
  705. struct uvc_frame *frame = stream->cur_frame;
  706. if (ccap->type != stream->type)
  707. return -EINVAL;
  708. ccap->bounds.left = 0;
  709. ccap->bounds.top = 0;
  710. ccap->bounds.width = frame->wWidth;
  711. ccap->bounds.height = frame->wHeight;
  712. ccap->defrect = ccap->bounds;
  713. ccap->pixelaspect.numerator = 1;
  714. ccap->pixelaspect.denominator = 1;
  715. break;
  716. }
  717. case VIDIOC_G_CROP:
  718. case VIDIOC_S_CROP:
  719. return -EINVAL;
  720. /* Buffers & streaming */
  721. case VIDIOC_REQBUFS:
  722. {
  723. struct v4l2_requestbuffers *rb = arg;
  724. unsigned int bufsize =
  725. stream->ctrl.dwMaxVideoFrameSize;
  726. if (rb->type != stream->type ||
  727. rb->memory != V4L2_MEMORY_MMAP)
  728. return -EINVAL;
  729. if ((ret = uvc_acquire_privileges(handle)) < 0)
  730. return ret;
  731. ret = uvc_alloc_buffers(&stream->queue, rb->count, bufsize);
  732. if (ret < 0)
  733. return ret;
  734. rb->count = ret;
  735. ret = 0;
  736. break;
  737. }
  738. case VIDIOC_QUERYBUF:
  739. {
  740. struct v4l2_buffer *buf = arg;
  741. if (buf->type != stream->type)
  742. return -EINVAL;
  743. if (!uvc_has_privileges(handle))
  744. return -EBUSY;
  745. return uvc_query_buffer(&stream->queue, buf);
  746. }
  747. case VIDIOC_QBUF:
  748. if (!uvc_has_privileges(handle))
  749. return -EBUSY;
  750. return uvc_queue_buffer(&stream->queue, arg);
  751. case VIDIOC_DQBUF:
  752. if (!uvc_has_privileges(handle))
  753. return -EBUSY;
  754. return uvc_dequeue_buffer(&stream->queue, arg,
  755. file->f_flags & O_NONBLOCK);
  756. case VIDIOC_STREAMON:
  757. {
  758. int *type = arg;
  759. if (*type != stream->type)
  760. return -EINVAL;
  761. if (!uvc_has_privileges(handle))
  762. return -EBUSY;
  763. ret = uvc_video_enable(stream, 1);
  764. if (ret < 0)
  765. return ret;
  766. break;
  767. }
  768. case VIDIOC_STREAMOFF:
  769. {
  770. int *type = arg;
  771. if (*type != stream->type)
  772. return -EINVAL;
  773. if (!uvc_has_privileges(handle))
  774. return -EBUSY;
  775. return uvc_video_enable(stream, 0);
  776. }
  777. /* Analog video standards make no sense for digital cameras. */
  778. case VIDIOC_ENUMSTD:
  779. case VIDIOC_QUERYSTD:
  780. case VIDIOC_G_STD:
  781. case VIDIOC_S_STD:
  782. case VIDIOC_OVERLAY:
  783. case VIDIOC_ENUMAUDIO:
  784. case VIDIOC_ENUMAUDOUT:
  785. case VIDIOC_ENUMOUTPUT:
  786. uvc_trace(UVC_TRACE_IOCTL, "Unsupported ioctl 0x%08x\n", cmd);
  787. return -EINVAL;
  788. /* Dynamic controls. */
  789. case UVCIOC_CTRL_ADD:
  790. {
  791. struct uvc_xu_control_info *xinfo = arg;
  792. struct uvc_control_info *info;
  793. if (!capable(CAP_SYS_ADMIN))
  794. return -EPERM;
  795. info = kzalloc(sizeof *info, GFP_KERNEL);
  796. if (info == NULL)
  797. return -ENOMEM;
  798. memcpy(info->entity, xinfo->entity, sizeof info->entity);
  799. info->index = xinfo->index;
  800. info->selector = xinfo->selector;
  801. info->size = xinfo->size;
  802. info->flags = xinfo->flags;
  803. info->flags |= UVC_CONTROL_GET_MIN | UVC_CONTROL_GET_MAX |
  804. UVC_CONTROL_GET_RES | UVC_CONTROL_GET_DEF;
  805. ret = uvc_ctrl_add_info(info);
  806. if (ret < 0)
  807. kfree(info);
  808. break;
  809. }
  810. case UVCIOC_CTRL_MAP:
  811. {
  812. struct uvc_xu_control_mapping *xmap = arg;
  813. struct uvc_control_mapping *map;
  814. if (!capable(CAP_SYS_ADMIN))
  815. return -EPERM;
  816. map = kzalloc(sizeof *map, GFP_KERNEL);
  817. if (map == NULL)
  818. return -ENOMEM;
  819. map->id = xmap->id;
  820. memcpy(map->name, xmap->name, sizeof map->name);
  821. memcpy(map->entity, xmap->entity, sizeof map->entity);
  822. map->selector = xmap->selector;
  823. map->size = xmap->size;
  824. map->offset = xmap->offset;
  825. map->v4l2_type = xmap->v4l2_type;
  826. map->data_type = xmap->data_type;
  827. ret = uvc_ctrl_add_mapping(map);
  828. if (ret < 0)
  829. kfree(map);
  830. break;
  831. }
  832. case UVCIOC_CTRL_GET:
  833. return uvc_xu_ctrl_query(chain, arg, 0);
  834. case UVCIOC_CTRL_SET:
  835. return uvc_xu_ctrl_query(chain, arg, 1);
  836. default:
  837. if ((ret = v4l_compat_translate_ioctl(file, cmd, arg,
  838. uvc_v4l2_do_ioctl)) == -ENOIOCTLCMD)
  839. uvc_trace(UVC_TRACE_IOCTL, "Unknown ioctl 0x%08x\n",
  840. cmd);
  841. return ret;
  842. }
  843. return ret;
  844. }
  845. static long uvc_v4l2_ioctl(struct file *file,
  846. unsigned int cmd, unsigned long arg)
  847. {
  848. if (uvc_trace_param & UVC_TRACE_IOCTL) {
  849. uvc_printk(KERN_DEBUG, "uvc_v4l2_ioctl(");
  850. v4l_printk_ioctl(cmd);
  851. printk(")\n");
  852. }
  853. return video_usercopy(file, cmd, arg, uvc_v4l2_do_ioctl);
  854. }
  855. static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
  856. size_t count, loff_t *ppos)
  857. {
  858. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
  859. return -ENODEV;
  860. }
  861. /*
  862. * VMA operations.
  863. */
  864. static void uvc_vm_open(struct vm_area_struct *vma)
  865. {
  866. struct uvc_buffer *buffer = vma->vm_private_data;
  867. buffer->vma_use_count++;
  868. }
  869. static void uvc_vm_close(struct vm_area_struct *vma)
  870. {
  871. struct uvc_buffer *buffer = vma->vm_private_data;
  872. buffer->vma_use_count--;
  873. }
  874. static const struct vm_operations_struct uvc_vm_ops = {
  875. .open = uvc_vm_open,
  876. .close = uvc_vm_close,
  877. };
  878. static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
  879. {
  880. struct uvc_fh *handle = (struct uvc_fh *)file->private_data;
  881. struct uvc_streaming *stream = handle->stream;
  882. struct uvc_video_queue *queue = &stream->queue;
  883. struct uvc_buffer *uninitialized_var(buffer);
  884. struct page *page;
  885. unsigned long addr, start, size;
  886. unsigned int i;
  887. int ret = 0;
  888. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
  889. start = vma->vm_start;
  890. size = vma->vm_end - vma->vm_start;
  891. mutex_lock(&queue->mutex);
  892. for (i = 0; i < queue->count; ++i) {
  893. buffer = &queue->buffer[i];
  894. if ((buffer->buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
  895. break;
  896. }
  897. if (i == queue->count || size != queue->buf_size) {
  898. ret = -EINVAL;
  899. goto done;
  900. }
  901. /*
  902. * VM_IO marks the area as being an mmaped region for I/O to a
  903. * device. It also prevents the region from being core dumped.
  904. */
  905. vma->vm_flags |= VM_IO;
  906. addr = (unsigned long)queue->mem + buffer->buf.m.offset;
  907. while (size > 0) {
  908. page = vmalloc_to_page((void *)addr);
  909. if ((ret = vm_insert_page(vma, start, page)) < 0)
  910. goto done;
  911. start += PAGE_SIZE;
  912. addr += PAGE_SIZE;
  913. size -= PAGE_SIZE;
  914. }
  915. vma->vm_ops = &uvc_vm_ops;
  916. vma->vm_private_data = buffer;
  917. uvc_vm_open(vma);
  918. done:
  919. mutex_unlock(&queue->mutex);
  920. return ret;
  921. }
  922. static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait)
  923. {
  924. struct uvc_fh *handle = (struct uvc_fh *)file->private_data;
  925. struct uvc_streaming *stream = handle->stream;
  926. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
  927. return uvc_queue_poll(&stream->queue, file, wait);
  928. }
  929. const struct v4l2_file_operations uvc_fops = {
  930. .owner = THIS_MODULE,
  931. .open = uvc_v4l2_open,
  932. .release = uvc_v4l2_release,
  933. .ioctl = uvc_v4l2_ioctl,
  934. .read = uvc_v4l2_read,
  935. .mmap = uvc_v4l2_mmap,
  936. .poll = uvc_v4l2_poll,
  937. };