uvc_v4l2.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. mutex_lock(&video->queue.mutex);
  122. if (uvc_free_buffers(&video->queue) < 0)
  123. printk(KERN_ERR "uvc_v4l2_release: Unable to free "
  124. "buffers.\n");
  125. mutex_unlock(&video->queue.mutex);
  126. file->private_data = NULL;
  127. v4l2_fh_del(&handle->vfh);
  128. v4l2_fh_exit(&handle->vfh);
  129. kfree(handle);
  130. return 0;
  131. }
  132. static long
  133. uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
  134. {
  135. struct video_device *vdev = video_devdata(file);
  136. struct uvc_device *uvc = video_get_drvdata(vdev);
  137. struct uvc_file_handle *handle = to_uvc_file_handle(file->private_data);
  138. struct usb_composite_dev *cdev = uvc->func.config->cdev;
  139. struct uvc_video *video = &uvc->video;
  140. int ret = 0;
  141. switch (cmd) {
  142. /* Query capabilities */
  143. case VIDIOC_QUERYCAP:
  144. {
  145. struct v4l2_capability *cap = arg;
  146. memset(cap, 0, sizeof *cap);
  147. strlcpy(cap->driver, "g_uvc", sizeof(cap->driver));
  148. strlcpy(cap->card, cdev->gadget->name, sizeof(cap->card));
  149. strlcpy(cap->bus_info, dev_name(&cdev->gadget->dev),
  150. sizeof cap->bus_info);
  151. cap->version = DRIVER_VERSION_NUMBER;
  152. cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
  153. break;
  154. }
  155. /* Get & Set format */
  156. case VIDIOC_G_FMT:
  157. {
  158. struct v4l2_format *fmt = arg;
  159. if (fmt->type != video->queue.type)
  160. return -EINVAL;
  161. return uvc_v4l2_get_format(video, fmt);
  162. }
  163. case VIDIOC_S_FMT:
  164. {
  165. struct v4l2_format *fmt = arg;
  166. if (fmt->type != video->queue.type)
  167. return -EINVAL;
  168. return uvc_v4l2_set_format(video, fmt);
  169. }
  170. /* Buffers & streaming */
  171. case VIDIOC_REQBUFS:
  172. {
  173. struct v4l2_requestbuffers *rb = arg;
  174. if (rb->type != video->queue.type ||
  175. rb->memory != V4L2_MEMORY_MMAP)
  176. return -EINVAL;
  177. ret = uvc_alloc_buffers(&video->queue, rb->count,
  178. video->imagesize);
  179. if (ret < 0)
  180. return ret;
  181. rb->count = ret;
  182. ret = 0;
  183. break;
  184. }
  185. case VIDIOC_QUERYBUF:
  186. {
  187. struct v4l2_buffer *buf = arg;
  188. if (buf->type != video->queue.type)
  189. return -EINVAL;
  190. return uvc_query_buffer(&video->queue, buf);
  191. }
  192. case VIDIOC_QBUF:
  193. if ((ret = uvc_queue_buffer(&video->queue, arg)) < 0)
  194. return ret;
  195. return uvc_video_pump(video);
  196. case VIDIOC_DQBUF:
  197. return uvc_dequeue_buffer(&video->queue, arg,
  198. file->f_flags & O_NONBLOCK);
  199. case VIDIOC_STREAMON:
  200. {
  201. int *type = arg;
  202. if (*type != video->queue.type)
  203. return -EINVAL;
  204. /* Enable UVC video. */
  205. ret = uvc_video_enable(video, 1);
  206. if (ret < 0)
  207. return ret;
  208. /*
  209. * Complete the alternate setting selection setup phase now that
  210. * userspace is ready to provide video frames.
  211. */
  212. uvc_function_setup_continue(uvc);
  213. uvc->state = UVC_STATE_STREAMING;
  214. return 0;
  215. }
  216. case VIDIOC_STREAMOFF:
  217. {
  218. int *type = arg;
  219. if (*type != video->queue.type)
  220. return -EINVAL;
  221. return uvc_video_enable(video, 0);
  222. }
  223. /* Events */
  224. case VIDIOC_DQEVENT:
  225. {
  226. struct v4l2_event *event = arg;
  227. ret = v4l2_event_dequeue(&handle->vfh, event,
  228. file->f_flags & O_NONBLOCK);
  229. if (ret == 0 && event->type == UVC_EVENT_SETUP) {
  230. struct uvc_event *uvc_event = (void *)&event->u.data;
  231. /* Tell the complete callback to generate an event for
  232. * the next request that will be enqueued by
  233. * uvc_event_write.
  234. */
  235. uvc->event_setup_out =
  236. !(uvc_event->req.bRequestType & USB_DIR_IN);
  237. uvc->event_length = uvc_event->req.wLength;
  238. }
  239. return ret;
  240. }
  241. case VIDIOC_SUBSCRIBE_EVENT:
  242. {
  243. struct v4l2_event_subscription *sub = arg;
  244. if (sub->type < UVC_EVENT_FIRST || sub->type > UVC_EVENT_LAST)
  245. return -EINVAL;
  246. return v4l2_event_subscribe(&handle->vfh, arg, 2, NULL);
  247. }
  248. case VIDIOC_UNSUBSCRIBE_EVENT:
  249. return v4l2_event_unsubscribe(&handle->vfh, arg);
  250. case UVCIOC_SEND_RESPONSE:
  251. ret = uvc_send_response(uvc, arg);
  252. break;
  253. default:
  254. return -ENOIOCTLCMD;
  255. }
  256. return ret;
  257. }
  258. static long
  259. uvc_v4l2_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  260. {
  261. return video_usercopy(file, cmd, arg, uvc_v4l2_do_ioctl);
  262. }
  263. static int
  264. uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
  265. {
  266. struct video_device *vdev = video_devdata(file);
  267. struct uvc_device *uvc = video_get_drvdata(vdev);
  268. return uvc_queue_mmap(&uvc->video.queue, vma);
  269. }
  270. static unsigned int
  271. uvc_v4l2_poll(struct file *file, poll_table *wait)
  272. {
  273. struct video_device *vdev = video_devdata(file);
  274. struct uvc_device *uvc = video_get_drvdata(vdev);
  275. struct uvc_file_handle *handle = to_uvc_file_handle(file->private_data);
  276. unsigned int mask = 0;
  277. poll_wait(file, &handle->vfh.wait, wait);
  278. if (v4l2_event_pending(&handle->vfh))
  279. mask |= POLLPRI;
  280. mask |= uvc_queue_poll(&uvc->video.queue, file, wait);
  281. return mask;
  282. }
  283. static struct v4l2_file_operations uvc_v4l2_fops = {
  284. .owner = THIS_MODULE,
  285. .open = uvc_v4l2_open,
  286. .release = uvc_v4l2_release,
  287. .ioctl = uvc_v4l2_ioctl,
  288. .mmap = uvc_v4l2_mmap,
  289. .poll = uvc_v4l2_poll,
  290. };