uvc_v4l2.c 38 KB

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