uvc_v4l2.c 28 KB

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