uvc_v4l2.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528
  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. ret = usb_autopm_get_interface(stream->dev->intf);
  404. if (ret < 0)
  405. return ret;
  406. /* Create the device handle. */
  407. handle = kzalloc(sizeof *handle, GFP_KERNEL);
  408. if (handle == NULL) {
  409. usb_autopm_put_interface(stream->dev->intf);
  410. return -ENOMEM;
  411. }
  412. mutex_lock(&stream->dev->lock);
  413. if (stream->dev->users == 0) {
  414. ret = uvc_status_start(stream->dev, GFP_KERNEL);
  415. if (ret < 0) {
  416. mutex_unlock(&stream->dev->lock);
  417. usb_autopm_put_interface(stream->dev->intf);
  418. kfree(handle);
  419. return ret;
  420. }
  421. }
  422. stream->dev->users++;
  423. mutex_unlock(&stream->dev->lock);
  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_queue_release(&stream->queue);
  440. /* Release the file handle. */
  441. uvc_dismiss_privileges(handle);
  442. v4l2_fh_del(&handle->vfh);
  443. v4l2_fh_exit(&handle->vfh);
  444. kfree(handle);
  445. file->private_data = NULL;
  446. mutex_lock(&stream->dev->lock);
  447. if (--stream->dev->users == 0)
  448. uvc_status_stop(stream->dev);
  449. mutex_unlock(&stream->dev->lock);
  450. usb_autopm_put_interface(stream->dev->intf);
  451. return 0;
  452. }
  453. static int uvc_ioctl_querycap(struct file *file, void *fh,
  454. struct v4l2_capability *cap)
  455. {
  456. struct video_device *vdev = video_devdata(file);
  457. struct uvc_fh *handle = file->private_data;
  458. struct uvc_video_chain *chain = handle->chain;
  459. struct uvc_streaming *stream = handle->stream;
  460. strlcpy(cap->driver, "uvcvideo", sizeof(cap->driver));
  461. strlcpy(cap->card, vdev->name, sizeof(cap->card));
  462. usb_make_path(stream->dev->udev, cap->bus_info, sizeof(cap->bus_info));
  463. cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
  464. | chain->caps;
  465. if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  466. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  467. else
  468. cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
  469. return 0;
  470. }
  471. static int uvc_ioctl_enum_fmt(struct uvc_streaming *stream,
  472. struct v4l2_fmtdesc *fmt)
  473. {
  474. struct uvc_format *format;
  475. enum v4l2_buf_type type = fmt->type;
  476. __u32 index = fmt->index;
  477. if (fmt->type != stream->type || fmt->index >= stream->nformats)
  478. return -EINVAL;
  479. memset(fmt, 0, sizeof(*fmt));
  480. fmt->index = index;
  481. fmt->type = type;
  482. format = &stream->format[fmt->index];
  483. fmt->flags = 0;
  484. if (format->flags & UVC_FMT_FLAG_COMPRESSED)
  485. fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
  486. strlcpy(fmt->description, format->name, sizeof(fmt->description));
  487. fmt->description[sizeof(fmt->description) - 1] = 0;
  488. fmt->pixelformat = format->fcc;
  489. return 0;
  490. }
  491. static int uvc_ioctl_enum_fmt_vid_cap(struct file *file, void *fh,
  492. struct v4l2_fmtdesc *fmt)
  493. {
  494. struct uvc_fh *handle = fh;
  495. struct uvc_streaming *stream = handle->stream;
  496. return uvc_ioctl_enum_fmt(stream, fmt);
  497. }
  498. static int uvc_ioctl_enum_fmt_vid_out(struct file *file, void *fh,
  499. struct v4l2_fmtdesc *fmt)
  500. {
  501. struct uvc_fh *handle = fh;
  502. struct uvc_streaming *stream = handle->stream;
  503. return uvc_ioctl_enum_fmt(stream, fmt);
  504. }
  505. static int uvc_ioctl_g_fmt_vid_cap(struct file *file, void *fh,
  506. struct v4l2_format *fmt)
  507. {
  508. struct uvc_fh *handle = fh;
  509. struct uvc_streaming *stream = handle->stream;
  510. return uvc_v4l2_get_format(stream, fmt);
  511. }
  512. static int uvc_ioctl_g_fmt_vid_out(struct file *file, void *fh,
  513. struct v4l2_format *fmt)
  514. {
  515. struct uvc_fh *handle = fh;
  516. struct uvc_streaming *stream = handle->stream;
  517. return uvc_v4l2_get_format(stream, fmt);
  518. }
  519. static int uvc_ioctl_s_fmt_vid_cap(struct file *file, void *fh,
  520. struct v4l2_format *fmt)
  521. {
  522. struct uvc_fh *handle = fh;
  523. struct uvc_streaming *stream = handle->stream;
  524. int ret;
  525. ret = uvc_acquire_privileges(handle);
  526. if (ret < 0)
  527. return ret;
  528. return uvc_v4l2_set_format(stream, fmt);
  529. }
  530. static int uvc_ioctl_s_fmt_vid_out(struct file *file, void *fh,
  531. struct v4l2_format *fmt)
  532. {
  533. struct uvc_fh *handle = fh;
  534. struct uvc_streaming *stream = handle->stream;
  535. int ret;
  536. ret = uvc_acquire_privileges(handle);
  537. if (ret < 0)
  538. return ret;
  539. return uvc_v4l2_set_format(stream, fmt);
  540. }
  541. static int uvc_ioctl_try_fmt_vid_cap(struct file *file, void *fh,
  542. struct v4l2_format *fmt)
  543. {
  544. struct uvc_fh *handle = fh;
  545. struct uvc_streaming *stream = handle->stream;
  546. struct uvc_streaming_control probe;
  547. return uvc_v4l2_try_format(stream, fmt, &probe, NULL, NULL);
  548. }
  549. static int uvc_ioctl_try_fmt_vid_out(struct file *file, void *fh,
  550. struct v4l2_format *fmt)
  551. {
  552. struct uvc_fh *handle = fh;
  553. struct uvc_streaming *stream = handle->stream;
  554. struct uvc_streaming_control probe;
  555. return uvc_v4l2_try_format(stream, fmt, &probe, NULL, NULL);
  556. }
  557. static int uvc_ioctl_reqbufs(struct file *file, void *fh,
  558. struct v4l2_requestbuffers *rb)
  559. {
  560. struct uvc_fh *handle = fh;
  561. struct uvc_streaming *stream = handle->stream;
  562. int ret;
  563. ret = uvc_acquire_privileges(handle);
  564. if (ret < 0)
  565. return ret;
  566. mutex_lock(&stream->mutex);
  567. ret = uvc_request_buffers(&stream->queue, rb);
  568. mutex_unlock(&stream->mutex);
  569. if (ret < 0)
  570. return ret;
  571. if (ret == 0)
  572. uvc_dismiss_privileges(handle);
  573. return 0;
  574. }
  575. static int uvc_ioctl_querybuf(struct file *file, void *fh,
  576. struct v4l2_buffer *buf)
  577. {
  578. struct uvc_fh *handle = fh;
  579. struct uvc_streaming *stream = handle->stream;
  580. if (!uvc_has_privileges(handle))
  581. return -EBUSY;
  582. return uvc_query_buffer(&stream->queue, buf);
  583. }
  584. static int uvc_ioctl_qbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
  585. {
  586. struct uvc_fh *handle = fh;
  587. struct uvc_streaming *stream = handle->stream;
  588. if (!uvc_has_privileges(handle))
  589. return -EBUSY;
  590. return uvc_queue_buffer(&stream->queue, buf);
  591. }
  592. static int uvc_ioctl_expbuf(struct file *file, void *fh,
  593. struct v4l2_exportbuffer *exp)
  594. {
  595. struct uvc_fh *handle = fh;
  596. struct uvc_streaming *stream = handle->stream;
  597. if (!uvc_has_privileges(handle))
  598. return -EBUSY;
  599. return uvc_export_buffer(&stream->queue, exp);
  600. }
  601. static int uvc_ioctl_dqbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
  602. {
  603. struct uvc_fh *handle = fh;
  604. struct uvc_streaming *stream = handle->stream;
  605. if (!uvc_has_privileges(handle))
  606. return -EBUSY;
  607. return uvc_dequeue_buffer(&stream->queue, buf,
  608. file->f_flags & O_NONBLOCK);
  609. }
  610. static int uvc_ioctl_create_bufs(struct file *file, void *fh,
  611. struct v4l2_create_buffers *cb)
  612. {
  613. struct uvc_fh *handle = fh;
  614. struct uvc_streaming *stream = handle->stream;
  615. int ret;
  616. ret = uvc_acquire_privileges(handle);
  617. if (ret < 0)
  618. return ret;
  619. return uvc_create_buffers(&stream->queue, cb);
  620. }
  621. static int uvc_ioctl_streamon(struct file *file, void *fh,
  622. enum v4l2_buf_type type)
  623. {
  624. struct uvc_fh *handle = fh;
  625. struct uvc_streaming *stream = handle->stream;
  626. int ret;
  627. if (!uvc_has_privileges(handle))
  628. return -EBUSY;
  629. mutex_lock(&stream->mutex);
  630. ret = uvc_queue_streamon(&stream->queue, type);
  631. mutex_unlock(&stream->mutex);
  632. return ret;
  633. }
  634. static int uvc_ioctl_streamoff(struct file *file, void *fh,
  635. enum v4l2_buf_type type)
  636. {
  637. struct uvc_fh *handle = fh;
  638. struct uvc_streaming *stream = handle->stream;
  639. if (!uvc_has_privileges(handle))
  640. return -EBUSY;
  641. mutex_lock(&stream->mutex);
  642. uvc_queue_streamoff(&stream->queue, type);
  643. mutex_unlock(&stream->mutex);
  644. return 0;
  645. }
  646. static int uvc_ioctl_enum_input(struct file *file, void *fh,
  647. struct v4l2_input *input)
  648. {
  649. struct uvc_fh *handle = fh;
  650. struct uvc_video_chain *chain = handle->chain;
  651. const struct uvc_entity *selector = chain->selector;
  652. struct uvc_entity *iterm = NULL;
  653. u32 index = input->index;
  654. int pin = 0;
  655. if (selector == NULL ||
  656. (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
  657. if (index != 0)
  658. return -EINVAL;
  659. list_for_each_entry(iterm, &chain->entities, chain) {
  660. if (UVC_ENTITY_IS_ITERM(iterm))
  661. break;
  662. }
  663. pin = iterm->id;
  664. } else if (index < selector->bNrInPins) {
  665. pin = selector->baSourceID[index];
  666. list_for_each_entry(iterm, &chain->entities, chain) {
  667. if (!UVC_ENTITY_IS_ITERM(iterm))
  668. continue;
  669. if (iterm->id == pin)
  670. break;
  671. }
  672. }
  673. if (iterm == NULL || iterm->id != pin)
  674. return -EINVAL;
  675. memset(input, 0, sizeof(*input));
  676. input->index = index;
  677. strlcpy(input->name, iterm->name, sizeof(input->name));
  678. if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA)
  679. input->type = V4L2_INPUT_TYPE_CAMERA;
  680. return 0;
  681. }
  682. static int uvc_ioctl_g_input(struct file *file, void *fh, unsigned int *input)
  683. {
  684. struct uvc_fh *handle = fh;
  685. struct uvc_video_chain *chain = handle->chain;
  686. int ret;
  687. u8 i;
  688. if (chain->selector == NULL ||
  689. (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
  690. *input = 0;
  691. return 0;
  692. }
  693. ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, chain->selector->id,
  694. chain->dev->intfnum, UVC_SU_INPUT_SELECT_CONTROL,
  695. &i, 1);
  696. if (ret < 0)
  697. return ret;
  698. *input = i - 1;
  699. return 0;
  700. }
  701. static int uvc_ioctl_s_input(struct file *file, void *fh, unsigned int input)
  702. {
  703. struct uvc_fh *handle = fh;
  704. struct uvc_video_chain *chain = handle->chain;
  705. int ret;
  706. u32 i;
  707. ret = uvc_acquire_privileges(handle);
  708. if (ret < 0)
  709. return ret;
  710. if (chain->selector == NULL ||
  711. (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
  712. if (input)
  713. return -EINVAL;
  714. return 0;
  715. }
  716. if (input >= chain->selector->bNrInPins)
  717. return -EINVAL;
  718. i = input + 1;
  719. return uvc_query_ctrl(chain->dev, UVC_SET_CUR, chain->selector->id,
  720. chain->dev->intfnum, UVC_SU_INPUT_SELECT_CONTROL,
  721. &i, 1);
  722. }
  723. static int uvc_ioctl_queryctrl(struct file *file, void *fh,
  724. struct v4l2_queryctrl *qc)
  725. {
  726. struct uvc_fh *handle = fh;
  727. struct uvc_video_chain *chain = handle->chain;
  728. return uvc_query_v4l2_ctrl(chain, qc);
  729. }
  730. static int uvc_ioctl_query_ext_ctrl(struct file *file, void *fh,
  731. struct v4l2_query_ext_ctrl *qec)
  732. {
  733. struct uvc_fh *handle = fh;
  734. struct uvc_video_chain *chain = handle->chain;
  735. struct v4l2_queryctrl qc = { qec->id };
  736. int ret;
  737. ret = uvc_query_v4l2_ctrl(chain, &qc);
  738. if (ret)
  739. return ret;
  740. qec->id = qc.id;
  741. qec->type = qc.type;
  742. strlcpy(qec->name, qc.name, sizeof(qec->name));
  743. qec->minimum = qc.minimum;
  744. qec->maximum = qc.maximum;
  745. qec->step = qc.step;
  746. qec->default_value = qc.default_value;
  747. qec->flags = qc.flags;
  748. qec->elem_size = 4;
  749. qec->elems = 1;
  750. qec->nr_of_dims = 0;
  751. memset(qec->dims, 0, sizeof(qec->dims));
  752. memset(qec->reserved, 0, sizeof(qec->reserved));
  753. return 0;
  754. }
  755. static int uvc_ioctl_g_ctrl(struct file *file, void *fh,
  756. struct v4l2_control *ctrl)
  757. {
  758. struct uvc_fh *handle = fh;
  759. struct uvc_video_chain *chain = handle->chain;
  760. struct v4l2_ext_control xctrl;
  761. int ret;
  762. memset(&xctrl, 0, sizeof(xctrl));
  763. xctrl.id = ctrl->id;
  764. ret = uvc_ctrl_begin(chain);
  765. if (ret < 0)
  766. return ret;
  767. ret = uvc_ctrl_get(chain, &xctrl);
  768. uvc_ctrl_rollback(handle);
  769. if (ret < 0)
  770. return ret;
  771. ctrl->value = xctrl.value;
  772. return 0;
  773. }
  774. static int uvc_ioctl_s_ctrl(struct file *file, void *fh,
  775. struct v4l2_control *ctrl)
  776. {
  777. struct uvc_fh *handle = fh;
  778. struct uvc_video_chain *chain = handle->chain;
  779. struct v4l2_ext_control xctrl;
  780. int ret;
  781. memset(&xctrl, 0, sizeof(xctrl));
  782. xctrl.id = ctrl->id;
  783. xctrl.value = ctrl->value;
  784. ret = uvc_ctrl_begin(chain);
  785. if (ret < 0)
  786. return ret;
  787. ret = uvc_ctrl_set(chain, &xctrl);
  788. if (ret < 0) {
  789. uvc_ctrl_rollback(handle);
  790. return ret;
  791. }
  792. ret = uvc_ctrl_commit(handle, &xctrl, 1);
  793. if (ret < 0)
  794. return ret;
  795. ctrl->value = xctrl.value;
  796. return 0;
  797. }
  798. static int uvc_ioctl_g_ext_ctrls(struct file *file, void *fh,
  799. struct v4l2_ext_controls *ctrls)
  800. {
  801. struct uvc_fh *handle = fh;
  802. struct uvc_video_chain *chain = handle->chain;
  803. struct v4l2_ext_control *ctrl = ctrls->controls;
  804. unsigned int i;
  805. int ret;
  806. ret = uvc_ctrl_begin(chain);
  807. if (ret < 0)
  808. return ret;
  809. for (i = 0; i < ctrls->count; ++ctrl, ++i) {
  810. ret = uvc_ctrl_get(chain, ctrl);
  811. if (ret < 0) {
  812. uvc_ctrl_rollback(handle);
  813. ctrls->error_idx = i;
  814. return ret;
  815. }
  816. }
  817. ctrls->error_idx = 0;
  818. return uvc_ctrl_rollback(handle);
  819. }
  820. static int uvc_ioctl_s_try_ext_ctrls(struct uvc_fh *handle,
  821. struct v4l2_ext_controls *ctrls,
  822. bool commit)
  823. {
  824. struct v4l2_ext_control *ctrl = ctrls->controls;
  825. struct uvc_video_chain *chain = handle->chain;
  826. unsigned int i;
  827. int ret;
  828. ret = uvc_ctrl_begin(chain);
  829. if (ret < 0)
  830. return ret;
  831. for (i = 0; i < ctrls->count; ++ctrl, ++i) {
  832. ret = uvc_ctrl_set(chain, ctrl);
  833. if (ret < 0) {
  834. uvc_ctrl_rollback(handle);
  835. ctrls->error_idx = commit ? ctrls->count : i;
  836. return ret;
  837. }
  838. }
  839. ctrls->error_idx = 0;
  840. if (commit)
  841. return uvc_ctrl_commit(handle, ctrls->controls, ctrls->count);
  842. else
  843. return uvc_ctrl_rollback(handle);
  844. }
  845. static int uvc_ioctl_s_ext_ctrls(struct file *file, void *fh,
  846. struct v4l2_ext_controls *ctrls)
  847. {
  848. struct uvc_fh *handle = fh;
  849. return uvc_ioctl_s_try_ext_ctrls(handle, ctrls, true);
  850. }
  851. static int uvc_ioctl_try_ext_ctrls(struct file *file, void *fh,
  852. struct v4l2_ext_controls *ctrls)
  853. {
  854. struct uvc_fh *handle = fh;
  855. return uvc_ioctl_s_try_ext_ctrls(handle, ctrls, false);
  856. }
  857. static int uvc_ioctl_querymenu(struct file *file, void *fh,
  858. struct v4l2_querymenu *qm)
  859. {
  860. struct uvc_fh *handle = fh;
  861. struct uvc_video_chain *chain = handle->chain;
  862. return uvc_query_v4l2_menu(chain, qm);
  863. }
  864. static int uvc_ioctl_g_selection(struct file *file, void *fh,
  865. struct v4l2_selection *sel)
  866. {
  867. struct uvc_fh *handle = fh;
  868. struct uvc_streaming *stream = handle->stream;
  869. if (sel->type != stream->type)
  870. return -EINVAL;
  871. switch (sel->target) {
  872. case V4L2_SEL_TGT_CROP_DEFAULT:
  873. case V4L2_SEL_TGT_CROP_BOUNDS:
  874. if (stream->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  875. return -EINVAL;
  876. break;
  877. case V4L2_SEL_TGT_COMPOSE_DEFAULT:
  878. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  879. if (stream->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  880. return -EINVAL;
  881. break;
  882. default:
  883. return -EINVAL;
  884. }
  885. sel->r.left = 0;
  886. sel->r.top = 0;
  887. mutex_lock(&stream->mutex);
  888. sel->r.width = stream->cur_frame->wWidth;
  889. sel->r.height = stream->cur_frame->wHeight;
  890. mutex_unlock(&stream->mutex);
  891. return 0;
  892. }
  893. static int uvc_ioctl_g_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. return uvc_v4l2_get_streamparm(stream, parm);
  899. }
  900. static int uvc_ioctl_s_parm(struct file *file, void *fh,
  901. struct v4l2_streamparm *parm)
  902. {
  903. struct uvc_fh *handle = fh;
  904. struct uvc_streaming *stream = handle->stream;
  905. int ret;
  906. ret = uvc_acquire_privileges(handle);
  907. if (ret < 0)
  908. return ret;
  909. return uvc_v4l2_set_streamparm(stream, parm);
  910. }
  911. static int uvc_ioctl_enum_framesizes(struct file *file, void *fh,
  912. struct v4l2_frmsizeenum *fsize)
  913. {
  914. struct uvc_fh *handle = fh;
  915. struct uvc_streaming *stream = handle->stream;
  916. struct uvc_format *format = NULL;
  917. struct uvc_frame *frame;
  918. int i;
  919. /* Look for the given pixel format */
  920. for (i = 0; i < stream->nformats; i++) {
  921. if (stream->format[i].fcc == fsize->pixel_format) {
  922. format = &stream->format[i];
  923. break;
  924. }
  925. }
  926. if (format == NULL)
  927. return -EINVAL;
  928. if (fsize->index >= format->nframes)
  929. return -EINVAL;
  930. frame = &format->frame[fsize->index];
  931. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  932. fsize->discrete.width = frame->wWidth;
  933. fsize->discrete.height = frame->wHeight;
  934. return 0;
  935. }
  936. static int uvc_ioctl_enum_frameintervals(struct file *file, void *fh,
  937. struct v4l2_frmivalenum *fival)
  938. {
  939. struct uvc_fh *handle = fh;
  940. struct uvc_streaming *stream = handle->stream;
  941. struct uvc_format *format = NULL;
  942. struct uvc_frame *frame = NULL;
  943. int i;
  944. /* Look for the given pixel format and frame size */
  945. for (i = 0; i < stream->nformats; i++) {
  946. if (stream->format[i].fcc == fival->pixel_format) {
  947. format = &stream->format[i];
  948. break;
  949. }
  950. }
  951. if (format == NULL)
  952. return -EINVAL;
  953. for (i = 0; i < format->nframes; i++) {
  954. if (format->frame[i].wWidth == fival->width &&
  955. format->frame[i].wHeight == fival->height) {
  956. frame = &format->frame[i];
  957. break;
  958. }
  959. }
  960. if (frame == NULL)
  961. return -EINVAL;
  962. if (frame->bFrameIntervalType) {
  963. if (fival->index >= frame->bFrameIntervalType)
  964. return -EINVAL;
  965. fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
  966. fival->discrete.numerator =
  967. frame->dwFrameInterval[fival->index];
  968. fival->discrete.denominator = 10000000;
  969. uvc_simplify_fraction(&fival->discrete.numerator,
  970. &fival->discrete.denominator, 8, 333);
  971. } else {
  972. if (fival->index)
  973. return -EINVAL;
  974. fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
  975. fival->stepwise.min.numerator = frame->dwFrameInterval[0];
  976. fival->stepwise.min.denominator = 10000000;
  977. fival->stepwise.max.numerator = frame->dwFrameInterval[1];
  978. fival->stepwise.max.denominator = 10000000;
  979. fival->stepwise.step.numerator = frame->dwFrameInterval[2];
  980. fival->stepwise.step.denominator = 10000000;
  981. uvc_simplify_fraction(&fival->stepwise.min.numerator,
  982. &fival->stepwise.min.denominator, 8, 333);
  983. uvc_simplify_fraction(&fival->stepwise.max.numerator,
  984. &fival->stepwise.max.denominator, 8, 333);
  985. uvc_simplify_fraction(&fival->stepwise.step.numerator,
  986. &fival->stepwise.step.denominator, 8, 333);
  987. }
  988. return 0;
  989. }
  990. static int uvc_ioctl_subscribe_event(struct v4l2_fh *fh,
  991. const struct v4l2_event_subscription *sub)
  992. {
  993. switch (sub->type) {
  994. case V4L2_EVENT_CTRL:
  995. return v4l2_event_subscribe(fh, sub, 0, &uvc_ctrl_sub_ev_ops);
  996. default:
  997. return -EINVAL;
  998. }
  999. }
  1000. static long uvc_ioctl_default(struct file *file, void *fh, bool valid_prio,
  1001. unsigned int cmd, void *arg)
  1002. {
  1003. struct uvc_fh *handle = fh;
  1004. struct uvc_video_chain *chain = handle->chain;
  1005. switch (cmd) {
  1006. /* Dynamic controls. */
  1007. case UVCIOC_CTRL_MAP:
  1008. return uvc_ioctl_ctrl_map(chain, arg);
  1009. case UVCIOC_CTRL_QUERY:
  1010. return uvc_xu_ctrl_query(chain, arg);
  1011. default:
  1012. return -ENOTTY;
  1013. }
  1014. }
  1015. #ifdef CONFIG_COMPAT
  1016. struct uvc_xu_control_mapping32 {
  1017. __u32 id;
  1018. __u8 name[32];
  1019. __u8 entity[16];
  1020. __u8 selector;
  1021. __u8 size;
  1022. __u8 offset;
  1023. __u32 v4l2_type;
  1024. __u32 data_type;
  1025. compat_caddr_t menu_info;
  1026. __u32 menu_count;
  1027. __u32 reserved[4];
  1028. };
  1029. static int uvc_v4l2_get_xu_mapping(struct uvc_xu_control_mapping *kp,
  1030. const struct uvc_xu_control_mapping32 __user *up)
  1031. {
  1032. struct uvc_menu_info __user *umenus;
  1033. struct uvc_menu_info __user *kmenus;
  1034. compat_caddr_t p;
  1035. if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
  1036. __copy_from_user(kp, up, offsetof(typeof(*up), menu_info)) ||
  1037. __get_user(kp->menu_count, &up->menu_count))
  1038. return -EFAULT;
  1039. memset(kp->reserved, 0, sizeof(kp->reserved));
  1040. if (kp->menu_count == 0) {
  1041. kp->menu_info = NULL;
  1042. return 0;
  1043. }
  1044. if (__get_user(p, &up->menu_info))
  1045. return -EFAULT;
  1046. umenus = compat_ptr(p);
  1047. if (!access_ok(VERIFY_READ, umenus, kp->menu_count * sizeof(*umenus)))
  1048. return -EFAULT;
  1049. kmenus = compat_alloc_user_space(kp->menu_count * sizeof(*kmenus));
  1050. if (kmenus == NULL)
  1051. return -EFAULT;
  1052. kp->menu_info = kmenus;
  1053. if (copy_in_user(kmenus, umenus, kp->menu_count * sizeof(*umenus)))
  1054. return -EFAULT;
  1055. return 0;
  1056. }
  1057. static int uvc_v4l2_put_xu_mapping(const struct uvc_xu_control_mapping *kp,
  1058. struct uvc_xu_control_mapping32 __user *up)
  1059. {
  1060. struct uvc_menu_info __user *umenus;
  1061. struct uvc_menu_info __user *kmenus = kp->menu_info;
  1062. compat_caddr_t p;
  1063. if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
  1064. __copy_to_user(up, kp, offsetof(typeof(*up), menu_info)) ||
  1065. __put_user(kp->menu_count, &up->menu_count))
  1066. return -EFAULT;
  1067. if (__clear_user(up->reserved, sizeof(up->reserved)))
  1068. return -EFAULT;
  1069. if (kp->menu_count == 0)
  1070. return 0;
  1071. if (get_user(p, &up->menu_info))
  1072. return -EFAULT;
  1073. umenus = compat_ptr(p);
  1074. if (copy_in_user(umenus, kmenus, kp->menu_count * sizeof(*umenus)))
  1075. return -EFAULT;
  1076. return 0;
  1077. }
  1078. struct uvc_xu_control_query32 {
  1079. __u8 unit;
  1080. __u8 selector;
  1081. __u8 query;
  1082. __u16 size;
  1083. compat_caddr_t data;
  1084. };
  1085. static int uvc_v4l2_get_xu_query(struct uvc_xu_control_query *kp,
  1086. const struct uvc_xu_control_query32 __user *up)
  1087. {
  1088. u8 __user *udata;
  1089. u8 __user *kdata;
  1090. compat_caddr_t p;
  1091. if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
  1092. __copy_from_user(kp, up, offsetof(typeof(*up), data)))
  1093. return -EFAULT;
  1094. if (kp->size == 0) {
  1095. kp->data = NULL;
  1096. return 0;
  1097. }
  1098. if (__get_user(p, &up->data))
  1099. return -EFAULT;
  1100. udata = compat_ptr(p);
  1101. if (!access_ok(VERIFY_READ, udata, kp->size))
  1102. return -EFAULT;
  1103. kdata = compat_alloc_user_space(kp->size);
  1104. if (kdata == NULL)
  1105. return -EFAULT;
  1106. kp->data = kdata;
  1107. if (copy_in_user(kdata, udata, kp->size))
  1108. return -EFAULT;
  1109. return 0;
  1110. }
  1111. static int uvc_v4l2_put_xu_query(const struct uvc_xu_control_query *kp,
  1112. struct uvc_xu_control_query32 __user *up)
  1113. {
  1114. u8 __user *udata;
  1115. u8 __user *kdata = kp->data;
  1116. compat_caddr_t p;
  1117. if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
  1118. __copy_to_user(up, kp, offsetof(typeof(*up), data)))
  1119. return -EFAULT;
  1120. if (kp->size == 0)
  1121. return 0;
  1122. if (get_user(p, &up->data))
  1123. return -EFAULT;
  1124. udata = compat_ptr(p);
  1125. if (!access_ok(VERIFY_READ, udata, kp->size))
  1126. return -EFAULT;
  1127. if (copy_in_user(udata, kdata, kp->size))
  1128. return -EFAULT;
  1129. return 0;
  1130. }
  1131. #define UVCIOC_CTRL_MAP32 _IOWR('u', 0x20, struct uvc_xu_control_mapping32)
  1132. #define UVCIOC_CTRL_QUERY32 _IOWR('u', 0x21, struct uvc_xu_control_query32)
  1133. static long uvc_v4l2_compat_ioctl32(struct file *file,
  1134. unsigned int cmd, unsigned long arg)
  1135. {
  1136. struct uvc_fh *handle = file->private_data;
  1137. union {
  1138. struct uvc_xu_control_mapping xmap;
  1139. struct uvc_xu_control_query xqry;
  1140. } karg;
  1141. void __user *up = compat_ptr(arg);
  1142. long ret;
  1143. switch (cmd) {
  1144. case UVCIOC_CTRL_MAP32:
  1145. ret = uvc_v4l2_get_xu_mapping(&karg.xmap, up);
  1146. if (ret)
  1147. return ret;
  1148. ret = uvc_ioctl_ctrl_map(handle->chain, &karg.xmap);
  1149. if (ret)
  1150. return ret;
  1151. ret = uvc_v4l2_put_xu_mapping(&karg.xmap, up);
  1152. if (ret)
  1153. return ret;
  1154. break;
  1155. case UVCIOC_CTRL_QUERY32:
  1156. ret = uvc_v4l2_get_xu_query(&karg.xqry, up);
  1157. if (ret)
  1158. return ret;
  1159. ret = uvc_xu_ctrl_query(handle->chain, &karg.xqry);
  1160. if (ret)
  1161. return ret;
  1162. ret = uvc_v4l2_put_xu_query(&karg.xqry, up);
  1163. if (ret)
  1164. return ret;
  1165. break;
  1166. default:
  1167. return -ENOIOCTLCMD;
  1168. }
  1169. return ret;
  1170. }
  1171. #endif
  1172. static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
  1173. size_t count, loff_t *ppos)
  1174. {
  1175. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
  1176. return -EINVAL;
  1177. }
  1178. static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
  1179. {
  1180. struct uvc_fh *handle = file->private_data;
  1181. struct uvc_streaming *stream = handle->stream;
  1182. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
  1183. return uvc_queue_mmap(&stream->queue, vma);
  1184. }
  1185. static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait)
  1186. {
  1187. struct uvc_fh *handle = file->private_data;
  1188. struct uvc_streaming *stream = handle->stream;
  1189. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
  1190. return uvc_queue_poll(&stream->queue, file, wait);
  1191. }
  1192. #ifndef CONFIG_MMU
  1193. static unsigned long uvc_v4l2_get_unmapped_area(struct file *file,
  1194. unsigned long addr, unsigned long len, unsigned long pgoff,
  1195. unsigned long flags)
  1196. {
  1197. struct uvc_fh *handle = file->private_data;
  1198. struct uvc_streaming *stream = handle->stream;
  1199. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_get_unmapped_area\n");
  1200. return uvc_queue_get_unmapped_area(&stream->queue, pgoff);
  1201. }
  1202. #endif
  1203. const struct v4l2_ioctl_ops uvc_ioctl_ops = {
  1204. .vidioc_querycap = uvc_ioctl_querycap,
  1205. .vidioc_enum_fmt_vid_cap = uvc_ioctl_enum_fmt_vid_cap,
  1206. .vidioc_enum_fmt_vid_out = uvc_ioctl_enum_fmt_vid_out,
  1207. .vidioc_g_fmt_vid_cap = uvc_ioctl_g_fmt_vid_cap,
  1208. .vidioc_g_fmt_vid_out = uvc_ioctl_g_fmt_vid_out,
  1209. .vidioc_s_fmt_vid_cap = uvc_ioctl_s_fmt_vid_cap,
  1210. .vidioc_s_fmt_vid_out = uvc_ioctl_s_fmt_vid_out,
  1211. .vidioc_try_fmt_vid_cap = uvc_ioctl_try_fmt_vid_cap,
  1212. .vidioc_try_fmt_vid_out = uvc_ioctl_try_fmt_vid_out,
  1213. .vidioc_reqbufs = uvc_ioctl_reqbufs,
  1214. .vidioc_querybuf = uvc_ioctl_querybuf,
  1215. .vidioc_qbuf = uvc_ioctl_qbuf,
  1216. .vidioc_expbuf = uvc_ioctl_expbuf,
  1217. .vidioc_dqbuf = uvc_ioctl_dqbuf,
  1218. .vidioc_create_bufs = uvc_ioctl_create_bufs,
  1219. .vidioc_streamon = uvc_ioctl_streamon,
  1220. .vidioc_streamoff = uvc_ioctl_streamoff,
  1221. .vidioc_enum_input = uvc_ioctl_enum_input,
  1222. .vidioc_g_input = uvc_ioctl_g_input,
  1223. .vidioc_s_input = uvc_ioctl_s_input,
  1224. .vidioc_queryctrl = uvc_ioctl_queryctrl,
  1225. .vidioc_query_ext_ctrl = uvc_ioctl_query_ext_ctrl,
  1226. .vidioc_g_ctrl = uvc_ioctl_g_ctrl,
  1227. .vidioc_s_ctrl = uvc_ioctl_s_ctrl,
  1228. .vidioc_g_ext_ctrls = uvc_ioctl_g_ext_ctrls,
  1229. .vidioc_s_ext_ctrls = uvc_ioctl_s_ext_ctrls,
  1230. .vidioc_try_ext_ctrls = uvc_ioctl_try_ext_ctrls,
  1231. .vidioc_querymenu = uvc_ioctl_querymenu,
  1232. .vidioc_g_selection = uvc_ioctl_g_selection,
  1233. .vidioc_g_parm = uvc_ioctl_g_parm,
  1234. .vidioc_s_parm = uvc_ioctl_s_parm,
  1235. .vidioc_enum_framesizes = uvc_ioctl_enum_framesizes,
  1236. .vidioc_enum_frameintervals = uvc_ioctl_enum_frameintervals,
  1237. .vidioc_subscribe_event = uvc_ioctl_subscribe_event,
  1238. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1239. .vidioc_default = uvc_ioctl_default,
  1240. };
  1241. const struct v4l2_file_operations uvc_fops = {
  1242. .owner = THIS_MODULE,
  1243. .open = uvc_v4l2_open,
  1244. .release = uvc_v4l2_release,
  1245. .unlocked_ioctl = video_ioctl2,
  1246. #ifdef CONFIG_COMPAT
  1247. .compat_ioctl32 = uvc_v4l2_compat_ioctl32,
  1248. #endif
  1249. .read = uvc_v4l2_read,
  1250. .mmap = uvc_v4l2_mmap,
  1251. .poll = uvc_v4l2_poll,
  1252. #ifndef CONFIG_MMU
  1253. .get_unmapped_area = uvc_v4l2_get_unmapped_area,
  1254. #endif
  1255. };