uvc_v4l2.c 8.3 KB

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