uvc_v4l2.c 8.0 KB

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