uvc_v4l2.c 28 KB

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