uvc_v4l2.c 28 KB

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