uvc_v4l2.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292
  1. /*
  2. * uvc_v4l2.c -- USB Video Class driver - V4L2 API
  3. *
  4. * Copyright (C) 2005-2010
  5. * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
  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/compat.h>
  14. #include <linux/kernel.h>
  15. #include <linux/version.h>
  16. #include <linux/list.h>
  17. #include <linux/module.h>
  18. #include <linux/slab.h>
  19. #include <linux/usb.h>
  20. #include <linux/videodev2.h>
  21. #include <linux/vmalloc.h>
  22. #include <linux/mm.h>
  23. #include <linux/wait.h>
  24. #include <linux/atomic.h>
  25. #include <media/v4l2-common.h>
  26. #include <media/v4l2-ioctl.h>
  27. #include "uvcvideo.h"
  28. /* ------------------------------------------------------------------------
  29. * UVC ioctls
  30. */
  31. static int uvc_ioctl_ctrl_map(struct uvc_video_chain *chain,
  32. struct uvc_xu_control_mapping *xmap)
  33. {
  34. struct uvc_control_mapping *map;
  35. unsigned int size;
  36. int ret;
  37. map = kzalloc(sizeof *map, GFP_KERNEL);
  38. if (map == NULL)
  39. return -ENOMEM;
  40. map->id = xmap->id;
  41. memcpy(map->name, xmap->name, sizeof map->name);
  42. memcpy(map->entity, xmap->entity, sizeof map->entity);
  43. map->selector = xmap->selector;
  44. map->size = xmap->size;
  45. map->offset = xmap->offset;
  46. map->v4l2_type = xmap->v4l2_type;
  47. map->data_type = xmap->data_type;
  48. switch (xmap->v4l2_type) {
  49. case V4L2_CTRL_TYPE_INTEGER:
  50. case V4L2_CTRL_TYPE_BOOLEAN:
  51. case V4L2_CTRL_TYPE_BUTTON:
  52. break;
  53. case V4L2_CTRL_TYPE_MENU:
  54. /* Prevent excessive memory consumption, as well as integer
  55. * overflows.
  56. */
  57. if (xmap->menu_count == 0 ||
  58. xmap->menu_count > UVC_MAX_CONTROL_MENU_ENTRIES) {
  59. ret = -EINVAL;
  60. goto done;
  61. }
  62. size = xmap->menu_count * sizeof(*map->menu_info);
  63. map->menu_info = kmalloc(size, GFP_KERNEL);
  64. if (map->menu_info == NULL) {
  65. ret = -ENOMEM;
  66. goto done;
  67. }
  68. if (copy_from_user(map->menu_info, xmap->menu_info, size)) {
  69. ret = -EFAULT;
  70. goto done;
  71. }
  72. map->menu_count = xmap->menu_count;
  73. break;
  74. default:
  75. uvc_trace(UVC_TRACE_CONTROL, "Unsupported V4L2 control type "
  76. "%u.\n", xmap->v4l2_type);
  77. ret = -ENOTTY;
  78. goto done;
  79. }
  80. ret = uvc_ctrl_add_mapping(chain, map);
  81. done:
  82. kfree(map->menu_info);
  83. kfree(map);
  84. return ret;
  85. }
  86. /* ------------------------------------------------------------------------
  87. * V4L2 interface
  88. */
  89. /*
  90. * Find the frame interval closest to the requested frame interval for the
  91. * given frame format and size. This should be done by the device as part of
  92. * the Video Probe and Commit negotiation, but some hardware don't implement
  93. * that feature.
  94. */
  95. static __u32 uvc_try_frame_interval(struct uvc_frame *frame, __u32 interval)
  96. {
  97. unsigned int i;
  98. if (frame->bFrameIntervalType) {
  99. __u32 best = -1, dist;
  100. for (i = 0; i < frame->bFrameIntervalType; ++i) {
  101. dist = interval > frame->dwFrameInterval[i]
  102. ? interval - frame->dwFrameInterval[i]
  103. : frame->dwFrameInterval[i] - interval;
  104. if (dist > best)
  105. break;
  106. best = dist;
  107. }
  108. interval = frame->dwFrameInterval[i-1];
  109. } else {
  110. const __u32 min = frame->dwFrameInterval[0];
  111. const __u32 max = frame->dwFrameInterval[1];
  112. const __u32 step = frame->dwFrameInterval[2];
  113. interval = min + (interval - min + step/2) / step * step;
  114. if (interval > max)
  115. interval = max;
  116. }
  117. return interval;
  118. }
  119. static int uvc_v4l2_try_format(struct uvc_streaming *stream,
  120. struct v4l2_format *fmt, struct uvc_streaming_control *probe,
  121. struct uvc_format **uvc_format, struct uvc_frame **uvc_frame)
  122. {
  123. struct uvc_format *format = NULL;
  124. struct uvc_frame *frame = NULL;
  125. __u16 rw, rh;
  126. unsigned int d, maxd;
  127. unsigned int i;
  128. __u32 interval;
  129. int ret = 0;
  130. __u8 *fcc;
  131. if (fmt->type != stream->type)
  132. return -EINVAL;
  133. fcc = (__u8 *)&fmt->fmt.pix.pixelformat;
  134. uvc_trace(UVC_TRACE_FORMAT, "Trying format 0x%08x (%c%c%c%c): %ux%u.\n",
  135. fmt->fmt.pix.pixelformat,
  136. fcc[0], fcc[1], fcc[2], fcc[3],
  137. fmt->fmt.pix.width, fmt->fmt.pix.height);
  138. /* Check if the hardware supports the requested format. */
  139. for (i = 0; i < stream->nformats; ++i) {
  140. format = &stream->format[i];
  141. if (format->fcc == fmt->fmt.pix.pixelformat)
  142. break;
  143. }
  144. if (format == NULL || format->fcc != fmt->fmt.pix.pixelformat) {
  145. uvc_trace(UVC_TRACE_FORMAT, "Unsupported format 0x%08x.\n",
  146. fmt->fmt.pix.pixelformat);
  147. return -EINVAL;
  148. }
  149. /* Find the closest image size. The distance between image sizes is
  150. * the size in pixels of the non-overlapping regions between the
  151. * requested size and the frame-specified size.
  152. */
  153. rw = fmt->fmt.pix.width;
  154. rh = fmt->fmt.pix.height;
  155. maxd = (unsigned int)-1;
  156. for (i = 0; i < format->nframes; ++i) {
  157. __u16 w = format->frame[i].wWidth;
  158. __u16 h = format->frame[i].wHeight;
  159. d = min(w, rw) * min(h, rh);
  160. d = w*h + rw*rh - 2*d;
  161. if (d < maxd) {
  162. maxd = d;
  163. frame = &format->frame[i];
  164. }
  165. if (maxd == 0)
  166. break;
  167. }
  168. if (frame == NULL) {
  169. uvc_trace(UVC_TRACE_FORMAT, "Unsupported size %ux%u.\n",
  170. fmt->fmt.pix.width, fmt->fmt.pix.height);
  171. return -EINVAL;
  172. }
  173. /* Use the default frame interval. */
  174. interval = frame->dwDefaultFrameInterval;
  175. uvc_trace(UVC_TRACE_FORMAT, "Using default frame interval %u.%u us "
  176. "(%u.%u fps).\n", interval/10, interval%10, 10000000/interval,
  177. (100000000/interval)%10);
  178. /* Set the format index, frame index and frame interval. */
  179. memset(probe, 0, sizeof *probe);
  180. probe->bmHint = 1; /* dwFrameInterval */
  181. probe->bFormatIndex = format->index;
  182. probe->bFrameIndex = frame->bFrameIndex;
  183. probe->dwFrameInterval = uvc_try_frame_interval(frame, interval);
  184. /* Some webcams stall the probe control set request when the
  185. * dwMaxVideoFrameSize field is set to zero. The UVC specification
  186. * clearly states that the field is read-only from the host, so this
  187. * is a webcam bug. Set dwMaxVideoFrameSize to the value reported by
  188. * the webcam to work around the problem.
  189. *
  190. * The workaround could probably be enabled for all webcams, so the
  191. * quirk can be removed if needed. It's currently useful to detect
  192. * webcam bugs and fix them before they hit the market (providing
  193. * developers test their webcams with the Linux driver as well as with
  194. * the Windows driver).
  195. */
  196. mutex_lock(&stream->mutex);
  197. if (stream->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS)
  198. probe->dwMaxVideoFrameSize =
  199. stream->ctrl.dwMaxVideoFrameSize;
  200. /* Probe the device. */
  201. ret = uvc_probe_video(stream, probe);
  202. mutex_unlock(&stream->mutex);
  203. if (ret < 0)
  204. goto done;
  205. fmt->fmt.pix.width = frame->wWidth;
  206. fmt->fmt.pix.height = frame->wHeight;
  207. fmt->fmt.pix.field = V4L2_FIELD_NONE;
  208. fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
  209. fmt->fmt.pix.sizeimage = probe->dwMaxVideoFrameSize;
  210. fmt->fmt.pix.colorspace = format->colorspace;
  211. fmt->fmt.pix.priv = 0;
  212. if (uvc_format != NULL)
  213. *uvc_format = format;
  214. if (uvc_frame != NULL)
  215. *uvc_frame = frame;
  216. done:
  217. return ret;
  218. }
  219. static int uvc_v4l2_get_format(struct uvc_streaming *stream,
  220. struct v4l2_format *fmt)
  221. {
  222. struct uvc_format *format;
  223. struct uvc_frame *frame;
  224. int ret = 0;
  225. if (fmt->type != stream->type)
  226. return -EINVAL;
  227. mutex_lock(&stream->mutex);
  228. format = stream->cur_format;
  229. frame = stream->cur_frame;
  230. if (format == NULL || frame == NULL) {
  231. ret = -EINVAL;
  232. goto done;
  233. }
  234. fmt->fmt.pix.pixelformat = format->fcc;
  235. fmt->fmt.pix.width = frame->wWidth;
  236. fmt->fmt.pix.height = frame->wHeight;
  237. fmt->fmt.pix.field = V4L2_FIELD_NONE;
  238. fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
  239. fmt->fmt.pix.sizeimage = stream->ctrl.dwMaxVideoFrameSize;
  240. fmt->fmt.pix.colorspace = format->colorspace;
  241. fmt->fmt.pix.priv = 0;
  242. done:
  243. mutex_unlock(&stream->mutex);
  244. return ret;
  245. }
  246. static int uvc_v4l2_set_format(struct uvc_streaming *stream,
  247. struct v4l2_format *fmt)
  248. {
  249. struct uvc_streaming_control probe;
  250. struct uvc_format *format;
  251. struct uvc_frame *frame;
  252. int ret;
  253. if (fmt->type != stream->type)
  254. return -EINVAL;
  255. ret = uvc_v4l2_try_format(stream, fmt, &probe, &format, &frame);
  256. if (ret < 0)
  257. return ret;
  258. mutex_lock(&stream->mutex);
  259. if (uvc_queue_allocated(&stream->queue)) {
  260. ret = -EBUSY;
  261. goto done;
  262. }
  263. memcpy(&stream->ctrl, &probe, sizeof probe);
  264. stream->cur_format = format;
  265. stream->cur_frame = frame;
  266. done:
  267. mutex_unlock(&stream->mutex);
  268. return ret;
  269. }
  270. static int uvc_v4l2_get_streamparm(struct uvc_streaming *stream,
  271. struct v4l2_streamparm *parm)
  272. {
  273. uint32_t numerator, denominator;
  274. if (parm->type != stream->type)
  275. return -EINVAL;
  276. mutex_lock(&stream->mutex);
  277. numerator = stream->ctrl.dwFrameInterval;
  278. mutex_unlock(&stream->mutex);
  279. denominator = 10000000;
  280. uvc_simplify_fraction(&numerator, &denominator, 8, 333);
  281. memset(parm, 0, sizeof *parm);
  282. parm->type = stream->type;
  283. if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  284. parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
  285. parm->parm.capture.capturemode = 0;
  286. parm->parm.capture.timeperframe.numerator = numerator;
  287. parm->parm.capture.timeperframe.denominator = denominator;
  288. parm->parm.capture.extendedmode = 0;
  289. parm->parm.capture.readbuffers = 0;
  290. } else {
  291. parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
  292. parm->parm.output.outputmode = 0;
  293. parm->parm.output.timeperframe.numerator = numerator;
  294. parm->parm.output.timeperframe.denominator = denominator;
  295. }
  296. return 0;
  297. }
  298. static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream,
  299. struct v4l2_streamparm *parm)
  300. {
  301. struct uvc_streaming_control probe;
  302. struct v4l2_fract timeperframe;
  303. uint32_t interval;
  304. int ret;
  305. if (parm->type != stream->type)
  306. return -EINVAL;
  307. if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  308. timeperframe = parm->parm.capture.timeperframe;
  309. else
  310. timeperframe = parm->parm.output.timeperframe;
  311. interval = uvc_fraction_to_interval(timeperframe.numerator,
  312. timeperframe.denominator);
  313. uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n",
  314. timeperframe.numerator, timeperframe.denominator, interval);
  315. mutex_lock(&stream->mutex);
  316. if (uvc_queue_streaming(&stream->queue)) {
  317. mutex_unlock(&stream->mutex);
  318. return -EBUSY;
  319. }
  320. memcpy(&probe, &stream->ctrl, sizeof probe);
  321. probe.dwFrameInterval =
  322. uvc_try_frame_interval(stream->cur_frame, interval);
  323. /* Probe the device with the new settings. */
  324. ret = uvc_probe_video(stream, &probe);
  325. if (ret < 0) {
  326. mutex_unlock(&stream->mutex);
  327. return ret;
  328. }
  329. memcpy(&stream->ctrl, &probe, sizeof probe);
  330. mutex_unlock(&stream->mutex);
  331. /* Return the actual frame period. */
  332. timeperframe.numerator = probe.dwFrameInterval;
  333. timeperframe.denominator = 10000000;
  334. uvc_simplify_fraction(&timeperframe.numerator,
  335. &timeperframe.denominator, 8, 333);
  336. if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  337. parm->parm.capture.timeperframe = timeperframe;
  338. else
  339. parm->parm.output.timeperframe = timeperframe;
  340. return 0;
  341. }
  342. /* ------------------------------------------------------------------------
  343. * Privilege management
  344. */
  345. /*
  346. * Privilege management is the multiple-open implementation basis. The current
  347. * implementation is completely transparent for the end-user and doesn't
  348. * require explicit use of the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls.
  349. * Those ioctls enable finer control on the device (by making possible for a
  350. * user to request exclusive access to a device), but are not mature yet.
  351. * Switching to the V4L2 priority mechanism might be considered in the future
  352. * if this situation changes.
  353. *
  354. * Each open instance of a UVC device can either be in a privileged or
  355. * unprivileged state. Only a single instance can be in a privileged state at
  356. * a given time. Trying to perform an operation that requires privileges will
  357. * automatically acquire the required privileges if possible, or return -EBUSY
  358. * otherwise. Privileges are dismissed when closing the instance or when
  359. * freeing the video buffers using VIDIOC_REQBUFS.
  360. *
  361. * Operations that require privileges are:
  362. *
  363. * - VIDIOC_S_INPUT
  364. * - VIDIOC_S_PARM
  365. * - VIDIOC_S_FMT
  366. * - VIDIOC_REQBUFS
  367. */
  368. static int uvc_acquire_privileges(struct uvc_fh *handle)
  369. {
  370. /* Always succeed if the handle is already privileged. */
  371. if (handle->state == UVC_HANDLE_ACTIVE)
  372. return 0;
  373. /* Check if the device already has a privileged handle. */
  374. if (atomic_inc_return(&handle->stream->active) != 1) {
  375. atomic_dec(&handle->stream->active);
  376. return -EBUSY;
  377. }
  378. handle->state = UVC_HANDLE_ACTIVE;
  379. return 0;
  380. }
  381. static void uvc_dismiss_privileges(struct uvc_fh *handle)
  382. {
  383. if (handle->state == UVC_HANDLE_ACTIVE)
  384. atomic_dec(&handle->stream->active);
  385. handle->state = UVC_HANDLE_PASSIVE;
  386. }
  387. static int uvc_has_privileges(struct uvc_fh *handle)
  388. {
  389. return handle->state == UVC_HANDLE_ACTIVE;
  390. }
  391. /* ------------------------------------------------------------------------
  392. * V4L2 file operations
  393. */
  394. static int uvc_v4l2_open(struct file *file)
  395. {
  396. struct uvc_streaming *stream;
  397. struct uvc_fh *handle;
  398. int ret = 0;
  399. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n");
  400. stream = video_drvdata(file);
  401. if (stream->dev->state & UVC_DEV_DISCONNECTED)
  402. return -ENODEV;
  403. ret = usb_autopm_get_interface(stream->dev->intf);
  404. if (ret < 0)
  405. return ret;
  406. /* Create the device handle. */
  407. handle = kzalloc(sizeof *handle, GFP_KERNEL);
  408. if (handle == NULL) {
  409. usb_autopm_put_interface(stream->dev->intf);
  410. return -ENOMEM;
  411. }
  412. if (atomic_inc_return(&stream->dev->users) == 1) {
  413. ret = uvc_status_start(stream->dev);
  414. if (ret < 0) {
  415. usb_autopm_put_interface(stream->dev->intf);
  416. atomic_dec(&stream->dev->users);
  417. kfree(handle);
  418. return ret;
  419. }
  420. }
  421. handle->chain = stream->chain;
  422. handle->stream = stream;
  423. handle->state = UVC_HANDLE_PASSIVE;
  424. file->private_data = handle;
  425. return 0;
  426. }
  427. static int uvc_v4l2_release(struct file *file)
  428. {
  429. struct uvc_fh *handle = file->private_data;
  430. struct uvc_streaming *stream = handle->stream;
  431. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n");
  432. /* Only free resources if this is a privileged handle. */
  433. if (uvc_has_privileges(handle)) {
  434. uvc_video_enable(stream, 0);
  435. uvc_free_buffers(&stream->queue);
  436. }
  437. /* Release the file handle. */
  438. uvc_dismiss_privileges(handle);
  439. kfree(handle);
  440. file->private_data = NULL;
  441. if (atomic_dec_return(&stream->dev->users) == 0)
  442. uvc_status_stop(stream->dev);
  443. usb_autopm_put_interface(stream->dev->intf);
  444. return 0;
  445. }
  446. static long uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
  447. {
  448. struct video_device *vdev = video_devdata(file);
  449. struct uvc_fh *handle = file->private_data;
  450. struct uvc_video_chain *chain = handle->chain;
  451. struct uvc_streaming *stream = handle->stream;
  452. long ret = 0;
  453. switch (cmd) {
  454. /* Query capabilities */
  455. case VIDIOC_QUERYCAP:
  456. {
  457. struct v4l2_capability *cap = arg;
  458. memset(cap, 0, sizeof *cap);
  459. strlcpy(cap->driver, "uvcvideo", sizeof cap->driver);
  460. strlcpy(cap->card, vdev->name, sizeof cap->card);
  461. usb_make_path(stream->dev->udev,
  462. cap->bus_info, sizeof(cap->bus_info));
  463. cap->version = LINUX_VERSION_CODE;
  464. if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  465. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
  466. | V4L2_CAP_STREAMING;
  467. else
  468. cap->capabilities = V4L2_CAP_VIDEO_OUTPUT
  469. | V4L2_CAP_STREAMING;
  470. break;
  471. }
  472. /* Get, Set & Query control */
  473. case VIDIOC_QUERYCTRL:
  474. return uvc_query_v4l2_ctrl(chain, arg);
  475. case VIDIOC_G_CTRL:
  476. {
  477. struct v4l2_control *ctrl = arg;
  478. struct v4l2_ext_control xctrl;
  479. memset(&xctrl, 0, sizeof xctrl);
  480. xctrl.id = ctrl->id;
  481. ret = uvc_ctrl_begin(chain);
  482. if (ret < 0)
  483. return ret;
  484. ret = uvc_ctrl_get(chain, &xctrl);
  485. uvc_ctrl_rollback(chain);
  486. if (ret >= 0)
  487. ctrl->value = xctrl.value;
  488. break;
  489. }
  490. case VIDIOC_S_CTRL:
  491. {
  492. struct v4l2_control *ctrl = arg;
  493. struct v4l2_ext_control xctrl;
  494. memset(&xctrl, 0, sizeof xctrl);
  495. xctrl.id = ctrl->id;
  496. xctrl.value = ctrl->value;
  497. ret = uvc_ctrl_begin(chain);
  498. if (ret < 0)
  499. return ret;
  500. ret = uvc_ctrl_set(chain, &xctrl);
  501. if (ret < 0) {
  502. uvc_ctrl_rollback(chain);
  503. return ret;
  504. }
  505. ret = uvc_ctrl_commit(chain);
  506. if (ret == 0)
  507. ctrl->value = xctrl.value;
  508. break;
  509. }
  510. case VIDIOC_QUERYMENU:
  511. return uvc_query_v4l2_menu(chain, arg);
  512. case VIDIOC_G_EXT_CTRLS:
  513. {
  514. struct v4l2_ext_controls *ctrls = arg;
  515. struct v4l2_ext_control *ctrl = ctrls->controls;
  516. unsigned int i;
  517. ret = uvc_ctrl_begin(chain);
  518. if (ret < 0)
  519. return ret;
  520. for (i = 0; i < ctrls->count; ++ctrl, ++i) {
  521. ret = uvc_ctrl_get(chain, ctrl);
  522. if (ret < 0) {
  523. uvc_ctrl_rollback(chain);
  524. ctrls->error_idx = i;
  525. return ret;
  526. }
  527. }
  528. ctrls->error_idx = 0;
  529. ret = uvc_ctrl_rollback(chain);
  530. break;
  531. }
  532. case VIDIOC_S_EXT_CTRLS:
  533. case VIDIOC_TRY_EXT_CTRLS:
  534. {
  535. struct v4l2_ext_controls *ctrls = arg;
  536. struct v4l2_ext_control *ctrl = ctrls->controls;
  537. unsigned int i;
  538. ret = uvc_ctrl_begin(chain);
  539. if (ret < 0)
  540. return ret;
  541. for (i = 0; i < ctrls->count; ++ctrl, ++i) {
  542. ret = uvc_ctrl_set(chain, ctrl);
  543. if (ret < 0) {
  544. uvc_ctrl_rollback(chain);
  545. ctrls->error_idx = i;
  546. return ret;
  547. }
  548. }
  549. ctrls->error_idx = 0;
  550. if (cmd == VIDIOC_S_EXT_CTRLS)
  551. ret = uvc_ctrl_commit(chain);
  552. else
  553. ret = uvc_ctrl_rollback(chain);
  554. break;
  555. }
  556. /* Get, Set & Enum input */
  557. case VIDIOC_ENUMINPUT:
  558. {
  559. const struct uvc_entity *selector = chain->selector;
  560. struct v4l2_input *input = arg;
  561. struct uvc_entity *iterm = NULL;
  562. u32 index = input->index;
  563. int pin = 0;
  564. if (selector == NULL ||
  565. (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
  566. if (index != 0)
  567. return -EINVAL;
  568. list_for_each_entry(iterm, &chain->entities, chain) {
  569. if (UVC_ENTITY_IS_ITERM(iterm))
  570. break;
  571. }
  572. pin = iterm->id;
  573. } else if (index < selector->bNrInPins) {
  574. pin = selector->baSourceID[index];
  575. list_for_each_entry(iterm, &chain->entities, chain) {
  576. if (!UVC_ENTITY_IS_ITERM(iterm))
  577. continue;
  578. if (iterm->id == pin)
  579. break;
  580. }
  581. }
  582. if (iterm == NULL || iterm->id != pin)
  583. return -EINVAL;
  584. memset(input, 0, sizeof *input);
  585. input->index = index;
  586. strlcpy(input->name, iterm->name, sizeof input->name);
  587. if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA)
  588. input->type = V4L2_INPUT_TYPE_CAMERA;
  589. break;
  590. }
  591. case VIDIOC_G_INPUT:
  592. {
  593. u8 input;
  594. if (chain->selector == NULL ||
  595. (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
  596. *(int *)arg = 0;
  597. break;
  598. }
  599. ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
  600. chain->selector->id, chain->dev->intfnum,
  601. UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
  602. if (ret < 0)
  603. return ret;
  604. *(int *)arg = input - 1;
  605. break;
  606. }
  607. case VIDIOC_S_INPUT:
  608. {
  609. u32 input = *(u32 *)arg + 1;
  610. if ((ret = uvc_acquire_privileges(handle)) < 0)
  611. return ret;
  612. if (chain->selector == NULL ||
  613. (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
  614. if (input != 1)
  615. return -EINVAL;
  616. break;
  617. }
  618. if (input == 0 || input > chain->selector->bNrInPins)
  619. return -EINVAL;
  620. return uvc_query_ctrl(chain->dev, UVC_SET_CUR,
  621. chain->selector->id, chain->dev->intfnum,
  622. UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
  623. }
  624. /* Try, Get, Set & Enum format */
  625. case VIDIOC_ENUM_FMT:
  626. {
  627. struct v4l2_fmtdesc *fmt = arg;
  628. struct uvc_format *format;
  629. enum v4l2_buf_type type = fmt->type;
  630. __u32 index = fmt->index;
  631. if (fmt->type != stream->type ||
  632. fmt->index >= stream->nformats)
  633. return -EINVAL;
  634. memset(fmt, 0, sizeof(*fmt));
  635. fmt->index = index;
  636. fmt->type = type;
  637. format = &stream->format[fmt->index];
  638. fmt->flags = 0;
  639. if (format->flags & UVC_FMT_FLAG_COMPRESSED)
  640. fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
  641. strlcpy(fmt->description, format->name,
  642. sizeof fmt->description);
  643. fmt->description[sizeof fmt->description - 1] = 0;
  644. fmt->pixelformat = format->fcc;
  645. break;
  646. }
  647. case VIDIOC_TRY_FMT:
  648. {
  649. struct uvc_streaming_control probe;
  650. return uvc_v4l2_try_format(stream, arg, &probe, NULL, NULL);
  651. }
  652. case VIDIOC_S_FMT:
  653. if ((ret = uvc_acquire_privileges(handle)) < 0)
  654. return ret;
  655. return uvc_v4l2_set_format(stream, arg);
  656. case VIDIOC_G_FMT:
  657. return uvc_v4l2_get_format(stream, arg);
  658. /* Frame size enumeration */
  659. case VIDIOC_ENUM_FRAMESIZES:
  660. {
  661. struct v4l2_frmsizeenum *fsize = arg;
  662. struct uvc_format *format = NULL;
  663. struct uvc_frame *frame;
  664. int i;
  665. /* Look for the given pixel format */
  666. for (i = 0; i < stream->nformats; i++) {
  667. if (stream->format[i].fcc ==
  668. fsize->pixel_format) {
  669. format = &stream->format[i];
  670. break;
  671. }
  672. }
  673. if (format == NULL)
  674. return -EINVAL;
  675. if (fsize->index >= format->nframes)
  676. return -EINVAL;
  677. frame = &format->frame[fsize->index];
  678. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  679. fsize->discrete.width = frame->wWidth;
  680. fsize->discrete.height = frame->wHeight;
  681. break;
  682. }
  683. /* Frame interval enumeration */
  684. case VIDIOC_ENUM_FRAMEINTERVALS:
  685. {
  686. struct v4l2_frmivalenum *fival = arg;
  687. struct uvc_format *format = NULL;
  688. struct uvc_frame *frame = NULL;
  689. int i;
  690. /* Look for the given pixel format and frame size */
  691. for (i = 0; i < stream->nformats; i++) {
  692. if (stream->format[i].fcc ==
  693. fival->pixel_format) {
  694. format = &stream->format[i];
  695. break;
  696. }
  697. }
  698. if (format == NULL)
  699. return -EINVAL;
  700. for (i = 0; i < format->nframes; i++) {
  701. if (format->frame[i].wWidth == fival->width &&
  702. format->frame[i].wHeight == fival->height) {
  703. frame = &format->frame[i];
  704. break;
  705. }
  706. }
  707. if (frame == NULL)
  708. return -EINVAL;
  709. if (frame->bFrameIntervalType) {
  710. if (fival->index >= frame->bFrameIntervalType)
  711. return -EINVAL;
  712. fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
  713. fival->discrete.numerator =
  714. frame->dwFrameInterval[fival->index];
  715. fival->discrete.denominator = 10000000;
  716. uvc_simplify_fraction(&fival->discrete.numerator,
  717. &fival->discrete.denominator, 8, 333);
  718. } else {
  719. fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
  720. fival->stepwise.min.numerator =
  721. frame->dwFrameInterval[0];
  722. fival->stepwise.min.denominator = 10000000;
  723. fival->stepwise.max.numerator =
  724. frame->dwFrameInterval[1];
  725. fival->stepwise.max.denominator = 10000000;
  726. fival->stepwise.step.numerator =
  727. frame->dwFrameInterval[2];
  728. fival->stepwise.step.denominator = 10000000;
  729. uvc_simplify_fraction(&fival->stepwise.min.numerator,
  730. &fival->stepwise.min.denominator, 8, 333);
  731. uvc_simplify_fraction(&fival->stepwise.max.numerator,
  732. &fival->stepwise.max.denominator, 8, 333);
  733. uvc_simplify_fraction(&fival->stepwise.step.numerator,
  734. &fival->stepwise.step.denominator, 8, 333);
  735. }
  736. break;
  737. }
  738. /* Get & Set streaming parameters */
  739. case VIDIOC_G_PARM:
  740. return uvc_v4l2_get_streamparm(stream, arg);
  741. case VIDIOC_S_PARM:
  742. if ((ret = uvc_acquire_privileges(handle)) < 0)
  743. return ret;
  744. return uvc_v4l2_set_streamparm(stream, arg);
  745. /* Cropping and scaling */
  746. case VIDIOC_CROPCAP:
  747. {
  748. struct v4l2_cropcap *ccap = arg;
  749. if (ccap->type != stream->type)
  750. return -EINVAL;
  751. ccap->bounds.left = 0;
  752. ccap->bounds.top = 0;
  753. mutex_lock(&stream->mutex);
  754. ccap->bounds.width = stream->cur_frame->wWidth;
  755. ccap->bounds.height = stream->cur_frame->wHeight;
  756. mutex_unlock(&stream->mutex);
  757. ccap->defrect = ccap->bounds;
  758. ccap->pixelaspect.numerator = 1;
  759. ccap->pixelaspect.denominator = 1;
  760. break;
  761. }
  762. case VIDIOC_G_CROP:
  763. case VIDIOC_S_CROP:
  764. return -EINVAL;
  765. /* Buffers & streaming */
  766. case VIDIOC_REQBUFS:
  767. if ((ret = uvc_acquire_privileges(handle)) < 0)
  768. return ret;
  769. mutex_lock(&stream->mutex);
  770. ret = uvc_alloc_buffers(&stream->queue, arg);
  771. mutex_unlock(&stream->mutex);
  772. if (ret < 0)
  773. return ret;
  774. if (ret == 0)
  775. uvc_dismiss_privileges(handle);
  776. ret = 0;
  777. break;
  778. case VIDIOC_QUERYBUF:
  779. {
  780. struct v4l2_buffer *buf = arg;
  781. if (!uvc_has_privileges(handle))
  782. return -EBUSY;
  783. return uvc_query_buffer(&stream->queue, buf);
  784. }
  785. case VIDIOC_QBUF:
  786. if (!uvc_has_privileges(handle))
  787. return -EBUSY;
  788. return uvc_queue_buffer(&stream->queue, arg);
  789. case VIDIOC_DQBUF:
  790. if (!uvc_has_privileges(handle))
  791. return -EBUSY;
  792. return uvc_dequeue_buffer(&stream->queue, arg,
  793. file->f_flags & O_NONBLOCK);
  794. case VIDIOC_STREAMON:
  795. {
  796. int *type = arg;
  797. if (*type != stream->type)
  798. return -EINVAL;
  799. if (!uvc_has_privileges(handle))
  800. return -EBUSY;
  801. mutex_lock(&stream->mutex);
  802. ret = uvc_video_enable(stream, 1);
  803. mutex_unlock(&stream->mutex);
  804. if (ret < 0)
  805. return ret;
  806. break;
  807. }
  808. case VIDIOC_STREAMOFF:
  809. {
  810. int *type = arg;
  811. if (*type != stream->type)
  812. return -EINVAL;
  813. if (!uvc_has_privileges(handle))
  814. return -EBUSY;
  815. return uvc_video_enable(stream, 0);
  816. }
  817. /* Analog video standards make no sense for digital cameras. */
  818. case VIDIOC_ENUMSTD:
  819. case VIDIOC_QUERYSTD:
  820. case VIDIOC_G_STD:
  821. case VIDIOC_S_STD:
  822. case VIDIOC_OVERLAY:
  823. case VIDIOC_ENUMAUDIO:
  824. case VIDIOC_ENUMAUDOUT:
  825. case VIDIOC_ENUMOUTPUT:
  826. uvc_trace(UVC_TRACE_IOCTL, "Unsupported ioctl 0x%08x\n", cmd);
  827. return -EINVAL;
  828. case UVCIOC_CTRL_MAP:
  829. return uvc_ioctl_ctrl_map(chain, arg);
  830. case UVCIOC_CTRL_QUERY:
  831. return uvc_xu_ctrl_query(chain, arg);
  832. default:
  833. uvc_trace(UVC_TRACE_IOCTL, "Unknown ioctl 0x%08x\n", cmd);
  834. return -ENOTTY;
  835. }
  836. return ret;
  837. }
  838. static long uvc_v4l2_ioctl(struct file *file,
  839. unsigned int cmd, unsigned long arg)
  840. {
  841. if (uvc_trace_param & UVC_TRACE_IOCTL) {
  842. uvc_printk(KERN_DEBUG, "uvc_v4l2_ioctl(");
  843. v4l_printk_ioctl(cmd);
  844. printk(")\n");
  845. }
  846. return video_usercopy(file, cmd, arg, uvc_v4l2_do_ioctl);
  847. }
  848. #ifdef CONFIG_COMPAT
  849. struct uvc_xu_control_mapping32 {
  850. __u32 id;
  851. __u8 name[32];
  852. __u8 entity[16];
  853. __u8 selector;
  854. __u8 size;
  855. __u8 offset;
  856. __u32 v4l2_type;
  857. __u32 data_type;
  858. compat_caddr_t menu_info;
  859. __u32 menu_count;
  860. __u32 reserved[4];
  861. };
  862. static int uvc_v4l2_get_xu_mapping(struct uvc_xu_control_mapping *kp,
  863. const struct uvc_xu_control_mapping32 __user *up)
  864. {
  865. struct uvc_menu_info __user *umenus;
  866. struct uvc_menu_info __user *kmenus;
  867. compat_caddr_t p;
  868. if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
  869. __copy_from_user(kp, up, offsetof(typeof(*up), menu_info)) ||
  870. __get_user(kp->menu_count, &up->menu_count))
  871. return -EFAULT;
  872. memset(kp->reserved, 0, sizeof(kp->reserved));
  873. if (kp->menu_count == 0) {
  874. kp->menu_info = NULL;
  875. return 0;
  876. }
  877. if (__get_user(p, &up->menu_info))
  878. return -EFAULT;
  879. umenus = compat_ptr(p);
  880. if (!access_ok(VERIFY_READ, umenus, kp->menu_count * sizeof(*umenus)))
  881. return -EFAULT;
  882. kmenus = compat_alloc_user_space(kp->menu_count * sizeof(*kmenus));
  883. if (kmenus == NULL)
  884. return -EFAULT;
  885. kp->menu_info = kmenus;
  886. if (copy_in_user(kmenus, umenus, kp->menu_count * sizeof(*umenus)))
  887. return -EFAULT;
  888. return 0;
  889. }
  890. static int uvc_v4l2_put_xu_mapping(const struct uvc_xu_control_mapping *kp,
  891. struct uvc_xu_control_mapping32 __user *up)
  892. {
  893. struct uvc_menu_info __user *umenus;
  894. struct uvc_menu_info __user *kmenus = kp->menu_info;
  895. compat_caddr_t p;
  896. if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
  897. __copy_to_user(up, kp, offsetof(typeof(*up), menu_info)) ||
  898. __put_user(kp->menu_count, &up->menu_count))
  899. return -EFAULT;
  900. __clear_user(up->reserved, sizeof(up->reserved));
  901. if (kp->menu_count == 0)
  902. return 0;
  903. if (get_user(p, &up->menu_info))
  904. return -EFAULT;
  905. umenus = compat_ptr(p);
  906. if (!access_ok(VERIFY_WRITE, umenus, kp->menu_count * sizeof(*umenus)))
  907. return -EFAULT;
  908. if (copy_in_user(umenus, kmenus, kp->menu_count * sizeof(*umenus)))
  909. return -EFAULT;
  910. return 0;
  911. }
  912. struct uvc_xu_control_query32 {
  913. __u8 unit;
  914. __u8 selector;
  915. __u8 query;
  916. __u16 size;
  917. compat_caddr_t data;
  918. };
  919. static int uvc_v4l2_get_xu_query(struct uvc_xu_control_query *kp,
  920. const struct uvc_xu_control_query32 __user *up)
  921. {
  922. u8 __user *udata;
  923. u8 __user *kdata;
  924. compat_caddr_t p;
  925. if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
  926. __copy_from_user(kp, up, offsetof(typeof(*up), data)))
  927. return -EFAULT;
  928. if (kp->size == 0) {
  929. kp->data = NULL;
  930. return 0;
  931. }
  932. if (__get_user(p, &up->data))
  933. return -EFAULT;
  934. udata = compat_ptr(p);
  935. if (!access_ok(VERIFY_READ, udata, kp->size))
  936. return -EFAULT;
  937. kdata = compat_alloc_user_space(kp->size);
  938. if (kdata == NULL)
  939. return -EFAULT;
  940. kp->data = kdata;
  941. if (copy_in_user(kdata, udata, kp->size))
  942. return -EFAULT;
  943. return 0;
  944. }
  945. static int uvc_v4l2_put_xu_query(const struct uvc_xu_control_query *kp,
  946. struct uvc_xu_control_query32 __user *up)
  947. {
  948. u8 __user *udata;
  949. u8 __user *kdata = kp->data;
  950. compat_caddr_t p;
  951. if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
  952. __copy_to_user(up, kp, offsetof(typeof(*up), data)))
  953. return -EFAULT;
  954. if (kp->size == 0)
  955. return 0;
  956. if (get_user(p, &up->data))
  957. return -EFAULT;
  958. udata = compat_ptr(p);
  959. if (!access_ok(VERIFY_READ, udata, kp->size))
  960. return -EFAULT;
  961. if (copy_in_user(udata, kdata, kp->size))
  962. return -EFAULT;
  963. return 0;
  964. }
  965. #define UVCIOC_CTRL_MAP32 _IOWR('u', 0x20, struct uvc_xu_control_mapping32)
  966. #define UVCIOC_CTRL_QUERY32 _IOWR('u', 0x21, struct uvc_xu_control_query32)
  967. static long uvc_v4l2_compat_ioctl32(struct file *file,
  968. unsigned int cmd, unsigned long arg)
  969. {
  970. union {
  971. struct uvc_xu_control_mapping xmap;
  972. struct uvc_xu_control_query xqry;
  973. } karg;
  974. void __user *up = compat_ptr(arg);
  975. mm_segment_t old_fs;
  976. long ret;
  977. switch (cmd) {
  978. case UVCIOC_CTRL_MAP32:
  979. cmd = UVCIOC_CTRL_MAP;
  980. ret = uvc_v4l2_get_xu_mapping(&karg.xmap, up);
  981. break;
  982. case UVCIOC_CTRL_QUERY32:
  983. cmd = UVCIOC_CTRL_QUERY;
  984. ret = uvc_v4l2_get_xu_query(&karg.xqry, up);
  985. break;
  986. default:
  987. return -ENOIOCTLCMD;
  988. }
  989. old_fs = get_fs();
  990. set_fs(KERNEL_DS);
  991. ret = uvc_v4l2_ioctl(file, cmd, (unsigned long)&karg);
  992. set_fs(old_fs);
  993. if (ret < 0)
  994. return ret;
  995. switch (cmd) {
  996. case UVCIOC_CTRL_MAP:
  997. ret = uvc_v4l2_put_xu_mapping(&karg.xmap, up);
  998. break;
  999. case UVCIOC_CTRL_QUERY:
  1000. ret = uvc_v4l2_put_xu_query(&karg.xqry, up);
  1001. break;
  1002. }
  1003. return ret;
  1004. }
  1005. #endif
  1006. static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
  1007. size_t count, loff_t *ppos)
  1008. {
  1009. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
  1010. return -EINVAL;
  1011. }
  1012. static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
  1013. {
  1014. struct uvc_fh *handle = file->private_data;
  1015. struct uvc_streaming *stream = handle->stream;
  1016. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
  1017. return uvc_queue_mmap(&stream->queue, vma);
  1018. }
  1019. static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait)
  1020. {
  1021. struct uvc_fh *handle = file->private_data;
  1022. struct uvc_streaming *stream = handle->stream;
  1023. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
  1024. return uvc_queue_poll(&stream->queue, file, wait);
  1025. }
  1026. #ifndef CONFIG_MMU
  1027. static unsigned long uvc_v4l2_get_unmapped_area(struct file *file,
  1028. unsigned long addr, unsigned long len, unsigned long pgoff,
  1029. unsigned long flags)
  1030. {
  1031. struct uvc_fh *handle = file->private_data;
  1032. struct uvc_streaming *stream = handle->stream;
  1033. uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_get_unmapped_area\n");
  1034. return uvc_queue_get_unmapped_area(&stream->queue, pgoff);
  1035. }
  1036. #endif
  1037. const struct v4l2_file_operations uvc_fops = {
  1038. .owner = THIS_MODULE,
  1039. .open = uvc_v4l2_open,
  1040. .release = uvc_v4l2_release,
  1041. .unlocked_ioctl = uvc_v4l2_ioctl,
  1042. #ifdef CONFIG_COMPAT
  1043. .compat_ioctl32 = uvc_v4l2_compat_ioctl32,
  1044. #endif
  1045. .read = uvc_v4l2_read,
  1046. .mmap = uvc_v4l2_mmap,
  1047. .poll = uvc_v4l2_poll,
  1048. #ifndef CONFIG_MMU
  1049. .get_unmapped_area = uvc_v4l2_get_unmapped_area,
  1050. #endif
  1051. };