f_uvc.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  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. /* TODO reference counting. */
  361. uvc->vdev.v4l2_dev = &uvc->v4l2_dev;
  362. uvc->vdev.fops = &uvc_v4l2_fops;
  363. uvc->vdev.ioctl_ops = &uvc_v4l2_ioctl_ops;
  364. uvc->vdev.release = video_device_release_empty;
  365. uvc->vdev.vfl_dir = VFL_DIR_TX;
  366. uvc->vdev.lock = &uvc->video.mutex;
  367. strlcpy(uvc->vdev.name, cdev->gadget->name, sizeof(uvc->vdev.name));
  368. video_set_drvdata(&uvc->vdev, uvc);
  369. return video_register_device(&uvc->vdev, VFL_TYPE_GRABBER, -1);
  370. }
  371. #define UVC_COPY_DESCRIPTOR(mem, dst, desc) \
  372. do { \
  373. memcpy(mem, desc, (desc)->bLength); \
  374. *(dst)++ = mem; \
  375. mem += (desc)->bLength; \
  376. } while (0);
  377. #define UVC_COPY_DESCRIPTORS(mem, dst, src) \
  378. do { \
  379. const struct usb_descriptor_header * const *__src; \
  380. for (__src = src; *__src; ++__src) { \
  381. memcpy(mem, *__src, (*__src)->bLength); \
  382. *dst++ = mem; \
  383. mem += (*__src)->bLength; \
  384. } \
  385. } while (0)
  386. static struct usb_descriptor_header **
  387. uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed)
  388. {
  389. struct uvc_input_header_descriptor *uvc_streaming_header;
  390. struct uvc_header_descriptor *uvc_control_header;
  391. const struct uvc_descriptor_header * const *uvc_control_desc;
  392. const struct uvc_descriptor_header * const *uvc_streaming_cls;
  393. const struct usb_descriptor_header * const *uvc_streaming_std;
  394. const struct usb_descriptor_header * const *src;
  395. struct usb_descriptor_header **dst;
  396. struct usb_descriptor_header **hdr;
  397. unsigned int control_size;
  398. unsigned int streaming_size;
  399. unsigned int n_desc;
  400. unsigned int bytes;
  401. void *mem;
  402. switch (speed) {
  403. case USB_SPEED_SUPER:
  404. uvc_control_desc = uvc->desc.ss_control;
  405. uvc_streaming_cls = uvc->desc.ss_streaming;
  406. uvc_streaming_std = uvc_ss_streaming;
  407. break;
  408. case USB_SPEED_HIGH:
  409. uvc_control_desc = uvc->desc.fs_control;
  410. uvc_streaming_cls = uvc->desc.hs_streaming;
  411. uvc_streaming_std = uvc_hs_streaming;
  412. break;
  413. case USB_SPEED_FULL:
  414. default:
  415. uvc_control_desc = uvc->desc.fs_control;
  416. uvc_streaming_cls = uvc->desc.fs_streaming;
  417. uvc_streaming_std = uvc_fs_streaming;
  418. break;
  419. }
  420. if (!uvc_control_desc || !uvc_streaming_cls)
  421. return ERR_PTR(-ENODEV);
  422. /* Descriptors layout
  423. *
  424. * uvc_iad
  425. * uvc_control_intf
  426. * Class-specific UVC control descriptors
  427. * uvc_control_ep
  428. * uvc_control_cs_ep
  429. * uvc_ss_control_comp (for SS only)
  430. * uvc_streaming_intf_alt0
  431. * Class-specific UVC streaming descriptors
  432. * uvc_{fs|hs}_streaming
  433. */
  434. /* Count descriptors and compute their size. */
  435. control_size = 0;
  436. streaming_size = 0;
  437. bytes = uvc_iad.bLength + uvc_control_intf.bLength
  438. + uvc_control_ep.bLength + uvc_control_cs_ep.bLength
  439. + uvc_streaming_intf_alt0.bLength;
  440. if (speed == USB_SPEED_SUPER) {
  441. bytes += uvc_ss_control_comp.bLength;
  442. n_desc = 6;
  443. } else {
  444. n_desc = 5;
  445. }
  446. for (src = (const struct usb_descriptor_header **)uvc_control_desc;
  447. *src; ++src) {
  448. control_size += (*src)->bLength;
  449. bytes += (*src)->bLength;
  450. n_desc++;
  451. }
  452. for (src = (const struct usb_descriptor_header **)uvc_streaming_cls;
  453. *src; ++src) {
  454. streaming_size += (*src)->bLength;
  455. bytes += (*src)->bLength;
  456. n_desc++;
  457. }
  458. for (src = uvc_streaming_std; *src; ++src) {
  459. bytes += (*src)->bLength;
  460. n_desc++;
  461. }
  462. mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL);
  463. if (mem == NULL)
  464. return NULL;
  465. hdr = mem;
  466. dst = mem;
  467. mem += (n_desc + 1) * sizeof(*src);
  468. /* Copy the descriptors. */
  469. UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad);
  470. UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf);
  471. uvc_control_header = mem;
  472. UVC_COPY_DESCRIPTORS(mem, dst,
  473. (const struct usb_descriptor_header **)uvc_control_desc);
  474. uvc_control_header->wTotalLength = cpu_to_le16(control_size);
  475. uvc_control_header->bInCollection = 1;
  476. uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf;
  477. UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_ep);
  478. if (speed == USB_SPEED_SUPER)
  479. UVC_COPY_DESCRIPTOR(mem, dst, &uvc_ss_control_comp);
  480. UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_cs_ep);
  481. UVC_COPY_DESCRIPTOR(mem, dst, &uvc_streaming_intf_alt0);
  482. uvc_streaming_header = mem;
  483. UVC_COPY_DESCRIPTORS(mem, dst,
  484. (const struct usb_descriptor_header**)uvc_streaming_cls);
  485. uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size);
  486. uvc_streaming_header->bEndpointAddress = uvc->video.ep->address;
  487. UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std);
  488. *dst = NULL;
  489. return hdr;
  490. }
  491. static int
  492. uvc_function_bind(struct usb_configuration *c, struct usb_function *f)
  493. {
  494. struct usb_composite_dev *cdev = c->cdev;
  495. struct uvc_device *uvc = to_uvc(f);
  496. struct usb_string *us;
  497. unsigned int max_packet_mult;
  498. unsigned int max_packet_size;
  499. struct usb_ep *ep;
  500. struct f_uvc_opts *opts;
  501. int ret = -EINVAL;
  502. INFO(cdev, "uvc_function_bind\n");
  503. opts = fi_to_f_uvc_opts(f->fi);
  504. /* Sanity check the streaming endpoint module parameters.
  505. */
  506. opts->streaming_interval = clamp(opts->streaming_interval, 1U, 16U);
  507. opts->streaming_maxpacket = clamp(opts->streaming_maxpacket, 1U, 3072U);
  508. opts->streaming_maxburst = min(opts->streaming_maxburst, 15U);
  509. /* Fill in the FS/HS/SS Video Streaming specific descriptors from the
  510. * module parameters.
  511. *
  512. * NOTE: We assume that the user knows what they are doing and won't
  513. * give parameters that their UDC doesn't support.
  514. */
  515. if (opts->streaming_maxpacket <= 1024) {
  516. max_packet_mult = 1;
  517. max_packet_size = opts->streaming_maxpacket;
  518. } else if (opts->streaming_maxpacket <= 2048) {
  519. max_packet_mult = 2;
  520. max_packet_size = opts->streaming_maxpacket / 2;
  521. } else {
  522. max_packet_mult = 3;
  523. max_packet_size = opts->streaming_maxpacket / 3;
  524. }
  525. uvc_fs_streaming_ep.wMaxPacketSize =
  526. cpu_to_le16(min(opts->streaming_maxpacket, 1023U));
  527. uvc_fs_streaming_ep.bInterval = opts->streaming_interval;
  528. uvc_hs_streaming_ep.wMaxPacketSize =
  529. cpu_to_le16(max_packet_size | ((max_packet_mult - 1) << 11));
  530. uvc_hs_streaming_ep.bInterval = opts->streaming_interval;
  531. uvc_ss_streaming_ep.wMaxPacketSize = cpu_to_le16(max_packet_size);
  532. uvc_ss_streaming_ep.bInterval = opts->streaming_interval;
  533. uvc_ss_streaming_comp.bmAttributes = max_packet_mult - 1;
  534. uvc_ss_streaming_comp.bMaxBurst = opts->streaming_maxburst;
  535. uvc_ss_streaming_comp.wBytesPerInterval =
  536. cpu_to_le16(max_packet_size * max_packet_mult *
  537. opts->streaming_maxburst);
  538. /* Allocate endpoints. */
  539. ep = usb_ep_autoconfig(cdev->gadget, &uvc_control_ep);
  540. if (!ep) {
  541. INFO(cdev, "Unable to allocate control EP\n");
  542. goto error;
  543. }
  544. uvc->control_ep = ep;
  545. ep->driver_data = uvc;
  546. if (gadget_is_superspeed(c->cdev->gadget))
  547. ep = usb_ep_autoconfig_ss(cdev->gadget, &uvc_ss_streaming_ep,
  548. &uvc_ss_streaming_comp);
  549. else if (gadget_is_dualspeed(cdev->gadget))
  550. ep = usb_ep_autoconfig(cdev->gadget, &uvc_hs_streaming_ep);
  551. else
  552. ep = usb_ep_autoconfig(cdev->gadget, &uvc_fs_streaming_ep);
  553. if (!ep) {
  554. INFO(cdev, "Unable to allocate streaming EP\n");
  555. goto error;
  556. }
  557. uvc->video.ep = ep;
  558. ep->driver_data = uvc;
  559. uvc_fs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
  560. uvc_hs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
  561. uvc_ss_streaming_ep.bEndpointAddress = uvc->video.ep->address;
  562. us = usb_gstrings_attach(cdev, uvc_function_strings,
  563. ARRAY_SIZE(uvc_en_us_strings));
  564. if (IS_ERR(us)) {
  565. ret = PTR_ERR(us);
  566. goto error;
  567. }
  568. uvc_iad.iFunction = us[UVC_STRING_CONTROL_IDX].id;
  569. uvc_control_intf.iInterface = us[UVC_STRING_CONTROL_IDX].id;
  570. ret = us[UVC_STRING_STREAMING_IDX].id;
  571. uvc_streaming_intf_alt0.iInterface = ret;
  572. uvc_streaming_intf_alt1.iInterface = ret;
  573. /* Allocate interface IDs. */
  574. if ((ret = usb_interface_id(c, f)) < 0)
  575. goto error;
  576. uvc_iad.bFirstInterface = ret;
  577. uvc_control_intf.bInterfaceNumber = ret;
  578. uvc->control_intf = ret;
  579. if ((ret = usb_interface_id(c, f)) < 0)
  580. goto error;
  581. uvc_streaming_intf_alt0.bInterfaceNumber = ret;
  582. uvc_streaming_intf_alt1.bInterfaceNumber = ret;
  583. uvc->streaming_intf = ret;
  584. /* Copy descriptors */
  585. f->fs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL);
  586. if (IS_ERR(f->fs_descriptors)) {
  587. ret = PTR_ERR(f->fs_descriptors);
  588. f->fs_descriptors = NULL;
  589. goto error;
  590. }
  591. if (gadget_is_dualspeed(cdev->gadget)) {
  592. f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH);
  593. if (IS_ERR(f->hs_descriptors)) {
  594. ret = PTR_ERR(f->hs_descriptors);
  595. f->hs_descriptors = NULL;
  596. goto error;
  597. }
  598. }
  599. if (gadget_is_superspeed(c->cdev->gadget)) {
  600. f->ss_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_SUPER);
  601. if (IS_ERR(f->ss_descriptors)) {
  602. ret = PTR_ERR(f->ss_descriptors);
  603. f->ss_descriptors = NULL;
  604. goto error;
  605. }
  606. }
  607. /* Preallocate control endpoint request. */
  608. uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
  609. uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL);
  610. if (uvc->control_req == NULL || uvc->control_buf == NULL) {
  611. ret = -ENOMEM;
  612. goto error;
  613. }
  614. uvc->control_req->buf = uvc->control_buf;
  615. uvc->control_req->complete = uvc_function_ep0_complete;
  616. uvc->control_req->context = uvc;
  617. if (v4l2_device_register(&cdev->gadget->dev, &uvc->v4l2_dev)) {
  618. printk(KERN_INFO "v4l2_device_register failed\n");
  619. goto error;
  620. }
  621. /* Initialise video. */
  622. ret = uvcg_video_init(&uvc->video);
  623. if (ret < 0)
  624. goto error;
  625. /* Register a V4L2 device. */
  626. ret = uvc_register_video(uvc);
  627. if (ret < 0) {
  628. printk(KERN_INFO "Unable to register video device\n");
  629. goto error;
  630. }
  631. return 0;
  632. error:
  633. v4l2_device_unregister(&uvc->v4l2_dev);
  634. if (uvc->control_ep)
  635. uvc->control_ep->driver_data = NULL;
  636. if (uvc->video.ep)
  637. uvc->video.ep->driver_data = NULL;
  638. if (uvc->control_req)
  639. usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
  640. kfree(uvc->control_buf);
  641. usb_free_all_descriptors(f);
  642. return ret;
  643. }
  644. /* --------------------------------------------------------------------------
  645. * USB gadget function
  646. */
  647. static void uvc_free_inst(struct usb_function_instance *f)
  648. {
  649. struct f_uvc_opts *opts = fi_to_f_uvc_opts(f);
  650. mutex_destroy(&opts->lock);
  651. kfree(opts);
  652. }
  653. static struct usb_function_instance *uvc_alloc_inst(void)
  654. {
  655. struct f_uvc_opts *opts;
  656. struct uvc_camera_terminal_descriptor *cd;
  657. struct uvc_processing_unit_descriptor *pd;
  658. struct uvc_output_terminal_descriptor *od;
  659. struct uvc_color_matching_descriptor *md;
  660. struct uvc_descriptor_header **ctl_cls;
  661. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  662. if (!opts)
  663. return ERR_PTR(-ENOMEM);
  664. opts->func_inst.free_func_inst = uvc_free_inst;
  665. mutex_init(&opts->lock);
  666. cd = &opts->uvc_camera_terminal;
  667. cd->bLength = UVC_DT_CAMERA_TERMINAL_SIZE(3);
  668. cd->bDescriptorType = USB_DT_CS_INTERFACE;
  669. cd->bDescriptorSubType = UVC_VC_INPUT_TERMINAL;
  670. cd->bTerminalID = 1;
  671. cd->wTerminalType = cpu_to_le16(0x0201);
  672. cd->bAssocTerminal = 0;
  673. cd->iTerminal = 0;
  674. cd->wObjectiveFocalLengthMin = cpu_to_le16(0);
  675. cd->wObjectiveFocalLengthMax = cpu_to_le16(0);
  676. cd->wOcularFocalLength = cpu_to_le16(0);
  677. cd->bControlSize = 3;
  678. cd->bmControls[0] = 2;
  679. cd->bmControls[1] = 0;
  680. cd->bmControls[2] = 0;
  681. pd = &opts->uvc_processing;
  682. pd->bLength = UVC_DT_PROCESSING_UNIT_SIZE(2);
  683. pd->bDescriptorType = USB_DT_CS_INTERFACE;
  684. pd->bDescriptorSubType = UVC_VC_PROCESSING_UNIT;
  685. pd->bUnitID = 2;
  686. pd->bSourceID = 1;
  687. pd->wMaxMultiplier = cpu_to_le16(16*1024);
  688. pd->bControlSize = 2;
  689. pd->bmControls[0] = 1;
  690. pd->bmControls[1] = 0;
  691. pd->iProcessing = 0;
  692. od = &opts->uvc_output_terminal;
  693. od->bLength = UVC_DT_OUTPUT_TERMINAL_SIZE;
  694. od->bDescriptorType = USB_DT_CS_INTERFACE;
  695. od->bDescriptorSubType = UVC_VC_OUTPUT_TERMINAL;
  696. od->bTerminalID = 3;
  697. od->wTerminalType = cpu_to_le16(0x0101);
  698. od->bAssocTerminal = 0;
  699. od->bSourceID = 2;
  700. od->iTerminal = 0;
  701. md = &opts->uvc_color_matching;
  702. md->bLength = UVC_DT_COLOR_MATCHING_SIZE;
  703. md->bDescriptorType = USB_DT_CS_INTERFACE;
  704. md->bDescriptorSubType = UVC_VS_COLORFORMAT;
  705. md->bColorPrimaries = 1;
  706. md->bTransferCharacteristics = 1;
  707. md->bMatrixCoefficients = 4;
  708. /* Prepare fs control class descriptors for configfs-based gadgets */
  709. ctl_cls = opts->uvc_fs_control_cls;
  710. ctl_cls[0] = NULL; /* assigned elsewhere by configfs */
  711. ctl_cls[1] = (struct uvc_descriptor_header *)cd;
  712. ctl_cls[2] = (struct uvc_descriptor_header *)pd;
  713. ctl_cls[3] = (struct uvc_descriptor_header *)od;
  714. ctl_cls[4] = NULL; /* NULL-terminate */
  715. opts->fs_control =
  716. (const struct uvc_descriptor_header * const *)ctl_cls;
  717. /* Prepare hs control class descriptors for configfs-based gadgets */
  718. ctl_cls = opts->uvc_ss_control_cls;
  719. ctl_cls[0] = NULL; /* assigned elsewhere by configfs */
  720. ctl_cls[1] = (struct uvc_descriptor_header *)cd;
  721. ctl_cls[2] = (struct uvc_descriptor_header *)pd;
  722. ctl_cls[3] = (struct uvc_descriptor_header *)od;
  723. ctl_cls[4] = NULL; /* NULL-terminate */
  724. opts->ss_control =
  725. (const struct uvc_descriptor_header * const *)ctl_cls;
  726. opts->streaming_interval = 1;
  727. opts->streaming_maxpacket = 1024;
  728. uvcg_attach_configfs(opts);
  729. return &opts->func_inst;
  730. }
  731. static void uvc_free(struct usb_function *f)
  732. {
  733. struct uvc_device *uvc = to_uvc(f);
  734. struct f_uvc_opts *opts = container_of(f->fi, struct f_uvc_opts,
  735. func_inst);
  736. --opts->refcnt;
  737. kfree(uvc);
  738. }
  739. static void uvc_unbind(struct usb_configuration *c, struct usb_function *f)
  740. {
  741. struct usb_composite_dev *cdev = c->cdev;
  742. struct uvc_device *uvc = to_uvc(f);
  743. INFO(cdev, "%s\n", __func__);
  744. video_unregister_device(&uvc->vdev);
  745. v4l2_device_unregister(&uvc->v4l2_dev);
  746. uvc->control_ep->driver_data = NULL;
  747. uvc->video.ep->driver_data = NULL;
  748. usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
  749. kfree(uvc->control_buf);
  750. usb_free_all_descriptors(f);
  751. }
  752. static struct usb_function *uvc_alloc(struct usb_function_instance *fi)
  753. {
  754. struct uvc_device *uvc;
  755. struct f_uvc_opts *opts;
  756. struct uvc_descriptor_header **strm_cls;
  757. uvc = kzalloc(sizeof(*uvc), GFP_KERNEL);
  758. if (uvc == NULL)
  759. return ERR_PTR(-ENOMEM);
  760. mutex_init(&uvc->video.mutex);
  761. uvc->state = UVC_STATE_DISCONNECTED;
  762. opts = fi_to_f_uvc_opts(fi);
  763. mutex_lock(&opts->lock);
  764. if (opts->uvc_fs_streaming_cls) {
  765. strm_cls = opts->uvc_fs_streaming_cls;
  766. opts->fs_streaming =
  767. (const struct uvc_descriptor_header * const *)strm_cls;
  768. }
  769. if (opts->uvc_hs_streaming_cls) {
  770. strm_cls = opts->uvc_hs_streaming_cls;
  771. opts->hs_streaming =
  772. (const struct uvc_descriptor_header * const *)strm_cls;
  773. }
  774. if (opts->uvc_ss_streaming_cls) {
  775. strm_cls = opts->uvc_ss_streaming_cls;
  776. opts->ss_streaming =
  777. (const struct uvc_descriptor_header * const *)strm_cls;
  778. }
  779. uvc->desc.fs_control = opts->fs_control;
  780. uvc->desc.ss_control = opts->ss_control;
  781. uvc->desc.fs_streaming = opts->fs_streaming;
  782. uvc->desc.hs_streaming = opts->hs_streaming;
  783. uvc->desc.ss_streaming = opts->ss_streaming;
  784. ++opts->refcnt;
  785. mutex_unlock(&opts->lock);
  786. /* Register the function. */
  787. uvc->func.name = "uvc";
  788. uvc->func.bind = uvc_function_bind;
  789. uvc->func.unbind = uvc_unbind;
  790. uvc->func.get_alt = uvc_function_get_alt;
  791. uvc->func.set_alt = uvc_function_set_alt;
  792. uvc->func.disable = uvc_function_disable;
  793. uvc->func.setup = uvc_function_setup;
  794. uvc->func.free_func = uvc_free;
  795. uvc->func.bind_deactivated = true;
  796. return &uvc->func;
  797. }
  798. DECLARE_USB_FUNCTION_INIT(uvc, uvc_alloc_inst, uvc_alloc);
  799. MODULE_LICENSE("GPL");
  800. MODULE_AUTHOR("Laurent Pinchart");