uvc_v4l2.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  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 uvc_video_device *video;
  334. struct uvc_fh *handle;
  335. int ret = 0;
  336. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n");
  337. mutex_lock(&uvc_driver.open_mutex);
  338. video = video_drvdata(file);
  339. if (video->dev->state & UVC_DEV_DISCONNECTED) {
  340. ret = -ENODEV;
  341. goto done;
  342. }
  343. ret = usb_autopm_get_interface(video->dev->intf);
  344. if (ret < 0)
  345. goto done;
  346. /* Create the device handle. */
  347. handle = kzalloc(sizeof *handle, GFP_KERNEL);
  348. if (handle == NULL) {
  349. usb_autopm_put_interface(video->dev->intf);
  350. ret = -ENOMEM;
  351. goto done;
  352. }
  353. handle->device = video;
  354. handle->state = UVC_HANDLE_PASSIVE;
  355. file->private_data = handle;
  356. kref_get(&video->dev->kref);
  357. done:
  358. mutex_unlock(&uvc_driver.open_mutex);
  359. return ret;
  360. }
  361. static int uvc_v4l2_release(struct inode *inode, struct file *file)
  362. {
  363. struct uvc_video_device *video = video_drvdata(file);
  364. struct uvc_fh *handle = (struct uvc_fh *)file->private_data;
  365. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n");
  366. /* Only free resources if this is a privileged handle. */
  367. if (uvc_has_privileges(handle)) {
  368. uvc_video_enable(video, 0);
  369. mutex_lock(&video->queue.mutex);
  370. if (uvc_free_buffers(&video->queue) < 0)
  371. uvc_printk(KERN_ERR, "uvc_v4l2_release: Unable to "
  372. "free buffers.\n");
  373. mutex_unlock(&video->queue.mutex);
  374. }
  375. /* Release the file handle. */
  376. uvc_dismiss_privileges(handle);
  377. kfree(handle);
  378. file->private_data = NULL;
  379. usb_autopm_put_interface(video->dev->intf);
  380. kref_put(&video->dev->kref, uvc_delete);
  381. return 0;
  382. }
  383. static int uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
  384. {
  385. struct video_device *vdev = video_devdata(file);
  386. struct uvc_video_device *video = video_get_drvdata(vdev);
  387. struct uvc_fh *handle = (struct uvc_fh *)file->private_data;
  388. int ret = 0;
  389. if (uvc_trace_param & UVC_TRACE_IOCTL)
  390. v4l_printk_ioctl(cmd);
  391. switch (cmd) {
  392. /* Query capabilities */
  393. case VIDIOC_QUERYCAP:
  394. {
  395. struct v4l2_capability *cap = arg;
  396. memset(cap, 0, sizeof *cap);
  397. strncpy(cap->driver, "uvcvideo", sizeof cap->driver);
  398. strncpy(cap->card, vdev->name, 32);
  399. strncpy(cap->bus_info, video->dev->udev->bus->bus_name,
  400. sizeof cap->bus_info);
  401. cap->version = DRIVER_VERSION_NUMBER;
  402. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
  403. | V4L2_CAP_STREAMING;
  404. break;
  405. }
  406. /* Get, Set & Query control */
  407. case VIDIOC_QUERYCTRL:
  408. return uvc_query_v4l2_ctrl(video, arg);
  409. case VIDIOC_G_CTRL:
  410. {
  411. struct v4l2_control *ctrl = arg;
  412. struct v4l2_ext_control xctrl;
  413. memset(&xctrl, 0, sizeof xctrl);
  414. xctrl.id = ctrl->id;
  415. uvc_ctrl_begin(video);
  416. ret = uvc_ctrl_get(video, &xctrl);
  417. uvc_ctrl_rollback(video);
  418. if (ret >= 0)
  419. ctrl->value = xctrl.value;
  420. break;
  421. }
  422. case VIDIOC_S_CTRL:
  423. {
  424. struct v4l2_control *ctrl = arg;
  425. struct v4l2_ext_control xctrl;
  426. memset(&xctrl, 0, sizeof xctrl);
  427. xctrl.id = ctrl->id;
  428. xctrl.value = ctrl->value;
  429. uvc_ctrl_begin(video);
  430. ret = uvc_ctrl_set(video, &xctrl);
  431. if (ret < 0) {
  432. uvc_ctrl_rollback(video);
  433. return ret;
  434. }
  435. ret = uvc_ctrl_commit(video);
  436. break;
  437. }
  438. case VIDIOC_QUERYMENU:
  439. return uvc_v4l2_query_menu(video, arg);
  440. case VIDIOC_G_EXT_CTRLS:
  441. {
  442. struct v4l2_ext_controls *ctrls = arg;
  443. struct v4l2_ext_control *ctrl = ctrls->controls;
  444. unsigned int i;
  445. uvc_ctrl_begin(video);
  446. for (i = 0; i < ctrls->count; ++ctrl, ++i) {
  447. ret = uvc_ctrl_get(video, ctrl);
  448. if (ret < 0) {
  449. uvc_ctrl_rollback(video);
  450. ctrls->error_idx = i;
  451. return ret;
  452. }
  453. }
  454. ctrls->error_idx = 0;
  455. ret = uvc_ctrl_rollback(video);
  456. break;
  457. }
  458. case VIDIOC_S_EXT_CTRLS:
  459. case VIDIOC_TRY_EXT_CTRLS:
  460. {
  461. struct v4l2_ext_controls *ctrls = arg;
  462. struct v4l2_ext_control *ctrl = ctrls->controls;
  463. unsigned int i;
  464. ret = uvc_ctrl_begin(video);
  465. if (ret < 0)
  466. return ret;
  467. for (i = 0; i < ctrls->count; ++ctrl, ++i) {
  468. ret = uvc_ctrl_set(video, ctrl);
  469. if (ret < 0) {
  470. uvc_ctrl_rollback(video);
  471. ctrls->error_idx = i;
  472. return ret;
  473. }
  474. }
  475. ctrls->error_idx = 0;
  476. if (cmd == VIDIOC_S_EXT_CTRLS)
  477. ret = uvc_ctrl_commit(video);
  478. else
  479. ret = uvc_ctrl_rollback(video);
  480. break;
  481. }
  482. /* Get, Set & Enum input */
  483. case VIDIOC_ENUMINPUT:
  484. {
  485. const struct uvc_entity *selector = video->selector;
  486. struct v4l2_input *input = arg;
  487. struct uvc_entity *iterm = NULL;
  488. u32 index = input->index;
  489. int pin = 0;
  490. if (selector == NULL ||
  491. (video->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
  492. if (index != 0)
  493. return -EINVAL;
  494. iterm = list_first_entry(&video->iterms,
  495. struct uvc_entity, chain);
  496. pin = iterm->id;
  497. } else if (pin < selector->selector.bNrInPins) {
  498. pin = selector->selector.baSourceID[index];
  499. list_for_each_entry(iterm, video->iterms.next, chain) {
  500. if (iterm->id == pin)
  501. break;
  502. }
  503. }
  504. if (iterm == NULL || iterm->id != pin)
  505. return -EINVAL;
  506. memset(input, 0, sizeof *input);
  507. input->index = index;
  508. strncpy(input->name, iterm->name, sizeof input->name);
  509. if (UVC_ENTITY_TYPE(iterm) == ITT_CAMERA)
  510. input->type = V4L2_INPUT_TYPE_CAMERA;
  511. break;
  512. }
  513. case VIDIOC_G_INPUT:
  514. {
  515. u8 input;
  516. if (video->selector == NULL ||
  517. (video->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
  518. *(int *)arg = 0;
  519. break;
  520. }
  521. ret = uvc_query_ctrl(video->dev, GET_CUR, video->selector->id,
  522. video->dev->intfnum, SU_INPUT_SELECT_CONTROL,
  523. &input, 1);
  524. if (ret < 0)
  525. return ret;
  526. *(int *)arg = input - 1;
  527. break;
  528. }
  529. case VIDIOC_S_INPUT:
  530. {
  531. u8 input = *(u32 *)arg + 1;
  532. if ((ret = uvc_acquire_privileges(handle)) < 0)
  533. return ret;
  534. if (video->selector == NULL ||
  535. (video->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
  536. if (input != 1)
  537. return -EINVAL;
  538. break;
  539. }
  540. if (input > video->selector->selector.bNrInPins)
  541. return -EINVAL;
  542. return uvc_query_ctrl(video->dev, SET_CUR, video->selector->id,
  543. video->dev->intfnum, SU_INPUT_SELECT_CONTROL,
  544. &input, 1);
  545. }
  546. /* Try, Get, Set & Enum format */
  547. case VIDIOC_ENUM_FMT:
  548. {
  549. struct v4l2_fmtdesc *fmt = arg;
  550. struct uvc_format *format;
  551. if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  552. fmt->index >= video->streaming->nformats)
  553. return -EINVAL;
  554. format = &video->streaming->format[fmt->index];
  555. fmt->flags = 0;
  556. if (format->flags & UVC_FMT_FLAG_COMPRESSED)
  557. fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
  558. strncpy(fmt->description, format->name,
  559. sizeof fmt->description);
  560. fmt->description[sizeof fmt->description - 1] = 0;
  561. fmt->pixelformat = format->fcc;
  562. break;
  563. }
  564. case VIDIOC_TRY_FMT:
  565. {
  566. struct uvc_streaming_control probe;
  567. if ((ret = uvc_acquire_privileges(handle)) < 0)
  568. return ret;
  569. return uvc_v4l2_try_format(video, arg, &probe, NULL, NULL);
  570. }
  571. case VIDIOC_S_FMT:
  572. if ((ret = uvc_acquire_privileges(handle)) < 0)
  573. return ret;
  574. return uvc_v4l2_set_format(video, arg);
  575. case VIDIOC_G_FMT:
  576. return uvc_v4l2_get_format(video, arg);
  577. /* Frame size enumeration */
  578. case VIDIOC_ENUM_FRAMESIZES:
  579. {
  580. struct v4l2_frmsizeenum *fsize = arg;
  581. struct uvc_format *format = NULL;
  582. struct uvc_frame *frame;
  583. int i;
  584. /* Look for the given pixel format */
  585. for (i = 0; i < video->streaming->nformats; i++) {
  586. if (video->streaming->format[i].fcc ==
  587. fsize->pixel_format) {
  588. format = &video->streaming->format[i];
  589. break;
  590. }
  591. }
  592. if (format == NULL)
  593. return -EINVAL;
  594. if (fsize->index >= format->nframes)
  595. return -EINVAL;
  596. frame = &format->frame[fsize->index];
  597. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  598. fsize->discrete.width = frame->wWidth;
  599. fsize->discrete.height = frame->wHeight;
  600. break;
  601. }
  602. /* Frame interval enumeration */
  603. case VIDIOC_ENUM_FRAMEINTERVALS:
  604. {
  605. struct v4l2_frmivalenum *fival = arg;
  606. struct uvc_format *format = NULL;
  607. struct uvc_frame *frame = NULL;
  608. int i;
  609. /* Look for the given pixel format and frame size */
  610. for (i = 0; i < video->streaming->nformats; i++) {
  611. if (video->streaming->format[i].fcc ==
  612. fival->pixel_format) {
  613. format = &video->streaming->format[i];
  614. break;
  615. }
  616. }
  617. if (format == NULL)
  618. return -EINVAL;
  619. for (i = 0; i < format->nframes; i++) {
  620. if (format->frame[i].wWidth == fival->width &&
  621. format->frame[i].wHeight == fival->height) {
  622. frame = &format->frame[i];
  623. break;
  624. }
  625. }
  626. if (frame == NULL)
  627. return -EINVAL;
  628. if (frame->bFrameIntervalType) {
  629. if (fival->index >= frame->bFrameIntervalType)
  630. return -EINVAL;
  631. fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
  632. fival->discrete.numerator =
  633. frame->dwFrameInterval[fival->index];
  634. fival->discrete.denominator = 10000000;
  635. uvc_simplify_fraction(&fival->discrete.numerator,
  636. &fival->discrete.denominator, 8, 333);
  637. } else {
  638. fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
  639. fival->stepwise.min.numerator =
  640. frame->dwFrameInterval[0];
  641. fival->stepwise.min.denominator = 10000000;
  642. fival->stepwise.max.numerator =
  643. frame->dwFrameInterval[1];
  644. fival->stepwise.max.denominator = 10000000;
  645. fival->stepwise.step.numerator =
  646. frame->dwFrameInterval[2];
  647. fival->stepwise.step.denominator = 10000000;
  648. uvc_simplify_fraction(&fival->stepwise.min.numerator,
  649. &fival->stepwise.min.denominator, 8, 333);
  650. uvc_simplify_fraction(&fival->stepwise.max.numerator,
  651. &fival->stepwise.max.denominator, 8, 333);
  652. uvc_simplify_fraction(&fival->stepwise.step.numerator,
  653. &fival->stepwise.step.denominator, 8, 333);
  654. }
  655. break;
  656. }
  657. /* Get & Set streaming parameters */
  658. case VIDIOC_G_PARM:
  659. return uvc_v4l2_get_streamparm(video, arg);
  660. case VIDIOC_S_PARM:
  661. if ((ret = uvc_acquire_privileges(handle)) < 0)
  662. return ret;
  663. return uvc_v4l2_set_streamparm(video, arg);
  664. /* Cropping and scaling */
  665. case VIDIOC_CROPCAP:
  666. {
  667. struct v4l2_cropcap *ccap = arg;
  668. struct uvc_frame *frame = video->streaming->cur_frame;
  669. if (ccap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  670. return -EINVAL;
  671. ccap->bounds.left = 0;
  672. ccap->bounds.top = 0;
  673. ccap->bounds.width = frame->wWidth;
  674. ccap->bounds.height = frame->wHeight;
  675. ccap->defrect = ccap->bounds;
  676. ccap->pixelaspect.numerator = 1;
  677. ccap->pixelaspect.denominator = 1;
  678. break;
  679. }
  680. case VIDIOC_G_CROP:
  681. case VIDIOC_S_CROP:
  682. return -EINVAL;
  683. /* Buffers & streaming */
  684. case VIDIOC_REQBUFS:
  685. {
  686. struct v4l2_requestbuffers *rb = arg;
  687. unsigned int bufsize =
  688. video->streaming->ctrl.dwMaxVideoFrameSize;
  689. if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  690. rb->memory != V4L2_MEMORY_MMAP)
  691. return -EINVAL;
  692. if ((ret = uvc_acquire_privileges(handle)) < 0)
  693. return ret;
  694. ret = uvc_alloc_buffers(&video->queue, rb->count, bufsize);
  695. if (ret < 0)
  696. return ret;
  697. rb->count = ret;
  698. ret = 0;
  699. break;
  700. }
  701. case VIDIOC_QUERYBUF:
  702. {
  703. struct v4l2_buffer *buf = arg;
  704. if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  705. return -EINVAL;
  706. if (!uvc_has_privileges(handle))
  707. return -EBUSY;
  708. return uvc_query_buffer(&video->queue, buf);
  709. }
  710. case VIDIOC_QBUF:
  711. if (!uvc_has_privileges(handle))
  712. return -EBUSY;
  713. return uvc_queue_buffer(&video->queue, arg);
  714. case VIDIOC_DQBUF:
  715. if (!uvc_has_privileges(handle))
  716. return -EBUSY;
  717. return uvc_dequeue_buffer(&video->queue, arg,
  718. file->f_flags & O_NONBLOCK);
  719. case VIDIOC_STREAMON:
  720. {
  721. int *type = arg;
  722. if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  723. return -EINVAL;
  724. if (!uvc_has_privileges(handle))
  725. return -EBUSY;
  726. if ((ret = uvc_video_enable(video, 1)) < 0)
  727. return ret;
  728. break;
  729. }
  730. case VIDIOC_STREAMOFF:
  731. {
  732. int *type = arg;
  733. if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  734. return -EINVAL;
  735. if (!uvc_has_privileges(handle))
  736. return -EBUSY;
  737. return uvc_video_enable(video, 0);
  738. }
  739. /* Analog video standards make no sense for digital cameras. */
  740. case VIDIOC_ENUMSTD:
  741. case VIDIOC_QUERYSTD:
  742. case VIDIOC_G_STD:
  743. case VIDIOC_S_STD:
  744. case VIDIOC_OVERLAY:
  745. case VIDIOC_ENUMAUDIO:
  746. case VIDIOC_ENUMAUDOUT:
  747. case VIDIOC_ENUMOUTPUT:
  748. uvc_trace(UVC_TRACE_IOCTL, "Unsupported ioctl 0x%08x\n", cmd);
  749. return -EINVAL;
  750. /* Dynamic controls. */
  751. case UVCIOC_CTRL_ADD:
  752. {
  753. struct uvc_xu_control_info *xinfo = arg;
  754. struct uvc_control_info *info;
  755. if (!capable(CAP_SYS_ADMIN))
  756. return -EPERM;
  757. info = kmalloc(sizeof *info, GFP_KERNEL);
  758. if (info == NULL)
  759. return -ENOMEM;
  760. memcpy(info->entity, xinfo->entity, sizeof info->entity);
  761. info->index = xinfo->index;
  762. info->selector = xinfo->selector;
  763. info->size = xinfo->size;
  764. info->flags = xinfo->flags;
  765. info->flags |= UVC_CONTROL_GET_MIN | UVC_CONTROL_GET_MAX |
  766. UVC_CONTROL_GET_RES | UVC_CONTROL_GET_DEF;
  767. ret = uvc_ctrl_add_info(info);
  768. if (ret < 0)
  769. kfree(info);
  770. break;
  771. }
  772. case UVCIOC_CTRL_MAP:
  773. {
  774. struct uvc_xu_control_mapping *xmap = arg;
  775. struct uvc_control_mapping *map;
  776. if (!capable(CAP_SYS_ADMIN))
  777. return -EPERM;
  778. map = kmalloc(sizeof *map, GFP_KERNEL);
  779. if (map == NULL)
  780. return -ENOMEM;
  781. map->id = xmap->id;
  782. memcpy(map->name, xmap->name, sizeof map->name);
  783. memcpy(map->entity, xmap->entity, sizeof map->entity);
  784. map->selector = xmap->selector;
  785. map->size = xmap->size;
  786. map->offset = xmap->offset;
  787. map->v4l2_type = xmap->v4l2_type;
  788. map->data_type = xmap->data_type;
  789. ret = uvc_ctrl_add_mapping(map);
  790. if (ret < 0)
  791. kfree(map);
  792. break;
  793. }
  794. case UVCIOC_CTRL_GET:
  795. return uvc_xu_ctrl_query(video, arg, 0);
  796. case UVCIOC_CTRL_SET:
  797. return uvc_xu_ctrl_query(video, arg, 1);
  798. default:
  799. if ((ret = v4l_compat_translate_ioctl(file, cmd, arg,
  800. uvc_v4l2_do_ioctl)) == -ENOIOCTLCMD)
  801. uvc_trace(UVC_TRACE_IOCTL, "Unknown ioctl 0x%08x\n",
  802. cmd);
  803. return ret;
  804. }
  805. return ret;
  806. }
  807. static int uvc_v4l2_ioctl(struct inode *inode, struct file *file,
  808. unsigned int cmd, unsigned long arg)
  809. {
  810. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_ioctl\n");
  811. return video_usercopy(file, cmd, arg, uvc_v4l2_do_ioctl);
  812. }
  813. static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
  814. size_t count, loff_t *ppos)
  815. {
  816. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
  817. return -ENODEV;
  818. }
  819. /*
  820. * VMA operations.
  821. */
  822. static void uvc_vm_open(struct vm_area_struct *vma)
  823. {
  824. struct uvc_buffer *buffer = vma->vm_private_data;
  825. buffer->vma_use_count++;
  826. }
  827. static void uvc_vm_close(struct vm_area_struct *vma)
  828. {
  829. struct uvc_buffer *buffer = vma->vm_private_data;
  830. buffer->vma_use_count--;
  831. }
  832. static struct vm_operations_struct uvc_vm_ops = {
  833. .open = uvc_vm_open,
  834. .close = uvc_vm_close,
  835. };
  836. static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
  837. {
  838. struct uvc_video_device *video = video_drvdata(file);
  839. struct uvc_buffer *uninitialized_var(buffer);
  840. struct page *page;
  841. unsigned long addr, start, size;
  842. unsigned int i;
  843. int ret = 0;
  844. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
  845. start = vma->vm_start;
  846. size = vma->vm_end - vma->vm_start;
  847. mutex_lock(&video->queue.mutex);
  848. for (i = 0; i < video->queue.count; ++i) {
  849. buffer = &video->queue.buffer[i];
  850. if ((buffer->buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
  851. break;
  852. }
  853. if (i == video->queue.count || size != video->queue.buf_size) {
  854. ret = -EINVAL;
  855. goto done;
  856. }
  857. /*
  858. * VM_IO marks the area as being an mmaped region for I/O to a
  859. * device. It also prevents the region from being core dumped.
  860. */
  861. vma->vm_flags |= VM_IO;
  862. addr = (unsigned long)video->queue.mem + buffer->buf.m.offset;
  863. while (size > 0) {
  864. page = vmalloc_to_page((void *)addr);
  865. if ((ret = vm_insert_page(vma, start, page)) < 0)
  866. goto done;
  867. start += PAGE_SIZE;
  868. addr += PAGE_SIZE;
  869. size -= PAGE_SIZE;
  870. }
  871. vma->vm_ops = &uvc_vm_ops;
  872. vma->vm_private_data = buffer;
  873. uvc_vm_open(vma);
  874. done:
  875. mutex_unlock(&video->queue.mutex);
  876. return ret;
  877. }
  878. static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait)
  879. {
  880. struct uvc_video_device *video = video_drvdata(file);
  881. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
  882. return uvc_queue_poll(&video->queue, file, wait);
  883. }
  884. struct file_operations uvc_fops = {
  885. .owner = THIS_MODULE,
  886. .open = uvc_v4l2_open,
  887. .release = uvc_v4l2_release,
  888. .ioctl = uvc_v4l2_ioctl,
  889. .compat_ioctl = v4l_compat_ioctl32,
  890. .llseek = no_llseek,
  891. .read = uvc_v4l2_read,
  892. .mmap = uvc_v4l2_mmap,
  893. .poll = uvc_v4l2_poll,
  894. };