uvc_v4l2.c 27 KB

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