uvc_v4l2.c 37 KB

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