f_uvc.c 26 KB

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