f_sourcesink.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287
  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/err.h>
  15. #include "g_zero.h"
  16. #include "u_f.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, NULL);
  360. if (ret)
  361. return ret;
  362. DBG(cdev, "%s speed %s: IN/%s, OUT/%s, ISO-IN/%s, ISO-OUT/%s\n",
  363. (gadget_is_superspeed(c->cdev->gadget) ? "super" :
  364. (gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full")),
  365. f->name, ss->in_ep->name, ss->out_ep->name,
  366. ss->iso_in_ep ? ss->iso_in_ep->name : "<none>",
  367. ss->iso_out_ep ? ss->iso_out_ep->name : "<none>");
  368. return 0;
  369. }
  370. static void
  371. sourcesink_free_func(struct usb_function *f)
  372. {
  373. struct f_ss_opts *opts;
  374. opts = container_of(f->fi, struct f_ss_opts, func_inst);
  375. mutex_lock(&opts->lock);
  376. opts->refcnt--;
  377. mutex_unlock(&opts->lock);
  378. usb_free_all_descriptors(f);
  379. kfree(func_to_ss(f));
  380. }
  381. /* optionally require specific source/sink data patterns */
  382. static int check_read_data(struct f_sourcesink *ss, struct usb_request *req)
  383. {
  384. unsigned i;
  385. u8 *buf = req->buf;
  386. struct usb_composite_dev *cdev = ss->function.config->cdev;
  387. int max_packet_size = le16_to_cpu(ss->out_ep->desc->wMaxPacketSize);
  388. if (ss->pattern == 2)
  389. return 0;
  390. for (i = 0; i < req->actual; i++, buf++) {
  391. switch (ss->pattern) {
  392. /* all-zeroes has no synchronization issues */
  393. case 0:
  394. if (*buf == 0)
  395. continue;
  396. break;
  397. /* "mod63" stays in sync with short-terminated transfers,
  398. * OR otherwise when host and gadget agree on how large
  399. * each usb transfer request should be. Resync is done
  400. * with set_interface or set_config. (We *WANT* it to
  401. * get quickly out of sync if controllers or their drivers
  402. * stutter for any reason, including buffer duplication...)
  403. */
  404. case 1:
  405. if (*buf == (u8)((i % max_packet_size) % 63))
  406. continue;
  407. break;
  408. }
  409. ERROR(cdev, "bad OUT byte, buf[%d] = %d\n", i, *buf);
  410. usb_ep_set_halt(ss->out_ep);
  411. return -EINVAL;
  412. }
  413. return 0;
  414. }
  415. static void reinit_write_data(struct usb_ep *ep, struct usb_request *req)
  416. {
  417. unsigned i;
  418. u8 *buf = req->buf;
  419. int max_packet_size = le16_to_cpu(ep->desc->wMaxPacketSize);
  420. struct f_sourcesink *ss = ep->driver_data;
  421. switch (ss->pattern) {
  422. case 0:
  423. memset(req->buf, 0, req->length);
  424. break;
  425. case 1:
  426. for (i = 0; i < req->length; i++)
  427. *buf++ = (u8) ((i % max_packet_size) % 63);
  428. break;
  429. case 2:
  430. break;
  431. }
  432. }
  433. static void source_sink_complete(struct usb_ep *ep, struct usb_request *req)
  434. {
  435. struct usb_composite_dev *cdev;
  436. struct f_sourcesink *ss = ep->driver_data;
  437. int status = req->status;
  438. /* driver_data will be null if ep has been disabled */
  439. if (!ss)
  440. return;
  441. cdev = ss->function.config->cdev;
  442. switch (status) {
  443. case 0: /* normal completion? */
  444. if (ep == ss->out_ep) {
  445. check_read_data(ss, req);
  446. if (ss->pattern != 2)
  447. memset(req->buf, 0x55, req->length);
  448. }
  449. break;
  450. /* this endpoint is normally active while we're configured */
  451. case -ECONNABORTED: /* hardware forced ep reset */
  452. case -ECONNRESET: /* request dequeued */
  453. case -ESHUTDOWN: /* disconnect from host */
  454. VDBG(cdev, "%s gone (%d), %d/%d\n", ep->name, status,
  455. req->actual, req->length);
  456. if (ep == ss->out_ep)
  457. check_read_data(ss, req);
  458. free_ep_req(ep, req);
  459. return;
  460. case -EOVERFLOW: /* buffer overrun on read means that
  461. * we didn't provide a big enough
  462. * buffer.
  463. */
  464. default:
  465. #if 1
  466. DBG(cdev, "%s complete --> %d, %d/%d\n", ep->name,
  467. status, req->actual, req->length);
  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:
  489. size = ss->isoc_maxpacket *
  490. (ss->isoc_mult + 1) *
  491. (ss->isoc_maxburst + 1);
  492. break;
  493. case USB_SPEED_HIGH:
  494. size = ss->isoc_maxpacket * (ss->isoc_mult + 1);
  495. break;
  496. default:
  497. size = ss->isoc_maxpacket > 1023 ?
  498. 1023 : ss->isoc_maxpacket;
  499. break;
  500. }
  501. ep = is_in ? ss->iso_in_ep : ss->iso_out_ep;
  502. qlen = ss->iso_qlen;
  503. } else {
  504. ep = is_in ? ss->in_ep : ss->out_ep;
  505. qlen = ss->bulk_qlen;
  506. size = ss->buflen;
  507. }
  508. for (i = 0; i < qlen; i++) {
  509. req = ss_alloc_ep_req(ep, size);
  510. if (!req)
  511. return -ENOMEM;
  512. req->complete = source_sink_complete;
  513. if (is_in)
  514. reinit_write_data(ep, req);
  515. else if (ss->pattern != 2)
  516. memset(req->buf, 0x55, req->length);
  517. status = usb_ep_queue(ep, req, GFP_ATOMIC);
  518. if (status) {
  519. struct usb_composite_dev *cdev;
  520. cdev = ss->function.config->cdev;
  521. ERROR(cdev, "start %s%s %s --> %d\n",
  522. is_iso ? "ISO-" : "", is_in ? "IN" : "OUT",
  523. ep->name, status);
  524. free_ep_req(ep, req);
  525. return status;
  526. }
  527. }
  528. return status;
  529. }
  530. static void disable_source_sink(struct f_sourcesink *ss)
  531. {
  532. struct usb_composite_dev *cdev;
  533. cdev = ss->function.config->cdev;
  534. disable_endpoints(cdev, ss->in_ep, ss->out_ep, ss->iso_in_ep,
  535. ss->iso_out_ep);
  536. VDBG(cdev, "%s disabled\n", ss->function.name);
  537. }
  538. static int
  539. enable_source_sink(struct usb_composite_dev *cdev, struct f_sourcesink *ss,
  540. int alt)
  541. {
  542. int result = 0;
  543. int speed = cdev->gadget->speed;
  544. struct usb_ep *ep;
  545. /* one bulk endpoint writes (sources) zeroes IN (to the host) */
  546. ep = ss->in_ep;
  547. result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
  548. if (result)
  549. return result;
  550. result = usb_ep_enable(ep);
  551. if (result < 0)
  552. return result;
  553. ep->driver_data = ss;
  554. result = source_sink_start_ep(ss, true, false, speed);
  555. if (result < 0) {
  556. fail:
  557. ep = ss->in_ep;
  558. usb_ep_disable(ep);
  559. return result;
  560. }
  561. /* one bulk endpoint reads (sinks) anything OUT (from the host) */
  562. ep = ss->out_ep;
  563. result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
  564. if (result)
  565. goto fail;
  566. result = usb_ep_enable(ep);
  567. if (result < 0)
  568. goto fail;
  569. ep->driver_data = ss;
  570. result = source_sink_start_ep(ss, false, false, speed);
  571. if (result < 0) {
  572. fail2:
  573. ep = ss->out_ep;
  574. usb_ep_disable(ep);
  575. goto fail;
  576. }
  577. if (alt == 0)
  578. goto out;
  579. /* one iso endpoint writes (sources) zeroes IN (to the host) */
  580. ep = ss->iso_in_ep;
  581. if (ep) {
  582. result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
  583. if (result)
  584. goto fail2;
  585. result = usb_ep_enable(ep);
  586. if (result < 0)
  587. goto fail2;
  588. ep->driver_data = ss;
  589. result = source_sink_start_ep(ss, true, true, speed);
  590. if (result < 0) {
  591. fail3:
  592. ep = ss->iso_in_ep;
  593. if (ep)
  594. usb_ep_disable(ep);
  595. goto fail2;
  596. }
  597. }
  598. /* one iso endpoint reads (sinks) anything OUT (from the host) */
  599. ep = ss->iso_out_ep;
  600. if (ep) {
  601. result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
  602. if (result)
  603. goto fail3;
  604. result = usb_ep_enable(ep);
  605. if (result < 0)
  606. goto fail3;
  607. ep->driver_data = ss;
  608. result = source_sink_start_ep(ss, false, true, speed);
  609. if (result < 0) {
  610. usb_ep_disable(ep);
  611. goto fail3;
  612. }
  613. }
  614. out:
  615. ss->cur_alt = alt;
  616. DBG(cdev, "%s enabled, alt intf %d\n", ss->function.name, alt);
  617. return result;
  618. }
  619. static int sourcesink_set_alt(struct usb_function *f,
  620. unsigned intf, unsigned alt)
  621. {
  622. struct f_sourcesink *ss = func_to_ss(f);
  623. struct usb_composite_dev *cdev = f->config->cdev;
  624. disable_source_sink(ss);
  625. return enable_source_sink(cdev, ss, alt);
  626. }
  627. static int sourcesink_get_alt(struct usb_function *f, unsigned intf)
  628. {
  629. struct f_sourcesink *ss = func_to_ss(f);
  630. return ss->cur_alt;
  631. }
  632. static void sourcesink_disable(struct usb_function *f)
  633. {
  634. struct f_sourcesink *ss = func_to_ss(f);
  635. disable_source_sink(ss);
  636. }
  637. /*-------------------------------------------------------------------------*/
  638. static int sourcesink_setup(struct usb_function *f,
  639. const struct usb_ctrlrequest *ctrl)
  640. {
  641. struct usb_configuration *c = f->config;
  642. struct usb_request *req = c->cdev->req;
  643. int value = -EOPNOTSUPP;
  644. u16 w_index = le16_to_cpu(ctrl->wIndex);
  645. u16 w_value = le16_to_cpu(ctrl->wValue);
  646. u16 w_length = le16_to_cpu(ctrl->wLength);
  647. req->length = USB_COMP_EP0_BUFSIZ;
  648. /* composite driver infrastructure handles everything except
  649. * the two control test requests.
  650. */
  651. switch (ctrl->bRequest) {
  652. /*
  653. * These are the same vendor-specific requests supported by
  654. * Intel's USB 2.0 compliance test devices. We exceed that
  655. * device spec by allowing multiple-packet requests.
  656. *
  657. * NOTE: the Control-OUT data stays in req->buf ... better
  658. * would be copying it into a scratch buffer, so that other
  659. * requests may safely intervene.
  660. */
  661. case 0x5b: /* control WRITE test -- fill the buffer */
  662. if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
  663. goto unknown;
  664. if (w_value || w_index)
  665. break;
  666. /* just read that many bytes into the buffer */
  667. if (w_length > req->length)
  668. break;
  669. value = w_length;
  670. break;
  671. case 0x5c: /* control READ test -- return the buffer */
  672. if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
  673. goto unknown;
  674. if (w_value || w_index)
  675. break;
  676. /* expect those bytes are still in the buffer; send back */
  677. if (w_length > req->length)
  678. break;
  679. value = w_length;
  680. break;
  681. default:
  682. unknown:
  683. VDBG(c->cdev,
  684. "unknown control req%02x.%02x v%04x i%04x l%d\n",
  685. ctrl->bRequestType, ctrl->bRequest,
  686. w_value, w_index, w_length);
  687. }
  688. /* respond with data transfer or status phase? */
  689. if (value >= 0) {
  690. VDBG(c->cdev, "source/sink req%02x.%02x v%04x i%04x l%d\n",
  691. ctrl->bRequestType, ctrl->bRequest,
  692. w_value, w_index, w_length);
  693. req->zero = 0;
  694. req->length = value;
  695. value = usb_ep_queue(c->cdev->gadget->ep0, req, GFP_ATOMIC);
  696. if (value < 0)
  697. ERROR(c->cdev, "source/sink response, err %d\n",
  698. value);
  699. }
  700. /* device either stalls (value < 0) or reports success */
  701. return value;
  702. }
  703. static struct usb_function *source_sink_alloc_func(
  704. struct usb_function_instance *fi)
  705. {
  706. struct f_sourcesink *ss;
  707. struct f_ss_opts *ss_opts;
  708. ss = kzalloc(sizeof(*ss), GFP_KERNEL);
  709. if (!ss)
  710. return ERR_PTR(-ENOMEM);
  711. ss_opts = container_of(fi, struct f_ss_opts, func_inst);
  712. mutex_lock(&ss_opts->lock);
  713. ss_opts->refcnt++;
  714. mutex_unlock(&ss_opts->lock);
  715. ss->pattern = ss_opts->pattern;
  716. ss->isoc_interval = ss_opts->isoc_interval;
  717. ss->isoc_maxpacket = ss_opts->isoc_maxpacket;
  718. ss->isoc_mult = ss_opts->isoc_mult;
  719. ss->isoc_maxburst = ss_opts->isoc_maxburst;
  720. ss->buflen = ss_opts->bulk_buflen;
  721. ss->bulk_qlen = ss_opts->bulk_qlen;
  722. ss->iso_qlen = ss_opts->iso_qlen;
  723. ss->function.name = "source/sink";
  724. ss->function.bind = sourcesink_bind;
  725. ss->function.set_alt = sourcesink_set_alt;
  726. ss->function.get_alt = sourcesink_get_alt;
  727. ss->function.disable = sourcesink_disable;
  728. ss->function.setup = sourcesink_setup;
  729. ss->function.strings = sourcesink_strings;
  730. ss->function.free_func = sourcesink_free_func;
  731. return &ss->function;
  732. }
  733. static inline struct f_ss_opts *to_f_ss_opts(struct config_item *item)
  734. {
  735. return container_of(to_config_group(item), struct f_ss_opts,
  736. func_inst.group);
  737. }
  738. static void ss_attr_release(struct config_item *item)
  739. {
  740. struct f_ss_opts *ss_opts = to_f_ss_opts(item);
  741. usb_put_function_instance(&ss_opts->func_inst);
  742. }
  743. static struct configfs_item_operations ss_item_ops = {
  744. .release = ss_attr_release,
  745. };
  746. static ssize_t f_ss_opts_pattern_show(struct config_item *item, char *page)
  747. {
  748. struct f_ss_opts *opts = to_f_ss_opts(item);
  749. int result;
  750. mutex_lock(&opts->lock);
  751. result = sprintf(page, "%u\n", opts->pattern);
  752. mutex_unlock(&opts->lock);
  753. return result;
  754. }
  755. static ssize_t f_ss_opts_pattern_store(struct config_item *item,
  756. const char *page, size_t len)
  757. {
  758. struct f_ss_opts *opts = to_f_ss_opts(item);
  759. int ret;
  760. u8 num;
  761. mutex_lock(&opts->lock);
  762. if (opts->refcnt) {
  763. ret = -EBUSY;
  764. goto end;
  765. }
  766. ret = kstrtou8(page, 0, &num);
  767. if (ret)
  768. goto end;
  769. if (num != 0 && num != 1 && num != 2) {
  770. ret = -EINVAL;
  771. goto end;
  772. }
  773. opts->pattern = num;
  774. ret = len;
  775. end:
  776. mutex_unlock(&opts->lock);
  777. return ret;
  778. }
  779. CONFIGFS_ATTR(f_ss_opts_, pattern);
  780. static ssize_t f_ss_opts_isoc_interval_show(struct config_item *item, char *page)
  781. {
  782. struct f_ss_opts *opts = to_f_ss_opts(item);
  783. int result;
  784. mutex_lock(&opts->lock);
  785. result = sprintf(page, "%u\n", opts->isoc_interval);
  786. mutex_unlock(&opts->lock);
  787. return result;
  788. }
  789. static ssize_t f_ss_opts_isoc_interval_store(struct config_item *item,
  790. const char *page, size_t len)
  791. {
  792. struct f_ss_opts *opts = to_f_ss_opts(item);
  793. int ret;
  794. u8 num;
  795. mutex_lock(&opts->lock);
  796. if (opts->refcnt) {
  797. ret = -EBUSY;
  798. goto end;
  799. }
  800. ret = kstrtou8(page, 0, &num);
  801. if (ret)
  802. goto end;
  803. if (num > 16) {
  804. ret = -EINVAL;
  805. goto end;
  806. }
  807. opts->isoc_interval = num;
  808. ret = len;
  809. end:
  810. mutex_unlock(&opts->lock);
  811. return ret;
  812. }
  813. CONFIGFS_ATTR(f_ss_opts_, isoc_interval);
  814. static ssize_t f_ss_opts_isoc_maxpacket_show(struct config_item *item, char *page)
  815. {
  816. struct f_ss_opts *opts = to_f_ss_opts(item);
  817. int result;
  818. mutex_lock(&opts->lock);
  819. result = sprintf(page, "%u\n", opts->isoc_maxpacket);
  820. mutex_unlock(&opts->lock);
  821. return result;
  822. }
  823. static ssize_t f_ss_opts_isoc_maxpacket_store(struct config_item *item,
  824. const char *page, size_t len)
  825. {
  826. struct f_ss_opts *opts = to_f_ss_opts(item);
  827. int ret;
  828. u16 num;
  829. mutex_lock(&opts->lock);
  830. if (opts->refcnt) {
  831. ret = -EBUSY;
  832. goto end;
  833. }
  834. ret = kstrtou16(page, 0, &num);
  835. if (ret)
  836. goto end;
  837. if (num > 1024) {
  838. ret = -EINVAL;
  839. goto end;
  840. }
  841. opts->isoc_maxpacket = num;
  842. ret = len;
  843. end:
  844. mutex_unlock(&opts->lock);
  845. return ret;
  846. }
  847. CONFIGFS_ATTR(f_ss_opts_, isoc_maxpacket);
  848. static ssize_t f_ss_opts_isoc_mult_show(struct config_item *item, char *page)
  849. {
  850. struct f_ss_opts *opts = to_f_ss_opts(item);
  851. int result;
  852. mutex_lock(&opts->lock);
  853. result = sprintf(page, "%u\n", opts->isoc_mult);
  854. mutex_unlock(&opts->lock);
  855. return result;
  856. }
  857. static ssize_t f_ss_opts_isoc_mult_store(struct config_item *item,
  858. const char *page, size_t len)
  859. {
  860. struct f_ss_opts *opts = to_f_ss_opts(item);
  861. int ret;
  862. u8 num;
  863. mutex_lock(&opts->lock);
  864. if (opts->refcnt) {
  865. ret = -EBUSY;
  866. goto end;
  867. }
  868. ret = kstrtou8(page, 0, &num);
  869. if (ret)
  870. goto end;
  871. if (num > 2) {
  872. ret = -EINVAL;
  873. goto end;
  874. }
  875. opts->isoc_mult = num;
  876. ret = len;
  877. end:
  878. mutex_unlock(&opts->lock);
  879. return ret;
  880. }
  881. CONFIGFS_ATTR(f_ss_opts_, isoc_mult);
  882. static ssize_t f_ss_opts_isoc_maxburst_show(struct config_item *item, char *page)
  883. {
  884. struct f_ss_opts *opts = to_f_ss_opts(item);
  885. int result;
  886. mutex_lock(&opts->lock);
  887. result = sprintf(page, "%u\n", opts->isoc_maxburst);
  888. mutex_unlock(&opts->lock);
  889. return result;
  890. }
  891. static ssize_t f_ss_opts_isoc_maxburst_store(struct config_item *item,
  892. const char *page, size_t len)
  893. {
  894. struct f_ss_opts *opts = to_f_ss_opts(item);
  895. int ret;
  896. u8 num;
  897. mutex_lock(&opts->lock);
  898. if (opts->refcnt) {
  899. ret = -EBUSY;
  900. goto end;
  901. }
  902. ret = kstrtou8(page, 0, &num);
  903. if (ret)
  904. goto end;
  905. if (num > 15) {
  906. ret = -EINVAL;
  907. goto end;
  908. }
  909. opts->isoc_maxburst = num;
  910. ret = len;
  911. end:
  912. mutex_unlock(&opts->lock);
  913. return ret;
  914. }
  915. CONFIGFS_ATTR(f_ss_opts_, isoc_maxburst);
  916. static ssize_t f_ss_opts_bulk_buflen_show(struct config_item *item, char *page)
  917. {
  918. struct f_ss_opts *opts = to_f_ss_opts(item);
  919. int result;
  920. mutex_lock(&opts->lock);
  921. result = sprintf(page, "%u\n", opts->bulk_buflen);
  922. mutex_unlock(&opts->lock);
  923. return result;
  924. }
  925. static ssize_t f_ss_opts_bulk_buflen_store(struct config_item *item,
  926. const char *page, size_t len)
  927. {
  928. struct f_ss_opts *opts = to_f_ss_opts(item);
  929. int ret;
  930. u32 num;
  931. mutex_lock(&opts->lock);
  932. if (opts->refcnt) {
  933. ret = -EBUSY;
  934. goto end;
  935. }
  936. ret = kstrtou32(page, 0, &num);
  937. if (ret)
  938. goto end;
  939. opts->bulk_buflen = num;
  940. ret = len;
  941. end:
  942. mutex_unlock(&opts->lock);
  943. return ret;
  944. }
  945. CONFIGFS_ATTR(f_ss_opts_, bulk_buflen);
  946. static ssize_t f_ss_opts_bulk_qlen_show(struct config_item *item, char *page)
  947. {
  948. struct f_ss_opts *opts = to_f_ss_opts(item);
  949. int result;
  950. mutex_lock(&opts->lock);
  951. result = sprintf(page, "%u\n", opts->bulk_qlen);
  952. mutex_unlock(&opts->lock);
  953. return result;
  954. }
  955. static ssize_t f_ss_opts_bulk_qlen_store(struct config_item *item,
  956. const char *page, size_t len)
  957. {
  958. struct f_ss_opts *opts = to_f_ss_opts(item);
  959. int ret;
  960. u32 num;
  961. mutex_lock(&opts->lock);
  962. if (opts->refcnt) {
  963. ret = -EBUSY;
  964. goto end;
  965. }
  966. ret = kstrtou32(page, 0, &num);
  967. if (ret)
  968. goto end;
  969. opts->bulk_qlen = num;
  970. ret = len;
  971. end:
  972. mutex_unlock(&opts->lock);
  973. return ret;
  974. }
  975. CONFIGFS_ATTR(f_ss_opts_, bulk_qlen);
  976. static ssize_t f_ss_opts_iso_qlen_show(struct config_item *item, char *page)
  977. {
  978. struct f_ss_opts *opts = to_f_ss_opts(item);
  979. int result;
  980. mutex_lock(&opts->lock);
  981. result = sprintf(page, "%u\n", opts->iso_qlen);
  982. mutex_unlock(&opts->lock);
  983. return result;
  984. }
  985. static ssize_t f_ss_opts_iso_qlen_store(struct config_item *item,
  986. const char *page, size_t len)
  987. {
  988. struct f_ss_opts *opts = to_f_ss_opts(item);
  989. int ret;
  990. u32 num;
  991. mutex_lock(&opts->lock);
  992. if (opts->refcnt) {
  993. ret = -EBUSY;
  994. goto end;
  995. }
  996. ret = kstrtou32(page, 0, &num);
  997. if (ret)
  998. goto end;
  999. opts->iso_qlen = num;
  1000. ret = len;
  1001. end:
  1002. mutex_unlock(&opts->lock);
  1003. return ret;
  1004. }
  1005. CONFIGFS_ATTR(f_ss_opts_, iso_qlen);
  1006. static struct configfs_attribute *ss_attrs[] = {
  1007. &f_ss_opts_attr_pattern,
  1008. &f_ss_opts_attr_isoc_interval,
  1009. &f_ss_opts_attr_isoc_maxpacket,
  1010. &f_ss_opts_attr_isoc_mult,
  1011. &f_ss_opts_attr_isoc_maxburst,
  1012. &f_ss_opts_attr_bulk_buflen,
  1013. &f_ss_opts_attr_bulk_qlen,
  1014. &f_ss_opts_attr_iso_qlen,
  1015. NULL,
  1016. };
  1017. static const struct config_item_type ss_func_type = {
  1018. .ct_item_ops = &ss_item_ops,
  1019. .ct_attrs = ss_attrs,
  1020. .ct_owner = THIS_MODULE,
  1021. };
  1022. static void source_sink_free_instance(struct usb_function_instance *fi)
  1023. {
  1024. struct f_ss_opts *ss_opts;
  1025. ss_opts = container_of(fi, struct f_ss_opts, func_inst);
  1026. kfree(ss_opts);
  1027. }
  1028. static struct usb_function_instance *source_sink_alloc_inst(void)
  1029. {
  1030. struct f_ss_opts *ss_opts;
  1031. ss_opts = kzalloc(sizeof(*ss_opts), GFP_KERNEL);
  1032. if (!ss_opts)
  1033. return ERR_PTR(-ENOMEM);
  1034. mutex_init(&ss_opts->lock);
  1035. ss_opts->func_inst.free_func_inst = source_sink_free_instance;
  1036. ss_opts->isoc_interval = GZERO_ISOC_INTERVAL;
  1037. ss_opts->isoc_maxpacket = GZERO_ISOC_MAXPACKET;
  1038. ss_opts->bulk_buflen = GZERO_BULK_BUFLEN;
  1039. ss_opts->bulk_qlen = GZERO_SS_BULK_QLEN;
  1040. ss_opts->iso_qlen = GZERO_SS_ISO_QLEN;
  1041. config_group_init_type_name(&ss_opts->func_inst.group, "",
  1042. &ss_func_type);
  1043. return &ss_opts->func_inst;
  1044. }
  1045. DECLARE_USB_FUNCTION(SourceSink, source_sink_alloc_inst,
  1046. source_sink_alloc_func);
  1047. static int __init sslb_modinit(void)
  1048. {
  1049. int ret;
  1050. ret = usb_function_register(&SourceSinkusb_func);
  1051. if (ret)
  1052. return ret;
  1053. ret = lb_modinit();
  1054. if (ret)
  1055. usb_function_unregister(&SourceSinkusb_func);
  1056. return ret;
  1057. }
  1058. static void __exit sslb_modexit(void)
  1059. {
  1060. usb_function_unregister(&SourceSinkusb_func);
  1061. lb_modexit();
  1062. }
  1063. module_init(sslb_modinit);
  1064. module_exit(sslb_modexit);
  1065. MODULE_LICENSE("GPL");