fotg210-hcd.c 158 KB

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