uvc_v4l2.c 36 KB

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