tcm_usb_gadget.c 59 KB

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