301-usb_support.patch 174 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301
  1. --- a/drivers/usb/Makefile
  2. +++ b/drivers/usb/Makefile
  3. @@ -16,6 +16,7 @@
  4. obj-$(CONFIG_USB_SL811_HCD) += host/
  5. obj-$(CONFIG_USB_U132_HCD) += host/
  6. obj-$(CONFIG_USB_R8A66597_HCD) += host/
  7. +obj-$(CONFIG_ETRAX_USB_HOST) += host/
  8. obj-$(CONFIG_USB_ACM) += class/
  9. obj-$(CONFIG_USB_PRINTER) += class/
  10. --- a/drivers/usb/host/Makefile
  11. +++ b/drivers/usb/host/Makefile
  12. @@ -17,3 +17,5 @@
  13. obj-$(CONFIG_USB_U132_HCD) += u132-hcd.o
  14. obj-$(CONFIG_USB_R8A66597_HCD) += r8a66597-hcd.o
  15. +#obj-$(CONFIG_USB_CARNEOL) += hc-crisv10.o
  16. +obj-$(CONFIG_ETRAX_USB_HOST) += hc-crisv10.o
  17. --- /dev/null
  18. +++ b/drivers/usb/host/hc-cris-dbg.h
  19. @@ -0,0 +1,143 @@
  20. +
  21. +/* macros for debug output */
  22. +
  23. +#define hcd_dbg(hcd, fmt, args...) \
  24. + dev_info(hcd->self.controller, fmt, ## args)
  25. +#define hcd_err(hcd, fmt, args...) \
  26. + dev_err(hcd->self.controller, fmt, ## args)
  27. +#define hcd_info(hcd, fmt, args...) \
  28. + dev_info(hcd->self.controller, fmt, ## args)
  29. +#define hcd_warn(hcd, fmt, args...) \
  30. + dev_warn(hcd->self.controller, fmt, ## args)
  31. +
  32. +/*
  33. +#define devdrv_dbg(fmt, args...) \
  34. + printk(KERN_INFO "usb_devdrv dbg: ");printk(fmt, ## args)
  35. +*/
  36. +#define devdrv_dbg(fmt, args...) {}
  37. +
  38. +#define devdrv_err(fmt, args...) \
  39. + printk(KERN_ERR "usb_devdrv error: ");printk(fmt, ## args)
  40. +#define devdrv_info(fmt, args...) \
  41. + printk(KERN_INFO "usb_devdrv: ");printk(fmt, ## args)
  42. +
  43. +#define irq_dbg(fmt, args...) \
  44. + printk(KERN_INFO "crisv10_irq dbg: ");printk(fmt, ## args)
  45. +#define irq_err(fmt, args...) \
  46. + printk(KERN_ERR "crisv10_irq error: ");printk(fmt, ## args)
  47. +#define irq_warn(fmt, args...) \
  48. + printk(KERN_INFO "crisv10_irq warn: ");printk(fmt, ## args)
  49. +#define irq_info(fmt, args...) \
  50. + printk(KERN_INFO "crisv10_hcd: ");printk(fmt, ## args)
  51. +
  52. +/*
  53. +#define rh_dbg(fmt, args...) \
  54. + printk(KERN_DEBUG "crisv10_rh dbg: ");printk(fmt, ## args)
  55. +*/
  56. +#define rh_dbg(fmt, args...) {}
  57. +
  58. +#define rh_err(fmt, args...) \
  59. + printk(KERN_ERR "crisv10_rh error: ");printk(fmt, ## args)
  60. +#define rh_warn(fmt, args...) \
  61. + printk(KERN_INFO "crisv10_rh warning: ");printk(fmt, ## args)
  62. +#define rh_info(fmt, args...) \
  63. + printk(KERN_INFO "crisv10_rh: ");printk(fmt, ## args)
  64. +
  65. +/*
  66. +#define tc_dbg(fmt, args...) \
  67. + printk(KERN_INFO "crisv10_tc dbg: ");printk(fmt, ## args)
  68. +*/
  69. +#define tc_dbg(fmt, args...) {while(0){}}
  70. +
  71. +#define tc_err(fmt, args...) \
  72. + printk(KERN_ERR "crisv10_tc error: ");printk(fmt, ## args)
  73. +/*
  74. +#define tc_warn(fmt, args...) \
  75. + printk(KERN_INFO "crisv10_tc warning: ");printk(fmt, ## args)
  76. +*/
  77. +#define tc_warn(fmt, args...) {while(0){}}
  78. +
  79. +#define tc_info(fmt, args...) \
  80. + printk(KERN_INFO "crisv10_tc: ");printk(fmt, ## args)
  81. +
  82. +
  83. +/* Debug print-outs for various traffic types */
  84. +
  85. +#define intr_warn(fmt, args...) \
  86. + printk(KERN_INFO "crisv10_intr warning: ");printk(fmt, ## args)
  87. +
  88. +#define intr_dbg(fmt, args...) \
  89. + printk(KERN_DEBUG "crisv10_intr dbg: ");printk(fmt, ## args)
  90. +/*
  91. +#define intr_dbg(fmt, args...) {while(0){}}
  92. +*/
  93. +
  94. +
  95. +#define isoc_err(fmt, args...) \
  96. + printk(KERN_ERR "crisv10_isoc error: ");printk(fmt, ## args)
  97. +/*
  98. +#define isoc_warn(fmt, args...) \
  99. + printk(KERN_INFO "crisv10_isoc warning: ");printk(fmt, ## args)
  100. +*/
  101. +#define isoc_warn(fmt, args...) {while(0){}}
  102. +
  103. +/*
  104. +#define isoc_dbg(fmt, args...) \
  105. + printk(KERN_INFO "crisv10_isoc dbg: ");printk(fmt, ## args)
  106. +*/
  107. +#define isoc_dbg(fmt, args...) {while(0){}}
  108. +
  109. +/*
  110. +#define timer_warn(fmt, args...) \
  111. + printk(KERN_INFO "crisv10_timer warning: ");printk(fmt, ## args)
  112. +*/
  113. +#define timer_warn(fmt, args...) {while(0){}}
  114. +
  115. +/*
  116. +#define timer_dbg(fmt, args...) \
  117. + printk(KERN_INFO "crisv10_timer dbg: ");printk(fmt, ## args)
  118. +*/
  119. +#define timer_dbg(fmt, args...) {while(0){}}
  120. +
  121. +
  122. +/* Debug printouts for events related to late finishing of URBs */
  123. +
  124. +#define late_dbg(fmt, args...) \
  125. + printk(KERN_INFO "crisv10_late dbg: ");printk(fmt, ## args)
  126. +/*
  127. +#define late_dbg(fmt, args...) {while(0){}}
  128. +*/
  129. +
  130. +#define late_warn(fmt, args...) \
  131. + printk(KERN_INFO "crisv10_late warning: ");printk(fmt, ## args)
  132. +/*
  133. +#define errno_dbg(fmt, args...) \
  134. + printk(KERN_INFO "crisv10_errno dbg: ");printk(fmt, ## args)
  135. +*/
  136. +#define errno_dbg(fmt, args...) {while(0){}}
  137. +
  138. +
  139. +#define dma_dbg(fmt, args...) \
  140. + printk(KERN_INFO "crisv10_dma dbg: ");printk(fmt, ## args)
  141. +#define dma_err(fmt, args...) \
  142. + printk(KERN_ERR "crisv10_dma error: ");printk(fmt, ## args)
  143. +#define dma_warn(fmt, args...) \
  144. + printk(KERN_INFO "crisv10_dma warning: ");printk(fmt, ## args)
  145. +#define dma_info(fmt, args...) \
  146. + printk(KERN_INFO "crisv10_dma: ");printk(fmt, ## args)
  147. +
  148. +
  149. +
  150. +#define str_dir(pipe) \
  151. + (usb_pipeout(pipe) ? "out" : "in")
  152. +#define str_type(pipe) \
  153. + ({ \
  154. + char *s = "?"; \
  155. + switch (usb_pipetype(pipe)) { \
  156. + case PIPE_ISOCHRONOUS: s = "iso"; break; \
  157. + case PIPE_INTERRUPT: s = "intr"; break; \
  158. + case PIPE_CONTROL: s = "ctrl"; break; \
  159. + case PIPE_BULK: s = "bulk"; break; \
  160. + }; \
  161. + s; \
  162. + })
  163. --- /dev/null
  164. +++ b/drivers/usb/host/hc-crisv10.c
  165. @@ -0,0 +1,4800 @@
  166. +/*
  167. + *
  168. + * ETRAX 100LX USB Host Controller Driver
  169. + *
  170. + * Copyright (C) 2005, 2006 Axis Communications AB
  171. + *
  172. + * Author: Konrad Eriksson <[email protected]>
  173. + *
  174. + */
  175. +
  176. +#include <linux/module.h>
  177. +#include <linux/kernel.h>
  178. +#include <linux/init.h>
  179. +#include <linux/moduleparam.h>
  180. +#include <linux/spinlock.h>
  181. +#include <linux/usb.h>
  182. +#include <linux/platform_device.h>
  183. +
  184. +#include <asm/io.h>
  185. +#include <asm/irq.h>
  186. +#include <asm/arch/dma.h>
  187. +#include <asm/arch/io_interface_mux.h>
  188. +
  189. +#include "../core/hcd.h"
  190. +#include "../core/hub.h"
  191. +#include "hc-crisv10.h"
  192. +#include "hc-cris-dbg.h"
  193. +
  194. +
  195. +/***************************************************************************/
  196. +/***************************************************************************/
  197. +/* Host Controller settings */
  198. +/***************************************************************************/
  199. +/***************************************************************************/
  200. +
  201. +#define VERSION "1.00 hinko.4"
  202. +#define COPYRIGHT "(c) 2005, 2006 Axis Communications AB"
  203. +#define DESCRIPTION "ETRAX 100LX USB Host Controller (2.6.25-rc9 port)"
  204. +
  205. +#define ETRAX_USB_HC_IRQ USB_HC_IRQ_NBR
  206. +#define ETRAX_USB_RX_IRQ USB_DMA_RX_IRQ_NBR
  207. +#define ETRAX_USB_TX_IRQ USB_DMA_TX_IRQ_NBR
  208. +
  209. +/* Number of physical ports in Etrax 100LX */
  210. +#define USB_ROOT_HUB_PORTS 2
  211. +
  212. +const char hc_name[] = "hc-crisv10";
  213. +const char product_desc[] = DESCRIPTION;
  214. +
  215. +/* The number of epids is, among other things, used for pre-allocating
  216. + ctrl, bulk and isoc EP descriptors (one for each epid).
  217. + Assumed to be > 1 when initiating the DMA lists. */
  218. +#define NBR_OF_EPIDS 32
  219. +
  220. +/* Support interrupt traffic intervals up to 128 ms. */
  221. +#define MAX_INTR_INTERVAL 128
  222. +
  223. +/* If periodic traffic (intr or isoc) is to be used, then one entry in the EP
  224. + table must be "invalid". By this we mean that we shouldn't care about epid
  225. + attentions for this epid, or at least handle them differently from epid
  226. + attentions for "valid" epids. This define determines which one to use
  227. + (don't change it). */
  228. +#define INVALID_EPID 31
  229. +/* A special epid for the bulk dummys. */
  230. +#define DUMMY_EPID 30
  231. +
  232. +/* Module settings */
  233. +
  234. +MODULE_DESCRIPTION(DESCRIPTION);
  235. +MODULE_LICENSE("GPL");
  236. +MODULE_AUTHOR("Konrad Eriksson <[email protected]>");
  237. +
  238. +
  239. +/* Module parameters */
  240. +
  241. +/* 0 = No ports enabled
  242. + 1 = Only port 1 enabled (on board ethernet on devboard)
  243. + 2 = Only port 2 enabled (external connector on devboard)
  244. + 3 = Both ports enabled
  245. +*/
  246. +static unsigned int ports = 3;
  247. +module_param(ports, uint, S_IRUGO);
  248. +MODULE_PARM_DESC(ports, "Bitmask indicating USB ports to use");
  249. +
  250. +
  251. +/***************************************************************************/
  252. +/***************************************************************************/
  253. +/* Shared global variables for this module */
  254. +/***************************************************************************/
  255. +/***************************************************************************/
  256. +
  257. +/* EP descriptor lists for non period transfers. Must be 32-bit aligned. */
  258. +static volatile struct USB_EP_Desc TxBulkEPList[NBR_OF_EPIDS] __attribute__ ((aligned (4)));
  259. +
  260. +static volatile struct USB_EP_Desc TxCtrlEPList[NBR_OF_EPIDS] __attribute__ ((aligned (4)));
  261. +
  262. +/* EP descriptor lists for period transfers. Must be 32-bit aligned. */
  263. +static volatile struct USB_EP_Desc TxIntrEPList[MAX_INTR_INTERVAL] __attribute__ ((aligned (4)));
  264. +static volatile struct USB_SB_Desc TxIntrSB_zout __attribute__ ((aligned (4)));
  265. +
  266. +static volatile struct USB_EP_Desc TxIsocEPList[NBR_OF_EPIDS] __attribute__ ((aligned (4)));
  267. +static volatile struct USB_SB_Desc TxIsocSB_zout __attribute__ ((aligned (4)));
  268. +
  269. +//static volatile struct USB_SB_Desc TxIsocSBList[NBR_OF_EPIDS] __attribute__ ((aligned (4)));
  270. +
  271. +/* After each enabled bulk EP IN we put two disabled EP descriptors with the eol flag set,
  272. + causing the DMA to stop the DMA channel. The first of these two has the intr flag set, which
  273. + gives us a dma8_sub0_descr interrupt. When we receive this, we advance the DMA one step in the
  274. + EP list and then restart the bulk channel, thus forcing a switch between bulk EP descriptors
  275. + in each frame. */
  276. +static volatile struct USB_EP_Desc TxBulkDummyEPList[NBR_OF_EPIDS][2] __attribute__ ((aligned (4)));
  277. +
  278. +/* List of URB pointers, where each points to the active URB for a epid.
  279. + For Bulk, Ctrl and Intr this means which URB that currently is added to
  280. + DMA lists (Isoc URBs are all directly added to DMA lists). As soon as
  281. + URB has completed is the queue examined and the first URB in queue is
  282. + removed and moved to the activeUrbList while its state change to STARTED and
  283. + its transfer(s) gets added to DMA list (exception Isoc where URBs enter
  284. + state STARTED directly and added transfers added to DMA lists). */
  285. +static struct urb *activeUrbList[NBR_OF_EPIDS];
  286. +
  287. +/* Additional software state info for each epid */
  288. +static struct etrax_epid epid_state[NBR_OF_EPIDS];
  289. +
  290. +/* Timer handles for bulk traffic timer used to avoid DMA bug where DMA stops
  291. + even if there is new data waiting to be processed */
  292. +static struct timer_list bulk_start_timer = TIMER_INITIALIZER(NULL, 0, 0);
  293. +static struct timer_list bulk_eot_timer = TIMER_INITIALIZER(NULL, 0, 0);
  294. +
  295. +/* We want the start timer to expire before the eot timer, because the former
  296. + might start traffic, thus making it unnecessary for the latter to time
  297. + out. */
  298. +#define BULK_START_TIMER_INTERVAL (HZ/50) /* 20 ms */
  299. +#define BULK_EOT_TIMER_INTERVAL (HZ/16) /* 60 ms */
  300. +
  301. +/* Delay before a URB completion happen when it's scheduled to be delayed */
  302. +#define LATER_TIMER_DELAY (HZ/50) /* 20 ms */
  303. +
  304. +/* Simplifying macros for checking software state info of a epid */
  305. +/* ----------------------------------------------------------------------- */
  306. +#define epid_inuse(epid) epid_state[epid].inuse
  307. +#define epid_out_traffic(epid) epid_state[epid].out_traffic
  308. +#define epid_isoc(epid) (epid_state[epid].type == PIPE_ISOCHRONOUS ? 1 : 0)
  309. +#define epid_intr(epid) (epid_state[epid].type == PIPE_INTERRUPT ? 1 : 0)
  310. +
  311. +
  312. +/***************************************************************************/
  313. +/***************************************************************************/
  314. +/* DEBUG FUNCTIONS */
  315. +/***************************************************************************/
  316. +/***************************************************************************/
  317. +/* Note that these functions are always available in their "__" variants,
  318. + for use in error situations. The "__" missing variants are controlled by
  319. + the USB_DEBUG_DESC/USB_DEBUG_URB macros. */
  320. +static void __dump_urb(struct urb* purb)
  321. +{
  322. + struct crisv10_urb_priv *urb_priv = purb->hcpriv;
  323. + int urb_num = -1;
  324. + if(urb_priv) {
  325. + urb_num = urb_priv->urb_num;
  326. + }
  327. + printk("\nURB:0x%x[%d]\n", (unsigned int)purb, urb_num);
  328. + printk("dev :0x%08lx\n", (unsigned long)purb->dev);
  329. + printk("pipe :0x%08x\n", purb->pipe);
  330. + printk("status :%d\n", purb->status);
  331. + printk("transfer_flags :0x%08x\n", purb->transfer_flags);
  332. + printk("transfer_buffer :0x%08lx\n", (unsigned long)purb->transfer_buffer);
  333. + printk("transfer_buffer_length:%d\n", purb->transfer_buffer_length);
  334. + printk("actual_length :%d\n", purb->actual_length);
  335. + printk("setup_packet :0x%08lx\n", (unsigned long)purb->setup_packet);
  336. + printk("start_frame :%d\n", purb->start_frame);
  337. + printk("number_of_packets :%d\n", purb->number_of_packets);
  338. + printk("interval :%d\n", purb->interval);
  339. + printk("error_count :%d\n", purb->error_count);
  340. + printk("context :0x%08lx\n", (unsigned long)purb->context);
  341. + printk("complete :0x%08lx\n\n", (unsigned long)purb->complete);
  342. +}
  343. +
  344. +static void __dump_in_desc(volatile struct USB_IN_Desc *in)
  345. +{
  346. + printk("\nUSB_IN_Desc at 0x%08lx\n", (unsigned long)in);
  347. + printk(" sw_len : 0x%04x (%d)\n", in->sw_len, in->sw_len);
  348. + printk(" command : 0x%04x\n", in->command);
  349. + printk(" next : 0x%08lx\n", in->next);
  350. + printk(" buf : 0x%08lx\n", in->buf);
  351. + printk(" hw_len : 0x%04x (%d)\n", in->hw_len, in->hw_len);
  352. + printk(" status : 0x%04x\n\n", in->status);
  353. +}
  354. +
  355. +static void __dump_sb_desc(volatile struct USB_SB_Desc *sb)
  356. +{
  357. + char tt = (sb->command & 0x30) >> 4;
  358. + char *tt_string;
  359. +
  360. + switch (tt) {
  361. + case 0:
  362. + tt_string = "zout";
  363. + break;
  364. + case 1:
  365. + tt_string = "in";
  366. + break;
  367. + case 2:
  368. + tt_string = "out";
  369. + break;
  370. + case 3:
  371. + tt_string = "setup";
  372. + break;
  373. + default:
  374. + tt_string = "unknown (weird)";
  375. + }
  376. +
  377. + printk(" USB_SB_Desc at 0x%08lx ", (unsigned long)sb);
  378. + printk(" command:0x%04x (", sb->command);
  379. + printk("rem:%d ", (sb->command & 0x3f00) >> 8);
  380. + printk("full:%d ", (sb->command & 0x40) >> 6);
  381. + printk("tt:%d(%s) ", tt, tt_string);
  382. + printk("intr:%d ", (sb->command & 0x8) >> 3);
  383. + printk("eot:%d ", (sb->command & 0x2) >> 1);
  384. + printk("eol:%d)", sb->command & 0x1);
  385. + printk(" sw_len:0x%04x(%d)", sb->sw_len, sb->sw_len);
  386. + printk(" next:0x%08lx", sb->next);
  387. + printk(" buf:0x%08lx\n", sb->buf);
  388. +}
  389. +
  390. +
  391. +static void __dump_ep_desc(volatile struct USB_EP_Desc *ep)
  392. +{
  393. + printk("USB_EP_Desc at 0x%08lx ", (unsigned long)ep);
  394. + printk(" command:0x%04x (", ep->command);
  395. + printk("ep_id:%d ", (ep->command & 0x1f00) >> 8);
  396. + printk("enable:%d ", (ep->command & 0x10) >> 4);
  397. + printk("intr:%d ", (ep->command & 0x8) >> 3);
  398. + printk("eof:%d ", (ep->command & 0x2) >> 1);
  399. + printk("eol:%d)", ep->command & 0x1);
  400. + printk(" hw_len:0x%04x(%d)", ep->hw_len, ep->hw_len);
  401. + printk(" next:0x%08lx", ep->next);
  402. + printk(" sub:0x%08lx\n", ep->sub);
  403. +}
  404. +
  405. +static inline void __dump_ep_list(int pipe_type)
  406. +{
  407. + volatile struct USB_EP_Desc *ep;
  408. + volatile struct USB_EP_Desc *first_ep;
  409. + volatile struct USB_SB_Desc *sb;
  410. +
  411. + switch (pipe_type)
  412. + {
  413. + case PIPE_BULK:
  414. + first_ep = &TxBulkEPList[0];
  415. + break;
  416. + case PIPE_CONTROL:
  417. + first_ep = &TxCtrlEPList[0];
  418. + break;
  419. + case PIPE_INTERRUPT:
  420. + first_ep = &TxIntrEPList[0];
  421. + break;
  422. + case PIPE_ISOCHRONOUS:
  423. + first_ep = &TxIsocEPList[0];
  424. + break;
  425. + default:
  426. + warn("Cannot dump unknown traffic type");
  427. + return;
  428. + }
  429. + ep = first_ep;
  430. +
  431. + printk("\n\nDumping EP list...\n\n");
  432. +
  433. + do {
  434. + __dump_ep_desc(ep);
  435. + /* Cannot phys_to_virt on 0 as it turns into 80000000, which is != 0. */
  436. + sb = ep->sub ? phys_to_virt(ep->sub) : 0;
  437. + while (sb) {
  438. + __dump_sb_desc(sb);
  439. + sb = sb->next ? phys_to_virt(sb->next) : 0;
  440. + }
  441. + ep = (volatile struct USB_EP_Desc *)(phys_to_virt(ep->next));
  442. +
  443. + } while (ep != first_ep);
  444. +}
  445. +
  446. +static inline void __dump_ept_data(int epid)
  447. +{
  448. + unsigned long flags;
  449. + __u32 r_usb_ept_data;
  450. +
  451. + if (epid < 0 || epid > 31) {
  452. + printk("Cannot dump ept data for invalid epid %d\n", epid);
  453. + return;
  454. + }
  455. +
  456. + local_irq_save(flags);
  457. + *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid);
  458. + nop();
  459. + r_usb_ept_data = *R_USB_EPT_DATA;
  460. + local_irq_restore(flags);
  461. +
  462. + printk(" R_USB_EPT_DATA = 0x%x for epid %d :\n", r_usb_ept_data, epid);
  463. + if (r_usb_ept_data == 0) {
  464. + /* No need for more detailed printing. */
  465. + return;
  466. + }
  467. + printk(" valid : %d\n", (r_usb_ept_data & 0x80000000) >> 31);
  468. + printk(" hold : %d\n", (r_usb_ept_data & 0x40000000) >> 30);
  469. + printk(" error_count_in : %d\n", (r_usb_ept_data & 0x30000000) >> 28);
  470. + printk(" t_in : %d\n", (r_usb_ept_data & 0x08000000) >> 27);
  471. + printk(" low_speed : %d\n", (r_usb_ept_data & 0x04000000) >> 26);
  472. + printk(" port : %d\n", (r_usb_ept_data & 0x03000000) >> 24);
  473. + printk(" error_code : %d\n", (r_usb_ept_data & 0x00c00000) >> 22);
  474. + printk(" t_out : %d\n", (r_usb_ept_data & 0x00200000) >> 21);
  475. + printk(" error_count_out : %d\n", (r_usb_ept_data & 0x00180000) >> 19);
  476. + printk(" max_len : %d\n", (r_usb_ept_data & 0x0003f800) >> 11);
  477. + printk(" ep : %d\n", (r_usb_ept_data & 0x00000780) >> 7);
  478. + printk(" dev : %d\n", (r_usb_ept_data & 0x0000003f));
  479. +}
  480. +
  481. +static inline void __dump_ept_data_iso(int epid)
  482. +{
  483. + unsigned long flags;
  484. + __u32 ept_data;
  485. +
  486. + if (epid < 0 || epid > 31) {
  487. + printk("Cannot dump ept data for invalid epid %d\n", epid);
  488. + return;
  489. + }
  490. +
  491. + local_irq_save(flags);
  492. + *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid);
  493. + nop();
  494. + ept_data = *R_USB_EPT_DATA_ISO;
  495. + local_irq_restore(flags);
  496. +
  497. + printk(" R_USB_EPT_DATA = 0x%x for epid %d :\n", ept_data, epid);
  498. + if (ept_data == 0) {
  499. + /* No need for more detailed printing. */
  500. + return;
  501. + }
  502. + printk(" valid : %d\n", IO_EXTRACT(R_USB_EPT_DATA_ISO, valid,
  503. + ept_data));
  504. + printk(" port : %d\n", IO_EXTRACT(R_USB_EPT_DATA_ISO, port,
  505. + ept_data));
  506. + printk(" error_code : %d\n", IO_EXTRACT(R_USB_EPT_DATA_ISO, error_code,
  507. + ept_data));
  508. + printk(" max_len : %d\n", IO_EXTRACT(R_USB_EPT_DATA_ISO, max_len,
  509. + ept_data));
  510. + printk(" ep : %d\n", IO_EXTRACT(R_USB_EPT_DATA_ISO, ep,
  511. + ept_data));
  512. + printk(" dev : %d\n", IO_EXTRACT(R_USB_EPT_DATA_ISO, dev,
  513. + ept_data));
  514. +}
  515. +
  516. +static inline void __dump_ept_data_list(void)
  517. +{
  518. + int i;
  519. +
  520. + printk("Dumping the whole R_USB_EPT_DATA list\n");
  521. +
  522. + for (i = 0; i < 32; i++) {
  523. + __dump_ept_data(i);
  524. + }
  525. +}
  526. +
  527. +static void debug_epid(int epid) {
  528. + int i;
  529. +
  530. + if(epid_isoc(epid)) {
  531. + __dump_ept_data_iso(epid);
  532. + } else {
  533. + __dump_ept_data(epid);
  534. + }
  535. +
  536. + printk("Bulk:\n");
  537. + for(i = 0; i < 32; i++) {
  538. + if(IO_EXTRACT(USB_EP_command, epid, TxBulkEPList[i].command) ==
  539. + epid) {
  540. + printk("%d: ", i); __dump_ep_desc(&(TxBulkEPList[i]));
  541. + }
  542. + }
  543. +
  544. + printk("Ctrl:\n");
  545. + for(i = 0; i < 32; i++) {
  546. + if(IO_EXTRACT(USB_EP_command, epid, TxCtrlEPList[i].command) ==
  547. + epid) {
  548. + printk("%d: ", i); __dump_ep_desc(&(TxCtrlEPList[i]));
  549. + }
  550. + }
  551. +
  552. + printk("Intr:\n");
  553. + for(i = 0; i < MAX_INTR_INTERVAL; i++) {
  554. + if(IO_EXTRACT(USB_EP_command, epid, TxIntrEPList[i].command) ==
  555. + epid) {
  556. + printk("%d: ", i); __dump_ep_desc(&(TxIntrEPList[i]));
  557. + }
  558. + }
  559. +
  560. + printk("Isoc:\n");
  561. + for(i = 0; i < 32; i++) {
  562. + if(IO_EXTRACT(USB_EP_command, epid, TxIsocEPList[i].command) ==
  563. + epid) {
  564. + printk("%d: ", i); __dump_ep_desc(&(TxIsocEPList[i]));
  565. + }
  566. + }
  567. +
  568. + __dump_ept_data_list();
  569. + __dump_ep_list(PIPE_INTERRUPT);
  570. + printk("\n\n");
  571. +}
  572. +
  573. +
  574. +
  575. +char* hcd_status_to_str(__u8 bUsbStatus) {
  576. + static char hcd_status_str[128];
  577. + hcd_status_str[0] = '\0';
  578. + if(bUsbStatus & IO_STATE(R_USB_STATUS, ourun, yes)) {
  579. + strcat(hcd_status_str, "ourun ");
  580. + }
  581. + if(bUsbStatus & IO_STATE(R_USB_STATUS, perror, yes)) {
  582. + strcat(hcd_status_str, "perror ");
  583. + }
  584. + if(bUsbStatus & IO_STATE(R_USB_STATUS, device_mode, yes)) {
  585. + strcat(hcd_status_str, "device_mode ");
  586. + }
  587. + if(bUsbStatus & IO_STATE(R_USB_STATUS, host_mode, yes)) {
  588. + strcat(hcd_status_str, "host_mode ");
  589. + }
  590. + if(bUsbStatus & IO_STATE(R_USB_STATUS, started, yes)) {
  591. + strcat(hcd_status_str, "started ");
  592. + }
  593. + if(bUsbStatus & IO_STATE(R_USB_STATUS, running, yes)) {
  594. + strcat(hcd_status_str, "running ");
  595. + }
  596. + return hcd_status_str;
  597. +}
  598. +
  599. +
  600. +char* sblist_to_str(struct USB_SB_Desc* sb_desc) {
  601. + static char sblist_to_str_buff[128];
  602. + char tmp[32], tmp2[32];
  603. + sblist_to_str_buff[0] = '\0';
  604. + while(sb_desc != NULL) {
  605. + switch(IO_EXTRACT(USB_SB_command, tt, sb_desc->command)) {
  606. + case 0: sprintf(tmp, "zout"); break;
  607. + case 1: sprintf(tmp, "in"); break;
  608. + case 2: sprintf(tmp, "out"); break;
  609. + case 3: sprintf(tmp, "setup"); break;
  610. + }
  611. + sprintf(tmp2, "(%s %d)", tmp, sb_desc->sw_len);
  612. + strcat(sblist_to_str_buff, tmp2);
  613. + if(sb_desc->next != 0) {
  614. + sb_desc = phys_to_virt(sb_desc->next);
  615. + } else {
  616. + sb_desc = NULL;
  617. + }
  618. + }
  619. + return sblist_to_str_buff;
  620. +}
  621. +
  622. +char* port_status_to_str(__u16 wPortStatus) {
  623. + static char port_status_str[128];
  624. + port_status_str[0] = '\0';
  625. + if(wPortStatus & IO_STATE(R_USB_RH_PORT_STATUS_1, connected, yes)) {
  626. + strcat(port_status_str, "connected ");
  627. + }
  628. + if(wPortStatus & IO_STATE(R_USB_RH_PORT_STATUS_1, enabled, yes)) {
  629. + strcat(port_status_str, "enabled ");
  630. + }
  631. + if(wPortStatus & IO_STATE(R_USB_RH_PORT_STATUS_1, suspended, yes)) {
  632. + strcat(port_status_str, "suspended ");
  633. + }
  634. + if(wPortStatus & IO_STATE(R_USB_RH_PORT_STATUS_1, reset, yes)) {
  635. + strcat(port_status_str, "reset ");
  636. + }
  637. + if(wPortStatus & IO_STATE(R_USB_RH_PORT_STATUS_1, speed, full)) {
  638. + strcat(port_status_str, "full-speed ");
  639. + } else {
  640. + strcat(port_status_str, "low-speed ");
  641. + }
  642. + return port_status_str;
  643. +}
  644. +
  645. +
  646. +char* endpoint_to_str(struct usb_endpoint_descriptor *ed) {
  647. + static char endpoint_to_str_buff[128];
  648. + char tmp[32];
  649. + int epnum = ed->bEndpointAddress & 0x0F;
  650. + int dir = ed->bEndpointAddress & 0x80;
  651. + int type = ed->bmAttributes & 0x03;
  652. + endpoint_to_str_buff[0] = '\0';
  653. + sprintf(endpoint_to_str_buff, "ep:%d ", epnum);
  654. + switch(type) {
  655. + case 0:
  656. + sprintf(tmp, " ctrl");
  657. + break;
  658. + case 1:
  659. + sprintf(tmp, " isoc");
  660. + break;
  661. + case 2:
  662. + sprintf(tmp, " bulk");
  663. + break;
  664. + case 3:
  665. + sprintf(tmp, " intr");
  666. + break;
  667. + }
  668. + strcat(endpoint_to_str_buff, tmp);
  669. + if(dir) {
  670. + sprintf(tmp, " in");
  671. + } else {
  672. + sprintf(tmp, " out");
  673. + }
  674. + strcat(endpoint_to_str_buff, tmp);
  675. +
  676. + return endpoint_to_str_buff;
  677. +}
  678. +
  679. +/* Debug helper functions for Transfer Controller */
  680. +char* pipe_to_str(unsigned int pipe) {
  681. + static char pipe_to_str_buff[128];
  682. + char tmp[64];
  683. + sprintf(pipe_to_str_buff, "dir:%s", str_dir(pipe));
  684. + sprintf(tmp, " type:%s", str_type(pipe));
  685. + strcat(pipe_to_str_buff, tmp);
  686. +
  687. + sprintf(tmp, " dev:%d", usb_pipedevice(pipe));
  688. + strcat(pipe_to_str_buff, tmp);
  689. + sprintf(tmp, " ep:%d", usb_pipeendpoint(pipe));
  690. + strcat(pipe_to_str_buff, tmp);
  691. + return pipe_to_str_buff;
  692. +}
  693. +
  694. +
  695. +#define USB_DEBUG_DESC 1
  696. +
  697. +#ifdef USB_DEBUG_DESC
  698. +#define dump_in_desc(x) __dump_in_desc(x)
  699. +#define dump_sb_desc(...) __dump_sb_desc(...)
  700. +#define dump_ep_desc(x) __dump_ep_desc(x)
  701. +#define dump_ept_data(x) __dump_ept_data(x)
  702. +#else
  703. +#define dump_in_desc(...) do {} while (0)
  704. +#define dump_sb_desc(...) do {} while (0)
  705. +#define dump_ep_desc(...) do {} while (0)
  706. +#endif
  707. +
  708. +
  709. +/* Uncomment this to enable massive function call trace
  710. + #define USB_DEBUG_TRACE */
  711. +//#define USB_DEBUG_TRACE 1
  712. +
  713. +#ifdef USB_DEBUG_TRACE
  714. +#define DBFENTER (printk(": Entering: %s\n", __FUNCTION__))
  715. +#define DBFEXIT (printk(": Exiting: %s\n", __FUNCTION__))
  716. +#else
  717. +#define DBFENTER do {} while (0)
  718. +#define DBFEXIT do {} while (0)
  719. +#endif
  720. +
  721. +#define CHECK_ALIGN(x) if (((__u32)(x)) & 0x00000003) \
  722. +{panic("Alignment check (DWORD) failed at %s:%s:%d\n", __FILE__, __FUNCTION__, __LINE__);}
  723. +
  724. +/* Most helpful debugging aid */
  725. +#define ASSERT(expr) ((void) ((expr) ? 0 : (err("assert failed at: %s %d",__FUNCTION__, __LINE__))))
  726. +
  727. +
  728. +/***************************************************************************/
  729. +/***************************************************************************/
  730. +/* Forward declarations */
  731. +/***************************************************************************/
  732. +/***************************************************************************/
  733. +void crisv10_hcd_epid_attn_irq(struct crisv10_irq_reg *reg);
  734. +void crisv10_hcd_port_status_irq(struct crisv10_irq_reg *reg);
  735. +void crisv10_hcd_ctl_status_irq(struct crisv10_irq_reg *reg);
  736. +void crisv10_hcd_isoc_eof_irq(struct crisv10_irq_reg *reg);
  737. +
  738. +void rh_port_status_change(__u16[]);
  739. +int rh_clear_port_feature(__u8, __u16);
  740. +int rh_set_port_feature(__u8, __u16);
  741. +static void rh_disable_port(unsigned int port);
  742. +
  743. +static void check_finished_bulk_tx_epids(struct usb_hcd *hcd,
  744. + int timer);
  745. +
  746. +//static int tc_setup_epid(struct usb_host_endpoint *ep, struct urb *urb,
  747. +// int mem_flags);
  748. +static int tc_setup_epid(struct urb *urb, int mem_flags);
  749. +static void tc_free_epid(struct usb_host_endpoint *ep);
  750. +static int tc_allocate_epid(void);
  751. +static void tc_finish_urb(struct usb_hcd *hcd, struct urb *urb, int status);
  752. +static void tc_finish_urb_later(struct usb_hcd *hcd, struct urb *urb,
  753. + int status);
  754. +
  755. +static int urb_priv_create(struct usb_hcd *hcd, struct urb *urb, int epid,
  756. + int mem_flags);
  757. +static void urb_priv_free(struct usb_hcd *hcd, struct urb *urb);
  758. +
  759. +static inline struct urb *urb_list_first(int epid);
  760. +static inline void urb_list_add(struct urb *urb, int epid,
  761. + int mem_flags);
  762. +static inline urb_entry_t *urb_list_entry(struct urb *urb, int epid);
  763. +static inline void urb_list_del(struct urb *urb, int epid);
  764. +static inline void urb_list_move_last(struct urb *urb, int epid);
  765. +static inline struct urb *urb_list_next(struct urb *urb, int epid);
  766. +
  767. +int create_sb_for_urb(struct urb *urb, int mem_flags);
  768. +int init_intr_urb(struct urb *urb, int mem_flags);
  769. +
  770. +static inline void etrax_epid_set(__u8 index, __u32 data);
  771. +static inline void etrax_epid_clear_error(__u8 index);
  772. +static inline void etrax_epid_set_toggle(__u8 index, __u8 dirout,
  773. + __u8 toggle);
  774. +static inline __u8 etrax_epid_get_toggle(__u8 index, __u8 dirout);
  775. +static inline __u32 etrax_epid_get(__u8 index);
  776. +
  777. +/* We're accessing the same register position in Etrax so
  778. + when we do full access the internal difference doesn't matter */
  779. +#define etrax_epid_iso_set(index, data) etrax_epid_set(index, data)
  780. +#define etrax_epid_iso_get(index) etrax_epid_get(index)
  781. +
  782. +
  783. +//static void tc_dma_process_isoc_urb(struct urb *urb);
  784. +static void tc_dma_process_queue(int epid);
  785. +static void tc_dma_unlink_intr_urb(struct urb *urb);
  786. +static irqreturn_t tc_dma_tx_interrupt(int irq, void *vhc);
  787. +static irqreturn_t tc_dma_rx_interrupt(int irq, void *vhc);
  788. +
  789. +static void tc_bulk_start_timer_func(unsigned long dummy);
  790. +static void tc_bulk_eot_timer_func(unsigned long dummy);
  791. +
  792. +
  793. +/*************************************************************/
  794. +/*************************************************************/
  795. +/* Host Controler Driver block */
  796. +/*************************************************************/
  797. +/*************************************************************/
  798. +
  799. +/* HCD operations */
  800. +static irqreturn_t crisv10_hcd_top_irq(int irq, void*);
  801. +static int crisv10_hcd_reset(struct usb_hcd *);
  802. +static int crisv10_hcd_start(struct usb_hcd *);
  803. +static void crisv10_hcd_stop(struct usb_hcd *);
  804. +#ifdef CONFIG_PM
  805. +static int crisv10_hcd_suspend(struct device *, u32, u32);
  806. +static int crisv10_hcd_resume(struct device *, u32);
  807. +#endif /* CONFIG_PM */
  808. +static int crisv10_hcd_get_frame(struct usb_hcd *);
  809. +
  810. +//static int tc_urb_enqueue(struct usb_hcd *, struct usb_host_endpoint *ep, struct urb *, gfp_t mem_flags);
  811. +static int tc_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags);
  812. +//static int tc_urb_dequeue(struct usb_hcd *, struct urb *);
  813. +static int tc_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status);
  814. +static void tc_endpoint_disable(struct usb_hcd *, struct usb_host_endpoint *ep);
  815. +
  816. +static int rh_status_data_request(struct usb_hcd *, char *);
  817. +static int rh_control_request(struct usb_hcd *, u16, u16, u16, char*, u16);
  818. +
  819. +#ifdef CONFIG_PM
  820. +static int crisv10_hcd_hub_suspend(struct usb_hcd *);
  821. +static int crisv10_hcd_hub_resume(struct usb_hcd *);
  822. +#endif /* CONFIG_PM */
  823. +#ifdef CONFIG_USB_OTG
  824. +static int crisv10_hcd_start_port_reset(struct usb_hcd *, unsigned);
  825. +#endif /* CONFIG_USB_OTG */
  826. +
  827. +/* host controller driver interface */
  828. +static const struct hc_driver crisv10_hc_driver =
  829. + {
  830. + .description = hc_name,
  831. + .product_desc = product_desc,
  832. + .hcd_priv_size = sizeof(struct crisv10_hcd),
  833. +
  834. + /* Attaching IRQ handler manualy in probe() */
  835. + /* .irq = crisv10_hcd_irq, */
  836. +
  837. + .flags = HCD_USB11,
  838. +
  839. + /* called to init HCD and root hub */
  840. + .reset = crisv10_hcd_reset,
  841. + .start = crisv10_hcd_start,
  842. +
  843. + /* cleanly make HCD stop writing memory and doing I/O */
  844. + .stop = crisv10_hcd_stop,
  845. +
  846. + /* return current frame number */
  847. + .get_frame_number = crisv10_hcd_get_frame,
  848. +
  849. +
  850. + /* Manage i/o requests via the Transfer Controller */
  851. + .urb_enqueue = tc_urb_enqueue,
  852. + .urb_dequeue = tc_urb_dequeue,
  853. +
  854. + /* hw synch, freeing endpoint resources that urb_dequeue can't */
  855. + .endpoint_disable = tc_endpoint_disable,
  856. +
  857. +
  858. + /* Root Hub support */
  859. + .hub_status_data = rh_status_data_request,
  860. + .hub_control = rh_control_request,
  861. +#ifdef CONFIG_PM
  862. + .hub_suspend = rh_suspend_request,
  863. + .hub_resume = rh_resume_request,
  864. +#endif /* CONFIG_PM */
  865. +#ifdef CONFIG_USB_OTG
  866. + .start_port_reset = crisv10_hcd_start_port_reset,
  867. +#endif /* CONFIG_USB_OTG */
  868. + };
  869. +
  870. +
  871. +/*
  872. + * conversion between pointers to a hcd and the corresponding
  873. + * crisv10_hcd
  874. + */
  875. +
  876. +static inline struct crisv10_hcd *hcd_to_crisv10_hcd(struct usb_hcd *hcd)
  877. +{
  878. + return (struct crisv10_hcd *) hcd->hcd_priv;
  879. +}
  880. +
  881. +static inline struct usb_hcd *crisv10_hcd_to_hcd(struct crisv10_hcd *hcd)
  882. +{
  883. + return container_of((void *) hcd, struct usb_hcd, hcd_priv);
  884. +}
  885. +
  886. +/* check if specified port is in use */
  887. +static inline int port_in_use(unsigned int port)
  888. +{
  889. + return ports & (1 << port);
  890. +}
  891. +
  892. +/* number of ports in use */
  893. +static inline unsigned int num_ports(void)
  894. +{
  895. + unsigned int i, num = 0;
  896. + for (i = 0; i < USB_ROOT_HUB_PORTS; i++)
  897. + if (port_in_use(i))
  898. + num++;
  899. + return num;
  900. +}
  901. +
  902. +/* map hub port number to the port number used internally by the HC */
  903. +static inline unsigned int map_port(unsigned int port)
  904. +{
  905. + unsigned int i, num = 0;
  906. + for (i = 0; i < USB_ROOT_HUB_PORTS; i++)
  907. + if (port_in_use(i))
  908. + if (++num == port)
  909. + return i;
  910. + return -1;
  911. +}
  912. +
  913. +/* size of descriptors in slab cache */
  914. +#ifndef MAX
  915. +#define MAX(x, y) ((x) > (y) ? (x) : (y))
  916. +#endif
  917. +
  918. +
  919. +/******************************************************************/
  920. +/* Hardware Interrupt functions */
  921. +/******************************************************************/
  922. +
  923. +/* Fast interrupt handler for HC */
  924. +static irqreturn_t crisv10_hcd_top_irq(int irq, void *vcd)
  925. +{
  926. + struct usb_hcd *hcd = vcd;
  927. + struct crisv10_irq_reg reg;
  928. + __u32 irq_mask;
  929. + unsigned long flags;
  930. +
  931. + DBFENTER;
  932. +
  933. + ASSERT(hcd != NULL);
  934. + reg.hcd = hcd;
  935. +
  936. + /* Turn of other interrupts while handling these sensitive cases */
  937. + local_irq_save(flags);
  938. +
  939. + /* Read out which interrupts that are flaged */
  940. + irq_mask = *R_USB_IRQ_MASK_READ;
  941. + reg.r_usb_irq_mask_read = irq_mask;
  942. +
  943. + /* Reading R_USB_STATUS clears the ctl_status interrupt. Note that
  944. + R_USB_STATUS must be read before R_USB_EPID_ATTN since reading the latter
  945. + clears the ourun and perror fields of R_USB_STATUS. */
  946. + reg.r_usb_status = *R_USB_STATUS;
  947. +
  948. + /* Reading R_USB_EPID_ATTN clears the iso_eof, bulk_eot and epid_attn
  949. + interrupts. */
  950. + reg.r_usb_epid_attn = *R_USB_EPID_ATTN;
  951. +
  952. + /* Reading R_USB_RH_PORT_STATUS_1 and R_USB_RH_PORT_STATUS_2 clears the
  953. + port_status interrupt. */
  954. + reg.r_usb_rh_port_status_1 = *R_USB_RH_PORT_STATUS_1;
  955. + reg.r_usb_rh_port_status_2 = *R_USB_RH_PORT_STATUS_2;
  956. +
  957. + /* Reading R_USB_FM_NUMBER clears the sof interrupt. */
  958. + /* Note: the lower 11 bits contain the actual frame number, sent with each
  959. + sof. */
  960. + reg.r_usb_fm_number = *R_USB_FM_NUMBER;
  961. +
  962. + /* Interrupts are handled in order of priority. */
  963. + if (irq_mask & IO_MASK(R_USB_IRQ_MASK_READ, port_status)) {
  964. + crisv10_hcd_port_status_irq(&reg);
  965. + }
  966. + if (irq_mask & IO_MASK(R_USB_IRQ_MASK_READ, epid_attn)) {
  967. + crisv10_hcd_epid_attn_irq(&reg);
  968. + }
  969. + if (irq_mask & IO_MASK(R_USB_IRQ_MASK_READ, ctl_status)) {
  970. + crisv10_hcd_ctl_status_irq(&reg);
  971. + }
  972. + if (irq_mask & IO_MASK(R_USB_IRQ_MASK_READ, iso_eof)) {
  973. + crisv10_hcd_isoc_eof_irq(&reg);
  974. + }
  975. + if (irq_mask & IO_MASK(R_USB_IRQ_MASK_READ, bulk_eot)) {
  976. + /* Update/restart the bulk start timer since obviously the channel is
  977. + running. */
  978. + mod_timer(&bulk_start_timer, jiffies + BULK_START_TIMER_INTERVAL);
  979. + /* Update/restart the bulk eot timer since we just received an bulk eot
  980. + interrupt. */
  981. + mod_timer(&bulk_eot_timer, jiffies + BULK_EOT_TIMER_INTERVAL);
  982. +
  983. + /* Check for finished bulk transfers on epids */
  984. + check_finished_bulk_tx_epids(hcd, 0);
  985. + }
  986. + local_irq_restore(flags);
  987. +
  988. + DBFEXIT;
  989. + return IRQ_HANDLED;
  990. +}
  991. +
  992. +
  993. +void crisv10_hcd_epid_attn_irq(struct crisv10_irq_reg *reg) {
  994. + struct usb_hcd *hcd = reg->hcd;
  995. + struct crisv10_urb_priv *urb_priv;
  996. + int epid;
  997. + DBFENTER;
  998. +
  999. + for (epid = 0; epid < NBR_OF_EPIDS; epid++) {
  1000. + if (test_bit(epid, (void *)&reg->r_usb_epid_attn)) {
  1001. + struct urb *urb;
  1002. + __u32 ept_data;
  1003. + int error_code;
  1004. +
  1005. + if (epid == DUMMY_EPID || epid == INVALID_EPID) {
  1006. + /* We definitely don't care about these ones. Besides, they are
  1007. + always disabled, so any possible disabling caused by the
  1008. + epid attention interrupt is irrelevant. */
  1009. + warn("Got epid_attn for INVALID_EPID or DUMMY_EPID (%d).", epid);
  1010. + continue;
  1011. + }
  1012. +
  1013. + if(!epid_inuse(epid)) {
  1014. + irq_err("Epid attention on epid:%d that isn't in use\n", epid);
  1015. + printk("R_USB_STATUS: 0x%x\n", reg->r_usb_status);
  1016. + debug_epid(epid);
  1017. + continue;
  1018. + }
  1019. +
  1020. + /* Note that although there are separate R_USB_EPT_DATA and
  1021. + R_USB_EPT_DATA_ISO registers, they are located at the same address and
  1022. + are of the same size. In other words, this read should be ok for isoc
  1023. + also. */
  1024. + ept_data = etrax_epid_get(epid);
  1025. + error_code = IO_EXTRACT(R_USB_EPT_DATA, error_code, ept_data);
  1026. +
  1027. + /* Get the active URB for this epid. We blatantly assume
  1028. + that only this URB could have caused the epid attention. */
  1029. + urb = activeUrbList[epid];
  1030. + if (urb == NULL) {
  1031. + irq_err("Attention on epid:%d error:%d with no active URB.\n",
  1032. + epid, error_code);
  1033. + printk("R_USB_STATUS: 0x%x\n", reg->r_usb_status);
  1034. + debug_epid(epid);
  1035. + continue;
  1036. + }
  1037. +
  1038. + urb_priv = (struct crisv10_urb_priv *)urb->hcpriv;
  1039. + ASSERT(urb_priv);
  1040. +
  1041. + /* Using IO_STATE_VALUE on R_USB_EPT_DATA should be ok for isoc also. */
  1042. + if (error_code == IO_STATE_VALUE(R_USB_EPT_DATA, error_code, no_error)) {
  1043. +
  1044. + /* Isoc traffic doesn't have error_count_in/error_count_out. */
  1045. + if ((usb_pipetype(urb->pipe) != PIPE_ISOCHRONOUS) &&
  1046. + (IO_EXTRACT(R_USB_EPT_DATA, error_count_in, ept_data) == 3 ||
  1047. + IO_EXTRACT(R_USB_EPT_DATA, error_count_out, ept_data) == 3)) {
  1048. + /* Check if URB allready is marked for late-finish, we can get
  1049. + several 3rd error for Intr traffic when a device is unplugged */
  1050. + if(urb_priv->later_data == NULL) {
  1051. + /* 3rd error. */
  1052. + irq_warn("3rd error for epid:%d (%s %s) URB:0x%x[%d]\n", epid,
  1053. + str_dir(urb->pipe), str_type(urb->pipe),
  1054. + (unsigned int)urb, urb_priv->urb_num);
  1055. +
  1056. + tc_finish_urb_later(hcd, urb, -EPROTO);
  1057. + }
  1058. +
  1059. + } else if (reg->r_usb_status & IO_MASK(R_USB_STATUS, perror)) {
  1060. + irq_warn("Perror for epid:%d\n", epid);
  1061. + printk("FM_NUMBER: %d\n", reg->r_usb_fm_number & 0x7ff);
  1062. + printk("R_USB_STATUS: 0x%x\n", reg->r_usb_status);
  1063. + __dump_urb(urb);
  1064. + debug_epid(epid);
  1065. +
  1066. + if (!(ept_data & IO_MASK(R_USB_EPT_DATA, valid))) {
  1067. + /* invalid ep_id */
  1068. + panic("Perror because of invalid epid."
  1069. + " Deconfigured too early?");
  1070. + } else {
  1071. + /* past eof1, near eof, zout transfer, setup transfer */
  1072. + /* Dump the urb and the relevant EP descriptor. */
  1073. + panic("Something wrong with DMA descriptor contents."
  1074. + " Too much traffic inserted?");
  1075. + }
  1076. + } else if (reg->r_usb_status & IO_MASK(R_USB_STATUS, ourun)) {
  1077. + /* buffer ourun */
  1078. + printk("FM_NUMBER: %d\n", reg->r_usb_fm_number & 0x7ff);
  1079. + printk("R_USB_STATUS: 0x%x\n", reg->r_usb_status);
  1080. + __dump_urb(urb);
  1081. + debug_epid(epid);
  1082. +
  1083. + panic("Buffer overrun/underrun for epid:%d. DMA too busy?", epid);
  1084. + } else {
  1085. + irq_warn("Attention on epid:%d (%s %s) with no error code\n", epid,
  1086. + str_dir(urb->pipe), str_type(urb->pipe));
  1087. + printk("R_USB_STATUS: 0x%x\n", reg->r_usb_status);
  1088. + __dump_urb(urb);
  1089. + debug_epid(epid);
  1090. + }
  1091. +
  1092. + } else if (error_code == IO_STATE_VALUE(R_USB_EPT_DATA, error_code,
  1093. + stall)) {
  1094. + /* Not really a protocol error, just says that the endpoint gave
  1095. + a stall response. Note that error_code cannot be stall for isoc. */
  1096. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  1097. + panic("Isoc traffic cannot stall");
  1098. + }
  1099. +
  1100. + tc_dbg("Stall for epid:%d (%s %s) URB:0x%x\n", epid,
  1101. + str_dir(urb->pipe), str_type(urb->pipe), (unsigned int)urb);
  1102. + tc_finish_urb(hcd, urb, -EPIPE);
  1103. +
  1104. + } else if (error_code == IO_STATE_VALUE(R_USB_EPT_DATA, error_code,
  1105. + bus_error)) {
  1106. + /* Two devices responded to a transaction request. Must be resolved
  1107. + by software. FIXME: Reset ports? */
  1108. + panic("Bus error for epid %d."
  1109. + " Two devices responded to transaction request\n",
  1110. + epid);
  1111. +
  1112. + } else if (error_code == IO_STATE_VALUE(R_USB_EPT_DATA, error_code,
  1113. + buffer_error)) {
  1114. + /* DMA overrun or underrun. */
  1115. + irq_warn("Buffer overrun/underrun for epid:%d (%s %s)\n", epid,
  1116. + str_dir(urb->pipe), str_type(urb->pipe));
  1117. +
  1118. + /* It seems that error_code = buffer_error in
  1119. + R_USB_EPT_DATA/R_USB_EPT_DATA_ISO and ourun = yes in R_USB_STATUS
  1120. + are the same error. */
  1121. + tc_finish_urb(hcd, urb, -EPROTO);
  1122. + } else {
  1123. + irq_warn("Unknown attention on epid:%d (%s %s)\n", epid,
  1124. + str_dir(urb->pipe), str_type(urb->pipe));
  1125. + dump_ept_data(epid);
  1126. + }
  1127. + }
  1128. + }
  1129. + DBFEXIT;
  1130. +}
  1131. +
  1132. +void crisv10_hcd_port_status_irq(struct crisv10_irq_reg *reg)
  1133. +{
  1134. + __u16 port_reg[USB_ROOT_HUB_PORTS];
  1135. + DBFENTER;
  1136. + port_reg[0] = reg->r_usb_rh_port_status_1;
  1137. + port_reg[1] = reg->r_usb_rh_port_status_2;
  1138. + rh_port_status_change(port_reg);
  1139. + DBFEXIT;
  1140. +}
  1141. +
  1142. +void crisv10_hcd_isoc_eof_irq(struct crisv10_irq_reg *reg)
  1143. +{
  1144. + int epid;
  1145. + struct urb *urb;
  1146. + struct crisv10_urb_priv *urb_priv;
  1147. +
  1148. + DBFENTER;
  1149. +
  1150. + for (epid = 0; epid < NBR_OF_EPIDS - 1; epid++) {
  1151. +
  1152. + /* Only check epids that are in use, is valid and has SB list */
  1153. + if (!epid_inuse(epid) || epid == INVALID_EPID ||
  1154. + TxIsocEPList[epid].sub == 0 || epid == DUMMY_EPID) {
  1155. + /* Nothing here to see. */
  1156. + continue;
  1157. + }
  1158. + ASSERT(epid_isoc(epid));
  1159. +
  1160. + /* Get the active URB for this epid (if any). */
  1161. + urb = activeUrbList[epid];
  1162. + if (urb == 0) {
  1163. + isoc_warn("Ignoring NULL urb for epid:%d\n", epid);
  1164. + continue;
  1165. + }
  1166. + if(!epid_out_traffic(epid)) {
  1167. + /* Sanity check. */
  1168. + ASSERT(usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS);
  1169. +
  1170. + urb_priv = (struct crisv10_urb_priv *)urb->hcpriv;
  1171. + ASSERT(urb_priv);
  1172. +
  1173. + if (urb_priv->urb_state == NOT_STARTED) {
  1174. + /* If ASAP is not set and urb->start_frame is the current frame,
  1175. + start the transfer. */
  1176. + if (!(urb->transfer_flags & URB_ISO_ASAP) &&
  1177. + (urb->start_frame == (*R_USB_FM_NUMBER & 0x7ff))) {
  1178. + /* EP should not be enabled if we're waiting for start_frame */
  1179. + ASSERT((TxIsocEPList[epid].command &
  1180. + IO_STATE(USB_EP_command, enable, yes)) == 0);
  1181. +
  1182. + isoc_warn("Enabling isoc IN EP descr for epid %d\n", epid);
  1183. + TxIsocEPList[epid].command |= IO_STATE(USB_EP_command, enable, yes);
  1184. +
  1185. + /* This urb is now active. */
  1186. + urb_priv->urb_state = STARTED;
  1187. + continue;
  1188. + }
  1189. + }
  1190. + }
  1191. + }
  1192. +
  1193. + DBFEXIT;
  1194. +}
  1195. +
  1196. +void crisv10_hcd_ctl_status_irq(struct crisv10_irq_reg *reg)
  1197. +{
  1198. + struct crisv10_hcd* crisv10_hcd = hcd_to_crisv10_hcd(reg->hcd);
  1199. +
  1200. + DBFENTER;
  1201. + ASSERT(crisv10_hcd);
  1202. +
  1203. + irq_dbg("ctr_status_irq, controller status: %s\n",
  1204. + hcd_status_to_str(reg->r_usb_status));
  1205. +
  1206. + /* FIXME: What should we do if we get ourun or perror? Dump the EP and SB
  1207. + list for the corresponding epid? */
  1208. + if (reg->r_usb_status & IO_MASK(R_USB_STATUS, ourun)) {
  1209. + panic("USB controller got ourun.");
  1210. + }
  1211. + if (reg->r_usb_status & IO_MASK(R_USB_STATUS, perror)) {
  1212. +
  1213. + /* Before, etrax_usb_do_intr_recover was called on this epid if it was
  1214. + an interrupt pipe. I don't see how re-enabling all EP descriptors
  1215. + will help if there was a programming error. */
  1216. + panic("USB controller got perror.");
  1217. + }
  1218. +
  1219. + /* Keep track of USB Controller, if it's running or not */
  1220. + if(reg->r_usb_status & IO_STATE(R_USB_STATUS, running, yes)) {
  1221. + crisv10_hcd->running = 1;
  1222. + } else {
  1223. + crisv10_hcd->running = 0;
  1224. + }
  1225. +
  1226. + if (reg->r_usb_status & IO_MASK(R_USB_STATUS, device_mode)) {
  1227. + /* We should never operate in device mode. */
  1228. + panic("USB controller in device mode.");
  1229. + }
  1230. +
  1231. + /* Set the flag to avoid getting "Unlink after no-IRQ? Controller is probably
  1232. + using the wrong IRQ" from hcd_unlink_urb() in drivers/usb/core/hcd.c */
  1233. + set_bit(HCD_FLAG_SAW_IRQ, &reg->hcd->flags);
  1234. +
  1235. + DBFEXIT;
  1236. +}
  1237. +
  1238. +
  1239. +/******************************************************************/
  1240. +/* Host Controller interface functions */
  1241. +/******************************************************************/
  1242. +
  1243. +static inline void crisv10_ready_wait(void) {
  1244. + volatile int timeout = 10000;
  1245. + /* Check the busy bit of USB controller in Etrax */
  1246. + while((*R_USB_COMMAND & IO_MASK(R_USB_COMMAND, busy)) &&
  1247. + (timeout-- > 0));
  1248. + if(timeout == 0) {
  1249. + warn("Timeout while waiting for USB controller to be idle\n");
  1250. + }
  1251. +}
  1252. +
  1253. +/* reset host controller */
  1254. +static int crisv10_hcd_reset(struct usb_hcd *hcd)
  1255. +{
  1256. + DBFENTER;
  1257. + hcd_dbg(hcd, "reset\n");
  1258. +
  1259. +
  1260. + /* Reset the USB interface. */
  1261. + /*
  1262. + *R_USB_COMMAND =
  1263. + IO_STATE(R_USB_COMMAND, port_sel, nop) |
  1264. + IO_STATE(R_USB_COMMAND, port_cmd, reset) |
  1265. + IO_STATE(R_USB_COMMAND, ctrl_cmd, reset);
  1266. + nop();
  1267. + */
  1268. + DBFEXIT;
  1269. + return 0;
  1270. +}
  1271. +
  1272. +/* start host controller */
  1273. +static int crisv10_hcd_start(struct usb_hcd *hcd)
  1274. +{
  1275. + DBFENTER;
  1276. + hcd_dbg(hcd, "start\n");
  1277. +
  1278. + crisv10_ready_wait();
  1279. +
  1280. + /* Start processing of USB traffic. */
  1281. + *R_USB_COMMAND =
  1282. + IO_STATE(R_USB_COMMAND, port_sel, nop) |
  1283. + IO_STATE(R_USB_COMMAND, port_cmd, reset) |
  1284. + IO_STATE(R_USB_COMMAND, ctrl_cmd, host_run);
  1285. +
  1286. + nop();
  1287. +
  1288. + hcd->state = HC_STATE_RUNNING;
  1289. +
  1290. + DBFEXIT;
  1291. + return 0;
  1292. +}
  1293. +
  1294. +/* stop host controller */
  1295. +static void crisv10_hcd_stop(struct usb_hcd *hcd)
  1296. +{
  1297. + DBFENTER;
  1298. + hcd_dbg(hcd, "stop\n");
  1299. + crisv10_hcd_reset(hcd);
  1300. + DBFEXIT;
  1301. +}
  1302. +
  1303. +/* return the current frame number */
  1304. +static int crisv10_hcd_get_frame(struct usb_hcd *hcd)
  1305. +{
  1306. + DBFENTER;
  1307. + DBFEXIT;
  1308. + return (*R_USB_FM_NUMBER & 0x7ff);
  1309. +}
  1310. +
  1311. +#ifdef CONFIG_USB_OTG
  1312. +
  1313. +static int crisv10_hcd_start_port_reset(struct usb_hcd *hcd, unsigned port)
  1314. +{
  1315. + return 0; /* no-op for now */
  1316. +}
  1317. +
  1318. +#endif /* CONFIG_USB_OTG */
  1319. +
  1320. +
  1321. +/******************************************************************/
  1322. +/* Root Hub functions */
  1323. +/******************************************************************/
  1324. +
  1325. +/* root hub status */
  1326. +static const struct usb_hub_status rh_hub_status =
  1327. + {
  1328. + .wHubStatus = 0,
  1329. + .wHubChange = 0,
  1330. + };
  1331. +
  1332. +/* root hub descriptor */
  1333. +static const u8 rh_hub_descr[] =
  1334. + {
  1335. + 0x09, /* bDescLength */
  1336. + 0x29, /* bDescriptorType */
  1337. + USB_ROOT_HUB_PORTS, /* bNbrPorts */
  1338. + 0x00, /* wHubCharacteristics */
  1339. + 0x00,
  1340. + 0x01, /* bPwrOn2pwrGood */
  1341. + 0x00, /* bHubContrCurrent */
  1342. + 0x00, /* DeviceRemovable */
  1343. + 0xff /* PortPwrCtrlMask */
  1344. + };
  1345. +
  1346. +/* Actual holder of root hub status*/
  1347. +struct crisv10_rh rh;
  1348. +
  1349. +/* Initialize root hub data structures (called from dvdrv_hcd_probe()) */
  1350. +int rh_init(void) {
  1351. + int i;
  1352. + /* Reset port status flags */
  1353. + for (i = 0; i < USB_ROOT_HUB_PORTS; i++) {
  1354. + rh.wPortChange[i] = 0;
  1355. + rh.wPortStatusPrev[i] = 0;
  1356. + }
  1357. + return 0;
  1358. +}
  1359. +
  1360. +#define RH_FEAT_MASK ((1<<USB_PORT_FEAT_CONNECTION)|\
  1361. + (1<<USB_PORT_FEAT_ENABLE)|\
  1362. + (1<<USB_PORT_FEAT_SUSPEND)|\
  1363. + (1<<USB_PORT_FEAT_RESET))
  1364. +
  1365. +/* Handle port status change interrupt (called from bottom part interrupt) */
  1366. +void rh_port_status_change(__u16 port_reg[]) {
  1367. + int i;
  1368. + __u16 wChange;
  1369. +
  1370. + for(i = 0; i < USB_ROOT_HUB_PORTS; i++) {
  1371. + /* Xor out changes since last read, masked for important flags */
  1372. + wChange = (port_reg[i] & RH_FEAT_MASK) ^ rh.wPortStatusPrev[i];
  1373. + /* Or changes together with (if any) saved changes */
  1374. + rh.wPortChange[i] |= wChange;
  1375. + /* Save new status */
  1376. + rh.wPortStatusPrev[i] = port_reg[i];
  1377. +
  1378. + if(wChange) {
  1379. + rh_dbg("Interrupt port_status change port%d: %s Current-status:%s\n", i+1,
  1380. + port_status_to_str(wChange),
  1381. + port_status_to_str(port_reg[i]));
  1382. + }
  1383. + }
  1384. +}
  1385. +
  1386. +/* Construct port status change bitmap for the root hub */
  1387. +static int rh_status_data_request(struct usb_hcd *hcd, char *buf)
  1388. +{
  1389. + struct crisv10_hcd* crisv10_hcd = hcd_to_crisv10_hcd(hcd);
  1390. + unsigned int i;
  1391. +
  1392. +// DBFENTER;
  1393. +
  1394. + /*
  1395. + * corresponds to hub status change EP (USB 2.0 spec section 11.13.4)
  1396. + * return bitmap indicating ports with status change
  1397. + */
  1398. + *buf = 0;
  1399. + spin_lock(&crisv10_hcd->lock);
  1400. + for (i = 1; i <= crisv10_hcd->num_ports; i++) {
  1401. + if (rh.wPortChange[map_port(i)]) {
  1402. + *buf |= (1 << i);
  1403. + rh_dbg("rh_status_data_request, change on port %d: %s Current Status: %s\n", i,
  1404. + port_status_to_str(rh.wPortChange[map_port(i)]),
  1405. + port_status_to_str(rh.wPortStatusPrev[map_port(i)]));
  1406. + }
  1407. + }
  1408. + spin_unlock(&crisv10_hcd->lock);
  1409. +
  1410. +// DBFEXIT;
  1411. +
  1412. + return *buf == 0 ? 0 : 1;
  1413. +}
  1414. +
  1415. +/* Handle a control request for the root hub (called from hcd_driver) */
  1416. +static int rh_control_request(struct usb_hcd *hcd,
  1417. + u16 typeReq,
  1418. + u16 wValue,
  1419. + u16 wIndex,
  1420. + char *buf,
  1421. + u16 wLength) {
  1422. +
  1423. + struct crisv10_hcd *crisv10_hcd = hcd_to_crisv10_hcd(hcd);
  1424. + int retval = 0;
  1425. + int len;
  1426. + DBFENTER;
  1427. +
  1428. + switch (typeReq) {
  1429. + case GetHubDescriptor:
  1430. + rh_dbg("GetHubDescriptor\n");
  1431. + len = min_t(unsigned int, sizeof rh_hub_descr, wLength);
  1432. + memcpy(buf, rh_hub_descr, len);
  1433. + buf[2] = crisv10_hcd->num_ports;
  1434. + break;
  1435. + case GetHubStatus:
  1436. + rh_dbg("GetHubStatus\n");
  1437. + len = min_t(unsigned int, sizeof rh_hub_status, wLength);
  1438. + memcpy(buf, &rh_hub_status, len);
  1439. + break;
  1440. + case GetPortStatus:
  1441. + if (!wIndex || wIndex > crisv10_hcd->num_ports)
  1442. + goto error;
  1443. + rh_dbg("GetportStatus, port:%d change:%s status:%s\n", wIndex,
  1444. + port_status_to_str(rh.wPortChange[map_port(wIndex)]),
  1445. + port_status_to_str(rh.wPortStatusPrev[map_port(wIndex)]));
  1446. + *(u16 *) buf = cpu_to_le16(rh.wPortStatusPrev[map_port(wIndex)]);
  1447. + *(u16 *) (buf + 2) = cpu_to_le16(rh.wPortChange[map_port(wIndex)]);
  1448. + break;
  1449. + case SetHubFeature:
  1450. + rh_dbg("SetHubFeature\n");
  1451. + case ClearHubFeature:
  1452. + rh_dbg("ClearHubFeature\n");
  1453. + switch (wValue) {
  1454. + case C_HUB_OVER_CURRENT:
  1455. + case C_HUB_LOCAL_POWER:
  1456. + rh_warn("Not implemented hub request:%d \n", typeReq);
  1457. + /* not implemented */
  1458. + break;
  1459. + default:
  1460. + goto error;
  1461. + }
  1462. + break;
  1463. + case SetPortFeature:
  1464. + if (!wIndex || wIndex > crisv10_hcd->num_ports)
  1465. + goto error;
  1466. + if(rh_set_port_feature(map_port(wIndex), wValue))
  1467. + goto error;
  1468. + break;
  1469. + case ClearPortFeature:
  1470. + if (!wIndex || wIndex > crisv10_hcd->num_ports)
  1471. + goto error;
  1472. + if(rh_clear_port_feature(map_port(wIndex), wValue))
  1473. + goto error;
  1474. + break;
  1475. + default:
  1476. + rh_warn("Unknown hub request: %d\n", typeReq);
  1477. + error:
  1478. + retval = -EPIPE;
  1479. + }
  1480. + DBFEXIT;
  1481. + return retval;
  1482. +}
  1483. +
  1484. +int rh_set_port_feature(__u8 bPort, __u16 wFeature) {
  1485. + __u8 bUsbCommand = 0;
  1486. + switch(wFeature) {
  1487. + case USB_PORT_FEAT_RESET:
  1488. + rh_dbg("SetPortFeature: reset\n");
  1489. + bUsbCommand |= IO_STATE(R_USB_COMMAND, port_cmd, reset);
  1490. + goto set;
  1491. + break;
  1492. + case USB_PORT_FEAT_SUSPEND:
  1493. + rh_dbg("SetPortFeature: suspend\n");
  1494. + bUsbCommand |= IO_STATE(R_USB_COMMAND, port_cmd, suspend);
  1495. + goto set;
  1496. + break;
  1497. + case USB_PORT_FEAT_POWER:
  1498. + rh_dbg("SetPortFeature: power\n");
  1499. + break;
  1500. + case USB_PORT_FEAT_C_CONNECTION:
  1501. + rh_dbg("SetPortFeature: c_connection\n");
  1502. + break;
  1503. + case USB_PORT_FEAT_C_RESET:
  1504. + rh_dbg("SetPortFeature: c_reset\n");
  1505. + break;
  1506. + case USB_PORT_FEAT_C_OVER_CURRENT:
  1507. + rh_dbg("SetPortFeature: c_over_current\n");
  1508. + break;
  1509. +
  1510. + set:
  1511. + /* Select which port via the port_sel field */
  1512. + bUsbCommand |= IO_FIELD(R_USB_COMMAND, port_sel, bPort+1);
  1513. +
  1514. + /* Make sure the controller isn't busy. */
  1515. + crisv10_ready_wait();
  1516. + /* Send out the actual command to the USB controller */
  1517. + *R_USB_COMMAND = bUsbCommand;
  1518. +
  1519. + /* If port reset then also bring USB controller into running state */
  1520. + if(wFeature == USB_PORT_FEAT_RESET) {
  1521. + /* Wait a while for controller to first become started after port reset */
  1522. + udelay(12000); /* 12ms blocking wait */
  1523. +
  1524. + /* Make sure the controller isn't busy. */
  1525. + crisv10_ready_wait();
  1526. +
  1527. + /* If all enabled ports were disabled the host controller goes down into
  1528. + started mode, so we need to bring it back into the running state.
  1529. + (This is safe even if it's already in the running state.) */
  1530. + *R_USB_COMMAND =
  1531. + IO_STATE(R_USB_COMMAND, port_sel, nop) |
  1532. + IO_STATE(R_USB_COMMAND, port_cmd, reset) |
  1533. + IO_STATE(R_USB_COMMAND, ctrl_cmd, host_run);
  1534. + }
  1535. +
  1536. + break;
  1537. + default:
  1538. + rh_dbg("SetPortFeature: unknown feature\n");
  1539. + return -1;
  1540. + }
  1541. + return 0;
  1542. +}
  1543. +
  1544. +int rh_clear_port_feature(__u8 bPort, __u16 wFeature) {
  1545. + switch(wFeature) {
  1546. + case USB_PORT_FEAT_ENABLE:
  1547. + rh_dbg("ClearPortFeature: enable\n");
  1548. + rh_disable_port(bPort);
  1549. + break;
  1550. + case USB_PORT_FEAT_SUSPEND:
  1551. + rh_dbg("ClearPortFeature: suspend\n");
  1552. + break;
  1553. + case USB_PORT_FEAT_POWER:
  1554. + rh_dbg("ClearPortFeature: power\n");
  1555. + break;
  1556. +
  1557. + case USB_PORT_FEAT_C_ENABLE:
  1558. + rh_dbg("ClearPortFeature: c_enable\n");
  1559. + goto clear;
  1560. + case USB_PORT_FEAT_C_SUSPEND:
  1561. + rh_dbg("ClearPortFeature: c_suspend\n");
  1562. + goto clear;
  1563. + case USB_PORT_FEAT_C_CONNECTION:
  1564. + rh_dbg("ClearPortFeature: c_connection\n");
  1565. + goto clear;
  1566. + case USB_PORT_FEAT_C_OVER_CURRENT:
  1567. + rh_dbg("ClearPortFeature: c_over_current\n");
  1568. + goto clear;
  1569. + case USB_PORT_FEAT_C_RESET:
  1570. + rh_dbg("ClearPortFeature: c_reset\n");
  1571. + goto clear;
  1572. + clear:
  1573. + rh.wPortChange[bPort] &= ~(1 << (wFeature - 16));
  1574. + break;
  1575. + default:
  1576. + rh_dbg("ClearPortFeature: unknown feature\n");
  1577. + return -1;
  1578. + }
  1579. + return 0;
  1580. +}
  1581. +
  1582. +
  1583. +#ifdef CONFIG_PM
  1584. +/* Handle a suspend request for the root hub (called from hcd_driver) */
  1585. +static int rh_suspend_request(struct usb_hcd *hcd)
  1586. +{
  1587. + return 0; /* no-op for now */
  1588. +}
  1589. +
  1590. +/* Handle a resume request for the root hub (called from hcd_driver) */
  1591. +static int rh_resume_request(struct usb_hcd *hcd)
  1592. +{
  1593. + return 0; /* no-op for now */
  1594. +}
  1595. +#endif /* CONFIG_PM */
  1596. +
  1597. +
  1598. +
  1599. +/* Wrapper function for workaround port disable registers in USB controller */
  1600. +static void rh_disable_port(unsigned int port) {
  1601. + volatile int timeout = 10000;
  1602. + volatile char* usb_portx_disable;
  1603. + switch(port) {
  1604. + case 0:
  1605. + usb_portx_disable = R_USB_PORT1_DISABLE;
  1606. + break;
  1607. + case 1:
  1608. + usb_portx_disable = R_USB_PORT2_DISABLE;
  1609. + break;
  1610. + default:
  1611. + /* Invalid port index */
  1612. + return;
  1613. + }
  1614. + /* Set disable flag in special register */
  1615. + *usb_portx_disable = IO_STATE(R_USB_PORT1_DISABLE, disable, yes);
  1616. + /* Wait until not enabled anymore */
  1617. + while((rh.wPortStatusPrev[port] &
  1618. + IO_STATE(R_USB_RH_PORT_STATUS_1, enabled, yes)) &&
  1619. + (timeout-- > 0));
  1620. + if(timeout == 0) {
  1621. + warn("Timeout while waiting for port %d to become disabled\n", port);
  1622. + }
  1623. + /* clear disable flag in special register */
  1624. + *usb_portx_disable = IO_STATE(R_USB_PORT1_DISABLE, disable, no);
  1625. + rh_info("Physical port %d disabled\n", port+1);
  1626. +}
  1627. +
  1628. +
  1629. +/******************************************************************/
  1630. +/* Transfer Controller (TC) functions */
  1631. +/******************************************************************/
  1632. +
  1633. +/* FIXME: Should RX_BUF_SIZE be a config option, or maybe we should adjust it
  1634. + dynamically?
  1635. + To adjust it dynamically we would have to get an interrupt when we reach
  1636. + the end of the rx descriptor list, or when we get close to the end, and
  1637. + then allocate more descriptors. */
  1638. +#define NBR_OF_RX_DESC 512
  1639. +#define RX_DESC_BUF_SIZE 1024
  1640. +#define RX_BUF_SIZE (NBR_OF_RX_DESC * RX_DESC_BUF_SIZE)
  1641. +
  1642. +
  1643. +/* Local variables for Transfer Controller */
  1644. +/* --------------------------------------- */
  1645. +
  1646. +/* This is a circular (double-linked) list of the active urbs for each epid.
  1647. + The head is never removed, and new urbs are linked onto the list as
  1648. + urb_entry_t elements. Don't reference urb_list directly; use the wrapper
  1649. + functions instead (which includes spin_locks) */
  1650. +static struct list_head urb_list[NBR_OF_EPIDS];
  1651. +
  1652. +/* Read about the need and usage of this lock in submit_ctrl_urb. */
  1653. +/* Lock for URB lists for each EPID */
  1654. +static spinlock_t urb_list_lock;
  1655. +
  1656. +/* Lock for EPID array register (R_USB_EPT_x) in Etrax */
  1657. +static spinlock_t etrax_epid_lock;
  1658. +
  1659. +/* Lock for dma8 sub0 handling */
  1660. +static spinlock_t etrax_dma8_sub0_lock;
  1661. +
  1662. +/* DMA IN cache bug. Align the DMA IN buffers to 32 bytes, i.e. a cache line.
  1663. + Since RX_DESC_BUF_SIZE is 1024 is a multiple of 32, all rx buffers will be
  1664. + cache aligned. */
  1665. +static volatile unsigned char RxBuf[RX_BUF_SIZE] __attribute__ ((aligned (32)));
  1666. +static volatile struct USB_IN_Desc RxDescList[NBR_OF_RX_DESC] __attribute__ ((aligned (4)));
  1667. +
  1668. +/* Pointers into RxDescList. */
  1669. +static volatile struct USB_IN_Desc *myNextRxDesc;
  1670. +static volatile struct USB_IN_Desc *myLastRxDesc;
  1671. +
  1672. +/* A zout transfer makes a memory access at the address of its buf pointer,
  1673. + which means that setting this buf pointer to 0 will cause an access to the
  1674. + flash. In addition to this, setting sw_len to 0 results in a 16/32 bytes
  1675. + (depending on DMA burst size) transfer.
  1676. + Instead, we set it to 1, and point it to this buffer. */
  1677. +static int zout_buffer[4] __attribute__ ((aligned (4)));
  1678. +
  1679. +/* Cache for allocating new EP and SB descriptors. */
  1680. +//static kmem_cache_t *usb_desc_cache;
  1681. +static struct kmem_cache *usb_desc_cache;
  1682. +
  1683. +/* Cache for the data allocated in the isoc descr top half. */
  1684. +//static kmem_cache_t *isoc_compl_cache;
  1685. +static struct kmem_cache *isoc_compl_cache;
  1686. +
  1687. +/* Cache for the data allocated when delayed finishing of URBs */
  1688. +//static kmem_cache_t *later_data_cache;
  1689. +static struct kmem_cache *later_data_cache;
  1690. +
  1691. +/* Counter to keep track of how many Isoc EP we have sat up. Used to enable
  1692. + and disable iso_eof interrupt. We only need these interrupts when we have
  1693. + Isoc data endpoints (consumes CPU cycles).
  1694. + FIXME: This could be more fine granular, so this interrupt is only enabled
  1695. + when we have a In Isoc URB not URB_ISO_ASAP flaged queued. */
  1696. +static int isoc_epid_counter;
  1697. +
  1698. +/* Protecting wrapper functions for R_USB_EPT_x */
  1699. +/* -------------------------------------------- */
  1700. +static inline void etrax_epid_set(__u8 index, __u32 data) {
  1701. + unsigned long flags;
  1702. + spin_lock_irqsave(&etrax_epid_lock, flags);
  1703. + *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, index);
  1704. + nop();
  1705. + *R_USB_EPT_DATA = data;
  1706. + spin_unlock_irqrestore(&etrax_epid_lock, flags);
  1707. +}
  1708. +
  1709. +static inline void etrax_epid_clear_error(__u8 index) {
  1710. + unsigned long flags;
  1711. + spin_lock_irqsave(&etrax_epid_lock, flags);
  1712. + *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, index);
  1713. + nop();
  1714. + *R_USB_EPT_DATA &=
  1715. + ~(IO_MASK(R_USB_EPT_DATA, error_count_in) |
  1716. + IO_MASK(R_USB_EPT_DATA, error_count_out) |
  1717. + IO_MASK(R_USB_EPT_DATA, error_code));
  1718. + spin_unlock_irqrestore(&etrax_epid_lock, flags);
  1719. +}
  1720. +
  1721. +static inline void etrax_epid_set_toggle(__u8 index, __u8 dirout,
  1722. + __u8 toggle) {
  1723. + unsigned long flags;
  1724. + spin_lock_irqsave(&etrax_epid_lock, flags);
  1725. + *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, index);
  1726. + nop();
  1727. + if(dirout) {
  1728. + *R_USB_EPT_DATA &= ~IO_MASK(R_USB_EPT_DATA, t_out);
  1729. + *R_USB_EPT_DATA |= IO_FIELD(R_USB_EPT_DATA, t_out, toggle);
  1730. + } else {
  1731. + *R_USB_EPT_DATA &= ~IO_MASK(R_USB_EPT_DATA, t_in);
  1732. + *R_USB_EPT_DATA |= IO_FIELD(R_USB_EPT_DATA, t_in, toggle);
  1733. + }
  1734. + spin_unlock_irqrestore(&etrax_epid_lock, flags);
  1735. +}
  1736. +
  1737. +static inline __u8 etrax_epid_get_toggle(__u8 index, __u8 dirout) {
  1738. + unsigned long flags;
  1739. + __u8 toggle;
  1740. + spin_lock_irqsave(&etrax_epid_lock, flags);
  1741. + *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, index);
  1742. + nop();
  1743. + if (dirout) {
  1744. + toggle = IO_EXTRACT(R_USB_EPT_DATA, t_out, *R_USB_EPT_DATA);
  1745. + } else {
  1746. + toggle = IO_EXTRACT(R_USB_EPT_DATA, t_in, *R_USB_EPT_DATA);
  1747. + }
  1748. + spin_unlock_irqrestore(&etrax_epid_lock, flags);
  1749. + return toggle;
  1750. +}
  1751. +
  1752. +
  1753. +static inline __u32 etrax_epid_get(__u8 index) {
  1754. + unsigned long flags;
  1755. + __u32 data;
  1756. + spin_lock_irqsave(&etrax_epid_lock, flags);
  1757. + *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, index);
  1758. + nop();
  1759. + data = *R_USB_EPT_DATA;
  1760. + spin_unlock_irqrestore(&etrax_epid_lock, flags);
  1761. + return data;
  1762. +}
  1763. +
  1764. +
  1765. +
  1766. +
  1767. +/* Main functions for Transfer Controller */
  1768. +/* -------------------------------------- */
  1769. +
  1770. +/* Init structs, memories and lists used by Transfer Controller */
  1771. +int tc_init(struct usb_hcd *hcd) {
  1772. + int i;
  1773. + /* Clear software state info for all epids */
  1774. + memset(epid_state, 0, sizeof(struct etrax_epid) * NBR_OF_EPIDS);
  1775. +
  1776. + /* Set Invalid and Dummy as being in use and disabled */
  1777. + epid_state[INVALID_EPID].inuse = 1;
  1778. + epid_state[DUMMY_EPID].inuse = 1;
  1779. + epid_state[INVALID_EPID].disabled = 1;
  1780. + epid_state[DUMMY_EPID].disabled = 1;
  1781. +
  1782. + /* Clear counter for how many Isoc epids we have sat up */
  1783. + isoc_epid_counter = 0;
  1784. +
  1785. + /* Initialize the urb list by initiating a head for each list.
  1786. + Also reset list hodling active URB for each epid */
  1787. + for (i = 0; i < NBR_OF_EPIDS; i++) {
  1788. + INIT_LIST_HEAD(&urb_list[i]);
  1789. + activeUrbList[i] = NULL;
  1790. + }
  1791. +
  1792. + /* Init lock for URB lists */
  1793. + spin_lock_init(&urb_list_lock);
  1794. + /* Init lock for Etrax R_USB_EPT register */
  1795. + spin_lock_init(&etrax_epid_lock);
  1796. + /* Init lock for Etrax dma8 sub0 handling */
  1797. + spin_lock_init(&etrax_dma8_sub0_lock);
  1798. +
  1799. + /* We use kmem_cache_* to make sure that all DMA desc. are dword aligned */
  1800. +
  1801. + /* Note that we specify sizeof(struct USB_EP_Desc) as the size, but also
  1802. + allocate SB descriptors from this cache. This is ok since
  1803. + sizeof(struct USB_EP_Desc) == sizeof(struct USB_SB_Desc). */
  1804. +// usb_desc_cache = kmem_cache_create("usb_desc_cache",
  1805. +// sizeof(struct USB_EP_Desc), 0,
  1806. +// SLAB_HWCACHE_ALIGN, 0, 0);
  1807. + usb_desc_cache = kmem_cache_create(
  1808. + "usb_desc_cache",
  1809. + sizeof(struct USB_EP_Desc),
  1810. + 0,
  1811. + SLAB_HWCACHE_ALIGN,
  1812. + NULL);
  1813. + if(usb_desc_cache == NULL) {
  1814. + return -ENOMEM;
  1815. + }
  1816. +
  1817. + /* Create slab cache for speedy allocation of memory for isoc bottom-half
  1818. + interrupt handling */
  1819. +// isoc_compl_cache =
  1820. +// kmem_cache_create("isoc_compl_cache",
  1821. +// sizeof(struct crisv10_isoc_complete_data),
  1822. +// 0, SLAB_HWCACHE_ALIGN, 0, 0);
  1823. + isoc_compl_cache = kmem_cache_create(
  1824. + "isoc_compl_cache",
  1825. + sizeof(struct crisv10_isoc_complete_data),
  1826. + 0,
  1827. + SLAB_HWCACHE_ALIGN,
  1828. + NULL
  1829. + );
  1830. +
  1831. + if(isoc_compl_cache == NULL) {
  1832. + return -ENOMEM;
  1833. + }
  1834. +
  1835. + /* Create slab cache for speedy allocation of memory for later URB finish
  1836. + struct */
  1837. +// later_data_cache =
  1838. +// kmem_cache_create("later_data_cache",
  1839. +// sizeof(struct urb_later_data),
  1840. +// 0, SLAB_HWCACHE_ALIGN, 0, 0);
  1841. +
  1842. + later_data_cache = kmem_cache_create(
  1843. + "later_data_cache",
  1844. + sizeof(struct urb_later_data),
  1845. + 0,
  1846. + SLAB_HWCACHE_ALIGN,
  1847. + NULL
  1848. + );
  1849. +
  1850. + if(later_data_cache == NULL) {
  1851. + return -ENOMEM;
  1852. + }
  1853. +
  1854. +
  1855. + /* Initiate the bulk start timer. */
  1856. + init_timer(&bulk_start_timer);
  1857. + bulk_start_timer.expires = jiffies + BULK_START_TIMER_INTERVAL;
  1858. + bulk_start_timer.function = tc_bulk_start_timer_func;
  1859. + add_timer(&bulk_start_timer);
  1860. +
  1861. +
  1862. + /* Initiate the bulk eot timer. */
  1863. + init_timer(&bulk_eot_timer);
  1864. + bulk_eot_timer.expires = jiffies + BULK_EOT_TIMER_INTERVAL;
  1865. + bulk_eot_timer.function = tc_bulk_eot_timer_func;
  1866. + bulk_eot_timer.data = (unsigned long)hcd;
  1867. + add_timer(&bulk_eot_timer);
  1868. +
  1869. + return 0;
  1870. +}
  1871. +
  1872. +/* Uninitialize all resources used by Transfer Controller */
  1873. +void tc_destroy(void) {
  1874. +
  1875. + /* Destroy all slab cache */
  1876. + kmem_cache_destroy(usb_desc_cache);
  1877. + kmem_cache_destroy(isoc_compl_cache);
  1878. + kmem_cache_destroy(later_data_cache);
  1879. +
  1880. + /* Remove timers */
  1881. + del_timer(&bulk_start_timer);
  1882. + del_timer(&bulk_eot_timer);
  1883. +}
  1884. +
  1885. +static void restart_dma8_sub0(void) {
  1886. + unsigned long flags;
  1887. + spin_lock_irqsave(&etrax_dma8_sub0_lock, flags);
  1888. + /* Verify that the dma is not running */
  1889. + if ((*R_DMA_CH8_SUB0_CMD & IO_MASK(R_DMA_CH8_SUB0_CMD, cmd)) == 0) {
  1890. + struct USB_EP_Desc *ep = (struct USB_EP_Desc *)phys_to_virt(*R_DMA_CH8_SUB0_EP);
  1891. + while (DUMMY_EPID == IO_EXTRACT(USB_EP_command, epid, ep->command)) {
  1892. + ep = (struct USB_EP_Desc *)phys_to_virt(ep->next);
  1893. + }
  1894. + /* Advance the DMA to the next EP descriptor that is not a DUMMY_EPID.
  1895. + * ep->next is already a physical address. virt_to_phys is needed, see
  1896. + * http://mhonarc.axis.se/dev-etrax/msg08630.html
  1897. + */
  1898. + //*R_DMA_CH8_SUB0_EP = ep->next;
  1899. + *R_DMA_CH8_SUB0_EP = virt_to_phys(ep);
  1900. + /* Restart the DMA */
  1901. + *R_DMA_CH8_SUB0_CMD = IO_STATE(R_DMA_CH8_SUB0_CMD, cmd, start);
  1902. + }
  1903. + spin_unlock_irqrestore(&etrax_dma8_sub0_lock, flags);
  1904. +}
  1905. +
  1906. +/* queue an URB with the transfer controller (called from hcd_driver) */
  1907. +//static int tc_urb_enqueue(struct usb_hcd *hcd,
  1908. +// struct usb_host_endpoint *ep,
  1909. +// struct urb *urb,
  1910. +// gfp_t mem_flags) {
  1911. +static int tc_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
  1912. +{
  1913. + int epid;
  1914. + int retval;
  1915. +// int bustime = 0;
  1916. + int maxpacket;
  1917. + unsigned long flags;
  1918. + struct crisv10_urb_priv *urb_priv;
  1919. + struct crisv10_hcd* crisv10_hcd = hcd_to_crisv10_hcd(hcd);
  1920. + DBFENTER;
  1921. +
  1922. + if(!(crisv10_hcd->running)) {
  1923. + /* The USB Controller is not running, probably because no device is
  1924. + attached. No idea to enqueue URBs then */
  1925. + tc_warn("Rejected enqueueing of URB:0x%x because no dev attached\n",
  1926. + (unsigned int)urb);
  1927. + return -ENOENT;
  1928. + }
  1929. +
  1930. + maxpacket = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe));
  1931. +
  1932. + /* hinko ignore usb_pipeisoc */
  1933. +#if 0
  1934. + /* Special case check for In Isoc transfers. Specification states that each
  1935. + In Isoc transfer consists of one packet and therefore it should fit into
  1936. + the transfer-buffer of an URB.
  1937. + We do the check here to be sure (an invalid scenario can be produced with
  1938. + parameters to the usbtest suite) */
  1939. + if(usb_pipeisoc(urb->pipe) && usb_pipein(urb->pipe) &&
  1940. + (urb->transfer_buffer_length < maxpacket)) {
  1941. + tc_err("Submit In Isoc URB with buffer length:%d to pipe with maxpacketlen: %d\n", urb->transfer_buffer_length, maxpacket);
  1942. + return -EMSGSIZE;
  1943. + }
  1944. +
  1945. + /* Check if there is enough bandwidth for periodic transfer */
  1946. + if(usb_pipeint(urb->pipe) || usb_pipeisoc(urb->pipe)) {
  1947. + /* only check (and later claim) if not already claimed */
  1948. + if (urb->bandwidth == 0) {
  1949. + bustime = usb_check_bandwidth(urb->dev, urb);
  1950. + if (bustime < 0) {
  1951. + tc_err("Not enough periodic bandwidth\n");
  1952. + return -ENOSPC;
  1953. + }
  1954. + }
  1955. + }
  1956. +#endif
  1957. +
  1958. + /* Check if there is a epid for URBs destination, if not this function
  1959. + set up one. */
  1960. + //epid = tc_setup_epid(ep, urb, mem_flags);
  1961. + epid = tc_setup_epid(urb, mem_flags);
  1962. + if (epid < 0) {
  1963. + tc_err("Failed setup epid:%d for URB:0x%x\n", epid, (unsigned int)urb);
  1964. + DBFEXIT;
  1965. + return -ENOMEM;
  1966. + }
  1967. +
  1968. + if(urb == activeUrbList[epid]) {
  1969. + tc_err("Resubmition of allready active URB:0x%x\n", (unsigned int)urb);
  1970. + return -ENXIO;
  1971. + }
  1972. +
  1973. + if(urb_list_entry(urb, epid)) {
  1974. + tc_err("Resubmition of allready queued URB:0x%x\n", (unsigned int)urb);
  1975. + return -ENXIO;
  1976. + }
  1977. +
  1978. + /* If we actively have flaged endpoint as disabled then refuse submition */
  1979. + if(epid_state[epid].disabled) {
  1980. + return -ENOENT;
  1981. + }
  1982. +
  1983. + /* Allocate and init HC-private data for URB */
  1984. + if(urb_priv_create(hcd, urb, epid, mem_flags) != 0) {
  1985. + DBFEXIT;
  1986. + return -ENOMEM;
  1987. + }
  1988. + urb_priv = urb->hcpriv;
  1989. +
  1990. + tc_dbg("Enqueue URB:0x%x[%d] epid:%d (%s) bufflen:%d\n",
  1991. + (unsigned int)urb, urb_priv->urb_num, epid,
  1992. + pipe_to_str(urb->pipe), urb->transfer_buffer_length);
  1993. +
  1994. + /* Create and link SBs required for this URB */
  1995. + retval = create_sb_for_urb(urb, mem_flags);
  1996. + if(retval != 0) {
  1997. + tc_err("Failed to create SBs for URB:0x%x[%d]\n", (unsigned int)urb,
  1998. + urb_priv->urb_num);
  1999. + urb_priv_free(hcd, urb);
  2000. + DBFEXIT;
  2001. + return retval;
  2002. + }
  2003. +
  2004. + /* Init intr EP pool if this URB is a INTR transfer. This pool is later
  2005. + used when inserting EPs in the TxIntrEPList. We do the alloc here
  2006. + so we can't run out of memory later */
  2007. + if(usb_pipeint(urb->pipe)) {
  2008. + retval = init_intr_urb(urb, mem_flags);
  2009. + if(retval != 0) {
  2010. + tc_warn("Failed to init Intr URB\n");
  2011. + urb_priv_free(hcd, urb);
  2012. + DBFEXIT;
  2013. + return retval;
  2014. + }
  2015. + }
  2016. +
  2017. + /* Disable other access when inserting USB */
  2018. +
  2019. + /* BUG on sleeping inside int disabled if using local_irq_save/local_irq_restore
  2020. + * her - because urb_list_add() and tc_dma_process_queue() save irqs again !??!
  2021. + */
  2022. +// local_irq_save(flags);
  2023. +
  2024. + /* hinko ignore usb_pipeisoc */
  2025. +#if 0
  2026. + /* Claim bandwidth, if needed */
  2027. + if(bustime) {
  2028. + usb_claim_bandwidth(urb->dev, urb, bustime, 0);
  2029. + }
  2030. +
  2031. + /* Add URB to EP queue */
  2032. + urb_list_add(urb, epid, mem_flags);
  2033. +
  2034. + if(usb_pipeisoc(urb->pipe)) {
  2035. + /* Special processing of Isoc URBs. */
  2036. + tc_dma_process_isoc_urb(urb);
  2037. + } else {
  2038. + /* Process EP queue for rest of the URB types (Bulk, Ctrl, Intr) */
  2039. + tc_dma_process_queue(epid);
  2040. + }
  2041. +#endif
  2042. + /* Add URB to EP queue */
  2043. + urb_list_add(urb, epid, mem_flags);
  2044. +
  2045. + /*hinko link/unlink urb -> ep */
  2046. + spin_lock_irqsave(&crisv10_hcd->lock, flags);
  2047. + //spin_lock(&crisv10_hcd->lock);
  2048. + retval = usb_hcd_link_urb_to_ep(hcd, urb);
  2049. + if (retval) {
  2050. + spin_unlock_irqrestore(&crisv10_hcd->lock, flags);
  2051. + tc_warn("Failed to link urb to ep\n");
  2052. + urb_priv_free(hcd, urb);
  2053. + DBFEXIT;
  2054. + return retval;
  2055. + }
  2056. + spin_unlock_irqrestore(&crisv10_hcd->lock, flags);
  2057. + //spin_unlock(&crisv10_hcd->lock);
  2058. +
  2059. + /* Process EP queue for rest of the URB types (Bulk, Ctrl, Intr) */
  2060. + tc_dma_process_queue(epid);
  2061. +
  2062. +// local_irq_restore(flags);
  2063. +
  2064. + DBFEXIT;
  2065. + return 0;
  2066. +}
  2067. +
  2068. +/* remove an URB from the transfer controller queues (called from hcd_driver)*/
  2069. +//static int tc_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
  2070. +static int tc_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  2071. +{
  2072. + struct crisv10_urb_priv *urb_priv;
  2073. + unsigned long flags;
  2074. + int epid;
  2075. +
  2076. + DBFENTER;
  2077. + /* Disable interrupts here since a descriptor interrupt for the isoc epid
  2078. + will modify the sb list. This could possibly be done more granular, but
  2079. + urb_dequeue should not be used frequently anyway.
  2080. + */
  2081. + local_irq_save(flags);
  2082. +
  2083. + urb_priv = urb->hcpriv;
  2084. +
  2085. + if (!urb_priv) {
  2086. + /* This happens if a device driver calls unlink on an urb that
  2087. + was never submitted (lazy driver) or if the urb was completed
  2088. + while dequeue was being called. */
  2089. + tc_warn("Dequeing of not enqueued URB:0x%x\n", (unsigned int)urb);
  2090. + local_irq_restore(flags);
  2091. + return 0;
  2092. + }
  2093. + epid = urb_priv->epid;
  2094. +
  2095. + tc_warn("Dequeing %s URB:0x%x[%d] (%s %s epid:%d) status:%d %s\n",
  2096. + (urb == activeUrbList[epid]) ? "active" : "queued",
  2097. + (unsigned int)urb, urb_priv->urb_num, str_dir(urb->pipe),
  2098. + str_type(urb->pipe), epid, urb->status,
  2099. + (urb_priv->later_data) ? "later-sched" : "");
  2100. +
  2101. + /* For Bulk, Ctrl and Intr are only one URB active at a time. So any URB
  2102. + that isn't active can be dequeued by just removing it from the queue */
  2103. + if(usb_pipebulk(urb->pipe) || usb_pipecontrol(urb->pipe) ||
  2104. + usb_pipeint(urb->pipe)) {
  2105. +
  2106. + /* Check if URB haven't gone further than the queue */
  2107. + if(urb != activeUrbList[epid]) {
  2108. + ASSERT(urb_priv->later_data == NULL);
  2109. + tc_warn("Dequeing URB:0x%x[%d] (%s %s epid:%d) from queue"
  2110. + " (not active)\n", (unsigned int)urb, urb_priv->urb_num,
  2111. + str_dir(urb->pipe), str_type(urb->pipe), epid);
  2112. +
  2113. + /* Finish the URB with error status from USB core */
  2114. + tc_finish_urb(hcd, urb, urb->status);
  2115. + local_irq_restore(flags);
  2116. + return 0;
  2117. + }
  2118. + }
  2119. +
  2120. + /* Set URB status to Unlink for handling when interrupt comes. */
  2121. + urb_priv->urb_state = UNLINK;
  2122. +
  2123. + /* Differentiate dequeing of Bulk and Ctrl from Isoc and Intr */
  2124. + switch(usb_pipetype(urb->pipe)) {
  2125. + case PIPE_BULK:
  2126. + /* Check if EP still is enabled */
  2127. + if (TxBulkEPList[epid].command & IO_MASK(USB_EP_command, enable)) {
  2128. + /* The EP was enabled, disable it. */
  2129. + TxBulkEPList[epid].command &= ~IO_MASK(USB_EP_command, enable);
  2130. + }
  2131. + /* Kicking dummy list out of the party. */
  2132. + TxBulkEPList[epid].next = virt_to_phys(&TxBulkEPList[(epid + 1) % NBR_OF_EPIDS]);
  2133. + break;
  2134. + case PIPE_CONTROL:
  2135. + /* Check if EP still is enabled */
  2136. + if (TxCtrlEPList[epid].command & IO_MASK(USB_EP_command, enable)) {
  2137. + /* The EP was enabled, disable it. */
  2138. + TxCtrlEPList[epid].command &= ~IO_MASK(USB_EP_command, enable);
  2139. + }
  2140. + break;
  2141. + case PIPE_ISOCHRONOUS:
  2142. + /* Disabling, busy-wait and unlinking of Isoc SBs will be done in
  2143. + finish_isoc_urb(). Because there might the case when URB is dequeued
  2144. + but there are other valid URBs waiting */
  2145. +
  2146. + /* Check if In Isoc EP still is enabled */
  2147. + if (TxIsocEPList[epid].command & IO_MASK(USB_EP_command, enable)) {
  2148. + /* The EP was enabled, disable it. */
  2149. + TxIsocEPList[epid].command &= ~IO_MASK(USB_EP_command, enable);
  2150. + }
  2151. + break;
  2152. + case PIPE_INTERRUPT:
  2153. + /* Special care is taken for interrupt URBs. EPs are unlinked in
  2154. + tc_finish_urb */
  2155. + break;
  2156. + default:
  2157. + break;
  2158. + }
  2159. +
  2160. + /* Asynchronous unlink, finish the URB later from scheduled or other
  2161. + event (data finished, error) */
  2162. + tc_finish_urb_later(hcd, urb, urb->status);
  2163. +
  2164. + local_irq_restore(flags);
  2165. + DBFEXIT;
  2166. + return 0;
  2167. +}
  2168. +
  2169. +
  2170. +static void tc_sync_finish_epid(struct usb_hcd *hcd, int epid) {
  2171. + volatile int timeout = 10000;
  2172. + struct urb* urb;
  2173. + struct crisv10_urb_priv* urb_priv;
  2174. + unsigned long flags;
  2175. +
  2176. + volatile struct USB_EP_Desc *first_ep; /* First EP in the list. */
  2177. + volatile struct USB_EP_Desc *curr_ep; /* Current EP, the iterator. */
  2178. + volatile struct USB_EP_Desc *next_ep; /* The EP after current. */
  2179. +
  2180. + int type = epid_state[epid].type;
  2181. +
  2182. + /* Setting this flag will cause enqueue() to return -ENOENT for new
  2183. + submitions on this endpoint and finish_urb() wont process queue further */
  2184. + epid_state[epid].disabled = 1;
  2185. +
  2186. + switch(type) {
  2187. + case PIPE_BULK:
  2188. + /* Check if EP still is enabled */
  2189. + if (TxBulkEPList[epid].command & IO_MASK(USB_EP_command, enable)) {
  2190. + /* The EP was enabled, disable it. */
  2191. + TxBulkEPList[epid].command &= ~IO_MASK(USB_EP_command, enable);
  2192. + tc_warn("sync_finish: Disabling EP for epid:%d\n", epid);
  2193. +
  2194. + /* Do busy-wait until DMA not using this EP descriptor anymore */
  2195. + while((*R_DMA_CH8_SUB0_EP ==
  2196. + virt_to_phys(&TxBulkEPList[epid])) &&
  2197. + (timeout-- > 0));
  2198. + if(timeout == 0) {
  2199. + warn("Timeout while waiting for DMA-TX-Bulk to leave EP for"
  2200. + " epid:%d\n", epid);
  2201. + }
  2202. + }
  2203. + break;
  2204. +
  2205. + case PIPE_CONTROL:
  2206. + /* Check if EP still is enabled */
  2207. + if (TxCtrlEPList[epid].command & IO_MASK(USB_EP_command, enable)) {
  2208. + /* The EP was enabled, disable it. */
  2209. + TxCtrlEPList[epid].command &= ~IO_MASK(USB_EP_command, enable);
  2210. + tc_warn("sync_finish: Disabling EP for epid:%d\n", epid);
  2211. +
  2212. + /* Do busy-wait until DMA not using this EP descriptor anymore */
  2213. + while((*R_DMA_CH8_SUB1_EP ==
  2214. + virt_to_phys(&TxCtrlEPList[epid])) &&
  2215. + (timeout-- > 0));
  2216. + if(timeout == 0) {
  2217. + warn("Timeout while waiting for DMA-TX-Ctrl to leave EP for"
  2218. + " epid:%d\n", epid);
  2219. + }
  2220. + }
  2221. + break;
  2222. +
  2223. + case PIPE_INTERRUPT:
  2224. + local_irq_save(flags);
  2225. + /* Disable all Intr EPs belonging to epid */
  2226. + first_ep = &TxIntrEPList[0];
  2227. + curr_ep = first_ep;
  2228. + do {
  2229. + next_ep = (struct USB_EP_Desc *)phys_to_virt(curr_ep->next);
  2230. + if (IO_EXTRACT(USB_EP_command, epid, next_ep->command) == epid) {
  2231. + /* Disable EP */
  2232. + next_ep->command &= ~IO_MASK(USB_EP_command, enable);
  2233. + }
  2234. + curr_ep = phys_to_virt(curr_ep->next);
  2235. + } while (curr_ep != first_ep);
  2236. +
  2237. + local_irq_restore(flags);
  2238. + break;
  2239. +
  2240. + case PIPE_ISOCHRONOUS:
  2241. + /* Check if EP still is enabled */
  2242. + if (TxIsocEPList[epid].command & IO_MASK(USB_EP_command, enable)) {
  2243. + tc_warn("sync_finish: Disabling Isoc EP for epid:%d\n", epid);
  2244. + /* The EP was enabled, disable it. */
  2245. + TxIsocEPList[epid].command &= ~IO_MASK(USB_EP_command, enable);
  2246. +
  2247. + while((*R_DMA_CH8_SUB3_EP == virt_to_phys(&TxIsocEPList[epid])) &&
  2248. + (timeout-- > 0));
  2249. + if(timeout == 0) {
  2250. + warn("Timeout while waiting for DMA-TX-Isoc to leave EP for"
  2251. + " epid:%d\n", epid);
  2252. + }
  2253. + }
  2254. + break;
  2255. + }
  2256. +
  2257. + local_irq_save(flags);
  2258. +
  2259. + /* Finish if there is active URB for this endpoint */
  2260. + if(activeUrbList[epid] != NULL) {
  2261. + urb = activeUrbList[epid];
  2262. + urb_priv = urb->hcpriv;
  2263. + ASSERT(urb_priv);
  2264. + tc_warn("Sync finish %s URB:0x%x[%d] (%s %s epid:%d) status:%d %s\n",
  2265. + (urb == activeUrbList[epid]) ? "active" : "queued",
  2266. + (unsigned int)urb, urb_priv->urb_num, str_dir(urb->pipe),
  2267. + str_type(urb->pipe), epid, urb->status,
  2268. + (urb_priv->later_data) ? "later-sched" : "");
  2269. +
  2270. + tc_finish_urb(hcd, activeUrbList[epid], -ENOENT);
  2271. + ASSERT(activeUrbList[epid] == NULL);
  2272. + }
  2273. +
  2274. + /* Finish any queued URBs for this endpoint. There won't be any resubmitions
  2275. + because epid_disabled causes enqueue() to fail for this endpoint */
  2276. + while((urb = urb_list_first(epid)) != NULL) {
  2277. + urb_priv = urb->hcpriv;
  2278. + ASSERT(urb_priv);
  2279. +
  2280. + tc_warn("Sync finish %s URB:0x%x[%d] (%s %s epid:%d) status:%d %s\n",
  2281. + (urb == activeUrbList[epid]) ? "active" : "queued",
  2282. + (unsigned int)urb, urb_priv->urb_num, str_dir(urb->pipe),
  2283. + str_type(urb->pipe), epid, urb->status,
  2284. + (urb_priv->later_data) ? "later-sched" : "");
  2285. +
  2286. + tc_finish_urb(hcd, urb, -ENOENT);
  2287. + }
  2288. + epid_state[epid].disabled = 0;
  2289. + local_irq_restore(flags);
  2290. +}
  2291. +
  2292. +/* free resources associated with an endpoint (called from hcd_driver) */
  2293. +static void tc_endpoint_disable(struct usb_hcd *hcd,
  2294. + struct usb_host_endpoint *ep) {
  2295. + DBFENTER;
  2296. + /* Only free epid if it has been allocated. We get two endpoint_disable
  2297. + requests for ctrl endpoints so ignore the second one */
  2298. + if(ep->hcpriv != NULL) {
  2299. + struct crisv10_ep_priv *ep_priv = ep->hcpriv;
  2300. + int epid = ep_priv->epid;
  2301. + tc_warn("endpoint_disable ep:0x%x ep-priv:0x%x (%s) (epid:%d freed)\n",
  2302. + (unsigned int)ep, (unsigned int)ep->hcpriv,
  2303. + endpoint_to_str(&(ep->desc)), epid);
  2304. +
  2305. + tc_sync_finish_epid(hcd, epid);
  2306. +
  2307. + ASSERT(activeUrbList[epid] == NULL);
  2308. + ASSERT(list_empty(&urb_list[epid]));
  2309. +
  2310. + tc_free_epid(ep);
  2311. + } else {
  2312. + tc_dbg("endpoint_disable ep:0x%x ep-priv:0x%x (%s)\n", (unsigned int)ep,
  2313. + (unsigned int)ep->hcpriv, endpoint_to_str(&(ep->desc)));
  2314. + }
  2315. + DBFEXIT;
  2316. +}
  2317. +
  2318. +//static void tc_finish_urb_later_proc(void *data) {
  2319. +static void tc_finish_urb_later_proc(struct work_struct *work) {
  2320. + unsigned long flags;
  2321. + //struct urb_later_data* uld = (struct urb_later_data*)data;
  2322. + struct urb_later_data* uld = container_of(work, struct urb_later_data, ws.work);
  2323. + local_irq_save(flags);
  2324. + if(uld->urb == NULL) {
  2325. + late_dbg("Later finish of URB = NULL (allready finished)\n");
  2326. + } else {
  2327. + struct crisv10_urb_priv* urb_priv = uld->urb->hcpriv;
  2328. + ASSERT(urb_priv);
  2329. + if(urb_priv->urb_num == uld->urb_num) {
  2330. + late_dbg("Later finish of URB:0x%x[%d]\n", (unsigned int)(uld->urb),
  2331. + urb_priv->urb_num);
  2332. + if(uld->status != uld->urb->status) {
  2333. + errno_dbg("Later-finish URB with status:%d, later-status:%d\n",
  2334. + uld->urb->status, uld->status);
  2335. + }
  2336. + if(uld != urb_priv->later_data) {
  2337. + panic("Scheduled uld not same as URBs uld\n");
  2338. + }
  2339. + tc_finish_urb(uld->hcd, uld->urb, uld->status);
  2340. + } else {
  2341. + late_warn("Ignoring later finish of URB:0x%x[%d]"
  2342. + ", urb_num doesn't match current URB:0x%x[%d]",
  2343. + (unsigned int)(uld->urb), uld->urb_num,
  2344. + (unsigned int)(uld->urb), urb_priv->urb_num);
  2345. + }
  2346. + }
  2347. + local_irq_restore(flags);
  2348. + kmem_cache_free(later_data_cache, uld);
  2349. +}
  2350. +
  2351. +static void tc_finish_urb_later(struct usb_hcd *hcd, struct urb *urb,
  2352. + int status) {
  2353. + struct crisv10_urb_priv *urb_priv = urb->hcpriv;
  2354. + struct urb_later_data* uld;
  2355. +
  2356. + ASSERT(urb_priv);
  2357. +
  2358. + if(urb_priv->later_data != NULL) {
  2359. + /* Later-finish allready scheduled for this URB, just update status to
  2360. + return when finishing later */
  2361. + errno_dbg("Later-finish schedule change URB status:%d with new"
  2362. + " status:%d\n", urb_priv->later_data->status, status);
  2363. +
  2364. + urb_priv->later_data->status = status;
  2365. + return;
  2366. + }
  2367. +
  2368. + uld = kmem_cache_alloc(later_data_cache, GFP_ATOMIC);
  2369. + ASSERT(uld);
  2370. +
  2371. + uld->hcd = hcd;
  2372. + uld->urb = urb;
  2373. + uld->urb_num = urb_priv->urb_num;
  2374. + uld->status = status;
  2375. +
  2376. + //INIT_WORK(&uld->ws, tc_finish_urb_later_proc, uld);
  2377. + INIT_DELAYED_WORK(&uld->ws, tc_finish_urb_later_proc);
  2378. + urb_priv->later_data = uld;
  2379. +
  2380. + /* Schedule the finishing of the URB to happen later */
  2381. + schedule_delayed_work(&uld->ws, LATER_TIMER_DELAY);
  2382. +}
  2383. +
  2384. + /* hinko ignore usb_pipeisoc */
  2385. +#if 0
  2386. +static void tc_finish_isoc_urb(struct usb_hcd *hcd, struct urb *urb,
  2387. + int status);
  2388. +#endif
  2389. +
  2390. +static void tc_finish_urb(struct usb_hcd *hcd, struct urb *urb, int status) {
  2391. + struct crisv10_hcd* crisv10_hcd = hcd_to_crisv10_hcd(hcd);
  2392. + struct crisv10_urb_priv *urb_priv = urb->hcpriv;
  2393. + int epid;
  2394. + char toggle;
  2395. + int urb_num;
  2396. +
  2397. + DBFENTER;
  2398. + ASSERT(urb_priv != NULL);
  2399. + epid = urb_priv->epid;
  2400. + urb_num = urb_priv->urb_num;
  2401. +
  2402. + if(urb != activeUrbList[epid]) {
  2403. + if(urb_list_entry(urb, epid)) {
  2404. + /* Remove this URB from the list. Only happens when URB are finished
  2405. + before having been processed (dequeing) */
  2406. + urb_list_del(urb, epid);
  2407. + } else {
  2408. + tc_warn("Finishing of URB:0x%x[%d] neither active or in queue for"
  2409. + " epid:%d\n", (unsigned int)urb, urb_num, epid);
  2410. + }
  2411. + }
  2412. +
  2413. + /* Cancel any pending later-finish of this URB */
  2414. + if(urb_priv->later_data) {
  2415. + urb_priv->later_data->urb = NULL;
  2416. + }
  2417. +
  2418. + /* For an IN pipe, we always set the actual length, regardless of whether
  2419. + there was an error or not (which means the device driver can use the data
  2420. + if it wants to). */
  2421. + if(usb_pipein(urb->pipe)) {
  2422. + urb->actual_length = urb_priv->rx_offset;
  2423. + } else {
  2424. + /* Set actual_length for OUT urbs also; the USB mass storage driver seems
  2425. + to want that. */
  2426. + if (status == 0 && urb->status == -EINPROGRESS) {
  2427. + urb->actual_length = urb->transfer_buffer_length;
  2428. + } else {
  2429. + /* We wouldn't know of any partial writes if there was an error. */
  2430. + urb->actual_length = 0;
  2431. + }
  2432. + }
  2433. +
  2434. +
  2435. + /* URB status mangling */
  2436. + if(urb->status == -EINPROGRESS) {
  2437. + /* The USB core hasn't changed the status, let's set our finish status */
  2438. + urb->status = status;
  2439. +
  2440. + if ((status == 0) && (urb->transfer_flags & URB_SHORT_NOT_OK) &&
  2441. + usb_pipein(urb->pipe) &&
  2442. + (urb->actual_length != urb->transfer_buffer_length)) {
  2443. + /* URB_SHORT_NOT_OK means that short reads (shorter than the endpoint's
  2444. + max length) is to be treated as an error. */
  2445. + errno_dbg("Finishing URB:0x%x[%d] with SHORT_NOT_OK flag and short"
  2446. + " data:%d\n", (unsigned int)urb, urb_num,
  2447. + urb->actual_length);
  2448. + urb->status = -EREMOTEIO;
  2449. + }
  2450. +
  2451. + if(urb_priv->urb_state == UNLINK) {
  2452. + /* URB has been requested to be unlinked asynchronously */
  2453. + urb->status = -ECONNRESET;
  2454. + errno_dbg("Fixing unlink status of URB:0x%x[%d] to:%d\n",
  2455. + (unsigned int)urb, urb_num, urb->status);
  2456. + }
  2457. + } else {
  2458. + /* The USB Core wants to signal some error via the URB, pass it through */
  2459. + }
  2460. +
  2461. + /* hinko ignore usb_pipeisoc */
  2462. +#if 0
  2463. + /* use completely different finish function for Isoc URBs */
  2464. + if(usb_pipeisoc(urb->pipe)) {
  2465. + tc_finish_isoc_urb(hcd, urb, status);
  2466. + return;
  2467. + }
  2468. +#endif
  2469. +
  2470. + /* Do special unlinking of EPs for Intr traffic */
  2471. + if(usb_pipeint(urb->pipe)) {
  2472. + tc_dma_unlink_intr_urb(urb);
  2473. + }
  2474. +
  2475. + /* hinko ignore usb_pipeisoc */
  2476. +#if 0
  2477. + /* Release allocated bandwidth for periodic transfers */
  2478. + if(usb_pipeint(urb->pipe) || usb_pipeisoc(urb->pipe))
  2479. + usb_release_bandwidth(urb->dev, urb, 0);
  2480. +#endif
  2481. +
  2482. + /* This URB is active on EP */
  2483. + if(urb == activeUrbList[epid]) {
  2484. + /* We need to fiddle with the toggle bits because the hardware doesn't do
  2485. + it for us. */
  2486. + toggle = etrax_epid_get_toggle(epid, usb_pipeout(urb->pipe));
  2487. + usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
  2488. + usb_pipeout(urb->pipe), toggle);
  2489. +
  2490. + /* Checks for Ctrl and Bulk EPs */
  2491. + switch(usb_pipetype(urb->pipe)) {
  2492. + case PIPE_BULK:
  2493. + /* Check so Bulk EP realy is disabled before finishing active URB */
  2494. + ASSERT((TxBulkEPList[epid].command & IO_MASK(USB_EP_command, enable)) ==
  2495. + IO_STATE(USB_EP_command, enable, no));
  2496. + /* Disable sub-pointer for EP to avoid next tx_interrupt() to
  2497. + process Bulk EP. */
  2498. + TxBulkEPList[epid].sub = 0;
  2499. + /* No need to wait for the DMA before changing the next pointer.
  2500. + The modulo NBR_OF_EPIDS isn't actually necessary, since we will never use
  2501. + the last one (INVALID_EPID) for actual traffic. */
  2502. + TxBulkEPList[epid].next =
  2503. + virt_to_phys(&TxBulkEPList[(epid + 1) % NBR_OF_EPIDS]);
  2504. + break;
  2505. + case PIPE_CONTROL:
  2506. + /* Check so Ctrl EP realy is disabled before finishing active URB */
  2507. + ASSERT((TxCtrlEPList[epid].command & IO_MASK(USB_EP_command, enable)) ==
  2508. + IO_STATE(USB_EP_command, enable, no));
  2509. + /* Disable sub-pointer for EP to avoid next tx_interrupt() to
  2510. + process Ctrl EP. */
  2511. + TxCtrlEPList[epid].sub = 0;
  2512. + break;
  2513. + }
  2514. + }
  2515. +
  2516. + /* Free HC-private URB data*/
  2517. + urb_priv_free(hcd, urb);
  2518. +
  2519. + if(urb->status) {
  2520. + errno_dbg("finish_urb (URB:0x%x[%d] %s %s) (data:%d) status:%d\n",
  2521. + (unsigned int)urb, urb_num, str_dir(urb->pipe),
  2522. + str_type(urb->pipe), urb->actual_length, urb->status);
  2523. + } else {
  2524. + tc_dbg("finish_urb (URB:0x%x[%d] %s %s) (data:%d) status:%d\n",
  2525. + (unsigned int)urb, urb_num, str_dir(urb->pipe),
  2526. + str_type(urb->pipe), urb->actual_length, urb->status);
  2527. + }
  2528. +
  2529. + /* If we just finished an active URB, clear active pointer. */
  2530. + if (urb == activeUrbList[epid]) {
  2531. + /* Make URB not active on EP anymore */
  2532. + activeUrbList[epid] = NULL;
  2533. +
  2534. + if(urb->status == 0) {
  2535. + /* URB finished sucessfully, process queue to see if there are any more
  2536. + URBs waiting before we call completion function.*/
  2537. + if(crisv10_hcd->running) {
  2538. + /* Only process queue if USB controller is running */
  2539. + tc_dma_process_queue(epid);
  2540. + } else {
  2541. + tc_warn("No processing of queue for epid:%d, USB Controller not"
  2542. + " running\n", epid);
  2543. + }
  2544. + }
  2545. + }
  2546. +
  2547. + /* Hand the URB from HCD to its USB device driver, using its completion
  2548. + functions */
  2549. +// usb_hcd_giveback_urb (hcd, urb);
  2550. + /**
  2551. + * usb_hcd_unlink_urb_from_ep - remove an URB from its endpoint queue
  2552. + * @hcd: host controller to which @urb was submitted
  2553. + * @urb: URB being unlinked
  2554. + *
  2555. + * Host controller drivers should call this routine before calling
  2556. + * usb_hcd_giveback_urb(). The HCD's private spinlock must be held and
  2557. + * interrupts must be disabled. The actions carried out here are required
  2558. + * for URB completion.
  2559. + */
  2560. +
  2561. + /*hinko link/unlink urb -> ep */
  2562. + //spin_lock(&crisv10_hcd->lock);
  2563. + unsigned long flags;
  2564. + spin_lock_irqsave(&crisv10_hcd->lock, flags);
  2565. + usb_hcd_unlink_urb_from_ep(hcd, urb);
  2566. + usb_hcd_giveback_urb(hcd, urb, status);
  2567. + //spin_unlock(&crisv10_hcd->lock);
  2568. + spin_unlock_irqrestore(&crisv10_hcd->lock, flags);
  2569. +
  2570. + /* Check the queue once more if the URB returned with error, because we
  2571. + didn't do it before the completion function because the specification
  2572. + states that the queue should not restart until all it's unlinked
  2573. + URBs have been fully retired, with the completion functions run */
  2574. + if(crisv10_hcd->running) {
  2575. + /* Only process queue if USB controller is running */
  2576. + tc_dma_process_queue(epid);
  2577. + } else {
  2578. + tc_warn("No processing of queue for epid:%d, USB Controller not running\n",
  2579. + epid);
  2580. + }
  2581. +
  2582. + DBFEXIT;
  2583. +}
  2584. +
  2585. + /* hinko ignore usb_pipeisoc */
  2586. +#if 0
  2587. +static void tc_finish_isoc_urb(struct usb_hcd *hcd, struct urb *urb,
  2588. + int status) {
  2589. + struct crisv10_urb_priv *urb_priv = urb->hcpriv;
  2590. + int epid, i;
  2591. + volatile int timeout = 10000;
  2592. +
  2593. + ASSERT(urb_priv);
  2594. + epid = urb_priv->epid;
  2595. +
  2596. + ASSERT(usb_pipeisoc(urb->pipe));
  2597. +
  2598. + /* Set that all isoc packets have status and length set before
  2599. + completing the urb. */
  2600. + for (i = urb_priv->isoc_packet_counter; i < urb->number_of_packets; i++){
  2601. + urb->iso_frame_desc[i].actual_length = 0;
  2602. + urb->iso_frame_desc[i].status = -EPROTO;
  2603. + }
  2604. +
  2605. + /* Check if the URB is currently active (done or error) */
  2606. + if(urb == activeUrbList[epid]) {
  2607. + /* Check if there are another In Isoc URB queued for this epid */
  2608. + if (!list_empty(&urb_list[epid])&& !epid_state[epid].disabled) {
  2609. + /* Move it from queue to active and mark it started so Isoc transfers
  2610. + won't be interrupted.
  2611. + All Isoc URBs data transfers are already added to DMA lists so we
  2612. + don't have to insert anything in DMA lists here. */
  2613. + activeUrbList[epid] = urb_list_first(epid);
  2614. + ((struct crisv10_urb_priv *)(activeUrbList[epid]->hcpriv))->urb_state =
  2615. + STARTED;
  2616. + urb_list_del(activeUrbList[epid], epid);
  2617. +
  2618. + if(urb->status) {
  2619. + errno_dbg("finish_isoc_urb (URB:0x%x[%d] %s %s) (%d of %d packets)"
  2620. + " status:%d, new waiting URB:0x%x[%d]\n",
  2621. + (unsigned int)urb, urb_priv->urb_num, str_dir(urb->pipe),
  2622. + str_type(urb->pipe), urb_priv->isoc_packet_counter,
  2623. + urb->number_of_packets, urb->status,
  2624. + (unsigned int)activeUrbList[epid],
  2625. + ((struct crisv10_urb_priv *)(activeUrbList[epid]->hcpriv))->urb_num);
  2626. + }
  2627. +
  2628. + } else { /* No other URB queued for this epid */
  2629. + if(urb->status) {
  2630. + errno_dbg("finish_isoc_urb (URB:0x%x[%d] %s %s) (%d of %d packets)"
  2631. + " status:%d, no new URB waiting\n",
  2632. + (unsigned int)urb, urb_priv->urb_num, str_dir(urb->pipe),
  2633. + str_type(urb->pipe), urb_priv->isoc_packet_counter,
  2634. + urb->number_of_packets, urb->status);
  2635. + }
  2636. +
  2637. + /* Check if EP is still enabled, then shut it down. */
  2638. + if (TxIsocEPList[epid].command & IO_MASK(USB_EP_command, enable)) {
  2639. + isoc_dbg("Isoc EP enabled for epid:%d, disabling it\n", epid);
  2640. +
  2641. + /* Should only occur for In Isoc EPs where SB isn't consumed. */
  2642. + ASSERT(usb_pipein(urb->pipe));
  2643. +
  2644. + /* Disable it and wait for it to stop */
  2645. + TxIsocEPList[epid].command &= ~IO_MASK(USB_EP_command, enable);
  2646. +
  2647. + /* Ah, the luxury of busy-wait. */
  2648. + while((*R_DMA_CH8_SUB3_EP == virt_to_phys(&TxIsocEPList[epid])) &&
  2649. + (timeout-- > 0));
  2650. + if(timeout == 0) {
  2651. + warn("Timeout while waiting for DMA-TX-Isoc to leave EP for epid:%d\n", epid);
  2652. + }
  2653. + }
  2654. +
  2655. + /* Unlink SB to say that epid is finished. */
  2656. + TxIsocEPList[epid].sub = 0;
  2657. + TxIsocEPList[epid].hw_len = 0;
  2658. +
  2659. + /* No URB active for EP anymore */
  2660. + activeUrbList[epid] = NULL;
  2661. + }
  2662. + } else { /* Finishing of not active URB (queued up with SBs thought) */
  2663. + isoc_warn("finish_isoc_urb (URB:0x%x %s) (%d of %d packets) status:%d,"
  2664. + " SB queued but not active\n",
  2665. + (unsigned int)urb, str_dir(urb->pipe),
  2666. + urb_priv->isoc_packet_counter, urb->number_of_packets,
  2667. + urb->status);
  2668. + if(usb_pipeout(urb->pipe)) {
  2669. + /* Finishing of not yet active Out Isoc URB needs unlinking of SBs. */
  2670. + struct USB_SB_Desc *iter_sb, *prev_sb, *next_sb;
  2671. +
  2672. + iter_sb = TxIsocEPList[epid].sub ?
  2673. + phys_to_virt(TxIsocEPList[epid].sub) : 0;
  2674. + prev_sb = 0;
  2675. +
  2676. + /* SB that is linked before this URBs first SB */
  2677. + while (iter_sb && (iter_sb != urb_priv->first_sb)) {
  2678. + prev_sb = iter_sb;
  2679. + iter_sb = iter_sb->next ? phys_to_virt(iter_sb->next) : 0;
  2680. + }
  2681. +
  2682. + if (iter_sb == 0) {
  2683. + /* Unlink of the URB currently being transmitted. */
  2684. + prev_sb = 0;
  2685. + iter_sb = TxIsocEPList[epid].sub ? phys_to_virt(TxIsocEPList[epid].sub) : 0;
  2686. + }
  2687. +
  2688. + while (iter_sb && (iter_sb != urb_priv->last_sb)) {
  2689. + iter_sb = iter_sb->next ? phys_to_virt(iter_sb->next) : 0;
  2690. + }
  2691. +
  2692. + if (iter_sb) {
  2693. + next_sb = iter_sb->next ? phys_to_virt(iter_sb->next) : 0;
  2694. + } else {
  2695. + /* This should only happen if the DMA has completed
  2696. + processing the SB list for this EP while interrupts
  2697. + are disabled. */
  2698. + isoc_dbg("Isoc urb not found, already sent?\n");
  2699. + next_sb = 0;
  2700. + }
  2701. + if (prev_sb) {
  2702. + prev_sb->next = next_sb ? virt_to_phys(next_sb) : 0;
  2703. + } else {
  2704. + TxIsocEPList[epid].sub = next_sb ? virt_to_phys(next_sb) : 0;
  2705. + }
  2706. + }
  2707. + }
  2708. +
  2709. + /* Free HC-private URB data*/
  2710. + urb_priv_free(hcd, urb);
  2711. +
  2712. + usb_release_bandwidth(urb->dev, urb, 0);
  2713. +
  2714. + /* Hand the URB from HCD to its USB device driver, using its completion
  2715. + functions */
  2716. + usb_hcd_giveback_urb (hcd, urb);
  2717. +}
  2718. +#endif
  2719. +
  2720. +static __u32 urb_num = 0;
  2721. +
  2722. +/* allocate and initialize URB private data */
  2723. +static int urb_priv_create(struct usb_hcd *hcd, struct urb *urb, int epid,
  2724. + int mem_flags) {
  2725. + struct crisv10_urb_priv *urb_priv;
  2726. +
  2727. + urb_priv = kmalloc(sizeof *urb_priv, mem_flags);
  2728. + if (!urb_priv)
  2729. + return -ENOMEM;
  2730. + memset(urb_priv, 0, sizeof *urb_priv);
  2731. +
  2732. + urb_priv->epid = epid;
  2733. + urb_priv->urb_state = NOT_STARTED;
  2734. +
  2735. + urb->hcpriv = urb_priv;
  2736. + /* Assign URB a sequence number, and increment counter */
  2737. + urb_priv->urb_num = urb_num;
  2738. + urb_num++;
  2739. + return 0;
  2740. +}
  2741. +
  2742. +/* free URB private data */
  2743. +static void urb_priv_free(struct usb_hcd *hcd, struct urb *urb) {
  2744. + int i;
  2745. + struct crisv10_urb_priv *urb_priv = urb->hcpriv;
  2746. + ASSERT(urb_priv != 0);
  2747. +
  2748. + /* Check it has any SBs linked that needs to be freed*/
  2749. + if(urb_priv->first_sb != NULL) {
  2750. + struct USB_SB_Desc *next_sb, *first_sb, *last_sb;
  2751. + int i = 0;
  2752. + first_sb = urb_priv->first_sb;
  2753. + last_sb = urb_priv->last_sb;
  2754. + ASSERT(last_sb);
  2755. + while(first_sb != last_sb) {
  2756. + next_sb = (struct USB_SB_Desc *)phys_to_virt(first_sb->next);
  2757. + kmem_cache_free(usb_desc_cache, first_sb);
  2758. + first_sb = next_sb;
  2759. + i++;
  2760. + }
  2761. + kmem_cache_free(usb_desc_cache, last_sb);
  2762. + i++;
  2763. + }
  2764. +
  2765. + /* Check if it has any EPs in its Intr pool that also needs to be freed */
  2766. + if(urb_priv->intr_ep_pool_length > 0) {
  2767. + for(i = 0; i < urb_priv->intr_ep_pool_length; i++) {
  2768. + kfree(urb_priv->intr_ep_pool[i]);
  2769. + }
  2770. + /*
  2771. + tc_dbg("Freed %d EPs from URB:0x%x EP pool\n",
  2772. + urb_priv->intr_ep_pool_length, (unsigned int)urb);
  2773. + */
  2774. + }
  2775. +
  2776. + kfree(urb_priv);
  2777. + urb->hcpriv = NULL;
  2778. +}
  2779. +
  2780. +static int ep_priv_create(struct usb_host_endpoint *ep, int mem_flags) {
  2781. + struct crisv10_ep_priv *ep_priv;
  2782. +
  2783. + ep_priv = kmalloc(sizeof *ep_priv, mem_flags);
  2784. + if (!ep_priv)
  2785. + return -ENOMEM;
  2786. + memset(ep_priv, 0, sizeof *ep_priv);
  2787. +
  2788. + ep->hcpriv = ep_priv;
  2789. + return 0;
  2790. +}
  2791. +
  2792. +static void ep_priv_free(struct usb_host_endpoint *ep) {
  2793. + struct crisv10_ep_priv *ep_priv = ep->hcpriv;
  2794. + ASSERT(ep_priv);
  2795. + kfree(ep_priv);
  2796. + ep->hcpriv = NULL;
  2797. +}
  2798. +
  2799. +/* EPID handling functions, managing EP-list in Etrax through wrappers */
  2800. +/* ------------------------------------------------------------------- */
  2801. +
  2802. +/* Sets up a new EPID for an endpoint or returns existing if found */
  2803. +//static int tc_setup_epid(struct usb_host_endpoint *ep, struct urb *urb,
  2804. +// int mem_flags) {
  2805. +static int tc_setup_epid(struct urb *urb, int mem_flags)
  2806. +{
  2807. + int epid;
  2808. + char devnum, endpoint, out_traffic, slow;
  2809. + int maxlen;
  2810. + __u32 epid_data;
  2811. + struct usb_host_endpoint *ep = urb->ep;
  2812. + struct crisv10_ep_priv *ep_priv = ep->hcpriv;
  2813. +
  2814. + DBFENTER;
  2815. +
  2816. + /* Check if a valid epid already is setup for this endpoint */
  2817. + if(ep_priv != NULL) {
  2818. + return ep_priv->epid;
  2819. + }
  2820. +
  2821. + /* We must find and initiate a new epid for this urb. */
  2822. + epid = tc_allocate_epid();
  2823. +
  2824. + if (epid == -1) {
  2825. + /* Failed to allocate a new epid. */
  2826. + DBFEXIT;
  2827. + return epid;
  2828. + }
  2829. +
  2830. + /* We now have a new epid to use. Claim it. */
  2831. + epid_state[epid].inuse = 1;
  2832. +
  2833. + /* Init private data for new endpoint */
  2834. + if(ep_priv_create(ep, mem_flags) != 0) {
  2835. + return -ENOMEM;
  2836. + }
  2837. + ep_priv = ep->hcpriv;
  2838. + ep_priv->epid = epid;
  2839. +
  2840. + devnum = usb_pipedevice(urb->pipe);
  2841. + endpoint = usb_pipeendpoint(urb->pipe);
  2842. + slow = (urb->dev->speed == USB_SPEED_LOW);
  2843. + maxlen = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe));
  2844. +
  2845. + if (usb_pipetype(urb->pipe) == PIPE_CONTROL) {
  2846. + /* We want both IN and OUT control traffic to be put on the same
  2847. + EP/SB list. */
  2848. + out_traffic = 1;
  2849. + } else {
  2850. + out_traffic = usb_pipeout(urb->pipe);
  2851. + }
  2852. +
  2853. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  2854. + epid_data = IO_STATE(R_USB_EPT_DATA_ISO, valid, yes) |
  2855. + /* FIXME: Change any to the actual port? */
  2856. + IO_STATE(R_USB_EPT_DATA_ISO, port, any) |
  2857. + IO_FIELD(R_USB_EPT_DATA_ISO, max_len, maxlen) |
  2858. + IO_FIELD(R_USB_EPT_DATA_ISO, ep, endpoint) |
  2859. + IO_FIELD(R_USB_EPT_DATA_ISO, dev, devnum);
  2860. + etrax_epid_iso_set(epid, epid_data);
  2861. + } else {
  2862. + epid_data = IO_STATE(R_USB_EPT_DATA, valid, yes) |
  2863. + IO_FIELD(R_USB_EPT_DATA, low_speed, slow) |
  2864. + /* FIXME: Change any to the actual port? */
  2865. + IO_STATE(R_USB_EPT_DATA, port, any) |
  2866. + IO_FIELD(R_USB_EPT_DATA, max_len, maxlen) |
  2867. + IO_FIELD(R_USB_EPT_DATA, ep, endpoint) |
  2868. + IO_FIELD(R_USB_EPT_DATA, dev, devnum);
  2869. + etrax_epid_set(epid, epid_data);
  2870. + }
  2871. +
  2872. + epid_state[epid].out_traffic = out_traffic;
  2873. + epid_state[epid].type = usb_pipetype(urb->pipe);
  2874. +
  2875. + tc_warn("Setting up ep:0x%x epid:%d (addr:%d endp:%d max_len:%d %s %s %s)\n",
  2876. + (unsigned int)ep, epid, devnum, endpoint, maxlen,
  2877. + str_type(urb->pipe), out_traffic ? "out" : "in",
  2878. + slow ? "low" : "full");
  2879. +
  2880. + /* Enable Isoc eof interrupt if we set up the first Isoc epid */
  2881. + if(usb_pipeisoc(urb->pipe)) {
  2882. + isoc_epid_counter++;
  2883. + if(isoc_epid_counter == 1) {
  2884. + isoc_warn("Enabled Isoc eof interrupt\n");
  2885. + *R_USB_IRQ_MASK_SET |= IO_STATE(R_USB_IRQ_MASK_SET, iso_eof, set);
  2886. + }
  2887. + }
  2888. +
  2889. + DBFEXIT;
  2890. + return epid;
  2891. +}
  2892. +
  2893. +static void tc_free_epid(struct usb_host_endpoint *ep) {
  2894. + unsigned long flags;
  2895. + struct crisv10_ep_priv *ep_priv = ep->hcpriv;
  2896. + int epid;
  2897. + volatile int timeout = 10000;
  2898. +
  2899. + DBFENTER;
  2900. +
  2901. + if (ep_priv == NULL) {
  2902. + tc_warn("Trying to free unused epid on ep:0x%x\n", (unsigned int)ep);
  2903. + DBFEXIT;
  2904. + return;
  2905. + }
  2906. +
  2907. + epid = ep_priv->epid;
  2908. +
  2909. + /* Disable Isoc eof interrupt if we free the last Isoc epid */
  2910. + if(epid_isoc(epid)) {
  2911. + ASSERT(isoc_epid_counter > 0);
  2912. + isoc_epid_counter--;
  2913. + if(isoc_epid_counter == 0) {
  2914. + *R_USB_IRQ_MASK_SET &= ~IO_STATE(R_USB_IRQ_MASK_SET, iso_eof, set);
  2915. + isoc_warn("Disabled Isoc eof interrupt\n");
  2916. + }
  2917. + }
  2918. +
  2919. + /* Take lock manualy instead of in epid_x_x wrappers,
  2920. + because we need to be polling here */
  2921. + spin_lock_irqsave(&etrax_epid_lock, flags);
  2922. +
  2923. + *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid);
  2924. + nop();
  2925. + while((*R_USB_EPT_DATA & IO_MASK(R_USB_EPT_DATA, hold)) &&
  2926. + (timeout-- > 0));
  2927. + if(timeout == 0) {
  2928. + warn("Timeout while waiting for epid:%d to drop hold\n", epid);
  2929. + }
  2930. + /* This will, among other things, set the valid field to 0. */
  2931. + *R_USB_EPT_DATA = 0;
  2932. + spin_unlock_irqrestore(&etrax_epid_lock, flags);
  2933. +
  2934. + /* Free resource in software state info list */
  2935. + epid_state[epid].inuse = 0;
  2936. +
  2937. + /* Free private endpoint data */
  2938. + ep_priv_free(ep);
  2939. +
  2940. + DBFEXIT;
  2941. +}
  2942. +
  2943. +static int tc_allocate_epid(void) {
  2944. + int i;
  2945. + DBFENTER;
  2946. + for (i = 0; i < NBR_OF_EPIDS; i++) {
  2947. + if (!epid_inuse(i)) {
  2948. + DBFEXIT;
  2949. + return i;
  2950. + }
  2951. + }
  2952. +
  2953. + tc_warn("Found no free epids\n");
  2954. + DBFEXIT;
  2955. + return -1;
  2956. +}
  2957. +
  2958. +
  2959. +/* Wrappers around the list functions (include/linux/list.h). */
  2960. +/* ---------------------------------------------------------- */
  2961. +static inline int __urb_list_empty(int epid) {
  2962. + int retval;
  2963. + retval = list_empty(&urb_list[epid]);
  2964. + return retval;
  2965. +}
  2966. +
  2967. +/* Returns first urb for this epid, or NULL if list is empty. */
  2968. +static inline struct urb *urb_list_first(int epid) {
  2969. + unsigned long flags;
  2970. + struct urb *first_urb = 0;
  2971. + spin_lock_irqsave(&urb_list_lock, flags);
  2972. + if (!__urb_list_empty(epid)) {
  2973. + /* Get the first urb (i.e. head->next). */
  2974. + urb_entry_t *urb_entry = list_entry((&urb_list[epid])->next, urb_entry_t, list);
  2975. + first_urb = urb_entry->urb;
  2976. + }
  2977. + spin_unlock_irqrestore(&urb_list_lock, flags);
  2978. + return first_urb;
  2979. +}
  2980. +
  2981. +/* Adds an urb_entry last in the list for this epid. */
  2982. +static inline void urb_list_add(struct urb *urb, int epid, int mem_flags) {
  2983. + unsigned long flags;
  2984. + urb_entry_t *urb_entry = (urb_entry_t *)kmalloc(sizeof(urb_entry_t), mem_flags);
  2985. + ASSERT(urb_entry);
  2986. +
  2987. + urb_entry->urb = urb;
  2988. + spin_lock_irqsave(&urb_list_lock, flags);
  2989. + list_add_tail(&urb_entry->list, &urb_list[epid]);
  2990. + spin_unlock_irqrestore(&urb_list_lock, flags);
  2991. +}
  2992. +
  2993. +/* Search through the list for an element that contains this urb. (The list
  2994. + is expected to be short and the one we are about to delete will often be
  2995. + the first in the list.)
  2996. + Should be protected by spin_locks in calling function */
  2997. +static inline urb_entry_t *__urb_list_entry(struct urb *urb, int epid) {
  2998. + struct list_head *entry;
  2999. + struct list_head *tmp;
  3000. + urb_entry_t *urb_entry;
  3001. +
  3002. + list_for_each_safe(entry, tmp, &urb_list[epid]) {
  3003. + urb_entry = list_entry(entry, urb_entry_t, list);
  3004. + ASSERT(urb_entry);
  3005. + ASSERT(urb_entry->urb);
  3006. +
  3007. + if (urb_entry->urb == urb) {
  3008. + return urb_entry;
  3009. + }
  3010. + }
  3011. + return 0;
  3012. +}
  3013. +
  3014. +/* Same function as above but for global use. Protects list by spinlock */
  3015. +static inline urb_entry_t *urb_list_entry(struct urb *urb, int epid) {
  3016. + unsigned long flags;
  3017. + urb_entry_t *urb_entry;
  3018. + spin_lock_irqsave(&urb_list_lock, flags);
  3019. + urb_entry = __urb_list_entry(urb, epid);
  3020. + spin_unlock_irqrestore(&urb_list_lock, flags);
  3021. + return (urb_entry);
  3022. +}
  3023. +
  3024. +/* Delete an urb from the list. */
  3025. +static inline void urb_list_del(struct urb *urb, int epid) {
  3026. + unsigned long flags;
  3027. + urb_entry_t *urb_entry;
  3028. +
  3029. + /* Delete entry and free. */
  3030. + spin_lock_irqsave(&urb_list_lock, flags);
  3031. + urb_entry = __urb_list_entry(urb, epid);
  3032. + ASSERT(urb_entry);
  3033. +
  3034. + list_del(&urb_entry->list);
  3035. + spin_unlock_irqrestore(&urb_list_lock, flags);
  3036. + kfree(urb_entry);
  3037. +}
  3038. +
  3039. +/* Move an urb to the end of the list. */
  3040. +static inline void urb_list_move_last(struct urb *urb, int epid) {
  3041. + unsigned long flags;
  3042. + urb_entry_t *urb_entry;
  3043. +
  3044. + spin_lock_irqsave(&urb_list_lock, flags);
  3045. + urb_entry = __urb_list_entry(urb, epid);
  3046. + ASSERT(urb_entry);
  3047. +
  3048. + list_del(&urb_entry->list);
  3049. + list_add_tail(&urb_entry->list, &urb_list[epid]);
  3050. + spin_unlock_irqrestore(&urb_list_lock, flags);
  3051. +}
  3052. +
  3053. +/* Get the next urb in the list. */
  3054. +static inline struct urb *urb_list_next(struct urb *urb, int epid) {
  3055. + unsigned long flags;
  3056. + urb_entry_t *urb_entry;
  3057. +
  3058. + spin_lock_irqsave(&urb_list_lock, flags);
  3059. + urb_entry = __urb_list_entry(urb, epid);
  3060. + ASSERT(urb_entry);
  3061. +
  3062. + if (urb_entry->list.next != &urb_list[epid]) {
  3063. + struct list_head *elem = urb_entry->list.next;
  3064. + urb_entry = list_entry(elem, urb_entry_t, list);
  3065. + spin_unlock_irqrestore(&urb_list_lock, flags);
  3066. + return urb_entry->urb;
  3067. + } else {
  3068. + spin_unlock_irqrestore(&urb_list_lock, flags);
  3069. + return NULL;
  3070. + }
  3071. +}
  3072. +
  3073. +struct USB_EP_Desc* create_ep(int epid, struct USB_SB_Desc* sb_desc,
  3074. + int mem_flags) {
  3075. + struct USB_EP_Desc *ep_desc;
  3076. + ep_desc = (struct USB_EP_Desc *) kmem_cache_alloc(usb_desc_cache, mem_flags);
  3077. + if(ep_desc == NULL)
  3078. + return NULL;
  3079. + memset(ep_desc, 0, sizeof(struct USB_EP_Desc));
  3080. +
  3081. + ep_desc->hw_len = 0;
  3082. + ep_desc->command = (IO_FIELD(USB_EP_command, epid, epid) |
  3083. + IO_STATE(USB_EP_command, enable, yes));
  3084. + if(sb_desc == NULL) {
  3085. + ep_desc->sub = 0;
  3086. + } else {
  3087. + ep_desc->sub = virt_to_phys(sb_desc);
  3088. + }
  3089. + return ep_desc;
  3090. +}
  3091. +
  3092. +#define TT_ZOUT 0
  3093. +#define TT_IN 1
  3094. +#define TT_OUT 2
  3095. +#define TT_SETUP 3
  3096. +
  3097. +#define CMD_EOL IO_STATE(USB_SB_command, eol, yes)
  3098. +#define CMD_INTR IO_STATE(USB_SB_command, intr, yes)
  3099. +#define CMD_FULL IO_STATE(USB_SB_command, full, yes)
  3100. +
  3101. +/* Allocation and setup of a generic SB. Used to create SETUP, OUT and ZOUT
  3102. + SBs. Also used by create_sb_in() to avoid same allocation procedure at two
  3103. + places */
  3104. +struct USB_SB_Desc* create_sb(struct USB_SB_Desc* sb_prev, int tt, void* data,
  3105. + int datalen, int mem_flags) {
  3106. + struct USB_SB_Desc *sb_desc;
  3107. + sb_desc = (struct USB_SB_Desc*)kmem_cache_alloc(usb_desc_cache, mem_flags);
  3108. + if(sb_desc == NULL)
  3109. + return NULL;
  3110. + memset(sb_desc, 0, sizeof(struct USB_SB_Desc));
  3111. +
  3112. + sb_desc->command = IO_FIELD(USB_SB_command, tt, tt) |
  3113. + IO_STATE(USB_SB_command, eot, yes);
  3114. +
  3115. + sb_desc->sw_len = datalen;
  3116. + if(data != NULL) {
  3117. + sb_desc->buf = virt_to_phys(data);
  3118. + } else {
  3119. + sb_desc->buf = 0;
  3120. + }
  3121. + if(sb_prev != NULL) {
  3122. + sb_prev->next = virt_to_phys(sb_desc);
  3123. + }
  3124. + return sb_desc;
  3125. +}
  3126. +
  3127. +/* Creates a copy of an existing SB by allocation space for it and copy
  3128. + settings */
  3129. +struct USB_SB_Desc* create_sb_copy(struct USB_SB_Desc* sb_orig, int mem_flags) {
  3130. + struct USB_SB_Desc *sb_desc;
  3131. + sb_desc = (struct USB_SB_Desc*)kmem_cache_alloc(usb_desc_cache, mem_flags);
  3132. + if(sb_desc == NULL)
  3133. + return NULL;
  3134. +
  3135. + memcpy(sb_desc, sb_orig, sizeof(struct USB_SB_Desc));
  3136. + return sb_desc;
  3137. +}
  3138. +
  3139. +/* A specific create_sb function for creation of in SBs. This is due to
  3140. + that datalen in In SBs shows how many packets we are expecting. It also
  3141. + sets up the rem field to show if how many bytes we expect in last packet
  3142. + if it's not a full one */
  3143. +struct USB_SB_Desc* create_sb_in(struct USB_SB_Desc* sb_prev, int datalen,
  3144. + int maxlen, int mem_flags) {
  3145. + struct USB_SB_Desc *sb_desc;
  3146. + sb_desc = create_sb(sb_prev, TT_IN, NULL,
  3147. + datalen ? (datalen - 1) / maxlen + 1 : 0, mem_flags);
  3148. + if(sb_desc == NULL)
  3149. + return NULL;
  3150. + sb_desc->command |= IO_FIELD(USB_SB_command, rem, datalen % maxlen);
  3151. + return sb_desc;
  3152. +}
  3153. +
  3154. +void set_sb_cmds(struct USB_SB_Desc *sb_desc, __u16 flags) {
  3155. + sb_desc->command |= flags;
  3156. +}
  3157. +
  3158. +int create_sb_for_urb(struct urb *urb, int mem_flags) {
  3159. + int is_out = !usb_pipein(urb->pipe);
  3160. + int type = usb_pipetype(urb->pipe);
  3161. + int maxlen = usb_maxpacket(urb->dev, urb->pipe, is_out);
  3162. + int buf_len = urb->transfer_buffer_length;
  3163. + void *buf = buf_len > 0 ? urb->transfer_buffer : NULL;
  3164. + struct USB_SB_Desc *sb_desc = NULL;
  3165. +
  3166. + struct crisv10_urb_priv *urb_priv = (struct crisv10_urb_priv *)urb->hcpriv;
  3167. + ASSERT(urb_priv != NULL);
  3168. +
  3169. + switch(type) {
  3170. + case PIPE_CONTROL:
  3171. + /* Setup stage */
  3172. + sb_desc = create_sb(NULL, TT_SETUP, urb->setup_packet, 8, mem_flags);
  3173. + if(sb_desc == NULL)
  3174. + return -ENOMEM;
  3175. + set_sb_cmds(sb_desc, CMD_FULL);
  3176. +
  3177. + /* Attach first SB to URB */
  3178. + urb_priv->first_sb = sb_desc;
  3179. +
  3180. + if (is_out) { /* Out Control URB */
  3181. + /* If this Control OUT transfer has an optional data stage we add
  3182. + an OUT token before the mandatory IN (status) token */
  3183. + if ((buf_len > 0) && buf) {
  3184. + sb_desc = create_sb(sb_desc, TT_OUT, buf, buf_len, mem_flags);
  3185. + if(sb_desc == NULL)
  3186. + return -ENOMEM;
  3187. + set_sb_cmds(sb_desc, CMD_FULL);
  3188. + }
  3189. +
  3190. + /* Status stage */
  3191. + /* The data length has to be exactly 1. This is due to a requirement
  3192. + of the USB specification that a host must be prepared to receive
  3193. + data in the status phase */
  3194. + sb_desc = create_sb(sb_desc, TT_IN, NULL, 1, mem_flags);
  3195. + if(sb_desc == NULL)
  3196. + return -ENOMEM;
  3197. + } else { /* In control URB */
  3198. + /* Data stage */
  3199. + sb_desc = create_sb_in(sb_desc, buf_len, maxlen, mem_flags);
  3200. + if(sb_desc == NULL)
  3201. + return -ENOMEM;
  3202. +
  3203. + /* Status stage */
  3204. + /* Read comment at zout_buffer declaration for an explanation to this. */
  3205. + sb_desc = create_sb(sb_desc, TT_ZOUT, &zout_buffer[0], 1, mem_flags);
  3206. + if(sb_desc == NULL)
  3207. + return -ENOMEM;
  3208. + /* Set descriptor interrupt flag for in URBs so we can finish URB after
  3209. + zout-packet has been sent */
  3210. + set_sb_cmds(sb_desc, CMD_INTR | CMD_FULL);
  3211. + }
  3212. + /* Set end-of-list flag in last SB */
  3213. + set_sb_cmds(sb_desc, CMD_EOL);
  3214. + /* Attach last SB to URB */
  3215. + urb_priv->last_sb = sb_desc;
  3216. + break;
  3217. +
  3218. + case PIPE_BULK:
  3219. + if (is_out) { /* Out Bulk URB */
  3220. + sb_desc = create_sb(NULL, TT_OUT, buf, buf_len, mem_flags);
  3221. + if(sb_desc == NULL)
  3222. + return -ENOMEM;
  3223. + /* The full field is set to yes, even if we don't actually check that
  3224. + this is a full-length transfer (i.e., that transfer_buffer_length %
  3225. + maxlen = 0).
  3226. + Setting full prevents the USB controller from sending an empty packet
  3227. + in that case. However, if URB_ZERO_PACKET was set we want that. */
  3228. + if (!(urb->transfer_flags & URB_ZERO_PACKET)) {
  3229. + set_sb_cmds(sb_desc, CMD_FULL);
  3230. + }
  3231. + } else { /* In Bulk URB */
  3232. + sb_desc = create_sb_in(NULL, buf_len, maxlen, mem_flags);
  3233. + if(sb_desc == NULL)
  3234. + return -ENOMEM;
  3235. + }
  3236. + /* Set end-of-list flag for last SB */
  3237. + set_sb_cmds(sb_desc, CMD_EOL);
  3238. +
  3239. + /* Attach SB to URB */
  3240. + urb_priv->first_sb = sb_desc;
  3241. + urb_priv->last_sb = sb_desc;
  3242. + break;
  3243. +
  3244. + case PIPE_INTERRUPT:
  3245. + if(is_out) { /* Out Intr URB */
  3246. + sb_desc = create_sb(NULL, TT_OUT, buf, buf_len, mem_flags);
  3247. + if(sb_desc == NULL)
  3248. + return -ENOMEM;
  3249. +
  3250. + /* The full field is set to yes, even if we don't actually check that
  3251. + this is a full-length transfer (i.e., that transfer_buffer_length %
  3252. + maxlen = 0).
  3253. + Setting full prevents the USB controller from sending an empty packet
  3254. + in that case. However, if URB_ZERO_PACKET was set we want that. */
  3255. + if (!(urb->transfer_flags & URB_ZERO_PACKET)) {
  3256. + set_sb_cmds(sb_desc, CMD_FULL);
  3257. + }
  3258. + /* Only generate TX interrupt if it's a Out URB*/
  3259. + set_sb_cmds(sb_desc, CMD_INTR);
  3260. +
  3261. + } else { /* In Intr URB */
  3262. + sb_desc = create_sb_in(NULL, buf_len, maxlen, mem_flags);
  3263. + if(sb_desc == NULL)
  3264. + return -ENOMEM;
  3265. + }
  3266. + /* Set end-of-list flag for last SB */
  3267. + set_sb_cmds(sb_desc, CMD_EOL);
  3268. +
  3269. + /* Attach SB to URB */
  3270. + urb_priv->first_sb = sb_desc;
  3271. + urb_priv->last_sb = sb_desc;
  3272. +
  3273. + break;
  3274. + case PIPE_ISOCHRONOUS:
  3275. + if(is_out) { /* Out Isoc URB */
  3276. + int i;
  3277. + if(urb->number_of_packets == 0) {
  3278. + tc_err("Can't create SBs for Isoc URB with zero packets\n");
  3279. + return -EPIPE;
  3280. + }
  3281. + /* Create one SB descriptor for each packet and link them together. */
  3282. + for(i = 0; i < urb->number_of_packets; i++) {
  3283. + if (urb->iso_frame_desc[i].length > 0) {
  3284. +
  3285. + sb_desc = create_sb(sb_desc, TT_OUT, urb->transfer_buffer +
  3286. + urb->iso_frame_desc[i].offset,
  3287. + urb->iso_frame_desc[i].length, mem_flags);
  3288. + if(sb_desc == NULL)
  3289. + return -ENOMEM;
  3290. +
  3291. + /* Check if it's a full length packet */
  3292. + if (urb->iso_frame_desc[i].length ==
  3293. + usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe))) {
  3294. + set_sb_cmds(sb_desc, CMD_FULL);
  3295. + }
  3296. +
  3297. + } else { /* zero length packet */
  3298. + sb_desc = create_sb(sb_desc, TT_ZOUT, &zout_buffer[0], 1, mem_flags);
  3299. + if(sb_desc == NULL)
  3300. + return -ENOMEM;
  3301. + set_sb_cmds(sb_desc, CMD_FULL);
  3302. + }
  3303. + /* Attach first SB descriptor to URB */
  3304. + if (i == 0) {
  3305. + urb_priv->first_sb = sb_desc;
  3306. + }
  3307. + }
  3308. + /* Set interrupt and end-of-list flags in last SB */
  3309. + set_sb_cmds(sb_desc, CMD_INTR | CMD_EOL);
  3310. + /* Attach last SB descriptor to URB */
  3311. + urb_priv->last_sb = sb_desc;
  3312. + tc_dbg("Created %d out SBs for Isoc URB:0x%x\n",
  3313. + urb->number_of_packets, (unsigned int)urb);
  3314. + } else { /* In Isoc URB */
  3315. + /* Actual number of packets is not relevant for periodic in traffic as
  3316. + long as it is more than zero. Set to 1 always. */
  3317. + sb_desc = create_sb(sb_desc, TT_IN, NULL, 1, mem_flags);
  3318. + if(sb_desc == NULL)
  3319. + return -ENOMEM;
  3320. + /* Set end-of-list flags for SB */
  3321. + set_sb_cmds(sb_desc, CMD_EOL);
  3322. +
  3323. + /* Attach SB to URB */
  3324. + urb_priv->first_sb = sb_desc;
  3325. + urb_priv->last_sb = sb_desc;
  3326. + }
  3327. + break;
  3328. + default:
  3329. + tc_err("Unknown pipe-type\n");
  3330. + return -EPIPE;
  3331. + break;
  3332. + }
  3333. + return 0;
  3334. +}
  3335. +
  3336. +int init_intr_urb(struct urb *urb, int mem_flags) {
  3337. + struct crisv10_urb_priv *urb_priv = (struct crisv10_urb_priv *)urb->hcpriv;
  3338. + struct USB_EP_Desc* ep_desc;
  3339. + int interval;
  3340. + int i;
  3341. + int ep_count;
  3342. +
  3343. + ASSERT(urb_priv != NULL);
  3344. + ASSERT(usb_pipeint(urb->pipe));
  3345. + /* We can't support interval longer than amount of eof descriptors in
  3346. + TxIntrEPList */
  3347. + if(urb->interval > MAX_INTR_INTERVAL) {
  3348. + tc_err("Interrupt interval %dms too big (max: %dms)\n", urb->interval,
  3349. + MAX_INTR_INTERVAL);
  3350. + return -EINVAL;
  3351. + }
  3352. +
  3353. + /* We assume that the SB descriptors already have been setup */
  3354. + ASSERT(urb_priv->first_sb != NULL);
  3355. +
  3356. + /* Round of the interval to 2^n, it is obvious that this code favours
  3357. + smaller numbers, but that is actually a good thing */
  3358. + /* FIXME: The "rounding error" for larger intervals will be quite
  3359. + large. For in traffic this shouldn't be a problem since it will only
  3360. + mean that we "poll" more often. */
  3361. + interval = urb->interval;
  3362. + for (i = 0; interval; i++) {
  3363. + interval = interval >> 1;
  3364. + }
  3365. + urb_priv->interval = 1 << (i - 1);
  3366. +
  3367. + /* We can only have max interval for Out Interrupt due to that we can only
  3368. + handle one linked in EP for a certain epid in the Intr descr array at the
  3369. + time. The USB Controller in the Etrax 100LX continues to process Intr EPs
  3370. + so we have no way of knowing which one that caused the actual transfer if
  3371. + we have several linked in. */
  3372. + if(usb_pipeout(urb->pipe)) {
  3373. + urb_priv->interval = MAX_INTR_INTERVAL;
  3374. + }
  3375. +
  3376. + /* Calculate amount of EPs needed */
  3377. + ep_count = MAX_INTR_INTERVAL / urb_priv->interval;
  3378. +
  3379. + for(i = 0; i < ep_count; i++) {
  3380. + ep_desc = create_ep(urb_priv->epid, urb_priv->first_sb, mem_flags);
  3381. + if(ep_desc == NULL) {
  3382. + /* Free any descriptors that we may have allocated before failure */
  3383. + while(i > 0) {
  3384. + i--;
  3385. + kfree(urb_priv->intr_ep_pool[i]);
  3386. + }
  3387. + return -ENOMEM;
  3388. + }
  3389. + urb_priv->intr_ep_pool[i] = ep_desc;
  3390. + }
  3391. + urb_priv->intr_ep_pool_length = ep_count;
  3392. + return 0;
  3393. +}
  3394. +
  3395. +/* DMA RX/TX functions */
  3396. +/* ----------------------- */
  3397. +
  3398. +static void tc_dma_init_rx_list(void) {
  3399. + int i;
  3400. +
  3401. + /* Setup descriptor list except last one */
  3402. + for (i = 0; i < (NBR_OF_RX_DESC - 1); i++) {
  3403. + RxDescList[i].sw_len = RX_DESC_BUF_SIZE;
  3404. + RxDescList[i].command = 0;
  3405. + RxDescList[i].next = virt_to_phys(&RxDescList[i + 1]);
  3406. + RxDescList[i].buf = virt_to_phys(RxBuf + (i * RX_DESC_BUF_SIZE));
  3407. + RxDescList[i].hw_len = 0;
  3408. + RxDescList[i].status = 0;
  3409. +
  3410. + /* DMA IN cache bug. (struct etrax_dma_descr has the same layout as
  3411. + USB_IN_Desc for the relevant fields.) */
  3412. + prepare_rx_descriptor((struct etrax_dma_descr*)&RxDescList[i]);
  3413. +
  3414. + }
  3415. + /* Special handling of last descriptor */
  3416. + RxDescList[i].sw_len = RX_DESC_BUF_SIZE;
  3417. + RxDescList[i].command = IO_STATE(USB_IN_command, eol, yes);
  3418. + RxDescList[i].next = virt_to_phys(&RxDescList[0]);
  3419. + RxDescList[i].buf = virt_to_phys(RxBuf + (i * RX_DESC_BUF_SIZE));
  3420. + RxDescList[i].hw_len = 0;
  3421. + RxDescList[i].status = 0;
  3422. +
  3423. + /* Setup list pointers that show progress in list */
  3424. + myNextRxDesc = &RxDescList[0];
  3425. + myLastRxDesc = &RxDescList[NBR_OF_RX_DESC - 1];
  3426. +
  3427. + flush_etrax_cache();
  3428. + /* Point DMA to first descriptor in list and start it */
  3429. + *R_DMA_CH9_FIRST = virt_to_phys(myNextRxDesc);
  3430. + *R_DMA_CH9_CMD = IO_STATE(R_DMA_CH9_CMD, cmd, start);
  3431. +}
  3432. +
  3433. +
  3434. +static void tc_dma_init_tx_bulk_list(void) {
  3435. + int i;
  3436. + volatile struct USB_EP_Desc *epDescr;
  3437. +
  3438. + for (i = 0; i < (NBR_OF_EPIDS - 1); i++) {
  3439. + epDescr = &(TxBulkEPList[i]);
  3440. + CHECK_ALIGN(epDescr);
  3441. + epDescr->hw_len = 0;
  3442. + epDescr->command = IO_FIELD(USB_EP_command, epid, i);
  3443. + epDescr->sub = 0;
  3444. + epDescr->next = virt_to_phys(&TxBulkEPList[i + 1]);
  3445. +
  3446. + /* Initiate two EPs, disabled and with the eol flag set. No need for any
  3447. + preserved epid. */
  3448. +
  3449. + /* The first one has the intr flag set so we get an interrupt when the DMA
  3450. + channel is about to become disabled. */
  3451. + CHECK_ALIGN(&TxBulkDummyEPList[i][0]);
  3452. + TxBulkDummyEPList[i][0].hw_len = 0;
  3453. + TxBulkDummyEPList[i][0].command = (IO_FIELD(USB_EP_command, epid, DUMMY_EPID) |
  3454. + IO_STATE(USB_EP_command, eol, yes) |
  3455. + IO_STATE(USB_EP_command, intr, yes));
  3456. + TxBulkDummyEPList[i][0].sub = 0;
  3457. + TxBulkDummyEPList[i][0].next = virt_to_phys(&TxBulkDummyEPList[i][1]);
  3458. +
  3459. + /* The second one. */
  3460. + CHECK_ALIGN(&TxBulkDummyEPList[i][1]);
  3461. + TxBulkDummyEPList[i][1].hw_len = 0;
  3462. + TxBulkDummyEPList[i][1].command = (IO_FIELD(USB_EP_command, epid, DUMMY_EPID) |
  3463. + IO_STATE(USB_EP_command, eol, yes));
  3464. + TxBulkDummyEPList[i][1].sub = 0;
  3465. + /* The last dummy's next pointer is the same as the current EP's next pointer. */
  3466. + TxBulkDummyEPList[i][1].next = virt_to_phys(&TxBulkEPList[i + 1]);
  3467. + }
  3468. +
  3469. + /* Special handling of last descr in list, make list circular */
  3470. + epDescr = &TxBulkEPList[i];
  3471. + CHECK_ALIGN(epDescr);
  3472. + epDescr->hw_len = 0;
  3473. + epDescr->command = IO_STATE(USB_EP_command, eol, yes) |
  3474. + IO_FIELD(USB_EP_command, epid, i);
  3475. + epDescr->sub = 0;
  3476. + epDescr->next = virt_to_phys(&TxBulkEPList[0]);
  3477. +
  3478. + /* Init DMA sub-channel pointers to last item in each list */
  3479. + *R_DMA_CH8_SUB0_EP = virt_to_phys(&TxBulkEPList[i]);
  3480. + /* No point in starting the bulk channel yet.
  3481. + *R_DMA_CH8_SUB0_CMD = IO_STATE(R_DMA_CH8_SUB0_CMD, cmd, start); */
  3482. +}
  3483. +
  3484. +static void tc_dma_init_tx_ctrl_list(void) {
  3485. + int i;
  3486. + volatile struct USB_EP_Desc *epDescr;
  3487. +
  3488. + for (i = 0; i < (NBR_OF_EPIDS - 1); i++) {
  3489. + epDescr = &(TxCtrlEPList[i]);
  3490. + CHECK_ALIGN(epDescr);
  3491. + epDescr->hw_len = 0;
  3492. + epDescr->command = IO_FIELD(USB_EP_command, epid, i);
  3493. + epDescr->sub = 0;
  3494. + epDescr->next = virt_to_phys(&TxCtrlEPList[i + 1]);
  3495. + }
  3496. + /* Special handling of last descr in list, make list circular */
  3497. + epDescr = &TxCtrlEPList[i];
  3498. + CHECK_ALIGN(epDescr);
  3499. + epDescr->hw_len = 0;
  3500. + epDescr->command = IO_STATE(USB_EP_command, eol, yes) |
  3501. + IO_FIELD(USB_EP_command, epid, i);
  3502. + epDescr->sub = 0;
  3503. + epDescr->next = virt_to_phys(&TxCtrlEPList[0]);
  3504. +
  3505. + /* Init DMA sub-channel pointers to last item in each list */
  3506. + *R_DMA_CH8_SUB1_EP = virt_to_phys(&TxCtrlEPList[i]);
  3507. + /* No point in starting the ctrl channel yet.
  3508. + *R_DMA_CH8_SUB1_CMD = IO_STATE(R_DMA_CH8_SUB0_CMD, cmd, start); */
  3509. +}
  3510. +
  3511. +
  3512. +static void tc_dma_init_tx_intr_list(void) {
  3513. + int i;
  3514. +
  3515. + TxIntrSB_zout.sw_len = 1;
  3516. + TxIntrSB_zout.next = 0;
  3517. + TxIntrSB_zout.buf = virt_to_phys(&zout_buffer[0]);
  3518. + TxIntrSB_zout.command = (IO_FIELD(USB_SB_command, rem, 0) |
  3519. + IO_STATE(USB_SB_command, tt, zout) |
  3520. + IO_STATE(USB_SB_command, full, yes) |
  3521. + IO_STATE(USB_SB_command, eot, yes) |
  3522. + IO_STATE(USB_SB_command, eol, yes));
  3523. +
  3524. + for (i = 0; i < (MAX_INTR_INTERVAL - 1); i++) {
  3525. + CHECK_ALIGN(&TxIntrEPList[i]);
  3526. + TxIntrEPList[i].hw_len = 0;
  3527. + TxIntrEPList[i].command =
  3528. + (IO_STATE(USB_EP_command, eof, yes) |
  3529. + IO_STATE(USB_EP_command, enable, yes) |
  3530. + IO_FIELD(USB_EP_command, epid, INVALID_EPID));
  3531. + TxIntrEPList[i].sub = virt_to_phys(&TxIntrSB_zout);
  3532. + TxIntrEPList[i].next = virt_to_phys(&TxIntrEPList[i + 1]);
  3533. + }
  3534. +
  3535. + /* Special handling of last descr in list, make list circular */
  3536. + CHECK_ALIGN(&TxIntrEPList[i]);
  3537. + TxIntrEPList[i].hw_len = 0;
  3538. + TxIntrEPList[i].command =
  3539. + (IO_STATE(USB_EP_command, eof, yes) |
  3540. + IO_STATE(USB_EP_command, eol, yes) |
  3541. + IO_STATE(USB_EP_command, enable, yes) |
  3542. + IO_FIELD(USB_EP_command, epid, INVALID_EPID));
  3543. + TxIntrEPList[i].sub = virt_to_phys(&TxIntrSB_zout);
  3544. + TxIntrEPList[i].next = virt_to_phys(&TxIntrEPList[0]);
  3545. +
  3546. + intr_dbg("Initiated Intr EP descriptor list\n");
  3547. +
  3548. +
  3549. + /* Connect DMA 8 sub-channel 2 to first in list */
  3550. + *R_DMA_CH8_SUB2_EP = virt_to_phys(&TxIntrEPList[0]);
  3551. +}
  3552. +
  3553. +static void tc_dma_init_tx_isoc_list(void) {
  3554. + int i;
  3555. +
  3556. + DBFENTER;
  3557. +
  3558. + /* Read comment at zout_buffer declaration for an explanation to this. */
  3559. + TxIsocSB_zout.sw_len = 1;
  3560. + TxIsocSB_zout.next = 0;
  3561. + TxIsocSB_zout.buf = virt_to_phys(&zout_buffer[0]);
  3562. + TxIsocSB_zout.command = (IO_FIELD(USB_SB_command, rem, 0) |
  3563. + IO_STATE(USB_SB_command, tt, zout) |
  3564. + IO_STATE(USB_SB_command, full, yes) |
  3565. + IO_STATE(USB_SB_command, eot, yes) |
  3566. + IO_STATE(USB_SB_command, eol, yes));
  3567. +
  3568. + /* The last isochronous EP descriptor is a dummy. */
  3569. + for (i = 0; i < (NBR_OF_EPIDS - 1); i++) {
  3570. + CHECK_ALIGN(&TxIsocEPList[i]);
  3571. + TxIsocEPList[i].hw_len = 0;
  3572. + TxIsocEPList[i].command = IO_FIELD(USB_EP_command, epid, i);
  3573. + TxIsocEPList[i].sub = 0;
  3574. + TxIsocEPList[i].next = virt_to_phys(&TxIsocEPList[i + 1]);
  3575. + }
  3576. +
  3577. + CHECK_ALIGN(&TxIsocEPList[i]);
  3578. + TxIsocEPList[i].hw_len = 0;
  3579. +
  3580. + /* Must enable the last EP descr to get eof interrupt. */
  3581. + TxIsocEPList[i].command = (IO_STATE(USB_EP_command, enable, yes) |
  3582. + IO_STATE(USB_EP_command, eof, yes) |
  3583. + IO_STATE(USB_EP_command, eol, yes) |
  3584. + IO_FIELD(USB_EP_command, epid, INVALID_EPID));
  3585. + TxIsocEPList[i].sub = virt_to_phys(&TxIsocSB_zout);
  3586. + TxIsocEPList[i].next = virt_to_phys(&TxIsocEPList[0]);
  3587. +
  3588. + *R_DMA_CH8_SUB3_EP = virt_to_phys(&TxIsocEPList[0]);
  3589. + *R_DMA_CH8_SUB3_CMD = IO_STATE(R_DMA_CH8_SUB3_CMD, cmd, start);
  3590. +}
  3591. +
  3592. +static int tc_dma_init(struct usb_hcd *hcd) {
  3593. + tc_dma_init_rx_list();
  3594. + tc_dma_init_tx_bulk_list();
  3595. + tc_dma_init_tx_ctrl_list();
  3596. + tc_dma_init_tx_intr_list();
  3597. + tc_dma_init_tx_isoc_list();
  3598. +
  3599. + if (cris_request_dma(USB_TX_DMA_NBR,
  3600. + "ETRAX 100LX built-in USB (Tx)",
  3601. + DMA_VERBOSE_ON_ERROR,
  3602. + dma_usb)) {
  3603. + err("Could not allocate DMA ch 8 for USB");
  3604. + return -EBUSY;
  3605. + }
  3606. +
  3607. + if (cris_request_dma(USB_RX_DMA_NBR,
  3608. + "ETRAX 100LX built-in USB (Rx)",
  3609. + DMA_VERBOSE_ON_ERROR,
  3610. + dma_usb)) {
  3611. + err("Could not allocate DMA ch 9 for USB");
  3612. + return -EBUSY;
  3613. + }
  3614. +
  3615. + *R_IRQ_MASK2_SET =
  3616. + /* Note that these interrupts are not used. */
  3617. + IO_STATE(R_IRQ_MASK2_SET, dma8_sub0_descr, set) |
  3618. + /* Sub channel 1 (ctrl) descr. interrupts are used. */
  3619. + IO_STATE(R_IRQ_MASK2_SET, dma8_sub1_descr, set) |
  3620. + IO_STATE(R_IRQ_MASK2_SET, dma8_sub2_descr, set) |
  3621. + /* Sub channel 3 (isoc) descr. interrupts are used. */
  3622. + IO_STATE(R_IRQ_MASK2_SET, dma8_sub3_descr, set);
  3623. +
  3624. + /* Note that the dma9_descr interrupt is not used. */
  3625. + *R_IRQ_MASK2_SET =
  3626. + IO_STATE(R_IRQ_MASK2_SET, dma9_eop, set) |
  3627. + IO_STATE(R_IRQ_MASK2_SET, dma9_descr, set);
  3628. +
  3629. + if (request_irq(ETRAX_USB_RX_IRQ, tc_dma_rx_interrupt, 0,
  3630. + "ETRAX 100LX built-in USB (Rx)", hcd)) {
  3631. + err("Could not allocate IRQ %d for USB", ETRAX_USB_RX_IRQ);
  3632. + return -EBUSY;
  3633. + }
  3634. +
  3635. + if (request_irq(ETRAX_USB_TX_IRQ, tc_dma_tx_interrupt, 0,
  3636. + "ETRAX 100LX built-in USB (Tx)", hcd)) {
  3637. + err("Could not allocate IRQ %d for USB", ETRAX_USB_TX_IRQ);
  3638. + return -EBUSY;
  3639. + }
  3640. +
  3641. + return 0;
  3642. +}
  3643. +
  3644. +static void tc_dma_destroy(void) {
  3645. + free_irq(ETRAX_USB_RX_IRQ, NULL);
  3646. + free_irq(ETRAX_USB_TX_IRQ, NULL);
  3647. +
  3648. + cris_free_dma(USB_TX_DMA_NBR, "ETRAX 100LX built-in USB (Tx)");
  3649. + cris_free_dma(USB_RX_DMA_NBR, "ETRAX 100LX built-in USB (Rx)");
  3650. +
  3651. +}
  3652. +
  3653. +static void tc_dma_link_intr_urb(struct urb *urb);
  3654. +
  3655. +/* Handle processing of Bulk, Ctrl and Intr queues */
  3656. +static void tc_dma_process_queue(int epid) {
  3657. + struct urb *urb;
  3658. + struct crisv10_urb_priv *urb_priv = urb->hcpriv;
  3659. + unsigned long flags;
  3660. + char toggle;
  3661. +
  3662. + if(epid_state[epid].disabled) {
  3663. + /* Don't process any URBs on a disabled endpoint */
  3664. + return;
  3665. + }
  3666. +
  3667. + /* Do not disturb us while fiddling with EPs and epids */
  3668. + local_irq_save(flags);
  3669. +
  3670. + /* For bulk, Ctrl and Intr can we only have one URB active at a time for
  3671. + a specific EP. */
  3672. + if(activeUrbList[epid] != NULL) {
  3673. + /* An URB is already active on EP, skip checking queue */
  3674. + local_irq_restore(flags);
  3675. + return;
  3676. + }
  3677. +
  3678. + urb = urb_list_first(epid);
  3679. + if(urb == NULL) {
  3680. + /* No URB waiting in EP queue. Nothing do to */
  3681. + local_irq_restore(flags);
  3682. + return;
  3683. + }
  3684. +
  3685. + urb_priv = urb->hcpriv;
  3686. + ASSERT(urb_priv != NULL);
  3687. + ASSERT(urb_priv->urb_state == NOT_STARTED);
  3688. + ASSERT(!usb_pipeisoc(urb->pipe));
  3689. +
  3690. + /* Remove this URB from the queue and move it to active */
  3691. + activeUrbList[epid] = urb;
  3692. + urb_list_del(urb, epid);
  3693. +
  3694. + urb_priv->urb_state = STARTED;
  3695. +
  3696. + /* Reset error counters (regardless of which direction this traffic is). */
  3697. + etrax_epid_clear_error(epid);
  3698. +
  3699. + /* Special handling of Intr EP lists */
  3700. + if(usb_pipeint(urb->pipe)) {
  3701. + tc_dma_link_intr_urb(urb);
  3702. + local_irq_restore(flags);
  3703. + return;
  3704. + }
  3705. +
  3706. + /* Software must preset the toggle bits for Bulk and Ctrl */
  3707. + if(usb_pipecontrol(urb->pipe)) {
  3708. + /* Toggle bits are initialized only during setup transaction in a
  3709. + CTRL transfer */
  3710. + etrax_epid_set_toggle(epid, 0, 0);
  3711. + etrax_epid_set_toggle(epid, 1, 0);
  3712. + } else {
  3713. + toggle = usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
  3714. + usb_pipeout(urb->pipe));
  3715. + etrax_epid_set_toggle(epid, usb_pipeout(urb->pipe), toggle);
  3716. + }
  3717. +
  3718. + tc_dbg("Added SBs from (URB:0x%x %s %s) to epid %d: %s\n",
  3719. + (unsigned int)urb, str_dir(urb->pipe), str_type(urb->pipe), epid,
  3720. + sblist_to_str(urb_priv->first_sb));
  3721. +
  3722. + /* We start the DMA sub channel without checking if it's running or not,
  3723. + because:
  3724. + 1) If it's already running, issuing the start command is a nop.
  3725. + 2) We avoid a test-and-set race condition. */
  3726. + switch(usb_pipetype(urb->pipe)) {
  3727. + case PIPE_BULK:
  3728. + /* Assert that the EP descriptor is disabled. */
  3729. + ASSERT(!(TxBulkEPList[epid].command & IO_MASK(USB_EP_command, enable)));
  3730. +
  3731. + /* Set up and enable the EP descriptor. */
  3732. + TxBulkEPList[epid].sub = virt_to_phys(urb_priv->first_sb);
  3733. + TxBulkEPList[epid].hw_len = 0;
  3734. + TxBulkEPList[epid].command |= IO_STATE(USB_EP_command, enable, yes);
  3735. +
  3736. + /* Check if the dummy list is already with us (if several urbs were queued). */
  3737. + if (usb_pipein(urb->pipe) && (TxBulkEPList[epid].next != virt_to_phys(&TxBulkDummyEPList[epid][0]))) {
  3738. + tc_dbg("Inviting dummy list to the party for urb 0x%lx, epid %d",
  3739. + (unsigned long)urb, epid);
  3740. +
  3741. + /* We don't need to check if the DMA is at this EP or not before changing the
  3742. + next pointer, since we will do it in one 32-bit write (EP descriptors are
  3743. + 32-bit aligned). */
  3744. + TxBulkEPList[epid].next = virt_to_phys(&TxBulkDummyEPList[epid][0]);
  3745. + }
  3746. +
  3747. + restart_dma8_sub0();
  3748. +
  3749. + /* Update/restart the bulk start timer since we just started the channel.*/
  3750. + mod_timer(&bulk_start_timer, jiffies + BULK_START_TIMER_INTERVAL);
  3751. + /* Update/restart the bulk eot timer since we just inserted traffic. */
  3752. + mod_timer(&bulk_eot_timer, jiffies + BULK_EOT_TIMER_INTERVAL);
  3753. + break;
  3754. + case PIPE_CONTROL:
  3755. + /* Assert that the EP descriptor is disabled. */
  3756. + ASSERT(!(TxCtrlEPList[epid].command & IO_MASK(USB_EP_command, enable)));
  3757. +
  3758. + /* Set up and enable the EP descriptor. */
  3759. + TxCtrlEPList[epid].sub = virt_to_phys(urb_priv->first_sb);
  3760. + TxCtrlEPList[epid].hw_len = 0;
  3761. + TxCtrlEPList[epid].command |= IO_STATE(USB_EP_command, enable, yes);
  3762. +
  3763. + *R_DMA_CH8_SUB1_CMD = IO_STATE(R_DMA_CH8_SUB1_CMD, cmd, start);
  3764. + break;
  3765. + }
  3766. + local_irq_restore(flags);
  3767. +}
  3768. +
  3769. +static void tc_dma_link_intr_urb(struct urb *urb) {
  3770. + struct crisv10_urb_priv *urb_priv = urb->hcpriv;
  3771. + volatile struct USB_EP_Desc *tmp_ep;
  3772. + struct USB_EP_Desc *ep_desc;
  3773. + int i = 0, epid;
  3774. + int pool_idx = 0;
  3775. +
  3776. + ASSERT(urb_priv != NULL);
  3777. + epid = urb_priv->epid;
  3778. + ASSERT(urb_priv->interval > 0);
  3779. + ASSERT(urb_priv->intr_ep_pool_length > 0);
  3780. +
  3781. + tmp_ep = &TxIntrEPList[0];
  3782. +
  3783. + /* Only insert one EP descriptor in list for Out Intr URBs.
  3784. + We can only handle Out Intr with interval of 128ms because
  3785. + it's not possible to insert several Out Intr EPs because they
  3786. + are not consumed by the DMA. */
  3787. + if(usb_pipeout(urb->pipe)) {
  3788. + ep_desc = urb_priv->intr_ep_pool[0];
  3789. + ASSERT(ep_desc);
  3790. + ep_desc->next = tmp_ep->next;
  3791. + tmp_ep->next = virt_to_phys(ep_desc);
  3792. + i++;
  3793. + } else {
  3794. + /* Loop through Intr EP descriptor list and insert EP for URB at
  3795. + specified interval */
  3796. + do {
  3797. + /* Each EP descriptor with eof flag sat signals a new frame */
  3798. + if (tmp_ep->command & IO_MASK(USB_EP_command, eof)) {
  3799. + /* Insert a EP from URBs EP pool at correct interval */
  3800. + if ((i % urb_priv->interval) == 0) {
  3801. + ep_desc = urb_priv->intr_ep_pool[pool_idx];
  3802. + ASSERT(ep_desc);
  3803. + ep_desc->next = tmp_ep->next;
  3804. + tmp_ep->next = virt_to_phys(ep_desc);
  3805. + pool_idx++;
  3806. + ASSERT(pool_idx <= urb_priv->intr_ep_pool_length);
  3807. + }
  3808. + i++;
  3809. + }
  3810. + tmp_ep = (struct USB_EP_Desc *)phys_to_virt(tmp_ep->next);
  3811. + } while(tmp_ep != &TxIntrEPList[0]);
  3812. + }
  3813. +
  3814. + intr_dbg("Added SBs to intr epid %d: %s interval:%d (%d EP)\n", epid,
  3815. + sblist_to_str(urb_priv->first_sb), urb_priv->interval, pool_idx);
  3816. +
  3817. + /* We start the DMA sub channel without checking if it's running or not,
  3818. + because:
  3819. + 1) If it's already running, issuing the start command is a nop.
  3820. + 2) We avoid a test-and-set race condition. */
  3821. + *R_DMA_CH8_SUB2_CMD = IO_STATE(R_DMA_CH8_SUB2_CMD, cmd, start);
  3822. +}
  3823. +
  3824. + /* hinko ignore usb_pipeisoc */
  3825. +#if 0
  3826. +static void tc_dma_process_isoc_urb(struct urb *urb) {
  3827. + unsigned long flags;
  3828. + struct crisv10_urb_priv *urb_priv = urb->hcpriv;
  3829. + int epid;
  3830. +
  3831. + /* Do not disturb us while fiddling with EPs and epids */
  3832. + local_irq_save(flags);
  3833. +
  3834. + ASSERT(urb_priv);
  3835. + ASSERT(urb_priv->first_sb);
  3836. + epid = urb_priv->epid;
  3837. +
  3838. + if(activeUrbList[epid] == NULL) {
  3839. + /* EP is idle, so make this URB active */
  3840. + activeUrbList[epid] = urb;
  3841. + urb_list_del(urb, epid);
  3842. + ASSERT(TxIsocEPList[epid].sub == 0);
  3843. + ASSERT(!(TxIsocEPList[epid].command &
  3844. + IO_STATE(USB_EP_command, enable, yes)));
  3845. +
  3846. + /* Differentiate between In and Out Isoc. Because In SBs are not consumed*/
  3847. + if(usb_pipein(urb->pipe)) {
  3848. + /* Each EP for In Isoc will have only one SB descriptor, setup when
  3849. + submitting the first active urb. We do it here by copying from URBs
  3850. + pre-allocated SB. */
  3851. + memcpy((void *)&(TxIsocSBList[epid]), urb_priv->first_sb,
  3852. + sizeof(TxIsocSBList[epid]));
  3853. + TxIsocEPList[epid].hw_len = 0;
  3854. + TxIsocEPList[epid].sub = virt_to_phys(&(TxIsocSBList[epid]));
  3855. + } else {
  3856. + /* For Out Isoc we attach the pre-allocated list of SBs for the URB */
  3857. + TxIsocEPList[epid].hw_len = 0;
  3858. + TxIsocEPList[epid].sub = virt_to_phys(urb_priv->first_sb);
  3859. +
  3860. + isoc_dbg("Attached first URB:0x%x[%d] to epid:%d first_sb:0x%x"
  3861. + " last_sb::0x%x\n",
  3862. + (unsigned int)urb, urb_priv->urb_num, epid,
  3863. + (unsigned int)(urb_priv->first_sb),
  3864. + (unsigned int)(urb_priv->last_sb));
  3865. + }
  3866. +
  3867. + if (urb->transfer_flags & URB_ISO_ASAP) {
  3868. + /* The isoc transfer should be started as soon as possible. The
  3869. + start_frame field is a return value if URB_ISO_ASAP was set. Comparing
  3870. + R_USB_FM_NUMBER with a USB Chief trace shows that the first isoc IN
  3871. + token is sent 2 frames later. I'm not sure how this affects usage of
  3872. + the start_frame field by the device driver, or how it affects things
  3873. + when USB_ISO_ASAP is not set, so therefore there's no compensation for
  3874. + the 2 frame "lag" here. */
  3875. + urb->start_frame = (*R_USB_FM_NUMBER & 0x7ff);
  3876. + TxIsocEPList[epid].command |= IO_STATE(USB_EP_command, enable, yes);
  3877. + urb_priv->urb_state = STARTED;
  3878. + isoc_dbg("URB_ISO_ASAP set, urb->start_frame set to %d\n",
  3879. + urb->start_frame);
  3880. + } else {
  3881. + /* Not started yet. */
  3882. + urb_priv->urb_state = NOT_STARTED;
  3883. + isoc_warn("urb_priv->urb_state set to NOT_STARTED for URB:0x%x\n",
  3884. + (unsigned int)urb);
  3885. + }
  3886. +
  3887. + } else {
  3888. + /* An URB is already active on the EP. Leave URB in queue and let
  3889. + finish_isoc_urb process it after current active URB */
  3890. + ASSERT(TxIsocEPList[epid].sub != 0);
  3891. +
  3892. + if(usb_pipein(urb->pipe)) {
  3893. + /* Because there already is a active In URB on this epid we do nothing
  3894. + and the finish_isoc_urb() function will handle switching to next URB*/
  3895. +
  3896. + } else { /* For Out Isoc, insert new URBs traffic last in SB-list. */
  3897. + struct USB_SB_Desc *temp_sb_desc;
  3898. +
  3899. + /* Set state STARTED to all Out Isoc URBs added to SB list because we
  3900. + don't know how many of them that are finished before descr interrupt*/
  3901. + urb_priv->urb_state = STARTED;
  3902. +
  3903. + /* Find end of current SB list by looking for SB with eol flag sat */
  3904. + temp_sb_desc = phys_to_virt(TxIsocEPList[epid].sub);
  3905. + while ((temp_sb_desc->command & IO_MASK(USB_SB_command, eol)) !=
  3906. + IO_STATE(USB_SB_command, eol, yes)) {
  3907. + ASSERT(temp_sb_desc->next);
  3908. + temp_sb_desc = phys_to_virt(temp_sb_desc->next);
  3909. + }
  3910. +
  3911. + isoc_dbg("Appended URB:0x%x[%d] (first:0x%x last:0x%x) to epid:%d"
  3912. + " sub:0x%x eol:0x%x\n",
  3913. + (unsigned int)urb, urb_priv->urb_num,
  3914. + (unsigned int)(urb_priv->first_sb),
  3915. + (unsigned int)(urb_priv->last_sb), epid,
  3916. + (unsigned int)phys_to_virt(TxIsocEPList[epid].sub),
  3917. + (unsigned int)temp_sb_desc);
  3918. +
  3919. + /* Next pointer must be set before eol is removed. */
  3920. + temp_sb_desc->next = virt_to_phys(urb_priv->first_sb);
  3921. + /* Clear the previous end of list flag since there is a new in the
  3922. + added SB descriptor list. */
  3923. + temp_sb_desc->command &= ~IO_MASK(USB_SB_command, eol);
  3924. +
  3925. + if (!(TxIsocEPList[epid].command & IO_MASK(USB_EP_command, enable))) {
  3926. + __u32 epid_data;
  3927. + /* 8.8.5 in Designer's Reference says we should check for and correct
  3928. + any errors in the EP here. That should not be necessary if
  3929. + epid_attn is handled correctly, so we assume all is ok. */
  3930. + epid_data = etrax_epid_iso_get(epid);
  3931. + if (IO_EXTRACT(R_USB_EPT_DATA, error_code, epid_data) !=
  3932. + IO_STATE_VALUE(R_USB_EPT_DATA, error_code, no_error)) {
  3933. + isoc_err("Disabled Isoc EP with error:%d on epid:%d when appending"
  3934. + " URB:0x%x[%d]\n",
  3935. + IO_EXTRACT(R_USB_EPT_DATA, error_code, epid_data), epid,
  3936. + (unsigned int)urb, urb_priv->urb_num);
  3937. + }
  3938. +
  3939. + /* The SB list was exhausted. */
  3940. + if (virt_to_phys(urb_priv->last_sb) != TxIsocEPList[epid].sub) {
  3941. + /* The new sublist did not get processed before the EP was
  3942. + disabled. Setup the EP again. */
  3943. +
  3944. + if(virt_to_phys(temp_sb_desc) == TxIsocEPList[epid].sub) {
  3945. + isoc_dbg("EP for epid:%d stoped at SB:0x%x before newly inserted"
  3946. + ", restarting from this URBs SB:0x%x\n",
  3947. + epid, (unsigned int)temp_sb_desc,
  3948. + (unsigned int)(urb_priv->first_sb));
  3949. + TxIsocEPList[epid].hw_len = 0;
  3950. + TxIsocEPList[epid].sub = virt_to_phys(urb_priv->first_sb);
  3951. + urb->start_frame = (*R_USB_FM_NUMBER & 0x7ff);
  3952. + /* Enable the EP again so data gets processed this time */
  3953. + TxIsocEPList[epid].command |=
  3954. + IO_STATE(USB_EP_command, enable, yes);
  3955. +
  3956. + } else {
  3957. + /* The EP has been disabled but not at end this URB (god knows
  3958. + where). This should generate an epid_attn so we should not be
  3959. + here */
  3960. + isoc_warn("EP was disabled on sb:0x%x before SB list for"
  3961. + " URB:0x%x[%d] got processed\n",
  3962. + (unsigned int)phys_to_virt(TxIsocEPList[epid].sub),
  3963. + (unsigned int)urb, urb_priv->urb_num);
  3964. + }
  3965. + } else {
  3966. + /* This might happend if we are slow on this function and isn't
  3967. + an error. */
  3968. + isoc_dbg("EP was disabled and finished with SBs from appended"
  3969. + " URB:0x%x[%d]\n", (unsigned int)urb, urb_priv->urb_num);
  3970. + }
  3971. + }
  3972. + }
  3973. + }
  3974. +
  3975. + /* Start the DMA sub channel */
  3976. + *R_DMA_CH8_SUB3_CMD = IO_STATE(R_DMA_CH8_SUB3_CMD, cmd, start);
  3977. +
  3978. + local_irq_restore(flags);
  3979. +}
  3980. +#endif
  3981. +
  3982. +static void tc_dma_unlink_intr_urb(struct urb *urb) {
  3983. + struct crisv10_urb_priv *urb_priv = urb->hcpriv;
  3984. + volatile struct USB_EP_Desc *first_ep; /* First EP in the list. */
  3985. + volatile struct USB_EP_Desc *curr_ep; /* Current EP, the iterator. */
  3986. + volatile struct USB_EP_Desc *next_ep; /* The EP after current. */
  3987. + volatile struct USB_EP_Desc *unlink_ep; /* The one we should remove from
  3988. + the list. */
  3989. + int count = 0;
  3990. + volatile int timeout = 10000;
  3991. + int epid;
  3992. +
  3993. + /* Read 8.8.4 in Designer's Reference, "Removing an EP Descriptor from the
  3994. + List". */
  3995. + ASSERT(urb_priv);
  3996. + ASSERT(urb_priv->intr_ep_pool_length > 0);
  3997. + epid = urb_priv->epid;
  3998. +
  3999. + /* First disable all Intr EPs belonging to epid for this URB */
  4000. + first_ep = &TxIntrEPList[0];
  4001. + curr_ep = first_ep;
  4002. + do {
  4003. + next_ep = (struct USB_EP_Desc *)phys_to_virt(curr_ep->next);
  4004. + if (IO_EXTRACT(USB_EP_command, epid, next_ep->command) == epid) {
  4005. + /* Disable EP */
  4006. + next_ep->command &= ~IO_MASK(USB_EP_command, enable);
  4007. + }
  4008. + curr_ep = phys_to_virt(curr_ep->next);
  4009. + } while (curr_ep != first_ep);
  4010. +
  4011. +
  4012. + /* Now unlink all EPs belonging to this epid from Descr list */
  4013. + first_ep = &TxIntrEPList[0];
  4014. + curr_ep = first_ep;
  4015. + do {
  4016. + next_ep = (struct USB_EP_Desc *)phys_to_virt(curr_ep->next);
  4017. + if (IO_EXTRACT(USB_EP_command, epid, next_ep->command) == epid) {
  4018. + /* This is the one we should unlink. */
  4019. + unlink_ep = next_ep;
  4020. +
  4021. + /* Actually unlink the EP from the DMA list. */
  4022. + curr_ep->next = unlink_ep->next;
  4023. +
  4024. + /* Wait until the DMA is no longer at this descriptor. */
  4025. + while((*R_DMA_CH8_SUB2_EP == virt_to_phys(unlink_ep)) &&
  4026. + (timeout-- > 0));
  4027. + if(timeout == 0) {
  4028. + warn("Timeout while waiting for DMA-TX-Intr to leave unlink EP\n");
  4029. + }
  4030. +
  4031. + count++;
  4032. + }
  4033. + curr_ep = phys_to_virt(curr_ep->next);
  4034. + } while (curr_ep != first_ep);
  4035. +
  4036. + if(count != urb_priv->intr_ep_pool_length) {
  4037. + intr_warn("Unlinked %d of %d Intr EPs for URB:0x%x[%d]\n", count,
  4038. + urb_priv->intr_ep_pool_length, (unsigned int)urb,
  4039. + urb_priv->urb_num);
  4040. + } else {
  4041. + intr_dbg("Unlinked %d of %d interrupt EPs for URB:0x%x\n", count,
  4042. + urb_priv->intr_ep_pool_length, (unsigned int)urb);
  4043. + }
  4044. +}
  4045. +
  4046. +static void check_finished_bulk_tx_epids(struct usb_hcd *hcd,
  4047. + int timer) {
  4048. + unsigned long flags;
  4049. + int epid;
  4050. + struct urb *urb;
  4051. + struct crisv10_urb_priv * urb_priv;
  4052. + __u32 epid_data;
  4053. +
  4054. + /* Protect TxEPList */
  4055. + local_irq_save(flags);
  4056. +
  4057. + for (epid = 0; epid < NBR_OF_EPIDS; epid++) {
  4058. + /* A finished EP descriptor is disabled and has a valid sub pointer */
  4059. + if (!(TxBulkEPList[epid].command & IO_MASK(USB_EP_command, enable)) &&
  4060. + (TxBulkEPList[epid].sub != 0)) {
  4061. +
  4062. + /* Get the active URB for this epid */
  4063. + urb = activeUrbList[epid];
  4064. + /* Sanity checks */
  4065. + ASSERT(urb);
  4066. + urb_priv = (struct crisv10_urb_priv *)urb->hcpriv;
  4067. + ASSERT(urb_priv);
  4068. +
  4069. + /* Only handle finished out Bulk EPs here,
  4070. + and let RX interrupt take care of the rest */
  4071. + if(!epid_out_traffic(epid)) {
  4072. + continue;
  4073. + }
  4074. +
  4075. + if(timer) {
  4076. + tc_warn("Found finished %s Bulk epid:%d URB:0x%x[%d] from timeout\n",
  4077. + epid_out_traffic(epid) ? "Out" : "In", epid, (unsigned int)urb,
  4078. + urb_priv->urb_num);
  4079. + } else {
  4080. + tc_dbg("Found finished %s Bulk epid:%d URB:0x%x[%d] from interrupt\n",
  4081. + epid_out_traffic(epid) ? "Out" : "In", epid, (unsigned int)urb,
  4082. + urb_priv->urb_num);
  4083. + }
  4084. +
  4085. + if(urb_priv->urb_state == UNLINK) {
  4086. + /* This Bulk URB is requested to be unlinked, that means that the EP
  4087. + has been disabled and we might not have sent all data */
  4088. + tc_finish_urb(hcd, urb, urb->status);
  4089. + continue;
  4090. + }
  4091. +
  4092. + ASSERT(urb_priv->urb_state == STARTED);
  4093. + if (phys_to_virt(TxBulkEPList[epid].sub) != urb_priv->last_sb) {
  4094. + tc_err("Endpoint got disabled before reaching last sb\n");
  4095. + }
  4096. +
  4097. + epid_data = etrax_epid_get(epid);
  4098. + if (IO_EXTRACT(R_USB_EPT_DATA, error_code, epid_data) ==
  4099. + IO_STATE_VALUE(R_USB_EPT_DATA, error_code, no_error)) {
  4100. + /* This means that the endpoint has no error, is disabled
  4101. + and had inserted traffic, i.e. transfer successfully completed. */
  4102. + tc_finish_urb(hcd, urb, 0);
  4103. + } else {
  4104. + /* Shouldn't happen. We expect errors to be caught by epid
  4105. + attention. */
  4106. + tc_err("Found disabled bulk EP desc (epid:%d error:%d)\n",
  4107. + epid, IO_EXTRACT(R_USB_EPT_DATA, error_code, epid_data));
  4108. + }
  4109. + } else {
  4110. + tc_dbg("Ignoring In Bulk epid:%d, let RX interrupt handle it\n", epid);
  4111. + }
  4112. + }
  4113. +
  4114. + local_irq_restore(flags);
  4115. +}
  4116. +
  4117. +static void check_finished_ctrl_tx_epids(struct usb_hcd *hcd) {
  4118. + unsigned long flags;
  4119. + int epid;
  4120. + struct urb *urb;
  4121. + struct crisv10_urb_priv * urb_priv;
  4122. + __u32 epid_data;
  4123. +
  4124. + /* Protect TxEPList */
  4125. + local_irq_save(flags);
  4126. +
  4127. + for (epid = 0; epid < NBR_OF_EPIDS; epid++) {
  4128. + if(epid == DUMMY_EPID)
  4129. + continue;
  4130. +
  4131. + /* A finished EP descriptor is disabled and has a valid sub pointer */
  4132. + if (!(TxCtrlEPList[epid].command & IO_MASK(USB_EP_command, enable)) &&
  4133. + (TxCtrlEPList[epid].sub != 0)) {
  4134. +
  4135. + /* Get the active URB for this epid */
  4136. + urb = activeUrbList[epid];
  4137. +
  4138. + if(urb == NULL) {
  4139. + tc_warn("Found finished Ctrl epid:%d with no active URB\n", epid);
  4140. + continue;
  4141. + }
  4142. +
  4143. + /* Sanity checks */
  4144. + ASSERT(usb_pipein(urb->pipe));
  4145. + urb_priv = (struct crisv10_urb_priv *)urb->hcpriv;
  4146. + ASSERT(urb_priv);
  4147. + if (phys_to_virt(TxCtrlEPList[epid].sub) != urb_priv->last_sb) {
  4148. + tc_err("Endpoint got disabled before reaching last sb\n");
  4149. + }
  4150. +
  4151. + epid_data = etrax_epid_get(epid);
  4152. + if (IO_EXTRACT(R_USB_EPT_DATA, error_code, epid_data) ==
  4153. + IO_STATE_VALUE(R_USB_EPT_DATA, error_code, no_error)) {
  4154. + /* This means that the endpoint has no error, is disabled
  4155. + and had inserted traffic, i.e. transfer successfully completed. */
  4156. +
  4157. + /* Check if RX-interrupt for In Ctrl has been processed before
  4158. + finishing the URB */
  4159. + if(urb_priv->ctrl_rx_done) {
  4160. + tc_dbg("Finishing In Ctrl URB:0x%x[%d] in tx_interrupt\n",
  4161. + (unsigned int)urb, urb_priv->urb_num);
  4162. + tc_finish_urb(hcd, urb, 0);
  4163. + } else {
  4164. + /* If we get zout descriptor interrupt before RX was done for a
  4165. + In Ctrl transfer, then we flag that and it will be finished
  4166. + in the RX-Interrupt */
  4167. + urb_priv->ctrl_zout_done = 1;
  4168. + tc_dbg("Got zout descr interrupt before RX interrupt\n");
  4169. + }
  4170. + } else {
  4171. + /* Shouldn't happen. We expect errors to be caught by epid
  4172. + attention. */
  4173. + tc_err("Found disabled Ctrl EP desc (epid:%d URB:0x%x[%d]) error_code:%d\n", epid, (unsigned int)urb, urb_priv->urb_num, IO_EXTRACT(R_USB_EPT_DATA, error_code, epid_data));
  4174. + __dump_ep_desc(&(TxCtrlEPList[epid]));
  4175. + __dump_ept_data(epid);
  4176. + }
  4177. + }
  4178. + }
  4179. + local_irq_restore(flags);
  4180. +}
  4181. +
  4182. + /* hinko ignore usb_pipeisoc */
  4183. +#if 0
  4184. +/* This function goes through all epids that are setup for Out Isoc transfers
  4185. + and marks (isoc_out_done) all queued URBs that the DMA has finished
  4186. + transfer for.
  4187. + No URB completetion is done here to make interrupt routine return quickly.
  4188. + URBs are completed later with help of complete_isoc_bottom_half() that
  4189. + becomes schedules when this functions is finished. */
  4190. +static void check_finished_isoc_tx_epids(void) {
  4191. + unsigned long flags;
  4192. + int epid;
  4193. + struct urb *urb;
  4194. + struct crisv10_urb_priv * urb_priv;
  4195. + struct USB_SB_Desc* sb_desc;
  4196. + int epid_done;
  4197. +
  4198. + /* Protect TxIsocEPList */
  4199. + local_irq_save(flags);
  4200. +
  4201. + for (epid = 0; epid < NBR_OF_EPIDS; epid++) {
  4202. + if (TxIsocEPList[epid].sub == 0 || epid == INVALID_EPID ||
  4203. + !epid_out_traffic(epid)) {
  4204. + /* Nothing here to see. */
  4205. + continue;
  4206. + }
  4207. + ASSERT(epid_inuse(epid));
  4208. + ASSERT(epid_isoc(epid));
  4209. +
  4210. + sb_desc = phys_to_virt(TxIsocEPList[epid].sub);
  4211. + /* Find the last descriptor of the currently active URB for this ep.
  4212. + This is the first descriptor in the sub list marked for a descriptor
  4213. + interrupt. */
  4214. + while (sb_desc && !IO_EXTRACT(USB_SB_command, intr, sb_desc->command)) {
  4215. + sb_desc = sb_desc->next ? phys_to_virt(sb_desc->next) : 0;
  4216. + }
  4217. + ASSERT(sb_desc);
  4218. +
  4219. + isoc_dbg("Descr IRQ checking epid:%d sub:0x%x intr:0x%x\n",
  4220. + epid, (unsigned int)phys_to_virt(TxIsocEPList[epid].sub),
  4221. + (unsigned int)sb_desc);
  4222. +
  4223. + urb = activeUrbList[epid];
  4224. + if(urb == NULL) {
  4225. + isoc_err("Isoc Descr irq on epid:%d with no active URB\n", epid);
  4226. + continue;
  4227. + }
  4228. +
  4229. + epid_done = 0;
  4230. + while(urb && !epid_done) {
  4231. + /* Sanity check. */
  4232. + ASSERT(usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS);
  4233. + ASSERT(usb_pipeout(urb->pipe));
  4234. +
  4235. + urb_priv = (struct crisv10_urb_priv *)urb->hcpriv;
  4236. + ASSERT(urb_priv);
  4237. + ASSERT(urb_priv->urb_state == STARTED ||
  4238. + urb_priv->urb_state == UNLINK);
  4239. +
  4240. + if (sb_desc != urb_priv->last_sb) {
  4241. + /* This urb has been sent. */
  4242. + urb_priv->isoc_out_done = 1;
  4243. +
  4244. + } else { /* Found URB that has last_sb as the interrupt reason */
  4245. +
  4246. + /* Check if EP has been disabled, meaning that all transfers are done*/
  4247. + if(!(TxIsocEPList[epid].command & IO_MASK(USB_EP_command, enable))) {
  4248. + ASSERT((sb_desc->command & IO_MASK(USB_SB_command, eol)) ==
  4249. + IO_STATE(USB_SB_command, eol, yes));
  4250. + ASSERT(sb_desc->next == 0);
  4251. + urb_priv->isoc_out_done = 1;
  4252. + } else {
  4253. + isoc_dbg("Skipping URB:0x%x[%d] because EP not disabled yet\n",
  4254. + (unsigned int)urb, urb_priv->urb_num);
  4255. + }
  4256. + /* Stop looking any further in queue */
  4257. + epid_done = 1;
  4258. + }
  4259. +
  4260. + if (!epid_done) {
  4261. + if(urb == activeUrbList[epid]) {
  4262. + urb = urb_list_first(epid);
  4263. + } else {
  4264. + urb = urb_list_next(urb, epid);
  4265. + }
  4266. + }
  4267. + } /* END: while(urb && !epid_done) */
  4268. + }
  4269. +
  4270. + local_irq_restore(flags);
  4271. +}
  4272. +
  4273. +
  4274. +/* This is where the Out Isoc URBs are realy completed. This function is
  4275. + scheduled from tc_dma_tx_interrupt() when one or more Out Isoc transfers
  4276. + are done. This functions completes all URBs earlier marked with
  4277. + isoc_out_done by fast interrupt routine check_finished_isoc_tx_epids() */
  4278. +
  4279. +static void complete_isoc_bottom_half(void *data) {
  4280. + struct crisv10_isoc_complete_data *comp_data;
  4281. + struct usb_iso_packet_descriptor *packet;
  4282. + struct crisv10_urb_priv * urb_priv;
  4283. + unsigned long flags;
  4284. + struct urb* urb;
  4285. + int epid_done;
  4286. + int epid;
  4287. + int i;
  4288. +
  4289. + comp_data = (struct crisv10_isoc_complete_data*)data;
  4290. +
  4291. + local_irq_save(flags);
  4292. +
  4293. + for (epid = 0; epid < NBR_OF_EPIDS - 1; epid++) {
  4294. + if(!epid_inuse(epid) || !epid_isoc(epid) || !epid_out_traffic(epid) || epid == DUMMY_EPID) {
  4295. + /* Only check valid Out Isoc epids */
  4296. + continue;
  4297. + }
  4298. +
  4299. + isoc_dbg("Isoc bottom-half checking epid:%d, sub:0x%x\n", epid,
  4300. + (unsigned int)phys_to_virt(TxIsocEPList[epid].sub));
  4301. +
  4302. + /* The descriptor interrupt handler has marked all transmitted Out Isoc
  4303. + URBs with isoc_out_done. Now we traverse all epids and for all that
  4304. + have out Isoc traffic we traverse its URB list and complete the
  4305. + transmitted URBs. */
  4306. + epid_done = 0;
  4307. + while (!epid_done) {
  4308. +
  4309. + /* Get the active urb (if any) */
  4310. + urb = activeUrbList[epid];
  4311. + if (urb == 0) {
  4312. + isoc_dbg("No active URB on epid:%d anymore\n", epid);
  4313. + epid_done = 1;
  4314. + continue;
  4315. + }
  4316. +
  4317. + /* Sanity check. */
  4318. + ASSERT(usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS);
  4319. + ASSERT(usb_pipeout(urb->pipe));
  4320. +
  4321. + urb_priv = (struct crisv10_urb_priv *)urb->hcpriv;
  4322. + ASSERT(urb_priv);
  4323. +
  4324. + if (!(urb_priv->isoc_out_done)) {
  4325. + /* We have reached URB that isn't flaged done yet, stop traversing. */
  4326. + isoc_dbg("Stoped traversing Out Isoc URBs on epid:%d"
  4327. + " before not yet flaged URB:0x%x[%d]\n",
  4328. + epid, (unsigned int)urb, urb_priv->urb_num);
  4329. + epid_done = 1;
  4330. + continue;
  4331. + }
  4332. +
  4333. + /* This urb has been sent. */
  4334. + isoc_dbg("Found URB:0x%x[%d] that is flaged isoc_out_done\n",
  4335. + (unsigned int)urb, urb_priv->urb_num);
  4336. +
  4337. + /* Set ok on transfered packets for this URB and finish it */
  4338. + for (i = 0; i < urb->number_of_packets; i++) {
  4339. + packet = &urb->iso_frame_desc[i];
  4340. + packet->status = 0;
  4341. + packet->actual_length = packet->length;
  4342. + }
  4343. + urb_priv->isoc_packet_counter = urb->number_of_packets;
  4344. + tc_finish_urb(comp_data->hcd, urb, 0);
  4345. +
  4346. + } /* END: while(!epid_done) */
  4347. + } /* END: for(epid...) */
  4348. +
  4349. + local_irq_restore(flags);
  4350. + kmem_cache_free(isoc_compl_cache, comp_data);
  4351. +}
  4352. +#endif
  4353. +
  4354. +static void check_finished_intr_tx_epids(struct usb_hcd *hcd) {
  4355. + unsigned long flags;
  4356. + int epid;
  4357. + struct urb *urb;
  4358. + struct crisv10_urb_priv * urb_priv;
  4359. + volatile struct USB_EP_Desc *curr_ep; /* Current EP, the iterator. */
  4360. + volatile struct USB_EP_Desc *next_ep; /* The EP after current. */
  4361. +
  4362. + /* Protect TxintrEPList */
  4363. + local_irq_save(flags);
  4364. +
  4365. + for (epid = 0; epid < NBR_OF_EPIDS; epid++) {
  4366. + if(!epid_inuse(epid) || !epid_intr(epid) || !epid_out_traffic(epid)) {
  4367. + /* Nothing to see on this epid. Only check valid Out Intr epids */
  4368. + continue;
  4369. + }
  4370. +
  4371. + urb = activeUrbList[epid];
  4372. + if(urb == 0) {
  4373. + intr_warn("Found Out Intr epid:%d with no active URB\n", epid);
  4374. + continue;
  4375. + }
  4376. +
  4377. + /* Sanity check. */
  4378. + ASSERT(usb_pipetype(urb->pipe) == PIPE_INTERRUPT);
  4379. + ASSERT(usb_pipeout(urb->pipe));
  4380. +
  4381. + urb_priv = (struct crisv10_urb_priv *)urb->hcpriv;
  4382. + ASSERT(urb_priv);
  4383. +
  4384. + /* Go through EPs between first and second sof-EP. It's here Out Intr EPs
  4385. + are inserted.*/
  4386. + curr_ep = &TxIntrEPList[0];
  4387. + do {
  4388. + next_ep = (struct USB_EP_Desc *)phys_to_virt(curr_ep->next);
  4389. + if(next_ep == urb_priv->intr_ep_pool[0]) {
  4390. + /* We found the Out Intr EP for this epid */
  4391. +
  4392. + /* Disable it so it doesn't get processed again */
  4393. + next_ep->command &= ~IO_MASK(USB_EP_command, enable);
  4394. +
  4395. + /* Finish the active Out Intr URB with status OK */
  4396. + tc_finish_urb(hcd, urb, 0);
  4397. + }
  4398. + curr_ep = phys_to_virt(curr_ep->next);
  4399. + } while (curr_ep != &TxIntrEPList[1]);
  4400. +
  4401. + }
  4402. + local_irq_restore(flags);
  4403. +}
  4404. +
  4405. +/* Interrupt handler for DMA8/IRQ24 with subchannels (called from hardware intr) */
  4406. +static irqreturn_t tc_dma_tx_interrupt(int irq, void *vhc) {
  4407. + struct usb_hcd *hcd = (struct usb_hcd*)vhc;
  4408. + ASSERT(hcd);
  4409. +
  4410. + if (*R_IRQ_READ2 & IO_MASK(R_IRQ_READ2, dma8_sub0_descr)) {
  4411. + /* Clear this interrupt */
  4412. + *R_DMA_CH8_SUB0_CLR_INTR = IO_STATE(R_DMA_CH8_SUB0_CLR_INTR, clr_descr, do);
  4413. + restart_dma8_sub0();
  4414. + }
  4415. +
  4416. + if (*R_IRQ_READ2 & IO_MASK(R_IRQ_READ2, dma8_sub1_descr)) {
  4417. + /* Clear this interrupt */
  4418. + *R_DMA_CH8_SUB1_CLR_INTR = IO_STATE(R_DMA_CH8_SUB1_CLR_INTR, clr_descr, do);
  4419. + check_finished_ctrl_tx_epids(hcd);
  4420. + }
  4421. +
  4422. + if (*R_IRQ_READ2 & IO_MASK(R_IRQ_READ2, dma8_sub2_descr)) {
  4423. + /* Clear this interrupt */
  4424. + *R_DMA_CH8_SUB2_CLR_INTR = IO_STATE(R_DMA_CH8_SUB2_CLR_INTR, clr_descr, do);
  4425. + check_finished_intr_tx_epids(hcd);
  4426. + }
  4427. +
  4428. + /* hinko ignore usb_pipeisoc */
  4429. +#if 0
  4430. + if (*R_IRQ_READ2 & IO_MASK(R_IRQ_READ2, dma8_sub3_descr)) {
  4431. + struct crisv10_isoc_complete_data* comp_data;
  4432. +
  4433. + /* Flag done Out Isoc for later completion */
  4434. + check_finished_isoc_tx_epids();
  4435. +
  4436. + /* Clear this interrupt */
  4437. + *R_DMA_CH8_SUB3_CLR_INTR = IO_STATE(R_DMA_CH8_SUB3_CLR_INTR, clr_descr, do);
  4438. + /* Schedule bottom half of Out Isoc completion function. This function
  4439. + finishes the URBs marked with isoc_out_done */
  4440. + comp_data = (struct crisv10_isoc_complete_data*)
  4441. + kmem_cache_alloc(isoc_compl_cache, GFP_ATOMIC);
  4442. + ASSERT(comp_data != NULL);
  4443. + comp_data ->hcd = hcd;
  4444. +
  4445. + //INIT_WORK(&comp_data->usb_bh, complete_isoc_bottom_half, comp_data);
  4446. + INIT_WORK(&comp_data->usb_bh, complete_isoc_bottom_half);
  4447. + schedule_work(&comp_data->usb_bh);
  4448. + }
  4449. +#endif
  4450. +
  4451. + return IRQ_HANDLED;
  4452. +}
  4453. +
  4454. +/* Interrupt handler for DMA9/IRQ25 (called from hardware intr) */
  4455. +static irqreturn_t tc_dma_rx_interrupt(int irq, void *vhc) {
  4456. + unsigned long flags;
  4457. + struct urb *urb;
  4458. + struct usb_hcd *hcd = (struct usb_hcd*)vhc;
  4459. + struct crisv10_urb_priv *urb_priv;
  4460. + int epid = 0;
  4461. + int real_error;
  4462. +
  4463. + ASSERT(hcd);
  4464. +
  4465. + /* Clear this interrupt. */
  4466. + *R_DMA_CH9_CLR_INTR = IO_STATE(R_DMA_CH9_CLR_INTR, clr_eop, do);
  4467. +
  4468. + /* Custom clear interrupt for this interrupt */
  4469. + /* The reason we cli here is that we call the driver's callback functions. */
  4470. + local_irq_save(flags);
  4471. +
  4472. + /* Note that this while loop assumes that all packets span only
  4473. + one rx descriptor. */
  4474. + while(myNextRxDesc->status & IO_MASK(USB_IN_status, eop)) {
  4475. + epid = IO_EXTRACT(USB_IN_status, epid, myNextRxDesc->status);
  4476. + /* Get the active URB for this epid */
  4477. + urb = activeUrbList[epid];
  4478. +
  4479. + ASSERT(epid_inuse(epid));
  4480. + if (!urb) {
  4481. + dma_err("No urb for epid %d in rx interrupt\n", epid);
  4482. + goto skip_out;
  4483. + }
  4484. +
  4485. + /* Check if any errors on epid */
  4486. + real_error = 0;
  4487. + if (myNextRxDesc->status & IO_MASK(USB_IN_status, error)) {
  4488. + __u32 r_usb_ept_data;
  4489. +
  4490. + if (usb_pipeisoc(urb->pipe)) {
  4491. + r_usb_ept_data = etrax_epid_iso_get(epid);
  4492. + if((r_usb_ept_data & IO_MASK(R_USB_EPT_DATA_ISO, valid)) &&
  4493. + (IO_EXTRACT(R_USB_EPT_DATA_ISO, error_code, r_usb_ept_data) == 0) &&
  4494. + (myNextRxDesc->status & IO_MASK(USB_IN_status, nodata))) {
  4495. + /* Not an error, just a failure to receive an expected iso
  4496. + in packet in this frame. This is not documented
  4497. + in the designers reference. Continue processing.
  4498. + */
  4499. + } else real_error = 1;
  4500. + } else real_error = 1;
  4501. + }
  4502. +
  4503. + if(real_error) {
  4504. + dma_err("Error in RX descr on epid:%d for URB 0x%x",
  4505. + epid, (unsigned int)urb);
  4506. + dump_ept_data(epid);
  4507. + dump_in_desc(myNextRxDesc);
  4508. + goto skip_out;
  4509. + }
  4510. +
  4511. + urb_priv = (struct crisv10_urb_priv *)urb->hcpriv;
  4512. + ASSERT(urb_priv);
  4513. + ASSERT(urb_priv->urb_state == STARTED ||
  4514. + urb_priv->urb_state == UNLINK);
  4515. +
  4516. + if ((usb_pipetype(urb->pipe) == PIPE_BULK) ||
  4517. + (usb_pipetype(urb->pipe) == PIPE_CONTROL) ||
  4518. + (usb_pipetype(urb->pipe) == PIPE_INTERRUPT)) {
  4519. +
  4520. + /* We get nodata for empty data transactions, and the rx descriptor's
  4521. + hw_len field is not valid in that case. No data to copy in other
  4522. + words. */
  4523. + if (myNextRxDesc->status & IO_MASK(USB_IN_status, nodata)) {
  4524. + /* No data to copy */
  4525. + } else {
  4526. + /*
  4527. + dma_dbg("Processing RX for URB:0x%x epid:%d (data:%d ofs:%d)\n",
  4528. + (unsigned int)urb, epid, myNextRxDesc->hw_len,
  4529. + urb_priv->rx_offset);
  4530. + */
  4531. + /* Only copy data if URB isn't flaged to be unlinked*/
  4532. + if(urb_priv->urb_state != UNLINK) {
  4533. + /* Make sure the data fits in the buffer. */
  4534. + if(urb_priv->rx_offset + myNextRxDesc->hw_len
  4535. + <= urb->transfer_buffer_length) {
  4536. +
  4537. + /* Copy the data to URBs buffer */
  4538. + memcpy(urb->transfer_buffer + urb_priv->rx_offset,
  4539. + phys_to_virt(myNextRxDesc->buf), myNextRxDesc->hw_len);
  4540. + urb_priv->rx_offset += myNextRxDesc->hw_len;
  4541. + } else {
  4542. + /* Signal overflow when returning URB */
  4543. + urb->status = -EOVERFLOW;
  4544. + tc_finish_urb_later(hcd, urb, urb->status);
  4545. + }
  4546. + }
  4547. + }
  4548. +
  4549. + /* Check if it was the last packet in the transfer */
  4550. + if (myNextRxDesc->status & IO_MASK(USB_IN_status, eot)) {
  4551. + /* Special handling for In Ctrl URBs. */
  4552. + if(usb_pipecontrol(urb->pipe) && usb_pipein(urb->pipe) &&
  4553. + !(urb_priv->ctrl_zout_done)) {
  4554. + /* Flag that RX part of Ctrl transfer is done. Because zout descr
  4555. + interrupt hasn't happend yet will the URB be finished in the
  4556. + TX-Interrupt. */
  4557. + urb_priv->ctrl_rx_done = 1;
  4558. + tc_dbg("Not finishing In Ctrl URB:0x%x from rx_interrupt, waiting"
  4559. + " for zout\n", (unsigned int)urb);
  4560. + } else {
  4561. + tc_finish_urb(hcd, urb, 0);
  4562. + }
  4563. + }
  4564. + } else { /* ISOC RX */
  4565. + /*
  4566. + isoc_dbg("Processing RX for epid:%d (URB:0x%x) ISOC pipe\n",
  4567. + epid, (unsigned int)urb);
  4568. + */
  4569. +
  4570. + struct usb_iso_packet_descriptor *packet;
  4571. +
  4572. + if (urb_priv->urb_state == UNLINK) {
  4573. + isoc_warn("Ignoring Isoc Rx data for urb being unlinked.\n");
  4574. + goto skip_out;
  4575. + } else if (urb_priv->urb_state == NOT_STARTED) {
  4576. + isoc_err("What? Got Rx data for Isoc urb that isn't started?\n");
  4577. + goto skip_out;
  4578. + }
  4579. +
  4580. + packet = &urb->iso_frame_desc[urb_priv->isoc_packet_counter];
  4581. + ASSERT(packet);
  4582. + packet->status = 0;
  4583. +
  4584. + if (myNextRxDesc->status & IO_MASK(USB_IN_status, nodata)) {
  4585. + /* We get nodata for empty data transactions, and the rx descriptor's
  4586. + hw_len field is not valid in that case. We copy 0 bytes however to
  4587. + stay in synch. */
  4588. + packet->actual_length = 0;
  4589. + } else {
  4590. + packet->actual_length = myNextRxDesc->hw_len;
  4591. + /* Make sure the data fits in the buffer. */
  4592. + ASSERT(packet->actual_length <= packet->length);
  4593. + memcpy(urb->transfer_buffer + packet->offset,
  4594. + phys_to_virt(myNextRxDesc->buf), packet->actual_length);
  4595. + if(packet->actual_length > 0)
  4596. + isoc_dbg("Copied %d bytes, packet %d for URB:0x%x[%d]\n",
  4597. + packet->actual_length, urb_priv->isoc_packet_counter,
  4598. + (unsigned int)urb, urb_priv->urb_num);
  4599. + }
  4600. +
  4601. + /* Increment the packet counter. */
  4602. + urb_priv->isoc_packet_counter++;
  4603. +
  4604. + /* Note that we don't care about the eot field in the rx descriptor's
  4605. + status. It will always be set for isoc traffic. */
  4606. + if (urb->number_of_packets == urb_priv->isoc_packet_counter) {
  4607. + /* Complete the urb with status OK. */
  4608. + tc_finish_urb(hcd, urb, 0);
  4609. + }
  4610. + }
  4611. +
  4612. + skip_out:
  4613. + myNextRxDesc->status = 0;
  4614. + myNextRxDesc->command |= IO_MASK(USB_IN_command, eol);
  4615. + myLastRxDesc->command &= ~IO_MASK(USB_IN_command, eol);
  4616. + myLastRxDesc = myNextRxDesc;
  4617. + myNextRxDesc = phys_to_virt(myNextRxDesc->next);
  4618. + flush_etrax_cache();
  4619. + *R_DMA_CH9_CMD = IO_STATE(R_DMA_CH9_CMD, cmd, restart);
  4620. + }
  4621. +
  4622. + local_irq_restore(flags);
  4623. +
  4624. + return IRQ_HANDLED;
  4625. +}
  4626. +
  4627. +static void tc_bulk_start_timer_func(unsigned long dummy) {
  4628. + /* We might enable an EP descriptor behind the current DMA position when
  4629. + it's about to decide that there are no more bulk traffic and it should
  4630. + stop the bulk channel.
  4631. + Therefore we periodically check if the bulk channel is stopped and there
  4632. + is an enabled bulk EP descriptor, in which case we start the bulk
  4633. + channel. */
  4634. +
  4635. + if (!(*R_DMA_CH8_SUB0_CMD & IO_MASK(R_DMA_CH8_SUB0_CMD, cmd))) {
  4636. + int epid;
  4637. +
  4638. + timer_dbg("bulk_start_timer: Bulk DMA channel not running.\n");
  4639. +
  4640. + for (epid = 0; epid < NBR_OF_EPIDS; epid++) {
  4641. + if (TxBulkEPList[epid].command & IO_MASK(USB_EP_command, enable)) {
  4642. + timer_warn("Found enabled EP for epid %d, starting bulk channel.\n",
  4643. + epid);
  4644. + restart_dma8_sub0();
  4645. +
  4646. + /* Restart the bulk eot timer since we just started the bulk channel.*/
  4647. + mod_timer(&bulk_eot_timer, jiffies + BULK_EOT_TIMER_INTERVAL);
  4648. +
  4649. + /* No need to search any further. */
  4650. + break;
  4651. + }
  4652. + }
  4653. + } else {
  4654. + timer_dbg("bulk_start_timer: Bulk DMA channel running.\n");
  4655. + }
  4656. +}
  4657. +
  4658. +static void tc_bulk_eot_timer_func(unsigned long dummy) {
  4659. + struct usb_hcd *hcd = (struct usb_hcd*)dummy;
  4660. + ASSERT(hcd);
  4661. + /* Because of a race condition in the top half, we might miss a bulk eot.
  4662. + This timer "simulates" a bulk eot if we don't get one for a while,
  4663. + hopefully correcting the situation. */
  4664. + timer_dbg("bulk_eot_timer timed out.\n");
  4665. + check_finished_bulk_tx_epids(hcd, 1);
  4666. +}
  4667. +
  4668. +
  4669. +/*************************************************************/
  4670. +/*************************************************************/
  4671. +/* Device driver block */
  4672. +/*************************************************************/
  4673. +/*************************************************************/
  4674. +
  4675. +/* Forward declarations for device driver functions */
  4676. +static int devdrv_hcd_probe(struct device *);
  4677. +static int devdrv_hcd_remove(struct device *);
  4678. +#ifdef CONFIG_PM
  4679. +static int devdrv_hcd_suspend(struct device *, u32, u32);
  4680. +static int devdrv_hcd_resume(struct device *, u32);
  4681. +#endif /* CONFIG_PM */
  4682. +
  4683. +/* the device */
  4684. +static struct platform_device *devdrv_hc_platform_device;
  4685. +
  4686. +/* device driver interface */
  4687. +static struct device_driver devdrv_hc_device_driver = {
  4688. + .name = (char *) hc_name,
  4689. + .bus = &platform_bus_type,
  4690. +
  4691. + .probe = devdrv_hcd_probe,
  4692. + .remove = devdrv_hcd_remove,
  4693. +
  4694. +#ifdef CONFIG_PM
  4695. + .suspend = devdrv_hcd_suspend,
  4696. + .resume = devdrv_hcd_resume,
  4697. +#endif /* CONFIG_PM */
  4698. +};
  4699. +
  4700. +/* initialize the host controller and driver */
  4701. +static int __init_or_module devdrv_hcd_probe(struct device *dev)
  4702. +{
  4703. + struct usb_hcd *hcd;
  4704. + struct crisv10_hcd *crisv10_hcd;
  4705. + int retval;
  4706. +
  4707. + /* Check DMA burst length */
  4708. + if(IO_EXTRACT(R_BUS_CONFIG, dma_burst, *R_BUS_CONFIG) !=
  4709. + IO_STATE(R_BUS_CONFIG, dma_burst, burst32)) {
  4710. + devdrv_err("Invalid DMA burst length in Etrax 100LX,"
  4711. + " needs to be 32\n");
  4712. + return -EPERM;
  4713. + }
  4714. +
  4715. + hcd = usb_create_hcd(&crisv10_hc_driver, dev, dev->bus_id);
  4716. + if (!hcd)
  4717. + return -ENOMEM;
  4718. +
  4719. + crisv10_hcd = hcd_to_crisv10_hcd(hcd);
  4720. + spin_lock_init(&crisv10_hcd->lock);
  4721. + crisv10_hcd->num_ports = num_ports();
  4722. + crisv10_hcd->running = 0;
  4723. +
  4724. + dev_set_drvdata(dev, crisv10_hcd);
  4725. +
  4726. + devdrv_dbg("ETRAX USB IRQs HC:%d RX:%d TX:%d\n", ETRAX_USB_HC_IRQ,
  4727. + ETRAX_USB_RX_IRQ, ETRAX_USB_TX_IRQ);
  4728. +
  4729. + /* Print out chip version read from registers */
  4730. + int rev_maj = *R_USB_REVISION & IO_MASK(R_USB_REVISION, major);
  4731. + int rev_min = *R_USB_REVISION & IO_MASK(R_USB_REVISION, minor);
  4732. + if(rev_min == 0) {
  4733. + devdrv_info("Etrax 100LX USB Revision %d v1,2\n", rev_maj);
  4734. + } else {
  4735. + devdrv_info("Etrax 100LX USB Revision %d v%d\n", rev_maj, rev_min);
  4736. + }
  4737. +
  4738. + devdrv_info("Bulk timer interval, start:%d eot:%d\n",
  4739. + BULK_START_TIMER_INTERVAL,
  4740. + BULK_EOT_TIMER_INTERVAL);
  4741. +
  4742. +
  4743. + /* Init root hub data structures */
  4744. + if(rh_init()) {
  4745. + devdrv_err("Failed init data for Root Hub\n");
  4746. + retval = -ENOMEM;
  4747. + }
  4748. +
  4749. + if(port_in_use(0)) {
  4750. + if (cris_request_io_interface(if_usb_1, "ETRAX100LX USB-HCD")) {
  4751. + printk(KERN_CRIT "usb-host: request IO interface usb1 failed");
  4752. + retval = -EBUSY;
  4753. + goto out;
  4754. + }
  4755. + devdrv_info("Claimed interface for USB physical port 1\n");
  4756. + }
  4757. + if(port_in_use(1)) {
  4758. + if (cris_request_io_interface(if_usb_2, "ETRAX100LX USB-HCD")) {
  4759. + /* Free first interface if second failed to be claimed */
  4760. + if(port_in_use(0)) {
  4761. + cris_free_io_interface(if_usb_1);
  4762. + }
  4763. + printk(KERN_CRIT "usb-host: request IO interface usb2 failed");
  4764. + retval = -EBUSY;
  4765. + goto out;
  4766. + }
  4767. + devdrv_info("Claimed interface for USB physical port 2\n");
  4768. + }
  4769. +
  4770. + /* Init transfer controller structs and locks */
  4771. + if((retval = tc_init(hcd)) != 0) {
  4772. + goto out;
  4773. + }
  4774. +
  4775. + /* Attach interrupt functions for DMA and init DMA controller */
  4776. + if((retval = tc_dma_init(hcd)) != 0) {
  4777. + goto out;
  4778. + }
  4779. +
  4780. + /* Attach the top IRQ handler for USB controller interrupts */
  4781. + if (request_irq(ETRAX_USB_HC_IRQ, crisv10_hcd_top_irq, 0,
  4782. + "ETRAX 100LX built-in USB (HC)", hcd)) {
  4783. + err("Could not allocate IRQ %d for USB", ETRAX_USB_HC_IRQ);
  4784. + retval = -EBUSY;
  4785. + goto out;
  4786. + }
  4787. +
  4788. + /* iso_eof is only enabled when isoc traffic is running. */
  4789. + *R_USB_IRQ_MASK_SET =
  4790. + /* IO_STATE(R_USB_IRQ_MASK_SET, iso_eof, set) | */
  4791. + IO_STATE(R_USB_IRQ_MASK_SET, bulk_eot, set) |
  4792. + IO_STATE(R_USB_IRQ_MASK_SET, epid_attn, set) |
  4793. + IO_STATE(R_USB_IRQ_MASK_SET, port_status, set) |
  4794. + IO_STATE(R_USB_IRQ_MASK_SET, ctl_status, set);
  4795. +
  4796. +
  4797. + crisv10_ready_wait();
  4798. + /* Reset the USB interface. */
  4799. + *R_USB_COMMAND =
  4800. + IO_STATE(R_USB_COMMAND, port_sel, nop) |
  4801. + IO_STATE(R_USB_COMMAND, port_cmd, reset) |
  4802. + IO_STATE(R_USB_COMMAND, ctrl_cmd, reset);
  4803. +
  4804. + /* Designer's Reference, p. 8 - 10 says we should Initate R_USB_FM_PSTART to
  4805. + 0x2A30 (10800), to guarantee that control traffic gets 10% of the
  4806. + bandwidth, and periodic transfer may allocate the rest (90%).
  4807. + This doesn't work though.
  4808. + The value 11960 is chosen to be just after the SOF token, with a couple
  4809. + of bit times extra for possible bit stuffing. */
  4810. + *R_USB_FM_PSTART = IO_FIELD(R_USB_FM_PSTART, value, 11960);
  4811. +
  4812. + crisv10_ready_wait();
  4813. + /* Configure the USB interface as a host controller. */
  4814. + *R_USB_COMMAND =
  4815. + IO_STATE(R_USB_COMMAND, port_sel, nop) |
  4816. + IO_STATE(R_USB_COMMAND, port_cmd, reset) |
  4817. + IO_STATE(R_USB_COMMAND, ctrl_cmd, host_config);
  4818. +
  4819. +
  4820. + /* Check so controller not busy before enabling ports */
  4821. + crisv10_ready_wait();
  4822. +
  4823. + /* Enable selected USB ports */
  4824. + if(port_in_use(0)) {
  4825. + *R_USB_PORT1_DISABLE = IO_STATE(R_USB_PORT1_DISABLE, disable, no);
  4826. + } else {
  4827. + *R_USB_PORT1_DISABLE = IO_STATE(R_USB_PORT1_DISABLE, disable, yes);
  4828. + }
  4829. + if(port_in_use(1)) {
  4830. + *R_USB_PORT2_DISABLE = IO_STATE(R_USB_PORT2_DISABLE, disable, no);
  4831. + } else {
  4832. + *R_USB_PORT2_DISABLE = IO_STATE(R_USB_PORT2_DISABLE, disable, yes);
  4833. + }
  4834. +
  4835. + crisv10_ready_wait();
  4836. + /* Start processing of USB traffic. */
  4837. + *R_USB_COMMAND =
  4838. + IO_STATE(R_USB_COMMAND, port_sel, nop) |
  4839. + IO_STATE(R_USB_COMMAND, port_cmd, reset) |
  4840. + IO_STATE(R_USB_COMMAND, ctrl_cmd, host_run);
  4841. +
  4842. + /* Do not continue probing initialization before USB interface is done */
  4843. + crisv10_ready_wait();
  4844. +
  4845. + /* Register our Host Controller to USB Core
  4846. + * Finish the remaining parts of generic HCD initialization: allocate the
  4847. + * buffers of consistent memory, register the bus
  4848. + * and call the driver's reset() and start() routines. */
  4849. + retval = usb_add_hcd(hcd, ETRAX_USB_HC_IRQ, IRQF_DISABLED);
  4850. + if (retval != 0) {
  4851. + devdrv_err("Failed registering HCD driver\n");
  4852. + goto out;
  4853. + }
  4854. +
  4855. + return 0;
  4856. +
  4857. + out:
  4858. + devdrv_hcd_remove(dev);
  4859. + return retval;
  4860. +}
  4861. +
  4862. +
  4863. +/* cleanup after the host controller and driver */
  4864. +static int __init_or_module devdrv_hcd_remove(struct device *dev)
  4865. +{
  4866. + struct crisv10_hcd *crisv10_hcd = dev_get_drvdata(dev);
  4867. + struct usb_hcd *hcd;
  4868. +
  4869. + if (!crisv10_hcd)
  4870. + return 0;
  4871. + hcd = crisv10_hcd_to_hcd(crisv10_hcd);
  4872. +
  4873. +
  4874. + /* Stop USB Controller in Etrax 100LX */
  4875. + crisv10_hcd_reset(hcd);
  4876. +
  4877. + usb_remove_hcd(hcd);
  4878. + devdrv_dbg("Removed HCD from USB Core\n");
  4879. +
  4880. + /* Free USB Controller IRQ */
  4881. + free_irq(ETRAX_USB_HC_IRQ, NULL);
  4882. +
  4883. + /* Free resources */
  4884. + tc_dma_destroy();
  4885. + tc_destroy();
  4886. +
  4887. +
  4888. + if(port_in_use(0)) {
  4889. + cris_free_io_interface(if_usb_1);
  4890. + }
  4891. + if(port_in_use(1)) {
  4892. + cris_free_io_interface(if_usb_2);
  4893. + }
  4894. +
  4895. + devdrv_dbg("Freed all claimed resources\n");
  4896. +
  4897. + return 0;
  4898. +}
  4899. +
  4900. +
  4901. +#ifdef CONFIG_PM
  4902. +
  4903. +static int devdrv_hcd_suspend(struct usb_hcd *hcd, u32 state, u32 level)
  4904. +{
  4905. + return 0; /* no-op for now */
  4906. +}
  4907. +
  4908. +static int devdrv_hcd_resume(struct usb_hcd *hcd, u32 level)
  4909. +{
  4910. + return 0; /* no-op for now */
  4911. +}
  4912. +
  4913. +#endif /* CONFIG_PM */
  4914. +
  4915. +
  4916. +
  4917. +/*************************************************************/
  4918. +/*************************************************************/
  4919. +/* Module block */
  4920. +/*************************************************************/
  4921. +/*************************************************************/
  4922. +
  4923. +/* register driver */
  4924. +static int __init module_hcd_init(void)
  4925. +{
  4926. +
  4927. + if (usb_disabled())
  4928. + return -ENODEV;
  4929. +
  4930. + /* Here we select enabled ports by following defines created from
  4931. + menuconfig */
  4932. +#ifndef CONFIG_ETRAX_USB_HOST_PORT1
  4933. + ports &= ~(1<<0);
  4934. +#endif
  4935. +#ifndef CONFIG_ETRAX_USB_HOST_PORT2
  4936. + ports &= ~(1<<1);
  4937. +#endif
  4938. +
  4939. + printk(KERN_INFO "%s version "VERSION" "COPYRIGHT"\n", product_desc);
  4940. +
  4941. + devdrv_hc_platform_device =
  4942. + platform_device_register_simple((char *) hc_name, 0, NULL, 0);
  4943. +
  4944. + if (IS_ERR(devdrv_hc_platform_device))
  4945. + return PTR_ERR(devdrv_hc_platform_device);
  4946. + return driver_register(&devdrv_hc_device_driver);
  4947. + /*
  4948. + * Note that we do not set the DMA mask for the device,
  4949. + * i.e. we pretend that we will use PIO, since no specific
  4950. + * allocation routines are needed for DMA buffers. This will
  4951. + * cause the HCD buffer allocation routines to fall back to
  4952. + * kmalloc().
  4953. + */
  4954. +}
  4955. +
  4956. +/* unregister driver */
  4957. +static void __exit module_hcd_exit(void)
  4958. +{
  4959. + driver_unregister(&devdrv_hc_device_driver);
  4960. +}
  4961. +
  4962. +
  4963. +/* Module hooks */
  4964. +module_init(module_hcd_init);
  4965. +module_exit(module_hcd_exit);
  4966. --- /dev/null
  4967. +++ b/drivers/usb/host/hc-crisv10.h
  4968. @@ -0,0 +1,331 @@
  4969. +#ifndef __LINUX_ETRAX_USB_H
  4970. +#define __LINUX_ETRAX_USB_H
  4971. +
  4972. +#include <linux/types.h>
  4973. +#include <linux/list.h>
  4974. +
  4975. +struct USB_IN_Desc {
  4976. + volatile __u16 sw_len;
  4977. + volatile __u16 command;
  4978. + volatile unsigned long next;
  4979. + volatile unsigned long buf;
  4980. + volatile __u16 hw_len;
  4981. + volatile __u16 status;
  4982. +};
  4983. +
  4984. +struct USB_SB_Desc {
  4985. + volatile __u16 sw_len;
  4986. + volatile __u16 command;
  4987. + volatile unsigned long next;
  4988. + volatile unsigned long buf;
  4989. +};
  4990. +
  4991. +struct USB_EP_Desc {
  4992. + volatile __u16 hw_len;
  4993. + volatile __u16 command;
  4994. + volatile unsigned long sub;
  4995. + volatile unsigned long next;
  4996. +};
  4997. +
  4998. +
  4999. +/* Root Hub port status struct */
  5000. +struct crisv10_rh {
  5001. + volatile __u16 wPortChange[2];
  5002. + volatile __u16 wPortStatusPrev[2];
  5003. +};
  5004. +
  5005. +/* HCD description */
  5006. +struct crisv10_hcd {
  5007. + spinlock_t lock;
  5008. + __u8 num_ports;
  5009. + __u8 running;
  5010. +};
  5011. +
  5012. +
  5013. +/* Endpoint HC private data description */
  5014. +struct crisv10_ep_priv {
  5015. + int epid;
  5016. +};
  5017. +
  5018. +/* Additional software state info for a USB Controller epid */
  5019. +struct etrax_epid {
  5020. + __u8 inuse; /* !0 = setup in Etrax and used for a endpoint */
  5021. + __u8 disabled; /* !0 = Temporarly disabled to avoid resubmission */
  5022. + __u8 type; /* Setup as: PIPE_BULK, PIPE_CONTROL ... */
  5023. + __u8 out_traffic; /* !0 = This epid is for out traffic */
  5024. +};
  5025. +
  5026. +/* Struct to hold information of scheduled later URB completion */
  5027. +struct urb_later_data {
  5028. +// struct work_struct ws;
  5029. + struct delayed_work ws;
  5030. + struct usb_hcd *hcd;
  5031. + struct urb *urb;
  5032. + int urb_num;
  5033. + int status;
  5034. +};
  5035. +
  5036. +
  5037. +typedef enum {
  5038. + STARTED,
  5039. + NOT_STARTED,
  5040. + UNLINK,
  5041. +} crisv10_urb_state_t;
  5042. +
  5043. +
  5044. +struct crisv10_urb_priv {
  5045. + /* Sequence number for this URB. Every new submited URB gets this from
  5046. + a incrementing counter. Used when a URB is scheduled for later finish to
  5047. + be sure that the intended URB hasn't already been completed (device
  5048. + drivers has a tendency to reuse URBs once they are completed, causing us
  5049. + to not be able to single old ones out only based on the URB pointer.) */
  5050. + __u32 urb_num;
  5051. +
  5052. + /* The first_sb field is used for freeing all SB descriptors belonging
  5053. + to an urb. The corresponding ep descriptor's sub pointer cannot be
  5054. + used for this since the DMA advances the sub pointer as it processes
  5055. + the sb list. */
  5056. + struct USB_SB_Desc *first_sb;
  5057. +
  5058. + /* The last_sb field referes to the last SB descriptor that belongs to
  5059. + this urb. This is important to know so we can free the SB descriptors
  5060. + that ranges between first_sb and last_sb. */
  5061. + struct USB_SB_Desc *last_sb;
  5062. +
  5063. + /* The rx_offset field is used in ctrl and bulk traffic to keep track
  5064. + of the offset in the urb's transfer_buffer where incoming data should be
  5065. + copied to. */
  5066. + __u32 rx_offset;
  5067. +
  5068. + /* Counter used in isochronous transfers to keep track of the
  5069. + number of packets received/transmitted. */
  5070. + __u32 isoc_packet_counter;
  5071. +
  5072. + /* Flag that marks if this Isoc Out URB has finished it's transfer. Used
  5073. + because several URBs can be finished before list is processed */
  5074. + __u8 isoc_out_done;
  5075. +
  5076. + /* This field is used to pass information about the urb's current state
  5077. + between the various interrupt handlers (thus marked volatile). */
  5078. + volatile crisv10_urb_state_t urb_state;
  5079. +
  5080. + /* In Ctrl transfers consist of (at least) 3 packets: SETUP, IN and ZOUT.
  5081. + When DMA8 sub-channel 2 has processed the SB list for this sequence we
  5082. + get a interrupt. We also get a interrupt for In transfers and which
  5083. + one of these interrupts that comes first depends of data size and device.
  5084. + To be sure that we have got both interrupts before we complete the URB
  5085. + we have these to flags that shows which part that has completed.
  5086. + We can then check when we get one of the interrupts that if the other has
  5087. + occured it's safe for us to complete the URB, otherwise we set appropriate
  5088. + flag and do the completion when we get the other interrupt. */
  5089. + volatile unsigned char ctrl_zout_done;
  5090. + volatile unsigned char ctrl_rx_done;
  5091. +
  5092. + /* Connection between the submitted urb and ETRAX epid number */
  5093. + __u8 epid;
  5094. +
  5095. + /* The rx_data_list field is used for periodic traffic, to hold
  5096. + received data for later processing in the the complete_urb functions,
  5097. + where the data us copied to the urb's transfer_buffer. Basically, we
  5098. + use this intermediate storage because we don't know when it's safe to
  5099. + reuse the transfer_buffer (FIXME?). */
  5100. + struct list_head rx_data_list;
  5101. +
  5102. +
  5103. + /* The interval time rounded up to closest 2^N */
  5104. + int interval;
  5105. +
  5106. + /* Pool of EP descriptors needed if it's a INTR transfer.
  5107. + Amount of EPs in pool correspons to how many INTR that should
  5108. + be inserted in TxIntrEPList (max 128, defined by MAX_INTR_INTERVAL) */
  5109. + struct USB_EP_Desc* intr_ep_pool[128];
  5110. +
  5111. + /* The mount of EPs allocated for this INTR URB */
  5112. + int intr_ep_pool_length;
  5113. +
  5114. + /* Pointer to info struct if URB is scheduled to be finished later */
  5115. + struct urb_later_data* later_data;
  5116. +};
  5117. +
  5118. +
  5119. +/* This struct is for passing data from the top half to the bottom half irq
  5120. + handlers */
  5121. +struct crisv10_irq_reg {
  5122. + struct usb_hcd* hcd;
  5123. + __u32 r_usb_epid_attn;
  5124. + __u8 r_usb_status;
  5125. + __u16 r_usb_rh_port_status_1;
  5126. + __u16 r_usb_rh_port_status_2;
  5127. + __u32 r_usb_irq_mask_read;
  5128. + __u32 r_usb_fm_number;
  5129. + struct work_struct usb_bh;
  5130. +};
  5131. +
  5132. +
  5133. +/* This struct is for passing data from the isoc top half to the isoc bottom
  5134. + half. */
  5135. +struct crisv10_isoc_complete_data {
  5136. + struct usb_hcd *hcd;
  5137. + struct urb *urb;
  5138. + struct work_struct usb_bh;
  5139. +};
  5140. +
  5141. +/* Entry item for URB lists for each endpint */
  5142. +typedef struct urb_entry
  5143. +{
  5144. + struct urb *urb;
  5145. + struct list_head list;
  5146. +} urb_entry_t;
  5147. +
  5148. +/* ---------------------------------------------------------------------------
  5149. + Virtual Root HUB
  5150. + ------------------------------------------------------------------------- */
  5151. +/* destination of request */
  5152. +#define RH_INTERFACE 0x01
  5153. +#define RH_ENDPOINT 0x02
  5154. +#define RH_OTHER 0x03
  5155. +
  5156. +#define RH_CLASS 0x20
  5157. +#define RH_VENDOR 0x40
  5158. +
  5159. +/* Requests: bRequest << 8 | bmRequestType */
  5160. +#define RH_GET_STATUS 0x0080
  5161. +#define RH_CLEAR_FEATURE 0x0100
  5162. +#define RH_SET_FEATURE 0x0300
  5163. +#define RH_SET_ADDRESS 0x0500
  5164. +#define RH_GET_DESCRIPTOR 0x0680
  5165. +#define RH_SET_DESCRIPTOR 0x0700
  5166. +#define RH_GET_CONFIGURATION 0x0880
  5167. +#define RH_SET_CONFIGURATION 0x0900
  5168. +#define RH_GET_STATE 0x0280
  5169. +#define RH_GET_INTERFACE 0x0A80
  5170. +#define RH_SET_INTERFACE 0x0B00
  5171. +#define RH_SYNC_FRAME 0x0C80
  5172. +/* Our Vendor Specific Request */
  5173. +#define RH_SET_EP 0x2000
  5174. +
  5175. +
  5176. +/* Hub port features */
  5177. +#define RH_PORT_CONNECTION 0x00
  5178. +#define RH_PORT_ENABLE 0x01
  5179. +#define RH_PORT_SUSPEND 0x02
  5180. +#define RH_PORT_OVER_CURRENT 0x03
  5181. +#define RH_PORT_RESET 0x04
  5182. +#define RH_PORT_POWER 0x08
  5183. +#define RH_PORT_LOW_SPEED 0x09
  5184. +#define RH_C_PORT_CONNECTION 0x10
  5185. +#define RH_C_PORT_ENABLE 0x11
  5186. +#define RH_C_PORT_SUSPEND 0x12
  5187. +#define RH_C_PORT_OVER_CURRENT 0x13
  5188. +#define RH_C_PORT_RESET 0x14
  5189. +
  5190. +/* Hub features */
  5191. +#define RH_C_HUB_LOCAL_POWER 0x00
  5192. +#define RH_C_HUB_OVER_CURRENT 0x01
  5193. +
  5194. +#define RH_DEVICE_REMOTE_WAKEUP 0x00
  5195. +#define RH_ENDPOINT_STALL 0x01
  5196. +
  5197. +/* Our Vendor Specific feature */
  5198. +#define RH_REMOVE_EP 0x00
  5199. +
  5200. +
  5201. +#define RH_ACK 0x01
  5202. +#define RH_REQ_ERR -1
  5203. +#define RH_NACK 0x00
  5204. +
  5205. +/* Field definitions for */
  5206. +
  5207. +#define USB_IN_command__eol__BITNR 0 /* command macros */
  5208. +#define USB_IN_command__eol__WIDTH 1
  5209. +#define USB_IN_command__eol__no 0
  5210. +#define USB_IN_command__eol__yes 1
  5211. +
  5212. +#define USB_IN_command__intr__BITNR 3
  5213. +#define USB_IN_command__intr__WIDTH 1
  5214. +#define USB_IN_command__intr__no 0
  5215. +#define USB_IN_command__intr__yes 1
  5216. +
  5217. +#define USB_IN_status__eop__BITNR 1 /* status macros. */
  5218. +#define USB_IN_status__eop__WIDTH 1
  5219. +#define USB_IN_status__eop__no 0
  5220. +#define USB_IN_status__eop__yes 1
  5221. +
  5222. +#define USB_IN_status__eot__BITNR 5
  5223. +#define USB_IN_status__eot__WIDTH 1
  5224. +#define USB_IN_status__eot__no 0
  5225. +#define USB_IN_status__eot__yes 1
  5226. +
  5227. +#define USB_IN_status__error__BITNR 6
  5228. +#define USB_IN_status__error__WIDTH 1
  5229. +#define USB_IN_status__error__no 0
  5230. +#define USB_IN_status__error__yes 1
  5231. +
  5232. +#define USB_IN_status__nodata__BITNR 7
  5233. +#define USB_IN_status__nodata__WIDTH 1
  5234. +#define USB_IN_status__nodata__no 0
  5235. +#define USB_IN_status__nodata__yes 1
  5236. +
  5237. +#define USB_IN_status__epid__BITNR 8
  5238. +#define USB_IN_status__epid__WIDTH 5
  5239. +
  5240. +#define USB_EP_command__eol__BITNR 0
  5241. +#define USB_EP_command__eol__WIDTH 1
  5242. +#define USB_EP_command__eol__no 0
  5243. +#define USB_EP_command__eol__yes 1
  5244. +
  5245. +#define USB_EP_command__eof__BITNR 1
  5246. +#define USB_EP_command__eof__WIDTH 1
  5247. +#define USB_EP_command__eof__no 0
  5248. +#define USB_EP_command__eof__yes 1
  5249. +
  5250. +#define USB_EP_command__intr__BITNR 3
  5251. +#define USB_EP_command__intr__WIDTH 1
  5252. +#define USB_EP_command__intr__no 0
  5253. +#define USB_EP_command__intr__yes 1
  5254. +
  5255. +#define USB_EP_command__enable__BITNR 4
  5256. +#define USB_EP_command__enable__WIDTH 1
  5257. +#define USB_EP_command__enable__no 0
  5258. +#define USB_EP_command__enable__yes 1
  5259. +
  5260. +#define USB_EP_command__hw_valid__BITNR 5
  5261. +#define USB_EP_command__hw_valid__WIDTH 1
  5262. +#define USB_EP_command__hw_valid__no 0
  5263. +#define USB_EP_command__hw_valid__yes 1
  5264. +
  5265. +#define USB_EP_command__epid__BITNR 8
  5266. +#define USB_EP_command__epid__WIDTH 5
  5267. +
  5268. +#define USB_SB_command__eol__BITNR 0 /* command macros. */
  5269. +#define USB_SB_command__eol__WIDTH 1
  5270. +#define USB_SB_command__eol__no 0
  5271. +#define USB_SB_command__eol__yes 1
  5272. +
  5273. +#define USB_SB_command__eot__BITNR 1
  5274. +#define USB_SB_command__eot__WIDTH 1
  5275. +#define USB_SB_command__eot__no 0
  5276. +#define USB_SB_command__eot__yes 1
  5277. +
  5278. +#define USB_SB_command__intr__BITNR 3
  5279. +#define USB_SB_command__intr__WIDTH 1
  5280. +#define USB_SB_command__intr__no 0
  5281. +#define USB_SB_command__intr__yes 1
  5282. +
  5283. +#define USB_SB_command__tt__BITNR 4
  5284. +#define USB_SB_command__tt__WIDTH 2
  5285. +#define USB_SB_command__tt__zout 0
  5286. +#define USB_SB_command__tt__in 1
  5287. +#define USB_SB_command__tt__out 2
  5288. +#define USB_SB_command__tt__setup 3
  5289. +
  5290. +
  5291. +#define USB_SB_command__rem__BITNR 8
  5292. +#define USB_SB_command__rem__WIDTH 6
  5293. +
  5294. +#define USB_SB_command__full__BITNR 6
  5295. +#define USB_SB_command__full__WIDTH 1
  5296. +#define USB_SB_command__full__no 0
  5297. +#define USB_SB_command__full__yes 1
  5298. +
  5299. +#endif