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;
  895. tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
  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.tag = cmd->tag = scsi_tag;
  902. cmd->fu = fu;
  903. return cmd;
  904. }
  905. static void usbg_release_cmd(struct se_cmd *);
  906. static int usbg_submit_command(struct f_uas *fu,
  907. void *cmdbuf, unsigned int len)
  908. {
  909. struct command_iu *cmd_iu = cmdbuf;
  910. struct usbg_cmd *cmd;
  911. struct usbg_tpg *tpg = fu->tpg;
  912. struct tcm_usbg_nexus *tv_nexus;
  913. u32 cmd_len;
  914. u16 scsi_tag;
  915. if (cmd_iu->iu_id != IU_ID_COMMAND) {
  916. pr_err("Unsupported type %d\n", cmd_iu->iu_id);
  917. return -EINVAL;
  918. }
  919. tv_nexus = tpg->tpg_nexus;
  920. if (!tv_nexus) {
  921. pr_err("Missing nexus, ignoring command\n");
  922. return -EINVAL;
  923. }
  924. cmd_len = (cmd_iu->len & ~0x3) + 16;
  925. if (cmd_len > USBG_MAX_CMD)
  926. return -EINVAL;
  927. scsi_tag = be16_to_cpup(&cmd_iu->tag);
  928. cmd = usbg_get_cmd(fu, tv_nexus, scsi_tag);
  929. if (IS_ERR(cmd)) {
  930. pr_err("usbg_get_cmd failed\n");
  931. return -ENOMEM;
  932. }
  933. memcpy(cmd->cmd_buf, cmd_iu->cdb, cmd_len);
  934. if (fu->flags & USBG_USE_STREAMS) {
  935. if (cmd->tag > UASP_SS_EP_COMP_NUM_STREAMS)
  936. goto err;
  937. if (!cmd->tag)
  938. cmd->stream = &fu->stream[0];
  939. else
  940. cmd->stream = &fu->stream[cmd->tag - 1];
  941. } else {
  942. cmd->stream = &fu->stream[0];
  943. }
  944. switch (cmd_iu->prio_attr & 0x7) {
  945. case UAS_HEAD_TAG:
  946. cmd->prio_attr = TCM_HEAD_TAG;
  947. break;
  948. case UAS_ORDERED_TAG:
  949. cmd->prio_attr = TCM_ORDERED_TAG;
  950. break;
  951. case UAS_ACA:
  952. cmd->prio_attr = TCM_ACA_TAG;
  953. break;
  954. default:
  955. pr_debug_once("Unsupported prio_attr: %02x.\n",
  956. cmd_iu->prio_attr);
  957. /* fall through */
  958. case UAS_SIMPLE_TAG:
  959. cmd->prio_attr = TCM_SIMPLE_TAG;
  960. break;
  961. }
  962. cmd->unpacked_lun = scsilun_to_int(&cmd_iu->lun);
  963. INIT_WORK(&cmd->work, usbg_cmd_work);
  964. queue_work(tpg->workqueue, &cmd->work);
  965. return 0;
  966. err:
  967. usbg_release_cmd(&cmd->se_cmd);
  968. return -EINVAL;
  969. }
  970. static void bot_cmd_work(struct work_struct *work)
  971. {
  972. struct usbg_cmd *cmd = container_of(work, struct usbg_cmd, work);
  973. struct se_cmd *se_cmd;
  974. struct tcm_usbg_nexus *tv_nexus;
  975. struct usbg_tpg *tpg;
  976. int dir;
  977. se_cmd = &cmd->se_cmd;
  978. tpg = cmd->fu->tpg;
  979. tv_nexus = tpg->tpg_nexus;
  980. dir = get_cmd_dir(cmd->cmd_buf);
  981. if (dir < 0) {
  982. transport_init_se_cmd(se_cmd,
  983. tv_nexus->tvn_se_sess->se_tpg->se_tpg_tfo,
  984. tv_nexus->tvn_se_sess, cmd->data_len, DMA_NONE,
  985. cmd->prio_attr, cmd->sense_iu.sense);
  986. goto out;
  987. }
  988. if (target_submit_cmd(se_cmd, tv_nexus->tvn_se_sess,
  989. cmd->cmd_buf, cmd->sense_iu.sense, cmd->unpacked_lun,
  990. cmd->data_len, cmd->prio_attr, dir, 0) < 0)
  991. goto out;
  992. return;
  993. out:
  994. transport_send_check_condition_and_sense(se_cmd,
  995. TCM_UNSUPPORTED_SCSI_OPCODE, 1);
  996. transport_generic_free_cmd(&cmd->se_cmd, 0);
  997. }
  998. static int bot_submit_command(struct f_uas *fu,
  999. void *cmdbuf, unsigned int len)
  1000. {
  1001. struct bulk_cb_wrap *cbw = cmdbuf;
  1002. struct usbg_cmd *cmd;
  1003. struct usbg_tpg *tpg = fu->tpg;
  1004. struct tcm_usbg_nexus *tv_nexus;
  1005. u32 cmd_len;
  1006. if (cbw->Signature != cpu_to_le32(US_BULK_CB_SIGN)) {
  1007. pr_err("Wrong signature on CBW\n");
  1008. return -EINVAL;
  1009. }
  1010. if (len != 31) {
  1011. pr_err("Wrong length for CBW\n");
  1012. return -EINVAL;
  1013. }
  1014. cmd_len = cbw->Length;
  1015. if (cmd_len < 1 || cmd_len > 16)
  1016. return -EINVAL;
  1017. tv_nexus = tpg->tpg_nexus;
  1018. if (!tv_nexus) {
  1019. pr_err("Missing nexus, ignoring command\n");
  1020. return -ENODEV;
  1021. }
  1022. cmd = usbg_get_cmd(fu, tv_nexus, cbw->Tag);
  1023. if (IS_ERR(cmd)) {
  1024. pr_err("usbg_get_cmd failed\n");
  1025. return -ENOMEM;
  1026. }
  1027. memcpy(cmd->cmd_buf, cbw->CDB, cmd_len);
  1028. cmd->bot_tag = cbw->Tag;
  1029. cmd->prio_attr = TCM_SIMPLE_TAG;
  1030. cmd->unpacked_lun = cbw->Lun;
  1031. cmd->is_read = cbw->Flags & US_BULK_FLAG_IN ? 1 : 0;
  1032. cmd->data_len = le32_to_cpu(cbw->DataTransferLength);
  1033. cmd->se_cmd.tag = le32_to_cpu(cmd->bot_tag);
  1034. INIT_WORK(&cmd->work, bot_cmd_work);
  1035. queue_work(tpg->workqueue, &cmd->work);
  1036. return 0;
  1037. }
  1038. /* Start fabric.c code */
  1039. static int usbg_check_true(struct se_portal_group *se_tpg)
  1040. {
  1041. return 1;
  1042. }
  1043. static int usbg_check_false(struct se_portal_group *se_tpg)
  1044. {
  1045. return 0;
  1046. }
  1047. static char *usbg_get_fabric_name(void)
  1048. {
  1049. return "usb_gadget";
  1050. }
  1051. static char *usbg_get_fabric_wwn(struct se_portal_group *se_tpg)
  1052. {
  1053. struct usbg_tpg *tpg = container_of(se_tpg,
  1054. struct usbg_tpg, se_tpg);
  1055. struct usbg_tport *tport = tpg->tport;
  1056. return &tport->tport_name[0];
  1057. }
  1058. static u16 usbg_get_tag(struct se_portal_group *se_tpg)
  1059. {
  1060. struct usbg_tpg *tpg = container_of(se_tpg,
  1061. struct usbg_tpg, se_tpg);
  1062. return tpg->tport_tpgt;
  1063. }
  1064. static u32 usbg_tpg_get_inst_index(struct se_portal_group *se_tpg)
  1065. {
  1066. return 1;
  1067. }
  1068. static void usbg_release_cmd(struct se_cmd *se_cmd)
  1069. {
  1070. struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd,
  1071. se_cmd);
  1072. struct se_session *se_sess = se_cmd->se_sess;
  1073. kfree(cmd->data_buf);
  1074. percpu_ida_free(&se_sess->sess_tag_pool, se_cmd->map_tag);
  1075. }
  1076. static u32 usbg_sess_get_index(struct se_session *se_sess)
  1077. {
  1078. return 0;
  1079. }
  1080. /*
  1081. * XXX Error recovery: return != 0 if we expect writes. Dunno when that could be
  1082. */
  1083. static int usbg_write_pending_status(struct se_cmd *se_cmd)
  1084. {
  1085. return 0;
  1086. }
  1087. static void usbg_set_default_node_attrs(struct se_node_acl *nacl)
  1088. {
  1089. }
  1090. static int usbg_get_cmd_state(struct se_cmd *se_cmd)
  1091. {
  1092. return 0;
  1093. }
  1094. static void usbg_queue_tm_rsp(struct se_cmd *se_cmd)
  1095. {
  1096. }
  1097. static void usbg_aborted_task(struct se_cmd *se_cmd)
  1098. {
  1099. }
  1100. static const char *usbg_check_wwn(const char *name)
  1101. {
  1102. const char *n;
  1103. unsigned int len;
  1104. n = strstr(name, "naa.");
  1105. if (!n)
  1106. return NULL;
  1107. n += 4;
  1108. len = strlen(n);
  1109. if (len == 0 || len > USBG_NAMELEN - 1)
  1110. return NULL;
  1111. return n;
  1112. }
  1113. static int usbg_init_nodeacl(struct se_node_acl *se_nacl, const char *name)
  1114. {
  1115. if (!usbg_check_wwn(name))
  1116. return -EINVAL;
  1117. return 0;
  1118. }
  1119. static struct se_portal_group *usbg_make_tpg(
  1120. struct se_wwn *wwn,
  1121. struct config_group *group,
  1122. const char *name)
  1123. {
  1124. struct usbg_tport *tport = container_of(wwn, struct usbg_tport,
  1125. tport_wwn);
  1126. struct usbg_tpg *tpg;
  1127. unsigned long tpgt;
  1128. int ret;
  1129. struct f_tcm_opts *opts;
  1130. unsigned i;
  1131. if (strstr(name, "tpgt_") != name)
  1132. return ERR_PTR(-EINVAL);
  1133. if (kstrtoul(name + 5, 0, &tpgt) || tpgt > UINT_MAX)
  1134. return ERR_PTR(-EINVAL);
  1135. ret = -ENODEV;
  1136. mutex_lock(&tpg_instances_lock);
  1137. for (i = 0; i < TPG_INSTANCES; ++i)
  1138. if (tpg_instances[i].func_inst && !tpg_instances[i].tpg)
  1139. break;
  1140. if (i == TPG_INSTANCES)
  1141. goto unlock_inst;
  1142. opts = container_of(tpg_instances[i].func_inst, struct f_tcm_opts,
  1143. func_inst);
  1144. mutex_lock(&opts->dep_lock);
  1145. if (!opts->ready)
  1146. goto unlock_dep;
  1147. if (opts->has_dep) {
  1148. if (!try_module_get(opts->dependent))
  1149. goto unlock_dep;
  1150. } else {
  1151. ret = configfs_depend_item_unlocked(
  1152. group->cg_subsys,
  1153. &opts->func_inst.group.cg_item);
  1154. if (ret)
  1155. goto unlock_dep;
  1156. }
  1157. tpg = kzalloc(sizeof(struct usbg_tpg), GFP_KERNEL);
  1158. ret = -ENOMEM;
  1159. if (!tpg)
  1160. goto unref_dep;
  1161. mutex_init(&tpg->tpg_mutex);
  1162. atomic_set(&tpg->tpg_port_count, 0);
  1163. tpg->workqueue = alloc_workqueue("tcm_usb_gadget", 0, 1);
  1164. if (!tpg->workqueue)
  1165. goto free_tpg;
  1166. tpg->tport = tport;
  1167. tpg->tport_tpgt = tpgt;
  1168. /*
  1169. * SPC doesn't assign a protocol identifier for USB-SCSI, so we
  1170. * pretend to be SAS..
  1171. */
  1172. ret = core_tpg_register(wwn, &tpg->se_tpg, SCSI_PROTOCOL_SAS);
  1173. if (ret < 0)
  1174. goto free_workqueue;
  1175. tpg_instances[i].tpg = tpg;
  1176. tpg->fi = tpg_instances[i].func_inst;
  1177. mutex_unlock(&opts->dep_lock);
  1178. mutex_unlock(&tpg_instances_lock);
  1179. return &tpg->se_tpg;
  1180. free_workqueue:
  1181. destroy_workqueue(tpg->workqueue);
  1182. free_tpg:
  1183. kfree(tpg);
  1184. unref_dep:
  1185. if (opts->has_dep)
  1186. module_put(opts->dependent);
  1187. else
  1188. configfs_undepend_item_unlocked(&opts->func_inst.group.cg_item);
  1189. unlock_dep:
  1190. mutex_unlock(&opts->dep_lock);
  1191. unlock_inst:
  1192. mutex_unlock(&tpg_instances_lock);
  1193. return ERR_PTR(ret);
  1194. }
  1195. static int tcm_usbg_drop_nexus(struct usbg_tpg *);
  1196. static void usbg_drop_tpg(struct se_portal_group *se_tpg)
  1197. {
  1198. struct usbg_tpg *tpg = container_of(se_tpg,
  1199. struct usbg_tpg, se_tpg);
  1200. unsigned i;
  1201. struct f_tcm_opts *opts;
  1202. tcm_usbg_drop_nexus(tpg);
  1203. core_tpg_deregister(se_tpg);
  1204. destroy_workqueue(tpg->workqueue);
  1205. mutex_lock(&tpg_instances_lock);
  1206. for (i = 0; i < TPG_INSTANCES; ++i)
  1207. if (tpg_instances[i].tpg == tpg)
  1208. break;
  1209. if (i < TPG_INSTANCES) {
  1210. tpg_instances[i].tpg = NULL;
  1211. opts = container_of(tpg_instances[i].func_inst,
  1212. struct f_tcm_opts, func_inst);
  1213. mutex_lock(&opts->dep_lock);
  1214. if (opts->has_dep)
  1215. module_put(opts->dependent);
  1216. else
  1217. configfs_undepend_item_unlocked(
  1218. &opts->func_inst.group.cg_item);
  1219. mutex_unlock(&opts->dep_lock);
  1220. }
  1221. mutex_unlock(&tpg_instances_lock);
  1222. kfree(tpg);
  1223. }
  1224. static struct se_wwn *usbg_make_tport(
  1225. struct target_fabric_configfs *tf,
  1226. struct config_group *group,
  1227. const char *name)
  1228. {
  1229. struct usbg_tport *tport;
  1230. const char *wnn_name;
  1231. u64 wwpn = 0;
  1232. wnn_name = usbg_check_wwn(name);
  1233. if (!wnn_name)
  1234. return ERR_PTR(-EINVAL);
  1235. tport = kzalloc(sizeof(struct usbg_tport), GFP_KERNEL);
  1236. if (!(tport))
  1237. return ERR_PTR(-ENOMEM);
  1238. tport->tport_wwpn = wwpn;
  1239. snprintf(tport->tport_name, sizeof(tport->tport_name), "%s", wnn_name);
  1240. return &tport->tport_wwn;
  1241. }
  1242. static void usbg_drop_tport(struct se_wwn *wwn)
  1243. {
  1244. struct usbg_tport *tport = container_of(wwn,
  1245. struct usbg_tport, tport_wwn);
  1246. kfree(tport);
  1247. }
  1248. /*
  1249. * If somebody feels like dropping the version property, go ahead.
  1250. */
  1251. static ssize_t usbg_wwn_version_show(struct config_item *item, char *page)
  1252. {
  1253. return sprintf(page, "usb-gadget fabric module\n");
  1254. }
  1255. CONFIGFS_ATTR_RO(usbg_wwn_, version);
  1256. static struct configfs_attribute *usbg_wwn_attrs[] = {
  1257. &usbg_wwn_attr_version,
  1258. NULL,
  1259. };
  1260. static ssize_t tcm_usbg_tpg_enable_show(struct config_item *item, char *page)
  1261. {
  1262. struct se_portal_group *se_tpg = to_tpg(item);
  1263. struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
  1264. return snprintf(page, PAGE_SIZE, "%u\n", tpg->gadget_connect);
  1265. }
  1266. static int usbg_attach(struct usbg_tpg *);
  1267. static void usbg_detach(struct usbg_tpg *);
  1268. static ssize_t tcm_usbg_tpg_enable_store(struct config_item *item,
  1269. const char *page, size_t count)
  1270. {
  1271. struct se_portal_group *se_tpg = to_tpg(item);
  1272. struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
  1273. bool op;
  1274. ssize_t ret;
  1275. ret = strtobool(page, &op);
  1276. if (ret)
  1277. return ret;
  1278. if ((op && tpg->gadget_connect) || (!op && !tpg->gadget_connect))
  1279. return -EINVAL;
  1280. if (op)
  1281. ret = usbg_attach(tpg);
  1282. else
  1283. usbg_detach(tpg);
  1284. if (ret)
  1285. return ret;
  1286. tpg->gadget_connect = op;
  1287. return count;
  1288. }
  1289. static ssize_t tcm_usbg_tpg_nexus_show(struct config_item *item, char *page)
  1290. {
  1291. struct se_portal_group *se_tpg = to_tpg(item);
  1292. struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
  1293. struct tcm_usbg_nexus *tv_nexus;
  1294. ssize_t ret;
  1295. mutex_lock(&tpg->tpg_mutex);
  1296. tv_nexus = tpg->tpg_nexus;
  1297. if (!tv_nexus) {
  1298. ret = -ENODEV;
  1299. goto out;
  1300. }
  1301. ret = snprintf(page, PAGE_SIZE, "%s\n",
  1302. tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
  1303. out:
  1304. mutex_unlock(&tpg->tpg_mutex);
  1305. return ret;
  1306. }
  1307. static int usbg_alloc_sess_cb(struct se_portal_group *se_tpg,
  1308. struct se_session *se_sess, void *p)
  1309. {
  1310. struct usbg_tpg *tpg = container_of(se_tpg,
  1311. struct usbg_tpg, se_tpg);
  1312. tpg->tpg_nexus = p;
  1313. return 0;
  1314. }
  1315. static int tcm_usbg_make_nexus(struct usbg_tpg *tpg, char *name)
  1316. {
  1317. struct tcm_usbg_nexus *tv_nexus;
  1318. int ret = 0;
  1319. mutex_lock(&tpg->tpg_mutex);
  1320. if (tpg->tpg_nexus) {
  1321. ret = -EEXIST;
  1322. pr_debug("tpg->tpg_nexus already exists\n");
  1323. goto out_unlock;
  1324. }
  1325. tv_nexus = kzalloc(sizeof(*tv_nexus), GFP_KERNEL);
  1326. if (!tv_nexus) {
  1327. ret = -ENOMEM;
  1328. goto out_unlock;
  1329. }
  1330. tv_nexus->tvn_se_sess = target_alloc_session(&tpg->se_tpg,
  1331. USB_G_DEFAULT_SESSION_TAGS,
  1332. sizeof(struct usbg_cmd),
  1333. TARGET_PROT_NORMAL, name,
  1334. tv_nexus, usbg_alloc_sess_cb);
  1335. if (IS_ERR(tv_nexus->tvn_se_sess)) {
  1336. #define MAKE_NEXUS_MSG "core_tpg_check_initiator_node_acl() failed for %s\n"
  1337. pr_debug(MAKE_NEXUS_MSG, name);
  1338. #undef MAKE_NEXUS_MSG
  1339. ret = PTR_ERR(tv_nexus->tvn_se_sess);
  1340. kfree(tv_nexus);
  1341. }
  1342. out_unlock:
  1343. mutex_unlock(&tpg->tpg_mutex);
  1344. return ret;
  1345. }
  1346. static int tcm_usbg_drop_nexus(struct usbg_tpg *tpg)
  1347. {
  1348. struct se_session *se_sess;
  1349. struct tcm_usbg_nexus *tv_nexus;
  1350. int ret = -ENODEV;
  1351. mutex_lock(&tpg->tpg_mutex);
  1352. tv_nexus = tpg->tpg_nexus;
  1353. if (!tv_nexus)
  1354. goto out;
  1355. se_sess = tv_nexus->tvn_se_sess;
  1356. if (!se_sess)
  1357. goto out;
  1358. if (atomic_read(&tpg->tpg_port_count)) {
  1359. ret = -EPERM;
  1360. #define MSG "Unable to remove Host I_T Nexus with active TPG port count: %d\n"
  1361. pr_err(MSG, atomic_read(&tpg->tpg_port_count));
  1362. #undef MSG
  1363. goto out;
  1364. }
  1365. pr_debug("Removing I_T Nexus to Initiator Port: %s\n",
  1366. tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
  1367. /*
  1368. * Release the SCSI I_T Nexus to the emulated vHost Target Port
  1369. */
  1370. transport_deregister_session(tv_nexus->tvn_se_sess);
  1371. tpg->tpg_nexus = NULL;
  1372. kfree(tv_nexus);
  1373. ret = 0;
  1374. out:
  1375. mutex_unlock(&tpg->tpg_mutex);
  1376. return ret;
  1377. }
  1378. static ssize_t tcm_usbg_tpg_nexus_store(struct config_item *item,
  1379. const char *page, size_t count)
  1380. {
  1381. struct se_portal_group *se_tpg = to_tpg(item);
  1382. struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
  1383. unsigned char i_port[USBG_NAMELEN], *ptr;
  1384. int ret;
  1385. if (!strncmp(page, "NULL", 4)) {
  1386. ret = tcm_usbg_drop_nexus(tpg);
  1387. return (!ret) ? count : ret;
  1388. }
  1389. if (strlen(page) >= USBG_NAMELEN) {
  1390. #define NEXUS_STORE_MSG "Emulated NAA Sas Address: %s, exceeds max: %d\n"
  1391. pr_err(NEXUS_STORE_MSG, page, USBG_NAMELEN);
  1392. #undef NEXUS_STORE_MSG
  1393. return -EINVAL;
  1394. }
  1395. snprintf(i_port, USBG_NAMELEN, "%s", page);
  1396. ptr = strstr(i_port, "naa.");
  1397. if (!ptr) {
  1398. pr_err("Missing 'naa.' prefix\n");
  1399. return -EINVAL;
  1400. }
  1401. if (i_port[strlen(i_port) - 1] == '\n')
  1402. i_port[strlen(i_port) - 1] = '\0';
  1403. ret = tcm_usbg_make_nexus(tpg, &i_port[0]);
  1404. if (ret < 0)
  1405. return ret;
  1406. return count;
  1407. }
  1408. CONFIGFS_ATTR(tcm_usbg_tpg_, enable);
  1409. CONFIGFS_ATTR(tcm_usbg_tpg_, nexus);
  1410. static struct configfs_attribute *usbg_base_attrs[] = {
  1411. &tcm_usbg_tpg_attr_enable,
  1412. &tcm_usbg_tpg_attr_nexus,
  1413. NULL,
  1414. };
  1415. static int usbg_port_link(struct se_portal_group *se_tpg, struct se_lun *lun)
  1416. {
  1417. struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
  1418. atomic_inc(&tpg->tpg_port_count);
  1419. smp_mb__after_atomic();
  1420. return 0;
  1421. }
  1422. static void usbg_port_unlink(struct se_portal_group *se_tpg,
  1423. struct se_lun *se_lun)
  1424. {
  1425. struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
  1426. atomic_dec(&tpg->tpg_port_count);
  1427. smp_mb__after_atomic();
  1428. }
  1429. static int usbg_check_stop_free(struct se_cmd *se_cmd)
  1430. {
  1431. return target_put_sess_cmd(se_cmd);
  1432. }
  1433. static const struct target_core_fabric_ops usbg_ops = {
  1434. .module = THIS_MODULE,
  1435. .name = "usb_gadget",
  1436. .get_fabric_name = usbg_get_fabric_name,
  1437. .tpg_get_wwn = usbg_get_fabric_wwn,
  1438. .tpg_get_tag = usbg_get_tag,
  1439. .tpg_check_demo_mode = usbg_check_true,
  1440. .tpg_check_demo_mode_cache = usbg_check_false,
  1441. .tpg_check_demo_mode_write_protect = usbg_check_false,
  1442. .tpg_check_prod_mode_write_protect = usbg_check_false,
  1443. .tpg_get_inst_index = usbg_tpg_get_inst_index,
  1444. .release_cmd = usbg_release_cmd,
  1445. .sess_get_index = usbg_sess_get_index,
  1446. .sess_get_initiator_sid = NULL,
  1447. .write_pending = usbg_send_write_request,
  1448. .write_pending_status = usbg_write_pending_status,
  1449. .set_default_node_attributes = usbg_set_default_node_attrs,
  1450. .get_cmd_state = usbg_get_cmd_state,
  1451. .queue_data_in = usbg_send_read_response,
  1452. .queue_status = usbg_send_status_response,
  1453. .queue_tm_rsp = usbg_queue_tm_rsp,
  1454. .aborted_task = usbg_aborted_task,
  1455. .check_stop_free = usbg_check_stop_free,
  1456. .fabric_make_wwn = usbg_make_tport,
  1457. .fabric_drop_wwn = usbg_drop_tport,
  1458. .fabric_make_tpg = usbg_make_tpg,
  1459. .fabric_drop_tpg = usbg_drop_tpg,
  1460. .fabric_post_link = usbg_port_link,
  1461. .fabric_pre_unlink = usbg_port_unlink,
  1462. .fabric_init_nodeacl = usbg_init_nodeacl,
  1463. .tfc_wwn_attrs = usbg_wwn_attrs,
  1464. .tfc_tpg_base_attrs = usbg_base_attrs,
  1465. };
  1466. /* Start gadget.c code */
  1467. static struct usb_interface_descriptor bot_intf_desc = {
  1468. .bLength = sizeof(bot_intf_desc),
  1469. .bDescriptorType = USB_DT_INTERFACE,
  1470. .bNumEndpoints = 2,
  1471. .bAlternateSetting = USB_G_ALT_INT_BBB,
  1472. .bInterfaceClass = USB_CLASS_MASS_STORAGE,
  1473. .bInterfaceSubClass = USB_SC_SCSI,
  1474. .bInterfaceProtocol = USB_PR_BULK,
  1475. };
  1476. static struct usb_interface_descriptor uasp_intf_desc = {
  1477. .bLength = sizeof(uasp_intf_desc),
  1478. .bDescriptorType = USB_DT_INTERFACE,
  1479. .bNumEndpoints = 4,
  1480. .bAlternateSetting = USB_G_ALT_INT_UAS,
  1481. .bInterfaceClass = USB_CLASS_MASS_STORAGE,
  1482. .bInterfaceSubClass = USB_SC_SCSI,
  1483. .bInterfaceProtocol = USB_PR_UAS,
  1484. };
  1485. static struct usb_endpoint_descriptor uasp_bi_desc = {
  1486. .bLength = USB_DT_ENDPOINT_SIZE,
  1487. .bDescriptorType = USB_DT_ENDPOINT,
  1488. .bEndpointAddress = USB_DIR_IN,
  1489. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1490. .wMaxPacketSize = cpu_to_le16(512),
  1491. };
  1492. static struct usb_endpoint_descriptor uasp_fs_bi_desc = {
  1493. .bLength = USB_DT_ENDPOINT_SIZE,
  1494. .bDescriptorType = USB_DT_ENDPOINT,
  1495. .bEndpointAddress = USB_DIR_IN,
  1496. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1497. };
  1498. static struct usb_pipe_usage_descriptor uasp_bi_pipe_desc = {
  1499. .bLength = sizeof(uasp_bi_pipe_desc),
  1500. .bDescriptorType = USB_DT_PIPE_USAGE,
  1501. .bPipeID = DATA_IN_PIPE_ID,
  1502. };
  1503. static struct usb_endpoint_descriptor uasp_ss_bi_desc = {
  1504. .bLength = USB_DT_ENDPOINT_SIZE,
  1505. .bDescriptorType = USB_DT_ENDPOINT,
  1506. .bEndpointAddress = USB_DIR_IN,
  1507. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1508. .wMaxPacketSize = cpu_to_le16(1024),
  1509. };
  1510. static struct usb_ss_ep_comp_descriptor uasp_bi_ep_comp_desc = {
  1511. .bLength = sizeof(uasp_bi_ep_comp_desc),
  1512. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  1513. .bMaxBurst = 0,
  1514. .bmAttributes = UASP_SS_EP_COMP_LOG_STREAMS,
  1515. .wBytesPerInterval = 0,
  1516. };
  1517. static struct usb_ss_ep_comp_descriptor bot_bi_ep_comp_desc = {
  1518. .bLength = sizeof(bot_bi_ep_comp_desc),
  1519. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  1520. .bMaxBurst = 0,
  1521. };
  1522. static struct usb_endpoint_descriptor uasp_bo_desc = {
  1523. .bLength = USB_DT_ENDPOINT_SIZE,
  1524. .bDescriptorType = USB_DT_ENDPOINT,
  1525. .bEndpointAddress = USB_DIR_OUT,
  1526. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1527. .wMaxPacketSize = cpu_to_le16(512),
  1528. };
  1529. static struct usb_endpoint_descriptor uasp_fs_bo_desc = {
  1530. .bLength = USB_DT_ENDPOINT_SIZE,
  1531. .bDescriptorType = USB_DT_ENDPOINT,
  1532. .bEndpointAddress = USB_DIR_OUT,
  1533. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1534. };
  1535. static struct usb_pipe_usage_descriptor uasp_bo_pipe_desc = {
  1536. .bLength = sizeof(uasp_bo_pipe_desc),
  1537. .bDescriptorType = USB_DT_PIPE_USAGE,
  1538. .bPipeID = DATA_OUT_PIPE_ID,
  1539. };
  1540. static struct usb_endpoint_descriptor uasp_ss_bo_desc = {
  1541. .bLength = USB_DT_ENDPOINT_SIZE,
  1542. .bDescriptorType = USB_DT_ENDPOINT,
  1543. .bEndpointAddress = USB_DIR_OUT,
  1544. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1545. .wMaxPacketSize = cpu_to_le16(0x400),
  1546. };
  1547. static struct usb_ss_ep_comp_descriptor uasp_bo_ep_comp_desc = {
  1548. .bLength = sizeof(uasp_bo_ep_comp_desc),
  1549. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  1550. .bmAttributes = UASP_SS_EP_COMP_LOG_STREAMS,
  1551. };
  1552. static struct usb_ss_ep_comp_descriptor bot_bo_ep_comp_desc = {
  1553. .bLength = sizeof(bot_bo_ep_comp_desc),
  1554. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  1555. };
  1556. static struct usb_endpoint_descriptor uasp_status_desc = {
  1557. .bLength = USB_DT_ENDPOINT_SIZE,
  1558. .bDescriptorType = USB_DT_ENDPOINT,
  1559. .bEndpointAddress = USB_DIR_IN,
  1560. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1561. .wMaxPacketSize = cpu_to_le16(512),
  1562. };
  1563. static struct usb_endpoint_descriptor uasp_fs_status_desc = {
  1564. .bLength = USB_DT_ENDPOINT_SIZE,
  1565. .bDescriptorType = USB_DT_ENDPOINT,
  1566. .bEndpointAddress = USB_DIR_IN,
  1567. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1568. };
  1569. static struct usb_pipe_usage_descriptor uasp_status_pipe_desc = {
  1570. .bLength = sizeof(uasp_status_pipe_desc),
  1571. .bDescriptorType = USB_DT_PIPE_USAGE,
  1572. .bPipeID = STATUS_PIPE_ID,
  1573. };
  1574. static struct usb_endpoint_descriptor uasp_ss_status_desc = {
  1575. .bLength = USB_DT_ENDPOINT_SIZE,
  1576. .bDescriptorType = USB_DT_ENDPOINT,
  1577. .bEndpointAddress = USB_DIR_IN,
  1578. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1579. .wMaxPacketSize = cpu_to_le16(1024),
  1580. };
  1581. static struct usb_ss_ep_comp_descriptor uasp_status_in_ep_comp_desc = {
  1582. .bLength = sizeof(uasp_status_in_ep_comp_desc),
  1583. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  1584. .bmAttributes = UASP_SS_EP_COMP_LOG_STREAMS,
  1585. };
  1586. static struct usb_endpoint_descriptor uasp_cmd_desc = {
  1587. .bLength = USB_DT_ENDPOINT_SIZE,
  1588. .bDescriptorType = USB_DT_ENDPOINT,
  1589. .bEndpointAddress = USB_DIR_OUT,
  1590. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1591. .wMaxPacketSize = cpu_to_le16(512),
  1592. };
  1593. static struct usb_endpoint_descriptor uasp_fs_cmd_desc = {
  1594. .bLength = USB_DT_ENDPOINT_SIZE,
  1595. .bDescriptorType = USB_DT_ENDPOINT,
  1596. .bEndpointAddress = USB_DIR_OUT,
  1597. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1598. };
  1599. static struct usb_pipe_usage_descriptor uasp_cmd_pipe_desc = {
  1600. .bLength = sizeof(uasp_cmd_pipe_desc),
  1601. .bDescriptorType = USB_DT_PIPE_USAGE,
  1602. .bPipeID = CMD_PIPE_ID,
  1603. };
  1604. static struct usb_endpoint_descriptor uasp_ss_cmd_desc = {
  1605. .bLength = USB_DT_ENDPOINT_SIZE,
  1606. .bDescriptorType = USB_DT_ENDPOINT,
  1607. .bEndpointAddress = USB_DIR_OUT,
  1608. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1609. .wMaxPacketSize = cpu_to_le16(1024),
  1610. };
  1611. static struct usb_ss_ep_comp_descriptor uasp_cmd_comp_desc = {
  1612. .bLength = sizeof(uasp_cmd_comp_desc),
  1613. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  1614. };
  1615. static struct usb_descriptor_header *uasp_fs_function_desc[] = {
  1616. (struct usb_descriptor_header *) &bot_intf_desc,
  1617. (struct usb_descriptor_header *) &uasp_fs_bi_desc,
  1618. (struct usb_descriptor_header *) &uasp_fs_bo_desc,
  1619. (struct usb_descriptor_header *) &uasp_intf_desc,
  1620. (struct usb_descriptor_header *) &uasp_fs_bi_desc,
  1621. (struct usb_descriptor_header *) &uasp_bi_pipe_desc,
  1622. (struct usb_descriptor_header *) &uasp_fs_bo_desc,
  1623. (struct usb_descriptor_header *) &uasp_bo_pipe_desc,
  1624. (struct usb_descriptor_header *) &uasp_fs_status_desc,
  1625. (struct usb_descriptor_header *) &uasp_status_pipe_desc,
  1626. (struct usb_descriptor_header *) &uasp_fs_cmd_desc,
  1627. (struct usb_descriptor_header *) &uasp_cmd_pipe_desc,
  1628. NULL,
  1629. };
  1630. static struct usb_descriptor_header *uasp_hs_function_desc[] = {
  1631. (struct usb_descriptor_header *) &bot_intf_desc,
  1632. (struct usb_descriptor_header *) &uasp_bi_desc,
  1633. (struct usb_descriptor_header *) &uasp_bo_desc,
  1634. (struct usb_descriptor_header *) &uasp_intf_desc,
  1635. (struct usb_descriptor_header *) &uasp_bi_desc,
  1636. (struct usb_descriptor_header *) &uasp_bi_pipe_desc,
  1637. (struct usb_descriptor_header *) &uasp_bo_desc,
  1638. (struct usb_descriptor_header *) &uasp_bo_pipe_desc,
  1639. (struct usb_descriptor_header *) &uasp_status_desc,
  1640. (struct usb_descriptor_header *) &uasp_status_pipe_desc,
  1641. (struct usb_descriptor_header *) &uasp_cmd_desc,
  1642. (struct usb_descriptor_header *) &uasp_cmd_pipe_desc,
  1643. NULL,
  1644. };
  1645. static struct usb_descriptor_header *uasp_ss_function_desc[] = {
  1646. (struct usb_descriptor_header *) &bot_intf_desc,
  1647. (struct usb_descriptor_header *) &uasp_ss_bi_desc,
  1648. (struct usb_descriptor_header *) &bot_bi_ep_comp_desc,
  1649. (struct usb_descriptor_header *) &uasp_ss_bo_desc,
  1650. (struct usb_descriptor_header *) &bot_bo_ep_comp_desc,
  1651. (struct usb_descriptor_header *) &uasp_intf_desc,
  1652. (struct usb_descriptor_header *) &uasp_ss_bi_desc,
  1653. (struct usb_descriptor_header *) &uasp_bi_ep_comp_desc,
  1654. (struct usb_descriptor_header *) &uasp_bi_pipe_desc,
  1655. (struct usb_descriptor_header *) &uasp_ss_bo_desc,
  1656. (struct usb_descriptor_header *) &uasp_bo_ep_comp_desc,
  1657. (struct usb_descriptor_header *) &uasp_bo_pipe_desc,
  1658. (struct usb_descriptor_header *) &uasp_ss_status_desc,
  1659. (struct usb_descriptor_header *) &uasp_status_in_ep_comp_desc,
  1660. (struct usb_descriptor_header *) &uasp_status_pipe_desc,
  1661. (struct usb_descriptor_header *) &uasp_ss_cmd_desc,
  1662. (struct usb_descriptor_header *) &uasp_cmd_comp_desc,
  1663. (struct usb_descriptor_header *) &uasp_cmd_pipe_desc,
  1664. NULL,
  1665. };
  1666. static struct usb_string tcm_us_strings[] = {
  1667. [USB_G_STR_INT_UAS].s = "USB Attached SCSI",
  1668. [USB_G_STR_INT_BBB].s = "Bulk Only Transport",
  1669. { },
  1670. };
  1671. static struct usb_gadget_strings tcm_stringtab = {
  1672. .language = 0x0409,
  1673. .strings = tcm_us_strings,
  1674. };
  1675. static struct usb_gadget_strings *tcm_strings[] = {
  1676. &tcm_stringtab,
  1677. NULL,
  1678. };
  1679. static int tcm_bind(struct usb_configuration *c, struct usb_function *f)
  1680. {
  1681. struct f_uas *fu = to_f_uas(f);
  1682. struct usb_string *us;
  1683. struct usb_gadget *gadget = c->cdev->gadget;
  1684. struct usb_ep *ep;
  1685. struct f_tcm_opts *opts;
  1686. int iface;
  1687. int ret;
  1688. opts = container_of(f->fi, struct f_tcm_opts, func_inst);
  1689. mutex_lock(&opts->dep_lock);
  1690. if (!opts->can_attach) {
  1691. mutex_unlock(&opts->dep_lock);
  1692. return -ENODEV;
  1693. }
  1694. mutex_unlock(&opts->dep_lock);
  1695. us = usb_gstrings_attach(c->cdev, tcm_strings,
  1696. ARRAY_SIZE(tcm_us_strings));
  1697. if (IS_ERR(us))
  1698. return PTR_ERR(us);
  1699. bot_intf_desc.iInterface = us[USB_G_STR_INT_BBB].id;
  1700. uasp_intf_desc.iInterface = us[USB_G_STR_INT_UAS].id;
  1701. iface = usb_interface_id(c, f);
  1702. if (iface < 0)
  1703. return iface;
  1704. bot_intf_desc.bInterfaceNumber = iface;
  1705. uasp_intf_desc.bInterfaceNumber = iface;
  1706. fu->iface = iface;
  1707. ep = usb_ep_autoconfig_ss(gadget, &uasp_ss_bi_desc,
  1708. &uasp_bi_ep_comp_desc);
  1709. if (!ep)
  1710. goto ep_fail;
  1711. fu->ep_in = ep;
  1712. ep = usb_ep_autoconfig_ss(gadget, &uasp_ss_bo_desc,
  1713. &uasp_bo_ep_comp_desc);
  1714. if (!ep)
  1715. goto ep_fail;
  1716. fu->ep_out = ep;
  1717. ep = usb_ep_autoconfig_ss(gadget, &uasp_ss_status_desc,
  1718. &uasp_status_in_ep_comp_desc);
  1719. if (!ep)
  1720. goto ep_fail;
  1721. fu->ep_status = ep;
  1722. ep = usb_ep_autoconfig_ss(gadget, &uasp_ss_cmd_desc,
  1723. &uasp_cmd_comp_desc);
  1724. if (!ep)
  1725. goto ep_fail;
  1726. fu->ep_cmd = ep;
  1727. /* Assume endpoint addresses are the same for both speeds */
  1728. uasp_bi_desc.bEndpointAddress = uasp_ss_bi_desc.bEndpointAddress;
  1729. uasp_bo_desc.bEndpointAddress = uasp_ss_bo_desc.bEndpointAddress;
  1730. uasp_status_desc.bEndpointAddress =
  1731. uasp_ss_status_desc.bEndpointAddress;
  1732. uasp_cmd_desc.bEndpointAddress = uasp_ss_cmd_desc.bEndpointAddress;
  1733. uasp_fs_bi_desc.bEndpointAddress = uasp_ss_bi_desc.bEndpointAddress;
  1734. uasp_fs_bo_desc.bEndpointAddress = uasp_ss_bo_desc.bEndpointAddress;
  1735. uasp_fs_status_desc.bEndpointAddress =
  1736. uasp_ss_status_desc.bEndpointAddress;
  1737. uasp_fs_cmd_desc.bEndpointAddress = uasp_ss_cmd_desc.bEndpointAddress;
  1738. ret = usb_assign_descriptors(f, uasp_fs_function_desc,
  1739. uasp_hs_function_desc, uasp_ss_function_desc, NULL);
  1740. if (ret)
  1741. goto ep_fail;
  1742. return 0;
  1743. ep_fail:
  1744. pr_err("Can't claim all required eps\n");
  1745. return -ENOTSUPP;
  1746. }
  1747. struct guas_setup_wq {
  1748. struct work_struct work;
  1749. struct f_uas *fu;
  1750. unsigned int alt;
  1751. };
  1752. static void tcm_delayed_set_alt(struct work_struct *wq)
  1753. {
  1754. struct guas_setup_wq *work = container_of(wq, struct guas_setup_wq,
  1755. work);
  1756. struct f_uas *fu = work->fu;
  1757. int alt = work->alt;
  1758. kfree(work);
  1759. if (fu->flags & USBG_IS_BOT)
  1760. bot_cleanup_old_alt(fu);
  1761. if (fu->flags & USBG_IS_UAS)
  1762. uasp_cleanup_old_alt(fu);
  1763. if (alt == USB_G_ALT_INT_BBB)
  1764. bot_set_alt(fu);
  1765. else if (alt == USB_G_ALT_INT_UAS)
  1766. uasp_set_alt(fu);
  1767. usb_composite_setup_continue(fu->function.config->cdev);
  1768. }
  1769. static int tcm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  1770. {
  1771. struct f_uas *fu = to_f_uas(f);
  1772. if ((alt == USB_G_ALT_INT_BBB) || (alt == USB_G_ALT_INT_UAS)) {
  1773. struct guas_setup_wq *work;
  1774. work = kmalloc(sizeof(*work), GFP_ATOMIC);
  1775. if (!work)
  1776. return -ENOMEM;
  1777. INIT_WORK(&work->work, tcm_delayed_set_alt);
  1778. work->fu = fu;
  1779. work->alt = alt;
  1780. schedule_work(&work->work);
  1781. return USB_GADGET_DELAYED_STATUS;
  1782. }
  1783. return -EOPNOTSUPP;
  1784. }
  1785. static void tcm_disable(struct usb_function *f)
  1786. {
  1787. struct f_uas *fu = to_f_uas(f);
  1788. if (fu->flags & USBG_IS_UAS)
  1789. uasp_cleanup_old_alt(fu);
  1790. else if (fu->flags & USBG_IS_BOT)
  1791. bot_cleanup_old_alt(fu);
  1792. fu->flags = 0;
  1793. }
  1794. static int tcm_setup(struct usb_function *f,
  1795. const struct usb_ctrlrequest *ctrl)
  1796. {
  1797. struct f_uas *fu = to_f_uas(f);
  1798. if (!(fu->flags & USBG_IS_BOT))
  1799. return -EOPNOTSUPP;
  1800. return usbg_bot_setup(f, ctrl);
  1801. }
  1802. static inline struct f_tcm_opts *to_f_tcm_opts(struct config_item *item)
  1803. {
  1804. return container_of(to_config_group(item), struct f_tcm_opts,
  1805. func_inst.group);
  1806. }
  1807. static void tcm_attr_release(struct config_item *item)
  1808. {
  1809. struct f_tcm_opts *opts = to_f_tcm_opts(item);
  1810. usb_put_function_instance(&opts->func_inst);
  1811. }
  1812. static struct configfs_item_operations tcm_item_ops = {
  1813. .release = tcm_attr_release,
  1814. };
  1815. static const struct config_item_type tcm_func_type = {
  1816. .ct_item_ops = &tcm_item_ops,
  1817. .ct_owner = THIS_MODULE,
  1818. };
  1819. static void tcm_free_inst(struct usb_function_instance *f)
  1820. {
  1821. struct f_tcm_opts *opts;
  1822. unsigned i;
  1823. opts = container_of(f, struct f_tcm_opts, func_inst);
  1824. mutex_lock(&tpg_instances_lock);
  1825. for (i = 0; i < TPG_INSTANCES; ++i)
  1826. if (tpg_instances[i].func_inst == f)
  1827. break;
  1828. if (i < TPG_INSTANCES)
  1829. tpg_instances[i].func_inst = NULL;
  1830. mutex_unlock(&tpg_instances_lock);
  1831. kfree(opts);
  1832. }
  1833. static int tcm_register_callback(struct usb_function_instance *f)
  1834. {
  1835. struct f_tcm_opts *opts = container_of(f, struct f_tcm_opts, func_inst);
  1836. mutex_lock(&opts->dep_lock);
  1837. opts->can_attach = true;
  1838. mutex_unlock(&opts->dep_lock);
  1839. return 0;
  1840. }
  1841. static void tcm_unregister_callback(struct usb_function_instance *f)
  1842. {
  1843. struct f_tcm_opts *opts = container_of(f, struct f_tcm_opts, func_inst);
  1844. mutex_lock(&opts->dep_lock);
  1845. unregister_gadget_item(opts->
  1846. func_inst.group.cg_item.ci_parent->ci_parent);
  1847. opts->can_attach = false;
  1848. mutex_unlock(&opts->dep_lock);
  1849. }
  1850. static int usbg_attach(struct usbg_tpg *tpg)
  1851. {
  1852. struct usb_function_instance *f = tpg->fi;
  1853. struct f_tcm_opts *opts = container_of(f, struct f_tcm_opts, func_inst);
  1854. if (opts->tcm_register_callback)
  1855. return opts->tcm_register_callback(f);
  1856. return 0;
  1857. }
  1858. static void usbg_detach(struct usbg_tpg *tpg)
  1859. {
  1860. struct usb_function_instance *f = tpg->fi;
  1861. struct f_tcm_opts *opts = container_of(f, struct f_tcm_opts, func_inst);
  1862. if (opts->tcm_unregister_callback)
  1863. opts->tcm_unregister_callback(f);
  1864. }
  1865. static int tcm_set_name(struct usb_function_instance *f, const char *name)
  1866. {
  1867. struct f_tcm_opts *opts = container_of(f, struct f_tcm_opts, func_inst);
  1868. pr_debug("tcm: Activating %s\n", name);
  1869. mutex_lock(&opts->dep_lock);
  1870. opts->ready = true;
  1871. mutex_unlock(&opts->dep_lock);
  1872. return 0;
  1873. }
  1874. static struct usb_function_instance *tcm_alloc_inst(void)
  1875. {
  1876. struct f_tcm_opts *opts;
  1877. int i;
  1878. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  1879. if (!opts)
  1880. return ERR_PTR(-ENOMEM);
  1881. mutex_lock(&tpg_instances_lock);
  1882. for (i = 0; i < TPG_INSTANCES; ++i)
  1883. if (!tpg_instances[i].func_inst)
  1884. break;
  1885. if (i == TPG_INSTANCES) {
  1886. mutex_unlock(&tpg_instances_lock);
  1887. kfree(opts);
  1888. return ERR_PTR(-EBUSY);
  1889. }
  1890. tpg_instances[i].func_inst = &opts->func_inst;
  1891. mutex_unlock(&tpg_instances_lock);
  1892. mutex_init(&opts->dep_lock);
  1893. opts->func_inst.set_inst_name = tcm_set_name;
  1894. opts->func_inst.free_func_inst = tcm_free_inst;
  1895. opts->tcm_register_callback = tcm_register_callback;
  1896. opts->tcm_unregister_callback = tcm_unregister_callback;
  1897. config_group_init_type_name(&opts->func_inst.group, "",
  1898. &tcm_func_type);
  1899. return &opts->func_inst;
  1900. }
  1901. static void tcm_free(struct usb_function *f)
  1902. {
  1903. struct f_uas *tcm = to_f_uas(f);
  1904. kfree(tcm);
  1905. }
  1906. static void tcm_unbind(struct usb_configuration *c, struct usb_function *f)
  1907. {
  1908. usb_free_all_descriptors(f);
  1909. }
  1910. static struct usb_function *tcm_alloc(struct usb_function_instance *fi)
  1911. {
  1912. struct f_uas *fu;
  1913. unsigned i;
  1914. mutex_lock(&tpg_instances_lock);
  1915. for (i = 0; i < TPG_INSTANCES; ++i)
  1916. if (tpg_instances[i].func_inst == fi)
  1917. break;
  1918. if (i == TPG_INSTANCES) {
  1919. mutex_unlock(&tpg_instances_lock);
  1920. return ERR_PTR(-ENODEV);
  1921. }
  1922. fu = kzalloc(sizeof(*fu), GFP_KERNEL);
  1923. if (!fu) {
  1924. mutex_unlock(&tpg_instances_lock);
  1925. return ERR_PTR(-ENOMEM);
  1926. }
  1927. fu->function.name = "Target Function";
  1928. fu->function.bind = tcm_bind;
  1929. fu->function.unbind = tcm_unbind;
  1930. fu->function.set_alt = tcm_set_alt;
  1931. fu->function.setup = tcm_setup;
  1932. fu->function.disable = tcm_disable;
  1933. fu->function.free_func = tcm_free;
  1934. fu->tpg = tpg_instances[i].tpg;
  1935. mutex_unlock(&tpg_instances_lock);
  1936. return &fu->function;
  1937. }
  1938. DECLARE_USB_FUNCTION(tcm, tcm_alloc_inst, tcm_alloc);
  1939. static int tcm_init(void)
  1940. {
  1941. int ret;
  1942. ret = usb_function_register(&tcmusb_func);
  1943. if (ret)
  1944. return ret;
  1945. ret = target_register_template(&usbg_ops);
  1946. if (ret)
  1947. usb_function_unregister(&tcmusb_func);
  1948. return ret;
  1949. }
  1950. module_init(tcm_init);
  1951. static void tcm_exit(void)
  1952. {
  1953. target_unregister_template(&usbg_ops);
  1954. usb_function_unregister(&tcmusb_func);
  1955. }
  1956. module_exit(tcm_exit);
  1957. MODULE_LICENSE("GPL");
  1958. MODULE_AUTHOR("Sebastian Andrzej Siewior");