uvc_v4l2.c 28 KB

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