uvc_v4l2.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * uvc_v4l2.c -- USB Video Class Gadget driver
  4. *
  5. * Copyright (C) 2009-2010
  6. * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
  7. */
  8. #include <linux/device.h>
  9. #include <linux/errno.h>
  10. #include <linux/kernel.h>
  11. #include <linux/list.h>
  12. #include <linux/usb/g_uvc.h>
  13. #include <linux/usb/uvc.h>
  14. #include <linux/videodev2.h>
  15. #include <linux/vmalloc.h>
  16. #include <linux/wait.h>
  17. #include <media/v4l2-dev.h>
  18. #include <media/v4l2-event.h>
  19. #include <media/v4l2-ioctl.h>
  20. #include "f_uvc.h"
  21. #include "uvc.h"
  22. #include "uvc_queue.h"
  23. #include "uvc_video.h"
  24. #include "uvc_v4l2.h"
  25. #include "uvc_configfs.h"
  26. static const struct uvc_format_desc *to_uvc_format(struct uvcg_format *uformat)
  27. {
  28. char guid[16] = UVC_GUID_FORMAT_MJPEG;
  29. const struct uvc_format_desc *format;
  30. struct uvcg_uncompressed *unc;
  31. if (uformat->type == UVCG_UNCOMPRESSED) {
  32. unc = to_uvcg_uncompressed(&uformat->group.cg_item);
  33. if (!unc)
  34. return ERR_PTR(-EINVAL);
  35. memcpy(guid, unc->desc.guidFormat, sizeof(guid));
  36. }
  37. format = uvc_format_by_guid(guid);
  38. if (!format)
  39. return ERR_PTR(-EINVAL);
  40. return format;
  41. }
  42. static int uvc_v4l2_get_bytesperline(struct uvcg_format *uformat,
  43. struct uvcg_frame *uframe)
  44. {
  45. struct uvcg_uncompressed *u;
  46. if (uformat->type == UVCG_UNCOMPRESSED) {
  47. u = to_uvcg_uncompressed(&uformat->group.cg_item);
  48. if (!u)
  49. return 0;
  50. return u->desc.bBitsPerPixel * uframe->frame.w_width / 8;
  51. }
  52. return 0;
  53. }
  54. static int uvc_get_frame_size(struct uvcg_format *uformat,
  55. struct uvcg_frame *uframe)
  56. {
  57. unsigned int bpl = uvc_v4l2_get_bytesperline(uformat, uframe);
  58. return bpl ? bpl * uframe->frame.w_height :
  59. uframe->frame.dw_max_video_frame_buffer_size;
  60. }
  61. static struct uvcg_format *find_format_by_index(struct uvc_device *uvc, int index)
  62. {
  63. struct uvcg_format_ptr *format;
  64. struct uvcg_format *uformat = NULL;
  65. int i = 1;
  66. list_for_each_entry(format, &uvc->header->formats, entry) {
  67. if (index == i) {
  68. uformat = format->fmt;
  69. break;
  70. }
  71. i++;
  72. }
  73. return uformat;
  74. }
  75. static struct uvcg_frame *find_frame_by_index(struct uvc_device *uvc,
  76. struct uvcg_format *uformat,
  77. int index)
  78. {
  79. struct uvcg_format_ptr *format;
  80. struct uvcg_frame_ptr *frame;
  81. struct uvcg_frame *uframe = NULL;
  82. list_for_each_entry(format, &uvc->header->formats, entry) {
  83. if (format->fmt->type != uformat->type)
  84. continue;
  85. list_for_each_entry(frame, &format->fmt->frames, entry) {
  86. if (index == frame->frm->frame.b_frame_index) {
  87. uframe = frame->frm;
  88. break;
  89. }
  90. }
  91. }
  92. return uframe;
  93. }
  94. static struct uvcg_format *find_format_by_pix(struct uvc_device *uvc,
  95. u32 pixelformat)
  96. {
  97. struct uvcg_format_ptr *format;
  98. struct uvcg_format *uformat = NULL;
  99. list_for_each_entry(format, &uvc->header->formats, entry) {
  100. const struct uvc_format_desc *fmtdesc = to_uvc_format(format->fmt);
  101. if (IS_ERR(fmtdesc))
  102. continue;
  103. if (fmtdesc->fcc == pixelformat) {
  104. uformat = format->fmt;
  105. break;
  106. }
  107. }
  108. return uformat;
  109. }
  110. static struct uvcg_frame *find_closest_frame_by_size(struct uvc_device *uvc,
  111. struct uvcg_format *uformat,
  112. u16 rw, u16 rh)
  113. {
  114. struct uvc_video *video = &uvc->video;
  115. struct uvcg_format_ptr *format;
  116. struct uvcg_frame_ptr *frame;
  117. struct uvcg_frame *uframe = NULL;
  118. unsigned int d, maxd;
  119. /* Find the closest image size. The distance between image sizes is
  120. * the size in pixels of the non-overlapping regions between the
  121. * requested size and the frame-specified size.
  122. */
  123. maxd = (unsigned int)-1;
  124. list_for_each_entry(format, &uvc->header->formats, entry) {
  125. if (format->fmt->type != uformat->type)
  126. continue;
  127. list_for_each_entry(frame, &format->fmt->frames, entry) {
  128. u16 w, h;
  129. w = frame->frm->frame.w_width;
  130. h = frame->frm->frame.w_height;
  131. d = min(w, rw) * min(h, rh);
  132. d = w*h + rw*rh - 2*d;
  133. if (d < maxd) {
  134. maxd = d;
  135. uframe = frame->frm;
  136. }
  137. if (maxd == 0)
  138. break;
  139. }
  140. }
  141. if (!uframe)
  142. uvcg_dbg(&video->uvc->func, "Unsupported size %ux%u\n", rw, rh);
  143. return uframe;
  144. }
  145. /* --------------------------------------------------------------------------
  146. * Requests handling
  147. */
  148. static int
  149. uvc_send_response(struct uvc_device *uvc, struct uvc_request_data *data)
  150. {
  151. struct usb_composite_dev *cdev = uvc->func.config->cdev;
  152. struct usb_request *req = uvc->control_req;
  153. if (data->length < 0)
  154. return usb_ep_set_halt(cdev->gadget->ep0);
  155. req->length = min_t(unsigned int, uvc->event_length, data->length);
  156. req->zero = data->length < uvc->event_length;
  157. memcpy(req->buf, data->data, req->length);
  158. return usb_ep_queue(cdev->gadget->ep0, req, GFP_KERNEL);
  159. }
  160. /* --------------------------------------------------------------------------
  161. * V4L2 ioctls
  162. */
  163. static int
  164. uvc_v4l2_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
  165. {
  166. struct video_device *vdev = video_devdata(file);
  167. struct uvc_device *uvc = video_get_drvdata(vdev);
  168. struct usb_composite_dev *cdev = uvc->func.config->cdev;
  169. strscpy(cap->driver, "g_uvc", sizeof(cap->driver));
  170. strscpy(cap->card, cdev->gadget->name, sizeof(cap->card));
  171. strscpy(cap->bus_info, dev_name(&cdev->gadget->dev),
  172. sizeof(cap->bus_info));
  173. return 0;
  174. }
  175. static int
  176. uvc_v4l2_get_format(struct file *file, void *fh, struct v4l2_format *fmt)
  177. {
  178. struct video_device *vdev = video_devdata(file);
  179. struct uvc_device *uvc = video_get_drvdata(vdev);
  180. struct uvc_video *video = &uvc->video;
  181. fmt->fmt.pix.pixelformat = video->fcc;
  182. fmt->fmt.pix.width = video->width;
  183. fmt->fmt.pix.height = video->height;
  184. fmt->fmt.pix.field = V4L2_FIELD_NONE;
  185. fmt->fmt.pix.bytesperline = video->bpp * video->width / 8;
  186. fmt->fmt.pix.sizeimage = video->imagesize;
  187. fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
  188. fmt->fmt.pix.priv = 0;
  189. return 0;
  190. }
  191. static int
  192. uvc_v4l2_try_format(struct file *file, void *fh, struct v4l2_format *fmt)
  193. {
  194. struct video_device *vdev = video_devdata(file);
  195. struct uvc_device *uvc = video_get_drvdata(vdev);
  196. struct uvc_video *video = &uvc->video;
  197. struct uvcg_format *uformat;
  198. struct uvcg_frame *uframe;
  199. const struct uvc_format_desc *fmtdesc;
  200. u8 *fcc;
  201. if (fmt->type != video->queue.queue.type)
  202. return -EINVAL;
  203. fcc = (u8 *)&fmt->fmt.pix.pixelformat;
  204. uvcg_dbg(&uvc->func, "Trying format 0x%08x (%c%c%c%c): %ux%u\n",
  205. fmt->fmt.pix.pixelformat,
  206. fcc[0], fcc[1], fcc[2], fcc[3],
  207. fmt->fmt.pix.width, fmt->fmt.pix.height);
  208. uformat = find_format_by_pix(uvc, fmt->fmt.pix.pixelformat);
  209. if (!uformat)
  210. return -EINVAL;
  211. uframe = find_closest_frame_by_size(uvc, uformat,
  212. fmt->fmt.pix.width, fmt->fmt.pix.height);
  213. if (!uframe)
  214. return -EINVAL;
  215. if (uformat->type == UVCG_UNCOMPRESSED) {
  216. struct uvcg_uncompressed *u =
  217. to_uvcg_uncompressed(&uformat->group.cg_item);
  218. if (!u)
  219. return 0;
  220. v4l2_fill_pixfmt(&fmt->fmt.pix, fmt->fmt.pix.pixelformat,
  221. uframe->frame.w_width, uframe->frame.w_height);
  222. if (fmt->fmt.pix.sizeimage != (uvc_v4l2_get_bytesperline(uformat, uframe) *
  223. uframe->frame.w_height))
  224. return -EINVAL;
  225. } else {
  226. fmt->fmt.pix.width = uframe->frame.w_width;
  227. fmt->fmt.pix.height = uframe->frame.w_height;
  228. fmt->fmt.pix.bytesperline = uvc_v4l2_get_bytesperline(uformat, uframe);
  229. fmt->fmt.pix.sizeimage = uvc_get_frame_size(uformat, uframe);
  230. fmtdesc = to_uvc_format(uformat);
  231. if (IS_ERR(fmtdesc))
  232. return PTR_ERR(fmtdesc);
  233. fmt->fmt.pix.pixelformat = fmtdesc->fcc;
  234. }
  235. fmt->fmt.pix.field = V4L2_FIELD_NONE;
  236. fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
  237. fmt->fmt.pix.priv = 0;
  238. return 0;
  239. }
  240. static int
  241. uvc_v4l2_set_format(struct file *file, void *fh, struct v4l2_format *fmt)
  242. {
  243. struct video_device *vdev = video_devdata(file);
  244. struct uvc_device *uvc = video_get_drvdata(vdev);
  245. struct uvc_video *video = &uvc->video;
  246. int ret;
  247. ret = uvc_v4l2_try_format(file, fh, fmt);
  248. if (ret)
  249. return ret;
  250. video->fcc = fmt->fmt.pix.pixelformat;
  251. video->bpp = fmt->fmt.pix.bytesperline * 8 / video->width;
  252. video->width = fmt->fmt.pix.width;
  253. video->height = fmt->fmt.pix.height;
  254. video->imagesize = fmt->fmt.pix.sizeimage;
  255. return ret;
  256. }
  257. static int
  258. uvc_v4l2_enum_frameintervals(struct file *file, void *fh,
  259. struct v4l2_frmivalenum *fival)
  260. {
  261. struct video_device *vdev = video_devdata(file);
  262. struct uvc_device *uvc = video_get_drvdata(vdev);
  263. struct uvcg_format *uformat = NULL;
  264. struct uvcg_frame *uframe = NULL;
  265. struct uvcg_frame_ptr *frame;
  266. uformat = find_format_by_pix(uvc, fival->pixel_format);
  267. if (!uformat)
  268. return -EINVAL;
  269. list_for_each_entry(frame, &uformat->frames, entry) {
  270. if (frame->frm->frame.w_width == fival->width &&
  271. frame->frm->frame.w_height == fival->height) {
  272. uframe = frame->frm;
  273. break;
  274. }
  275. }
  276. if (!uframe)
  277. return -EINVAL;
  278. if (fival->index >= uframe->frame.b_frame_interval_type)
  279. return -EINVAL;
  280. fival->discrete.numerator =
  281. uframe->dw_frame_interval[fival->index];
  282. /* TODO: handle V4L2_FRMIVAL_TYPE_STEPWISE */
  283. fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
  284. fival->discrete.denominator = 10000000;
  285. v4l2_simplify_fraction(&fival->discrete.numerator,
  286. &fival->discrete.denominator, 8, 333);
  287. return 0;
  288. }
  289. static int
  290. uvc_v4l2_enum_framesizes(struct file *file, void *fh,
  291. struct v4l2_frmsizeenum *fsize)
  292. {
  293. struct video_device *vdev = video_devdata(file);
  294. struct uvc_device *uvc = video_get_drvdata(vdev);
  295. struct uvcg_format *uformat = NULL;
  296. struct uvcg_frame *uframe = NULL;
  297. uformat = find_format_by_pix(uvc, fsize->pixel_format);
  298. if (!uformat)
  299. return -EINVAL;
  300. if (fsize->index >= uformat->num_frames)
  301. return -EINVAL;
  302. uframe = find_frame_by_index(uvc, uformat, fsize->index + 1);
  303. if (!uframe)
  304. return -EINVAL;
  305. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  306. fsize->discrete.width = uframe->frame.w_width;
  307. fsize->discrete.height = uframe->frame.w_height;
  308. return 0;
  309. }
  310. static int
  311. uvc_v4l2_enum_format(struct file *file, void *fh, struct v4l2_fmtdesc *f)
  312. {
  313. struct video_device *vdev = video_devdata(file);
  314. struct uvc_device *uvc = video_get_drvdata(vdev);
  315. const struct uvc_format_desc *fmtdesc;
  316. struct uvcg_format *uformat;
  317. if (f->index >= uvc->header->num_fmt)
  318. return -EINVAL;
  319. uformat = find_format_by_index(uvc, f->index + 1);
  320. if (!uformat)
  321. return -EINVAL;
  322. fmtdesc = to_uvc_format(uformat);
  323. if (IS_ERR(fmtdesc))
  324. return PTR_ERR(fmtdesc);
  325. f->pixelformat = fmtdesc->fcc;
  326. return 0;
  327. }
  328. static int
  329. uvc_v4l2_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *b)
  330. {
  331. struct video_device *vdev = video_devdata(file);
  332. struct uvc_device *uvc = video_get_drvdata(vdev);
  333. struct uvc_video *video = &uvc->video;
  334. if (b->type != video->queue.queue.type)
  335. return -EINVAL;
  336. return uvcg_alloc_buffers(&video->queue, b);
  337. }
  338. static int
  339. uvc_v4l2_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
  340. {
  341. struct video_device *vdev = video_devdata(file);
  342. struct uvc_device *uvc = video_get_drvdata(vdev);
  343. struct uvc_video *video = &uvc->video;
  344. return uvcg_query_buffer(&video->queue, b);
  345. }
  346. static int
  347. uvc_v4l2_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
  348. {
  349. struct video_device *vdev = video_devdata(file);
  350. struct uvc_device *uvc = video_get_drvdata(vdev);
  351. struct uvc_video *video = &uvc->video;
  352. int ret;
  353. ret = uvcg_queue_buffer(&video->queue, b);
  354. if (ret < 0)
  355. return ret;
  356. if (uvc->state == UVC_STATE_STREAMING)
  357. queue_work(video->async_wq, &video->pump);
  358. return ret;
  359. }
  360. static int
  361. uvc_v4l2_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
  362. {
  363. struct video_device *vdev = video_devdata(file);
  364. struct uvc_device *uvc = video_get_drvdata(vdev);
  365. struct uvc_video *video = &uvc->video;
  366. return uvcg_dequeue_buffer(&video->queue, b, file->f_flags & O_NONBLOCK);
  367. }
  368. static int
  369. uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
  370. {
  371. struct video_device *vdev = video_devdata(file);
  372. struct uvc_device *uvc = video_get_drvdata(vdev);
  373. struct uvc_video *video = &uvc->video;
  374. int ret;
  375. if (type != video->queue.queue.type)
  376. return -EINVAL;
  377. /* Enable UVC video. */
  378. ret = uvcg_video_enable(video);
  379. if (ret < 0)
  380. return ret;
  381. /*
  382. * Complete the alternate setting selection setup phase now that
  383. * userspace is ready to provide video frames.
  384. */
  385. uvc_function_setup_continue(uvc, 0);
  386. uvc->state = UVC_STATE_STREAMING;
  387. return 0;
  388. }
  389. static int
  390. uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
  391. {
  392. struct video_device *vdev = video_devdata(file);
  393. struct uvc_device *uvc = video_get_drvdata(vdev);
  394. struct uvc_video *video = &uvc->video;
  395. int ret = 0;
  396. if (type != video->queue.queue.type)
  397. return -EINVAL;
  398. ret = uvcg_video_disable(video);
  399. if (ret < 0)
  400. return ret;
  401. uvc->state = UVC_STATE_CONNECTED;
  402. uvc_function_setup_continue(uvc, 1);
  403. return 0;
  404. }
  405. static int
  406. uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
  407. const struct v4l2_event_subscription *sub)
  408. {
  409. struct uvc_device *uvc = video_get_drvdata(fh->vdev);
  410. struct uvc_file_handle *handle = to_uvc_file_handle(fh);
  411. int ret;
  412. if (sub->type < UVC_EVENT_FIRST || sub->type > UVC_EVENT_LAST)
  413. return -EINVAL;
  414. if (sub->type == UVC_EVENT_SETUP && uvc->func_connected)
  415. return -EBUSY;
  416. ret = v4l2_event_subscribe(fh, sub, 2, NULL);
  417. if (ret < 0)
  418. return ret;
  419. if (sub->type == UVC_EVENT_SETUP) {
  420. uvc->func_connected = true;
  421. handle->is_uvc_app_handle = true;
  422. uvc_function_connect(uvc);
  423. }
  424. return 0;
  425. }
  426. static void uvc_v4l2_disable(struct uvc_device *uvc)
  427. {
  428. uvc_function_disconnect(uvc);
  429. uvcg_video_disable(&uvc->video);
  430. uvcg_free_buffers(&uvc->video.queue);
  431. uvc->func_connected = false;
  432. wake_up_interruptible(&uvc->func_connected_queue);
  433. }
  434. static int
  435. uvc_v4l2_unsubscribe_event(struct v4l2_fh *fh,
  436. const struct v4l2_event_subscription *sub)
  437. {
  438. struct uvc_device *uvc = video_get_drvdata(fh->vdev);
  439. struct uvc_file_handle *handle = to_uvc_file_handle(fh);
  440. int ret;
  441. ret = v4l2_event_unsubscribe(fh, sub);
  442. if (ret < 0)
  443. return ret;
  444. if (sub->type == UVC_EVENT_SETUP && handle->is_uvc_app_handle) {
  445. uvc_v4l2_disable(uvc);
  446. handle->is_uvc_app_handle = false;
  447. }
  448. return 0;
  449. }
  450. static long
  451. uvc_v4l2_ioctl_default(struct file *file, void *fh, bool valid_prio,
  452. unsigned int cmd, void *arg)
  453. {
  454. struct video_device *vdev = video_devdata(file);
  455. struct uvc_device *uvc = video_get_drvdata(vdev);
  456. switch (cmd) {
  457. case UVCIOC_SEND_RESPONSE:
  458. return uvc_send_response(uvc, arg);
  459. default:
  460. return -ENOIOCTLCMD;
  461. }
  462. }
  463. const struct v4l2_ioctl_ops uvc_v4l2_ioctl_ops = {
  464. .vidioc_querycap = uvc_v4l2_querycap,
  465. .vidioc_try_fmt_vid_out = uvc_v4l2_try_format,
  466. .vidioc_g_fmt_vid_out = uvc_v4l2_get_format,
  467. .vidioc_s_fmt_vid_out = uvc_v4l2_set_format,
  468. .vidioc_enum_frameintervals = uvc_v4l2_enum_frameintervals,
  469. .vidioc_enum_framesizes = uvc_v4l2_enum_framesizes,
  470. .vidioc_enum_fmt_vid_out = uvc_v4l2_enum_format,
  471. .vidioc_reqbufs = uvc_v4l2_reqbufs,
  472. .vidioc_querybuf = uvc_v4l2_querybuf,
  473. .vidioc_qbuf = uvc_v4l2_qbuf,
  474. .vidioc_dqbuf = uvc_v4l2_dqbuf,
  475. .vidioc_streamon = uvc_v4l2_streamon,
  476. .vidioc_streamoff = uvc_v4l2_streamoff,
  477. .vidioc_subscribe_event = uvc_v4l2_subscribe_event,
  478. .vidioc_unsubscribe_event = uvc_v4l2_unsubscribe_event,
  479. .vidioc_default = uvc_v4l2_ioctl_default,
  480. };
  481. /* --------------------------------------------------------------------------
  482. * V4L2
  483. */
  484. static int
  485. uvc_v4l2_open(struct file *file)
  486. {
  487. struct video_device *vdev = video_devdata(file);
  488. struct uvc_device *uvc = video_get_drvdata(vdev);
  489. struct uvc_file_handle *handle;
  490. handle = kzalloc(sizeof(*handle), GFP_KERNEL);
  491. if (handle == NULL)
  492. return -ENOMEM;
  493. v4l2_fh_init(&handle->vfh, vdev);
  494. v4l2_fh_add(&handle->vfh);
  495. handle->device = &uvc->video;
  496. file->private_data = &handle->vfh;
  497. return 0;
  498. }
  499. static int
  500. uvc_v4l2_release(struct file *file)
  501. {
  502. struct video_device *vdev = video_devdata(file);
  503. struct uvc_device *uvc = video_get_drvdata(vdev);
  504. struct uvc_file_handle *handle = to_uvc_file_handle(file->private_data);
  505. struct uvc_video *video = handle->device;
  506. mutex_lock(&video->mutex);
  507. if (handle->is_uvc_app_handle)
  508. uvc_v4l2_disable(uvc);
  509. mutex_unlock(&video->mutex);
  510. file->private_data = NULL;
  511. v4l2_fh_del(&handle->vfh);
  512. v4l2_fh_exit(&handle->vfh);
  513. kfree(handle);
  514. return 0;
  515. }
  516. static int
  517. uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
  518. {
  519. struct video_device *vdev = video_devdata(file);
  520. struct uvc_device *uvc = video_get_drvdata(vdev);
  521. return uvcg_queue_mmap(&uvc->video.queue, vma);
  522. }
  523. static __poll_t
  524. uvc_v4l2_poll(struct file *file, poll_table *wait)
  525. {
  526. struct video_device *vdev = video_devdata(file);
  527. struct uvc_device *uvc = video_get_drvdata(vdev);
  528. return uvcg_queue_poll(&uvc->video.queue, file, wait);
  529. }
  530. #ifndef CONFIG_MMU
  531. static unsigned long uvcg_v4l2_get_unmapped_area(struct file *file,
  532. unsigned long addr, unsigned long len, unsigned long pgoff,
  533. unsigned long flags)
  534. {
  535. struct video_device *vdev = video_devdata(file);
  536. struct uvc_device *uvc = video_get_drvdata(vdev);
  537. return uvcg_queue_get_unmapped_area(&uvc->video.queue, pgoff);
  538. }
  539. #endif
  540. const struct v4l2_file_operations uvc_v4l2_fops = {
  541. .owner = THIS_MODULE,
  542. .open = uvc_v4l2_open,
  543. .release = uvc_v4l2_release,
  544. .unlocked_ioctl = video_ioctl2,
  545. .mmap = uvc_v4l2_mmap,
  546. .poll = uvc_v4l2_poll,
  547. #ifndef CONFIG_MMU
  548. .get_unmapped_area = uvcg_v4l2_get_unmapped_area,
  549. #endif
  550. };