oxu210hp-hcd.c 99 KB

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