uvc_v4l2.c 9.3 KB

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