uvc_v4l2.c 26 KB

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