uvc_v4l2.c 32 KB

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