uvc_v4l2.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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/mutex.h>
  17. #include <linux/videodev2.h>
  18. #include <linux/vmalloc.h>
  19. #include <linux/wait.h>
  20. #include <media/v4l2-dev.h>
  21. #include <media/v4l2-event.h>
  22. #include <media/v4l2-ioctl.h>
  23. #include "uvc.h"
  24. #include "uvc_queue.h"
  25. /* --------------------------------------------------------------------------
  26. * Requests handling
  27. */
  28. static int
  29. uvc_send_response(struct uvc_device *uvc, struct uvc_request_data *data)
  30. {
  31. struct usb_composite_dev *cdev = uvc->func.config->cdev;
  32. struct usb_request *req = uvc->control_req;
  33. if (data->length < 0)
  34. return usb_ep_set_halt(cdev->gadget->ep0);
  35. req->length = min_t(unsigned int, uvc->event_length, data->length);
  36. req->zero = data->length < uvc->event_length;
  37. memcpy(req->buf, data->data, req->length);
  38. return usb_ep_queue(cdev->gadget->ep0, req, GFP_KERNEL);
  39. }
  40. /* --------------------------------------------------------------------------
  41. * V4L2
  42. */
  43. struct uvc_format
  44. {
  45. u8 bpp;
  46. u32 fcc;
  47. };
  48. static struct uvc_format uvc_formats[] = {
  49. { 16, V4L2_PIX_FMT_YUYV },
  50. { 0, V4L2_PIX_FMT_MJPEG },
  51. };
  52. static int
  53. uvc_v4l2_get_format(struct uvc_video *video, struct v4l2_format *fmt)
  54. {
  55. fmt->fmt.pix.pixelformat = video->fcc;
  56. fmt->fmt.pix.width = video->width;
  57. fmt->fmt.pix.height = video->height;
  58. fmt->fmt.pix.field = V4L2_FIELD_NONE;
  59. fmt->fmt.pix.bytesperline = video->bpp * video->width / 8;
  60. fmt->fmt.pix.sizeimage = video->imagesize;
  61. fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
  62. fmt->fmt.pix.priv = 0;
  63. return 0;
  64. }
  65. static int
  66. uvc_v4l2_set_format(struct uvc_video *video, struct v4l2_format *fmt)
  67. {
  68. struct uvc_format *format;
  69. unsigned int imagesize;
  70. unsigned int bpl;
  71. unsigned int i;
  72. for (i = 0; i < ARRAY_SIZE(uvc_formats); ++i) {
  73. format = &uvc_formats[i];
  74. if (format->fcc == fmt->fmt.pix.pixelformat)
  75. break;
  76. }
  77. if (i == ARRAY_SIZE(uvc_formats)) {
  78. printk(KERN_INFO "Unsupported format 0x%08x.\n",
  79. fmt->fmt.pix.pixelformat);
  80. return -EINVAL;
  81. }
  82. bpl = format->bpp * fmt->fmt.pix.width / 8;
  83. imagesize = bpl ? bpl * fmt->fmt.pix.height : fmt->fmt.pix.sizeimage;
  84. video->fcc = format->fcc;
  85. video->bpp = format->bpp;
  86. video->width = fmt->fmt.pix.width;
  87. video->height = fmt->fmt.pix.height;
  88. video->imagesize = imagesize;
  89. fmt->fmt.pix.field = V4L2_FIELD_NONE;
  90. fmt->fmt.pix.bytesperline = bpl;
  91. fmt->fmt.pix.sizeimage = imagesize;
  92. fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
  93. fmt->fmt.pix.priv = 0;
  94. return 0;
  95. }
  96. static int
  97. uvc_v4l2_open(struct file *file)
  98. {
  99. struct video_device *vdev = video_devdata(file);
  100. struct uvc_device *uvc = video_get_drvdata(vdev);
  101. struct uvc_file_handle *handle;
  102. handle = kzalloc(sizeof(*handle), GFP_KERNEL);
  103. if (handle == NULL)
  104. return -ENOMEM;
  105. v4l2_fh_init(&handle->vfh, vdev);
  106. v4l2_fh_add(&handle->vfh);
  107. handle->device = &uvc->video;
  108. file->private_data = &handle->vfh;
  109. uvc_function_connect(uvc);
  110. return 0;
  111. }
  112. static int
  113. uvc_v4l2_release(struct file *file)
  114. {
  115. struct video_device *vdev = video_devdata(file);
  116. struct uvc_device *uvc = video_get_drvdata(vdev);
  117. struct uvc_file_handle *handle = to_uvc_file_handle(file->private_data);
  118. struct uvc_video *video = handle->device;
  119. uvc_function_disconnect(uvc);
  120. uvc_video_enable(video, 0);
  121. uvc_free_buffers(&video->queue);
  122. file->private_data = NULL;
  123. v4l2_fh_del(&handle->vfh);
  124. v4l2_fh_exit(&handle->vfh);
  125. kfree(handle);
  126. return 0;
  127. }
  128. static long
  129. uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
  130. {
  131. struct video_device *vdev = video_devdata(file);
  132. struct uvc_device *uvc = video_get_drvdata(vdev);
  133. struct uvc_file_handle *handle = to_uvc_file_handle(file->private_data);
  134. struct usb_composite_dev *cdev = uvc->func.config->cdev;
  135. struct uvc_video *video = &uvc->video;
  136. int ret = 0;
  137. switch (cmd) {
  138. /* Query capabilities */
  139. case VIDIOC_QUERYCAP:
  140. {
  141. struct v4l2_capability *cap = arg;
  142. memset(cap, 0, sizeof *cap);
  143. strlcpy(cap->driver, "g_uvc", sizeof(cap->driver));
  144. strlcpy(cap->card, cdev->gadget->name, sizeof(cap->card));
  145. strlcpy(cap->bus_info, dev_name(&cdev->gadget->dev),
  146. sizeof cap->bus_info);
  147. cap->version = DRIVER_VERSION_NUMBER;
  148. cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
  149. break;
  150. }
  151. /* Get & Set format */
  152. case VIDIOC_G_FMT:
  153. {
  154. struct v4l2_format *fmt = arg;
  155. if (fmt->type != video->queue.queue.type)
  156. return -EINVAL;
  157. return uvc_v4l2_get_format(video, fmt);
  158. }
  159. case VIDIOC_S_FMT:
  160. {
  161. struct v4l2_format *fmt = arg;
  162. if (fmt->type != video->queue.queue.type)
  163. return -EINVAL;
  164. return uvc_v4l2_set_format(video, fmt);
  165. }
  166. /* Buffers & streaming */
  167. case VIDIOC_REQBUFS:
  168. {
  169. struct v4l2_requestbuffers *rb = arg;
  170. if (rb->type != video->queue.queue.type)
  171. return -EINVAL;
  172. ret = uvc_alloc_buffers(&video->queue, rb);
  173. if (ret < 0)
  174. return ret;
  175. ret = 0;
  176. break;
  177. }
  178. case VIDIOC_QUERYBUF:
  179. {
  180. struct v4l2_buffer *buf = arg;
  181. return uvc_query_buffer(&video->queue, buf);
  182. }
  183. case VIDIOC_QBUF:
  184. if ((ret = uvc_queue_buffer(&video->queue, arg)) < 0)
  185. return ret;
  186. return uvc_video_pump(video);
  187. case VIDIOC_DQBUF:
  188. return uvc_dequeue_buffer(&video->queue, arg,
  189. file->f_flags & O_NONBLOCK);
  190. case VIDIOC_STREAMON:
  191. {
  192. int *type = arg;
  193. if (*type != video->queue.queue.type)
  194. return -EINVAL;
  195. /* Enable UVC video. */
  196. ret = uvc_video_enable(video, 1);
  197. if (ret < 0)
  198. return ret;
  199. /*
  200. * Complete the alternate setting selection setup phase now that
  201. * userspace is ready to provide video frames.
  202. */
  203. uvc_function_setup_continue(uvc);
  204. uvc->state = UVC_STATE_STREAMING;
  205. return 0;
  206. }
  207. case VIDIOC_STREAMOFF:
  208. {
  209. int *type = arg;
  210. if (*type != video->queue.queue.type)
  211. return -EINVAL;
  212. return uvc_video_enable(video, 0);
  213. }
  214. /* Events */
  215. case VIDIOC_DQEVENT:
  216. {
  217. struct v4l2_event *event = arg;
  218. ret = v4l2_event_dequeue(&handle->vfh, event,
  219. file->f_flags & O_NONBLOCK);
  220. if (ret == 0 && event->type == UVC_EVENT_SETUP) {
  221. struct uvc_event *uvc_event = (void *)&event->u.data;
  222. /* Tell the complete callback to generate an event for
  223. * the next request that will be enqueued by
  224. * uvc_event_write.
  225. */
  226. uvc->event_setup_out =
  227. !(uvc_event->req.bRequestType & USB_DIR_IN);
  228. uvc->event_length = uvc_event->req.wLength;
  229. }
  230. return ret;
  231. }
  232. case VIDIOC_SUBSCRIBE_EVENT:
  233. {
  234. struct v4l2_event_subscription *sub = arg;
  235. if (sub->type < UVC_EVENT_FIRST || sub->type > UVC_EVENT_LAST)
  236. return -EINVAL;
  237. return v4l2_event_subscribe(&handle->vfh, arg, 2, NULL);
  238. }
  239. case VIDIOC_UNSUBSCRIBE_EVENT:
  240. return v4l2_event_unsubscribe(&handle->vfh, arg);
  241. case UVCIOC_SEND_RESPONSE:
  242. ret = uvc_send_response(uvc, arg);
  243. break;
  244. default:
  245. return -ENOIOCTLCMD;
  246. }
  247. return ret;
  248. }
  249. static long
  250. uvc_v4l2_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  251. {
  252. return video_usercopy(file, cmd, arg, uvc_v4l2_do_ioctl);
  253. }
  254. static int
  255. uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
  256. {
  257. struct video_device *vdev = video_devdata(file);
  258. struct uvc_device *uvc = video_get_drvdata(vdev);
  259. return uvc_queue_mmap(&uvc->video.queue, vma);
  260. }
  261. static unsigned int
  262. uvc_v4l2_poll(struct file *file, poll_table *wait)
  263. {
  264. struct video_device *vdev = video_devdata(file);
  265. struct uvc_device *uvc = video_get_drvdata(vdev);
  266. return uvc_queue_poll(&uvc->video.queue, file, wait);
  267. }
  268. #ifndef CONFIG_MMU
  269. static unsigned long uvc_v4l2_get_unmapped_area(struct file *file,
  270. unsigned long addr, unsigned long len, unsigned long pgoff,
  271. unsigned long flags)
  272. {
  273. struct video_device *vdev = video_devdata(file);
  274. struct uvc_device *uvc = video_get_drvdata(vdev);
  275. return uvc_queue_get_unmapped_area(&uvc->video.queue, pgoff);
  276. }
  277. #endif
  278. static struct v4l2_file_operations uvc_v4l2_fops = {
  279. .owner = THIS_MODULE,
  280. .open = uvc_v4l2_open,
  281. .release = uvc_v4l2_release,
  282. .ioctl = uvc_v4l2_ioctl,
  283. .mmap = uvc_v4l2_mmap,
  284. .poll = uvc_v4l2_poll,
  285. #ifndef CONFIG_MMU
  286. .get_unmapped_area = uvc_v4l2_get_unmapped_area,
  287. #endif
  288. };