f_uvc.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  1. /*
  2. * uvc_gadget.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/module.h>
  14. #include <linux/device.h>
  15. #include <linux/errno.h>
  16. #include <linux/fs.h>
  17. #include <linux/list.h>
  18. #include <linux/mutex.h>
  19. #include <linux/string.h>
  20. #include <linux/usb/ch9.h>
  21. #include <linux/usb/gadget.h>
  22. #include <linux/usb/video.h>
  23. #include <linux/vmalloc.h>
  24. #include <linux/wait.h>
  25. #include <media/v4l2-dev.h>
  26. #include <media/v4l2-event.h>
  27. #include "u_uvc.h"
  28. #include "uvc.h"
  29. #include "uvc_configfs.h"
  30. #include "uvc_v4l2.h"
  31. #include "uvc_video.h"
  32. unsigned int uvc_gadget_trace_param;
  33. /* --------------------------------------------------------------------------
  34. * Function descriptors
  35. */
  36. /* string IDs are assigned dynamically */
  37. #define UVC_STRING_CONTROL_IDX 0
  38. #define UVC_STRING_STREAMING_IDX 1
  39. static struct usb_string uvc_en_us_strings[] = {
  40. [UVC_STRING_CONTROL_IDX].s = "UVC Camera",
  41. [UVC_STRING_STREAMING_IDX].s = "Video Streaming",
  42. { }
  43. };
  44. static struct usb_gadget_strings uvc_stringtab = {
  45. .language = 0x0409, /* en-us */
  46. .strings = uvc_en_us_strings,
  47. };
  48. static struct usb_gadget_strings *uvc_function_strings[] = {
  49. &uvc_stringtab,
  50. NULL,
  51. };
  52. #define UVC_INTF_VIDEO_CONTROL 0
  53. #define UVC_INTF_VIDEO_STREAMING 1
  54. #define UVC_STATUS_MAX_PACKET_SIZE 16 /* 16 bytes status */
  55. static struct usb_interface_assoc_descriptor uvc_iad = {
  56. .bLength = sizeof(uvc_iad),
  57. .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
  58. .bFirstInterface = 0,
  59. .bInterfaceCount = 2,
  60. .bFunctionClass = USB_CLASS_VIDEO,
  61. .bFunctionSubClass = UVC_SC_VIDEO_INTERFACE_COLLECTION,
  62. .bFunctionProtocol = 0x00,
  63. .iFunction = 0,
  64. };
  65. static struct usb_interface_descriptor uvc_control_intf = {
  66. .bLength = USB_DT_INTERFACE_SIZE,
  67. .bDescriptorType = USB_DT_INTERFACE,
  68. .bInterfaceNumber = UVC_INTF_VIDEO_CONTROL,
  69. .bAlternateSetting = 0,
  70. .bNumEndpoints = 1,
  71. .bInterfaceClass = USB_CLASS_VIDEO,
  72. .bInterfaceSubClass = UVC_SC_VIDEOCONTROL,
  73. .bInterfaceProtocol = 0x00,
  74. .iInterface = 0,
  75. };
  76. static struct usb_endpoint_descriptor uvc_control_ep = {
  77. .bLength = USB_DT_ENDPOINT_SIZE,
  78. .bDescriptorType = USB_DT_ENDPOINT,
  79. .bEndpointAddress = USB_DIR_IN,
  80. .bmAttributes = USB_ENDPOINT_XFER_INT,
  81. .wMaxPacketSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
  82. .bInterval = 8,
  83. };
  84. static struct usb_ss_ep_comp_descriptor uvc_ss_control_comp = {
  85. .bLength = sizeof(uvc_ss_control_comp),
  86. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  87. /* The following 3 values can be tweaked if necessary. */
  88. .bMaxBurst = 0,
  89. .bmAttributes = 0,
  90. .wBytesPerInterval = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
  91. };
  92. static struct uvc_control_endpoint_descriptor uvc_control_cs_ep = {
  93. .bLength = UVC_DT_CONTROL_ENDPOINT_SIZE,
  94. .bDescriptorType = USB_DT_CS_ENDPOINT,
  95. .bDescriptorSubType = UVC_EP_INTERRUPT,
  96. .wMaxTransferSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
  97. };
  98. static struct usb_interface_descriptor uvc_streaming_intf_alt0 = {
  99. .bLength = USB_DT_INTERFACE_SIZE,
  100. .bDescriptorType = USB_DT_INTERFACE,
  101. .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING,
  102. .bAlternateSetting = 0,
  103. .bNumEndpoints = 0,
  104. .bInterfaceClass = USB_CLASS_VIDEO,
  105. .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING,
  106. .bInterfaceProtocol = 0x00,
  107. .iInterface = 0,
  108. };
  109. static struct usb_interface_descriptor uvc_streaming_intf_alt1 = {
  110. .bLength = USB_DT_INTERFACE_SIZE,
  111. .bDescriptorType = USB_DT_INTERFACE,
  112. .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING,
  113. .bAlternateSetting = 1,
  114. .bNumEndpoints = 1,
  115. .bInterfaceClass = USB_CLASS_VIDEO,
  116. .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING,
  117. .bInterfaceProtocol = 0x00,
  118. .iInterface = 0,
  119. };
  120. static struct usb_endpoint_descriptor uvc_fs_streaming_ep = {
  121. .bLength = USB_DT_ENDPOINT_SIZE,
  122. .bDescriptorType = USB_DT_ENDPOINT,
  123. .bEndpointAddress = USB_DIR_IN,
  124. .bmAttributes = USB_ENDPOINT_SYNC_ASYNC
  125. | USB_ENDPOINT_XFER_ISOC,
  126. /* The wMaxPacketSize and bInterval values will be initialized from
  127. * module parameters.
  128. */
  129. };
  130. static struct usb_endpoint_descriptor uvc_hs_streaming_ep = {
  131. .bLength = USB_DT_ENDPOINT_SIZE,
  132. .bDescriptorType = USB_DT_ENDPOINT,
  133. .bEndpointAddress = USB_DIR_IN,
  134. .bmAttributes = USB_ENDPOINT_SYNC_ASYNC
  135. | USB_ENDPOINT_XFER_ISOC,
  136. /* The wMaxPacketSize and bInterval values will be initialized from
  137. * module parameters.
  138. */
  139. };
  140. static struct usb_endpoint_descriptor uvc_ss_streaming_ep = {
  141. .bLength = USB_DT_ENDPOINT_SIZE,
  142. .bDescriptorType = USB_DT_ENDPOINT,
  143. .bEndpointAddress = USB_DIR_IN,
  144. .bmAttributes = USB_ENDPOINT_SYNC_ASYNC
  145. | USB_ENDPOINT_XFER_ISOC,
  146. /* The wMaxPacketSize and bInterval values will be initialized from
  147. * module parameters.
  148. */
  149. };
  150. static struct usb_ss_ep_comp_descriptor uvc_ss_streaming_comp = {
  151. .bLength = sizeof(uvc_ss_streaming_comp),
  152. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  153. /* The bMaxBurst, bmAttributes and wBytesPerInterval values will be
  154. * initialized from module parameters.
  155. */
  156. };
  157. static const struct usb_descriptor_header * const uvc_fs_streaming[] = {
  158. (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
  159. (struct usb_descriptor_header *) &uvc_fs_streaming_ep,
  160. NULL,
  161. };
  162. static const struct usb_descriptor_header * const uvc_hs_streaming[] = {
  163. (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
  164. (struct usb_descriptor_header *) &uvc_hs_streaming_ep,
  165. NULL,
  166. };
  167. static const struct usb_descriptor_header * const uvc_ss_streaming[] = {
  168. (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
  169. (struct usb_descriptor_header *) &uvc_ss_streaming_ep,
  170. (struct usb_descriptor_header *) &uvc_ss_streaming_comp,
  171. NULL,
  172. };
  173. void uvc_set_trace_param(unsigned int trace)
  174. {
  175. uvc_gadget_trace_param = trace;
  176. }
  177. EXPORT_SYMBOL(uvc_set_trace_param);
  178. /* --------------------------------------------------------------------------
  179. * Control requests
  180. */
  181. static void
  182. uvc_function_ep0_complete(struct usb_ep *ep, struct usb_request *req)
  183. {
  184. struct uvc_device *uvc = req->context;
  185. struct v4l2_event v4l2_event;
  186. struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
  187. if (uvc->event_setup_out) {
  188. uvc->event_setup_out = 0;
  189. memset(&v4l2_event, 0, sizeof(v4l2_event));
  190. v4l2_event.type = UVC_EVENT_DATA;
  191. uvc_event->data.length = req->actual;
  192. memcpy(&uvc_event->data.data, req->buf, req->actual);
  193. v4l2_event_queue(uvc->vdev, &v4l2_event);
  194. }
  195. }
  196. static int
  197. uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
  198. {
  199. struct uvc_device *uvc = to_uvc(f);
  200. struct v4l2_event v4l2_event;
  201. struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
  202. /* printk(KERN_INFO "setup request %02x %02x value %04x index %04x %04x\n",
  203. * ctrl->bRequestType, ctrl->bRequest, le16_to_cpu(ctrl->wValue),
  204. * le16_to_cpu(ctrl->wIndex), le16_to_cpu(ctrl->wLength));
  205. */
  206. if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) {
  207. INFO(f->config->cdev, "invalid request type\n");
  208. return -EINVAL;
  209. }
  210. /* Stall too big requests. */
  211. if (le16_to_cpu(ctrl->wLength) > UVC_MAX_REQUEST_SIZE)
  212. return -EINVAL;
  213. /* Tell the complete callback to generate an event for the next request
  214. * that will be enqueued by UVCIOC_SEND_RESPONSE.
  215. */
  216. uvc->event_setup_out = !(ctrl->bRequestType & USB_DIR_IN);
  217. uvc->event_length = le16_to_cpu(ctrl->wLength);
  218. memset(&v4l2_event, 0, sizeof(v4l2_event));
  219. v4l2_event.type = UVC_EVENT_SETUP;
  220. memcpy(&uvc_event->req, ctrl, sizeof(uvc_event->req));
  221. v4l2_event_queue(uvc->vdev, &v4l2_event);
  222. return 0;
  223. }
  224. void uvc_function_setup_continue(struct uvc_device *uvc)
  225. {
  226. struct usb_composite_dev *cdev = uvc->func.config->cdev;
  227. usb_composite_setup_continue(cdev);
  228. }
  229. static int
  230. uvc_function_get_alt(struct usb_function *f, unsigned interface)
  231. {
  232. struct uvc_device *uvc = to_uvc(f);
  233. INFO(f->config->cdev, "uvc_function_get_alt(%u)\n", interface);
  234. if (interface == uvc->control_intf)
  235. return 0;
  236. else if (interface != uvc->streaming_intf)
  237. return -EINVAL;
  238. else
  239. return uvc->video.ep->driver_data ? 1 : 0;
  240. }
  241. static int
  242. uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
  243. {
  244. struct uvc_device *uvc = to_uvc(f);
  245. struct usb_composite_dev *cdev = f->config->cdev;
  246. struct v4l2_event v4l2_event;
  247. struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
  248. int ret;
  249. INFO(cdev, "uvc_function_set_alt(%u, %u)\n", interface, alt);
  250. if (interface == uvc->control_intf) {
  251. if (alt)
  252. return -EINVAL;
  253. if (uvc->control_ep->driver_data) {
  254. INFO(cdev, "reset UVC Control\n");
  255. usb_ep_disable(uvc->control_ep);
  256. uvc->control_ep->driver_data = NULL;
  257. }
  258. if (!uvc->control_ep->desc)
  259. if (config_ep_by_speed(cdev->gadget, f, uvc->control_ep))
  260. return -EINVAL;
  261. usb_ep_enable(uvc->control_ep);
  262. uvc->control_ep->driver_data = uvc;
  263. if (uvc->state == UVC_STATE_DISCONNECTED) {
  264. memset(&v4l2_event, 0, sizeof(v4l2_event));
  265. v4l2_event.type = UVC_EVENT_CONNECT;
  266. uvc_event->speed = cdev->gadget->speed;
  267. v4l2_event_queue(uvc->vdev, &v4l2_event);
  268. uvc->state = UVC_STATE_CONNECTED;
  269. }
  270. return 0;
  271. }
  272. if (interface != uvc->streaming_intf)
  273. return -EINVAL;
  274. /* TODO
  275. if (usb_endpoint_xfer_bulk(&uvc->desc.vs_ep))
  276. return alt ? -EINVAL : 0;
  277. */
  278. switch (alt) {
  279. case 0:
  280. if (uvc->state != UVC_STATE_STREAMING)
  281. return 0;
  282. if (uvc->video.ep) {
  283. usb_ep_disable(uvc->video.ep);
  284. uvc->video.ep->driver_data = NULL;
  285. }
  286. memset(&v4l2_event, 0, sizeof(v4l2_event));
  287. v4l2_event.type = UVC_EVENT_STREAMOFF;
  288. v4l2_event_queue(uvc->vdev, &v4l2_event);
  289. uvc->state = UVC_STATE_CONNECTED;
  290. return 0;
  291. case 1:
  292. if (uvc->state != UVC_STATE_CONNECTED)
  293. return 0;
  294. if (!uvc->video.ep)
  295. return -EINVAL;
  296. if (uvc->video.ep->driver_data) {
  297. INFO(cdev, "reset UVC\n");
  298. usb_ep_disable(uvc->video.ep);
  299. uvc->video.ep->driver_data = NULL;
  300. }
  301. ret = config_ep_by_speed(f->config->cdev->gadget,
  302. &(uvc->func), uvc->video.ep);
  303. if (ret)
  304. return ret;
  305. usb_ep_enable(uvc->video.ep);
  306. uvc->video.ep->driver_data = uvc;
  307. memset(&v4l2_event, 0, sizeof(v4l2_event));
  308. v4l2_event.type = UVC_EVENT_STREAMON;
  309. v4l2_event_queue(uvc->vdev, &v4l2_event);
  310. return USB_GADGET_DELAYED_STATUS;
  311. default:
  312. return -EINVAL;
  313. }
  314. }
  315. static void
  316. uvc_function_disable(struct usb_function *f)
  317. {
  318. struct uvc_device *uvc = to_uvc(f);
  319. struct v4l2_event v4l2_event;
  320. INFO(f->config->cdev, "uvc_function_disable\n");
  321. memset(&v4l2_event, 0, sizeof(v4l2_event));
  322. v4l2_event.type = UVC_EVENT_DISCONNECT;
  323. v4l2_event_queue(uvc->vdev, &v4l2_event);
  324. uvc->state = UVC_STATE_DISCONNECTED;
  325. if (uvc->video.ep->driver_data) {
  326. usb_ep_disable(uvc->video.ep);
  327. uvc->video.ep->driver_data = NULL;
  328. }
  329. if (uvc->control_ep->driver_data) {
  330. usb_ep_disable(uvc->control_ep);
  331. uvc->control_ep->driver_data = NULL;
  332. }
  333. }
  334. /* --------------------------------------------------------------------------
  335. * Connection / disconnection
  336. */
  337. void
  338. uvc_function_connect(struct uvc_device *uvc)
  339. {
  340. struct usb_composite_dev *cdev = uvc->func.config->cdev;
  341. int ret;
  342. if ((ret = usb_function_activate(&uvc->func)) < 0)
  343. INFO(cdev, "UVC connect failed with %d\n", ret);
  344. }
  345. void
  346. uvc_function_disconnect(struct uvc_device *uvc)
  347. {
  348. struct usb_composite_dev *cdev = uvc->func.config->cdev;
  349. int ret;
  350. if ((ret = usb_function_deactivate(&uvc->func)) < 0)
  351. INFO(cdev, "UVC disconnect failed with %d\n", ret);
  352. }
  353. /* --------------------------------------------------------------------------
  354. * USB probe and disconnect
  355. */
  356. static int
  357. uvc_register_video(struct uvc_device *uvc)
  358. {
  359. struct usb_composite_dev *cdev = uvc->func.config->cdev;
  360. struct video_device *video;
  361. /* TODO reference counting. */
  362. video = video_device_alloc();
  363. if (video == NULL)
  364. return -ENOMEM;
  365. video->v4l2_dev = &uvc->v4l2_dev;
  366. video->fops = &uvc_v4l2_fops;
  367. video->ioctl_ops = &uvc_v4l2_ioctl_ops;
  368. video->release = video_device_release;
  369. video->vfl_dir = VFL_DIR_TX;
  370. strlcpy(video->name, cdev->gadget->name, sizeof(video->name));
  371. uvc->vdev = video;
  372. video_set_drvdata(video, uvc);
  373. return video_register_device(video, VFL_TYPE_GRABBER, -1);
  374. }
  375. #define UVC_COPY_DESCRIPTOR(mem, dst, desc) \
  376. do { \
  377. memcpy(mem, desc, (desc)->bLength); \
  378. *(dst)++ = mem; \
  379. mem += (desc)->bLength; \
  380. } while (0);
  381. #define UVC_COPY_DESCRIPTORS(mem, dst, src) \
  382. do { \
  383. const struct usb_descriptor_header * const *__src; \
  384. for (__src = src; *__src; ++__src) { \
  385. memcpy(mem, *__src, (*__src)->bLength); \
  386. *dst++ = mem; \
  387. mem += (*__src)->bLength; \
  388. } \
  389. } while (0)
  390. static struct usb_descriptor_header **
  391. uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed)
  392. {
  393. struct uvc_input_header_descriptor *uvc_streaming_header;
  394. struct uvc_header_descriptor *uvc_control_header;
  395. const struct uvc_descriptor_header * const *uvc_control_desc;
  396. const struct uvc_descriptor_header * const *uvc_streaming_cls;
  397. const struct usb_descriptor_header * const *uvc_streaming_std;
  398. const struct usb_descriptor_header * const *src;
  399. struct usb_descriptor_header **dst;
  400. struct usb_descriptor_header **hdr;
  401. unsigned int control_size;
  402. unsigned int streaming_size;
  403. unsigned int n_desc;
  404. unsigned int bytes;
  405. void *mem;
  406. switch (speed) {
  407. case USB_SPEED_SUPER:
  408. uvc_control_desc = uvc->desc.ss_control;
  409. uvc_streaming_cls = uvc->desc.ss_streaming;
  410. uvc_streaming_std = uvc_ss_streaming;
  411. break;
  412. case USB_SPEED_HIGH:
  413. uvc_control_desc = uvc->desc.fs_control;
  414. uvc_streaming_cls = uvc->desc.hs_streaming;
  415. uvc_streaming_std = uvc_hs_streaming;
  416. break;
  417. case USB_SPEED_FULL:
  418. default:
  419. uvc_control_desc = uvc->desc.fs_control;
  420. uvc_streaming_cls = uvc->desc.fs_streaming;
  421. uvc_streaming_std = uvc_fs_streaming;
  422. break;
  423. }
  424. if (!uvc_control_desc || !uvc_streaming_cls)
  425. return ERR_PTR(-ENODEV);
  426. /* Descriptors layout
  427. *
  428. * uvc_iad
  429. * uvc_control_intf
  430. * Class-specific UVC control descriptors
  431. * uvc_control_ep
  432. * uvc_control_cs_ep
  433. * uvc_ss_control_comp (for SS only)
  434. * uvc_streaming_intf_alt0
  435. * Class-specific UVC streaming descriptors
  436. * uvc_{fs|hs}_streaming
  437. */
  438. /* Count descriptors and compute their size. */
  439. control_size = 0;
  440. streaming_size = 0;
  441. bytes = uvc_iad.bLength + uvc_control_intf.bLength
  442. + uvc_control_ep.bLength + uvc_control_cs_ep.bLength
  443. + uvc_streaming_intf_alt0.bLength;
  444. if (speed == USB_SPEED_SUPER) {
  445. bytes += uvc_ss_control_comp.bLength;
  446. n_desc = 6;
  447. } else {
  448. n_desc = 5;
  449. }
  450. for (src = (const struct usb_descriptor_header **)uvc_control_desc;
  451. *src; ++src) {
  452. control_size += (*src)->bLength;
  453. bytes += (*src)->bLength;
  454. n_desc++;
  455. }
  456. for (src = (const struct usb_descriptor_header **)uvc_streaming_cls;
  457. *src; ++src) {
  458. streaming_size += (*src)->bLength;
  459. bytes += (*src)->bLength;
  460. n_desc++;
  461. }
  462. for (src = uvc_streaming_std; *src; ++src) {
  463. bytes += (*src)->bLength;
  464. n_desc++;
  465. }
  466. mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL);
  467. if (mem == NULL)
  468. return NULL;
  469. hdr = mem;
  470. dst = mem;
  471. mem += (n_desc + 1) * sizeof(*src);
  472. /* Copy the descriptors. */
  473. UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad);
  474. UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf);
  475. uvc_control_header = mem;
  476. UVC_COPY_DESCRIPTORS(mem, dst,
  477. (const struct usb_descriptor_header **)uvc_control_desc);
  478. uvc_control_header->wTotalLength = cpu_to_le16(control_size);
  479. uvc_control_header->bInCollection = 1;
  480. uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf;
  481. UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_ep);
  482. if (speed == USB_SPEED_SUPER)
  483. UVC_COPY_DESCRIPTOR(mem, dst, &uvc_ss_control_comp);
  484. UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_cs_ep);
  485. UVC_COPY_DESCRIPTOR(mem, dst, &uvc_streaming_intf_alt0);
  486. uvc_streaming_header = mem;
  487. UVC_COPY_DESCRIPTORS(mem, dst,
  488. (const struct usb_descriptor_header**)uvc_streaming_cls);
  489. uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size);
  490. uvc_streaming_header->bEndpointAddress = uvc->video.ep->address;
  491. UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std);
  492. *dst = NULL;
  493. return hdr;
  494. }
  495. static int
  496. uvc_function_bind(struct usb_configuration *c, struct usb_function *f)
  497. {
  498. struct usb_composite_dev *cdev = c->cdev;
  499. struct uvc_device *uvc = to_uvc(f);
  500. struct usb_string *us;
  501. unsigned int max_packet_mult;
  502. unsigned int max_packet_size;
  503. struct usb_ep *ep;
  504. struct f_uvc_opts *opts;
  505. int ret = -EINVAL;
  506. INFO(cdev, "uvc_function_bind\n");
  507. opts = fi_to_f_uvc_opts(f->fi);
  508. /* Sanity check the streaming endpoint module parameters.
  509. */
  510. opts->streaming_interval = clamp(opts->streaming_interval, 1U, 16U);
  511. opts->streaming_maxpacket = clamp(opts->streaming_maxpacket, 1U, 3072U);
  512. opts->streaming_maxburst = min(opts->streaming_maxburst, 15U);
  513. /* Fill in the FS/HS/SS Video Streaming specific descriptors from the
  514. * module parameters.
  515. *
  516. * NOTE: We assume that the user knows what they are doing and won't
  517. * give parameters that their UDC doesn't support.
  518. */
  519. if (opts->streaming_maxpacket <= 1024) {
  520. max_packet_mult = 1;
  521. max_packet_size = opts->streaming_maxpacket;
  522. } else if (opts->streaming_maxpacket <= 2048) {
  523. max_packet_mult = 2;
  524. max_packet_size = opts->streaming_maxpacket / 2;
  525. } else {
  526. max_packet_mult = 3;
  527. max_packet_size = opts->streaming_maxpacket / 3;
  528. }
  529. uvc_fs_streaming_ep.wMaxPacketSize =
  530. cpu_to_le16(min(opts->streaming_maxpacket, 1023U));
  531. uvc_fs_streaming_ep.bInterval = opts->streaming_interval;
  532. uvc_hs_streaming_ep.wMaxPacketSize =
  533. cpu_to_le16(max_packet_size | ((max_packet_mult - 1) << 11));
  534. uvc_hs_streaming_ep.bInterval = opts->streaming_interval;
  535. uvc_ss_streaming_ep.wMaxPacketSize = cpu_to_le16(max_packet_size);
  536. uvc_ss_streaming_ep.bInterval = opts->streaming_interval;
  537. uvc_ss_streaming_comp.bmAttributes = max_packet_mult - 1;
  538. uvc_ss_streaming_comp.bMaxBurst = opts->streaming_maxburst;
  539. uvc_ss_streaming_comp.wBytesPerInterval =
  540. cpu_to_le16(max_packet_size * max_packet_mult *
  541. opts->streaming_maxburst);
  542. /* Allocate endpoints. */
  543. ep = usb_ep_autoconfig(cdev->gadget, &uvc_control_ep);
  544. if (!ep) {
  545. INFO(cdev, "Unable to allocate control EP\n");
  546. goto error;
  547. }
  548. uvc->control_ep = ep;
  549. ep->driver_data = uvc;
  550. if (gadget_is_superspeed(c->cdev->gadget))
  551. ep = usb_ep_autoconfig_ss(cdev->gadget, &uvc_ss_streaming_ep,
  552. &uvc_ss_streaming_comp);
  553. else if (gadget_is_dualspeed(cdev->gadget))
  554. ep = usb_ep_autoconfig(cdev->gadget, &uvc_hs_streaming_ep);
  555. else
  556. ep = usb_ep_autoconfig(cdev->gadget, &uvc_fs_streaming_ep);
  557. if (!ep) {
  558. INFO(cdev, "Unable to allocate streaming EP\n");
  559. goto error;
  560. }
  561. uvc->video.ep = ep;
  562. ep->driver_data = uvc;
  563. uvc_fs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
  564. uvc_hs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
  565. uvc_ss_streaming_ep.bEndpointAddress = uvc->video.ep->address;
  566. us = usb_gstrings_attach(cdev, uvc_function_strings,
  567. ARRAY_SIZE(uvc_en_us_strings));
  568. if (IS_ERR(us)) {
  569. ret = PTR_ERR(us);
  570. goto error;
  571. }
  572. uvc_iad.iFunction = us[UVC_STRING_CONTROL_IDX].id;
  573. uvc_control_intf.iInterface = us[UVC_STRING_CONTROL_IDX].id;
  574. ret = us[UVC_STRING_STREAMING_IDX].id;
  575. uvc_streaming_intf_alt0.iInterface = ret;
  576. uvc_streaming_intf_alt1.iInterface = ret;
  577. /* Allocate interface IDs. */
  578. if ((ret = usb_interface_id(c, f)) < 0)
  579. goto error;
  580. uvc_iad.bFirstInterface = ret;
  581. uvc_control_intf.bInterfaceNumber = ret;
  582. uvc->control_intf = ret;
  583. if ((ret = usb_interface_id(c, f)) < 0)
  584. goto error;
  585. uvc_streaming_intf_alt0.bInterfaceNumber = ret;
  586. uvc_streaming_intf_alt1.bInterfaceNumber = ret;
  587. uvc->streaming_intf = ret;
  588. /* Copy descriptors */
  589. f->fs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL);
  590. if (IS_ERR(f->fs_descriptors)) {
  591. ret = PTR_ERR(f->fs_descriptors);
  592. f->fs_descriptors = NULL;
  593. goto error;
  594. }
  595. if (gadget_is_dualspeed(cdev->gadget)) {
  596. f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH);
  597. if (IS_ERR(f->hs_descriptors)) {
  598. ret = PTR_ERR(f->hs_descriptors);
  599. f->hs_descriptors = NULL;
  600. goto error;
  601. }
  602. }
  603. if (gadget_is_superspeed(c->cdev->gadget)) {
  604. f->ss_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_SUPER);
  605. if (IS_ERR(f->ss_descriptors)) {
  606. ret = PTR_ERR(f->ss_descriptors);
  607. f->ss_descriptors = NULL;
  608. goto error;
  609. }
  610. }
  611. /* Preallocate control endpoint request. */
  612. uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
  613. uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL);
  614. if (uvc->control_req == NULL || uvc->control_buf == NULL) {
  615. ret = -ENOMEM;
  616. goto error;
  617. }
  618. uvc->control_req->buf = uvc->control_buf;
  619. uvc->control_req->complete = uvc_function_ep0_complete;
  620. uvc->control_req->context = uvc;
  621. /* Avoid letting this gadget enumerate until the userspace server is
  622. * active.
  623. */
  624. if ((ret = usb_function_deactivate(f)) < 0)
  625. goto error;
  626. if (v4l2_device_register(&cdev->gadget->dev, &uvc->v4l2_dev)) {
  627. printk(KERN_INFO "v4l2_device_register failed\n");
  628. goto error;
  629. }
  630. /* Initialise video. */
  631. ret = uvcg_video_init(&uvc->video);
  632. if (ret < 0)
  633. goto error;
  634. /* Register a V4L2 device. */
  635. ret = uvc_register_video(uvc);
  636. if (ret < 0) {
  637. printk(KERN_INFO "Unable to register video device\n");
  638. goto error;
  639. }
  640. return 0;
  641. error:
  642. v4l2_device_unregister(&uvc->v4l2_dev);
  643. if (uvc->vdev)
  644. video_device_release(uvc->vdev);
  645. if (uvc->control_ep)
  646. uvc->control_ep->driver_data = NULL;
  647. if (uvc->video.ep)
  648. uvc->video.ep->driver_data = NULL;
  649. if (uvc->control_req)
  650. usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
  651. kfree(uvc->control_buf);
  652. usb_free_all_descriptors(f);
  653. return ret;
  654. }
  655. /* --------------------------------------------------------------------------
  656. * USB gadget function
  657. */
  658. static void uvc_free_inst(struct usb_function_instance *f)
  659. {
  660. struct f_uvc_opts *opts = fi_to_f_uvc_opts(f);
  661. mutex_destroy(&opts->lock);
  662. kfree(opts);
  663. }
  664. static struct usb_function_instance *uvc_alloc_inst(void)
  665. {
  666. struct f_uvc_opts *opts;
  667. struct uvc_camera_terminal_descriptor *cd;
  668. struct uvc_processing_unit_descriptor *pd;
  669. struct uvc_output_terminal_descriptor *od;
  670. struct uvc_color_matching_descriptor *md;
  671. struct uvc_descriptor_header **ctl_cls;
  672. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  673. if (!opts)
  674. return ERR_PTR(-ENOMEM);
  675. opts->func_inst.free_func_inst = uvc_free_inst;
  676. mutex_init(&opts->lock);
  677. cd = &opts->uvc_camera_terminal;
  678. cd->bLength = UVC_DT_CAMERA_TERMINAL_SIZE(3);
  679. cd->bDescriptorType = USB_DT_CS_INTERFACE;
  680. cd->bDescriptorSubType = UVC_VC_INPUT_TERMINAL;
  681. cd->bTerminalID = 1;
  682. cd->wTerminalType = cpu_to_le16(0x0201);
  683. cd->bAssocTerminal = 0;
  684. cd->iTerminal = 0;
  685. cd->wObjectiveFocalLengthMin = cpu_to_le16(0);
  686. cd->wObjectiveFocalLengthMax = cpu_to_le16(0);
  687. cd->wOcularFocalLength = cpu_to_le16(0);
  688. cd->bControlSize = 3;
  689. cd->bmControls[0] = 2;
  690. cd->bmControls[1] = 0;
  691. cd->bmControls[2] = 0;
  692. pd = &opts->uvc_processing;
  693. pd->bLength = UVC_DT_PROCESSING_UNIT_SIZE(2);
  694. pd->bDescriptorType = USB_DT_CS_INTERFACE;
  695. pd->bDescriptorSubType = UVC_VC_PROCESSING_UNIT;
  696. pd->bUnitID = 2;
  697. pd->bSourceID = 1;
  698. pd->wMaxMultiplier = cpu_to_le16(16*1024);
  699. pd->bControlSize = 2;
  700. pd->bmControls[0] = 1;
  701. pd->bmControls[1] = 0;
  702. pd->iProcessing = 0;
  703. od = &opts->uvc_output_terminal;
  704. od->bLength = UVC_DT_OUTPUT_TERMINAL_SIZE;
  705. od->bDescriptorType = USB_DT_CS_INTERFACE;
  706. od->bDescriptorSubType = UVC_VC_OUTPUT_TERMINAL;
  707. od->bTerminalID = 3;
  708. od->wTerminalType = cpu_to_le16(0x0101);
  709. od->bAssocTerminal = 0;
  710. od->bSourceID = 2;
  711. od->iTerminal = 0;
  712. md = &opts->uvc_color_matching;
  713. md->bLength = UVC_DT_COLOR_MATCHING_SIZE;
  714. md->bDescriptorType = USB_DT_CS_INTERFACE;
  715. md->bDescriptorSubType = UVC_VS_COLORFORMAT;
  716. md->bColorPrimaries = 1;
  717. md->bTransferCharacteristics = 1;
  718. md->bMatrixCoefficients = 4;
  719. /* Prepare fs control class descriptors for configfs-based gadgets */
  720. ctl_cls = opts->uvc_fs_control_cls;
  721. ctl_cls[0] = NULL; /* assigned elsewhere by configfs */
  722. ctl_cls[1] = (struct uvc_descriptor_header *)cd;
  723. ctl_cls[2] = (struct uvc_descriptor_header *)pd;
  724. ctl_cls[3] = (struct uvc_descriptor_header *)od;
  725. ctl_cls[4] = NULL; /* NULL-terminate */
  726. opts->fs_control =
  727. (const struct uvc_descriptor_header * const *)ctl_cls;
  728. /* Prepare hs control class descriptors for configfs-based gadgets */
  729. ctl_cls = opts->uvc_ss_control_cls;
  730. ctl_cls[0] = NULL; /* assigned elsewhere by configfs */
  731. ctl_cls[1] = (struct uvc_descriptor_header *)cd;
  732. ctl_cls[2] = (struct uvc_descriptor_header *)pd;
  733. ctl_cls[3] = (struct uvc_descriptor_header *)od;
  734. ctl_cls[4] = NULL; /* NULL-terminate */
  735. opts->ss_control =
  736. (const struct uvc_descriptor_header * const *)ctl_cls;
  737. opts->streaming_interval = 1;
  738. opts->streaming_maxpacket = 1024;
  739. uvcg_attach_configfs(opts);
  740. return &opts->func_inst;
  741. }
  742. static void uvc_free(struct usb_function *f)
  743. {
  744. struct uvc_device *uvc = to_uvc(f);
  745. struct f_uvc_opts *opts = container_of(f->fi, struct f_uvc_opts,
  746. func_inst);
  747. --opts->refcnt;
  748. kfree(uvc);
  749. }
  750. static void uvc_unbind(struct usb_configuration *c, struct usb_function *f)
  751. {
  752. struct usb_composite_dev *cdev = c->cdev;
  753. struct uvc_device *uvc = to_uvc(f);
  754. INFO(cdev, "%s\n", __func__);
  755. video_unregister_device(uvc->vdev);
  756. v4l2_device_unregister(&uvc->v4l2_dev);
  757. uvc->control_ep->driver_data = NULL;
  758. uvc->video.ep->driver_data = NULL;
  759. usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
  760. kfree(uvc->control_buf);
  761. usb_free_all_descriptors(f);
  762. }
  763. static struct usb_function *uvc_alloc(struct usb_function_instance *fi)
  764. {
  765. struct uvc_device *uvc;
  766. struct f_uvc_opts *opts;
  767. struct uvc_descriptor_header **strm_cls;
  768. uvc = kzalloc(sizeof(*uvc), GFP_KERNEL);
  769. if (uvc == NULL)
  770. return ERR_PTR(-ENOMEM);
  771. uvc->state = UVC_STATE_DISCONNECTED;
  772. opts = fi_to_f_uvc_opts(fi);
  773. mutex_lock(&opts->lock);
  774. if (opts->uvc_fs_streaming_cls) {
  775. strm_cls = opts->uvc_fs_streaming_cls;
  776. opts->fs_streaming =
  777. (const struct uvc_descriptor_header * const *)strm_cls;
  778. }
  779. if (opts->uvc_hs_streaming_cls) {
  780. strm_cls = opts->uvc_hs_streaming_cls;
  781. opts->hs_streaming =
  782. (const struct uvc_descriptor_header * const *)strm_cls;
  783. }
  784. if (opts->uvc_ss_streaming_cls) {
  785. strm_cls = opts->uvc_ss_streaming_cls;
  786. opts->ss_streaming =
  787. (const struct uvc_descriptor_header * const *)strm_cls;
  788. }
  789. uvc->desc.fs_control = opts->fs_control;
  790. uvc->desc.ss_control = opts->ss_control;
  791. uvc->desc.fs_streaming = opts->fs_streaming;
  792. uvc->desc.hs_streaming = opts->hs_streaming;
  793. uvc->desc.ss_streaming = opts->ss_streaming;
  794. ++opts->refcnt;
  795. mutex_unlock(&opts->lock);
  796. /* Register the function. */
  797. uvc->func.name = "uvc";
  798. uvc->func.bind = uvc_function_bind;
  799. uvc->func.unbind = uvc_unbind;
  800. uvc->func.get_alt = uvc_function_get_alt;
  801. uvc->func.set_alt = uvc_function_set_alt;
  802. uvc->func.disable = uvc_function_disable;
  803. uvc->func.setup = uvc_function_setup;
  804. uvc->func.free_func = uvc_free;
  805. return &uvc->func;
  806. }
  807. DECLARE_USB_FUNCTION_INIT(uvc, uvc_alloc_inst, uvc_alloc);
  808. MODULE_LICENSE("GPL");
  809. MODULE_AUTHOR("Laurent Pinchart");