uvc_v4l2.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383
  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_CREATE_BUFS:
  831. {
  832. struct v4l2_create_buffers *cb = arg;
  833. ret = uvc_acquire_privileges(handle);
  834. if (ret < 0)
  835. return ret;
  836. return uvc_create_buffers(&stream->queue, cb);
  837. }
  838. case VIDIOC_QBUF:
  839. if (!uvc_has_privileges(handle))
  840. return -EBUSY;
  841. return uvc_queue_buffer(&stream->queue, arg);
  842. case VIDIOC_DQBUF:
  843. if (!uvc_has_privileges(handle))
  844. return -EBUSY;
  845. return uvc_dequeue_buffer(&stream->queue, arg,
  846. file->f_flags & O_NONBLOCK);
  847. case VIDIOC_STREAMON:
  848. {
  849. int *type = arg;
  850. if (*type != stream->type)
  851. return -EINVAL;
  852. ret = v4l2_prio_check(vdev->prio, handle->vfh.prio);
  853. if (ret < 0)
  854. return ret;
  855. if (!uvc_has_privileges(handle))
  856. return -EBUSY;
  857. mutex_lock(&stream->mutex);
  858. ret = uvc_video_enable(stream, 1);
  859. mutex_unlock(&stream->mutex);
  860. if (ret < 0)
  861. return ret;
  862. break;
  863. }
  864. case VIDIOC_STREAMOFF:
  865. {
  866. int *type = arg;
  867. if (*type != stream->type)
  868. return -EINVAL;
  869. ret = v4l2_prio_check(vdev->prio, handle->vfh.prio);
  870. if (ret < 0)
  871. return ret;
  872. if (!uvc_has_privileges(handle))
  873. return -EBUSY;
  874. return uvc_video_enable(stream, 0);
  875. }
  876. case VIDIOC_SUBSCRIBE_EVENT:
  877. {
  878. struct v4l2_event_subscription *sub = arg;
  879. switch (sub->type) {
  880. case V4L2_EVENT_CTRL:
  881. return v4l2_event_subscribe(&handle->vfh, sub, 0,
  882. &uvc_ctrl_sub_ev_ops);
  883. default:
  884. return -EINVAL;
  885. }
  886. }
  887. case VIDIOC_UNSUBSCRIBE_EVENT:
  888. return v4l2_event_unsubscribe(&handle->vfh, arg);
  889. case VIDIOC_DQEVENT:
  890. return v4l2_event_dequeue(&handle->vfh, arg,
  891. file->f_flags & O_NONBLOCK);
  892. /* Analog video standards make no sense for digital cameras. */
  893. case VIDIOC_ENUMSTD:
  894. case VIDIOC_QUERYSTD:
  895. case VIDIOC_G_STD:
  896. case VIDIOC_S_STD:
  897. case VIDIOC_OVERLAY:
  898. case VIDIOC_ENUMAUDIO:
  899. case VIDIOC_ENUMAUDOUT:
  900. case VIDIOC_ENUMOUTPUT:
  901. uvc_trace(UVC_TRACE_IOCTL, "Unsupported ioctl 0x%08x\n", cmd);
  902. return -ENOTTY;
  903. case UVCIOC_CTRL_MAP:
  904. return uvc_ioctl_ctrl_map(chain, arg);
  905. case UVCIOC_CTRL_QUERY:
  906. return uvc_xu_ctrl_query(chain, arg);
  907. default:
  908. uvc_trace(UVC_TRACE_IOCTL, "Unknown ioctl 0x%08x\n", cmd);
  909. return -ENOTTY;
  910. }
  911. return ret;
  912. }
  913. static long uvc_v4l2_ioctl(struct file *file,
  914. unsigned int cmd, unsigned long arg)
  915. {
  916. if (uvc_trace_param & UVC_TRACE_IOCTL) {
  917. uvc_printk(KERN_DEBUG, "uvc_v4l2_ioctl(");
  918. v4l_printk_ioctl(NULL, cmd);
  919. printk(")\n");
  920. }
  921. return video_usercopy(file, cmd, arg, uvc_v4l2_do_ioctl);
  922. }
  923. #ifdef CONFIG_COMPAT
  924. struct uvc_xu_control_mapping32 {
  925. __u32 id;
  926. __u8 name[32];
  927. __u8 entity[16];
  928. __u8 selector;
  929. __u8 size;
  930. __u8 offset;
  931. __u32 v4l2_type;
  932. __u32 data_type;
  933. compat_caddr_t menu_info;
  934. __u32 menu_count;
  935. __u32 reserved[4];
  936. };
  937. static int uvc_v4l2_get_xu_mapping(struct uvc_xu_control_mapping *kp,
  938. const struct uvc_xu_control_mapping32 __user *up)
  939. {
  940. struct uvc_menu_info __user *umenus;
  941. struct uvc_menu_info __user *kmenus;
  942. compat_caddr_t p;
  943. if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
  944. __copy_from_user(kp, up, offsetof(typeof(*up), menu_info)) ||
  945. __get_user(kp->menu_count, &up->menu_count))
  946. return -EFAULT;
  947. memset(kp->reserved, 0, sizeof(kp->reserved));
  948. if (kp->menu_count == 0) {
  949. kp->menu_info = NULL;
  950. return 0;
  951. }
  952. if (__get_user(p, &up->menu_info))
  953. return -EFAULT;
  954. umenus = compat_ptr(p);
  955. if (!access_ok(VERIFY_READ, umenus, kp->menu_count * sizeof(*umenus)))
  956. return -EFAULT;
  957. kmenus = compat_alloc_user_space(kp->menu_count * sizeof(*kmenus));
  958. if (kmenus == NULL)
  959. return -EFAULT;
  960. kp->menu_info = kmenus;
  961. if (copy_in_user(kmenus, umenus, kp->menu_count * sizeof(*umenus)))
  962. return -EFAULT;
  963. return 0;
  964. }
  965. static int uvc_v4l2_put_xu_mapping(const struct uvc_xu_control_mapping *kp,
  966. struct uvc_xu_control_mapping32 __user *up)
  967. {
  968. struct uvc_menu_info __user *umenus;
  969. struct uvc_menu_info __user *kmenus = kp->menu_info;
  970. compat_caddr_t p;
  971. if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
  972. __copy_to_user(up, kp, offsetof(typeof(*up), menu_info)) ||
  973. __put_user(kp->menu_count, &up->menu_count))
  974. return -EFAULT;
  975. if (__clear_user(up->reserved, sizeof(up->reserved)))
  976. return -EFAULT;
  977. if (kp->menu_count == 0)
  978. return 0;
  979. if (get_user(p, &up->menu_info))
  980. return -EFAULT;
  981. umenus = compat_ptr(p);
  982. if (copy_in_user(umenus, kmenus, kp->menu_count * sizeof(*umenus)))
  983. return -EFAULT;
  984. return 0;
  985. }
  986. struct uvc_xu_control_query32 {
  987. __u8 unit;
  988. __u8 selector;
  989. __u8 query;
  990. __u16 size;
  991. compat_caddr_t data;
  992. };
  993. static int uvc_v4l2_get_xu_query(struct uvc_xu_control_query *kp,
  994. const struct uvc_xu_control_query32 __user *up)
  995. {
  996. u8 __user *udata;
  997. u8 __user *kdata;
  998. compat_caddr_t p;
  999. if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
  1000. __copy_from_user(kp, up, offsetof(typeof(*up), data)))
  1001. return -EFAULT;
  1002. if (kp->size == 0) {
  1003. kp->data = NULL;
  1004. return 0;
  1005. }
  1006. if (__get_user(p, &up->data))
  1007. return -EFAULT;
  1008. udata = compat_ptr(p);
  1009. if (!access_ok(VERIFY_READ, udata, kp->size))
  1010. return -EFAULT;
  1011. kdata = compat_alloc_user_space(kp->size);
  1012. if (kdata == NULL)
  1013. return -EFAULT;
  1014. kp->data = kdata;
  1015. if (copy_in_user(kdata, udata, kp->size))
  1016. return -EFAULT;
  1017. return 0;
  1018. }
  1019. static int uvc_v4l2_put_xu_query(const struct uvc_xu_control_query *kp,
  1020. struct uvc_xu_control_query32 __user *up)
  1021. {
  1022. u8 __user *udata;
  1023. u8 __user *kdata = kp->data;
  1024. compat_caddr_t p;
  1025. if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
  1026. __copy_to_user(up, kp, offsetof(typeof(*up), data)))
  1027. return -EFAULT;
  1028. if (kp->size == 0)
  1029. return 0;
  1030. if (get_user(p, &up->data))
  1031. return -EFAULT;
  1032. udata = compat_ptr(p);
  1033. if (!access_ok(VERIFY_READ, udata, kp->size))
  1034. return -EFAULT;
  1035. if (copy_in_user(udata, kdata, kp->size))
  1036. return -EFAULT;
  1037. return 0;
  1038. }
  1039. #define UVCIOC_CTRL_MAP32 _IOWR('u', 0x20, struct uvc_xu_control_mapping32)
  1040. #define UVCIOC_CTRL_QUERY32 _IOWR('u', 0x21, struct uvc_xu_control_query32)
  1041. static long uvc_v4l2_compat_ioctl32(struct file *file,
  1042. unsigned int cmd, unsigned long arg)
  1043. {
  1044. union {
  1045. struct uvc_xu_control_mapping xmap;
  1046. struct uvc_xu_control_query xqry;
  1047. } karg;
  1048. void __user *up = compat_ptr(arg);
  1049. mm_segment_t old_fs;
  1050. long ret;
  1051. switch (cmd) {
  1052. case UVCIOC_CTRL_MAP32:
  1053. cmd = UVCIOC_CTRL_MAP;
  1054. ret = uvc_v4l2_get_xu_mapping(&karg.xmap, up);
  1055. break;
  1056. case UVCIOC_CTRL_QUERY32:
  1057. cmd = UVCIOC_CTRL_QUERY;
  1058. ret = uvc_v4l2_get_xu_query(&karg.xqry, up);
  1059. break;
  1060. default:
  1061. return -ENOIOCTLCMD;
  1062. }
  1063. old_fs = get_fs();
  1064. set_fs(KERNEL_DS);
  1065. ret = uvc_v4l2_ioctl(file, cmd, (unsigned long)&karg);
  1066. set_fs(old_fs);
  1067. if (ret < 0)
  1068. return ret;
  1069. switch (cmd) {
  1070. case UVCIOC_CTRL_MAP:
  1071. ret = uvc_v4l2_put_xu_mapping(&karg.xmap, up);
  1072. break;
  1073. case UVCIOC_CTRL_QUERY:
  1074. ret = uvc_v4l2_put_xu_query(&karg.xqry, up);
  1075. break;
  1076. }
  1077. return ret;
  1078. }
  1079. #endif
  1080. static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
  1081. size_t count, loff_t *ppos)
  1082. {
  1083. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
  1084. return -EINVAL;
  1085. }
  1086. static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
  1087. {
  1088. struct uvc_fh *handle = file->private_data;
  1089. struct uvc_streaming *stream = handle->stream;
  1090. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
  1091. return uvc_queue_mmap(&stream->queue, vma);
  1092. }
  1093. static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait)
  1094. {
  1095. struct uvc_fh *handle = file->private_data;
  1096. struct uvc_streaming *stream = handle->stream;
  1097. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
  1098. return uvc_queue_poll(&stream->queue, file, wait);
  1099. }
  1100. #ifndef CONFIG_MMU
  1101. static unsigned long uvc_v4l2_get_unmapped_area(struct file *file,
  1102. unsigned long addr, unsigned long len, unsigned long pgoff,
  1103. unsigned long flags)
  1104. {
  1105. struct uvc_fh *handle = file->private_data;
  1106. struct uvc_streaming *stream = handle->stream;
  1107. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_get_unmapped_area\n");
  1108. return uvc_queue_get_unmapped_area(&stream->queue, pgoff);
  1109. }
  1110. #endif
  1111. const struct v4l2_file_operations uvc_fops = {
  1112. .owner = THIS_MODULE,
  1113. .open = uvc_v4l2_open,
  1114. .release = uvc_v4l2_release,
  1115. .unlocked_ioctl = uvc_v4l2_ioctl,
  1116. #ifdef CONFIG_COMPAT
  1117. .compat_ioctl32 = uvc_v4l2_compat_ioctl32,
  1118. #endif
  1119. .read = uvc_v4l2_read,
  1120. .mmap = uvc_v4l2_mmap,
  1121. .poll = uvc_v4l2_poll,
  1122. #ifndef CONFIG_MMU
  1123. .get_unmapped_area = uvc_v4l2_get_unmapped_area,
  1124. #endif
  1125. };