uvc_v4l2.c 26 KB

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