tcm_usb_gadget.c 53 KB

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