uvc_v4l2.c 32 KB

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