uvc_v4l2.c 27 KB

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