tcm_usb_gadget.c 59 KB

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