tcm_usb_gadget.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480
  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. transport_generic_process_write(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. transport_generic_process_write(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 int usbg_new_cmd(struct se_cmd *se_cmd)
  1168. {
  1169. struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd,
  1170. se_cmd);
  1171. int ret;
  1172. ret = target_setup_cmd_from_cdb(se_cmd, cmd->cmd_buf);
  1173. if (ret)
  1174. return ret;
  1175. return transport_generic_map_mem_to_cmd(se_cmd, NULL, 0, NULL, 0);
  1176. }
  1177. static void usbg_cmd_release(struct kref *ref)
  1178. {
  1179. struct usbg_cmd *cmd = container_of(ref, struct usbg_cmd,
  1180. ref);
  1181. transport_generic_free_cmd(&cmd->se_cmd, 0);
  1182. }
  1183. static void usbg_release_cmd(struct se_cmd *se_cmd)
  1184. {
  1185. struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd,
  1186. se_cmd);
  1187. kfree(cmd->data_buf);
  1188. kfree(cmd);
  1189. return;
  1190. }
  1191. static int usbg_shutdown_session(struct se_session *se_sess)
  1192. {
  1193. return 0;
  1194. }
  1195. static void usbg_close_session(struct se_session *se_sess)
  1196. {
  1197. return;
  1198. }
  1199. static u32 usbg_sess_get_index(struct se_session *se_sess)
  1200. {
  1201. return 0;
  1202. }
  1203. /*
  1204. * XXX Error recovery: return != 0 if we expect writes. Dunno when that could be
  1205. */
  1206. static int usbg_write_pending_status(struct se_cmd *se_cmd)
  1207. {
  1208. return 0;
  1209. }
  1210. static void usbg_set_default_node_attrs(struct se_node_acl *nacl)
  1211. {
  1212. return;
  1213. }
  1214. static u32 usbg_get_task_tag(struct se_cmd *se_cmd)
  1215. {
  1216. struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd,
  1217. se_cmd);
  1218. struct f_uas *fu = cmd->fu;
  1219. if (fu->flags & USBG_IS_BOT)
  1220. return le32_to_cpu(cmd->bot_tag);
  1221. else
  1222. return cmd->tag;
  1223. }
  1224. static int usbg_get_cmd_state(struct se_cmd *se_cmd)
  1225. {
  1226. return 0;
  1227. }
  1228. static int usbg_queue_tm_rsp(struct se_cmd *se_cmd)
  1229. {
  1230. return 0;
  1231. }
  1232. static u16 usbg_set_fabric_sense_len(struct se_cmd *se_cmd, u32 sense_length)
  1233. {
  1234. return 0;
  1235. }
  1236. static u16 usbg_get_fabric_sense_len(void)
  1237. {
  1238. return 0;
  1239. }
  1240. static const char *usbg_check_wwn(const char *name)
  1241. {
  1242. const char *n;
  1243. unsigned int len;
  1244. n = strstr(name, "naa.");
  1245. if (!n)
  1246. return NULL;
  1247. n += 4;
  1248. len = strlen(n);
  1249. if (len == 0 || len > USBG_NAMELEN - 1)
  1250. return NULL;
  1251. return n;
  1252. }
  1253. static struct se_node_acl *usbg_make_nodeacl(
  1254. struct se_portal_group *se_tpg,
  1255. struct config_group *group,
  1256. const char *name)
  1257. {
  1258. struct se_node_acl *se_nacl, *se_nacl_new;
  1259. struct usbg_nacl *nacl;
  1260. u64 wwpn = 0;
  1261. u32 nexus_depth;
  1262. const char *wnn_name;
  1263. wnn_name = usbg_check_wwn(name);
  1264. if (!wnn_name)
  1265. return ERR_PTR(-EINVAL);
  1266. se_nacl_new = usbg_alloc_fabric_acl(se_tpg);
  1267. if (!(se_nacl_new))
  1268. return ERR_PTR(-ENOMEM);
  1269. nexus_depth = 1;
  1270. /*
  1271. * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
  1272. * when converting a NodeACL from demo mode -> explict
  1273. */
  1274. se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
  1275. name, nexus_depth);
  1276. if (IS_ERR(se_nacl)) {
  1277. usbg_release_fabric_acl(se_tpg, se_nacl_new);
  1278. return se_nacl;
  1279. }
  1280. /*
  1281. * Locate our struct usbg_nacl and set the FC Nport WWPN
  1282. */
  1283. nacl = container_of(se_nacl, struct usbg_nacl, se_node_acl);
  1284. nacl->iport_wwpn = wwpn;
  1285. snprintf(nacl->iport_name, sizeof(nacl->iport_name), "%s", name);
  1286. return se_nacl;
  1287. }
  1288. static void usbg_drop_nodeacl(struct se_node_acl *se_acl)
  1289. {
  1290. struct usbg_nacl *nacl = container_of(se_acl,
  1291. struct usbg_nacl, se_node_acl);
  1292. core_tpg_del_initiator_node_acl(se_acl->se_tpg, se_acl, 1);
  1293. kfree(nacl);
  1294. }
  1295. struct usbg_tpg *the_only_tpg_I_currently_have;
  1296. static struct se_portal_group *usbg_make_tpg(
  1297. struct se_wwn *wwn,
  1298. struct config_group *group,
  1299. const char *name)
  1300. {
  1301. struct usbg_tport *tport = container_of(wwn, struct usbg_tport,
  1302. tport_wwn);
  1303. struct usbg_tpg *tpg;
  1304. unsigned long tpgt;
  1305. int ret;
  1306. if (strstr(name, "tpgt_") != name)
  1307. return ERR_PTR(-EINVAL);
  1308. if (kstrtoul(name + 5, 0, &tpgt) || tpgt > UINT_MAX)
  1309. return ERR_PTR(-EINVAL);
  1310. if (the_only_tpg_I_currently_have) {
  1311. pr_err("Until the gadget framework can't handle multiple\n");
  1312. pr_err("gadgets, you can't do this here.\n");
  1313. return ERR_PTR(-EBUSY);
  1314. }
  1315. tpg = kzalloc(sizeof(struct usbg_tpg), GFP_KERNEL);
  1316. if (!tpg) {
  1317. printk(KERN_ERR "Unable to allocate struct usbg_tpg");
  1318. return ERR_PTR(-ENOMEM);
  1319. }
  1320. mutex_init(&tpg->tpg_mutex);
  1321. atomic_set(&tpg->tpg_port_count, 0);
  1322. tpg->workqueue = alloc_workqueue("tcm_usb_gadget", 0, 1);
  1323. if (!tpg->workqueue) {
  1324. kfree(tpg);
  1325. return NULL;
  1326. }
  1327. tpg->tport = tport;
  1328. tpg->tport_tpgt = tpgt;
  1329. ret = core_tpg_register(&usbg_fabric_configfs->tf_ops, wwn,
  1330. &tpg->se_tpg, tpg,
  1331. TRANSPORT_TPG_TYPE_NORMAL);
  1332. if (ret < 0) {
  1333. destroy_workqueue(tpg->workqueue);
  1334. kfree(tpg);
  1335. return NULL;
  1336. }
  1337. the_only_tpg_I_currently_have = tpg;
  1338. return &tpg->se_tpg;
  1339. }
  1340. static void usbg_drop_tpg(struct se_portal_group *se_tpg)
  1341. {
  1342. struct usbg_tpg *tpg = container_of(se_tpg,
  1343. struct usbg_tpg, se_tpg);
  1344. core_tpg_deregister(se_tpg);
  1345. destroy_workqueue(tpg->workqueue);
  1346. kfree(tpg);
  1347. the_only_tpg_I_currently_have = NULL;
  1348. }
  1349. static struct se_wwn *usbg_make_tport(
  1350. struct target_fabric_configfs *tf,
  1351. struct config_group *group,
  1352. const char *name)
  1353. {
  1354. struct usbg_tport *tport;
  1355. const char *wnn_name;
  1356. u64 wwpn = 0;
  1357. wnn_name = usbg_check_wwn(name);
  1358. if (!wnn_name)
  1359. return ERR_PTR(-EINVAL);
  1360. tport = kzalloc(sizeof(struct usbg_tport), GFP_KERNEL);
  1361. if (!(tport)) {
  1362. printk(KERN_ERR "Unable to allocate struct usbg_tport");
  1363. return ERR_PTR(-ENOMEM);
  1364. }
  1365. tport->tport_wwpn = wwpn;
  1366. snprintf(tport->tport_name, sizeof(tport->tport_name), wnn_name);
  1367. return &tport->tport_wwn;
  1368. }
  1369. static void usbg_drop_tport(struct se_wwn *wwn)
  1370. {
  1371. struct usbg_tport *tport = container_of(wwn,
  1372. struct usbg_tport, tport_wwn);
  1373. kfree(tport);
  1374. }
  1375. /*
  1376. * If somebody feels like dropping the version property, go ahead.
  1377. */
  1378. static ssize_t usbg_wwn_show_attr_version(
  1379. struct target_fabric_configfs *tf,
  1380. char *page)
  1381. {
  1382. return sprintf(page, "usb-gadget fabric module\n");
  1383. }
  1384. TF_WWN_ATTR_RO(usbg, version);
  1385. static struct configfs_attribute *usbg_wwn_attrs[] = {
  1386. &usbg_wwn_version.attr,
  1387. NULL,
  1388. };
  1389. static ssize_t tcm_usbg_tpg_show_enable(
  1390. struct se_portal_group *se_tpg,
  1391. char *page)
  1392. {
  1393. struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
  1394. return snprintf(page, PAGE_SIZE, "%u\n", tpg->gadget_connect);
  1395. }
  1396. static int usbg_attach(struct usbg_tpg *);
  1397. static void usbg_detach(struct usbg_tpg *);
  1398. static ssize_t tcm_usbg_tpg_store_enable(
  1399. struct se_portal_group *se_tpg,
  1400. const char *page,
  1401. size_t count)
  1402. {
  1403. struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
  1404. unsigned long op;
  1405. ssize_t ret;
  1406. ret = kstrtoul(page, 0, &op);
  1407. if (ret < 0)
  1408. return -EINVAL;
  1409. if (op > 1)
  1410. return -EINVAL;
  1411. if (op && tpg->gadget_connect)
  1412. goto out;
  1413. if (!op && !tpg->gadget_connect)
  1414. goto out;
  1415. if (op) {
  1416. ret = usbg_attach(tpg);
  1417. if (ret)
  1418. goto out;
  1419. } else {
  1420. usbg_detach(tpg);
  1421. }
  1422. tpg->gadget_connect = op;
  1423. out:
  1424. return count;
  1425. }
  1426. TF_TPG_BASE_ATTR(tcm_usbg, enable, S_IRUGO | S_IWUSR);
  1427. static ssize_t tcm_usbg_tpg_show_nexus(
  1428. struct se_portal_group *se_tpg,
  1429. char *page)
  1430. {
  1431. struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
  1432. struct tcm_usbg_nexus *tv_nexus;
  1433. ssize_t ret;
  1434. mutex_lock(&tpg->tpg_mutex);
  1435. tv_nexus = tpg->tpg_nexus;
  1436. if (!tv_nexus) {
  1437. ret = -ENODEV;
  1438. goto out;
  1439. }
  1440. ret = snprintf(page, PAGE_SIZE, "%s\n",
  1441. tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
  1442. out:
  1443. mutex_unlock(&tpg->tpg_mutex);
  1444. return ret;
  1445. }
  1446. static int tcm_usbg_make_nexus(struct usbg_tpg *tpg, char *name)
  1447. {
  1448. struct se_portal_group *se_tpg;
  1449. struct tcm_usbg_nexus *tv_nexus;
  1450. int ret;
  1451. mutex_lock(&tpg->tpg_mutex);
  1452. if (tpg->tpg_nexus) {
  1453. ret = -EEXIST;
  1454. pr_debug("tpg->tpg_nexus already exists\n");
  1455. goto err_unlock;
  1456. }
  1457. se_tpg = &tpg->se_tpg;
  1458. ret = -ENOMEM;
  1459. tv_nexus = kzalloc(sizeof(*tv_nexus), GFP_KERNEL);
  1460. if (!tv_nexus) {
  1461. pr_err("Unable to allocate struct tcm_vhost_nexus\n");
  1462. goto err_unlock;
  1463. }
  1464. tv_nexus->tvn_se_sess = transport_init_session();
  1465. if (IS_ERR(tv_nexus->tvn_se_sess))
  1466. goto err_free;
  1467. /*
  1468. * Since we are running in 'demo mode' this call with generate a
  1469. * struct se_node_acl for the tcm_vhost struct se_portal_group with
  1470. * the SCSI Initiator port name of the passed configfs group 'name'.
  1471. */
  1472. tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
  1473. se_tpg, name);
  1474. if (!tv_nexus->tvn_se_sess->se_node_acl) {
  1475. pr_debug("core_tpg_check_initiator_node_acl() failed"
  1476. " for %s\n", name);
  1477. goto err_session;
  1478. }
  1479. /*
  1480. * Now register the TCM vHost virtual I_T Nexus as active with the
  1481. * call to __transport_register_session()
  1482. */
  1483. __transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl,
  1484. tv_nexus->tvn_se_sess, tv_nexus);
  1485. tpg->tpg_nexus = tv_nexus;
  1486. mutex_unlock(&tpg->tpg_mutex);
  1487. return 0;
  1488. err_session:
  1489. transport_free_session(tv_nexus->tvn_se_sess);
  1490. err_free:
  1491. kfree(tv_nexus);
  1492. err_unlock:
  1493. mutex_unlock(&tpg->tpg_mutex);
  1494. return ret;
  1495. }
  1496. static int tcm_usbg_drop_nexus(struct usbg_tpg *tpg)
  1497. {
  1498. struct se_session *se_sess;
  1499. struct tcm_usbg_nexus *tv_nexus;
  1500. int ret = -ENODEV;
  1501. mutex_lock(&tpg->tpg_mutex);
  1502. tv_nexus = tpg->tpg_nexus;
  1503. if (!tv_nexus)
  1504. goto out;
  1505. se_sess = tv_nexus->tvn_se_sess;
  1506. if (!se_sess)
  1507. goto out;
  1508. if (atomic_read(&tpg->tpg_port_count)) {
  1509. ret = -EPERM;
  1510. pr_err("Unable to remove Host I_T Nexus with"
  1511. " active TPG port count: %d\n",
  1512. atomic_read(&tpg->tpg_port_count));
  1513. goto out;
  1514. }
  1515. pr_debug("Removing I_T Nexus to Initiator Port: %s\n",
  1516. tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
  1517. /*
  1518. * Release the SCSI I_T Nexus to the emulated vHost Target Port
  1519. */
  1520. transport_deregister_session(tv_nexus->tvn_se_sess);
  1521. tpg->tpg_nexus = NULL;
  1522. kfree(tv_nexus);
  1523. out:
  1524. mutex_unlock(&tpg->tpg_mutex);
  1525. return 0;
  1526. }
  1527. static ssize_t tcm_usbg_tpg_store_nexus(
  1528. struct se_portal_group *se_tpg,
  1529. const char *page,
  1530. size_t count)
  1531. {
  1532. struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
  1533. unsigned char i_port[USBG_NAMELEN], *ptr;
  1534. int ret;
  1535. if (!strncmp(page, "NULL", 4)) {
  1536. ret = tcm_usbg_drop_nexus(tpg);
  1537. return (!ret) ? count : ret;
  1538. }
  1539. if (strlen(page) > USBG_NAMELEN) {
  1540. pr_err("Emulated NAA Sas Address: %s, exceeds"
  1541. " max: %d\n", page, USBG_NAMELEN);
  1542. return -EINVAL;
  1543. }
  1544. snprintf(i_port, USBG_NAMELEN, "%s", page);
  1545. ptr = strstr(i_port, "naa.");
  1546. if (!ptr) {
  1547. pr_err("Missing 'naa.' prefix\n");
  1548. return -EINVAL;
  1549. }
  1550. if (i_port[strlen(i_port) - 1] == '\n')
  1551. i_port[strlen(i_port) - 1] = '\0';
  1552. ret = tcm_usbg_make_nexus(tpg, &i_port[4]);
  1553. if (ret < 0)
  1554. return ret;
  1555. return count;
  1556. }
  1557. TF_TPG_BASE_ATTR(tcm_usbg, nexus, S_IRUGO | S_IWUSR);
  1558. static struct configfs_attribute *usbg_base_attrs[] = {
  1559. &tcm_usbg_tpg_enable.attr,
  1560. &tcm_usbg_tpg_nexus.attr,
  1561. NULL,
  1562. };
  1563. static int usbg_port_link(struct se_portal_group *se_tpg, struct se_lun *lun)
  1564. {
  1565. struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
  1566. atomic_inc(&tpg->tpg_port_count);
  1567. smp_mb__after_atomic_inc();
  1568. return 0;
  1569. }
  1570. static void usbg_port_unlink(struct se_portal_group *se_tpg,
  1571. struct se_lun *se_lun)
  1572. {
  1573. struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
  1574. atomic_dec(&tpg->tpg_port_count);
  1575. smp_mb__after_atomic_dec();
  1576. }
  1577. static int usbg_check_stop_free(struct se_cmd *se_cmd)
  1578. {
  1579. struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd,
  1580. se_cmd);
  1581. kref_put(&cmd->ref, usbg_cmd_release);
  1582. return 1;
  1583. }
  1584. static struct target_core_fabric_ops usbg_ops = {
  1585. .get_fabric_name = usbg_get_fabric_name,
  1586. .get_fabric_proto_ident = usbg_get_fabric_proto_ident,
  1587. .tpg_get_wwn = usbg_get_fabric_wwn,
  1588. .tpg_get_tag = usbg_get_tag,
  1589. .tpg_get_default_depth = usbg_get_default_depth,
  1590. .tpg_get_pr_transport_id = usbg_get_pr_transport_id,
  1591. .tpg_get_pr_transport_id_len = usbg_get_pr_transport_id_len,
  1592. .tpg_parse_pr_out_transport_id = usbg_parse_pr_out_transport_id,
  1593. .tpg_check_demo_mode = usbg_check_true,
  1594. .tpg_check_demo_mode_cache = usbg_check_false,
  1595. .tpg_check_demo_mode_write_protect = usbg_check_false,
  1596. .tpg_check_prod_mode_write_protect = usbg_check_false,
  1597. .tpg_alloc_fabric_acl = usbg_alloc_fabric_acl,
  1598. .tpg_release_fabric_acl = usbg_release_fabric_acl,
  1599. .tpg_get_inst_index = usbg_tpg_get_inst_index,
  1600. .new_cmd_map = usbg_new_cmd,
  1601. .release_cmd = usbg_release_cmd,
  1602. .shutdown_session = usbg_shutdown_session,
  1603. .close_session = usbg_close_session,
  1604. .sess_get_index = usbg_sess_get_index,
  1605. .sess_get_initiator_sid = NULL,
  1606. .write_pending = usbg_send_write_request,
  1607. .write_pending_status = usbg_write_pending_status,
  1608. .set_default_node_attributes = usbg_set_default_node_attrs,
  1609. .get_task_tag = usbg_get_task_tag,
  1610. .get_cmd_state = usbg_get_cmd_state,
  1611. .queue_data_in = usbg_send_read_response,
  1612. .queue_status = usbg_send_status_response,
  1613. .queue_tm_rsp = usbg_queue_tm_rsp,
  1614. .get_fabric_sense_len = usbg_get_fabric_sense_len,
  1615. .set_fabric_sense_len = usbg_set_fabric_sense_len,
  1616. .check_stop_free = usbg_check_stop_free,
  1617. .fabric_make_wwn = usbg_make_tport,
  1618. .fabric_drop_wwn = usbg_drop_tport,
  1619. .fabric_make_tpg = usbg_make_tpg,
  1620. .fabric_drop_tpg = usbg_drop_tpg,
  1621. .fabric_post_link = usbg_port_link,
  1622. .fabric_pre_unlink = usbg_port_unlink,
  1623. .fabric_make_np = NULL,
  1624. .fabric_drop_np = NULL,
  1625. .fabric_make_nodeacl = usbg_make_nodeacl,
  1626. .fabric_drop_nodeacl = usbg_drop_nodeacl,
  1627. };
  1628. static int usbg_register_configfs(void)
  1629. {
  1630. struct target_fabric_configfs *fabric;
  1631. int ret;
  1632. fabric = target_fabric_configfs_init(THIS_MODULE, "usb_gadget");
  1633. if (IS_ERR(fabric)) {
  1634. printk(KERN_ERR "target_fabric_configfs_init() failed\n");
  1635. return PTR_ERR(fabric);
  1636. }
  1637. fabric->tf_ops = usbg_ops;
  1638. TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = usbg_wwn_attrs;
  1639. TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = usbg_base_attrs;
  1640. TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL;
  1641. TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
  1642. TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
  1643. TF_CIT_TMPL(fabric)->tfc_tpg_nacl_base_cit.ct_attrs = NULL;
  1644. TF_CIT_TMPL(fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
  1645. TF_CIT_TMPL(fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
  1646. TF_CIT_TMPL(fabric)->tfc_tpg_nacl_param_cit.ct_attrs = NULL;
  1647. ret = target_fabric_configfs_register(fabric);
  1648. if (ret < 0) {
  1649. printk(KERN_ERR "target_fabric_configfs_register() failed"
  1650. " for usb-gadget\n");
  1651. return ret;
  1652. }
  1653. usbg_fabric_configfs = fabric;
  1654. return 0;
  1655. };
  1656. static void usbg_deregister_configfs(void)
  1657. {
  1658. if (!(usbg_fabric_configfs))
  1659. return;
  1660. target_fabric_configfs_deregister(usbg_fabric_configfs);
  1661. usbg_fabric_configfs = NULL;
  1662. };
  1663. /* Start gadget.c code */
  1664. static struct usb_interface_descriptor bot_intf_desc = {
  1665. .bLength = sizeof(bot_intf_desc),
  1666. .bDescriptorType = USB_DT_INTERFACE,
  1667. .bAlternateSetting = 0,
  1668. .bNumEndpoints = 2,
  1669. .bAlternateSetting = USB_G_ALT_INT_BBB,
  1670. .bInterfaceClass = USB_CLASS_MASS_STORAGE,
  1671. .bInterfaceSubClass = USB_SC_SCSI,
  1672. .bInterfaceProtocol = USB_PR_BULK,
  1673. .iInterface = USB_G_STR_INT_UAS,
  1674. };
  1675. static struct usb_interface_descriptor uasp_intf_desc = {
  1676. .bLength = sizeof(uasp_intf_desc),
  1677. .bDescriptorType = USB_DT_INTERFACE,
  1678. .bNumEndpoints = 4,
  1679. .bAlternateSetting = USB_G_ALT_INT_UAS,
  1680. .bInterfaceClass = USB_CLASS_MASS_STORAGE,
  1681. .bInterfaceSubClass = USB_SC_SCSI,
  1682. .bInterfaceProtocol = USB_PR_UAS,
  1683. .iInterface = USB_G_STR_INT_BBB,
  1684. };
  1685. static struct usb_endpoint_descriptor uasp_bi_desc = {
  1686. .bLength = USB_DT_ENDPOINT_SIZE,
  1687. .bDescriptorType = USB_DT_ENDPOINT,
  1688. .bEndpointAddress = USB_DIR_IN,
  1689. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1690. .wMaxPacketSize = cpu_to_le16(512),
  1691. };
  1692. static struct usb_endpoint_descriptor uasp_fs_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. };
  1698. static struct usb_pipe_usage_descriptor uasp_bi_pipe_desc = {
  1699. .bLength = sizeof(uasp_bi_pipe_desc),
  1700. .bDescriptorType = USB_DT_PIPE_USAGE,
  1701. .bPipeID = DATA_IN_PIPE_ID,
  1702. };
  1703. static struct usb_endpoint_descriptor uasp_ss_bi_desc = {
  1704. .bLength = USB_DT_ENDPOINT_SIZE,
  1705. .bDescriptorType = USB_DT_ENDPOINT,
  1706. .bEndpointAddress = USB_DIR_IN,
  1707. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1708. .wMaxPacketSize = cpu_to_le16(1024),
  1709. };
  1710. static struct usb_ss_ep_comp_descriptor uasp_bi_ep_comp_desc = {
  1711. .bLength = sizeof(uasp_bi_ep_comp_desc),
  1712. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  1713. .bMaxBurst = 0,
  1714. .bmAttributes = UASP_SS_EP_COMP_LOG_STREAMS,
  1715. .wBytesPerInterval = 0,
  1716. };
  1717. static struct usb_ss_ep_comp_descriptor bot_bi_ep_comp_desc = {
  1718. .bLength = sizeof(bot_bi_ep_comp_desc),
  1719. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  1720. .bMaxBurst = 0,
  1721. };
  1722. static struct usb_endpoint_descriptor uasp_bo_desc = {
  1723. .bLength = USB_DT_ENDPOINT_SIZE,
  1724. .bDescriptorType = USB_DT_ENDPOINT,
  1725. .bEndpointAddress = USB_DIR_OUT,
  1726. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1727. .wMaxPacketSize = cpu_to_le16(512),
  1728. };
  1729. static struct usb_endpoint_descriptor uasp_fs_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. };
  1735. static struct usb_pipe_usage_descriptor uasp_bo_pipe_desc = {
  1736. .bLength = sizeof(uasp_bo_pipe_desc),
  1737. .bDescriptorType = USB_DT_PIPE_USAGE,
  1738. .bPipeID = DATA_OUT_PIPE_ID,
  1739. };
  1740. static struct usb_endpoint_descriptor uasp_ss_bo_desc = {
  1741. .bLength = USB_DT_ENDPOINT_SIZE,
  1742. .bDescriptorType = USB_DT_ENDPOINT,
  1743. .bEndpointAddress = USB_DIR_OUT,
  1744. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1745. .wMaxPacketSize = cpu_to_le16(0x400),
  1746. };
  1747. static struct usb_ss_ep_comp_descriptor uasp_bo_ep_comp_desc = {
  1748. .bLength = sizeof(uasp_bo_ep_comp_desc),
  1749. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  1750. .bmAttributes = UASP_SS_EP_COMP_LOG_STREAMS,
  1751. };
  1752. static struct usb_ss_ep_comp_descriptor bot_bo_ep_comp_desc = {
  1753. .bLength = sizeof(bot_bo_ep_comp_desc),
  1754. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  1755. };
  1756. static struct usb_endpoint_descriptor uasp_status_desc = {
  1757. .bLength = USB_DT_ENDPOINT_SIZE,
  1758. .bDescriptorType = USB_DT_ENDPOINT,
  1759. .bEndpointAddress = USB_DIR_IN,
  1760. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1761. .wMaxPacketSize = cpu_to_le16(512),
  1762. };
  1763. static struct usb_endpoint_descriptor uasp_fs_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. };
  1769. static struct usb_pipe_usage_descriptor uasp_status_pipe_desc = {
  1770. .bLength = sizeof(uasp_status_pipe_desc),
  1771. .bDescriptorType = USB_DT_PIPE_USAGE,
  1772. .bPipeID = STATUS_PIPE_ID,
  1773. };
  1774. static struct usb_endpoint_descriptor uasp_ss_status_desc = {
  1775. .bLength = USB_DT_ENDPOINT_SIZE,
  1776. .bDescriptorType = USB_DT_ENDPOINT,
  1777. .bEndpointAddress = USB_DIR_IN,
  1778. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1779. .wMaxPacketSize = cpu_to_le16(1024),
  1780. };
  1781. static struct usb_ss_ep_comp_descriptor uasp_status_in_ep_comp_desc = {
  1782. .bLength = sizeof(uasp_status_in_ep_comp_desc),
  1783. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  1784. .bmAttributes = UASP_SS_EP_COMP_LOG_STREAMS,
  1785. };
  1786. static struct usb_endpoint_descriptor uasp_cmd_desc = {
  1787. .bLength = USB_DT_ENDPOINT_SIZE,
  1788. .bDescriptorType = USB_DT_ENDPOINT,
  1789. .bEndpointAddress = USB_DIR_OUT,
  1790. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1791. .wMaxPacketSize = cpu_to_le16(512),
  1792. };
  1793. static struct usb_endpoint_descriptor uasp_fs_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. };
  1799. static struct usb_pipe_usage_descriptor uasp_cmd_pipe_desc = {
  1800. .bLength = sizeof(uasp_cmd_pipe_desc),
  1801. .bDescriptorType = USB_DT_PIPE_USAGE,
  1802. .bPipeID = CMD_PIPE_ID,
  1803. };
  1804. static struct usb_endpoint_descriptor uasp_ss_cmd_desc = {
  1805. .bLength = USB_DT_ENDPOINT_SIZE,
  1806. .bDescriptorType = USB_DT_ENDPOINT,
  1807. .bEndpointAddress = USB_DIR_OUT,
  1808. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1809. .wMaxPacketSize = cpu_to_le16(1024),
  1810. };
  1811. static struct usb_ss_ep_comp_descriptor uasp_cmd_comp_desc = {
  1812. .bLength = sizeof(uasp_cmd_comp_desc),
  1813. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  1814. };
  1815. static struct usb_descriptor_header *uasp_fs_function_desc[] = {
  1816. (struct usb_descriptor_header *) &bot_intf_desc,
  1817. (struct usb_descriptor_header *) &uasp_fs_bi_desc,
  1818. (struct usb_descriptor_header *) &uasp_fs_bo_desc,
  1819. (struct usb_descriptor_header *) &uasp_intf_desc,
  1820. (struct usb_descriptor_header *) &uasp_fs_bi_desc,
  1821. (struct usb_descriptor_header *) &uasp_bi_pipe_desc,
  1822. (struct usb_descriptor_header *) &uasp_fs_bo_desc,
  1823. (struct usb_descriptor_header *) &uasp_bo_pipe_desc,
  1824. (struct usb_descriptor_header *) &uasp_fs_status_desc,
  1825. (struct usb_descriptor_header *) &uasp_status_pipe_desc,
  1826. (struct usb_descriptor_header *) &uasp_fs_cmd_desc,
  1827. (struct usb_descriptor_header *) &uasp_cmd_pipe_desc,
  1828. };
  1829. static struct usb_descriptor_header *uasp_hs_function_desc[] = {
  1830. (struct usb_descriptor_header *) &bot_intf_desc,
  1831. (struct usb_descriptor_header *) &uasp_bi_desc,
  1832. (struct usb_descriptor_header *) &uasp_bo_desc,
  1833. (struct usb_descriptor_header *) &uasp_intf_desc,
  1834. (struct usb_descriptor_header *) &uasp_bi_desc,
  1835. (struct usb_descriptor_header *) &uasp_bi_pipe_desc,
  1836. (struct usb_descriptor_header *) &uasp_bo_desc,
  1837. (struct usb_descriptor_header *) &uasp_bo_pipe_desc,
  1838. (struct usb_descriptor_header *) &uasp_status_desc,
  1839. (struct usb_descriptor_header *) &uasp_status_pipe_desc,
  1840. (struct usb_descriptor_header *) &uasp_cmd_desc,
  1841. (struct usb_descriptor_header *) &uasp_cmd_pipe_desc,
  1842. NULL,
  1843. };
  1844. static struct usb_descriptor_header *uasp_ss_function_desc[] = {
  1845. (struct usb_descriptor_header *) &bot_intf_desc,
  1846. (struct usb_descriptor_header *) &uasp_ss_bi_desc,
  1847. (struct usb_descriptor_header *) &bot_bi_ep_comp_desc,
  1848. (struct usb_descriptor_header *) &uasp_ss_bo_desc,
  1849. (struct usb_descriptor_header *) &bot_bo_ep_comp_desc,
  1850. (struct usb_descriptor_header *) &uasp_intf_desc,
  1851. (struct usb_descriptor_header *) &uasp_ss_bi_desc,
  1852. (struct usb_descriptor_header *) &uasp_bi_ep_comp_desc,
  1853. (struct usb_descriptor_header *) &uasp_bi_pipe_desc,
  1854. (struct usb_descriptor_header *) &uasp_ss_bo_desc,
  1855. (struct usb_descriptor_header *) &uasp_bo_ep_comp_desc,
  1856. (struct usb_descriptor_header *) &uasp_bo_pipe_desc,
  1857. (struct usb_descriptor_header *) &uasp_ss_status_desc,
  1858. (struct usb_descriptor_header *) &uasp_status_in_ep_comp_desc,
  1859. (struct usb_descriptor_header *) &uasp_status_pipe_desc,
  1860. (struct usb_descriptor_header *) &uasp_ss_cmd_desc,
  1861. (struct usb_descriptor_header *) &uasp_cmd_comp_desc,
  1862. (struct usb_descriptor_header *) &uasp_cmd_pipe_desc,
  1863. NULL,
  1864. };
  1865. #define UAS_VENDOR_ID 0x0525 /* NetChip */
  1866. #define UAS_PRODUCT_ID 0xa4a5 /* Linux-USB File-backed Storage Gadget */
  1867. static struct usb_device_descriptor usbg_device_desc = {
  1868. .bLength = sizeof(usbg_device_desc),
  1869. .bDescriptorType = USB_DT_DEVICE,
  1870. .bcdUSB = cpu_to_le16(0x0200),
  1871. .bDeviceClass = USB_CLASS_PER_INTERFACE,
  1872. .idVendor = cpu_to_le16(UAS_VENDOR_ID),
  1873. .idProduct = cpu_to_le16(UAS_PRODUCT_ID),
  1874. .iManufacturer = USB_G_STR_MANUFACTOR,
  1875. .iProduct = USB_G_STR_PRODUCT,
  1876. .iSerialNumber = USB_G_STR_SERIAL,
  1877. .bNumConfigurations = 1,
  1878. };
  1879. static struct usb_string usbg_us_strings[] = {
  1880. { USB_G_STR_MANUFACTOR, "Target Manufactor"},
  1881. { USB_G_STR_PRODUCT, "Target Product"},
  1882. { USB_G_STR_SERIAL, "000000000001"},
  1883. { USB_G_STR_CONFIG, "default config"},
  1884. { USB_G_STR_INT_UAS, "USB Attached SCSI"},
  1885. { USB_G_STR_INT_BBB, "Bulk Only Transport"},
  1886. { },
  1887. };
  1888. static struct usb_gadget_strings usbg_stringtab = {
  1889. .language = 0x0409,
  1890. .strings = usbg_us_strings,
  1891. };
  1892. static struct usb_gadget_strings *usbg_strings[] = {
  1893. &usbg_stringtab,
  1894. NULL,
  1895. };
  1896. static int guas_unbind(struct usb_composite_dev *cdev)
  1897. {
  1898. return 0;
  1899. }
  1900. static struct usb_configuration usbg_config_driver = {
  1901. .label = "Linux Target",
  1902. .bConfigurationValue = 1,
  1903. .iConfiguration = USB_G_STR_CONFIG,
  1904. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  1905. };
  1906. static void give_back_ep(struct usb_ep **pep)
  1907. {
  1908. struct usb_ep *ep = *pep;
  1909. if (!ep)
  1910. return;
  1911. ep->driver_data = NULL;
  1912. }
  1913. static int usbg_bind(struct usb_configuration *c, struct usb_function *f)
  1914. {
  1915. struct f_uas *fu = to_f_uas(f);
  1916. struct usb_gadget *gadget = c->cdev->gadget;
  1917. struct usb_ep *ep;
  1918. int iface;
  1919. iface = usb_interface_id(c, f);
  1920. if (iface < 0)
  1921. return iface;
  1922. bot_intf_desc.bInterfaceNumber = iface;
  1923. uasp_intf_desc.bInterfaceNumber = iface;
  1924. fu->iface = iface;
  1925. ep = usb_ep_autoconfig_ss(gadget, &uasp_ss_bi_desc,
  1926. &uasp_bi_ep_comp_desc);
  1927. if (!ep)
  1928. goto ep_fail;
  1929. ep->driver_data = fu;
  1930. fu->ep_in = ep;
  1931. ep = usb_ep_autoconfig_ss(gadget, &uasp_ss_bo_desc,
  1932. &uasp_bo_ep_comp_desc);
  1933. if (!ep)
  1934. goto ep_fail;
  1935. ep->driver_data = fu;
  1936. fu->ep_out = ep;
  1937. ep = usb_ep_autoconfig_ss(gadget, &uasp_ss_status_desc,
  1938. &uasp_status_in_ep_comp_desc);
  1939. if (!ep)
  1940. goto ep_fail;
  1941. ep->driver_data = fu;
  1942. fu->ep_status = ep;
  1943. ep = usb_ep_autoconfig_ss(gadget, &uasp_ss_cmd_desc,
  1944. &uasp_cmd_comp_desc);
  1945. if (!ep)
  1946. goto ep_fail;
  1947. ep->driver_data = fu;
  1948. fu->ep_cmd = ep;
  1949. /* Assume endpoint addresses are the same for both speeds */
  1950. uasp_bi_desc.bEndpointAddress = uasp_ss_bi_desc.bEndpointAddress;
  1951. uasp_bo_desc.bEndpointAddress = uasp_ss_bo_desc.bEndpointAddress;
  1952. uasp_status_desc.bEndpointAddress =
  1953. uasp_ss_status_desc.bEndpointAddress;
  1954. uasp_cmd_desc.bEndpointAddress = uasp_ss_cmd_desc.bEndpointAddress;
  1955. uasp_fs_bi_desc.bEndpointAddress = uasp_ss_bi_desc.bEndpointAddress;
  1956. uasp_fs_bo_desc.bEndpointAddress = uasp_ss_bo_desc.bEndpointAddress;
  1957. uasp_fs_status_desc.bEndpointAddress =
  1958. uasp_ss_status_desc.bEndpointAddress;
  1959. uasp_fs_cmd_desc.bEndpointAddress = uasp_ss_cmd_desc.bEndpointAddress;
  1960. return 0;
  1961. ep_fail:
  1962. pr_err("Can't claim all required eps\n");
  1963. give_back_ep(&fu->ep_in);
  1964. give_back_ep(&fu->ep_out);
  1965. give_back_ep(&fu->ep_status);
  1966. give_back_ep(&fu->ep_cmd);
  1967. return -ENOTSUPP;
  1968. }
  1969. static void usbg_unbind(struct usb_configuration *c, struct usb_function *f)
  1970. {
  1971. struct f_uas *fu = to_f_uas(f);
  1972. kfree(fu);
  1973. }
  1974. struct guas_setup_wq {
  1975. struct work_struct work;
  1976. struct f_uas *fu;
  1977. unsigned int alt;
  1978. };
  1979. static void usbg_delayed_set_alt(struct work_struct *wq)
  1980. {
  1981. struct guas_setup_wq *work = container_of(wq, struct guas_setup_wq,
  1982. work);
  1983. struct f_uas *fu = work->fu;
  1984. int alt = work->alt;
  1985. kfree(work);
  1986. if (fu->flags & USBG_IS_BOT)
  1987. bot_cleanup_old_alt(fu);
  1988. if (fu->flags & USBG_IS_UAS)
  1989. uasp_cleanup_old_alt(fu);
  1990. if (alt == USB_G_ALT_INT_BBB)
  1991. bot_set_alt(fu);
  1992. else if (alt == USB_G_ALT_INT_UAS)
  1993. uasp_set_alt(fu);
  1994. usb_composite_setup_continue(fu->function.config->cdev);
  1995. }
  1996. static int usbg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  1997. {
  1998. struct f_uas *fu = to_f_uas(f);
  1999. if ((alt == USB_G_ALT_INT_BBB) || (alt == USB_G_ALT_INT_UAS)) {
  2000. struct guas_setup_wq *work;
  2001. work = kmalloc(sizeof(*work), GFP_ATOMIC);
  2002. if (!work)
  2003. return -ENOMEM;
  2004. INIT_WORK(&work->work, usbg_delayed_set_alt);
  2005. work->fu = fu;
  2006. work->alt = alt;
  2007. schedule_work(&work->work);
  2008. return USB_GADGET_DELAYED_STATUS;
  2009. }
  2010. return -EOPNOTSUPP;
  2011. }
  2012. static void usbg_disable(struct usb_function *f)
  2013. {
  2014. struct f_uas *fu = to_f_uas(f);
  2015. if (fu->flags & USBG_IS_UAS)
  2016. uasp_cleanup_old_alt(fu);
  2017. else if (fu->flags & USBG_IS_BOT)
  2018. bot_cleanup_old_alt(fu);
  2019. fu->flags = 0;
  2020. }
  2021. static int usbg_setup(struct usb_function *f,
  2022. const struct usb_ctrlrequest *ctrl)
  2023. {
  2024. struct f_uas *fu = to_f_uas(f);
  2025. if (!(fu->flags & USBG_IS_BOT))
  2026. return -EOPNOTSUPP;
  2027. return usbg_bot_setup(f, ctrl);
  2028. }
  2029. static int usbg_cfg_bind(struct usb_configuration *c)
  2030. {
  2031. struct f_uas *fu;
  2032. int ret;
  2033. fu = kzalloc(sizeof(*fu), GFP_KERNEL);
  2034. if (!fu)
  2035. return -ENOMEM;
  2036. fu->function.name = "Target Function";
  2037. fu->function.descriptors = uasp_fs_function_desc;
  2038. fu->function.hs_descriptors = uasp_hs_function_desc;
  2039. fu->function.ss_descriptors = uasp_ss_function_desc;
  2040. fu->function.bind = usbg_bind;
  2041. fu->function.unbind = usbg_unbind;
  2042. fu->function.set_alt = usbg_set_alt;
  2043. fu->function.setup = usbg_setup;
  2044. fu->function.disable = usbg_disable;
  2045. fu->tpg = the_only_tpg_I_currently_have;
  2046. ret = usb_add_function(c, &fu->function);
  2047. if (ret)
  2048. goto err;
  2049. return 0;
  2050. err:
  2051. kfree(fu);
  2052. return ret;
  2053. }
  2054. static int usb_target_bind(struct usb_composite_dev *cdev)
  2055. {
  2056. int ret;
  2057. ret = usb_add_config(cdev, &usbg_config_driver,
  2058. usbg_cfg_bind);
  2059. return 0;
  2060. }
  2061. static struct usb_composite_driver usbg_driver = {
  2062. .name = "g_target",
  2063. .dev = &usbg_device_desc,
  2064. .strings = usbg_strings,
  2065. .max_speed = USB_SPEED_SUPER,
  2066. .unbind = guas_unbind,
  2067. };
  2068. static int usbg_attach(struct usbg_tpg *tpg)
  2069. {
  2070. return usb_composite_probe(&usbg_driver, usb_target_bind);
  2071. }
  2072. static void usbg_detach(struct usbg_tpg *tpg)
  2073. {
  2074. usb_composite_unregister(&usbg_driver);
  2075. }
  2076. static int __init usb_target_gadget_init(void)
  2077. {
  2078. int ret;
  2079. ret = usbg_register_configfs();
  2080. return ret;
  2081. }
  2082. module_init(usb_target_gadget_init);
  2083. static void __exit usb_target_gadget_exit(void)
  2084. {
  2085. usbg_deregister_configfs();
  2086. }
  2087. module_exit(usb_target_gadget_exit);
  2088. MODULE_AUTHOR("Sebastian Andrzej Siewior <bigeasy@linutronix.de>");
  2089. MODULE_DESCRIPTION("usb-gadget fabric");
  2090. MODULE_LICENSE("GPL v2");