uvc_v4l2.c 29 KB

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