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