uvc_v4l2.c 28 KB

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