hcd.c 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208
  1. /*
  2. * hcd.c - DesignWare HS OTG Controller host-mode routines
  3. *
  4. * Copyright (C) 2004-2013 Synopsys, Inc.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions, and the following disclaimer,
  11. * without modification.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. The names of the above-listed copyright holders may not be used
  16. * to endorse or promote products derived from this software without
  17. * specific prior written permission.
  18. *
  19. * ALTERNATIVELY, this software may be distributed under the terms of the
  20. * GNU General Public License ("GPL") as published by the Free Software
  21. * Foundation; either version 2 of the License, or (at your option) any
  22. * later version.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  25. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  26. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  27. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  28. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  29. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  30. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  31. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  32. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  33. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  34. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. /*
  37. * This file contains the core HCD code, and implements the Linux hc_driver
  38. * API
  39. */
  40. #include <linux/kernel.h>
  41. #include <linux/module.h>
  42. #include <linux/spinlock.h>
  43. #include <linux/interrupt.h>
  44. #include <linux/dma-mapping.h>
  45. #include <linux/delay.h>
  46. #include <linux/io.h>
  47. #include <linux/slab.h>
  48. #include <linux/usb.h>
  49. #include <linux/usb/hcd.h>
  50. #include <linux/usb/ch11.h>
  51. #include "core.h"
  52. #include "hcd.h"
  53. /**
  54. * dwc2_dump_channel_info() - Prints the state of a host channel
  55. *
  56. * @hsotg: Programming view of DWC_otg controller
  57. * @chan: Pointer to the channel to dump
  58. *
  59. * Must be called with interrupt disabled and spinlock held
  60. *
  61. * NOTE: This function will be removed once the peripheral controller code
  62. * is integrated and the driver is stable
  63. */
  64. static void dwc2_dump_channel_info(struct dwc2_hsotg *hsotg,
  65. struct dwc2_host_chan *chan)
  66. {
  67. #ifdef VERBOSE_DEBUG
  68. int num_channels = hsotg->core_params->host_channels;
  69. struct dwc2_qh *qh;
  70. u32 hcchar;
  71. u32 hcsplt;
  72. u32 hctsiz;
  73. u32 hc_dma;
  74. int i;
  75. if (chan == NULL)
  76. return;
  77. hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
  78. hcsplt = dwc2_readl(hsotg->regs + HCSPLT(chan->hc_num));
  79. hctsiz = dwc2_readl(hsotg->regs + HCTSIZ(chan->hc_num));
  80. hc_dma = dwc2_readl(hsotg->regs + HCDMA(chan->hc_num));
  81. dev_dbg(hsotg->dev, " Assigned to channel %p:\n", chan);
  82. dev_dbg(hsotg->dev, " hcchar 0x%08x, hcsplt 0x%08x\n",
  83. hcchar, hcsplt);
  84. dev_dbg(hsotg->dev, " hctsiz 0x%08x, hc_dma 0x%08x\n",
  85. hctsiz, hc_dma);
  86. dev_dbg(hsotg->dev, " dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
  87. chan->dev_addr, chan->ep_num, chan->ep_is_in);
  88. dev_dbg(hsotg->dev, " ep_type: %d\n", chan->ep_type);
  89. dev_dbg(hsotg->dev, " max_packet: %d\n", chan->max_packet);
  90. dev_dbg(hsotg->dev, " data_pid_start: %d\n", chan->data_pid_start);
  91. dev_dbg(hsotg->dev, " xfer_started: %d\n", chan->xfer_started);
  92. dev_dbg(hsotg->dev, " halt_status: %d\n", chan->halt_status);
  93. dev_dbg(hsotg->dev, " xfer_buf: %p\n", chan->xfer_buf);
  94. dev_dbg(hsotg->dev, " xfer_dma: %08lx\n",
  95. (unsigned long)chan->xfer_dma);
  96. dev_dbg(hsotg->dev, " xfer_len: %d\n", chan->xfer_len);
  97. dev_dbg(hsotg->dev, " qh: %p\n", chan->qh);
  98. dev_dbg(hsotg->dev, " NP inactive sched:\n");
  99. list_for_each_entry(qh, &hsotg->non_periodic_sched_inactive,
  100. qh_list_entry)
  101. dev_dbg(hsotg->dev, " %p\n", qh);
  102. dev_dbg(hsotg->dev, " NP active sched:\n");
  103. list_for_each_entry(qh, &hsotg->non_periodic_sched_active,
  104. qh_list_entry)
  105. dev_dbg(hsotg->dev, " %p\n", qh);
  106. dev_dbg(hsotg->dev, " Channels:\n");
  107. for (i = 0; i < num_channels; i++) {
  108. struct dwc2_host_chan *chan = hsotg->hc_ptr_array[i];
  109. dev_dbg(hsotg->dev, " %2d: %p\n", i, chan);
  110. }
  111. #endif /* VERBOSE_DEBUG */
  112. }
  113. /*
  114. * Processes all the URBs in a single list of QHs. Completes them with
  115. * -ETIMEDOUT and frees the QTD.
  116. *
  117. * Must be called with interrupt disabled and spinlock held
  118. */
  119. static void dwc2_kill_urbs_in_qh_list(struct dwc2_hsotg *hsotg,
  120. struct list_head *qh_list)
  121. {
  122. struct dwc2_qh *qh, *qh_tmp;
  123. struct dwc2_qtd *qtd, *qtd_tmp;
  124. list_for_each_entry_safe(qh, qh_tmp, qh_list, qh_list_entry) {
  125. list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
  126. qtd_list_entry) {
  127. dwc2_host_complete(hsotg, qtd, -ECONNRESET);
  128. dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
  129. }
  130. }
  131. }
  132. static void dwc2_qh_list_free(struct dwc2_hsotg *hsotg,
  133. struct list_head *qh_list)
  134. {
  135. struct dwc2_qtd *qtd, *qtd_tmp;
  136. struct dwc2_qh *qh, *qh_tmp;
  137. unsigned long flags;
  138. if (!qh_list->next)
  139. /* The list hasn't been initialized yet */
  140. return;
  141. spin_lock_irqsave(&hsotg->lock, flags);
  142. /* Ensure there are no QTDs or URBs left */
  143. dwc2_kill_urbs_in_qh_list(hsotg, qh_list);
  144. list_for_each_entry_safe(qh, qh_tmp, qh_list, qh_list_entry) {
  145. dwc2_hcd_qh_unlink(hsotg, qh);
  146. /* Free each QTD in the QH's QTD list */
  147. list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
  148. qtd_list_entry)
  149. dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
  150. spin_unlock_irqrestore(&hsotg->lock, flags);
  151. dwc2_hcd_qh_free(hsotg, qh);
  152. spin_lock_irqsave(&hsotg->lock, flags);
  153. }
  154. spin_unlock_irqrestore(&hsotg->lock, flags);
  155. }
  156. /*
  157. * Responds with an error status of -ETIMEDOUT to all URBs in the non-periodic
  158. * and periodic schedules. The QTD associated with each URB is removed from
  159. * the schedule and freed. This function may be called when a disconnect is
  160. * detected or when the HCD is being stopped.
  161. *
  162. * Must be called with interrupt disabled and spinlock held
  163. */
  164. static void dwc2_kill_all_urbs(struct dwc2_hsotg *hsotg)
  165. {
  166. dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_inactive);
  167. dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_active);
  168. dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_inactive);
  169. dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_ready);
  170. dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_assigned);
  171. dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_queued);
  172. }
  173. /**
  174. * dwc2_hcd_start() - Starts the HCD when switching to Host mode
  175. *
  176. * @hsotg: Pointer to struct dwc2_hsotg
  177. */
  178. void dwc2_hcd_start(struct dwc2_hsotg *hsotg)
  179. {
  180. u32 hprt0;
  181. if (hsotg->op_state == OTG_STATE_B_HOST) {
  182. /*
  183. * Reset the port. During a HNP mode switch the reset
  184. * needs to occur within 1ms and have a duration of at
  185. * least 50ms.
  186. */
  187. hprt0 = dwc2_read_hprt0(hsotg);
  188. hprt0 |= HPRT0_RST;
  189. dwc2_writel(hprt0, hsotg->regs + HPRT0);
  190. }
  191. queue_delayed_work(hsotg->wq_otg, &hsotg->start_work,
  192. msecs_to_jiffies(50));
  193. }
  194. /* Must be called with interrupt disabled and spinlock held */
  195. static void dwc2_hcd_cleanup_channels(struct dwc2_hsotg *hsotg)
  196. {
  197. int num_channels = hsotg->core_params->host_channels;
  198. struct dwc2_host_chan *channel;
  199. u32 hcchar;
  200. int i;
  201. if (hsotg->core_params->dma_enable <= 0) {
  202. /* Flush out any channel requests in slave mode */
  203. for (i = 0; i < num_channels; i++) {
  204. channel = hsotg->hc_ptr_array[i];
  205. if (!list_empty(&channel->hc_list_entry))
  206. continue;
  207. hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
  208. if (hcchar & HCCHAR_CHENA) {
  209. hcchar &= ~(HCCHAR_CHENA | HCCHAR_EPDIR);
  210. hcchar |= HCCHAR_CHDIS;
  211. dwc2_writel(hcchar, hsotg->regs + HCCHAR(i));
  212. }
  213. }
  214. }
  215. for (i = 0; i < num_channels; i++) {
  216. channel = hsotg->hc_ptr_array[i];
  217. if (!list_empty(&channel->hc_list_entry))
  218. continue;
  219. hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
  220. if (hcchar & HCCHAR_CHENA) {
  221. /* Halt the channel */
  222. hcchar |= HCCHAR_CHDIS;
  223. dwc2_writel(hcchar, hsotg->regs + HCCHAR(i));
  224. }
  225. dwc2_hc_cleanup(hsotg, channel);
  226. list_add_tail(&channel->hc_list_entry, &hsotg->free_hc_list);
  227. /*
  228. * Added for Descriptor DMA to prevent channel double cleanup in
  229. * release_channel_ddma(), which is called from ep_disable when
  230. * device disconnects
  231. */
  232. channel->qh = NULL;
  233. }
  234. /* All channels have been freed, mark them available */
  235. if (hsotg->core_params->uframe_sched > 0) {
  236. hsotg->available_host_channels =
  237. hsotg->core_params->host_channels;
  238. } else {
  239. hsotg->non_periodic_channels = 0;
  240. hsotg->periodic_channels = 0;
  241. }
  242. }
  243. /**
  244. * dwc2_hcd_disconnect() - Handles disconnect of the HCD
  245. *
  246. * @hsotg: Pointer to struct dwc2_hsotg
  247. *
  248. * Must be called with interrupt disabled and spinlock held
  249. */
  250. void dwc2_hcd_disconnect(struct dwc2_hsotg *hsotg)
  251. {
  252. u32 intr;
  253. /* Set status flags for the hub driver */
  254. hsotg->flags.b.port_connect_status_change = 1;
  255. hsotg->flags.b.port_connect_status = 0;
  256. /*
  257. * Shutdown any transfers in process by clearing the Tx FIFO Empty
  258. * interrupt mask and status bits and disabling subsequent host
  259. * channel interrupts.
  260. */
  261. intr = dwc2_readl(hsotg->regs + GINTMSK);
  262. intr &= ~(GINTSTS_NPTXFEMP | GINTSTS_PTXFEMP | GINTSTS_HCHINT);
  263. dwc2_writel(intr, hsotg->regs + GINTMSK);
  264. intr = GINTSTS_NPTXFEMP | GINTSTS_PTXFEMP | GINTSTS_HCHINT;
  265. dwc2_writel(intr, hsotg->regs + GINTSTS);
  266. /*
  267. * Turn off the vbus power only if the core has transitioned to device
  268. * mode. If still in host mode, need to keep power on to detect a
  269. * reconnection.
  270. */
  271. if (dwc2_is_device_mode(hsotg)) {
  272. if (hsotg->op_state != OTG_STATE_A_SUSPEND) {
  273. dev_dbg(hsotg->dev, "Disconnect: PortPower off\n");
  274. dwc2_writel(0, hsotg->regs + HPRT0);
  275. }
  276. dwc2_disable_host_interrupts(hsotg);
  277. }
  278. /* Respond with an error status to all URBs in the schedule */
  279. dwc2_kill_all_urbs(hsotg);
  280. if (dwc2_is_host_mode(hsotg))
  281. /* Clean up any host channels that were in use */
  282. dwc2_hcd_cleanup_channels(hsotg);
  283. dwc2_host_disconnect(hsotg);
  284. }
  285. /**
  286. * dwc2_hcd_rem_wakeup() - Handles Remote Wakeup
  287. *
  288. * @hsotg: Pointer to struct dwc2_hsotg
  289. */
  290. static void dwc2_hcd_rem_wakeup(struct dwc2_hsotg *hsotg)
  291. {
  292. if (hsotg->bus_suspended) {
  293. hsotg->flags.b.port_suspend_change = 1;
  294. usb_hcd_resume_root_hub(hsotg->priv);
  295. }
  296. if (hsotg->lx_state == DWC2_L1)
  297. hsotg->flags.b.port_l1_change = 1;
  298. }
  299. /**
  300. * dwc2_hcd_stop() - Halts the DWC_otg host mode operations in a clean manner
  301. *
  302. * @hsotg: Pointer to struct dwc2_hsotg
  303. *
  304. * Must be called with interrupt disabled and spinlock held
  305. */
  306. void dwc2_hcd_stop(struct dwc2_hsotg *hsotg)
  307. {
  308. dev_dbg(hsotg->dev, "DWC OTG HCD STOP\n");
  309. /*
  310. * The root hub should be disconnected before this function is called.
  311. * The disconnect will clear the QTD lists (via ..._hcd_urb_dequeue)
  312. * and the QH lists (via ..._hcd_endpoint_disable).
  313. */
  314. /* Turn off all host-specific interrupts */
  315. dwc2_disable_host_interrupts(hsotg);
  316. /* Turn off the vbus power */
  317. dev_dbg(hsotg->dev, "PortPower off\n");
  318. dwc2_writel(0, hsotg->regs + HPRT0);
  319. }
  320. /* Caller must hold driver lock */
  321. static int dwc2_hcd_urb_enqueue(struct dwc2_hsotg *hsotg,
  322. struct dwc2_hcd_urb *urb, struct dwc2_qh *qh,
  323. struct dwc2_qtd *qtd)
  324. {
  325. u32 intr_mask;
  326. int retval;
  327. int dev_speed;
  328. if (!hsotg->flags.b.port_connect_status) {
  329. /* No longer connected */
  330. dev_err(hsotg->dev, "Not connected\n");
  331. return -ENODEV;
  332. }
  333. dev_speed = dwc2_host_get_speed(hsotg, urb->priv);
  334. /* Some configurations cannot support LS traffic on a FS root port */
  335. if ((dev_speed == USB_SPEED_LOW) &&
  336. (hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED) &&
  337. (hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI)) {
  338. u32 hprt0 = dwc2_readl(hsotg->regs + HPRT0);
  339. u32 prtspd = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
  340. if (prtspd == HPRT0_SPD_FULL_SPEED)
  341. return -ENODEV;
  342. }
  343. if (!qtd)
  344. return -EINVAL;
  345. dwc2_hcd_qtd_init(qtd, urb);
  346. retval = dwc2_hcd_qtd_add(hsotg, qtd, qh);
  347. if (retval) {
  348. dev_err(hsotg->dev,
  349. "DWC OTG HCD URB Enqueue failed adding QTD. Error status %d\n",
  350. retval);
  351. return retval;
  352. }
  353. intr_mask = dwc2_readl(hsotg->regs + GINTMSK);
  354. if (!(intr_mask & GINTSTS_SOF)) {
  355. enum dwc2_transaction_type tr_type;
  356. if (qtd->qh->ep_type == USB_ENDPOINT_XFER_BULK &&
  357. !(qtd->urb->flags & URB_GIVEBACK_ASAP))
  358. /*
  359. * Do not schedule SG transactions until qtd has
  360. * URB_GIVEBACK_ASAP set
  361. */
  362. return 0;
  363. tr_type = dwc2_hcd_select_transactions(hsotg);
  364. if (tr_type != DWC2_TRANSACTION_NONE)
  365. dwc2_hcd_queue_transactions(hsotg, tr_type);
  366. }
  367. return 0;
  368. }
  369. /* Must be called with interrupt disabled and spinlock held */
  370. static int dwc2_hcd_urb_dequeue(struct dwc2_hsotg *hsotg,
  371. struct dwc2_hcd_urb *urb)
  372. {
  373. struct dwc2_qh *qh;
  374. struct dwc2_qtd *urb_qtd;
  375. urb_qtd = urb->qtd;
  376. if (!urb_qtd) {
  377. dev_dbg(hsotg->dev, "## Urb QTD is NULL ##\n");
  378. return -EINVAL;
  379. }
  380. qh = urb_qtd->qh;
  381. if (!qh) {
  382. dev_dbg(hsotg->dev, "## Urb QTD QH is NULL ##\n");
  383. return -EINVAL;
  384. }
  385. urb->priv = NULL;
  386. if (urb_qtd->in_process && qh->channel) {
  387. dwc2_dump_channel_info(hsotg, qh->channel);
  388. /* The QTD is in process (it has been assigned to a channel) */
  389. if (hsotg->flags.b.port_connect_status)
  390. /*
  391. * If still connected (i.e. in host mode), halt the
  392. * channel so it can be used for other transfers. If
  393. * no longer connected, the host registers can't be
  394. * written to halt the channel since the core is in
  395. * device mode.
  396. */
  397. dwc2_hc_halt(hsotg, qh->channel,
  398. DWC2_HC_XFER_URB_DEQUEUE);
  399. }
  400. /*
  401. * Free the QTD and clean up the associated QH. Leave the QH in the
  402. * schedule if it has any remaining QTDs.
  403. */
  404. if (hsotg->core_params->dma_desc_enable <= 0) {
  405. u8 in_process = urb_qtd->in_process;
  406. dwc2_hcd_qtd_unlink_and_free(hsotg, urb_qtd, qh);
  407. if (in_process) {
  408. dwc2_hcd_qh_deactivate(hsotg, qh, 0);
  409. qh->channel = NULL;
  410. } else if (list_empty(&qh->qtd_list)) {
  411. dwc2_hcd_qh_unlink(hsotg, qh);
  412. }
  413. } else {
  414. dwc2_hcd_qtd_unlink_and_free(hsotg, urb_qtd, qh);
  415. }
  416. return 0;
  417. }
  418. /* Must NOT be called with interrupt disabled or spinlock held */
  419. static int dwc2_hcd_endpoint_disable(struct dwc2_hsotg *hsotg,
  420. struct usb_host_endpoint *ep, int retry)
  421. {
  422. struct dwc2_qtd *qtd, *qtd_tmp;
  423. struct dwc2_qh *qh;
  424. unsigned long flags;
  425. int rc;
  426. spin_lock_irqsave(&hsotg->lock, flags);
  427. qh = ep->hcpriv;
  428. if (!qh) {
  429. rc = -EINVAL;
  430. goto err;
  431. }
  432. while (!list_empty(&qh->qtd_list) && retry--) {
  433. if (retry == 0) {
  434. dev_err(hsotg->dev,
  435. "## timeout in dwc2_hcd_endpoint_disable() ##\n");
  436. rc = -EBUSY;
  437. goto err;
  438. }
  439. spin_unlock_irqrestore(&hsotg->lock, flags);
  440. usleep_range(20000, 40000);
  441. spin_lock_irqsave(&hsotg->lock, flags);
  442. qh = ep->hcpriv;
  443. if (!qh) {
  444. rc = -EINVAL;
  445. goto err;
  446. }
  447. }
  448. dwc2_hcd_qh_unlink(hsotg, qh);
  449. /* Free each QTD in the QH's QTD list */
  450. list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry)
  451. dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
  452. ep->hcpriv = NULL;
  453. spin_unlock_irqrestore(&hsotg->lock, flags);
  454. dwc2_hcd_qh_free(hsotg, qh);
  455. return 0;
  456. err:
  457. ep->hcpriv = NULL;
  458. spin_unlock_irqrestore(&hsotg->lock, flags);
  459. return rc;
  460. }
  461. /* Must be called with interrupt disabled and spinlock held */
  462. static int dwc2_hcd_endpoint_reset(struct dwc2_hsotg *hsotg,
  463. struct usb_host_endpoint *ep)
  464. {
  465. struct dwc2_qh *qh = ep->hcpriv;
  466. if (!qh)
  467. return -EINVAL;
  468. qh->data_toggle = DWC2_HC_PID_DATA0;
  469. return 0;
  470. }
  471. /*
  472. * Initializes dynamic portions of the DWC_otg HCD state
  473. *
  474. * Must be called with interrupt disabled and spinlock held
  475. */
  476. static void dwc2_hcd_reinit(struct dwc2_hsotg *hsotg)
  477. {
  478. struct dwc2_host_chan *chan, *chan_tmp;
  479. int num_channels;
  480. int i;
  481. hsotg->flags.d32 = 0;
  482. hsotg->non_periodic_qh_ptr = &hsotg->non_periodic_sched_active;
  483. if (hsotg->core_params->uframe_sched > 0) {
  484. hsotg->available_host_channels =
  485. hsotg->core_params->host_channels;
  486. } else {
  487. hsotg->non_periodic_channels = 0;
  488. hsotg->periodic_channels = 0;
  489. }
  490. /*
  491. * Put all channels in the free channel list and clean up channel
  492. * states
  493. */
  494. list_for_each_entry_safe(chan, chan_tmp, &hsotg->free_hc_list,
  495. hc_list_entry)
  496. list_del_init(&chan->hc_list_entry);
  497. num_channels = hsotg->core_params->host_channels;
  498. for (i = 0; i < num_channels; i++) {
  499. chan = hsotg->hc_ptr_array[i];
  500. list_add_tail(&chan->hc_list_entry, &hsotg->free_hc_list);
  501. dwc2_hc_cleanup(hsotg, chan);
  502. }
  503. /* Initialize the DWC core for host mode operation */
  504. dwc2_core_host_init(hsotg);
  505. }
  506. static void dwc2_hc_init_split(struct dwc2_hsotg *hsotg,
  507. struct dwc2_host_chan *chan,
  508. struct dwc2_qtd *qtd, struct dwc2_hcd_urb *urb)
  509. {
  510. int hub_addr, hub_port;
  511. chan->do_split = 1;
  512. chan->xact_pos = qtd->isoc_split_pos;
  513. chan->complete_split = qtd->complete_split;
  514. dwc2_host_hub_info(hsotg, urb->priv, &hub_addr, &hub_port);
  515. chan->hub_addr = (u8)hub_addr;
  516. chan->hub_port = (u8)hub_port;
  517. }
  518. static void *dwc2_hc_init_xfer(struct dwc2_hsotg *hsotg,
  519. struct dwc2_host_chan *chan,
  520. struct dwc2_qtd *qtd, void *bufptr)
  521. {
  522. struct dwc2_hcd_urb *urb = qtd->urb;
  523. struct dwc2_hcd_iso_packet_desc *frame_desc;
  524. switch (dwc2_hcd_get_pipe_type(&urb->pipe_info)) {
  525. case USB_ENDPOINT_XFER_CONTROL:
  526. chan->ep_type = USB_ENDPOINT_XFER_CONTROL;
  527. switch (qtd->control_phase) {
  528. case DWC2_CONTROL_SETUP:
  529. dev_vdbg(hsotg->dev, " Control setup transaction\n");
  530. chan->do_ping = 0;
  531. chan->ep_is_in = 0;
  532. chan->data_pid_start = DWC2_HC_PID_SETUP;
  533. if (hsotg->core_params->dma_enable > 0)
  534. chan->xfer_dma = urb->setup_dma;
  535. else
  536. chan->xfer_buf = urb->setup_packet;
  537. chan->xfer_len = 8;
  538. bufptr = NULL;
  539. break;
  540. case DWC2_CONTROL_DATA:
  541. dev_vdbg(hsotg->dev, " Control data transaction\n");
  542. chan->data_pid_start = qtd->data_toggle;
  543. break;
  544. case DWC2_CONTROL_STATUS:
  545. /*
  546. * Direction is opposite of data direction or IN if no
  547. * data
  548. */
  549. dev_vdbg(hsotg->dev, " Control status transaction\n");
  550. if (urb->length == 0)
  551. chan->ep_is_in = 1;
  552. else
  553. chan->ep_is_in =
  554. dwc2_hcd_is_pipe_out(&urb->pipe_info);
  555. if (chan->ep_is_in)
  556. chan->do_ping = 0;
  557. chan->data_pid_start = DWC2_HC_PID_DATA1;
  558. chan->xfer_len = 0;
  559. if (hsotg->core_params->dma_enable > 0)
  560. chan->xfer_dma = hsotg->status_buf_dma;
  561. else
  562. chan->xfer_buf = hsotg->status_buf;
  563. bufptr = NULL;
  564. break;
  565. }
  566. break;
  567. case USB_ENDPOINT_XFER_BULK:
  568. chan->ep_type = USB_ENDPOINT_XFER_BULK;
  569. break;
  570. case USB_ENDPOINT_XFER_INT:
  571. chan->ep_type = USB_ENDPOINT_XFER_INT;
  572. break;
  573. case USB_ENDPOINT_XFER_ISOC:
  574. chan->ep_type = USB_ENDPOINT_XFER_ISOC;
  575. if (hsotg->core_params->dma_desc_enable > 0)
  576. break;
  577. frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
  578. frame_desc->status = 0;
  579. if (hsotg->core_params->dma_enable > 0) {
  580. chan->xfer_dma = urb->dma;
  581. chan->xfer_dma += frame_desc->offset +
  582. qtd->isoc_split_offset;
  583. } else {
  584. chan->xfer_buf = urb->buf;
  585. chan->xfer_buf += frame_desc->offset +
  586. qtd->isoc_split_offset;
  587. }
  588. chan->xfer_len = frame_desc->length - qtd->isoc_split_offset;
  589. /* For non-dword aligned buffers */
  590. if (hsotg->core_params->dma_enable > 0 &&
  591. (chan->xfer_dma & 0x3))
  592. bufptr = (u8 *)urb->buf + frame_desc->offset +
  593. qtd->isoc_split_offset;
  594. else
  595. bufptr = NULL;
  596. if (chan->xact_pos == DWC2_HCSPLT_XACTPOS_ALL) {
  597. if (chan->xfer_len <= 188)
  598. chan->xact_pos = DWC2_HCSPLT_XACTPOS_ALL;
  599. else
  600. chan->xact_pos = DWC2_HCSPLT_XACTPOS_BEGIN;
  601. }
  602. break;
  603. }
  604. return bufptr;
  605. }
  606. static int dwc2_hc_setup_align_buf(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
  607. struct dwc2_host_chan *chan,
  608. struct dwc2_hcd_urb *urb, void *bufptr)
  609. {
  610. u32 buf_size;
  611. struct urb *usb_urb;
  612. struct usb_hcd *hcd;
  613. if (!qh->dw_align_buf) {
  614. if (chan->ep_type != USB_ENDPOINT_XFER_ISOC)
  615. buf_size = hsotg->core_params->max_transfer_size;
  616. else
  617. /* 3072 = 3 max-size Isoc packets */
  618. buf_size = 3072;
  619. qh->dw_align_buf = kmalloc(buf_size, GFP_ATOMIC | GFP_DMA);
  620. if (!qh->dw_align_buf)
  621. return -ENOMEM;
  622. qh->dw_align_buf_size = buf_size;
  623. }
  624. if (chan->xfer_len) {
  625. dev_vdbg(hsotg->dev, "%s(): non-aligned buffer\n", __func__);
  626. usb_urb = urb->priv;
  627. if (usb_urb) {
  628. if (usb_urb->transfer_flags &
  629. (URB_SETUP_MAP_SINGLE | URB_DMA_MAP_SG |
  630. URB_DMA_MAP_PAGE | URB_DMA_MAP_SINGLE)) {
  631. hcd = dwc2_hsotg_to_hcd(hsotg);
  632. usb_hcd_unmap_urb_for_dma(hcd, usb_urb);
  633. }
  634. if (!chan->ep_is_in)
  635. memcpy(qh->dw_align_buf, bufptr,
  636. chan->xfer_len);
  637. } else {
  638. dev_warn(hsotg->dev, "no URB in dwc2_urb\n");
  639. }
  640. }
  641. qh->dw_align_buf_dma = dma_map_single(hsotg->dev,
  642. qh->dw_align_buf, qh->dw_align_buf_size,
  643. chan->ep_is_in ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  644. if (dma_mapping_error(hsotg->dev, qh->dw_align_buf_dma)) {
  645. dev_err(hsotg->dev, "can't map align_buf\n");
  646. chan->align_buf = 0;
  647. return -EINVAL;
  648. }
  649. chan->align_buf = qh->dw_align_buf_dma;
  650. return 0;
  651. }
  652. /**
  653. * dwc2_assign_and_init_hc() - Assigns transactions from a QTD to a free host
  654. * channel and initializes the host channel to perform the transactions. The
  655. * host channel is removed from the free list.
  656. *
  657. * @hsotg: The HCD state structure
  658. * @qh: Transactions from the first QTD for this QH are selected and assigned
  659. * to a free host channel
  660. */
  661. static int dwc2_assign_and_init_hc(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
  662. {
  663. struct dwc2_host_chan *chan;
  664. struct dwc2_hcd_urb *urb;
  665. struct dwc2_qtd *qtd;
  666. void *bufptr = NULL;
  667. if (dbg_qh(qh))
  668. dev_vdbg(hsotg->dev, "%s(%p,%p)\n", __func__, hsotg, qh);
  669. if (list_empty(&qh->qtd_list)) {
  670. dev_dbg(hsotg->dev, "No QTDs in QH list\n");
  671. return -ENOMEM;
  672. }
  673. if (list_empty(&hsotg->free_hc_list)) {
  674. dev_dbg(hsotg->dev, "No free channel to assign\n");
  675. return -ENOMEM;
  676. }
  677. chan = list_first_entry(&hsotg->free_hc_list, struct dwc2_host_chan,
  678. hc_list_entry);
  679. /* Remove host channel from free list */
  680. list_del_init(&chan->hc_list_entry);
  681. qtd = list_first_entry(&qh->qtd_list, struct dwc2_qtd, qtd_list_entry);
  682. urb = qtd->urb;
  683. qh->channel = chan;
  684. qtd->in_process = 1;
  685. /*
  686. * Use usb_pipedevice to determine device address. This address is
  687. * 0 before the SET_ADDRESS command and the correct address afterward.
  688. */
  689. chan->dev_addr = dwc2_hcd_get_dev_addr(&urb->pipe_info);
  690. chan->ep_num = dwc2_hcd_get_ep_num(&urb->pipe_info);
  691. chan->speed = qh->dev_speed;
  692. chan->max_packet = dwc2_max_packet(qh->maxp);
  693. chan->xfer_started = 0;
  694. chan->halt_status = DWC2_HC_XFER_NO_HALT_STATUS;
  695. chan->error_state = (qtd->error_count > 0);
  696. chan->halt_on_queue = 0;
  697. chan->halt_pending = 0;
  698. chan->requests = 0;
  699. /*
  700. * The following values may be modified in the transfer type section
  701. * below. The xfer_len value may be reduced when the transfer is
  702. * started to accommodate the max widths of the XferSize and PktCnt
  703. * fields in the HCTSIZn register.
  704. */
  705. chan->ep_is_in = (dwc2_hcd_is_pipe_in(&urb->pipe_info) != 0);
  706. if (chan->ep_is_in)
  707. chan->do_ping = 0;
  708. else
  709. chan->do_ping = qh->ping_state;
  710. chan->data_pid_start = qh->data_toggle;
  711. chan->multi_count = 1;
  712. if (urb->actual_length > urb->length &&
  713. !dwc2_hcd_is_pipe_in(&urb->pipe_info))
  714. urb->actual_length = urb->length;
  715. if (hsotg->core_params->dma_enable > 0) {
  716. chan->xfer_dma = urb->dma + urb->actual_length;
  717. /* For non-dword aligned case */
  718. if (hsotg->core_params->dma_desc_enable <= 0 &&
  719. (chan->xfer_dma & 0x3))
  720. bufptr = (u8 *)urb->buf + urb->actual_length;
  721. } else {
  722. chan->xfer_buf = (u8 *)urb->buf + urb->actual_length;
  723. }
  724. chan->xfer_len = urb->length - urb->actual_length;
  725. chan->xfer_count = 0;
  726. /* Set the split attributes if required */
  727. if (qh->do_split)
  728. dwc2_hc_init_split(hsotg, chan, qtd, urb);
  729. else
  730. chan->do_split = 0;
  731. /* Set the transfer attributes */
  732. bufptr = dwc2_hc_init_xfer(hsotg, chan, qtd, bufptr);
  733. /* Non DWORD-aligned buffer case */
  734. if (bufptr) {
  735. dev_vdbg(hsotg->dev, "Non-aligned buffer\n");
  736. if (dwc2_hc_setup_align_buf(hsotg, qh, chan, urb, bufptr)) {
  737. dev_err(hsotg->dev,
  738. "%s: Failed to allocate memory to handle non-dword aligned buffer\n",
  739. __func__);
  740. /* Add channel back to free list */
  741. chan->align_buf = 0;
  742. chan->multi_count = 0;
  743. list_add_tail(&chan->hc_list_entry,
  744. &hsotg->free_hc_list);
  745. qtd->in_process = 0;
  746. qh->channel = NULL;
  747. return -ENOMEM;
  748. }
  749. } else {
  750. chan->align_buf = 0;
  751. }
  752. if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
  753. chan->ep_type == USB_ENDPOINT_XFER_ISOC)
  754. /*
  755. * This value may be modified when the transfer is started
  756. * to reflect the actual transfer length
  757. */
  758. chan->multi_count = dwc2_hb_mult(qh->maxp);
  759. if (hsotg->core_params->dma_desc_enable > 0)
  760. chan->desc_list_addr = qh->desc_list_dma;
  761. dwc2_hc_init(hsotg, chan);
  762. chan->qh = qh;
  763. return 0;
  764. }
  765. /**
  766. * dwc2_hcd_select_transactions() - Selects transactions from the HCD transfer
  767. * schedule and assigns them to available host channels. Called from the HCD
  768. * interrupt handler functions.
  769. *
  770. * @hsotg: The HCD state structure
  771. *
  772. * Return: The types of new transactions that were assigned to host channels
  773. */
  774. enum dwc2_transaction_type dwc2_hcd_select_transactions(
  775. struct dwc2_hsotg *hsotg)
  776. {
  777. enum dwc2_transaction_type ret_val = DWC2_TRANSACTION_NONE;
  778. struct list_head *qh_ptr;
  779. struct dwc2_qh *qh;
  780. int num_channels;
  781. #ifdef DWC2_DEBUG_SOF
  782. dev_vdbg(hsotg->dev, " Select Transactions\n");
  783. #endif
  784. /* Process entries in the periodic ready list */
  785. qh_ptr = hsotg->periodic_sched_ready.next;
  786. while (qh_ptr != &hsotg->periodic_sched_ready) {
  787. if (list_empty(&hsotg->free_hc_list))
  788. break;
  789. if (hsotg->core_params->uframe_sched > 0) {
  790. if (hsotg->available_host_channels <= 1)
  791. break;
  792. hsotg->available_host_channels--;
  793. }
  794. qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
  795. if (dwc2_assign_and_init_hc(hsotg, qh))
  796. break;
  797. /*
  798. * Move the QH from the periodic ready schedule to the
  799. * periodic assigned schedule
  800. */
  801. qh_ptr = qh_ptr->next;
  802. list_move(&qh->qh_list_entry, &hsotg->periodic_sched_assigned);
  803. ret_val = DWC2_TRANSACTION_PERIODIC;
  804. }
  805. /*
  806. * Process entries in the inactive portion of the non-periodic
  807. * schedule. Some free host channels may not be used if they are
  808. * reserved for periodic transfers.
  809. */
  810. num_channels = hsotg->core_params->host_channels;
  811. qh_ptr = hsotg->non_periodic_sched_inactive.next;
  812. while (qh_ptr != &hsotg->non_periodic_sched_inactive) {
  813. if (hsotg->core_params->uframe_sched <= 0 &&
  814. hsotg->non_periodic_channels >= num_channels -
  815. hsotg->periodic_channels)
  816. break;
  817. if (list_empty(&hsotg->free_hc_list))
  818. break;
  819. qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
  820. if (hsotg->core_params->uframe_sched > 0) {
  821. if (hsotg->available_host_channels < 1)
  822. break;
  823. hsotg->available_host_channels--;
  824. }
  825. if (dwc2_assign_and_init_hc(hsotg, qh))
  826. break;
  827. /*
  828. * Move the QH from the non-periodic inactive schedule to the
  829. * non-periodic active schedule
  830. */
  831. qh_ptr = qh_ptr->next;
  832. list_move(&qh->qh_list_entry,
  833. &hsotg->non_periodic_sched_active);
  834. if (ret_val == DWC2_TRANSACTION_NONE)
  835. ret_val = DWC2_TRANSACTION_NON_PERIODIC;
  836. else
  837. ret_val = DWC2_TRANSACTION_ALL;
  838. if (hsotg->core_params->uframe_sched <= 0)
  839. hsotg->non_periodic_channels++;
  840. }
  841. return ret_val;
  842. }
  843. /**
  844. * dwc2_queue_transaction() - Attempts to queue a single transaction request for
  845. * a host channel associated with either a periodic or non-periodic transfer
  846. *
  847. * @hsotg: The HCD state structure
  848. * @chan: Host channel descriptor associated with either a periodic or
  849. * non-periodic transfer
  850. * @fifo_dwords_avail: Number of DWORDs available in the periodic Tx FIFO
  851. * for periodic transfers or the non-periodic Tx FIFO
  852. * for non-periodic transfers
  853. *
  854. * Return: 1 if a request is queued and more requests may be needed to
  855. * complete the transfer, 0 if no more requests are required for this
  856. * transfer, -1 if there is insufficient space in the Tx FIFO
  857. *
  858. * This function assumes that there is space available in the appropriate
  859. * request queue. For an OUT transfer or SETUP transaction in Slave mode,
  860. * it checks whether space is available in the appropriate Tx FIFO.
  861. *
  862. * Must be called with interrupt disabled and spinlock held
  863. */
  864. static int dwc2_queue_transaction(struct dwc2_hsotg *hsotg,
  865. struct dwc2_host_chan *chan,
  866. u16 fifo_dwords_avail)
  867. {
  868. int retval = 0;
  869. if (hsotg->core_params->dma_enable > 0) {
  870. if (hsotg->core_params->dma_desc_enable > 0) {
  871. if (!chan->xfer_started ||
  872. chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
  873. dwc2_hcd_start_xfer_ddma(hsotg, chan->qh);
  874. chan->qh->ping_state = 0;
  875. }
  876. } else if (!chan->xfer_started) {
  877. dwc2_hc_start_transfer(hsotg, chan);
  878. chan->qh->ping_state = 0;
  879. }
  880. } else if (chan->halt_pending) {
  881. /* Don't queue a request if the channel has been halted */
  882. } else if (chan->halt_on_queue) {
  883. dwc2_hc_halt(hsotg, chan, chan->halt_status);
  884. } else if (chan->do_ping) {
  885. if (!chan->xfer_started)
  886. dwc2_hc_start_transfer(hsotg, chan);
  887. } else if (!chan->ep_is_in ||
  888. chan->data_pid_start == DWC2_HC_PID_SETUP) {
  889. if ((fifo_dwords_avail * 4) >= chan->max_packet) {
  890. if (!chan->xfer_started) {
  891. dwc2_hc_start_transfer(hsotg, chan);
  892. retval = 1;
  893. } else {
  894. retval = dwc2_hc_continue_transfer(hsotg, chan);
  895. }
  896. } else {
  897. retval = -1;
  898. }
  899. } else {
  900. if (!chan->xfer_started) {
  901. dwc2_hc_start_transfer(hsotg, chan);
  902. retval = 1;
  903. } else {
  904. retval = dwc2_hc_continue_transfer(hsotg, chan);
  905. }
  906. }
  907. return retval;
  908. }
  909. /*
  910. * Processes periodic channels for the next frame and queues transactions for
  911. * these channels to the DWC_otg controller. After queueing transactions, the
  912. * Periodic Tx FIFO Empty interrupt is enabled if there are more transactions
  913. * to queue as Periodic Tx FIFO or request queue space becomes available.
  914. * Otherwise, the Periodic Tx FIFO Empty interrupt is disabled.
  915. *
  916. * Must be called with interrupt disabled and spinlock held
  917. */
  918. static void dwc2_process_periodic_channels(struct dwc2_hsotg *hsotg)
  919. {
  920. struct list_head *qh_ptr;
  921. struct dwc2_qh *qh;
  922. u32 tx_status;
  923. u32 fspcavail;
  924. u32 gintmsk;
  925. int status;
  926. int no_queue_space = 0;
  927. int no_fifo_space = 0;
  928. u32 qspcavail;
  929. if (dbg_perio())
  930. dev_vdbg(hsotg->dev, "Queue periodic transactions\n");
  931. tx_status = dwc2_readl(hsotg->regs + HPTXSTS);
  932. qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
  933. TXSTS_QSPCAVAIL_SHIFT;
  934. fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
  935. TXSTS_FSPCAVAIL_SHIFT;
  936. if (dbg_perio()) {
  937. dev_vdbg(hsotg->dev, " P Tx Req Queue Space Avail (before queue): %d\n",
  938. qspcavail);
  939. dev_vdbg(hsotg->dev, " P Tx FIFO Space Avail (before queue): %d\n",
  940. fspcavail);
  941. }
  942. qh_ptr = hsotg->periodic_sched_assigned.next;
  943. while (qh_ptr != &hsotg->periodic_sched_assigned) {
  944. tx_status = dwc2_readl(hsotg->regs + HPTXSTS);
  945. qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
  946. TXSTS_QSPCAVAIL_SHIFT;
  947. if (qspcavail == 0) {
  948. no_queue_space = 1;
  949. break;
  950. }
  951. qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
  952. if (!qh->channel) {
  953. qh_ptr = qh_ptr->next;
  954. continue;
  955. }
  956. /* Make sure EP's TT buffer is clean before queueing qtds */
  957. if (qh->tt_buffer_dirty) {
  958. qh_ptr = qh_ptr->next;
  959. continue;
  960. }
  961. /*
  962. * Set a flag if we're queuing high-bandwidth in slave mode.
  963. * The flag prevents any halts to get into the request queue in
  964. * the middle of multiple high-bandwidth packets getting queued.
  965. */
  966. if (hsotg->core_params->dma_enable <= 0 &&
  967. qh->channel->multi_count > 1)
  968. hsotg->queuing_high_bandwidth = 1;
  969. fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
  970. TXSTS_FSPCAVAIL_SHIFT;
  971. status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail);
  972. if (status < 0) {
  973. no_fifo_space = 1;
  974. break;
  975. }
  976. /*
  977. * In Slave mode, stay on the current transfer until there is
  978. * nothing more to do or the high-bandwidth request count is
  979. * reached. In DMA mode, only need to queue one request. The
  980. * controller automatically handles multiple packets for
  981. * high-bandwidth transfers.
  982. */
  983. if (hsotg->core_params->dma_enable > 0 || status == 0 ||
  984. qh->channel->requests == qh->channel->multi_count) {
  985. qh_ptr = qh_ptr->next;
  986. /*
  987. * Move the QH from the periodic assigned schedule to
  988. * the periodic queued schedule
  989. */
  990. list_move(&qh->qh_list_entry,
  991. &hsotg->periodic_sched_queued);
  992. /* done queuing high bandwidth */
  993. hsotg->queuing_high_bandwidth = 0;
  994. }
  995. }
  996. if (hsotg->core_params->dma_enable <= 0) {
  997. tx_status = dwc2_readl(hsotg->regs + HPTXSTS);
  998. qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
  999. TXSTS_QSPCAVAIL_SHIFT;
  1000. fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
  1001. TXSTS_FSPCAVAIL_SHIFT;
  1002. if (dbg_perio()) {
  1003. dev_vdbg(hsotg->dev,
  1004. " P Tx Req Queue Space Avail (after queue): %d\n",
  1005. qspcavail);
  1006. dev_vdbg(hsotg->dev,
  1007. " P Tx FIFO Space Avail (after queue): %d\n",
  1008. fspcavail);
  1009. }
  1010. if (!list_empty(&hsotg->periodic_sched_assigned) ||
  1011. no_queue_space || no_fifo_space) {
  1012. /*
  1013. * May need to queue more transactions as the request
  1014. * queue or Tx FIFO empties. Enable the periodic Tx
  1015. * FIFO empty interrupt. (Always use the half-empty
  1016. * level to ensure that new requests are loaded as
  1017. * soon as possible.)
  1018. */
  1019. gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
  1020. gintmsk |= GINTSTS_PTXFEMP;
  1021. dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
  1022. } else {
  1023. /*
  1024. * Disable the Tx FIFO empty interrupt since there are
  1025. * no more transactions that need to be queued right
  1026. * now. This function is called from interrupt
  1027. * handlers to queue more transactions as transfer
  1028. * states change.
  1029. */
  1030. gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
  1031. gintmsk &= ~GINTSTS_PTXFEMP;
  1032. dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
  1033. }
  1034. }
  1035. }
  1036. /*
  1037. * Processes active non-periodic channels and queues transactions for these
  1038. * channels to the DWC_otg controller. After queueing transactions, the NP Tx
  1039. * FIFO Empty interrupt is enabled if there are more transactions to queue as
  1040. * NP Tx FIFO or request queue space becomes available. Otherwise, the NP Tx
  1041. * FIFO Empty interrupt is disabled.
  1042. *
  1043. * Must be called with interrupt disabled and spinlock held
  1044. */
  1045. static void dwc2_process_non_periodic_channels(struct dwc2_hsotg *hsotg)
  1046. {
  1047. struct list_head *orig_qh_ptr;
  1048. struct dwc2_qh *qh;
  1049. u32 tx_status;
  1050. u32 qspcavail;
  1051. u32 fspcavail;
  1052. u32 gintmsk;
  1053. int status;
  1054. int no_queue_space = 0;
  1055. int no_fifo_space = 0;
  1056. int more_to_do = 0;
  1057. dev_vdbg(hsotg->dev, "Queue non-periodic transactions\n");
  1058. tx_status = dwc2_readl(hsotg->regs + GNPTXSTS);
  1059. qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
  1060. TXSTS_QSPCAVAIL_SHIFT;
  1061. fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
  1062. TXSTS_FSPCAVAIL_SHIFT;
  1063. dev_vdbg(hsotg->dev, " NP Tx Req Queue Space Avail (before queue): %d\n",
  1064. qspcavail);
  1065. dev_vdbg(hsotg->dev, " NP Tx FIFO Space Avail (before queue): %d\n",
  1066. fspcavail);
  1067. /*
  1068. * Keep track of the starting point. Skip over the start-of-list
  1069. * entry.
  1070. */
  1071. if (hsotg->non_periodic_qh_ptr == &hsotg->non_periodic_sched_active)
  1072. hsotg->non_periodic_qh_ptr = hsotg->non_periodic_qh_ptr->next;
  1073. orig_qh_ptr = hsotg->non_periodic_qh_ptr;
  1074. /*
  1075. * Process once through the active list or until no more space is
  1076. * available in the request queue or the Tx FIFO
  1077. */
  1078. do {
  1079. tx_status = dwc2_readl(hsotg->regs + GNPTXSTS);
  1080. qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
  1081. TXSTS_QSPCAVAIL_SHIFT;
  1082. if (hsotg->core_params->dma_enable <= 0 && qspcavail == 0) {
  1083. no_queue_space = 1;
  1084. break;
  1085. }
  1086. qh = list_entry(hsotg->non_periodic_qh_ptr, struct dwc2_qh,
  1087. qh_list_entry);
  1088. if (!qh->channel)
  1089. goto next;
  1090. /* Make sure EP's TT buffer is clean before queueing qtds */
  1091. if (qh->tt_buffer_dirty)
  1092. goto next;
  1093. fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
  1094. TXSTS_FSPCAVAIL_SHIFT;
  1095. status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail);
  1096. if (status > 0) {
  1097. more_to_do = 1;
  1098. } else if (status < 0) {
  1099. no_fifo_space = 1;
  1100. break;
  1101. }
  1102. next:
  1103. /* Advance to next QH, skipping start-of-list entry */
  1104. hsotg->non_periodic_qh_ptr = hsotg->non_periodic_qh_ptr->next;
  1105. if (hsotg->non_periodic_qh_ptr ==
  1106. &hsotg->non_periodic_sched_active)
  1107. hsotg->non_periodic_qh_ptr =
  1108. hsotg->non_periodic_qh_ptr->next;
  1109. } while (hsotg->non_periodic_qh_ptr != orig_qh_ptr);
  1110. if (hsotg->core_params->dma_enable <= 0) {
  1111. tx_status = dwc2_readl(hsotg->regs + GNPTXSTS);
  1112. qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
  1113. TXSTS_QSPCAVAIL_SHIFT;
  1114. fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
  1115. TXSTS_FSPCAVAIL_SHIFT;
  1116. dev_vdbg(hsotg->dev,
  1117. " NP Tx Req Queue Space Avail (after queue): %d\n",
  1118. qspcavail);
  1119. dev_vdbg(hsotg->dev,
  1120. " NP Tx FIFO Space Avail (after queue): %d\n",
  1121. fspcavail);
  1122. if (more_to_do || no_queue_space || no_fifo_space) {
  1123. /*
  1124. * May need to queue more transactions as the request
  1125. * queue or Tx FIFO empties. Enable the non-periodic
  1126. * Tx FIFO empty interrupt. (Always use the half-empty
  1127. * level to ensure that new requests are loaded as
  1128. * soon as possible.)
  1129. */
  1130. gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
  1131. gintmsk |= GINTSTS_NPTXFEMP;
  1132. dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
  1133. } else {
  1134. /*
  1135. * Disable the Tx FIFO empty interrupt since there are
  1136. * no more transactions that need to be queued right
  1137. * now. This function is called from interrupt
  1138. * handlers to queue more transactions as transfer
  1139. * states change.
  1140. */
  1141. gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
  1142. gintmsk &= ~GINTSTS_NPTXFEMP;
  1143. dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
  1144. }
  1145. }
  1146. }
  1147. /**
  1148. * dwc2_hcd_queue_transactions() - Processes the currently active host channels
  1149. * and queues transactions for these channels to the DWC_otg controller. Called
  1150. * from the HCD interrupt handler functions.
  1151. *
  1152. * @hsotg: The HCD state structure
  1153. * @tr_type: The type(s) of transactions to queue (non-periodic, periodic,
  1154. * or both)
  1155. *
  1156. * Must be called with interrupt disabled and spinlock held
  1157. */
  1158. void dwc2_hcd_queue_transactions(struct dwc2_hsotg *hsotg,
  1159. enum dwc2_transaction_type tr_type)
  1160. {
  1161. #ifdef DWC2_DEBUG_SOF
  1162. dev_vdbg(hsotg->dev, "Queue Transactions\n");
  1163. #endif
  1164. /* Process host channels associated with periodic transfers */
  1165. if ((tr_type == DWC2_TRANSACTION_PERIODIC ||
  1166. tr_type == DWC2_TRANSACTION_ALL) &&
  1167. !list_empty(&hsotg->periodic_sched_assigned))
  1168. dwc2_process_periodic_channels(hsotg);
  1169. /* Process host channels associated with non-periodic transfers */
  1170. if (tr_type == DWC2_TRANSACTION_NON_PERIODIC ||
  1171. tr_type == DWC2_TRANSACTION_ALL) {
  1172. if (!list_empty(&hsotg->non_periodic_sched_active)) {
  1173. dwc2_process_non_periodic_channels(hsotg);
  1174. } else {
  1175. /*
  1176. * Ensure NP Tx FIFO empty interrupt is disabled when
  1177. * there are no non-periodic transfers to process
  1178. */
  1179. u32 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
  1180. gintmsk &= ~GINTSTS_NPTXFEMP;
  1181. dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
  1182. }
  1183. }
  1184. }
  1185. static void dwc2_conn_id_status_change(struct work_struct *work)
  1186. {
  1187. struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
  1188. wf_otg);
  1189. u32 count = 0;
  1190. u32 gotgctl;
  1191. unsigned long flags;
  1192. dev_dbg(hsotg->dev, "%s()\n", __func__);
  1193. gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
  1194. dev_dbg(hsotg->dev, "gotgctl=%0x\n", gotgctl);
  1195. dev_dbg(hsotg->dev, "gotgctl.b.conidsts=%d\n",
  1196. !!(gotgctl & GOTGCTL_CONID_B));
  1197. /* B-Device connector (Device Mode) */
  1198. if (gotgctl & GOTGCTL_CONID_B) {
  1199. /* Wait for switch to device mode */
  1200. dev_dbg(hsotg->dev, "connId B\n");
  1201. while (!dwc2_is_device_mode(hsotg)) {
  1202. dev_info(hsotg->dev,
  1203. "Waiting for Peripheral Mode, Mode=%s\n",
  1204. dwc2_is_host_mode(hsotg) ? "Host" :
  1205. "Peripheral");
  1206. usleep_range(20000, 40000);
  1207. if (++count > 250)
  1208. break;
  1209. }
  1210. if (count > 250)
  1211. dev_err(hsotg->dev,
  1212. "Connection id status change timed out\n");
  1213. hsotg->op_state = OTG_STATE_B_PERIPHERAL;
  1214. dwc2_core_init(hsotg, false, -1);
  1215. dwc2_enable_global_interrupts(hsotg);
  1216. spin_lock_irqsave(&hsotg->lock, flags);
  1217. dwc2_hsotg_core_init_disconnected(hsotg, false);
  1218. spin_unlock_irqrestore(&hsotg->lock, flags);
  1219. dwc2_hsotg_core_connect(hsotg);
  1220. } else {
  1221. /* A-Device connector (Host Mode) */
  1222. dev_dbg(hsotg->dev, "connId A\n");
  1223. while (!dwc2_is_host_mode(hsotg)) {
  1224. dev_info(hsotg->dev, "Waiting for Host Mode, Mode=%s\n",
  1225. dwc2_is_host_mode(hsotg) ?
  1226. "Host" : "Peripheral");
  1227. usleep_range(20000, 40000);
  1228. if (++count > 250)
  1229. break;
  1230. }
  1231. if (count > 250)
  1232. dev_err(hsotg->dev,
  1233. "Connection id status change timed out\n");
  1234. spin_lock_irqsave(&hsotg->lock, flags);
  1235. dwc2_hsotg_disconnect(hsotg);
  1236. spin_unlock_irqrestore(&hsotg->lock, flags);
  1237. hsotg->op_state = OTG_STATE_A_HOST;
  1238. /* Initialize the Core for Host mode */
  1239. dwc2_core_init(hsotg, false, -1);
  1240. dwc2_enable_global_interrupts(hsotg);
  1241. dwc2_hcd_start(hsotg);
  1242. }
  1243. }
  1244. static void dwc2_wakeup_detected(unsigned long data)
  1245. {
  1246. struct dwc2_hsotg *hsotg = (struct dwc2_hsotg *)data;
  1247. u32 hprt0;
  1248. dev_dbg(hsotg->dev, "%s()\n", __func__);
  1249. /*
  1250. * Clear the Resume after 70ms. (Need 20 ms minimum. Use 70 ms
  1251. * so that OPT tests pass with all PHYs.)
  1252. */
  1253. hprt0 = dwc2_read_hprt0(hsotg);
  1254. dev_dbg(hsotg->dev, "Resume: HPRT0=%0x\n", hprt0);
  1255. hprt0 &= ~HPRT0_RES;
  1256. dwc2_writel(hprt0, hsotg->regs + HPRT0);
  1257. dev_dbg(hsotg->dev, "Clear Resume: HPRT0=%0x\n",
  1258. dwc2_readl(hsotg->regs + HPRT0));
  1259. dwc2_hcd_rem_wakeup(hsotg);
  1260. hsotg->bus_suspended = 0;
  1261. /* Change to L0 state */
  1262. hsotg->lx_state = DWC2_L0;
  1263. }
  1264. static int dwc2_host_is_b_hnp_enabled(struct dwc2_hsotg *hsotg)
  1265. {
  1266. struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
  1267. return hcd->self.b_hnp_enable;
  1268. }
  1269. /* Must NOT be called with interrupt disabled or spinlock held */
  1270. static void dwc2_port_suspend(struct dwc2_hsotg *hsotg, u16 windex)
  1271. {
  1272. unsigned long flags;
  1273. u32 hprt0;
  1274. u32 pcgctl;
  1275. u32 gotgctl;
  1276. dev_dbg(hsotg->dev, "%s()\n", __func__);
  1277. spin_lock_irqsave(&hsotg->lock, flags);
  1278. if (windex == hsotg->otg_port && dwc2_host_is_b_hnp_enabled(hsotg)) {
  1279. gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
  1280. gotgctl |= GOTGCTL_HSTSETHNPEN;
  1281. dwc2_writel(gotgctl, hsotg->regs + GOTGCTL);
  1282. hsotg->op_state = OTG_STATE_A_SUSPEND;
  1283. }
  1284. hprt0 = dwc2_read_hprt0(hsotg);
  1285. hprt0 |= HPRT0_SUSP;
  1286. dwc2_writel(hprt0, hsotg->regs + HPRT0);
  1287. hsotg->bus_suspended = 1;
  1288. /*
  1289. * If hibernation is supported, Phy clock will be suspended
  1290. * after registers are backuped.
  1291. */
  1292. if (!hsotg->core_params->hibernation) {
  1293. /* Suspend the Phy Clock */
  1294. pcgctl = dwc2_readl(hsotg->regs + PCGCTL);
  1295. pcgctl |= PCGCTL_STOPPCLK;
  1296. dwc2_writel(pcgctl, hsotg->regs + PCGCTL);
  1297. udelay(10);
  1298. }
  1299. /* For HNP the bus must be suspended for at least 200ms */
  1300. if (dwc2_host_is_b_hnp_enabled(hsotg)) {
  1301. pcgctl = dwc2_readl(hsotg->regs + PCGCTL);
  1302. pcgctl &= ~PCGCTL_STOPPCLK;
  1303. dwc2_writel(pcgctl, hsotg->regs + PCGCTL);
  1304. spin_unlock_irqrestore(&hsotg->lock, flags);
  1305. usleep_range(200000, 250000);
  1306. } else {
  1307. spin_unlock_irqrestore(&hsotg->lock, flags);
  1308. }
  1309. }
  1310. /* Must NOT be called with interrupt disabled or spinlock held */
  1311. static void dwc2_port_resume(struct dwc2_hsotg *hsotg)
  1312. {
  1313. unsigned long flags;
  1314. u32 hprt0;
  1315. u32 pcgctl;
  1316. spin_lock_irqsave(&hsotg->lock, flags);
  1317. /*
  1318. * If hibernation is supported, Phy clock is already resumed
  1319. * after registers restore.
  1320. */
  1321. if (!hsotg->core_params->hibernation) {
  1322. pcgctl = dwc2_readl(hsotg->regs + PCGCTL);
  1323. pcgctl &= ~PCGCTL_STOPPCLK;
  1324. dwc2_writel(pcgctl, hsotg->regs + PCGCTL);
  1325. spin_unlock_irqrestore(&hsotg->lock, flags);
  1326. usleep_range(20000, 40000);
  1327. spin_lock_irqsave(&hsotg->lock, flags);
  1328. }
  1329. hprt0 = dwc2_read_hprt0(hsotg);
  1330. hprt0 |= HPRT0_RES;
  1331. hprt0 &= ~HPRT0_SUSP;
  1332. dwc2_writel(hprt0, hsotg->regs + HPRT0);
  1333. spin_unlock_irqrestore(&hsotg->lock, flags);
  1334. msleep(USB_RESUME_TIMEOUT);
  1335. spin_lock_irqsave(&hsotg->lock, flags);
  1336. hprt0 = dwc2_read_hprt0(hsotg);
  1337. hprt0 &= ~(HPRT0_RES | HPRT0_SUSP);
  1338. dwc2_writel(hprt0, hsotg->regs + HPRT0);
  1339. hsotg->bus_suspended = 0;
  1340. spin_unlock_irqrestore(&hsotg->lock, flags);
  1341. }
  1342. /* Handles hub class-specific requests */
  1343. static int dwc2_hcd_hub_control(struct dwc2_hsotg *hsotg, u16 typereq,
  1344. u16 wvalue, u16 windex, char *buf, u16 wlength)
  1345. {
  1346. struct usb_hub_descriptor *hub_desc;
  1347. int retval = 0;
  1348. u32 hprt0;
  1349. u32 port_status;
  1350. u32 speed;
  1351. u32 pcgctl;
  1352. switch (typereq) {
  1353. case ClearHubFeature:
  1354. dev_dbg(hsotg->dev, "ClearHubFeature %1xh\n", wvalue);
  1355. switch (wvalue) {
  1356. case C_HUB_LOCAL_POWER:
  1357. case C_HUB_OVER_CURRENT:
  1358. /* Nothing required here */
  1359. break;
  1360. default:
  1361. retval = -EINVAL;
  1362. dev_err(hsotg->dev,
  1363. "ClearHubFeature request %1xh unknown\n",
  1364. wvalue);
  1365. }
  1366. break;
  1367. case ClearPortFeature:
  1368. if (wvalue != USB_PORT_FEAT_L1)
  1369. if (!windex || windex > 1)
  1370. goto error;
  1371. switch (wvalue) {
  1372. case USB_PORT_FEAT_ENABLE:
  1373. dev_dbg(hsotg->dev,
  1374. "ClearPortFeature USB_PORT_FEAT_ENABLE\n");
  1375. hprt0 = dwc2_read_hprt0(hsotg);
  1376. hprt0 |= HPRT0_ENA;
  1377. dwc2_writel(hprt0, hsotg->regs + HPRT0);
  1378. break;
  1379. case USB_PORT_FEAT_SUSPEND:
  1380. dev_dbg(hsotg->dev,
  1381. "ClearPortFeature USB_PORT_FEAT_SUSPEND\n");
  1382. if (hsotg->bus_suspended)
  1383. dwc2_port_resume(hsotg);
  1384. break;
  1385. case USB_PORT_FEAT_POWER:
  1386. dev_dbg(hsotg->dev,
  1387. "ClearPortFeature USB_PORT_FEAT_POWER\n");
  1388. hprt0 = dwc2_read_hprt0(hsotg);
  1389. hprt0 &= ~HPRT0_PWR;
  1390. dwc2_writel(hprt0, hsotg->regs + HPRT0);
  1391. break;
  1392. case USB_PORT_FEAT_INDICATOR:
  1393. dev_dbg(hsotg->dev,
  1394. "ClearPortFeature USB_PORT_FEAT_INDICATOR\n");
  1395. /* Port indicator not supported */
  1396. break;
  1397. case USB_PORT_FEAT_C_CONNECTION:
  1398. /*
  1399. * Clears driver's internal Connect Status Change flag
  1400. */
  1401. dev_dbg(hsotg->dev,
  1402. "ClearPortFeature USB_PORT_FEAT_C_CONNECTION\n");
  1403. hsotg->flags.b.port_connect_status_change = 0;
  1404. break;
  1405. case USB_PORT_FEAT_C_RESET:
  1406. /* Clears driver's internal Port Reset Change flag */
  1407. dev_dbg(hsotg->dev,
  1408. "ClearPortFeature USB_PORT_FEAT_C_RESET\n");
  1409. hsotg->flags.b.port_reset_change = 0;
  1410. break;
  1411. case USB_PORT_FEAT_C_ENABLE:
  1412. /*
  1413. * Clears the driver's internal Port Enable/Disable
  1414. * Change flag
  1415. */
  1416. dev_dbg(hsotg->dev,
  1417. "ClearPortFeature USB_PORT_FEAT_C_ENABLE\n");
  1418. hsotg->flags.b.port_enable_change = 0;
  1419. break;
  1420. case USB_PORT_FEAT_C_SUSPEND:
  1421. /*
  1422. * Clears the driver's internal Port Suspend Change
  1423. * flag, which is set when resume signaling on the host
  1424. * port is complete
  1425. */
  1426. dev_dbg(hsotg->dev,
  1427. "ClearPortFeature USB_PORT_FEAT_C_SUSPEND\n");
  1428. hsotg->flags.b.port_suspend_change = 0;
  1429. break;
  1430. case USB_PORT_FEAT_C_PORT_L1:
  1431. dev_dbg(hsotg->dev,
  1432. "ClearPortFeature USB_PORT_FEAT_C_PORT_L1\n");
  1433. hsotg->flags.b.port_l1_change = 0;
  1434. break;
  1435. case USB_PORT_FEAT_C_OVER_CURRENT:
  1436. dev_dbg(hsotg->dev,
  1437. "ClearPortFeature USB_PORT_FEAT_C_OVER_CURRENT\n");
  1438. hsotg->flags.b.port_over_current_change = 0;
  1439. break;
  1440. default:
  1441. retval = -EINVAL;
  1442. dev_err(hsotg->dev,
  1443. "ClearPortFeature request %1xh unknown or unsupported\n",
  1444. wvalue);
  1445. }
  1446. break;
  1447. case GetHubDescriptor:
  1448. dev_dbg(hsotg->dev, "GetHubDescriptor\n");
  1449. hub_desc = (struct usb_hub_descriptor *)buf;
  1450. hub_desc->bDescLength = 9;
  1451. hub_desc->bDescriptorType = USB_DT_HUB;
  1452. hub_desc->bNbrPorts = 1;
  1453. hub_desc->wHubCharacteristics =
  1454. cpu_to_le16(HUB_CHAR_COMMON_LPSM |
  1455. HUB_CHAR_INDV_PORT_OCPM);
  1456. hub_desc->bPwrOn2PwrGood = 1;
  1457. hub_desc->bHubContrCurrent = 0;
  1458. hub_desc->u.hs.DeviceRemovable[0] = 0;
  1459. hub_desc->u.hs.DeviceRemovable[1] = 0xff;
  1460. break;
  1461. case GetHubStatus:
  1462. dev_dbg(hsotg->dev, "GetHubStatus\n");
  1463. memset(buf, 0, 4);
  1464. break;
  1465. case GetPortStatus:
  1466. dev_vdbg(hsotg->dev,
  1467. "GetPortStatus wIndex=0x%04x flags=0x%08x\n", windex,
  1468. hsotg->flags.d32);
  1469. if (!windex || windex > 1)
  1470. goto error;
  1471. port_status = 0;
  1472. if (hsotg->flags.b.port_connect_status_change)
  1473. port_status |= USB_PORT_STAT_C_CONNECTION << 16;
  1474. if (hsotg->flags.b.port_enable_change)
  1475. port_status |= USB_PORT_STAT_C_ENABLE << 16;
  1476. if (hsotg->flags.b.port_suspend_change)
  1477. port_status |= USB_PORT_STAT_C_SUSPEND << 16;
  1478. if (hsotg->flags.b.port_l1_change)
  1479. port_status |= USB_PORT_STAT_C_L1 << 16;
  1480. if (hsotg->flags.b.port_reset_change)
  1481. port_status |= USB_PORT_STAT_C_RESET << 16;
  1482. if (hsotg->flags.b.port_over_current_change) {
  1483. dev_warn(hsotg->dev, "Overcurrent change detected\n");
  1484. port_status |= USB_PORT_STAT_C_OVERCURRENT << 16;
  1485. }
  1486. if (!hsotg->flags.b.port_connect_status) {
  1487. /*
  1488. * The port is disconnected, which means the core is
  1489. * either in device mode or it soon will be. Just
  1490. * return 0's for the remainder of the port status
  1491. * since the port register can't be read if the core
  1492. * is in device mode.
  1493. */
  1494. *(__le32 *)buf = cpu_to_le32(port_status);
  1495. break;
  1496. }
  1497. hprt0 = dwc2_readl(hsotg->regs + HPRT0);
  1498. dev_vdbg(hsotg->dev, " HPRT0: 0x%08x\n", hprt0);
  1499. if (hprt0 & HPRT0_CONNSTS)
  1500. port_status |= USB_PORT_STAT_CONNECTION;
  1501. if (hprt0 & HPRT0_ENA)
  1502. port_status |= USB_PORT_STAT_ENABLE;
  1503. if (hprt0 & HPRT0_SUSP)
  1504. port_status |= USB_PORT_STAT_SUSPEND;
  1505. if (hprt0 & HPRT0_OVRCURRACT)
  1506. port_status |= USB_PORT_STAT_OVERCURRENT;
  1507. if (hprt0 & HPRT0_RST)
  1508. port_status |= USB_PORT_STAT_RESET;
  1509. if (hprt0 & HPRT0_PWR)
  1510. port_status |= USB_PORT_STAT_POWER;
  1511. speed = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
  1512. if (speed == HPRT0_SPD_HIGH_SPEED)
  1513. port_status |= USB_PORT_STAT_HIGH_SPEED;
  1514. else if (speed == HPRT0_SPD_LOW_SPEED)
  1515. port_status |= USB_PORT_STAT_LOW_SPEED;
  1516. if (hprt0 & HPRT0_TSTCTL_MASK)
  1517. port_status |= USB_PORT_STAT_TEST;
  1518. /* USB_PORT_FEAT_INDICATOR unsupported always 0 */
  1519. dev_vdbg(hsotg->dev, "port_status=%08x\n", port_status);
  1520. *(__le32 *)buf = cpu_to_le32(port_status);
  1521. break;
  1522. case SetHubFeature:
  1523. dev_dbg(hsotg->dev, "SetHubFeature\n");
  1524. /* No HUB features supported */
  1525. break;
  1526. case SetPortFeature:
  1527. dev_dbg(hsotg->dev, "SetPortFeature\n");
  1528. if (wvalue != USB_PORT_FEAT_TEST && (!windex || windex > 1))
  1529. goto error;
  1530. if (!hsotg->flags.b.port_connect_status) {
  1531. /*
  1532. * The port is disconnected, which means the core is
  1533. * either in device mode or it soon will be. Just
  1534. * return without doing anything since the port
  1535. * register can't be written if the core is in device
  1536. * mode.
  1537. */
  1538. break;
  1539. }
  1540. switch (wvalue) {
  1541. case USB_PORT_FEAT_SUSPEND:
  1542. dev_dbg(hsotg->dev,
  1543. "SetPortFeature - USB_PORT_FEAT_SUSPEND\n");
  1544. if (windex != hsotg->otg_port)
  1545. goto error;
  1546. dwc2_port_suspend(hsotg, windex);
  1547. break;
  1548. case USB_PORT_FEAT_POWER:
  1549. dev_dbg(hsotg->dev,
  1550. "SetPortFeature - USB_PORT_FEAT_POWER\n");
  1551. hprt0 = dwc2_read_hprt0(hsotg);
  1552. hprt0 |= HPRT0_PWR;
  1553. dwc2_writel(hprt0, hsotg->regs + HPRT0);
  1554. break;
  1555. case USB_PORT_FEAT_RESET:
  1556. hprt0 = dwc2_read_hprt0(hsotg);
  1557. dev_dbg(hsotg->dev,
  1558. "SetPortFeature - USB_PORT_FEAT_RESET\n");
  1559. pcgctl = dwc2_readl(hsotg->regs + PCGCTL);
  1560. pcgctl &= ~(PCGCTL_ENBL_SLEEP_GATING | PCGCTL_STOPPCLK);
  1561. dwc2_writel(pcgctl, hsotg->regs + PCGCTL);
  1562. /* ??? Original driver does this */
  1563. dwc2_writel(0, hsotg->regs + PCGCTL);
  1564. hprt0 = dwc2_read_hprt0(hsotg);
  1565. /* Clear suspend bit if resetting from suspend state */
  1566. hprt0 &= ~HPRT0_SUSP;
  1567. /*
  1568. * When B-Host the Port reset bit is set in the Start
  1569. * HCD Callback function, so that the reset is started
  1570. * within 1ms of the HNP success interrupt
  1571. */
  1572. if (!dwc2_hcd_is_b_host(hsotg)) {
  1573. hprt0 |= HPRT0_PWR | HPRT0_RST;
  1574. dev_dbg(hsotg->dev,
  1575. "In host mode, hprt0=%08x\n", hprt0);
  1576. dwc2_writel(hprt0, hsotg->regs + HPRT0);
  1577. }
  1578. /* Clear reset bit in 10ms (FS/LS) or 50ms (HS) */
  1579. usleep_range(50000, 70000);
  1580. hprt0 &= ~HPRT0_RST;
  1581. dwc2_writel(hprt0, hsotg->regs + HPRT0);
  1582. hsotg->lx_state = DWC2_L0; /* Now back to On state */
  1583. break;
  1584. case USB_PORT_FEAT_INDICATOR:
  1585. dev_dbg(hsotg->dev,
  1586. "SetPortFeature - USB_PORT_FEAT_INDICATOR\n");
  1587. /* Not supported */
  1588. break;
  1589. case USB_PORT_FEAT_TEST:
  1590. hprt0 = dwc2_read_hprt0(hsotg);
  1591. dev_dbg(hsotg->dev,
  1592. "SetPortFeature - USB_PORT_FEAT_TEST\n");
  1593. hprt0 &= ~HPRT0_TSTCTL_MASK;
  1594. hprt0 |= (windex >> 8) << HPRT0_TSTCTL_SHIFT;
  1595. dwc2_writel(hprt0, hsotg->regs + HPRT0);
  1596. break;
  1597. default:
  1598. retval = -EINVAL;
  1599. dev_err(hsotg->dev,
  1600. "SetPortFeature %1xh unknown or unsupported\n",
  1601. wvalue);
  1602. break;
  1603. }
  1604. break;
  1605. default:
  1606. error:
  1607. retval = -EINVAL;
  1608. dev_dbg(hsotg->dev,
  1609. "Unknown hub control request: %1xh wIndex: %1xh wValue: %1xh\n",
  1610. typereq, windex, wvalue);
  1611. break;
  1612. }
  1613. return retval;
  1614. }
  1615. static int dwc2_hcd_is_status_changed(struct dwc2_hsotg *hsotg, int port)
  1616. {
  1617. int retval;
  1618. if (port != 1)
  1619. return -EINVAL;
  1620. retval = (hsotg->flags.b.port_connect_status_change ||
  1621. hsotg->flags.b.port_reset_change ||
  1622. hsotg->flags.b.port_enable_change ||
  1623. hsotg->flags.b.port_suspend_change ||
  1624. hsotg->flags.b.port_over_current_change);
  1625. if (retval) {
  1626. dev_dbg(hsotg->dev,
  1627. "DWC OTG HCD HUB STATUS DATA: Root port status changed\n");
  1628. dev_dbg(hsotg->dev, " port_connect_status_change: %d\n",
  1629. hsotg->flags.b.port_connect_status_change);
  1630. dev_dbg(hsotg->dev, " port_reset_change: %d\n",
  1631. hsotg->flags.b.port_reset_change);
  1632. dev_dbg(hsotg->dev, " port_enable_change: %d\n",
  1633. hsotg->flags.b.port_enable_change);
  1634. dev_dbg(hsotg->dev, " port_suspend_change: %d\n",
  1635. hsotg->flags.b.port_suspend_change);
  1636. dev_dbg(hsotg->dev, " port_over_current_change: %d\n",
  1637. hsotg->flags.b.port_over_current_change);
  1638. }
  1639. return retval;
  1640. }
  1641. int dwc2_hcd_get_frame_number(struct dwc2_hsotg *hsotg)
  1642. {
  1643. u32 hfnum = dwc2_readl(hsotg->regs + HFNUM);
  1644. #ifdef DWC2_DEBUG_SOF
  1645. dev_vdbg(hsotg->dev, "DWC OTG HCD GET FRAME NUMBER %d\n",
  1646. (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT);
  1647. #endif
  1648. return (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT;
  1649. }
  1650. int dwc2_hcd_is_b_host(struct dwc2_hsotg *hsotg)
  1651. {
  1652. return hsotg->op_state == OTG_STATE_B_HOST;
  1653. }
  1654. static struct dwc2_hcd_urb *dwc2_hcd_urb_alloc(struct dwc2_hsotg *hsotg,
  1655. int iso_desc_count,
  1656. gfp_t mem_flags)
  1657. {
  1658. struct dwc2_hcd_urb *urb;
  1659. u32 size = sizeof(*urb) + iso_desc_count *
  1660. sizeof(struct dwc2_hcd_iso_packet_desc);
  1661. urb = kzalloc(size, mem_flags);
  1662. if (urb)
  1663. urb->packet_count = iso_desc_count;
  1664. return urb;
  1665. }
  1666. static void dwc2_hcd_urb_set_pipeinfo(struct dwc2_hsotg *hsotg,
  1667. struct dwc2_hcd_urb *urb, u8 dev_addr,
  1668. u8 ep_num, u8 ep_type, u8 ep_dir, u16 mps)
  1669. {
  1670. if (dbg_perio() ||
  1671. ep_type == USB_ENDPOINT_XFER_BULK ||
  1672. ep_type == USB_ENDPOINT_XFER_CONTROL)
  1673. dev_vdbg(hsotg->dev,
  1674. "addr=%d, ep_num=%d, ep_dir=%1x, ep_type=%1x, mps=%d\n",
  1675. dev_addr, ep_num, ep_dir, ep_type, mps);
  1676. urb->pipe_info.dev_addr = dev_addr;
  1677. urb->pipe_info.ep_num = ep_num;
  1678. urb->pipe_info.pipe_type = ep_type;
  1679. urb->pipe_info.pipe_dir = ep_dir;
  1680. urb->pipe_info.mps = mps;
  1681. }
  1682. /*
  1683. * NOTE: This function will be removed once the peripheral controller code
  1684. * is integrated and the driver is stable
  1685. */
  1686. void dwc2_hcd_dump_state(struct dwc2_hsotg *hsotg)
  1687. {
  1688. #ifdef DEBUG
  1689. struct dwc2_host_chan *chan;
  1690. struct dwc2_hcd_urb *urb;
  1691. struct dwc2_qtd *qtd;
  1692. int num_channels;
  1693. u32 np_tx_status;
  1694. u32 p_tx_status;
  1695. int i;
  1696. num_channels = hsotg->core_params->host_channels;
  1697. dev_dbg(hsotg->dev, "\n");
  1698. dev_dbg(hsotg->dev,
  1699. "************************************************************\n");
  1700. dev_dbg(hsotg->dev, "HCD State:\n");
  1701. dev_dbg(hsotg->dev, " Num channels: %d\n", num_channels);
  1702. for (i = 0; i < num_channels; i++) {
  1703. chan = hsotg->hc_ptr_array[i];
  1704. dev_dbg(hsotg->dev, " Channel %d:\n", i);
  1705. dev_dbg(hsotg->dev,
  1706. " dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
  1707. chan->dev_addr, chan->ep_num, chan->ep_is_in);
  1708. dev_dbg(hsotg->dev, " speed: %d\n", chan->speed);
  1709. dev_dbg(hsotg->dev, " ep_type: %d\n", chan->ep_type);
  1710. dev_dbg(hsotg->dev, " max_packet: %d\n", chan->max_packet);
  1711. dev_dbg(hsotg->dev, " data_pid_start: %d\n",
  1712. chan->data_pid_start);
  1713. dev_dbg(hsotg->dev, " multi_count: %d\n", chan->multi_count);
  1714. dev_dbg(hsotg->dev, " xfer_started: %d\n",
  1715. chan->xfer_started);
  1716. dev_dbg(hsotg->dev, " xfer_buf: %p\n", chan->xfer_buf);
  1717. dev_dbg(hsotg->dev, " xfer_dma: %08lx\n",
  1718. (unsigned long)chan->xfer_dma);
  1719. dev_dbg(hsotg->dev, " xfer_len: %d\n", chan->xfer_len);
  1720. dev_dbg(hsotg->dev, " xfer_count: %d\n", chan->xfer_count);
  1721. dev_dbg(hsotg->dev, " halt_on_queue: %d\n",
  1722. chan->halt_on_queue);
  1723. dev_dbg(hsotg->dev, " halt_pending: %d\n",
  1724. chan->halt_pending);
  1725. dev_dbg(hsotg->dev, " halt_status: %d\n", chan->halt_status);
  1726. dev_dbg(hsotg->dev, " do_split: %d\n", chan->do_split);
  1727. dev_dbg(hsotg->dev, " complete_split: %d\n",
  1728. chan->complete_split);
  1729. dev_dbg(hsotg->dev, " hub_addr: %d\n", chan->hub_addr);
  1730. dev_dbg(hsotg->dev, " hub_port: %d\n", chan->hub_port);
  1731. dev_dbg(hsotg->dev, " xact_pos: %d\n", chan->xact_pos);
  1732. dev_dbg(hsotg->dev, " requests: %d\n", chan->requests);
  1733. dev_dbg(hsotg->dev, " qh: %p\n", chan->qh);
  1734. if (chan->xfer_started) {
  1735. u32 hfnum, hcchar, hctsiz, hcint, hcintmsk;
  1736. hfnum = dwc2_readl(hsotg->regs + HFNUM);
  1737. hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
  1738. hctsiz = dwc2_readl(hsotg->regs + HCTSIZ(i));
  1739. hcint = dwc2_readl(hsotg->regs + HCINT(i));
  1740. hcintmsk = dwc2_readl(hsotg->regs + HCINTMSK(i));
  1741. dev_dbg(hsotg->dev, " hfnum: 0x%08x\n", hfnum);
  1742. dev_dbg(hsotg->dev, " hcchar: 0x%08x\n", hcchar);
  1743. dev_dbg(hsotg->dev, " hctsiz: 0x%08x\n", hctsiz);
  1744. dev_dbg(hsotg->dev, " hcint: 0x%08x\n", hcint);
  1745. dev_dbg(hsotg->dev, " hcintmsk: 0x%08x\n", hcintmsk);
  1746. }
  1747. if (!(chan->xfer_started && chan->qh))
  1748. continue;
  1749. list_for_each_entry(qtd, &chan->qh->qtd_list, qtd_list_entry) {
  1750. if (!qtd->in_process)
  1751. break;
  1752. urb = qtd->urb;
  1753. dev_dbg(hsotg->dev, " URB Info:\n");
  1754. dev_dbg(hsotg->dev, " qtd: %p, urb: %p\n",
  1755. qtd, urb);
  1756. if (urb) {
  1757. dev_dbg(hsotg->dev,
  1758. " Dev: %d, EP: %d %s\n",
  1759. dwc2_hcd_get_dev_addr(&urb->pipe_info),
  1760. dwc2_hcd_get_ep_num(&urb->pipe_info),
  1761. dwc2_hcd_is_pipe_in(&urb->pipe_info) ?
  1762. "IN" : "OUT");
  1763. dev_dbg(hsotg->dev,
  1764. " Max packet size: %d\n",
  1765. dwc2_hcd_get_mps(&urb->pipe_info));
  1766. dev_dbg(hsotg->dev,
  1767. " transfer_buffer: %p\n",
  1768. urb->buf);
  1769. dev_dbg(hsotg->dev,
  1770. " transfer_dma: %08lx\n",
  1771. (unsigned long)urb->dma);
  1772. dev_dbg(hsotg->dev,
  1773. " transfer_buffer_length: %d\n",
  1774. urb->length);
  1775. dev_dbg(hsotg->dev, " actual_length: %d\n",
  1776. urb->actual_length);
  1777. }
  1778. }
  1779. }
  1780. dev_dbg(hsotg->dev, " non_periodic_channels: %d\n",
  1781. hsotg->non_periodic_channels);
  1782. dev_dbg(hsotg->dev, " periodic_channels: %d\n",
  1783. hsotg->periodic_channels);
  1784. dev_dbg(hsotg->dev, " periodic_usecs: %d\n", hsotg->periodic_usecs);
  1785. np_tx_status = dwc2_readl(hsotg->regs + GNPTXSTS);
  1786. dev_dbg(hsotg->dev, " NP Tx Req Queue Space Avail: %d\n",
  1787. (np_tx_status & TXSTS_QSPCAVAIL_MASK) >> TXSTS_QSPCAVAIL_SHIFT);
  1788. dev_dbg(hsotg->dev, " NP Tx FIFO Space Avail: %d\n",
  1789. (np_tx_status & TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT);
  1790. p_tx_status = dwc2_readl(hsotg->regs + HPTXSTS);
  1791. dev_dbg(hsotg->dev, " P Tx Req Queue Space Avail: %d\n",
  1792. (p_tx_status & TXSTS_QSPCAVAIL_MASK) >> TXSTS_QSPCAVAIL_SHIFT);
  1793. dev_dbg(hsotg->dev, " P Tx FIFO Space Avail: %d\n",
  1794. (p_tx_status & TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT);
  1795. dwc2_hcd_dump_frrem(hsotg);
  1796. dwc2_dump_global_registers(hsotg);
  1797. dwc2_dump_host_registers(hsotg);
  1798. dev_dbg(hsotg->dev,
  1799. "************************************************************\n");
  1800. dev_dbg(hsotg->dev, "\n");
  1801. #endif
  1802. }
  1803. /*
  1804. * NOTE: This function will be removed once the peripheral controller code
  1805. * is integrated and the driver is stable
  1806. */
  1807. void dwc2_hcd_dump_frrem(struct dwc2_hsotg *hsotg)
  1808. {
  1809. #ifdef DWC2_DUMP_FRREM
  1810. dev_dbg(hsotg->dev, "Frame remaining at SOF:\n");
  1811. dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
  1812. hsotg->frrem_samples, hsotg->frrem_accum,
  1813. hsotg->frrem_samples > 0 ?
  1814. hsotg->frrem_accum / hsotg->frrem_samples : 0);
  1815. dev_dbg(hsotg->dev, "\n");
  1816. dev_dbg(hsotg->dev, "Frame remaining at start_transfer (uframe 7):\n");
  1817. dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
  1818. hsotg->hfnum_7_samples,
  1819. hsotg->hfnum_7_frrem_accum,
  1820. hsotg->hfnum_7_samples > 0 ?
  1821. hsotg->hfnum_7_frrem_accum / hsotg->hfnum_7_samples : 0);
  1822. dev_dbg(hsotg->dev, "Frame remaining at start_transfer (uframe 0):\n");
  1823. dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
  1824. hsotg->hfnum_0_samples,
  1825. hsotg->hfnum_0_frrem_accum,
  1826. hsotg->hfnum_0_samples > 0 ?
  1827. hsotg->hfnum_0_frrem_accum / hsotg->hfnum_0_samples : 0);
  1828. dev_dbg(hsotg->dev, "Frame remaining at start_transfer (uframe 1-6):\n");
  1829. dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
  1830. hsotg->hfnum_other_samples,
  1831. hsotg->hfnum_other_frrem_accum,
  1832. hsotg->hfnum_other_samples > 0 ?
  1833. hsotg->hfnum_other_frrem_accum / hsotg->hfnum_other_samples :
  1834. 0);
  1835. dev_dbg(hsotg->dev, "\n");
  1836. dev_dbg(hsotg->dev, "Frame remaining at sample point A (uframe 7):\n");
  1837. dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
  1838. hsotg->hfnum_7_samples_a, hsotg->hfnum_7_frrem_accum_a,
  1839. hsotg->hfnum_7_samples_a > 0 ?
  1840. hsotg->hfnum_7_frrem_accum_a / hsotg->hfnum_7_samples_a : 0);
  1841. dev_dbg(hsotg->dev, "Frame remaining at sample point A (uframe 0):\n");
  1842. dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
  1843. hsotg->hfnum_0_samples_a, hsotg->hfnum_0_frrem_accum_a,
  1844. hsotg->hfnum_0_samples_a > 0 ?
  1845. hsotg->hfnum_0_frrem_accum_a / hsotg->hfnum_0_samples_a : 0);
  1846. dev_dbg(hsotg->dev, "Frame remaining at sample point A (uframe 1-6):\n");
  1847. dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
  1848. hsotg->hfnum_other_samples_a, hsotg->hfnum_other_frrem_accum_a,
  1849. hsotg->hfnum_other_samples_a > 0 ?
  1850. hsotg->hfnum_other_frrem_accum_a / hsotg->hfnum_other_samples_a
  1851. : 0);
  1852. dev_dbg(hsotg->dev, "\n");
  1853. dev_dbg(hsotg->dev, "Frame remaining at sample point B (uframe 7):\n");
  1854. dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
  1855. hsotg->hfnum_7_samples_b, hsotg->hfnum_7_frrem_accum_b,
  1856. hsotg->hfnum_7_samples_b > 0 ?
  1857. hsotg->hfnum_7_frrem_accum_b / hsotg->hfnum_7_samples_b : 0);
  1858. dev_dbg(hsotg->dev, "Frame remaining at sample point B (uframe 0):\n");
  1859. dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
  1860. hsotg->hfnum_0_samples_b, hsotg->hfnum_0_frrem_accum_b,
  1861. (hsotg->hfnum_0_samples_b > 0) ?
  1862. hsotg->hfnum_0_frrem_accum_b / hsotg->hfnum_0_samples_b : 0);
  1863. dev_dbg(hsotg->dev, "Frame remaining at sample point B (uframe 1-6):\n");
  1864. dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
  1865. hsotg->hfnum_other_samples_b, hsotg->hfnum_other_frrem_accum_b,
  1866. (hsotg->hfnum_other_samples_b > 0) ?
  1867. hsotg->hfnum_other_frrem_accum_b / hsotg->hfnum_other_samples_b
  1868. : 0);
  1869. #endif
  1870. }
  1871. struct wrapper_priv_data {
  1872. struct dwc2_hsotg *hsotg;
  1873. };
  1874. /* Gets the dwc2_hsotg from a usb_hcd */
  1875. static struct dwc2_hsotg *dwc2_hcd_to_hsotg(struct usb_hcd *hcd)
  1876. {
  1877. struct wrapper_priv_data *p;
  1878. p = (struct wrapper_priv_data *) &hcd->hcd_priv;
  1879. return p->hsotg;
  1880. }
  1881. static int _dwc2_hcd_start(struct usb_hcd *hcd);
  1882. void dwc2_host_start(struct dwc2_hsotg *hsotg)
  1883. {
  1884. struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
  1885. hcd->self.is_b_host = dwc2_hcd_is_b_host(hsotg);
  1886. _dwc2_hcd_start(hcd);
  1887. }
  1888. void dwc2_host_disconnect(struct dwc2_hsotg *hsotg)
  1889. {
  1890. struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
  1891. hcd->self.is_b_host = 0;
  1892. }
  1893. void dwc2_host_hub_info(struct dwc2_hsotg *hsotg, void *context, int *hub_addr,
  1894. int *hub_port)
  1895. {
  1896. struct urb *urb = context;
  1897. if (urb->dev->tt)
  1898. *hub_addr = urb->dev->tt->hub->devnum;
  1899. else
  1900. *hub_addr = 0;
  1901. *hub_port = urb->dev->ttport;
  1902. }
  1903. int dwc2_host_get_speed(struct dwc2_hsotg *hsotg, void *context)
  1904. {
  1905. struct urb *urb = context;
  1906. return urb->dev->speed;
  1907. }
  1908. static void dwc2_allocate_bus_bandwidth(struct usb_hcd *hcd, u16 bw,
  1909. struct urb *urb)
  1910. {
  1911. struct usb_bus *bus = hcd_to_bus(hcd);
  1912. if (urb->interval)
  1913. bus->bandwidth_allocated += bw / urb->interval;
  1914. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
  1915. bus->bandwidth_isoc_reqs++;
  1916. else
  1917. bus->bandwidth_int_reqs++;
  1918. }
  1919. static void dwc2_free_bus_bandwidth(struct usb_hcd *hcd, u16 bw,
  1920. struct urb *urb)
  1921. {
  1922. struct usb_bus *bus = hcd_to_bus(hcd);
  1923. if (urb->interval)
  1924. bus->bandwidth_allocated -= bw / urb->interval;
  1925. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
  1926. bus->bandwidth_isoc_reqs--;
  1927. else
  1928. bus->bandwidth_int_reqs--;
  1929. }
  1930. /*
  1931. * Sets the final status of an URB and returns it to the upper layer. Any
  1932. * required cleanup of the URB is performed.
  1933. *
  1934. * Must be called with interrupt disabled and spinlock held
  1935. */
  1936. void dwc2_host_complete(struct dwc2_hsotg *hsotg, struct dwc2_qtd *qtd,
  1937. int status)
  1938. {
  1939. struct urb *urb;
  1940. int i;
  1941. if (!qtd) {
  1942. dev_dbg(hsotg->dev, "## %s: qtd is NULL ##\n", __func__);
  1943. return;
  1944. }
  1945. if (!qtd->urb) {
  1946. dev_dbg(hsotg->dev, "## %s: qtd->urb is NULL ##\n", __func__);
  1947. return;
  1948. }
  1949. urb = qtd->urb->priv;
  1950. if (!urb) {
  1951. dev_dbg(hsotg->dev, "## %s: urb->priv is NULL ##\n", __func__);
  1952. return;
  1953. }
  1954. urb->actual_length = dwc2_hcd_urb_get_actual_length(qtd->urb);
  1955. if (dbg_urb(urb))
  1956. dev_vdbg(hsotg->dev,
  1957. "%s: urb %p device %d ep %d-%s status %d actual %d\n",
  1958. __func__, urb, usb_pipedevice(urb->pipe),
  1959. usb_pipeendpoint(urb->pipe),
  1960. usb_pipein(urb->pipe) ? "IN" : "OUT", status,
  1961. urb->actual_length);
  1962. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  1963. urb->error_count = dwc2_hcd_urb_get_error_count(qtd->urb);
  1964. for (i = 0; i < urb->number_of_packets; ++i) {
  1965. urb->iso_frame_desc[i].actual_length =
  1966. dwc2_hcd_urb_get_iso_desc_actual_length(
  1967. qtd->urb, i);
  1968. urb->iso_frame_desc[i].status =
  1969. dwc2_hcd_urb_get_iso_desc_status(qtd->urb, i);
  1970. }
  1971. }
  1972. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS && dbg_perio()) {
  1973. for (i = 0; i < urb->number_of_packets; i++)
  1974. dev_vdbg(hsotg->dev, " ISO Desc %d status %d\n",
  1975. i, urb->iso_frame_desc[i].status);
  1976. }
  1977. urb->status = status;
  1978. if (!status) {
  1979. if ((urb->transfer_flags & URB_SHORT_NOT_OK) &&
  1980. urb->actual_length < urb->transfer_buffer_length)
  1981. urb->status = -EREMOTEIO;
  1982. }
  1983. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS ||
  1984. usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
  1985. struct usb_host_endpoint *ep = urb->ep;
  1986. if (ep)
  1987. dwc2_free_bus_bandwidth(dwc2_hsotg_to_hcd(hsotg),
  1988. dwc2_hcd_get_ep_bandwidth(hsotg, ep),
  1989. urb);
  1990. }
  1991. usb_hcd_unlink_urb_from_ep(dwc2_hsotg_to_hcd(hsotg), urb);
  1992. urb->hcpriv = NULL;
  1993. kfree(qtd->urb);
  1994. qtd->urb = NULL;
  1995. spin_unlock(&hsotg->lock);
  1996. usb_hcd_giveback_urb(dwc2_hsotg_to_hcd(hsotg), urb, status);
  1997. spin_lock(&hsotg->lock);
  1998. }
  1999. /*
  2000. * Work queue function for starting the HCD when A-Cable is connected
  2001. */
  2002. static void dwc2_hcd_start_func(struct work_struct *work)
  2003. {
  2004. struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
  2005. start_work.work);
  2006. dev_dbg(hsotg->dev, "%s() %p\n", __func__, hsotg);
  2007. dwc2_host_start(hsotg);
  2008. }
  2009. /*
  2010. * Reset work queue function
  2011. */
  2012. static void dwc2_hcd_reset_func(struct work_struct *work)
  2013. {
  2014. struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
  2015. reset_work.work);
  2016. u32 hprt0;
  2017. dev_dbg(hsotg->dev, "USB RESET function called\n");
  2018. hprt0 = dwc2_read_hprt0(hsotg);
  2019. hprt0 &= ~HPRT0_RST;
  2020. dwc2_writel(hprt0, hsotg->regs + HPRT0);
  2021. hsotg->flags.b.port_reset_change = 1;
  2022. }
  2023. /*
  2024. * =========================================================================
  2025. * Linux HC Driver Functions
  2026. * =========================================================================
  2027. */
  2028. /*
  2029. * Initializes the DWC_otg controller and its root hub and prepares it for host
  2030. * mode operation. Activates the root port. Returns 0 on success and a negative
  2031. * error code on failure.
  2032. */
  2033. static int _dwc2_hcd_start(struct usb_hcd *hcd)
  2034. {
  2035. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  2036. struct usb_bus *bus = hcd_to_bus(hcd);
  2037. unsigned long flags;
  2038. dev_dbg(hsotg->dev, "DWC OTG HCD START\n");
  2039. spin_lock_irqsave(&hsotg->lock, flags);
  2040. hsotg->lx_state = DWC2_L0;
  2041. hcd->state = HC_STATE_RUNNING;
  2042. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  2043. if (dwc2_is_device_mode(hsotg)) {
  2044. spin_unlock_irqrestore(&hsotg->lock, flags);
  2045. return 0; /* why 0 ?? */
  2046. }
  2047. dwc2_hcd_reinit(hsotg);
  2048. /* Initialize and connect root hub if one is not already attached */
  2049. if (bus->root_hub) {
  2050. dev_dbg(hsotg->dev, "DWC OTG HCD Has Root Hub\n");
  2051. /* Inform the HUB driver to resume */
  2052. usb_hcd_resume_root_hub(hcd);
  2053. }
  2054. spin_unlock_irqrestore(&hsotg->lock, flags);
  2055. return 0;
  2056. }
  2057. /*
  2058. * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
  2059. * stopped.
  2060. */
  2061. static void _dwc2_hcd_stop(struct usb_hcd *hcd)
  2062. {
  2063. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  2064. unsigned long flags;
  2065. /* Turn off all host-specific interrupts */
  2066. dwc2_disable_host_interrupts(hsotg);
  2067. /* Wait for interrupt processing to finish */
  2068. synchronize_irq(hcd->irq);
  2069. spin_lock_irqsave(&hsotg->lock, flags);
  2070. /* Ensure hcd is disconnected */
  2071. dwc2_hcd_disconnect(hsotg);
  2072. dwc2_hcd_stop(hsotg);
  2073. hsotg->lx_state = DWC2_L3;
  2074. hcd->state = HC_STATE_HALT;
  2075. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  2076. spin_unlock_irqrestore(&hsotg->lock, flags);
  2077. usleep_range(1000, 3000);
  2078. }
  2079. static int _dwc2_hcd_suspend(struct usb_hcd *hcd)
  2080. {
  2081. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  2082. unsigned long flags;
  2083. int ret = 0;
  2084. u32 hprt0;
  2085. spin_lock_irqsave(&hsotg->lock, flags);
  2086. if (hsotg->lx_state != DWC2_L0)
  2087. goto unlock;
  2088. if (!HCD_HW_ACCESSIBLE(hcd))
  2089. goto unlock;
  2090. if (!hsotg->core_params->hibernation)
  2091. goto skip_power_saving;
  2092. /*
  2093. * Drive USB suspend and disable port Power
  2094. * if usb bus is not suspended.
  2095. */
  2096. if (!hsotg->bus_suspended) {
  2097. hprt0 = dwc2_read_hprt0(hsotg);
  2098. hprt0 |= HPRT0_SUSP;
  2099. hprt0 &= ~HPRT0_PWR;
  2100. dwc2_writel(hprt0, hsotg->regs + HPRT0);
  2101. }
  2102. /* Enter hibernation */
  2103. ret = dwc2_enter_hibernation(hsotg);
  2104. if (ret) {
  2105. if (ret != -ENOTSUPP)
  2106. dev_err(hsotg->dev,
  2107. "enter hibernation failed\n");
  2108. goto skip_power_saving;
  2109. }
  2110. /* Ask phy to be suspended */
  2111. if (!IS_ERR_OR_NULL(hsotg->uphy)) {
  2112. spin_unlock_irqrestore(&hsotg->lock, flags);
  2113. usb_phy_set_suspend(hsotg->uphy, true);
  2114. spin_lock_irqsave(&hsotg->lock, flags);
  2115. }
  2116. /* After entering hibernation, hardware is no more accessible */
  2117. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  2118. skip_power_saving:
  2119. hsotg->lx_state = DWC2_L2;
  2120. unlock:
  2121. spin_unlock_irqrestore(&hsotg->lock, flags);
  2122. return ret;
  2123. }
  2124. static int _dwc2_hcd_resume(struct usb_hcd *hcd)
  2125. {
  2126. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  2127. unsigned long flags;
  2128. int ret = 0;
  2129. spin_lock_irqsave(&hsotg->lock, flags);
  2130. if (hsotg->lx_state != DWC2_L2)
  2131. goto unlock;
  2132. if (!hsotg->core_params->hibernation) {
  2133. hsotg->lx_state = DWC2_L0;
  2134. goto unlock;
  2135. }
  2136. /*
  2137. * Set HW accessible bit before powering on the controller
  2138. * since an interrupt may rise.
  2139. */
  2140. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  2141. /*
  2142. * Enable power if not already done.
  2143. * This must not be spinlocked since duration
  2144. * of this call is unknown.
  2145. */
  2146. if (!IS_ERR_OR_NULL(hsotg->uphy)) {
  2147. spin_unlock_irqrestore(&hsotg->lock, flags);
  2148. usb_phy_set_suspend(hsotg->uphy, false);
  2149. spin_lock_irqsave(&hsotg->lock, flags);
  2150. }
  2151. /* Exit hibernation */
  2152. ret = dwc2_exit_hibernation(hsotg, true);
  2153. if (ret && (ret != -ENOTSUPP))
  2154. dev_err(hsotg->dev, "exit hibernation failed\n");
  2155. hsotg->lx_state = DWC2_L0;
  2156. spin_unlock_irqrestore(&hsotg->lock, flags);
  2157. if (hsotg->bus_suspended) {
  2158. spin_lock_irqsave(&hsotg->lock, flags);
  2159. hsotg->flags.b.port_suspend_change = 1;
  2160. spin_unlock_irqrestore(&hsotg->lock, flags);
  2161. dwc2_port_resume(hsotg);
  2162. } else {
  2163. /* Wait for controller to correctly update D+/D- level */
  2164. usleep_range(3000, 5000);
  2165. /*
  2166. * Clear Port Enable and Port Status changes.
  2167. * Enable Port Power.
  2168. */
  2169. dwc2_writel(HPRT0_PWR | HPRT0_CONNDET |
  2170. HPRT0_ENACHG, hsotg->regs + HPRT0);
  2171. /* Wait for controller to detect Port Connect */
  2172. usleep_range(5000, 7000);
  2173. }
  2174. return ret;
  2175. unlock:
  2176. spin_unlock_irqrestore(&hsotg->lock, flags);
  2177. return ret;
  2178. }
  2179. /* Returns the current frame number */
  2180. static int _dwc2_hcd_get_frame_number(struct usb_hcd *hcd)
  2181. {
  2182. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  2183. return dwc2_hcd_get_frame_number(hsotg);
  2184. }
  2185. static void dwc2_dump_urb_info(struct usb_hcd *hcd, struct urb *urb,
  2186. char *fn_name)
  2187. {
  2188. #ifdef VERBOSE_DEBUG
  2189. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  2190. char *pipetype;
  2191. char *speed;
  2192. dev_vdbg(hsotg->dev, "%s, urb %p\n", fn_name, urb);
  2193. dev_vdbg(hsotg->dev, " Device address: %d\n",
  2194. usb_pipedevice(urb->pipe));
  2195. dev_vdbg(hsotg->dev, " Endpoint: %d, %s\n",
  2196. usb_pipeendpoint(urb->pipe),
  2197. usb_pipein(urb->pipe) ? "IN" : "OUT");
  2198. switch (usb_pipetype(urb->pipe)) {
  2199. case PIPE_CONTROL:
  2200. pipetype = "CONTROL";
  2201. break;
  2202. case PIPE_BULK:
  2203. pipetype = "BULK";
  2204. break;
  2205. case PIPE_INTERRUPT:
  2206. pipetype = "INTERRUPT";
  2207. break;
  2208. case PIPE_ISOCHRONOUS:
  2209. pipetype = "ISOCHRONOUS";
  2210. break;
  2211. default:
  2212. pipetype = "UNKNOWN";
  2213. break;
  2214. }
  2215. dev_vdbg(hsotg->dev, " Endpoint type: %s %s (%s)\n", pipetype,
  2216. usb_urb_dir_in(urb) ? "IN" : "OUT", usb_pipein(urb->pipe) ?
  2217. "IN" : "OUT");
  2218. switch (urb->dev->speed) {
  2219. case USB_SPEED_HIGH:
  2220. speed = "HIGH";
  2221. break;
  2222. case USB_SPEED_FULL:
  2223. speed = "FULL";
  2224. break;
  2225. case USB_SPEED_LOW:
  2226. speed = "LOW";
  2227. break;
  2228. default:
  2229. speed = "UNKNOWN";
  2230. break;
  2231. }
  2232. dev_vdbg(hsotg->dev, " Speed: %s\n", speed);
  2233. dev_vdbg(hsotg->dev, " Max packet size: %d\n",
  2234. usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)));
  2235. dev_vdbg(hsotg->dev, " Data buffer length: %d\n",
  2236. urb->transfer_buffer_length);
  2237. dev_vdbg(hsotg->dev, " Transfer buffer: %p, Transfer DMA: %08lx\n",
  2238. urb->transfer_buffer, (unsigned long)urb->transfer_dma);
  2239. dev_vdbg(hsotg->dev, " Setup buffer: %p, Setup DMA: %08lx\n",
  2240. urb->setup_packet, (unsigned long)urb->setup_dma);
  2241. dev_vdbg(hsotg->dev, " Interval: %d\n", urb->interval);
  2242. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  2243. int i;
  2244. for (i = 0; i < urb->number_of_packets; i++) {
  2245. dev_vdbg(hsotg->dev, " ISO Desc %d:\n", i);
  2246. dev_vdbg(hsotg->dev, " offset: %d, length %d\n",
  2247. urb->iso_frame_desc[i].offset,
  2248. urb->iso_frame_desc[i].length);
  2249. }
  2250. }
  2251. #endif
  2252. }
  2253. /*
  2254. * Starts processing a USB transfer request specified by a USB Request Block
  2255. * (URB). mem_flags indicates the type of memory allocation to use while
  2256. * processing this URB.
  2257. */
  2258. static int _dwc2_hcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
  2259. gfp_t mem_flags)
  2260. {
  2261. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  2262. struct usb_host_endpoint *ep = urb->ep;
  2263. struct dwc2_hcd_urb *dwc2_urb;
  2264. int i;
  2265. int retval;
  2266. int alloc_bandwidth = 0;
  2267. u8 ep_type = 0;
  2268. u32 tflags = 0;
  2269. void *buf;
  2270. unsigned long flags;
  2271. struct dwc2_qh *qh;
  2272. bool qh_allocated = false;
  2273. struct dwc2_qtd *qtd;
  2274. if (dbg_urb(urb)) {
  2275. dev_vdbg(hsotg->dev, "DWC OTG HCD URB Enqueue\n");
  2276. dwc2_dump_urb_info(hcd, urb, "urb_enqueue");
  2277. }
  2278. if (ep == NULL)
  2279. return -EINVAL;
  2280. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS ||
  2281. usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
  2282. spin_lock_irqsave(&hsotg->lock, flags);
  2283. if (!dwc2_hcd_is_bandwidth_allocated(hsotg, ep))
  2284. alloc_bandwidth = 1;
  2285. spin_unlock_irqrestore(&hsotg->lock, flags);
  2286. }
  2287. switch (usb_pipetype(urb->pipe)) {
  2288. case PIPE_CONTROL:
  2289. ep_type = USB_ENDPOINT_XFER_CONTROL;
  2290. break;
  2291. case PIPE_ISOCHRONOUS:
  2292. ep_type = USB_ENDPOINT_XFER_ISOC;
  2293. break;
  2294. case PIPE_BULK:
  2295. ep_type = USB_ENDPOINT_XFER_BULK;
  2296. break;
  2297. case PIPE_INTERRUPT:
  2298. ep_type = USB_ENDPOINT_XFER_INT;
  2299. break;
  2300. default:
  2301. dev_warn(hsotg->dev, "Wrong ep type\n");
  2302. }
  2303. dwc2_urb = dwc2_hcd_urb_alloc(hsotg, urb->number_of_packets,
  2304. mem_flags);
  2305. if (!dwc2_urb)
  2306. return -ENOMEM;
  2307. dwc2_hcd_urb_set_pipeinfo(hsotg, dwc2_urb, usb_pipedevice(urb->pipe),
  2308. usb_pipeendpoint(urb->pipe), ep_type,
  2309. usb_pipein(urb->pipe),
  2310. usb_maxpacket(urb->dev, urb->pipe,
  2311. !(usb_pipein(urb->pipe))));
  2312. buf = urb->transfer_buffer;
  2313. if (hcd->self.uses_dma) {
  2314. if (!buf && (urb->transfer_dma & 3)) {
  2315. dev_err(hsotg->dev,
  2316. "%s: unaligned transfer with no transfer_buffer",
  2317. __func__);
  2318. retval = -EINVAL;
  2319. goto fail0;
  2320. }
  2321. }
  2322. if (!(urb->transfer_flags & URB_NO_INTERRUPT))
  2323. tflags |= URB_GIVEBACK_ASAP;
  2324. if (urb->transfer_flags & URB_ZERO_PACKET)
  2325. tflags |= URB_SEND_ZERO_PACKET;
  2326. dwc2_urb->priv = urb;
  2327. dwc2_urb->buf = buf;
  2328. dwc2_urb->dma = urb->transfer_dma;
  2329. dwc2_urb->length = urb->transfer_buffer_length;
  2330. dwc2_urb->setup_packet = urb->setup_packet;
  2331. dwc2_urb->setup_dma = urb->setup_dma;
  2332. dwc2_urb->flags = tflags;
  2333. dwc2_urb->interval = urb->interval;
  2334. dwc2_urb->status = -EINPROGRESS;
  2335. for (i = 0; i < urb->number_of_packets; ++i)
  2336. dwc2_hcd_urb_set_iso_desc_params(dwc2_urb, i,
  2337. urb->iso_frame_desc[i].offset,
  2338. urb->iso_frame_desc[i].length);
  2339. urb->hcpriv = dwc2_urb;
  2340. qh = (struct dwc2_qh *) ep->hcpriv;
  2341. /* Create QH for the endpoint if it doesn't exist */
  2342. if (!qh) {
  2343. qh = dwc2_hcd_qh_create(hsotg, dwc2_urb, mem_flags);
  2344. if (!qh) {
  2345. retval = -ENOMEM;
  2346. goto fail0;
  2347. }
  2348. ep->hcpriv = qh;
  2349. qh_allocated = true;
  2350. }
  2351. qtd = kzalloc(sizeof(*qtd), mem_flags);
  2352. if (!qtd) {
  2353. retval = -ENOMEM;
  2354. goto fail1;
  2355. }
  2356. spin_lock_irqsave(&hsotg->lock, flags);
  2357. retval = usb_hcd_link_urb_to_ep(hcd, urb);
  2358. if (retval)
  2359. goto fail2;
  2360. retval = dwc2_hcd_urb_enqueue(hsotg, dwc2_urb, qh, qtd);
  2361. if (retval)
  2362. goto fail3;
  2363. if (alloc_bandwidth) {
  2364. dwc2_allocate_bus_bandwidth(hcd,
  2365. dwc2_hcd_get_ep_bandwidth(hsotg, ep),
  2366. urb);
  2367. }
  2368. spin_unlock_irqrestore(&hsotg->lock, flags);
  2369. return 0;
  2370. fail3:
  2371. dwc2_urb->priv = NULL;
  2372. usb_hcd_unlink_urb_from_ep(hcd, urb);
  2373. fail2:
  2374. spin_unlock_irqrestore(&hsotg->lock, flags);
  2375. urb->hcpriv = NULL;
  2376. kfree(qtd);
  2377. fail1:
  2378. if (qh_allocated) {
  2379. struct dwc2_qtd *qtd2, *qtd2_tmp;
  2380. ep->hcpriv = NULL;
  2381. dwc2_hcd_qh_unlink(hsotg, qh);
  2382. /* Free each QTD in the QH's QTD list */
  2383. list_for_each_entry_safe(qtd2, qtd2_tmp, &qh->qtd_list,
  2384. qtd_list_entry)
  2385. dwc2_hcd_qtd_unlink_and_free(hsotg, qtd2, qh);
  2386. dwc2_hcd_qh_free(hsotg, qh);
  2387. }
  2388. fail0:
  2389. kfree(dwc2_urb);
  2390. return retval;
  2391. }
  2392. /*
  2393. * Aborts/cancels a USB transfer request. Always returns 0 to indicate success.
  2394. */
  2395. static int _dwc2_hcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
  2396. int status)
  2397. {
  2398. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  2399. int rc;
  2400. unsigned long flags;
  2401. dev_dbg(hsotg->dev, "DWC OTG HCD URB Dequeue\n");
  2402. dwc2_dump_urb_info(hcd, urb, "urb_dequeue");
  2403. spin_lock_irqsave(&hsotg->lock, flags);
  2404. rc = usb_hcd_check_unlink_urb(hcd, urb, status);
  2405. if (rc)
  2406. goto out;
  2407. if (!urb->hcpriv) {
  2408. dev_dbg(hsotg->dev, "## urb->hcpriv is NULL ##\n");
  2409. goto out;
  2410. }
  2411. rc = dwc2_hcd_urb_dequeue(hsotg, urb->hcpriv);
  2412. usb_hcd_unlink_urb_from_ep(hcd, urb);
  2413. kfree(urb->hcpriv);
  2414. urb->hcpriv = NULL;
  2415. /* Higher layer software sets URB status */
  2416. spin_unlock(&hsotg->lock);
  2417. usb_hcd_giveback_urb(hcd, urb, status);
  2418. spin_lock(&hsotg->lock);
  2419. dev_dbg(hsotg->dev, "Called usb_hcd_giveback_urb()\n");
  2420. dev_dbg(hsotg->dev, " urb->status = %d\n", urb->status);
  2421. out:
  2422. spin_unlock_irqrestore(&hsotg->lock, flags);
  2423. return rc;
  2424. }
  2425. /*
  2426. * Frees resources in the DWC_otg controller related to a given endpoint. Also
  2427. * clears state in the HCD related to the endpoint. Any URBs for the endpoint
  2428. * must already be dequeued.
  2429. */
  2430. static void _dwc2_hcd_endpoint_disable(struct usb_hcd *hcd,
  2431. struct usb_host_endpoint *ep)
  2432. {
  2433. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  2434. dev_dbg(hsotg->dev,
  2435. "DWC OTG HCD EP DISABLE: bEndpointAddress=0x%02x, ep->hcpriv=%p\n",
  2436. ep->desc.bEndpointAddress, ep->hcpriv);
  2437. dwc2_hcd_endpoint_disable(hsotg, ep, 250);
  2438. }
  2439. /*
  2440. * Resets endpoint specific parameter values, in current version used to reset
  2441. * the data toggle (as a WA). This function can be called from usb_clear_halt
  2442. * routine.
  2443. */
  2444. static void _dwc2_hcd_endpoint_reset(struct usb_hcd *hcd,
  2445. struct usb_host_endpoint *ep)
  2446. {
  2447. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  2448. unsigned long flags;
  2449. dev_dbg(hsotg->dev,
  2450. "DWC OTG HCD EP RESET: bEndpointAddress=0x%02x\n",
  2451. ep->desc.bEndpointAddress);
  2452. spin_lock_irqsave(&hsotg->lock, flags);
  2453. dwc2_hcd_endpoint_reset(hsotg, ep);
  2454. spin_unlock_irqrestore(&hsotg->lock, flags);
  2455. }
  2456. /*
  2457. * Handles host mode interrupts for the DWC_otg controller. Returns IRQ_NONE if
  2458. * there was no interrupt to handle. Returns IRQ_HANDLED if there was a valid
  2459. * interrupt.
  2460. *
  2461. * This function is called by the USB core when an interrupt occurs
  2462. */
  2463. static irqreturn_t _dwc2_hcd_irq(struct usb_hcd *hcd)
  2464. {
  2465. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  2466. return dwc2_handle_hcd_intr(hsotg);
  2467. }
  2468. /*
  2469. * Creates Status Change bitmap for the root hub and root port. The bitmap is
  2470. * returned in buf. Bit 0 is the status change indicator for the root hub. Bit 1
  2471. * is the status change indicator for the single root port. Returns 1 if either
  2472. * change indicator is 1, otherwise returns 0.
  2473. */
  2474. static int _dwc2_hcd_hub_status_data(struct usb_hcd *hcd, char *buf)
  2475. {
  2476. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  2477. buf[0] = dwc2_hcd_is_status_changed(hsotg, 1) << 1;
  2478. return buf[0] != 0;
  2479. }
  2480. /* Handles hub class-specific requests */
  2481. static int _dwc2_hcd_hub_control(struct usb_hcd *hcd, u16 typereq, u16 wvalue,
  2482. u16 windex, char *buf, u16 wlength)
  2483. {
  2484. int retval = dwc2_hcd_hub_control(dwc2_hcd_to_hsotg(hcd), typereq,
  2485. wvalue, windex, buf, wlength);
  2486. return retval;
  2487. }
  2488. /* Handles hub TT buffer clear completions */
  2489. static void _dwc2_hcd_clear_tt_buffer_complete(struct usb_hcd *hcd,
  2490. struct usb_host_endpoint *ep)
  2491. {
  2492. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  2493. struct dwc2_qh *qh;
  2494. unsigned long flags;
  2495. qh = ep->hcpriv;
  2496. if (!qh)
  2497. return;
  2498. spin_lock_irqsave(&hsotg->lock, flags);
  2499. qh->tt_buffer_dirty = 0;
  2500. if (hsotg->flags.b.port_connect_status)
  2501. dwc2_hcd_queue_transactions(hsotg, DWC2_TRANSACTION_ALL);
  2502. spin_unlock_irqrestore(&hsotg->lock, flags);
  2503. }
  2504. static struct hc_driver dwc2_hc_driver = {
  2505. .description = "dwc2_hsotg",
  2506. .product_desc = "DWC OTG Controller",
  2507. .hcd_priv_size = sizeof(struct wrapper_priv_data),
  2508. .irq = _dwc2_hcd_irq,
  2509. .flags = HCD_MEMORY | HCD_USB2,
  2510. .start = _dwc2_hcd_start,
  2511. .stop = _dwc2_hcd_stop,
  2512. .urb_enqueue = _dwc2_hcd_urb_enqueue,
  2513. .urb_dequeue = _dwc2_hcd_urb_dequeue,
  2514. .endpoint_disable = _dwc2_hcd_endpoint_disable,
  2515. .endpoint_reset = _dwc2_hcd_endpoint_reset,
  2516. .get_frame_number = _dwc2_hcd_get_frame_number,
  2517. .hub_status_data = _dwc2_hcd_hub_status_data,
  2518. .hub_control = _dwc2_hcd_hub_control,
  2519. .clear_tt_buffer_complete = _dwc2_hcd_clear_tt_buffer_complete,
  2520. .bus_suspend = _dwc2_hcd_suspend,
  2521. .bus_resume = _dwc2_hcd_resume,
  2522. };
  2523. /*
  2524. * Frees secondary storage associated with the dwc2_hsotg structure contained
  2525. * in the struct usb_hcd field
  2526. */
  2527. static void dwc2_hcd_free(struct dwc2_hsotg *hsotg)
  2528. {
  2529. u32 ahbcfg;
  2530. u32 dctl;
  2531. int i;
  2532. dev_dbg(hsotg->dev, "DWC OTG HCD FREE\n");
  2533. /* Free memory for QH/QTD lists */
  2534. dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_inactive);
  2535. dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_active);
  2536. dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_inactive);
  2537. dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_ready);
  2538. dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_assigned);
  2539. dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_queued);
  2540. /* Free memory for the host channels */
  2541. for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  2542. struct dwc2_host_chan *chan = hsotg->hc_ptr_array[i];
  2543. if (chan != NULL) {
  2544. dev_dbg(hsotg->dev, "HCD Free channel #%i, chan=%p\n",
  2545. i, chan);
  2546. hsotg->hc_ptr_array[i] = NULL;
  2547. kfree(chan);
  2548. }
  2549. }
  2550. if (hsotg->core_params->dma_enable > 0) {
  2551. if (hsotg->status_buf) {
  2552. dma_free_coherent(hsotg->dev, DWC2_HCD_STATUS_BUF_SIZE,
  2553. hsotg->status_buf,
  2554. hsotg->status_buf_dma);
  2555. hsotg->status_buf = NULL;
  2556. }
  2557. } else {
  2558. kfree(hsotg->status_buf);
  2559. hsotg->status_buf = NULL;
  2560. }
  2561. ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
  2562. /* Disable all interrupts */
  2563. ahbcfg &= ~GAHBCFG_GLBL_INTR_EN;
  2564. dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
  2565. dwc2_writel(0, hsotg->regs + GINTMSK);
  2566. if (hsotg->hw_params.snpsid >= DWC2_CORE_REV_3_00a) {
  2567. dctl = dwc2_readl(hsotg->regs + DCTL);
  2568. dctl |= DCTL_SFTDISCON;
  2569. dwc2_writel(dctl, hsotg->regs + DCTL);
  2570. }
  2571. if (hsotg->wq_otg) {
  2572. if (!cancel_work_sync(&hsotg->wf_otg))
  2573. flush_workqueue(hsotg->wq_otg);
  2574. destroy_workqueue(hsotg->wq_otg);
  2575. }
  2576. del_timer(&hsotg->wkp_timer);
  2577. }
  2578. static void dwc2_hcd_release(struct dwc2_hsotg *hsotg)
  2579. {
  2580. /* Turn off all host-specific interrupts */
  2581. dwc2_disable_host_interrupts(hsotg);
  2582. dwc2_hcd_free(hsotg);
  2583. }
  2584. /*
  2585. * Initializes the HCD. This function allocates memory for and initializes the
  2586. * static parts of the usb_hcd and dwc2_hsotg structures. It also registers the
  2587. * USB bus with the core and calls the hc_driver->start() function. It returns
  2588. * a negative error on failure.
  2589. */
  2590. int dwc2_hcd_init(struct dwc2_hsotg *hsotg, int irq)
  2591. {
  2592. struct usb_hcd *hcd;
  2593. struct dwc2_host_chan *channel;
  2594. u32 hcfg;
  2595. int i, num_channels;
  2596. int retval;
  2597. if (usb_disabled())
  2598. return -ENODEV;
  2599. dev_dbg(hsotg->dev, "DWC OTG HCD INIT\n");
  2600. retval = -ENOMEM;
  2601. hcfg = dwc2_readl(hsotg->regs + HCFG);
  2602. dev_dbg(hsotg->dev, "hcfg=%08x\n", hcfg);
  2603. #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
  2604. hsotg->frame_num_array = kzalloc(sizeof(*hsotg->frame_num_array) *
  2605. FRAME_NUM_ARRAY_SIZE, GFP_KERNEL);
  2606. if (!hsotg->frame_num_array)
  2607. goto error1;
  2608. hsotg->last_frame_num_array = kzalloc(
  2609. sizeof(*hsotg->last_frame_num_array) *
  2610. FRAME_NUM_ARRAY_SIZE, GFP_KERNEL);
  2611. if (!hsotg->last_frame_num_array)
  2612. goto error1;
  2613. hsotg->last_frame_num = HFNUM_MAX_FRNUM;
  2614. #endif
  2615. /* Check if the bus driver or platform code has setup a dma_mask */
  2616. if (hsotg->core_params->dma_enable > 0 &&
  2617. hsotg->dev->dma_mask == NULL) {
  2618. dev_warn(hsotg->dev,
  2619. "dma_mask not set, disabling DMA\n");
  2620. hsotg->core_params->dma_enable = 0;
  2621. hsotg->core_params->dma_desc_enable = 0;
  2622. }
  2623. /* Set device flags indicating whether the HCD supports DMA */
  2624. if (hsotg->core_params->dma_enable > 0) {
  2625. if (dma_set_mask(hsotg->dev, DMA_BIT_MASK(32)) < 0)
  2626. dev_warn(hsotg->dev, "can't set DMA mask\n");
  2627. if (dma_set_coherent_mask(hsotg->dev, DMA_BIT_MASK(32)) < 0)
  2628. dev_warn(hsotg->dev, "can't set coherent DMA mask\n");
  2629. }
  2630. hcd = usb_create_hcd(&dwc2_hc_driver, hsotg->dev, dev_name(hsotg->dev));
  2631. if (!hcd)
  2632. goto error1;
  2633. if (hsotg->core_params->dma_enable <= 0)
  2634. hcd->self.uses_dma = 0;
  2635. hcd->has_tt = 1;
  2636. ((struct wrapper_priv_data *) &hcd->hcd_priv)->hsotg = hsotg;
  2637. hsotg->priv = hcd;
  2638. /*
  2639. * Disable the global interrupt until all the interrupt handlers are
  2640. * installed
  2641. */
  2642. dwc2_disable_global_interrupts(hsotg);
  2643. /* Initialize the DWC_otg core, and select the Phy type */
  2644. retval = dwc2_core_init(hsotg, true, irq);
  2645. if (retval)
  2646. goto error2;
  2647. /* Create new workqueue and init work */
  2648. retval = -ENOMEM;
  2649. hsotg->wq_otg = create_singlethread_workqueue("dwc2");
  2650. if (!hsotg->wq_otg) {
  2651. dev_err(hsotg->dev, "Failed to create workqueue\n");
  2652. goto error2;
  2653. }
  2654. INIT_WORK(&hsotg->wf_otg, dwc2_conn_id_status_change);
  2655. setup_timer(&hsotg->wkp_timer, dwc2_wakeup_detected,
  2656. (unsigned long)hsotg);
  2657. /* Initialize the non-periodic schedule */
  2658. INIT_LIST_HEAD(&hsotg->non_periodic_sched_inactive);
  2659. INIT_LIST_HEAD(&hsotg->non_periodic_sched_active);
  2660. /* Initialize the periodic schedule */
  2661. INIT_LIST_HEAD(&hsotg->periodic_sched_inactive);
  2662. INIT_LIST_HEAD(&hsotg->periodic_sched_ready);
  2663. INIT_LIST_HEAD(&hsotg->periodic_sched_assigned);
  2664. INIT_LIST_HEAD(&hsotg->periodic_sched_queued);
  2665. /*
  2666. * Create a host channel descriptor for each host channel implemented
  2667. * in the controller. Initialize the channel descriptor array.
  2668. */
  2669. INIT_LIST_HEAD(&hsotg->free_hc_list);
  2670. num_channels = hsotg->core_params->host_channels;
  2671. memset(&hsotg->hc_ptr_array[0], 0, sizeof(hsotg->hc_ptr_array));
  2672. for (i = 0; i < num_channels; i++) {
  2673. channel = kzalloc(sizeof(*channel), GFP_KERNEL);
  2674. if (channel == NULL)
  2675. goto error3;
  2676. channel->hc_num = i;
  2677. hsotg->hc_ptr_array[i] = channel;
  2678. }
  2679. if (hsotg->core_params->uframe_sched > 0)
  2680. dwc2_hcd_init_usecs(hsotg);
  2681. /* Initialize hsotg start work */
  2682. INIT_DELAYED_WORK(&hsotg->start_work, dwc2_hcd_start_func);
  2683. /* Initialize port reset work */
  2684. INIT_DELAYED_WORK(&hsotg->reset_work, dwc2_hcd_reset_func);
  2685. /*
  2686. * Allocate space for storing data on status transactions. Normally no
  2687. * data is sent, but this space acts as a bit bucket. This must be
  2688. * done after usb_add_hcd since that function allocates the DMA buffer
  2689. * pool.
  2690. */
  2691. if (hsotg->core_params->dma_enable > 0)
  2692. hsotg->status_buf = dma_alloc_coherent(hsotg->dev,
  2693. DWC2_HCD_STATUS_BUF_SIZE,
  2694. &hsotg->status_buf_dma, GFP_KERNEL);
  2695. else
  2696. hsotg->status_buf = kzalloc(DWC2_HCD_STATUS_BUF_SIZE,
  2697. GFP_KERNEL);
  2698. if (!hsotg->status_buf)
  2699. goto error3;
  2700. hsotg->otg_port = 1;
  2701. hsotg->frame_list = NULL;
  2702. hsotg->frame_list_dma = 0;
  2703. hsotg->periodic_qh_count = 0;
  2704. /* Initiate lx_state to L3 disconnected state */
  2705. hsotg->lx_state = DWC2_L3;
  2706. hcd->self.otg_port = hsotg->otg_port;
  2707. /* Don't support SG list at this point */
  2708. hcd->self.sg_tablesize = 0;
  2709. if (!IS_ERR_OR_NULL(hsotg->uphy))
  2710. otg_set_host(hsotg->uphy->otg, &hcd->self);
  2711. /*
  2712. * Finish generic HCD initialization and start the HCD. This function
  2713. * allocates the DMA buffer pool, registers the USB bus, requests the
  2714. * IRQ line, and calls hcd_start method.
  2715. */
  2716. retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
  2717. if (retval < 0)
  2718. goto error3;
  2719. device_wakeup_enable(hcd->self.controller);
  2720. dwc2_hcd_dump_state(hsotg);
  2721. dwc2_enable_global_interrupts(hsotg);
  2722. return 0;
  2723. error3:
  2724. dwc2_hcd_release(hsotg);
  2725. error2:
  2726. usb_put_hcd(hcd);
  2727. error1:
  2728. #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
  2729. kfree(hsotg->last_frame_num_array);
  2730. kfree(hsotg->frame_num_array);
  2731. #endif
  2732. dev_err(hsotg->dev, "%s() FAILED, returning %d\n", __func__, retval);
  2733. return retval;
  2734. }
  2735. /*
  2736. * Removes the HCD.
  2737. * Frees memory and resources associated with the HCD and deregisters the bus.
  2738. */
  2739. void dwc2_hcd_remove(struct dwc2_hsotg *hsotg)
  2740. {
  2741. struct usb_hcd *hcd;
  2742. dev_dbg(hsotg->dev, "DWC OTG HCD REMOVE\n");
  2743. hcd = dwc2_hsotg_to_hcd(hsotg);
  2744. dev_dbg(hsotg->dev, "hsotg->hcd = %p\n", hcd);
  2745. if (!hcd) {
  2746. dev_dbg(hsotg->dev, "%s: dwc2_hsotg_to_hcd(hsotg) NULL!\n",
  2747. __func__);
  2748. return;
  2749. }
  2750. if (!IS_ERR_OR_NULL(hsotg->uphy))
  2751. otg_set_host(hsotg->uphy->otg, NULL);
  2752. usb_remove_hcd(hcd);
  2753. hsotg->priv = NULL;
  2754. dwc2_hcd_release(hsotg);
  2755. usb_put_hcd(hcd);
  2756. #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
  2757. kfree(hsotg->last_frame_num_array);
  2758. kfree(hsotg->frame_num_array);
  2759. #endif
  2760. }