f_uvc.c 26 KB

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