f_uvc.c 26 KB

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