uvc_v4l2.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384
  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/compat.h>
  14. #include <linux/kernel.h>
  15. #include <linux/version.h>
  16. #include <linux/list.h>
  17. #include <linux/module.h>
  18. #include <linux/slab.h>
  19. #include <linux/usb.h>
  20. #include <linux/videodev2.h>
  21. #include <linux/vmalloc.h>
  22. #include <linux/mm.h>
  23. #include <linux/wait.h>
  24. #include <linux/atomic.h>
  25. #include <media/v4l2-common.h>
  26. #include <media/v4l2-ctrls.h>
  27. #include <media/v4l2-event.h>
  28. #include <media/v4l2-ioctl.h>
  29. #include "uvcvideo.h"
  30. /* ------------------------------------------------------------------------
  31. * UVC ioctls
  32. */
  33. static int uvc_ioctl_ctrl_map(struct uvc_video_chain *chain,
  34. struct uvc_xu_control_mapping *xmap)
  35. {
  36. struct uvc_control_mapping *map;
  37. unsigned int size;
  38. int ret;
  39. map = kzalloc(sizeof *map, GFP_KERNEL);
  40. if (map == NULL)
  41. return -ENOMEM;
  42. map->id = xmap->id;
  43. memcpy(map->name, xmap->name, sizeof map->name);
  44. memcpy(map->entity, xmap->entity, sizeof map->entity);
  45. map->selector = xmap->selector;
  46. map->size = xmap->size;
  47. map->offset = xmap->offset;
  48. map->v4l2_type = xmap->v4l2_type;
  49. map->data_type = xmap->data_type;
  50. switch (xmap->v4l2_type) {
  51. case V4L2_CTRL_TYPE_INTEGER:
  52. case V4L2_CTRL_TYPE_BOOLEAN:
  53. case V4L2_CTRL_TYPE_BUTTON:
  54. break;
  55. case V4L2_CTRL_TYPE_MENU:
  56. /* Prevent excessive memory consumption, as well as integer
  57. * overflows.
  58. */
  59. if (xmap->menu_count == 0 ||
  60. xmap->menu_count > UVC_MAX_CONTROL_MENU_ENTRIES) {
  61. ret = -EINVAL;
  62. goto done;
  63. }
  64. size = xmap->menu_count * sizeof(*map->menu_info);
  65. map->menu_info = kmalloc(size, GFP_KERNEL);
  66. if (map->menu_info == NULL) {
  67. ret = -ENOMEM;
  68. goto done;
  69. }
  70. if (copy_from_user(map->menu_info, xmap->menu_info, size)) {
  71. ret = -EFAULT;
  72. goto done;
  73. }
  74. map->menu_count = xmap->menu_count;
  75. break;
  76. default:
  77. uvc_trace(UVC_TRACE_CONTROL, "Unsupported V4L2 control type "
  78. "%u.\n", xmap->v4l2_type);
  79. ret = -ENOTTY;
  80. goto done;
  81. }
  82. ret = uvc_ctrl_add_mapping(chain, map);
  83. done:
  84. kfree(map->menu_info);
  85. kfree(map);
  86. return ret;
  87. }
  88. /* ------------------------------------------------------------------------
  89. * V4L2 interface
  90. */
  91. /*
  92. * Find the frame interval closest to the requested frame interval for the
  93. * given frame format and size. This should be done by the device as part of
  94. * the Video Probe and Commit negotiation, but some hardware don't implement
  95. * that feature.
  96. */
  97. static __u32 uvc_try_frame_interval(struct uvc_frame *frame, __u32 interval)
  98. {
  99. unsigned int i;
  100. if (frame->bFrameIntervalType) {
  101. __u32 best = -1, dist;
  102. for (i = 0; i < frame->bFrameIntervalType; ++i) {
  103. dist = interval > frame->dwFrameInterval[i]
  104. ? interval - frame->dwFrameInterval[i]
  105. : frame->dwFrameInterval[i] - interval;
  106. if (dist > best)
  107. break;
  108. best = dist;
  109. }
  110. interval = frame->dwFrameInterval[i-1];
  111. } else {
  112. const __u32 min = frame->dwFrameInterval[0];
  113. const __u32 max = frame->dwFrameInterval[1];
  114. const __u32 step = frame->dwFrameInterval[2];
  115. interval = min + (interval - min + step/2) / step * step;
  116. if (interval > max)
  117. interval = max;
  118. }
  119. return interval;
  120. }
  121. static int uvc_v4l2_try_format(struct uvc_streaming *stream,
  122. struct v4l2_format *fmt, struct uvc_streaming_control *probe,
  123. struct uvc_format **uvc_format, struct uvc_frame **uvc_frame)
  124. {
  125. struct uvc_format *format = NULL;
  126. struct uvc_frame *frame = NULL;
  127. __u16 rw, rh;
  128. unsigned int d, maxd;
  129. unsigned int i;
  130. __u32 interval;
  131. int ret = 0;
  132. __u8 *fcc;
  133. if (fmt->type != stream->type)
  134. return -EINVAL;
  135. fcc = (__u8 *)&fmt->fmt.pix.pixelformat;
  136. uvc_trace(UVC_TRACE_FORMAT, "Trying format 0x%08x (%c%c%c%c): %ux%u.\n",
  137. fmt->fmt.pix.pixelformat,
  138. fcc[0], fcc[1], fcc[2], fcc[3],
  139. fmt->fmt.pix.width, fmt->fmt.pix.height);
  140. /* Check if the hardware supports the requested format, use the default
  141. * format otherwise.
  142. */
  143. for (i = 0; i < stream->nformats; ++i) {
  144. format = &stream->format[i];
  145. if (format->fcc == fmt->fmt.pix.pixelformat)
  146. break;
  147. }
  148. if (i == stream->nformats) {
  149. format = stream->def_format;
  150. fmt->fmt.pix.pixelformat = format->fcc;
  151. }
  152. /* Find the closest image size. The distance between image sizes is
  153. * the size in pixels of the non-overlapping regions between the
  154. * requested size and the frame-specified size.
  155. */
  156. rw = fmt->fmt.pix.width;
  157. rh = fmt->fmt.pix.height;
  158. maxd = (unsigned int)-1;
  159. for (i = 0; i < format->nframes; ++i) {
  160. __u16 w = format->frame[i].wWidth;
  161. __u16 h = format->frame[i].wHeight;
  162. d = min(w, rw) * min(h, rh);
  163. d = w*h + rw*rh - 2*d;
  164. if (d < maxd) {
  165. maxd = d;
  166. frame = &format->frame[i];
  167. }
  168. if (maxd == 0)
  169. break;
  170. }
  171. if (frame == NULL) {
  172. uvc_trace(UVC_TRACE_FORMAT, "Unsupported size %ux%u.\n",
  173. fmt->fmt.pix.width, fmt->fmt.pix.height);
  174. return -EINVAL;
  175. }
  176. /* Use the default frame interval. */
  177. interval = frame->dwDefaultFrameInterval;
  178. uvc_trace(UVC_TRACE_FORMAT, "Using default frame interval %u.%u us "
  179. "(%u.%u fps).\n", interval/10, interval%10, 10000000/interval,
  180. (100000000/interval)%10);
  181. /* Set the format index, frame index and frame interval. */
  182. memset(probe, 0, sizeof *probe);
  183. probe->bmHint = 1; /* dwFrameInterval */
  184. probe->bFormatIndex = format->index;
  185. probe->bFrameIndex = frame->bFrameIndex;
  186. probe->dwFrameInterval = uvc_try_frame_interval(frame, interval);
  187. /* Some webcams stall the probe control set request when the
  188. * dwMaxVideoFrameSize field is set to zero. The UVC specification
  189. * clearly states that the field is read-only from the host, so this
  190. * is a webcam bug. Set dwMaxVideoFrameSize to the value reported by
  191. * the webcam to work around the problem.
  192. *
  193. * The workaround could probably be enabled for all webcams, so the
  194. * quirk can be removed if needed. It's currently useful to detect
  195. * webcam bugs and fix them before they hit the market (providing
  196. * developers test their webcams with the Linux driver as well as with
  197. * the Windows driver).
  198. */
  199. mutex_lock(&stream->mutex);
  200. if (stream->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS)
  201. probe->dwMaxVideoFrameSize =
  202. stream->ctrl.dwMaxVideoFrameSize;
  203. /* Probe the device. */
  204. ret = uvc_probe_video(stream, probe);
  205. mutex_unlock(&stream->mutex);
  206. if (ret < 0)
  207. goto done;
  208. fmt->fmt.pix.width = frame->wWidth;
  209. fmt->fmt.pix.height = frame->wHeight;
  210. fmt->fmt.pix.field = V4L2_FIELD_NONE;
  211. fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
  212. fmt->fmt.pix.sizeimage = probe->dwMaxVideoFrameSize;
  213. fmt->fmt.pix.colorspace = format->colorspace;
  214. fmt->fmt.pix.priv = 0;
  215. if (uvc_format != NULL)
  216. *uvc_format = format;
  217. if (uvc_frame != NULL)
  218. *uvc_frame = frame;
  219. done:
  220. return ret;
  221. }
  222. static int uvc_v4l2_get_format(struct uvc_streaming *stream,
  223. struct v4l2_format *fmt)
  224. {
  225. struct uvc_format *format;
  226. struct uvc_frame *frame;
  227. int ret = 0;
  228. if (fmt->type != stream->type)
  229. return -EINVAL;
  230. mutex_lock(&stream->mutex);
  231. format = stream->cur_format;
  232. frame = stream->cur_frame;
  233. if (format == NULL || frame == NULL) {
  234. ret = -EINVAL;
  235. goto done;
  236. }
  237. fmt->fmt.pix.pixelformat = format->fcc;
  238. fmt->fmt.pix.width = frame->wWidth;
  239. fmt->fmt.pix.height = frame->wHeight;
  240. fmt->fmt.pix.field = V4L2_FIELD_NONE;
  241. fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
  242. fmt->fmt.pix.sizeimage = stream->ctrl.dwMaxVideoFrameSize;
  243. fmt->fmt.pix.colorspace = format->colorspace;
  244. fmt->fmt.pix.priv = 0;
  245. done:
  246. mutex_unlock(&stream->mutex);
  247. return ret;
  248. }
  249. static int uvc_v4l2_set_format(struct uvc_streaming *stream,
  250. struct v4l2_format *fmt)
  251. {
  252. struct uvc_streaming_control probe;
  253. struct uvc_format *format;
  254. struct uvc_frame *frame;
  255. int ret;
  256. if (fmt->type != stream->type)
  257. return -EINVAL;
  258. ret = uvc_v4l2_try_format(stream, fmt, &probe, &format, &frame);
  259. if (ret < 0)
  260. return ret;
  261. mutex_lock(&stream->mutex);
  262. if (uvc_queue_allocated(&stream->queue)) {
  263. ret = -EBUSY;
  264. goto done;
  265. }
  266. stream->ctrl = probe;
  267. stream->cur_format = format;
  268. stream->cur_frame = frame;
  269. stream->frame_size = fmt->fmt.pix.sizeimage;
  270. done:
  271. mutex_unlock(&stream->mutex);
  272. return ret;
  273. }
  274. static int uvc_v4l2_get_streamparm(struct uvc_streaming *stream,
  275. struct v4l2_streamparm *parm)
  276. {
  277. uint32_t numerator, denominator;
  278. if (parm->type != stream->type)
  279. return -EINVAL;
  280. mutex_lock(&stream->mutex);
  281. numerator = stream->ctrl.dwFrameInterval;
  282. mutex_unlock(&stream->mutex);
  283. denominator = 10000000;
  284. uvc_simplify_fraction(&numerator, &denominator, 8, 333);
  285. memset(parm, 0, sizeof *parm);
  286. parm->type = stream->type;
  287. if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  288. parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
  289. parm->parm.capture.capturemode = 0;
  290. parm->parm.capture.timeperframe.numerator = numerator;
  291. parm->parm.capture.timeperframe.denominator = denominator;
  292. parm->parm.capture.extendedmode = 0;
  293. parm->parm.capture.readbuffers = 0;
  294. } else {
  295. parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
  296. parm->parm.output.outputmode = 0;
  297. parm->parm.output.timeperframe.numerator = numerator;
  298. parm->parm.output.timeperframe.denominator = denominator;
  299. }
  300. return 0;
  301. }
  302. static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream,
  303. struct v4l2_streamparm *parm)
  304. {
  305. struct uvc_streaming_control probe;
  306. struct v4l2_fract timeperframe;
  307. uint32_t interval;
  308. int ret;
  309. if (parm->type != stream->type)
  310. return -EINVAL;
  311. if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  312. timeperframe = parm->parm.capture.timeperframe;
  313. else
  314. timeperframe = parm->parm.output.timeperframe;
  315. interval = uvc_fraction_to_interval(timeperframe.numerator,
  316. timeperframe.denominator);
  317. uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n",
  318. timeperframe.numerator, timeperframe.denominator, interval);
  319. mutex_lock(&stream->mutex);
  320. if (uvc_queue_streaming(&stream->queue)) {
  321. mutex_unlock(&stream->mutex);
  322. return -EBUSY;
  323. }
  324. probe = stream->ctrl;
  325. probe.dwFrameInterval =
  326. uvc_try_frame_interval(stream->cur_frame, interval);
  327. /* Probe the device with the new settings. */
  328. ret = uvc_probe_video(stream, &probe);
  329. if (ret < 0) {
  330. mutex_unlock(&stream->mutex);
  331. return ret;
  332. }
  333. stream->ctrl = probe;
  334. mutex_unlock(&stream->mutex);
  335. /* Return the actual frame period. */
  336. timeperframe.numerator = probe.dwFrameInterval;
  337. timeperframe.denominator = 10000000;
  338. uvc_simplify_fraction(&timeperframe.numerator,
  339. &timeperframe.denominator, 8, 333);
  340. if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  341. parm->parm.capture.timeperframe = timeperframe;
  342. else
  343. parm->parm.output.timeperframe = timeperframe;
  344. return 0;
  345. }
  346. /* ------------------------------------------------------------------------
  347. * Privilege management
  348. */
  349. /*
  350. * Privilege management is the multiple-open implementation basis. The current
  351. * implementation is completely transparent for the end-user and doesn't
  352. * require explicit use of the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls.
  353. * Those ioctls enable finer control on the device (by making possible for a
  354. * user to request exclusive access to a device), but are not mature yet.
  355. * Switching to the V4L2 priority mechanism might be considered in the future
  356. * if this situation changes.
  357. *
  358. * Each open instance of a UVC device can either be in a privileged or
  359. * unprivileged state. Only a single instance can be in a privileged state at
  360. * a given time. Trying to perform an operation that requires privileges will
  361. * automatically acquire the required privileges if possible, or return -EBUSY
  362. * otherwise. Privileges are dismissed when closing the instance or when
  363. * freeing the video buffers using VIDIOC_REQBUFS.
  364. *
  365. * Operations that require privileges are:
  366. *
  367. * - VIDIOC_S_INPUT
  368. * - VIDIOC_S_PARM
  369. * - VIDIOC_S_FMT
  370. * - VIDIOC_REQBUFS
  371. */
  372. static int uvc_acquire_privileges(struct uvc_fh *handle)
  373. {
  374. /* Always succeed if the handle is already privileged. */
  375. if (handle->state == UVC_HANDLE_ACTIVE)
  376. return 0;
  377. /* Check if the device already has a privileged handle. */
  378. if (atomic_inc_return(&handle->stream->active) != 1) {
  379. atomic_dec(&handle->stream->active);
  380. return -EBUSY;
  381. }
  382. handle->state = UVC_HANDLE_ACTIVE;
  383. return 0;
  384. }
  385. static void uvc_dismiss_privileges(struct uvc_fh *handle)
  386. {
  387. if (handle->state == UVC_HANDLE_ACTIVE)
  388. atomic_dec(&handle->stream->active);
  389. handle->state = UVC_HANDLE_PASSIVE;
  390. }
  391. static int uvc_has_privileges(struct uvc_fh *handle)
  392. {
  393. return handle->state == UVC_HANDLE_ACTIVE;
  394. }
  395. /* ------------------------------------------------------------------------
  396. * V4L2 file operations
  397. */
  398. static int uvc_v4l2_open(struct file *file)
  399. {
  400. struct uvc_streaming *stream;
  401. struct uvc_fh *handle;
  402. int ret = 0;
  403. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n");
  404. stream = video_drvdata(file);
  405. if (stream->dev->state & UVC_DEV_DISCONNECTED)
  406. return -ENODEV;
  407. ret = usb_autopm_get_interface(stream->dev->intf);
  408. if (ret < 0)
  409. return ret;
  410. /* Create the device handle. */
  411. handle = kzalloc(sizeof *handle, GFP_KERNEL);
  412. if (handle == NULL) {
  413. usb_autopm_put_interface(stream->dev->intf);
  414. return -ENOMEM;
  415. }
  416. mutex_lock(&stream->dev->lock);
  417. if (stream->dev->users == 0) {
  418. ret = uvc_status_start(stream->dev, GFP_KERNEL);
  419. if (ret < 0) {
  420. mutex_unlock(&stream->dev->lock);
  421. usb_autopm_put_interface(stream->dev->intf);
  422. kfree(handle);
  423. return ret;
  424. }
  425. }
  426. stream->dev->users++;
  427. mutex_unlock(&stream->dev->lock);
  428. v4l2_fh_init(&handle->vfh, stream->vdev);
  429. v4l2_fh_add(&handle->vfh);
  430. handle->chain = stream->chain;
  431. handle->stream = stream;
  432. handle->state = UVC_HANDLE_PASSIVE;
  433. file->private_data = handle;
  434. return 0;
  435. }
  436. static int uvc_v4l2_release(struct file *file)
  437. {
  438. struct uvc_fh *handle = file->private_data;
  439. struct uvc_streaming *stream = handle->stream;
  440. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n");
  441. /* Only free resources if this is a privileged handle. */
  442. if (uvc_has_privileges(handle)) {
  443. uvc_video_enable(stream, 0);
  444. uvc_free_buffers(&stream->queue);
  445. }
  446. /* Release the file handle. */
  447. uvc_dismiss_privileges(handle);
  448. v4l2_fh_del(&handle->vfh);
  449. v4l2_fh_exit(&handle->vfh);
  450. kfree(handle);
  451. file->private_data = NULL;
  452. mutex_lock(&stream->dev->lock);
  453. if (--stream->dev->users == 0)
  454. uvc_status_stop(stream->dev);
  455. mutex_unlock(&stream->dev->lock);
  456. usb_autopm_put_interface(stream->dev->intf);
  457. return 0;
  458. }
  459. static long uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
  460. {
  461. struct video_device *vdev = video_devdata(file);
  462. struct uvc_fh *handle = file->private_data;
  463. struct uvc_video_chain *chain = handle->chain;
  464. struct uvc_streaming *stream = handle->stream;
  465. long ret = 0;
  466. switch (cmd) {
  467. /* Query capabilities */
  468. case VIDIOC_QUERYCAP:
  469. {
  470. struct v4l2_capability *cap = arg;
  471. memset(cap, 0, sizeof *cap);
  472. strlcpy(cap->driver, "uvcvideo", sizeof cap->driver);
  473. strlcpy(cap->card, vdev->name, sizeof cap->card);
  474. usb_make_path(stream->dev->udev,
  475. cap->bus_info, sizeof(cap->bus_info));
  476. cap->version = LINUX_VERSION_CODE;
  477. cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
  478. | chain->caps;
  479. if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  480. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE
  481. | V4L2_CAP_STREAMING;
  482. else
  483. cap->device_caps = V4L2_CAP_VIDEO_OUTPUT
  484. | V4L2_CAP_STREAMING;
  485. break;
  486. }
  487. /* Priority */
  488. case VIDIOC_G_PRIORITY:
  489. *(u32 *)arg = v4l2_prio_max(vdev->prio);
  490. break;
  491. case VIDIOC_S_PRIORITY:
  492. ret = v4l2_prio_check(vdev->prio, handle->vfh.prio);
  493. if (ret < 0)
  494. return ret;
  495. return v4l2_prio_change(vdev->prio, &handle->vfh.prio,
  496. *(u32 *)arg);
  497. /* Get, Set & Query control */
  498. case VIDIOC_QUERYCTRL:
  499. return uvc_query_v4l2_ctrl(chain, arg);
  500. case VIDIOC_G_CTRL:
  501. {
  502. struct v4l2_control *ctrl = arg;
  503. struct v4l2_ext_control xctrl;
  504. memset(&xctrl, 0, sizeof xctrl);
  505. xctrl.id = ctrl->id;
  506. ret = uvc_ctrl_begin(chain);
  507. if (ret < 0)
  508. return ret;
  509. ret = uvc_ctrl_get(chain, &xctrl);
  510. uvc_ctrl_rollback(handle);
  511. if (ret >= 0)
  512. ctrl->value = xctrl.value;
  513. break;
  514. }
  515. case VIDIOC_S_CTRL:
  516. {
  517. struct v4l2_control *ctrl = arg;
  518. struct v4l2_ext_control xctrl;
  519. ret = v4l2_prio_check(vdev->prio, handle->vfh.prio);
  520. if (ret < 0)
  521. return ret;
  522. memset(&xctrl, 0, sizeof xctrl);
  523. xctrl.id = ctrl->id;
  524. xctrl.value = ctrl->value;
  525. ret = uvc_ctrl_begin(chain);
  526. if (ret < 0)
  527. return ret;
  528. ret = uvc_ctrl_set(chain, &xctrl);
  529. if (ret < 0) {
  530. uvc_ctrl_rollback(handle);
  531. return ret;
  532. }
  533. ret = uvc_ctrl_commit(handle, &xctrl, 1);
  534. if (ret == 0)
  535. ctrl->value = xctrl.value;
  536. break;
  537. }
  538. case VIDIOC_QUERYMENU:
  539. return uvc_query_v4l2_menu(chain, arg);
  540. case VIDIOC_G_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_get(chain, ctrl);
  550. if (ret < 0) {
  551. uvc_ctrl_rollback(handle);
  552. ctrls->error_idx = i;
  553. return ret;
  554. }
  555. }
  556. ctrls->error_idx = 0;
  557. ret = uvc_ctrl_rollback(handle);
  558. break;
  559. }
  560. case VIDIOC_S_EXT_CTRLS:
  561. ret = v4l2_prio_check(vdev->prio, handle->vfh.prio);
  562. if (ret < 0)
  563. return ret;
  564. /* Fall through */
  565. case VIDIOC_TRY_EXT_CTRLS:
  566. {
  567. struct v4l2_ext_controls *ctrls = arg;
  568. struct v4l2_ext_control *ctrl = ctrls->controls;
  569. unsigned int i;
  570. ret = uvc_ctrl_begin(chain);
  571. if (ret < 0)
  572. return ret;
  573. for (i = 0; i < ctrls->count; ++ctrl, ++i) {
  574. ret = uvc_ctrl_set(chain, ctrl);
  575. if (ret < 0) {
  576. uvc_ctrl_rollback(handle);
  577. ctrls->error_idx = cmd == VIDIOC_S_EXT_CTRLS
  578. ? ctrls->count : i;
  579. return ret;
  580. }
  581. }
  582. ctrls->error_idx = 0;
  583. if (cmd == VIDIOC_S_EXT_CTRLS)
  584. ret = uvc_ctrl_commit(handle,
  585. ctrls->controls, ctrls->count);
  586. else
  587. ret = uvc_ctrl_rollback(handle);
  588. break;
  589. }
  590. /* Get, Set & Enum input */
  591. case VIDIOC_ENUMINPUT:
  592. {
  593. const struct uvc_entity *selector = chain->selector;
  594. struct v4l2_input *input = arg;
  595. struct uvc_entity *iterm = NULL;
  596. u32 index = input->index;
  597. int pin = 0;
  598. if (selector == NULL ||
  599. (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
  600. if (index != 0)
  601. return -EINVAL;
  602. list_for_each_entry(iterm, &chain->entities, chain) {
  603. if (UVC_ENTITY_IS_ITERM(iterm))
  604. break;
  605. }
  606. pin = iterm->id;
  607. } else if (index < selector->bNrInPins) {
  608. pin = selector->baSourceID[index];
  609. list_for_each_entry(iterm, &chain->entities, chain) {
  610. if (!UVC_ENTITY_IS_ITERM(iterm))
  611. continue;
  612. if (iterm->id == pin)
  613. break;
  614. }
  615. }
  616. if (iterm == NULL || iterm->id != pin)
  617. return -EINVAL;
  618. memset(input, 0, sizeof *input);
  619. input->index = index;
  620. strlcpy(input->name, iterm->name, sizeof input->name);
  621. if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA)
  622. input->type = V4L2_INPUT_TYPE_CAMERA;
  623. break;
  624. }
  625. case VIDIOC_G_INPUT:
  626. {
  627. u8 input;
  628. if (chain->selector == NULL ||
  629. (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
  630. *(int *)arg = 0;
  631. break;
  632. }
  633. ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
  634. chain->selector->id, chain->dev->intfnum,
  635. UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
  636. if (ret < 0)
  637. return ret;
  638. *(int *)arg = input - 1;
  639. break;
  640. }
  641. case VIDIOC_S_INPUT:
  642. {
  643. u32 input = *(u32 *)arg + 1;
  644. ret = v4l2_prio_check(vdev->prio, handle->vfh.prio);
  645. if (ret < 0)
  646. return ret;
  647. if ((ret = uvc_acquire_privileges(handle)) < 0)
  648. return ret;
  649. if (chain->selector == NULL ||
  650. (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
  651. if (input != 1)
  652. return -EINVAL;
  653. break;
  654. }
  655. if (input == 0 || input > chain->selector->bNrInPins)
  656. return -EINVAL;
  657. return uvc_query_ctrl(chain->dev, UVC_SET_CUR,
  658. chain->selector->id, chain->dev->intfnum,
  659. UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
  660. }
  661. /* Try, Get, Set & Enum format */
  662. case VIDIOC_ENUM_FMT:
  663. {
  664. struct v4l2_fmtdesc *fmt = arg;
  665. struct uvc_format *format;
  666. enum v4l2_buf_type type = fmt->type;
  667. __u32 index = fmt->index;
  668. if (fmt->type != stream->type ||
  669. fmt->index >= stream->nformats)
  670. return -EINVAL;
  671. memset(fmt, 0, sizeof(*fmt));
  672. fmt->index = index;
  673. fmt->type = type;
  674. format = &stream->format[fmt->index];
  675. fmt->flags = 0;
  676. if (format->flags & UVC_FMT_FLAG_COMPRESSED)
  677. fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
  678. strlcpy(fmt->description, format->name,
  679. sizeof fmt->description);
  680. fmt->description[sizeof fmt->description - 1] = 0;
  681. fmt->pixelformat = format->fcc;
  682. break;
  683. }
  684. case VIDIOC_TRY_FMT:
  685. {
  686. struct uvc_streaming_control probe;
  687. return uvc_v4l2_try_format(stream, arg, &probe, NULL, NULL);
  688. }
  689. case VIDIOC_S_FMT:
  690. ret = v4l2_prio_check(vdev->prio, handle->vfh.prio);
  691. if (ret < 0)
  692. return ret;
  693. if ((ret = uvc_acquire_privileges(handle)) < 0)
  694. return ret;
  695. return uvc_v4l2_set_format(stream, arg);
  696. case VIDIOC_G_FMT:
  697. return uvc_v4l2_get_format(stream, arg);
  698. /* Frame size enumeration */
  699. case VIDIOC_ENUM_FRAMESIZES:
  700. {
  701. struct v4l2_frmsizeenum *fsize = arg;
  702. struct uvc_format *format = NULL;
  703. struct uvc_frame *frame;
  704. int i;
  705. /* Look for the given pixel format */
  706. for (i = 0; i < stream->nformats; i++) {
  707. if (stream->format[i].fcc ==
  708. fsize->pixel_format) {
  709. format = &stream->format[i];
  710. break;
  711. }
  712. }
  713. if (format == NULL)
  714. return -EINVAL;
  715. if (fsize->index >= format->nframes)
  716. return -EINVAL;
  717. frame = &format->frame[fsize->index];
  718. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  719. fsize->discrete.width = frame->wWidth;
  720. fsize->discrete.height = frame->wHeight;
  721. break;
  722. }
  723. /* Frame interval enumeration */
  724. case VIDIOC_ENUM_FRAMEINTERVALS:
  725. {
  726. struct v4l2_frmivalenum *fival = arg;
  727. struct uvc_format *format = NULL;
  728. struct uvc_frame *frame = NULL;
  729. int i;
  730. /* Look for the given pixel format and frame size */
  731. for (i = 0; i < stream->nformats; i++) {
  732. if (stream->format[i].fcc ==
  733. fival->pixel_format) {
  734. format = &stream->format[i];
  735. break;
  736. }
  737. }
  738. if (format == NULL)
  739. return -EINVAL;
  740. for (i = 0; i < format->nframes; i++) {
  741. if (format->frame[i].wWidth == fival->width &&
  742. format->frame[i].wHeight == fival->height) {
  743. frame = &format->frame[i];
  744. break;
  745. }
  746. }
  747. if (frame == NULL)
  748. return -EINVAL;
  749. if (frame->bFrameIntervalType) {
  750. if (fival->index >= frame->bFrameIntervalType)
  751. return -EINVAL;
  752. fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
  753. fival->discrete.numerator =
  754. frame->dwFrameInterval[fival->index];
  755. fival->discrete.denominator = 10000000;
  756. uvc_simplify_fraction(&fival->discrete.numerator,
  757. &fival->discrete.denominator, 8, 333);
  758. } else {
  759. fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
  760. fival->stepwise.min.numerator =
  761. frame->dwFrameInterval[0];
  762. fival->stepwise.min.denominator = 10000000;
  763. fival->stepwise.max.numerator =
  764. frame->dwFrameInterval[1];
  765. fival->stepwise.max.denominator = 10000000;
  766. fival->stepwise.step.numerator =
  767. frame->dwFrameInterval[2];
  768. fival->stepwise.step.denominator = 10000000;
  769. uvc_simplify_fraction(&fival->stepwise.min.numerator,
  770. &fival->stepwise.min.denominator, 8, 333);
  771. uvc_simplify_fraction(&fival->stepwise.max.numerator,
  772. &fival->stepwise.max.denominator, 8, 333);
  773. uvc_simplify_fraction(&fival->stepwise.step.numerator,
  774. &fival->stepwise.step.denominator, 8, 333);
  775. }
  776. break;
  777. }
  778. /* Get & Set streaming parameters */
  779. case VIDIOC_G_PARM:
  780. return uvc_v4l2_get_streamparm(stream, arg);
  781. case VIDIOC_S_PARM:
  782. ret = v4l2_prio_check(vdev->prio, handle->vfh.prio);
  783. if (ret < 0)
  784. return ret;
  785. if ((ret = uvc_acquire_privileges(handle)) < 0)
  786. return ret;
  787. return uvc_v4l2_set_streamparm(stream, arg);
  788. /* Cropping and scaling */
  789. case VIDIOC_CROPCAP:
  790. {
  791. struct v4l2_cropcap *ccap = arg;
  792. if (ccap->type != stream->type)
  793. return -EINVAL;
  794. ccap->bounds.left = 0;
  795. ccap->bounds.top = 0;
  796. mutex_lock(&stream->mutex);
  797. ccap->bounds.width = stream->cur_frame->wWidth;
  798. ccap->bounds.height = stream->cur_frame->wHeight;
  799. mutex_unlock(&stream->mutex);
  800. ccap->defrect = ccap->bounds;
  801. ccap->pixelaspect.numerator = 1;
  802. ccap->pixelaspect.denominator = 1;
  803. break;
  804. }
  805. case VIDIOC_G_CROP:
  806. case VIDIOC_S_CROP:
  807. return -ENOTTY;
  808. /* Buffers & streaming */
  809. case VIDIOC_REQBUFS:
  810. ret = v4l2_prio_check(vdev->prio, handle->vfh.prio);
  811. if (ret < 0)
  812. return ret;
  813. if ((ret = uvc_acquire_privileges(handle)) < 0)
  814. return ret;
  815. mutex_lock(&stream->mutex);
  816. ret = uvc_alloc_buffers(&stream->queue, arg);
  817. mutex_unlock(&stream->mutex);
  818. if (ret < 0)
  819. return ret;
  820. if (ret == 0)
  821. uvc_dismiss_privileges(handle);
  822. ret = 0;
  823. break;
  824. case VIDIOC_QUERYBUF:
  825. {
  826. struct v4l2_buffer *buf = arg;
  827. if (!uvc_has_privileges(handle))
  828. return -EBUSY;
  829. return uvc_query_buffer(&stream->queue, buf);
  830. }
  831. case VIDIOC_CREATE_BUFS:
  832. {
  833. struct v4l2_create_buffers *cb = arg;
  834. ret = uvc_acquire_privileges(handle);
  835. if (ret < 0)
  836. return ret;
  837. return uvc_create_buffers(&stream->queue, cb);
  838. }
  839. case VIDIOC_QBUF:
  840. if (!uvc_has_privileges(handle))
  841. return -EBUSY;
  842. return uvc_queue_buffer(&stream->queue, arg);
  843. case VIDIOC_DQBUF:
  844. if (!uvc_has_privileges(handle))
  845. return -EBUSY;
  846. return uvc_dequeue_buffer(&stream->queue, arg,
  847. file->f_flags & O_NONBLOCK);
  848. case VIDIOC_STREAMON:
  849. {
  850. int *type = arg;
  851. if (*type != stream->type)
  852. return -EINVAL;
  853. ret = v4l2_prio_check(vdev->prio, handle->vfh.prio);
  854. if (ret < 0)
  855. return ret;
  856. if (!uvc_has_privileges(handle))
  857. return -EBUSY;
  858. mutex_lock(&stream->mutex);
  859. ret = uvc_video_enable(stream, 1);
  860. mutex_unlock(&stream->mutex);
  861. if (ret < 0)
  862. return ret;
  863. break;
  864. }
  865. case VIDIOC_STREAMOFF:
  866. {
  867. int *type = arg;
  868. if (*type != stream->type)
  869. return -EINVAL;
  870. ret = v4l2_prio_check(vdev->prio, handle->vfh.prio);
  871. if (ret < 0)
  872. return ret;
  873. if (!uvc_has_privileges(handle))
  874. return -EBUSY;
  875. return uvc_video_enable(stream, 0);
  876. }
  877. case VIDIOC_SUBSCRIBE_EVENT:
  878. {
  879. struct v4l2_event_subscription *sub = arg;
  880. switch (sub->type) {
  881. case V4L2_EVENT_CTRL:
  882. return v4l2_event_subscribe(&handle->vfh, sub, 0,
  883. &uvc_ctrl_sub_ev_ops);
  884. default:
  885. return -EINVAL;
  886. }
  887. }
  888. case VIDIOC_UNSUBSCRIBE_EVENT:
  889. return v4l2_event_unsubscribe(&handle->vfh, arg);
  890. case VIDIOC_DQEVENT:
  891. return v4l2_event_dequeue(&handle->vfh, arg,
  892. file->f_flags & O_NONBLOCK);
  893. /* Analog video standards make no sense for digital cameras. */
  894. case VIDIOC_ENUMSTD:
  895. case VIDIOC_QUERYSTD:
  896. case VIDIOC_G_STD:
  897. case VIDIOC_S_STD:
  898. case VIDIOC_OVERLAY:
  899. case VIDIOC_ENUMAUDIO:
  900. case VIDIOC_ENUMAUDOUT:
  901. case VIDIOC_ENUMOUTPUT:
  902. uvc_trace(UVC_TRACE_IOCTL, "Unsupported ioctl 0x%08x\n", cmd);
  903. return -ENOTTY;
  904. case UVCIOC_CTRL_MAP:
  905. return uvc_ioctl_ctrl_map(chain, arg);
  906. case UVCIOC_CTRL_QUERY:
  907. return uvc_xu_ctrl_query(chain, arg);
  908. default:
  909. uvc_trace(UVC_TRACE_IOCTL, "Unknown ioctl 0x%08x\n", cmd);
  910. return -ENOTTY;
  911. }
  912. return ret;
  913. }
  914. static long uvc_v4l2_ioctl(struct file *file,
  915. unsigned int cmd, unsigned long arg)
  916. {
  917. if (uvc_trace_param & UVC_TRACE_IOCTL) {
  918. uvc_printk(KERN_DEBUG, "uvc_v4l2_ioctl(");
  919. v4l_printk_ioctl(NULL, cmd);
  920. printk(")\n");
  921. }
  922. return video_usercopy(file, cmd, arg, uvc_v4l2_do_ioctl);
  923. }
  924. #ifdef CONFIG_COMPAT
  925. struct uvc_xu_control_mapping32 {
  926. __u32 id;
  927. __u8 name[32];
  928. __u8 entity[16];
  929. __u8 selector;
  930. __u8 size;
  931. __u8 offset;
  932. __u32 v4l2_type;
  933. __u32 data_type;
  934. compat_caddr_t menu_info;
  935. __u32 menu_count;
  936. __u32 reserved[4];
  937. };
  938. static int uvc_v4l2_get_xu_mapping(struct uvc_xu_control_mapping *kp,
  939. const struct uvc_xu_control_mapping32 __user *up)
  940. {
  941. struct uvc_menu_info __user *umenus;
  942. struct uvc_menu_info __user *kmenus;
  943. compat_caddr_t p;
  944. if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
  945. __copy_from_user(kp, up, offsetof(typeof(*up), menu_info)) ||
  946. __get_user(kp->menu_count, &up->menu_count))
  947. return -EFAULT;
  948. memset(kp->reserved, 0, sizeof(kp->reserved));
  949. if (kp->menu_count == 0) {
  950. kp->menu_info = NULL;
  951. return 0;
  952. }
  953. if (__get_user(p, &up->menu_info))
  954. return -EFAULT;
  955. umenus = compat_ptr(p);
  956. if (!access_ok(VERIFY_READ, umenus, kp->menu_count * sizeof(*umenus)))
  957. return -EFAULT;
  958. kmenus = compat_alloc_user_space(kp->menu_count * sizeof(*kmenus));
  959. if (kmenus == NULL)
  960. return -EFAULT;
  961. kp->menu_info = kmenus;
  962. if (copy_in_user(kmenus, umenus, kp->menu_count * sizeof(*umenus)))
  963. return -EFAULT;
  964. return 0;
  965. }
  966. static int uvc_v4l2_put_xu_mapping(const struct uvc_xu_control_mapping *kp,
  967. struct uvc_xu_control_mapping32 __user *up)
  968. {
  969. struct uvc_menu_info __user *umenus;
  970. struct uvc_menu_info __user *kmenus = kp->menu_info;
  971. compat_caddr_t p;
  972. if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
  973. __copy_to_user(up, kp, offsetof(typeof(*up), menu_info)) ||
  974. __put_user(kp->menu_count, &up->menu_count))
  975. return -EFAULT;
  976. if (__clear_user(up->reserved, sizeof(up->reserved)))
  977. return -EFAULT;
  978. if (kp->menu_count == 0)
  979. return 0;
  980. if (get_user(p, &up->menu_info))
  981. return -EFAULT;
  982. umenus = compat_ptr(p);
  983. if (copy_in_user(umenus, kmenus, kp->menu_count * sizeof(*umenus)))
  984. return -EFAULT;
  985. return 0;
  986. }
  987. struct uvc_xu_control_query32 {
  988. __u8 unit;
  989. __u8 selector;
  990. __u8 query;
  991. __u16 size;
  992. compat_caddr_t data;
  993. };
  994. static int uvc_v4l2_get_xu_query(struct uvc_xu_control_query *kp,
  995. const struct uvc_xu_control_query32 __user *up)
  996. {
  997. u8 __user *udata;
  998. u8 __user *kdata;
  999. compat_caddr_t p;
  1000. if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
  1001. __copy_from_user(kp, up, offsetof(typeof(*up), data)))
  1002. return -EFAULT;
  1003. if (kp->size == 0) {
  1004. kp->data = NULL;
  1005. return 0;
  1006. }
  1007. if (__get_user(p, &up->data))
  1008. return -EFAULT;
  1009. udata = compat_ptr(p);
  1010. if (!access_ok(VERIFY_READ, udata, kp->size))
  1011. return -EFAULT;
  1012. kdata = compat_alloc_user_space(kp->size);
  1013. if (kdata == NULL)
  1014. return -EFAULT;
  1015. kp->data = kdata;
  1016. if (copy_in_user(kdata, udata, kp->size))
  1017. return -EFAULT;
  1018. return 0;
  1019. }
  1020. static int uvc_v4l2_put_xu_query(const struct uvc_xu_control_query *kp,
  1021. struct uvc_xu_control_query32 __user *up)
  1022. {
  1023. u8 __user *udata;
  1024. u8 __user *kdata = kp->data;
  1025. compat_caddr_t p;
  1026. if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
  1027. __copy_to_user(up, kp, offsetof(typeof(*up), data)))
  1028. return -EFAULT;
  1029. if (kp->size == 0)
  1030. return 0;
  1031. if (get_user(p, &up->data))
  1032. return -EFAULT;
  1033. udata = compat_ptr(p);
  1034. if (!access_ok(VERIFY_READ, udata, kp->size))
  1035. return -EFAULT;
  1036. if (copy_in_user(udata, kdata, kp->size))
  1037. return -EFAULT;
  1038. return 0;
  1039. }
  1040. #define UVCIOC_CTRL_MAP32 _IOWR('u', 0x20, struct uvc_xu_control_mapping32)
  1041. #define UVCIOC_CTRL_QUERY32 _IOWR('u', 0x21, struct uvc_xu_control_query32)
  1042. static long uvc_v4l2_compat_ioctl32(struct file *file,
  1043. unsigned int cmd, unsigned long arg)
  1044. {
  1045. union {
  1046. struct uvc_xu_control_mapping xmap;
  1047. struct uvc_xu_control_query xqry;
  1048. } karg;
  1049. void __user *up = compat_ptr(arg);
  1050. mm_segment_t old_fs;
  1051. long ret;
  1052. switch (cmd) {
  1053. case UVCIOC_CTRL_MAP32:
  1054. cmd = UVCIOC_CTRL_MAP;
  1055. ret = uvc_v4l2_get_xu_mapping(&karg.xmap, up);
  1056. break;
  1057. case UVCIOC_CTRL_QUERY32:
  1058. cmd = UVCIOC_CTRL_QUERY;
  1059. ret = uvc_v4l2_get_xu_query(&karg.xqry, up);
  1060. break;
  1061. default:
  1062. return -ENOIOCTLCMD;
  1063. }
  1064. old_fs = get_fs();
  1065. set_fs(KERNEL_DS);
  1066. ret = uvc_v4l2_ioctl(file, cmd, (unsigned long)&karg);
  1067. set_fs(old_fs);
  1068. if (ret < 0)
  1069. return ret;
  1070. switch (cmd) {
  1071. case UVCIOC_CTRL_MAP:
  1072. ret = uvc_v4l2_put_xu_mapping(&karg.xmap, up);
  1073. break;
  1074. case UVCIOC_CTRL_QUERY:
  1075. ret = uvc_v4l2_put_xu_query(&karg.xqry, up);
  1076. break;
  1077. }
  1078. return ret;
  1079. }
  1080. #endif
  1081. static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
  1082. size_t count, loff_t *ppos)
  1083. {
  1084. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
  1085. return -EINVAL;
  1086. }
  1087. static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
  1088. {
  1089. struct uvc_fh *handle = file->private_data;
  1090. struct uvc_streaming *stream = handle->stream;
  1091. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
  1092. return uvc_queue_mmap(&stream->queue, vma);
  1093. }
  1094. static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait)
  1095. {
  1096. struct uvc_fh *handle = file->private_data;
  1097. struct uvc_streaming *stream = handle->stream;
  1098. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
  1099. return uvc_queue_poll(&stream->queue, file, wait);
  1100. }
  1101. #ifndef CONFIG_MMU
  1102. static unsigned long uvc_v4l2_get_unmapped_area(struct file *file,
  1103. unsigned long addr, unsigned long len, unsigned long pgoff,
  1104. unsigned long flags)
  1105. {
  1106. struct uvc_fh *handle = file->private_data;
  1107. struct uvc_streaming *stream = handle->stream;
  1108. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_get_unmapped_area\n");
  1109. return uvc_queue_get_unmapped_area(&stream->queue, pgoff);
  1110. }
  1111. #endif
  1112. const struct v4l2_file_operations uvc_fops = {
  1113. .owner = THIS_MODULE,
  1114. .open = uvc_v4l2_open,
  1115. .release = uvc_v4l2_release,
  1116. .unlocked_ioctl = uvc_v4l2_ioctl,
  1117. #ifdef CONFIG_COMPAT
  1118. .compat_ioctl32 = uvc_v4l2_compat_ioctl32,
  1119. #endif
  1120. .read = uvc_v4l2_read,
  1121. .mmap = uvc_v4l2_mmap,
  1122. .poll = uvc_v4l2_poll,
  1123. #ifndef CONFIG_MMU
  1124. .get_unmapped_area = uvc_v4l2_get_unmapped_area,
  1125. #endif
  1126. };