uvc_v4l2.c 28 KB

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