uvc_v4l2.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372
  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. done:
  270. mutex_unlock(&stream->mutex);
  271. return ret;
  272. }
  273. static int uvc_v4l2_get_streamparm(struct uvc_streaming *stream,
  274. struct v4l2_streamparm *parm)
  275. {
  276. uint32_t numerator, denominator;
  277. if (parm->type != stream->type)
  278. return -EINVAL;
  279. mutex_lock(&stream->mutex);
  280. numerator = stream->ctrl.dwFrameInterval;
  281. mutex_unlock(&stream->mutex);
  282. denominator = 10000000;
  283. uvc_simplify_fraction(&numerator, &denominator, 8, 333);
  284. memset(parm, 0, sizeof *parm);
  285. parm->type = stream->type;
  286. if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  287. parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
  288. parm->parm.capture.capturemode = 0;
  289. parm->parm.capture.timeperframe.numerator = numerator;
  290. parm->parm.capture.timeperframe.denominator = denominator;
  291. parm->parm.capture.extendedmode = 0;
  292. parm->parm.capture.readbuffers = 0;
  293. } else {
  294. parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
  295. parm->parm.output.outputmode = 0;
  296. parm->parm.output.timeperframe.numerator = numerator;
  297. parm->parm.output.timeperframe.denominator = denominator;
  298. }
  299. return 0;
  300. }
  301. static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream,
  302. struct v4l2_streamparm *parm)
  303. {
  304. struct uvc_streaming_control probe;
  305. struct v4l2_fract timeperframe;
  306. uint32_t interval;
  307. int ret;
  308. if (parm->type != stream->type)
  309. return -EINVAL;
  310. if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  311. timeperframe = parm->parm.capture.timeperframe;
  312. else
  313. timeperframe = parm->parm.output.timeperframe;
  314. interval = uvc_fraction_to_interval(timeperframe.numerator,
  315. timeperframe.denominator);
  316. uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n",
  317. timeperframe.numerator, timeperframe.denominator, interval);
  318. mutex_lock(&stream->mutex);
  319. if (uvc_queue_streaming(&stream->queue)) {
  320. mutex_unlock(&stream->mutex);
  321. return -EBUSY;
  322. }
  323. probe = stream->ctrl;
  324. probe.dwFrameInterval =
  325. uvc_try_frame_interval(stream->cur_frame, interval);
  326. /* Probe the device with the new settings. */
  327. ret = uvc_probe_video(stream, &probe);
  328. if (ret < 0) {
  329. mutex_unlock(&stream->mutex);
  330. return ret;
  331. }
  332. stream->ctrl = probe;
  333. mutex_unlock(&stream->mutex);
  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. mutex_lock(&stream->dev->lock);
  416. if (stream->dev->users == 0) {
  417. ret = uvc_status_start(stream->dev, GFP_KERNEL);
  418. if (ret < 0) {
  419. mutex_unlock(&stream->dev->lock);
  420. usb_autopm_put_interface(stream->dev->intf);
  421. kfree(handle);
  422. return ret;
  423. }
  424. }
  425. stream->dev->users++;
  426. mutex_unlock(&stream->dev->lock);
  427. v4l2_fh_init(&handle->vfh, stream->vdev);
  428. v4l2_fh_add(&handle->vfh);
  429. handle->chain = stream->chain;
  430. handle->stream = stream;
  431. handle->state = UVC_HANDLE_PASSIVE;
  432. file->private_data = handle;
  433. return 0;
  434. }
  435. static int uvc_v4l2_release(struct file *file)
  436. {
  437. struct uvc_fh *handle = file->private_data;
  438. struct uvc_streaming *stream = handle->stream;
  439. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n");
  440. /* Only free resources if this is a privileged handle. */
  441. if (uvc_has_privileges(handle)) {
  442. uvc_video_enable(stream, 0);
  443. uvc_free_buffers(&stream->queue);
  444. }
  445. /* Release the file handle. */
  446. uvc_dismiss_privileges(handle);
  447. v4l2_fh_del(&handle->vfh);
  448. v4l2_fh_exit(&handle->vfh);
  449. kfree(handle);
  450. file->private_data = NULL;
  451. mutex_lock(&stream->dev->lock);
  452. if (--stream->dev->users == 0)
  453. uvc_status_stop(stream->dev);
  454. mutex_unlock(&stream->dev->lock);
  455. usb_autopm_put_interface(stream->dev->intf);
  456. return 0;
  457. }
  458. static long uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
  459. {
  460. struct video_device *vdev = video_devdata(file);
  461. struct uvc_fh *handle = file->private_data;
  462. struct uvc_video_chain *chain = handle->chain;
  463. struct uvc_streaming *stream = handle->stream;
  464. long ret = 0;
  465. switch (cmd) {
  466. /* Query capabilities */
  467. case VIDIOC_QUERYCAP:
  468. {
  469. struct v4l2_capability *cap = arg;
  470. memset(cap, 0, sizeof *cap);
  471. strlcpy(cap->driver, "uvcvideo", sizeof cap->driver);
  472. strlcpy(cap->card, vdev->name, sizeof cap->card);
  473. usb_make_path(stream->dev->udev,
  474. cap->bus_info, sizeof(cap->bus_info));
  475. cap->version = LINUX_VERSION_CODE;
  476. cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
  477. | chain->caps;
  478. if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  479. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE
  480. | V4L2_CAP_STREAMING;
  481. else
  482. cap->device_caps = V4L2_CAP_VIDEO_OUTPUT
  483. | V4L2_CAP_STREAMING;
  484. break;
  485. }
  486. /* Priority */
  487. case VIDIOC_G_PRIORITY:
  488. *(u32 *)arg = v4l2_prio_max(vdev->prio);
  489. break;
  490. case VIDIOC_S_PRIORITY:
  491. ret = v4l2_prio_check(vdev->prio, handle->vfh.prio);
  492. if (ret < 0)
  493. return ret;
  494. return v4l2_prio_change(vdev->prio, &handle->vfh.prio,
  495. *(u32 *)arg);
  496. /* Get, Set & Query control */
  497. case VIDIOC_QUERYCTRL:
  498. return uvc_query_v4l2_ctrl(chain, arg);
  499. case VIDIOC_G_CTRL:
  500. {
  501. struct v4l2_control *ctrl = arg;
  502. struct v4l2_ext_control xctrl;
  503. memset(&xctrl, 0, sizeof xctrl);
  504. xctrl.id = ctrl->id;
  505. ret = uvc_ctrl_begin(chain);
  506. if (ret < 0)
  507. return ret;
  508. ret = uvc_ctrl_get(chain, &xctrl);
  509. uvc_ctrl_rollback(handle);
  510. if (ret >= 0)
  511. ctrl->value = xctrl.value;
  512. break;
  513. }
  514. case VIDIOC_S_CTRL:
  515. {
  516. struct v4l2_control *ctrl = arg;
  517. struct v4l2_ext_control xctrl;
  518. ret = v4l2_prio_check(vdev->prio, handle->vfh.prio);
  519. if (ret < 0)
  520. return ret;
  521. memset(&xctrl, 0, sizeof xctrl);
  522. xctrl.id = ctrl->id;
  523. xctrl.value = ctrl->value;
  524. ret = uvc_ctrl_begin(chain);
  525. if (ret < 0)
  526. return ret;
  527. ret = uvc_ctrl_set(chain, &xctrl);
  528. if (ret < 0) {
  529. uvc_ctrl_rollback(handle);
  530. return ret;
  531. }
  532. ret = uvc_ctrl_commit(handle, &xctrl, 1);
  533. if (ret == 0)
  534. ctrl->value = xctrl.value;
  535. break;
  536. }
  537. case VIDIOC_QUERYMENU:
  538. return uvc_query_v4l2_menu(chain, arg);
  539. case VIDIOC_G_EXT_CTRLS:
  540. {
  541. struct v4l2_ext_controls *ctrls = arg;
  542. struct v4l2_ext_control *ctrl = ctrls->controls;
  543. unsigned int i;
  544. ret = uvc_ctrl_begin(chain);
  545. if (ret < 0)
  546. return ret;
  547. for (i = 0; i < ctrls->count; ++ctrl, ++i) {
  548. ret = uvc_ctrl_get(chain, ctrl);
  549. if (ret < 0) {
  550. uvc_ctrl_rollback(handle);
  551. ctrls->error_idx = i;
  552. return ret;
  553. }
  554. }
  555. ctrls->error_idx = 0;
  556. ret = uvc_ctrl_rollback(handle);
  557. break;
  558. }
  559. case VIDIOC_S_EXT_CTRLS:
  560. ret = v4l2_prio_check(vdev->prio, handle->vfh.prio);
  561. if (ret < 0)
  562. return ret;
  563. /* Fall through */
  564. case VIDIOC_TRY_EXT_CTRLS:
  565. {
  566. struct v4l2_ext_controls *ctrls = arg;
  567. struct v4l2_ext_control *ctrl = ctrls->controls;
  568. unsigned int i;
  569. ret = uvc_ctrl_begin(chain);
  570. if (ret < 0)
  571. return ret;
  572. for (i = 0; i < ctrls->count; ++ctrl, ++i) {
  573. ret = uvc_ctrl_set(chain, ctrl);
  574. if (ret < 0) {
  575. uvc_ctrl_rollback(handle);
  576. ctrls->error_idx = cmd == VIDIOC_S_EXT_CTRLS
  577. ? ctrls->count : i;
  578. return ret;
  579. }
  580. }
  581. ctrls->error_idx = 0;
  582. if (cmd == VIDIOC_S_EXT_CTRLS)
  583. ret = uvc_ctrl_commit(handle,
  584. ctrls->controls, ctrls->count);
  585. else
  586. ret = uvc_ctrl_rollback(handle);
  587. break;
  588. }
  589. /* Get, Set & Enum input */
  590. case VIDIOC_ENUMINPUT:
  591. {
  592. const struct uvc_entity *selector = chain->selector;
  593. struct v4l2_input *input = arg;
  594. struct uvc_entity *iterm = NULL;
  595. u32 index = input->index;
  596. int pin = 0;
  597. if (selector == NULL ||
  598. (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
  599. if (index != 0)
  600. return -EINVAL;
  601. list_for_each_entry(iterm, &chain->entities, chain) {
  602. if (UVC_ENTITY_IS_ITERM(iterm))
  603. break;
  604. }
  605. pin = iterm->id;
  606. } else if (index < selector->bNrInPins) {
  607. pin = selector->baSourceID[index];
  608. list_for_each_entry(iterm, &chain->entities, chain) {
  609. if (!UVC_ENTITY_IS_ITERM(iterm))
  610. continue;
  611. if (iterm->id == pin)
  612. break;
  613. }
  614. }
  615. if (iterm == NULL || iterm->id != pin)
  616. return -EINVAL;
  617. memset(input, 0, sizeof *input);
  618. input->index = index;
  619. strlcpy(input->name, iterm->name, sizeof input->name);
  620. if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA)
  621. input->type = V4L2_INPUT_TYPE_CAMERA;
  622. break;
  623. }
  624. case VIDIOC_G_INPUT:
  625. {
  626. u8 input;
  627. if (chain->selector == NULL ||
  628. (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
  629. *(int *)arg = 0;
  630. break;
  631. }
  632. ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
  633. chain->selector->id, chain->dev->intfnum,
  634. UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
  635. if (ret < 0)
  636. return ret;
  637. *(int *)arg = input - 1;
  638. break;
  639. }
  640. case VIDIOC_S_INPUT:
  641. {
  642. u32 input = *(u32 *)arg + 1;
  643. ret = v4l2_prio_check(vdev->prio, handle->vfh.prio);
  644. if (ret < 0)
  645. return ret;
  646. if ((ret = uvc_acquire_privileges(handle)) < 0)
  647. return ret;
  648. if (chain->selector == NULL ||
  649. (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
  650. if (input != 1)
  651. return -EINVAL;
  652. break;
  653. }
  654. if (input == 0 || input > chain->selector->bNrInPins)
  655. return -EINVAL;
  656. return uvc_query_ctrl(chain->dev, UVC_SET_CUR,
  657. chain->selector->id, chain->dev->intfnum,
  658. UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
  659. }
  660. /* Try, Get, Set & Enum format */
  661. case VIDIOC_ENUM_FMT:
  662. {
  663. struct v4l2_fmtdesc *fmt = arg;
  664. struct uvc_format *format;
  665. enum v4l2_buf_type type = fmt->type;
  666. __u32 index = fmt->index;
  667. if (fmt->type != stream->type ||
  668. fmt->index >= stream->nformats)
  669. return -EINVAL;
  670. memset(fmt, 0, sizeof(*fmt));
  671. fmt->index = index;
  672. fmt->type = type;
  673. format = &stream->format[fmt->index];
  674. fmt->flags = 0;
  675. if (format->flags & UVC_FMT_FLAG_COMPRESSED)
  676. fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
  677. strlcpy(fmt->description, format->name,
  678. sizeof fmt->description);
  679. fmt->description[sizeof fmt->description - 1] = 0;
  680. fmt->pixelformat = format->fcc;
  681. break;
  682. }
  683. case VIDIOC_TRY_FMT:
  684. {
  685. struct uvc_streaming_control probe;
  686. return uvc_v4l2_try_format(stream, arg, &probe, NULL, NULL);
  687. }
  688. case VIDIOC_S_FMT:
  689. ret = v4l2_prio_check(vdev->prio, handle->vfh.prio);
  690. if (ret < 0)
  691. return ret;
  692. if ((ret = uvc_acquire_privileges(handle)) < 0)
  693. return ret;
  694. return uvc_v4l2_set_format(stream, arg);
  695. case VIDIOC_G_FMT:
  696. return uvc_v4l2_get_format(stream, arg);
  697. /* Frame size enumeration */
  698. case VIDIOC_ENUM_FRAMESIZES:
  699. {
  700. struct v4l2_frmsizeenum *fsize = arg;
  701. struct uvc_format *format = NULL;
  702. struct uvc_frame *frame;
  703. int i;
  704. /* Look for the given pixel format */
  705. for (i = 0; i < stream->nformats; i++) {
  706. if (stream->format[i].fcc ==
  707. fsize->pixel_format) {
  708. format = &stream->format[i];
  709. break;
  710. }
  711. }
  712. if (format == NULL)
  713. return -EINVAL;
  714. if (fsize->index >= format->nframes)
  715. return -EINVAL;
  716. frame = &format->frame[fsize->index];
  717. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  718. fsize->discrete.width = frame->wWidth;
  719. fsize->discrete.height = frame->wHeight;
  720. break;
  721. }
  722. /* Frame interval enumeration */
  723. case VIDIOC_ENUM_FRAMEINTERVALS:
  724. {
  725. struct v4l2_frmivalenum *fival = arg;
  726. struct uvc_format *format = NULL;
  727. struct uvc_frame *frame = NULL;
  728. int i;
  729. /* Look for the given pixel format and frame size */
  730. for (i = 0; i < stream->nformats; i++) {
  731. if (stream->format[i].fcc ==
  732. fival->pixel_format) {
  733. format = &stream->format[i];
  734. break;
  735. }
  736. }
  737. if (format == NULL)
  738. return -EINVAL;
  739. for (i = 0; i < format->nframes; i++) {
  740. if (format->frame[i].wWidth == fival->width &&
  741. format->frame[i].wHeight == fival->height) {
  742. frame = &format->frame[i];
  743. break;
  744. }
  745. }
  746. if (frame == NULL)
  747. return -EINVAL;
  748. if (frame->bFrameIntervalType) {
  749. if (fival->index >= frame->bFrameIntervalType)
  750. return -EINVAL;
  751. fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
  752. fival->discrete.numerator =
  753. frame->dwFrameInterval[fival->index];
  754. fival->discrete.denominator = 10000000;
  755. uvc_simplify_fraction(&fival->discrete.numerator,
  756. &fival->discrete.denominator, 8, 333);
  757. } else {
  758. fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
  759. fival->stepwise.min.numerator =
  760. frame->dwFrameInterval[0];
  761. fival->stepwise.min.denominator = 10000000;
  762. fival->stepwise.max.numerator =
  763. frame->dwFrameInterval[1];
  764. fival->stepwise.max.denominator = 10000000;
  765. fival->stepwise.step.numerator =
  766. frame->dwFrameInterval[2];
  767. fival->stepwise.step.denominator = 10000000;
  768. uvc_simplify_fraction(&fival->stepwise.min.numerator,
  769. &fival->stepwise.min.denominator, 8, 333);
  770. uvc_simplify_fraction(&fival->stepwise.max.numerator,
  771. &fival->stepwise.max.denominator, 8, 333);
  772. uvc_simplify_fraction(&fival->stepwise.step.numerator,
  773. &fival->stepwise.step.denominator, 8, 333);
  774. }
  775. break;
  776. }
  777. /* Get & Set streaming parameters */
  778. case VIDIOC_G_PARM:
  779. return uvc_v4l2_get_streamparm(stream, arg);
  780. case VIDIOC_S_PARM:
  781. ret = v4l2_prio_check(vdev->prio, handle->vfh.prio);
  782. if (ret < 0)
  783. return ret;
  784. if ((ret = uvc_acquire_privileges(handle)) < 0)
  785. return ret;
  786. return uvc_v4l2_set_streamparm(stream, arg);
  787. /* Cropping and scaling */
  788. case VIDIOC_CROPCAP:
  789. {
  790. struct v4l2_cropcap *ccap = arg;
  791. if (ccap->type != stream->type)
  792. return -EINVAL;
  793. ccap->bounds.left = 0;
  794. ccap->bounds.top = 0;
  795. mutex_lock(&stream->mutex);
  796. ccap->bounds.width = stream->cur_frame->wWidth;
  797. ccap->bounds.height = stream->cur_frame->wHeight;
  798. mutex_unlock(&stream->mutex);
  799. ccap->defrect = ccap->bounds;
  800. ccap->pixelaspect.numerator = 1;
  801. ccap->pixelaspect.denominator = 1;
  802. break;
  803. }
  804. case VIDIOC_G_CROP:
  805. case VIDIOC_S_CROP:
  806. return -ENOTTY;
  807. /* Buffers & streaming */
  808. case VIDIOC_REQBUFS:
  809. ret = v4l2_prio_check(vdev->prio, handle->vfh.prio);
  810. if (ret < 0)
  811. return ret;
  812. if ((ret = uvc_acquire_privileges(handle)) < 0)
  813. return ret;
  814. mutex_lock(&stream->mutex);
  815. ret = uvc_alloc_buffers(&stream->queue, arg);
  816. mutex_unlock(&stream->mutex);
  817. if (ret < 0)
  818. return ret;
  819. if (ret == 0)
  820. uvc_dismiss_privileges(handle);
  821. ret = 0;
  822. break;
  823. case VIDIOC_QUERYBUF:
  824. {
  825. struct v4l2_buffer *buf = arg;
  826. if (!uvc_has_privileges(handle))
  827. return -EBUSY;
  828. return uvc_query_buffer(&stream->queue, buf);
  829. }
  830. case VIDIOC_QBUF:
  831. if (!uvc_has_privileges(handle))
  832. return -EBUSY;
  833. return uvc_queue_buffer(&stream->queue, arg);
  834. case VIDIOC_DQBUF:
  835. if (!uvc_has_privileges(handle))
  836. return -EBUSY;
  837. return uvc_dequeue_buffer(&stream->queue, arg,
  838. file->f_flags & O_NONBLOCK);
  839. case VIDIOC_STREAMON:
  840. {
  841. int *type = arg;
  842. if (*type != stream->type)
  843. return -EINVAL;
  844. ret = v4l2_prio_check(vdev->prio, handle->vfh.prio);
  845. if (ret < 0)
  846. return ret;
  847. if (!uvc_has_privileges(handle))
  848. return -EBUSY;
  849. mutex_lock(&stream->mutex);
  850. ret = uvc_video_enable(stream, 1);
  851. mutex_unlock(&stream->mutex);
  852. if (ret < 0)
  853. return ret;
  854. break;
  855. }
  856. case VIDIOC_STREAMOFF:
  857. {
  858. int *type = arg;
  859. if (*type != stream->type)
  860. return -EINVAL;
  861. ret = v4l2_prio_check(vdev->prio, handle->vfh.prio);
  862. if (ret < 0)
  863. return ret;
  864. if (!uvc_has_privileges(handle))
  865. return -EBUSY;
  866. return uvc_video_enable(stream, 0);
  867. }
  868. case VIDIOC_SUBSCRIBE_EVENT:
  869. {
  870. struct v4l2_event_subscription *sub = arg;
  871. switch (sub->type) {
  872. case V4L2_EVENT_CTRL:
  873. return v4l2_event_subscribe(&handle->vfh, sub, 0,
  874. &uvc_ctrl_sub_ev_ops);
  875. default:
  876. return -EINVAL;
  877. }
  878. }
  879. case VIDIOC_UNSUBSCRIBE_EVENT:
  880. return v4l2_event_unsubscribe(&handle->vfh, arg);
  881. case VIDIOC_DQEVENT:
  882. return v4l2_event_dequeue(&handle->vfh, arg,
  883. file->f_flags & O_NONBLOCK);
  884. /* Analog video standards make no sense for digital cameras. */
  885. case VIDIOC_ENUMSTD:
  886. case VIDIOC_QUERYSTD:
  887. case VIDIOC_G_STD:
  888. case VIDIOC_S_STD:
  889. case VIDIOC_OVERLAY:
  890. case VIDIOC_ENUMAUDIO:
  891. case VIDIOC_ENUMAUDOUT:
  892. case VIDIOC_ENUMOUTPUT:
  893. uvc_trace(UVC_TRACE_IOCTL, "Unsupported ioctl 0x%08x\n", cmd);
  894. return -ENOTTY;
  895. case UVCIOC_CTRL_MAP:
  896. return uvc_ioctl_ctrl_map(chain, arg);
  897. case UVCIOC_CTRL_QUERY:
  898. return uvc_xu_ctrl_query(chain, arg);
  899. default:
  900. uvc_trace(UVC_TRACE_IOCTL, "Unknown ioctl 0x%08x\n", cmd);
  901. return -ENOTTY;
  902. }
  903. return ret;
  904. }
  905. static long uvc_v4l2_ioctl(struct file *file,
  906. unsigned int cmd, unsigned long arg)
  907. {
  908. if (uvc_trace_param & UVC_TRACE_IOCTL) {
  909. uvc_printk(KERN_DEBUG, "uvc_v4l2_ioctl(");
  910. v4l_printk_ioctl(NULL, cmd);
  911. printk(")\n");
  912. }
  913. return video_usercopy(file, cmd, arg, uvc_v4l2_do_ioctl);
  914. }
  915. #ifdef CONFIG_COMPAT
  916. struct uvc_xu_control_mapping32 {
  917. __u32 id;
  918. __u8 name[32];
  919. __u8 entity[16];
  920. __u8 selector;
  921. __u8 size;
  922. __u8 offset;
  923. __u32 v4l2_type;
  924. __u32 data_type;
  925. compat_caddr_t menu_info;
  926. __u32 menu_count;
  927. __u32 reserved[4];
  928. };
  929. static int uvc_v4l2_get_xu_mapping(struct uvc_xu_control_mapping *kp,
  930. const struct uvc_xu_control_mapping32 __user *up)
  931. {
  932. struct uvc_menu_info __user *umenus;
  933. struct uvc_menu_info __user *kmenus;
  934. compat_caddr_t p;
  935. if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
  936. __copy_from_user(kp, up, offsetof(typeof(*up), menu_info)) ||
  937. __get_user(kp->menu_count, &up->menu_count))
  938. return -EFAULT;
  939. memset(kp->reserved, 0, sizeof(kp->reserved));
  940. if (kp->menu_count == 0) {
  941. kp->menu_info = NULL;
  942. return 0;
  943. }
  944. if (__get_user(p, &up->menu_info))
  945. return -EFAULT;
  946. umenus = compat_ptr(p);
  947. if (!access_ok(VERIFY_READ, umenus, kp->menu_count * sizeof(*umenus)))
  948. return -EFAULT;
  949. kmenus = compat_alloc_user_space(kp->menu_count * sizeof(*kmenus));
  950. if (kmenus == NULL)
  951. return -EFAULT;
  952. kp->menu_info = kmenus;
  953. if (copy_in_user(kmenus, umenus, kp->menu_count * sizeof(*umenus)))
  954. return -EFAULT;
  955. return 0;
  956. }
  957. static int uvc_v4l2_put_xu_mapping(const struct uvc_xu_control_mapping *kp,
  958. struct uvc_xu_control_mapping32 __user *up)
  959. {
  960. struct uvc_menu_info __user *umenus;
  961. struct uvc_menu_info __user *kmenus = kp->menu_info;
  962. compat_caddr_t p;
  963. if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
  964. __copy_to_user(up, kp, offsetof(typeof(*up), menu_info)) ||
  965. __put_user(kp->menu_count, &up->menu_count))
  966. return -EFAULT;
  967. if (__clear_user(up->reserved, sizeof(up->reserved)))
  968. return -EFAULT;
  969. if (kp->menu_count == 0)
  970. return 0;
  971. if (get_user(p, &up->menu_info))
  972. return -EFAULT;
  973. umenus = compat_ptr(p);
  974. if (copy_in_user(umenus, kmenus, kp->menu_count * sizeof(*umenus)))
  975. return -EFAULT;
  976. return 0;
  977. }
  978. struct uvc_xu_control_query32 {
  979. __u8 unit;
  980. __u8 selector;
  981. __u8 query;
  982. __u16 size;
  983. compat_caddr_t data;
  984. };
  985. static int uvc_v4l2_get_xu_query(struct uvc_xu_control_query *kp,
  986. const struct uvc_xu_control_query32 __user *up)
  987. {
  988. u8 __user *udata;
  989. u8 __user *kdata;
  990. compat_caddr_t p;
  991. if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
  992. __copy_from_user(kp, up, offsetof(typeof(*up), data)))
  993. return -EFAULT;
  994. if (kp->size == 0) {
  995. kp->data = NULL;
  996. return 0;
  997. }
  998. if (__get_user(p, &up->data))
  999. return -EFAULT;
  1000. udata = compat_ptr(p);
  1001. if (!access_ok(VERIFY_READ, udata, kp->size))
  1002. return -EFAULT;
  1003. kdata = compat_alloc_user_space(kp->size);
  1004. if (kdata == NULL)
  1005. return -EFAULT;
  1006. kp->data = kdata;
  1007. if (copy_in_user(kdata, udata, kp->size))
  1008. return -EFAULT;
  1009. return 0;
  1010. }
  1011. static int uvc_v4l2_put_xu_query(const struct uvc_xu_control_query *kp,
  1012. struct uvc_xu_control_query32 __user *up)
  1013. {
  1014. u8 __user *udata;
  1015. u8 __user *kdata = kp->data;
  1016. compat_caddr_t p;
  1017. if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
  1018. __copy_to_user(up, kp, offsetof(typeof(*up), data)))
  1019. return -EFAULT;
  1020. if (kp->size == 0)
  1021. return 0;
  1022. if (get_user(p, &up->data))
  1023. return -EFAULT;
  1024. udata = compat_ptr(p);
  1025. if (!access_ok(VERIFY_READ, udata, kp->size))
  1026. return -EFAULT;
  1027. if (copy_in_user(udata, kdata, kp->size))
  1028. return -EFAULT;
  1029. return 0;
  1030. }
  1031. #define UVCIOC_CTRL_MAP32 _IOWR('u', 0x20, struct uvc_xu_control_mapping32)
  1032. #define UVCIOC_CTRL_QUERY32 _IOWR('u', 0x21, struct uvc_xu_control_query32)
  1033. static long uvc_v4l2_compat_ioctl32(struct file *file,
  1034. unsigned int cmd, unsigned long arg)
  1035. {
  1036. union {
  1037. struct uvc_xu_control_mapping xmap;
  1038. struct uvc_xu_control_query xqry;
  1039. } karg;
  1040. void __user *up = compat_ptr(arg);
  1041. mm_segment_t old_fs;
  1042. long ret;
  1043. switch (cmd) {
  1044. case UVCIOC_CTRL_MAP32:
  1045. cmd = UVCIOC_CTRL_MAP;
  1046. ret = uvc_v4l2_get_xu_mapping(&karg.xmap, up);
  1047. break;
  1048. case UVCIOC_CTRL_QUERY32:
  1049. cmd = UVCIOC_CTRL_QUERY;
  1050. ret = uvc_v4l2_get_xu_query(&karg.xqry, up);
  1051. break;
  1052. default:
  1053. return -ENOIOCTLCMD;
  1054. }
  1055. old_fs = get_fs();
  1056. set_fs(KERNEL_DS);
  1057. ret = uvc_v4l2_ioctl(file, cmd, (unsigned long)&karg);
  1058. set_fs(old_fs);
  1059. if (ret < 0)
  1060. return ret;
  1061. switch (cmd) {
  1062. case UVCIOC_CTRL_MAP:
  1063. ret = uvc_v4l2_put_xu_mapping(&karg.xmap, up);
  1064. break;
  1065. case UVCIOC_CTRL_QUERY:
  1066. ret = uvc_v4l2_put_xu_query(&karg.xqry, up);
  1067. break;
  1068. }
  1069. return ret;
  1070. }
  1071. #endif
  1072. static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
  1073. size_t count, loff_t *ppos)
  1074. {
  1075. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
  1076. return -EINVAL;
  1077. }
  1078. static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
  1079. {
  1080. struct uvc_fh *handle = file->private_data;
  1081. struct uvc_streaming *stream = handle->stream;
  1082. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
  1083. return uvc_queue_mmap(&stream->queue, vma);
  1084. }
  1085. static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait)
  1086. {
  1087. struct uvc_fh *handle = file->private_data;
  1088. struct uvc_streaming *stream = handle->stream;
  1089. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
  1090. return uvc_queue_poll(&stream->queue, file, wait);
  1091. }
  1092. #ifndef CONFIG_MMU
  1093. static unsigned long uvc_v4l2_get_unmapped_area(struct file *file,
  1094. unsigned long addr, unsigned long len, unsigned long pgoff,
  1095. unsigned long flags)
  1096. {
  1097. struct uvc_fh *handle = file->private_data;
  1098. struct uvc_streaming *stream = handle->stream;
  1099. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_get_unmapped_area\n");
  1100. return uvc_queue_get_unmapped_area(&stream->queue, pgoff);
  1101. }
  1102. #endif
  1103. const struct v4l2_file_operations uvc_fops = {
  1104. .owner = THIS_MODULE,
  1105. .open = uvc_v4l2_open,
  1106. .release = uvc_v4l2_release,
  1107. .unlocked_ioctl = uvc_v4l2_ioctl,
  1108. #ifdef CONFIG_COMPAT
  1109. .compat_ioctl32 = uvc_v4l2_compat_ioctl32,
  1110. #endif
  1111. .read = uvc_v4l2_read,
  1112. .mmap = uvc_v4l2_mmap,
  1113. .poll = uvc_v4l2_poll,
  1114. #ifndef CONFIG_MMU
  1115. .get_unmapped_area = uvc_v4l2_get_unmapped_area,
  1116. #endif
  1117. };