uvc_v4l2.c 28 KB

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