tcm_usb_gadget.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475
  1. /* Target based USB-Gadget
  2. *
  3. * UAS protocol handling, target callbacks, configfs handling,
  4. * BBB (USB Mass Storage Class Bulk-Only (BBB) and Transport protocol handling.
  5. *
  6. * Author: Sebastian Andrzej Siewior <bigeasy at linutronix dot de>
  7. * License: GPLv2 as published by FSF.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/types.h>
  12. #include <linux/string.h>
  13. #include <linux/configfs.h>
  14. #include <linux/ctype.h>
  15. #include <linux/usb/ch9.h>
  16. #include <linux/usb/composite.h>
  17. #include <linux/usb/gadget.h>
  18. #include <linux/usb/storage.h>
  19. #include <scsi/scsi.h>
  20. #include <scsi/scsi_tcq.h>
  21. #include <target/target_core_base.h>
  22. #include <target/target_core_fabric.h>
  23. #include <target/target_core_fabric_configfs.h>
  24. #include <target/target_core_configfs.h>
  25. #include <target/configfs_macros.h>
  26. #include <asm/unaligned.h>
  27. #include "tcm_usb_gadget.h"
  28. USB_GADGET_COMPOSITE_OPTIONS();
  29. static struct target_fabric_configfs *usbg_fabric_configfs;
  30. static inline struct f_uas *to_f_uas(struct usb_function *f)
  31. {
  32. return container_of(f, struct f_uas, function);
  33. }
  34. static void usbg_cmd_release(struct kref *);
  35. static inline void usbg_cleanup_cmd(struct usbg_cmd *cmd)
  36. {
  37. kref_put(&cmd->ref, usbg_cmd_release);
  38. }
  39. /* Start bot.c code */
  40. static int bot_enqueue_cmd_cbw(struct f_uas *fu)
  41. {
  42. int ret;
  43. if (fu->flags & USBG_BOT_CMD_PEND)
  44. return 0;
  45. ret = usb_ep_queue(fu->ep_out, fu->cmd.req, GFP_ATOMIC);
  46. if (!ret)
  47. fu->flags |= USBG_BOT_CMD_PEND;
  48. return ret;
  49. }
  50. static void bot_status_complete(struct usb_ep *ep, struct usb_request *req)
  51. {
  52. struct usbg_cmd *cmd = req->context;
  53. struct f_uas *fu = cmd->fu;
  54. usbg_cleanup_cmd(cmd);
  55. if (req->status < 0) {
  56. pr_err("ERR %s(%d)\n", __func__, __LINE__);
  57. return;
  58. }
  59. /* CSW completed, wait for next CBW */
  60. bot_enqueue_cmd_cbw(fu);
  61. }
  62. static void bot_enqueue_sense_code(struct f_uas *fu, struct usbg_cmd *cmd)
  63. {
  64. struct bulk_cs_wrap *csw = &fu->bot_status.csw;
  65. int ret;
  66. u8 *sense;
  67. unsigned int csw_stat;
  68. csw_stat = cmd->csw_code;
  69. /*
  70. * We can't send SENSE as a response. So we take ASC & ASCQ from our
  71. * sense buffer and queue it and hope the host sends a REQUEST_SENSE
  72. * command where it learns why we failed.
  73. */
  74. sense = cmd->sense_iu.sense;
  75. csw->Tag = cmd->bot_tag;
  76. csw->Status = csw_stat;
  77. fu->bot_status.req->context = cmd;
  78. ret = usb_ep_queue(fu->ep_in, fu->bot_status.req, GFP_ATOMIC);
  79. if (ret)
  80. pr_err("%s(%d) ERR: %d\n", __func__, __LINE__, ret);
  81. }
  82. static void bot_err_compl(struct usb_ep *ep, struct usb_request *req)
  83. {
  84. struct usbg_cmd *cmd = req->context;
  85. struct f_uas *fu = cmd->fu;
  86. if (req->status < 0)
  87. pr_err("ERR %s(%d)\n", __func__, __LINE__);
  88. if (cmd->data_len) {
  89. if (cmd->data_len > ep->maxpacket) {
  90. req->length = ep->maxpacket;
  91. cmd->data_len -= ep->maxpacket;
  92. } else {
  93. req->length = cmd->data_len;
  94. cmd->data_len = 0;
  95. }
  96. usb_ep_queue(ep, req, GFP_ATOMIC);
  97. return ;
  98. }
  99. bot_enqueue_sense_code(fu, cmd);
  100. }
  101. static void bot_send_bad_status(struct usbg_cmd *cmd)
  102. {
  103. struct f_uas *fu = cmd->fu;
  104. struct bulk_cs_wrap *csw = &fu->bot_status.csw;
  105. struct usb_request *req;
  106. struct usb_ep *ep;
  107. csw->Residue = cpu_to_le32(cmd->data_len);
  108. if (cmd->data_len) {
  109. if (cmd->is_read) {
  110. ep = fu->ep_in;
  111. req = fu->bot_req_in;
  112. } else {
  113. ep = fu->ep_out;
  114. req = fu->bot_req_out;
  115. }
  116. if (cmd->data_len > fu->ep_in->maxpacket) {
  117. req->length = ep->maxpacket;
  118. cmd->data_len -= ep->maxpacket;
  119. } else {
  120. req->length = cmd->data_len;
  121. cmd->data_len = 0;
  122. }
  123. req->complete = bot_err_compl;
  124. req->context = cmd;
  125. req->buf = fu->cmd.buf;
  126. usb_ep_queue(ep, req, GFP_KERNEL);
  127. } else {
  128. bot_enqueue_sense_code(fu, cmd);
  129. }
  130. }
  131. static int bot_send_status(struct usbg_cmd *cmd, bool moved_data)
  132. {
  133. struct f_uas *fu = cmd->fu;
  134. struct bulk_cs_wrap *csw = &fu->bot_status.csw;
  135. int ret;
  136. if (cmd->se_cmd.scsi_status == SAM_STAT_GOOD) {
  137. if (!moved_data && cmd->data_len) {
  138. /*
  139. * the host wants to move data, we don't. Fill / empty
  140. * the pipe and then send the csw with reside set.
  141. */
  142. cmd->csw_code = US_BULK_STAT_OK;
  143. bot_send_bad_status(cmd);
  144. return 0;
  145. }
  146. csw->Tag = cmd->bot_tag;
  147. csw->Residue = cpu_to_le32(0);
  148. csw->Status = US_BULK_STAT_OK;
  149. fu->bot_status.req->context = cmd;
  150. ret = usb_ep_queue(fu->ep_in, fu->bot_status.req, GFP_KERNEL);
  151. if (ret)
  152. pr_err("%s(%d) ERR: %d\n", __func__, __LINE__, ret);
  153. } else {
  154. cmd->csw_code = US_BULK_STAT_FAIL;
  155. bot_send_bad_status(cmd);
  156. }
  157. return 0;
  158. }
  159. /*
  160. * Called after command (no data transfer) or after the write (to device)
  161. * operation is completed
  162. */
  163. static int bot_send_status_response(struct usbg_cmd *cmd)
  164. {
  165. bool moved_data = false;
  166. if (!cmd->is_read)
  167. moved_data = true;
  168. return bot_send_status(cmd, moved_data);
  169. }
  170. /* Read request completed, now we have to send the CSW */
  171. static void bot_read_compl(struct usb_ep *ep, struct usb_request *req)
  172. {
  173. struct usbg_cmd *cmd = req->context;
  174. if (req->status < 0)
  175. pr_err("ERR %s(%d)\n", __func__, __LINE__);
  176. bot_send_status(cmd, true);
  177. }
  178. static int bot_send_read_response(struct usbg_cmd *cmd)
  179. {
  180. struct f_uas *fu = cmd->fu;
  181. struct se_cmd *se_cmd = &cmd->se_cmd;
  182. struct usb_gadget *gadget = fuas_to_gadget(fu);
  183. int ret;
  184. if (!cmd->data_len) {
  185. cmd->csw_code = US_BULK_STAT_PHASE;
  186. bot_send_bad_status(cmd);
  187. return 0;
  188. }
  189. if (!gadget->sg_supported) {
  190. cmd->data_buf = kmalloc(se_cmd->data_length, GFP_ATOMIC);
  191. if (!cmd->data_buf)
  192. return -ENOMEM;
  193. sg_copy_to_buffer(se_cmd->t_data_sg,
  194. se_cmd->t_data_nents,
  195. cmd->data_buf,
  196. se_cmd->data_length);
  197. fu->bot_req_in->buf = cmd->data_buf;
  198. } else {
  199. fu->bot_req_in->buf = NULL;
  200. fu->bot_req_in->num_sgs = se_cmd->t_data_nents;
  201. fu->bot_req_in->sg = se_cmd->t_data_sg;
  202. }
  203. fu->bot_req_in->complete = bot_read_compl;
  204. fu->bot_req_in->length = se_cmd->data_length;
  205. fu->bot_req_in->context = cmd;
  206. ret = usb_ep_queue(fu->ep_in, fu->bot_req_in, GFP_ATOMIC);
  207. if (ret)
  208. pr_err("%s(%d)\n", __func__, __LINE__);
  209. return 0;
  210. }
  211. static void usbg_data_write_cmpl(struct usb_ep *, struct usb_request *);
  212. static int usbg_prepare_w_request(struct usbg_cmd *, struct usb_request *);
  213. static int bot_send_write_request(struct usbg_cmd *cmd)
  214. {
  215. struct f_uas *fu = cmd->fu;
  216. struct se_cmd *se_cmd = &cmd->se_cmd;
  217. struct usb_gadget *gadget = fuas_to_gadget(fu);
  218. int ret;
  219. init_completion(&cmd->write_complete);
  220. cmd->fu = fu;
  221. if (!cmd->data_len) {
  222. cmd->csw_code = US_BULK_STAT_PHASE;
  223. return -EINVAL;
  224. }
  225. if (!gadget->sg_supported) {
  226. cmd->data_buf = kmalloc(se_cmd->data_length, GFP_KERNEL);
  227. if (!cmd->data_buf)
  228. return -ENOMEM;
  229. fu->bot_req_out->buf = cmd->data_buf;
  230. } else {
  231. fu->bot_req_out->buf = NULL;
  232. fu->bot_req_out->num_sgs = se_cmd->t_data_nents;
  233. fu->bot_req_out->sg = se_cmd->t_data_sg;
  234. }
  235. fu->bot_req_out->complete = usbg_data_write_cmpl;
  236. fu->bot_req_out->length = se_cmd->data_length;
  237. fu->bot_req_out->context = cmd;
  238. ret = usbg_prepare_w_request(cmd, fu->bot_req_out);
  239. if (ret)
  240. goto cleanup;
  241. ret = usb_ep_queue(fu->ep_out, fu->bot_req_out, GFP_KERNEL);
  242. if (ret)
  243. pr_err("%s(%d)\n", __func__, __LINE__);
  244. wait_for_completion(&cmd->write_complete);
  245. target_execute_cmd(se_cmd);
  246. cleanup:
  247. return ret;
  248. }
  249. static int bot_submit_command(struct f_uas *, void *, unsigned int);
  250. static void bot_cmd_complete(struct usb_ep *ep, struct usb_request *req)
  251. {
  252. struct f_uas *fu = req->context;
  253. int ret;
  254. fu->flags &= ~USBG_BOT_CMD_PEND;
  255. if (req->status < 0)
  256. return;
  257. ret = bot_submit_command(fu, req->buf, req->actual);
  258. if (ret)
  259. pr_err("%s(%d): %d\n", __func__, __LINE__, ret);
  260. }
  261. static int bot_prepare_reqs(struct f_uas *fu)
  262. {
  263. int ret;
  264. fu->bot_req_in = usb_ep_alloc_request(fu->ep_in, GFP_KERNEL);
  265. if (!fu->bot_req_in)
  266. goto err;
  267. fu->bot_req_out = usb_ep_alloc_request(fu->ep_out, GFP_KERNEL);
  268. if (!fu->bot_req_out)
  269. goto err_out;
  270. fu->cmd.req = usb_ep_alloc_request(fu->ep_out, GFP_KERNEL);
  271. if (!fu->cmd.req)
  272. goto err_cmd;
  273. fu->bot_status.req = usb_ep_alloc_request(fu->ep_in, GFP_KERNEL);
  274. if (!fu->bot_status.req)
  275. goto err_sts;
  276. fu->bot_status.req->buf = &fu->bot_status.csw;
  277. fu->bot_status.req->length = US_BULK_CS_WRAP_LEN;
  278. fu->bot_status.req->complete = bot_status_complete;
  279. fu->bot_status.csw.Signature = cpu_to_le32(US_BULK_CS_SIGN);
  280. fu->cmd.buf = kmalloc(fu->ep_out->maxpacket, GFP_KERNEL);
  281. if (!fu->cmd.buf)
  282. goto err_buf;
  283. fu->cmd.req->complete = bot_cmd_complete;
  284. fu->cmd.req->buf = fu->cmd.buf;
  285. fu->cmd.req->length = fu->ep_out->maxpacket;
  286. fu->cmd.req->context = fu;
  287. ret = bot_enqueue_cmd_cbw(fu);
  288. if (ret)
  289. goto err_queue;
  290. return 0;
  291. err_queue:
  292. kfree(fu->cmd.buf);
  293. fu->cmd.buf = NULL;
  294. err_buf:
  295. usb_ep_free_request(fu->ep_in, fu->bot_status.req);
  296. err_sts:
  297. usb_ep_free_request(fu->ep_out, fu->cmd.req);
  298. fu->cmd.req = NULL;
  299. err_cmd:
  300. usb_ep_free_request(fu->ep_out, fu->bot_req_out);
  301. fu->bot_req_out = NULL;
  302. err_out:
  303. usb_ep_free_request(fu->ep_in, fu->bot_req_in);
  304. fu->bot_req_in = NULL;
  305. err:
  306. pr_err("BOT: endpoint setup failed\n");
  307. return -ENOMEM;
  308. }
  309. void bot_cleanup_old_alt(struct f_uas *fu)
  310. {
  311. if (!(fu->flags & USBG_ENABLED))
  312. return;
  313. usb_ep_disable(fu->ep_in);
  314. usb_ep_disable(fu->ep_out);
  315. if (!fu->bot_req_in)
  316. return;
  317. usb_ep_free_request(fu->ep_in, fu->bot_req_in);
  318. usb_ep_free_request(fu->ep_out, fu->bot_req_out);
  319. usb_ep_free_request(fu->ep_out, fu->cmd.req);
  320. usb_ep_free_request(fu->ep_out, fu->bot_status.req);
  321. kfree(fu->cmd.buf);
  322. fu->bot_req_in = NULL;
  323. fu->bot_req_out = NULL;
  324. fu->cmd.req = NULL;
  325. fu->bot_status.req = NULL;
  326. fu->cmd.buf = NULL;
  327. }
  328. static void bot_set_alt(struct f_uas *fu)
  329. {
  330. struct usb_function *f = &fu->function;
  331. struct usb_gadget *gadget = f->config->cdev->gadget;
  332. int ret;
  333. fu->flags = USBG_IS_BOT;
  334. config_ep_by_speed(gadget, f, fu->ep_in);
  335. ret = usb_ep_enable(fu->ep_in);
  336. if (ret)
  337. goto err_b_in;
  338. config_ep_by_speed(gadget, f, fu->ep_out);
  339. ret = usb_ep_enable(fu->ep_out);
  340. if (ret)
  341. goto err_b_out;
  342. ret = bot_prepare_reqs(fu);
  343. if (ret)
  344. goto err_wq;
  345. fu->flags |= USBG_ENABLED;
  346. pr_info("Using the BOT protocol\n");
  347. return;
  348. err_wq:
  349. usb_ep_disable(fu->ep_out);
  350. err_b_out:
  351. usb_ep_disable(fu->ep_in);
  352. err_b_in:
  353. fu->flags = USBG_IS_BOT;
  354. }
  355. static int usbg_bot_setup(struct usb_function *f,
  356. const struct usb_ctrlrequest *ctrl)
  357. {
  358. struct f_uas *fu = to_f_uas(f);
  359. struct usb_composite_dev *cdev = f->config->cdev;
  360. u16 w_value = le16_to_cpu(ctrl->wValue);
  361. u16 w_length = le16_to_cpu(ctrl->wLength);
  362. int luns;
  363. u8 *ret_lun;
  364. switch (ctrl->bRequest) {
  365. case US_BULK_GET_MAX_LUN:
  366. if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_CLASS |
  367. USB_RECIP_INTERFACE))
  368. return -ENOTSUPP;
  369. if (w_length < 1)
  370. return -EINVAL;
  371. if (w_value != 0)
  372. return -EINVAL;
  373. luns = atomic_read(&fu->tpg->tpg_port_count);
  374. if (!luns) {
  375. pr_err("No LUNs configured?\n");
  376. return -EINVAL;
  377. }
  378. /*
  379. * If 4 LUNs are present we return 3 i.e. LUN 0..3 can be
  380. * accessed. The upper limit is 0xf
  381. */
  382. luns--;
  383. if (luns > 0xf) {
  384. pr_info_once("Limiting the number of luns to 16\n");
  385. luns = 0xf;
  386. }
  387. ret_lun = cdev->req->buf;
  388. *ret_lun = luns;
  389. cdev->req->length = 1;
  390. return usb_ep_queue(cdev->gadget->ep0, cdev->req, GFP_ATOMIC);
  391. break;
  392. case US_BULK_RESET_REQUEST:
  393. /* XXX maybe we should remove previous requests for IN + OUT */
  394. bot_enqueue_cmd_cbw(fu);
  395. return 0;
  396. break;
  397. };
  398. return -ENOTSUPP;
  399. }
  400. /* Start uas.c code */
  401. static void uasp_cleanup_one_stream(struct f_uas *fu, struct uas_stream *stream)
  402. {
  403. /* We have either all three allocated or none */
  404. if (!stream->req_in)
  405. return;
  406. usb_ep_free_request(fu->ep_in, stream->req_in);
  407. usb_ep_free_request(fu->ep_out, stream->req_out);
  408. usb_ep_free_request(fu->ep_status, stream->req_status);
  409. stream->req_in = NULL;
  410. stream->req_out = NULL;
  411. stream->req_status = NULL;
  412. }
  413. static void uasp_free_cmdreq(struct f_uas *fu)
  414. {
  415. usb_ep_free_request(fu->ep_cmd, fu->cmd.req);
  416. kfree(fu->cmd.buf);
  417. fu->cmd.req = NULL;
  418. fu->cmd.buf = NULL;
  419. }
  420. static void uasp_cleanup_old_alt(struct f_uas *fu)
  421. {
  422. int i;
  423. if (!(fu->flags & USBG_ENABLED))
  424. return;
  425. usb_ep_disable(fu->ep_in);
  426. usb_ep_disable(fu->ep_out);
  427. usb_ep_disable(fu->ep_status);
  428. usb_ep_disable(fu->ep_cmd);
  429. for (i = 0; i < UASP_SS_EP_COMP_NUM_STREAMS; i++)
  430. uasp_cleanup_one_stream(fu, &fu->stream[i]);
  431. uasp_free_cmdreq(fu);
  432. }
  433. static void uasp_status_data_cmpl(struct usb_ep *ep, struct usb_request *req);
  434. static int uasp_prepare_r_request(struct usbg_cmd *cmd)
  435. {
  436. struct se_cmd *se_cmd = &cmd->se_cmd;
  437. struct f_uas *fu = cmd->fu;
  438. struct usb_gadget *gadget = fuas_to_gadget(fu);
  439. struct uas_stream *stream = cmd->stream;
  440. if (!gadget->sg_supported) {
  441. cmd->data_buf = kmalloc(se_cmd->data_length, GFP_ATOMIC);
  442. if (!cmd->data_buf)
  443. return -ENOMEM;
  444. sg_copy_to_buffer(se_cmd->t_data_sg,
  445. se_cmd->t_data_nents,
  446. cmd->data_buf,
  447. se_cmd->data_length);
  448. stream->req_in->buf = cmd->data_buf;
  449. } else {
  450. stream->req_in->buf = NULL;
  451. stream->req_in->num_sgs = se_cmd->t_data_nents;
  452. stream->req_in->sg = se_cmd->t_data_sg;
  453. }
  454. stream->req_in->complete = uasp_status_data_cmpl;
  455. stream->req_in->length = se_cmd->data_length;
  456. stream->req_in->context = cmd;
  457. cmd->state = UASP_SEND_STATUS;
  458. return 0;
  459. }
  460. static void uasp_prepare_status(struct usbg_cmd *cmd)
  461. {
  462. struct se_cmd *se_cmd = &cmd->se_cmd;
  463. struct sense_iu *iu = &cmd->sense_iu;
  464. struct uas_stream *stream = cmd->stream;
  465. cmd->state = UASP_QUEUE_COMMAND;
  466. iu->iu_id = IU_ID_STATUS;
  467. iu->tag = cpu_to_be16(cmd->tag);
  468. /*
  469. * iu->status_qual = cpu_to_be16(STATUS QUALIFIER SAM-4. Where R U?);
  470. */
  471. iu->len = cpu_to_be16(se_cmd->scsi_sense_length);
  472. iu->status = se_cmd->scsi_status;
  473. stream->req_status->context = cmd;
  474. stream->req_status->length = se_cmd->scsi_sense_length + 16;
  475. stream->req_status->buf = iu;
  476. stream->req_status->complete = uasp_status_data_cmpl;
  477. }
  478. static void uasp_status_data_cmpl(struct usb_ep *ep, struct usb_request *req)
  479. {
  480. struct usbg_cmd *cmd = req->context;
  481. struct uas_stream *stream = cmd->stream;
  482. struct f_uas *fu = cmd->fu;
  483. int ret;
  484. if (req->status < 0)
  485. goto cleanup;
  486. switch (cmd->state) {
  487. case UASP_SEND_DATA:
  488. ret = uasp_prepare_r_request(cmd);
  489. if (ret)
  490. goto cleanup;
  491. ret = usb_ep_queue(fu->ep_in, stream->req_in, GFP_ATOMIC);
  492. if (ret)
  493. pr_err("%s(%d) => %d\n", __func__, __LINE__, ret);
  494. break;
  495. case UASP_RECEIVE_DATA:
  496. ret = usbg_prepare_w_request(cmd, stream->req_out);
  497. if (ret)
  498. goto cleanup;
  499. ret = usb_ep_queue(fu->ep_out, stream->req_out, GFP_ATOMIC);
  500. if (ret)
  501. pr_err("%s(%d) => %d\n", __func__, __LINE__, ret);
  502. break;
  503. case UASP_SEND_STATUS:
  504. uasp_prepare_status(cmd);
  505. ret = usb_ep_queue(fu->ep_status, stream->req_status,
  506. GFP_ATOMIC);
  507. if (ret)
  508. pr_err("%s(%d) => %d\n", __func__, __LINE__, ret);
  509. break;
  510. case UASP_QUEUE_COMMAND:
  511. usbg_cleanup_cmd(cmd);
  512. usb_ep_queue(fu->ep_cmd, fu->cmd.req, GFP_ATOMIC);
  513. break;
  514. default:
  515. BUG();
  516. };
  517. return;
  518. cleanup:
  519. usbg_cleanup_cmd(cmd);
  520. }
  521. static int uasp_send_status_response(struct usbg_cmd *cmd)
  522. {
  523. struct f_uas *fu = cmd->fu;
  524. struct uas_stream *stream = cmd->stream;
  525. struct sense_iu *iu = &cmd->sense_iu;
  526. iu->tag = cpu_to_be16(cmd->tag);
  527. stream->req_status->complete = uasp_status_data_cmpl;
  528. stream->req_status->context = cmd;
  529. cmd->fu = fu;
  530. uasp_prepare_status(cmd);
  531. return usb_ep_queue(fu->ep_status, stream->req_status, GFP_ATOMIC);
  532. }
  533. static int uasp_send_read_response(struct usbg_cmd *cmd)
  534. {
  535. struct f_uas *fu = cmd->fu;
  536. struct uas_stream *stream = cmd->stream;
  537. struct sense_iu *iu = &cmd->sense_iu;
  538. int ret;
  539. cmd->fu = fu;
  540. iu->tag = cpu_to_be16(cmd->tag);
  541. if (fu->flags & USBG_USE_STREAMS) {
  542. ret = uasp_prepare_r_request(cmd);
  543. if (ret)
  544. goto out;
  545. ret = usb_ep_queue(fu->ep_in, stream->req_in, GFP_ATOMIC);
  546. if (ret) {
  547. pr_err("%s(%d) => %d\n", __func__, __LINE__, ret);
  548. kfree(cmd->data_buf);
  549. cmd->data_buf = NULL;
  550. }
  551. } else {
  552. iu->iu_id = IU_ID_READ_READY;
  553. iu->tag = cpu_to_be16(cmd->tag);
  554. stream->req_status->complete = uasp_status_data_cmpl;
  555. stream->req_status->context = cmd;
  556. cmd->state = UASP_SEND_DATA;
  557. stream->req_status->buf = iu;
  558. stream->req_status->length = sizeof(struct iu);
  559. ret = usb_ep_queue(fu->ep_status, stream->req_status,
  560. GFP_ATOMIC);
  561. if (ret)
  562. pr_err("%s(%d) => %d\n", __func__, __LINE__, ret);
  563. }
  564. out:
  565. return ret;
  566. }
  567. static int uasp_send_write_request(struct usbg_cmd *cmd)
  568. {
  569. struct f_uas *fu = cmd->fu;
  570. struct se_cmd *se_cmd = &cmd->se_cmd;
  571. struct uas_stream *stream = cmd->stream;
  572. struct sense_iu *iu = &cmd->sense_iu;
  573. int ret;
  574. init_completion(&cmd->write_complete);
  575. cmd->fu = fu;
  576. iu->tag = cpu_to_be16(cmd->tag);
  577. if (fu->flags & USBG_USE_STREAMS) {
  578. ret = usbg_prepare_w_request(cmd, stream->req_out);
  579. if (ret)
  580. goto cleanup;
  581. ret = usb_ep_queue(fu->ep_out, stream->req_out, GFP_ATOMIC);
  582. if (ret)
  583. pr_err("%s(%d)\n", __func__, __LINE__);
  584. } else {
  585. iu->iu_id = IU_ID_WRITE_READY;
  586. iu->tag = cpu_to_be16(cmd->tag);
  587. stream->req_status->complete = uasp_status_data_cmpl;
  588. stream->req_status->context = cmd;
  589. cmd->state = UASP_RECEIVE_DATA;
  590. stream->req_status->buf = iu;
  591. stream->req_status->length = sizeof(struct iu);
  592. ret = usb_ep_queue(fu->ep_status, stream->req_status,
  593. GFP_ATOMIC);
  594. if (ret)
  595. pr_err("%s(%d)\n", __func__, __LINE__);
  596. }
  597. wait_for_completion(&cmd->write_complete);
  598. target_execute_cmd(se_cmd);
  599. cleanup:
  600. return ret;
  601. }
  602. static int usbg_submit_command(struct f_uas *, void *, unsigned int);
  603. static void uasp_cmd_complete(struct usb_ep *ep, struct usb_request *req)
  604. {
  605. struct f_uas *fu = req->context;
  606. int ret;
  607. if (req->status < 0)
  608. return;
  609. ret = usbg_submit_command(fu, req->buf, req->actual);
  610. /*
  611. * Once we tune for performance enqueue the command req here again so
  612. * we can receive a second command while we processing this one. Pay
  613. * attention to properly sync STAUS endpoint with DATA IN + OUT so you
  614. * don't break HS.
  615. */
  616. if (!ret)
  617. return;
  618. usb_ep_queue(fu->ep_cmd, fu->cmd.req, GFP_ATOMIC);
  619. }
  620. static int uasp_alloc_stream_res(struct f_uas *fu, struct uas_stream *stream)
  621. {
  622. stream->req_in = usb_ep_alloc_request(fu->ep_in, GFP_KERNEL);
  623. if (!stream->req_in)
  624. goto out;
  625. stream->req_out = usb_ep_alloc_request(fu->ep_out, GFP_KERNEL);
  626. if (!stream->req_out)
  627. goto err_out;
  628. stream->req_status = usb_ep_alloc_request(fu->ep_status, GFP_KERNEL);
  629. if (!stream->req_status)
  630. goto err_sts;
  631. return 0;
  632. err_sts:
  633. usb_ep_free_request(fu->ep_status, stream->req_status);
  634. stream->req_status = NULL;
  635. err_out:
  636. usb_ep_free_request(fu->ep_out, stream->req_out);
  637. stream->req_out = NULL;
  638. out:
  639. return -ENOMEM;
  640. }
  641. static int uasp_alloc_cmd(struct f_uas *fu)
  642. {
  643. fu->cmd.req = usb_ep_alloc_request(fu->ep_cmd, GFP_KERNEL);
  644. if (!fu->cmd.req)
  645. goto err;
  646. fu->cmd.buf = kmalloc(fu->ep_cmd->maxpacket, GFP_KERNEL);
  647. if (!fu->cmd.buf)
  648. goto err_buf;
  649. fu->cmd.req->complete = uasp_cmd_complete;
  650. fu->cmd.req->buf = fu->cmd.buf;
  651. fu->cmd.req->length = fu->ep_cmd->maxpacket;
  652. fu->cmd.req->context = fu;
  653. return 0;
  654. err_buf:
  655. usb_ep_free_request(fu->ep_cmd, fu->cmd.req);
  656. err:
  657. return -ENOMEM;
  658. }
  659. static void uasp_setup_stream_res(struct f_uas *fu, int max_streams)
  660. {
  661. int i;
  662. for (i = 0; i < max_streams; i++) {
  663. struct uas_stream *s = &fu->stream[i];
  664. s->req_in->stream_id = i + 1;
  665. s->req_out->stream_id = i + 1;
  666. s->req_status->stream_id = i + 1;
  667. }
  668. }
  669. static int uasp_prepare_reqs(struct f_uas *fu)
  670. {
  671. int ret;
  672. int i;
  673. int max_streams;
  674. if (fu->flags & USBG_USE_STREAMS)
  675. max_streams = UASP_SS_EP_COMP_NUM_STREAMS;
  676. else
  677. max_streams = 1;
  678. for (i = 0; i < max_streams; i++) {
  679. ret = uasp_alloc_stream_res(fu, &fu->stream[i]);
  680. if (ret)
  681. goto err_cleanup;
  682. }
  683. ret = uasp_alloc_cmd(fu);
  684. if (ret)
  685. goto err_free_stream;
  686. uasp_setup_stream_res(fu, max_streams);
  687. ret = usb_ep_queue(fu->ep_cmd, fu->cmd.req, GFP_ATOMIC);
  688. if (ret)
  689. goto err_free_stream;
  690. return 0;
  691. err_free_stream:
  692. uasp_free_cmdreq(fu);
  693. err_cleanup:
  694. if (i) {
  695. do {
  696. uasp_cleanup_one_stream(fu, &fu->stream[i - 1]);
  697. i--;
  698. } while (i);
  699. }
  700. pr_err("UASP: endpoint setup failed\n");
  701. return ret;
  702. }
  703. static void uasp_set_alt(struct f_uas *fu)
  704. {
  705. struct usb_function *f = &fu->function;
  706. struct usb_gadget *gadget = f->config->cdev->gadget;
  707. int ret;
  708. fu->flags = USBG_IS_UAS;
  709. if (gadget->speed == USB_SPEED_SUPER)
  710. fu->flags |= USBG_USE_STREAMS;
  711. config_ep_by_speed(gadget, f, fu->ep_in);
  712. ret = usb_ep_enable(fu->ep_in);
  713. if (ret)
  714. goto err_b_in;
  715. config_ep_by_speed(gadget, f, fu->ep_out);
  716. ret = usb_ep_enable(fu->ep_out);
  717. if (ret)
  718. goto err_b_out;
  719. config_ep_by_speed(gadget, f, fu->ep_cmd);
  720. ret = usb_ep_enable(fu->ep_cmd);
  721. if (ret)
  722. goto err_cmd;
  723. config_ep_by_speed(gadget, f, fu->ep_status);
  724. ret = usb_ep_enable(fu->ep_status);
  725. if (ret)
  726. goto err_status;
  727. ret = uasp_prepare_reqs(fu);
  728. if (ret)
  729. goto err_wq;
  730. fu->flags |= USBG_ENABLED;
  731. pr_info("Using the UAS protocol\n");
  732. return;
  733. err_wq:
  734. usb_ep_disable(fu->ep_status);
  735. err_status:
  736. usb_ep_disable(fu->ep_cmd);
  737. err_cmd:
  738. usb_ep_disable(fu->ep_out);
  739. err_b_out:
  740. usb_ep_disable(fu->ep_in);
  741. err_b_in:
  742. fu->flags = 0;
  743. }
  744. static int get_cmd_dir(const unsigned char *cdb)
  745. {
  746. int ret;
  747. switch (cdb[0]) {
  748. case READ_6:
  749. case READ_10:
  750. case READ_12:
  751. case READ_16:
  752. case INQUIRY:
  753. case MODE_SENSE:
  754. case MODE_SENSE_10:
  755. case SERVICE_ACTION_IN:
  756. case MAINTENANCE_IN:
  757. case PERSISTENT_RESERVE_IN:
  758. case SECURITY_PROTOCOL_IN:
  759. case ACCESS_CONTROL_IN:
  760. case REPORT_LUNS:
  761. case READ_BLOCK_LIMITS:
  762. case READ_POSITION:
  763. case READ_CAPACITY:
  764. case READ_TOC:
  765. case READ_FORMAT_CAPACITIES:
  766. case REQUEST_SENSE:
  767. ret = DMA_FROM_DEVICE;
  768. break;
  769. case WRITE_6:
  770. case WRITE_10:
  771. case WRITE_12:
  772. case WRITE_16:
  773. case MODE_SELECT:
  774. case MODE_SELECT_10:
  775. case WRITE_VERIFY:
  776. case WRITE_VERIFY_12:
  777. case PERSISTENT_RESERVE_OUT:
  778. case MAINTENANCE_OUT:
  779. case SECURITY_PROTOCOL_OUT:
  780. case ACCESS_CONTROL_OUT:
  781. ret = DMA_TO_DEVICE;
  782. break;
  783. case ALLOW_MEDIUM_REMOVAL:
  784. case TEST_UNIT_READY:
  785. case SYNCHRONIZE_CACHE:
  786. case START_STOP:
  787. case ERASE:
  788. case REZERO_UNIT:
  789. case SEEK_10:
  790. case SPACE:
  791. case VERIFY:
  792. case WRITE_FILEMARKS:
  793. ret = DMA_NONE;
  794. break;
  795. default:
  796. pr_warn("target: Unknown data direction for SCSI Opcode "
  797. "0x%02x\n", cdb[0]);
  798. ret = -EINVAL;
  799. }
  800. return ret;
  801. }
  802. static void usbg_data_write_cmpl(struct usb_ep *ep, struct usb_request *req)
  803. {
  804. struct usbg_cmd *cmd = req->context;
  805. struct se_cmd *se_cmd = &cmd->se_cmd;
  806. if (req->status < 0) {
  807. pr_err("%s() state %d transfer failed\n", __func__, cmd->state);
  808. goto cleanup;
  809. }
  810. if (req->num_sgs == 0) {
  811. sg_copy_from_buffer(se_cmd->t_data_sg,
  812. se_cmd->t_data_nents,
  813. cmd->data_buf,
  814. se_cmd->data_length);
  815. }
  816. complete(&cmd->write_complete);
  817. return;
  818. cleanup:
  819. usbg_cleanup_cmd(cmd);
  820. }
  821. static int usbg_prepare_w_request(struct usbg_cmd *cmd, struct usb_request *req)
  822. {
  823. struct se_cmd *se_cmd = &cmd->se_cmd;
  824. struct f_uas *fu = cmd->fu;
  825. struct usb_gadget *gadget = fuas_to_gadget(fu);
  826. if (!gadget->sg_supported) {
  827. cmd->data_buf = kmalloc(se_cmd->data_length, GFP_ATOMIC);
  828. if (!cmd->data_buf)
  829. return -ENOMEM;
  830. req->buf = cmd->data_buf;
  831. } else {
  832. req->buf = NULL;
  833. req->num_sgs = se_cmd->t_data_nents;
  834. req->sg = se_cmd->t_data_sg;
  835. }
  836. req->complete = usbg_data_write_cmpl;
  837. req->length = se_cmd->data_length;
  838. req->context = cmd;
  839. return 0;
  840. }
  841. static int usbg_send_status_response(struct se_cmd *se_cmd)
  842. {
  843. struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd,
  844. se_cmd);
  845. struct f_uas *fu = cmd->fu;
  846. if (fu->flags & USBG_IS_BOT)
  847. return bot_send_status_response(cmd);
  848. else
  849. return uasp_send_status_response(cmd);
  850. }
  851. static int usbg_send_write_request(struct se_cmd *se_cmd)
  852. {
  853. struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd,
  854. se_cmd);
  855. struct f_uas *fu = cmd->fu;
  856. if (fu->flags & USBG_IS_BOT)
  857. return bot_send_write_request(cmd);
  858. else
  859. return uasp_send_write_request(cmd);
  860. }
  861. static int usbg_send_read_response(struct se_cmd *se_cmd)
  862. {
  863. struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd,
  864. se_cmd);
  865. struct f_uas *fu = cmd->fu;
  866. if (fu->flags & USBG_IS_BOT)
  867. return bot_send_read_response(cmd);
  868. else
  869. return uasp_send_read_response(cmd);
  870. }
  871. static void usbg_cmd_work(struct work_struct *work)
  872. {
  873. struct usbg_cmd *cmd = container_of(work, struct usbg_cmd, work);
  874. struct se_cmd *se_cmd;
  875. struct tcm_usbg_nexus *tv_nexus;
  876. struct usbg_tpg *tpg;
  877. int dir;
  878. se_cmd = &cmd->se_cmd;
  879. tpg = cmd->fu->tpg;
  880. tv_nexus = tpg->tpg_nexus;
  881. dir = get_cmd_dir(cmd->cmd_buf);
  882. if (dir < 0) {
  883. transport_init_se_cmd(se_cmd,
  884. tv_nexus->tvn_se_sess->se_tpg->se_tpg_tfo,
  885. tv_nexus->tvn_se_sess, cmd->data_len, DMA_NONE,
  886. cmd->prio_attr, cmd->sense_iu.sense);
  887. goto out;
  888. }
  889. if (target_submit_cmd(se_cmd, tv_nexus->tvn_se_sess,
  890. cmd->cmd_buf, cmd->sense_iu.sense, cmd->unpacked_lun,
  891. 0, cmd->prio_attr, dir, TARGET_SCF_UNKNOWN_SIZE) < 0)
  892. goto out;
  893. return;
  894. out:
  895. transport_send_check_condition_and_sense(se_cmd,
  896. TCM_UNSUPPORTED_SCSI_OPCODE, 1);
  897. usbg_cleanup_cmd(cmd);
  898. }
  899. static int usbg_submit_command(struct f_uas *fu,
  900. void *cmdbuf, unsigned int len)
  901. {
  902. struct command_iu *cmd_iu = cmdbuf;
  903. struct usbg_cmd *cmd;
  904. struct usbg_tpg *tpg;
  905. struct se_cmd *se_cmd;
  906. struct tcm_usbg_nexus *tv_nexus;
  907. u32 cmd_len;
  908. int ret;
  909. if (cmd_iu->iu_id != IU_ID_COMMAND) {
  910. pr_err("Unsupported type %d\n", cmd_iu->iu_id);
  911. return -EINVAL;
  912. }
  913. cmd = kzalloc(sizeof *cmd, GFP_ATOMIC);
  914. if (!cmd)
  915. return -ENOMEM;
  916. cmd->fu = fu;
  917. /* XXX until I figure out why I can't free in on complete */
  918. kref_init(&cmd->ref);
  919. kref_get(&cmd->ref);
  920. tpg = fu->tpg;
  921. cmd_len = (cmd_iu->len & ~0x3) + 16;
  922. if (cmd_len > USBG_MAX_CMD)
  923. goto err;
  924. memcpy(cmd->cmd_buf, cmd_iu->cdb, cmd_len);
  925. cmd->tag = be16_to_cpup(&cmd_iu->tag);
  926. if (fu->flags & USBG_USE_STREAMS) {
  927. if (cmd->tag > UASP_SS_EP_COMP_NUM_STREAMS)
  928. goto err;
  929. if (!cmd->tag)
  930. cmd->stream = &fu->stream[0];
  931. else
  932. cmd->stream = &fu->stream[cmd->tag - 1];
  933. } else {
  934. cmd->stream = &fu->stream[0];
  935. }
  936. tv_nexus = tpg->tpg_nexus;
  937. if (!tv_nexus) {
  938. pr_err("Missing nexus, ignoring command\n");
  939. goto err;
  940. }
  941. switch (cmd_iu->prio_attr & 0x7) {
  942. case UAS_HEAD_TAG:
  943. cmd->prio_attr = MSG_HEAD_TAG;
  944. break;
  945. case UAS_ORDERED_TAG:
  946. cmd->prio_attr = MSG_ORDERED_TAG;
  947. break;
  948. case UAS_ACA:
  949. cmd->prio_attr = MSG_ACA_TAG;
  950. break;
  951. default:
  952. pr_debug_once("Unsupported prio_attr: %02x.\n",
  953. cmd_iu->prio_attr);
  954. case UAS_SIMPLE_TAG:
  955. cmd->prio_attr = MSG_SIMPLE_TAG;
  956. break;
  957. }
  958. se_cmd = &cmd->se_cmd;
  959. cmd->unpacked_lun = scsilun_to_int(&cmd_iu->lun);
  960. INIT_WORK(&cmd->work, usbg_cmd_work);
  961. ret = queue_work(tpg->workqueue, &cmd->work);
  962. if (ret < 0)
  963. goto err;
  964. return 0;
  965. err:
  966. kfree(cmd);
  967. return -EINVAL;
  968. }
  969. static void bot_cmd_work(struct work_struct *work)
  970. {
  971. struct usbg_cmd *cmd = container_of(work, struct usbg_cmd, work);
  972. struct se_cmd *se_cmd;
  973. struct tcm_usbg_nexus *tv_nexus;
  974. struct usbg_tpg *tpg;
  975. int dir;
  976. se_cmd = &cmd->se_cmd;
  977. tpg = cmd->fu->tpg;
  978. tv_nexus = tpg->tpg_nexus;
  979. dir = get_cmd_dir(cmd->cmd_buf);
  980. if (dir < 0) {
  981. transport_init_se_cmd(se_cmd,
  982. tv_nexus->tvn_se_sess->se_tpg->se_tpg_tfo,
  983. tv_nexus->tvn_se_sess, cmd->data_len, DMA_NONE,
  984. cmd->prio_attr, cmd->sense_iu.sense);
  985. goto out;
  986. }
  987. if (target_submit_cmd(se_cmd, tv_nexus->tvn_se_sess,
  988. cmd->cmd_buf, cmd->sense_iu.sense, cmd->unpacked_lun,
  989. cmd->data_len, cmd->prio_attr, dir, 0) < 0)
  990. goto out;
  991. return;
  992. out:
  993. transport_send_check_condition_and_sense(se_cmd,
  994. TCM_UNSUPPORTED_SCSI_OPCODE, 1);
  995. usbg_cleanup_cmd(cmd);
  996. }
  997. static int bot_submit_command(struct f_uas *fu,
  998. void *cmdbuf, unsigned int len)
  999. {
  1000. struct bulk_cb_wrap *cbw = cmdbuf;
  1001. struct usbg_cmd *cmd;
  1002. struct usbg_tpg *tpg;
  1003. struct se_cmd *se_cmd;
  1004. struct tcm_usbg_nexus *tv_nexus;
  1005. u32 cmd_len;
  1006. int ret;
  1007. if (cbw->Signature != cpu_to_le32(US_BULK_CB_SIGN)) {
  1008. pr_err("Wrong signature on CBW\n");
  1009. return -EINVAL;
  1010. }
  1011. if (len != 31) {
  1012. pr_err("Wrong length for CBW\n");
  1013. return -EINVAL;
  1014. }
  1015. cmd_len = cbw->Length;
  1016. if (cmd_len < 1 || cmd_len > 16)
  1017. return -EINVAL;
  1018. cmd = kzalloc(sizeof *cmd, GFP_ATOMIC);
  1019. if (!cmd)
  1020. return -ENOMEM;
  1021. cmd->fu = fu;
  1022. /* XXX until I figure out why I can't free in on complete */
  1023. kref_init(&cmd->ref);
  1024. kref_get(&cmd->ref);
  1025. tpg = fu->tpg;
  1026. memcpy(cmd->cmd_buf, cbw->CDB, cmd_len);
  1027. cmd->bot_tag = cbw->Tag;
  1028. tv_nexus = tpg->tpg_nexus;
  1029. if (!tv_nexus) {
  1030. pr_err("Missing nexus, ignoring command\n");
  1031. goto err;
  1032. }
  1033. cmd->prio_attr = MSG_SIMPLE_TAG;
  1034. se_cmd = &cmd->se_cmd;
  1035. cmd->unpacked_lun = cbw->Lun;
  1036. cmd->is_read = cbw->Flags & US_BULK_FLAG_IN ? 1 : 0;
  1037. cmd->data_len = le32_to_cpu(cbw->DataTransferLength);
  1038. INIT_WORK(&cmd->work, bot_cmd_work);
  1039. ret = queue_work(tpg->workqueue, &cmd->work);
  1040. if (ret < 0)
  1041. goto err;
  1042. return 0;
  1043. err:
  1044. kfree(cmd);
  1045. return -EINVAL;
  1046. }
  1047. /* Start fabric.c code */
  1048. static int usbg_check_true(struct se_portal_group *se_tpg)
  1049. {
  1050. return 1;
  1051. }
  1052. static int usbg_check_false(struct se_portal_group *se_tpg)
  1053. {
  1054. return 0;
  1055. }
  1056. static char *usbg_get_fabric_name(void)
  1057. {
  1058. return "usb_gadget";
  1059. }
  1060. static u8 usbg_get_fabric_proto_ident(struct se_portal_group *se_tpg)
  1061. {
  1062. struct usbg_tpg *tpg = container_of(se_tpg,
  1063. struct usbg_tpg, se_tpg);
  1064. struct usbg_tport *tport = tpg->tport;
  1065. u8 proto_id;
  1066. switch (tport->tport_proto_id) {
  1067. case SCSI_PROTOCOL_SAS:
  1068. default:
  1069. proto_id = sas_get_fabric_proto_ident(se_tpg);
  1070. break;
  1071. }
  1072. return proto_id;
  1073. }
  1074. static char *usbg_get_fabric_wwn(struct se_portal_group *se_tpg)
  1075. {
  1076. struct usbg_tpg *tpg = container_of(se_tpg,
  1077. struct usbg_tpg, se_tpg);
  1078. struct usbg_tport *tport = tpg->tport;
  1079. return &tport->tport_name[0];
  1080. }
  1081. static u16 usbg_get_tag(struct se_portal_group *se_tpg)
  1082. {
  1083. struct usbg_tpg *tpg = container_of(se_tpg,
  1084. struct usbg_tpg, se_tpg);
  1085. return tpg->tport_tpgt;
  1086. }
  1087. static u32 usbg_get_default_depth(struct se_portal_group *se_tpg)
  1088. {
  1089. return 1;
  1090. }
  1091. static u32 usbg_get_pr_transport_id(
  1092. struct se_portal_group *se_tpg,
  1093. struct se_node_acl *se_nacl,
  1094. struct t10_pr_registration *pr_reg,
  1095. int *format_code,
  1096. unsigned char *buf)
  1097. {
  1098. struct usbg_tpg *tpg = container_of(se_tpg,
  1099. struct usbg_tpg, se_tpg);
  1100. struct usbg_tport *tport = tpg->tport;
  1101. int ret = 0;
  1102. switch (tport->tport_proto_id) {
  1103. case SCSI_PROTOCOL_SAS:
  1104. default:
  1105. ret = sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
  1106. format_code, buf);
  1107. break;
  1108. }
  1109. return ret;
  1110. }
  1111. static u32 usbg_get_pr_transport_id_len(
  1112. struct se_portal_group *se_tpg,
  1113. struct se_node_acl *se_nacl,
  1114. struct t10_pr_registration *pr_reg,
  1115. int *format_code)
  1116. {
  1117. struct usbg_tpg *tpg = container_of(se_tpg,
  1118. struct usbg_tpg, se_tpg);
  1119. struct usbg_tport *tport = tpg->tport;
  1120. int ret = 0;
  1121. switch (tport->tport_proto_id) {
  1122. case SCSI_PROTOCOL_SAS:
  1123. default:
  1124. ret = sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
  1125. format_code);
  1126. break;
  1127. }
  1128. return ret;
  1129. }
  1130. static char *usbg_parse_pr_out_transport_id(
  1131. struct se_portal_group *se_tpg,
  1132. const char *buf,
  1133. u32 *out_tid_len,
  1134. char **port_nexus_ptr)
  1135. {
  1136. struct usbg_tpg *tpg = container_of(se_tpg,
  1137. struct usbg_tpg, se_tpg);
  1138. struct usbg_tport *tport = tpg->tport;
  1139. char *tid = NULL;
  1140. switch (tport->tport_proto_id) {
  1141. case SCSI_PROTOCOL_SAS:
  1142. default:
  1143. tid = sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
  1144. port_nexus_ptr);
  1145. }
  1146. return tid;
  1147. }
  1148. static struct se_node_acl *usbg_alloc_fabric_acl(struct se_portal_group *se_tpg)
  1149. {
  1150. struct usbg_nacl *nacl;
  1151. nacl = kzalloc(sizeof(struct usbg_nacl), GFP_KERNEL);
  1152. if (!nacl) {
  1153. printk(KERN_ERR "Unable to allocate struct usbg_nacl\n");
  1154. return NULL;
  1155. }
  1156. return &nacl->se_node_acl;
  1157. }
  1158. static void usbg_release_fabric_acl(
  1159. struct se_portal_group *se_tpg,
  1160. struct se_node_acl *se_nacl)
  1161. {
  1162. struct usbg_nacl *nacl = container_of(se_nacl,
  1163. struct usbg_nacl, se_node_acl);
  1164. kfree(nacl);
  1165. }
  1166. static u32 usbg_tpg_get_inst_index(struct se_portal_group *se_tpg)
  1167. {
  1168. return 1;
  1169. }
  1170. static void usbg_cmd_release(struct kref *ref)
  1171. {
  1172. struct usbg_cmd *cmd = container_of(ref, struct usbg_cmd,
  1173. ref);
  1174. transport_generic_free_cmd(&cmd->se_cmd, 0);
  1175. }
  1176. static void usbg_release_cmd(struct se_cmd *se_cmd)
  1177. {
  1178. struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd,
  1179. se_cmd);
  1180. kfree(cmd->data_buf);
  1181. kfree(cmd);
  1182. return;
  1183. }
  1184. static int usbg_shutdown_session(struct se_session *se_sess)
  1185. {
  1186. return 0;
  1187. }
  1188. static void usbg_close_session(struct se_session *se_sess)
  1189. {
  1190. return;
  1191. }
  1192. static u32 usbg_sess_get_index(struct se_session *se_sess)
  1193. {
  1194. return 0;
  1195. }
  1196. /*
  1197. * XXX Error recovery: return != 0 if we expect writes. Dunno when that could be
  1198. */
  1199. static int usbg_write_pending_status(struct se_cmd *se_cmd)
  1200. {
  1201. return 0;
  1202. }
  1203. static void usbg_set_default_node_attrs(struct se_node_acl *nacl)
  1204. {
  1205. return;
  1206. }
  1207. static u32 usbg_get_task_tag(struct se_cmd *se_cmd)
  1208. {
  1209. struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd,
  1210. se_cmd);
  1211. struct f_uas *fu = cmd->fu;
  1212. if (fu->flags & USBG_IS_BOT)
  1213. return le32_to_cpu(cmd->bot_tag);
  1214. else
  1215. return cmd->tag;
  1216. }
  1217. static int usbg_get_cmd_state(struct se_cmd *se_cmd)
  1218. {
  1219. return 0;
  1220. }
  1221. static int usbg_queue_tm_rsp(struct se_cmd *se_cmd)
  1222. {
  1223. return 0;
  1224. }
  1225. static const char *usbg_check_wwn(const char *name)
  1226. {
  1227. const char *n;
  1228. unsigned int len;
  1229. n = strstr(name, "naa.");
  1230. if (!n)
  1231. return NULL;
  1232. n += 4;
  1233. len = strlen(n);
  1234. if (len == 0 || len > USBG_NAMELEN - 1)
  1235. return NULL;
  1236. return n;
  1237. }
  1238. static struct se_node_acl *usbg_make_nodeacl(
  1239. struct se_portal_group *se_tpg,
  1240. struct config_group *group,
  1241. const char *name)
  1242. {
  1243. struct se_node_acl *se_nacl, *se_nacl_new;
  1244. struct usbg_nacl *nacl;
  1245. u64 wwpn = 0;
  1246. u32 nexus_depth;
  1247. const char *wnn_name;
  1248. wnn_name = usbg_check_wwn(name);
  1249. if (!wnn_name)
  1250. return ERR_PTR(-EINVAL);
  1251. se_nacl_new = usbg_alloc_fabric_acl(se_tpg);
  1252. if (!(se_nacl_new))
  1253. return ERR_PTR(-ENOMEM);
  1254. nexus_depth = 1;
  1255. /*
  1256. * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
  1257. * when converting a NodeACL from demo mode -> explict
  1258. */
  1259. se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
  1260. name, nexus_depth);
  1261. if (IS_ERR(se_nacl)) {
  1262. usbg_release_fabric_acl(se_tpg, se_nacl_new);
  1263. return se_nacl;
  1264. }
  1265. /*
  1266. * Locate our struct usbg_nacl and set the FC Nport WWPN
  1267. */
  1268. nacl = container_of(se_nacl, struct usbg_nacl, se_node_acl);
  1269. nacl->iport_wwpn = wwpn;
  1270. snprintf(nacl->iport_name, sizeof(nacl->iport_name), "%s", name);
  1271. return se_nacl;
  1272. }
  1273. static void usbg_drop_nodeacl(struct se_node_acl *se_acl)
  1274. {
  1275. struct usbg_nacl *nacl = container_of(se_acl,
  1276. struct usbg_nacl, se_node_acl);
  1277. core_tpg_del_initiator_node_acl(se_acl->se_tpg, se_acl, 1);
  1278. kfree(nacl);
  1279. }
  1280. struct usbg_tpg *the_only_tpg_I_currently_have;
  1281. static struct se_portal_group *usbg_make_tpg(
  1282. struct se_wwn *wwn,
  1283. struct config_group *group,
  1284. const char *name)
  1285. {
  1286. struct usbg_tport *tport = container_of(wwn, struct usbg_tport,
  1287. tport_wwn);
  1288. struct usbg_tpg *tpg;
  1289. unsigned long tpgt;
  1290. int ret;
  1291. if (strstr(name, "tpgt_") != name)
  1292. return ERR_PTR(-EINVAL);
  1293. if (kstrtoul(name + 5, 0, &tpgt) || tpgt > UINT_MAX)
  1294. return ERR_PTR(-EINVAL);
  1295. if (the_only_tpg_I_currently_have) {
  1296. pr_err("Until the gadget framework can't handle multiple\n");
  1297. pr_err("gadgets, you can't do this here.\n");
  1298. return ERR_PTR(-EBUSY);
  1299. }
  1300. tpg = kzalloc(sizeof(struct usbg_tpg), GFP_KERNEL);
  1301. if (!tpg) {
  1302. printk(KERN_ERR "Unable to allocate struct usbg_tpg");
  1303. return ERR_PTR(-ENOMEM);
  1304. }
  1305. mutex_init(&tpg->tpg_mutex);
  1306. atomic_set(&tpg->tpg_port_count, 0);
  1307. tpg->workqueue = alloc_workqueue("tcm_usb_gadget", 0, 1);
  1308. if (!tpg->workqueue) {
  1309. kfree(tpg);
  1310. return NULL;
  1311. }
  1312. tpg->tport = tport;
  1313. tpg->tport_tpgt = tpgt;
  1314. ret = core_tpg_register(&usbg_fabric_configfs->tf_ops, wwn,
  1315. &tpg->se_tpg, tpg,
  1316. TRANSPORT_TPG_TYPE_NORMAL);
  1317. if (ret < 0) {
  1318. destroy_workqueue(tpg->workqueue);
  1319. kfree(tpg);
  1320. return NULL;
  1321. }
  1322. the_only_tpg_I_currently_have = tpg;
  1323. return &tpg->se_tpg;
  1324. }
  1325. static void usbg_drop_tpg(struct se_portal_group *se_tpg)
  1326. {
  1327. struct usbg_tpg *tpg = container_of(se_tpg,
  1328. struct usbg_tpg, se_tpg);
  1329. core_tpg_deregister(se_tpg);
  1330. destroy_workqueue(tpg->workqueue);
  1331. kfree(tpg);
  1332. the_only_tpg_I_currently_have = NULL;
  1333. }
  1334. static struct se_wwn *usbg_make_tport(
  1335. struct target_fabric_configfs *tf,
  1336. struct config_group *group,
  1337. const char *name)
  1338. {
  1339. struct usbg_tport *tport;
  1340. const char *wnn_name;
  1341. u64 wwpn = 0;
  1342. wnn_name = usbg_check_wwn(name);
  1343. if (!wnn_name)
  1344. return ERR_PTR(-EINVAL);
  1345. tport = kzalloc(sizeof(struct usbg_tport), GFP_KERNEL);
  1346. if (!(tport)) {
  1347. printk(KERN_ERR "Unable to allocate struct usbg_tport");
  1348. return ERR_PTR(-ENOMEM);
  1349. }
  1350. tport->tport_wwpn = wwpn;
  1351. snprintf(tport->tport_name, sizeof(tport->tport_name), wnn_name);
  1352. return &tport->tport_wwn;
  1353. }
  1354. static void usbg_drop_tport(struct se_wwn *wwn)
  1355. {
  1356. struct usbg_tport *tport = container_of(wwn,
  1357. struct usbg_tport, tport_wwn);
  1358. kfree(tport);
  1359. }
  1360. /*
  1361. * If somebody feels like dropping the version property, go ahead.
  1362. */
  1363. static ssize_t usbg_wwn_show_attr_version(
  1364. struct target_fabric_configfs *tf,
  1365. char *page)
  1366. {
  1367. return sprintf(page, "usb-gadget fabric module\n");
  1368. }
  1369. TF_WWN_ATTR_RO(usbg, version);
  1370. static struct configfs_attribute *usbg_wwn_attrs[] = {
  1371. &usbg_wwn_version.attr,
  1372. NULL,
  1373. };
  1374. static ssize_t tcm_usbg_tpg_show_enable(
  1375. struct se_portal_group *se_tpg,
  1376. char *page)
  1377. {
  1378. struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
  1379. return snprintf(page, PAGE_SIZE, "%u\n", tpg->gadget_connect);
  1380. }
  1381. static int usbg_attach(struct usbg_tpg *);
  1382. static void usbg_detach(struct usbg_tpg *);
  1383. static ssize_t tcm_usbg_tpg_store_enable(
  1384. struct se_portal_group *se_tpg,
  1385. const char *page,
  1386. size_t count)
  1387. {
  1388. struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
  1389. unsigned long op;
  1390. ssize_t ret;
  1391. ret = kstrtoul(page, 0, &op);
  1392. if (ret < 0)
  1393. return -EINVAL;
  1394. if (op > 1)
  1395. return -EINVAL;
  1396. if (op && tpg->gadget_connect)
  1397. goto out;
  1398. if (!op && !tpg->gadget_connect)
  1399. goto out;
  1400. if (op) {
  1401. ret = usbg_attach(tpg);
  1402. if (ret)
  1403. goto out;
  1404. } else {
  1405. usbg_detach(tpg);
  1406. }
  1407. tpg->gadget_connect = op;
  1408. out:
  1409. return count;
  1410. }
  1411. TF_TPG_BASE_ATTR(tcm_usbg, enable, S_IRUGO | S_IWUSR);
  1412. static ssize_t tcm_usbg_tpg_show_nexus(
  1413. struct se_portal_group *se_tpg,
  1414. char *page)
  1415. {
  1416. struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
  1417. struct tcm_usbg_nexus *tv_nexus;
  1418. ssize_t ret;
  1419. mutex_lock(&tpg->tpg_mutex);
  1420. tv_nexus = tpg->tpg_nexus;
  1421. if (!tv_nexus) {
  1422. ret = -ENODEV;
  1423. goto out;
  1424. }
  1425. ret = snprintf(page, PAGE_SIZE, "%s\n",
  1426. tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
  1427. out:
  1428. mutex_unlock(&tpg->tpg_mutex);
  1429. return ret;
  1430. }
  1431. static int tcm_usbg_make_nexus(struct usbg_tpg *tpg, char *name)
  1432. {
  1433. struct se_portal_group *se_tpg;
  1434. struct tcm_usbg_nexus *tv_nexus;
  1435. int ret;
  1436. mutex_lock(&tpg->tpg_mutex);
  1437. if (tpg->tpg_nexus) {
  1438. ret = -EEXIST;
  1439. pr_debug("tpg->tpg_nexus already exists\n");
  1440. goto err_unlock;
  1441. }
  1442. se_tpg = &tpg->se_tpg;
  1443. ret = -ENOMEM;
  1444. tv_nexus = kzalloc(sizeof(*tv_nexus), GFP_KERNEL);
  1445. if (!tv_nexus) {
  1446. pr_err("Unable to allocate struct tcm_vhost_nexus\n");
  1447. goto err_unlock;
  1448. }
  1449. tv_nexus->tvn_se_sess = transport_init_session();
  1450. if (IS_ERR(tv_nexus->tvn_se_sess))
  1451. goto err_free;
  1452. /*
  1453. * Since we are running in 'demo mode' this call with generate a
  1454. * struct se_node_acl for the tcm_vhost struct se_portal_group with
  1455. * the SCSI Initiator port name of the passed configfs group 'name'.
  1456. */
  1457. tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
  1458. se_tpg, name);
  1459. if (!tv_nexus->tvn_se_sess->se_node_acl) {
  1460. pr_debug("core_tpg_check_initiator_node_acl() failed"
  1461. " for %s\n", name);
  1462. goto err_session;
  1463. }
  1464. /*
  1465. * Now register the TCM vHost virtual I_T Nexus as active with the
  1466. * call to __transport_register_session()
  1467. */
  1468. __transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl,
  1469. tv_nexus->tvn_se_sess, tv_nexus);
  1470. tpg->tpg_nexus = tv_nexus;
  1471. mutex_unlock(&tpg->tpg_mutex);
  1472. return 0;
  1473. err_session:
  1474. transport_free_session(tv_nexus->tvn_se_sess);
  1475. err_free:
  1476. kfree(tv_nexus);
  1477. err_unlock:
  1478. mutex_unlock(&tpg->tpg_mutex);
  1479. return ret;
  1480. }
  1481. static int tcm_usbg_drop_nexus(struct usbg_tpg *tpg)
  1482. {
  1483. struct se_session *se_sess;
  1484. struct tcm_usbg_nexus *tv_nexus;
  1485. int ret = -ENODEV;
  1486. mutex_lock(&tpg->tpg_mutex);
  1487. tv_nexus = tpg->tpg_nexus;
  1488. if (!tv_nexus)
  1489. goto out;
  1490. se_sess = tv_nexus->tvn_se_sess;
  1491. if (!se_sess)
  1492. goto out;
  1493. if (atomic_read(&tpg->tpg_port_count)) {
  1494. ret = -EPERM;
  1495. pr_err("Unable to remove Host I_T Nexus with"
  1496. " active TPG port count: %d\n",
  1497. atomic_read(&tpg->tpg_port_count));
  1498. goto out;
  1499. }
  1500. pr_debug("Removing I_T Nexus to Initiator Port: %s\n",
  1501. tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
  1502. /*
  1503. * Release the SCSI I_T Nexus to the emulated vHost Target Port
  1504. */
  1505. transport_deregister_session(tv_nexus->tvn_se_sess);
  1506. tpg->tpg_nexus = NULL;
  1507. kfree(tv_nexus);
  1508. out:
  1509. mutex_unlock(&tpg->tpg_mutex);
  1510. return 0;
  1511. }
  1512. static ssize_t tcm_usbg_tpg_store_nexus(
  1513. struct se_portal_group *se_tpg,
  1514. const char *page,
  1515. size_t count)
  1516. {
  1517. struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
  1518. unsigned char i_port[USBG_NAMELEN], *ptr;
  1519. int ret;
  1520. if (!strncmp(page, "NULL", 4)) {
  1521. ret = tcm_usbg_drop_nexus(tpg);
  1522. return (!ret) ? count : ret;
  1523. }
  1524. if (strlen(page) >= USBG_NAMELEN) {
  1525. pr_err("Emulated NAA Sas Address: %s, exceeds"
  1526. " max: %d\n", page, USBG_NAMELEN);
  1527. return -EINVAL;
  1528. }
  1529. snprintf(i_port, USBG_NAMELEN, "%s", page);
  1530. ptr = strstr(i_port, "naa.");
  1531. if (!ptr) {
  1532. pr_err("Missing 'naa.' prefix\n");
  1533. return -EINVAL;
  1534. }
  1535. if (i_port[strlen(i_port) - 1] == '\n')
  1536. i_port[strlen(i_port) - 1] = '\0';
  1537. ret = tcm_usbg_make_nexus(tpg, &i_port[4]);
  1538. if (ret < 0)
  1539. return ret;
  1540. return count;
  1541. }
  1542. TF_TPG_BASE_ATTR(tcm_usbg, nexus, S_IRUGO | S_IWUSR);
  1543. static struct configfs_attribute *usbg_base_attrs[] = {
  1544. &tcm_usbg_tpg_enable.attr,
  1545. &tcm_usbg_tpg_nexus.attr,
  1546. NULL,
  1547. };
  1548. static int usbg_port_link(struct se_portal_group *se_tpg, struct se_lun *lun)
  1549. {
  1550. struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
  1551. atomic_inc(&tpg->tpg_port_count);
  1552. smp_mb__after_atomic_inc();
  1553. return 0;
  1554. }
  1555. static void usbg_port_unlink(struct se_portal_group *se_tpg,
  1556. struct se_lun *se_lun)
  1557. {
  1558. struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
  1559. atomic_dec(&tpg->tpg_port_count);
  1560. smp_mb__after_atomic_dec();
  1561. }
  1562. static int usbg_check_stop_free(struct se_cmd *se_cmd)
  1563. {
  1564. struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd,
  1565. se_cmd);
  1566. kref_put(&cmd->ref, usbg_cmd_release);
  1567. return 1;
  1568. }
  1569. static struct target_core_fabric_ops usbg_ops = {
  1570. .get_fabric_name = usbg_get_fabric_name,
  1571. .get_fabric_proto_ident = usbg_get_fabric_proto_ident,
  1572. .tpg_get_wwn = usbg_get_fabric_wwn,
  1573. .tpg_get_tag = usbg_get_tag,
  1574. .tpg_get_default_depth = usbg_get_default_depth,
  1575. .tpg_get_pr_transport_id = usbg_get_pr_transport_id,
  1576. .tpg_get_pr_transport_id_len = usbg_get_pr_transport_id_len,
  1577. .tpg_parse_pr_out_transport_id = usbg_parse_pr_out_transport_id,
  1578. .tpg_check_demo_mode = usbg_check_true,
  1579. .tpg_check_demo_mode_cache = usbg_check_false,
  1580. .tpg_check_demo_mode_write_protect = usbg_check_false,
  1581. .tpg_check_prod_mode_write_protect = usbg_check_false,
  1582. .tpg_alloc_fabric_acl = usbg_alloc_fabric_acl,
  1583. .tpg_release_fabric_acl = usbg_release_fabric_acl,
  1584. .tpg_get_inst_index = usbg_tpg_get_inst_index,
  1585. .release_cmd = usbg_release_cmd,
  1586. .shutdown_session = usbg_shutdown_session,
  1587. .close_session = usbg_close_session,
  1588. .sess_get_index = usbg_sess_get_index,
  1589. .sess_get_initiator_sid = NULL,
  1590. .write_pending = usbg_send_write_request,
  1591. .write_pending_status = usbg_write_pending_status,
  1592. .set_default_node_attributes = usbg_set_default_node_attrs,
  1593. .get_task_tag = usbg_get_task_tag,
  1594. .get_cmd_state = usbg_get_cmd_state,
  1595. .queue_data_in = usbg_send_read_response,
  1596. .queue_status = usbg_send_status_response,
  1597. .queue_tm_rsp = usbg_queue_tm_rsp,
  1598. .check_stop_free = usbg_check_stop_free,
  1599. .fabric_make_wwn = usbg_make_tport,
  1600. .fabric_drop_wwn = usbg_drop_tport,
  1601. .fabric_make_tpg = usbg_make_tpg,
  1602. .fabric_drop_tpg = usbg_drop_tpg,
  1603. .fabric_post_link = usbg_port_link,
  1604. .fabric_pre_unlink = usbg_port_unlink,
  1605. .fabric_make_np = NULL,
  1606. .fabric_drop_np = NULL,
  1607. .fabric_make_nodeacl = usbg_make_nodeacl,
  1608. .fabric_drop_nodeacl = usbg_drop_nodeacl,
  1609. };
  1610. static int usbg_register_configfs(void)
  1611. {
  1612. struct target_fabric_configfs *fabric;
  1613. int ret;
  1614. fabric = target_fabric_configfs_init(THIS_MODULE, "usb_gadget");
  1615. if (IS_ERR(fabric)) {
  1616. printk(KERN_ERR "target_fabric_configfs_init() failed\n");
  1617. return PTR_ERR(fabric);
  1618. }
  1619. fabric->tf_ops = usbg_ops;
  1620. TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = usbg_wwn_attrs;
  1621. TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = usbg_base_attrs;
  1622. TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL;
  1623. TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
  1624. TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
  1625. TF_CIT_TMPL(fabric)->tfc_tpg_nacl_base_cit.ct_attrs = NULL;
  1626. TF_CIT_TMPL(fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
  1627. TF_CIT_TMPL(fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
  1628. TF_CIT_TMPL(fabric)->tfc_tpg_nacl_param_cit.ct_attrs = NULL;
  1629. ret = target_fabric_configfs_register(fabric);
  1630. if (ret < 0) {
  1631. printk(KERN_ERR "target_fabric_configfs_register() failed"
  1632. " for usb-gadget\n");
  1633. return ret;
  1634. }
  1635. usbg_fabric_configfs = fabric;
  1636. return 0;
  1637. };
  1638. static void usbg_deregister_configfs(void)
  1639. {
  1640. if (!(usbg_fabric_configfs))
  1641. return;
  1642. target_fabric_configfs_deregister(usbg_fabric_configfs);
  1643. usbg_fabric_configfs = NULL;
  1644. };
  1645. /* Start gadget.c code */
  1646. static struct usb_interface_descriptor bot_intf_desc = {
  1647. .bLength = sizeof(bot_intf_desc),
  1648. .bDescriptorType = USB_DT_INTERFACE,
  1649. .bNumEndpoints = 2,
  1650. .bAlternateSetting = USB_G_ALT_INT_BBB,
  1651. .bInterfaceClass = USB_CLASS_MASS_STORAGE,
  1652. .bInterfaceSubClass = USB_SC_SCSI,
  1653. .bInterfaceProtocol = USB_PR_BULK,
  1654. };
  1655. static struct usb_interface_descriptor uasp_intf_desc = {
  1656. .bLength = sizeof(uasp_intf_desc),
  1657. .bDescriptorType = USB_DT_INTERFACE,
  1658. .bNumEndpoints = 4,
  1659. .bAlternateSetting = USB_G_ALT_INT_UAS,
  1660. .bInterfaceClass = USB_CLASS_MASS_STORAGE,
  1661. .bInterfaceSubClass = USB_SC_SCSI,
  1662. .bInterfaceProtocol = USB_PR_UAS,
  1663. };
  1664. static struct usb_endpoint_descriptor uasp_bi_desc = {
  1665. .bLength = USB_DT_ENDPOINT_SIZE,
  1666. .bDescriptorType = USB_DT_ENDPOINT,
  1667. .bEndpointAddress = USB_DIR_IN,
  1668. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1669. .wMaxPacketSize = cpu_to_le16(512),
  1670. };
  1671. static struct usb_endpoint_descriptor uasp_fs_bi_desc = {
  1672. .bLength = USB_DT_ENDPOINT_SIZE,
  1673. .bDescriptorType = USB_DT_ENDPOINT,
  1674. .bEndpointAddress = USB_DIR_IN,
  1675. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1676. };
  1677. static struct usb_pipe_usage_descriptor uasp_bi_pipe_desc = {
  1678. .bLength = sizeof(uasp_bi_pipe_desc),
  1679. .bDescriptorType = USB_DT_PIPE_USAGE,
  1680. .bPipeID = DATA_IN_PIPE_ID,
  1681. };
  1682. static struct usb_endpoint_descriptor uasp_ss_bi_desc = {
  1683. .bLength = USB_DT_ENDPOINT_SIZE,
  1684. .bDescriptorType = USB_DT_ENDPOINT,
  1685. .bEndpointAddress = USB_DIR_IN,
  1686. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1687. .wMaxPacketSize = cpu_to_le16(1024),
  1688. };
  1689. static struct usb_ss_ep_comp_descriptor uasp_bi_ep_comp_desc = {
  1690. .bLength = sizeof(uasp_bi_ep_comp_desc),
  1691. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  1692. .bMaxBurst = 0,
  1693. .bmAttributes = UASP_SS_EP_COMP_LOG_STREAMS,
  1694. .wBytesPerInterval = 0,
  1695. };
  1696. static struct usb_ss_ep_comp_descriptor bot_bi_ep_comp_desc = {
  1697. .bLength = sizeof(bot_bi_ep_comp_desc),
  1698. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  1699. .bMaxBurst = 0,
  1700. };
  1701. static struct usb_endpoint_descriptor uasp_bo_desc = {
  1702. .bLength = USB_DT_ENDPOINT_SIZE,
  1703. .bDescriptorType = USB_DT_ENDPOINT,
  1704. .bEndpointAddress = USB_DIR_OUT,
  1705. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1706. .wMaxPacketSize = cpu_to_le16(512),
  1707. };
  1708. static struct usb_endpoint_descriptor uasp_fs_bo_desc = {
  1709. .bLength = USB_DT_ENDPOINT_SIZE,
  1710. .bDescriptorType = USB_DT_ENDPOINT,
  1711. .bEndpointAddress = USB_DIR_OUT,
  1712. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1713. };
  1714. static struct usb_pipe_usage_descriptor uasp_bo_pipe_desc = {
  1715. .bLength = sizeof(uasp_bo_pipe_desc),
  1716. .bDescriptorType = USB_DT_PIPE_USAGE,
  1717. .bPipeID = DATA_OUT_PIPE_ID,
  1718. };
  1719. static struct usb_endpoint_descriptor uasp_ss_bo_desc = {
  1720. .bLength = USB_DT_ENDPOINT_SIZE,
  1721. .bDescriptorType = USB_DT_ENDPOINT,
  1722. .bEndpointAddress = USB_DIR_OUT,
  1723. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1724. .wMaxPacketSize = cpu_to_le16(0x400),
  1725. };
  1726. static struct usb_ss_ep_comp_descriptor uasp_bo_ep_comp_desc = {
  1727. .bLength = sizeof(uasp_bo_ep_comp_desc),
  1728. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  1729. .bmAttributes = UASP_SS_EP_COMP_LOG_STREAMS,
  1730. };
  1731. static struct usb_ss_ep_comp_descriptor bot_bo_ep_comp_desc = {
  1732. .bLength = sizeof(bot_bo_ep_comp_desc),
  1733. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  1734. };
  1735. static struct usb_endpoint_descriptor uasp_status_desc = {
  1736. .bLength = USB_DT_ENDPOINT_SIZE,
  1737. .bDescriptorType = USB_DT_ENDPOINT,
  1738. .bEndpointAddress = USB_DIR_IN,
  1739. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1740. .wMaxPacketSize = cpu_to_le16(512),
  1741. };
  1742. static struct usb_endpoint_descriptor uasp_fs_status_desc = {
  1743. .bLength = USB_DT_ENDPOINT_SIZE,
  1744. .bDescriptorType = USB_DT_ENDPOINT,
  1745. .bEndpointAddress = USB_DIR_IN,
  1746. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1747. };
  1748. static struct usb_pipe_usage_descriptor uasp_status_pipe_desc = {
  1749. .bLength = sizeof(uasp_status_pipe_desc),
  1750. .bDescriptorType = USB_DT_PIPE_USAGE,
  1751. .bPipeID = STATUS_PIPE_ID,
  1752. };
  1753. static struct usb_endpoint_descriptor uasp_ss_status_desc = {
  1754. .bLength = USB_DT_ENDPOINT_SIZE,
  1755. .bDescriptorType = USB_DT_ENDPOINT,
  1756. .bEndpointAddress = USB_DIR_IN,
  1757. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1758. .wMaxPacketSize = cpu_to_le16(1024),
  1759. };
  1760. static struct usb_ss_ep_comp_descriptor uasp_status_in_ep_comp_desc = {
  1761. .bLength = sizeof(uasp_status_in_ep_comp_desc),
  1762. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  1763. .bmAttributes = UASP_SS_EP_COMP_LOG_STREAMS,
  1764. };
  1765. static struct usb_endpoint_descriptor uasp_cmd_desc = {
  1766. .bLength = USB_DT_ENDPOINT_SIZE,
  1767. .bDescriptorType = USB_DT_ENDPOINT,
  1768. .bEndpointAddress = USB_DIR_OUT,
  1769. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1770. .wMaxPacketSize = cpu_to_le16(512),
  1771. };
  1772. static struct usb_endpoint_descriptor uasp_fs_cmd_desc = {
  1773. .bLength = USB_DT_ENDPOINT_SIZE,
  1774. .bDescriptorType = USB_DT_ENDPOINT,
  1775. .bEndpointAddress = USB_DIR_OUT,
  1776. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1777. };
  1778. static struct usb_pipe_usage_descriptor uasp_cmd_pipe_desc = {
  1779. .bLength = sizeof(uasp_cmd_pipe_desc),
  1780. .bDescriptorType = USB_DT_PIPE_USAGE,
  1781. .bPipeID = CMD_PIPE_ID,
  1782. };
  1783. static struct usb_endpoint_descriptor uasp_ss_cmd_desc = {
  1784. .bLength = USB_DT_ENDPOINT_SIZE,
  1785. .bDescriptorType = USB_DT_ENDPOINT,
  1786. .bEndpointAddress = USB_DIR_OUT,
  1787. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1788. .wMaxPacketSize = cpu_to_le16(1024),
  1789. };
  1790. static struct usb_ss_ep_comp_descriptor uasp_cmd_comp_desc = {
  1791. .bLength = sizeof(uasp_cmd_comp_desc),
  1792. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  1793. };
  1794. static struct usb_descriptor_header *uasp_fs_function_desc[] = {
  1795. (struct usb_descriptor_header *) &bot_intf_desc,
  1796. (struct usb_descriptor_header *) &uasp_fs_bi_desc,
  1797. (struct usb_descriptor_header *) &uasp_fs_bo_desc,
  1798. (struct usb_descriptor_header *) &uasp_intf_desc,
  1799. (struct usb_descriptor_header *) &uasp_fs_bi_desc,
  1800. (struct usb_descriptor_header *) &uasp_bi_pipe_desc,
  1801. (struct usb_descriptor_header *) &uasp_fs_bo_desc,
  1802. (struct usb_descriptor_header *) &uasp_bo_pipe_desc,
  1803. (struct usb_descriptor_header *) &uasp_fs_status_desc,
  1804. (struct usb_descriptor_header *) &uasp_status_pipe_desc,
  1805. (struct usb_descriptor_header *) &uasp_fs_cmd_desc,
  1806. (struct usb_descriptor_header *) &uasp_cmd_pipe_desc,
  1807. NULL,
  1808. };
  1809. static struct usb_descriptor_header *uasp_hs_function_desc[] = {
  1810. (struct usb_descriptor_header *) &bot_intf_desc,
  1811. (struct usb_descriptor_header *) &uasp_bi_desc,
  1812. (struct usb_descriptor_header *) &uasp_bo_desc,
  1813. (struct usb_descriptor_header *) &uasp_intf_desc,
  1814. (struct usb_descriptor_header *) &uasp_bi_desc,
  1815. (struct usb_descriptor_header *) &uasp_bi_pipe_desc,
  1816. (struct usb_descriptor_header *) &uasp_bo_desc,
  1817. (struct usb_descriptor_header *) &uasp_bo_pipe_desc,
  1818. (struct usb_descriptor_header *) &uasp_status_desc,
  1819. (struct usb_descriptor_header *) &uasp_status_pipe_desc,
  1820. (struct usb_descriptor_header *) &uasp_cmd_desc,
  1821. (struct usb_descriptor_header *) &uasp_cmd_pipe_desc,
  1822. NULL,
  1823. };
  1824. static struct usb_descriptor_header *uasp_ss_function_desc[] = {
  1825. (struct usb_descriptor_header *) &bot_intf_desc,
  1826. (struct usb_descriptor_header *) &uasp_ss_bi_desc,
  1827. (struct usb_descriptor_header *) &bot_bi_ep_comp_desc,
  1828. (struct usb_descriptor_header *) &uasp_ss_bo_desc,
  1829. (struct usb_descriptor_header *) &bot_bo_ep_comp_desc,
  1830. (struct usb_descriptor_header *) &uasp_intf_desc,
  1831. (struct usb_descriptor_header *) &uasp_ss_bi_desc,
  1832. (struct usb_descriptor_header *) &uasp_bi_ep_comp_desc,
  1833. (struct usb_descriptor_header *) &uasp_bi_pipe_desc,
  1834. (struct usb_descriptor_header *) &uasp_ss_bo_desc,
  1835. (struct usb_descriptor_header *) &uasp_bo_ep_comp_desc,
  1836. (struct usb_descriptor_header *) &uasp_bo_pipe_desc,
  1837. (struct usb_descriptor_header *) &uasp_ss_status_desc,
  1838. (struct usb_descriptor_header *) &uasp_status_in_ep_comp_desc,
  1839. (struct usb_descriptor_header *) &uasp_status_pipe_desc,
  1840. (struct usb_descriptor_header *) &uasp_ss_cmd_desc,
  1841. (struct usb_descriptor_header *) &uasp_cmd_comp_desc,
  1842. (struct usb_descriptor_header *) &uasp_cmd_pipe_desc,
  1843. NULL,
  1844. };
  1845. #define UAS_VENDOR_ID 0x0525 /* NetChip */
  1846. #define UAS_PRODUCT_ID 0xa4a5 /* Linux-USB File-backed Storage Gadget */
  1847. static struct usb_device_descriptor usbg_device_desc = {
  1848. .bLength = sizeof(usbg_device_desc),
  1849. .bDescriptorType = USB_DT_DEVICE,
  1850. .bcdUSB = cpu_to_le16(0x0200),
  1851. .bDeviceClass = USB_CLASS_PER_INTERFACE,
  1852. .idVendor = cpu_to_le16(UAS_VENDOR_ID),
  1853. .idProduct = cpu_to_le16(UAS_PRODUCT_ID),
  1854. .bNumConfigurations = 1,
  1855. };
  1856. static struct usb_string usbg_us_strings[] = {
  1857. [USB_GADGET_MANUFACTURER_IDX].s = "Target Manufactor",
  1858. [USB_GADGET_PRODUCT_IDX].s = "Target Product",
  1859. [USB_GADGET_SERIAL_IDX].s = "000000000001",
  1860. [USB_G_STR_CONFIG].s = "default config",
  1861. [USB_G_STR_INT_UAS].s = "USB Attached SCSI",
  1862. [USB_G_STR_INT_BBB].s = "Bulk Only Transport",
  1863. { },
  1864. };
  1865. static struct usb_gadget_strings usbg_stringtab = {
  1866. .language = 0x0409,
  1867. .strings = usbg_us_strings,
  1868. };
  1869. static struct usb_gadget_strings *usbg_strings[] = {
  1870. &usbg_stringtab,
  1871. NULL,
  1872. };
  1873. static int guas_unbind(struct usb_composite_dev *cdev)
  1874. {
  1875. return 0;
  1876. }
  1877. static struct usb_configuration usbg_config_driver = {
  1878. .label = "Linux Target",
  1879. .bConfigurationValue = 1,
  1880. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  1881. };
  1882. static void give_back_ep(struct usb_ep **pep)
  1883. {
  1884. struct usb_ep *ep = *pep;
  1885. if (!ep)
  1886. return;
  1887. ep->driver_data = NULL;
  1888. }
  1889. static int usbg_bind(struct usb_configuration *c, struct usb_function *f)
  1890. {
  1891. struct f_uas *fu = to_f_uas(f);
  1892. struct usb_gadget *gadget = c->cdev->gadget;
  1893. struct usb_ep *ep;
  1894. int iface;
  1895. int ret;
  1896. iface = usb_interface_id(c, f);
  1897. if (iface < 0)
  1898. return iface;
  1899. bot_intf_desc.bInterfaceNumber = iface;
  1900. uasp_intf_desc.bInterfaceNumber = iface;
  1901. fu->iface = iface;
  1902. ep = usb_ep_autoconfig_ss(gadget, &uasp_ss_bi_desc,
  1903. &uasp_bi_ep_comp_desc);
  1904. if (!ep)
  1905. goto ep_fail;
  1906. ep->driver_data = fu;
  1907. fu->ep_in = ep;
  1908. ep = usb_ep_autoconfig_ss(gadget, &uasp_ss_bo_desc,
  1909. &uasp_bo_ep_comp_desc);
  1910. if (!ep)
  1911. goto ep_fail;
  1912. ep->driver_data = fu;
  1913. fu->ep_out = ep;
  1914. ep = usb_ep_autoconfig_ss(gadget, &uasp_ss_status_desc,
  1915. &uasp_status_in_ep_comp_desc);
  1916. if (!ep)
  1917. goto ep_fail;
  1918. ep->driver_data = fu;
  1919. fu->ep_status = ep;
  1920. ep = usb_ep_autoconfig_ss(gadget, &uasp_ss_cmd_desc,
  1921. &uasp_cmd_comp_desc);
  1922. if (!ep)
  1923. goto ep_fail;
  1924. ep->driver_data = fu;
  1925. fu->ep_cmd = ep;
  1926. /* Assume endpoint addresses are the same for both speeds */
  1927. uasp_bi_desc.bEndpointAddress = uasp_ss_bi_desc.bEndpointAddress;
  1928. uasp_bo_desc.bEndpointAddress = uasp_ss_bo_desc.bEndpointAddress;
  1929. uasp_status_desc.bEndpointAddress =
  1930. uasp_ss_status_desc.bEndpointAddress;
  1931. uasp_cmd_desc.bEndpointAddress = uasp_ss_cmd_desc.bEndpointAddress;
  1932. uasp_fs_bi_desc.bEndpointAddress = uasp_ss_bi_desc.bEndpointAddress;
  1933. uasp_fs_bo_desc.bEndpointAddress = uasp_ss_bo_desc.bEndpointAddress;
  1934. uasp_fs_status_desc.bEndpointAddress =
  1935. uasp_ss_status_desc.bEndpointAddress;
  1936. uasp_fs_cmd_desc.bEndpointAddress = uasp_ss_cmd_desc.bEndpointAddress;
  1937. ret = usb_assign_descriptors(f, uasp_fs_function_desc,
  1938. uasp_hs_function_desc, uasp_ss_function_desc);
  1939. if (ret)
  1940. goto ep_fail;
  1941. return 0;
  1942. ep_fail:
  1943. pr_err("Can't claim all required eps\n");
  1944. give_back_ep(&fu->ep_in);
  1945. give_back_ep(&fu->ep_out);
  1946. give_back_ep(&fu->ep_status);
  1947. give_back_ep(&fu->ep_cmd);
  1948. return -ENOTSUPP;
  1949. }
  1950. static void usbg_unbind(struct usb_configuration *c, struct usb_function *f)
  1951. {
  1952. struct f_uas *fu = to_f_uas(f);
  1953. usb_free_all_descriptors(f);
  1954. kfree(fu);
  1955. }
  1956. struct guas_setup_wq {
  1957. struct work_struct work;
  1958. struct f_uas *fu;
  1959. unsigned int alt;
  1960. };
  1961. static void usbg_delayed_set_alt(struct work_struct *wq)
  1962. {
  1963. struct guas_setup_wq *work = container_of(wq, struct guas_setup_wq,
  1964. work);
  1965. struct f_uas *fu = work->fu;
  1966. int alt = work->alt;
  1967. kfree(work);
  1968. if (fu->flags & USBG_IS_BOT)
  1969. bot_cleanup_old_alt(fu);
  1970. if (fu->flags & USBG_IS_UAS)
  1971. uasp_cleanup_old_alt(fu);
  1972. if (alt == USB_G_ALT_INT_BBB)
  1973. bot_set_alt(fu);
  1974. else if (alt == USB_G_ALT_INT_UAS)
  1975. uasp_set_alt(fu);
  1976. usb_composite_setup_continue(fu->function.config->cdev);
  1977. }
  1978. static int usbg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  1979. {
  1980. struct f_uas *fu = to_f_uas(f);
  1981. if ((alt == USB_G_ALT_INT_BBB) || (alt == USB_G_ALT_INT_UAS)) {
  1982. struct guas_setup_wq *work;
  1983. work = kmalloc(sizeof(*work), GFP_ATOMIC);
  1984. if (!work)
  1985. return -ENOMEM;
  1986. INIT_WORK(&work->work, usbg_delayed_set_alt);
  1987. work->fu = fu;
  1988. work->alt = alt;
  1989. schedule_work(&work->work);
  1990. return USB_GADGET_DELAYED_STATUS;
  1991. }
  1992. return -EOPNOTSUPP;
  1993. }
  1994. static void usbg_disable(struct usb_function *f)
  1995. {
  1996. struct f_uas *fu = to_f_uas(f);
  1997. if (fu->flags & USBG_IS_UAS)
  1998. uasp_cleanup_old_alt(fu);
  1999. else if (fu->flags & USBG_IS_BOT)
  2000. bot_cleanup_old_alt(fu);
  2001. fu->flags = 0;
  2002. }
  2003. static int usbg_setup(struct usb_function *f,
  2004. const struct usb_ctrlrequest *ctrl)
  2005. {
  2006. struct f_uas *fu = to_f_uas(f);
  2007. if (!(fu->flags & USBG_IS_BOT))
  2008. return -EOPNOTSUPP;
  2009. return usbg_bot_setup(f, ctrl);
  2010. }
  2011. static int usbg_cfg_bind(struct usb_configuration *c)
  2012. {
  2013. struct f_uas *fu;
  2014. int ret;
  2015. fu = kzalloc(sizeof(*fu), GFP_KERNEL);
  2016. if (!fu)
  2017. return -ENOMEM;
  2018. fu->function.name = "Target Function";
  2019. fu->function.bind = usbg_bind;
  2020. fu->function.unbind = usbg_unbind;
  2021. fu->function.set_alt = usbg_set_alt;
  2022. fu->function.setup = usbg_setup;
  2023. fu->function.disable = usbg_disable;
  2024. fu->tpg = the_only_tpg_I_currently_have;
  2025. bot_intf_desc.iInterface = usbg_us_strings[USB_G_STR_INT_BBB].id;
  2026. uasp_intf_desc.iInterface = usbg_us_strings[USB_G_STR_INT_UAS].id;
  2027. ret = usb_add_function(c, &fu->function);
  2028. if (ret)
  2029. goto err;
  2030. return 0;
  2031. err:
  2032. kfree(fu);
  2033. return ret;
  2034. }
  2035. static int usb_target_bind(struct usb_composite_dev *cdev)
  2036. {
  2037. int ret;
  2038. ret = usb_string_ids_tab(cdev, usbg_us_strings);
  2039. if (ret)
  2040. return ret;
  2041. usbg_device_desc.iManufacturer =
  2042. usbg_us_strings[USB_GADGET_MANUFACTURER_IDX].id;
  2043. usbg_device_desc.iProduct = usbg_us_strings[USB_GADGET_PRODUCT_IDX].id;
  2044. usbg_device_desc.iSerialNumber =
  2045. usbg_us_strings[USB_GADGET_SERIAL_IDX].id;
  2046. usbg_config_driver.iConfiguration =
  2047. usbg_us_strings[USB_G_STR_CONFIG].id;
  2048. ret = usb_add_config(cdev, &usbg_config_driver,
  2049. usbg_cfg_bind);
  2050. if (ret)
  2051. return ret;
  2052. usb_composite_overwrite_options(cdev, &coverwrite);
  2053. return 0;
  2054. }
  2055. static __refdata struct usb_composite_driver usbg_driver = {
  2056. .name = "g_target",
  2057. .dev = &usbg_device_desc,
  2058. .strings = usbg_strings,
  2059. .max_speed = USB_SPEED_SUPER,
  2060. .bind = usb_target_bind,
  2061. .unbind = guas_unbind,
  2062. };
  2063. static int usbg_attach(struct usbg_tpg *tpg)
  2064. {
  2065. return usb_composite_probe(&usbg_driver);
  2066. }
  2067. static void usbg_detach(struct usbg_tpg *tpg)
  2068. {
  2069. usb_composite_unregister(&usbg_driver);
  2070. }
  2071. static int __init usb_target_gadget_init(void)
  2072. {
  2073. int ret;
  2074. ret = usbg_register_configfs();
  2075. return ret;
  2076. }
  2077. module_init(usb_target_gadget_init);
  2078. static void __exit usb_target_gadget_exit(void)
  2079. {
  2080. usbg_deregister_configfs();
  2081. }
  2082. module_exit(usb_target_gadget_exit);
  2083. MODULE_AUTHOR("Sebastian Andrzej Siewior <bigeasy@linutronix.de>");
  2084. MODULE_DESCRIPTION("usb-gadget fabric");
  2085. MODULE_LICENSE("GPL v2");