oxu210hp-hcd.c 99 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958
  1. /*
  2. * Copyright (c) 2008 Rodolfo Giometti <giometti@linux.it>
  3. * Copyright (c) 2008 Eurotech S.p.A. <info@eurtech.it>
  4. *
  5. * This code is *strongly* based on EHCI-HCD code by David Brownell since
  6. * the chip is a quasi-EHCI compatible.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  15. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  16. * for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software Foundation,
  20. * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <linux/module.h>
  23. #include <linux/pci.h>
  24. #include <linux/dmapool.h>
  25. #include <linux/kernel.h>
  26. #include <linux/delay.h>
  27. #include <linux/ioport.h>
  28. #include <linux/sched.h>
  29. #include <linux/slab.h>
  30. #include <linux/errno.h>
  31. #include <linux/init.h>
  32. #include <linux/timer.h>
  33. #include <linux/list.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/usb.h>
  36. #include <linux/usb/hcd.h>
  37. #include <linux/moduleparam.h>
  38. #include <linux/dma-mapping.h>
  39. #include <linux/io.h>
  40. #include <asm/irq.h>
  41. #include <asm/system.h>
  42. #include <asm/unaligned.h>
  43. #include <linux/irq.h>
  44. #include <linux/platform_device.h>
  45. #include "oxu210hp.h"
  46. #define DRIVER_VERSION "0.0.50"
  47. /*
  48. * Main defines
  49. */
  50. #define oxu_dbg(oxu, fmt, args...) \
  51. dev_dbg(oxu_to_hcd(oxu)->self.controller , fmt , ## args)
  52. #define oxu_err(oxu, fmt, args...) \
  53. dev_err(oxu_to_hcd(oxu)->self.controller , fmt , ## args)
  54. #define oxu_info(oxu, fmt, args...) \
  55. dev_info(oxu_to_hcd(oxu)->self.controller , fmt , ## args)
  56. static inline struct usb_hcd *oxu_to_hcd(struct oxu_hcd *oxu)
  57. {
  58. return container_of((void *) oxu, struct usb_hcd, hcd_priv);
  59. }
  60. static inline struct oxu_hcd *hcd_to_oxu(struct usb_hcd *hcd)
  61. {
  62. return (struct oxu_hcd *) (hcd->hcd_priv);
  63. }
  64. /*
  65. * Debug stuff
  66. */
  67. #undef OXU_URB_TRACE
  68. #undef OXU_VERBOSE_DEBUG
  69. #ifdef OXU_VERBOSE_DEBUG
  70. #define oxu_vdbg oxu_dbg
  71. #else
  72. #define oxu_vdbg(oxu, fmt, args...) /* Nop */
  73. #endif
  74. #ifdef DEBUG
  75. static int __attribute__((__unused__))
  76. dbg_status_buf(char *buf, unsigned len, const char *label, u32 status)
  77. {
  78. return scnprintf(buf, len, "%s%sstatus %04x%s%s%s%s%s%s%s%s%s%s",
  79. label, label[0] ? " " : "", status,
  80. (status & STS_ASS) ? " Async" : "",
  81. (status & STS_PSS) ? " Periodic" : "",
  82. (status & STS_RECL) ? " Recl" : "",
  83. (status & STS_HALT) ? " Halt" : "",
  84. (status & STS_IAA) ? " IAA" : "",
  85. (status & STS_FATAL) ? " FATAL" : "",
  86. (status & STS_FLR) ? " FLR" : "",
  87. (status & STS_PCD) ? " PCD" : "",
  88. (status & STS_ERR) ? " ERR" : "",
  89. (status & STS_INT) ? " INT" : ""
  90. );
  91. }
  92. static int __attribute__((__unused__))
  93. dbg_intr_buf(char *buf, unsigned len, const char *label, u32 enable)
  94. {
  95. return scnprintf(buf, len, "%s%sintrenable %02x%s%s%s%s%s%s",
  96. label, label[0] ? " " : "", enable,
  97. (enable & STS_IAA) ? " IAA" : "",
  98. (enable & STS_FATAL) ? " FATAL" : "",
  99. (enable & STS_FLR) ? " FLR" : "",
  100. (enable & STS_PCD) ? " PCD" : "",
  101. (enable & STS_ERR) ? " ERR" : "",
  102. (enable & STS_INT) ? " INT" : ""
  103. );
  104. }
  105. static const char *const fls_strings[] =
  106. { "1024", "512", "256", "??" };
  107. static int dbg_command_buf(char *buf, unsigned len,
  108. const char *label, u32 command)
  109. {
  110. return scnprintf(buf, len,
  111. "%s%scommand %06x %s=%d ithresh=%d%s%s%s%s period=%s%s %s",
  112. label, label[0] ? " " : "", command,
  113. (command & CMD_PARK) ? "park" : "(park)",
  114. CMD_PARK_CNT(command),
  115. (command >> 16) & 0x3f,
  116. (command & CMD_LRESET) ? " LReset" : "",
  117. (command & CMD_IAAD) ? " IAAD" : "",
  118. (command & CMD_ASE) ? " Async" : "",
  119. (command & CMD_PSE) ? " Periodic" : "",
  120. fls_strings[(command >> 2) & 0x3],
  121. (command & CMD_RESET) ? " Reset" : "",
  122. (command & CMD_RUN) ? "RUN" : "HALT"
  123. );
  124. }
  125. static int dbg_port_buf(char *buf, unsigned len, const char *label,
  126. int port, u32 status)
  127. {
  128. char *sig;
  129. /* signaling state */
  130. switch (status & (3 << 10)) {
  131. case 0 << 10:
  132. sig = "se0";
  133. break;
  134. case 1 << 10:
  135. sig = "k"; /* low speed */
  136. break;
  137. case 2 << 10:
  138. sig = "j";
  139. break;
  140. default:
  141. sig = "?";
  142. break;
  143. }
  144. return scnprintf(buf, len,
  145. "%s%sport %d status %06x%s%s sig=%s%s%s%s%s%s%s%s%s%s",
  146. label, label[0] ? " " : "", port, status,
  147. (status & PORT_POWER) ? " POWER" : "",
  148. (status & PORT_OWNER) ? " OWNER" : "",
  149. sig,
  150. (status & PORT_RESET) ? " RESET" : "",
  151. (status & PORT_SUSPEND) ? " SUSPEND" : "",
  152. (status & PORT_RESUME) ? " RESUME" : "",
  153. (status & PORT_OCC) ? " OCC" : "",
  154. (status & PORT_OC) ? " OC" : "",
  155. (status & PORT_PEC) ? " PEC" : "",
  156. (status & PORT_PE) ? " PE" : "",
  157. (status & PORT_CSC) ? " CSC" : "",
  158. (status & PORT_CONNECT) ? " CONNECT" : ""
  159. );
  160. }
  161. #else
  162. static inline int __attribute__((__unused__))
  163. dbg_status_buf(char *buf, unsigned len, const char *label, u32 status)
  164. { return 0; }
  165. static inline int __attribute__((__unused__))
  166. dbg_command_buf(char *buf, unsigned len, const char *label, u32 command)
  167. { return 0; }
  168. static inline int __attribute__((__unused__))
  169. dbg_intr_buf(char *buf, unsigned len, const char *label, u32 enable)
  170. { return 0; }
  171. static inline int __attribute__((__unused__))
  172. dbg_port_buf(char *buf, unsigned len, const char *label, int port, u32 status)
  173. { return 0; }
  174. #endif /* DEBUG */
  175. /* functions have the "wrong" filename when they're output... */
  176. #define dbg_status(oxu, label, status) { \
  177. char _buf[80]; \
  178. dbg_status_buf(_buf, sizeof _buf, label, status); \
  179. oxu_dbg(oxu, "%s\n", _buf); \
  180. }
  181. #define dbg_cmd(oxu, label, command) { \
  182. char _buf[80]; \
  183. dbg_command_buf(_buf, sizeof _buf, label, command); \
  184. oxu_dbg(oxu, "%s\n", _buf); \
  185. }
  186. #define dbg_port(oxu, label, port, status) { \
  187. char _buf[80]; \
  188. dbg_port_buf(_buf, sizeof _buf, label, port, status); \
  189. oxu_dbg(oxu, "%s\n", _buf); \
  190. }
  191. /*
  192. * Module parameters
  193. */
  194. /* Initial IRQ latency: faster than hw default */
  195. static int log2_irq_thresh; /* 0 to 6 */
  196. module_param(log2_irq_thresh, int, S_IRUGO);
  197. MODULE_PARM_DESC(log2_irq_thresh, "log2 IRQ latency, 1-64 microframes");
  198. /* Initial park setting: slower than hw default */
  199. static unsigned park;
  200. module_param(park, uint, S_IRUGO);
  201. MODULE_PARM_DESC(park, "park setting; 1-3 back-to-back async packets");
  202. /* For flakey hardware, ignore overcurrent indicators */
  203. static bool ignore_oc;
  204. module_param(ignore_oc, bool, S_IRUGO);
  205. MODULE_PARM_DESC(ignore_oc, "ignore bogus hardware overcurrent indications");
  206. static void ehci_work(struct oxu_hcd *oxu);
  207. static int oxu_hub_control(struct usb_hcd *hcd,
  208. u16 typeReq, u16 wValue, u16 wIndex,
  209. char *buf, u16 wLength);
  210. /*
  211. * Local functions
  212. */
  213. /* Low level read/write registers functions */
  214. static inline u32 oxu_readl(void *base, u32 reg)
  215. {
  216. return readl(base + reg);
  217. }
  218. static inline void oxu_writel(void *base, u32 reg, u32 val)
  219. {
  220. writel(val, base + reg);
  221. }
  222. static inline void timer_action_done(struct oxu_hcd *oxu,
  223. enum ehci_timer_action action)
  224. {
  225. clear_bit(action, &oxu->actions);
  226. }
  227. static inline void timer_action(struct oxu_hcd *oxu,
  228. enum ehci_timer_action action)
  229. {
  230. if (!test_and_set_bit(action, &oxu->actions)) {
  231. unsigned long t;
  232. switch (action) {
  233. case TIMER_IAA_WATCHDOG:
  234. t = EHCI_IAA_JIFFIES;
  235. break;
  236. case TIMER_IO_WATCHDOG:
  237. t = EHCI_IO_JIFFIES;
  238. break;
  239. case TIMER_ASYNC_OFF:
  240. t = EHCI_ASYNC_JIFFIES;
  241. break;
  242. case TIMER_ASYNC_SHRINK:
  243. default:
  244. t = EHCI_SHRINK_JIFFIES;
  245. break;
  246. }
  247. t += jiffies;
  248. /* all timings except IAA watchdog can be overridden.
  249. * async queue SHRINK often precedes IAA. while it's ready
  250. * to go OFF neither can matter, and afterwards the IO
  251. * watchdog stops unless there's still periodic traffic.
  252. */
  253. if (action != TIMER_IAA_WATCHDOG
  254. && t > oxu->watchdog.expires
  255. && timer_pending(&oxu->watchdog))
  256. return;
  257. mod_timer(&oxu->watchdog, t);
  258. }
  259. }
  260. /*
  261. * handshake - spin reading hc until handshake completes or fails
  262. * @ptr: address of hc register to be read
  263. * @mask: bits to look at in result of read
  264. * @done: value of those bits when handshake succeeds
  265. * @usec: timeout in microseconds
  266. *
  267. * Returns negative errno, or zero on success
  268. *
  269. * Success happens when the "mask" bits have the specified value (hardware
  270. * handshake done). There are two failure modes: "usec" have passed (major
  271. * hardware flakeout), or the register reads as all-ones (hardware removed).
  272. *
  273. * That last failure should_only happen in cases like physical cardbus eject
  274. * before driver shutdown. But it also seems to be caused by bugs in cardbus
  275. * bridge shutdown: shutting down the bridge before the devices using it.
  276. */
  277. static int handshake(struct oxu_hcd *oxu, void __iomem *ptr,
  278. u32 mask, u32 done, int usec)
  279. {
  280. u32 result;
  281. do {
  282. result = readl(ptr);
  283. if (result == ~(u32)0) /* card removed */
  284. return -ENODEV;
  285. result &= mask;
  286. if (result == done)
  287. return 0;
  288. udelay(1);
  289. usec--;
  290. } while (usec > 0);
  291. return -ETIMEDOUT;
  292. }
  293. /* Force HC to halt state from unknown (EHCI spec section 2.3) */
  294. static int ehci_halt(struct oxu_hcd *oxu)
  295. {
  296. u32 temp = readl(&oxu->regs->status);
  297. /* disable any irqs left enabled by previous code */
  298. writel(0, &oxu->regs->intr_enable);
  299. if ((temp & STS_HALT) != 0)
  300. return 0;
  301. temp = readl(&oxu->regs->command);
  302. temp &= ~CMD_RUN;
  303. writel(temp, &oxu->regs->command);
  304. return handshake(oxu, &oxu->regs->status,
  305. STS_HALT, STS_HALT, 16 * 125);
  306. }
  307. /* Put TDI/ARC silicon into EHCI mode */
  308. static void tdi_reset(struct oxu_hcd *oxu)
  309. {
  310. u32 __iomem *reg_ptr;
  311. u32 tmp;
  312. reg_ptr = (u32 __iomem *)(((u8 __iomem *)oxu->regs) + 0x68);
  313. tmp = readl(reg_ptr);
  314. tmp |= 0x3;
  315. writel(tmp, reg_ptr);
  316. }
  317. /* Reset a non-running (STS_HALT == 1) controller */
  318. static int ehci_reset(struct oxu_hcd *oxu)
  319. {
  320. int retval;
  321. u32 command = readl(&oxu->regs->command);
  322. command |= CMD_RESET;
  323. dbg_cmd(oxu, "reset", command);
  324. writel(command, &oxu->regs->command);
  325. oxu_to_hcd(oxu)->state = HC_STATE_HALT;
  326. oxu->next_statechange = jiffies;
  327. retval = handshake(oxu, &oxu->regs->command,
  328. CMD_RESET, 0, 250 * 1000);
  329. if (retval)
  330. return retval;
  331. tdi_reset(oxu);
  332. return retval;
  333. }
  334. /* Idle the controller (from running) */
  335. static void ehci_quiesce(struct oxu_hcd *oxu)
  336. {
  337. u32 temp;
  338. #ifdef DEBUG
  339. if (!HC_IS_RUNNING(oxu_to_hcd(oxu)->state))
  340. BUG();
  341. #endif
  342. /* wait for any schedule enables/disables to take effect */
  343. temp = readl(&oxu->regs->command) << 10;
  344. temp &= STS_ASS | STS_PSS;
  345. if (handshake(oxu, &oxu->regs->status, STS_ASS | STS_PSS,
  346. temp, 16 * 125) != 0) {
  347. oxu_to_hcd(oxu)->state = HC_STATE_HALT;
  348. return;
  349. }
  350. /* then disable anything that's still active */
  351. temp = readl(&oxu->regs->command);
  352. temp &= ~(CMD_ASE | CMD_IAAD | CMD_PSE);
  353. writel(temp, &oxu->regs->command);
  354. /* hardware can take 16 microframes to turn off ... */
  355. if (handshake(oxu, &oxu->regs->status, STS_ASS | STS_PSS,
  356. 0, 16 * 125) != 0) {
  357. oxu_to_hcd(oxu)->state = HC_STATE_HALT;
  358. return;
  359. }
  360. }
  361. static int check_reset_complete(struct oxu_hcd *oxu, int index,
  362. u32 __iomem *status_reg, int port_status)
  363. {
  364. if (!(port_status & PORT_CONNECT)) {
  365. oxu->reset_done[index] = 0;
  366. return port_status;
  367. }
  368. /* if reset finished and it's still not enabled -- handoff */
  369. if (!(port_status & PORT_PE)) {
  370. oxu_dbg(oxu, "Failed to enable port %d on root hub TT\n",
  371. index+1);
  372. return port_status;
  373. } else
  374. oxu_dbg(oxu, "port %d high speed\n", index + 1);
  375. return port_status;
  376. }
  377. static void ehci_hub_descriptor(struct oxu_hcd *oxu,
  378. struct usb_hub_descriptor *desc)
  379. {
  380. int ports = HCS_N_PORTS(oxu->hcs_params);
  381. u16 temp;
  382. desc->bDescriptorType = 0x29;
  383. desc->bPwrOn2PwrGood = 10; /* oxu 1.0, 2.3.9 says 20ms max */
  384. desc->bHubContrCurrent = 0;
  385. desc->bNbrPorts = ports;
  386. temp = 1 + (ports / 8);
  387. desc->bDescLength = 7 + 2 * temp;
  388. /* ports removable, and usb 1.0 legacy PortPwrCtrlMask */
  389. memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
  390. memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
  391. temp = 0x0008; /* per-port overcurrent reporting */
  392. if (HCS_PPC(oxu->hcs_params))
  393. temp |= 0x0001; /* per-port power control */
  394. else
  395. temp |= 0x0002; /* no power switching */
  396. desc->wHubCharacteristics = (__force __u16)cpu_to_le16(temp);
  397. }
  398. /* Allocate an OXU210HP on-chip memory data buffer
  399. *
  400. * An on-chip memory data buffer is required for each OXU210HP USB transfer.
  401. * Each transfer descriptor has one or more on-chip memory data buffers.
  402. *
  403. * Data buffers are allocated from a fix sized pool of data blocks.
  404. * To minimise fragmentation and give reasonable memory utlisation,
  405. * data buffers are allocated with sizes the power of 2 multiples of
  406. * the block size, starting on an address a multiple of the allocated size.
  407. *
  408. * FIXME: callers of this function require a buffer to be allocated for
  409. * len=0. This is a waste of on-chip memory and should be fix. Then this
  410. * function should be changed to not allocate a buffer for len=0.
  411. */
  412. static int oxu_buf_alloc(struct oxu_hcd *oxu, struct ehci_qtd *qtd, int len)
  413. {
  414. int n_blocks; /* minium blocks needed to hold len */
  415. int a_blocks; /* blocks allocated */
  416. int i, j;
  417. /* Don't allocte bigger than supported */
  418. if (len > BUFFER_SIZE * BUFFER_NUM) {
  419. oxu_err(oxu, "buffer too big (%d)\n", len);
  420. return -ENOMEM;
  421. }
  422. spin_lock(&oxu->mem_lock);
  423. /* Number of blocks needed to hold len */
  424. n_blocks = (len + BUFFER_SIZE - 1) / BUFFER_SIZE;
  425. /* Round the number of blocks up to the power of 2 */
  426. for (a_blocks = 1; a_blocks < n_blocks; a_blocks <<= 1)
  427. ;
  428. /* Find a suitable available data buffer */
  429. for (i = 0; i < BUFFER_NUM;
  430. i += max(a_blocks, (int)oxu->db_used[i])) {
  431. /* Check all the required blocks are available */
  432. for (j = 0; j < a_blocks; j++)
  433. if (oxu->db_used[i + j])
  434. break;
  435. if (j != a_blocks)
  436. continue;
  437. /* Allocate blocks found! */
  438. qtd->buffer = (void *) &oxu->mem->db_pool[i];
  439. qtd->buffer_dma = virt_to_phys(qtd->buffer);
  440. qtd->qtd_buffer_len = BUFFER_SIZE * a_blocks;
  441. oxu->db_used[i] = a_blocks;
  442. spin_unlock(&oxu->mem_lock);
  443. return 0;
  444. }
  445. /* Failed */
  446. spin_unlock(&oxu->mem_lock);
  447. return -ENOMEM;
  448. }
  449. static void oxu_buf_free(struct oxu_hcd *oxu, struct ehci_qtd *qtd)
  450. {
  451. int index;
  452. spin_lock(&oxu->mem_lock);
  453. index = (qtd->buffer - (void *) &oxu->mem->db_pool[0])
  454. / BUFFER_SIZE;
  455. oxu->db_used[index] = 0;
  456. qtd->qtd_buffer_len = 0;
  457. qtd->buffer_dma = 0;
  458. qtd->buffer = NULL;
  459. spin_unlock(&oxu->mem_lock);
  460. }
  461. static inline void ehci_qtd_init(struct ehci_qtd *qtd, dma_addr_t dma)
  462. {
  463. memset(qtd, 0, sizeof *qtd);
  464. qtd->qtd_dma = dma;
  465. qtd->hw_token = cpu_to_le32(QTD_STS_HALT);
  466. qtd->hw_next = EHCI_LIST_END;
  467. qtd->hw_alt_next = EHCI_LIST_END;
  468. INIT_LIST_HEAD(&qtd->qtd_list);
  469. }
  470. static inline void oxu_qtd_free(struct oxu_hcd *oxu, struct ehci_qtd *qtd)
  471. {
  472. int index;
  473. if (qtd->buffer)
  474. oxu_buf_free(oxu, qtd);
  475. spin_lock(&oxu->mem_lock);
  476. index = qtd - &oxu->mem->qtd_pool[0];
  477. oxu->qtd_used[index] = 0;
  478. spin_unlock(&oxu->mem_lock);
  479. }
  480. static struct ehci_qtd *ehci_qtd_alloc(struct oxu_hcd *oxu)
  481. {
  482. int i;
  483. struct ehci_qtd *qtd = NULL;
  484. spin_lock(&oxu->mem_lock);
  485. for (i = 0; i < QTD_NUM; i++)
  486. if (!oxu->qtd_used[i])
  487. break;
  488. if (i < QTD_NUM) {
  489. qtd = (struct ehci_qtd *) &oxu->mem->qtd_pool[i];
  490. memset(qtd, 0, sizeof *qtd);
  491. qtd->hw_token = cpu_to_le32(QTD_STS_HALT);
  492. qtd->hw_next = EHCI_LIST_END;
  493. qtd->hw_alt_next = EHCI_LIST_END;
  494. INIT_LIST_HEAD(&qtd->qtd_list);
  495. qtd->qtd_dma = virt_to_phys(qtd);
  496. oxu->qtd_used[i] = 1;
  497. }
  498. spin_unlock(&oxu->mem_lock);
  499. return qtd;
  500. }
  501. static void oxu_qh_free(struct oxu_hcd *oxu, struct ehci_qh *qh)
  502. {
  503. int index;
  504. spin_lock(&oxu->mem_lock);
  505. index = qh - &oxu->mem->qh_pool[0];
  506. oxu->qh_used[index] = 0;
  507. spin_unlock(&oxu->mem_lock);
  508. }
  509. static void qh_destroy(struct kref *kref)
  510. {
  511. struct ehci_qh *qh = container_of(kref, struct ehci_qh, kref);
  512. struct oxu_hcd *oxu = qh->oxu;
  513. /* clean qtds first, and know this is not linked */
  514. if (!list_empty(&qh->qtd_list) || qh->qh_next.ptr) {
  515. oxu_dbg(oxu, "unused qh not empty!\n");
  516. BUG();
  517. }
  518. if (qh->dummy)
  519. oxu_qtd_free(oxu, qh->dummy);
  520. oxu_qh_free(oxu, qh);
  521. }
  522. static struct ehci_qh *oxu_qh_alloc(struct oxu_hcd *oxu)
  523. {
  524. int i;
  525. struct ehci_qh *qh = NULL;
  526. spin_lock(&oxu->mem_lock);
  527. for (i = 0; i < QHEAD_NUM; i++)
  528. if (!oxu->qh_used[i])
  529. break;
  530. if (i < QHEAD_NUM) {
  531. qh = (struct ehci_qh *) &oxu->mem->qh_pool[i];
  532. memset(qh, 0, sizeof *qh);
  533. kref_init(&qh->kref);
  534. qh->oxu = oxu;
  535. qh->qh_dma = virt_to_phys(qh);
  536. INIT_LIST_HEAD(&qh->qtd_list);
  537. /* dummy td enables safe urb queuing */
  538. qh->dummy = ehci_qtd_alloc(oxu);
  539. if (qh->dummy == NULL) {
  540. oxu_dbg(oxu, "no dummy td\n");
  541. oxu->qh_used[i] = 0;
  542. qh = NULL;
  543. goto unlock;
  544. }
  545. oxu->qh_used[i] = 1;
  546. }
  547. unlock:
  548. spin_unlock(&oxu->mem_lock);
  549. return qh;
  550. }
  551. /* to share a qh (cpu threads, or hc) */
  552. static inline struct ehci_qh *qh_get(struct ehci_qh *qh)
  553. {
  554. kref_get(&qh->kref);
  555. return qh;
  556. }
  557. static inline void qh_put(struct ehci_qh *qh)
  558. {
  559. kref_put(&qh->kref, qh_destroy);
  560. }
  561. static void oxu_murb_free(struct oxu_hcd *oxu, struct oxu_murb *murb)
  562. {
  563. int index;
  564. spin_lock(&oxu->mem_lock);
  565. index = murb - &oxu->murb_pool[0];
  566. oxu->murb_used[index] = 0;
  567. spin_unlock(&oxu->mem_lock);
  568. }
  569. static struct oxu_murb *oxu_murb_alloc(struct oxu_hcd *oxu)
  570. {
  571. int i;
  572. struct oxu_murb *murb = NULL;
  573. spin_lock(&oxu->mem_lock);
  574. for (i = 0; i < MURB_NUM; i++)
  575. if (!oxu->murb_used[i])
  576. break;
  577. if (i < MURB_NUM) {
  578. murb = &(oxu->murb_pool)[i];
  579. oxu->murb_used[i] = 1;
  580. }
  581. spin_unlock(&oxu->mem_lock);
  582. return murb;
  583. }
  584. /* The queue heads and transfer descriptors are managed from pools tied
  585. * to each of the "per device" structures.
  586. * This is the initialisation and cleanup code.
  587. */
  588. static void ehci_mem_cleanup(struct oxu_hcd *oxu)
  589. {
  590. kfree(oxu->murb_pool);
  591. oxu->murb_pool = NULL;
  592. if (oxu->async)
  593. qh_put(oxu->async);
  594. oxu->async = NULL;
  595. del_timer(&oxu->urb_timer);
  596. oxu->periodic = NULL;
  597. /* shadow periodic table */
  598. kfree(oxu->pshadow);
  599. oxu->pshadow = NULL;
  600. }
  601. /* Remember to add cleanup code (above) if you add anything here.
  602. */
  603. static int ehci_mem_init(struct oxu_hcd *oxu, gfp_t flags)
  604. {
  605. int i;
  606. for (i = 0; i < oxu->periodic_size; i++)
  607. oxu->mem->frame_list[i] = EHCI_LIST_END;
  608. for (i = 0; i < QHEAD_NUM; i++)
  609. oxu->qh_used[i] = 0;
  610. for (i = 0; i < QTD_NUM; i++)
  611. oxu->qtd_used[i] = 0;
  612. oxu->murb_pool = kcalloc(MURB_NUM, sizeof(struct oxu_murb), flags);
  613. if (!oxu->murb_pool)
  614. goto fail;
  615. for (i = 0; i < MURB_NUM; i++)
  616. oxu->murb_used[i] = 0;
  617. oxu->async = oxu_qh_alloc(oxu);
  618. if (!oxu->async)
  619. goto fail;
  620. oxu->periodic = (__le32 *) &oxu->mem->frame_list;
  621. oxu->periodic_dma = virt_to_phys(oxu->periodic);
  622. for (i = 0; i < oxu->periodic_size; i++)
  623. oxu->periodic[i] = EHCI_LIST_END;
  624. /* software shadow of hardware table */
  625. oxu->pshadow = kcalloc(oxu->periodic_size, sizeof(void *), flags);
  626. if (oxu->pshadow != NULL)
  627. return 0;
  628. fail:
  629. oxu_dbg(oxu, "couldn't init memory\n");
  630. ehci_mem_cleanup(oxu);
  631. return -ENOMEM;
  632. }
  633. /* Fill a qtd, returning how much of the buffer we were able to queue up.
  634. */
  635. static int qtd_fill(struct ehci_qtd *qtd, dma_addr_t buf, size_t len,
  636. int token, int maxpacket)
  637. {
  638. int i, count;
  639. u64 addr = buf;
  640. /* one buffer entry per 4K ... first might be short or unaligned */
  641. qtd->hw_buf[0] = cpu_to_le32((u32)addr);
  642. qtd->hw_buf_hi[0] = cpu_to_le32((u32)(addr >> 32));
  643. count = 0x1000 - (buf & 0x0fff); /* rest of that page */
  644. if (likely(len < count)) /* ... iff needed */
  645. count = len;
  646. else {
  647. buf += 0x1000;
  648. buf &= ~0x0fff;
  649. /* per-qtd limit: from 16K to 20K (best alignment) */
  650. for (i = 1; count < len && i < 5; i++) {
  651. addr = buf;
  652. qtd->hw_buf[i] = cpu_to_le32((u32)addr);
  653. qtd->hw_buf_hi[i] = cpu_to_le32((u32)(addr >> 32));
  654. buf += 0x1000;
  655. if ((count + 0x1000) < len)
  656. count += 0x1000;
  657. else
  658. count = len;
  659. }
  660. /* short packets may only terminate transfers */
  661. if (count != len)
  662. count -= (count % maxpacket);
  663. }
  664. qtd->hw_token = cpu_to_le32((count << 16) | token);
  665. qtd->length = count;
  666. return count;
  667. }
  668. static inline void qh_update(struct oxu_hcd *oxu,
  669. struct ehci_qh *qh, struct ehci_qtd *qtd)
  670. {
  671. /* writes to an active overlay are unsafe */
  672. BUG_ON(qh->qh_state != QH_STATE_IDLE);
  673. qh->hw_qtd_next = QTD_NEXT(qtd->qtd_dma);
  674. qh->hw_alt_next = EHCI_LIST_END;
  675. /* Except for control endpoints, we make hardware maintain data
  676. * toggle (like OHCI) ... here (re)initialize the toggle in the QH,
  677. * and set the pseudo-toggle in udev. Only usb_clear_halt() will
  678. * ever clear it.
  679. */
  680. if (!(qh->hw_info1 & cpu_to_le32(1 << 14))) {
  681. unsigned is_out, epnum;
  682. is_out = !(qtd->hw_token & cpu_to_le32(1 << 8));
  683. epnum = (le32_to_cpup(&qh->hw_info1) >> 8) & 0x0f;
  684. if (unlikely(!usb_gettoggle(qh->dev, epnum, is_out))) {
  685. qh->hw_token &= ~cpu_to_le32(QTD_TOGGLE);
  686. usb_settoggle(qh->dev, epnum, is_out, 1);
  687. }
  688. }
  689. /* HC must see latest qtd and qh data before we clear ACTIVE+HALT */
  690. wmb();
  691. qh->hw_token &= cpu_to_le32(QTD_TOGGLE | QTD_STS_PING);
  692. }
  693. /* If it weren't for a common silicon quirk (writing the dummy into the qh
  694. * overlay, so qh->hw_token wrongly becomes inactive/halted), only fault
  695. * recovery (including urb dequeue) would need software changes to a QH...
  696. */
  697. static void qh_refresh(struct oxu_hcd *oxu, struct ehci_qh *qh)
  698. {
  699. struct ehci_qtd *qtd;
  700. if (list_empty(&qh->qtd_list))
  701. qtd = qh->dummy;
  702. else {
  703. qtd = list_entry(qh->qtd_list.next,
  704. struct ehci_qtd, qtd_list);
  705. /* first qtd may already be partially processed */
  706. if (cpu_to_le32(qtd->qtd_dma) == qh->hw_current)
  707. qtd = NULL;
  708. }
  709. if (qtd)
  710. qh_update(oxu, qh, qtd);
  711. }
  712. static void qtd_copy_status(struct oxu_hcd *oxu, struct urb *urb,
  713. size_t length, u32 token)
  714. {
  715. /* count IN/OUT bytes, not SETUP (even short packets) */
  716. if (likely(QTD_PID(token) != 2))
  717. urb->actual_length += length - QTD_LENGTH(token);
  718. /* don't modify error codes */
  719. if (unlikely(urb->status != -EINPROGRESS))
  720. return;
  721. /* force cleanup after short read; not always an error */
  722. if (unlikely(IS_SHORT_READ(token)))
  723. urb->status = -EREMOTEIO;
  724. /* serious "can't proceed" faults reported by the hardware */
  725. if (token & QTD_STS_HALT) {
  726. if (token & QTD_STS_BABBLE) {
  727. /* FIXME "must" disable babbling device's port too */
  728. urb->status = -EOVERFLOW;
  729. } else if (token & QTD_STS_MMF) {
  730. /* fs/ls interrupt xfer missed the complete-split */
  731. urb->status = -EPROTO;
  732. } else if (token & QTD_STS_DBE) {
  733. urb->status = (QTD_PID(token) == 1) /* IN ? */
  734. ? -ENOSR /* hc couldn't read data */
  735. : -ECOMM; /* hc couldn't write data */
  736. } else if (token & QTD_STS_XACT) {
  737. /* timeout, bad crc, wrong PID, etc; retried */
  738. if (QTD_CERR(token))
  739. urb->status = -EPIPE;
  740. else {
  741. oxu_dbg(oxu, "devpath %s ep%d%s 3strikes\n",
  742. urb->dev->devpath,
  743. usb_pipeendpoint(urb->pipe),
  744. usb_pipein(urb->pipe) ? "in" : "out");
  745. urb->status = -EPROTO;
  746. }
  747. /* CERR nonzero + no errors + halt --> stall */
  748. } else if (QTD_CERR(token))
  749. urb->status = -EPIPE;
  750. else /* unknown */
  751. urb->status = -EPROTO;
  752. oxu_vdbg(oxu, "dev%d ep%d%s qtd token %08x --> status %d\n",
  753. usb_pipedevice(urb->pipe),
  754. usb_pipeendpoint(urb->pipe),
  755. usb_pipein(urb->pipe) ? "in" : "out",
  756. token, urb->status);
  757. }
  758. }
  759. static void ehci_urb_done(struct oxu_hcd *oxu, struct urb *urb)
  760. __releases(oxu->lock)
  761. __acquires(oxu->lock)
  762. {
  763. if (likely(urb->hcpriv != NULL)) {
  764. struct ehci_qh *qh = (struct ehci_qh *) urb->hcpriv;
  765. /* S-mask in a QH means it's an interrupt urb */
  766. if ((qh->hw_info2 & cpu_to_le32(QH_SMASK)) != 0) {
  767. /* ... update hc-wide periodic stats (for usbfs) */
  768. oxu_to_hcd(oxu)->self.bandwidth_int_reqs--;
  769. }
  770. qh_put(qh);
  771. }
  772. urb->hcpriv = NULL;
  773. switch (urb->status) {
  774. case -EINPROGRESS: /* success */
  775. urb->status = 0;
  776. default: /* fault */
  777. break;
  778. case -EREMOTEIO: /* fault or normal */
  779. if (!(urb->transfer_flags & URB_SHORT_NOT_OK))
  780. urb->status = 0;
  781. break;
  782. case -ECONNRESET: /* canceled */
  783. case -ENOENT:
  784. break;
  785. }
  786. #ifdef OXU_URB_TRACE
  787. oxu_dbg(oxu, "%s %s urb %p ep%d%s status %d len %d/%d\n",
  788. __func__, urb->dev->devpath, urb,
  789. usb_pipeendpoint(urb->pipe),
  790. usb_pipein(urb->pipe) ? "in" : "out",
  791. urb->status,
  792. urb->actual_length, urb->transfer_buffer_length);
  793. #endif
  794. /* complete() can reenter this HCD */
  795. spin_unlock(&oxu->lock);
  796. usb_hcd_giveback_urb(oxu_to_hcd(oxu), urb, urb->status);
  797. spin_lock(&oxu->lock);
  798. }
  799. static void start_unlink_async(struct oxu_hcd *oxu, struct ehci_qh *qh);
  800. static void unlink_async(struct oxu_hcd *oxu, struct ehci_qh *qh);
  801. static void intr_deschedule(struct oxu_hcd *oxu, struct ehci_qh *qh);
  802. static int qh_schedule(struct oxu_hcd *oxu, struct ehci_qh *qh);
  803. #define HALT_BIT cpu_to_le32(QTD_STS_HALT)
  804. /* Process and free completed qtds for a qh, returning URBs to drivers.
  805. * Chases up to qh->hw_current. Returns number of completions called,
  806. * indicating how much "real" work we did.
  807. */
  808. static unsigned qh_completions(struct oxu_hcd *oxu, struct ehci_qh *qh)
  809. {
  810. struct ehci_qtd *last = NULL, *end = qh->dummy;
  811. struct list_head *entry, *tmp;
  812. int stopped;
  813. unsigned count = 0;
  814. int do_status = 0;
  815. u8 state;
  816. struct oxu_murb *murb = NULL;
  817. if (unlikely(list_empty(&qh->qtd_list)))
  818. return count;
  819. /* completions (or tasks on other cpus) must never clobber HALT
  820. * till we've gone through and cleaned everything up, even when
  821. * they add urbs to this qh's queue or mark them for unlinking.
  822. *
  823. * NOTE: unlinking expects to be done in queue order.
  824. */
  825. state = qh->qh_state;
  826. qh->qh_state = QH_STATE_COMPLETING;
  827. stopped = (state == QH_STATE_IDLE);
  828. /* remove de-activated QTDs from front of queue.
  829. * after faults (including short reads), cleanup this urb
  830. * then let the queue advance.
  831. * if queue is stopped, handles unlinks.
  832. */
  833. list_for_each_safe(entry, tmp, &qh->qtd_list) {
  834. struct ehci_qtd *qtd;
  835. struct urb *urb;
  836. u32 token = 0;
  837. qtd = list_entry(entry, struct ehci_qtd, qtd_list);
  838. urb = qtd->urb;
  839. /* Clean up any state from previous QTD ...*/
  840. if (last) {
  841. if (likely(last->urb != urb)) {
  842. if (last->urb->complete == NULL) {
  843. murb = (struct oxu_murb *) last->urb;
  844. last->urb = murb->main;
  845. if (murb->last) {
  846. ehci_urb_done(oxu, last->urb);
  847. count++;
  848. }
  849. oxu_murb_free(oxu, murb);
  850. } else {
  851. ehci_urb_done(oxu, last->urb);
  852. count++;
  853. }
  854. }
  855. oxu_qtd_free(oxu, last);
  856. last = NULL;
  857. }
  858. /* ignore urbs submitted during completions we reported */
  859. if (qtd == end)
  860. break;
  861. /* hardware copies qtd out of qh overlay */
  862. rmb();
  863. token = le32_to_cpu(qtd->hw_token);
  864. /* always clean up qtds the hc de-activated */
  865. if ((token & QTD_STS_ACTIVE) == 0) {
  866. if ((token & QTD_STS_HALT) != 0) {
  867. stopped = 1;
  868. /* magic dummy for some short reads; qh won't advance.
  869. * that silicon quirk can kick in with this dummy too.
  870. */
  871. } else if (IS_SHORT_READ(token) &&
  872. !(qtd->hw_alt_next & EHCI_LIST_END)) {
  873. stopped = 1;
  874. goto halt;
  875. }
  876. /* stop scanning when we reach qtds the hc is using */
  877. } else if (likely(!stopped &&
  878. HC_IS_RUNNING(oxu_to_hcd(oxu)->state))) {
  879. break;
  880. } else {
  881. stopped = 1;
  882. if (unlikely(!HC_IS_RUNNING(oxu_to_hcd(oxu)->state)))
  883. urb->status = -ESHUTDOWN;
  884. /* ignore active urbs unless some previous qtd
  885. * for the urb faulted (including short read) or
  886. * its urb was canceled. we may patch qh or qtds.
  887. */
  888. if (likely(urb->status == -EINPROGRESS))
  889. continue;
  890. /* issue status after short control reads */
  891. if (unlikely(do_status != 0)
  892. && QTD_PID(token) == 0 /* OUT */) {
  893. do_status = 0;
  894. continue;
  895. }
  896. /* token in overlay may be most current */
  897. if (state == QH_STATE_IDLE
  898. && cpu_to_le32(qtd->qtd_dma)
  899. == qh->hw_current)
  900. token = le32_to_cpu(qh->hw_token);
  901. /* force halt for unlinked or blocked qh, so we'll
  902. * patch the qh later and so that completions can't
  903. * activate it while we "know" it's stopped.
  904. */
  905. if ((HALT_BIT & qh->hw_token) == 0) {
  906. halt:
  907. qh->hw_token |= HALT_BIT;
  908. wmb();
  909. }
  910. }
  911. /* Remove it from the queue */
  912. qtd_copy_status(oxu, urb->complete ?
  913. urb : ((struct oxu_murb *) urb)->main,
  914. qtd->length, token);
  915. if ((usb_pipein(qtd->urb->pipe)) &&
  916. (NULL != qtd->transfer_buffer))
  917. memcpy(qtd->transfer_buffer, qtd->buffer, qtd->length);
  918. do_status = (urb->status == -EREMOTEIO)
  919. && usb_pipecontrol(urb->pipe);
  920. if (stopped && qtd->qtd_list.prev != &qh->qtd_list) {
  921. last = list_entry(qtd->qtd_list.prev,
  922. struct ehci_qtd, qtd_list);
  923. last->hw_next = qtd->hw_next;
  924. }
  925. list_del(&qtd->qtd_list);
  926. last = qtd;
  927. }
  928. /* last urb's completion might still need calling */
  929. if (likely(last != NULL)) {
  930. if (last->urb->complete == NULL) {
  931. murb = (struct oxu_murb *) last->urb;
  932. last->urb = murb->main;
  933. if (murb->last) {
  934. ehci_urb_done(oxu, last->urb);
  935. count++;
  936. }
  937. oxu_murb_free(oxu, murb);
  938. } else {
  939. ehci_urb_done(oxu, last->urb);
  940. count++;
  941. }
  942. oxu_qtd_free(oxu, last);
  943. }
  944. /* restore original state; caller must unlink or relink */
  945. qh->qh_state = state;
  946. /* be sure the hardware's done with the qh before refreshing
  947. * it after fault cleanup, or recovering from silicon wrongly
  948. * overlaying the dummy qtd (which reduces DMA chatter).
  949. */
  950. if (stopped != 0 || qh->hw_qtd_next == EHCI_LIST_END) {
  951. switch (state) {
  952. case QH_STATE_IDLE:
  953. qh_refresh(oxu, qh);
  954. break;
  955. case QH_STATE_LINKED:
  956. /* should be rare for periodic transfers,
  957. * except maybe high bandwidth ...
  958. */
  959. if ((cpu_to_le32(QH_SMASK)
  960. & qh->hw_info2) != 0) {
  961. intr_deschedule(oxu, qh);
  962. (void) qh_schedule(oxu, qh);
  963. } else
  964. unlink_async(oxu, qh);
  965. break;
  966. /* otherwise, unlink already started */
  967. }
  968. }
  969. return count;
  970. }
  971. /* High bandwidth multiplier, as encoded in highspeed endpoint descriptors */
  972. #define hb_mult(wMaxPacketSize) (1 + (((wMaxPacketSize) >> 11) & 0x03))
  973. /* ... and packet size, for any kind of endpoint descriptor */
  974. #define max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
  975. /* Reverse of qh_urb_transaction: free a list of TDs.
  976. * used for cleanup after errors, before HC sees an URB's TDs.
  977. */
  978. static void qtd_list_free(struct oxu_hcd *oxu,
  979. struct urb *urb, struct list_head *qtd_list)
  980. {
  981. struct list_head *entry, *temp;
  982. list_for_each_safe(entry, temp, qtd_list) {
  983. struct ehci_qtd *qtd;
  984. qtd = list_entry(entry, struct ehci_qtd, qtd_list);
  985. list_del(&qtd->qtd_list);
  986. oxu_qtd_free(oxu, qtd);
  987. }
  988. }
  989. /* Create a list of filled qtds for this URB; won't link into qh.
  990. */
  991. static struct list_head *qh_urb_transaction(struct oxu_hcd *oxu,
  992. struct urb *urb,
  993. struct list_head *head,
  994. gfp_t flags)
  995. {
  996. struct ehci_qtd *qtd, *qtd_prev;
  997. dma_addr_t buf;
  998. int len, maxpacket;
  999. int is_input;
  1000. u32 token;
  1001. void *transfer_buf = NULL;
  1002. int ret;
  1003. /*
  1004. * URBs map to sequences of QTDs: one logical transaction
  1005. */
  1006. qtd = ehci_qtd_alloc(oxu);
  1007. if (unlikely(!qtd))
  1008. return NULL;
  1009. list_add_tail(&qtd->qtd_list, head);
  1010. qtd->urb = urb;
  1011. token = QTD_STS_ACTIVE;
  1012. token |= (EHCI_TUNE_CERR << 10);
  1013. /* for split transactions, SplitXState initialized to zero */
  1014. len = urb->transfer_buffer_length;
  1015. is_input = usb_pipein(urb->pipe);
  1016. if (!urb->transfer_buffer && urb->transfer_buffer_length && is_input)
  1017. urb->transfer_buffer = phys_to_virt(urb->transfer_dma);
  1018. if (usb_pipecontrol(urb->pipe)) {
  1019. /* SETUP pid */
  1020. ret = oxu_buf_alloc(oxu, qtd, sizeof(struct usb_ctrlrequest));
  1021. if (ret)
  1022. goto cleanup;
  1023. qtd_fill(qtd, qtd->buffer_dma, sizeof(struct usb_ctrlrequest),
  1024. token | (2 /* "setup" */ << 8), 8);
  1025. memcpy(qtd->buffer, qtd->urb->setup_packet,
  1026. sizeof(struct usb_ctrlrequest));
  1027. /* ... and always at least one more pid */
  1028. token ^= QTD_TOGGLE;
  1029. qtd_prev = qtd;
  1030. qtd = ehci_qtd_alloc(oxu);
  1031. if (unlikely(!qtd))
  1032. goto cleanup;
  1033. qtd->urb = urb;
  1034. qtd_prev->hw_next = QTD_NEXT(qtd->qtd_dma);
  1035. list_add_tail(&qtd->qtd_list, head);
  1036. /* for zero length DATA stages, STATUS is always IN */
  1037. if (len == 0)
  1038. token |= (1 /* "in" */ << 8);
  1039. }
  1040. /*
  1041. * Data transfer stage: buffer setup
  1042. */
  1043. ret = oxu_buf_alloc(oxu, qtd, len);
  1044. if (ret)
  1045. goto cleanup;
  1046. buf = qtd->buffer_dma;
  1047. transfer_buf = urb->transfer_buffer;
  1048. if (!is_input)
  1049. memcpy(qtd->buffer, qtd->urb->transfer_buffer, len);
  1050. if (is_input)
  1051. token |= (1 /* "in" */ << 8);
  1052. /* else it's already initted to "out" pid (0 << 8) */
  1053. maxpacket = max_packet(usb_maxpacket(urb->dev, urb->pipe, !is_input));
  1054. /*
  1055. * buffer gets wrapped in one or more qtds;
  1056. * last one may be "short" (including zero len)
  1057. * and may serve as a control status ack
  1058. */
  1059. for (;;) {
  1060. int this_qtd_len;
  1061. this_qtd_len = qtd_fill(qtd, buf, len, token, maxpacket);
  1062. qtd->transfer_buffer = transfer_buf;
  1063. len -= this_qtd_len;
  1064. buf += this_qtd_len;
  1065. transfer_buf += this_qtd_len;
  1066. if (is_input)
  1067. qtd->hw_alt_next = oxu->async->hw_alt_next;
  1068. /* qh makes control packets use qtd toggle; maybe switch it */
  1069. if ((maxpacket & (this_qtd_len + (maxpacket - 1))) == 0)
  1070. token ^= QTD_TOGGLE;
  1071. if (likely(len <= 0))
  1072. break;
  1073. qtd_prev = qtd;
  1074. qtd = ehci_qtd_alloc(oxu);
  1075. if (unlikely(!qtd))
  1076. goto cleanup;
  1077. if (likely(len > 0)) {
  1078. ret = oxu_buf_alloc(oxu, qtd, len);
  1079. if (ret)
  1080. goto cleanup;
  1081. }
  1082. qtd->urb = urb;
  1083. qtd_prev->hw_next = QTD_NEXT(qtd->qtd_dma);
  1084. list_add_tail(&qtd->qtd_list, head);
  1085. }
  1086. /* unless the bulk/interrupt caller wants a chance to clean
  1087. * up after short reads, hc should advance qh past this urb
  1088. */
  1089. if (likely((urb->transfer_flags & URB_SHORT_NOT_OK) == 0
  1090. || usb_pipecontrol(urb->pipe)))
  1091. qtd->hw_alt_next = EHCI_LIST_END;
  1092. /*
  1093. * control requests may need a terminating data "status" ack;
  1094. * bulk ones may need a terminating short packet (zero length).
  1095. */
  1096. if (likely(urb->transfer_buffer_length != 0)) {
  1097. int one_more = 0;
  1098. if (usb_pipecontrol(urb->pipe)) {
  1099. one_more = 1;
  1100. token ^= 0x0100; /* "in" <--> "out" */
  1101. token |= QTD_TOGGLE; /* force DATA1 */
  1102. } else if (usb_pipebulk(urb->pipe)
  1103. && (urb->transfer_flags & URB_ZERO_PACKET)
  1104. && !(urb->transfer_buffer_length % maxpacket)) {
  1105. one_more = 1;
  1106. }
  1107. if (one_more) {
  1108. qtd_prev = qtd;
  1109. qtd = ehci_qtd_alloc(oxu);
  1110. if (unlikely(!qtd))
  1111. goto cleanup;
  1112. qtd->urb = urb;
  1113. qtd_prev->hw_next = QTD_NEXT(qtd->qtd_dma);
  1114. list_add_tail(&qtd->qtd_list, head);
  1115. /* never any data in such packets */
  1116. qtd_fill(qtd, 0, 0, token, 0);
  1117. }
  1118. }
  1119. /* by default, enable interrupt on urb completion */
  1120. qtd->hw_token |= cpu_to_le32(QTD_IOC);
  1121. return head;
  1122. cleanup:
  1123. qtd_list_free(oxu, urb, head);
  1124. return NULL;
  1125. }
  1126. /* Each QH holds a qtd list; a QH is used for everything except iso.
  1127. *
  1128. * For interrupt urbs, the scheduler must set the microframe scheduling
  1129. * mask(s) each time the QH gets scheduled. For highspeed, that's
  1130. * just one microframe in the s-mask. For split interrupt transactions
  1131. * there are additional complications: c-mask, maybe FSTNs.
  1132. */
  1133. static struct ehci_qh *qh_make(struct oxu_hcd *oxu,
  1134. struct urb *urb, gfp_t flags)
  1135. {
  1136. struct ehci_qh *qh = oxu_qh_alloc(oxu);
  1137. u32 info1 = 0, info2 = 0;
  1138. int is_input, type;
  1139. int maxp = 0;
  1140. if (!qh)
  1141. return qh;
  1142. /*
  1143. * init endpoint/device data for this QH
  1144. */
  1145. info1 |= usb_pipeendpoint(urb->pipe) << 8;
  1146. info1 |= usb_pipedevice(urb->pipe) << 0;
  1147. is_input = usb_pipein(urb->pipe);
  1148. type = usb_pipetype(urb->pipe);
  1149. maxp = usb_maxpacket(urb->dev, urb->pipe, !is_input);
  1150. /* Compute interrupt scheduling parameters just once, and save.
  1151. * - allowing for high bandwidth, how many nsec/uframe are used?
  1152. * - split transactions need a second CSPLIT uframe; same question
  1153. * - splits also need a schedule gap (for full/low speed I/O)
  1154. * - qh has a polling interval
  1155. *
  1156. * For control/bulk requests, the HC or TT handles these.
  1157. */
  1158. if (type == PIPE_INTERRUPT) {
  1159. qh->usecs = NS_TO_US(usb_calc_bus_time(USB_SPEED_HIGH,
  1160. is_input, 0,
  1161. hb_mult(maxp) * max_packet(maxp)));
  1162. qh->start = NO_FRAME;
  1163. if (urb->dev->speed == USB_SPEED_HIGH) {
  1164. qh->c_usecs = 0;
  1165. qh->gap_uf = 0;
  1166. qh->period = urb->interval >> 3;
  1167. if (qh->period == 0 && urb->interval != 1) {
  1168. /* NOTE interval 2 or 4 uframes could work.
  1169. * But interval 1 scheduling is simpler, and
  1170. * includes high bandwidth.
  1171. */
  1172. dbg("intr period %d uframes, NYET!",
  1173. urb->interval);
  1174. goto done;
  1175. }
  1176. } else {
  1177. struct usb_tt *tt = urb->dev->tt;
  1178. int think_time;
  1179. /* gap is f(FS/LS transfer times) */
  1180. qh->gap_uf = 1 + usb_calc_bus_time(urb->dev->speed,
  1181. is_input, 0, maxp) / (125 * 1000);
  1182. /* FIXME this just approximates SPLIT/CSPLIT times */
  1183. if (is_input) { /* SPLIT, gap, CSPLIT+DATA */
  1184. qh->c_usecs = qh->usecs + HS_USECS(0);
  1185. qh->usecs = HS_USECS(1);
  1186. } else { /* SPLIT+DATA, gap, CSPLIT */
  1187. qh->usecs += HS_USECS(1);
  1188. qh->c_usecs = HS_USECS(0);
  1189. }
  1190. think_time = tt ? tt->think_time : 0;
  1191. qh->tt_usecs = NS_TO_US(think_time +
  1192. usb_calc_bus_time(urb->dev->speed,
  1193. is_input, 0, max_packet(maxp)));
  1194. qh->period = urb->interval;
  1195. }
  1196. }
  1197. /* support for tt scheduling, and access to toggles */
  1198. qh->dev = urb->dev;
  1199. /* using TT? */
  1200. switch (urb->dev->speed) {
  1201. case USB_SPEED_LOW:
  1202. info1 |= (1 << 12); /* EPS "low" */
  1203. /* FALL THROUGH */
  1204. case USB_SPEED_FULL:
  1205. /* EPS 0 means "full" */
  1206. if (type != PIPE_INTERRUPT)
  1207. info1 |= (EHCI_TUNE_RL_TT << 28);
  1208. if (type == PIPE_CONTROL) {
  1209. info1 |= (1 << 27); /* for TT */
  1210. info1 |= 1 << 14; /* toggle from qtd */
  1211. }
  1212. info1 |= maxp << 16;
  1213. info2 |= (EHCI_TUNE_MULT_TT << 30);
  1214. info2 |= urb->dev->ttport << 23;
  1215. /* NOTE: if (PIPE_INTERRUPT) { scheduler sets c-mask } */
  1216. break;
  1217. case USB_SPEED_HIGH: /* no TT involved */
  1218. info1 |= (2 << 12); /* EPS "high" */
  1219. if (type == PIPE_CONTROL) {
  1220. info1 |= (EHCI_TUNE_RL_HS << 28);
  1221. info1 |= 64 << 16; /* usb2 fixed maxpacket */
  1222. info1 |= 1 << 14; /* toggle from qtd */
  1223. info2 |= (EHCI_TUNE_MULT_HS << 30);
  1224. } else if (type == PIPE_BULK) {
  1225. info1 |= (EHCI_TUNE_RL_HS << 28);
  1226. info1 |= 512 << 16; /* usb2 fixed maxpacket */
  1227. info2 |= (EHCI_TUNE_MULT_HS << 30);
  1228. } else { /* PIPE_INTERRUPT */
  1229. info1 |= max_packet(maxp) << 16;
  1230. info2 |= hb_mult(maxp) << 30;
  1231. }
  1232. break;
  1233. default:
  1234. dbg("bogus dev %p speed %d", urb->dev, urb->dev->speed);
  1235. done:
  1236. qh_put(qh);
  1237. return NULL;
  1238. }
  1239. /* NOTE: if (PIPE_INTERRUPT) { scheduler sets s-mask } */
  1240. /* init as live, toggle clear, advance to dummy */
  1241. qh->qh_state = QH_STATE_IDLE;
  1242. qh->hw_info1 = cpu_to_le32(info1);
  1243. qh->hw_info2 = cpu_to_le32(info2);
  1244. usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), !is_input, 1);
  1245. qh_refresh(oxu, qh);
  1246. return qh;
  1247. }
  1248. /* Move qh (and its qtds) onto async queue; maybe enable queue.
  1249. */
  1250. static void qh_link_async(struct oxu_hcd *oxu, struct ehci_qh *qh)
  1251. {
  1252. __le32 dma = QH_NEXT(qh->qh_dma);
  1253. struct ehci_qh *head;
  1254. /* (re)start the async schedule? */
  1255. head = oxu->async;
  1256. timer_action_done(oxu, TIMER_ASYNC_OFF);
  1257. if (!head->qh_next.qh) {
  1258. u32 cmd = readl(&oxu->regs->command);
  1259. if (!(cmd & CMD_ASE)) {
  1260. /* in case a clear of CMD_ASE didn't take yet */
  1261. (void)handshake(oxu, &oxu->regs->status,
  1262. STS_ASS, 0, 150);
  1263. cmd |= CMD_ASE | CMD_RUN;
  1264. writel(cmd, &oxu->regs->command);
  1265. oxu_to_hcd(oxu)->state = HC_STATE_RUNNING;
  1266. /* posted write need not be known to HC yet ... */
  1267. }
  1268. }
  1269. /* clear halt and/or toggle; and maybe recover from silicon quirk */
  1270. if (qh->qh_state == QH_STATE_IDLE)
  1271. qh_refresh(oxu, qh);
  1272. /* splice right after start */
  1273. qh->qh_next = head->qh_next;
  1274. qh->hw_next = head->hw_next;
  1275. wmb();
  1276. head->qh_next.qh = qh;
  1277. head->hw_next = dma;
  1278. qh->qh_state = QH_STATE_LINKED;
  1279. /* qtd completions reported later by interrupt */
  1280. }
  1281. #define QH_ADDR_MASK cpu_to_le32(0x7f)
  1282. /*
  1283. * For control/bulk/interrupt, return QH with these TDs appended.
  1284. * Allocates and initializes the QH if necessary.
  1285. * Returns null if it can't allocate a QH it needs to.
  1286. * If the QH has TDs (urbs) already, that's great.
  1287. */
  1288. static struct ehci_qh *qh_append_tds(struct oxu_hcd *oxu,
  1289. struct urb *urb, struct list_head *qtd_list,
  1290. int epnum, void **ptr)
  1291. {
  1292. struct ehci_qh *qh = NULL;
  1293. qh = (struct ehci_qh *) *ptr;
  1294. if (unlikely(qh == NULL)) {
  1295. /* can't sleep here, we have oxu->lock... */
  1296. qh = qh_make(oxu, urb, GFP_ATOMIC);
  1297. *ptr = qh;
  1298. }
  1299. if (likely(qh != NULL)) {
  1300. struct ehci_qtd *qtd;
  1301. if (unlikely(list_empty(qtd_list)))
  1302. qtd = NULL;
  1303. else
  1304. qtd = list_entry(qtd_list->next, struct ehci_qtd,
  1305. qtd_list);
  1306. /* control qh may need patching ... */
  1307. if (unlikely(epnum == 0)) {
  1308. /* usb_reset_device() briefly reverts to address 0 */
  1309. if (usb_pipedevice(urb->pipe) == 0)
  1310. qh->hw_info1 &= ~QH_ADDR_MASK;
  1311. }
  1312. /* just one way to queue requests: swap with the dummy qtd.
  1313. * only hc or qh_refresh() ever modify the overlay.
  1314. */
  1315. if (likely(qtd != NULL)) {
  1316. struct ehci_qtd *dummy;
  1317. dma_addr_t dma;
  1318. __le32 token;
  1319. /* to avoid racing the HC, use the dummy td instead of
  1320. * the first td of our list (becomes new dummy). both
  1321. * tds stay deactivated until we're done, when the
  1322. * HC is allowed to fetch the old dummy (4.10.2).
  1323. */
  1324. token = qtd->hw_token;
  1325. qtd->hw_token = HALT_BIT;
  1326. wmb();
  1327. dummy = qh->dummy;
  1328. dma = dummy->qtd_dma;
  1329. *dummy = *qtd;
  1330. dummy->qtd_dma = dma;
  1331. list_del(&qtd->qtd_list);
  1332. list_add(&dummy->qtd_list, qtd_list);
  1333. list_splice(qtd_list, qh->qtd_list.prev);
  1334. ehci_qtd_init(qtd, qtd->qtd_dma);
  1335. qh->dummy = qtd;
  1336. /* hc must see the new dummy at list end */
  1337. dma = qtd->qtd_dma;
  1338. qtd = list_entry(qh->qtd_list.prev,
  1339. struct ehci_qtd, qtd_list);
  1340. qtd->hw_next = QTD_NEXT(dma);
  1341. /* let the hc process these next qtds */
  1342. dummy->hw_token = (token & ~(0x80));
  1343. wmb();
  1344. dummy->hw_token = token;
  1345. urb->hcpriv = qh_get(qh);
  1346. }
  1347. }
  1348. return qh;
  1349. }
  1350. static int submit_async(struct oxu_hcd *oxu, struct urb *urb,
  1351. struct list_head *qtd_list, gfp_t mem_flags)
  1352. {
  1353. struct ehci_qtd *qtd;
  1354. int epnum;
  1355. unsigned long flags;
  1356. struct ehci_qh *qh = NULL;
  1357. int rc = 0;
  1358. qtd = list_entry(qtd_list->next, struct ehci_qtd, qtd_list);
  1359. epnum = urb->ep->desc.bEndpointAddress;
  1360. #ifdef OXU_URB_TRACE
  1361. oxu_dbg(oxu, "%s %s urb %p ep%d%s len %d, qtd %p [qh %p]\n",
  1362. __func__, urb->dev->devpath, urb,
  1363. epnum & 0x0f, (epnum & USB_DIR_IN) ? "in" : "out",
  1364. urb->transfer_buffer_length,
  1365. qtd, urb->ep->hcpriv);
  1366. #endif
  1367. spin_lock_irqsave(&oxu->lock, flags);
  1368. if (unlikely(!HCD_HW_ACCESSIBLE(oxu_to_hcd(oxu)))) {
  1369. rc = -ESHUTDOWN;
  1370. goto done;
  1371. }
  1372. qh = qh_append_tds(oxu, urb, qtd_list, epnum, &urb->ep->hcpriv);
  1373. if (unlikely(qh == NULL)) {
  1374. rc = -ENOMEM;
  1375. goto done;
  1376. }
  1377. /* Control/bulk operations through TTs don't need scheduling,
  1378. * the HC and TT handle it when the TT has a buffer ready.
  1379. */
  1380. if (likely(qh->qh_state == QH_STATE_IDLE))
  1381. qh_link_async(oxu, qh_get(qh));
  1382. done:
  1383. spin_unlock_irqrestore(&oxu->lock, flags);
  1384. if (unlikely(qh == NULL))
  1385. qtd_list_free(oxu, urb, qtd_list);
  1386. return rc;
  1387. }
  1388. /* The async qh for the qtds being reclaimed are now unlinked from the HC */
  1389. static void end_unlink_async(struct oxu_hcd *oxu)
  1390. {
  1391. struct ehci_qh *qh = oxu->reclaim;
  1392. struct ehci_qh *next;
  1393. timer_action_done(oxu, TIMER_IAA_WATCHDOG);
  1394. qh->qh_state = QH_STATE_IDLE;
  1395. qh->qh_next.qh = NULL;
  1396. qh_put(qh); /* refcount from reclaim */
  1397. /* other unlink(s) may be pending (in QH_STATE_UNLINK_WAIT) */
  1398. next = qh->reclaim;
  1399. oxu->reclaim = next;
  1400. oxu->reclaim_ready = 0;
  1401. qh->reclaim = NULL;
  1402. qh_completions(oxu, qh);
  1403. if (!list_empty(&qh->qtd_list)
  1404. && HC_IS_RUNNING(oxu_to_hcd(oxu)->state))
  1405. qh_link_async(oxu, qh);
  1406. else {
  1407. qh_put(qh); /* refcount from async list */
  1408. /* it's not free to turn the async schedule on/off; leave it
  1409. * active but idle for a while once it empties.
  1410. */
  1411. if (HC_IS_RUNNING(oxu_to_hcd(oxu)->state)
  1412. && oxu->async->qh_next.qh == NULL)
  1413. timer_action(oxu, TIMER_ASYNC_OFF);
  1414. }
  1415. if (next) {
  1416. oxu->reclaim = NULL;
  1417. start_unlink_async(oxu, next);
  1418. }
  1419. }
  1420. /* makes sure the async qh will become idle */
  1421. /* caller must own oxu->lock */
  1422. static void start_unlink_async(struct oxu_hcd *oxu, struct ehci_qh *qh)
  1423. {
  1424. int cmd = readl(&oxu->regs->command);
  1425. struct ehci_qh *prev;
  1426. #ifdef DEBUG
  1427. assert_spin_locked(&oxu->lock);
  1428. if (oxu->reclaim || (qh->qh_state != QH_STATE_LINKED
  1429. && qh->qh_state != QH_STATE_UNLINK_WAIT))
  1430. BUG();
  1431. #endif
  1432. /* stop async schedule right now? */
  1433. if (unlikely(qh == oxu->async)) {
  1434. /* can't get here without STS_ASS set */
  1435. if (oxu_to_hcd(oxu)->state != HC_STATE_HALT
  1436. && !oxu->reclaim) {
  1437. /* ... and CMD_IAAD clear */
  1438. writel(cmd & ~CMD_ASE, &oxu->regs->command);
  1439. wmb();
  1440. /* handshake later, if we need to */
  1441. timer_action_done(oxu, TIMER_ASYNC_OFF);
  1442. }
  1443. return;
  1444. }
  1445. qh->qh_state = QH_STATE_UNLINK;
  1446. oxu->reclaim = qh = qh_get(qh);
  1447. prev = oxu->async;
  1448. while (prev->qh_next.qh != qh)
  1449. prev = prev->qh_next.qh;
  1450. prev->hw_next = qh->hw_next;
  1451. prev->qh_next = qh->qh_next;
  1452. wmb();
  1453. if (unlikely(oxu_to_hcd(oxu)->state == HC_STATE_HALT)) {
  1454. /* if (unlikely(qh->reclaim != 0))
  1455. * this will recurse, probably not much
  1456. */
  1457. end_unlink_async(oxu);
  1458. return;
  1459. }
  1460. oxu->reclaim_ready = 0;
  1461. cmd |= CMD_IAAD;
  1462. writel(cmd, &oxu->regs->command);
  1463. (void) readl(&oxu->regs->command);
  1464. timer_action(oxu, TIMER_IAA_WATCHDOG);
  1465. }
  1466. static void scan_async(struct oxu_hcd *oxu)
  1467. {
  1468. struct ehci_qh *qh;
  1469. enum ehci_timer_action action = TIMER_IO_WATCHDOG;
  1470. if (!++(oxu->stamp))
  1471. oxu->stamp++;
  1472. timer_action_done(oxu, TIMER_ASYNC_SHRINK);
  1473. rescan:
  1474. qh = oxu->async->qh_next.qh;
  1475. if (likely(qh != NULL)) {
  1476. do {
  1477. /* clean any finished work for this qh */
  1478. if (!list_empty(&qh->qtd_list)
  1479. && qh->stamp != oxu->stamp) {
  1480. int temp;
  1481. /* unlinks could happen here; completion
  1482. * reporting drops the lock. rescan using
  1483. * the latest schedule, but don't rescan
  1484. * qhs we already finished (no looping).
  1485. */
  1486. qh = qh_get(qh);
  1487. qh->stamp = oxu->stamp;
  1488. temp = qh_completions(oxu, qh);
  1489. qh_put(qh);
  1490. if (temp != 0)
  1491. goto rescan;
  1492. }
  1493. /* unlink idle entries, reducing HC PCI usage as well
  1494. * as HCD schedule-scanning costs. delay for any qh
  1495. * we just scanned, there's a not-unusual case that it
  1496. * doesn't stay idle for long.
  1497. * (plus, avoids some kind of re-activation race.)
  1498. */
  1499. if (list_empty(&qh->qtd_list)) {
  1500. if (qh->stamp == oxu->stamp)
  1501. action = TIMER_ASYNC_SHRINK;
  1502. else if (!oxu->reclaim
  1503. && qh->qh_state == QH_STATE_LINKED)
  1504. start_unlink_async(oxu, qh);
  1505. }
  1506. qh = qh->qh_next.qh;
  1507. } while (qh);
  1508. }
  1509. if (action == TIMER_ASYNC_SHRINK)
  1510. timer_action(oxu, TIMER_ASYNC_SHRINK);
  1511. }
  1512. /*
  1513. * periodic_next_shadow - return "next" pointer on shadow list
  1514. * @periodic: host pointer to qh/itd/sitd
  1515. * @tag: hardware tag for type of this record
  1516. */
  1517. static union ehci_shadow *periodic_next_shadow(union ehci_shadow *periodic,
  1518. __le32 tag)
  1519. {
  1520. switch (tag) {
  1521. default:
  1522. case Q_TYPE_QH:
  1523. return &periodic->qh->qh_next;
  1524. }
  1525. }
  1526. /* caller must hold oxu->lock */
  1527. static void periodic_unlink(struct oxu_hcd *oxu, unsigned frame, void *ptr)
  1528. {
  1529. union ehci_shadow *prev_p = &oxu->pshadow[frame];
  1530. __le32 *hw_p = &oxu->periodic[frame];
  1531. union ehci_shadow here = *prev_p;
  1532. /* find predecessor of "ptr"; hw and shadow lists are in sync */
  1533. while (here.ptr && here.ptr != ptr) {
  1534. prev_p = periodic_next_shadow(prev_p, Q_NEXT_TYPE(*hw_p));
  1535. hw_p = here.hw_next;
  1536. here = *prev_p;
  1537. }
  1538. /* an interrupt entry (at list end) could have been shared */
  1539. if (!here.ptr)
  1540. return;
  1541. /* update shadow and hardware lists ... the old "next" pointers
  1542. * from ptr may still be in use, the caller updates them.
  1543. */
  1544. *prev_p = *periodic_next_shadow(&here, Q_NEXT_TYPE(*hw_p));
  1545. *hw_p = *here.hw_next;
  1546. }
  1547. /* how many of the uframe's 125 usecs are allocated? */
  1548. static unsigned short periodic_usecs(struct oxu_hcd *oxu,
  1549. unsigned frame, unsigned uframe)
  1550. {
  1551. __le32 *hw_p = &oxu->periodic[frame];
  1552. union ehci_shadow *q = &oxu->pshadow[frame];
  1553. unsigned usecs = 0;
  1554. while (q->ptr) {
  1555. switch (Q_NEXT_TYPE(*hw_p)) {
  1556. case Q_TYPE_QH:
  1557. default:
  1558. /* is it in the S-mask? */
  1559. if (q->qh->hw_info2 & cpu_to_le32(1 << uframe))
  1560. usecs += q->qh->usecs;
  1561. /* ... or C-mask? */
  1562. if (q->qh->hw_info2 & cpu_to_le32(1 << (8 + uframe)))
  1563. usecs += q->qh->c_usecs;
  1564. hw_p = &q->qh->hw_next;
  1565. q = &q->qh->qh_next;
  1566. break;
  1567. }
  1568. }
  1569. #ifdef DEBUG
  1570. if (usecs > 100)
  1571. oxu_err(oxu, "uframe %d sched overrun: %d usecs\n",
  1572. frame * 8 + uframe, usecs);
  1573. #endif
  1574. return usecs;
  1575. }
  1576. static int enable_periodic(struct oxu_hcd *oxu)
  1577. {
  1578. u32 cmd;
  1579. int status;
  1580. /* did clearing PSE did take effect yet?
  1581. * takes effect only at frame boundaries...
  1582. */
  1583. status = handshake(oxu, &oxu->regs->status, STS_PSS, 0, 9 * 125);
  1584. if (status != 0) {
  1585. oxu_to_hcd(oxu)->state = HC_STATE_HALT;
  1586. usb_hc_died(oxu_to_hcd(oxu));
  1587. return status;
  1588. }
  1589. cmd = readl(&oxu->regs->command) | CMD_PSE;
  1590. writel(cmd, &oxu->regs->command);
  1591. /* posted write ... PSS happens later */
  1592. oxu_to_hcd(oxu)->state = HC_STATE_RUNNING;
  1593. /* make sure ehci_work scans these */
  1594. oxu->next_uframe = readl(&oxu->regs->frame_index)
  1595. % (oxu->periodic_size << 3);
  1596. return 0;
  1597. }
  1598. static int disable_periodic(struct oxu_hcd *oxu)
  1599. {
  1600. u32 cmd;
  1601. int status;
  1602. /* did setting PSE not take effect yet?
  1603. * takes effect only at frame boundaries...
  1604. */
  1605. status = handshake(oxu, &oxu->regs->status, STS_PSS, STS_PSS, 9 * 125);
  1606. if (status != 0) {
  1607. oxu_to_hcd(oxu)->state = HC_STATE_HALT;
  1608. usb_hc_died(oxu_to_hcd(oxu));
  1609. return status;
  1610. }
  1611. cmd = readl(&oxu->regs->command) & ~CMD_PSE;
  1612. writel(cmd, &oxu->regs->command);
  1613. /* posted write ... */
  1614. oxu->next_uframe = -1;
  1615. return 0;
  1616. }
  1617. /* periodic schedule slots have iso tds (normal or split) first, then a
  1618. * sparse tree for active interrupt transfers.
  1619. *
  1620. * this just links in a qh; caller guarantees uframe masks are set right.
  1621. * no FSTN support (yet; oxu 0.96+)
  1622. */
  1623. static int qh_link_periodic(struct oxu_hcd *oxu, struct ehci_qh *qh)
  1624. {
  1625. unsigned i;
  1626. unsigned period = qh->period;
  1627. dev_dbg(&qh->dev->dev,
  1628. "link qh%d-%04x/%p start %d [%d/%d us]\n",
  1629. period, le32_to_cpup(&qh->hw_info2) & (QH_CMASK | QH_SMASK),
  1630. qh, qh->start, qh->usecs, qh->c_usecs);
  1631. /* high bandwidth, or otherwise every microframe */
  1632. if (period == 0)
  1633. period = 1;
  1634. for (i = qh->start; i < oxu->periodic_size; i += period) {
  1635. union ehci_shadow *prev = &oxu->pshadow[i];
  1636. __le32 *hw_p = &oxu->periodic[i];
  1637. union ehci_shadow here = *prev;
  1638. __le32 type = 0;
  1639. /* skip the iso nodes at list head */
  1640. while (here.ptr) {
  1641. type = Q_NEXT_TYPE(*hw_p);
  1642. if (type == Q_TYPE_QH)
  1643. break;
  1644. prev = periodic_next_shadow(prev, type);
  1645. hw_p = &here.qh->hw_next;
  1646. here = *prev;
  1647. }
  1648. /* sorting each branch by period (slow-->fast)
  1649. * enables sharing interior tree nodes
  1650. */
  1651. while (here.ptr && qh != here.qh) {
  1652. if (qh->period > here.qh->period)
  1653. break;
  1654. prev = &here.qh->qh_next;
  1655. hw_p = &here.qh->hw_next;
  1656. here = *prev;
  1657. }
  1658. /* link in this qh, unless some earlier pass did that */
  1659. if (qh != here.qh) {
  1660. qh->qh_next = here;
  1661. if (here.qh)
  1662. qh->hw_next = *hw_p;
  1663. wmb();
  1664. prev->qh = qh;
  1665. *hw_p = QH_NEXT(qh->qh_dma);
  1666. }
  1667. }
  1668. qh->qh_state = QH_STATE_LINKED;
  1669. qh_get(qh);
  1670. /* update per-qh bandwidth for usbfs */
  1671. oxu_to_hcd(oxu)->self.bandwidth_allocated += qh->period
  1672. ? ((qh->usecs + qh->c_usecs) / qh->period)
  1673. : (qh->usecs * 8);
  1674. /* maybe enable periodic schedule processing */
  1675. if (!oxu->periodic_sched++)
  1676. return enable_periodic(oxu);
  1677. return 0;
  1678. }
  1679. static void qh_unlink_periodic(struct oxu_hcd *oxu, struct ehci_qh *qh)
  1680. {
  1681. unsigned i;
  1682. unsigned period;
  1683. /* FIXME:
  1684. * IF this isn't high speed
  1685. * and this qh is active in the current uframe
  1686. * (and overlay token SplitXstate is false?)
  1687. * THEN
  1688. * qh->hw_info1 |= cpu_to_le32(1 << 7 "ignore");
  1689. */
  1690. /* high bandwidth, or otherwise part of every microframe */
  1691. period = qh->period;
  1692. if (period == 0)
  1693. period = 1;
  1694. for (i = qh->start; i < oxu->periodic_size; i += period)
  1695. periodic_unlink(oxu, i, qh);
  1696. /* update per-qh bandwidth for usbfs */
  1697. oxu_to_hcd(oxu)->self.bandwidth_allocated -= qh->period
  1698. ? ((qh->usecs + qh->c_usecs) / qh->period)
  1699. : (qh->usecs * 8);
  1700. dev_dbg(&qh->dev->dev,
  1701. "unlink qh%d-%04x/%p start %d [%d/%d us]\n",
  1702. qh->period,
  1703. le32_to_cpup(&qh->hw_info2) & (QH_CMASK | QH_SMASK),
  1704. qh, qh->start, qh->usecs, qh->c_usecs);
  1705. /* qh->qh_next still "live" to HC */
  1706. qh->qh_state = QH_STATE_UNLINK;
  1707. qh->qh_next.ptr = NULL;
  1708. qh_put(qh);
  1709. /* maybe turn off periodic schedule */
  1710. oxu->periodic_sched--;
  1711. if (!oxu->periodic_sched)
  1712. (void) disable_periodic(oxu);
  1713. }
  1714. static void intr_deschedule(struct oxu_hcd *oxu, struct ehci_qh *qh)
  1715. {
  1716. unsigned wait;
  1717. qh_unlink_periodic(oxu, qh);
  1718. /* simple/paranoid: always delay, expecting the HC needs to read
  1719. * qh->hw_next or finish a writeback after SPLIT/CSPLIT ... and
  1720. * expect khubd to clean up after any CSPLITs we won't issue.
  1721. * active high speed queues may need bigger delays...
  1722. */
  1723. if (list_empty(&qh->qtd_list)
  1724. || (cpu_to_le32(QH_CMASK) & qh->hw_info2) != 0)
  1725. wait = 2;
  1726. else
  1727. wait = 55; /* worst case: 3 * 1024 */
  1728. udelay(wait);
  1729. qh->qh_state = QH_STATE_IDLE;
  1730. qh->hw_next = EHCI_LIST_END;
  1731. wmb();
  1732. }
  1733. static int check_period(struct oxu_hcd *oxu,
  1734. unsigned frame, unsigned uframe,
  1735. unsigned period, unsigned usecs)
  1736. {
  1737. int claimed;
  1738. /* complete split running into next frame?
  1739. * given FSTN support, we could sometimes check...
  1740. */
  1741. if (uframe >= 8)
  1742. return 0;
  1743. /*
  1744. * 80% periodic == 100 usec/uframe available
  1745. * convert "usecs we need" to "max already claimed"
  1746. */
  1747. usecs = 100 - usecs;
  1748. /* we "know" 2 and 4 uframe intervals were rejected; so
  1749. * for period 0, check _every_ microframe in the schedule.
  1750. */
  1751. if (unlikely(period == 0)) {
  1752. do {
  1753. for (uframe = 0; uframe < 7; uframe++) {
  1754. claimed = periodic_usecs(oxu, frame, uframe);
  1755. if (claimed > usecs)
  1756. return 0;
  1757. }
  1758. } while ((frame += 1) < oxu->periodic_size);
  1759. /* just check the specified uframe, at that period */
  1760. } else {
  1761. do {
  1762. claimed = periodic_usecs(oxu, frame, uframe);
  1763. if (claimed > usecs)
  1764. return 0;
  1765. } while ((frame += period) < oxu->periodic_size);
  1766. }
  1767. return 1;
  1768. }
  1769. static int check_intr_schedule(struct oxu_hcd *oxu,
  1770. unsigned frame, unsigned uframe,
  1771. const struct ehci_qh *qh, __le32 *c_maskp)
  1772. {
  1773. int retval = -ENOSPC;
  1774. if (qh->c_usecs && uframe >= 6) /* FSTN territory? */
  1775. goto done;
  1776. if (!check_period(oxu, frame, uframe, qh->period, qh->usecs))
  1777. goto done;
  1778. if (!qh->c_usecs) {
  1779. retval = 0;
  1780. *c_maskp = 0;
  1781. goto done;
  1782. }
  1783. done:
  1784. return retval;
  1785. }
  1786. /* "first fit" scheduling policy used the first time through,
  1787. * or when the previous schedule slot can't be re-used.
  1788. */
  1789. static int qh_schedule(struct oxu_hcd *oxu, struct ehci_qh *qh)
  1790. {
  1791. int status;
  1792. unsigned uframe;
  1793. __le32 c_mask;
  1794. unsigned frame; /* 0..(qh->period - 1), or NO_FRAME */
  1795. qh_refresh(oxu, qh);
  1796. qh->hw_next = EHCI_LIST_END;
  1797. frame = qh->start;
  1798. /* reuse the previous schedule slots, if we can */
  1799. if (frame < qh->period) {
  1800. uframe = ffs(le32_to_cpup(&qh->hw_info2) & QH_SMASK);
  1801. status = check_intr_schedule(oxu, frame, --uframe,
  1802. qh, &c_mask);
  1803. } else {
  1804. uframe = 0;
  1805. c_mask = 0;
  1806. status = -ENOSPC;
  1807. }
  1808. /* else scan the schedule to find a group of slots such that all
  1809. * uframes have enough periodic bandwidth available.
  1810. */
  1811. if (status) {
  1812. /* "normal" case, uframing flexible except with splits */
  1813. if (qh->period) {
  1814. frame = qh->period - 1;
  1815. do {
  1816. for (uframe = 0; uframe < 8; uframe++) {
  1817. status = check_intr_schedule(oxu,
  1818. frame, uframe, qh,
  1819. &c_mask);
  1820. if (status == 0)
  1821. break;
  1822. }
  1823. } while (status && frame--);
  1824. /* qh->period == 0 means every uframe */
  1825. } else {
  1826. frame = 0;
  1827. status = check_intr_schedule(oxu, 0, 0, qh, &c_mask);
  1828. }
  1829. if (status)
  1830. goto done;
  1831. qh->start = frame;
  1832. /* reset S-frame and (maybe) C-frame masks */
  1833. qh->hw_info2 &= cpu_to_le32(~(QH_CMASK | QH_SMASK));
  1834. qh->hw_info2 |= qh->period
  1835. ? cpu_to_le32(1 << uframe)
  1836. : cpu_to_le32(QH_SMASK);
  1837. qh->hw_info2 |= c_mask;
  1838. } else
  1839. oxu_dbg(oxu, "reused qh %p schedule\n", qh);
  1840. /* stuff into the periodic schedule */
  1841. status = qh_link_periodic(oxu, qh);
  1842. done:
  1843. return status;
  1844. }
  1845. static int intr_submit(struct oxu_hcd *oxu, struct urb *urb,
  1846. struct list_head *qtd_list, gfp_t mem_flags)
  1847. {
  1848. unsigned epnum;
  1849. unsigned long flags;
  1850. struct ehci_qh *qh;
  1851. int status = 0;
  1852. struct list_head empty;
  1853. /* get endpoint and transfer/schedule data */
  1854. epnum = urb->ep->desc.bEndpointAddress;
  1855. spin_lock_irqsave(&oxu->lock, flags);
  1856. if (unlikely(!HCD_HW_ACCESSIBLE(oxu_to_hcd(oxu)))) {
  1857. status = -ESHUTDOWN;
  1858. goto done;
  1859. }
  1860. /* get qh and force any scheduling errors */
  1861. INIT_LIST_HEAD(&empty);
  1862. qh = qh_append_tds(oxu, urb, &empty, epnum, &urb->ep->hcpriv);
  1863. if (qh == NULL) {
  1864. status = -ENOMEM;
  1865. goto done;
  1866. }
  1867. if (qh->qh_state == QH_STATE_IDLE) {
  1868. status = qh_schedule(oxu, qh);
  1869. if (status != 0)
  1870. goto done;
  1871. }
  1872. /* then queue the urb's tds to the qh */
  1873. qh = qh_append_tds(oxu, urb, qtd_list, epnum, &urb->ep->hcpriv);
  1874. BUG_ON(qh == NULL);
  1875. /* ... update usbfs periodic stats */
  1876. oxu_to_hcd(oxu)->self.bandwidth_int_reqs++;
  1877. done:
  1878. spin_unlock_irqrestore(&oxu->lock, flags);
  1879. if (status)
  1880. qtd_list_free(oxu, urb, qtd_list);
  1881. return status;
  1882. }
  1883. static inline int itd_submit(struct oxu_hcd *oxu, struct urb *urb,
  1884. gfp_t mem_flags)
  1885. {
  1886. oxu_dbg(oxu, "iso support is missing!\n");
  1887. return -ENOSYS;
  1888. }
  1889. static inline int sitd_submit(struct oxu_hcd *oxu, struct urb *urb,
  1890. gfp_t mem_flags)
  1891. {
  1892. oxu_dbg(oxu, "split iso support is missing!\n");
  1893. return -ENOSYS;
  1894. }
  1895. static void scan_periodic(struct oxu_hcd *oxu)
  1896. {
  1897. unsigned frame, clock, now_uframe, mod;
  1898. unsigned modified;
  1899. mod = oxu->periodic_size << 3;
  1900. /*
  1901. * When running, scan from last scan point up to "now"
  1902. * else clean up by scanning everything that's left.
  1903. * Touches as few pages as possible: cache-friendly.
  1904. */
  1905. now_uframe = oxu->next_uframe;
  1906. if (HC_IS_RUNNING(oxu_to_hcd(oxu)->state))
  1907. clock = readl(&oxu->regs->frame_index);
  1908. else
  1909. clock = now_uframe + mod - 1;
  1910. clock %= mod;
  1911. for (;;) {
  1912. union ehci_shadow q, *q_p;
  1913. __le32 type, *hw_p;
  1914. unsigned uframes;
  1915. /* don't scan past the live uframe */
  1916. frame = now_uframe >> 3;
  1917. if (frame == (clock >> 3))
  1918. uframes = now_uframe & 0x07;
  1919. else {
  1920. /* safe to scan the whole frame at once */
  1921. now_uframe |= 0x07;
  1922. uframes = 8;
  1923. }
  1924. restart:
  1925. /* scan each element in frame's queue for completions */
  1926. q_p = &oxu->pshadow[frame];
  1927. hw_p = &oxu->periodic[frame];
  1928. q.ptr = q_p->ptr;
  1929. type = Q_NEXT_TYPE(*hw_p);
  1930. modified = 0;
  1931. while (q.ptr != NULL) {
  1932. union ehci_shadow temp;
  1933. int live;
  1934. live = HC_IS_RUNNING(oxu_to_hcd(oxu)->state);
  1935. switch (type) {
  1936. case Q_TYPE_QH:
  1937. /* handle any completions */
  1938. temp.qh = qh_get(q.qh);
  1939. type = Q_NEXT_TYPE(q.qh->hw_next);
  1940. q = q.qh->qh_next;
  1941. modified = qh_completions(oxu, temp.qh);
  1942. if (unlikely(list_empty(&temp.qh->qtd_list)))
  1943. intr_deschedule(oxu, temp.qh);
  1944. qh_put(temp.qh);
  1945. break;
  1946. default:
  1947. dbg("corrupt type %d frame %d shadow %p",
  1948. type, frame, q.ptr);
  1949. q.ptr = NULL;
  1950. }
  1951. /* assume completion callbacks modify the queue */
  1952. if (unlikely(modified))
  1953. goto restart;
  1954. }
  1955. /* Stop when we catch up to the HC */
  1956. /* FIXME: this assumes we won't get lapped when
  1957. * latencies climb; that should be rare, but...
  1958. * detect it, and just go all the way around.
  1959. * FLR might help detect this case, so long as latencies
  1960. * don't exceed periodic_size msec (default 1.024 sec).
  1961. */
  1962. /* FIXME: likewise assumes HC doesn't halt mid-scan */
  1963. if (now_uframe == clock) {
  1964. unsigned now;
  1965. if (!HC_IS_RUNNING(oxu_to_hcd(oxu)->state))
  1966. break;
  1967. oxu->next_uframe = now_uframe;
  1968. now = readl(&oxu->regs->frame_index) % mod;
  1969. if (now_uframe == now)
  1970. break;
  1971. /* rescan the rest of this frame, then ... */
  1972. clock = now;
  1973. } else {
  1974. now_uframe++;
  1975. now_uframe %= mod;
  1976. }
  1977. }
  1978. }
  1979. /* On some systems, leaving remote wakeup enabled prevents system shutdown.
  1980. * The firmware seems to think that powering off is a wakeup event!
  1981. * This routine turns off remote wakeup and everything else, on all ports.
  1982. */
  1983. static void ehci_turn_off_all_ports(struct oxu_hcd *oxu)
  1984. {
  1985. int port = HCS_N_PORTS(oxu->hcs_params);
  1986. while (port--)
  1987. writel(PORT_RWC_BITS, &oxu->regs->port_status[port]);
  1988. }
  1989. static void ehci_port_power(struct oxu_hcd *oxu, int is_on)
  1990. {
  1991. unsigned port;
  1992. if (!HCS_PPC(oxu->hcs_params))
  1993. return;
  1994. oxu_dbg(oxu, "...power%s ports...\n", is_on ? "up" : "down");
  1995. for (port = HCS_N_PORTS(oxu->hcs_params); port > 0; )
  1996. (void) oxu_hub_control(oxu_to_hcd(oxu),
  1997. is_on ? SetPortFeature : ClearPortFeature,
  1998. USB_PORT_FEAT_POWER,
  1999. port--, NULL, 0);
  2000. msleep(20);
  2001. }
  2002. /* Called from some interrupts, timers, and so on.
  2003. * It calls driver completion functions, after dropping oxu->lock.
  2004. */
  2005. static void ehci_work(struct oxu_hcd *oxu)
  2006. {
  2007. timer_action_done(oxu, TIMER_IO_WATCHDOG);
  2008. if (oxu->reclaim_ready)
  2009. end_unlink_async(oxu);
  2010. /* another CPU may drop oxu->lock during a schedule scan while
  2011. * it reports urb completions. this flag guards against bogus
  2012. * attempts at re-entrant schedule scanning.
  2013. */
  2014. if (oxu->scanning)
  2015. return;
  2016. oxu->scanning = 1;
  2017. scan_async(oxu);
  2018. if (oxu->next_uframe != -1)
  2019. scan_periodic(oxu);
  2020. oxu->scanning = 0;
  2021. /* the IO watchdog guards against hardware or driver bugs that
  2022. * misplace IRQs, and should let us run completely without IRQs.
  2023. * such lossage has been observed on both VT6202 and VT8235.
  2024. */
  2025. if (HC_IS_RUNNING(oxu_to_hcd(oxu)->state) &&
  2026. (oxu->async->qh_next.ptr != NULL ||
  2027. oxu->periodic_sched != 0))
  2028. timer_action(oxu, TIMER_IO_WATCHDOG);
  2029. }
  2030. static void unlink_async(struct oxu_hcd *oxu, struct ehci_qh *qh)
  2031. {
  2032. /* if we need to use IAA and it's busy, defer */
  2033. if (qh->qh_state == QH_STATE_LINKED
  2034. && oxu->reclaim
  2035. && HC_IS_RUNNING(oxu_to_hcd(oxu)->state)) {
  2036. struct ehci_qh *last;
  2037. for (last = oxu->reclaim;
  2038. last->reclaim;
  2039. last = last->reclaim)
  2040. continue;
  2041. qh->qh_state = QH_STATE_UNLINK_WAIT;
  2042. last->reclaim = qh;
  2043. /* bypass IAA if the hc can't care */
  2044. } else if (!HC_IS_RUNNING(oxu_to_hcd(oxu)->state) && oxu->reclaim)
  2045. end_unlink_async(oxu);
  2046. /* something else might have unlinked the qh by now */
  2047. if (qh->qh_state == QH_STATE_LINKED)
  2048. start_unlink_async(oxu, qh);
  2049. }
  2050. /*
  2051. * USB host controller methods
  2052. */
  2053. static irqreturn_t oxu210_hcd_irq(struct usb_hcd *hcd)
  2054. {
  2055. struct oxu_hcd *oxu = hcd_to_oxu(hcd);
  2056. u32 status, pcd_status = 0;
  2057. int bh;
  2058. spin_lock(&oxu->lock);
  2059. status = readl(&oxu->regs->status);
  2060. /* e.g. cardbus physical eject */
  2061. if (status == ~(u32) 0) {
  2062. oxu_dbg(oxu, "device removed\n");
  2063. goto dead;
  2064. }
  2065. /* Shared IRQ? */
  2066. status &= INTR_MASK;
  2067. if (!status || unlikely(hcd->state == HC_STATE_HALT)) {
  2068. spin_unlock(&oxu->lock);
  2069. return IRQ_NONE;
  2070. }
  2071. /* clear (just) interrupts */
  2072. writel(status, &oxu->regs->status);
  2073. readl(&oxu->regs->command); /* unblock posted write */
  2074. bh = 0;
  2075. #ifdef OXU_VERBOSE_DEBUG
  2076. /* unrequested/ignored: Frame List Rollover */
  2077. dbg_status(oxu, "irq", status);
  2078. #endif
  2079. /* INT, ERR, and IAA interrupt rates can be throttled */
  2080. /* normal [4.15.1.2] or error [4.15.1.1] completion */
  2081. if (likely((status & (STS_INT|STS_ERR)) != 0))
  2082. bh = 1;
  2083. /* complete the unlinking of some qh [4.15.2.3] */
  2084. if (status & STS_IAA) {
  2085. oxu->reclaim_ready = 1;
  2086. bh = 1;
  2087. }
  2088. /* remote wakeup [4.3.1] */
  2089. if (status & STS_PCD) {
  2090. unsigned i = HCS_N_PORTS(oxu->hcs_params);
  2091. pcd_status = status;
  2092. /* resume root hub? */
  2093. if (!(readl(&oxu->regs->command) & CMD_RUN))
  2094. usb_hcd_resume_root_hub(hcd);
  2095. while (i--) {
  2096. int pstatus = readl(&oxu->regs->port_status[i]);
  2097. if (pstatus & PORT_OWNER)
  2098. continue;
  2099. if (!(pstatus & PORT_RESUME)
  2100. || oxu->reset_done[i] != 0)
  2101. continue;
  2102. /* start 20 msec resume signaling from this port,
  2103. * and make khubd collect PORT_STAT_C_SUSPEND to
  2104. * stop that signaling.
  2105. */
  2106. oxu->reset_done[i] = jiffies + msecs_to_jiffies(20);
  2107. oxu_dbg(oxu, "port %d remote wakeup\n", i + 1);
  2108. mod_timer(&hcd->rh_timer, oxu->reset_done[i]);
  2109. }
  2110. }
  2111. /* PCI errors [4.15.2.4] */
  2112. if (unlikely((status & STS_FATAL) != 0)) {
  2113. /* bogus "fatal" IRQs appear on some chips... why? */
  2114. status = readl(&oxu->regs->status);
  2115. dbg_cmd(oxu, "fatal", readl(&oxu->regs->command));
  2116. dbg_status(oxu, "fatal", status);
  2117. if (status & STS_HALT) {
  2118. oxu_err(oxu, "fatal error\n");
  2119. dead:
  2120. ehci_reset(oxu);
  2121. writel(0, &oxu->regs->configured_flag);
  2122. usb_hc_died(hcd);
  2123. /* generic layer kills/unlinks all urbs, then
  2124. * uses oxu_stop to clean up the rest
  2125. */
  2126. bh = 1;
  2127. }
  2128. }
  2129. if (bh)
  2130. ehci_work(oxu);
  2131. spin_unlock(&oxu->lock);
  2132. if (pcd_status & STS_PCD)
  2133. usb_hcd_poll_rh_status(hcd);
  2134. return IRQ_HANDLED;
  2135. }
  2136. static irqreturn_t oxu_irq(struct usb_hcd *hcd)
  2137. {
  2138. struct oxu_hcd *oxu = hcd_to_oxu(hcd);
  2139. int ret = IRQ_HANDLED;
  2140. u32 status = oxu_readl(hcd->regs, OXU_CHIPIRQSTATUS);
  2141. u32 enable = oxu_readl(hcd->regs, OXU_CHIPIRQEN_SET);
  2142. /* Disable all interrupt */
  2143. oxu_writel(hcd->regs, OXU_CHIPIRQEN_CLR, enable);
  2144. if ((oxu->is_otg && (status & OXU_USBOTGI)) ||
  2145. (!oxu->is_otg && (status & OXU_USBSPHI)))
  2146. oxu210_hcd_irq(hcd);
  2147. else
  2148. ret = IRQ_NONE;
  2149. /* Enable all interrupt back */
  2150. oxu_writel(hcd->regs, OXU_CHIPIRQEN_SET, enable);
  2151. return ret;
  2152. }
  2153. static void oxu_watchdog(unsigned long param)
  2154. {
  2155. struct oxu_hcd *oxu = (struct oxu_hcd *) param;
  2156. unsigned long flags;
  2157. spin_lock_irqsave(&oxu->lock, flags);
  2158. /* lost IAA irqs wedge things badly; seen with a vt8235 */
  2159. if (oxu->reclaim) {
  2160. u32 status = readl(&oxu->regs->status);
  2161. if (status & STS_IAA) {
  2162. oxu_vdbg(oxu, "lost IAA\n");
  2163. writel(STS_IAA, &oxu->regs->status);
  2164. oxu->reclaim_ready = 1;
  2165. }
  2166. }
  2167. /* stop async processing after it's idled a bit */
  2168. if (test_bit(TIMER_ASYNC_OFF, &oxu->actions))
  2169. start_unlink_async(oxu, oxu->async);
  2170. /* oxu could run by timer, without IRQs ... */
  2171. ehci_work(oxu);
  2172. spin_unlock_irqrestore(&oxu->lock, flags);
  2173. }
  2174. /* One-time init, only for memory state.
  2175. */
  2176. static int oxu_hcd_init(struct usb_hcd *hcd)
  2177. {
  2178. struct oxu_hcd *oxu = hcd_to_oxu(hcd);
  2179. u32 temp;
  2180. int retval;
  2181. u32 hcc_params;
  2182. spin_lock_init(&oxu->lock);
  2183. init_timer(&oxu->watchdog);
  2184. oxu->watchdog.function = oxu_watchdog;
  2185. oxu->watchdog.data = (unsigned long) oxu;
  2186. /*
  2187. * hw default: 1K periodic list heads, one per frame.
  2188. * periodic_size can shrink by USBCMD update if hcc_params allows.
  2189. */
  2190. oxu->periodic_size = DEFAULT_I_TDPS;
  2191. retval = ehci_mem_init(oxu, GFP_KERNEL);
  2192. if (retval < 0)
  2193. return retval;
  2194. /* controllers may cache some of the periodic schedule ... */
  2195. hcc_params = readl(&oxu->caps->hcc_params);
  2196. if (HCC_ISOC_CACHE(hcc_params)) /* full frame cache */
  2197. oxu->i_thresh = 8;
  2198. else /* N microframes cached */
  2199. oxu->i_thresh = 2 + HCC_ISOC_THRES(hcc_params);
  2200. oxu->reclaim = NULL;
  2201. oxu->reclaim_ready = 0;
  2202. oxu->next_uframe = -1;
  2203. /*
  2204. * dedicate a qh for the async ring head, since we couldn't unlink
  2205. * a 'real' qh without stopping the async schedule [4.8]. use it
  2206. * as the 'reclamation list head' too.
  2207. * its dummy is used in hw_alt_next of many tds, to prevent the qh
  2208. * from automatically advancing to the next td after short reads.
  2209. */
  2210. oxu->async->qh_next.qh = NULL;
  2211. oxu->async->hw_next = QH_NEXT(oxu->async->qh_dma);
  2212. oxu->async->hw_info1 = cpu_to_le32(QH_HEAD);
  2213. oxu->async->hw_token = cpu_to_le32(QTD_STS_HALT);
  2214. oxu->async->hw_qtd_next = EHCI_LIST_END;
  2215. oxu->async->qh_state = QH_STATE_LINKED;
  2216. oxu->async->hw_alt_next = QTD_NEXT(oxu->async->dummy->qtd_dma);
  2217. /* clear interrupt enables, set irq latency */
  2218. if (log2_irq_thresh < 0 || log2_irq_thresh > 6)
  2219. log2_irq_thresh = 0;
  2220. temp = 1 << (16 + log2_irq_thresh);
  2221. if (HCC_CANPARK(hcc_params)) {
  2222. /* HW default park == 3, on hardware that supports it (like
  2223. * NVidia and ALI silicon), maximizes throughput on the async
  2224. * schedule by avoiding QH fetches between transfers.
  2225. *
  2226. * With fast usb storage devices and NForce2, "park" seems to
  2227. * make problems: throughput reduction (!), data errors...
  2228. */
  2229. if (park) {
  2230. park = min(park, (unsigned) 3);
  2231. temp |= CMD_PARK;
  2232. temp |= park << 8;
  2233. }
  2234. oxu_dbg(oxu, "park %d\n", park);
  2235. }
  2236. if (HCC_PGM_FRAMELISTLEN(hcc_params)) {
  2237. /* periodic schedule size can be smaller than default */
  2238. temp &= ~(3 << 2);
  2239. temp |= (EHCI_TUNE_FLS << 2);
  2240. }
  2241. oxu->command = temp;
  2242. return 0;
  2243. }
  2244. /* Called during probe() after chip reset completes.
  2245. */
  2246. static int oxu_reset(struct usb_hcd *hcd)
  2247. {
  2248. struct oxu_hcd *oxu = hcd_to_oxu(hcd);
  2249. int ret;
  2250. spin_lock_init(&oxu->mem_lock);
  2251. INIT_LIST_HEAD(&oxu->urb_list);
  2252. oxu->urb_len = 0;
  2253. /* FIMXE */
  2254. hcd->self.controller->dma_mask = NULL;
  2255. if (oxu->is_otg) {
  2256. oxu->caps = hcd->regs + OXU_OTG_CAP_OFFSET;
  2257. oxu->regs = hcd->regs + OXU_OTG_CAP_OFFSET + \
  2258. HC_LENGTH(readl(&oxu->caps->hc_capbase));
  2259. oxu->mem = hcd->regs + OXU_SPH_MEM;
  2260. } else {
  2261. oxu->caps = hcd->regs + OXU_SPH_CAP_OFFSET;
  2262. oxu->regs = hcd->regs + OXU_SPH_CAP_OFFSET + \
  2263. HC_LENGTH(readl(&oxu->caps->hc_capbase));
  2264. oxu->mem = hcd->regs + OXU_OTG_MEM;
  2265. }
  2266. oxu->hcs_params = readl(&oxu->caps->hcs_params);
  2267. oxu->sbrn = 0x20;
  2268. ret = oxu_hcd_init(hcd);
  2269. if (ret)
  2270. return ret;
  2271. return 0;
  2272. }
  2273. static int oxu_run(struct usb_hcd *hcd)
  2274. {
  2275. struct oxu_hcd *oxu = hcd_to_oxu(hcd);
  2276. int retval;
  2277. u32 temp, hcc_params;
  2278. hcd->uses_new_polling = 1;
  2279. /* EHCI spec section 4.1 */
  2280. retval = ehci_reset(oxu);
  2281. if (retval != 0) {
  2282. ehci_mem_cleanup(oxu);
  2283. return retval;
  2284. }
  2285. writel(oxu->periodic_dma, &oxu->regs->frame_list);
  2286. writel((u32) oxu->async->qh_dma, &oxu->regs->async_next);
  2287. /* hcc_params controls whether oxu->regs->segment must (!!!)
  2288. * be used; it constrains QH/ITD/SITD and QTD locations.
  2289. * pci_pool consistent memory always uses segment zero.
  2290. * streaming mappings for I/O buffers, like pci_map_single(),
  2291. * can return segments above 4GB, if the device allows.
  2292. *
  2293. * NOTE: the dma mask is visible through dma_supported(), so
  2294. * drivers can pass this info along ... like NETIF_F_HIGHDMA,
  2295. * Scsi_Host.highmem_io, and so forth. It's readonly to all
  2296. * host side drivers though.
  2297. */
  2298. hcc_params = readl(&oxu->caps->hcc_params);
  2299. if (HCC_64BIT_ADDR(hcc_params))
  2300. writel(0, &oxu->regs->segment);
  2301. oxu->command &= ~(CMD_LRESET | CMD_IAAD | CMD_PSE |
  2302. CMD_ASE | CMD_RESET);
  2303. oxu->command |= CMD_RUN;
  2304. writel(oxu->command, &oxu->regs->command);
  2305. dbg_cmd(oxu, "init", oxu->command);
  2306. /*
  2307. * Start, enabling full USB 2.0 functionality ... usb 1.1 devices
  2308. * are explicitly handed to companion controller(s), so no TT is
  2309. * involved with the root hub. (Except where one is integrated,
  2310. * and there's no companion controller unless maybe for USB OTG.)
  2311. */
  2312. hcd->state = HC_STATE_RUNNING;
  2313. writel(FLAG_CF, &oxu->regs->configured_flag);
  2314. readl(&oxu->regs->command); /* unblock posted writes */
  2315. temp = HC_VERSION(readl(&oxu->caps->hc_capbase));
  2316. oxu_info(oxu, "USB %x.%x started, quasi-EHCI %x.%02x, driver %s%s\n",
  2317. ((oxu->sbrn & 0xf0)>>4), (oxu->sbrn & 0x0f),
  2318. temp >> 8, temp & 0xff, DRIVER_VERSION,
  2319. ignore_oc ? ", overcurrent ignored" : "");
  2320. writel(INTR_MASK, &oxu->regs->intr_enable); /* Turn On Interrupts */
  2321. return 0;
  2322. }
  2323. static void oxu_stop(struct usb_hcd *hcd)
  2324. {
  2325. struct oxu_hcd *oxu = hcd_to_oxu(hcd);
  2326. /* Turn off port power on all root hub ports. */
  2327. ehci_port_power(oxu, 0);
  2328. /* no more interrupts ... */
  2329. del_timer_sync(&oxu->watchdog);
  2330. spin_lock_irq(&oxu->lock);
  2331. if (HC_IS_RUNNING(hcd->state))
  2332. ehci_quiesce(oxu);
  2333. ehci_reset(oxu);
  2334. writel(0, &oxu->regs->intr_enable);
  2335. spin_unlock_irq(&oxu->lock);
  2336. /* let companion controllers work when we aren't */
  2337. writel(0, &oxu->regs->configured_flag);
  2338. /* root hub is shut down separately (first, when possible) */
  2339. spin_lock_irq(&oxu->lock);
  2340. if (oxu->async)
  2341. ehci_work(oxu);
  2342. spin_unlock_irq(&oxu->lock);
  2343. ehci_mem_cleanup(oxu);
  2344. dbg_status(oxu, "oxu_stop completed", readl(&oxu->regs->status));
  2345. }
  2346. /* Kick in for silicon on any bus (not just pci, etc).
  2347. * This forcibly disables dma and IRQs, helping kexec and other cases
  2348. * where the next system software may expect clean state.
  2349. */
  2350. static void oxu_shutdown(struct usb_hcd *hcd)
  2351. {
  2352. struct oxu_hcd *oxu = hcd_to_oxu(hcd);
  2353. (void) ehci_halt(oxu);
  2354. ehci_turn_off_all_ports(oxu);
  2355. /* make BIOS/etc use companion controller during reboot */
  2356. writel(0, &oxu->regs->configured_flag);
  2357. /* unblock posted writes */
  2358. readl(&oxu->regs->configured_flag);
  2359. }
  2360. /* Non-error returns are a promise to giveback() the urb later
  2361. * we drop ownership so next owner (or urb unlink) can get it
  2362. *
  2363. * urb + dev is in hcd.self.controller.urb_list
  2364. * we're queueing TDs onto software and hardware lists
  2365. *
  2366. * hcd-specific init for hcpriv hasn't been done yet
  2367. *
  2368. * NOTE: control, bulk, and interrupt share the same code to append TDs
  2369. * to a (possibly active) QH, and the same QH scanning code.
  2370. */
  2371. static int __oxu_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
  2372. gfp_t mem_flags)
  2373. {
  2374. struct oxu_hcd *oxu = hcd_to_oxu(hcd);
  2375. struct list_head qtd_list;
  2376. INIT_LIST_HEAD(&qtd_list);
  2377. switch (usb_pipetype(urb->pipe)) {
  2378. case PIPE_CONTROL:
  2379. case PIPE_BULK:
  2380. default:
  2381. if (!qh_urb_transaction(oxu, urb, &qtd_list, mem_flags))
  2382. return -ENOMEM;
  2383. return submit_async(oxu, urb, &qtd_list, mem_flags);
  2384. case PIPE_INTERRUPT:
  2385. if (!qh_urb_transaction(oxu, urb, &qtd_list, mem_flags))
  2386. return -ENOMEM;
  2387. return intr_submit(oxu, urb, &qtd_list, mem_flags);
  2388. case PIPE_ISOCHRONOUS:
  2389. if (urb->dev->speed == USB_SPEED_HIGH)
  2390. return itd_submit(oxu, urb, mem_flags);
  2391. else
  2392. return sitd_submit(oxu, urb, mem_flags);
  2393. }
  2394. }
  2395. /* This function is responsible for breaking URBs with big data size
  2396. * into smaller size and processing small urbs in sequence.
  2397. */
  2398. static int oxu_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
  2399. gfp_t mem_flags)
  2400. {
  2401. struct oxu_hcd *oxu = hcd_to_oxu(hcd);
  2402. int num, rem;
  2403. int transfer_buffer_length;
  2404. void *transfer_buffer;
  2405. struct urb *murb;
  2406. int i, ret;
  2407. /* If not bulk pipe just enqueue the URB */
  2408. if (!usb_pipebulk(urb->pipe))
  2409. return __oxu_urb_enqueue(hcd, urb, mem_flags);
  2410. /* Otherwise we should verify the USB transfer buffer size! */
  2411. transfer_buffer = urb->transfer_buffer;
  2412. transfer_buffer_length = urb->transfer_buffer_length;
  2413. num = urb->transfer_buffer_length / 4096;
  2414. rem = urb->transfer_buffer_length % 4096;
  2415. if (rem != 0)
  2416. num++;
  2417. /* If URB is smaller than 4096 bytes just enqueue it! */
  2418. if (num == 1)
  2419. return __oxu_urb_enqueue(hcd, urb, mem_flags);
  2420. /* Ok, we have more job to do! :) */
  2421. for (i = 0; i < num - 1; i++) {
  2422. /* Get free micro URB poll till a free urb is received */
  2423. do {
  2424. murb = (struct urb *) oxu_murb_alloc(oxu);
  2425. if (!murb)
  2426. schedule();
  2427. } while (!murb);
  2428. /* Coping the urb */
  2429. memcpy(murb, urb, sizeof(struct urb));
  2430. murb->transfer_buffer_length = 4096;
  2431. murb->transfer_buffer = transfer_buffer + i * 4096;
  2432. /* Null pointer for the encodes that this is a micro urb */
  2433. murb->complete = NULL;
  2434. ((struct oxu_murb *) murb)->main = urb;
  2435. ((struct oxu_murb *) murb)->last = 0;
  2436. /* This loop is to guarantee urb to be processed when there's
  2437. * not enough resources at a particular time by retrying.
  2438. */
  2439. do {
  2440. ret = __oxu_urb_enqueue(hcd, murb, mem_flags);
  2441. if (ret)
  2442. schedule();
  2443. } while (ret);
  2444. }
  2445. /* Last urb requires special handling */
  2446. /* Get free micro URB poll till a free urb is received */
  2447. do {
  2448. murb = (struct urb *) oxu_murb_alloc(oxu);
  2449. if (!murb)
  2450. schedule();
  2451. } while (!murb);
  2452. /* Coping the urb */
  2453. memcpy(murb, urb, sizeof(struct urb));
  2454. murb->transfer_buffer_length = rem > 0 ? rem : 4096;
  2455. murb->transfer_buffer = transfer_buffer + (num - 1) * 4096;
  2456. /* Null pointer for the encodes that this is a micro urb */
  2457. murb->complete = NULL;
  2458. ((struct oxu_murb *) murb)->main = urb;
  2459. ((struct oxu_murb *) murb)->last = 1;
  2460. do {
  2461. ret = __oxu_urb_enqueue(hcd, murb, mem_flags);
  2462. if (ret)
  2463. schedule();
  2464. } while (ret);
  2465. return ret;
  2466. }
  2467. /* Remove from hardware lists.
  2468. * Completions normally happen asynchronously
  2469. */
  2470. static int oxu_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  2471. {
  2472. struct oxu_hcd *oxu = hcd_to_oxu(hcd);
  2473. struct ehci_qh *qh;
  2474. unsigned long flags;
  2475. spin_lock_irqsave(&oxu->lock, flags);
  2476. switch (usb_pipetype(urb->pipe)) {
  2477. case PIPE_CONTROL:
  2478. case PIPE_BULK:
  2479. default:
  2480. qh = (struct ehci_qh *) urb->hcpriv;
  2481. if (!qh)
  2482. break;
  2483. unlink_async(oxu, qh);
  2484. break;
  2485. case PIPE_INTERRUPT:
  2486. qh = (struct ehci_qh *) urb->hcpriv;
  2487. if (!qh)
  2488. break;
  2489. switch (qh->qh_state) {
  2490. case QH_STATE_LINKED:
  2491. intr_deschedule(oxu, qh);
  2492. /* FALL THROUGH */
  2493. case QH_STATE_IDLE:
  2494. qh_completions(oxu, qh);
  2495. break;
  2496. default:
  2497. oxu_dbg(oxu, "bogus qh %p state %d\n",
  2498. qh, qh->qh_state);
  2499. goto done;
  2500. }
  2501. /* reschedule QH iff another request is queued */
  2502. if (!list_empty(&qh->qtd_list)
  2503. && HC_IS_RUNNING(hcd->state)) {
  2504. int status;
  2505. status = qh_schedule(oxu, qh);
  2506. spin_unlock_irqrestore(&oxu->lock, flags);
  2507. if (status != 0) {
  2508. /* shouldn't happen often, but ...
  2509. * FIXME kill those tds' urbs
  2510. */
  2511. err("can't reschedule qh %p, err %d",
  2512. qh, status);
  2513. }
  2514. return status;
  2515. }
  2516. break;
  2517. }
  2518. done:
  2519. spin_unlock_irqrestore(&oxu->lock, flags);
  2520. return 0;
  2521. }
  2522. /* Bulk qh holds the data toggle */
  2523. static void oxu_endpoint_disable(struct usb_hcd *hcd,
  2524. struct usb_host_endpoint *ep)
  2525. {
  2526. struct oxu_hcd *oxu = hcd_to_oxu(hcd);
  2527. unsigned long flags;
  2528. struct ehci_qh *qh, *tmp;
  2529. /* ASSERT: any requests/urbs are being unlinked */
  2530. /* ASSERT: nobody can be submitting urbs for this any more */
  2531. rescan:
  2532. spin_lock_irqsave(&oxu->lock, flags);
  2533. qh = ep->hcpriv;
  2534. if (!qh)
  2535. goto done;
  2536. /* endpoints can be iso streams. for now, we don't
  2537. * accelerate iso completions ... so spin a while.
  2538. */
  2539. if (qh->hw_info1 == 0) {
  2540. oxu_vdbg(oxu, "iso delay\n");
  2541. goto idle_timeout;
  2542. }
  2543. if (!HC_IS_RUNNING(hcd->state))
  2544. qh->qh_state = QH_STATE_IDLE;
  2545. switch (qh->qh_state) {
  2546. case QH_STATE_LINKED:
  2547. for (tmp = oxu->async->qh_next.qh;
  2548. tmp && tmp != qh;
  2549. tmp = tmp->qh_next.qh)
  2550. continue;
  2551. /* periodic qh self-unlinks on empty */
  2552. if (!tmp)
  2553. goto nogood;
  2554. unlink_async(oxu, qh);
  2555. /* FALL THROUGH */
  2556. case QH_STATE_UNLINK: /* wait for hw to finish? */
  2557. idle_timeout:
  2558. spin_unlock_irqrestore(&oxu->lock, flags);
  2559. schedule_timeout_uninterruptible(1);
  2560. goto rescan;
  2561. case QH_STATE_IDLE: /* fully unlinked */
  2562. if (list_empty(&qh->qtd_list)) {
  2563. qh_put(qh);
  2564. break;
  2565. }
  2566. /* else FALL THROUGH */
  2567. default:
  2568. nogood:
  2569. /* caller was supposed to have unlinked any requests;
  2570. * that's not our job. just leak this memory.
  2571. */
  2572. oxu_err(oxu, "qh %p (#%02x) state %d%s\n",
  2573. qh, ep->desc.bEndpointAddress, qh->qh_state,
  2574. list_empty(&qh->qtd_list) ? "" : "(has tds)");
  2575. break;
  2576. }
  2577. ep->hcpriv = NULL;
  2578. done:
  2579. spin_unlock_irqrestore(&oxu->lock, flags);
  2580. }
  2581. static int oxu_get_frame(struct usb_hcd *hcd)
  2582. {
  2583. struct oxu_hcd *oxu = hcd_to_oxu(hcd);
  2584. return (readl(&oxu->regs->frame_index) >> 3) %
  2585. oxu->periodic_size;
  2586. }
  2587. /* Build "status change" packet (one or two bytes) from HC registers */
  2588. static int oxu_hub_status_data(struct usb_hcd *hcd, char *buf)
  2589. {
  2590. struct oxu_hcd *oxu = hcd_to_oxu(hcd);
  2591. u32 temp, mask, status = 0;
  2592. int ports, i, retval = 1;
  2593. unsigned long flags;
  2594. /* if !USB_SUSPEND, root hub timers won't get shut down ... */
  2595. if (!HC_IS_RUNNING(hcd->state))
  2596. return 0;
  2597. /* init status to no-changes */
  2598. buf[0] = 0;
  2599. ports = HCS_N_PORTS(oxu->hcs_params);
  2600. if (ports > 7) {
  2601. buf[1] = 0;
  2602. retval++;
  2603. }
  2604. /* Some boards (mostly VIA?) report bogus overcurrent indications,
  2605. * causing massive log spam unless we completely ignore them. It
  2606. * may be relevant that VIA VT8235 controllers, where PORT_POWER is
  2607. * always set, seem to clear PORT_OCC and PORT_CSC when writing to
  2608. * PORT_POWER; that's surprising, but maybe within-spec.
  2609. */
  2610. if (!ignore_oc)
  2611. mask = PORT_CSC | PORT_PEC | PORT_OCC;
  2612. else
  2613. mask = PORT_CSC | PORT_PEC;
  2614. /* no hub change reports (bit 0) for now (power, ...) */
  2615. /* port N changes (bit N)? */
  2616. spin_lock_irqsave(&oxu->lock, flags);
  2617. for (i = 0; i < ports; i++) {
  2618. temp = readl(&oxu->regs->port_status[i]);
  2619. /*
  2620. * Return status information even for ports with OWNER set.
  2621. * Otherwise khubd wouldn't see the disconnect event when a
  2622. * high-speed device is switched over to the companion
  2623. * controller by the user.
  2624. */
  2625. if (!(temp & PORT_CONNECT))
  2626. oxu->reset_done[i] = 0;
  2627. if ((temp & mask) != 0 || ((temp & PORT_RESUME) != 0 &&
  2628. time_after_eq(jiffies, oxu->reset_done[i]))) {
  2629. if (i < 7)
  2630. buf[0] |= 1 << (i + 1);
  2631. else
  2632. buf[1] |= 1 << (i - 7);
  2633. status = STS_PCD;
  2634. }
  2635. }
  2636. /* FIXME autosuspend idle root hubs */
  2637. spin_unlock_irqrestore(&oxu->lock, flags);
  2638. return status ? retval : 0;
  2639. }
  2640. /* Returns the speed of a device attached to a port on the root hub. */
  2641. static inline unsigned int oxu_port_speed(struct oxu_hcd *oxu,
  2642. unsigned int portsc)
  2643. {
  2644. switch ((portsc >> 26) & 3) {
  2645. case 0:
  2646. return 0;
  2647. case 1:
  2648. return USB_PORT_STAT_LOW_SPEED;
  2649. case 2:
  2650. default:
  2651. return USB_PORT_STAT_HIGH_SPEED;
  2652. }
  2653. }
  2654. #define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
  2655. static int oxu_hub_control(struct usb_hcd *hcd, u16 typeReq,
  2656. u16 wValue, u16 wIndex, char *buf, u16 wLength)
  2657. {
  2658. struct oxu_hcd *oxu = hcd_to_oxu(hcd);
  2659. int ports = HCS_N_PORTS(oxu->hcs_params);
  2660. u32 __iomem *status_reg = &oxu->regs->port_status[wIndex - 1];
  2661. u32 temp, status;
  2662. unsigned long flags;
  2663. int retval = 0;
  2664. unsigned selector;
  2665. /*
  2666. * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
  2667. * HCS_INDICATOR may say we can change LEDs to off/amber/green.
  2668. * (track current state ourselves) ... blink for diagnostics,
  2669. * power, "this is the one", etc. EHCI spec supports this.
  2670. */
  2671. spin_lock_irqsave(&oxu->lock, flags);
  2672. switch (typeReq) {
  2673. case ClearHubFeature:
  2674. switch (wValue) {
  2675. case C_HUB_LOCAL_POWER:
  2676. case C_HUB_OVER_CURRENT:
  2677. /* no hub-wide feature/status flags */
  2678. break;
  2679. default:
  2680. goto error;
  2681. }
  2682. break;
  2683. case ClearPortFeature:
  2684. if (!wIndex || wIndex > ports)
  2685. goto error;
  2686. wIndex--;
  2687. temp = readl(status_reg);
  2688. /*
  2689. * Even if OWNER is set, so the port is owned by the
  2690. * companion controller, khubd needs to be able to clear
  2691. * the port-change status bits (especially
  2692. * USB_PORT_STAT_C_CONNECTION).
  2693. */
  2694. switch (wValue) {
  2695. case USB_PORT_FEAT_ENABLE:
  2696. writel(temp & ~PORT_PE, status_reg);
  2697. break;
  2698. case USB_PORT_FEAT_C_ENABLE:
  2699. writel((temp & ~PORT_RWC_BITS) | PORT_PEC, status_reg);
  2700. break;
  2701. case USB_PORT_FEAT_SUSPEND:
  2702. if (temp & PORT_RESET)
  2703. goto error;
  2704. if (temp & PORT_SUSPEND) {
  2705. if ((temp & PORT_PE) == 0)
  2706. goto error;
  2707. /* resume signaling for 20 msec */
  2708. temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
  2709. writel(temp | PORT_RESUME, status_reg);
  2710. oxu->reset_done[wIndex] = jiffies
  2711. + msecs_to_jiffies(20);
  2712. }
  2713. break;
  2714. case USB_PORT_FEAT_C_SUSPEND:
  2715. /* we auto-clear this feature */
  2716. break;
  2717. case USB_PORT_FEAT_POWER:
  2718. if (HCS_PPC(oxu->hcs_params))
  2719. writel(temp & ~(PORT_RWC_BITS | PORT_POWER),
  2720. status_reg);
  2721. break;
  2722. case USB_PORT_FEAT_C_CONNECTION:
  2723. writel((temp & ~PORT_RWC_BITS) | PORT_CSC, status_reg);
  2724. break;
  2725. case USB_PORT_FEAT_C_OVER_CURRENT:
  2726. writel((temp & ~PORT_RWC_BITS) | PORT_OCC, status_reg);
  2727. break;
  2728. case USB_PORT_FEAT_C_RESET:
  2729. /* GetPortStatus clears reset */
  2730. break;
  2731. default:
  2732. goto error;
  2733. }
  2734. readl(&oxu->regs->command); /* unblock posted write */
  2735. break;
  2736. case GetHubDescriptor:
  2737. ehci_hub_descriptor(oxu, (struct usb_hub_descriptor *)
  2738. buf);
  2739. break;
  2740. case GetHubStatus:
  2741. /* no hub-wide feature/status flags */
  2742. memset(buf, 0, 4);
  2743. break;
  2744. case GetPortStatus:
  2745. if (!wIndex || wIndex > ports)
  2746. goto error;
  2747. wIndex--;
  2748. status = 0;
  2749. temp = readl(status_reg);
  2750. /* wPortChange bits */
  2751. if (temp & PORT_CSC)
  2752. status |= USB_PORT_STAT_C_CONNECTION << 16;
  2753. if (temp & PORT_PEC)
  2754. status |= USB_PORT_STAT_C_ENABLE << 16;
  2755. if ((temp & PORT_OCC) && !ignore_oc)
  2756. status |= USB_PORT_STAT_C_OVERCURRENT << 16;
  2757. /* whoever resumes must GetPortStatus to complete it!! */
  2758. if (temp & PORT_RESUME) {
  2759. /* Remote Wakeup received? */
  2760. if (!oxu->reset_done[wIndex]) {
  2761. /* resume signaling for 20 msec */
  2762. oxu->reset_done[wIndex] = jiffies
  2763. + msecs_to_jiffies(20);
  2764. /* check the port again */
  2765. mod_timer(&oxu_to_hcd(oxu)->rh_timer,
  2766. oxu->reset_done[wIndex]);
  2767. }
  2768. /* resume completed? */
  2769. else if (time_after_eq(jiffies,
  2770. oxu->reset_done[wIndex])) {
  2771. status |= USB_PORT_STAT_C_SUSPEND << 16;
  2772. oxu->reset_done[wIndex] = 0;
  2773. /* stop resume signaling */
  2774. temp = readl(status_reg);
  2775. writel(temp & ~(PORT_RWC_BITS | PORT_RESUME),
  2776. status_reg);
  2777. retval = handshake(oxu, status_reg,
  2778. PORT_RESUME, 0, 2000 /* 2msec */);
  2779. if (retval != 0) {
  2780. oxu_err(oxu,
  2781. "port %d resume error %d\n",
  2782. wIndex + 1, retval);
  2783. goto error;
  2784. }
  2785. temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
  2786. }
  2787. }
  2788. /* whoever resets must GetPortStatus to complete it!! */
  2789. if ((temp & PORT_RESET)
  2790. && time_after_eq(jiffies,
  2791. oxu->reset_done[wIndex])) {
  2792. status |= USB_PORT_STAT_C_RESET << 16;
  2793. oxu->reset_done[wIndex] = 0;
  2794. /* force reset to complete */
  2795. writel(temp & ~(PORT_RWC_BITS | PORT_RESET),
  2796. status_reg);
  2797. /* REVISIT: some hardware needs 550+ usec to clear
  2798. * this bit; seems too long to spin routinely...
  2799. */
  2800. retval = handshake(oxu, status_reg,
  2801. PORT_RESET, 0, 750);
  2802. if (retval != 0) {
  2803. oxu_err(oxu, "port %d reset error %d\n",
  2804. wIndex + 1, retval);
  2805. goto error;
  2806. }
  2807. /* see what we found out */
  2808. temp = check_reset_complete(oxu, wIndex, status_reg,
  2809. readl(status_reg));
  2810. }
  2811. /* transfer dedicated ports to the companion hc */
  2812. if ((temp & PORT_CONNECT) &&
  2813. test_bit(wIndex, &oxu->companion_ports)) {
  2814. temp &= ~PORT_RWC_BITS;
  2815. temp |= PORT_OWNER;
  2816. writel(temp, status_reg);
  2817. oxu_dbg(oxu, "port %d --> companion\n", wIndex + 1);
  2818. temp = readl(status_reg);
  2819. }
  2820. /*
  2821. * Even if OWNER is set, there's no harm letting khubd
  2822. * see the wPortStatus values (they should all be 0 except
  2823. * for PORT_POWER anyway).
  2824. */
  2825. if (temp & PORT_CONNECT) {
  2826. status |= USB_PORT_STAT_CONNECTION;
  2827. /* status may be from integrated TT */
  2828. status |= oxu_port_speed(oxu, temp);
  2829. }
  2830. if (temp & PORT_PE)
  2831. status |= USB_PORT_STAT_ENABLE;
  2832. if (temp & (PORT_SUSPEND|PORT_RESUME))
  2833. status |= USB_PORT_STAT_SUSPEND;
  2834. if (temp & PORT_OC)
  2835. status |= USB_PORT_STAT_OVERCURRENT;
  2836. if (temp & PORT_RESET)
  2837. status |= USB_PORT_STAT_RESET;
  2838. if (temp & PORT_POWER)
  2839. status |= USB_PORT_STAT_POWER;
  2840. #ifndef OXU_VERBOSE_DEBUG
  2841. if (status & ~0xffff) /* only if wPortChange is interesting */
  2842. #endif
  2843. dbg_port(oxu, "GetStatus", wIndex + 1, temp);
  2844. put_unaligned(cpu_to_le32(status), (__le32 *) buf);
  2845. break;
  2846. case SetHubFeature:
  2847. switch (wValue) {
  2848. case C_HUB_LOCAL_POWER:
  2849. case C_HUB_OVER_CURRENT:
  2850. /* no hub-wide feature/status flags */
  2851. break;
  2852. default:
  2853. goto error;
  2854. }
  2855. break;
  2856. case SetPortFeature:
  2857. selector = wIndex >> 8;
  2858. wIndex &= 0xff;
  2859. if (!wIndex || wIndex > ports)
  2860. goto error;
  2861. wIndex--;
  2862. temp = readl(status_reg);
  2863. if (temp & PORT_OWNER)
  2864. break;
  2865. temp &= ~PORT_RWC_BITS;
  2866. switch (wValue) {
  2867. case USB_PORT_FEAT_SUSPEND:
  2868. if ((temp & PORT_PE) == 0
  2869. || (temp & PORT_RESET) != 0)
  2870. goto error;
  2871. if (device_may_wakeup(&hcd->self.root_hub->dev))
  2872. temp |= PORT_WAKE_BITS;
  2873. writel(temp | PORT_SUSPEND, status_reg);
  2874. break;
  2875. case USB_PORT_FEAT_POWER:
  2876. if (HCS_PPC(oxu->hcs_params))
  2877. writel(temp | PORT_POWER, status_reg);
  2878. break;
  2879. case USB_PORT_FEAT_RESET:
  2880. if (temp & PORT_RESUME)
  2881. goto error;
  2882. /* line status bits may report this as low speed,
  2883. * which can be fine if this root hub has a
  2884. * transaction translator built in.
  2885. */
  2886. oxu_vdbg(oxu, "port %d reset\n", wIndex + 1);
  2887. temp |= PORT_RESET;
  2888. temp &= ~PORT_PE;
  2889. /*
  2890. * caller must wait, then call GetPortStatus
  2891. * usb 2.0 spec says 50 ms resets on root
  2892. */
  2893. oxu->reset_done[wIndex] = jiffies
  2894. + msecs_to_jiffies(50);
  2895. writel(temp, status_reg);
  2896. break;
  2897. /* For downstream facing ports (these): one hub port is put
  2898. * into test mode according to USB2 11.24.2.13, then the hub
  2899. * must be reset (which for root hub now means rmmod+modprobe,
  2900. * or else system reboot). See EHCI 2.3.9 and 4.14 for info
  2901. * about the EHCI-specific stuff.
  2902. */
  2903. case USB_PORT_FEAT_TEST:
  2904. if (!selector || selector > 5)
  2905. goto error;
  2906. ehci_quiesce(oxu);
  2907. ehci_halt(oxu);
  2908. temp |= selector << 16;
  2909. writel(temp, status_reg);
  2910. break;
  2911. default:
  2912. goto error;
  2913. }
  2914. readl(&oxu->regs->command); /* unblock posted writes */
  2915. break;
  2916. default:
  2917. error:
  2918. /* "stall" on error */
  2919. retval = -EPIPE;
  2920. }
  2921. spin_unlock_irqrestore(&oxu->lock, flags);
  2922. return retval;
  2923. }
  2924. #ifdef CONFIG_PM
  2925. static int oxu_bus_suspend(struct usb_hcd *hcd)
  2926. {
  2927. struct oxu_hcd *oxu = hcd_to_oxu(hcd);
  2928. int port;
  2929. int mask;
  2930. oxu_dbg(oxu, "suspend root hub\n");
  2931. if (time_before(jiffies, oxu->next_statechange))
  2932. msleep(5);
  2933. port = HCS_N_PORTS(oxu->hcs_params);
  2934. spin_lock_irq(&oxu->lock);
  2935. /* stop schedules, clean any completed work */
  2936. if (HC_IS_RUNNING(hcd->state)) {
  2937. ehci_quiesce(oxu);
  2938. hcd->state = HC_STATE_QUIESCING;
  2939. }
  2940. oxu->command = readl(&oxu->regs->command);
  2941. if (oxu->reclaim)
  2942. oxu->reclaim_ready = 1;
  2943. ehci_work(oxu);
  2944. /* Unlike other USB host controller types, EHCI doesn't have
  2945. * any notion of "global" or bus-wide suspend. The driver has
  2946. * to manually suspend all the active unsuspended ports, and
  2947. * then manually resume them in the bus_resume() routine.
  2948. */
  2949. oxu->bus_suspended = 0;
  2950. while (port--) {
  2951. u32 __iomem *reg = &oxu->regs->port_status[port];
  2952. u32 t1 = readl(reg) & ~PORT_RWC_BITS;
  2953. u32 t2 = t1;
  2954. /* keep track of which ports we suspend */
  2955. if ((t1 & PORT_PE) && !(t1 & PORT_OWNER) &&
  2956. !(t1 & PORT_SUSPEND)) {
  2957. t2 |= PORT_SUSPEND;
  2958. set_bit(port, &oxu->bus_suspended);
  2959. }
  2960. /* enable remote wakeup on all ports */
  2961. if (device_may_wakeup(&hcd->self.root_hub->dev))
  2962. t2 |= PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E;
  2963. else
  2964. t2 &= ~(PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E);
  2965. if (t1 != t2) {
  2966. oxu_vdbg(oxu, "port %d, %08x -> %08x\n",
  2967. port + 1, t1, t2);
  2968. writel(t2, reg);
  2969. }
  2970. }
  2971. /* turn off now-idle HC */
  2972. del_timer_sync(&oxu->watchdog);
  2973. ehci_halt(oxu);
  2974. hcd->state = HC_STATE_SUSPENDED;
  2975. /* allow remote wakeup */
  2976. mask = INTR_MASK;
  2977. if (!device_may_wakeup(&hcd->self.root_hub->dev))
  2978. mask &= ~STS_PCD;
  2979. writel(mask, &oxu->regs->intr_enable);
  2980. readl(&oxu->regs->intr_enable);
  2981. oxu->next_statechange = jiffies + msecs_to_jiffies(10);
  2982. spin_unlock_irq(&oxu->lock);
  2983. return 0;
  2984. }
  2985. /* Caller has locked the root hub, and should reset/reinit on error */
  2986. static int oxu_bus_resume(struct usb_hcd *hcd)
  2987. {
  2988. struct oxu_hcd *oxu = hcd_to_oxu(hcd);
  2989. u32 temp;
  2990. int i;
  2991. if (time_before(jiffies, oxu->next_statechange))
  2992. msleep(5);
  2993. spin_lock_irq(&oxu->lock);
  2994. /* Ideally and we've got a real resume here, and no port's power
  2995. * was lost. (For PCI, that means Vaux was maintained.) But we
  2996. * could instead be restoring a swsusp snapshot -- so that BIOS was
  2997. * the last user of the controller, not reset/pm hardware keeping
  2998. * state we gave to it.
  2999. */
  3000. temp = readl(&oxu->regs->intr_enable);
  3001. oxu_dbg(oxu, "resume root hub%s\n", temp ? "" : " after power loss");
  3002. /* at least some APM implementations will try to deliver
  3003. * IRQs right away, so delay them until we're ready.
  3004. */
  3005. writel(0, &oxu->regs->intr_enable);
  3006. /* re-init operational registers */
  3007. writel(0, &oxu->regs->segment);
  3008. writel(oxu->periodic_dma, &oxu->regs->frame_list);
  3009. writel((u32) oxu->async->qh_dma, &oxu->regs->async_next);
  3010. /* restore CMD_RUN, framelist size, and irq threshold */
  3011. writel(oxu->command, &oxu->regs->command);
  3012. /* Some controller/firmware combinations need a delay during which
  3013. * they set up the port statuses. See Bugzilla #8190. */
  3014. mdelay(8);
  3015. /* manually resume the ports we suspended during bus_suspend() */
  3016. i = HCS_N_PORTS(oxu->hcs_params);
  3017. while (i--) {
  3018. temp = readl(&oxu->regs->port_status[i]);
  3019. temp &= ~(PORT_RWC_BITS
  3020. | PORT_WKOC_E | PORT_WKDISC_E | PORT_WKCONN_E);
  3021. if (test_bit(i, &oxu->bus_suspended) && (temp & PORT_SUSPEND)) {
  3022. oxu->reset_done[i] = jiffies + msecs_to_jiffies(20);
  3023. temp |= PORT_RESUME;
  3024. }
  3025. writel(temp, &oxu->regs->port_status[i]);
  3026. }
  3027. i = HCS_N_PORTS(oxu->hcs_params);
  3028. mdelay(20);
  3029. while (i--) {
  3030. temp = readl(&oxu->regs->port_status[i]);
  3031. if (test_bit(i, &oxu->bus_suspended) && (temp & PORT_SUSPEND)) {
  3032. temp &= ~(PORT_RWC_BITS | PORT_RESUME);
  3033. writel(temp, &oxu->regs->port_status[i]);
  3034. oxu_vdbg(oxu, "resumed port %d\n", i + 1);
  3035. }
  3036. }
  3037. (void) readl(&oxu->regs->command);
  3038. /* maybe re-activate the schedule(s) */
  3039. temp = 0;
  3040. if (oxu->async->qh_next.qh)
  3041. temp |= CMD_ASE;
  3042. if (oxu->periodic_sched)
  3043. temp |= CMD_PSE;
  3044. if (temp) {
  3045. oxu->command |= temp;
  3046. writel(oxu->command, &oxu->regs->command);
  3047. }
  3048. oxu->next_statechange = jiffies + msecs_to_jiffies(5);
  3049. hcd->state = HC_STATE_RUNNING;
  3050. /* Now we can safely re-enable irqs */
  3051. writel(INTR_MASK, &oxu->regs->intr_enable);
  3052. spin_unlock_irq(&oxu->lock);
  3053. return 0;
  3054. }
  3055. #else
  3056. static int oxu_bus_suspend(struct usb_hcd *hcd)
  3057. {
  3058. return 0;
  3059. }
  3060. static int oxu_bus_resume(struct usb_hcd *hcd)
  3061. {
  3062. return 0;
  3063. }
  3064. #endif /* CONFIG_PM */
  3065. static const struct hc_driver oxu_hc_driver = {
  3066. .description = "oxu210hp_hcd",
  3067. .product_desc = "oxu210hp HCD",
  3068. .hcd_priv_size = sizeof(struct oxu_hcd),
  3069. /*
  3070. * Generic hardware linkage
  3071. */
  3072. .irq = oxu_irq,
  3073. .flags = HCD_MEMORY | HCD_USB2,
  3074. /*
  3075. * Basic lifecycle operations
  3076. */
  3077. .reset = oxu_reset,
  3078. .start = oxu_run,
  3079. .stop = oxu_stop,
  3080. .shutdown = oxu_shutdown,
  3081. /*
  3082. * Managing i/o requests and associated device resources
  3083. */
  3084. .urb_enqueue = oxu_urb_enqueue,
  3085. .urb_dequeue = oxu_urb_dequeue,
  3086. .endpoint_disable = oxu_endpoint_disable,
  3087. /*
  3088. * Scheduling support
  3089. */
  3090. .get_frame_number = oxu_get_frame,
  3091. /*
  3092. * Root hub support
  3093. */
  3094. .hub_status_data = oxu_hub_status_data,
  3095. .hub_control = oxu_hub_control,
  3096. .bus_suspend = oxu_bus_suspend,
  3097. .bus_resume = oxu_bus_resume,
  3098. };
  3099. /*
  3100. * Module stuff
  3101. */
  3102. static void oxu_configuration(struct platform_device *pdev, void *base)
  3103. {
  3104. u32 tmp;
  3105. /* Initialize top level registers.
  3106. * First write ever
  3107. */
  3108. oxu_writel(base, OXU_HOSTIFCONFIG, 0x0000037D);
  3109. oxu_writel(base, OXU_SOFTRESET, OXU_SRESET);
  3110. oxu_writel(base, OXU_HOSTIFCONFIG, 0x0000037D);
  3111. tmp = oxu_readl(base, OXU_PIOBURSTREADCTRL);
  3112. oxu_writel(base, OXU_PIOBURSTREADCTRL, tmp | 0x0040);
  3113. oxu_writel(base, OXU_ASO, OXU_SPHPOEN | OXU_OVRCCURPUPDEN |
  3114. OXU_COMPARATOR | OXU_ASO_OP);
  3115. tmp = oxu_readl(base, OXU_CLKCTRL_SET);
  3116. oxu_writel(base, OXU_CLKCTRL_SET, tmp | OXU_SYSCLKEN | OXU_USBOTGCLKEN);
  3117. /* Clear all top interrupt enable */
  3118. oxu_writel(base, OXU_CHIPIRQEN_CLR, 0xff);
  3119. /* Clear all top interrupt status */
  3120. oxu_writel(base, OXU_CHIPIRQSTATUS, 0xff);
  3121. /* Enable all needed top interrupt except OTG SPH core */
  3122. oxu_writel(base, OXU_CHIPIRQEN_SET, OXU_USBSPHLPWUI | OXU_USBOTGLPWUI);
  3123. }
  3124. static int oxu_verify_id(struct platform_device *pdev, void *base)
  3125. {
  3126. u32 id;
  3127. static const char * const bo[] = {
  3128. "reserved",
  3129. "128-pin LQFP",
  3130. "84-pin TFBGA",
  3131. "reserved",
  3132. };
  3133. /* Read controller signature register to find a match */
  3134. id = oxu_readl(base, OXU_DEVICEID);
  3135. dev_info(&pdev->dev, "device ID %x\n", id);
  3136. if ((id & OXU_REV_MASK) != (OXU_REV_2100 << OXU_REV_SHIFT))
  3137. return -1;
  3138. dev_info(&pdev->dev, "found device %x %s (%04x:%04x)\n",
  3139. id >> OXU_REV_SHIFT,
  3140. bo[(id & OXU_BO_MASK) >> OXU_BO_SHIFT],
  3141. (id & OXU_MAJ_REV_MASK) >> OXU_MAJ_REV_SHIFT,
  3142. (id & OXU_MIN_REV_MASK) >> OXU_MIN_REV_SHIFT);
  3143. return 0;
  3144. }
  3145. static const struct hc_driver oxu_hc_driver;
  3146. static struct usb_hcd *oxu_create(struct platform_device *pdev,
  3147. unsigned long memstart, unsigned long memlen,
  3148. void *base, int irq, int otg)
  3149. {
  3150. struct device *dev = &pdev->dev;
  3151. struct usb_hcd *hcd;
  3152. struct oxu_hcd *oxu;
  3153. int ret;
  3154. /* Set endian mode and host mode */
  3155. oxu_writel(base + (otg ? OXU_OTG_CORE_OFFSET : OXU_SPH_CORE_OFFSET),
  3156. OXU_USBMODE,
  3157. OXU_CM_HOST_ONLY | OXU_ES_LITTLE | OXU_VBPS);
  3158. hcd = usb_create_hcd(&oxu_hc_driver, dev,
  3159. otg ? "oxu210hp_otg" : "oxu210hp_sph");
  3160. if (!hcd)
  3161. return ERR_PTR(-ENOMEM);
  3162. hcd->rsrc_start = memstart;
  3163. hcd->rsrc_len = memlen;
  3164. hcd->regs = base;
  3165. hcd->irq = irq;
  3166. hcd->state = HC_STATE_HALT;
  3167. oxu = hcd_to_oxu(hcd);
  3168. oxu->is_otg = otg;
  3169. ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
  3170. if (ret < 0)
  3171. return ERR_PTR(ret);
  3172. return hcd;
  3173. }
  3174. static int oxu_init(struct platform_device *pdev,
  3175. unsigned long memstart, unsigned long memlen,
  3176. void *base, int irq)
  3177. {
  3178. struct oxu_info *info = platform_get_drvdata(pdev);
  3179. struct usb_hcd *hcd;
  3180. int ret;
  3181. /* First time configuration at start up */
  3182. oxu_configuration(pdev, base);
  3183. ret = oxu_verify_id(pdev, base);
  3184. if (ret) {
  3185. dev_err(&pdev->dev, "no devices found!\n");
  3186. return -ENODEV;
  3187. }
  3188. /* Create the OTG controller */
  3189. hcd = oxu_create(pdev, memstart, memlen, base, irq, 1);
  3190. if (IS_ERR(hcd)) {
  3191. dev_err(&pdev->dev, "cannot create OTG controller!\n");
  3192. ret = PTR_ERR(hcd);
  3193. goto error_create_otg;
  3194. }
  3195. info->hcd[0] = hcd;
  3196. /* Create the SPH host controller */
  3197. hcd = oxu_create(pdev, memstart, memlen, base, irq, 0);
  3198. if (IS_ERR(hcd)) {
  3199. dev_err(&pdev->dev, "cannot create SPH controller!\n");
  3200. ret = PTR_ERR(hcd);
  3201. goto error_create_sph;
  3202. }
  3203. info->hcd[1] = hcd;
  3204. oxu_writel(base, OXU_CHIPIRQEN_SET,
  3205. oxu_readl(base, OXU_CHIPIRQEN_SET) | 3);
  3206. return 0;
  3207. error_create_sph:
  3208. usb_remove_hcd(info->hcd[0]);
  3209. usb_put_hcd(info->hcd[0]);
  3210. error_create_otg:
  3211. return ret;
  3212. }
  3213. static int oxu_drv_probe(struct platform_device *pdev)
  3214. {
  3215. struct resource *res;
  3216. void *base;
  3217. unsigned long memstart, memlen;
  3218. int irq, ret;
  3219. struct oxu_info *info;
  3220. if (usb_disabled())
  3221. return -ENODEV;
  3222. /*
  3223. * Get the platform resources
  3224. */
  3225. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  3226. if (!res) {
  3227. dev_err(&pdev->dev,
  3228. "no IRQ! Check %s setup!\n", dev_name(&pdev->dev));
  3229. return -ENODEV;
  3230. }
  3231. irq = res->start;
  3232. dev_dbg(&pdev->dev, "IRQ resource %d\n", irq);
  3233. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  3234. if (!res) {
  3235. dev_err(&pdev->dev, "no registers address! Check %s setup!\n",
  3236. dev_name(&pdev->dev));
  3237. return -ENODEV;
  3238. }
  3239. memstart = res->start;
  3240. memlen = resource_size(res);
  3241. dev_dbg(&pdev->dev, "MEM resource %lx-%lx\n", memstart, memlen);
  3242. if (!request_mem_region(memstart, memlen,
  3243. oxu_hc_driver.description)) {
  3244. dev_dbg(&pdev->dev, "memory area already in use\n");
  3245. return -EBUSY;
  3246. }
  3247. ret = irq_set_irq_type(irq, IRQF_TRIGGER_FALLING);
  3248. if (ret) {
  3249. dev_err(&pdev->dev, "error setting irq type\n");
  3250. ret = -EFAULT;
  3251. goto error_set_irq_type;
  3252. }
  3253. base = ioremap(memstart, memlen);
  3254. if (!base) {
  3255. dev_dbg(&pdev->dev, "error mapping memory\n");
  3256. ret = -EFAULT;
  3257. goto error_ioremap;
  3258. }
  3259. /* Allocate a driver data struct to hold useful info for both
  3260. * SPH & OTG devices
  3261. */
  3262. info = kzalloc(sizeof(struct oxu_info), GFP_KERNEL);
  3263. if (!info) {
  3264. dev_dbg(&pdev->dev, "error allocating memory\n");
  3265. ret = -EFAULT;
  3266. goto error_alloc;
  3267. }
  3268. platform_set_drvdata(pdev, info);
  3269. ret = oxu_init(pdev, memstart, memlen, base, irq);
  3270. if (ret < 0) {
  3271. dev_dbg(&pdev->dev, "cannot init USB devices\n");
  3272. goto error_init;
  3273. }
  3274. dev_info(&pdev->dev, "devices enabled and running\n");
  3275. platform_set_drvdata(pdev, info);
  3276. return 0;
  3277. error_init:
  3278. kfree(info);
  3279. platform_set_drvdata(pdev, NULL);
  3280. error_alloc:
  3281. iounmap(base);
  3282. error_set_irq_type:
  3283. error_ioremap:
  3284. release_mem_region(memstart, memlen);
  3285. dev_err(&pdev->dev, "init %s fail, %d\n", dev_name(&pdev->dev), ret);
  3286. return ret;
  3287. }
  3288. static void oxu_remove(struct platform_device *pdev, struct usb_hcd *hcd)
  3289. {
  3290. usb_remove_hcd(hcd);
  3291. usb_put_hcd(hcd);
  3292. }
  3293. static int oxu_drv_remove(struct platform_device *pdev)
  3294. {
  3295. struct oxu_info *info = platform_get_drvdata(pdev);
  3296. unsigned long memstart = info->hcd[0]->rsrc_start,
  3297. memlen = info->hcd[0]->rsrc_len;
  3298. void *base = info->hcd[0]->regs;
  3299. oxu_remove(pdev, info->hcd[0]);
  3300. oxu_remove(pdev, info->hcd[1]);
  3301. iounmap(base);
  3302. release_mem_region(memstart, memlen);
  3303. kfree(info);
  3304. platform_set_drvdata(pdev, NULL);
  3305. return 0;
  3306. }
  3307. static void oxu_drv_shutdown(struct platform_device *pdev)
  3308. {
  3309. oxu_drv_remove(pdev);
  3310. }
  3311. #if 0
  3312. /* FIXME: TODO */
  3313. static int oxu_drv_suspend(struct device *dev)
  3314. {
  3315. struct platform_device *pdev = to_platform_device(dev);
  3316. struct usb_hcd *hcd = dev_get_drvdata(dev);
  3317. return 0;
  3318. }
  3319. static int oxu_drv_resume(struct device *dev)
  3320. {
  3321. struct platform_device *pdev = to_platform_device(dev);
  3322. struct usb_hcd *hcd = dev_get_drvdata(dev);
  3323. return 0;
  3324. }
  3325. #else
  3326. #define oxu_drv_suspend NULL
  3327. #define oxu_drv_resume NULL
  3328. #endif
  3329. static struct platform_driver oxu_driver = {
  3330. .probe = oxu_drv_probe,
  3331. .remove = oxu_drv_remove,
  3332. .shutdown = oxu_drv_shutdown,
  3333. .suspend = oxu_drv_suspend,
  3334. .resume = oxu_drv_resume,
  3335. .driver = {
  3336. .name = "oxu210hp-hcd",
  3337. .bus = &platform_bus_type
  3338. }
  3339. };
  3340. module_platform_driver(oxu_driver);
  3341. MODULE_DESCRIPTION("Oxford OXU210HP HCD driver - ver. " DRIVER_VERSION);
  3342. MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
  3343. MODULE_LICENSE("GPL");