tcm_usb_gadget.c 59 KB

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