f_uvc.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  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/device.h>
  14. #include <linux/errno.h>
  15. #include <linux/fs.h>
  16. #include <linux/list.h>
  17. #include <linux/mutex.h>
  18. #include <linux/string.h>
  19. #include <linux/usb/ch9.h>
  20. #include <linux/usb/gadget.h>
  21. #include <linux/usb/video.h>
  22. #include <linux/vmalloc.h>
  23. #include <linux/wait.h>
  24. #include <media/v4l2-dev.h>
  25. #include <media/v4l2-event.h>
  26. #include "uvc.h"
  27. unsigned int uvc_gadget_trace_param;
  28. /*-------------------------------------------------------------------------*/
  29. /* module parameters specific to the Video streaming endpoint */
  30. static unsigned int streaming_interval = 1;
  31. module_param(streaming_interval, uint, S_IRUGO|S_IWUSR);
  32. MODULE_PARM_DESC(streaming_interval, "1 - 16");
  33. static unsigned int streaming_maxpacket = 1024;
  34. module_param(streaming_maxpacket, uint, S_IRUGO|S_IWUSR);
  35. MODULE_PARM_DESC(streaming_maxpacket, "1 - 1023 (FS), 1 - 3072 (hs/ss)");
  36. static unsigned int streaming_maxburst;
  37. module_param(streaming_maxburst, uint, S_IRUGO|S_IWUSR);
  38. MODULE_PARM_DESC(streaming_maxburst, "0 - 15 (ss only)");
  39. /* --------------------------------------------------------------------------
  40. * Function descriptors
  41. */
  42. /* string IDs are assigned dynamically */
  43. #define UVC_STRING_CONTROL_IDX 0
  44. #define UVC_STRING_STREAMING_IDX 1
  45. static struct usb_string uvc_en_us_strings[] = {
  46. [UVC_STRING_CONTROL_IDX].s = "UVC Camera",
  47. [UVC_STRING_STREAMING_IDX].s = "Video Streaming",
  48. { }
  49. };
  50. static struct usb_gadget_strings uvc_stringtab = {
  51. .language = 0x0409, /* en-us */
  52. .strings = uvc_en_us_strings,
  53. };
  54. static struct usb_gadget_strings *uvc_function_strings[] = {
  55. &uvc_stringtab,
  56. NULL,
  57. };
  58. #define UVC_INTF_VIDEO_CONTROL 0
  59. #define UVC_INTF_VIDEO_STREAMING 1
  60. #define UVC_STATUS_MAX_PACKET_SIZE 16 /* 16 bytes status */
  61. static struct usb_interface_assoc_descriptor uvc_iad __initdata = {
  62. .bLength = sizeof(uvc_iad),
  63. .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
  64. .bFirstInterface = 0,
  65. .bInterfaceCount = 2,
  66. .bFunctionClass = USB_CLASS_VIDEO,
  67. .bFunctionSubClass = UVC_SC_VIDEO_INTERFACE_COLLECTION,
  68. .bFunctionProtocol = 0x00,
  69. .iFunction = 0,
  70. };
  71. static struct usb_interface_descriptor uvc_control_intf __initdata = {
  72. .bLength = USB_DT_INTERFACE_SIZE,
  73. .bDescriptorType = USB_DT_INTERFACE,
  74. .bInterfaceNumber = UVC_INTF_VIDEO_CONTROL,
  75. .bAlternateSetting = 0,
  76. .bNumEndpoints = 1,
  77. .bInterfaceClass = USB_CLASS_VIDEO,
  78. .bInterfaceSubClass = UVC_SC_VIDEOCONTROL,
  79. .bInterfaceProtocol = 0x00,
  80. .iInterface = 0,
  81. };
  82. static struct usb_endpoint_descriptor uvc_control_ep __initdata = {
  83. .bLength = USB_DT_ENDPOINT_SIZE,
  84. .bDescriptorType = USB_DT_ENDPOINT,
  85. .bEndpointAddress = USB_DIR_IN,
  86. .bmAttributes = USB_ENDPOINT_XFER_INT,
  87. .wMaxPacketSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
  88. .bInterval = 8,
  89. };
  90. static struct usb_ss_ep_comp_descriptor uvc_ss_control_comp __initdata = {
  91. .bLength = sizeof(uvc_ss_control_comp),
  92. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  93. /* The following 3 values can be tweaked if necessary. */
  94. .bMaxBurst = 0,
  95. .bmAttributes = 0,
  96. .wBytesPerInterval = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
  97. };
  98. static struct uvc_control_endpoint_descriptor uvc_control_cs_ep __initdata = {
  99. .bLength = UVC_DT_CONTROL_ENDPOINT_SIZE,
  100. .bDescriptorType = USB_DT_CS_ENDPOINT,
  101. .bDescriptorSubType = UVC_EP_INTERRUPT,
  102. .wMaxTransferSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
  103. };
  104. static struct usb_interface_descriptor uvc_streaming_intf_alt0 __initdata = {
  105. .bLength = USB_DT_INTERFACE_SIZE,
  106. .bDescriptorType = USB_DT_INTERFACE,
  107. .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING,
  108. .bAlternateSetting = 0,
  109. .bNumEndpoints = 0,
  110. .bInterfaceClass = USB_CLASS_VIDEO,
  111. .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING,
  112. .bInterfaceProtocol = 0x00,
  113. .iInterface = 0,
  114. };
  115. static struct usb_interface_descriptor uvc_streaming_intf_alt1 __initdata = {
  116. .bLength = USB_DT_INTERFACE_SIZE,
  117. .bDescriptorType = USB_DT_INTERFACE,
  118. .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING,
  119. .bAlternateSetting = 1,
  120. .bNumEndpoints = 1,
  121. .bInterfaceClass = USB_CLASS_VIDEO,
  122. .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING,
  123. .bInterfaceProtocol = 0x00,
  124. .iInterface = 0,
  125. };
  126. static struct usb_endpoint_descriptor uvc_fs_streaming_ep __initdata = {
  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. .wMaxPacketSize = 0,
  136. .bInterval = 0,
  137. };
  138. static struct usb_endpoint_descriptor uvc_hs_streaming_ep __initdata = {
  139. .bLength = USB_DT_ENDPOINT_SIZE,
  140. .bDescriptorType = USB_DT_ENDPOINT,
  141. .bEndpointAddress = USB_DIR_IN,
  142. .bmAttributes = USB_ENDPOINT_SYNC_ASYNC
  143. | USB_ENDPOINT_XFER_ISOC,
  144. /* The wMaxPacketSize and bInterval values will be initialized from
  145. * module parameters.
  146. */
  147. .wMaxPacketSize = 0,
  148. .bInterval = 0,
  149. };
  150. static struct usb_endpoint_descriptor uvc_ss_streaming_ep __initdata = {
  151. .bLength = USB_DT_ENDPOINT_SIZE,
  152. .bDescriptorType = USB_DT_ENDPOINT,
  153. .bEndpointAddress = USB_DIR_IN,
  154. .bmAttributes = USB_ENDPOINT_SYNC_ASYNC
  155. | USB_ENDPOINT_XFER_ISOC,
  156. /* The wMaxPacketSize and bInterval values will be initialized from
  157. * module parameters.
  158. */
  159. .wMaxPacketSize = 0,
  160. .bInterval = 0,
  161. };
  162. static struct usb_ss_ep_comp_descriptor uvc_ss_streaming_comp __initdata = {
  163. .bLength = sizeof(uvc_ss_streaming_comp),
  164. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  165. /* The following 3 values can be tweaked if necessary. */
  166. .bMaxBurst = 0,
  167. .bmAttributes = 0,
  168. .wBytesPerInterval = cpu_to_le16(1024),
  169. };
  170. static const struct usb_descriptor_header * const uvc_fs_streaming[] = {
  171. (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
  172. (struct usb_descriptor_header *) &uvc_fs_streaming_ep,
  173. NULL,
  174. };
  175. static const struct usb_descriptor_header * const uvc_hs_streaming[] = {
  176. (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
  177. (struct usb_descriptor_header *) &uvc_hs_streaming_ep,
  178. NULL,
  179. };
  180. static const struct usb_descriptor_header * const uvc_ss_streaming[] = {
  181. (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
  182. (struct usb_descriptor_header *) &uvc_ss_streaming_ep,
  183. (struct usb_descriptor_header *) &uvc_ss_streaming_comp,
  184. NULL,
  185. };
  186. /* --------------------------------------------------------------------------
  187. * Control requests
  188. */
  189. static void
  190. uvc_function_ep0_complete(struct usb_ep *ep, struct usb_request *req)
  191. {
  192. struct uvc_device *uvc = req->context;
  193. struct v4l2_event v4l2_event;
  194. struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
  195. if (uvc->event_setup_out) {
  196. uvc->event_setup_out = 0;
  197. memset(&v4l2_event, 0, sizeof(v4l2_event));
  198. v4l2_event.type = UVC_EVENT_DATA;
  199. uvc_event->data.length = req->actual;
  200. memcpy(&uvc_event->data.data, req->buf, req->actual);
  201. v4l2_event_queue(uvc->vdev, &v4l2_event);
  202. }
  203. }
  204. static int
  205. uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
  206. {
  207. struct uvc_device *uvc = to_uvc(f);
  208. struct v4l2_event v4l2_event;
  209. struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
  210. /* printk(KERN_INFO "setup request %02x %02x value %04x index %04x %04x\n",
  211. * ctrl->bRequestType, ctrl->bRequest, le16_to_cpu(ctrl->wValue),
  212. * le16_to_cpu(ctrl->wIndex), le16_to_cpu(ctrl->wLength));
  213. */
  214. if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) {
  215. INFO(f->config->cdev, "invalid request type\n");
  216. return -EINVAL;
  217. }
  218. /* Stall too big requests. */
  219. if (le16_to_cpu(ctrl->wLength) > UVC_MAX_REQUEST_SIZE)
  220. return -EINVAL;
  221. memset(&v4l2_event, 0, sizeof(v4l2_event));
  222. v4l2_event.type = UVC_EVENT_SETUP;
  223. memcpy(&uvc_event->req, ctrl, sizeof(uvc_event->req));
  224. v4l2_event_queue(uvc->vdev, &v4l2_event);
  225. return 0;
  226. }
  227. void uvc_function_setup_continue(struct uvc_device *uvc)
  228. {
  229. struct usb_composite_dev *cdev = uvc->func.config->cdev;
  230. usb_composite_setup_continue(cdev);
  231. }
  232. static int
  233. uvc_function_get_alt(struct usb_function *f, unsigned interface)
  234. {
  235. struct uvc_device *uvc = to_uvc(f);
  236. INFO(f->config->cdev, "uvc_function_get_alt(%u)\n", interface);
  237. if (interface == uvc->control_intf)
  238. return 0;
  239. else if (interface != uvc->streaming_intf)
  240. return -EINVAL;
  241. else
  242. return uvc->state == UVC_STATE_STREAMING ? 1 : 0;
  243. }
  244. static int
  245. uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
  246. {
  247. struct uvc_device *uvc = to_uvc(f);
  248. struct v4l2_event v4l2_event;
  249. struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
  250. int ret;
  251. INFO(f->config->cdev, "uvc_function_set_alt(%u, %u)\n", interface, alt);
  252. if (interface == uvc->control_intf) {
  253. if (alt)
  254. return -EINVAL;
  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 = f->config->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. ret = config_ep_by_speed(f->config->cdev->gadget,
  286. &(uvc->func), uvc->video.ep);
  287. if (ret)
  288. return ret;
  289. usb_ep_enable(uvc->video.ep);
  290. }
  291. memset(&v4l2_event, 0, sizeof(v4l2_event));
  292. v4l2_event.type = UVC_EVENT_STREAMON;
  293. v4l2_event_queue(uvc->vdev, &v4l2_event);
  294. return USB_GADGET_DELAYED_STATUS;
  295. default:
  296. return -EINVAL;
  297. }
  298. }
  299. static void
  300. uvc_function_disable(struct usb_function *f)
  301. {
  302. struct uvc_device *uvc = to_uvc(f);
  303. struct v4l2_event v4l2_event;
  304. INFO(f->config->cdev, "uvc_function_disable\n");
  305. memset(&v4l2_event, 0, sizeof(v4l2_event));
  306. v4l2_event.type = UVC_EVENT_DISCONNECT;
  307. v4l2_event_queue(uvc->vdev, &v4l2_event);
  308. uvc->state = UVC_STATE_DISCONNECTED;
  309. }
  310. /* --------------------------------------------------------------------------
  311. * Connection / disconnection
  312. */
  313. void
  314. uvc_function_connect(struct uvc_device *uvc)
  315. {
  316. struct usb_composite_dev *cdev = uvc->func.config->cdev;
  317. int ret;
  318. if ((ret = usb_function_activate(&uvc->func)) < 0)
  319. INFO(cdev, "UVC connect failed with %d\n", ret);
  320. }
  321. void
  322. uvc_function_disconnect(struct uvc_device *uvc)
  323. {
  324. struct usb_composite_dev *cdev = uvc->func.config->cdev;
  325. int ret;
  326. if ((ret = usb_function_deactivate(&uvc->func)) < 0)
  327. INFO(cdev, "UVC disconnect failed with %d\n", ret);
  328. }
  329. /* --------------------------------------------------------------------------
  330. * USB probe and disconnect
  331. */
  332. static int
  333. uvc_register_video(struct uvc_device *uvc)
  334. {
  335. struct usb_composite_dev *cdev = uvc->func.config->cdev;
  336. struct video_device *video;
  337. /* TODO reference counting. */
  338. video = video_device_alloc();
  339. if (video == NULL)
  340. return -ENOMEM;
  341. video->parent = &cdev->gadget->dev;
  342. video->fops = &uvc_v4l2_fops;
  343. video->release = video_device_release;
  344. strlcpy(video->name, cdev->gadget->name, sizeof(video->name));
  345. uvc->vdev = video;
  346. video_set_drvdata(video, uvc);
  347. return video_register_device(video, VFL_TYPE_GRABBER, -1);
  348. }
  349. #define UVC_COPY_DESCRIPTOR(mem, dst, desc) \
  350. do { \
  351. memcpy(mem, desc, (desc)->bLength); \
  352. *(dst)++ = mem; \
  353. mem += (desc)->bLength; \
  354. } while (0);
  355. #define UVC_COPY_DESCRIPTORS(mem, dst, src) \
  356. do { \
  357. const struct usb_descriptor_header * const *__src; \
  358. for (__src = src; *__src; ++__src) { \
  359. memcpy(mem, *__src, (*__src)->bLength); \
  360. *dst++ = mem; \
  361. mem += (*__src)->bLength; \
  362. } \
  363. } while (0)
  364. static struct usb_descriptor_header ** __init
  365. uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed)
  366. {
  367. struct uvc_input_header_descriptor *uvc_streaming_header;
  368. struct uvc_header_descriptor *uvc_control_header;
  369. const struct uvc_descriptor_header * const *uvc_control_desc;
  370. const struct uvc_descriptor_header * const *uvc_streaming_cls;
  371. const struct usb_descriptor_header * const *uvc_streaming_std;
  372. const struct usb_descriptor_header * const *src;
  373. struct usb_descriptor_header **dst;
  374. struct usb_descriptor_header **hdr;
  375. unsigned int control_size;
  376. unsigned int streaming_size;
  377. unsigned int n_desc;
  378. unsigned int bytes;
  379. void *mem;
  380. switch (speed) {
  381. case USB_SPEED_SUPER:
  382. uvc_control_desc = uvc->desc.ss_control;
  383. uvc_streaming_cls = uvc->desc.ss_streaming;
  384. uvc_streaming_std = uvc_ss_streaming;
  385. break;
  386. case USB_SPEED_HIGH:
  387. uvc_control_desc = uvc->desc.fs_control;
  388. uvc_streaming_cls = uvc->desc.hs_streaming;
  389. uvc_streaming_std = uvc_hs_streaming;
  390. break;
  391. case USB_SPEED_FULL:
  392. default:
  393. uvc_control_desc = uvc->desc.fs_control;
  394. uvc_streaming_cls = uvc->desc.fs_streaming;
  395. uvc_streaming_std = uvc_fs_streaming;
  396. break;
  397. }
  398. /* Descriptors layout
  399. *
  400. * uvc_iad
  401. * uvc_control_intf
  402. * Class-specific UVC control descriptors
  403. * uvc_control_ep
  404. * uvc_control_cs_ep
  405. * uvc_ss_control_comp (for SS only)
  406. * uvc_streaming_intf_alt0
  407. * Class-specific UVC streaming descriptors
  408. * uvc_{fs|hs}_streaming
  409. */
  410. /* Count descriptors and compute their size. */
  411. control_size = 0;
  412. streaming_size = 0;
  413. bytes = uvc_iad.bLength + uvc_control_intf.bLength
  414. + uvc_control_ep.bLength + uvc_control_cs_ep.bLength
  415. + uvc_streaming_intf_alt0.bLength;
  416. if (speed == USB_SPEED_SUPER) {
  417. bytes += uvc_ss_control_comp.bLength;
  418. n_desc = 6;
  419. } else {
  420. n_desc = 5;
  421. }
  422. for (src = (const struct usb_descriptor_header **)uvc_control_desc;
  423. *src; ++src) {
  424. control_size += (*src)->bLength;
  425. bytes += (*src)->bLength;
  426. n_desc++;
  427. }
  428. for (src = (const struct usb_descriptor_header **)uvc_streaming_cls;
  429. *src; ++src) {
  430. streaming_size += (*src)->bLength;
  431. bytes += (*src)->bLength;
  432. n_desc++;
  433. }
  434. for (src = uvc_streaming_std; *src; ++src) {
  435. bytes += (*src)->bLength;
  436. n_desc++;
  437. }
  438. mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL);
  439. if (mem == NULL)
  440. return NULL;
  441. hdr = mem;
  442. dst = mem;
  443. mem += (n_desc + 1) * sizeof(*src);
  444. /* Copy the descriptors. */
  445. UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad);
  446. UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf);
  447. uvc_control_header = mem;
  448. UVC_COPY_DESCRIPTORS(mem, dst,
  449. (const struct usb_descriptor_header **)uvc_control_desc);
  450. uvc_control_header->wTotalLength = cpu_to_le16(control_size);
  451. uvc_control_header->bInCollection = 1;
  452. uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf;
  453. UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_ep);
  454. if (speed == USB_SPEED_SUPER)
  455. UVC_COPY_DESCRIPTOR(mem, dst, &uvc_ss_control_comp);
  456. UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_cs_ep);
  457. UVC_COPY_DESCRIPTOR(mem, dst, &uvc_streaming_intf_alt0);
  458. uvc_streaming_header = mem;
  459. UVC_COPY_DESCRIPTORS(mem, dst,
  460. (const struct usb_descriptor_header**)uvc_streaming_cls);
  461. uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size);
  462. uvc_streaming_header->bEndpointAddress = uvc->video.ep->address;
  463. UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std);
  464. *dst = NULL;
  465. return hdr;
  466. }
  467. static void
  468. uvc_function_unbind(struct usb_configuration *c, struct usb_function *f)
  469. {
  470. struct usb_composite_dev *cdev = c->cdev;
  471. struct uvc_device *uvc = to_uvc(f);
  472. INFO(cdev, "uvc_function_unbind\n");
  473. video_unregister_device(uvc->vdev);
  474. uvc->control_ep->driver_data = NULL;
  475. uvc->video.ep->driver_data = NULL;
  476. uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id = 0;
  477. usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
  478. kfree(uvc->control_buf);
  479. usb_free_all_descriptors(f);
  480. kfree(uvc);
  481. }
  482. static int __init
  483. uvc_function_bind(struct usb_configuration *c, struct usb_function *f)
  484. {
  485. struct usb_composite_dev *cdev = c->cdev;
  486. struct uvc_device *uvc = to_uvc(f);
  487. unsigned int max_packet_mult;
  488. unsigned int max_packet_size;
  489. struct usb_ep *ep;
  490. int ret = -EINVAL;
  491. INFO(cdev, "uvc_function_bind\n");
  492. /* Sanity check the streaming endpoint module parameters.
  493. */
  494. streaming_interval = clamp(streaming_interval, 1U, 16U);
  495. streaming_maxpacket = clamp(streaming_maxpacket, 1U, 3072U);
  496. streaming_maxburst = min(streaming_maxburst, 15U);
  497. /* Fill in the FS/HS/SS Video Streaming specific descriptors from the
  498. * module parameters.
  499. *
  500. * NOTE: We assume that the user knows what they are doing and won't
  501. * give parameters that their UDC doesn't support.
  502. */
  503. if (streaming_maxpacket <= 1024) {
  504. max_packet_mult = 1;
  505. max_packet_size = streaming_maxpacket;
  506. } else if (streaming_maxpacket <= 2048) {
  507. max_packet_mult = 2;
  508. max_packet_size = streaming_maxpacket / 2;
  509. } else {
  510. max_packet_mult = 3;
  511. max_packet_size = streaming_maxpacket / 3;
  512. }
  513. uvc_fs_streaming_ep.wMaxPacketSize = min(streaming_maxpacket, 1023U);
  514. uvc_fs_streaming_ep.bInterval = streaming_interval;
  515. uvc_hs_streaming_ep.wMaxPacketSize = max_packet_size;
  516. uvc_hs_streaming_ep.wMaxPacketSize |= ((max_packet_mult - 1) << 11);
  517. uvc_hs_streaming_ep.bInterval = streaming_interval;
  518. uvc_ss_streaming_ep.wMaxPacketSize = max_packet_size;
  519. uvc_ss_streaming_ep.bInterval = streaming_interval;
  520. uvc_ss_streaming_comp.bmAttributes = max_packet_mult - 1;
  521. uvc_ss_streaming_comp.bMaxBurst = streaming_maxburst;
  522. uvc_ss_streaming_comp.wBytesPerInterval =
  523. max_packet_size * max_packet_mult * streaming_maxburst;
  524. /* Allocate endpoints. */
  525. ep = usb_ep_autoconfig(cdev->gadget, &uvc_control_ep);
  526. if (!ep) {
  527. INFO(cdev, "Unable to allocate control EP\n");
  528. goto error;
  529. }
  530. uvc->control_ep = ep;
  531. ep->driver_data = uvc;
  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. ep->driver_data = uvc;
  545. uvc_fs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
  546. uvc_hs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
  547. uvc_ss_streaming_ep.bEndpointAddress = uvc->video.ep->address;
  548. /* Allocate interface IDs. */
  549. if ((ret = usb_interface_id(c, f)) < 0)
  550. goto error;
  551. uvc_iad.bFirstInterface = ret;
  552. uvc_control_intf.bInterfaceNumber = ret;
  553. uvc->control_intf = ret;
  554. if ((ret = usb_interface_id(c, f)) < 0)
  555. goto error;
  556. uvc_streaming_intf_alt0.bInterfaceNumber = ret;
  557. uvc_streaming_intf_alt1.bInterfaceNumber = ret;
  558. uvc->streaming_intf = ret;
  559. /* Copy descriptors */
  560. f->fs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL);
  561. if (gadget_is_dualspeed(cdev->gadget))
  562. f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH);
  563. if (gadget_is_superspeed(c->cdev->gadget))
  564. f->ss_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_SUPER);
  565. /* Preallocate control endpoint request. */
  566. uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
  567. uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL);
  568. if (uvc->control_req == NULL || uvc->control_buf == NULL) {
  569. ret = -ENOMEM;
  570. goto error;
  571. }
  572. uvc->control_req->buf = uvc->control_buf;
  573. uvc->control_req->complete = uvc_function_ep0_complete;
  574. uvc->control_req->context = uvc;
  575. /* Avoid letting this gadget enumerate until the userspace server is
  576. * active.
  577. */
  578. if ((ret = usb_function_deactivate(f)) < 0)
  579. goto error;
  580. /* Initialise video. */
  581. ret = uvc_video_init(&uvc->video);
  582. if (ret < 0)
  583. goto error;
  584. /* Register a V4L2 device. */
  585. ret = uvc_register_video(uvc);
  586. if (ret < 0) {
  587. printk(KERN_INFO "Unable to register video device\n");
  588. goto error;
  589. }
  590. return 0;
  591. error:
  592. if (uvc->vdev)
  593. video_device_release(uvc->vdev);
  594. if (uvc->control_ep)
  595. uvc->control_ep->driver_data = NULL;
  596. if (uvc->video.ep)
  597. uvc->video.ep->driver_data = NULL;
  598. if (uvc->control_req) {
  599. usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
  600. kfree(uvc->control_buf);
  601. }
  602. usb_free_all_descriptors(f);
  603. return ret;
  604. }
  605. /* --------------------------------------------------------------------------
  606. * USB gadget function
  607. */
  608. /**
  609. * uvc_bind_config - add a UVC function to a configuration
  610. * @c: the configuration to support the UVC instance
  611. * Context: single threaded during gadget setup
  612. *
  613. * Returns zero on success, else negative errno.
  614. *
  615. * Caller must have called @uvc_setup(). Caller is also responsible for
  616. * calling @uvc_cleanup() before module unload.
  617. */
  618. int __init
  619. uvc_bind_config(struct usb_configuration *c,
  620. const struct uvc_descriptor_header * const *fs_control,
  621. const struct uvc_descriptor_header * const *ss_control,
  622. const struct uvc_descriptor_header * const *fs_streaming,
  623. const struct uvc_descriptor_header * const *hs_streaming,
  624. const struct uvc_descriptor_header * const *ss_streaming)
  625. {
  626. struct uvc_device *uvc;
  627. int ret = 0;
  628. /* TODO Check if the USB device controller supports the required
  629. * features.
  630. */
  631. if (!gadget_is_dualspeed(c->cdev->gadget))
  632. return -EINVAL;
  633. uvc = kzalloc(sizeof(*uvc), GFP_KERNEL);
  634. if (uvc == NULL)
  635. return -ENOMEM;
  636. uvc->state = UVC_STATE_DISCONNECTED;
  637. /* Validate the descriptors. */
  638. if (fs_control == NULL || fs_control[0] == NULL ||
  639. fs_control[0]->bDescriptorSubType != UVC_VC_HEADER)
  640. goto error;
  641. if (ss_control == NULL || ss_control[0] == NULL ||
  642. ss_control[0]->bDescriptorSubType != UVC_VC_HEADER)
  643. goto error;
  644. if (fs_streaming == NULL || fs_streaming[0] == NULL ||
  645. fs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
  646. goto error;
  647. if (hs_streaming == NULL || hs_streaming[0] == NULL ||
  648. hs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
  649. goto error;
  650. if (ss_streaming == NULL || ss_streaming[0] == NULL ||
  651. ss_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
  652. goto error;
  653. uvc->desc.fs_control = fs_control;
  654. uvc->desc.ss_control = ss_control;
  655. uvc->desc.fs_streaming = fs_streaming;
  656. uvc->desc.hs_streaming = hs_streaming;
  657. uvc->desc.ss_streaming = ss_streaming;
  658. /* String descriptors are global, we only need to allocate string IDs
  659. * for the first UVC function. UVC functions beyond the first (if any)
  660. * will reuse the same IDs.
  661. */
  662. if (uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id == 0) {
  663. ret = usb_string_ids_tab(c->cdev, uvc_en_us_strings);
  664. if (ret)
  665. goto error;
  666. uvc_iad.iFunction =
  667. uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id;
  668. uvc_control_intf.iInterface =
  669. uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id;
  670. ret = uvc_en_us_strings[UVC_STRING_STREAMING_IDX].id;
  671. uvc_streaming_intf_alt0.iInterface = ret;
  672. uvc_streaming_intf_alt1.iInterface = ret;
  673. }
  674. /* Register the function. */
  675. uvc->func.name = "uvc";
  676. uvc->func.strings = uvc_function_strings;
  677. uvc->func.bind = uvc_function_bind;
  678. uvc->func.unbind = uvc_function_unbind;
  679. uvc->func.get_alt = uvc_function_get_alt;
  680. uvc->func.set_alt = uvc_function_set_alt;
  681. uvc->func.disable = uvc_function_disable;
  682. uvc->func.setup = uvc_function_setup;
  683. ret = usb_add_function(c, &uvc->func);
  684. if (ret)
  685. kfree(uvc);
  686. return ret;
  687. error:
  688. kfree(uvc);
  689. return ret;
  690. }
  691. module_param_named(trace, uvc_gadget_trace_param, uint, S_IRUGO|S_IWUSR);
  692. MODULE_PARM_DESC(trace, "Trace level bitmask");