tcm_usb_gadget.c 59 KB

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