fotg210-hcd.c 158 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731
  1. /* Faraday FOTG210 EHCI-like driver
  2. *
  3. * Copyright (c) 2013 Faraday Technology Corporation
  4. *
  5. * Author: Yuan-Hsin Chen <yhchen@faraday-tech.com>
  6. * Feng-Hsin Chiang <john453@faraday-tech.com>
  7. * Po-Yu Chuang <ratbert.chuang@gmail.com>
  8. *
  9. * Most of code borrowed from the Linux-3.7 EHCI driver
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the
  13. * Free Software Foundation; either version 2 of the License, or (at your
  14. * option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  18. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  19. * for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software Foundation,
  23. * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25. #include <linux/module.h>
  26. #include <linux/device.h>
  27. #include <linux/dmapool.h>
  28. #include <linux/kernel.h>
  29. #include <linux/delay.h>
  30. #include <linux/ioport.h>
  31. #include <linux/sched.h>
  32. #include <linux/vmalloc.h>
  33. #include <linux/errno.h>
  34. #include <linux/init.h>
  35. #include <linux/hrtimer.h>
  36. #include <linux/list.h>
  37. #include <linux/interrupt.h>
  38. #include <linux/usb.h>
  39. #include <linux/usb/hcd.h>
  40. #include <linux/moduleparam.h>
  41. #include <linux/dma-mapping.h>
  42. #include <linux/debugfs.h>
  43. #include <linux/slab.h>
  44. #include <linux/uaccess.h>
  45. #include <linux/platform_device.h>
  46. #include <linux/io.h>
  47. #include <asm/byteorder.h>
  48. #include <asm/irq.h>
  49. #include <asm/unaligned.h>
  50. #define DRIVER_AUTHOR "Yuan-Hsin Chen"
  51. #define DRIVER_DESC "FOTG210 Host Controller (EHCI) Driver"
  52. static const char hcd_name[] = "fotg210_hcd";
  53. #undef FOTG210_URB_TRACE
  54. #define FOTG210_STATS
  55. /* magic numbers that can affect system performance */
  56. #define FOTG210_TUNE_CERR 3 /* 0-3 qtd retries; 0 == don't stop */
  57. #define FOTG210_TUNE_RL_HS 4 /* nak throttle; see 4.9 */
  58. #define FOTG210_TUNE_RL_TT 0
  59. #define FOTG210_TUNE_MULT_HS 1 /* 1-3 transactions/uframe; 4.10.3 */
  60. #define FOTG210_TUNE_MULT_TT 1
  61. /* Some drivers think it's safe to schedule isochronous transfers more than 256
  62. * ms into the future (partly as a result of an old bug in the scheduling
  63. * code). In an attempt to avoid trouble, we will use a minimum scheduling
  64. * length of 512 frames instead of 256.
  65. */
  66. #define FOTG210_TUNE_FLS 1 /* (medium) 512-frame schedule */
  67. /* Initial IRQ latency: faster than hw default */
  68. static int log2_irq_thresh; /* 0 to 6 */
  69. module_param(log2_irq_thresh, int, S_IRUGO);
  70. MODULE_PARM_DESC(log2_irq_thresh, "log2 IRQ latency, 1-64 microframes");
  71. /* initial park setting: slower than hw default */
  72. static unsigned park;
  73. module_param(park, uint, S_IRUGO);
  74. MODULE_PARM_DESC(park, "park setting; 1-3 back-to-back async packets");
  75. /* for link power management(LPM) feature */
  76. static unsigned int hird;
  77. module_param(hird, int, S_IRUGO);
  78. MODULE_PARM_DESC(hird, "host initiated resume duration, +1 for each 75us");
  79. #define INTR_MASK (STS_IAA | STS_FATAL | STS_PCD | STS_ERR | STS_INT)
  80. #include "fotg210.h"
  81. #define fotg210_dbg(fotg210, fmt, args...) \
  82. dev_dbg(fotg210_to_hcd(fotg210)->self.controller, fmt, ## args)
  83. #define fotg210_err(fotg210, fmt, args...) \
  84. dev_err(fotg210_to_hcd(fotg210)->self.controller, fmt, ## args)
  85. #define fotg210_info(fotg210, fmt, args...) \
  86. dev_info(fotg210_to_hcd(fotg210)->self.controller, fmt, ## args)
  87. #define fotg210_warn(fotg210, fmt, args...) \
  88. dev_warn(fotg210_to_hcd(fotg210)->self.controller, fmt, ## args)
  89. /* check the values in the HCSPARAMS register (host controller _Structural_
  90. * parameters) see EHCI spec, Table 2-4 for each value
  91. */
  92. static void dbg_hcs_params(struct fotg210_hcd *fotg210, char *label)
  93. {
  94. u32 params = fotg210_readl(fotg210, &fotg210->caps->hcs_params);
  95. fotg210_dbg(fotg210, "%s hcs_params 0x%x ports=%d\n", label, params,
  96. HCS_N_PORTS(params));
  97. }
  98. /* check the values in the HCCPARAMS register (host controller _Capability_
  99. * parameters) see EHCI Spec, Table 2-5 for each value
  100. */
  101. static void dbg_hcc_params(struct fotg210_hcd *fotg210, char *label)
  102. {
  103. u32 params = fotg210_readl(fotg210, &fotg210->caps->hcc_params);
  104. fotg210_dbg(fotg210, "%s hcc_params %04x uframes %s%s\n", label,
  105. params,
  106. HCC_PGM_FRAMELISTLEN(params) ? "256/512/1024" : "1024",
  107. HCC_CANPARK(params) ? " park" : "");
  108. }
  109. static void __maybe_unused
  110. dbg_qtd(const char *label, struct fotg210_hcd *fotg210, struct fotg210_qtd *qtd)
  111. {
  112. fotg210_dbg(fotg210, "%s td %p n%08x %08x t%08x p0=%08x\n", label, qtd,
  113. hc32_to_cpup(fotg210, &qtd->hw_next),
  114. hc32_to_cpup(fotg210, &qtd->hw_alt_next),
  115. hc32_to_cpup(fotg210, &qtd->hw_token),
  116. hc32_to_cpup(fotg210, &qtd->hw_buf[0]));
  117. if (qtd->hw_buf[1])
  118. fotg210_dbg(fotg210, " p1=%08x p2=%08x p3=%08x p4=%08x\n",
  119. hc32_to_cpup(fotg210, &qtd->hw_buf[1]),
  120. hc32_to_cpup(fotg210, &qtd->hw_buf[2]),
  121. hc32_to_cpup(fotg210, &qtd->hw_buf[3]),
  122. hc32_to_cpup(fotg210, &qtd->hw_buf[4]));
  123. }
  124. static void __maybe_unused
  125. dbg_qh(const char *label, struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
  126. {
  127. struct fotg210_qh_hw *hw = qh->hw;
  128. fotg210_dbg(fotg210, "%s qh %p n%08x info %x %x qtd %x\n", label, qh,
  129. hw->hw_next, hw->hw_info1, hw->hw_info2,
  130. hw->hw_current);
  131. dbg_qtd("overlay", fotg210, (struct fotg210_qtd *) &hw->hw_qtd_next);
  132. }
  133. static void __maybe_unused
  134. dbg_itd(const char *label, struct fotg210_hcd *fotg210, struct fotg210_itd *itd)
  135. {
  136. fotg210_dbg(fotg210, "%s[%d] itd %p, next %08x, urb %p\n", label,
  137. itd->frame, itd, hc32_to_cpu(fotg210, itd->hw_next),
  138. itd->urb);
  139. fotg210_dbg(fotg210,
  140. " trans: %08x %08x %08x %08x %08x %08x %08x %08x\n",
  141. hc32_to_cpu(fotg210, itd->hw_transaction[0]),
  142. hc32_to_cpu(fotg210, itd->hw_transaction[1]),
  143. hc32_to_cpu(fotg210, itd->hw_transaction[2]),
  144. hc32_to_cpu(fotg210, itd->hw_transaction[3]),
  145. hc32_to_cpu(fotg210, itd->hw_transaction[4]),
  146. hc32_to_cpu(fotg210, itd->hw_transaction[5]),
  147. hc32_to_cpu(fotg210, itd->hw_transaction[6]),
  148. hc32_to_cpu(fotg210, itd->hw_transaction[7]));
  149. fotg210_dbg(fotg210,
  150. " buf: %08x %08x %08x %08x %08x %08x %08x\n",
  151. hc32_to_cpu(fotg210, itd->hw_bufp[0]),
  152. hc32_to_cpu(fotg210, itd->hw_bufp[1]),
  153. hc32_to_cpu(fotg210, itd->hw_bufp[2]),
  154. hc32_to_cpu(fotg210, itd->hw_bufp[3]),
  155. hc32_to_cpu(fotg210, itd->hw_bufp[4]),
  156. hc32_to_cpu(fotg210, itd->hw_bufp[5]),
  157. hc32_to_cpu(fotg210, itd->hw_bufp[6]));
  158. fotg210_dbg(fotg210, " index: %d %d %d %d %d %d %d %d\n",
  159. itd->index[0], itd->index[1], itd->index[2],
  160. itd->index[3], itd->index[4], itd->index[5],
  161. itd->index[6], itd->index[7]);
  162. }
  163. static int __maybe_unused
  164. dbg_status_buf(char *buf, unsigned len, const char *label, u32 status)
  165. {
  166. return scnprintf(buf, len, "%s%sstatus %04x%s%s%s%s%s%s%s%s%s%s",
  167. label, label[0] ? " " : "", status,
  168. (status & STS_ASS) ? " Async" : "",
  169. (status & STS_PSS) ? " Periodic" : "",
  170. (status & STS_RECL) ? " Recl" : "",
  171. (status & STS_HALT) ? " Halt" : "",
  172. (status & STS_IAA) ? " IAA" : "",
  173. (status & STS_FATAL) ? " FATAL" : "",
  174. (status & STS_FLR) ? " FLR" : "",
  175. (status & STS_PCD) ? " PCD" : "",
  176. (status & STS_ERR) ? " ERR" : "",
  177. (status & STS_INT) ? " INT" : "");
  178. }
  179. static int __maybe_unused
  180. dbg_intr_buf(char *buf, unsigned len, const char *label, u32 enable)
  181. {
  182. return scnprintf(buf, len, "%s%sintrenable %02x%s%s%s%s%s%s",
  183. label, label[0] ? " " : "", enable,
  184. (enable & STS_IAA) ? " IAA" : "",
  185. (enable & STS_FATAL) ? " FATAL" : "",
  186. (enable & STS_FLR) ? " FLR" : "",
  187. (enable & STS_PCD) ? " PCD" : "",
  188. (enable & STS_ERR) ? " ERR" : "",
  189. (enable & STS_INT) ? " INT" : "");
  190. }
  191. static const char *const fls_strings[] = { "1024", "512", "256", "??" };
  192. static int dbg_command_buf(char *buf, unsigned len, const char *label,
  193. u32 command)
  194. {
  195. return scnprintf(buf, len,
  196. "%s%scommand %07x %s=%d ithresh=%d%s%s%s period=%s%s %s",
  197. label, label[0] ? " " : "", command,
  198. (command & CMD_PARK) ? " park" : "(park)",
  199. CMD_PARK_CNT(command),
  200. (command >> 16) & 0x3f,
  201. (command & CMD_IAAD) ? " IAAD" : "",
  202. (command & CMD_ASE) ? " Async" : "",
  203. (command & CMD_PSE) ? " Periodic" : "",
  204. fls_strings[(command >> 2) & 0x3],
  205. (command & CMD_RESET) ? " Reset" : "",
  206. (command & CMD_RUN) ? "RUN" : "HALT");
  207. }
  208. static char *dbg_port_buf(char *buf, unsigned len, const char *label, int port,
  209. u32 status)
  210. {
  211. char *sig;
  212. /* signaling state */
  213. switch (status & (3 << 10)) {
  214. case 0 << 10:
  215. sig = "se0";
  216. break;
  217. case 1 << 10:
  218. sig = "k";
  219. break; /* low speed */
  220. case 2 << 10:
  221. sig = "j";
  222. break;
  223. default:
  224. sig = "?";
  225. break;
  226. }
  227. scnprintf(buf, len, "%s%sport:%d status %06x %d sig=%s%s%s%s%s%s%s%s",
  228. label, label[0] ? " " : "", port, status,
  229. status >> 25, /*device address */
  230. sig,
  231. (status & PORT_RESET) ? " RESET" : "",
  232. (status & PORT_SUSPEND) ? " SUSPEND" : "",
  233. (status & PORT_RESUME) ? " RESUME" : "",
  234. (status & PORT_PEC) ? " PEC" : "",
  235. (status & PORT_PE) ? " PE" : "",
  236. (status & PORT_CSC) ? " CSC" : "",
  237. (status & PORT_CONNECT) ? " CONNECT" : "");
  238. return buf;
  239. }
  240. /* functions have the "wrong" filename when they're output... */
  241. #define dbg_status(fotg210, label, status) { \
  242. char _buf[80]; \
  243. dbg_status_buf(_buf, sizeof(_buf), label, status); \
  244. fotg210_dbg(fotg210, "%s\n", _buf); \
  245. }
  246. #define dbg_cmd(fotg210, label, command) { \
  247. char _buf[80]; \
  248. dbg_command_buf(_buf, sizeof(_buf), label, command); \
  249. fotg210_dbg(fotg210, "%s\n", _buf); \
  250. }
  251. #define dbg_port(fotg210, label, port, status) { \
  252. char _buf[80]; \
  253. fotg210_dbg(fotg210, "%s\n", \
  254. dbg_port_buf(_buf, sizeof(_buf), label, port, status));\
  255. }
  256. /* troubleshooting help: expose state in debugfs */
  257. static int debug_async_open(struct inode *, struct file *);
  258. static int debug_periodic_open(struct inode *, struct file *);
  259. static int debug_registers_open(struct inode *, struct file *);
  260. static int debug_async_open(struct inode *, struct file *);
  261. static ssize_t debug_output(struct file*, char __user*, size_t, loff_t*);
  262. static int debug_close(struct inode *, struct file *);
  263. static const struct file_operations debug_async_fops = {
  264. .owner = THIS_MODULE,
  265. .open = debug_async_open,
  266. .read = debug_output,
  267. .release = debug_close,
  268. .llseek = default_llseek,
  269. };
  270. static const struct file_operations debug_periodic_fops = {
  271. .owner = THIS_MODULE,
  272. .open = debug_periodic_open,
  273. .read = debug_output,
  274. .release = debug_close,
  275. .llseek = default_llseek,
  276. };
  277. static const struct file_operations debug_registers_fops = {
  278. .owner = THIS_MODULE,
  279. .open = debug_registers_open,
  280. .read = debug_output,
  281. .release = debug_close,
  282. .llseek = default_llseek,
  283. };
  284. static struct dentry *fotg210_debug_root;
  285. struct debug_buffer {
  286. ssize_t (*fill_func)(struct debug_buffer *); /* fill method */
  287. struct usb_bus *bus;
  288. struct mutex mutex; /* protect filling of buffer */
  289. size_t count; /* number of characters filled into buffer */
  290. char *output_buf;
  291. size_t alloc_size;
  292. };
  293. static inline char speed_char(u32 scratch)
  294. {
  295. switch (scratch & (3 << 12)) {
  296. case QH_FULL_SPEED:
  297. return 'f';
  298. case QH_LOW_SPEED:
  299. return 'l';
  300. case QH_HIGH_SPEED:
  301. return 'h';
  302. default:
  303. return '?';
  304. }
  305. }
  306. static inline char token_mark(struct fotg210_hcd *fotg210, __hc32 token)
  307. {
  308. __u32 v = hc32_to_cpu(fotg210, token);
  309. if (v & QTD_STS_ACTIVE)
  310. return '*';
  311. if (v & QTD_STS_HALT)
  312. return '-';
  313. if (!IS_SHORT_READ(v))
  314. return ' ';
  315. /* tries to advance through hw_alt_next */
  316. return '/';
  317. }
  318. static void qh_lines(struct fotg210_hcd *fotg210, struct fotg210_qh *qh,
  319. char **nextp, unsigned *sizep)
  320. {
  321. u32 scratch;
  322. u32 hw_curr;
  323. struct fotg210_qtd *td;
  324. unsigned temp;
  325. unsigned size = *sizep;
  326. char *next = *nextp;
  327. char mark;
  328. __le32 list_end = FOTG210_LIST_END(fotg210);
  329. struct fotg210_qh_hw *hw = qh->hw;
  330. if (hw->hw_qtd_next == list_end) /* NEC does this */
  331. mark = '@';
  332. else
  333. mark = token_mark(fotg210, hw->hw_token);
  334. if (mark == '/') { /* qh_alt_next controls qh advance? */
  335. if ((hw->hw_alt_next & QTD_MASK(fotg210)) ==
  336. fotg210->async->hw->hw_alt_next)
  337. mark = '#'; /* blocked */
  338. else if (hw->hw_alt_next == list_end)
  339. mark = '.'; /* use hw_qtd_next */
  340. /* else alt_next points to some other qtd */
  341. }
  342. scratch = hc32_to_cpup(fotg210, &hw->hw_info1);
  343. hw_curr = (mark == '*') ? hc32_to_cpup(fotg210, &hw->hw_current) : 0;
  344. temp = scnprintf(next, size,
  345. "qh/%p dev%d %cs ep%d %08x %08x(%08x%c %s nak%d)",
  346. qh, scratch & 0x007f,
  347. speed_char(scratch),
  348. (scratch >> 8) & 0x000f,
  349. scratch, hc32_to_cpup(fotg210, &hw->hw_info2),
  350. hc32_to_cpup(fotg210, &hw->hw_token), mark,
  351. (cpu_to_hc32(fotg210, QTD_TOGGLE) & hw->hw_token)
  352. ? "data1" : "data0",
  353. (hc32_to_cpup(fotg210, &hw->hw_alt_next) >> 1) & 0x0f);
  354. size -= temp;
  355. next += temp;
  356. /* hc may be modifying the list as we read it ... */
  357. list_for_each_entry(td, &qh->qtd_list, qtd_list) {
  358. scratch = hc32_to_cpup(fotg210, &td->hw_token);
  359. mark = ' ';
  360. if (hw_curr == td->qtd_dma)
  361. mark = '*';
  362. else if (hw->hw_qtd_next == cpu_to_hc32(fotg210, td->qtd_dma))
  363. mark = '+';
  364. else if (QTD_LENGTH(scratch)) {
  365. if (td->hw_alt_next == fotg210->async->hw->hw_alt_next)
  366. mark = '#';
  367. else if (td->hw_alt_next != list_end)
  368. mark = '/';
  369. }
  370. temp = snprintf(next, size,
  371. "\n\t%p%c%s len=%d %08x urb %p",
  372. td, mark, ({ char *tmp;
  373. switch ((scratch>>8)&0x03) {
  374. case 0:
  375. tmp = "out";
  376. break;
  377. case 1:
  378. tmp = "in";
  379. break;
  380. case 2:
  381. tmp = "setup";
  382. break;
  383. default:
  384. tmp = "?";
  385. break;
  386. } tmp; }),
  387. (scratch >> 16) & 0x7fff,
  388. scratch,
  389. td->urb);
  390. if (size < temp)
  391. temp = size;
  392. size -= temp;
  393. next += temp;
  394. if (temp == size)
  395. goto done;
  396. }
  397. temp = snprintf(next, size, "\n");
  398. if (size < temp)
  399. temp = size;
  400. size -= temp;
  401. next += temp;
  402. done:
  403. *sizep = size;
  404. *nextp = next;
  405. }
  406. static ssize_t fill_async_buffer(struct debug_buffer *buf)
  407. {
  408. struct usb_hcd *hcd;
  409. struct fotg210_hcd *fotg210;
  410. unsigned long flags;
  411. unsigned temp, size;
  412. char *next;
  413. struct fotg210_qh *qh;
  414. hcd = bus_to_hcd(buf->bus);
  415. fotg210 = hcd_to_fotg210(hcd);
  416. next = buf->output_buf;
  417. size = buf->alloc_size;
  418. *next = 0;
  419. /* dumps a snapshot of the async schedule.
  420. * usually empty except for long-term bulk reads, or head.
  421. * one QH per line, and TDs we know about
  422. */
  423. spin_lock_irqsave(&fotg210->lock, flags);
  424. for (qh = fotg210->async->qh_next.qh; size > 0 && qh;
  425. qh = qh->qh_next.qh)
  426. qh_lines(fotg210, qh, &next, &size);
  427. if (fotg210->async_unlink && size > 0) {
  428. temp = scnprintf(next, size, "\nunlink =\n");
  429. size -= temp;
  430. next += temp;
  431. for (qh = fotg210->async_unlink; size > 0 && qh;
  432. qh = qh->unlink_next)
  433. qh_lines(fotg210, qh, &next, &size);
  434. }
  435. spin_unlock_irqrestore(&fotg210->lock, flags);
  436. return strlen(buf->output_buf);
  437. }
  438. /* count tds, get ep direction */
  439. static unsigned output_buf_tds_dir(char *buf, struct fotg210_hcd *fotg210,
  440. struct fotg210_qh_hw *hw, struct fotg210_qh *qh, unsigned size)
  441. {
  442. u32 scratch = hc32_to_cpup(fotg210, &hw->hw_info1);
  443. struct fotg210_qtd *qtd;
  444. char *type = "";
  445. unsigned temp = 0;
  446. /* count tds, get ep direction */
  447. list_for_each_entry(qtd, &qh->qtd_list, qtd_list) {
  448. temp++;
  449. switch ((hc32_to_cpu(fotg210, qtd->hw_token) >> 8) & 0x03) {
  450. case 0:
  451. type = "out";
  452. continue;
  453. case 1:
  454. type = "in";
  455. continue;
  456. }
  457. }
  458. return scnprintf(buf, size, "(%c%d ep%d%s [%d/%d] q%d p%d)",
  459. speed_char(scratch), scratch & 0x007f,
  460. (scratch >> 8) & 0x000f, type, qh->usecs,
  461. qh->c_usecs, temp, (scratch >> 16) & 0x7ff);
  462. }
  463. #define DBG_SCHED_LIMIT 64
  464. static ssize_t fill_periodic_buffer(struct debug_buffer *buf)
  465. {
  466. struct usb_hcd *hcd;
  467. struct fotg210_hcd *fotg210;
  468. unsigned long flags;
  469. union fotg210_shadow p, *seen;
  470. unsigned temp, size, seen_count;
  471. char *next;
  472. unsigned i;
  473. __hc32 tag;
  474. seen = kmalloc_array(DBG_SCHED_LIMIT, sizeof(*seen), GFP_ATOMIC);
  475. if (!seen)
  476. return 0;
  477. seen_count = 0;
  478. hcd = bus_to_hcd(buf->bus);
  479. fotg210 = hcd_to_fotg210(hcd);
  480. next = buf->output_buf;
  481. size = buf->alloc_size;
  482. temp = scnprintf(next, size, "size = %d\n", fotg210->periodic_size);
  483. size -= temp;
  484. next += temp;
  485. /* dump a snapshot of the periodic schedule.
  486. * iso changes, interrupt usually doesn't.
  487. */
  488. spin_lock_irqsave(&fotg210->lock, flags);
  489. for (i = 0; i < fotg210->periodic_size; i++) {
  490. p = fotg210->pshadow[i];
  491. if (likely(!p.ptr))
  492. continue;
  493. tag = Q_NEXT_TYPE(fotg210, fotg210->periodic[i]);
  494. temp = scnprintf(next, size, "%4d: ", i);
  495. size -= temp;
  496. next += temp;
  497. do {
  498. struct fotg210_qh_hw *hw;
  499. switch (hc32_to_cpu(fotg210, tag)) {
  500. case Q_TYPE_QH:
  501. hw = p.qh->hw;
  502. temp = scnprintf(next, size, " qh%d-%04x/%p",
  503. p.qh->period,
  504. hc32_to_cpup(fotg210,
  505. &hw->hw_info2)
  506. /* uframe masks */
  507. & (QH_CMASK | QH_SMASK),
  508. p.qh);
  509. size -= temp;
  510. next += temp;
  511. /* don't repeat what follows this qh */
  512. for (temp = 0; temp < seen_count; temp++) {
  513. if (seen[temp].ptr != p.ptr)
  514. continue;
  515. if (p.qh->qh_next.ptr) {
  516. temp = scnprintf(next, size,
  517. " ...");
  518. size -= temp;
  519. next += temp;
  520. }
  521. break;
  522. }
  523. /* show more info the first time around */
  524. if (temp == seen_count) {
  525. temp = output_buf_tds_dir(next,
  526. fotg210, hw,
  527. p.qh, size);
  528. if (seen_count < DBG_SCHED_LIMIT)
  529. seen[seen_count++].qh = p.qh;
  530. } else
  531. temp = 0;
  532. tag = Q_NEXT_TYPE(fotg210, hw->hw_next);
  533. p = p.qh->qh_next;
  534. break;
  535. case Q_TYPE_FSTN:
  536. temp = scnprintf(next, size,
  537. " fstn-%8x/%p",
  538. p.fstn->hw_prev, p.fstn);
  539. tag = Q_NEXT_TYPE(fotg210, p.fstn->hw_next);
  540. p = p.fstn->fstn_next;
  541. break;
  542. case Q_TYPE_ITD:
  543. temp = scnprintf(next, size,
  544. " itd/%p", p.itd);
  545. tag = Q_NEXT_TYPE(fotg210, p.itd->hw_next);
  546. p = p.itd->itd_next;
  547. break;
  548. }
  549. size -= temp;
  550. next += temp;
  551. } while (p.ptr);
  552. temp = scnprintf(next, size, "\n");
  553. size -= temp;
  554. next += temp;
  555. }
  556. spin_unlock_irqrestore(&fotg210->lock, flags);
  557. kfree(seen);
  558. return buf->alloc_size - size;
  559. }
  560. #undef DBG_SCHED_LIMIT
  561. static const char *rh_state_string(struct fotg210_hcd *fotg210)
  562. {
  563. switch (fotg210->rh_state) {
  564. case FOTG210_RH_HALTED:
  565. return "halted";
  566. case FOTG210_RH_SUSPENDED:
  567. return "suspended";
  568. case FOTG210_RH_RUNNING:
  569. return "running";
  570. case FOTG210_RH_STOPPING:
  571. return "stopping";
  572. }
  573. return "?";
  574. }
  575. static ssize_t fill_registers_buffer(struct debug_buffer *buf)
  576. {
  577. struct usb_hcd *hcd;
  578. struct fotg210_hcd *fotg210;
  579. unsigned long flags;
  580. unsigned temp, size, i;
  581. char *next, scratch[80];
  582. static const char fmt[] = "%*s\n";
  583. static const char label[] = "";
  584. hcd = bus_to_hcd(buf->bus);
  585. fotg210 = hcd_to_fotg210(hcd);
  586. next = buf->output_buf;
  587. size = buf->alloc_size;
  588. spin_lock_irqsave(&fotg210->lock, flags);
  589. if (!HCD_HW_ACCESSIBLE(hcd)) {
  590. size = scnprintf(next, size,
  591. "bus %s, device %s\n"
  592. "%s\n"
  593. "SUSPENDED(no register access)\n",
  594. hcd->self.controller->bus->name,
  595. dev_name(hcd->self.controller),
  596. hcd->product_desc);
  597. goto done;
  598. }
  599. /* Capability Registers */
  600. i = HC_VERSION(fotg210, fotg210_readl(fotg210,
  601. &fotg210->caps->hc_capbase));
  602. temp = scnprintf(next, size,
  603. "bus %s, device %s\n"
  604. "%s\n"
  605. "EHCI %x.%02x, rh state %s\n",
  606. hcd->self.controller->bus->name,
  607. dev_name(hcd->self.controller),
  608. hcd->product_desc,
  609. i >> 8, i & 0x0ff, rh_state_string(fotg210));
  610. size -= temp;
  611. next += temp;
  612. /* FIXME interpret both types of params */
  613. i = fotg210_readl(fotg210, &fotg210->caps->hcs_params);
  614. temp = scnprintf(next, size, "structural params 0x%08x\n", i);
  615. size -= temp;
  616. next += temp;
  617. i = fotg210_readl(fotg210, &fotg210->caps->hcc_params);
  618. temp = scnprintf(next, size, "capability params 0x%08x\n", i);
  619. size -= temp;
  620. next += temp;
  621. /* Operational Registers */
  622. temp = dbg_status_buf(scratch, sizeof(scratch), label,
  623. fotg210_readl(fotg210, &fotg210->regs->status));
  624. temp = scnprintf(next, size, fmt, temp, scratch);
  625. size -= temp;
  626. next += temp;
  627. temp = dbg_command_buf(scratch, sizeof(scratch), label,
  628. fotg210_readl(fotg210, &fotg210->regs->command));
  629. temp = scnprintf(next, size, fmt, temp, scratch);
  630. size -= temp;
  631. next += temp;
  632. temp = dbg_intr_buf(scratch, sizeof(scratch), label,
  633. fotg210_readl(fotg210, &fotg210->regs->intr_enable));
  634. temp = scnprintf(next, size, fmt, temp, scratch);
  635. size -= temp;
  636. next += temp;
  637. temp = scnprintf(next, size, "uframe %04x\n",
  638. fotg210_read_frame_index(fotg210));
  639. size -= temp;
  640. next += temp;
  641. if (fotg210->async_unlink) {
  642. temp = scnprintf(next, size, "async unlink qh %p\n",
  643. fotg210->async_unlink);
  644. size -= temp;
  645. next += temp;
  646. }
  647. #ifdef FOTG210_STATS
  648. temp = scnprintf(next, size,
  649. "irq normal %ld err %ld iaa %ld(lost %ld)\n",
  650. fotg210->stats.normal, fotg210->stats.error,
  651. fotg210->stats.iaa, fotg210->stats.lost_iaa);
  652. size -= temp;
  653. next += temp;
  654. temp = scnprintf(next, size, "complete %ld unlink %ld\n",
  655. fotg210->stats.complete, fotg210->stats.unlink);
  656. size -= temp;
  657. next += temp;
  658. #endif
  659. done:
  660. spin_unlock_irqrestore(&fotg210->lock, flags);
  661. return buf->alloc_size - size;
  662. }
  663. static struct debug_buffer
  664. *alloc_buffer(struct usb_bus *bus, ssize_t (*fill_func)(struct debug_buffer *))
  665. {
  666. struct debug_buffer *buf;
  667. buf = kzalloc(sizeof(struct debug_buffer), GFP_KERNEL);
  668. if (buf) {
  669. buf->bus = bus;
  670. buf->fill_func = fill_func;
  671. mutex_init(&buf->mutex);
  672. buf->alloc_size = PAGE_SIZE;
  673. }
  674. return buf;
  675. }
  676. static int fill_buffer(struct debug_buffer *buf)
  677. {
  678. int ret = 0;
  679. if (!buf->output_buf)
  680. buf->output_buf = vmalloc(buf->alloc_size);
  681. if (!buf->output_buf) {
  682. ret = -ENOMEM;
  683. goto out;
  684. }
  685. ret = buf->fill_func(buf);
  686. if (ret >= 0) {
  687. buf->count = ret;
  688. ret = 0;
  689. }
  690. out:
  691. return ret;
  692. }
  693. static ssize_t debug_output(struct file *file, char __user *user_buf,
  694. size_t len, loff_t *offset)
  695. {
  696. struct debug_buffer *buf = file->private_data;
  697. int ret = 0;
  698. mutex_lock(&buf->mutex);
  699. if (buf->count == 0) {
  700. ret = fill_buffer(buf);
  701. if (ret != 0) {
  702. mutex_unlock(&buf->mutex);
  703. goto out;
  704. }
  705. }
  706. mutex_unlock(&buf->mutex);
  707. ret = simple_read_from_buffer(user_buf, len, offset,
  708. buf->output_buf, buf->count);
  709. out:
  710. return ret;
  711. }
  712. static int debug_close(struct inode *inode, struct file *file)
  713. {
  714. struct debug_buffer *buf = file->private_data;
  715. if (buf) {
  716. vfree(buf->output_buf);
  717. kfree(buf);
  718. }
  719. return 0;
  720. }
  721. static int debug_async_open(struct inode *inode, struct file *file)
  722. {
  723. file->private_data = alloc_buffer(inode->i_private, fill_async_buffer);
  724. return file->private_data ? 0 : -ENOMEM;
  725. }
  726. static int debug_periodic_open(struct inode *inode, struct file *file)
  727. {
  728. struct debug_buffer *buf;
  729. buf = alloc_buffer(inode->i_private, fill_periodic_buffer);
  730. if (!buf)
  731. return -ENOMEM;
  732. buf->alloc_size = (sizeof(void *) == 4 ? 6 : 8)*PAGE_SIZE;
  733. file->private_data = buf;
  734. return 0;
  735. }
  736. static int debug_registers_open(struct inode *inode, struct file *file)
  737. {
  738. file->private_data = alloc_buffer(inode->i_private,
  739. fill_registers_buffer);
  740. return file->private_data ? 0 : -ENOMEM;
  741. }
  742. static inline void create_debug_files(struct fotg210_hcd *fotg210)
  743. {
  744. struct usb_bus *bus = &fotg210_to_hcd(fotg210)->self;
  745. fotg210->debug_dir = debugfs_create_dir(bus->bus_name,
  746. fotg210_debug_root);
  747. if (!fotg210->debug_dir)
  748. return;
  749. if (!debugfs_create_file("async", S_IRUGO, fotg210->debug_dir, bus,
  750. &debug_async_fops))
  751. goto file_error;
  752. if (!debugfs_create_file("periodic", S_IRUGO, fotg210->debug_dir, bus,
  753. &debug_periodic_fops))
  754. goto file_error;
  755. if (!debugfs_create_file("registers", S_IRUGO, fotg210->debug_dir, bus,
  756. &debug_registers_fops))
  757. goto file_error;
  758. return;
  759. file_error:
  760. debugfs_remove_recursive(fotg210->debug_dir);
  761. }
  762. static inline void remove_debug_files(struct fotg210_hcd *fotg210)
  763. {
  764. debugfs_remove_recursive(fotg210->debug_dir);
  765. }
  766. /* handshake - spin reading hc until handshake completes or fails
  767. * @ptr: address of hc register to be read
  768. * @mask: bits to look at in result of read
  769. * @done: value of those bits when handshake succeeds
  770. * @usec: timeout in microseconds
  771. *
  772. * Returns negative errno, or zero on success
  773. *
  774. * Success happens when the "mask" bits have the specified value (hardware
  775. * handshake done). There are two failure modes: "usec" have passed (major
  776. * hardware flakeout), or the register reads as all-ones (hardware removed).
  777. *
  778. * That last failure should_only happen in cases like physical cardbus eject
  779. * before driver shutdown. But it also seems to be caused by bugs in cardbus
  780. * bridge shutdown: shutting down the bridge before the devices using it.
  781. */
  782. static int handshake(struct fotg210_hcd *fotg210, void __iomem *ptr,
  783. u32 mask, u32 done, int usec)
  784. {
  785. u32 result;
  786. do {
  787. result = fotg210_readl(fotg210, ptr);
  788. if (result == ~(u32)0) /* card removed */
  789. return -ENODEV;
  790. result &= mask;
  791. if (result == done)
  792. return 0;
  793. udelay(1);
  794. usec--;
  795. } while (usec > 0);
  796. return -ETIMEDOUT;
  797. }
  798. /* Force HC to halt state from unknown (EHCI spec section 2.3).
  799. * Must be called with interrupts enabled and the lock not held.
  800. */
  801. static int fotg210_halt(struct fotg210_hcd *fotg210)
  802. {
  803. u32 temp;
  804. spin_lock_irq(&fotg210->lock);
  805. /* disable any irqs left enabled by previous code */
  806. fotg210_writel(fotg210, 0, &fotg210->regs->intr_enable);
  807. /*
  808. * This routine gets called during probe before fotg210->command
  809. * has been initialized, so we can't rely on its value.
  810. */
  811. fotg210->command &= ~CMD_RUN;
  812. temp = fotg210_readl(fotg210, &fotg210->regs->command);
  813. temp &= ~(CMD_RUN | CMD_IAAD);
  814. fotg210_writel(fotg210, temp, &fotg210->regs->command);
  815. spin_unlock_irq(&fotg210->lock);
  816. synchronize_irq(fotg210_to_hcd(fotg210)->irq);
  817. return handshake(fotg210, &fotg210->regs->status,
  818. STS_HALT, STS_HALT, 16 * 125);
  819. }
  820. /* Reset a non-running (STS_HALT == 1) controller.
  821. * Must be called with interrupts enabled and the lock not held.
  822. */
  823. static int fotg210_reset(struct fotg210_hcd *fotg210)
  824. {
  825. int retval;
  826. u32 command = fotg210_readl(fotg210, &fotg210->regs->command);
  827. /* If the EHCI debug controller is active, special care must be
  828. * taken before and after a host controller reset
  829. */
  830. if (fotg210->debug && !dbgp_reset_prep(fotg210_to_hcd(fotg210)))
  831. fotg210->debug = NULL;
  832. command |= CMD_RESET;
  833. dbg_cmd(fotg210, "reset", command);
  834. fotg210_writel(fotg210, command, &fotg210->regs->command);
  835. fotg210->rh_state = FOTG210_RH_HALTED;
  836. fotg210->next_statechange = jiffies;
  837. retval = handshake(fotg210, &fotg210->regs->command,
  838. CMD_RESET, 0, 250 * 1000);
  839. if (retval)
  840. return retval;
  841. if (fotg210->debug)
  842. dbgp_external_startup(fotg210_to_hcd(fotg210));
  843. fotg210->port_c_suspend = fotg210->suspended_ports =
  844. fotg210->resuming_ports = 0;
  845. return retval;
  846. }
  847. /* Idle the controller (turn off the schedules).
  848. * Must be called with interrupts enabled and the lock not held.
  849. */
  850. static void fotg210_quiesce(struct fotg210_hcd *fotg210)
  851. {
  852. u32 temp;
  853. if (fotg210->rh_state != FOTG210_RH_RUNNING)
  854. return;
  855. /* wait for any schedule enables/disables to take effect */
  856. temp = (fotg210->command << 10) & (STS_ASS | STS_PSS);
  857. handshake(fotg210, &fotg210->regs->status, STS_ASS | STS_PSS, temp,
  858. 16 * 125);
  859. /* then disable anything that's still active */
  860. spin_lock_irq(&fotg210->lock);
  861. fotg210->command &= ~(CMD_ASE | CMD_PSE);
  862. fotg210_writel(fotg210, fotg210->command, &fotg210->regs->command);
  863. spin_unlock_irq(&fotg210->lock);
  864. /* hardware can take 16 microframes to turn off ... */
  865. handshake(fotg210, &fotg210->regs->status, STS_ASS | STS_PSS, 0,
  866. 16 * 125);
  867. }
  868. static void end_unlink_async(struct fotg210_hcd *fotg210);
  869. static void unlink_empty_async(struct fotg210_hcd *fotg210);
  870. static void fotg210_work(struct fotg210_hcd *fotg210);
  871. static void start_unlink_intr(struct fotg210_hcd *fotg210,
  872. struct fotg210_qh *qh);
  873. static void end_unlink_intr(struct fotg210_hcd *fotg210, struct fotg210_qh *qh);
  874. /* Set a bit in the USBCMD register */
  875. static void fotg210_set_command_bit(struct fotg210_hcd *fotg210, u32 bit)
  876. {
  877. fotg210->command |= bit;
  878. fotg210_writel(fotg210, fotg210->command, &fotg210->regs->command);
  879. /* unblock posted write */
  880. fotg210_readl(fotg210, &fotg210->regs->command);
  881. }
  882. /* Clear a bit in the USBCMD register */
  883. static void fotg210_clear_command_bit(struct fotg210_hcd *fotg210, u32 bit)
  884. {
  885. fotg210->command &= ~bit;
  886. fotg210_writel(fotg210, fotg210->command, &fotg210->regs->command);
  887. /* unblock posted write */
  888. fotg210_readl(fotg210, &fotg210->regs->command);
  889. }
  890. /* EHCI timer support... Now using hrtimers.
  891. *
  892. * Lots of different events are triggered from fotg210->hrtimer. Whenever
  893. * the timer routine runs, it checks each possible event; events that are
  894. * currently enabled and whose expiration time has passed get handled.
  895. * The set of enabled events is stored as a collection of bitflags in
  896. * fotg210->enabled_hrtimer_events, and they are numbered in order of
  897. * increasing delay values (ranging between 1 ms and 100 ms).
  898. *
  899. * Rather than implementing a sorted list or tree of all pending events,
  900. * we keep track only of the lowest-numbered pending event, in
  901. * fotg210->next_hrtimer_event. Whenever fotg210->hrtimer gets restarted, its
  902. * expiration time is set to the timeout value for this event.
  903. *
  904. * As a result, events might not get handled right away; the actual delay
  905. * could be anywhere up to twice the requested delay. This doesn't
  906. * matter, because none of the events are especially time-critical. The
  907. * ones that matter most all have a delay of 1 ms, so they will be
  908. * handled after 2 ms at most, which is okay. In addition to this, we
  909. * allow for an expiration range of 1 ms.
  910. */
  911. /* Delay lengths for the hrtimer event types.
  912. * Keep this list sorted by delay length, in the same order as
  913. * the event types indexed by enum fotg210_hrtimer_event in fotg210.h.
  914. */
  915. static unsigned event_delays_ns[] = {
  916. 1 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_POLL_ASS */
  917. 1 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_POLL_PSS */
  918. 1 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_POLL_DEAD */
  919. 1125 * NSEC_PER_USEC, /* FOTG210_HRTIMER_UNLINK_INTR */
  920. 2 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_FREE_ITDS */
  921. 6 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_ASYNC_UNLINKS */
  922. 10 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_IAA_WATCHDOG */
  923. 10 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_DISABLE_PERIODIC */
  924. 15 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_DISABLE_ASYNC */
  925. 100 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_IO_WATCHDOG */
  926. };
  927. /* Enable a pending hrtimer event */
  928. static void fotg210_enable_event(struct fotg210_hcd *fotg210, unsigned event,
  929. bool resched)
  930. {
  931. ktime_t *timeout = &fotg210->hr_timeouts[event];
  932. if (resched)
  933. *timeout = ktime_add(ktime_get(), event_delays_ns[event]);
  934. fotg210->enabled_hrtimer_events |= (1 << event);
  935. /* Track only the lowest-numbered pending event */
  936. if (event < fotg210->next_hrtimer_event) {
  937. fotg210->next_hrtimer_event = event;
  938. hrtimer_start_range_ns(&fotg210->hrtimer, *timeout,
  939. NSEC_PER_MSEC, HRTIMER_MODE_ABS);
  940. }
  941. }
  942. /* Poll the STS_ASS status bit; see when it agrees with CMD_ASE */
  943. static void fotg210_poll_ASS(struct fotg210_hcd *fotg210)
  944. {
  945. unsigned actual, want;
  946. /* Don't enable anything if the controller isn't running (e.g., died) */
  947. if (fotg210->rh_state != FOTG210_RH_RUNNING)
  948. return;
  949. want = (fotg210->command & CMD_ASE) ? STS_ASS : 0;
  950. actual = fotg210_readl(fotg210, &fotg210->regs->status) & STS_ASS;
  951. if (want != actual) {
  952. /* Poll again later, but give up after about 20 ms */
  953. if (fotg210->ASS_poll_count++ < 20) {
  954. fotg210_enable_event(fotg210, FOTG210_HRTIMER_POLL_ASS,
  955. true);
  956. return;
  957. }
  958. fotg210_dbg(fotg210, "Waited too long for the async schedule status (%x/%x), giving up\n",
  959. want, actual);
  960. }
  961. fotg210->ASS_poll_count = 0;
  962. /* The status is up-to-date; restart or stop the schedule as needed */
  963. if (want == 0) { /* Stopped */
  964. if (fotg210->async_count > 0)
  965. fotg210_set_command_bit(fotg210, CMD_ASE);
  966. } else { /* Running */
  967. if (fotg210->async_count == 0) {
  968. /* Turn off the schedule after a while */
  969. fotg210_enable_event(fotg210,
  970. FOTG210_HRTIMER_DISABLE_ASYNC,
  971. true);
  972. }
  973. }
  974. }
  975. /* Turn off the async schedule after a brief delay */
  976. static void fotg210_disable_ASE(struct fotg210_hcd *fotg210)
  977. {
  978. fotg210_clear_command_bit(fotg210, CMD_ASE);
  979. }
  980. /* Poll the STS_PSS status bit; see when it agrees with CMD_PSE */
  981. static void fotg210_poll_PSS(struct fotg210_hcd *fotg210)
  982. {
  983. unsigned actual, want;
  984. /* Don't do anything if the controller isn't running (e.g., died) */
  985. if (fotg210->rh_state != FOTG210_RH_RUNNING)
  986. return;
  987. want = (fotg210->command & CMD_PSE) ? STS_PSS : 0;
  988. actual = fotg210_readl(fotg210, &fotg210->regs->status) & STS_PSS;
  989. if (want != actual) {
  990. /* Poll again later, but give up after about 20 ms */
  991. if (fotg210->PSS_poll_count++ < 20) {
  992. fotg210_enable_event(fotg210, FOTG210_HRTIMER_POLL_PSS,
  993. true);
  994. return;
  995. }
  996. fotg210_dbg(fotg210, "Waited too long for the periodic schedule status (%x/%x), giving up\n",
  997. want, actual);
  998. }
  999. fotg210->PSS_poll_count = 0;
  1000. /* The status is up-to-date; restart or stop the schedule as needed */
  1001. if (want == 0) { /* Stopped */
  1002. if (fotg210->periodic_count > 0)
  1003. fotg210_set_command_bit(fotg210, CMD_PSE);
  1004. } else { /* Running */
  1005. if (fotg210->periodic_count == 0) {
  1006. /* Turn off the schedule after a while */
  1007. fotg210_enable_event(fotg210,
  1008. FOTG210_HRTIMER_DISABLE_PERIODIC,
  1009. true);
  1010. }
  1011. }
  1012. }
  1013. /* Turn off the periodic schedule after a brief delay */
  1014. static void fotg210_disable_PSE(struct fotg210_hcd *fotg210)
  1015. {
  1016. fotg210_clear_command_bit(fotg210, CMD_PSE);
  1017. }
  1018. /* Poll the STS_HALT status bit; see when a dead controller stops */
  1019. static void fotg210_handle_controller_death(struct fotg210_hcd *fotg210)
  1020. {
  1021. if (!(fotg210_readl(fotg210, &fotg210->regs->status) & STS_HALT)) {
  1022. /* Give up after a few milliseconds */
  1023. if (fotg210->died_poll_count++ < 5) {
  1024. /* Try again later */
  1025. fotg210_enable_event(fotg210,
  1026. FOTG210_HRTIMER_POLL_DEAD, true);
  1027. return;
  1028. }
  1029. fotg210_warn(fotg210, "Waited too long for the controller to stop, giving up\n");
  1030. }
  1031. /* Clean up the mess */
  1032. fotg210->rh_state = FOTG210_RH_HALTED;
  1033. fotg210_writel(fotg210, 0, &fotg210->regs->intr_enable);
  1034. fotg210_work(fotg210);
  1035. end_unlink_async(fotg210);
  1036. /* Not in process context, so don't try to reset the controller */
  1037. }
  1038. /* Handle unlinked interrupt QHs once they are gone from the hardware */
  1039. static void fotg210_handle_intr_unlinks(struct fotg210_hcd *fotg210)
  1040. {
  1041. bool stopped = (fotg210->rh_state < FOTG210_RH_RUNNING);
  1042. /*
  1043. * Process all the QHs on the intr_unlink list that were added
  1044. * before the current unlink cycle began. The list is in
  1045. * temporal order, so stop when we reach the first entry in the
  1046. * current cycle. But if the root hub isn't running then
  1047. * process all the QHs on the list.
  1048. */
  1049. fotg210->intr_unlinking = true;
  1050. while (fotg210->intr_unlink) {
  1051. struct fotg210_qh *qh = fotg210->intr_unlink;
  1052. if (!stopped && qh->unlink_cycle == fotg210->intr_unlink_cycle)
  1053. break;
  1054. fotg210->intr_unlink = qh->unlink_next;
  1055. qh->unlink_next = NULL;
  1056. end_unlink_intr(fotg210, qh);
  1057. }
  1058. /* Handle remaining entries later */
  1059. if (fotg210->intr_unlink) {
  1060. fotg210_enable_event(fotg210, FOTG210_HRTIMER_UNLINK_INTR,
  1061. true);
  1062. ++fotg210->intr_unlink_cycle;
  1063. }
  1064. fotg210->intr_unlinking = false;
  1065. }
  1066. /* Start another free-iTDs/siTDs cycle */
  1067. static void start_free_itds(struct fotg210_hcd *fotg210)
  1068. {
  1069. if (!(fotg210->enabled_hrtimer_events &
  1070. BIT(FOTG210_HRTIMER_FREE_ITDS))) {
  1071. fotg210->last_itd_to_free = list_entry(
  1072. fotg210->cached_itd_list.prev,
  1073. struct fotg210_itd, itd_list);
  1074. fotg210_enable_event(fotg210, FOTG210_HRTIMER_FREE_ITDS, true);
  1075. }
  1076. }
  1077. /* Wait for controller to stop using old iTDs and siTDs */
  1078. static void end_free_itds(struct fotg210_hcd *fotg210)
  1079. {
  1080. struct fotg210_itd *itd, *n;
  1081. if (fotg210->rh_state < FOTG210_RH_RUNNING)
  1082. fotg210->last_itd_to_free = NULL;
  1083. list_for_each_entry_safe(itd, n, &fotg210->cached_itd_list, itd_list) {
  1084. list_del(&itd->itd_list);
  1085. dma_pool_free(fotg210->itd_pool, itd, itd->itd_dma);
  1086. if (itd == fotg210->last_itd_to_free)
  1087. break;
  1088. }
  1089. if (!list_empty(&fotg210->cached_itd_list))
  1090. start_free_itds(fotg210);
  1091. }
  1092. /* Handle lost (or very late) IAA interrupts */
  1093. static void fotg210_iaa_watchdog(struct fotg210_hcd *fotg210)
  1094. {
  1095. if (fotg210->rh_state != FOTG210_RH_RUNNING)
  1096. return;
  1097. /*
  1098. * Lost IAA irqs wedge things badly; seen first with a vt8235.
  1099. * So we need this watchdog, but must protect it against both
  1100. * (a) SMP races against real IAA firing and retriggering, and
  1101. * (b) clean HC shutdown, when IAA watchdog was pending.
  1102. */
  1103. if (fotg210->async_iaa) {
  1104. u32 cmd, status;
  1105. /* If we get here, IAA is *REALLY* late. It's barely
  1106. * conceivable that the system is so busy that CMD_IAAD
  1107. * is still legitimately set, so let's be sure it's
  1108. * clear before we read STS_IAA. (The HC should clear
  1109. * CMD_IAAD when it sets STS_IAA.)
  1110. */
  1111. cmd = fotg210_readl(fotg210, &fotg210->regs->command);
  1112. /*
  1113. * If IAA is set here it either legitimately triggered
  1114. * after the watchdog timer expired (_way_ late, so we'll
  1115. * still count it as lost) ... or a silicon erratum:
  1116. * - VIA seems to set IAA without triggering the IRQ;
  1117. * - IAAD potentially cleared without setting IAA.
  1118. */
  1119. status = fotg210_readl(fotg210, &fotg210->regs->status);
  1120. if ((status & STS_IAA) || !(cmd & CMD_IAAD)) {
  1121. COUNT(fotg210->stats.lost_iaa);
  1122. fotg210_writel(fotg210, STS_IAA,
  1123. &fotg210->regs->status);
  1124. }
  1125. fotg210_dbg(fotg210, "IAA watchdog: status %x cmd %x\n",
  1126. status, cmd);
  1127. end_unlink_async(fotg210);
  1128. }
  1129. }
  1130. /* Enable the I/O watchdog, if appropriate */
  1131. static void turn_on_io_watchdog(struct fotg210_hcd *fotg210)
  1132. {
  1133. /* Not needed if the controller isn't running or it's already enabled */
  1134. if (fotg210->rh_state != FOTG210_RH_RUNNING ||
  1135. (fotg210->enabled_hrtimer_events &
  1136. BIT(FOTG210_HRTIMER_IO_WATCHDOG)))
  1137. return;
  1138. /*
  1139. * Isochronous transfers always need the watchdog.
  1140. * For other sorts we use it only if the flag is set.
  1141. */
  1142. if (fotg210->isoc_count > 0 || (fotg210->need_io_watchdog &&
  1143. fotg210->async_count + fotg210->intr_count > 0))
  1144. fotg210_enable_event(fotg210, FOTG210_HRTIMER_IO_WATCHDOG,
  1145. true);
  1146. }
  1147. /* Handler functions for the hrtimer event types.
  1148. * Keep this array in the same order as the event types indexed by
  1149. * enum fotg210_hrtimer_event in fotg210.h.
  1150. */
  1151. static void (*event_handlers[])(struct fotg210_hcd *) = {
  1152. fotg210_poll_ASS, /* FOTG210_HRTIMER_POLL_ASS */
  1153. fotg210_poll_PSS, /* FOTG210_HRTIMER_POLL_PSS */
  1154. fotg210_handle_controller_death, /* FOTG210_HRTIMER_POLL_DEAD */
  1155. fotg210_handle_intr_unlinks, /* FOTG210_HRTIMER_UNLINK_INTR */
  1156. end_free_itds, /* FOTG210_HRTIMER_FREE_ITDS */
  1157. unlink_empty_async, /* FOTG210_HRTIMER_ASYNC_UNLINKS */
  1158. fotg210_iaa_watchdog, /* FOTG210_HRTIMER_IAA_WATCHDOG */
  1159. fotg210_disable_PSE, /* FOTG210_HRTIMER_DISABLE_PERIODIC */
  1160. fotg210_disable_ASE, /* FOTG210_HRTIMER_DISABLE_ASYNC */
  1161. fotg210_work, /* FOTG210_HRTIMER_IO_WATCHDOG */
  1162. };
  1163. static enum hrtimer_restart fotg210_hrtimer_func(struct hrtimer *t)
  1164. {
  1165. struct fotg210_hcd *fotg210 =
  1166. container_of(t, struct fotg210_hcd, hrtimer);
  1167. ktime_t now;
  1168. unsigned long events;
  1169. unsigned long flags;
  1170. unsigned e;
  1171. spin_lock_irqsave(&fotg210->lock, flags);
  1172. events = fotg210->enabled_hrtimer_events;
  1173. fotg210->enabled_hrtimer_events = 0;
  1174. fotg210->next_hrtimer_event = FOTG210_HRTIMER_NO_EVENT;
  1175. /*
  1176. * Check each pending event. If its time has expired, handle
  1177. * the event; otherwise re-enable it.
  1178. */
  1179. now = ktime_get();
  1180. for_each_set_bit(e, &events, FOTG210_HRTIMER_NUM_EVENTS) {
  1181. if (now >= fotg210->hr_timeouts[e])
  1182. event_handlers[e](fotg210);
  1183. else
  1184. fotg210_enable_event(fotg210, e, false);
  1185. }
  1186. spin_unlock_irqrestore(&fotg210->lock, flags);
  1187. return HRTIMER_NORESTART;
  1188. }
  1189. #define fotg210_bus_suspend NULL
  1190. #define fotg210_bus_resume NULL
  1191. static int check_reset_complete(struct fotg210_hcd *fotg210, int index,
  1192. u32 __iomem *status_reg, int port_status)
  1193. {
  1194. if (!(port_status & PORT_CONNECT))
  1195. return port_status;
  1196. /* if reset finished and it's still not enabled -- handoff */
  1197. if (!(port_status & PORT_PE))
  1198. /* with integrated TT, there's nobody to hand it to! */
  1199. fotg210_dbg(fotg210, "Failed to enable port %d on root hub TT\n",
  1200. index + 1);
  1201. else
  1202. fotg210_dbg(fotg210, "port %d reset complete, port enabled\n",
  1203. index + 1);
  1204. return port_status;
  1205. }
  1206. /* build "status change" packet (one or two bytes) from HC registers */
  1207. static int fotg210_hub_status_data(struct usb_hcd *hcd, char *buf)
  1208. {
  1209. struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  1210. u32 temp, status;
  1211. u32 mask;
  1212. int retval = 1;
  1213. unsigned long flags;
  1214. /* init status to no-changes */
  1215. buf[0] = 0;
  1216. /* Inform the core about resumes-in-progress by returning
  1217. * a non-zero value even if there are no status changes.
  1218. */
  1219. status = fotg210->resuming_ports;
  1220. mask = PORT_CSC | PORT_PEC;
  1221. /* PORT_RESUME from hardware ~= PORT_STAT_C_SUSPEND */
  1222. /* no hub change reports (bit 0) for now (power, ...) */
  1223. /* port N changes (bit N)? */
  1224. spin_lock_irqsave(&fotg210->lock, flags);
  1225. temp = fotg210_readl(fotg210, &fotg210->regs->port_status);
  1226. /*
  1227. * Return status information even for ports with OWNER set.
  1228. * Otherwise hub_wq wouldn't see the disconnect event when a
  1229. * high-speed device is switched over to the companion
  1230. * controller by the user.
  1231. */
  1232. if ((temp & mask) != 0 || test_bit(0, &fotg210->port_c_suspend) ||
  1233. (fotg210->reset_done[0] &&
  1234. time_after_eq(jiffies, fotg210->reset_done[0]))) {
  1235. buf[0] |= 1 << 1;
  1236. status = STS_PCD;
  1237. }
  1238. /* FIXME autosuspend idle root hubs */
  1239. spin_unlock_irqrestore(&fotg210->lock, flags);
  1240. return status ? retval : 0;
  1241. }
  1242. static void fotg210_hub_descriptor(struct fotg210_hcd *fotg210,
  1243. struct usb_hub_descriptor *desc)
  1244. {
  1245. int ports = HCS_N_PORTS(fotg210->hcs_params);
  1246. u16 temp;
  1247. desc->bDescriptorType = USB_DT_HUB;
  1248. desc->bPwrOn2PwrGood = 10; /* fotg210 1.0, 2.3.9 says 20ms max */
  1249. desc->bHubContrCurrent = 0;
  1250. desc->bNbrPorts = ports;
  1251. temp = 1 + (ports / 8);
  1252. desc->bDescLength = 7 + 2 * temp;
  1253. /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
  1254. memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
  1255. memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
  1256. temp = HUB_CHAR_INDV_PORT_OCPM; /* per-port overcurrent reporting */
  1257. temp |= HUB_CHAR_NO_LPSM; /* no power switching */
  1258. desc->wHubCharacteristics = cpu_to_le16(temp);
  1259. }
  1260. static int fotg210_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
  1261. u16 wIndex, char *buf, u16 wLength)
  1262. {
  1263. struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  1264. int ports = HCS_N_PORTS(fotg210->hcs_params);
  1265. u32 __iomem *status_reg = &fotg210->regs->port_status;
  1266. u32 temp, temp1, status;
  1267. unsigned long flags;
  1268. int retval = 0;
  1269. unsigned selector;
  1270. /*
  1271. * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
  1272. * HCS_INDICATOR may say we can change LEDs to off/amber/green.
  1273. * (track current state ourselves) ... blink for diagnostics,
  1274. * power, "this is the one", etc. EHCI spec supports this.
  1275. */
  1276. spin_lock_irqsave(&fotg210->lock, flags);
  1277. switch (typeReq) {
  1278. case ClearHubFeature:
  1279. switch (wValue) {
  1280. case C_HUB_LOCAL_POWER:
  1281. case C_HUB_OVER_CURRENT:
  1282. /* no hub-wide feature/status flags */
  1283. break;
  1284. default:
  1285. goto error;
  1286. }
  1287. break;
  1288. case ClearPortFeature:
  1289. if (!wIndex || wIndex > ports)
  1290. goto error;
  1291. wIndex--;
  1292. temp = fotg210_readl(fotg210, status_reg);
  1293. temp &= ~PORT_RWC_BITS;
  1294. /*
  1295. * Even if OWNER is set, so the port is owned by the
  1296. * companion controller, hub_wq needs to be able to clear
  1297. * the port-change status bits (especially
  1298. * USB_PORT_STAT_C_CONNECTION).
  1299. */
  1300. switch (wValue) {
  1301. case USB_PORT_FEAT_ENABLE:
  1302. fotg210_writel(fotg210, temp & ~PORT_PE, status_reg);
  1303. break;
  1304. case USB_PORT_FEAT_C_ENABLE:
  1305. fotg210_writel(fotg210, temp | PORT_PEC, status_reg);
  1306. break;
  1307. case USB_PORT_FEAT_SUSPEND:
  1308. if (temp & PORT_RESET)
  1309. goto error;
  1310. if (!(temp & PORT_SUSPEND))
  1311. break;
  1312. if ((temp & PORT_PE) == 0)
  1313. goto error;
  1314. /* resume signaling for 20 msec */
  1315. fotg210_writel(fotg210, temp | PORT_RESUME, status_reg);
  1316. fotg210->reset_done[wIndex] = jiffies
  1317. + msecs_to_jiffies(USB_RESUME_TIMEOUT);
  1318. break;
  1319. case USB_PORT_FEAT_C_SUSPEND:
  1320. clear_bit(wIndex, &fotg210->port_c_suspend);
  1321. break;
  1322. case USB_PORT_FEAT_C_CONNECTION:
  1323. fotg210_writel(fotg210, temp | PORT_CSC, status_reg);
  1324. break;
  1325. case USB_PORT_FEAT_C_OVER_CURRENT:
  1326. fotg210_writel(fotg210, temp | OTGISR_OVC,
  1327. &fotg210->regs->otgisr);
  1328. break;
  1329. case USB_PORT_FEAT_C_RESET:
  1330. /* GetPortStatus clears reset */
  1331. break;
  1332. default:
  1333. goto error;
  1334. }
  1335. fotg210_readl(fotg210, &fotg210->regs->command);
  1336. break;
  1337. case GetHubDescriptor:
  1338. fotg210_hub_descriptor(fotg210, (struct usb_hub_descriptor *)
  1339. buf);
  1340. break;
  1341. case GetHubStatus:
  1342. /* no hub-wide feature/status flags */
  1343. memset(buf, 0, 4);
  1344. /*cpu_to_le32s ((u32 *) buf); */
  1345. break;
  1346. case GetPortStatus:
  1347. if (!wIndex || wIndex > ports)
  1348. goto error;
  1349. wIndex--;
  1350. status = 0;
  1351. temp = fotg210_readl(fotg210, status_reg);
  1352. /* wPortChange bits */
  1353. if (temp & PORT_CSC)
  1354. status |= USB_PORT_STAT_C_CONNECTION << 16;
  1355. if (temp & PORT_PEC)
  1356. status |= USB_PORT_STAT_C_ENABLE << 16;
  1357. temp1 = fotg210_readl(fotg210, &fotg210->regs->otgisr);
  1358. if (temp1 & OTGISR_OVC)
  1359. status |= USB_PORT_STAT_C_OVERCURRENT << 16;
  1360. /* whoever resumes must GetPortStatus to complete it!! */
  1361. if (temp & PORT_RESUME) {
  1362. /* Remote Wakeup received? */
  1363. if (!fotg210->reset_done[wIndex]) {
  1364. /* resume signaling for 20 msec */
  1365. fotg210->reset_done[wIndex] = jiffies
  1366. + msecs_to_jiffies(20);
  1367. /* check the port again */
  1368. mod_timer(&fotg210_to_hcd(fotg210)->rh_timer,
  1369. fotg210->reset_done[wIndex]);
  1370. }
  1371. /* resume completed? */
  1372. else if (time_after_eq(jiffies,
  1373. fotg210->reset_done[wIndex])) {
  1374. clear_bit(wIndex, &fotg210->suspended_ports);
  1375. set_bit(wIndex, &fotg210->port_c_suspend);
  1376. fotg210->reset_done[wIndex] = 0;
  1377. /* stop resume signaling */
  1378. temp = fotg210_readl(fotg210, status_reg);
  1379. fotg210_writel(fotg210, temp &
  1380. ~(PORT_RWC_BITS | PORT_RESUME),
  1381. status_reg);
  1382. clear_bit(wIndex, &fotg210->resuming_ports);
  1383. retval = handshake(fotg210, status_reg,
  1384. PORT_RESUME, 0, 2000);/* 2ms */
  1385. if (retval != 0) {
  1386. fotg210_err(fotg210,
  1387. "port %d resume error %d\n",
  1388. wIndex + 1, retval);
  1389. goto error;
  1390. }
  1391. temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
  1392. }
  1393. }
  1394. /* whoever resets must GetPortStatus to complete it!! */
  1395. if ((temp & PORT_RESET) && time_after_eq(jiffies,
  1396. fotg210->reset_done[wIndex])) {
  1397. status |= USB_PORT_STAT_C_RESET << 16;
  1398. fotg210->reset_done[wIndex] = 0;
  1399. clear_bit(wIndex, &fotg210->resuming_ports);
  1400. /* force reset to complete */
  1401. fotg210_writel(fotg210,
  1402. temp & ~(PORT_RWC_BITS | PORT_RESET),
  1403. status_reg);
  1404. /* REVISIT: some hardware needs 550+ usec to clear
  1405. * this bit; seems too long to spin routinely...
  1406. */
  1407. retval = handshake(fotg210, status_reg,
  1408. PORT_RESET, 0, 1000);
  1409. if (retval != 0) {
  1410. fotg210_err(fotg210, "port %d reset error %d\n",
  1411. wIndex + 1, retval);
  1412. goto error;
  1413. }
  1414. /* see what we found out */
  1415. temp = check_reset_complete(fotg210, wIndex, status_reg,
  1416. fotg210_readl(fotg210, status_reg));
  1417. }
  1418. if (!(temp & (PORT_RESUME|PORT_RESET))) {
  1419. fotg210->reset_done[wIndex] = 0;
  1420. clear_bit(wIndex, &fotg210->resuming_ports);
  1421. }
  1422. /* transfer dedicated ports to the companion hc */
  1423. if ((temp & PORT_CONNECT) &&
  1424. test_bit(wIndex, &fotg210->companion_ports)) {
  1425. temp &= ~PORT_RWC_BITS;
  1426. fotg210_writel(fotg210, temp, status_reg);
  1427. fotg210_dbg(fotg210, "port %d --> companion\n",
  1428. wIndex + 1);
  1429. temp = fotg210_readl(fotg210, status_reg);
  1430. }
  1431. /*
  1432. * Even if OWNER is set, there's no harm letting hub_wq
  1433. * see the wPortStatus values (they should all be 0 except
  1434. * for PORT_POWER anyway).
  1435. */
  1436. if (temp & PORT_CONNECT) {
  1437. status |= USB_PORT_STAT_CONNECTION;
  1438. status |= fotg210_port_speed(fotg210, temp);
  1439. }
  1440. if (temp & PORT_PE)
  1441. status |= USB_PORT_STAT_ENABLE;
  1442. /* maybe the port was unsuspended without our knowledge */
  1443. if (temp & (PORT_SUSPEND|PORT_RESUME)) {
  1444. status |= USB_PORT_STAT_SUSPEND;
  1445. } else if (test_bit(wIndex, &fotg210->suspended_ports)) {
  1446. clear_bit(wIndex, &fotg210->suspended_ports);
  1447. clear_bit(wIndex, &fotg210->resuming_ports);
  1448. fotg210->reset_done[wIndex] = 0;
  1449. if (temp & PORT_PE)
  1450. set_bit(wIndex, &fotg210->port_c_suspend);
  1451. }
  1452. temp1 = fotg210_readl(fotg210, &fotg210->regs->otgisr);
  1453. if (temp1 & OTGISR_OVC)
  1454. status |= USB_PORT_STAT_OVERCURRENT;
  1455. if (temp & PORT_RESET)
  1456. status |= USB_PORT_STAT_RESET;
  1457. if (test_bit(wIndex, &fotg210->port_c_suspend))
  1458. status |= USB_PORT_STAT_C_SUSPEND << 16;
  1459. if (status & ~0xffff) /* only if wPortChange is interesting */
  1460. dbg_port(fotg210, "GetStatus", wIndex + 1, temp);
  1461. put_unaligned_le32(status, buf);
  1462. break;
  1463. case SetHubFeature:
  1464. switch (wValue) {
  1465. case C_HUB_LOCAL_POWER:
  1466. case C_HUB_OVER_CURRENT:
  1467. /* no hub-wide feature/status flags */
  1468. break;
  1469. default:
  1470. goto error;
  1471. }
  1472. break;
  1473. case SetPortFeature:
  1474. selector = wIndex >> 8;
  1475. wIndex &= 0xff;
  1476. if (!wIndex || wIndex > ports)
  1477. goto error;
  1478. wIndex--;
  1479. temp = fotg210_readl(fotg210, status_reg);
  1480. temp &= ~PORT_RWC_BITS;
  1481. switch (wValue) {
  1482. case USB_PORT_FEAT_SUSPEND:
  1483. if ((temp & PORT_PE) == 0
  1484. || (temp & PORT_RESET) != 0)
  1485. goto error;
  1486. /* After above check the port must be connected.
  1487. * Set appropriate bit thus could put phy into low power
  1488. * mode if we have hostpc feature
  1489. */
  1490. fotg210_writel(fotg210, temp | PORT_SUSPEND,
  1491. status_reg);
  1492. set_bit(wIndex, &fotg210->suspended_ports);
  1493. break;
  1494. case USB_PORT_FEAT_RESET:
  1495. if (temp & PORT_RESUME)
  1496. goto error;
  1497. /* line status bits may report this as low speed,
  1498. * which can be fine if this root hub has a
  1499. * transaction translator built in.
  1500. */
  1501. fotg210_dbg(fotg210, "port %d reset\n", wIndex + 1);
  1502. temp |= PORT_RESET;
  1503. temp &= ~PORT_PE;
  1504. /*
  1505. * caller must wait, then call GetPortStatus
  1506. * usb 2.0 spec says 50 ms resets on root
  1507. */
  1508. fotg210->reset_done[wIndex] = jiffies
  1509. + msecs_to_jiffies(50);
  1510. fotg210_writel(fotg210, temp, status_reg);
  1511. break;
  1512. /* For downstream facing ports (these): one hub port is put
  1513. * into test mode according to USB2 11.24.2.13, then the hub
  1514. * must be reset (which for root hub now means rmmod+modprobe,
  1515. * or else system reboot). See EHCI 2.3.9 and 4.14 for info
  1516. * about the EHCI-specific stuff.
  1517. */
  1518. case USB_PORT_FEAT_TEST:
  1519. if (!selector || selector > 5)
  1520. goto error;
  1521. spin_unlock_irqrestore(&fotg210->lock, flags);
  1522. fotg210_quiesce(fotg210);
  1523. spin_lock_irqsave(&fotg210->lock, flags);
  1524. /* Put all enabled ports into suspend */
  1525. temp = fotg210_readl(fotg210, status_reg) &
  1526. ~PORT_RWC_BITS;
  1527. if (temp & PORT_PE)
  1528. fotg210_writel(fotg210, temp | PORT_SUSPEND,
  1529. status_reg);
  1530. spin_unlock_irqrestore(&fotg210->lock, flags);
  1531. fotg210_halt(fotg210);
  1532. spin_lock_irqsave(&fotg210->lock, flags);
  1533. temp = fotg210_readl(fotg210, status_reg);
  1534. temp |= selector << 16;
  1535. fotg210_writel(fotg210, temp, status_reg);
  1536. break;
  1537. default:
  1538. goto error;
  1539. }
  1540. fotg210_readl(fotg210, &fotg210->regs->command);
  1541. break;
  1542. default:
  1543. error:
  1544. /* "stall" on error */
  1545. retval = -EPIPE;
  1546. }
  1547. spin_unlock_irqrestore(&fotg210->lock, flags);
  1548. return retval;
  1549. }
  1550. static void __maybe_unused fotg210_relinquish_port(struct usb_hcd *hcd,
  1551. int portnum)
  1552. {
  1553. return;
  1554. }
  1555. static int __maybe_unused fotg210_port_handed_over(struct usb_hcd *hcd,
  1556. int portnum)
  1557. {
  1558. return 0;
  1559. }
  1560. /* There's basically three types of memory:
  1561. * - data used only by the HCD ... kmalloc is fine
  1562. * - async and periodic schedules, shared by HC and HCD ... these
  1563. * need to use dma_pool or dma_alloc_coherent
  1564. * - driver buffers, read/written by HC ... single shot DMA mapped
  1565. *
  1566. * There's also "register" data (e.g. PCI or SOC), which is memory mapped.
  1567. * No memory seen by this driver is pageable.
  1568. */
  1569. /* Allocate the key transfer structures from the previously allocated pool */
  1570. static inline void fotg210_qtd_init(struct fotg210_hcd *fotg210,
  1571. struct fotg210_qtd *qtd, dma_addr_t dma)
  1572. {
  1573. memset(qtd, 0, sizeof(*qtd));
  1574. qtd->qtd_dma = dma;
  1575. qtd->hw_token = cpu_to_hc32(fotg210, QTD_STS_HALT);
  1576. qtd->hw_next = FOTG210_LIST_END(fotg210);
  1577. qtd->hw_alt_next = FOTG210_LIST_END(fotg210);
  1578. INIT_LIST_HEAD(&qtd->qtd_list);
  1579. }
  1580. static struct fotg210_qtd *fotg210_qtd_alloc(struct fotg210_hcd *fotg210,
  1581. gfp_t flags)
  1582. {
  1583. struct fotg210_qtd *qtd;
  1584. dma_addr_t dma;
  1585. qtd = dma_pool_alloc(fotg210->qtd_pool, flags, &dma);
  1586. if (qtd != NULL)
  1587. fotg210_qtd_init(fotg210, qtd, dma);
  1588. return qtd;
  1589. }
  1590. static inline void fotg210_qtd_free(struct fotg210_hcd *fotg210,
  1591. struct fotg210_qtd *qtd)
  1592. {
  1593. dma_pool_free(fotg210->qtd_pool, qtd, qtd->qtd_dma);
  1594. }
  1595. static void qh_destroy(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
  1596. {
  1597. /* clean qtds first, and know this is not linked */
  1598. if (!list_empty(&qh->qtd_list) || qh->qh_next.ptr) {
  1599. fotg210_dbg(fotg210, "unused qh not empty!\n");
  1600. BUG();
  1601. }
  1602. if (qh->dummy)
  1603. fotg210_qtd_free(fotg210, qh->dummy);
  1604. dma_pool_free(fotg210->qh_pool, qh->hw, qh->qh_dma);
  1605. kfree(qh);
  1606. }
  1607. static struct fotg210_qh *fotg210_qh_alloc(struct fotg210_hcd *fotg210,
  1608. gfp_t flags)
  1609. {
  1610. struct fotg210_qh *qh;
  1611. dma_addr_t dma;
  1612. qh = kzalloc(sizeof(*qh), GFP_ATOMIC);
  1613. if (!qh)
  1614. goto done;
  1615. qh->hw = (struct fotg210_qh_hw *)
  1616. dma_pool_alloc(fotg210->qh_pool, flags, &dma);
  1617. if (!qh->hw)
  1618. goto fail;
  1619. memset(qh->hw, 0, sizeof(*qh->hw));
  1620. qh->qh_dma = dma;
  1621. INIT_LIST_HEAD(&qh->qtd_list);
  1622. /* dummy td enables safe urb queuing */
  1623. qh->dummy = fotg210_qtd_alloc(fotg210, flags);
  1624. if (qh->dummy == NULL) {
  1625. fotg210_dbg(fotg210, "no dummy td\n");
  1626. goto fail1;
  1627. }
  1628. done:
  1629. return qh;
  1630. fail1:
  1631. dma_pool_free(fotg210->qh_pool, qh->hw, qh->qh_dma);
  1632. fail:
  1633. kfree(qh);
  1634. return NULL;
  1635. }
  1636. /* The queue heads and transfer descriptors are managed from pools tied
  1637. * to each of the "per device" structures.
  1638. * This is the initialisation and cleanup code.
  1639. */
  1640. static void fotg210_mem_cleanup(struct fotg210_hcd *fotg210)
  1641. {
  1642. if (fotg210->async)
  1643. qh_destroy(fotg210, fotg210->async);
  1644. fotg210->async = NULL;
  1645. if (fotg210->dummy)
  1646. qh_destroy(fotg210, fotg210->dummy);
  1647. fotg210->dummy = NULL;
  1648. /* DMA consistent memory and pools */
  1649. dma_pool_destroy(fotg210->qtd_pool);
  1650. fotg210->qtd_pool = NULL;
  1651. dma_pool_destroy(fotg210->qh_pool);
  1652. fotg210->qh_pool = NULL;
  1653. dma_pool_destroy(fotg210->itd_pool);
  1654. fotg210->itd_pool = NULL;
  1655. if (fotg210->periodic)
  1656. dma_free_coherent(fotg210_to_hcd(fotg210)->self.controller,
  1657. fotg210->periodic_size * sizeof(u32),
  1658. fotg210->periodic, fotg210->periodic_dma);
  1659. fotg210->periodic = NULL;
  1660. /* shadow periodic table */
  1661. kfree(fotg210->pshadow);
  1662. fotg210->pshadow = NULL;
  1663. }
  1664. /* remember to add cleanup code (above) if you add anything here */
  1665. static int fotg210_mem_init(struct fotg210_hcd *fotg210, gfp_t flags)
  1666. {
  1667. int i;
  1668. /* QTDs for control/bulk/intr transfers */
  1669. fotg210->qtd_pool = dma_pool_create("fotg210_qtd",
  1670. fotg210_to_hcd(fotg210)->self.controller,
  1671. sizeof(struct fotg210_qtd),
  1672. 32 /* byte alignment (for hw parts) */,
  1673. 4096 /* can't cross 4K */);
  1674. if (!fotg210->qtd_pool)
  1675. goto fail;
  1676. /* QHs for control/bulk/intr transfers */
  1677. fotg210->qh_pool = dma_pool_create("fotg210_qh",
  1678. fotg210_to_hcd(fotg210)->self.controller,
  1679. sizeof(struct fotg210_qh_hw),
  1680. 32 /* byte alignment (for hw parts) */,
  1681. 4096 /* can't cross 4K */);
  1682. if (!fotg210->qh_pool)
  1683. goto fail;
  1684. fotg210->async = fotg210_qh_alloc(fotg210, flags);
  1685. if (!fotg210->async)
  1686. goto fail;
  1687. /* ITD for high speed ISO transfers */
  1688. fotg210->itd_pool = dma_pool_create("fotg210_itd",
  1689. fotg210_to_hcd(fotg210)->self.controller,
  1690. sizeof(struct fotg210_itd),
  1691. 64 /* byte alignment (for hw parts) */,
  1692. 4096 /* can't cross 4K */);
  1693. if (!fotg210->itd_pool)
  1694. goto fail;
  1695. /* Hardware periodic table */
  1696. fotg210->periodic = (__le32 *)
  1697. dma_alloc_coherent(fotg210_to_hcd(fotg210)->self.controller,
  1698. fotg210->periodic_size * sizeof(__le32),
  1699. &fotg210->periodic_dma, 0);
  1700. if (fotg210->periodic == NULL)
  1701. goto fail;
  1702. for (i = 0; i < fotg210->periodic_size; i++)
  1703. fotg210->periodic[i] = FOTG210_LIST_END(fotg210);
  1704. /* software shadow of hardware table */
  1705. fotg210->pshadow = kcalloc(fotg210->periodic_size, sizeof(void *),
  1706. flags);
  1707. if (fotg210->pshadow != NULL)
  1708. return 0;
  1709. fail:
  1710. fotg210_dbg(fotg210, "couldn't init memory\n");
  1711. fotg210_mem_cleanup(fotg210);
  1712. return -ENOMEM;
  1713. }
  1714. /* EHCI hardware queue manipulation ... the core. QH/QTD manipulation.
  1715. *
  1716. * Control, bulk, and interrupt traffic all use "qh" lists. They list "qtd"
  1717. * entries describing USB transactions, max 16-20kB/entry (with 4kB-aligned
  1718. * buffers needed for the larger number). We use one QH per endpoint, queue
  1719. * multiple urbs (all three types) per endpoint. URBs may need several qtds.
  1720. *
  1721. * ISO traffic uses "ISO TD" (itd) records, and (along with
  1722. * interrupts) needs careful scheduling. Performance improvements can be
  1723. * an ongoing challenge. That's in "ehci-sched.c".
  1724. *
  1725. * USB 1.1 devices are handled (a) by "companion" OHCI or UHCI root hubs,
  1726. * or otherwise through transaction translators (TTs) in USB 2.0 hubs using
  1727. * (b) special fields in qh entries or (c) split iso entries. TTs will
  1728. * buffer low/full speed data so the host collects it at high speed.
  1729. */
  1730. /* fill a qtd, returning how much of the buffer we were able to queue up */
  1731. static int qtd_fill(struct fotg210_hcd *fotg210, struct fotg210_qtd *qtd,
  1732. dma_addr_t buf, size_t len, int token, int maxpacket)
  1733. {
  1734. int i, count;
  1735. u64 addr = buf;
  1736. /* one buffer entry per 4K ... first might be short or unaligned */
  1737. qtd->hw_buf[0] = cpu_to_hc32(fotg210, (u32)addr);
  1738. qtd->hw_buf_hi[0] = cpu_to_hc32(fotg210, (u32)(addr >> 32));
  1739. count = 0x1000 - (buf & 0x0fff); /* rest of that page */
  1740. if (likely(len < count)) /* ... iff needed */
  1741. count = len;
  1742. else {
  1743. buf += 0x1000;
  1744. buf &= ~0x0fff;
  1745. /* per-qtd limit: from 16K to 20K (best alignment) */
  1746. for (i = 1; count < len && i < 5; i++) {
  1747. addr = buf;
  1748. qtd->hw_buf[i] = cpu_to_hc32(fotg210, (u32)addr);
  1749. qtd->hw_buf_hi[i] = cpu_to_hc32(fotg210,
  1750. (u32)(addr >> 32));
  1751. buf += 0x1000;
  1752. if ((count + 0x1000) < len)
  1753. count += 0x1000;
  1754. else
  1755. count = len;
  1756. }
  1757. /* short packets may only terminate transfers */
  1758. if (count != len)
  1759. count -= (count % maxpacket);
  1760. }
  1761. qtd->hw_token = cpu_to_hc32(fotg210, (count << 16) | token);
  1762. qtd->length = count;
  1763. return count;
  1764. }
  1765. static inline void qh_update(struct fotg210_hcd *fotg210,
  1766. struct fotg210_qh *qh, struct fotg210_qtd *qtd)
  1767. {
  1768. struct fotg210_qh_hw *hw = qh->hw;
  1769. /* writes to an active overlay are unsafe */
  1770. BUG_ON(qh->qh_state != QH_STATE_IDLE);
  1771. hw->hw_qtd_next = QTD_NEXT(fotg210, qtd->qtd_dma);
  1772. hw->hw_alt_next = FOTG210_LIST_END(fotg210);
  1773. /* Except for control endpoints, we make hardware maintain data
  1774. * toggle (like OHCI) ... here (re)initialize the toggle in the QH,
  1775. * and set the pseudo-toggle in udev. Only usb_clear_halt() will
  1776. * ever clear it.
  1777. */
  1778. if (!(hw->hw_info1 & cpu_to_hc32(fotg210, QH_TOGGLE_CTL))) {
  1779. unsigned is_out, epnum;
  1780. is_out = qh->is_out;
  1781. epnum = (hc32_to_cpup(fotg210, &hw->hw_info1) >> 8) & 0x0f;
  1782. if (unlikely(!usb_gettoggle(qh->dev, epnum, is_out))) {
  1783. hw->hw_token &= ~cpu_to_hc32(fotg210, QTD_TOGGLE);
  1784. usb_settoggle(qh->dev, epnum, is_out, 1);
  1785. }
  1786. }
  1787. hw->hw_token &= cpu_to_hc32(fotg210, QTD_TOGGLE | QTD_STS_PING);
  1788. }
  1789. /* if it weren't for a common silicon quirk (writing the dummy into the qh
  1790. * overlay, so qh->hw_token wrongly becomes inactive/halted), only fault
  1791. * recovery (including urb dequeue) would need software changes to a QH...
  1792. */
  1793. static void qh_refresh(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
  1794. {
  1795. struct fotg210_qtd *qtd;
  1796. if (list_empty(&qh->qtd_list))
  1797. qtd = qh->dummy;
  1798. else {
  1799. qtd = list_entry(qh->qtd_list.next,
  1800. struct fotg210_qtd, qtd_list);
  1801. /*
  1802. * first qtd may already be partially processed.
  1803. * If we come here during unlink, the QH overlay region
  1804. * might have reference to the just unlinked qtd. The
  1805. * qtd is updated in qh_completions(). Update the QH
  1806. * overlay here.
  1807. */
  1808. if (cpu_to_hc32(fotg210, qtd->qtd_dma) == qh->hw->hw_current) {
  1809. qh->hw->hw_qtd_next = qtd->hw_next;
  1810. qtd = NULL;
  1811. }
  1812. }
  1813. if (qtd)
  1814. qh_update(fotg210, qh, qtd);
  1815. }
  1816. static void qh_link_async(struct fotg210_hcd *fotg210, struct fotg210_qh *qh);
  1817. static void fotg210_clear_tt_buffer_complete(struct usb_hcd *hcd,
  1818. struct usb_host_endpoint *ep)
  1819. {
  1820. struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  1821. struct fotg210_qh *qh = ep->hcpriv;
  1822. unsigned long flags;
  1823. spin_lock_irqsave(&fotg210->lock, flags);
  1824. qh->clearing_tt = 0;
  1825. if (qh->qh_state == QH_STATE_IDLE && !list_empty(&qh->qtd_list)
  1826. && fotg210->rh_state == FOTG210_RH_RUNNING)
  1827. qh_link_async(fotg210, qh);
  1828. spin_unlock_irqrestore(&fotg210->lock, flags);
  1829. }
  1830. static void fotg210_clear_tt_buffer(struct fotg210_hcd *fotg210,
  1831. struct fotg210_qh *qh, struct urb *urb, u32 token)
  1832. {
  1833. /* If an async split transaction gets an error or is unlinked,
  1834. * the TT buffer may be left in an indeterminate state. We
  1835. * have to clear the TT buffer.
  1836. *
  1837. * Note: this routine is never called for Isochronous transfers.
  1838. */
  1839. if (urb->dev->tt && !usb_pipeint(urb->pipe) && !qh->clearing_tt) {
  1840. struct usb_device *tt = urb->dev->tt->hub;
  1841. dev_dbg(&tt->dev,
  1842. "clear tt buffer port %d, a%d ep%d t%08x\n",
  1843. urb->dev->ttport, urb->dev->devnum,
  1844. usb_pipeendpoint(urb->pipe), token);
  1845. if (urb->dev->tt->hub !=
  1846. fotg210_to_hcd(fotg210)->self.root_hub) {
  1847. if (usb_hub_clear_tt_buffer(urb) == 0)
  1848. qh->clearing_tt = 1;
  1849. }
  1850. }
  1851. }
  1852. static int qtd_copy_status(struct fotg210_hcd *fotg210, struct urb *urb,
  1853. size_t length, u32 token)
  1854. {
  1855. int status = -EINPROGRESS;
  1856. /* count IN/OUT bytes, not SETUP (even short packets) */
  1857. if (likely(QTD_PID(token) != 2))
  1858. urb->actual_length += length - QTD_LENGTH(token);
  1859. /* don't modify error codes */
  1860. if (unlikely(urb->unlinked))
  1861. return status;
  1862. /* force cleanup after short read; not always an error */
  1863. if (unlikely(IS_SHORT_READ(token)))
  1864. status = -EREMOTEIO;
  1865. /* serious "can't proceed" faults reported by the hardware */
  1866. if (token & QTD_STS_HALT) {
  1867. if (token & QTD_STS_BABBLE) {
  1868. /* FIXME "must" disable babbling device's port too */
  1869. status = -EOVERFLOW;
  1870. /* CERR nonzero + halt --> stall */
  1871. } else if (QTD_CERR(token)) {
  1872. status = -EPIPE;
  1873. /* In theory, more than one of the following bits can be set
  1874. * since they are sticky and the transaction is retried.
  1875. * Which to test first is rather arbitrary.
  1876. */
  1877. } else if (token & QTD_STS_MMF) {
  1878. /* fs/ls interrupt xfer missed the complete-split */
  1879. status = -EPROTO;
  1880. } else if (token & QTD_STS_DBE) {
  1881. status = (QTD_PID(token) == 1) /* IN ? */
  1882. ? -ENOSR /* hc couldn't read data */
  1883. : -ECOMM; /* hc couldn't write data */
  1884. } else if (token & QTD_STS_XACT) {
  1885. /* timeout, bad CRC, wrong PID, etc */
  1886. fotg210_dbg(fotg210, "devpath %s ep%d%s 3strikes\n",
  1887. urb->dev->devpath,
  1888. usb_pipeendpoint(urb->pipe),
  1889. usb_pipein(urb->pipe) ? "in" : "out");
  1890. status = -EPROTO;
  1891. } else { /* unknown */
  1892. status = -EPROTO;
  1893. }
  1894. fotg210_dbg(fotg210,
  1895. "dev%d ep%d%s qtd token %08x --> status %d\n",
  1896. usb_pipedevice(urb->pipe),
  1897. usb_pipeendpoint(urb->pipe),
  1898. usb_pipein(urb->pipe) ? "in" : "out",
  1899. token, status);
  1900. }
  1901. return status;
  1902. }
  1903. static void fotg210_urb_done(struct fotg210_hcd *fotg210, struct urb *urb,
  1904. int status)
  1905. __releases(fotg210->lock)
  1906. __acquires(fotg210->lock)
  1907. {
  1908. if (likely(urb->hcpriv != NULL)) {
  1909. struct fotg210_qh *qh = (struct fotg210_qh *) urb->hcpriv;
  1910. /* S-mask in a QH means it's an interrupt urb */
  1911. if ((qh->hw->hw_info2 & cpu_to_hc32(fotg210, QH_SMASK)) != 0) {
  1912. /* ... update hc-wide periodic stats (for usbfs) */
  1913. fotg210_to_hcd(fotg210)->self.bandwidth_int_reqs--;
  1914. }
  1915. }
  1916. if (unlikely(urb->unlinked)) {
  1917. COUNT(fotg210->stats.unlink);
  1918. } else {
  1919. /* report non-error and short read status as zero */
  1920. if (status == -EINPROGRESS || status == -EREMOTEIO)
  1921. status = 0;
  1922. COUNT(fotg210->stats.complete);
  1923. }
  1924. #ifdef FOTG210_URB_TRACE
  1925. fotg210_dbg(fotg210,
  1926. "%s %s urb %p ep%d%s status %d len %d/%d\n",
  1927. __func__, urb->dev->devpath, urb,
  1928. usb_pipeendpoint(urb->pipe),
  1929. usb_pipein(urb->pipe) ? "in" : "out",
  1930. status,
  1931. urb->actual_length, urb->transfer_buffer_length);
  1932. #endif
  1933. /* complete() can reenter this HCD */
  1934. usb_hcd_unlink_urb_from_ep(fotg210_to_hcd(fotg210), urb);
  1935. spin_unlock(&fotg210->lock);
  1936. usb_hcd_giveback_urb(fotg210_to_hcd(fotg210), urb, status);
  1937. spin_lock(&fotg210->lock);
  1938. }
  1939. static int qh_schedule(struct fotg210_hcd *fotg210, struct fotg210_qh *qh);
  1940. /* Process and free completed qtds for a qh, returning URBs to drivers.
  1941. * Chases up to qh->hw_current. Returns number of completions called,
  1942. * indicating how much "real" work we did.
  1943. */
  1944. static unsigned qh_completions(struct fotg210_hcd *fotg210,
  1945. struct fotg210_qh *qh)
  1946. {
  1947. struct fotg210_qtd *last, *end = qh->dummy;
  1948. struct fotg210_qtd *qtd, *tmp;
  1949. int last_status;
  1950. int stopped;
  1951. unsigned count = 0;
  1952. u8 state;
  1953. struct fotg210_qh_hw *hw = qh->hw;
  1954. if (unlikely(list_empty(&qh->qtd_list)))
  1955. return count;
  1956. /* completions (or tasks on other cpus) must never clobber HALT
  1957. * till we've gone through and cleaned everything up, even when
  1958. * they add urbs to this qh's queue or mark them for unlinking.
  1959. *
  1960. * NOTE: unlinking expects to be done in queue order.
  1961. *
  1962. * It's a bug for qh->qh_state to be anything other than
  1963. * QH_STATE_IDLE, unless our caller is scan_async() or
  1964. * scan_intr().
  1965. */
  1966. state = qh->qh_state;
  1967. qh->qh_state = QH_STATE_COMPLETING;
  1968. stopped = (state == QH_STATE_IDLE);
  1969. rescan:
  1970. last = NULL;
  1971. last_status = -EINPROGRESS;
  1972. qh->needs_rescan = 0;
  1973. /* remove de-activated QTDs from front of queue.
  1974. * after faults (including short reads), cleanup this urb
  1975. * then let the queue advance.
  1976. * if queue is stopped, handles unlinks.
  1977. */
  1978. list_for_each_entry_safe(qtd, tmp, &qh->qtd_list, qtd_list) {
  1979. struct urb *urb;
  1980. u32 token = 0;
  1981. urb = qtd->urb;
  1982. /* clean up any state from previous QTD ...*/
  1983. if (last) {
  1984. if (likely(last->urb != urb)) {
  1985. fotg210_urb_done(fotg210, last->urb,
  1986. last_status);
  1987. count++;
  1988. last_status = -EINPROGRESS;
  1989. }
  1990. fotg210_qtd_free(fotg210, last);
  1991. last = NULL;
  1992. }
  1993. /* ignore urbs submitted during completions we reported */
  1994. if (qtd == end)
  1995. break;
  1996. /* hardware copies qtd out of qh overlay */
  1997. rmb();
  1998. token = hc32_to_cpu(fotg210, qtd->hw_token);
  1999. /* always clean up qtds the hc de-activated */
  2000. retry_xacterr:
  2001. if ((token & QTD_STS_ACTIVE) == 0) {
  2002. /* Report Data Buffer Error: non-fatal but useful */
  2003. if (token & QTD_STS_DBE)
  2004. fotg210_dbg(fotg210,
  2005. "detected DataBufferErr for urb %p ep%d%s len %d, qtd %p [qh %p]\n",
  2006. urb, usb_endpoint_num(&urb->ep->desc),
  2007. usb_endpoint_dir_in(&urb->ep->desc)
  2008. ? "in" : "out",
  2009. urb->transfer_buffer_length, qtd, qh);
  2010. /* on STALL, error, and short reads this urb must
  2011. * complete and all its qtds must be recycled.
  2012. */
  2013. if ((token & QTD_STS_HALT) != 0) {
  2014. /* retry transaction errors until we
  2015. * reach the software xacterr limit
  2016. */
  2017. if ((token & QTD_STS_XACT) &&
  2018. QTD_CERR(token) == 0 &&
  2019. ++qh->xacterrs < QH_XACTERR_MAX &&
  2020. !urb->unlinked) {
  2021. fotg210_dbg(fotg210,
  2022. "detected XactErr len %zu/%zu retry %d\n",
  2023. qtd->length - QTD_LENGTH(token),
  2024. qtd->length,
  2025. qh->xacterrs);
  2026. /* reset the token in the qtd and the
  2027. * qh overlay (which still contains
  2028. * the qtd) so that we pick up from
  2029. * where we left off
  2030. */
  2031. token &= ~QTD_STS_HALT;
  2032. token |= QTD_STS_ACTIVE |
  2033. (FOTG210_TUNE_CERR << 10);
  2034. qtd->hw_token = cpu_to_hc32(fotg210,
  2035. token);
  2036. wmb();
  2037. hw->hw_token = cpu_to_hc32(fotg210,
  2038. token);
  2039. goto retry_xacterr;
  2040. }
  2041. stopped = 1;
  2042. /* magic dummy for some short reads; qh won't advance.
  2043. * that silicon quirk can kick in with this dummy too.
  2044. *
  2045. * other short reads won't stop the queue, including
  2046. * control transfers (status stage handles that) or
  2047. * most other single-qtd reads ... the queue stops if
  2048. * URB_SHORT_NOT_OK was set so the driver submitting
  2049. * the urbs could clean it up.
  2050. */
  2051. } else if (IS_SHORT_READ(token) &&
  2052. !(qtd->hw_alt_next &
  2053. FOTG210_LIST_END(fotg210))) {
  2054. stopped = 1;
  2055. }
  2056. /* stop scanning when we reach qtds the hc is using */
  2057. } else if (likely(!stopped
  2058. && fotg210->rh_state >= FOTG210_RH_RUNNING)) {
  2059. break;
  2060. /* scan the whole queue for unlinks whenever it stops */
  2061. } else {
  2062. stopped = 1;
  2063. /* cancel everything if we halt, suspend, etc */
  2064. if (fotg210->rh_state < FOTG210_RH_RUNNING)
  2065. last_status = -ESHUTDOWN;
  2066. /* this qtd is active; skip it unless a previous qtd
  2067. * for its urb faulted, or its urb was canceled.
  2068. */
  2069. else if (last_status == -EINPROGRESS && !urb->unlinked)
  2070. continue;
  2071. /* qh unlinked; token in overlay may be most current */
  2072. if (state == QH_STATE_IDLE &&
  2073. cpu_to_hc32(fotg210, qtd->qtd_dma)
  2074. == hw->hw_current) {
  2075. token = hc32_to_cpu(fotg210, hw->hw_token);
  2076. /* An unlink may leave an incomplete
  2077. * async transaction in the TT buffer.
  2078. * We have to clear it.
  2079. */
  2080. fotg210_clear_tt_buffer(fotg210, qh, urb,
  2081. token);
  2082. }
  2083. }
  2084. /* unless we already know the urb's status, collect qtd status
  2085. * and update count of bytes transferred. in common short read
  2086. * cases with only one data qtd (including control transfers),
  2087. * queue processing won't halt. but with two or more qtds (for
  2088. * example, with a 32 KB transfer), when the first qtd gets a
  2089. * short read the second must be removed by hand.
  2090. */
  2091. if (last_status == -EINPROGRESS) {
  2092. last_status = qtd_copy_status(fotg210, urb,
  2093. qtd->length, token);
  2094. if (last_status == -EREMOTEIO &&
  2095. (qtd->hw_alt_next &
  2096. FOTG210_LIST_END(fotg210)))
  2097. last_status = -EINPROGRESS;
  2098. /* As part of low/full-speed endpoint-halt processing
  2099. * we must clear the TT buffer (11.17.5).
  2100. */
  2101. if (unlikely(last_status != -EINPROGRESS &&
  2102. last_status != -EREMOTEIO)) {
  2103. /* The TT's in some hubs malfunction when they
  2104. * receive this request following a STALL (they
  2105. * stop sending isochronous packets). Since a
  2106. * STALL can't leave the TT buffer in a busy
  2107. * state (if you believe Figures 11-48 - 11-51
  2108. * in the USB 2.0 spec), we won't clear the TT
  2109. * buffer in this case. Strictly speaking this
  2110. * is a violation of the spec.
  2111. */
  2112. if (last_status != -EPIPE)
  2113. fotg210_clear_tt_buffer(fotg210, qh,
  2114. urb, token);
  2115. }
  2116. }
  2117. /* if we're removing something not at the queue head,
  2118. * patch the hardware queue pointer.
  2119. */
  2120. if (stopped && qtd->qtd_list.prev != &qh->qtd_list) {
  2121. last = list_entry(qtd->qtd_list.prev,
  2122. struct fotg210_qtd, qtd_list);
  2123. last->hw_next = qtd->hw_next;
  2124. }
  2125. /* remove qtd; it's recycled after possible urb completion */
  2126. list_del(&qtd->qtd_list);
  2127. last = qtd;
  2128. /* reinit the xacterr counter for the next qtd */
  2129. qh->xacterrs = 0;
  2130. }
  2131. /* last urb's completion might still need calling */
  2132. if (likely(last != NULL)) {
  2133. fotg210_urb_done(fotg210, last->urb, last_status);
  2134. count++;
  2135. fotg210_qtd_free(fotg210, last);
  2136. }
  2137. /* Do we need to rescan for URBs dequeued during a giveback? */
  2138. if (unlikely(qh->needs_rescan)) {
  2139. /* If the QH is already unlinked, do the rescan now. */
  2140. if (state == QH_STATE_IDLE)
  2141. goto rescan;
  2142. /* Otherwise we have to wait until the QH is fully unlinked.
  2143. * Our caller will start an unlink if qh->needs_rescan is
  2144. * set. But if an unlink has already started, nothing needs
  2145. * to be done.
  2146. */
  2147. if (state != QH_STATE_LINKED)
  2148. qh->needs_rescan = 0;
  2149. }
  2150. /* restore original state; caller must unlink or relink */
  2151. qh->qh_state = state;
  2152. /* be sure the hardware's done with the qh before refreshing
  2153. * it after fault cleanup, or recovering from silicon wrongly
  2154. * overlaying the dummy qtd (which reduces DMA chatter).
  2155. */
  2156. if (stopped != 0 || hw->hw_qtd_next == FOTG210_LIST_END(fotg210)) {
  2157. switch (state) {
  2158. case QH_STATE_IDLE:
  2159. qh_refresh(fotg210, qh);
  2160. break;
  2161. case QH_STATE_LINKED:
  2162. /* We won't refresh a QH that's linked (after the HC
  2163. * stopped the queue). That avoids a race:
  2164. * - HC reads first part of QH;
  2165. * - CPU updates that first part and the token;
  2166. * - HC reads rest of that QH, including token
  2167. * Result: HC gets an inconsistent image, and then
  2168. * DMAs to/from the wrong memory (corrupting it).
  2169. *
  2170. * That should be rare for interrupt transfers,
  2171. * except maybe high bandwidth ...
  2172. */
  2173. /* Tell the caller to start an unlink */
  2174. qh->needs_rescan = 1;
  2175. break;
  2176. /* otherwise, unlink already started */
  2177. }
  2178. }
  2179. return count;
  2180. }
  2181. /* high bandwidth multiplier, as encoded in highspeed endpoint descriptors */
  2182. #define hb_mult(wMaxPacketSize) (1 + (((wMaxPacketSize) >> 11) & 0x03))
  2183. /* ... and packet size, for any kind of endpoint descriptor */
  2184. #define max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
  2185. /* reverse of qh_urb_transaction: free a list of TDs.
  2186. * used for cleanup after errors, before HC sees an URB's TDs.
  2187. */
  2188. static void qtd_list_free(struct fotg210_hcd *fotg210, struct urb *urb,
  2189. struct list_head *head)
  2190. {
  2191. struct fotg210_qtd *qtd, *temp;
  2192. list_for_each_entry_safe(qtd, temp, head, qtd_list) {
  2193. list_del(&qtd->qtd_list);
  2194. fotg210_qtd_free(fotg210, qtd);
  2195. }
  2196. }
  2197. /* create a list of filled qtds for this URB; won't link into qh.
  2198. */
  2199. static struct list_head *qh_urb_transaction(struct fotg210_hcd *fotg210,
  2200. struct urb *urb, struct list_head *head, gfp_t flags)
  2201. {
  2202. struct fotg210_qtd *qtd, *qtd_prev;
  2203. dma_addr_t buf;
  2204. int len, this_sg_len, maxpacket;
  2205. int is_input;
  2206. u32 token;
  2207. int i;
  2208. struct scatterlist *sg;
  2209. /*
  2210. * URBs map to sequences of QTDs: one logical transaction
  2211. */
  2212. qtd = fotg210_qtd_alloc(fotg210, flags);
  2213. if (unlikely(!qtd))
  2214. return NULL;
  2215. list_add_tail(&qtd->qtd_list, head);
  2216. qtd->urb = urb;
  2217. token = QTD_STS_ACTIVE;
  2218. token |= (FOTG210_TUNE_CERR << 10);
  2219. /* for split transactions, SplitXState initialized to zero */
  2220. len = urb->transfer_buffer_length;
  2221. is_input = usb_pipein(urb->pipe);
  2222. if (usb_pipecontrol(urb->pipe)) {
  2223. /* SETUP pid */
  2224. qtd_fill(fotg210, qtd, urb->setup_dma,
  2225. sizeof(struct usb_ctrlrequest),
  2226. token | (2 /* "setup" */ << 8), 8);
  2227. /* ... and always at least one more pid */
  2228. token ^= QTD_TOGGLE;
  2229. qtd_prev = qtd;
  2230. qtd = fotg210_qtd_alloc(fotg210, flags);
  2231. if (unlikely(!qtd))
  2232. goto cleanup;
  2233. qtd->urb = urb;
  2234. qtd_prev->hw_next = QTD_NEXT(fotg210, qtd->qtd_dma);
  2235. list_add_tail(&qtd->qtd_list, head);
  2236. /* for zero length DATA stages, STATUS is always IN */
  2237. if (len == 0)
  2238. token |= (1 /* "in" */ << 8);
  2239. }
  2240. /*
  2241. * data transfer stage: buffer setup
  2242. */
  2243. i = urb->num_mapped_sgs;
  2244. if (len > 0 && i > 0) {
  2245. sg = urb->sg;
  2246. buf = sg_dma_address(sg);
  2247. /* urb->transfer_buffer_length may be smaller than the
  2248. * size of the scatterlist (or vice versa)
  2249. */
  2250. this_sg_len = min_t(int, sg_dma_len(sg), len);
  2251. } else {
  2252. sg = NULL;
  2253. buf = urb->transfer_dma;
  2254. this_sg_len = len;
  2255. }
  2256. if (is_input)
  2257. token |= (1 /* "in" */ << 8);
  2258. /* else it's already initted to "out" pid (0 << 8) */
  2259. maxpacket = max_packet(usb_maxpacket(urb->dev, urb->pipe, !is_input));
  2260. /*
  2261. * buffer gets wrapped in one or more qtds;
  2262. * last one may be "short" (including zero len)
  2263. * and may serve as a control status ack
  2264. */
  2265. for (;;) {
  2266. int this_qtd_len;
  2267. this_qtd_len = qtd_fill(fotg210, qtd, buf, this_sg_len, token,
  2268. maxpacket);
  2269. this_sg_len -= this_qtd_len;
  2270. len -= this_qtd_len;
  2271. buf += this_qtd_len;
  2272. /*
  2273. * short reads advance to a "magic" dummy instead of the next
  2274. * qtd ... that forces the queue to stop, for manual cleanup.
  2275. * (this will usually be overridden later.)
  2276. */
  2277. if (is_input)
  2278. qtd->hw_alt_next = fotg210->async->hw->hw_alt_next;
  2279. /* qh makes control packets use qtd toggle; maybe switch it */
  2280. if ((maxpacket & (this_qtd_len + (maxpacket - 1))) == 0)
  2281. token ^= QTD_TOGGLE;
  2282. if (likely(this_sg_len <= 0)) {
  2283. if (--i <= 0 || len <= 0)
  2284. break;
  2285. sg = sg_next(sg);
  2286. buf = sg_dma_address(sg);
  2287. this_sg_len = min_t(int, sg_dma_len(sg), len);
  2288. }
  2289. qtd_prev = qtd;
  2290. qtd = fotg210_qtd_alloc(fotg210, flags);
  2291. if (unlikely(!qtd))
  2292. goto cleanup;
  2293. qtd->urb = urb;
  2294. qtd_prev->hw_next = QTD_NEXT(fotg210, qtd->qtd_dma);
  2295. list_add_tail(&qtd->qtd_list, head);
  2296. }
  2297. /*
  2298. * unless the caller requires manual cleanup after short reads,
  2299. * have the alt_next mechanism keep the queue running after the
  2300. * last data qtd (the only one, for control and most other cases).
  2301. */
  2302. if (likely((urb->transfer_flags & URB_SHORT_NOT_OK) == 0 ||
  2303. usb_pipecontrol(urb->pipe)))
  2304. qtd->hw_alt_next = FOTG210_LIST_END(fotg210);
  2305. /*
  2306. * control requests may need a terminating data "status" ack;
  2307. * other OUT ones may need a terminating short packet
  2308. * (zero length).
  2309. */
  2310. if (likely(urb->transfer_buffer_length != 0)) {
  2311. int one_more = 0;
  2312. if (usb_pipecontrol(urb->pipe)) {
  2313. one_more = 1;
  2314. token ^= 0x0100; /* "in" <--> "out" */
  2315. token |= QTD_TOGGLE; /* force DATA1 */
  2316. } else if (usb_pipeout(urb->pipe)
  2317. && (urb->transfer_flags & URB_ZERO_PACKET)
  2318. && !(urb->transfer_buffer_length % maxpacket)) {
  2319. one_more = 1;
  2320. }
  2321. if (one_more) {
  2322. qtd_prev = qtd;
  2323. qtd = fotg210_qtd_alloc(fotg210, flags);
  2324. if (unlikely(!qtd))
  2325. goto cleanup;
  2326. qtd->urb = urb;
  2327. qtd_prev->hw_next = QTD_NEXT(fotg210, qtd->qtd_dma);
  2328. list_add_tail(&qtd->qtd_list, head);
  2329. /* never any data in such packets */
  2330. qtd_fill(fotg210, qtd, 0, 0, token, 0);
  2331. }
  2332. }
  2333. /* by default, enable interrupt on urb completion */
  2334. if (likely(!(urb->transfer_flags & URB_NO_INTERRUPT)))
  2335. qtd->hw_token |= cpu_to_hc32(fotg210, QTD_IOC);
  2336. return head;
  2337. cleanup:
  2338. qtd_list_free(fotg210, urb, head);
  2339. return NULL;
  2340. }
  2341. /* Would be best to create all qh's from config descriptors,
  2342. * when each interface/altsetting is established. Unlink
  2343. * any previous qh and cancel its urbs first; endpoints are
  2344. * implicitly reset then (data toggle too).
  2345. * That'd mean updating how usbcore talks to HCDs. (2.7?)
  2346. */
  2347. /* Each QH holds a qtd list; a QH is used for everything except iso.
  2348. *
  2349. * For interrupt urbs, the scheduler must set the microframe scheduling
  2350. * mask(s) each time the QH gets scheduled. For highspeed, that's
  2351. * just one microframe in the s-mask. For split interrupt transactions
  2352. * there are additional complications: c-mask, maybe FSTNs.
  2353. */
  2354. static struct fotg210_qh *qh_make(struct fotg210_hcd *fotg210, struct urb *urb,
  2355. gfp_t flags)
  2356. {
  2357. struct fotg210_qh *qh = fotg210_qh_alloc(fotg210, flags);
  2358. u32 info1 = 0, info2 = 0;
  2359. int is_input, type;
  2360. int maxp = 0;
  2361. struct usb_tt *tt = urb->dev->tt;
  2362. struct fotg210_qh_hw *hw;
  2363. if (!qh)
  2364. return qh;
  2365. /*
  2366. * init endpoint/device data for this QH
  2367. */
  2368. info1 |= usb_pipeendpoint(urb->pipe) << 8;
  2369. info1 |= usb_pipedevice(urb->pipe) << 0;
  2370. is_input = usb_pipein(urb->pipe);
  2371. type = usb_pipetype(urb->pipe);
  2372. maxp = usb_maxpacket(urb->dev, urb->pipe, !is_input);
  2373. /* 1024 byte maxpacket is a hardware ceiling. High bandwidth
  2374. * acts like up to 3KB, but is built from smaller packets.
  2375. */
  2376. if (max_packet(maxp) > 1024) {
  2377. fotg210_dbg(fotg210, "bogus qh maxpacket %d\n",
  2378. max_packet(maxp));
  2379. goto done;
  2380. }
  2381. /* Compute interrupt scheduling parameters just once, and save.
  2382. * - allowing for high bandwidth, how many nsec/uframe are used?
  2383. * - split transactions need a second CSPLIT uframe; same question
  2384. * - splits also need a schedule gap (for full/low speed I/O)
  2385. * - qh has a polling interval
  2386. *
  2387. * For control/bulk requests, the HC or TT handles these.
  2388. */
  2389. if (type == PIPE_INTERRUPT) {
  2390. qh->usecs = NS_TO_US(usb_calc_bus_time(USB_SPEED_HIGH,
  2391. is_input, 0,
  2392. hb_mult(maxp) * max_packet(maxp)));
  2393. qh->start = NO_FRAME;
  2394. if (urb->dev->speed == USB_SPEED_HIGH) {
  2395. qh->c_usecs = 0;
  2396. qh->gap_uf = 0;
  2397. qh->period = urb->interval >> 3;
  2398. if (qh->period == 0 && urb->interval != 1) {
  2399. /* NOTE interval 2 or 4 uframes could work.
  2400. * But interval 1 scheduling is simpler, and
  2401. * includes high bandwidth.
  2402. */
  2403. urb->interval = 1;
  2404. } else if (qh->period > fotg210->periodic_size) {
  2405. qh->period = fotg210->periodic_size;
  2406. urb->interval = qh->period << 3;
  2407. }
  2408. } else {
  2409. int think_time;
  2410. /* gap is f(FS/LS transfer times) */
  2411. qh->gap_uf = 1 + usb_calc_bus_time(urb->dev->speed,
  2412. is_input, 0, maxp) / (125 * 1000);
  2413. /* FIXME this just approximates SPLIT/CSPLIT times */
  2414. if (is_input) { /* SPLIT, gap, CSPLIT+DATA */
  2415. qh->c_usecs = qh->usecs + HS_USECS(0);
  2416. qh->usecs = HS_USECS(1);
  2417. } else { /* SPLIT+DATA, gap, CSPLIT */
  2418. qh->usecs += HS_USECS(1);
  2419. qh->c_usecs = HS_USECS(0);
  2420. }
  2421. think_time = tt ? tt->think_time : 0;
  2422. qh->tt_usecs = NS_TO_US(think_time +
  2423. usb_calc_bus_time(urb->dev->speed,
  2424. is_input, 0, max_packet(maxp)));
  2425. qh->period = urb->interval;
  2426. if (qh->period > fotg210->periodic_size) {
  2427. qh->period = fotg210->periodic_size;
  2428. urb->interval = qh->period;
  2429. }
  2430. }
  2431. }
  2432. /* support for tt scheduling, and access to toggles */
  2433. qh->dev = urb->dev;
  2434. /* using TT? */
  2435. switch (urb->dev->speed) {
  2436. case USB_SPEED_LOW:
  2437. info1 |= QH_LOW_SPEED;
  2438. /* FALL THROUGH */
  2439. case USB_SPEED_FULL:
  2440. /* EPS 0 means "full" */
  2441. if (type != PIPE_INTERRUPT)
  2442. info1 |= (FOTG210_TUNE_RL_TT << 28);
  2443. if (type == PIPE_CONTROL) {
  2444. info1 |= QH_CONTROL_EP; /* for TT */
  2445. info1 |= QH_TOGGLE_CTL; /* toggle from qtd */
  2446. }
  2447. info1 |= maxp << 16;
  2448. info2 |= (FOTG210_TUNE_MULT_TT << 30);
  2449. /* Some Freescale processors have an erratum in which the
  2450. * port number in the queue head was 0..N-1 instead of 1..N.
  2451. */
  2452. if (fotg210_has_fsl_portno_bug(fotg210))
  2453. info2 |= (urb->dev->ttport-1) << 23;
  2454. else
  2455. info2 |= urb->dev->ttport << 23;
  2456. /* set the address of the TT; for TDI's integrated
  2457. * root hub tt, leave it zeroed.
  2458. */
  2459. if (tt && tt->hub != fotg210_to_hcd(fotg210)->self.root_hub)
  2460. info2 |= tt->hub->devnum << 16;
  2461. /* NOTE: if (PIPE_INTERRUPT) { scheduler sets c-mask } */
  2462. break;
  2463. case USB_SPEED_HIGH: /* no TT involved */
  2464. info1 |= QH_HIGH_SPEED;
  2465. if (type == PIPE_CONTROL) {
  2466. info1 |= (FOTG210_TUNE_RL_HS << 28);
  2467. info1 |= 64 << 16; /* usb2 fixed maxpacket */
  2468. info1 |= QH_TOGGLE_CTL; /* toggle from qtd */
  2469. info2 |= (FOTG210_TUNE_MULT_HS << 30);
  2470. } else if (type == PIPE_BULK) {
  2471. info1 |= (FOTG210_TUNE_RL_HS << 28);
  2472. /* The USB spec says that high speed bulk endpoints
  2473. * always use 512 byte maxpacket. But some device
  2474. * vendors decided to ignore that, and MSFT is happy
  2475. * to help them do so. So now people expect to use
  2476. * such nonconformant devices with Linux too; sigh.
  2477. */
  2478. info1 |= max_packet(maxp) << 16;
  2479. info2 |= (FOTG210_TUNE_MULT_HS << 30);
  2480. } else { /* PIPE_INTERRUPT */
  2481. info1 |= max_packet(maxp) << 16;
  2482. info2 |= hb_mult(maxp) << 30;
  2483. }
  2484. break;
  2485. default:
  2486. fotg210_dbg(fotg210, "bogus dev %p speed %d\n", urb->dev,
  2487. urb->dev->speed);
  2488. done:
  2489. qh_destroy(fotg210, qh);
  2490. return NULL;
  2491. }
  2492. /* NOTE: if (PIPE_INTERRUPT) { scheduler sets s-mask } */
  2493. /* init as live, toggle clear, advance to dummy */
  2494. qh->qh_state = QH_STATE_IDLE;
  2495. hw = qh->hw;
  2496. hw->hw_info1 = cpu_to_hc32(fotg210, info1);
  2497. hw->hw_info2 = cpu_to_hc32(fotg210, info2);
  2498. qh->is_out = !is_input;
  2499. usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), !is_input, 1);
  2500. qh_refresh(fotg210, qh);
  2501. return qh;
  2502. }
  2503. static void enable_async(struct fotg210_hcd *fotg210)
  2504. {
  2505. if (fotg210->async_count++)
  2506. return;
  2507. /* Stop waiting to turn off the async schedule */
  2508. fotg210->enabled_hrtimer_events &= ~BIT(FOTG210_HRTIMER_DISABLE_ASYNC);
  2509. /* Don't start the schedule until ASS is 0 */
  2510. fotg210_poll_ASS(fotg210);
  2511. turn_on_io_watchdog(fotg210);
  2512. }
  2513. static void disable_async(struct fotg210_hcd *fotg210)
  2514. {
  2515. if (--fotg210->async_count)
  2516. return;
  2517. /* The async schedule and async_unlink list are supposed to be empty */
  2518. WARN_ON(fotg210->async->qh_next.qh || fotg210->async_unlink);
  2519. /* Don't turn off the schedule until ASS is 1 */
  2520. fotg210_poll_ASS(fotg210);
  2521. }
  2522. /* move qh (and its qtds) onto async queue; maybe enable queue. */
  2523. static void qh_link_async(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
  2524. {
  2525. __hc32 dma = QH_NEXT(fotg210, qh->qh_dma);
  2526. struct fotg210_qh *head;
  2527. /* Don't link a QH if there's a Clear-TT-Buffer pending */
  2528. if (unlikely(qh->clearing_tt))
  2529. return;
  2530. WARN_ON(qh->qh_state != QH_STATE_IDLE);
  2531. /* clear halt and/or toggle; and maybe recover from silicon quirk */
  2532. qh_refresh(fotg210, qh);
  2533. /* splice right after start */
  2534. head = fotg210->async;
  2535. qh->qh_next = head->qh_next;
  2536. qh->hw->hw_next = head->hw->hw_next;
  2537. wmb();
  2538. head->qh_next.qh = qh;
  2539. head->hw->hw_next = dma;
  2540. qh->xacterrs = 0;
  2541. qh->qh_state = QH_STATE_LINKED;
  2542. /* qtd completions reported later by interrupt */
  2543. enable_async(fotg210);
  2544. }
  2545. /* For control/bulk/interrupt, return QH with these TDs appended.
  2546. * Allocates and initializes the QH if necessary.
  2547. * Returns null if it can't allocate a QH it needs to.
  2548. * If the QH has TDs (urbs) already, that's great.
  2549. */
  2550. static struct fotg210_qh *qh_append_tds(struct fotg210_hcd *fotg210,
  2551. struct urb *urb, struct list_head *qtd_list,
  2552. int epnum, void **ptr)
  2553. {
  2554. struct fotg210_qh *qh = NULL;
  2555. __hc32 qh_addr_mask = cpu_to_hc32(fotg210, 0x7f);
  2556. qh = (struct fotg210_qh *) *ptr;
  2557. if (unlikely(qh == NULL)) {
  2558. /* can't sleep here, we have fotg210->lock... */
  2559. qh = qh_make(fotg210, urb, GFP_ATOMIC);
  2560. *ptr = qh;
  2561. }
  2562. if (likely(qh != NULL)) {
  2563. struct fotg210_qtd *qtd;
  2564. if (unlikely(list_empty(qtd_list)))
  2565. qtd = NULL;
  2566. else
  2567. qtd = list_entry(qtd_list->next, struct fotg210_qtd,
  2568. qtd_list);
  2569. /* control qh may need patching ... */
  2570. if (unlikely(epnum == 0)) {
  2571. /* usb_reset_device() briefly reverts to address 0 */
  2572. if (usb_pipedevice(urb->pipe) == 0)
  2573. qh->hw->hw_info1 &= ~qh_addr_mask;
  2574. }
  2575. /* just one way to queue requests: swap with the dummy qtd.
  2576. * only hc or qh_refresh() ever modify the overlay.
  2577. */
  2578. if (likely(qtd != NULL)) {
  2579. struct fotg210_qtd *dummy;
  2580. dma_addr_t dma;
  2581. __hc32 token;
  2582. /* to avoid racing the HC, use the dummy td instead of
  2583. * the first td of our list (becomes new dummy). both
  2584. * tds stay deactivated until we're done, when the
  2585. * HC is allowed to fetch the old dummy (4.10.2).
  2586. */
  2587. token = qtd->hw_token;
  2588. qtd->hw_token = HALT_BIT(fotg210);
  2589. dummy = qh->dummy;
  2590. dma = dummy->qtd_dma;
  2591. *dummy = *qtd;
  2592. dummy->qtd_dma = dma;
  2593. list_del(&qtd->qtd_list);
  2594. list_add(&dummy->qtd_list, qtd_list);
  2595. list_splice_tail(qtd_list, &qh->qtd_list);
  2596. fotg210_qtd_init(fotg210, qtd, qtd->qtd_dma);
  2597. qh->dummy = qtd;
  2598. /* hc must see the new dummy at list end */
  2599. dma = qtd->qtd_dma;
  2600. qtd = list_entry(qh->qtd_list.prev,
  2601. struct fotg210_qtd, qtd_list);
  2602. qtd->hw_next = QTD_NEXT(fotg210, dma);
  2603. /* let the hc process these next qtds */
  2604. wmb();
  2605. dummy->hw_token = token;
  2606. urb->hcpriv = qh;
  2607. }
  2608. }
  2609. return qh;
  2610. }
  2611. static int submit_async(struct fotg210_hcd *fotg210, struct urb *urb,
  2612. struct list_head *qtd_list, gfp_t mem_flags)
  2613. {
  2614. int epnum;
  2615. unsigned long flags;
  2616. struct fotg210_qh *qh = NULL;
  2617. int rc;
  2618. epnum = urb->ep->desc.bEndpointAddress;
  2619. #ifdef FOTG210_URB_TRACE
  2620. {
  2621. struct fotg210_qtd *qtd;
  2622. qtd = list_entry(qtd_list->next, struct fotg210_qtd, qtd_list);
  2623. fotg210_dbg(fotg210,
  2624. "%s %s urb %p ep%d%s len %d, qtd %p [qh %p]\n",
  2625. __func__, urb->dev->devpath, urb,
  2626. epnum & 0x0f, (epnum & USB_DIR_IN)
  2627. ? "in" : "out",
  2628. urb->transfer_buffer_length,
  2629. qtd, urb->ep->hcpriv);
  2630. }
  2631. #endif
  2632. spin_lock_irqsave(&fotg210->lock, flags);
  2633. if (unlikely(!HCD_HW_ACCESSIBLE(fotg210_to_hcd(fotg210)))) {
  2634. rc = -ESHUTDOWN;
  2635. goto done;
  2636. }
  2637. rc = usb_hcd_link_urb_to_ep(fotg210_to_hcd(fotg210), urb);
  2638. if (unlikely(rc))
  2639. goto done;
  2640. qh = qh_append_tds(fotg210, urb, qtd_list, epnum, &urb->ep->hcpriv);
  2641. if (unlikely(qh == NULL)) {
  2642. usb_hcd_unlink_urb_from_ep(fotg210_to_hcd(fotg210), urb);
  2643. rc = -ENOMEM;
  2644. goto done;
  2645. }
  2646. /* Control/bulk operations through TTs don't need scheduling,
  2647. * the HC and TT handle it when the TT has a buffer ready.
  2648. */
  2649. if (likely(qh->qh_state == QH_STATE_IDLE))
  2650. qh_link_async(fotg210, qh);
  2651. done:
  2652. spin_unlock_irqrestore(&fotg210->lock, flags);
  2653. if (unlikely(qh == NULL))
  2654. qtd_list_free(fotg210, urb, qtd_list);
  2655. return rc;
  2656. }
  2657. static void single_unlink_async(struct fotg210_hcd *fotg210,
  2658. struct fotg210_qh *qh)
  2659. {
  2660. struct fotg210_qh *prev;
  2661. /* Add to the end of the list of QHs waiting for the next IAAD */
  2662. qh->qh_state = QH_STATE_UNLINK;
  2663. if (fotg210->async_unlink)
  2664. fotg210->async_unlink_last->unlink_next = qh;
  2665. else
  2666. fotg210->async_unlink = qh;
  2667. fotg210->async_unlink_last = qh;
  2668. /* Unlink it from the schedule */
  2669. prev = fotg210->async;
  2670. while (prev->qh_next.qh != qh)
  2671. prev = prev->qh_next.qh;
  2672. prev->hw->hw_next = qh->hw->hw_next;
  2673. prev->qh_next = qh->qh_next;
  2674. if (fotg210->qh_scan_next == qh)
  2675. fotg210->qh_scan_next = qh->qh_next.qh;
  2676. }
  2677. static void start_iaa_cycle(struct fotg210_hcd *fotg210, bool nested)
  2678. {
  2679. /*
  2680. * Do nothing if an IAA cycle is already running or
  2681. * if one will be started shortly.
  2682. */
  2683. if (fotg210->async_iaa || fotg210->async_unlinking)
  2684. return;
  2685. /* Do all the waiting QHs at once */
  2686. fotg210->async_iaa = fotg210->async_unlink;
  2687. fotg210->async_unlink = NULL;
  2688. /* If the controller isn't running, we don't have to wait for it */
  2689. if (unlikely(fotg210->rh_state < FOTG210_RH_RUNNING)) {
  2690. if (!nested) /* Avoid recursion */
  2691. end_unlink_async(fotg210);
  2692. /* Otherwise start a new IAA cycle */
  2693. } else if (likely(fotg210->rh_state == FOTG210_RH_RUNNING)) {
  2694. /* Make sure the unlinks are all visible to the hardware */
  2695. wmb();
  2696. fotg210_writel(fotg210, fotg210->command | CMD_IAAD,
  2697. &fotg210->regs->command);
  2698. fotg210_readl(fotg210, &fotg210->regs->command);
  2699. fotg210_enable_event(fotg210, FOTG210_HRTIMER_IAA_WATCHDOG,
  2700. true);
  2701. }
  2702. }
  2703. /* the async qh for the qtds being unlinked are now gone from the HC */
  2704. static void end_unlink_async(struct fotg210_hcd *fotg210)
  2705. {
  2706. struct fotg210_qh *qh;
  2707. /* Process the idle QHs */
  2708. restart:
  2709. fotg210->async_unlinking = true;
  2710. while (fotg210->async_iaa) {
  2711. qh = fotg210->async_iaa;
  2712. fotg210->async_iaa = qh->unlink_next;
  2713. qh->unlink_next = NULL;
  2714. qh->qh_state = QH_STATE_IDLE;
  2715. qh->qh_next.qh = NULL;
  2716. qh_completions(fotg210, qh);
  2717. if (!list_empty(&qh->qtd_list) &&
  2718. fotg210->rh_state == FOTG210_RH_RUNNING)
  2719. qh_link_async(fotg210, qh);
  2720. disable_async(fotg210);
  2721. }
  2722. fotg210->async_unlinking = false;
  2723. /* Start a new IAA cycle if any QHs are waiting for it */
  2724. if (fotg210->async_unlink) {
  2725. start_iaa_cycle(fotg210, true);
  2726. if (unlikely(fotg210->rh_state < FOTG210_RH_RUNNING))
  2727. goto restart;
  2728. }
  2729. }
  2730. static void unlink_empty_async(struct fotg210_hcd *fotg210)
  2731. {
  2732. struct fotg210_qh *qh, *next;
  2733. bool stopped = (fotg210->rh_state < FOTG210_RH_RUNNING);
  2734. bool check_unlinks_later = false;
  2735. /* Unlink all the async QHs that have been empty for a timer cycle */
  2736. next = fotg210->async->qh_next.qh;
  2737. while (next) {
  2738. qh = next;
  2739. next = qh->qh_next.qh;
  2740. if (list_empty(&qh->qtd_list) &&
  2741. qh->qh_state == QH_STATE_LINKED) {
  2742. if (!stopped && qh->unlink_cycle ==
  2743. fotg210->async_unlink_cycle)
  2744. check_unlinks_later = true;
  2745. else
  2746. single_unlink_async(fotg210, qh);
  2747. }
  2748. }
  2749. /* Start a new IAA cycle if any QHs are waiting for it */
  2750. if (fotg210->async_unlink)
  2751. start_iaa_cycle(fotg210, false);
  2752. /* QHs that haven't been empty for long enough will be handled later */
  2753. if (check_unlinks_later) {
  2754. fotg210_enable_event(fotg210, FOTG210_HRTIMER_ASYNC_UNLINKS,
  2755. true);
  2756. ++fotg210->async_unlink_cycle;
  2757. }
  2758. }
  2759. /* makes sure the async qh will become idle */
  2760. /* caller must own fotg210->lock */
  2761. static void start_unlink_async(struct fotg210_hcd *fotg210,
  2762. struct fotg210_qh *qh)
  2763. {
  2764. /*
  2765. * If the QH isn't linked then there's nothing we can do
  2766. * unless we were called during a giveback, in which case
  2767. * qh_completions() has to deal with it.
  2768. */
  2769. if (qh->qh_state != QH_STATE_LINKED) {
  2770. if (qh->qh_state == QH_STATE_COMPLETING)
  2771. qh->needs_rescan = 1;
  2772. return;
  2773. }
  2774. single_unlink_async(fotg210, qh);
  2775. start_iaa_cycle(fotg210, false);
  2776. }
  2777. static void scan_async(struct fotg210_hcd *fotg210)
  2778. {
  2779. struct fotg210_qh *qh;
  2780. bool check_unlinks_later = false;
  2781. fotg210->qh_scan_next = fotg210->async->qh_next.qh;
  2782. while (fotg210->qh_scan_next) {
  2783. qh = fotg210->qh_scan_next;
  2784. fotg210->qh_scan_next = qh->qh_next.qh;
  2785. rescan:
  2786. /* clean any finished work for this qh */
  2787. if (!list_empty(&qh->qtd_list)) {
  2788. int temp;
  2789. /*
  2790. * Unlinks could happen here; completion reporting
  2791. * drops the lock. That's why fotg210->qh_scan_next
  2792. * always holds the next qh to scan; if the next qh
  2793. * gets unlinked then fotg210->qh_scan_next is adjusted
  2794. * in single_unlink_async().
  2795. */
  2796. temp = qh_completions(fotg210, qh);
  2797. if (qh->needs_rescan) {
  2798. start_unlink_async(fotg210, qh);
  2799. } else if (list_empty(&qh->qtd_list)
  2800. && qh->qh_state == QH_STATE_LINKED) {
  2801. qh->unlink_cycle = fotg210->async_unlink_cycle;
  2802. check_unlinks_later = true;
  2803. } else if (temp != 0)
  2804. goto rescan;
  2805. }
  2806. }
  2807. /*
  2808. * Unlink empty entries, reducing DMA usage as well
  2809. * as HCD schedule-scanning costs. Delay for any qh
  2810. * we just scanned, there's a not-unusual case that it
  2811. * doesn't stay idle for long.
  2812. */
  2813. if (check_unlinks_later && fotg210->rh_state == FOTG210_RH_RUNNING &&
  2814. !(fotg210->enabled_hrtimer_events &
  2815. BIT(FOTG210_HRTIMER_ASYNC_UNLINKS))) {
  2816. fotg210_enable_event(fotg210,
  2817. FOTG210_HRTIMER_ASYNC_UNLINKS, true);
  2818. ++fotg210->async_unlink_cycle;
  2819. }
  2820. }
  2821. /* EHCI scheduled transaction support: interrupt, iso, split iso
  2822. * These are called "periodic" transactions in the EHCI spec.
  2823. *
  2824. * Note that for interrupt transfers, the QH/QTD manipulation is shared
  2825. * with the "asynchronous" transaction support (control/bulk transfers).
  2826. * The only real difference is in how interrupt transfers are scheduled.
  2827. *
  2828. * For ISO, we make an "iso_stream" head to serve the same role as a QH.
  2829. * It keeps track of every ITD (or SITD) that's linked, and holds enough
  2830. * pre-calculated schedule data to make appending to the queue be quick.
  2831. */
  2832. static int fotg210_get_frame(struct usb_hcd *hcd);
  2833. /* periodic_next_shadow - return "next" pointer on shadow list
  2834. * @periodic: host pointer to qh/itd
  2835. * @tag: hardware tag for type of this record
  2836. */
  2837. static union fotg210_shadow *periodic_next_shadow(struct fotg210_hcd *fotg210,
  2838. union fotg210_shadow *periodic, __hc32 tag)
  2839. {
  2840. switch (hc32_to_cpu(fotg210, tag)) {
  2841. case Q_TYPE_QH:
  2842. return &periodic->qh->qh_next;
  2843. case Q_TYPE_FSTN:
  2844. return &periodic->fstn->fstn_next;
  2845. default:
  2846. return &periodic->itd->itd_next;
  2847. }
  2848. }
  2849. static __hc32 *shadow_next_periodic(struct fotg210_hcd *fotg210,
  2850. union fotg210_shadow *periodic, __hc32 tag)
  2851. {
  2852. switch (hc32_to_cpu(fotg210, tag)) {
  2853. /* our fotg210_shadow.qh is actually software part */
  2854. case Q_TYPE_QH:
  2855. return &periodic->qh->hw->hw_next;
  2856. /* others are hw parts */
  2857. default:
  2858. return periodic->hw_next;
  2859. }
  2860. }
  2861. /* caller must hold fotg210->lock */
  2862. static void periodic_unlink(struct fotg210_hcd *fotg210, unsigned frame,
  2863. void *ptr)
  2864. {
  2865. union fotg210_shadow *prev_p = &fotg210->pshadow[frame];
  2866. __hc32 *hw_p = &fotg210->periodic[frame];
  2867. union fotg210_shadow here = *prev_p;
  2868. /* find predecessor of "ptr"; hw and shadow lists are in sync */
  2869. while (here.ptr && here.ptr != ptr) {
  2870. prev_p = periodic_next_shadow(fotg210, prev_p,
  2871. Q_NEXT_TYPE(fotg210, *hw_p));
  2872. hw_p = shadow_next_periodic(fotg210, &here,
  2873. Q_NEXT_TYPE(fotg210, *hw_p));
  2874. here = *prev_p;
  2875. }
  2876. /* an interrupt entry (at list end) could have been shared */
  2877. if (!here.ptr)
  2878. return;
  2879. /* update shadow and hardware lists ... the old "next" pointers
  2880. * from ptr may still be in use, the caller updates them.
  2881. */
  2882. *prev_p = *periodic_next_shadow(fotg210, &here,
  2883. Q_NEXT_TYPE(fotg210, *hw_p));
  2884. *hw_p = *shadow_next_periodic(fotg210, &here,
  2885. Q_NEXT_TYPE(fotg210, *hw_p));
  2886. }
  2887. /* how many of the uframe's 125 usecs are allocated? */
  2888. static unsigned short periodic_usecs(struct fotg210_hcd *fotg210,
  2889. unsigned frame, unsigned uframe)
  2890. {
  2891. __hc32 *hw_p = &fotg210->periodic[frame];
  2892. union fotg210_shadow *q = &fotg210->pshadow[frame];
  2893. unsigned usecs = 0;
  2894. struct fotg210_qh_hw *hw;
  2895. while (q->ptr) {
  2896. switch (hc32_to_cpu(fotg210, Q_NEXT_TYPE(fotg210, *hw_p))) {
  2897. case Q_TYPE_QH:
  2898. hw = q->qh->hw;
  2899. /* is it in the S-mask? */
  2900. if (hw->hw_info2 & cpu_to_hc32(fotg210, 1 << uframe))
  2901. usecs += q->qh->usecs;
  2902. /* ... or C-mask? */
  2903. if (hw->hw_info2 & cpu_to_hc32(fotg210,
  2904. 1 << (8 + uframe)))
  2905. usecs += q->qh->c_usecs;
  2906. hw_p = &hw->hw_next;
  2907. q = &q->qh->qh_next;
  2908. break;
  2909. /* case Q_TYPE_FSTN: */
  2910. default:
  2911. /* for "save place" FSTNs, count the relevant INTR
  2912. * bandwidth from the previous frame
  2913. */
  2914. if (q->fstn->hw_prev != FOTG210_LIST_END(fotg210))
  2915. fotg210_dbg(fotg210, "ignoring FSTN cost ...\n");
  2916. hw_p = &q->fstn->hw_next;
  2917. q = &q->fstn->fstn_next;
  2918. break;
  2919. case Q_TYPE_ITD:
  2920. if (q->itd->hw_transaction[uframe])
  2921. usecs += q->itd->stream->usecs;
  2922. hw_p = &q->itd->hw_next;
  2923. q = &q->itd->itd_next;
  2924. break;
  2925. }
  2926. }
  2927. if (usecs > fotg210->uframe_periodic_max)
  2928. fotg210_err(fotg210, "uframe %d sched overrun: %d usecs\n",
  2929. frame * 8 + uframe, usecs);
  2930. return usecs;
  2931. }
  2932. static int same_tt(struct usb_device *dev1, struct usb_device *dev2)
  2933. {
  2934. if (!dev1->tt || !dev2->tt)
  2935. return 0;
  2936. if (dev1->tt != dev2->tt)
  2937. return 0;
  2938. if (dev1->tt->multi)
  2939. return dev1->ttport == dev2->ttport;
  2940. else
  2941. return 1;
  2942. }
  2943. /* return true iff the device's transaction translator is available
  2944. * for a periodic transfer starting at the specified frame, using
  2945. * all the uframes in the mask.
  2946. */
  2947. static int tt_no_collision(struct fotg210_hcd *fotg210, unsigned period,
  2948. struct usb_device *dev, unsigned frame, u32 uf_mask)
  2949. {
  2950. if (period == 0) /* error */
  2951. return 0;
  2952. /* note bandwidth wastage: split never follows csplit
  2953. * (different dev or endpoint) until the next uframe.
  2954. * calling convention doesn't make that distinction.
  2955. */
  2956. for (; frame < fotg210->periodic_size; frame += period) {
  2957. union fotg210_shadow here;
  2958. __hc32 type;
  2959. struct fotg210_qh_hw *hw;
  2960. here = fotg210->pshadow[frame];
  2961. type = Q_NEXT_TYPE(fotg210, fotg210->periodic[frame]);
  2962. while (here.ptr) {
  2963. switch (hc32_to_cpu(fotg210, type)) {
  2964. case Q_TYPE_ITD:
  2965. type = Q_NEXT_TYPE(fotg210, here.itd->hw_next);
  2966. here = here.itd->itd_next;
  2967. continue;
  2968. case Q_TYPE_QH:
  2969. hw = here.qh->hw;
  2970. if (same_tt(dev, here.qh->dev)) {
  2971. u32 mask;
  2972. mask = hc32_to_cpu(fotg210,
  2973. hw->hw_info2);
  2974. /* "knows" no gap is needed */
  2975. mask |= mask >> 8;
  2976. if (mask & uf_mask)
  2977. break;
  2978. }
  2979. type = Q_NEXT_TYPE(fotg210, hw->hw_next);
  2980. here = here.qh->qh_next;
  2981. continue;
  2982. /* case Q_TYPE_FSTN: */
  2983. default:
  2984. fotg210_dbg(fotg210,
  2985. "periodic frame %d bogus type %d\n",
  2986. frame, type);
  2987. }
  2988. /* collision or error */
  2989. return 0;
  2990. }
  2991. }
  2992. /* no collision */
  2993. return 1;
  2994. }
  2995. static void enable_periodic(struct fotg210_hcd *fotg210)
  2996. {
  2997. if (fotg210->periodic_count++)
  2998. return;
  2999. /* Stop waiting to turn off the periodic schedule */
  3000. fotg210->enabled_hrtimer_events &=
  3001. ~BIT(FOTG210_HRTIMER_DISABLE_PERIODIC);
  3002. /* Don't start the schedule until PSS is 0 */
  3003. fotg210_poll_PSS(fotg210);
  3004. turn_on_io_watchdog(fotg210);
  3005. }
  3006. static void disable_periodic(struct fotg210_hcd *fotg210)
  3007. {
  3008. if (--fotg210->periodic_count)
  3009. return;
  3010. /* Don't turn off the schedule until PSS is 1 */
  3011. fotg210_poll_PSS(fotg210);
  3012. }
  3013. /* periodic schedule slots have iso tds (normal or split) first, then a
  3014. * sparse tree for active interrupt transfers.
  3015. *
  3016. * this just links in a qh; caller guarantees uframe masks are set right.
  3017. * no FSTN support (yet; fotg210 0.96+)
  3018. */
  3019. static void qh_link_periodic(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
  3020. {
  3021. unsigned i;
  3022. unsigned period = qh->period;
  3023. dev_dbg(&qh->dev->dev,
  3024. "link qh%d-%04x/%p start %d [%d/%d us]\n", period,
  3025. hc32_to_cpup(fotg210, &qh->hw->hw_info2) &
  3026. (QH_CMASK | QH_SMASK), qh, qh->start, qh->usecs,
  3027. qh->c_usecs);
  3028. /* high bandwidth, or otherwise every microframe */
  3029. if (period == 0)
  3030. period = 1;
  3031. for (i = qh->start; i < fotg210->periodic_size; i += period) {
  3032. union fotg210_shadow *prev = &fotg210->pshadow[i];
  3033. __hc32 *hw_p = &fotg210->periodic[i];
  3034. union fotg210_shadow here = *prev;
  3035. __hc32 type = 0;
  3036. /* skip the iso nodes at list head */
  3037. while (here.ptr) {
  3038. type = Q_NEXT_TYPE(fotg210, *hw_p);
  3039. if (type == cpu_to_hc32(fotg210, Q_TYPE_QH))
  3040. break;
  3041. prev = periodic_next_shadow(fotg210, prev, type);
  3042. hw_p = shadow_next_periodic(fotg210, &here, type);
  3043. here = *prev;
  3044. }
  3045. /* sorting each branch by period (slow-->fast)
  3046. * enables sharing interior tree nodes
  3047. */
  3048. while (here.ptr && qh != here.qh) {
  3049. if (qh->period > here.qh->period)
  3050. break;
  3051. prev = &here.qh->qh_next;
  3052. hw_p = &here.qh->hw->hw_next;
  3053. here = *prev;
  3054. }
  3055. /* link in this qh, unless some earlier pass did that */
  3056. if (qh != here.qh) {
  3057. qh->qh_next = here;
  3058. if (here.qh)
  3059. qh->hw->hw_next = *hw_p;
  3060. wmb();
  3061. prev->qh = qh;
  3062. *hw_p = QH_NEXT(fotg210, qh->qh_dma);
  3063. }
  3064. }
  3065. qh->qh_state = QH_STATE_LINKED;
  3066. qh->xacterrs = 0;
  3067. /* update per-qh bandwidth for usbfs */
  3068. fotg210_to_hcd(fotg210)->self.bandwidth_allocated += qh->period
  3069. ? ((qh->usecs + qh->c_usecs) / qh->period)
  3070. : (qh->usecs * 8);
  3071. list_add(&qh->intr_node, &fotg210->intr_qh_list);
  3072. /* maybe enable periodic schedule processing */
  3073. ++fotg210->intr_count;
  3074. enable_periodic(fotg210);
  3075. }
  3076. static void qh_unlink_periodic(struct fotg210_hcd *fotg210,
  3077. struct fotg210_qh *qh)
  3078. {
  3079. unsigned i;
  3080. unsigned period;
  3081. /*
  3082. * If qh is for a low/full-speed device, simply unlinking it
  3083. * could interfere with an ongoing split transaction. To unlink
  3084. * it safely would require setting the QH_INACTIVATE bit and
  3085. * waiting at least one frame, as described in EHCI 4.12.2.5.
  3086. *
  3087. * We won't bother with any of this. Instead, we assume that the
  3088. * only reason for unlinking an interrupt QH while the current URB
  3089. * is still active is to dequeue all the URBs (flush the whole
  3090. * endpoint queue).
  3091. *
  3092. * If rebalancing the periodic schedule is ever implemented, this
  3093. * approach will no longer be valid.
  3094. */
  3095. /* high bandwidth, or otherwise part of every microframe */
  3096. period = qh->period;
  3097. if (!period)
  3098. period = 1;
  3099. for (i = qh->start; i < fotg210->periodic_size; i += period)
  3100. periodic_unlink(fotg210, i, qh);
  3101. /* update per-qh bandwidth for usbfs */
  3102. fotg210_to_hcd(fotg210)->self.bandwidth_allocated -= qh->period
  3103. ? ((qh->usecs + qh->c_usecs) / qh->period)
  3104. : (qh->usecs * 8);
  3105. dev_dbg(&qh->dev->dev,
  3106. "unlink qh%d-%04x/%p start %d [%d/%d us]\n",
  3107. qh->period, hc32_to_cpup(fotg210, &qh->hw->hw_info2) &
  3108. (QH_CMASK | QH_SMASK), qh, qh->start, qh->usecs,
  3109. qh->c_usecs);
  3110. /* qh->qh_next still "live" to HC */
  3111. qh->qh_state = QH_STATE_UNLINK;
  3112. qh->qh_next.ptr = NULL;
  3113. if (fotg210->qh_scan_next == qh)
  3114. fotg210->qh_scan_next = list_entry(qh->intr_node.next,
  3115. struct fotg210_qh, intr_node);
  3116. list_del(&qh->intr_node);
  3117. }
  3118. static void start_unlink_intr(struct fotg210_hcd *fotg210,
  3119. struct fotg210_qh *qh)
  3120. {
  3121. /* If the QH isn't linked then there's nothing we can do
  3122. * unless we were called during a giveback, in which case
  3123. * qh_completions() has to deal with it.
  3124. */
  3125. if (qh->qh_state != QH_STATE_LINKED) {
  3126. if (qh->qh_state == QH_STATE_COMPLETING)
  3127. qh->needs_rescan = 1;
  3128. return;
  3129. }
  3130. qh_unlink_periodic(fotg210, qh);
  3131. /* Make sure the unlinks are visible before starting the timer */
  3132. wmb();
  3133. /*
  3134. * The EHCI spec doesn't say how long it takes the controller to
  3135. * stop accessing an unlinked interrupt QH. The timer delay is
  3136. * 9 uframes; presumably that will be long enough.
  3137. */
  3138. qh->unlink_cycle = fotg210->intr_unlink_cycle;
  3139. /* New entries go at the end of the intr_unlink list */
  3140. if (fotg210->intr_unlink)
  3141. fotg210->intr_unlink_last->unlink_next = qh;
  3142. else
  3143. fotg210->intr_unlink = qh;
  3144. fotg210->intr_unlink_last = qh;
  3145. if (fotg210->intr_unlinking)
  3146. ; /* Avoid recursive calls */
  3147. else if (fotg210->rh_state < FOTG210_RH_RUNNING)
  3148. fotg210_handle_intr_unlinks(fotg210);
  3149. else if (fotg210->intr_unlink == qh) {
  3150. fotg210_enable_event(fotg210, FOTG210_HRTIMER_UNLINK_INTR,
  3151. true);
  3152. ++fotg210->intr_unlink_cycle;
  3153. }
  3154. }
  3155. static void end_unlink_intr(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
  3156. {
  3157. struct fotg210_qh_hw *hw = qh->hw;
  3158. int rc;
  3159. qh->qh_state = QH_STATE_IDLE;
  3160. hw->hw_next = FOTG210_LIST_END(fotg210);
  3161. qh_completions(fotg210, qh);
  3162. /* reschedule QH iff another request is queued */
  3163. if (!list_empty(&qh->qtd_list) &&
  3164. fotg210->rh_state == FOTG210_RH_RUNNING) {
  3165. rc = qh_schedule(fotg210, qh);
  3166. /* An error here likely indicates handshake failure
  3167. * or no space left in the schedule. Neither fault
  3168. * should happen often ...
  3169. *
  3170. * FIXME kill the now-dysfunctional queued urbs
  3171. */
  3172. if (rc != 0)
  3173. fotg210_err(fotg210, "can't reschedule qh %p, err %d\n",
  3174. qh, rc);
  3175. }
  3176. /* maybe turn off periodic schedule */
  3177. --fotg210->intr_count;
  3178. disable_periodic(fotg210);
  3179. }
  3180. static int check_period(struct fotg210_hcd *fotg210, unsigned frame,
  3181. unsigned uframe, unsigned period, unsigned usecs)
  3182. {
  3183. int claimed;
  3184. /* complete split running into next frame?
  3185. * given FSTN support, we could sometimes check...
  3186. */
  3187. if (uframe >= 8)
  3188. return 0;
  3189. /* convert "usecs we need" to "max already claimed" */
  3190. usecs = fotg210->uframe_periodic_max - usecs;
  3191. /* we "know" 2 and 4 uframe intervals were rejected; so
  3192. * for period 0, check _every_ microframe in the schedule.
  3193. */
  3194. if (unlikely(period == 0)) {
  3195. do {
  3196. for (uframe = 0; uframe < 7; uframe++) {
  3197. claimed = periodic_usecs(fotg210, frame,
  3198. uframe);
  3199. if (claimed > usecs)
  3200. return 0;
  3201. }
  3202. } while ((frame += 1) < fotg210->periodic_size);
  3203. /* just check the specified uframe, at that period */
  3204. } else {
  3205. do {
  3206. claimed = periodic_usecs(fotg210, frame, uframe);
  3207. if (claimed > usecs)
  3208. return 0;
  3209. } while ((frame += period) < fotg210->periodic_size);
  3210. }
  3211. /* success! */
  3212. return 1;
  3213. }
  3214. static int check_intr_schedule(struct fotg210_hcd *fotg210, unsigned frame,
  3215. unsigned uframe, const struct fotg210_qh *qh, __hc32 *c_maskp)
  3216. {
  3217. int retval = -ENOSPC;
  3218. u8 mask = 0;
  3219. if (qh->c_usecs && uframe >= 6) /* FSTN territory? */
  3220. goto done;
  3221. if (!check_period(fotg210, frame, uframe, qh->period, qh->usecs))
  3222. goto done;
  3223. if (!qh->c_usecs) {
  3224. retval = 0;
  3225. *c_maskp = 0;
  3226. goto done;
  3227. }
  3228. /* Make sure this tt's buffer is also available for CSPLITs.
  3229. * We pessimize a bit; probably the typical full speed case
  3230. * doesn't need the second CSPLIT.
  3231. *
  3232. * NOTE: both SPLIT and CSPLIT could be checked in just
  3233. * one smart pass...
  3234. */
  3235. mask = 0x03 << (uframe + qh->gap_uf);
  3236. *c_maskp = cpu_to_hc32(fotg210, mask << 8);
  3237. mask |= 1 << uframe;
  3238. if (tt_no_collision(fotg210, qh->period, qh->dev, frame, mask)) {
  3239. if (!check_period(fotg210, frame, uframe + qh->gap_uf + 1,
  3240. qh->period, qh->c_usecs))
  3241. goto done;
  3242. if (!check_period(fotg210, frame, uframe + qh->gap_uf,
  3243. qh->period, qh->c_usecs))
  3244. goto done;
  3245. retval = 0;
  3246. }
  3247. done:
  3248. return retval;
  3249. }
  3250. /* "first fit" scheduling policy used the first time through,
  3251. * or when the previous schedule slot can't be re-used.
  3252. */
  3253. static int qh_schedule(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
  3254. {
  3255. int status;
  3256. unsigned uframe;
  3257. __hc32 c_mask;
  3258. unsigned frame; /* 0..(qh->period - 1), or NO_FRAME */
  3259. struct fotg210_qh_hw *hw = qh->hw;
  3260. qh_refresh(fotg210, qh);
  3261. hw->hw_next = FOTG210_LIST_END(fotg210);
  3262. frame = qh->start;
  3263. /* reuse the previous schedule slots, if we can */
  3264. if (frame < qh->period) {
  3265. uframe = ffs(hc32_to_cpup(fotg210, &hw->hw_info2) & QH_SMASK);
  3266. status = check_intr_schedule(fotg210, frame, --uframe,
  3267. qh, &c_mask);
  3268. } else {
  3269. uframe = 0;
  3270. c_mask = 0;
  3271. status = -ENOSPC;
  3272. }
  3273. /* else scan the schedule to find a group of slots such that all
  3274. * uframes have enough periodic bandwidth available.
  3275. */
  3276. if (status) {
  3277. /* "normal" case, uframing flexible except with splits */
  3278. if (qh->period) {
  3279. int i;
  3280. for (i = qh->period; status && i > 0; --i) {
  3281. frame = ++fotg210->random_frame % qh->period;
  3282. for (uframe = 0; uframe < 8; uframe++) {
  3283. status = check_intr_schedule(fotg210,
  3284. frame, uframe, qh,
  3285. &c_mask);
  3286. if (status == 0)
  3287. break;
  3288. }
  3289. }
  3290. /* qh->period == 0 means every uframe */
  3291. } else {
  3292. frame = 0;
  3293. status = check_intr_schedule(fotg210, 0, 0, qh,
  3294. &c_mask);
  3295. }
  3296. if (status)
  3297. goto done;
  3298. qh->start = frame;
  3299. /* reset S-frame and (maybe) C-frame masks */
  3300. hw->hw_info2 &= cpu_to_hc32(fotg210, ~(QH_CMASK | QH_SMASK));
  3301. hw->hw_info2 |= qh->period
  3302. ? cpu_to_hc32(fotg210, 1 << uframe)
  3303. : cpu_to_hc32(fotg210, QH_SMASK);
  3304. hw->hw_info2 |= c_mask;
  3305. } else
  3306. fotg210_dbg(fotg210, "reused qh %p schedule\n", qh);
  3307. /* stuff into the periodic schedule */
  3308. qh_link_periodic(fotg210, qh);
  3309. done:
  3310. return status;
  3311. }
  3312. static int intr_submit(struct fotg210_hcd *fotg210, struct urb *urb,
  3313. struct list_head *qtd_list, gfp_t mem_flags)
  3314. {
  3315. unsigned epnum;
  3316. unsigned long flags;
  3317. struct fotg210_qh *qh;
  3318. int status;
  3319. struct list_head empty;
  3320. /* get endpoint and transfer/schedule data */
  3321. epnum = urb->ep->desc.bEndpointAddress;
  3322. spin_lock_irqsave(&fotg210->lock, flags);
  3323. if (unlikely(!HCD_HW_ACCESSIBLE(fotg210_to_hcd(fotg210)))) {
  3324. status = -ESHUTDOWN;
  3325. goto done_not_linked;
  3326. }
  3327. status = usb_hcd_link_urb_to_ep(fotg210_to_hcd(fotg210), urb);
  3328. if (unlikely(status))
  3329. goto done_not_linked;
  3330. /* get qh and force any scheduling errors */
  3331. INIT_LIST_HEAD(&empty);
  3332. qh = qh_append_tds(fotg210, urb, &empty, epnum, &urb->ep->hcpriv);
  3333. if (qh == NULL) {
  3334. status = -ENOMEM;
  3335. goto done;
  3336. }
  3337. if (qh->qh_state == QH_STATE_IDLE) {
  3338. status = qh_schedule(fotg210, qh);
  3339. if (status)
  3340. goto done;
  3341. }
  3342. /* then queue the urb's tds to the qh */
  3343. qh = qh_append_tds(fotg210, urb, qtd_list, epnum, &urb->ep->hcpriv);
  3344. BUG_ON(qh == NULL);
  3345. /* ... update usbfs periodic stats */
  3346. fotg210_to_hcd(fotg210)->self.bandwidth_int_reqs++;
  3347. done:
  3348. if (unlikely(status))
  3349. usb_hcd_unlink_urb_from_ep(fotg210_to_hcd(fotg210), urb);
  3350. done_not_linked:
  3351. spin_unlock_irqrestore(&fotg210->lock, flags);
  3352. if (status)
  3353. qtd_list_free(fotg210, urb, qtd_list);
  3354. return status;
  3355. }
  3356. static void scan_intr(struct fotg210_hcd *fotg210)
  3357. {
  3358. struct fotg210_qh *qh;
  3359. list_for_each_entry_safe(qh, fotg210->qh_scan_next,
  3360. &fotg210->intr_qh_list, intr_node) {
  3361. rescan:
  3362. /* clean any finished work for this qh */
  3363. if (!list_empty(&qh->qtd_list)) {
  3364. int temp;
  3365. /*
  3366. * Unlinks could happen here; completion reporting
  3367. * drops the lock. That's why fotg210->qh_scan_next
  3368. * always holds the next qh to scan; if the next qh
  3369. * gets unlinked then fotg210->qh_scan_next is adjusted
  3370. * in qh_unlink_periodic().
  3371. */
  3372. temp = qh_completions(fotg210, qh);
  3373. if (unlikely(qh->needs_rescan ||
  3374. (list_empty(&qh->qtd_list) &&
  3375. qh->qh_state == QH_STATE_LINKED)))
  3376. start_unlink_intr(fotg210, qh);
  3377. else if (temp != 0)
  3378. goto rescan;
  3379. }
  3380. }
  3381. }
  3382. /* fotg210_iso_stream ops work with both ITD and SITD */
  3383. static struct fotg210_iso_stream *iso_stream_alloc(gfp_t mem_flags)
  3384. {
  3385. struct fotg210_iso_stream *stream;
  3386. stream = kzalloc(sizeof(*stream), mem_flags);
  3387. if (likely(stream != NULL)) {
  3388. INIT_LIST_HEAD(&stream->td_list);
  3389. INIT_LIST_HEAD(&stream->free_list);
  3390. stream->next_uframe = -1;
  3391. }
  3392. return stream;
  3393. }
  3394. static void iso_stream_init(struct fotg210_hcd *fotg210,
  3395. struct fotg210_iso_stream *stream, struct usb_device *dev,
  3396. int pipe, unsigned interval)
  3397. {
  3398. u32 buf1;
  3399. unsigned epnum, maxp;
  3400. int is_input;
  3401. long bandwidth;
  3402. unsigned multi;
  3403. /*
  3404. * this might be a "high bandwidth" highspeed endpoint,
  3405. * as encoded in the ep descriptor's wMaxPacket field
  3406. */
  3407. epnum = usb_pipeendpoint(pipe);
  3408. is_input = usb_pipein(pipe) ? USB_DIR_IN : 0;
  3409. maxp = usb_maxpacket(dev, pipe, !is_input);
  3410. if (is_input)
  3411. buf1 = (1 << 11);
  3412. else
  3413. buf1 = 0;
  3414. maxp = max_packet(maxp);
  3415. multi = hb_mult(maxp);
  3416. buf1 |= maxp;
  3417. maxp *= multi;
  3418. stream->buf0 = cpu_to_hc32(fotg210, (epnum << 8) | dev->devnum);
  3419. stream->buf1 = cpu_to_hc32(fotg210, buf1);
  3420. stream->buf2 = cpu_to_hc32(fotg210, multi);
  3421. /* usbfs wants to report the average usecs per frame tied up
  3422. * when transfers on this endpoint are scheduled ...
  3423. */
  3424. if (dev->speed == USB_SPEED_FULL) {
  3425. interval <<= 3;
  3426. stream->usecs = NS_TO_US(usb_calc_bus_time(dev->speed,
  3427. is_input, 1, maxp));
  3428. stream->usecs /= 8;
  3429. } else {
  3430. stream->highspeed = 1;
  3431. stream->usecs = HS_USECS_ISO(maxp);
  3432. }
  3433. bandwidth = stream->usecs * 8;
  3434. bandwidth /= interval;
  3435. stream->bandwidth = bandwidth;
  3436. stream->udev = dev;
  3437. stream->bEndpointAddress = is_input | epnum;
  3438. stream->interval = interval;
  3439. stream->maxp = maxp;
  3440. }
  3441. static struct fotg210_iso_stream *iso_stream_find(struct fotg210_hcd *fotg210,
  3442. struct urb *urb)
  3443. {
  3444. unsigned epnum;
  3445. struct fotg210_iso_stream *stream;
  3446. struct usb_host_endpoint *ep;
  3447. unsigned long flags;
  3448. epnum = usb_pipeendpoint(urb->pipe);
  3449. if (usb_pipein(urb->pipe))
  3450. ep = urb->dev->ep_in[epnum];
  3451. else
  3452. ep = urb->dev->ep_out[epnum];
  3453. spin_lock_irqsave(&fotg210->lock, flags);
  3454. stream = ep->hcpriv;
  3455. if (unlikely(stream == NULL)) {
  3456. stream = iso_stream_alloc(GFP_ATOMIC);
  3457. if (likely(stream != NULL)) {
  3458. ep->hcpriv = stream;
  3459. stream->ep = ep;
  3460. iso_stream_init(fotg210, stream, urb->dev, urb->pipe,
  3461. urb->interval);
  3462. }
  3463. /* if dev->ep[epnum] is a QH, hw is set */
  3464. } else if (unlikely(stream->hw != NULL)) {
  3465. fotg210_dbg(fotg210, "dev %s ep%d%s, not iso??\n",
  3466. urb->dev->devpath, epnum,
  3467. usb_pipein(urb->pipe) ? "in" : "out");
  3468. stream = NULL;
  3469. }
  3470. spin_unlock_irqrestore(&fotg210->lock, flags);
  3471. return stream;
  3472. }
  3473. /* fotg210_iso_sched ops can be ITD-only or SITD-only */
  3474. static struct fotg210_iso_sched *iso_sched_alloc(unsigned packets,
  3475. gfp_t mem_flags)
  3476. {
  3477. struct fotg210_iso_sched *iso_sched;
  3478. int size = sizeof(*iso_sched);
  3479. size += packets * sizeof(struct fotg210_iso_packet);
  3480. iso_sched = kzalloc(size, mem_flags);
  3481. if (likely(iso_sched != NULL))
  3482. INIT_LIST_HEAD(&iso_sched->td_list);
  3483. return iso_sched;
  3484. }
  3485. static inline void itd_sched_init(struct fotg210_hcd *fotg210,
  3486. struct fotg210_iso_sched *iso_sched,
  3487. struct fotg210_iso_stream *stream, struct urb *urb)
  3488. {
  3489. unsigned i;
  3490. dma_addr_t dma = urb->transfer_dma;
  3491. /* how many uframes are needed for these transfers */
  3492. iso_sched->span = urb->number_of_packets * stream->interval;
  3493. /* figure out per-uframe itd fields that we'll need later
  3494. * when we fit new itds into the schedule.
  3495. */
  3496. for (i = 0; i < urb->number_of_packets; i++) {
  3497. struct fotg210_iso_packet *uframe = &iso_sched->packet[i];
  3498. unsigned length;
  3499. dma_addr_t buf;
  3500. u32 trans;
  3501. length = urb->iso_frame_desc[i].length;
  3502. buf = dma + urb->iso_frame_desc[i].offset;
  3503. trans = FOTG210_ISOC_ACTIVE;
  3504. trans |= buf & 0x0fff;
  3505. if (unlikely(((i + 1) == urb->number_of_packets))
  3506. && !(urb->transfer_flags & URB_NO_INTERRUPT))
  3507. trans |= FOTG210_ITD_IOC;
  3508. trans |= length << 16;
  3509. uframe->transaction = cpu_to_hc32(fotg210, trans);
  3510. /* might need to cross a buffer page within a uframe */
  3511. uframe->bufp = (buf & ~(u64)0x0fff);
  3512. buf += length;
  3513. if (unlikely((uframe->bufp != (buf & ~(u64)0x0fff))))
  3514. uframe->cross = 1;
  3515. }
  3516. }
  3517. static void iso_sched_free(struct fotg210_iso_stream *stream,
  3518. struct fotg210_iso_sched *iso_sched)
  3519. {
  3520. if (!iso_sched)
  3521. return;
  3522. /* caller must hold fotg210->lock!*/
  3523. list_splice(&iso_sched->td_list, &stream->free_list);
  3524. kfree(iso_sched);
  3525. }
  3526. static int itd_urb_transaction(struct fotg210_iso_stream *stream,
  3527. struct fotg210_hcd *fotg210, struct urb *urb, gfp_t mem_flags)
  3528. {
  3529. struct fotg210_itd *itd;
  3530. dma_addr_t itd_dma;
  3531. int i;
  3532. unsigned num_itds;
  3533. struct fotg210_iso_sched *sched;
  3534. unsigned long flags;
  3535. sched = iso_sched_alloc(urb->number_of_packets, mem_flags);
  3536. if (unlikely(sched == NULL))
  3537. return -ENOMEM;
  3538. itd_sched_init(fotg210, sched, stream, urb);
  3539. if (urb->interval < 8)
  3540. num_itds = 1 + (sched->span + 7) / 8;
  3541. else
  3542. num_itds = urb->number_of_packets;
  3543. /* allocate/init ITDs */
  3544. spin_lock_irqsave(&fotg210->lock, flags);
  3545. for (i = 0; i < num_itds; i++) {
  3546. /*
  3547. * Use iTDs from the free list, but not iTDs that may
  3548. * still be in use by the hardware.
  3549. */
  3550. if (likely(!list_empty(&stream->free_list))) {
  3551. itd = list_first_entry(&stream->free_list,
  3552. struct fotg210_itd, itd_list);
  3553. if (itd->frame == fotg210->now_frame)
  3554. goto alloc_itd;
  3555. list_del(&itd->itd_list);
  3556. itd_dma = itd->itd_dma;
  3557. } else {
  3558. alloc_itd:
  3559. spin_unlock_irqrestore(&fotg210->lock, flags);
  3560. itd = dma_pool_alloc(fotg210->itd_pool, mem_flags,
  3561. &itd_dma);
  3562. spin_lock_irqsave(&fotg210->lock, flags);
  3563. if (!itd) {
  3564. iso_sched_free(stream, sched);
  3565. spin_unlock_irqrestore(&fotg210->lock, flags);
  3566. return -ENOMEM;
  3567. }
  3568. }
  3569. memset(itd, 0, sizeof(*itd));
  3570. itd->itd_dma = itd_dma;
  3571. list_add(&itd->itd_list, &sched->td_list);
  3572. }
  3573. spin_unlock_irqrestore(&fotg210->lock, flags);
  3574. /* temporarily store schedule info in hcpriv */
  3575. urb->hcpriv = sched;
  3576. urb->error_count = 0;
  3577. return 0;
  3578. }
  3579. static inline int itd_slot_ok(struct fotg210_hcd *fotg210, u32 mod, u32 uframe,
  3580. u8 usecs, u32 period)
  3581. {
  3582. uframe %= period;
  3583. do {
  3584. /* can't commit more than uframe_periodic_max usec */
  3585. if (periodic_usecs(fotg210, uframe >> 3, uframe & 0x7)
  3586. > (fotg210->uframe_periodic_max - usecs))
  3587. return 0;
  3588. /* we know urb->interval is 2^N uframes */
  3589. uframe += period;
  3590. } while (uframe < mod);
  3591. return 1;
  3592. }
  3593. /* This scheduler plans almost as far into the future as it has actual
  3594. * periodic schedule slots. (Affected by TUNE_FLS, which defaults to
  3595. * "as small as possible" to be cache-friendlier.) That limits the size
  3596. * transfers you can stream reliably; avoid more than 64 msec per urb.
  3597. * Also avoid queue depths of less than fotg210's worst irq latency (affected
  3598. * by the per-urb URB_NO_INTERRUPT hint, the log2_irq_thresh module parameter,
  3599. * and other factors); or more than about 230 msec total (for portability,
  3600. * given FOTG210_TUNE_FLS and the slop). Or, write a smarter scheduler!
  3601. */
  3602. #define SCHEDULE_SLOP 80 /* microframes */
  3603. static int iso_stream_schedule(struct fotg210_hcd *fotg210, struct urb *urb,
  3604. struct fotg210_iso_stream *stream)
  3605. {
  3606. u32 now, next, start, period, span;
  3607. int status;
  3608. unsigned mod = fotg210->periodic_size << 3;
  3609. struct fotg210_iso_sched *sched = urb->hcpriv;
  3610. period = urb->interval;
  3611. span = sched->span;
  3612. if (span > mod - SCHEDULE_SLOP) {
  3613. fotg210_dbg(fotg210, "iso request %p too long\n", urb);
  3614. status = -EFBIG;
  3615. goto fail;
  3616. }
  3617. now = fotg210_read_frame_index(fotg210) & (mod - 1);
  3618. /* Typical case: reuse current schedule, stream is still active.
  3619. * Hopefully there are no gaps from the host falling behind
  3620. * (irq delays etc), but if there are we'll take the next
  3621. * slot in the schedule, implicitly assuming URB_ISO_ASAP.
  3622. */
  3623. if (likely(!list_empty(&stream->td_list))) {
  3624. u32 excess;
  3625. /* For high speed devices, allow scheduling within the
  3626. * isochronous scheduling threshold. For full speed devices
  3627. * and Intel PCI-based controllers, don't (work around for
  3628. * Intel ICH9 bug).
  3629. */
  3630. if (!stream->highspeed && fotg210->fs_i_thresh)
  3631. next = now + fotg210->i_thresh;
  3632. else
  3633. next = now;
  3634. /* Fell behind (by up to twice the slop amount)?
  3635. * We decide based on the time of the last currently-scheduled
  3636. * slot, not the time of the next available slot.
  3637. */
  3638. excess = (stream->next_uframe - period - next) & (mod - 1);
  3639. if (excess >= mod - 2 * SCHEDULE_SLOP)
  3640. start = next + excess - mod + period *
  3641. DIV_ROUND_UP(mod - excess, period);
  3642. else
  3643. start = next + excess + period;
  3644. if (start - now >= mod) {
  3645. fotg210_dbg(fotg210, "request %p would overflow (%d+%d >= %d)\n",
  3646. urb, start - now - period, period,
  3647. mod);
  3648. status = -EFBIG;
  3649. goto fail;
  3650. }
  3651. }
  3652. /* need to schedule; when's the next (u)frame we could start?
  3653. * this is bigger than fotg210->i_thresh allows; scheduling itself
  3654. * isn't free, the slop should handle reasonably slow cpus. it
  3655. * can also help high bandwidth if the dma and irq loads don't
  3656. * jump until after the queue is primed.
  3657. */
  3658. else {
  3659. int done = 0;
  3660. start = SCHEDULE_SLOP + (now & ~0x07);
  3661. /* NOTE: assumes URB_ISO_ASAP, to limit complexity/bugs */
  3662. /* find a uframe slot with enough bandwidth.
  3663. * Early uframes are more precious because full-speed
  3664. * iso IN transfers can't use late uframes,
  3665. * and therefore they should be allocated last.
  3666. */
  3667. next = start;
  3668. start += period;
  3669. do {
  3670. start--;
  3671. /* check schedule: enough space? */
  3672. if (itd_slot_ok(fotg210, mod, start,
  3673. stream->usecs, period))
  3674. done = 1;
  3675. } while (start > next && !done);
  3676. /* no room in the schedule */
  3677. if (!done) {
  3678. fotg210_dbg(fotg210, "iso resched full %p (now %d max %d)\n",
  3679. urb, now, now + mod);
  3680. status = -ENOSPC;
  3681. goto fail;
  3682. }
  3683. }
  3684. /* Tried to schedule too far into the future? */
  3685. if (unlikely(start - now + span - period >=
  3686. mod - 2 * SCHEDULE_SLOP)) {
  3687. fotg210_dbg(fotg210, "request %p would overflow (%d+%d >= %d)\n",
  3688. urb, start - now, span - period,
  3689. mod - 2 * SCHEDULE_SLOP);
  3690. status = -EFBIG;
  3691. goto fail;
  3692. }
  3693. stream->next_uframe = start & (mod - 1);
  3694. /* report high speed start in uframes; full speed, in frames */
  3695. urb->start_frame = stream->next_uframe;
  3696. if (!stream->highspeed)
  3697. urb->start_frame >>= 3;
  3698. /* Make sure scan_isoc() sees these */
  3699. if (fotg210->isoc_count == 0)
  3700. fotg210->next_frame = now >> 3;
  3701. return 0;
  3702. fail:
  3703. iso_sched_free(stream, sched);
  3704. urb->hcpriv = NULL;
  3705. return status;
  3706. }
  3707. static inline void itd_init(struct fotg210_hcd *fotg210,
  3708. struct fotg210_iso_stream *stream, struct fotg210_itd *itd)
  3709. {
  3710. int i;
  3711. /* it's been recently zeroed */
  3712. itd->hw_next = FOTG210_LIST_END(fotg210);
  3713. itd->hw_bufp[0] = stream->buf0;
  3714. itd->hw_bufp[1] = stream->buf1;
  3715. itd->hw_bufp[2] = stream->buf2;
  3716. for (i = 0; i < 8; i++)
  3717. itd->index[i] = -1;
  3718. /* All other fields are filled when scheduling */
  3719. }
  3720. static inline void itd_patch(struct fotg210_hcd *fotg210,
  3721. struct fotg210_itd *itd, struct fotg210_iso_sched *iso_sched,
  3722. unsigned index, u16 uframe)
  3723. {
  3724. struct fotg210_iso_packet *uf = &iso_sched->packet[index];
  3725. unsigned pg = itd->pg;
  3726. uframe &= 0x07;
  3727. itd->index[uframe] = index;
  3728. itd->hw_transaction[uframe] = uf->transaction;
  3729. itd->hw_transaction[uframe] |= cpu_to_hc32(fotg210, pg << 12);
  3730. itd->hw_bufp[pg] |= cpu_to_hc32(fotg210, uf->bufp & ~(u32)0);
  3731. itd->hw_bufp_hi[pg] |= cpu_to_hc32(fotg210, (u32)(uf->bufp >> 32));
  3732. /* iso_frame_desc[].offset must be strictly increasing */
  3733. if (unlikely(uf->cross)) {
  3734. u64 bufp = uf->bufp + 4096;
  3735. itd->pg = ++pg;
  3736. itd->hw_bufp[pg] |= cpu_to_hc32(fotg210, bufp & ~(u32)0);
  3737. itd->hw_bufp_hi[pg] |= cpu_to_hc32(fotg210, (u32)(bufp >> 32));
  3738. }
  3739. }
  3740. static inline void itd_link(struct fotg210_hcd *fotg210, unsigned frame,
  3741. struct fotg210_itd *itd)
  3742. {
  3743. union fotg210_shadow *prev = &fotg210->pshadow[frame];
  3744. __hc32 *hw_p = &fotg210->periodic[frame];
  3745. union fotg210_shadow here = *prev;
  3746. __hc32 type = 0;
  3747. /* skip any iso nodes which might belong to previous microframes */
  3748. while (here.ptr) {
  3749. type = Q_NEXT_TYPE(fotg210, *hw_p);
  3750. if (type == cpu_to_hc32(fotg210, Q_TYPE_QH))
  3751. break;
  3752. prev = periodic_next_shadow(fotg210, prev, type);
  3753. hw_p = shadow_next_periodic(fotg210, &here, type);
  3754. here = *prev;
  3755. }
  3756. itd->itd_next = here;
  3757. itd->hw_next = *hw_p;
  3758. prev->itd = itd;
  3759. itd->frame = frame;
  3760. wmb();
  3761. *hw_p = cpu_to_hc32(fotg210, itd->itd_dma | Q_TYPE_ITD);
  3762. }
  3763. /* fit urb's itds into the selected schedule slot; activate as needed */
  3764. static void itd_link_urb(struct fotg210_hcd *fotg210, struct urb *urb,
  3765. unsigned mod, struct fotg210_iso_stream *stream)
  3766. {
  3767. int packet;
  3768. unsigned next_uframe, uframe, frame;
  3769. struct fotg210_iso_sched *iso_sched = urb->hcpriv;
  3770. struct fotg210_itd *itd;
  3771. next_uframe = stream->next_uframe & (mod - 1);
  3772. if (unlikely(list_empty(&stream->td_list))) {
  3773. fotg210_to_hcd(fotg210)->self.bandwidth_allocated
  3774. += stream->bandwidth;
  3775. fotg210_dbg(fotg210,
  3776. "schedule devp %s ep%d%s-iso period %d start %d.%d\n",
  3777. urb->dev->devpath, stream->bEndpointAddress & 0x0f,
  3778. (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
  3779. urb->interval,
  3780. next_uframe >> 3, next_uframe & 0x7);
  3781. }
  3782. /* fill iTDs uframe by uframe */
  3783. for (packet = 0, itd = NULL; packet < urb->number_of_packets;) {
  3784. if (itd == NULL) {
  3785. /* ASSERT: we have all necessary itds */
  3786. /* ASSERT: no itds for this endpoint in this uframe */
  3787. itd = list_entry(iso_sched->td_list.next,
  3788. struct fotg210_itd, itd_list);
  3789. list_move_tail(&itd->itd_list, &stream->td_list);
  3790. itd->stream = stream;
  3791. itd->urb = urb;
  3792. itd_init(fotg210, stream, itd);
  3793. }
  3794. uframe = next_uframe & 0x07;
  3795. frame = next_uframe >> 3;
  3796. itd_patch(fotg210, itd, iso_sched, packet, uframe);
  3797. next_uframe += stream->interval;
  3798. next_uframe &= mod - 1;
  3799. packet++;
  3800. /* link completed itds into the schedule */
  3801. if (((next_uframe >> 3) != frame)
  3802. || packet == urb->number_of_packets) {
  3803. itd_link(fotg210, frame & (fotg210->periodic_size - 1),
  3804. itd);
  3805. itd = NULL;
  3806. }
  3807. }
  3808. stream->next_uframe = next_uframe;
  3809. /* don't need that schedule data any more */
  3810. iso_sched_free(stream, iso_sched);
  3811. urb->hcpriv = NULL;
  3812. ++fotg210->isoc_count;
  3813. enable_periodic(fotg210);
  3814. }
  3815. #define ISO_ERRS (FOTG210_ISOC_BUF_ERR | FOTG210_ISOC_BABBLE |\
  3816. FOTG210_ISOC_XACTERR)
  3817. /* Process and recycle a completed ITD. Return true iff its urb completed,
  3818. * and hence its completion callback probably added things to the hardware
  3819. * schedule.
  3820. *
  3821. * Note that we carefully avoid recycling this descriptor until after any
  3822. * completion callback runs, so that it won't be reused quickly. That is,
  3823. * assuming (a) no more than two urbs per frame on this endpoint, and also
  3824. * (b) only this endpoint's completions submit URBs. It seems some silicon
  3825. * corrupts things if you reuse completed descriptors very quickly...
  3826. */
  3827. static bool itd_complete(struct fotg210_hcd *fotg210, struct fotg210_itd *itd)
  3828. {
  3829. struct urb *urb = itd->urb;
  3830. struct usb_iso_packet_descriptor *desc;
  3831. u32 t;
  3832. unsigned uframe;
  3833. int urb_index = -1;
  3834. struct fotg210_iso_stream *stream = itd->stream;
  3835. struct usb_device *dev;
  3836. bool retval = false;
  3837. /* for each uframe with a packet */
  3838. for (uframe = 0; uframe < 8; uframe++) {
  3839. if (likely(itd->index[uframe] == -1))
  3840. continue;
  3841. urb_index = itd->index[uframe];
  3842. desc = &urb->iso_frame_desc[urb_index];
  3843. t = hc32_to_cpup(fotg210, &itd->hw_transaction[uframe]);
  3844. itd->hw_transaction[uframe] = 0;
  3845. /* report transfer status */
  3846. if (unlikely(t & ISO_ERRS)) {
  3847. urb->error_count++;
  3848. if (t & FOTG210_ISOC_BUF_ERR)
  3849. desc->status = usb_pipein(urb->pipe)
  3850. ? -ENOSR /* hc couldn't read */
  3851. : -ECOMM; /* hc couldn't write */
  3852. else if (t & FOTG210_ISOC_BABBLE)
  3853. desc->status = -EOVERFLOW;
  3854. else /* (t & FOTG210_ISOC_XACTERR) */
  3855. desc->status = -EPROTO;
  3856. /* HC need not update length with this error */
  3857. if (!(t & FOTG210_ISOC_BABBLE)) {
  3858. desc->actual_length =
  3859. fotg210_itdlen(urb, desc, t);
  3860. urb->actual_length += desc->actual_length;
  3861. }
  3862. } else if (likely((t & FOTG210_ISOC_ACTIVE) == 0)) {
  3863. desc->status = 0;
  3864. desc->actual_length = fotg210_itdlen(urb, desc, t);
  3865. urb->actual_length += desc->actual_length;
  3866. } else {
  3867. /* URB was too late */
  3868. desc->status = -EXDEV;
  3869. }
  3870. }
  3871. /* handle completion now? */
  3872. if (likely((urb_index + 1) != urb->number_of_packets))
  3873. goto done;
  3874. /* ASSERT: it's really the last itd for this urb
  3875. * list_for_each_entry (itd, &stream->td_list, itd_list)
  3876. * BUG_ON (itd->urb == urb);
  3877. */
  3878. /* give urb back to the driver; completion often (re)submits */
  3879. dev = urb->dev;
  3880. fotg210_urb_done(fotg210, urb, 0);
  3881. retval = true;
  3882. urb = NULL;
  3883. --fotg210->isoc_count;
  3884. disable_periodic(fotg210);
  3885. if (unlikely(list_is_singular(&stream->td_list))) {
  3886. fotg210_to_hcd(fotg210)->self.bandwidth_allocated
  3887. -= stream->bandwidth;
  3888. fotg210_dbg(fotg210,
  3889. "deschedule devp %s ep%d%s-iso\n",
  3890. dev->devpath, stream->bEndpointAddress & 0x0f,
  3891. (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
  3892. }
  3893. done:
  3894. itd->urb = NULL;
  3895. /* Add to the end of the free list for later reuse */
  3896. list_move_tail(&itd->itd_list, &stream->free_list);
  3897. /* Recycle the iTDs when the pipeline is empty (ep no longer in use) */
  3898. if (list_empty(&stream->td_list)) {
  3899. list_splice_tail_init(&stream->free_list,
  3900. &fotg210->cached_itd_list);
  3901. start_free_itds(fotg210);
  3902. }
  3903. return retval;
  3904. }
  3905. static int itd_submit(struct fotg210_hcd *fotg210, struct urb *urb,
  3906. gfp_t mem_flags)
  3907. {
  3908. int status = -EINVAL;
  3909. unsigned long flags;
  3910. struct fotg210_iso_stream *stream;
  3911. /* Get iso_stream head */
  3912. stream = iso_stream_find(fotg210, urb);
  3913. if (unlikely(stream == NULL)) {
  3914. fotg210_dbg(fotg210, "can't get iso stream\n");
  3915. return -ENOMEM;
  3916. }
  3917. if (unlikely(urb->interval != stream->interval &&
  3918. fotg210_port_speed(fotg210, 0) ==
  3919. USB_PORT_STAT_HIGH_SPEED)) {
  3920. fotg210_dbg(fotg210, "can't change iso interval %d --> %d\n",
  3921. stream->interval, urb->interval);
  3922. goto done;
  3923. }
  3924. #ifdef FOTG210_URB_TRACE
  3925. fotg210_dbg(fotg210,
  3926. "%s %s urb %p ep%d%s len %d, %d pkts %d uframes[%p]\n",
  3927. __func__, urb->dev->devpath, urb,
  3928. usb_pipeendpoint(urb->pipe),
  3929. usb_pipein(urb->pipe) ? "in" : "out",
  3930. urb->transfer_buffer_length,
  3931. urb->number_of_packets, urb->interval,
  3932. stream);
  3933. #endif
  3934. /* allocate ITDs w/o locking anything */
  3935. status = itd_urb_transaction(stream, fotg210, urb, mem_flags);
  3936. if (unlikely(status < 0)) {
  3937. fotg210_dbg(fotg210, "can't init itds\n");
  3938. goto done;
  3939. }
  3940. /* schedule ... need to lock */
  3941. spin_lock_irqsave(&fotg210->lock, flags);
  3942. if (unlikely(!HCD_HW_ACCESSIBLE(fotg210_to_hcd(fotg210)))) {
  3943. status = -ESHUTDOWN;
  3944. goto done_not_linked;
  3945. }
  3946. status = usb_hcd_link_urb_to_ep(fotg210_to_hcd(fotg210), urb);
  3947. if (unlikely(status))
  3948. goto done_not_linked;
  3949. status = iso_stream_schedule(fotg210, urb, stream);
  3950. if (likely(status == 0))
  3951. itd_link_urb(fotg210, urb, fotg210->periodic_size << 3, stream);
  3952. else
  3953. usb_hcd_unlink_urb_from_ep(fotg210_to_hcd(fotg210), urb);
  3954. done_not_linked:
  3955. spin_unlock_irqrestore(&fotg210->lock, flags);
  3956. done:
  3957. return status;
  3958. }
  3959. static inline int scan_frame_queue(struct fotg210_hcd *fotg210, unsigned frame,
  3960. unsigned now_frame, bool live)
  3961. {
  3962. unsigned uf;
  3963. bool modified;
  3964. union fotg210_shadow q, *q_p;
  3965. __hc32 type, *hw_p;
  3966. /* scan each element in frame's queue for completions */
  3967. q_p = &fotg210->pshadow[frame];
  3968. hw_p = &fotg210->periodic[frame];
  3969. q.ptr = q_p->ptr;
  3970. type = Q_NEXT_TYPE(fotg210, *hw_p);
  3971. modified = false;
  3972. while (q.ptr) {
  3973. switch (hc32_to_cpu(fotg210, type)) {
  3974. case Q_TYPE_ITD:
  3975. /* If this ITD is still active, leave it for
  3976. * later processing ... check the next entry.
  3977. * No need to check for activity unless the
  3978. * frame is current.
  3979. */
  3980. if (frame == now_frame && live) {
  3981. rmb();
  3982. for (uf = 0; uf < 8; uf++) {
  3983. if (q.itd->hw_transaction[uf] &
  3984. ITD_ACTIVE(fotg210))
  3985. break;
  3986. }
  3987. if (uf < 8) {
  3988. q_p = &q.itd->itd_next;
  3989. hw_p = &q.itd->hw_next;
  3990. type = Q_NEXT_TYPE(fotg210,
  3991. q.itd->hw_next);
  3992. q = *q_p;
  3993. break;
  3994. }
  3995. }
  3996. /* Take finished ITDs out of the schedule
  3997. * and process them: recycle, maybe report
  3998. * URB completion. HC won't cache the
  3999. * pointer for much longer, if at all.
  4000. */
  4001. *q_p = q.itd->itd_next;
  4002. *hw_p = q.itd->hw_next;
  4003. type = Q_NEXT_TYPE(fotg210, q.itd->hw_next);
  4004. wmb();
  4005. modified = itd_complete(fotg210, q.itd);
  4006. q = *q_p;
  4007. break;
  4008. default:
  4009. fotg210_dbg(fotg210, "corrupt type %d frame %d shadow %p\n",
  4010. type, frame, q.ptr);
  4011. /* FALL THROUGH */
  4012. case Q_TYPE_QH:
  4013. case Q_TYPE_FSTN:
  4014. /* End of the iTDs and siTDs */
  4015. q.ptr = NULL;
  4016. break;
  4017. }
  4018. /* assume completion callbacks modify the queue */
  4019. if (unlikely(modified && fotg210->isoc_count > 0))
  4020. return -EINVAL;
  4021. }
  4022. return 0;
  4023. }
  4024. static void scan_isoc(struct fotg210_hcd *fotg210)
  4025. {
  4026. unsigned uf, now_frame, frame, ret;
  4027. unsigned fmask = fotg210->periodic_size - 1;
  4028. bool live;
  4029. /*
  4030. * When running, scan from last scan point up to "now"
  4031. * else clean up by scanning everything that's left.
  4032. * Touches as few pages as possible: cache-friendly.
  4033. */
  4034. if (fotg210->rh_state >= FOTG210_RH_RUNNING) {
  4035. uf = fotg210_read_frame_index(fotg210);
  4036. now_frame = (uf >> 3) & fmask;
  4037. live = true;
  4038. } else {
  4039. now_frame = (fotg210->next_frame - 1) & fmask;
  4040. live = false;
  4041. }
  4042. fotg210->now_frame = now_frame;
  4043. frame = fotg210->next_frame;
  4044. for (;;) {
  4045. ret = 1;
  4046. while (ret != 0)
  4047. ret = scan_frame_queue(fotg210, frame,
  4048. now_frame, live);
  4049. /* Stop when we have reached the current frame */
  4050. if (frame == now_frame)
  4051. break;
  4052. frame = (frame + 1) & fmask;
  4053. }
  4054. fotg210->next_frame = now_frame;
  4055. }
  4056. /* Display / Set uframe_periodic_max
  4057. */
  4058. static ssize_t show_uframe_periodic_max(struct device *dev,
  4059. struct device_attribute *attr, char *buf)
  4060. {
  4061. struct fotg210_hcd *fotg210;
  4062. int n;
  4063. fotg210 = hcd_to_fotg210(bus_to_hcd(dev_get_drvdata(dev)));
  4064. n = scnprintf(buf, PAGE_SIZE, "%d\n", fotg210->uframe_periodic_max);
  4065. return n;
  4066. }
  4067. static ssize_t store_uframe_periodic_max(struct device *dev,
  4068. struct device_attribute *attr, const char *buf, size_t count)
  4069. {
  4070. struct fotg210_hcd *fotg210;
  4071. unsigned uframe_periodic_max;
  4072. unsigned frame, uframe;
  4073. unsigned short allocated_max;
  4074. unsigned long flags;
  4075. ssize_t ret;
  4076. fotg210 = hcd_to_fotg210(bus_to_hcd(dev_get_drvdata(dev)));
  4077. if (kstrtouint(buf, 0, &uframe_periodic_max) < 0)
  4078. return -EINVAL;
  4079. if (uframe_periodic_max < 100 || uframe_periodic_max >= 125) {
  4080. fotg210_info(fotg210, "rejecting invalid request for uframe_periodic_max=%u\n",
  4081. uframe_periodic_max);
  4082. return -EINVAL;
  4083. }
  4084. ret = -EINVAL;
  4085. /*
  4086. * lock, so that our checking does not race with possible periodic
  4087. * bandwidth allocation through submitting new urbs.
  4088. */
  4089. spin_lock_irqsave(&fotg210->lock, flags);
  4090. /*
  4091. * for request to decrease max periodic bandwidth, we have to check
  4092. * every microframe in the schedule to see whether the decrease is
  4093. * possible.
  4094. */
  4095. if (uframe_periodic_max < fotg210->uframe_periodic_max) {
  4096. allocated_max = 0;
  4097. for (frame = 0; frame < fotg210->periodic_size; ++frame)
  4098. for (uframe = 0; uframe < 7; ++uframe)
  4099. allocated_max = max(allocated_max,
  4100. periodic_usecs(fotg210, frame,
  4101. uframe));
  4102. if (allocated_max > uframe_periodic_max) {
  4103. fotg210_info(fotg210,
  4104. "cannot decrease uframe_periodic_max because periodic bandwidth is already allocated (%u > %u)\n",
  4105. allocated_max, uframe_periodic_max);
  4106. goto out_unlock;
  4107. }
  4108. }
  4109. /* increasing is always ok */
  4110. fotg210_info(fotg210,
  4111. "setting max periodic bandwidth to %u%% (== %u usec/uframe)\n",
  4112. 100 * uframe_periodic_max/125, uframe_periodic_max);
  4113. if (uframe_periodic_max != 100)
  4114. fotg210_warn(fotg210, "max periodic bandwidth set is non-standard\n");
  4115. fotg210->uframe_periodic_max = uframe_periodic_max;
  4116. ret = count;
  4117. out_unlock:
  4118. spin_unlock_irqrestore(&fotg210->lock, flags);
  4119. return ret;
  4120. }
  4121. static DEVICE_ATTR(uframe_periodic_max, 0644, show_uframe_periodic_max,
  4122. store_uframe_periodic_max);
  4123. static inline int create_sysfs_files(struct fotg210_hcd *fotg210)
  4124. {
  4125. struct device *controller = fotg210_to_hcd(fotg210)->self.controller;
  4126. return device_create_file(controller, &dev_attr_uframe_periodic_max);
  4127. }
  4128. static inline void remove_sysfs_files(struct fotg210_hcd *fotg210)
  4129. {
  4130. struct device *controller = fotg210_to_hcd(fotg210)->self.controller;
  4131. device_remove_file(controller, &dev_attr_uframe_periodic_max);
  4132. }
  4133. /* On some systems, leaving remote wakeup enabled prevents system shutdown.
  4134. * The firmware seems to think that powering off is a wakeup event!
  4135. * This routine turns off remote wakeup and everything else, on all ports.
  4136. */
  4137. static void fotg210_turn_off_all_ports(struct fotg210_hcd *fotg210)
  4138. {
  4139. u32 __iomem *status_reg = &fotg210->regs->port_status;
  4140. fotg210_writel(fotg210, PORT_RWC_BITS, status_reg);
  4141. }
  4142. /* Halt HC, turn off all ports, and let the BIOS use the companion controllers.
  4143. * Must be called with interrupts enabled and the lock not held.
  4144. */
  4145. static void fotg210_silence_controller(struct fotg210_hcd *fotg210)
  4146. {
  4147. fotg210_halt(fotg210);
  4148. spin_lock_irq(&fotg210->lock);
  4149. fotg210->rh_state = FOTG210_RH_HALTED;
  4150. fotg210_turn_off_all_ports(fotg210);
  4151. spin_unlock_irq(&fotg210->lock);
  4152. }
  4153. /* fotg210_shutdown kick in for silicon on any bus (not just pci, etc).
  4154. * This forcibly disables dma and IRQs, helping kexec and other cases
  4155. * where the next system software may expect clean state.
  4156. */
  4157. static void fotg210_shutdown(struct usb_hcd *hcd)
  4158. {
  4159. struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  4160. spin_lock_irq(&fotg210->lock);
  4161. fotg210->shutdown = true;
  4162. fotg210->rh_state = FOTG210_RH_STOPPING;
  4163. fotg210->enabled_hrtimer_events = 0;
  4164. spin_unlock_irq(&fotg210->lock);
  4165. fotg210_silence_controller(fotg210);
  4166. hrtimer_cancel(&fotg210->hrtimer);
  4167. }
  4168. /* fotg210_work is called from some interrupts, timers, and so on.
  4169. * it calls driver completion functions, after dropping fotg210->lock.
  4170. */
  4171. static void fotg210_work(struct fotg210_hcd *fotg210)
  4172. {
  4173. /* another CPU may drop fotg210->lock during a schedule scan while
  4174. * it reports urb completions. this flag guards against bogus
  4175. * attempts at re-entrant schedule scanning.
  4176. */
  4177. if (fotg210->scanning) {
  4178. fotg210->need_rescan = true;
  4179. return;
  4180. }
  4181. fotg210->scanning = true;
  4182. rescan:
  4183. fotg210->need_rescan = false;
  4184. if (fotg210->async_count)
  4185. scan_async(fotg210);
  4186. if (fotg210->intr_count > 0)
  4187. scan_intr(fotg210);
  4188. if (fotg210->isoc_count > 0)
  4189. scan_isoc(fotg210);
  4190. if (fotg210->need_rescan)
  4191. goto rescan;
  4192. fotg210->scanning = false;
  4193. /* the IO watchdog guards against hardware or driver bugs that
  4194. * misplace IRQs, and should let us run completely without IRQs.
  4195. * such lossage has been observed on both VT6202 and VT8235.
  4196. */
  4197. turn_on_io_watchdog(fotg210);
  4198. }
  4199. /* Called when the fotg210_hcd module is removed.
  4200. */
  4201. static void fotg210_stop(struct usb_hcd *hcd)
  4202. {
  4203. struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  4204. fotg210_dbg(fotg210, "stop\n");
  4205. /* no more interrupts ... */
  4206. spin_lock_irq(&fotg210->lock);
  4207. fotg210->enabled_hrtimer_events = 0;
  4208. spin_unlock_irq(&fotg210->lock);
  4209. fotg210_quiesce(fotg210);
  4210. fotg210_silence_controller(fotg210);
  4211. fotg210_reset(fotg210);
  4212. hrtimer_cancel(&fotg210->hrtimer);
  4213. remove_sysfs_files(fotg210);
  4214. remove_debug_files(fotg210);
  4215. /* root hub is shut down separately (first, when possible) */
  4216. spin_lock_irq(&fotg210->lock);
  4217. end_free_itds(fotg210);
  4218. spin_unlock_irq(&fotg210->lock);
  4219. fotg210_mem_cleanup(fotg210);
  4220. #ifdef FOTG210_STATS
  4221. fotg210_dbg(fotg210, "irq normal %ld err %ld iaa %ld (lost %ld)\n",
  4222. fotg210->stats.normal, fotg210->stats.error,
  4223. fotg210->stats.iaa, fotg210->stats.lost_iaa);
  4224. fotg210_dbg(fotg210, "complete %ld unlink %ld\n",
  4225. fotg210->stats.complete, fotg210->stats.unlink);
  4226. #endif
  4227. dbg_status(fotg210, "fotg210_stop completed",
  4228. fotg210_readl(fotg210, &fotg210->regs->status));
  4229. }
  4230. /* one-time init, only for memory state */
  4231. static int hcd_fotg210_init(struct usb_hcd *hcd)
  4232. {
  4233. struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  4234. u32 temp;
  4235. int retval;
  4236. u32 hcc_params;
  4237. struct fotg210_qh_hw *hw;
  4238. spin_lock_init(&fotg210->lock);
  4239. /*
  4240. * keep io watchdog by default, those good HCDs could turn off it later
  4241. */
  4242. fotg210->need_io_watchdog = 1;
  4243. hrtimer_init(&fotg210->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  4244. fotg210->hrtimer.function = fotg210_hrtimer_func;
  4245. fotg210->next_hrtimer_event = FOTG210_HRTIMER_NO_EVENT;
  4246. hcc_params = fotg210_readl(fotg210, &fotg210->caps->hcc_params);
  4247. /*
  4248. * by default set standard 80% (== 100 usec/uframe) max periodic
  4249. * bandwidth as required by USB 2.0
  4250. */
  4251. fotg210->uframe_periodic_max = 100;
  4252. /*
  4253. * hw default: 1K periodic list heads, one per frame.
  4254. * periodic_size can shrink by USBCMD update if hcc_params allows.
  4255. */
  4256. fotg210->periodic_size = DEFAULT_I_TDPS;
  4257. INIT_LIST_HEAD(&fotg210->intr_qh_list);
  4258. INIT_LIST_HEAD(&fotg210->cached_itd_list);
  4259. if (HCC_PGM_FRAMELISTLEN(hcc_params)) {
  4260. /* periodic schedule size can be smaller than default */
  4261. switch (FOTG210_TUNE_FLS) {
  4262. case 0:
  4263. fotg210->periodic_size = 1024;
  4264. break;
  4265. case 1:
  4266. fotg210->periodic_size = 512;
  4267. break;
  4268. case 2:
  4269. fotg210->periodic_size = 256;
  4270. break;
  4271. default:
  4272. BUG();
  4273. }
  4274. }
  4275. retval = fotg210_mem_init(fotg210, GFP_KERNEL);
  4276. if (retval < 0)
  4277. return retval;
  4278. /* controllers may cache some of the periodic schedule ... */
  4279. fotg210->i_thresh = 2;
  4280. /*
  4281. * dedicate a qh for the async ring head, since we couldn't unlink
  4282. * a 'real' qh without stopping the async schedule [4.8]. use it
  4283. * as the 'reclamation list head' too.
  4284. * its dummy is used in hw_alt_next of many tds, to prevent the qh
  4285. * from automatically advancing to the next td after short reads.
  4286. */
  4287. fotg210->async->qh_next.qh = NULL;
  4288. hw = fotg210->async->hw;
  4289. hw->hw_next = QH_NEXT(fotg210, fotg210->async->qh_dma);
  4290. hw->hw_info1 = cpu_to_hc32(fotg210, QH_HEAD);
  4291. hw->hw_token = cpu_to_hc32(fotg210, QTD_STS_HALT);
  4292. hw->hw_qtd_next = FOTG210_LIST_END(fotg210);
  4293. fotg210->async->qh_state = QH_STATE_LINKED;
  4294. hw->hw_alt_next = QTD_NEXT(fotg210, fotg210->async->dummy->qtd_dma);
  4295. /* clear interrupt enables, set irq latency */
  4296. if (log2_irq_thresh < 0 || log2_irq_thresh > 6)
  4297. log2_irq_thresh = 0;
  4298. temp = 1 << (16 + log2_irq_thresh);
  4299. if (HCC_CANPARK(hcc_params)) {
  4300. /* HW default park == 3, on hardware that supports it (like
  4301. * NVidia and ALI silicon), maximizes throughput on the async
  4302. * schedule by avoiding QH fetches between transfers.
  4303. *
  4304. * With fast usb storage devices and NForce2, "park" seems to
  4305. * make problems: throughput reduction (!), data errors...
  4306. */
  4307. if (park) {
  4308. park = min_t(unsigned, park, 3);
  4309. temp |= CMD_PARK;
  4310. temp |= park << 8;
  4311. }
  4312. fotg210_dbg(fotg210, "park %d\n", park);
  4313. }
  4314. if (HCC_PGM_FRAMELISTLEN(hcc_params)) {
  4315. /* periodic schedule size can be smaller than default */
  4316. temp &= ~(3 << 2);
  4317. temp |= (FOTG210_TUNE_FLS << 2);
  4318. }
  4319. fotg210->command = temp;
  4320. /* Accept arbitrarily long scatter-gather lists */
  4321. if (!(hcd->driver->flags & HCD_LOCAL_MEM))
  4322. hcd->self.sg_tablesize = ~0;
  4323. return 0;
  4324. }
  4325. /* start HC running; it's halted, hcd_fotg210_init() has been run (once) */
  4326. static int fotg210_run(struct usb_hcd *hcd)
  4327. {
  4328. struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  4329. u32 temp;
  4330. u32 hcc_params;
  4331. hcd->uses_new_polling = 1;
  4332. /* EHCI spec section 4.1 */
  4333. fotg210_writel(fotg210, fotg210->periodic_dma,
  4334. &fotg210->regs->frame_list);
  4335. fotg210_writel(fotg210, (u32)fotg210->async->qh_dma,
  4336. &fotg210->regs->async_next);
  4337. /*
  4338. * hcc_params controls whether fotg210->regs->segment must (!!!)
  4339. * be used; it constrains QH/ITD/SITD and QTD locations.
  4340. * pci_pool consistent memory always uses segment zero.
  4341. * streaming mappings for I/O buffers, like pci_map_single(),
  4342. * can return segments above 4GB, if the device allows.
  4343. *
  4344. * NOTE: the dma mask is visible through dev->dma_mask, so
  4345. * drivers can pass this info along ... like NETIF_F_HIGHDMA,
  4346. * Scsi_Host.highmem_io, and so forth. It's readonly to all
  4347. * host side drivers though.
  4348. */
  4349. hcc_params = fotg210_readl(fotg210, &fotg210->caps->hcc_params);
  4350. /*
  4351. * Philips, Intel, and maybe others need CMD_RUN before the
  4352. * root hub will detect new devices (why?); NEC doesn't
  4353. */
  4354. fotg210->command &= ~(CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
  4355. fotg210->command |= CMD_RUN;
  4356. fotg210_writel(fotg210, fotg210->command, &fotg210->regs->command);
  4357. dbg_cmd(fotg210, "init", fotg210->command);
  4358. /*
  4359. * Start, enabling full USB 2.0 functionality ... usb 1.1 devices
  4360. * are explicitly handed to companion controller(s), so no TT is
  4361. * involved with the root hub. (Except where one is integrated,
  4362. * and there's no companion controller unless maybe for USB OTG.)
  4363. *
  4364. * Turning on the CF flag will transfer ownership of all ports
  4365. * from the companions to the EHCI controller. If any of the
  4366. * companions are in the middle of a port reset at the time, it
  4367. * could cause trouble. Write-locking ehci_cf_port_reset_rwsem
  4368. * guarantees that no resets are in progress. After we set CF,
  4369. * a short delay lets the hardware catch up; new resets shouldn't
  4370. * be started before the port switching actions could complete.
  4371. */
  4372. down_write(&ehci_cf_port_reset_rwsem);
  4373. fotg210->rh_state = FOTG210_RH_RUNNING;
  4374. /* unblock posted writes */
  4375. fotg210_readl(fotg210, &fotg210->regs->command);
  4376. usleep_range(5000, 10000);
  4377. up_write(&ehci_cf_port_reset_rwsem);
  4378. fotg210->last_periodic_enable = ktime_get_real();
  4379. temp = HC_VERSION(fotg210,
  4380. fotg210_readl(fotg210, &fotg210->caps->hc_capbase));
  4381. fotg210_info(fotg210,
  4382. "USB %x.%x started, EHCI %x.%02x\n",
  4383. ((fotg210->sbrn & 0xf0) >> 4), (fotg210->sbrn & 0x0f),
  4384. temp >> 8, temp & 0xff);
  4385. fotg210_writel(fotg210, INTR_MASK,
  4386. &fotg210->regs->intr_enable); /* Turn On Interrupts */
  4387. /* GRR this is run-once init(), being done every time the HC starts.
  4388. * So long as they're part of class devices, we can't do it init()
  4389. * since the class device isn't created that early.
  4390. */
  4391. create_debug_files(fotg210);
  4392. create_sysfs_files(fotg210);
  4393. return 0;
  4394. }
  4395. static int fotg210_setup(struct usb_hcd *hcd)
  4396. {
  4397. struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  4398. int retval;
  4399. fotg210->regs = (void __iomem *)fotg210->caps +
  4400. HC_LENGTH(fotg210,
  4401. fotg210_readl(fotg210, &fotg210->caps->hc_capbase));
  4402. dbg_hcs_params(fotg210, "reset");
  4403. dbg_hcc_params(fotg210, "reset");
  4404. /* cache this readonly data; minimize chip reads */
  4405. fotg210->hcs_params = fotg210_readl(fotg210,
  4406. &fotg210->caps->hcs_params);
  4407. fotg210->sbrn = HCD_USB2;
  4408. /* data structure init */
  4409. retval = hcd_fotg210_init(hcd);
  4410. if (retval)
  4411. return retval;
  4412. retval = fotg210_halt(fotg210);
  4413. if (retval)
  4414. return retval;
  4415. fotg210_reset(fotg210);
  4416. return 0;
  4417. }
  4418. static irqreturn_t fotg210_irq(struct usb_hcd *hcd)
  4419. {
  4420. struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  4421. u32 status, masked_status, pcd_status = 0, cmd;
  4422. int bh;
  4423. spin_lock(&fotg210->lock);
  4424. status = fotg210_readl(fotg210, &fotg210->regs->status);
  4425. /* e.g. cardbus physical eject */
  4426. if (status == ~(u32) 0) {
  4427. fotg210_dbg(fotg210, "device removed\n");
  4428. goto dead;
  4429. }
  4430. /*
  4431. * We don't use STS_FLR, but some controllers don't like it to
  4432. * remain on, so mask it out along with the other status bits.
  4433. */
  4434. masked_status = status & (INTR_MASK | STS_FLR);
  4435. /* Shared IRQ? */
  4436. if (!masked_status ||
  4437. unlikely(fotg210->rh_state == FOTG210_RH_HALTED)) {
  4438. spin_unlock(&fotg210->lock);
  4439. return IRQ_NONE;
  4440. }
  4441. /* clear (just) interrupts */
  4442. fotg210_writel(fotg210, masked_status, &fotg210->regs->status);
  4443. cmd = fotg210_readl(fotg210, &fotg210->regs->command);
  4444. bh = 0;
  4445. /* unrequested/ignored: Frame List Rollover */
  4446. dbg_status(fotg210, "irq", status);
  4447. /* INT, ERR, and IAA interrupt rates can be throttled */
  4448. /* normal [4.15.1.2] or error [4.15.1.1] completion */
  4449. if (likely((status & (STS_INT|STS_ERR)) != 0)) {
  4450. if (likely((status & STS_ERR) == 0))
  4451. COUNT(fotg210->stats.normal);
  4452. else
  4453. COUNT(fotg210->stats.error);
  4454. bh = 1;
  4455. }
  4456. /* complete the unlinking of some qh [4.15.2.3] */
  4457. if (status & STS_IAA) {
  4458. /* Turn off the IAA watchdog */
  4459. fotg210->enabled_hrtimer_events &=
  4460. ~BIT(FOTG210_HRTIMER_IAA_WATCHDOG);
  4461. /*
  4462. * Mild optimization: Allow another IAAD to reset the
  4463. * hrtimer, if one occurs before the next expiration.
  4464. * In theory we could always cancel the hrtimer, but
  4465. * tests show that about half the time it will be reset
  4466. * for some other event anyway.
  4467. */
  4468. if (fotg210->next_hrtimer_event == FOTG210_HRTIMER_IAA_WATCHDOG)
  4469. ++fotg210->next_hrtimer_event;
  4470. /* guard against (alleged) silicon errata */
  4471. if (cmd & CMD_IAAD)
  4472. fotg210_dbg(fotg210, "IAA with IAAD still set?\n");
  4473. if (fotg210->async_iaa) {
  4474. COUNT(fotg210->stats.iaa);
  4475. end_unlink_async(fotg210);
  4476. } else
  4477. fotg210_dbg(fotg210, "IAA with nothing unlinked?\n");
  4478. }
  4479. /* remote wakeup [4.3.1] */
  4480. if (status & STS_PCD) {
  4481. int pstatus;
  4482. u32 __iomem *status_reg = &fotg210->regs->port_status;
  4483. /* kick root hub later */
  4484. pcd_status = status;
  4485. /* resume root hub? */
  4486. if (fotg210->rh_state == FOTG210_RH_SUSPENDED)
  4487. usb_hcd_resume_root_hub(hcd);
  4488. pstatus = fotg210_readl(fotg210, status_reg);
  4489. if (test_bit(0, &fotg210->suspended_ports) &&
  4490. ((pstatus & PORT_RESUME) ||
  4491. !(pstatus & PORT_SUSPEND)) &&
  4492. (pstatus & PORT_PE) &&
  4493. fotg210->reset_done[0] == 0) {
  4494. /* start 20 msec resume signaling from this port,
  4495. * and make hub_wq collect PORT_STAT_C_SUSPEND to
  4496. * stop that signaling. Use 5 ms extra for safety,
  4497. * like usb_port_resume() does.
  4498. */
  4499. fotg210->reset_done[0] = jiffies + msecs_to_jiffies(25);
  4500. set_bit(0, &fotg210->resuming_ports);
  4501. fotg210_dbg(fotg210, "port 1 remote wakeup\n");
  4502. mod_timer(&hcd->rh_timer, fotg210->reset_done[0]);
  4503. }
  4504. }
  4505. /* PCI errors [4.15.2.4] */
  4506. if (unlikely((status & STS_FATAL) != 0)) {
  4507. fotg210_err(fotg210, "fatal error\n");
  4508. dbg_cmd(fotg210, "fatal", cmd);
  4509. dbg_status(fotg210, "fatal", status);
  4510. dead:
  4511. usb_hc_died(hcd);
  4512. /* Don't let the controller do anything more */
  4513. fotg210->shutdown = true;
  4514. fotg210->rh_state = FOTG210_RH_STOPPING;
  4515. fotg210->command &= ~(CMD_RUN | CMD_ASE | CMD_PSE);
  4516. fotg210_writel(fotg210, fotg210->command,
  4517. &fotg210->regs->command);
  4518. fotg210_writel(fotg210, 0, &fotg210->regs->intr_enable);
  4519. fotg210_handle_controller_death(fotg210);
  4520. /* Handle completions when the controller stops */
  4521. bh = 0;
  4522. }
  4523. if (bh)
  4524. fotg210_work(fotg210);
  4525. spin_unlock(&fotg210->lock);
  4526. if (pcd_status)
  4527. usb_hcd_poll_rh_status(hcd);
  4528. return IRQ_HANDLED;
  4529. }
  4530. /* non-error returns are a promise to giveback() the urb later
  4531. * we drop ownership so next owner (or urb unlink) can get it
  4532. *
  4533. * urb + dev is in hcd.self.controller.urb_list
  4534. * we're queueing TDs onto software and hardware lists
  4535. *
  4536. * hcd-specific init for hcpriv hasn't been done yet
  4537. *
  4538. * NOTE: control, bulk, and interrupt share the same code to append TDs
  4539. * to a (possibly active) QH, and the same QH scanning code.
  4540. */
  4541. static int fotg210_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
  4542. gfp_t mem_flags)
  4543. {
  4544. struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  4545. struct list_head qtd_list;
  4546. INIT_LIST_HEAD(&qtd_list);
  4547. switch (usb_pipetype(urb->pipe)) {
  4548. case PIPE_CONTROL:
  4549. /* qh_completions() code doesn't handle all the fault cases
  4550. * in multi-TD control transfers. Even 1KB is rare anyway.
  4551. */
  4552. if (urb->transfer_buffer_length > (16 * 1024))
  4553. return -EMSGSIZE;
  4554. /* FALLTHROUGH */
  4555. /* case PIPE_BULK: */
  4556. default:
  4557. if (!qh_urb_transaction(fotg210, urb, &qtd_list, mem_flags))
  4558. return -ENOMEM;
  4559. return submit_async(fotg210, urb, &qtd_list, mem_flags);
  4560. case PIPE_INTERRUPT:
  4561. if (!qh_urb_transaction(fotg210, urb, &qtd_list, mem_flags))
  4562. return -ENOMEM;
  4563. return intr_submit(fotg210, urb, &qtd_list, mem_flags);
  4564. case PIPE_ISOCHRONOUS:
  4565. return itd_submit(fotg210, urb, mem_flags);
  4566. }
  4567. }
  4568. /* remove from hardware lists
  4569. * completions normally happen asynchronously
  4570. */
  4571. static int fotg210_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  4572. {
  4573. struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  4574. struct fotg210_qh *qh;
  4575. unsigned long flags;
  4576. int rc;
  4577. spin_lock_irqsave(&fotg210->lock, flags);
  4578. rc = usb_hcd_check_unlink_urb(hcd, urb, status);
  4579. if (rc)
  4580. goto done;
  4581. switch (usb_pipetype(urb->pipe)) {
  4582. /* case PIPE_CONTROL: */
  4583. /* case PIPE_BULK:*/
  4584. default:
  4585. qh = (struct fotg210_qh *) urb->hcpriv;
  4586. if (!qh)
  4587. break;
  4588. switch (qh->qh_state) {
  4589. case QH_STATE_LINKED:
  4590. case QH_STATE_COMPLETING:
  4591. start_unlink_async(fotg210, qh);
  4592. break;
  4593. case QH_STATE_UNLINK:
  4594. case QH_STATE_UNLINK_WAIT:
  4595. /* already started */
  4596. break;
  4597. case QH_STATE_IDLE:
  4598. /* QH might be waiting for a Clear-TT-Buffer */
  4599. qh_completions(fotg210, qh);
  4600. break;
  4601. }
  4602. break;
  4603. case PIPE_INTERRUPT:
  4604. qh = (struct fotg210_qh *) urb->hcpriv;
  4605. if (!qh)
  4606. break;
  4607. switch (qh->qh_state) {
  4608. case QH_STATE_LINKED:
  4609. case QH_STATE_COMPLETING:
  4610. start_unlink_intr(fotg210, qh);
  4611. break;
  4612. case QH_STATE_IDLE:
  4613. qh_completions(fotg210, qh);
  4614. break;
  4615. default:
  4616. fotg210_dbg(fotg210, "bogus qh %p state %d\n",
  4617. qh, qh->qh_state);
  4618. goto done;
  4619. }
  4620. break;
  4621. case PIPE_ISOCHRONOUS:
  4622. /* itd... */
  4623. /* wait till next completion, do it then. */
  4624. /* completion irqs can wait up to 1024 msec, */
  4625. break;
  4626. }
  4627. done:
  4628. spin_unlock_irqrestore(&fotg210->lock, flags);
  4629. return rc;
  4630. }
  4631. /* bulk qh holds the data toggle */
  4632. static void fotg210_endpoint_disable(struct usb_hcd *hcd,
  4633. struct usb_host_endpoint *ep)
  4634. {
  4635. struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  4636. unsigned long flags;
  4637. struct fotg210_qh *qh, *tmp;
  4638. /* ASSERT: any requests/urbs are being unlinked */
  4639. /* ASSERT: nobody can be submitting urbs for this any more */
  4640. rescan:
  4641. spin_lock_irqsave(&fotg210->lock, flags);
  4642. qh = ep->hcpriv;
  4643. if (!qh)
  4644. goto done;
  4645. /* endpoints can be iso streams. for now, we don't
  4646. * accelerate iso completions ... so spin a while.
  4647. */
  4648. if (qh->hw == NULL) {
  4649. struct fotg210_iso_stream *stream = ep->hcpriv;
  4650. if (!list_empty(&stream->td_list))
  4651. goto idle_timeout;
  4652. /* BUG_ON(!list_empty(&stream->free_list)); */
  4653. kfree(stream);
  4654. goto done;
  4655. }
  4656. if (fotg210->rh_state < FOTG210_RH_RUNNING)
  4657. qh->qh_state = QH_STATE_IDLE;
  4658. switch (qh->qh_state) {
  4659. case QH_STATE_LINKED:
  4660. case QH_STATE_COMPLETING:
  4661. for (tmp = fotg210->async->qh_next.qh;
  4662. tmp && tmp != qh;
  4663. tmp = tmp->qh_next.qh)
  4664. continue;
  4665. /* periodic qh self-unlinks on empty, and a COMPLETING qh
  4666. * may already be unlinked.
  4667. */
  4668. if (tmp)
  4669. start_unlink_async(fotg210, qh);
  4670. /* FALL THROUGH */
  4671. case QH_STATE_UNLINK: /* wait for hw to finish? */
  4672. case QH_STATE_UNLINK_WAIT:
  4673. idle_timeout:
  4674. spin_unlock_irqrestore(&fotg210->lock, flags);
  4675. schedule_timeout_uninterruptible(1);
  4676. goto rescan;
  4677. case QH_STATE_IDLE: /* fully unlinked */
  4678. if (qh->clearing_tt)
  4679. goto idle_timeout;
  4680. if (list_empty(&qh->qtd_list)) {
  4681. qh_destroy(fotg210, qh);
  4682. break;
  4683. }
  4684. /* else FALL THROUGH */
  4685. default:
  4686. /* caller was supposed to have unlinked any requests;
  4687. * that's not our job. just leak this memory.
  4688. */
  4689. fotg210_err(fotg210, "qh %p (#%02x) state %d%s\n",
  4690. qh, ep->desc.bEndpointAddress, qh->qh_state,
  4691. list_empty(&qh->qtd_list) ? "" : "(has tds)");
  4692. break;
  4693. }
  4694. done:
  4695. ep->hcpriv = NULL;
  4696. spin_unlock_irqrestore(&fotg210->lock, flags);
  4697. }
  4698. static void fotg210_endpoint_reset(struct usb_hcd *hcd,
  4699. struct usb_host_endpoint *ep)
  4700. {
  4701. struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  4702. struct fotg210_qh *qh;
  4703. int eptype = usb_endpoint_type(&ep->desc);
  4704. int epnum = usb_endpoint_num(&ep->desc);
  4705. int is_out = usb_endpoint_dir_out(&ep->desc);
  4706. unsigned long flags;
  4707. if (eptype != USB_ENDPOINT_XFER_BULK && eptype != USB_ENDPOINT_XFER_INT)
  4708. return;
  4709. spin_lock_irqsave(&fotg210->lock, flags);
  4710. qh = ep->hcpriv;
  4711. /* For Bulk and Interrupt endpoints we maintain the toggle state
  4712. * in the hardware; the toggle bits in udev aren't used at all.
  4713. * When an endpoint is reset by usb_clear_halt() we must reset
  4714. * the toggle bit in the QH.
  4715. */
  4716. if (qh) {
  4717. usb_settoggle(qh->dev, epnum, is_out, 0);
  4718. if (!list_empty(&qh->qtd_list)) {
  4719. WARN_ONCE(1, "clear_halt for a busy endpoint\n");
  4720. } else if (qh->qh_state == QH_STATE_LINKED ||
  4721. qh->qh_state == QH_STATE_COMPLETING) {
  4722. /* The toggle value in the QH can't be updated
  4723. * while the QH is active. Unlink it now;
  4724. * re-linking will call qh_refresh().
  4725. */
  4726. if (eptype == USB_ENDPOINT_XFER_BULK)
  4727. start_unlink_async(fotg210, qh);
  4728. else
  4729. start_unlink_intr(fotg210, qh);
  4730. }
  4731. }
  4732. spin_unlock_irqrestore(&fotg210->lock, flags);
  4733. }
  4734. static int fotg210_get_frame(struct usb_hcd *hcd)
  4735. {
  4736. struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  4737. return (fotg210_read_frame_index(fotg210) >> 3) %
  4738. fotg210->periodic_size;
  4739. }
  4740. /* The EHCI in ChipIdea HDRC cannot be a separate module or device,
  4741. * because its registers (and irq) are shared between host/gadget/otg
  4742. * functions and in order to facilitate role switching we cannot
  4743. * give the fotg210 driver exclusive access to those.
  4744. */
  4745. MODULE_DESCRIPTION(DRIVER_DESC);
  4746. MODULE_AUTHOR(DRIVER_AUTHOR);
  4747. MODULE_LICENSE("GPL");
  4748. static const struct hc_driver fotg210_fotg210_hc_driver = {
  4749. .description = hcd_name,
  4750. .product_desc = "Faraday USB2.0 Host Controller",
  4751. .hcd_priv_size = sizeof(struct fotg210_hcd),
  4752. /*
  4753. * generic hardware linkage
  4754. */
  4755. .irq = fotg210_irq,
  4756. .flags = HCD_MEMORY | HCD_USB2,
  4757. /*
  4758. * basic lifecycle operations
  4759. */
  4760. .reset = hcd_fotg210_init,
  4761. .start = fotg210_run,
  4762. .stop = fotg210_stop,
  4763. .shutdown = fotg210_shutdown,
  4764. /*
  4765. * managing i/o requests and associated device resources
  4766. */
  4767. .urb_enqueue = fotg210_urb_enqueue,
  4768. .urb_dequeue = fotg210_urb_dequeue,
  4769. .endpoint_disable = fotg210_endpoint_disable,
  4770. .endpoint_reset = fotg210_endpoint_reset,
  4771. /*
  4772. * scheduling support
  4773. */
  4774. .get_frame_number = fotg210_get_frame,
  4775. /*
  4776. * root hub support
  4777. */
  4778. .hub_status_data = fotg210_hub_status_data,
  4779. .hub_control = fotg210_hub_control,
  4780. .bus_suspend = fotg210_bus_suspend,
  4781. .bus_resume = fotg210_bus_resume,
  4782. .relinquish_port = fotg210_relinquish_port,
  4783. .port_handed_over = fotg210_port_handed_over,
  4784. .clear_tt_buffer_complete = fotg210_clear_tt_buffer_complete,
  4785. };
  4786. static void fotg210_init(struct fotg210_hcd *fotg210)
  4787. {
  4788. u32 value;
  4789. iowrite32(GMIR_MDEV_INT | GMIR_MOTG_INT | GMIR_INT_POLARITY,
  4790. &fotg210->regs->gmir);
  4791. value = ioread32(&fotg210->regs->otgcsr);
  4792. value &= ~OTGCSR_A_BUS_DROP;
  4793. value |= OTGCSR_A_BUS_REQ;
  4794. iowrite32(value, &fotg210->regs->otgcsr);
  4795. }
  4796. /**
  4797. * fotg210_hcd_probe - initialize faraday FOTG210 HCDs
  4798. *
  4799. * Allocates basic resources for this USB host controller, and
  4800. * then invokes the start() method for the HCD associated with it
  4801. * through the hotplug entry's driver_data.
  4802. */
  4803. static int fotg210_hcd_probe(struct platform_device *pdev)
  4804. {
  4805. struct device *dev = &pdev->dev;
  4806. struct usb_hcd *hcd;
  4807. struct resource *res;
  4808. int irq;
  4809. int retval = -ENODEV;
  4810. struct fotg210_hcd *fotg210;
  4811. if (usb_disabled())
  4812. return -ENODEV;
  4813. pdev->dev.power.power_state = PMSG_ON;
  4814. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  4815. if (!res) {
  4816. dev_err(dev, "Found HC with no IRQ. Check %s setup!\n",
  4817. dev_name(dev));
  4818. return -ENODEV;
  4819. }
  4820. irq = res->start;
  4821. hcd = usb_create_hcd(&fotg210_fotg210_hc_driver, dev,
  4822. dev_name(dev));
  4823. if (!hcd) {
  4824. dev_err(dev, "failed to create hcd with err %d\n", retval);
  4825. retval = -ENOMEM;
  4826. goto fail_create_hcd;
  4827. }
  4828. hcd->has_tt = 1;
  4829. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  4830. hcd->regs = devm_ioremap_resource(&pdev->dev, res);
  4831. if (IS_ERR(hcd->regs)) {
  4832. retval = PTR_ERR(hcd->regs);
  4833. goto failed;
  4834. }
  4835. hcd->rsrc_start = res->start;
  4836. hcd->rsrc_len = resource_size(res);
  4837. fotg210 = hcd_to_fotg210(hcd);
  4838. fotg210->caps = hcd->regs;
  4839. retval = fotg210_setup(hcd);
  4840. if (retval)
  4841. goto failed;
  4842. fotg210_init(fotg210);
  4843. retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
  4844. if (retval) {
  4845. dev_err(dev, "failed to add hcd with err %d\n", retval);
  4846. goto failed;
  4847. }
  4848. device_wakeup_enable(hcd->self.controller);
  4849. return retval;
  4850. failed:
  4851. usb_put_hcd(hcd);
  4852. fail_create_hcd:
  4853. dev_err(dev, "init %s fail, %d\n", dev_name(dev), retval);
  4854. return retval;
  4855. }
  4856. /**
  4857. * fotg210_hcd_remove - shutdown processing for EHCI HCDs
  4858. * @dev: USB Host Controller being removed
  4859. *
  4860. */
  4861. static int fotg210_hcd_remove(struct platform_device *pdev)
  4862. {
  4863. struct device *dev = &pdev->dev;
  4864. struct usb_hcd *hcd = dev_get_drvdata(dev);
  4865. if (!hcd)
  4866. return 0;
  4867. usb_remove_hcd(hcd);
  4868. usb_put_hcd(hcd);
  4869. return 0;
  4870. }
  4871. static struct platform_driver fotg210_hcd_driver = {
  4872. .driver = {
  4873. .name = "fotg210-hcd",
  4874. },
  4875. .probe = fotg210_hcd_probe,
  4876. .remove = fotg210_hcd_remove,
  4877. };
  4878. static int __init fotg210_hcd_init(void)
  4879. {
  4880. int retval = 0;
  4881. if (usb_disabled())
  4882. return -ENODEV;
  4883. pr_info("%s: " DRIVER_DESC "\n", hcd_name);
  4884. set_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
  4885. if (test_bit(USB_UHCI_LOADED, &usb_hcds_loaded) ||
  4886. test_bit(USB_OHCI_LOADED, &usb_hcds_loaded))
  4887. pr_warn("Warning! fotg210_hcd should always be loaded before uhci_hcd and ohci_hcd, not after\n");
  4888. pr_debug("%s: block sizes: qh %Zd qtd %Zd itd %Zd\n",
  4889. hcd_name, sizeof(struct fotg210_qh),
  4890. sizeof(struct fotg210_qtd),
  4891. sizeof(struct fotg210_itd));
  4892. fotg210_debug_root = debugfs_create_dir("fotg210", usb_debug_root);
  4893. if (!fotg210_debug_root) {
  4894. retval = -ENOENT;
  4895. goto err_debug;
  4896. }
  4897. retval = platform_driver_register(&fotg210_hcd_driver);
  4898. if (retval < 0)
  4899. goto clean;
  4900. return retval;
  4901. clean:
  4902. debugfs_remove(fotg210_debug_root);
  4903. fotg210_debug_root = NULL;
  4904. err_debug:
  4905. clear_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
  4906. return retval;
  4907. }
  4908. module_init(fotg210_hcd_init);
  4909. static void __exit fotg210_hcd_cleanup(void)
  4910. {
  4911. platform_driver_unregister(&fotg210_hcd_driver);
  4912. debugfs_remove(fotg210_debug_root);
  4913. clear_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
  4914. }
  4915. module_exit(fotg210_hcd_cleanup);