uvc_v4l2.c 38 KB

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