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