uvc_v4l2.c 37 KB

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