f_tcm.c 56 KB

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