uvc_v4l2.c 26 KB

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