uvc_v4l2.c 9.3 KB

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