Advertisement
Guest User

Untitled

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