f_sourcesink.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * f_sourcesink.c - USB peripheral source/sink configuration driver
  4. *
  5. * Copyright (C) 2003-2008 David Brownell
  6. * Copyright (C) 2008 by Nokia Corporation
  7. */
  8. /* #define VERBOSE_DEBUG */
  9. #include <linux/slab.h>
  10. #include <linux/kernel.h>
  11. #include <linux/device.h>
  12. #include <linux/module.h>
  13. #include <linux/usb/composite.h>
  14. #include <linux/usb/func_utils.h>
  15. #include <linux/err.h>
  16. #include "g_zero.h"
  17. /*
  18. * SOURCE/SINK FUNCTION ... a primary testing vehicle for USB peripheral
  19. * controller drivers.
  20. *
  21. * This just sinks bulk packets OUT to the peripheral and sources them IN
  22. * to the host, optionally with specific data patterns for integrity tests.
  23. * As such it supports basic functionality and load tests.
  24. *
  25. * In terms of control messaging, this supports all the standard requests
  26. * plus two that support control-OUT tests. If the optional "autoresume"
  27. * mode is enabled, it provides good functional coverage for the "USBCV"
  28. * test harness from USB-IF.
  29. */
  30. struct f_sourcesink {
  31. struct usb_function function;
  32. struct usb_ep *in_ep;
  33. struct usb_ep *out_ep;
  34. struct usb_ep *iso_in_ep;
  35. struct usb_ep *iso_out_ep;
  36. int cur_alt;
  37. unsigned pattern;
  38. unsigned isoc_interval;
  39. unsigned isoc_maxpacket;
  40. unsigned isoc_mult;
  41. unsigned isoc_maxburst;
  42. unsigned buflen;
  43. unsigned bulk_qlen;
  44. unsigned iso_qlen;
  45. };
  46. static inline struct f_sourcesink *func_to_ss(struct usb_function *f)
  47. {
  48. return container_of(f, struct f_sourcesink, function);
  49. }
  50. /*-------------------------------------------------------------------------*/
  51. static struct usb_interface_descriptor source_sink_intf_alt0 = {
  52. .bLength = USB_DT_INTERFACE_SIZE,
  53. .bDescriptorType = USB_DT_INTERFACE,
  54. .bAlternateSetting = 0,
  55. .bNumEndpoints = 2,
  56. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  57. /* .iInterface = DYNAMIC */
  58. };
  59. static struct usb_interface_descriptor source_sink_intf_alt1 = {
  60. .bLength = USB_DT_INTERFACE_SIZE,
  61. .bDescriptorType = USB_DT_INTERFACE,
  62. .bAlternateSetting = 1,
  63. .bNumEndpoints = 4,
  64. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  65. /* .iInterface = DYNAMIC */
  66. };
  67. /* full speed support: */
  68. static struct usb_endpoint_descriptor fs_source_desc = {
  69. .bLength = USB_DT_ENDPOINT_SIZE,
  70. .bDescriptorType = USB_DT_ENDPOINT,
  71. .bEndpointAddress = USB_DIR_IN,
  72. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  73. };
  74. static struct usb_endpoint_descriptor fs_sink_desc = {
  75. .bLength = USB_DT_ENDPOINT_SIZE,
  76. .bDescriptorType = USB_DT_ENDPOINT,
  77. .bEndpointAddress = USB_DIR_OUT,
  78. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  79. };
  80. static struct usb_endpoint_descriptor fs_iso_source_desc = {
  81. .bLength = USB_DT_ENDPOINT_SIZE,
  82. .bDescriptorType = USB_DT_ENDPOINT,
  83. .bEndpointAddress = USB_DIR_IN,
  84. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  85. .wMaxPacketSize = cpu_to_le16(1023),
  86. .bInterval = 4,
  87. };
  88. static struct usb_endpoint_descriptor fs_iso_sink_desc = {
  89. .bLength = USB_DT_ENDPOINT_SIZE,
  90. .bDescriptorType = USB_DT_ENDPOINT,
  91. .bEndpointAddress = USB_DIR_OUT,
  92. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  93. .wMaxPacketSize = cpu_to_le16(1023),
  94. .bInterval = 4,
  95. };
  96. static struct usb_descriptor_header *fs_source_sink_descs[] = {
  97. (struct usb_descriptor_header *) &source_sink_intf_alt0,
  98. (struct usb_descriptor_header *) &fs_sink_desc,
  99. (struct usb_descriptor_header *) &fs_source_desc,
  100. (struct usb_descriptor_header *) &source_sink_intf_alt1,
  101. #define FS_ALT_IFC_1_OFFSET 3
  102. (struct usb_descriptor_header *) &fs_sink_desc,
  103. (struct usb_descriptor_header *) &fs_source_desc,
  104. (struct usb_descriptor_header *) &fs_iso_sink_desc,
  105. (struct usb_descriptor_header *) &fs_iso_source_desc,
  106. NULL,
  107. };
  108. /* high speed support: */
  109. static struct usb_endpoint_descriptor hs_source_desc = {
  110. .bLength = USB_DT_ENDPOINT_SIZE,
  111. .bDescriptorType = USB_DT_ENDPOINT,
  112. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  113. .wMaxPacketSize = cpu_to_le16(512),
  114. };
  115. static struct usb_endpoint_descriptor hs_sink_desc = {
  116. .bLength = USB_DT_ENDPOINT_SIZE,
  117. .bDescriptorType = USB_DT_ENDPOINT,
  118. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  119. .wMaxPacketSize = cpu_to_le16(512),
  120. };
  121. static struct usb_endpoint_descriptor hs_iso_source_desc = {
  122. .bLength = USB_DT_ENDPOINT_SIZE,
  123. .bDescriptorType = USB_DT_ENDPOINT,
  124. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  125. .wMaxPacketSize = cpu_to_le16(1024),
  126. .bInterval = 4,
  127. };
  128. static struct usb_endpoint_descriptor hs_iso_sink_desc = {
  129. .bLength = USB_DT_ENDPOINT_SIZE,
  130. .bDescriptorType = USB_DT_ENDPOINT,
  131. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  132. .wMaxPacketSize = cpu_to_le16(1024),
  133. .bInterval = 4,
  134. };
  135. static struct usb_descriptor_header *hs_source_sink_descs[] = {
  136. (struct usb_descriptor_header *) &source_sink_intf_alt0,
  137. (struct usb_descriptor_header *) &hs_source_desc,
  138. (struct usb_descriptor_header *) &hs_sink_desc,
  139. (struct usb_descriptor_header *) &source_sink_intf_alt1,
  140. #define HS_ALT_IFC_1_OFFSET 3
  141. (struct usb_descriptor_header *) &hs_source_desc,
  142. (struct usb_descriptor_header *) &hs_sink_desc,
  143. (struct usb_descriptor_header *) &hs_iso_source_desc,
  144. (struct usb_descriptor_header *) &hs_iso_sink_desc,
  145. NULL,
  146. };
  147. /* super speed support: */
  148. static struct usb_endpoint_descriptor ss_source_desc = {
  149. .bLength = USB_DT_ENDPOINT_SIZE,
  150. .bDescriptorType = USB_DT_ENDPOINT,
  151. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  152. .wMaxPacketSize = cpu_to_le16(1024),
  153. };
  154. static struct usb_ss_ep_comp_descriptor ss_source_comp_desc = {
  155. .bLength = USB_DT_SS_EP_COMP_SIZE,
  156. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  157. .bMaxBurst = 0,
  158. .bmAttributes = 0,
  159. .wBytesPerInterval = 0,
  160. };
  161. static struct usb_endpoint_descriptor ss_sink_desc = {
  162. .bLength = USB_DT_ENDPOINT_SIZE,
  163. .bDescriptorType = USB_DT_ENDPOINT,
  164. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  165. .wMaxPacketSize = cpu_to_le16(1024),
  166. };
  167. static struct usb_ss_ep_comp_descriptor ss_sink_comp_desc = {
  168. .bLength = USB_DT_SS_EP_COMP_SIZE,
  169. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  170. .bMaxBurst = 0,
  171. .bmAttributes = 0,
  172. .wBytesPerInterval = 0,
  173. };
  174. static struct usb_endpoint_descriptor ss_iso_source_desc = {
  175. .bLength = USB_DT_ENDPOINT_SIZE,
  176. .bDescriptorType = USB_DT_ENDPOINT,
  177. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  178. .wMaxPacketSize = cpu_to_le16(1024),
  179. .bInterval = 4,
  180. };
  181. static struct usb_ss_ep_comp_descriptor ss_iso_source_comp_desc = {
  182. .bLength = USB_DT_SS_EP_COMP_SIZE,
  183. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  184. .bMaxBurst = 0,
  185. .bmAttributes = 0,
  186. .wBytesPerInterval = cpu_to_le16(1024),
  187. };
  188. static struct usb_endpoint_descriptor ss_iso_sink_desc = {
  189. .bLength = USB_DT_ENDPOINT_SIZE,
  190. .bDescriptorType = USB_DT_ENDPOINT,
  191. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  192. .wMaxPacketSize = cpu_to_le16(1024),
  193. .bInterval = 4,
  194. };
  195. static struct usb_ss_ep_comp_descriptor ss_iso_sink_comp_desc = {
  196. .bLength = USB_DT_SS_EP_COMP_SIZE,
  197. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  198. .bMaxBurst = 0,
  199. .bmAttributes = 0,
  200. .wBytesPerInterval = cpu_to_le16(1024),
  201. };
  202. static struct usb_descriptor_header *ss_source_sink_descs[] = {
  203. (struct usb_descriptor_header *) &source_sink_intf_alt0,
  204. (struct usb_descriptor_header *) &ss_source_desc,
  205. (struct usb_descriptor_header *) &ss_source_comp_desc,
  206. (struct usb_descriptor_header *) &ss_sink_desc,
  207. (struct usb_descriptor_header *) &ss_sink_comp_desc,
  208. (struct usb_descriptor_header *) &source_sink_intf_alt1,
  209. #define SS_ALT_IFC_1_OFFSET 5
  210. (struct usb_descriptor_header *) &ss_source_desc,
  211. (struct usb_descriptor_header *) &ss_source_comp_desc,
  212. (struct usb_descriptor_header *) &ss_sink_desc,
  213. (struct usb_descriptor_header *) &ss_sink_comp_desc,
  214. (struct usb_descriptor_header *) &ss_iso_source_desc,
  215. (struct usb_descriptor_header *) &ss_iso_source_comp_desc,
  216. (struct usb_descriptor_header *) &ss_iso_sink_desc,
  217. (struct usb_descriptor_header *) &ss_iso_sink_comp_desc,
  218. NULL,
  219. };
  220. /* function-specific strings: */
  221. static struct usb_string strings_sourcesink[] = {
  222. [0].s = "source and sink data",
  223. { } /* end of list */
  224. };
  225. static struct usb_gadget_strings stringtab_sourcesink = {
  226. .language = 0x0409, /* en-us */
  227. .strings = strings_sourcesink,
  228. };
  229. static struct usb_gadget_strings *sourcesink_strings[] = {
  230. &stringtab_sourcesink,
  231. NULL,
  232. };
  233. /*-------------------------------------------------------------------------*/
  234. static inline struct usb_request *ss_alloc_ep_req(struct usb_ep *ep, int len)
  235. {
  236. return alloc_ep_req(ep, len);
  237. }
  238. static void disable_ep(struct usb_composite_dev *cdev, struct usb_ep *ep)
  239. {
  240. int value;
  241. value = usb_ep_disable(ep);
  242. if (value < 0)
  243. DBG(cdev, "disable %s --> %d\n", ep->name, value);
  244. }
  245. void disable_endpoints(struct usb_composite_dev *cdev,
  246. struct usb_ep *in, struct usb_ep *out,
  247. struct usb_ep *iso_in, struct usb_ep *iso_out)
  248. {
  249. disable_ep(cdev, in);
  250. disable_ep(cdev, out);
  251. if (iso_in)
  252. disable_ep(cdev, iso_in);
  253. if (iso_out)
  254. disable_ep(cdev, iso_out);
  255. }
  256. static int
  257. sourcesink_bind(struct usb_configuration *c, struct usb_function *f)
  258. {
  259. struct usb_composite_dev *cdev = c->cdev;
  260. struct f_sourcesink *ss = func_to_ss(f);
  261. int id;
  262. int ret;
  263. /* allocate interface ID(s) */
  264. id = usb_interface_id(c, f);
  265. if (id < 0)
  266. return id;
  267. source_sink_intf_alt0.bInterfaceNumber = id;
  268. source_sink_intf_alt1.bInterfaceNumber = id;
  269. /* allocate bulk endpoints */
  270. ss->in_ep = usb_ep_autoconfig(cdev->gadget, &fs_source_desc);
  271. if (!ss->in_ep) {
  272. autoconf_fail:
  273. ERROR(cdev, "%s: can't autoconfigure on %s\n",
  274. f->name, cdev->gadget->name);
  275. return -ENODEV;
  276. }
  277. ss->out_ep = usb_ep_autoconfig(cdev->gadget, &fs_sink_desc);
  278. if (!ss->out_ep)
  279. goto autoconf_fail;
  280. /* sanity check the isoc module parameters */
  281. if (ss->isoc_interval < 1)
  282. ss->isoc_interval = 1;
  283. if (ss->isoc_interval > 16)
  284. ss->isoc_interval = 16;
  285. if (ss->isoc_mult > 2)
  286. ss->isoc_mult = 2;
  287. if (ss->isoc_maxburst > 15)
  288. ss->isoc_maxburst = 15;
  289. /* fill in the FS isoc descriptors from the module parameters */
  290. fs_iso_source_desc.wMaxPacketSize = ss->isoc_maxpacket > 1023 ?
  291. 1023 : ss->isoc_maxpacket;
  292. fs_iso_source_desc.bInterval = ss->isoc_interval;
  293. fs_iso_sink_desc.wMaxPacketSize = ss->isoc_maxpacket > 1023 ?
  294. 1023 : ss->isoc_maxpacket;
  295. fs_iso_sink_desc.bInterval = ss->isoc_interval;
  296. /* allocate iso endpoints */
  297. ss->iso_in_ep = usb_ep_autoconfig(cdev->gadget, &fs_iso_source_desc);
  298. if (!ss->iso_in_ep)
  299. goto no_iso;
  300. ss->iso_out_ep = usb_ep_autoconfig(cdev->gadget, &fs_iso_sink_desc);
  301. if (!ss->iso_out_ep) {
  302. usb_ep_autoconfig_release(ss->iso_in_ep);
  303. ss->iso_in_ep = NULL;
  304. no_iso:
  305. /*
  306. * We still want to work even if the UDC doesn't have isoc
  307. * endpoints, so null out the alt interface that contains
  308. * them and continue.
  309. */
  310. fs_source_sink_descs[FS_ALT_IFC_1_OFFSET] = NULL;
  311. hs_source_sink_descs[HS_ALT_IFC_1_OFFSET] = NULL;
  312. ss_source_sink_descs[SS_ALT_IFC_1_OFFSET] = NULL;
  313. }
  314. if (ss->isoc_maxpacket > 1024)
  315. ss->isoc_maxpacket = 1024;
  316. /* support high speed hardware */
  317. hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
  318. hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
  319. /*
  320. * Fill in the HS isoc descriptors from the module parameters.
  321. * We assume that the user knows what they are doing and won't
  322. * give parameters that their UDC doesn't support.
  323. */
  324. hs_iso_source_desc.wMaxPacketSize = ss->isoc_maxpacket;
  325. hs_iso_source_desc.wMaxPacketSize |= ss->isoc_mult << 11;
  326. hs_iso_source_desc.bInterval = ss->isoc_interval;
  327. hs_iso_source_desc.bEndpointAddress =
  328. fs_iso_source_desc.bEndpointAddress;
  329. hs_iso_sink_desc.wMaxPacketSize = ss->isoc_maxpacket;
  330. hs_iso_sink_desc.wMaxPacketSize |= ss->isoc_mult << 11;
  331. hs_iso_sink_desc.bInterval = ss->isoc_interval;
  332. hs_iso_sink_desc.bEndpointAddress = fs_iso_sink_desc.bEndpointAddress;
  333. /* support super speed hardware */
  334. ss_source_desc.bEndpointAddress =
  335. fs_source_desc.bEndpointAddress;
  336. ss_sink_desc.bEndpointAddress =
  337. fs_sink_desc.bEndpointAddress;
  338. /*
  339. * Fill in the SS isoc descriptors from the module parameters.
  340. * We assume that the user knows what they are doing and won't
  341. * give parameters that their UDC doesn't support.
  342. */
  343. ss_iso_source_desc.wMaxPacketSize = ss->isoc_maxpacket;
  344. ss_iso_source_desc.bInterval = ss->isoc_interval;
  345. ss_iso_source_comp_desc.bmAttributes = ss->isoc_mult;
  346. ss_iso_source_comp_desc.bMaxBurst = ss->isoc_maxburst;
  347. ss_iso_source_comp_desc.wBytesPerInterval = ss->isoc_maxpacket *
  348. (ss->isoc_mult + 1) * (ss->isoc_maxburst + 1);
  349. ss_iso_source_desc.bEndpointAddress =
  350. fs_iso_source_desc.bEndpointAddress;
  351. ss_iso_sink_desc.wMaxPacketSize = ss->isoc_maxpacket;
  352. ss_iso_sink_desc.bInterval = ss->isoc_interval;
  353. ss_iso_sink_comp_desc.bmAttributes = ss->isoc_mult;
  354. ss_iso_sink_comp_desc.bMaxBurst = ss->isoc_maxburst;
  355. ss_iso_sink_comp_desc.wBytesPerInterval = ss->isoc_maxpacket *
  356. (ss->isoc_mult + 1) * (ss->isoc_maxburst + 1);
  357. ss_iso_sink_desc.bEndpointAddress = fs_iso_sink_desc.bEndpointAddress;
  358. ret = usb_assign_descriptors(f, fs_source_sink_descs,
  359. hs_source_sink_descs, ss_source_sink_descs,
  360. ss_source_sink_descs);
  361. if (ret)
  362. return ret;
  363. DBG(cdev, "%s: IN/%s, OUT/%s, ISO-IN/%s, ISO-OUT/%s\n",
  364. f->name, ss->in_ep->name, ss->out_ep->name,
  365. ss->iso_in_ep ? ss->iso_in_ep->name : "<none>",
  366. ss->iso_out_ep ? ss->iso_out_ep->name : "<none>");
  367. return 0;
  368. }
  369. static void
  370. sourcesink_free_func(struct usb_function *f)
  371. {
  372. struct f_ss_opts *opts;
  373. opts = container_of(f->fi, struct f_ss_opts, func_inst);
  374. mutex_lock(&opts->lock);
  375. opts->refcnt--;
  376. mutex_unlock(&opts->lock);
  377. usb_free_all_descriptors(f);
  378. kfree(func_to_ss(f));
  379. }
  380. /* optionally require specific source/sink data patterns */
  381. static int check_read_data(struct f_sourcesink *ss, struct usb_request *req)
  382. {
  383. unsigned i;
  384. u8 *buf = req->buf;
  385. struct usb_composite_dev *cdev = ss->function.config->cdev;
  386. int max_packet_size = le16_to_cpu(ss->out_ep->desc->wMaxPacketSize);
  387. if (ss->pattern == 2)
  388. return 0;
  389. for (i = 0; i < req->actual; i++, buf++) {
  390. switch (ss->pattern) {
  391. /* all-zeroes has no synchronization issues */
  392. case 0:
  393. if (*buf == 0)
  394. continue;
  395. break;
  396. /* "mod63" stays in sync with short-terminated transfers,
  397. * OR otherwise when host and gadget agree on how large
  398. * each usb transfer request should be. Resync is done
  399. * with set_interface or set_config. (We *WANT* it to
  400. * get quickly out of sync if controllers or their drivers
  401. * stutter for any reason, including buffer duplication...)
  402. */
  403. case 1:
  404. if (*buf == (u8)((i % max_packet_size) % 63))
  405. continue;
  406. break;
  407. }
  408. ERROR(cdev, "bad OUT byte, buf[%d] = %d\n", i, *buf);
  409. usb_ep_set_halt(ss->out_ep);
  410. return -EINVAL;
  411. }
  412. return 0;
  413. }
  414. static void reinit_write_data(struct usb_ep *ep, struct usb_request *req)
  415. {
  416. unsigned i;
  417. u8 *buf = req->buf;
  418. int max_packet_size = le16_to_cpu(ep->desc->wMaxPacketSize);
  419. struct f_sourcesink *ss = ep->driver_data;
  420. switch (ss->pattern) {
  421. case 0:
  422. memset(req->buf, 0, req->length);
  423. break;
  424. case 1:
  425. for (i = 0; i < req->length; i++)
  426. *buf++ = (u8) ((i % max_packet_size) % 63);
  427. break;
  428. case 2:
  429. break;
  430. }
  431. }
  432. static void source_sink_complete(struct usb_ep *ep, struct usb_request *req)
  433. {
  434. struct usb_composite_dev *cdev;
  435. struct f_sourcesink *ss = ep->driver_data;
  436. int status = req->status;
  437. /* driver_data will be null if ep has been disabled */
  438. if (!ss)
  439. return;
  440. cdev = ss->function.config->cdev;
  441. switch (status) {
  442. case 0: /* normal completion? */
  443. if (ep == ss->out_ep) {
  444. check_read_data(ss, req);
  445. if (ss->pattern != 2)
  446. memset(req->buf, 0x55, req->length);
  447. }
  448. break;
  449. /* this endpoint is normally active while we're configured */
  450. case -ECONNABORTED: /* hardware forced ep reset */
  451. case -ECONNRESET: /* request dequeued */
  452. case -ESHUTDOWN: /* disconnect from host */
  453. VDBG(cdev, "%s gone (%d), %d/%d\n", ep->name, status,
  454. req->actual, req->length);
  455. if (ep == ss->out_ep)
  456. check_read_data(ss, req);
  457. free_ep_req(ep, req);
  458. return;
  459. case -EOVERFLOW: /* buffer overrun on read means that
  460. * we didn't provide a big enough
  461. * buffer.
  462. */
  463. default:
  464. #if 1
  465. DBG(cdev, "%s complete --> %d, %d/%d\n", ep->name,
  466. status, req->actual, req->length);
  467. break;
  468. #endif
  469. case -EREMOTEIO: /* short read */
  470. break;
  471. }
  472. status = usb_ep_queue(ep, req, GFP_ATOMIC);
  473. if (status) {
  474. ERROR(cdev, "kill %s: resubmit %d bytes --> %d\n",
  475. ep->name, req->length, status);
  476. usb_ep_set_halt(ep);
  477. /* FIXME recover later ... somehow */
  478. }
  479. }
  480. static int source_sink_start_ep(struct f_sourcesink *ss, bool is_in,
  481. bool is_iso, int speed)
  482. {
  483. struct usb_ep *ep;
  484. struct usb_request *req;
  485. int i, size, qlen, status = 0;
  486. if (is_iso) {
  487. switch (speed) {
  488. case USB_SPEED_SUPER_PLUS:
  489. case USB_SPEED_SUPER:
  490. size = ss->isoc_maxpacket *
  491. (ss->isoc_mult + 1) *
  492. (ss->isoc_maxburst + 1);
  493. break;
  494. case USB_SPEED_HIGH:
  495. size = ss->isoc_maxpacket * (ss->isoc_mult + 1);
  496. break;
  497. default:
  498. size = ss->isoc_maxpacket > 1023 ?
  499. 1023 : ss->isoc_maxpacket;
  500. break;
  501. }
  502. ep = is_in ? ss->iso_in_ep : ss->iso_out_ep;
  503. qlen = ss->iso_qlen;
  504. } else {
  505. ep = is_in ? ss->in_ep : ss->out_ep;
  506. qlen = ss->bulk_qlen;
  507. size = ss->buflen;
  508. }
  509. for (i = 0; i < qlen; i++) {
  510. req = ss_alloc_ep_req(ep, size);
  511. if (!req)
  512. return -ENOMEM;
  513. req->complete = source_sink_complete;
  514. if (is_in)
  515. reinit_write_data(ep, req);
  516. else if (ss->pattern != 2)
  517. memset(req->buf, 0x55, req->length);
  518. status = usb_ep_queue(ep, req, GFP_ATOMIC);
  519. if (status) {
  520. struct usb_composite_dev *cdev;
  521. cdev = ss->function.config->cdev;
  522. ERROR(cdev, "start %s%s %s --> %d\n",
  523. is_iso ? "ISO-" : "", is_in ? "IN" : "OUT",
  524. ep->name, status);
  525. free_ep_req(ep, req);
  526. return status;
  527. }
  528. }
  529. return status;
  530. }
  531. static void disable_source_sink(struct f_sourcesink *ss)
  532. {
  533. struct usb_composite_dev *cdev;
  534. cdev = ss->function.config->cdev;
  535. disable_endpoints(cdev, ss->in_ep, ss->out_ep, ss->iso_in_ep,
  536. ss->iso_out_ep);
  537. VDBG(cdev, "%s disabled\n", ss->function.name);
  538. }
  539. static int
  540. enable_source_sink(struct usb_composite_dev *cdev, struct f_sourcesink *ss,
  541. int alt)
  542. {
  543. int result = 0;
  544. int speed = cdev->gadget->speed;
  545. struct usb_ep *ep;
  546. /* one bulk endpoint writes (sources) zeroes IN (to the host) */
  547. ep = ss->in_ep;
  548. result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
  549. if (result)
  550. return result;
  551. result = usb_ep_enable(ep);
  552. if (result < 0)
  553. return result;
  554. ep->driver_data = ss;
  555. result = source_sink_start_ep(ss, true, false, speed);
  556. if (result < 0) {
  557. fail:
  558. ep = ss->in_ep;
  559. usb_ep_disable(ep);
  560. return result;
  561. }
  562. /* one bulk endpoint reads (sinks) anything OUT (from the host) */
  563. ep = ss->out_ep;
  564. result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
  565. if (result)
  566. goto fail;
  567. result = usb_ep_enable(ep);
  568. if (result < 0)
  569. goto fail;
  570. ep->driver_data = ss;
  571. result = source_sink_start_ep(ss, false, false, speed);
  572. if (result < 0) {
  573. fail2:
  574. ep = ss->out_ep;
  575. usb_ep_disable(ep);
  576. goto fail;
  577. }
  578. if (alt == 0)
  579. goto out;
  580. /* one iso endpoint writes (sources) zeroes IN (to the host) */
  581. ep = ss->iso_in_ep;
  582. if (ep) {
  583. result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
  584. if (result)
  585. goto fail2;
  586. result = usb_ep_enable(ep);
  587. if (result < 0)
  588. goto fail2;
  589. ep->driver_data = ss;
  590. result = source_sink_start_ep(ss, true, true, speed);
  591. if (result < 0) {
  592. fail3:
  593. ep = ss->iso_in_ep;
  594. if (ep)
  595. usb_ep_disable(ep);
  596. goto fail2;
  597. }
  598. }
  599. /* one iso endpoint reads (sinks) anything OUT (from the host) */
  600. ep = ss->iso_out_ep;
  601. if (ep) {
  602. result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
  603. if (result)
  604. goto fail3;
  605. result = usb_ep_enable(ep);
  606. if (result < 0)
  607. goto fail3;
  608. ep->driver_data = ss;
  609. result = source_sink_start_ep(ss, false, true, speed);
  610. if (result < 0) {
  611. usb_ep_disable(ep);
  612. goto fail3;
  613. }
  614. }
  615. out:
  616. ss->cur_alt = alt;
  617. DBG(cdev, "%s enabled, alt intf %d\n", ss->function.name, alt);
  618. return result;
  619. }
  620. static int sourcesink_set_alt(struct usb_function *f,
  621. unsigned intf, unsigned alt)
  622. {
  623. struct f_sourcesink *ss = func_to_ss(f);
  624. struct usb_composite_dev *cdev = f->config->cdev;
  625. disable_source_sink(ss);
  626. return enable_source_sink(cdev, ss, alt);
  627. }
  628. static int sourcesink_get_alt(struct usb_function *f, unsigned intf)
  629. {
  630. struct f_sourcesink *ss = func_to_ss(f);
  631. return ss->cur_alt;
  632. }
  633. static void sourcesink_disable(struct usb_function *f)
  634. {
  635. struct f_sourcesink *ss = func_to_ss(f);
  636. disable_source_sink(ss);
  637. }
  638. /*-------------------------------------------------------------------------*/
  639. static int sourcesink_setup(struct usb_function *f,
  640. const struct usb_ctrlrequest *ctrl)
  641. {
  642. struct usb_configuration *c = f->config;
  643. struct usb_request *req = c->cdev->req;
  644. int value = -EOPNOTSUPP;
  645. u16 w_index = le16_to_cpu(ctrl->wIndex);
  646. u16 w_value = le16_to_cpu(ctrl->wValue);
  647. u16 w_length = le16_to_cpu(ctrl->wLength);
  648. req->length = USB_COMP_EP0_BUFSIZ;
  649. /* composite driver infrastructure handles everything except
  650. * the two control test requests.
  651. */
  652. switch (ctrl->bRequest) {
  653. /*
  654. * These are the same vendor-specific requests supported by
  655. * Intel's USB 2.0 compliance test devices. We exceed that
  656. * device spec by allowing multiple-packet requests.
  657. *
  658. * NOTE: the Control-OUT data stays in req->buf ... better
  659. * would be copying it into a scratch buffer, so that other
  660. * requests may safely intervene.
  661. */
  662. case 0x5b: /* control WRITE test -- fill the buffer */
  663. if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
  664. goto unknown;
  665. if (w_value || w_index)
  666. break;
  667. /* just read that many bytes into the buffer */
  668. if (w_length > req->length)
  669. break;
  670. value = w_length;
  671. break;
  672. case 0x5c: /* control READ test -- return the buffer */
  673. if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
  674. goto unknown;
  675. if (w_value || w_index)
  676. break;
  677. /* expect those bytes are still in the buffer; send back */
  678. if (w_length > req->length)
  679. break;
  680. value = w_length;
  681. break;
  682. default:
  683. unknown:
  684. VDBG(c->cdev,
  685. "unknown control req%02x.%02x v%04x i%04x l%d\n",
  686. ctrl->bRequestType, ctrl->bRequest,
  687. w_value, w_index, w_length);
  688. }
  689. /* respond with data transfer or status phase? */
  690. if (value >= 0) {
  691. VDBG(c->cdev, "source/sink req%02x.%02x v%04x i%04x l%d\n",
  692. ctrl->bRequestType, ctrl->bRequest,
  693. w_value, w_index, w_length);
  694. req->zero = 0;
  695. req->length = value;
  696. value = usb_ep_queue(c->cdev->gadget->ep0, req, GFP_ATOMIC);
  697. if (value < 0)
  698. ERROR(c->cdev, "source/sink response, err %d\n",
  699. value);
  700. }
  701. /* device either stalls (value < 0) or reports success */
  702. return value;
  703. }
  704. static struct usb_function *source_sink_alloc_func(
  705. struct usb_function_instance *fi)
  706. {
  707. struct f_sourcesink *ss;
  708. struct f_ss_opts *ss_opts;
  709. ss = kzalloc(sizeof(*ss), GFP_KERNEL);
  710. if (!ss)
  711. return ERR_PTR(-ENOMEM);
  712. ss_opts = container_of(fi, struct f_ss_opts, func_inst);
  713. mutex_lock(&ss_opts->lock);
  714. ss_opts->refcnt++;
  715. mutex_unlock(&ss_opts->lock);
  716. ss->pattern = ss_opts->pattern;
  717. ss->isoc_interval = ss_opts->isoc_interval;
  718. ss->isoc_maxpacket = ss_opts->isoc_maxpacket;
  719. ss->isoc_mult = ss_opts->isoc_mult;
  720. ss->isoc_maxburst = ss_opts->isoc_maxburst;
  721. ss->buflen = ss_opts->bulk_buflen;
  722. ss->bulk_qlen = ss_opts->bulk_qlen;
  723. ss->iso_qlen = ss_opts->iso_qlen;
  724. ss->function.name = "source/sink";
  725. ss->function.bind = sourcesink_bind;
  726. ss->function.set_alt = sourcesink_set_alt;
  727. ss->function.get_alt = sourcesink_get_alt;
  728. ss->function.disable = sourcesink_disable;
  729. ss->function.setup = sourcesink_setup;
  730. ss->function.strings = sourcesink_strings;
  731. ss->function.free_func = sourcesink_free_func;
  732. return &ss->function;
  733. }
  734. static inline struct f_ss_opts *to_f_ss_opts(struct config_item *item)
  735. {
  736. return container_of(to_config_group(item), struct f_ss_opts,
  737. func_inst.group);
  738. }
  739. static void ss_attr_release(struct config_item *item)
  740. {
  741. struct f_ss_opts *ss_opts = to_f_ss_opts(item);
  742. usb_put_function_instance(&ss_opts->func_inst);
  743. }
  744. static struct configfs_item_operations ss_item_ops = {
  745. .release = ss_attr_release,
  746. };
  747. static ssize_t f_ss_opts_pattern_show(struct config_item *item, char *page)
  748. {
  749. struct f_ss_opts *opts = to_f_ss_opts(item);
  750. int result;
  751. mutex_lock(&opts->lock);
  752. result = sprintf(page, "%u\n", opts->pattern);
  753. mutex_unlock(&opts->lock);
  754. return result;
  755. }
  756. static ssize_t f_ss_opts_pattern_store(struct config_item *item,
  757. const char *page, size_t len)
  758. {
  759. struct f_ss_opts *opts = to_f_ss_opts(item);
  760. int ret;
  761. u8 num;
  762. mutex_lock(&opts->lock);
  763. if (opts->refcnt) {
  764. ret = -EBUSY;
  765. goto end;
  766. }
  767. ret = kstrtou8(page, 0, &num);
  768. if (ret)
  769. goto end;
  770. if (num != 0 && num != 1 && num != 2) {
  771. ret = -EINVAL;
  772. goto end;
  773. }
  774. opts->pattern = num;
  775. ret = len;
  776. end:
  777. mutex_unlock(&opts->lock);
  778. return ret;
  779. }
  780. CONFIGFS_ATTR(f_ss_opts_, pattern);
  781. static ssize_t f_ss_opts_isoc_interval_show(struct config_item *item, char *page)
  782. {
  783. struct f_ss_opts *opts = to_f_ss_opts(item);
  784. int result;
  785. mutex_lock(&opts->lock);
  786. result = sprintf(page, "%u\n", opts->isoc_interval);
  787. mutex_unlock(&opts->lock);
  788. return result;
  789. }
  790. static ssize_t f_ss_opts_isoc_interval_store(struct config_item *item,
  791. const char *page, size_t len)
  792. {
  793. struct f_ss_opts *opts = to_f_ss_opts(item);
  794. int ret;
  795. u8 num;
  796. mutex_lock(&opts->lock);
  797. if (opts->refcnt) {
  798. ret = -EBUSY;
  799. goto end;
  800. }
  801. ret = kstrtou8(page, 0, &num);
  802. if (ret)
  803. goto end;
  804. if (num > 16) {
  805. ret = -EINVAL;
  806. goto end;
  807. }
  808. opts->isoc_interval = num;
  809. ret = len;
  810. end:
  811. mutex_unlock(&opts->lock);
  812. return ret;
  813. }
  814. CONFIGFS_ATTR(f_ss_opts_, isoc_interval);
  815. static ssize_t f_ss_opts_isoc_maxpacket_show(struct config_item *item, char *page)
  816. {
  817. struct f_ss_opts *opts = to_f_ss_opts(item);
  818. int result;
  819. mutex_lock(&opts->lock);
  820. result = sprintf(page, "%u\n", opts->isoc_maxpacket);
  821. mutex_unlock(&opts->lock);
  822. return result;
  823. }
  824. static ssize_t f_ss_opts_isoc_maxpacket_store(struct config_item *item,
  825. const char *page, size_t len)
  826. {
  827. struct f_ss_opts *opts = to_f_ss_opts(item);
  828. int ret;
  829. u16 num;
  830. mutex_lock(&opts->lock);
  831. if (opts->refcnt) {
  832. ret = -EBUSY;
  833. goto end;
  834. }
  835. ret = kstrtou16(page, 0, &num);
  836. if (ret)
  837. goto end;
  838. if (num > 1024) {
  839. ret = -EINVAL;
  840. goto end;
  841. }
  842. opts->isoc_maxpacket = num;
  843. ret = len;
  844. end:
  845. mutex_unlock(&opts->lock);
  846. return ret;
  847. }
  848. CONFIGFS_ATTR(f_ss_opts_, isoc_maxpacket);
  849. static ssize_t f_ss_opts_isoc_mult_show(struct config_item *item, char *page)
  850. {
  851. struct f_ss_opts *opts = to_f_ss_opts(item);
  852. int result;
  853. mutex_lock(&opts->lock);
  854. result = sprintf(page, "%u\n", opts->isoc_mult);
  855. mutex_unlock(&opts->lock);
  856. return result;
  857. }
  858. static ssize_t f_ss_opts_isoc_mult_store(struct config_item *item,
  859. const char *page, size_t len)
  860. {
  861. struct f_ss_opts *opts = to_f_ss_opts(item);
  862. int ret;
  863. u8 num;
  864. mutex_lock(&opts->lock);
  865. if (opts->refcnt) {
  866. ret = -EBUSY;
  867. goto end;
  868. }
  869. ret = kstrtou8(page, 0, &num);
  870. if (ret)
  871. goto end;
  872. if (num > 2) {
  873. ret = -EINVAL;
  874. goto end;
  875. }
  876. opts->isoc_mult = num;
  877. ret = len;
  878. end:
  879. mutex_unlock(&opts->lock);
  880. return ret;
  881. }
  882. CONFIGFS_ATTR(f_ss_opts_, isoc_mult);
  883. static ssize_t f_ss_opts_isoc_maxburst_show(struct config_item *item, char *page)
  884. {
  885. struct f_ss_opts *opts = to_f_ss_opts(item);
  886. int result;
  887. mutex_lock(&opts->lock);
  888. result = sprintf(page, "%u\n", opts->isoc_maxburst);
  889. mutex_unlock(&opts->lock);
  890. return result;
  891. }
  892. static ssize_t f_ss_opts_isoc_maxburst_store(struct config_item *item,
  893. const char *page, size_t len)
  894. {
  895. struct f_ss_opts *opts = to_f_ss_opts(item);
  896. int ret;
  897. u8 num;
  898. mutex_lock(&opts->lock);
  899. if (opts->refcnt) {
  900. ret = -EBUSY;
  901. goto end;
  902. }
  903. ret = kstrtou8(page, 0, &num);
  904. if (ret)
  905. goto end;
  906. if (num > 15) {
  907. ret = -EINVAL;
  908. goto end;
  909. }
  910. opts->isoc_maxburst = num;
  911. ret = len;
  912. end:
  913. mutex_unlock(&opts->lock);
  914. return ret;
  915. }
  916. CONFIGFS_ATTR(f_ss_opts_, isoc_maxburst);
  917. static ssize_t f_ss_opts_bulk_buflen_show(struct config_item *item, char *page)
  918. {
  919. struct f_ss_opts *opts = to_f_ss_opts(item);
  920. int result;
  921. mutex_lock(&opts->lock);
  922. result = sprintf(page, "%u\n", opts->bulk_buflen);
  923. mutex_unlock(&opts->lock);
  924. return result;
  925. }
  926. static ssize_t f_ss_opts_bulk_buflen_store(struct config_item *item,
  927. const char *page, size_t len)
  928. {
  929. struct f_ss_opts *opts = to_f_ss_opts(item);
  930. int ret;
  931. u32 num;
  932. mutex_lock(&opts->lock);
  933. if (opts->refcnt) {
  934. ret = -EBUSY;
  935. goto end;
  936. }
  937. ret = kstrtou32(page, 0, &num);
  938. if (ret)
  939. goto end;
  940. opts->bulk_buflen = num;
  941. ret = len;
  942. end:
  943. mutex_unlock(&opts->lock);
  944. return ret;
  945. }
  946. CONFIGFS_ATTR(f_ss_opts_, bulk_buflen);
  947. static ssize_t f_ss_opts_bulk_qlen_show(struct config_item *item, char *page)
  948. {
  949. struct f_ss_opts *opts = to_f_ss_opts(item);
  950. int result;
  951. mutex_lock(&opts->lock);
  952. result = sprintf(page, "%u\n", opts->bulk_qlen);
  953. mutex_unlock(&opts->lock);
  954. return result;
  955. }
  956. static ssize_t f_ss_opts_bulk_qlen_store(struct config_item *item,
  957. const char *page, size_t len)
  958. {
  959. struct f_ss_opts *opts = to_f_ss_opts(item);
  960. int ret;
  961. u32 num;
  962. mutex_lock(&opts->lock);
  963. if (opts->refcnt) {
  964. ret = -EBUSY;
  965. goto end;
  966. }
  967. ret = kstrtou32(page, 0, &num);
  968. if (ret)
  969. goto end;
  970. opts->bulk_qlen = num;
  971. ret = len;
  972. end:
  973. mutex_unlock(&opts->lock);
  974. return ret;
  975. }
  976. CONFIGFS_ATTR(f_ss_opts_, bulk_qlen);
  977. static ssize_t f_ss_opts_iso_qlen_show(struct config_item *item, char *page)
  978. {
  979. struct f_ss_opts *opts = to_f_ss_opts(item);
  980. int result;
  981. mutex_lock(&opts->lock);
  982. result = sprintf(page, "%u\n", opts->iso_qlen);
  983. mutex_unlock(&opts->lock);
  984. return result;
  985. }
  986. static ssize_t f_ss_opts_iso_qlen_store(struct config_item *item,
  987. const char *page, size_t len)
  988. {
  989. struct f_ss_opts *opts = to_f_ss_opts(item);
  990. int ret;
  991. u32 num;
  992. mutex_lock(&opts->lock);
  993. if (opts->refcnt) {
  994. ret = -EBUSY;
  995. goto end;
  996. }
  997. ret = kstrtou32(page, 0, &num);
  998. if (ret)
  999. goto end;
  1000. opts->iso_qlen = num;
  1001. ret = len;
  1002. end:
  1003. mutex_unlock(&opts->lock);
  1004. return ret;
  1005. }
  1006. CONFIGFS_ATTR(f_ss_opts_, iso_qlen);
  1007. static struct configfs_attribute *ss_attrs[] = {
  1008. &f_ss_opts_attr_pattern,
  1009. &f_ss_opts_attr_isoc_interval,
  1010. &f_ss_opts_attr_isoc_maxpacket,
  1011. &f_ss_opts_attr_isoc_mult,
  1012. &f_ss_opts_attr_isoc_maxburst,
  1013. &f_ss_opts_attr_bulk_buflen,
  1014. &f_ss_opts_attr_bulk_qlen,
  1015. &f_ss_opts_attr_iso_qlen,
  1016. NULL,
  1017. };
  1018. static const struct config_item_type ss_func_type = {
  1019. .ct_item_ops = &ss_item_ops,
  1020. .ct_attrs = ss_attrs,
  1021. .ct_owner = THIS_MODULE,
  1022. };
  1023. static void source_sink_free_instance(struct usb_function_instance *fi)
  1024. {
  1025. struct f_ss_opts *ss_opts;
  1026. ss_opts = container_of(fi, struct f_ss_opts, func_inst);
  1027. kfree(ss_opts);
  1028. }
  1029. static struct usb_function_instance *source_sink_alloc_inst(void)
  1030. {
  1031. struct f_ss_opts *ss_opts;
  1032. ss_opts = kzalloc(sizeof(*ss_opts), GFP_KERNEL);
  1033. if (!ss_opts)
  1034. return ERR_PTR(-ENOMEM);
  1035. mutex_init(&ss_opts->lock);
  1036. ss_opts->func_inst.free_func_inst = source_sink_free_instance;
  1037. ss_opts->isoc_interval = GZERO_ISOC_INTERVAL;
  1038. ss_opts->isoc_maxpacket = GZERO_ISOC_MAXPACKET;
  1039. ss_opts->bulk_buflen = GZERO_BULK_BUFLEN;
  1040. ss_opts->bulk_qlen = GZERO_SS_BULK_QLEN;
  1041. ss_opts->iso_qlen = GZERO_SS_ISO_QLEN;
  1042. config_group_init_type_name(&ss_opts->func_inst.group, "",
  1043. &ss_func_type);
  1044. return &ss_opts->func_inst;
  1045. }
  1046. DECLARE_USB_FUNCTION(SourceSink, source_sink_alloc_inst,
  1047. source_sink_alloc_func);
  1048. static int __init sslb_modinit(void)
  1049. {
  1050. int ret;
  1051. ret = usb_function_register(&SourceSinkusb_func);
  1052. if (ret)
  1053. return ret;
  1054. ret = lb_modinit();
  1055. if (ret)
  1056. usb_function_unregister(&SourceSinkusb_func);
  1057. return ret;
  1058. }
  1059. static void __exit sslb_modexit(void)
  1060. {
  1061. usb_function_unregister(&SourceSinkusb_func);
  1062. lb_modexit();
  1063. }
  1064. module_init(sslb_modinit);
  1065. module_exit(sslb_modexit);
  1066. MODULE_DESCRIPTION("USB peripheral source/sink configuration driver");
  1067. MODULE_LICENSE("GPL");