uvc_v4l2.c 28 KB

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