uvc_v4l2.c 28 KB

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