uvc_v4l2.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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(uvc->event_length, data->length);
  36. req->zero = data->length < uvc->event_length;
  37. req->dma = DMA_ADDR_INVALID;
  38. memcpy(req->buf, data->data, data->length);
  39. return usb_ep_queue(cdev->gadget->ep0, req, GFP_KERNEL);
  40. }
  41. /* --------------------------------------------------------------------------
  42. * V4L2
  43. */
  44. struct uvc_format
  45. {
  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_get_format(struct uvc_video *video, struct v4l2_format *fmt)
  55. {
  56. fmt->fmt.pix.pixelformat = video->fcc;
  57. fmt->fmt.pix.width = video->width;
  58. fmt->fmt.pix.height = video->height;
  59. fmt->fmt.pix.field = V4L2_FIELD_NONE;
  60. fmt->fmt.pix.bytesperline = video->bpp * video->width / 8;
  61. fmt->fmt.pix.sizeimage = video->imagesize;
  62. fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
  63. fmt->fmt.pix.priv = 0;
  64. return 0;
  65. }
  66. static int
  67. uvc_v4l2_set_format(struct uvc_video *video, struct v4l2_format *fmt)
  68. {
  69. struct uvc_format *format;
  70. unsigned int imagesize;
  71. unsigned int bpl;
  72. unsigned int i;
  73. for (i = 0; i < ARRAY_SIZE(uvc_formats); ++i) {
  74. format = &uvc_formats[i];
  75. if (format->fcc == fmt->fmt.pix.pixelformat)
  76. break;
  77. }
  78. if (i == ARRAY_SIZE(uvc_formats)) {
  79. printk(KERN_INFO "Unsupported format 0x%08x.\n",
  80. fmt->fmt.pix.pixelformat);
  81. return -EINVAL;
  82. }
  83. bpl = format->bpp * fmt->fmt.pix.width / 8;
  84. imagesize = bpl ? bpl * fmt->fmt.pix.height : fmt->fmt.pix.sizeimage;
  85. video->fcc = format->fcc;
  86. video->bpp = format->bpp;
  87. video->width = fmt->fmt.pix.width;
  88. video->height = fmt->fmt.pix.height;
  89. video->imagesize = imagesize;
  90. fmt->fmt.pix.field = V4L2_FIELD_NONE;
  91. fmt->fmt.pix.bytesperline = bpl;
  92. fmt->fmt.pix.sizeimage = imagesize;
  93. fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
  94. fmt->fmt.pix.priv = 0;
  95. return 0;
  96. }
  97. static int
  98. uvc_v4l2_open(struct file *file)
  99. {
  100. struct video_device *vdev = video_devdata(file);
  101. struct uvc_device *uvc = video_get_drvdata(vdev);
  102. struct uvc_file_handle *handle;
  103. handle = kzalloc(sizeof(*handle), GFP_KERNEL);
  104. if (handle == NULL)
  105. return -ENOMEM;
  106. v4l2_fh_init(&handle->vfh, vdev);
  107. v4l2_fh_add(&handle->vfh);
  108. handle->device = &uvc->video;
  109. file->private_data = &handle->vfh;
  110. uvc_function_connect(uvc);
  111. return 0;
  112. }
  113. static int
  114. uvc_v4l2_release(struct file *file)
  115. {
  116. struct video_device *vdev = video_devdata(file);
  117. struct uvc_device *uvc = video_get_drvdata(vdev);
  118. struct uvc_file_handle *handle = to_uvc_file_handle(file->private_data);
  119. struct uvc_video *video = handle->device;
  120. uvc_function_disconnect(uvc);
  121. uvc_video_enable(video, 0);
  122. mutex_lock(&video->queue.mutex);
  123. if (uvc_free_buffers(&video->queue) < 0)
  124. printk(KERN_ERR "uvc_v4l2_release: Unable to free "
  125. "buffers.\n");
  126. mutex_unlock(&video->queue.mutex);
  127. file->private_data = NULL;
  128. v4l2_fh_del(&handle->vfh);
  129. v4l2_fh_exit(&handle->vfh);
  130. kfree(handle);
  131. return 0;
  132. }
  133. static long
  134. uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
  135. {
  136. struct video_device *vdev = video_devdata(file);
  137. struct uvc_device *uvc = video_get_drvdata(vdev);
  138. struct uvc_file_handle *handle = to_uvc_file_handle(file->private_data);
  139. struct usb_composite_dev *cdev = uvc->func.config->cdev;
  140. struct uvc_video *video = &uvc->video;
  141. int ret = 0;
  142. switch (cmd) {
  143. /* Query capabilities */
  144. case VIDIOC_QUERYCAP:
  145. {
  146. struct v4l2_capability *cap = arg;
  147. memset(cap, 0, sizeof *cap);
  148. strncpy(cap->driver, "g_uvc", sizeof(cap->driver));
  149. strncpy(cap->card, cdev->gadget->name, sizeof(cap->card));
  150. strncpy(cap->bus_info, dev_name(&cdev->gadget->dev),
  151. sizeof cap->bus_info);
  152. cap->version = DRIVER_VERSION_NUMBER;
  153. cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
  154. break;
  155. }
  156. /* Get & Set format */
  157. case VIDIOC_G_FMT:
  158. {
  159. struct v4l2_format *fmt = arg;
  160. if (fmt->type != video->queue.type)
  161. return -EINVAL;
  162. return uvc_v4l2_get_format(video, fmt);
  163. }
  164. case VIDIOC_S_FMT:
  165. {
  166. struct v4l2_format *fmt = arg;
  167. if (fmt->type != video->queue.type)
  168. return -EINVAL;
  169. return uvc_v4l2_set_format(video, fmt);
  170. }
  171. /* Buffers & streaming */
  172. case VIDIOC_REQBUFS:
  173. {
  174. struct v4l2_requestbuffers *rb = arg;
  175. if (rb->type != video->queue.type ||
  176. rb->memory != V4L2_MEMORY_MMAP)
  177. return -EINVAL;
  178. ret = uvc_alloc_buffers(&video->queue, rb->count,
  179. video->imagesize);
  180. if (ret < 0)
  181. return ret;
  182. rb->count = ret;
  183. ret = 0;
  184. break;
  185. }
  186. case VIDIOC_QUERYBUF:
  187. {
  188. struct v4l2_buffer *buf = arg;
  189. if (buf->type != video->queue.type)
  190. return -EINVAL;
  191. return uvc_query_buffer(&video->queue, buf);
  192. }
  193. case VIDIOC_QBUF:
  194. if ((ret = uvc_queue_buffer(&video->queue, arg)) < 0)
  195. return ret;
  196. return uvc_video_pump(video);
  197. case VIDIOC_DQBUF:
  198. return uvc_dequeue_buffer(&video->queue, arg,
  199. file->f_flags & O_NONBLOCK);
  200. case VIDIOC_STREAMON:
  201. {
  202. int *type = arg;
  203. if (*type != video->queue.type)
  204. return -EINVAL;
  205. return uvc_video_enable(video, 1);
  206. }
  207. case VIDIOC_STREAMOFF:
  208. {
  209. int *type = arg;
  210. if (*type != video->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);
  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. struct uvc_file_handle *handle = to_uvc_file_handle(file->private_data);
  267. unsigned int mask = 0;
  268. poll_wait(file, &handle->vfh.wait, wait);
  269. if (v4l2_event_pending(&handle->vfh))
  270. mask |= POLLPRI;
  271. mask |= uvc_queue_poll(&uvc->video.queue, file, wait);
  272. return mask;
  273. }
  274. static struct v4l2_file_operations uvc_v4l2_fops = {
  275. .owner = THIS_MODULE,
  276. .open = uvc_v4l2_open,
  277. .release = uvc_v4l2_release,
  278. .ioctl = uvc_v4l2_ioctl,
  279. .mmap = uvc_v4l2_mmap,
  280. .poll = uvc_v4l2_poll,
  281. };