uvc_v4l2.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290
  1. /*
  2. * uvc_v4l2.c -- USB Video Class driver - V4L2 API
  3. *
  4. * Copyright (C) 2005-2010
  5. * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. */
  13. #include <linux/compat.h>
  14. #include <linux/kernel.h>
  15. #include <linux/version.h>
  16. #include <linux/list.h>
  17. #include <linux/module.h>
  18. #include <linux/slab.h>
  19. #include <linux/usb.h>
  20. #include <linux/videodev2.h>
  21. #include <linux/vmalloc.h>
  22. #include <linux/mm.h>
  23. #include <linux/wait.h>
  24. #include <linux/atomic.h>
  25. #include <media/v4l2-common.h>
  26. #include <media/v4l2-ioctl.h>
  27. #include "uvcvideo.h"
  28. /* ------------------------------------------------------------------------
  29. * UVC ioctls
  30. */
  31. static int uvc_ioctl_ctrl_map(struct uvc_video_chain *chain,
  32. struct uvc_xu_control_mapping *xmap)
  33. {
  34. struct uvc_control_mapping *map;
  35. unsigned int size;
  36. int ret;
  37. map = kzalloc(sizeof *map, GFP_KERNEL);
  38. if (map == NULL)
  39. return -ENOMEM;
  40. map->id = xmap->id;
  41. memcpy(map->name, xmap->name, sizeof map->name);
  42. memcpy(map->entity, xmap->entity, sizeof map->entity);
  43. map->selector = xmap->selector;
  44. map->size = xmap->size;
  45. map->offset = xmap->offset;
  46. map->v4l2_type = xmap->v4l2_type;
  47. map->data_type = xmap->data_type;
  48. switch (xmap->v4l2_type) {
  49. case V4L2_CTRL_TYPE_INTEGER:
  50. case V4L2_CTRL_TYPE_BOOLEAN:
  51. case V4L2_CTRL_TYPE_BUTTON:
  52. break;
  53. case V4L2_CTRL_TYPE_MENU:
  54. /* Prevent excessive memory consumption, as well as integer
  55. * overflows.
  56. */
  57. if (xmap->menu_count == 0 ||
  58. xmap->menu_count > UVC_MAX_CONTROL_MENU_ENTRIES) {
  59. ret = -EINVAL;
  60. goto done;
  61. }
  62. size = xmap->menu_count * sizeof(*map->menu_info);
  63. map->menu_info = kmalloc(size, GFP_KERNEL);
  64. if (map->menu_info == NULL) {
  65. ret = -ENOMEM;
  66. goto done;
  67. }
  68. if (copy_from_user(map->menu_info, xmap->menu_info, size)) {
  69. ret = -EFAULT;
  70. goto done;
  71. }
  72. map->menu_count = xmap->menu_count;
  73. break;
  74. default:
  75. uvc_trace(UVC_TRACE_CONTROL, "Unsupported V4L2 control type "
  76. "%u.\n", xmap->v4l2_type);
  77. ret = -ENOTTY;
  78. goto done;
  79. }
  80. ret = uvc_ctrl_add_mapping(chain, map);
  81. done:
  82. kfree(map->menu_info);
  83. kfree(map);
  84. return ret;
  85. }
  86. /* ------------------------------------------------------------------------
  87. * V4L2 interface
  88. */
  89. /*
  90. * Find the frame interval closest to the requested frame interval for the
  91. * given frame format and size. This should be done by the device as part of
  92. * the Video Probe and Commit negotiation, but some hardware don't implement
  93. * that feature.
  94. */
  95. static __u32 uvc_try_frame_interval(struct uvc_frame *frame, __u32 interval)
  96. {
  97. unsigned int i;
  98. if (frame->bFrameIntervalType) {
  99. __u32 best = -1, dist;
  100. for (i = 0; i < frame->bFrameIntervalType; ++i) {
  101. dist = interval > frame->dwFrameInterval[i]
  102. ? interval - frame->dwFrameInterval[i]
  103. : frame->dwFrameInterval[i] - interval;
  104. if (dist > best)
  105. break;
  106. best = dist;
  107. }
  108. interval = frame->dwFrameInterval[i-1];
  109. } else {
  110. const __u32 min = frame->dwFrameInterval[0];
  111. const __u32 max = frame->dwFrameInterval[1];
  112. const __u32 step = frame->dwFrameInterval[2];
  113. interval = min + (interval - min + step/2) / step * step;
  114. if (interval > max)
  115. interval = max;
  116. }
  117. return interval;
  118. }
  119. static int uvc_v4l2_try_format(struct uvc_streaming *stream,
  120. struct v4l2_format *fmt, struct uvc_streaming_control *probe,
  121. struct uvc_format **uvc_format, struct uvc_frame **uvc_frame)
  122. {
  123. struct uvc_format *format = NULL;
  124. struct uvc_frame *frame = NULL;
  125. __u16 rw, rh;
  126. unsigned int d, maxd;
  127. unsigned int i;
  128. __u32 interval;
  129. int ret = 0;
  130. __u8 *fcc;
  131. if (fmt->type != stream->type)
  132. return -EINVAL;
  133. fcc = (__u8 *)&fmt->fmt.pix.pixelformat;
  134. uvc_trace(UVC_TRACE_FORMAT, "Trying format 0x%08x (%c%c%c%c): %ux%u.\n",
  135. fmt->fmt.pix.pixelformat,
  136. fcc[0], fcc[1], fcc[2], fcc[3],
  137. fmt->fmt.pix.width, fmt->fmt.pix.height);
  138. /* Check if the hardware supports the requested format. */
  139. for (i = 0; i < stream->nformats; ++i) {
  140. format = &stream->format[i];
  141. if (format->fcc == fmt->fmt.pix.pixelformat)
  142. break;
  143. }
  144. if (format == NULL || format->fcc != fmt->fmt.pix.pixelformat) {
  145. uvc_trace(UVC_TRACE_FORMAT, "Unsupported format 0x%08x.\n",
  146. fmt->fmt.pix.pixelformat);
  147. return -EINVAL;
  148. }
  149. /* Find the closest image size. The distance between image sizes is
  150. * the size in pixels of the non-overlapping regions between the
  151. * requested size and the frame-specified size.
  152. */
  153. rw = fmt->fmt.pix.width;
  154. rh = fmt->fmt.pix.height;
  155. maxd = (unsigned int)-1;
  156. for (i = 0; i < format->nframes; ++i) {
  157. __u16 w = format->frame[i].wWidth;
  158. __u16 h = format->frame[i].wHeight;
  159. d = min(w, rw) * min(h, rh);
  160. d = w*h + rw*rh - 2*d;
  161. if (d < maxd) {
  162. maxd = d;
  163. frame = &format->frame[i];
  164. }
  165. if (maxd == 0)
  166. break;
  167. }
  168. if (frame == NULL) {
  169. uvc_trace(UVC_TRACE_FORMAT, "Unsupported size %ux%u.\n",
  170. fmt->fmt.pix.width, fmt->fmt.pix.height);
  171. return -EINVAL;
  172. }
  173. /* Use the default frame interval. */
  174. interval = frame->dwDefaultFrameInterval;
  175. uvc_trace(UVC_TRACE_FORMAT, "Using default frame interval %u.%u us "
  176. "(%u.%u fps).\n", interval/10, interval%10, 10000000/interval,
  177. (100000000/interval)%10);
  178. /* Set the format index, frame index and frame interval. */
  179. memset(probe, 0, sizeof *probe);
  180. probe->bmHint = 1; /* dwFrameInterval */
  181. probe->bFormatIndex = format->index;
  182. probe->bFrameIndex = frame->bFrameIndex;
  183. probe->dwFrameInterval = uvc_try_frame_interval(frame, interval);
  184. /* Some webcams stall the probe control set request when the
  185. * dwMaxVideoFrameSize field is set to zero. The UVC specification
  186. * clearly states that the field is read-only from the host, so this
  187. * is a webcam bug. Set dwMaxVideoFrameSize to the value reported by
  188. * the webcam to work around the problem.
  189. *
  190. * The workaround could probably be enabled for all webcams, so the
  191. * quirk can be removed if needed. It's currently useful to detect
  192. * webcam bugs and fix them before they hit the market (providing
  193. * developers test their webcams with the Linux driver as well as with
  194. * the Windows driver).
  195. */
  196. mutex_lock(&stream->mutex);
  197. if (stream->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS)
  198. probe->dwMaxVideoFrameSize =
  199. stream->ctrl.dwMaxVideoFrameSize;
  200. /* Probe the device. */
  201. ret = uvc_probe_video(stream, probe);
  202. mutex_unlock(&stream->mutex);
  203. if (ret < 0)
  204. goto done;
  205. fmt->fmt.pix.width = frame->wWidth;
  206. fmt->fmt.pix.height = frame->wHeight;
  207. fmt->fmt.pix.field = V4L2_FIELD_NONE;
  208. fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
  209. fmt->fmt.pix.sizeimage = probe->dwMaxVideoFrameSize;
  210. fmt->fmt.pix.colorspace = format->colorspace;
  211. fmt->fmt.pix.priv = 0;
  212. if (uvc_format != NULL)
  213. *uvc_format = format;
  214. if (uvc_frame != NULL)
  215. *uvc_frame = frame;
  216. done:
  217. return ret;
  218. }
  219. static int uvc_v4l2_get_format(struct uvc_streaming *stream,
  220. struct v4l2_format *fmt)
  221. {
  222. struct uvc_format *format;
  223. struct uvc_frame *frame;
  224. int ret = 0;
  225. if (fmt->type != stream->type)
  226. return -EINVAL;
  227. mutex_lock(&stream->mutex);
  228. format = stream->cur_format;
  229. frame = stream->cur_frame;
  230. if (format == NULL || frame == NULL) {
  231. ret = -EINVAL;
  232. goto done;
  233. }
  234. fmt->fmt.pix.pixelformat = format->fcc;
  235. fmt->fmt.pix.width = frame->wWidth;
  236. fmt->fmt.pix.height = frame->wHeight;
  237. fmt->fmt.pix.field = V4L2_FIELD_NONE;
  238. fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
  239. fmt->fmt.pix.sizeimage = stream->ctrl.dwMaxVideoFrameSize;
  240. fmt->fmt.pix.colorspace = format->colorspace;
  241. fmt->fmt.pix.priv = 0;
  242. done:
  243. mutex_unlock(&stream->mutex);
  244. return ret;
  245. }
  246. static int uvc_v4l2_set_format(struct uvc_streaming *stream,
  247. struct v4l2_format *fmt)
  248. {
  249. struct uvc_streaming_control probe;
  250. struct uvc_format *format;
  251. struct uvc_frame *frame;
  252. int ret;
  253. if (fmt->type != stream->type)
  254. return -EINVAL;
  255. ret = uvc_v4l2_try_format(stream, fmt, &probe, &format, &frame);
  256. if (ret < 0)
  257. return ret;
  258. mutex_lock(&stream->mutex);
  259. if (uvc_queue_allocated(&stream->queue)) {
  260. ret = -EBUSY;
  261. goto done;
  262. }
  263. memcpy(&stream->ctrl, &probe, sizeof probe);
  264. stream->cur_format = format;
  265. stream->cur_frame = frame;
  266. done:
  267. mutex_unlock(&stream->mutex);
  268. return ret;
  269. }
  270. static int uvc_v4l2_get_streamparm(struct uvc_streaming *stream,
  271. struct v4l2_streamparm *parm)
  272. {
  273. uint32_t numerator, denominator;
  274. if (parm->type != stream->type)
  275. return -EINVAL;
  276. mutex_lock(&stream->mutex);
  277. numerator = stream->ctrl.dwFrameInterval;
  278. mutex_unlock(&stream->mutex);
  279. denominator = 10000000;
  280. uvc_simplify_fraction(&numerator, &denominator, 8, 333);
  281. memset(parm, 0, sizeof *parm);
  282. parm->type = stream->type;
  283. if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  284. parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
  285. parm->parm.capture.capturemode = 0;
  286. parm->parm.capture.timeperframe.numerator = numerator;
  287. parm->parm.capture.timeperframe.denominator = denominator;
  288. parm->parm.capture.extendedmode = 0;
  289. parm->parm.capture.readbuffers = 0;
  290. } else {
  291. parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
  292. parm->parm.output.outputmode = 0;
  293. parm->parm.output.timeperframe.numerator = numerator;
  294. parm->parm.output.timeperframe.denominator = denominator;
  295. }
  296. return 0;
  297. }
  298. static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream,
  299. struct v4l2_streamparm *parm)
  300. {
  301. struct uvc_streaming_control probe;
  302. struct v4l2_fract timeperframe;
  303. uint32_t interval;
  304. int ret;
  305. if (parm->type != stream->type)
  306. return -EINVAL;
  307. if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  308. timeperframe = parm->parm.capture.timeperframe;
  309. else
  310. timeperframe = parm->parm.output.timeperframe;
  311. interval = uvc_fraction_to_interval(timeperframe.numerator,
  312. timeperframe.denominator);
  313. uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n",
  314. timeperframe.numerator, timeperframe.denominator, interval);
  315. mutex_lock(&stream->mutex);
  316. if (uvc_queue_streaming(&stream->queue)) {
  317. mutex_unlock(&stream->mutex);
  318. return -EBUSY;
  319. }
  320. memcpy(&probe, &stream->ctrl, sizeof probe);
  321. probe.dwFrameInterval =
  322. uvc_try_frame_interval(stream->cur_frame, interval);
  323. /* Probe the device with the new settings. */
  324. ret = uvc_probe_video(stream, &probe);
  325. if (ret < 0) {
  326. mutex_unlock(&stream->mutex);
  327. return ret;
  328. }
  329. memcpy(&stream->ctrl, &probe, sizeof probe);
  330. mutex_unlock(&stream->mutex);
  331. /* Return the actual frame period. */
  332. timeperframe.numerator = probe.dwFrameInterval;
  333. timeperframe.denominator = 10000000;
  334. uvc_simplify_fraction(&timeperframe.numerator,
  335. &timeperframe.denominator, 8, 333);
  336. if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  337. parm->parm.capture.timeperframe = timeperframe;
  338. else
  339. parm->parm.output.timeperframe = timeperframe;
  340. return 0;
  341. }
  342. /* ------------------------------------------------------------------------
  343. * Privilege management
  344. */
  345. /*
  346. * Privilege management is the multiple-open implementation basis. The current
  347. * implementation is completely transparent for the end-user and doesn't
  348. * require explicit use of the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls.
  349. * Those ioctls enable finer control on the device (by making possible for a
  350. * user to request exclusive access to a device), but are not mature yet.
  351. * Switching to the V4L2 priority mechanism might be considered in the future
  352. * if this situation changes.
  353. *
  354. * Each open instance of a UVC device can either be in a privileged or
  355. * unprivileged state. Only a single instance can be in a privileged state at
  356. * a given time. Trying to perform an operation that requires privileges will
  357. * automatically acquire the required privileges if possible, or return -EBUSY
  358. * otherwise. Privileges are dismissed when closing the instance or when
  359. * freeing the video buffers using VIDIOC_REQBUFS.
  360. *
  361. * Operations that require privileges are:
  362. *
  363. * - VIDIOC_S_INPUT
  364. * - VIDIOC_S_PARM
  365. * - VIDIOC_S_FMT
  366. * - VIDIOC_REQBUFS
  367. */
  368. static int uvc_acquire_privileges(struct uvc_fh *handle)
  369. {
  370. /* Always succeed if the handle is already privileged. */
  371. if (handle->state == UVC_HANDLE_ACTIVE)
  372. return 0;
  373. /* Check if the device already has a privileged handle. */
  374. if (atomic_inc_return(&handle->stream->active) != 1) {
  375. atomic_dec(&handle->stream->active);
  376. return -EBUSY;
  377. }
  378. handle->state = UVC_HANDLE_ACTIVE;
  379. return 0;
  380. }
  381. static void uvc_dismiss_privileges(struct uvc_fh *handle)
  382. {
  383. if (handle->state == UVC_HANDLE_ACTIVE)
  384. atomic_dec(&handle->stream->active);
  385. handle->state = UVC_HANDLE_PASSIVE;
  386. }
  387. static int uvc_has_privileges(struct uvc_fh *handle)
  388. {
  389. return handle->state == UVC_HANDLE_ACTIVE;
  390. }
  391. /* ------------------------------------------------------------------------
  392. * V4L2 file operations
  393. */
  394. static int uvc_v4l2_open(struct file *file)
  395. {
  396. struct uvc_streaming *stream;
  397. struct uvc_fh *handle;
  398. int ret = 0;
  399. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n");
  400. stream = video_drvdata(file);
  401. if (stream->dev->state & UVC_DEV_DISCONNECTED)
  402. return -ENODEV;
  403. ret = usb_autopm_get_interface(stream->dev->intf);
  404. if (ret < 0)
  405. return ret;
  406. /* Create the device handle. */
  407. handle = kzalloc(sizeof *handle, GFP_KERNEL);
  408. if (handle == NULL) {
  409. usb_autopm_put_interface(stream->dev->intf);
  410. return -ENOMEM;
  411. }
  412. if (atomic_inc_return(&stream->dev->users) == 1) {
  413. ret = uvc_status_start(stream->dev);
  414. if (ret < 0) {
  415. usb_autopm_put_interface(stream->dev->intf);
  416. atomic_dec(&stream->dev->users);
  417. kfree(handle);
  418. return ret;
  419. }
  420. }
  421. handle->chain = stream->chain;
  422. handle->stream = stream;
  423. handle->state = UVC_HANDLE_PASSIVE;
  424. file->private_data = handle;
  425. return 0;
  426. }
  427. static int uvc_v4l2_release(struct file *file)
  428. {
  429. struct uvc_fh *handle = file->private_data;
  430. struct uvc_streaming *stream = handle->stream;
  431. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n");
  432. /* Only free resources if this is a privileged handle. */
  433. if (uvc_has_privileges(handle)) {
  434. uvc_video_enable(stream, 0);
  435. uvc_free_buffers(&stream->queue);
  436. }
  437. /* Release the file handle. */
  438. uvc_dismiss_privileges(handle);
  439. kfree(handle);
  440. file->private_data = NULL;
  441. if (atomic_dec_return(&stream->dev->users) == 0)
  442. uvc_status_stop(stream->dev);
  443. usb_autopm_put_interface(stream->dev->intf);
  444. return 0;
  445. }
  446. static long uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
  447. {
  448. struct video_device *vdev = video_devdata(file);
  449. struct uvc_fh *handle = file->private_data;
  450. struct uvc_video_chain *chain = handle->chain;
  451. struct uvc_streaming *stream = handle->stream;
  452. long ret = 0;
  453. switch (cmd) {
  454. /* Query capabilities */
  455. case VIDIOC_QUERYCAP:
  456. {
  457. struct v4l2_capability *cap = arg;
  458. memset(cap, 0, sizeof *cap);
  459. strlcpy(cap->driver, "uvcvideo", sizeof cap->driver);
  460. strlcpy(cap->card, vdev->name, sizeof cap->card);
  461. usb_make_path(stream->dev->udev,
  462. cap->bus_info, sizeof(cap->bus_info));
  463. cap->version = LINUX_VERSION_CODE;
  464. if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  465. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
  466. | V4L2_CAP_STREAMING;
  467. else
  468. cap->capabilities = V4L2_CAP_VIDEO_OUTPUT
  469. | V4L2_CAP_STREAMING;
  470. break;
  471. }
  472. /* Get, Set & Query control */
  473. case VIDIOC_QUERYCTRL:
  474. return uvc_query_v4l2_ctrl(chain, arg);
  475. case VIDIOC_G_CTRL:
  476. {
  477. struct v4l2_control *ctrl = arg;
  478. struct v4l2_ext_control xctrl;
  479. memset(&xctrl, 0, sizeof xctrl);
  480. xctrl.id = ctrl->id;
  481. ret = uvc_ctrl_begin(chain);
  482. if (ret < 0)
  483. return ret;
  484. ret = uvc_ctrl_get(chain, &xctrl);
  485. uvc_ctrl_rollback(chain);
  486. if (ret >= 0)
  487. ctrl->value = xctrl.value;
  488. break;
  489. }
  490. case VIDIOC_S_CTRL:
  491. {
  492. struct v4l2_control *ctrl = arg;
  493. struct v4l2_ext_control xctrl;
  494. memset(&xctrl, 0, sizeof xctrl);
  495. xctrl.id = ctrl->id;
  496. xctrl.value = ctrl->value;
  497. ret = uvc_ctrl_begin(chain);
  498. if (ret < 0)
  499. return ret;
  500. ret = uvc_ctrl_set(chain, &xctrl);
  501. if (ret < 0) {
  502. uvc_ctrl_rollback(chain);
  503. return ret;
  504. }
  505. ret = uvc_ctrl_commit(chain);
  506. if (ret == 0)
  507. ctrl->value = xctrl.value;
  508. break;
  509. }
  510. case VIDIOC_QUERYMENU:
  511. return uvc_query_v4l2_menu(chain, arg);
  512. case VIDIOC_G_EXT_CTRLS:
  513. {
  514. struct v4l2_ext_controls *ctrls = arg;
  515. struct v4l2_ext_control *ctrl = ctrls->controls;
  516. unsigned int i;
  517. ret = uvc_ctrl_begin(chain);
  518. if (ret < 0)
  519. return ret;
  520. for (i = 0; i < ctrls->count; ++ctrl, ++i) {
  521. ret = uvc_ctrl_get(chain, ctrl);
  522. if (ret < 0) {
  523. uvc_ctrl_rollback(chain);
  524. ctrls->error_idx = i;
  525. return ret;
  526. }
  527. }
  528. ctrls->error_idx = 0;
  529. ret = uvc_ctrl_rollback(chain);
  530. break;
  531. }
  532. case VIDIOC_S_EXT_CTRLS:
  533. case VIDIOC_TRY_EXT_CTRLS:
  534. {
  535. struct v4l2_ext_controls *ctrls = arg;
  536. struct v4l2_ext_control *ctrl = ctrls->controls;
  537. unsigned int i;
  538. ret = uvc_ctrl_begin(chain);
  539. if (ret < 0)
  540. return ret;
  541. for (i = 0; i < ctrls->count; ++ctrl, ++i) {
  542. ret = uvc_ctrl_set(chain, ctrl);
  543. if (ret < 0) {
  544. uvc_ctrl_rollback(chain);
  545. ctrls->error_idx = i;
  546. return ret;
  547. }
  548. }
  549. ctrls->error_idx = 0;
  550. if (cmd == VIDIOC_S_EXT_CTRLS)
  551. ret = uvc_ctrl_commit(chain);
  552. else
  553. ret = uvc_ctrl_rollback(chain);
  554. break;
  555. }
  556. /* Get, Set & Enum input */
  557. case VIDIOC_ENUMINPUT:
  558. {
  559. const struct uvc_entity *selector = chain->selector;
  560. struct v4l2_input *input = arg;
  561. struct uvc_entity *iterm = NULL;
  562. u32 index = input->index;
  563. int pin = 0;
  564. if (selector == NULL ||
  565. (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
  566. if (index != 0)
  567. return -EINVAL;
  568. list_for_each_entry(iterm, &chain->entities, chain) {
  569. if (UVC_ENTITY_IS_ITERM(iterm))
  570. break;
  571. }
  572. pin = iterm->id;
  573. } else if (pin < selector->bNrInPins) {
  574. pin = selector->baSourceID[index];
  575. list_for_each_entry(iterm, &chain->entities, chain) {
  576. if (!UVC_ENTITY_IS_ITERM(iterm))
  577. continue;
  578. if (iterm->id == pin)
  579. break;
  580. }
  581. }
  582. if (iterm == NULL || iterm->id != pin)
  583. return -EINVAL;
  584. memset(input, 0, sizeof *input);
  585. input->index = index;
  586. strlcpy(input->name, iterm->name, sizeof input->name);
  587. if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA)
  588. input->type = V4L2_INPUT_TYPE_CAMERA;
  589. break;
  590. }
  591. case VIDIOC_G_INPUT:
  592. {
  593. u8 input;
  594. if (chain->selector == NULL ||
  595. (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
  596. *(int *)arg = 0;
  597. break;
  598. }
  599. ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
  600. chain->selector->id, chain->dev->intfnum,
  601. UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
  602. if (ret < 0)
  603. return ret;
  604. *(int *)arg = input - 1;
  605. break;
  606. }
  607. case VIDIOC_S_INPUT:
  608. {
  609. u32 input = *(u32 *)arg + 1;
  610. if ((ret = uvc_acquire_privileges(handle)) < 0)
  611. return ret;
  612. if (chain->selector == NULL ||
  613. (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
  614. if (input != 1)
  615. return -EINVAL;
  616. break;
  617. }
  618. if (input == 0 || input > chain->selector->bNrInPins)
  619. return -EINVAL;
  620. return uvc_query_ctrl(chain->dev, UVC_SET_CUR,
  621. chain->selector->id, chain->dev->intfnum,
  622. UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
  623. }
  624. /* Try, Get, Set & Enum format */
  625. case VIDIOC_ENUM_FMT:
  626. {
  627. struct v4l2_fmtdesc *fmt = arg;
  628. struct uvc_format *format;
  629. enum v4l2_buf_type type = fmt->type;
  630. __u32 index = fmt->index;
  631. if (fmt->type != stream->type ||
  632. fmt->index >= stream->nformats)
  633. return -EINVAL;
  634. memset(fmt, 0, sizeof(*fmt));
  635. fmt->index = index;
  636. fmt->type = type;
  637. format = &stream->format[fmt->index];
  638. fmt->flags = 0;
  639. if (format->flags & UVC_FMT_FLAG_COMPRESSED)
  640. fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
  641. strlcpy(fmt->description, format->name,
  642. sizeof fmt->description);
  643. fmt->description[sizeof fmt->description - 1] = 0;
  644. fmt->pixelformat = format->fcc;
  645. break;
  646. }
  647. case VIDIOC_TRY_FMT:
  648. {
  649. struct uvc_streaming_control probe;
  650. return uvc_v4l2_try_format(stream, arg, &probe, NULL, NULL);
  651. }
  652. case VIDIOC_S_FMT:
  653. if ((ret = uvc_acquire_privileges(handle)) < 0)
  654. return ret;
  655. return uvc_v4l2_set_format(stream, arg);
  656. case VIDIOC_G_FMT:
  657. return uvc_v4l2_get_format(stream, arg);
  658. /* Frame size enumeration */
  659. case VIDIOC_ENUM_FRAMESIZES:
  660. {
  661. struct v4l2_frmsizeenum *fsize = arg;
  662. struct uvc_format *format = NULL;
  663. struct uvc_frame *frame;
  664. int i;
  665. /* Look for the given pixel format */
  666. for (i = 0; i < stream->nformats; i++) {
  667. if (stream->format[i].fcc ==
  668. fsize->pixel_format) {
  669. format = &stream->format[i];
  670. break;
  671. }
  672. }
  673. if (format == NULL)
  674. return -EINVAL;
  675. if (fsize->index >= format->nframes)
  676. return -EINVAL;
  677. frame = &format->frame[fsize->index];
  678. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  679. fsize->discrete.width = frame->wWidth;
  680. fsize->discrete.height = frame->wHeight;
  681. break;
  682. }
  683. /* Frame interval enumeration */
  684. case VIDIOC_ENUM_FRAMEINTERVALS:
  685. {
  686. struct v4l2_frmivalenum *fival = arg;
  687. struct uvc_format *format = NULL;
  688. struct uvc_frame *frame = NULL;
  689. int i;
  690. /* Look for the given pixel format and frame size */
  691. for (i = 0; i < stream->nformats; i++) {
  692. if (stream->format[i].fcc ==
  693. fival->pixel_format) {
  694. format = &stream->format[i];
  695. break;
  696. }
  697. }
  698. if (format == NULL)
  699. return -EINVAL;
  700. for (i = 0; i < format->nframes; i++) {
  701. if (format->frame[i].wWidth == fival->width &&
  702. format->frame[i].wHeight == fival->height) {
  703. frame = &format->frame[i];
  704. break;
  705. }
  706. }
  707. if (frame == NULL)
  708. return -EINVAL;
  709. if (frame->bFrameIntervalType) {
  710. if (fival->index >= frame->bFrameIntervalType)
  711. return -EINVAL;
  712. fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
  713. fival->discrete.numerator =
  714. frame->dwFrameInterval[fival->index];
  715. fival->discrete.denominator = 10000000;
  716. uvc_simplify_fraction(&fival->discrete.numerator,
  717. &fival->discrete.denominator, 8, 333);
  718. } else {
  719. fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
  720. fival->stepwise.min.numerator =
  721. frame->dwFrameInterval[0];
  722. fival->stepwise.min.denominator = 10000000;
  723. fival->stepwise.max.numerator =
  724. frame->dwFrameInterval[1];
  725. fival->stepwise.max.denominator = 10000000;
  726. fival->stepwise.step.numerator =
  727. frame->dwFrameInterval[2];
  728. fival->stepwise.step.denominator = 10000000;
  729. uvc_simplify_fraction(&fival->stepwise.min.numerator,
  730. &fival->stepwise.min.denominator, 8, 333);
  731. uvc_simplify_fraction(&fival->stepwise.max.numerator,
  732. &fival->stepwise.max.denominator, 8, 333);
  733. uvc_simplify_fraction(&fival->stepwise.step.numerator,
  734. &fival->stepwise.step.denominator, 8, 333);
  735. }
  736. break;
  737. }
  738. /* Get & Set streaming parameters */
  739. case VIDIOC_G_PARM:
  740. return uvc_v4l2_get_streamparm(stream, arg);
  741. case VIDIOC_S_PARM:
  742. if ((ret = uvc_acquire_privileges(handle)) < 0)
  743. return ret;
  744. return uvc_v4l2_set_streamparm(stream, arg);
  745. /* Cropping and scaling */
  746. case VIDIOC_CROPCAP:
  747. {
  748. struct v4l2_cropcap *ccap = arg;
  749. if (ccap->type != stream->type)
  750. return -EINVAL;
  751. ccap->bounds.left = 0;
  752. ccap->bounds.top = 0;
  753. mutex_lock(&stream->mutex);
  754. ccap->bounds.width = stream->cur_frame->wWidth;
  755. ccap->bounds.height = stream->cur_frame->wHeight;
  756. mutex_unlock(&stream->mutex);
  757. ccap->defrect = ccap->bounds;
  758. ccap->pixelaspect.numerator = 1;
  759. ccap->pixelaspect.denominator = 1;
  760. break;
  761. }
  762. case VIDIOC_G_CROP:
  763. case VIDIOC_S_CROP:
  764. return -EINVAL;
  765. /* Buffers & streaming */
  766. case VIDIOC_REQBUFS:
  767. if ((ret = uvc_acquire_privileges(handle)) < 0)
  768. return ret;
  769. mutex_lock(&stream->mutex);
  770. ret = uvc_alloc_buffers(&stream->queue, arg);
  771. mutex_unlock(&stream->mutex);
  772. if (ret < 0)
  773. return ret;
  774. if (ret == 0)
  775. uvc_dismiss_privileges(handle);
  776. ret = 0;
  777. break;
  778. case VIDIOC_QUERYBUF:
  779. {
  780. struct v4l2_buffer *buf = arg;
  781. if (!uvc_has_privileges(handle))
  782. return -EBUSY;
  783. return uvc_query_buffer(&stream->queue, buf);
  784. }
  785. case VIDIOC_QBUF:
  786. if (!uvc_has_privileges(handle))
  787. return -EBUSY;
  788. return uvc_queue_buffer(&stream->queue, arg);
  789. case VIDIOC_DQBUF:
  790. if (!uvc_has_privileges(handle))
  791. return -EBUSY;
  792. return uvc_dequeue_buffer(&stream->queue, arg,
  793. file->f_flags & O_NONBLOCK);
  794. case VIDIOC_STREAMON:
  795. {
  796. int *type = arg;
  797. if (*type != stream->type)
  798. return -EINVAL;
  799. if (!uvc_has_privileges(handle))
  800. return -EBUSY;
  801. mutex_lock(&stream->mutex);
  802. ret = uvc_video_enable(stream, 1);
  803. mutex_unlock(&stream->mutex);
  804. if (ret < 0)
  805. return ret;
  806. break;
  807. }
  808. case VIDIOC_STREAMOFF:
  809. {
  810. int *type = arg;
  811. if (*type != stream->type)
  812. return -EINVAL;
  813. if (!uvc_has_privileges(handle))
  814. return -EBUSY;
  815. return uvc_video_enable(stream, 0);
  816. }
  817. /* Analog video standards make no sense for digital cameras. */
  818. case VIDIOC_ENUMSTD:
  819. case VIDIOC_QUERYSTD:
  820. case VIDIOC_G_STD:
  821. case VIDIOC_S_STD:
  822. case VIDIOC_OVERLAY:
  823. case VIDIOC_ENUMAUDIO:
  824. case VIDIOC_ENUMAUDOUT:
  825. case VIDIOC_ENUMOUTPUT:
  826. uvc_trace(UVC_TRACE_IOCTL, "Unsupported ioctl 0x%08x\n", cmd);
  827. return -EINVAL;
  828. case UVCIOC_CTRL_MAP:
  829. return uvc_ioctl_ctrl_map(chain, arg);
  830. case UVCIOC_CTRL_QUERY:
  831. return uvc_xu_ctrl_query(chain, arg);
  832. default:
  833. uvc_trace(UVC_TRACE_IOCTL, "Unknown ioctl 0x%08x\n", cmd);
  834. return -ENOTTY;
  835. }
  836. return ret;
  837. }
  838. static long uvc_v4l2_ioctl(struct file *file,
  839. unsigned int cmd, unsigned long arg)
  840. {
  841. if (uvc_trace_param & UVC_TRACE_IOCTL) {
  842. uvc_printk(KERN_DEBUG, "uvc_v4l2_ioctl(");
  843. v4l_printk_ioctl(cmd);
  844. printk(")\n");
  845. }
  846. return video_usercopy(file, cmd, arg, uvc_v4l2_do_ioctl);
  847. }
  848. #ifdef CONFIG_COMPAT
  849. struct uvc_xu_control_mapping32 {
  850. __u32 id;
  851. __u8 name[32];
  852. __u8 entity[16];
  853. __u8 selector;
  854. __u8 size;
  855. __u8 offset;
  856. __u32 v4l2_type;
  857. __u32 data_type;
  858. compat_caddr_t menu_info;
  859. __u32 menu_count;
  860. __u32 reserved[4];
  861. };
  862. static int uvc_v4l2_get_xu_mapping(struct uvc_xu_control_mapping *kp,
  863. const struct uvc_xu_control_mapping32 __user *up)
  864. {
  865. struct uvc_menu_info __user *umenus;
  866. struct uvc_menu_info __user *kmenus;
  867. compat_caddr_t p;
  868. if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
  869. __copy_from_user(kp, up, offsetof(typeof(*up), menu_info)) ||
  870. __get_user(kp->menu_count, &up->menu_count))
  871. return -EFAULT;
  872. memset(kp->reserved, 0, sizeof(kp->reserved));
  873. if (kp->menu_count == 0) {
  874. kp->menu_info = NULL;
  875. return 0;
  876. }
  877. if (__get_user(p, &up->menu_info))
  878. return -EFAULT;
  879. umenus = compat_ptr(p);
  880. if (!access_ok(VERIFY_READ, umenus, kp->menu_count * sizeof(*umenus)))
  881. return -EFAULT;
  882. kmenus = compat_alloc_user_space(kp->menu_count * sizeof(*kmenus));
  883. if (kmenus == NULL)
  884. return -EFAULT;
  885. kp->menu_info = kmenus;
  886. if (copy_in_user(kmenus, umenus, kp->menu_count * sizeof(*umenus)))
  887. return -EFAULT;
  888. return 0;
  889. }
  890. static int uvc_v4l2_put_xu_mapping(const struct uvc_xu_control_mapping *kp,
  891. struct uvc_xu_control_mapping32 __user *up)
  892. {
  893. struct uvc_menu_info __user *umenus;
  894. struct uvc_menu_info __user *kmenus = kp->menu_info;
  895. compat_caddr_t p;
  896. if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
  897. __copy_to_user(up, kp, offsetof(typeof(*up), menu_info)) ||
  898. __put_user(kp->menu_count, &up->menu_count))
  899. return -EFAULT;
  900. if (__clear_user(up->reserved, sizeof(up->reserved)))
  901. return -EFAULT;
  902. if (kp->menu_count == 0)
  903. return 0;
  904. if (get_user(p, &up->menu_info))
  905. return -EFAULT;
  906. umenus = compat_ptr(p);
  907. if (copy_in_user(umenus, kmenus, kp->menu_count * sizeof(*umenus)))
  908. return -EFAULT;
  909. return 0;
  910. }
  911. struct uvc_xu_control_query32 {
  912. __u8 unit;
  913. __u8 selector;
  914. __u8 query;
  915. __u16 size;
  916. compat_caddr_t data;
  917. };
  918. static int uvc_v4l2_get_xu_query(struct uvc_xu_control_query *kp,
  919. const struct uvc_xu_control_query32 __user *up)
  920. {
  921. u8 __user *udata;
  922. u8 __user *kdata;
  923. compat_caddr_t p;
  924. if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
  925. __copy_from_user(kp, up, offsetof(typeof(*up), data)))
  926. return -EFAULT;
  927. if (kp->size == 0) {
  928. kp->data = NULL;
  929. return 0;
  930. }
  931. if (__get_user(p, &up->data))
  932. return -EFAULT;
  933. udata = compat_ptr(p);
  934. if (!access_ok(VERIFY_READ, udata, kp->size))
  935. return -EFAULT;
  936. kdata = compat_alloc_user_space(kp->size);
  937. if (kdata == NULL)
  938. return -EFAULT;
  939. kp->data = kdata;
  940. if (copy_in_user(kdata, udata, kp->size))
  941. return -EFAULT;
  942. return 0;
  943. }
  944. static int uvc_v4l2_put_xu_query(const struct uvc_xu_control_query *kp,
  945. struct uvc_xu_control_query32 __user *up)
  946. {
  947. u8 __user *udata;
  948. u8 __user *kdata = kp->data;
  949. compat_caddr_t p;
  950. if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
  951. __copy_to_user(up, kp, offsetof(typeof(*up), data)))
  952. return -EFAULT;
  953. if (kp->size == 0)
  954. return 0;
  955. if (get_user(p, &up->data))
  956. return -EFAULT;
  957. udata = compat_ptr(p);
  958. if (!access_ok(VERIFY_READ, udata, kp->size))
  959. return -EFAULT;
  960. if (copy_in_user(udata, kdata, kp->size))
  961. return -EFAULT;
  962. return 0;
  963. }
  964. #define UVCIOC_CTRL_MAP32 _IOWR('u', 0x20, struct uvc_xu_control_mapping32)
  965. #define UVCIOC_CTRL_QUERY32 _IOWR('u', 0x21, struct uvc_xu_control_query32)
  966. static long uvc_v4l2_compat_ioctl32(struct file *file,
  967. unsigned int cmd, unsigned long arg)
  968. {
  969. union {
  970. struct uvc_xu_control_mapping xmap;
  971. struct uvc_xu_control_query xqry;
  972. } karg;
  973. void __user *up = compat_ptr(arg);
  974. mm_segment_t old_fs;
  975. long ret;
  976. switch (cmd) {
  977. case UVCIOC_CTRL_MAP32:
  978. cmd = UVCIOC_CTRL_MAP;
  979. ret = uvc_v4l2_get_xu_mapping(&karg.xmap, up);
  980. break;
  981. case UVCIOC_CTRL_QUERY32:
  982. cmd = UVCIOC_CTRL_QUERY;
  983. ret = uvc_v4l2_get_xu_query(&karg.xqry, up);
  984. break;
  985. default:
  986. return -ENOIOCTLCMD;
  987. }
  988. old_fs = get_fs();
  989. set_fs(KERNEL_DS);
  990. ret = uvc_v4l2_ioctl(file, cmd, (unsigned long)&karg);
  991. set_fs(old_fs);
  992. if (ret < 0)
  993. return ret;
  994. switch (cmd) {
  995. case UVCIOC_CTRL_MAP:
  996. ret = uvc_v4l2_put_xu_mapping(&karg.xmap, up);
  997. break;
  998. case UVCIOC_CTRL_QUERY:
  999. ret = uvc_v4l2_put_xu_query(&karg.xqry, up);
  1000. break;
  1001. }
  1002. return ret;
  1003. }
  1004. #endif
  1005. static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
  1006. size_t count, loff_t *ppos)
  1007. {
  1008. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
  1009. return -EINVAL;
  1010. }
  1011. static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
  1012. {
  1013. struct uvc_fh *handle = file->private_data;
  1014. struct uvc_streaming *stream = handle->stream;
  1015. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
  1016. return uvc_queue_mmap(&stream->queue, vma);
  1017. }
  1018. static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait)
  1019. {
  1020. struct uvc_fh *handle = file->private_data;
  1021. struct uvc_streaming *stream = handle->stream;
  1022. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
  1023. return uvc_queue_poll(&stream->queue, file, wait);
  1024. }
  1025. #ifndef CONFIG_MMU
  1026. static unsigned long uvc_v4l2_get_unmapped_area(struct file *file,
  1027. unsigned long addr, unsigned long len, unsigned long pgoff,
  1028. unsigned long flags)
  1029. {
  1030. struct uvc_fh *handle = file->private_data;
  1031. struct uvc_streaming *stream = handle->stream;
  1032. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_get_unmapped_area\n");
  1033. return uvc_queue_get_unmapped_area(&stream->queue, pgoff);
  1034. }
  1035. #endif
  1036. const struct v4l2_file_operations uvc_fops = {
  1037. .owner = THIS_MODULE,
  1038. .open = uvc_v4l2_open,
  1039. .release = uvc_v4l2_release,
  1040. .unlocked_ioctl = uvc_v4l2_ioctl,
  1041. #ifdef CONFIG_COMPAT
  1042. .compat_ioctl32 = uvc_v4l2_compat_ioctl32,
  1043. #endif
  1044. .read = uvc_v4l2_read,
  1045. .mmap = uvc_v4l2_mmap,
  1046. .poll = uvc_v4l2_poll,
  1047. #ifndef CONFIG_MMU
  1048. .get_unmapped_area = uvc_v4l2_get_unmapped_area,
  1049. #endif
  1050. };