uvc_v4l2.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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/kernel.h>
  9. #include <linux/device.h>
  10. #include <linux/errno.h>
  11. #include <linux/list.h>
  12. #include <linux/videodev2.h>
  13. #include <linux/vmalloc.h>
  14. #include <linux/wait.h>
  15. #include <media/v4l2-dev.h>
  16. #include <media/v4l2-event.h>
  17. #include <media/v4l2-ioctl.h>
  18. #include "f_uvc.h"
  19. #include "uvc.h"
  20. #include "uvc_queue.h"
  21. #include "uvc_video.h"
  22. #include "uvc_v4l2.h"
  23. /* --------------------------------------------------------------------------
  24. * Requests handling
  25. */
  26. static int
  27. uvc_send_response(struct uvc_device *uvc, struct uvc_request_data *data)
  28. {
  29. struct usb_composite_dev *cdev = uvc->func.config->cdev;
  30. struct usb_request *req = uvc->control_req;
  31. if (data->length < 0)
  32. return usb_ep_set_halt(cdev->gadget->ep0);
  33. req->length = min_t(unsigned int, uvc->event_length, data->length);
  34. req->zero = data->length < uvc->event_length;
  35. memcpy(req->buf, data->data, req->length);
  36. return usb_ep_queue(cdev->gadget->ep0, req, GFP_KERNEL);
  37. }
  38. /* --------------------------------------------------------------------------
  39. * V4L2 ioctls
  40. */
  41. struct uvc_format {
  42. u8 bpp;
  43. u32 fcc;
  44. };
  45. static struct uvc_format uvc_formats[] = {
  46. { 16, V4L2_PIX_FMT_YUYV },
  47. { 0, V4L2_PIX_FMT_MJPEG },
  48. };
  49. static int
  50. uvc_v4l2_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
  51. {
  52. struct video_device *vdev = video_devdata(file);
  53. struct uvc_device *uvc = video_get_drvdata(vdev);
  54. struct usb_composite_dev *cdev = uvc->func.config->cdev;
  55. strlcpy(cap->driver, "g_uvc", sizeof(cap->driver));
  56. strlcpy(cap->card, cdev->gadget->name, sizeof(cap->card));
  57. strlcpy(cap->bus_info, dev_name(&cdev->gadget->dev),
  58. sizeof(cap->bus_info));
  59. cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
  60. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  61. return 0;
  62. }
  63. static int
  64. uvc_v4l2_get_format(struct file *file, void *fh, struct v4l2_format *fmt)
  65. {
  66. struct video_device *vdev = video_devdata(file);
  67. struct uvc_device *uvc = video_get_drvdata(vdev);
  68. struct uvc_video *video = &uvc->video;
  69. fmt->fmt.pix.pixelformat = video->fcc;
  70. fmt->fmt.pix.width = video->width;
  71. fmt->fmt.pix.height = video->height;
  72. fmt->fmt.pix.field = V4L2_FIELD_NONE;
  73. fmt->fmt.pix.bytesperline = video->bpp * video->width / 8;
  74. fmt->fmt.pix.sizeimage = video->imagesize;
  75. fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
  76. fmt->fmt.pix.priv = 0;
  77. return 0;
  78. }
  79. static int
  80. uvc_v4l2_set_format(struct file *file, void *fh, struct v4l2_format *fmt)
  81. {
  82. struct video_device *vdev = video_devdata(file);
  83. struct uvc_device *uvc = video_get_drvdata(vdev);
  84. struct uvc_video *video = &uvc->video;
  85. struct uvc_format *format;
  86. unsigned int imagesize;
  87. unsigned int bpl;
  88. unsigned int i;
  89. for (i = 0; i < ARRAY_SIZE(uvc_formats); ++i) {
  90. format = &uvc_formats[i];
  91. if (format->fcc == fmt->fmt.pix.pixelformat)
  92. break;
  93. }
  94. if (i == ARRAY_SIZE(uvc_formats)) {
  95. printk(KERN_INFO "Unsupported format 0x%08x.\n",
  96. fmt->fmt.pix.pixelformat);
  97. return -EINVAL;
  98. }
  99. bpl = format->bpp * fmt->fmt.pix.width / 8;
  100. imagesize = bpl ? bpl * fmt->fmt.pix.height : fmt->fmt.pix.sizeimage;
  101. video->fcc = format->fcc;
  102. video->bpp = format->bpp;
  103. video->width = fmt->fmt.pix.width;
  104. video->height = fmt->fmt.pix.height;
  105. video->imagesize = imagesize;
  106. fmt->fmt.pix.field = V4L2_FIELD_NONE;
  107. fmt->fmt.pix.bytesperline = bpl;
  108. fmt->fmt.pix.sizeimage = imagesize;
  109. fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
  110. fmt->fmt.pix.priv = 0;
  111. return 0;
  112. }
  113. static int
  114. uvc_v4l2_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *b)
  115. {
  116. struct video_device *vdev = video_devdata(file);
  117. struct uvc_device *uvc = video_get_drvdata(vdev);
  118. struct uvc_video *video = &uvc->video;
  119. if (b->type != video->queue.queue.type)
  120. return -EINVAL;
  121. return uvcg_alloc_buffers(&video->queue, b);
  122. }
  123. static int
  124. uvc_v4l2_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
  125. {
  126. struct video_device *vdev = video_devdata(file);
  127. struct uvc_device *uvc = video_get_drvdata(vdev);
  128. struct uvc_video *video = &uvc->video;
  129. return uvcg_query_buffer(&video->queue, b);
  130. }
  131. static int
  132. uvc_v4l2_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
  133. {
  134. struct video_device *vdev = video_devdata(file);
  135. struct uvc_device *uvc = video_get_drvdata(vdev);
  136. struct uvc_video *video = &uvc->video;
  137. int ret;
  138. ret = uvcg_queue_buffer(&video->queue, b);
  139. if (ret < 0)
  140. return ret;
  141. return uvcg_video_pump(video);
  142. }
  143. static int
  144. uvc_v4l2_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
  145. {
  146. struct video_device *vdev = video_devdata(file);
  147. struct uvc_device *uvc = video_get_drvdata(vdev);
  148. struct uvc_video *video = &uvc->video;
  149. return uvcg_dequeue_buffer(&video->queue, b, file->f_flags & O_NONBLOCK);
  150. }
  151. static int
  152. uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
  153. {
  154. struct video_device *vdev = video_devdata(file);
  155. struct uvc_device *uvc = video_get_drvdata(vdev);
  156. struct uvc_video *video = &uvc->video;
  157. int ret;
  158. if (type != video->queue.queue.type)
  159. return -EINVAL;
  160. /* Enable UVC video. */
  161. ret = uvcg_video_enable(video, 1);
  162. if (ret < 0)
  163. return ret;
  164. /*
  165. * Complete the alternate setting selection setup phase now that
  166. * userspace is ready to provide video frames.
  167. */
  168. uvc_function_setup_continue(uvc);
  169. uvc->state = UVC_STATE_STREAMING;
  170. return 0;
  171. }
  172. static int
  173. uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
  174. {
  175. struct video_device *vdev = video_devdata(file);
  176. struct uvc_device *uvc = video_get_drvdata(vdev);
  177. struct uvc_video *video = &uvc->video;
  178. if (type != video->queue.queue.type)
  179. return -EINVAL;
  180. return uvcg_video_enable(video, 0);
  181. }
  182. static int
  183. uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
  184. const struct v4l2_event_subscription *sub)
  185. {
  186. if (sub->type < UVC_EVENT_FIRST || sub->type > UVC_EVENT_LAST)
  187. return -EINVAL;
  188. return v4l2_event_subscribe(fh, sub, 2, NULL);
  189. }
  190. static int
  191. uvc_v4l2_unsubscribe_event(struct v4l2_fh *fh,
  192. const struct v4l2_event_subscription *sub)
  193. {
  194. return v4l2_event_unsubscribe(fh, sub);
  195. }
  196. static long
  197. uvc_v4l2_ioctl_default(struct file *file, void *fh, bool valid_prio,
  198. unsigned int cmd, void *arg)
  199. {
  200. struct video_device *vdev = video_devdata(file);
  201. struct uvc_device *uvc = video_get_drvdata(vdev);
  202. switch (cmd) {
  203. case UVCIOC_SEND_RESPONSE:
  204. return uvc_send_response(uvc, arg);
  205. default:
  206. return -ENOIOCTLCMD;
  207. }
  208. }
  209. const struct v4l2_ioctl_ops uvc_v4l2_ioctl_ops = {
  210. .vidioc_querycap = uvc_v4l2_querycap,
  211. .vidioc_g_fmt_vid_out = uvc_v4l2_get_format,
  212. .vidioc_s_fmt_vid_out = uvc_v4l2_set_format,
  213. .vidioc_reqbufs = uvc_v4l2_reqbufs,
  214. .vidioc_querybuf = uvc_v4l2_querybuf,
  215. .vidioc_qbuf = uvc_v4l2_qbuf,
  216. .vidioc_dqbuf = uvc_v4l2_dqbuf,
  217. .vidioc_streamon = uvc_v4l2_streamon,
  218. .vidioc_streamoff = uvc_v4l2_streamoff,
  219. .vidioc_subscribe_event = uvc_v4l2_subscribe_event,
  220. .vidioc_unsubscribe_event = uvc_v4l2_unsubscribe_event,
  221. .vidioc_default = uvc_v4l2_ioctl_default,
  222. };
  223. /* --------------------------------------------------------------------------
  224. * V4L2
  225. */
  226. static int
  227. uvc_v4l2_open(struct file *file)
  228. {
  229. struct video_device *vdev = video_devdata(file);
  230. struct uvc_device *uvc = video_get_drvdata(vdev);
  231. struct uvc_file_handle *handle;
  232. handle = kzalloc(sizeof(*handle), GFP_KERNEL);
  233. if (handle == NULL)
  234. return -ENOMEM;
  235. v4l2_fh_init(&handle->vfh, vdev);
  236. v4l2_fh_add(&handle->vfh);
  237. handle->device = &uvc->video;
  238. file->private_data = &handle->vfh;
  239. uvc_function_connect(uvc);
  240. return 0;
  241. }
  242. static int
  243. uvc_v4l2_release(struct file *file)
  244. {
  245. struct video_device *vdev = video_devdata(file);
  246. struct uvc_device *uvc = video_get_drvdata(vdev);
  247. struct uvc_file_handle *handle = to_uvc_file_handle(file->private_data);
  248. struct uvc_video *video = handle->device;
  249. uvc_function_disconnect(uvc);
  250. mutex_lock(&video->mutex);
  251. uvcg_video_enable(video, 0);
  252. uvcg_free_buffers(&video->queue);
  253. mutex_unlock(&video->mutex);
  254. file->private_data = NULL;
  255. v4l2_fh_del(&handle->vfh);
  256. v4l2_fh_exit(&handle->vfh);
  257. kfree(handle);
  258. return 0;
  259. }
  260. static int
  261. uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
  262. {
  263. struct video_device *vdev = video_devdata(file);
  264. struct uvc_device *uvc = video_get_drvdata(vdev);
  265. return uvcg_queue_mmap(&uvc->video.queue, vma);
  266. }
  267. static __poll_t
  268. uvc_v4l2_poll(struct file *file, poll_table *wait)
  269. {
  270. struct video_device *vdev = video_devdata(file);
  271. struct uvc_device *uvc = video_get_drvdata(vdev);
  272. return uvcg_queue_poll(&uvc->video.queue, file, wait);
  273. }
  274. #ifndef CONFIG_MMU
  275. static unsigned long uvcg_v4l2_get_unmapped_area(struct file *file,
  276. unsigned long addr, unsigned long len, unsigned long pgoff,
  277. unsigned long flags)
  278. {
  279. struct video_device *vdev = video_devdata(file);
  280. struct uvc_device *uvc = video_get_drvdata(vdev);
  281. return uvcg_queue_get_unmapped_area(&uvc->video.queue, pgoff);
  282. }
  283. #endif
  284. const struct v4l2_file_operations uvc_v4l2_fops = {
  285. .owner = THIS_MODULE,
  286. .open = uvc_v4l2_open,
  287. .release = uvc_v4l2_release,
  288. .unlocked_ioctl = video_ioctl2,
  289. .mmap = uvc_v4l2_mmap,
  290. .poll = uvc_v4l2_poll,
  291. #ifndef CONFIG_MMU
  292. .get_unmapped_area = uvcg_v4l2_get_unmapped_area,
  293. #endif
  294. };